/home/cern/BDSIM_new/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 BDSGlobals version
00008    Added/Changed Sampler code for Plane Sampler or Cylinder Sampler (GABs Code)
00009 */
00010 
00011 
00012 
00013 const int DEBUG = 0;
00014 
00015 //======================================================
00016 //======================================================
00017 #include "BDSGlobalConstants.hh" // must be first in include list
00018 
00019 #include "BDSEventAction.hh"
00020 
00021 #include <ctime> 
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 "G4FieldManager.hh"
00042 #include "G4ChordFinder.hh"
00043 
00044 #include "BDSSampler.hh"
00045 #include "BDSSamplerHit.hh"
00046 #include "BDSEnergyCounterHit.hh"
00047 
00048 #include "BDSLWCalorimeter.hh"
00049 #include "BDSLWCalorimeterHit.hh"
00050 
00051 #include "BDSSynchrotronRadiation.hh"
00052 
00053 #include "BDSAcceleratorComponent.hh"
00054 
00055 #include "BDSOutput.hh"
00056 
00057 #include "BDSRunManager.hh"
00058 
00059 //typedef list<BDSAcceleratorComponent*>  BDSBeamline;
00060 //BDSBeamline theBeamline;
00061 
00062 //typedef std::vector<G4int> MuonTrackVector;
00063 //MuonTrackVector* theMuonTrackVector;
00064 
00065 typedef std::map<G4String,int> LogVolCountMap;
00066 extern LogVolCountMap* LogVolCount;
00067 
00068 typedef list<BDSEnergyCounterSD*>  ECList;
00069 extern ECList* theECList;
00070 
00071 extern G4double BDSeBremFireDist;
00072 extern G4double BDSeBremZMin,BDSeBremZMax;
00073 
00074 // link to G4eBremsstrahlung.cc and G4GammaConversion.cc for muon studies
00075 extern G4bool BDSeBremsFiredThisEvent;
00076 extern G4double BDSeBremFireDist;
00077 
00078 G4double htot;
00079 G4int event_number;
00080 G4bool FireLaserCompton;
00081 
00082 
00083 extern BDSOutput bdsOutput;
00084 extern G4String outputFilename;
00085 extern G4bool isBatch;
00086 
00087 
00088 extern G4bool verbose;      // run options
00089 extern G4bool verboseStep;
00090 extern G4bool verboseEvent;
00091 extern G4int verboseEventNumber;
00092 extern G4bool isBatch;
00093 
00094 extern int nptwiss;
00095 
00096 
00097 //======================================================
00098 
00099 BDSEventAction::BDSEventAction()
00100 :SamplerCollID_plane(-1),SamplerCollID_cylin(-1),
00101 LWCalorimeterCollID(-1),drawFlag("all")
00102 { 
00103   if(isBatch) printModulo=1000;
00104   else printModulo=1;
00105   
00106   itsOutputFileNumber=1;
00107   
00108   itsRecordSize=1024;
00109     
00110   LastComp=NULL;
00111 }
00112 
00113 //======================================================
00114 
00115 BDSEventAction::~BDSEventAction()
00116 {
00117 }
00118 
00119 //======================================================
00120 
00121 void BDSEventAction::BeginOfEventAction(const G4Event* evt)
00122 { 
00123   if(DEBUG) G4cout<<"BDSEventAction : processing begin of event action"<<G4endl;
00124 
00125   event_number = evt->GetEventID();
00126   htot=0.;
00127 
00128   
00129   //if(BDSGlobals->GetStoreMuonTrajectories())
00130   //  theMuonTrackVector=new MuonTrackVector();
00131   
00132   
00133    if(BDSGlobals->DoTwiss())
00134     {
00135       if(event_number==0) {
00136         if(!BDSGlobals->GetSynchRescale()) G4cout << "\n---> Calculating Twiss Parameters"<<G4endl;
00137         if(BDSGlobals->GetSynchRescale()) G4cout<<"\n---> Calculating Twiss Parameters and Rescaling magnets" <<G4endl;
00138       }
00139     }
00140   else
00141     {
00142       if ((event_number+1)%printModulo ==0)
00143         {
00144           G4cout << "\n---> Begin of event: " << event_number ;
00145           
00146 //        if(BDSGlobals->GetUseTimer())
00147 //          { 
00148 //            BDSGlobals->GetTimer()->Stop();
00149 //            G4cout<<" Time: "<<*BDSGlobals->GetTimer();
00150 //            BDSGlobals->GetTimer()->Start();
00151 //          }
00152           G4cout << G4endl;
00153         }
00154     }
00155 
00156   if(verboseEvent) G4cout<<"Begin of event: "<<event_number<<G4endl ;
00157 
00158   
00159   G4SDManager * SDman = G4SDManager::GetSDMpointer();
00160   //G4cout << bdsOutput.GetPlaneSamplerNumber() << " < PlaneSamplers" << G4endl;
00161   //if( bdsOutput.GetPlaneSamplerNumber() > 0)
00162   {   
00163     //if (SamplerCollID_plane==-1)
00164     SamplerCollID_plane = SDman->GetCollectionID("Sampler_plane");
00165   }
00166   
00167   //if( bdsOutput.GetCylinderSamplerNumber() > 0 )
00168   {   
00169     //if (SamplerCollID_cylin==-1)
00170     //SamplerCollID_cylin = SDman->GetCollectionID("Sampler_cylinder"); //WHY COMMENTED???
00171   }
00172   
00173   //if( bdsOutput.GetLWCalorimeterNumber() > 0 )
00174   {
00175     //if (LWCalorimeterCollID==-1) 
00176     //LWCalorimeterCollID = SDman->GetCollectionID("LWCalorimeterCollection");
00177   }
00178   FireLaserCompton=true;
00179 
00180   if(DEBUG) G4cout<<"BDSEventAction : begin of event action done"<<G4endl;
00181 }
00182 
00183 //======================================================
00184 
00185 void BDSEventAction::EndOfEventAction(const G4Event* evt)
00186 {
00187   if(DEBUG) G4cout<<"BDSEventAction : processing end of event action"<<G4endl;
00188 
00189   if(BDSGlobals->DoTwiss())
00190     {
00191       if(event_number==nptwiss-1)
00192         {
00193           G4cout << "\n---> Done" <<G4endl;
00194           G4EventManager::GetEventManager()->GetStackManager()->clear();
00195         }
00196     }
00197 
00198  
00199   if(verboseEvent || verboseEventNumber == event_number)
00200     G4cout<<"processing end of event"<<G4endl;
00201 
00202   G4SDManager * SDman = G4SDManager::GetSDMpointer();
00203 
00204   G4HCofThisEvent* HCE = evt->GetHCofThisEvent();
00205   
00206   BDSSamplerHitsCollection*  SampHC=NULL;
00207   //BDSLWCalorimeterHitsCollection* LWCalHC=NULL;
00208   BDSEnergyCounterHitsCollection* BDSEnergyCounter_HC=NULL;
00209 
00210   if(DEBUG) G4cout<<"BDSEventAction : storing hits"<<G4endl;
00211 
00212   // are there any planar samplers?
00213   // if so, record the hits for each sampler 
00214 
00215   if(DEBUG) G4cout<<"BDSEventAction : processing planar hits collection"<<G4endl;
00216 
00217   if(SamplerCollID_plane>=0)
00218     SampHC = (BDSSamplerHitsCollection*)(HCE->GetHC(SamplerCollID_plane));
00219 
00220   if(SampHC)  bdsOutput.WriteHits(SampHC);
00221         
00222 
00223   SampHC=NULL;
00224 
00225   // are there any cylindrical samplers?
00226   // if so, record the hits
00227 
00228   if(DEBUG) G4cout<<"BDSEventAction : processing cylinder hits collection"<<G4endl;
00229 
00230   if(SamplerCollID_cylin>=0)
00231     SampHC = (BDSSamplerHitsCollection*)(HCE->GetHC(SamplerCollID_cylin));
00232 
00233   if (SampHC) bdsOutput.WriteHits(SampHC);
00234 
00235 
00236 
00237   // are there any Laser wire calorimeters?
00238   // TODO : check it !!! at present not writing LW stuff
00239   // remember to uncomment LWCalHC above if using this
00240 
00241   // if(DEBUG) G4cout<<"BDSEventAction : processing laserwire calorimeter hits collection"<<G4endl;
00242 
00243   // if(LWCalorimeterCollID>=0) 
00244   //   LWCalHC=(BDSLWCalorimeterHitsCollection*)(HCE->GetHC(LWCalorimeterCollID));
00245 
00246   // if (LWCalHC) bdsOutput.WriteHits(SampHC);
00247 
00248 
00249   // create energy loss histogram
00250 
00251   if(DEBUG) G4cout<<"BDSEventAction : storing energy loss histograms"<<G4endl;
00252   
00253   for(iEC=theECList->begin();iEC!=theECList->end();iEC++)
00254     {
00255       G4String name=(*iEC)->GetCollectionName(0);
00256       
00257       G4int BDSEnergyCounter_ID= SDman->GetCollectionID(name);
00258       
00259       if(BDSEnergyCounter_ID>=0)
00260         {
00261           BDSEnergyCounter_HC=
00262             (BDSEnergyCounterHitsCollection*)(HCE->GetHC(BDSEnergyCounter_ID));
00263         
00264           if(BDSEnergyCounter_HC) 
00265             bdsOutput.WriteEnergyLoss(BDSEnergyCounter_HC);
00266         }
00267     }
00268 
00269 
00270   // if 0 events per ntuples - set max allowed events per ntuples  
00271 
00272   int evntsPerNtuple = BDSGlobals->GetNumberOfEventsPerNtuple();
00273 
00274   if(evntsPerNtuple>0)
00275     if ((event_number+1)% evntsPerNtuple == 0 && 
00276                 event_number+1 != BDSGlobals->GetNumberToGenerate())
00277       {
00278         if(DEBUG) G4cout<<"writing to file "<<G4endl;
00279         // notify the output about the event end
00280         // this can be used for splitting output files etc.
00281         bdsOutput.Commit(itsOutputFileNumber++);
00282         if(DEBUG) G4cout<<"done"<<G4endl;
00283       }
00284 
00285 
00286 
00287   // Save interesting trajectories
00288 
00289   if(DEBUG) G4cout<<"BDSEventAction : storing trajectories"<<G4endl;
00290 
00291   G4TrajectoryContainer* TrajCont=evt->GetTrajectoryContainer();
00292 
00293   if(!TrajCont) return;
00294   
00295   TrajectoryVector* TrajVec=TrajCont->GetVector();
00296   bdsOutput.WriteTrajectory(TrajVec);
00297 
00298   //the logic for controlling which trajectories are stored is already
00299   //implemented in BDSUserTrackingAction.cc
00300   /*
00301   if(BDSGlobals->GetStoreTrajectory()&& TrajVec)
00302     {
00303       if(DEBUG) G4cout<<"PROCESSING MUON TRAJECTORY VECTOR"<<G4endl;
00304       bdsOutput.WriteTrajectory(TrajVec);
00305     }
00306 
00307 
00308   if(BDSGlobals->GetStoreMuonTrajectories()&& TrajVec)
00309     {
00310       if(DEBUG) G4cout<<"PROCESSING MUON TRAJECTORY VECTOR"<<G4endl;
00311       bdsOutput.WriteTrajectory(TrajVec);
00312     }
00313 
00314   if(BDSGlobals->GetStoreNeutronTrajectories()&& TrajVec)
00315     {
00316       if(DEBUG) G4cout<<"PROCESSING NEUTRON TRAJECTORY VECTOR"<<G4endl;
00317       bdsOutput.WriteTrajectory(TrajVec);
00318     }
00319   */
00320 
00321   // needed to draw trajectories and hits:
00322   if(!isBatch) evt->Draw();
00323   
00324   // clear out the remaining trajectories
00325   TrajectoryVector::iterator iT;
00326   if(TrajVec)
00327     {
00328       for(iT=TrajVec->begin();iT<TrajVec->end();iT++)
00329         {
00330           G4Trajectory* Traj=(G4Trajectory*)(*iT);
00331           delete Traj;
00332           TrajVec->erase(iT);
00333           iT--;
00334         }
00335     }
00336   
00337   if(DEBUG) G4cout<<"BDSEventAction : end of event action done"<<G4endl;
00338 }
00339 
00340 //======================================================

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