HGCal Test Beam  03a93d6239a951948e06fb3ef8dae4cbdebfad30
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
EventAction.cc
Go to the documentation of this file.
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"
8 
9 #include "G4RunManager.hh"
10 #include "G4Event.hh"
11 #include "G4UnitsTable.hh"
12 
13 #include <iomanip>
14 
15 //
16 EventAction::EventAction()
17 {
18  runAct = (RunAction*)G4RunManager::GetRunManager()->GetUserRunAction();
19  eventMessenger = new EventActionMessenger(this);
20  printModulo = 100;
21  outF_ = TFile::Open("PFcal.root", "RECREATE");
22  outF_->cd();
23 
24  DetectorConstruction* detector
25  = (DetectorConstruction*)(G4RunManager::GetRunManager()->
26  GetUserDetectorConstruction());
27 
28  // cache save tracks flag
29 
30  saveTracks = detector->saveTracks();
31 
32  // A hack to avoid compiler warning
33  int hack = CLHEP::HepRandomGenActive;
34  int model = hack;
35 
36  model = detector->getModel();
37  int version = detector->getVersion();
38  double xysize = detector->GetCalorSizeXY();
39  double cellsize = detector->GetCellSize();
40 
41  //save some info
42  HGCSSInfo* info = new HGCSSInfo();
43  info->calorSizeXY(xysize);
44  info->cellSize(cellsize);
45  info->model(model);
46  info->version(version);
47 
48  G4cout << " -- check Info: version = " << info->version()
49  << " model = " << info->model() << G4endl;
50  outF_->WriteObjectAny(info, "HGCSSInfo", "Info");
51 
52  //honeycomb
53  geomConv_ = new HGCSSGeometryConversion(info->model(),
54  xysize,
55  cellsize);
56  geomConv_->initialiseHoneyComb(xysize, cellsize);
57 
58  //square map
59  geomConv_->initialiseSquareMap(xysize, cellsize);
60 
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_);
68  if ( saveTracks )
69  tree_->Branch("HGCSSTrackSegmentVec",
70  "std::vector<HGCSSTrackSegment>", &trkvec_);
71 }
72 
73 //
74 EventAction::~EventAction()
75 {
76  outF_->cd();
77  tree_->Write();
78  outF_->Close();
79  delete eventMessenger;
80 }
81 
82 //
83 void EventAction::BeginOfEventAction(const G4Event* evt)
84 {
85  evtNb_ = evt->GetEventID();
86  if (evtNb_ % printModulo == 0) {
87  G4cout << "\n---> Start event: " << evtNb_ << G4endl;
88  CLHEP::HepRandom::showEngineStatus();
89  }
90 }
91 
92 //
93 void EventAction::Store(G4int trackId, G4int pdgId, G4double globalTime,
94  const G4ThreeVector& p1, const G4ThreeVector& p2,
95  const G4ThreeVector& p)
96 {
97  if ( saveTracks )
98  trkvec_.push_back(HGCSSTrackSegment(trackId, pdgId, globalTime,
99  p1, p2, p));
100 }
101 
102 //
103 void EventAction::Detect(G4double edep,
104  G4double stepl,
105  G4double globalTime,
106  G4int pdgId,
107  G4VPhysicalVolume* volume,
108  const G4ThreeVector& position,
109  G4int trackID,
110  G4int parentID,
111  const HGCSSGenParticle& genPart)
112 {
113  for(size_t i = 0; i < detector_->size(); i++)
114  (*detector_)[i].add(edep,
115  stepl,
116  globalTime,
117  pdgId,
118  volume,
119  position,
120  trackID,
121  parentID,
122  i);
123  if (genPart.isIncoming()) genvec_.push_back(genPart);
124 }
125 
126 bool EventAction::isFirstVolume(const std::string volname) const
127 {
128  if ( ! detector_ ) return false;
129  if ( detector_->size() == 0 ) return false;
130 
131  // get the first sampling section.
132  // loop over all of its volumes
133  // and check if one of them matches the
134  // given volume name
135  SamplingSection& section = (*detector_)[0];
136  if ( section.n_elements == 0 ) return false;
137 
138  bool found = 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 ) {
142  found = true;
143  break;
144  }
145  }
146  return found;
147 }
148 
149 //
150 void EventAction::EndOfEventAction(const G4Event* g4evt)
151 {
152  assert(g4evt != 0);
153  bool debug(evtNb_ % printModulo == 0);
154 
155  hitvec_.clear();
156  trkvec_.clear();
157  genvec_.clear();
158  ssvec_.clear();
159 
160  event_.eventNumber(evtNb_);
161 
162  //G4cout << " EndOfEventAction - Number of primary vertices: "
163  // << g4evt->GetNumberOfPrimaryVertex() << G4endl;
164  assert(g4evt->GetNumberOfPrimaryVertex() > 0);
165 
166  if ( debug )
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()
171  << G4endl;
172 
173  event_.vtx_x(g4evt->GetPrimaryVertex(0)->GetX0());
174  event_.vtx_y(g4evt->GetPrimaryVertex(0)->GetY0());
175  event_.vtx_z(g4evt->GetPrimaryVertex(0)->GetZ0());
176 
177  ssvec_.clear();
178  ssvec_.reserve(detector_->size());
179 
180  for(size_t i = 0; i < detector_->size(); i++) {
181  HGCSSSamplingSection lSec;
182 
183  // NOTE: get a reference, NOT a copy!
184  SamplingSection& section = (*detector_)[i];
185 
186  lSec.volNb(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);
201 
202  if (evtNb_ == 1) std::cout << "if (layer==" << i << ") return "
203  << lSec.voldEdx() << ";"
204  << std::endl;
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;
209 
210  if (i > 0) {
211  // look at previous section, but check that it has
212  // sensitive elements.
213  SamplingSection& prevsection = (*detector_)[i - 1];
214  if ( prevsection.SiHitVecSize() > 0 ) {
215  const G4SiHitVec vhit = prevsection.getSiHitVec(idx);
216  section.trackParticleHistory(idx, vhit);
217  }
218  }
219 
220  for (unsigned iSiHit(0);
221  iSiHit < section.getSiHitVec(idx).size();
222  ++iSiHit) {
223  G4SiHit lSiHit = section.getSiHitVec(idx)[iSiHit];
224  HGCSSSimHit lHit(lSiHit, section.sens_layer[idx], is_scint
225  ? geomConv_->squareMap()
226  : geomConv_->hexagonMap());
227 
228  isInserted = lHitMap.
229  insert(std::pair<unsigned, HGCSSSimHit>(lHit.cellid(), lHit));
230  if (!isInserted.second) isInserted.first->second.Add(lSiHit);
231  }
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);
237  }
238 
239  }//loop on sensitive layers
240 
241  if(debug) {
242  section.report( (i == 0) );
243  }
244  //if (i==0) G4cout << " ** evt " << evt->GetEventID() << G4endl;
245  section.resetCounters();
246  }
247  if(debug) {
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;
252  }
253 
254  tree_->Fill();
255 
256  //G4cout << "END(EventAction::EndOfEventAction" << G4endl;
257 }