1 #include "HGCal/TBStandaloneSimulator/interface/EventAction.hh"
2 #include "HGCal/TBStandaloneSimulator/interface/RunAction.hh"
3 #include "HGCal/TBStandaloneSimulator/interface/EventActionMessenger.hh"
4 #include "HGCal/TBStandaloneSimulator/interface/DetectorConstruction.hh"
5 #include "HGCal/TBStandaloneSimulator/interface/HGCSSInfo.hh"
6 #include "HGCal/TBStandaloneSimulator/interface/HGCSSSimHit.hh"
7 #include "HGCal/TBStandaloneSimulator/interface/HGCSSTrackSegment.h"
9 #include "G4RunManager.hh"
11 #include "G4UnitsTable.hh"
16 EventAction::EventAction()
18 runAct = (RunAction*)G4RunManager::GetRunManager()->GetUserRunAction();
19 eventMessenger =
new EventActionMessenger(
this);
21 outF_ = TFile::Open(
"PFcal.root",
"RECREATE");
24 DetectorConstruction* detector
25 = (DetectorConstruction*)(G4RunManager::GetRunManager()->
26 GetUserDetectorConstruction());
30 saveTracks = detector->saveTracks();
33 int hack = CLHEP::HepRandomGenActive;
36 model = detector->getModel();
37 int version = detector->getVersion();
38 double xysize = detector->GetCalorSizeXY();
39 double cellsize = detector->GetCellSize();
42 HGCSSInfo* info =
new HGCSSInfo();
43 info->calorSizeXY(xysize);
44 info->cellSize(cellsize);
46 info->version(version);
48 G4cout <<
" -- check Info: version = " << info->version()
49 <<
" model = " << info->model() << G4endl;
50 outF_->WriteObjectAny(info,
"HGCSSInfo",
"Info");
53 geomConv_ =
new HGCSSGeometryConversion(info->model(),
56 geomConv_->initialiseHoneyComb(xysize, cellsize);
59 geomConv_->initialiseSquareMap(xysize, cellsize);
61 tree_ =
new TTree(
"HGCSSTree",
"HGC Standalone simulation");
62 tree_->Branch(
"HGCSSEvent",
"HGCSSEvent", &event_);
63 tree_->Branch(
"HGCSSSamplingSectionVec",
64 "std::vector<HGCSSSamplingSection>", &ssvec_);
65 tree_->Branch(
"HGCSSSimHitVec",
"std::vector<HGCSSSimHit>", &hitvec_);
66 tree_->Branch(
"HGCSSGenParticleVec",
67 "std::vector<HGCSSGenParticle>", &genvec_);
69 tree_->Branch(
"HGCSSTrackSegmentVec",
70 "std::vector<HGCSSTrackSegment>", &trkvec_);
74 EventAction::~EventAction()
79 delete eventMessenger;
83 void EventAction::BeginOfEventAction(
const G4Event* evt)
85 evtNb_ = evt->GetEventID();
86 if (evtNb_ % printModulo == 0) {
87 G4cout <<
"\n---> Start event: " << evtNb_ << G4endl;
88 CLHEP::HepRandom::showEngineStatus();
93 void EventAction::Store(G4int trackId, G4int pdgId, G4double globalTime,
94 const G4ThreeVector& p1,
const G4ThreeVector& p2,
95 const G4ThreeVector& p)
103 void EventAction::Detect(G4double edep,
107 G4VPhysicalVolume* volume,
108 const G4ThreeVector& position,
111 const HGCSSGenParticle& genPart)
113 for(
size_t i = 0; i < detector_->size(); i++)
114 (*detector_)[i].add(edep,
123 if (genPart.isIncoming()) genvec_.push_back(genPart);
126 bool EventAction::isFirstVolume(
const std::string volname)
const
128 if ( ! detector_ )
return false;
129 if ( detector_->size() == 0 )
return false;
135 SamplingSection& section = (*detector_)[0];
136 if ( section.n_elements == 0 )
return false;
139 for(
size_t c = 0; c < section.ele_vol.size(); c++) {
140 std::string name = std::string(section.ele_vol[c]->GetName());
141 if ( name == volname ) {
150 void EventAction::EndOfEventAction(
const G4Event* g4evt)
153 bool debug(evtNb_ % printModulo == 0);
160 event_.eventNumber(evtNb_);
164 assert(g4evt->GetNumberOfPrimaryVertex() > 0);
167 G4cout <<
" -- vtx pos x=" << g4evt->GetPrimaryVertex(0)->GetX0()
168 <<
" y=" << g4evt->GetPrimaryVertex(0)->GetY0()
169 <<
" z=" << g4evt->GetPrimaryVertex(0)->GetZ0()
170 <<
" t=" << g4evt->GetPrimaryVertex(0)->GetT0()
173 event_.vtx_x(g4evt->GetPrimaryVertex(0)->GetX0());
174 event_.vtx_y(g4evt->GetPrimaryVertex(0)->GetY0());
175 event_.vtx_z(g4evt->GetPrimaryVertex(0)->GetZ0());
178 ssvec_.reserve(detector_->size());
180 for(
size_t i = 0; i < detector_->size(); i++) {
181 HGCSSSamplingSection lSec;
184 SamplingSection& section = (*detector_)[i];
187 lSec.volX0trans(section.getAbsorberX0());
188 lSec.voldEdx(section.getAbsorberdEdx());
189 lSec.volLambdatrans(section.getAbsorberLambda());
190 lSec.absorberE(section.getAbsorbedEnergy());
191 lSec.measuredE(section.getMeasuredEnergy(
false));
192 lSec.totalE(section.getTotalEnergy());
193 lSec.gFrac(section.getPhotonFraction());
194 lSec.eFrac(section.getElectronFraction());
195 lSec.muFrac(section.getMuonFraction());
196 lSec.neutronFrac(section.getNeutronFraction());
197 lSec.hadFrac(section.getHadronicFraction());
198 lSec.avgTime(section.getAverageTime());
199 lSec.nSiHits(section.getTotalSensHits());
200 ssvec_.push_back(lSec);
202 if (evtNb_ == 1) std::cout <<
"if (layer==" << i <<
") return "
203 << lSec.voldEdx() <<
";"
205 bool is_scint = section.hasScintillator;
206 for (
unsigned idx(0); idx < section.n_sens_elements; ++idx) {
207 std::map<unsigned, HGCSSSimHit> lHitMap;
208 std::pair<std::map<unsigned, HGCSSSimHit>::iterator,
bool> isInserted;
213 SamplingSection& prevsection = (*detector_)[i - 1];
214 if ( prevsection.SiHitVecSize() > 0 ) {
215 const G4SiHitVec vhit = prevsection.getSiHitVec(idx);
216 section.trackParticleHistory(idx, vhit);
220 for (
unsigned iSiHit(0);
221 iSiHit < section.getSiHitVec(idx).size();
223 G4SiHit lSiHit = section.getSiHitVec(idx)[iSiHit];
224 HGCSSSimHit lHit(lSiHit, section.sens_layer[idx], is_scint
225 ? geomConv_->squareMap()
226 : geomConv_->hexagonMap());
228 isInserted = lHitMap.
229 insert(std::pair<unsigned, HGCSSSimHit>(lHit.cellid(), lHit));
230 if (!isInserted.second) isInserted.first->second.Add(lSiHit);
232 std::map<unsigned, HGCSSSimHit>::iterator lIter = lHitMap.begin();
233 hitvec_.reserve(hitvec_.size() + lHitMap.size());
234 for (; lIter != lHitMap.end(); ++lIter) {
235 (lIter->second).calculateTime();
236 hitvec_.push_back(lIter->second);
242 section.report( (i == 0) );
245 section.resetCounters();
248 G4cout <<
" -- Number of track segments = " << trkvec_.size() << G4endl
249 <<
" -- Number of truth particles = " << genvec_.size() << G4endl
250 <<
" -- Number of simhits = " << hitvec_.size() << G4endl
251 <<
" -- Number of sampling sections = " << ssvec_.size() << G4endl;