3 #include "G4RunManager.hh" 4 #include "G4Trajectory.hh" 14 #include "CLHEP/Units/PhysicalConstants.h" 15 using namespace CLHEP;
22 for(
unsigned int t=0; t<tids.size(); t++)
24 if(hierarchy.find(tids[t]) != hierarchy.end())
25 otids.push_back(hierarchy[tids[t]]);
37 for(
int t=0; t<size; t++)
45 vector<G4ThreeVector> zthre;
46 for(
int t=0; t<size; t++)
47 zthre.push_back(G4ThreeVector(0,0,0));
52 vector<int>
vector_mtids(map<int, TInfos> tinfos, vector<int> tids)
55 for(
unsigned int t=0; t<tids.size(); t++)
56 mtids.push_back(tinfos[tids[t]].mtid);
61 vector<int>
vector_mpids(map<int, TInfos> tinfos, vector<int> tids)
64 for(
unsigned int t=0; t<tids.size(); t++)
65 mpids.push_back(tinfos[tids[t]].mpid);
70 vector<G4ThreeVector>
vector_mvert(map<int, TInfos> tinfos, vector<int> tids)
72 vector<G4ThreeVector> mvert;
73 for(
unsigned int t=0; t<tids.size(); t++)
74 mvert.push_back(tinfos[tids[t]].mv);
82 hd_msg = gemcOpt.
optMap[
"LOG_MSG"].args +
" Event Action: >> ";
83 Modulo = (int) gemcOpt.optMap[
"PRINT_EVENT"].arg ;
84 VERB = gemcOpt.optMap[
"BANK_VERBOSITY"].arg ;
85 catch_v = gemcOpt.optMap[
"CATCH"].args;
86 SAVE_ALL_MOTHERS = (
int) gemcOpt.optMap[
"SAVE_ALL_MOTHERS"].arg ;
88 MAXP = (int) gemcOpt.optMap[
"NGENP"].arg;
96 if(SAVE_ALL_MOTHERS>1)
98 lundOutput =
new ofstream(
"background.dat");
99 cout <<
" > Opening background.dat file to save background particles in LUND format." << endl;
102 evtN = gemcOpt.optMap[
"EVTN"].arg;
107 if(SAVE_ALL_MOTHERS>1)
113 rw.getRunNumber(evtN);
116 if(evtN%Modulo == 0 )
118 cout << hd_msg <<
" Begin of event " << evtN <<
" Run Number: " << rw.runNo;
119 if(rw.isNewRun) cout <<
" (new) ";
121 cout << hd_msg <<
" Random Number: " << G4UniformRand() << endl;
132 if(evtN%Modulo == 0 )
133 cout << hd_msg <<
" Starting Event Action Routine " << evtN <<
" Run Number: " << rw.runNo << endl;
140 for(map<string, sensitiveDetector*>::iterator it = SeDe_Map.begin(); it!= SeDe_Map.end(); it++)
144 MHC = it->second->GetMHitCollection();
145 nhits = MHC->GetSize();
147 for(
int h=0; h<nhits; h++)
149 vector<int> tids = (*MHC)[h]->GetTIds();
151 for(
unsigned int t=0; t<tids.size(); t++)
152 track_db.insert(tids[t]);
155 if(SAVE_ALL_MOTHERS>1)
157 vector<int> pids = (*MHC)[h]->GetPIDs();
159 vector<G4ThreeVector> vtxs = (*MHC)[h]->GetPos();
160 vector<G4ThreeVector> mmts = (*MHC)[h]->GetMoms();
161 vector<double> tims = (*MHC)[h]->GetTime();
164 for(
unsigned int t=0; t<tids.size(); t++)
165 if(bgMap.find(tids[t]) == bgMap.end())
166 bgMap[tids[t]] =
BGParts(pids[t], tims[t], vtxs[t], mmts[t]);
174 map<int, TInfos> tinfos;
175 set<int>::iterator it;
178 G4TrajectoryContainer *trajectoryContainer;
182 trajectoryContainer = evt->GetTrajectoryContainer();
186 cout <<
" >> Total number of tracks " << trajectoryContainer->size() << endl;
188 while(trajectoryContainer && track_db.size())
191 for(
unsigned int i=0; i< trajectoryContainer->size(); i++)
194 G4Trajectory* trj = (G4Trajectory*)(*(evt->GetTrajectoryContainer()))[i];
195 int tid = trj->GetTrackID();
199 if(momDaughter.find(tid) == momDaughter.end())
200 momDaughter[tid] = trj->GetParentID();
204 it = track_db.find(tid);
205 if(it != track_db.end())
207 int mtid = trj->GetParentID();
208 tinfos[tid] =
TInfos(mtid);
218 for(map<int, int>::iterator itm = momDaughter.begin(); itm != momDaughter.end(); itm++)
220 int ancestor = itm->first;
221 if(momDaughter[ancestor] == 0)
222 hierarchy[itm->first] = itm->first;
224 while (momDaughter[ancestor] != 0)
226 hierarchy[itm->first] = momDaughter[ancestor];
227 ancestor = momDaughter[ancestor];
232 for(map<int, TInfos>::iterator itm = tinfos.begin(); itm != tinfos.end(); itm++)
234 int mtid = (*itm).second.mtid;
236 for(
unsigned int i=0; i< trajectoryContainer->size() && mtid != 0; i++)
238 G4Trajectory* trj = (G4Trajectory*)(*(evt->GetTrajectoryContainer()))[i];
239 int tid = trj->GetTrackID();
242 tinfos[(*itm).first].mpid = trj->GetPDGEncoding();
243 tinfos[(*itm).first].mv = trj->GetPoint(0)->GetPosition();
248 if(SAVE_ALL_MOTHERS>2)
251 for(map<int, BGParts>::iterator it = bgMap.begin(); it != bgMap.end(); it++)
252 bgtIDs.push_back(it->first);
254 for(
unsigned i=0; i<bgtIDs.size(); i++)
256 int daughter = bgtIDs[i];
258 if(momDaughter.find(daughter) != momDaughter.end())
259 momCheck = momDaughter[daughter];
264 if(bgMap.find(momCheck) != bgMap.end())
268 if(bgMap.find(daughter) != bgMap.end())
269 bgMap.erase(bgMap.find(daughter));
272 if(momDaughter.find(momCheck) != momDaughter.end())
273 momCheck = momDaughter[momCheck];
287 map<string, outputFactoryInMap>::iterator ito = outputFactoryMap->find(outContainer->outType);
288 if(ito == outputFactoryMap->end())
290 if(outContainer->outType !=
"no")
291 cout << hd_msg <<
" Warning: output type <" << outContainer->outType
292 <<
"> is not registered in the outContainerput Factory. " << endl
293 <<
" This event will not be written out." << endl;
304 map<string, double> header;
305 header[
"runNo"] = rw.runNo;
306 header[
"evn"] = evtN;
307 header[
"evn_type"] = -1;
308 header[
"beamPol"] = gen_action->getBeamPol();
310 for(
unsigned i=0; i<gen_action->lundUserDefined.size(); i++)
312 string tmp =
"var" +
stringify((
int) i+1);
313 header[tmp] = gen_action->lundUserDefined[i];
321 vector<generatedParticle> MPrimaries;
322 for(
int pv=0; pv<evt->GetNumberOfPrimaryVertex() && pv<MAXP; pv++)
325 G4PrimaryVertex* MPV = evt->GetPrimaryVertex(pv);
326 Mparticle.
vertex = MPV->GetPosition();
327 for(
int pp = 0; pp<MPV->GetNumberOfParticle() && pv<MAXP; pp++)
330 G4PrimaryParticle *PP = MPV->GetPrimary(pp);
331 Mparticle.
momentum = PP->GetMomentum();
332 Mparticle.
PID = PP->GetPDGcode();
334 MPrimaries.push_back(Mparticle) ;
337 if(SAVE_ALL_MOTHERS>1)
340 for(map<string, sensitiveDetector*>::iterator it = SeDe_Map.begin(); it!= SeDe_Map.end(); it++)
342 MHC = it->second->GetMHitCollection();
343 nhits = MHC->GetSize();
351 string vname = (*MHC)[0]->GetDetector().name;
352 string hitType = it->second->GetDetectorHitType(vname);
355 if(!hitProcessRoutine)
358 hitProcessRoutine->
init(hitType, gemcOpt, gPars);
360 bool WRITE_TRUE_INTEGRATED = 0;
361 bool WRITE_TRUE_ALL = 0;
362 if(WRITE_INTRAW.find(hitType) != string::npos) WRITE_TRUE_INTEGRATED = 1;
363 if(WRITE_ALLRAW.find(hitType) != string::npos) WRITE_TRUE_ALL = 1;
365 vector<hitOutput> allRawOutput;
368 for(
unsigned pi = 0; pi<MPrimaries.size(); pi++)
372 for(
int h=0; h<nhits; h++)
374 MHit* aHit = (*MHC)[h];
382 vector<int> tids = aHit->
GetTIds();
383 vector<int> otids = vector_otids(tids);
394 vector<int> hitByPrimary;
395 for(
unsigned pi = 0; pi<MPrimaries.size(); pi++)
396 hitByPrimary.push_back(0);
399 for(
unsigned pi = 0; pi<MPrimaries.size(); pi++)
401 vector<double> edeps = aHit->
GetEdep();
402 vector<double> times = aHit->
GetTime();
403 MPrimaries[pi].pSum.back().nphe = aHit->
GetTIds().size();
404 for(
unsigned ss =0; ss<edeps.size(); ss++)
406 if(otids[ss] == (
int) pi+1)
408 MPrimaries[pi].pSum.back().etot += edeps[ss];
411 if(MPrimaries[pi].pSum.back().t < 0 || MPrimaries[pi].pSum.back().t > times[ss])
412 MPrimaries[pi].pSum.back().t = times[ss];
417 if(hitByPrimary[pi]) MPrimaries[pi].pSum.back().stat++;
419 if(MPrimaries[pi].pSum.back().etot > 0 || MPrimaries[pi].pSum.back().nphe > 0)
420 MPrimaries[pi].pSum.back().dname = hitType;
426 int thisHitSize = aHit->
GetId().size();
428 vector<G4ThreeVector> zthre =
vector_zthre(thisHitSize);
441 allRawOutput.push_back(thisHitOutput);
443 string vname = aHit->
GetId()[aHit->
GetId().size()-1].name;
444 if(VERB > 4 || vname.find(catch_v) != string::npos)
446 cout << hd_msg <<
" Hit " << h + 1 <<
" -- total number of steps this hit: " << aHit->
GetPos().size() << endl;
447 cout << aHit->
GetId();
449 for(
unsigned int e=0; e<aHit->
GetPos().size(); e++) Etot = Etot + aHit->
GetEdep()[e];
450 cout <<
" Total energy deposited: " << Etot/MeV <<
" MeV" << endl;
458 if(WRITE_TRUE_INTEGRATED)
466 processOutputFactory->
writeG4RawAll(outContainer, allRawOutput, hitType, banksMap);
471 for(
unsigned pi = 0; pi<MPrimaries.size(); pi++)
473 cout <<
" Particle " << pi + 1 <<
" has " << MPrimaries[pi].pSum.size() <<
" particle summaries:" << endl;
474 for(
unsigned ss =0; ss<MPrimaries[pi].pSum.size(); ss++)
476 cout <<
" \t det: " << MPrimaries[pi].pSum[ss].dname <<
477 " Etot: " << MPrimaries[pi].pSum[ss].etot <<
478 " time: " << MPrimaries[pi].pSum[ss].t << endl;
488 if(WRITE_INTDGT.find(hitType) == string::npos)
492 vector<hitOutput> allDgtOutput;
493 for(
int h=0; h<nhits; h++)
497 MHit* aHit = (*MHC)[h];
500 allDgtOutput.push_back(thisHitOutput);
502 string vname = aHit->
GetId()[aHit->
GetId().size()-1].name;
503 if(VERB > 4 || vname.find(catch_v) != string::npos)
505 cout << hd_msg <<
" Hit " << h + 1 <<
" -- total number of steps this hit: " << aHit->
GetPos().size() << endl;
506 cout << aHit->
GetId();
508 for(
unsigned int e=0; e<aHit->
GetPos().size(); e++) Etot = Etot + aHit->
GetEdep()[e];
509 cout <<
" Total energy deposited: " << Etot/MeV <<
" MeV" << endl;
521 if(SIGNALVT.find(hitType) != string::npos)
523 vector<hitOutput> allVTOutput;
525 for(
int h=0; h<nhits; h++)
529 MHit* aHit = (*MHC)[h];
534 allVTOutput.push_back(thisHitOutput);
538 string vname = aHit->
GetId()[aHit->
GetId().size()-1].name;
539 if(VERB > 4 || vname.find(catch_v) != string::npos)
541 cout << hd_msg <<
" Hit " << h + 1 <<
" -- total number of steps this hit: " << aHit->
GetPos().size() << endl;
542 cout << aHit->
GetId();
544 for(
unsigned int e=0; e<aHit->
GetPos().size(); e++) Etot = Etot + aHit->
GetEdep()[e];
545 cout <<
" Total energy deposited: " << Etot/MeV <<
" MeV" << endl;
550 delete hitProcessRoutine;
554 processOutputFactory->
writeGenerated(outContainer, MPrimaries, banksMap);
556 processOutputFactory->
writeEvent(outContainer);
557 delete processOutputFactory;
560 if(evtN%Modulo == 0 )
561 cout << hd_msg <<
" End of Event " << evtN <<
" Routine..." << endl << endl;
580 *lundOutput << (int) bgMap.size() <<
"\t" << evtN <<
"\t 0 0 0 0 0 0 0 0 " << endl;
583 for(map<int, BGParts>::iterator it = bgMap.begin(); it != bgMap.end(); it++)
584 *lundOutput << i++ <<
"\t0\t1\t" << it->second.pid <<
"\t0\t" << it->first <<
"\t" 585 << it->second.p.x()/GeV <<
"\t" << it->second.p.y()/GeV <<
"\t" << it->second.p.z()/GeV <<
"\t" << it->second.time <<
"\t0\t" 586 << it->second.v.x()/cm <<
"\t" << it->second.v.y()/cm <<
"\t" << it->second.v.z()/cm << endl;
virtual map< double, double > signalVT(MHit *)
void setSignal(map< double, double > s)
virtual void initWithRunNumber(int runno)
void init(string name, goptions go, map< string, double > gp)
void SetmPIDs(vector< int > mpid)
virtual map< int, int > quantumS(map< double, double >, MHit *)
MEventAction(goptions, map< string, double >)
Constructor copies gemc options.
void setQuantumS(map< int, int > s)
virtual map< string, double > integrateDgt(MHit *, int)=0
void SetmTrackIds(vector< int > tid)
vector< identifier > GetId()
virtual void writeHeader(outputContainer *, map< string, double >, gBank)=0
vector< G4ThreeVector > vector_zthre(int size)
provides a vector of (0,0,0)
string replaceCharWithChars(string input, string x, string y)
vector< int > vector_mpids(map< int, TInfos > tinfos, vector< int > tids)
void EndOfEventAction(const G4Event *)
Routine at the end of each event.
void setRaws(map< string, double > r)
map< string, vector< double > > allRaws(MHit *, int)
void BeginOfEventAction(const G4Event *)
Routine at the start of each event.
gBank getBankFromMap(string name, map< string, gBank > *banksMap)
vector< G4ThreeVector > vector_mvert(map< int, TInfos > tinfos, vector< int > tids)
string stringify(double x)
vector< G4ThreeVector > GetPos()
map< string, aopt > optMap
Options map.
G4THitsCollection< MHit > MHitCollection
vector< int > vector_otids(vector< int > tids)
return original track id of a vector of tid
void SetoTrackIds(vector< int > tid)
void setDgtz(map< string, double > d)
vector< double > GetTime()
vector< int > vector_mtids(map< int, TInfos > tinfos, vector< int > tids)
virtual void writeG4RawIntegrated(outputContainer *, vector< hitOutput >, string, map< string, gBank > *)=0
virtual void writeEvent(outputContainer *)=0
map< double, double > getSignalVT()
outputFactory * getOutputFactory(map< string, outputFactoryInMap > *outputFactoryMap, string outputType)
void setAllRaws(map< string, vector< double > > r)
virtual void writeG4RawAll(outputContainer *, vector< hitOutput >, string, map< string, gBank > *)=0
virtual void writeG4DgtIntegrated(outputContainer *, vector< hitOutput >, string, map< string, gBank > *)=0
vector< double > GetEdep()
void SetmVerts(vector< G4ThreeVector > ver)
HitProcess * getHitProcess(map< string, HitProcess_Factory > *hitProcessMap, string HCname)
~MEventAction()
Destructor.
vector< int > vector_zint(int size)
provides a vector of 0
map< string, double > integrateRaw(MHit *, int, bool)
virtual void writeGenerated(outputContainer *, vector< generatedParticle >, map< string, gBank > *banksMap)=0