src/BDSEventAction.cc

00001 /* BDSIM code.    Version 1.0
00002    Author: Grahame A. Blair, Royal Holloway, Univ. of London.
00003    Last modified 24.7.2002
00004    Copyright (c) 2002 by G.A.Blair.  ALL RIGHTS RESERVED. 
00005 
00006    Modified 22.03.05 by J.C.Carter, Royal Holloway, Univ. of London.
00007    Removed StringFromInt function - using BDSGlobalConstants::Instance() version
00008    Added/Changed Sampler code for Plane Sampler or Cylinder Sampler (GABs Code)
00009 */
00010 
00011 
00012 //======================================================
00013 //======================================================
00014 #include "BDSExecOptions.hh"
00015 #include "BDSGlobalConstants.hh" 
00016 #include "BDSDebug.hh"
00017 #include "BDSEventAction.hh"
00018 
00019 #include <ctime> 
00020 #include <list>
00021 #include <map>
00022 #include <vector>
00023 #include <algorithm>
00024 
00025 #include "G4Event.hh"
00026 #include "G4EventManager.hh"
00027 #include "G4Run.hh"
00028 #include "G4RunManager.hh"
00029 #include "G4StackManager.hh"
00030 #include "G4HCofThisEvent.hh"
00031 #include "G4VHitsCollection.hh"
00032 #include "G4TrajectoryContainer.hh"
00033 #include "G4Trajectory.hh"
00034 #include "G4VVisManager.hh"
00035 #include "G4SDManager.hh"
00036 #include "G4UImanager.hh"
00037 #include "G4ios.hh"
00038 #include "G4UnitsTable.hh"
00039 #include "Randomize.hh"
00040 
00041 #include "G4ChordFinder.hh"
00042 
00043 #include "BDSSampler.hh"
00044 #include "BDSSamplerHit.hh"
00045 #include "BDSEnergyCounterHit.hh"
00046 
00047 #include "BDSLWCalorimeter.hh"
00048 #include "BDSLWCalorimeterHit.hh"
00049 
00050 #include "BDSSynchrotronRadiation.hh"
00051 
00052 #include "BDSAcceleratorComponent.hh"
00053 
00054 #include "BDSOutput.hh"
00055 
00056 #include "BDSRunManager.hh"
00057 
00058 typedef std::map<G4String,int> LogVolCountMap;
00059 extern LogVolCountMap* LogVolCount;
00060 
00061 typedef std::list<BDSEnergyCounterSD*>  ECList;
00062 extern ECList* theECList;
00063 
00064 extern G4double BDSeBremFireDist;
00065 extern G4double BDSeBremZMin,BDSeBremZMax;
00066 
00067 // link to G4eBremsstrahlung.cc and G4GammaConversion.cc for muon studies
00068 extern G4bool BDSeBremsFiredThisEvent;
00069 extern G4double BDSeBremFireDist;
00070 
00071 G4double htot;
00072 G4int event_number;
00073 G4bool FireLaserCompton;
00074 
00075 extern BDSOutput* bdsOutput;
00076 
00077 
00078 //======================================================
00079 
00080 BDSEventAction::BDSEventAction():
00081   SamplerCollID_plane(-1),SamplerCollID_cylin(-1),
00082   LWCalorimeterCollID(-1),drawFlag("all"),
00083   Traj(NULL),trajEndPoint(NULL)
00084 { 
00085   verbose            = BDSExecOptions::Instance()->GetVerbose();
00086   verboseStep        = BDSExecOptions::Instance()->GetVerboseStep();
00087   verboseEvent       = BDSExecOptions::Instance()->GetVerboseEvent();
00088   verboseEventNumber = BDSExecOptions::Instance()->GetVerboseEventNumber();
00089   isBatch            = BDSExecOptions::Instance()->GetBatch();
00090   nptwiss            = BDSExecOptions::Instance()->GetNPTwiss();
00091 
00092   if(isBatch) printModulo=10;
00093   else printModulo=1;
00094   
00095   itsOutputFileNumber=1;
00096   
00097   itsRecordSize=1024;
00098     
00099   LastComp=NULL;
00100 }
00101 
00102 //======================================================
00103 
00104 BDSEventAction::~BDSEventAction()
00105 {
00106 //   delete Traj;
00107 //   delete trajEndPoint;
00108 }
00109 
00110 //======================================================
00111 
00112 void BDSEventAction::BeginOfEventAction(const G4Event* evt)
00113 { 
00114   G4cout<<"BDSEventAction::BeginOfEventAction>"<<G4endl;
00115 #ifdef DEBUG
00116   G4cout<<"BDSEventAction : processing begin of event action"<<G4endl;
00117 #endif
00118 
00119   event_number = evt->GetEventID();
00120   htot=0.;
00121 
00122   
00123    if(BDSGlobalConstants::Instance()->DoTwiss())
00124      {
00125        if(event_number==0) {
00126          if(!BDSGlobalConstants::Instance()->GetSynchRescale()) G4cout << "\n---> Calculating Twiss Parameters"<<G4endl;
00127          if(BDSGlobalConstants::Instance()->GetSynchRescale()) G4cout<<"\n---> Calculating Twiss Parameters and Rescaling magnets" <<G4endl;
00128        }
00129      }
00130    else
00131      {
00132        if (BDSGlobalConstants::Instance()->isReference==false && (event_number+1)%printModulo ==0)
00133          {
00134            G4cout << "\n---> Begin of event: " << event_number ;
00135            G4cout << G4endl;
00136          }
00137      }
00138    
00139    if(verboseEvent) G4cout<<"Begin of event: "<<event_number<<G4endl ;
00140    
00141    
00142    G4SDManager * SDman = G4SDManager::GetSDMpointer();
00143    if( BDSSampler::GetNSamplers() > 0)
00144      {   
00145        SamplerCollID_plane = SDman->GetCollectionID("Sampler_plane");
00146      }
00147    
00148    if( BDSSamplerCylinder::GetNSamplers() > 0 )
00149      {   
00150        SamplerCollID_cylin = SDman->GetCollectionID("Sampler_cylinder"); 
00151      }
00152    
00153    //if( bdsOutput->GetLWCalorimeterNumber() > 0 )
00154    {
00155      //if (LWCalorimeterCollID==-1) 
00156      //LWCalorimeterCollID = SDman->GetCollectionID("LWCalorimeterCollection");
00157    }
00158    FireLaserCompton=true;
00159    
00160 #ifdef DEBUG
00161    G4cout<<"BDSEventAction : begin of event action done"<<G4endl;
00162 #endif
00163 }
00164 
00165 //======================================================
00166 
00167 void BDSEventAction::EndOfEventAction(const G4Event* evt)
00168 {
00169 #ifdef DEBUG
00170   G4cout<<"BDSEventAction : processing end of event action"<<G4endl;
00171 #endif
00172   
00173   if(BDSGlobalConstants::Instance()->DoTwiss())
00174     {
00175       if(event_number==nptwiss-1)
00176         {
00177           G4cout << "\n---> Done" <<G4endl;
00178           G4EventManager::GetEventManager()->GetStackManager()->clear();
00179         }
00180     }
00181 
00182  
00183   if(verboseEvent || verboseEventNumber == event_number)
00184     G4cout<<"processing end of event"<<G4endl;
00185 
00186   G4SDManager * SDman = G4SDManager::GetSDMpointer();
00187 
00188   G4HCofThisEvent* HCE = evt->GetHCofThisEvent();
00189   
00190   BDSSamplerHitsCollection*  SampHC=NULL;
00191   //BDSLWCalorimeterHitsCollection* LWCalHC=NULL;
00192   BDSEnergyCounterHitsCollection* BDSEnergyCounter_HC=NULL;
00193 
00194 #ifdef DEBUG 
00195   G4cout<<"BDSEventAction : storing hits"<<G4endl;
00196 #endif
00197 
00198   // are there any planar samplers?
00199   // if so, record the hits for each sampler 
00200 
00201 #ifdef DEBUG 
00202   G4cout<<"BDSEventAction : processing planar hits collection"<<G4endl;
00203 #endif
00204   
00205   if(SamplerCollID_plane>=0)
00206     SampHC = (BDSSamplerHitsCollection*)(HCE->GetHC(SamplerCollID_plane));
00207   
00208   if(SampHC){
00209 #ifdef DEBUG
00210     G4cout << __METHOD_NAME__ << " - planar hits collection found. Writing hits." << G4endl;
00211 #endif
00212     bdsOutput->WriteHits(SampHC);
00213   } else {
00214 #ifdef DEBUG
00215     G4cout << __METHOD_NAME__ << " - no planar hits collection found. Not writing hits." << G4endl;
00216 #endif
00217   }
00218   SampHC=NULL;
00219   
00220   // are there any cylindrical samplers?
00221   // if so, record the hits
00222 
00223 #ifdef DEBUG
00224 G4cout<<"BDSEventAction : processing cylinder hits collection"<<G4endl;
00225 #endif
00226 
00227   if(SamplerCollID_cylin>=0)
00228     SampHC = (BDSSamplerHitsCollection*)(HCE->GetHC(SamplerCollID_cylin));
00229 
00230   if (SampHC)  bdsOutput->WriteHits(SampHC);
00231   
00232   // are there any Laser wire calorimeters?
00233   // TODO : check it !!! at present not writing LW stuff
00234   // remember to uncomment LWCalHC above if using this
00235 
00236   // #ifdef DEBUG 
00237   //  G4cout<<"BDSEventAction : processing laserwire calorimeter hits collection"<<G4endl;
00238   //#endif
00239   // if(LWCalorimeterCollID>=0) 
00240   //   LWCalHC=(BDSLWCalorimeterHitsCollection*)(HCE->GetHC(LWCalorimeterCollID));
00241 
00242   // if (LWCalHC) bdsOutput->WriteHits(SampHC);
00243 
00244 
00245   // create energy loss histogram
00246 
00247 #ifdef DEBUG 
00248   G4cout<<"BDSEventAction : storing energy loss histograms"<<G4endl;
00249 #endif
00250   
00251 #if 1
00252   for(iEC=theECList->begin();iEC!=theECList->end();iEC++)
00253     {
00254       G4String name=(*iEC)->GetCollectionName(0);
00255       
00256       G4int BDSEnergyCounter_ID= SDman->GetCollectionID(name);
00257       
00258       if(BDSEnergyCounter_ID>=0)
00259         {
00260           BDSEnergyCounter_HC=
00261             (BDSEnergyCounterHitsCollection*)(HCE->GetHC(BDSEnergyCounter_ID));
00262         
00263           if(BDSEnergyCounter_HC) {
00264             bdsOutput->WriteEnergyLoss(BDSEnergyCounter_HC);
00265           }
00266         }
00267     }
00268   G4cout << __METHOD_NAME__ << " finished writing energy loss." << G4endl;
00269 #endif
00270   
00271   // if events per ntuples not set (default 0) - only write out at end 
00272   G4cout << __METHOD_NAME__ << " getting number of events per ntuple..." << G4endl;
00273   int evntsPerNtuple = BDSGlobalConstants::Instance()->GetNumberOfEventsPerNtuple();
00274   G4cout << __METHOD_NAME__ << " finished getting number of events per ntuple." << G4endl;
00275   if( (evntsPerNtuple>0 && (event_number+1)%evntsPerNtuple == 0) || 
00276       (event_number+1) == BDSGlobalConstants::Instance()->GetNumberToGenerate())
00277     {
00278       G4cout << __METHOD_NAME__ << " writing out events." << G4endl;
00279 #ifdef DEBUG 
00280       G4cout<<"writing to file "<<G4endl;
00281 #endif
00282       // notify the output about the event end
00283       // this can be used for splitting output files etc.
00284       //        bdsOutput->Commit(itsOutputFileNumber++);
00285       if((event_number+1) == BDSGlobalConstants::Instance()->GetNumberToGenerate()) {
00286         bdsOutput->Write(); // write last file
00287       } else {
00288         bdsOutput->Commit(); // write and open new file
00289       }
00290 #ifdef DEBUG
00291       G4cout<<"done"<<G4endl;
00292 #endif
00293     }
00294   
00295   // Save interesting trajectories
00296   
00297   G4TrajectoryContainer* TrajCont=evt->GetTrajectoryContainer();
00298 
00299   if(!TrajCont) return;
00300   
00301   TrajectoryVector* TrajVec=TrajCont->GetVector();
00302   TrajectoryVector::iterator iT1;
00303 
00304   
00305   if(BDSGlobalConstants::Instance()->GetStoreTrajectory() ||
00306      BDSGlobalConstants::Instance()->GetStoreMuonTrajectories() ||
00307      BDSGlobalConstants::Instance()->GetStoreNeutronTrajectories()){
00308 #ifdef DEBUG
00309   G4cout<<"BDSEventAction : storing trajectories"<<G4endl;
00310 #endif
00311     // clear out trajectories that don't reach point x
00312     for(iT1=TrajVec->begin();iT1<TrajVec->end();iT1++){
00313       this->Traj=(G4VTrajectory*)(*iT1);
00314       this->trajEndPoint = this->Traj->GetPoint((int)Traj->GetPointEntries()-1);
00315       this->trajEndPointThreeVector = this->trajEndPoint->GetPosition();
00316       if(trajEndPointThreeVector.z()/1000.0>BDSGlobalConstants::Instance()->GetTrajCutGTZ()  && 
00317          (sqrt(pow(trajEndPointThreeVector.x()/1000.0,2) + pow(trajEndPointThreeVector.y()/1000.0,2))<BDSGlobalConstants::Instance()->GetTrajCutLTR())
00318          ){ 
00319         this->interestingTrajectories.push_back(Traj);
00320       }
00321       
00322     }
00323     //Output interesting trajectories
00324     if(interestingTrajectories.size()>0){
00325       bdsOutput->WriteTrajectory(interestingTrajectories);
00326       interestingTrajectories.clear();
00327     }
00328   }
00329 
00330   // needed to draw trajectories and hits:
00331   if(!isBatch) {
00332 #ifdef DEBUG 
00333     G4cout<<"BDSEventAction : drawing"<<G4endl;
00334 #endif
00335     evt->Draw();
00336   }
00337   
00338   //clear out the remaining trajectories
00339 #ifdef DEBUG 
00340   G4cout<<"BDSEventAction : deleting trajectories"<<G4endl;
00341 #endif
00342   TrajCont->clearAndDestroy();
00343 #ifdef DEBUG 
00344  G4cout<<"BDSEventAction : end of event action done"<<G4endl;
00345 #endif
00346   }
00347 
00348 //======================================================

Generated on 27 Aug 2013 for BDSIM by  doxygen 1.4.7