/home/cern/BDSIM_new/src/BDSOutput.cc

00001 #include "BDSOutput.hh"
00002 #include "BDSSamplerSD.hh"
00003 #include <ctime>
00004 
00005 extern G4String outputFilename;
00006 BDSOutput::BDSOutput()
00007 {
00008   format = _ASCII; // default - write an ascii file
00009 }
00010 
00011 BDSOutput::BDSOutput(int fmt)
00012 {
00013   format = fmt;
00014 }
00015 
00016 BDSOutput::~BDSOutput()
00017 {
00018   if(of != NULL)
00019     of.close();
00020 
00021 
00022 #ifdef USE_ROOT
00023   if(format==_ROOT)
00024     if(theRootOutputFile->IsOpen())
00025       {
00026         theRootOutputFile->Write();
00027         theRootOutputFile->Close();
00028         delete theRootOutputFile;
00029       }
00030 #endif
00031 }
00032 
00033 
00034 void BDSOutput::SetFormat(G4int val)
00035 {
00036   format = val;
00037   time_t tm = time(NULL);
00038 
00039   if( format == _ASCII)
00040     {
00041       G4String filename = outputFilename+".txt";
00042       G4cout<<"output format ASCII, filename: "<<filename<<G4endl;
00043       of.open(filename);
00044       of<<"### BDSIM output created "<<ctime(&tm)<<" ####"<<G4endl;
00045       of<<"# PT E[GeV] X[mum] Y[mum] Z[m] Xp[rad] Yp[rad]  "<<G4endl;
00046 
00047     }
00048   if( format == _ROOT)
00049     {
00050       G4cout<<"output format ROOT"<<G4endl;
00051     }
00052 }
00053 
00054 // output initialization (ROOT only)
00055 void BDSOutput::Init(G4int FileNum)
00056 {
00057   if(format != _ROOT) return;
00058 
00059 #ifdef USE_ROOT
00060   // set up the root file
00061   G4String filename = outputFilename + "_" 
00062     + BDSGlobals->StringFromInt(FileNum) + ".root";
00063   
00064   G4cout<<"Setting up new file: "<<filename<<G4endl;
00065   theRootOutputFile=new TFile(filename,"RECREATE", "BDS output file");
00066   
00067 
00068   //build sampler tree
00069   for(G4int i=0;i<BDSSampler::GetNSamplers();i++)
00070     {
00071       //G4String name="samp"+BDSGlobals->StringFromInt(i+1);
00072       G4String name=SampName[i];
00073       TTree* SamplerTree = new TTree(name, "Sampler output");
00074       
00075       SamplerTree->Branch("E0",&E0,"E0 (GeV)/F");
00076       SamplerTree->Branch("x0",&x0,"x0 (mum)/F");
00077       SamplerTree->Branch("y0",&y0,"y0 (mum)/F");
00078       SamplerTree->Branch("z0",&z0,"z0 (m)/F");
00079       SamplerTree->Branch("xp0",&xp0,"xp0 (rad)/F");
00080       SamplerTree->Branch("yp0",&yp0,"yp0 (rad)/F");
00081       SamplerTree->Branch("zp0",&zp0,"zp0 (rad)/F");
00082       SamplerTree->Branch("t0",&t0,"t0 (ns)/F");
00083 
00084       SamplerTree->Branch("E",&E,"E (GeV)/F");
00085       SamplerTree->Branch("x",&x,"x (mum)/F");
00086       SamplerTree->Branch("y",&y,"y (mum)/F");
00087       SamplerTree->Branch("z",&z,"z (m)/F");
00088       SamplerTree->Branch("xp",&xp,"xp (rad)/F");
00089       SamplerTree->Branch("yp",&yp,"yp (rad)/F");
00090       SamplerTree->Branch("zp",&zp,"zp (rad)/F");
00091       SamplerTree->Branch("t",&t,"t (ns)/F");
00092 
00093       SamplerTree->Branch("X",&X,"X (mum)/F");
00094       SamplerTree->Branch("Y",&Y,"Y (mum)/F");
00095       SamplerTree->Branch("Z",&Z,"Z (m)/F");
00096       SamplerTree->Branch("Xp",&Xp,"Xp (rad)/F");
00097       SamplerTree->Branch("Yp",&Yp,"Yp (rad)/F");
00098       SamplerTree->Branch("Zp",&Zp,"Zp (rad)/F");
00099 
00100       SamplerTree->Branch("s",&s,"s (m)/F");
00101 
00102       SamplerTree->Branch("weight",&weight,"weight/F");
00103       SamplerTree->Branch("partID",&part,"partID/I");
00104       SamplerTree->Branch("nEvent",&nev,"nEvent/I");
00105       SamplerTree->Branch("parentID",&pID,"parentID/I");
00106       SamplerTree->Branch("trackID",&track_id,"trackID/I");
00107     }
00108 
00109   if(BDSGlobals->GetStoreTrajectory() || BDSGlobals->GetStoreMuonTrajectories() || BDSGlobals->GetStoreNeutronTrajectories()) 
00110     // create a tree with trajectories
00111     {
00112       //G4cout<<"BDSOutput::storing trajectories set"<<G4endl;
00113       TTree* TrajTree = new TTree("Trajectories", "Trajectories");
00114       TrajTree->Branch("x",&x,"x/F");
00115       TrajTree->Branch("y",&y,"y/F");
00116       TrajTree->Branch("z",&z,"z/F");
00117       TrajTree->Branch("part",&part,"part/I");
00118     }
00119 
00120   // build energy loss histogram
00121   G4int nBins = G4int(zMax/m);
00122 
00123   EnergyLossHisto = new TH1F("ElossHisto", "Energy Loss",nBins,0.,zMax/m);
00124   EnergyLossNtuple= new TNtuple("ElossNtuple", "Energy Loss","z:E:partID:parentID");
00125 
00126 #endif
00127 }
00128 
00129 void BDSOutput::WriteHits(BDSSamplerHitsCollection *hc)
00130 {
00131   if( format == _ASCII) {
00132     
00133     //of<<"#hits (PDGtype  E[GeV],x[micron],y[micron],z[m],x'[rad],y'[rad]):"<<G4endl;
00134     
00135     G4cout.precision(6);
00136     
00137     for (G4int i=0; i<hc->entries(); i++)
00138       {
00139         of<<(*hc)[i]->GetPDGtype()
00140           <<" "
00141           <<(*hc)[i]->GetMom()/GeV
00142           <<" "
00143           <<(*hc)[i]->GetX()/micrometer
00144           <<" "
00145           <<(*hc)[i]->GetY()/micrometer
00146           <<" "
00147           <<(*hc)[i]->GetZ() / m
00148           <<" "
00149           <<(*hc)[i]->GetXPrime() / radian
00150           <<" "
00151           <<(*hc)[i]->GetYPrime() / radian
00152           <<G4endl;
00153       }
00154     
00155     of<<G4endl;
00156     //of<<"end of hits collection"<<G4endl;
00157   }
00158  
00159   if( format == _ROOT) {
00160 #ifdef USE_ROOT
00161     G4String name;
00162 
00163 
00164     for (G4int i=0; i<hc->entries(); i++)
00165      {
00166        
00167        //if ((*hc)[i]->GetType()=="plane") 
00168        //name="samp";
00169        //else if ((*hc)[i]->GetType()=="cylinder")
00170        //name ="cyln";
00171        //name="samp" + BDSGlobals->StringFromInt((*hc)[i]->GetNumber());
00172 
00173        TTree* sTree=(TTree*)gDirectory->Get((*hc)[i]->GetName());
00174        
00175        if(!sTree) G4Exception("BDSOutput: ROOT Sampler not found!");
00176 
00177        E0=(*hc)[i]->GetInitMom() / GeV;
00178        x0=(*hc)[i]->GetInitX() / micrometer;
00179        y0=(*hc)[i]->GetInitY() / micrometer;
00180        z0=(*hc)[i]->GetInitZ() / m;
00181        xp0=(*hc)[i]->GetInitXPrime() / radian;
00182        yp0=(*hc)[i]->GetInitYPrime() / radian;
00183        zp0=(*hc)[i]->GetInitZPrime() / radian;
00184        t0=(*hc)[i]->GetInitT() / ns;
00185 
00186        E=(*hc)[i]->GetMom() / GeV;
00187        x=(*hc)[i]->GetX() / micrometer;
00188        y=(*hc)[i]->GetY() / micrometer;
00189        z=(*hc)[i]->GetZ() / m;
00190        xp=(*hc)[i]->GetXPrime() / radian;
00191        yp=(*hc)[i]->GetYPrime() / radian;
00192        zp=(*hc)[i]->GetZPrime() / radian;
00193        t=(*hc)[i]->GetT() / ns;
00194 
00195        X=(*hc)[i]->GetGlobalX() / m;
00196        Y=(*hc)[i]->GetGlobalY() / m;
00197        Z=(*hc)[i]->GetGlobalZ() / m;
00198        Xp=(*hc)[i]->GetGlobalXPrime() / radian;
00199        Yp=(*hc)[i]->GetGlobalYPrime() / radian;
00200        Zp=(*hc)[i]->GetGlobalZPrime() / radian;
00201 
00202        s=(*hc)[i]->GetS() / m;
00203 
00204        weight=(*hc)[i]->GetWeight();
00205        part=(*hc)[i]->GetPDGtype(); 
00206        nev=(*hc)[i]->GetEventNo(); 
00207        pID=(*hc)[i]->GetParentID(); 
00208        track_id=(*hc)[i]->GetTrackID();
00209       
00210        sTree->Fill();
00211        
00212      }
00213 #endif
00214   }
00215  
00216 }
00217 
00218 // write a trajectory to a root/ascii file
00219 G4int BDSOutput::WriteTrajectory(TrajectoryVector* TrajVec)
00220 {
00221 //  G4cout<<"a trajectory stored..."<<G4endl;
00222   
00223   G4int tID;
00224   G4TrajectoryPoint* TrajPoint;
00225   G4ThreeVector TrajPos;
00226 
00227 #ifdef USE_ROOT
00228   TTree* TrajTree;
00229     
00230   G4String name = "Trajectories";
00231       
00232   TrajTree=(TTree*)gDirectory->Get(name);
00233 
00234   if(TrajTree == NULL) { G4cerr<<"TrajTree=NULL"<<G4endl; return -1;}
00235 #endif
00236 
00237 
00238   if(TrajVec)
00239     {
00240       TrajectoryVector::iterator iT;
00241       
00242       for(iT=TrajVec->begin();iT<TrajVec->end();iT++)
00243         {
00244           G4Trajectory* Traj=(G4Trajectory*)(*iT);
00245           
00246           tID=Traj->GetTrackID();             
00247           part = Traj->GetPDGEncoding();
00248           
00249           for(G4int j=0; j<Traj->GetPointEntries(); j++)
00250             {
00251               TrajPoint=(G4TrajectoryPoint*)Traj->GetPoint(j);
00252               TrajPos=TrajPoint->GetPosition();
00253               
00254               x = TrajPos.x() / m;
00255               y = TrajPos.y() / m;
00256               z = TrajPos.z() / m;
00257 
00258 #ifdef USE_ROOT
00259               if( format == _ROOT) 
00260                 TrajTree->Fill();
00261 #endif
00262 
00263               //G4cout<<"TrajPos="<<TrajPos<<G4endl;
00264             }
00265         }
00266     }
00267   
00268   return 0;
00269 }
00270 
00271 // make energy loss histo
00272 
00273 void BDSOutput::WriteEnergyLoss(BDSEnergyCounterHitsCollection* hc)
00274 {
00275 
00276   if( format == _ROOT) {
00277 #ifdef USE_ROOT
00278   
00279     G4int n_hit = hc->entries();
00280     
00281     for (G4int i=0;i<n_hit;i++)
00282       {
00283         G4double Energy=(*hc)[i]->GetEnergy();
00284         G4double EWeightZ=(*hc)[i]->
00285           GetEnergyWeightedPosition()/Energy;
00286         G4int partID = (*hc)[i]->GetPartID();
00287         G4int parentID = (*hc)[i]->GetParentID();
00288         EnergyLossHisto->Fill(EWeightZ/m,Energy/GeV);
00289 
00290         EnergyLossNtuple->Fill(EWeightZ/m,Energy/GeV,partID,parentID);
00291 
00292       }
00293 #endif
00294   }
00295 
00296  if( format == _ASCII) {
00297   
00298     G4int n_hit = hc->entries();
00299     
00300     for (G4int i=0;i<n_hit;i++)
00301       {
00302         G4double Energy=(*hc)[i]->GetEnergy();
00303         G4double EWeightZ=(*hc)[i]->
00304           GetEnergyWeightedPosition()/Energy;
00305         G4int partID = (*hc)[i]->GetPartID();
00306         G4int parentID = (*hc)[i]->GetParentID();
00307         
00308         of<<EWeightZ/m<<"  "<<Energy/GeV<<"  "<<partID<<"  "<<parentID<<G4endl;
00309 
00310       }
00311 
00312   }
00313 
00314 }
00315 
00316 
00317 
00318 // write some comments to the output file
00319 // only for ASCII output
00320 void BDSOutput::Echo(G4String str)
00321 {
00322   if(format == _ASCII)  of<<"#"<<str<<G4endl;
00323   else // default
00324     G4cout<<"#"<<str<<G4endl;
00325 }
00326 
00327 G4int BDSOutput::Commit(G4int FileNum)
00328 {
00329 #ifdef USE_ROOT
00330   if(format==_ROOT)
00331     if(theRootOutputFile->IsOpen())
00332       {
00333         theRootOutputFile->Write();
00334         theRootOutputFile->Close();
00335         delete theRootOutputFile;
00336       }
00337 #endif
00338 
00339   Init(FileNum);
00340   return 0;
00341 }
00342 

Generated on Wed Mar 5 17:25:22 2008 for BDSIM by  doxygen 1.5.3