/scratch0/jsnuveri/BDSIM/BDSIMgit/bdsim/src/BDSEventAction.cc

00001 #include "BDSExecOptions.hh"
00002 #include "BDSGlobalConstants.hh" 
00003 #include "BDSDebug.hh"
00004 #include "BDSEventAction.hh"
00005 
00006 #include <list>
00007 #include <map>
00008 #include <vector>
00009 #include <algorithm>
00010 
00011 #include "BDSAnalysisManager.hh"
00012 #include "BDSEnergyCounterHit.hh"
00013 #include "BDSOutputBase.hh" 
00014 #include "BDSRunManager.hh"
00015 #include "BDSSamplerHit.hh"
00016 #include "BDSTrajectory.hh"
00017 
00018 #include "G4Event.hh"
00019 #include "G4EventManager.hh"
00020 #include "G4Run.hh"
00021 #include "G4HCofThisEvent.hh"
00022 #include "G4TrajectoryContainer.hh"
00023 #include "G4Trajectory.hh"
00024 #include "G4SDManager.hh"
00025 #include "G4ios.hh"
00026 #include "Randomize.hh"
00027 #include "G4PrimaryVertex.hh"
00028 #include "G4PrimaryParticle.hh"
00029 
00030 extern BDSOutputBase* bdsOutput;         // output interface
00031 
00032 G4bool FireLaserCompton;  // bool to ensure that Laserwire can only occur once in an event
00033 
00034 BDSEventAction::BDSEventAction():
00035   analMan(NULL),
00036   samplerCollID_plane(-1),
00037   samplerCollID_cylin(-1),
00038   energyCounterCollID(-1),
00039   primaryCounterCollID(-1),
00040   Traj(NULL),
00041   trajEndPoint(NULL)
00042 { 
00043   verbose            = BDSExecOptions::Instance()->GetVerbose();
00044   verboseEvent       = BDSExecOptions::Instance()->GetVerboseEvent();
00045   verboseEventNumber = BDSExecOptions::Instance()->GetVerboseEventNumber();
00046   isBatch            = BDSExecOptions::Instance()->GetBatch();
00047 
00048   if(isBatch) printModulo=10;
00049   else printModulo=1;
00050 }
00051 
00052 BDSEventAction::~BDSEventAction()
00053 {}
00054 
00055 void BDSEventAction::BeginOfEventAction(const G4Event* evt)
00056 { 
00057 #ifdef BDSDEBUG
00058   G4cout << __METHOD_NAME__ << G4endl;
00059   G4cout << __METHOD_NAME__ << " Processing begin of event action" << G4endl;
00060 #endif
00061   // get pointer to analysis manager
00062   analMan = BDSAnalysisManager::Instance();
00063 
00064   // even number feedback
00065   G4int event_number = evt->GetEventID();
00066   if ((event_number+1)%printModulo == 0)
00067     {G4cout << "\n---> Begin of event: " << event_number << G4endl;}
00068   if(verboseEvent) G4cout << __METHOD_NAME__ << "event #"<<event_number<<G4endl ;
00069 
00070   // get hit collection IDs for easy access
00071   G4SDManager* g4SDMan = G4SDManager::GetSDMpointer();
00072   if(samplerCollID_plane < 0)
00073     {samplerCollID_plane    = g4SDMan->GetCollectionID("Sampler_plane");}
00074   if(samplerCollID_cylin < 0)
00075     {samplerCollID_cylin    = g4SDMan->GetCollectionID("Sampler_cylinder");}
00076   if(energyCounterCollID < 0)
00077     {energyCounterCollID  = g4SDMan->GetCollectionID("ec_on_axis_read_out/energy_counter");}
00078   if(primaryCounterCollID < 0)
00079     {primaryCounterCollID = g4SDMan->GetCollectionID("ec_on_axis_read_out/primary_counter");}
00080    
00081   //if (lWCalorimeterCollID<1) 
00082   //{lWCalorimeterCollID = G4SDManager::GetSDMpointer()->GetCollectionID("LWCalorimeterCollection");}
00083   FireLaserCompton=true;
00084    
00085 #ifdef BDSDEBUG
00086   G4cout << __METHOD_NAME__ << "begin of event action done"<<G4endl;
00087 #endif
00088 }
00089 
00090 void BDSEventAction::EndOfEventAction(const G4Event* evt)
00091 {
00092 #ifdef BDSDEBUG
00093   G4cout<<"BDSEventAction : processing end of event action"<<G4endl;
00094 #endif
00095   G4HCofThisEvent* HCE = evt->GetHCofThisEvent();
00096 
00097   G4int event_number = evt->GetEventID();
00098   if(verboseEvent || verboseEventNumber == event_number){
00099     G4cout << __METHOD_NAME__ << " processing end of event"<<G4endl;
00100   }
00101  
00102 #ifdef BDSDEBUG 
00103   G4cout<<"BDSEventAction : storing hits"<<G4endl;
00104 #endif 
00105   //Record the primary events
00106   AddPrimaryHits();
00107   
00108 #ifdef BDSDEBUG 
00109   G4cout<<"BDSEventAction : processing planar hits collection"<<G4endl;
00110 #endif
00111   
00112   BDSSamplerHitsCollection*  SampHC=NULL;
00113   if(samplerCollID_plane>=0)
00114     SampHC = (BDSSamplerHitsCollection*)(evt->GetHCofThisEvent()->GetHC(samplerCollID_plane));
00115   
00116   if(SampHC){
00117 #ifdef BDSDEBUG
00118     G4cout << __METHOD_NAME__ << " - planar hits collection found. Writing hits." << G4endl;
00119 #endif
00120     bdsOutput->WriteHits(SampHC);
00121   } else {
00122 #ifdef BDSDEBUG
00123     G4cout << __METHOD_NAME__ << " - no planar hits collection found. Not writing hits." << G4endl;
00124 #endif
00125   }
00126   SampHC=NULL;
00127   
00128   // are there any cylindrical samplers? if so, record the hits
00129 #ifdef BDSDEBUG
00130   G4cout<<"BDSEventAction : processing cylinder hits collection"<<G4endl;
00131 #endif
00132   if(samplerCollID_cylin>=0)
00133     SampHC = (BDSSamplerHitsCollection*)(HCE->GetHC(samplerCollID_cylin));
00134 
00135   if(SampHC)
00136     {bdsOutput->WriteHits(SampHC);}
00137   
00138   // are there any Laser wire calorimeters?
00139   // TODO : check it !!! at present not writing LW stuff
00140   // remember to uncomment LWCalHC above if using this
00141   //BDSLWCalorimeterHitsCollection* LWCalHC=NULL;
00142   // if(LWCalorimeterCollID>=0) 
00143   //   LWCalHC=(BDSLWCalorimeterHitsCollection*)(evt->GetHCofThisEvent()->GetHC(LWCalorimeterCollID));
00144   // if (LWCalHC) bdsOutput->WriteHits(SampHC);
00145   
00146   // create energy loss histogram
00147 #ifdef BDSDEBUG 
00148   G4cout<<"BDSEventAction : storing energy loss histograms"<<G4endl;
00149 #endif
00150   
00151   BDSEnergyCounterHitsCollection* energyCounterHits = 
00152     (BDSEnergyCounterHitsCollection*)(HCE->GetHC(energyCounterCollID));
00153   BDSEnergyCounterHitsCollection* primaryCounterHits = 
00154     (BDSEnergyCounterHitsCollection*)(HCE->GetHC(primaryCounterCollID));
00155 
00156   BDSAnalysisManager* analMan = BDSAnalysisManager::Instance();
00157   //if we have energy deposition hits, write them
00158   if(energyCounterHits)
00159     {
00160       bdsOutput->WriteEnergyLoss(energyCounterHits); // write hits
00161       //bin hits in histograms
00162       for (G4int i = 0; i < energyCounterHits->entries(); i++)
00163         {
00164           //general eloss histo
00165           analMan->Fill1DHistogram(2,(*energyCounterHits)[i]->GetS()/CLHEP::m,(*energyCounterHits)[i]->GetEnergy()/CLHEP::GeV);
00166           //per element eloss histo
00167           analMan->Fill1DHistogram(5,(*energyCounterHits)[i]->GetS()/CLHEP::m,(*energyCounterHits)[i]->GetEnergy()/CLHEP::GeV);
00168         }
00169     }
00170 
00171   //if we have primary hits, find the first one and write that
00172   if(primaryCounterHits) {
00173     if (primaryCounterHits->entries()>0){
00174       BDSEnergyCounterHit* thePrimaryHit  = BDS::LowestSPosPrimaryHit(primaryCounterHits);
00175       BDSEnergyCounterHit* thePrimaryLoss = BDS::HighestSPosPrimaryHit(primaryCounterHits);
00176       //write
00177       if (thePrimaryHit && thePrimaryLoss)
00178         {
00179           bdsOutput->WritePrimaryLoss(thePrimaryLoss);
00180           bdsOutput->WritePrimaryHit(thePrimaryHit);
00181           // general histos
00182           analMan->Fill1DHistogram(0,thePrimaryHit->GetS()/CLHEP::m);
00183           analMan->Fill1DHistogram(1,thePrimaryLoss->GetS()/CLHEP::m);
00184           // per element histos
00185           analMan->Fill1DHistogram(3,thePrimaryHit->GetS()/CLHEP::m);
00186           analMan->Fill1DHistogram(4,thePrimaryLoss->GetS()/CLHEP::m);
00187         }
00188     }
00189   }
00190   
00191 #ifdef BDSDEBUG
00192   G4cout << __METHOD_NAME__ << " finished writing energy loss." << G4endl;
00193 #endif
00194   
00195   // if events per ntuples not set (default 0) - only write out at end 
00196   int evntsPerNtuple = BDSGlobalConstants::Instance()->GetNumberOfEventsPerNtuple();
00197 
00198   if (evntsPerNtuple>0 && (event_number+1)%evntsPerNtuple == 0)
00199     {
00200 #ifdef BDSDEBUG
00201       G4cout << __METHOD_NAME__ << " writing events." << G4endl;
00202 #endif      
00203       bdsOutput->Commit(); // write and open new file
00204 #ifdef BDSDEBUG
00205       G4cout<<"done"<<G4endl;
00206 #endif
00207     }
00208 
00209   // needed to draw trajectories and hits:
00210   if(!isBatch) {
00211 #ifdef BDSDEBUG 
00212     G4cout<<"BDSEventAction : drawing"<<G4endl;
00213 #endif
00214     evt->Draw();
00215   }
00216     
00217   // Save interesting trajectories
00218   
00219   if(BDSGlobalConstants::Instance()->GetStoreTrajectory() ||
00220      BDSGlobalConstants::Instance()->GetStoreMuonTrajectories() ||
00221      BDSGlobalConstants::Instance()->GetStoreNeutronTrajectories()){
00222 #ifdef BDSDEBUG
00223     G4cout<<"BDSEventAction : storing trajectories"<<G4endl;
00224 #endif
00225     G4TrajectoryContainer* TrajCont=evt->GetTrajectoryContainer();
00226     if(!TrajCont) return;
00227     TrajectoryVector* TrajVec=TrajCont->GetVector();
00228     TrajectoryVector::iterator iT1;
00229     // clear out trajectories that don't reach point x
00230     for(iT1=TrajVec->begin();iT1<TrajVec->end();iT1++){
00231       this->Traj=(BDSTrajectory*)(*iT1);
00232       this->trajEndPoint = (BDSTrajectoryPoint*)this->Traj->GetPoint((int)Traj->GetPointEntries()-1);
00233       this->trajEndPointThreeVector = this->trajEndPoint->GetPosition();
00234       if(trajEndPointThreeVector.z()/1000.0>BDSGlobalConstants::Instance()->GetTrajCutGTZ()  && 
00235          (sqrt(pow(trajEndPointThreeVector.x()/1000.0,2) + pow(trajEndPointThreeVector.y()/1000.0,2))<BDSGlobalConstants::Instance()->GetTrajCutLTR())
00236          ){ 
00237         this->interestingTrajectories.push_back(Traj);
00238       }
00239     }
00240     //Output interesting trajectories
00241     if(interestingTrajectories.size()>0){
00242       bdsOutput->WriteTrajectory(interestingTrajectories);
00243       interestingTrajectories.clear();
00244     }
00245   }
00246 
00247   //clear out the remaining trajectories
00248 #ifdef BDSDEBUG 
00249   //  G4cout<<"BDSEventAction : deleting trajectories"<<G4endl;
00250 #endif
00251   //  TrajCont->clearAndDestroy();
00252 #ifdef BDSDEBUG 
00253  G4cout<<"BDSEventAction : end of event action done"<<G4endl;
00254 #endif
00255 }
00256 
00257 void BDSEventAction::AddPrimaryHits(){
00258 #ifdef BDSDEBUG
00259   G4cout << __METHOD_NAME__ << G4endl;
00260 #endif
00261   //Save the primary particle as a hit 
00262   G4PrimaryVertex* primaryVertex= BDSRunManager::GetRunManager()->GetCurrentEvent()->GetPrimaryVertex();
00263   G4PrimaryParticle* primaryParticle=primaryVertex->GetPrimary();
00264   G4ThreeVector momDir = primaryParticle->GetMomentumDirection();
00265   G4double E = primaryParticle->GetTotalEnergy();
00266   G4double xp = momDir.x();
00267   G4double yp = momDir.y();
00268   G4double zp = momDir.z();
00269   G4double x0 = primaryVertex->GetX0();
00270   G4double y0 = primaryVertex->GetY0();
00271   G4double z0 = primaryVertex->GetZ0();
00272   G4double t = primaryVertex->GetT0();
00273   G4double weight = primaryParticle->GetWeight();
00274   G4int PDGType=primaryParticle->GetPDGcode();
00275   G4int nEvent = BDSRunManager::GetRunManager()->GetCurrentEvent()->GetEventID();
00276   G4String samplerName="primaries";
00277   G4int turnstaken = BDSGlobalConstants::Instance()->GetTurnsTaken();
00278   bdsOutput->WritePrimary(samplerName, E, x0, y0, z0, xp, yp, zp, t, weight, PDGType, nEvent, turnstaken);
00279 
00280 #ifdef BDSDEBUG
00281   G4cout << __METHOD_NAME__ << " finished" << G4endl;
00282 #endif
00283   
00284 }

Generated on 28 Jun 2015 for BDSIM by  doxygen 1.4.7