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;
00031
00032 G4bool FireLaserCompton;
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
00062 analMan = BDSAnalysisManager::Instance();
00063
00064
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
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
00082
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
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
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
00139
00140
00141
00142
00143
00144
00145
00146
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
00158 if(energyCounterHits)
00159 {
00160 bdsOutput->WriteEnergyLoss(energyCounterHits);
00161
00162 for (G4int i = 0; i < energyCounterHits->entries(); i++)
00163 {
00164
00165 analMan->Fill1DHistogram(2,(*energyCounterHits)[i]->GetS()/CLHEP::m,(*energyCounterHits)[i]->GetEnergy()/CLHEP::GeV);
00166
00167 analMan->Fill1DHistogram(5,(*energyCounterHits)[i]->GetS()/CLHEP::m,(*energyCounterHits)[i]->GetEnergy()/CLHEP::GeV);
00168 }
00169 }
00170
00171
00172 if(primaryCounterHits) {
00173 if (primaryCounterHits->entries()>0){
00174 BDSEnergyCounterHit* thePrimaryHit = BDS::LowestSPosPrimaryHit(primaryCounterHits);
00175 BDSEnergyCounterHit* thePrimaryLoss = BDS::HighestSPosPrimaryHit(primaryCounterHits);
00176
00177 if (thePrimaryHit && thePrimaryLoss)
00178 {
00179 bdsOutput->WritePrimaryLoss(thePrimaryLoss);
00180 bdsOutput->WritePrimaryHit(thePrimaryHit);
00181
00182 analMan->Fill1DHistogram(0,thePrimaryHit->GetS()/CLHEP::m);
00183 analMan->Fill1DHistogram(1,thePrimaryLoss->GetS()/CLHEP::m);
00184
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
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();
00204 #ifdef BDSDEBUG
00205 G4cout<<"done"<<G4endl;
00206 #endif
00207 }
00208
00209
00210 if(!isBatch) {
00211 #ifdef BDSDEBUG
00212 G4cout<<"BDSEventAction : drawing"<<G4endl;
00213 #endif
00214 evt->Draw();
00215 }
00216
00217
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
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
00241 if(interestingTrajectories.size()>0){
00242 bdsOutput->WriteTrajectory(interestingTrajectories);
00243 interestingTrajectories.clear();
00244 }
00245 }
00246
00247
00248 #ifdef BDSDEBUG
00249
00250 #endif
00251
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
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 }