HGCal Test Beam  03a93d6239a951948e06fb3ef8dae4cbdebfad30
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
SamplingSection.cc
Go to the documentation of this file.
1 #include <cassert>
2 #include "G4VPhysicalVolume.hh"
3 #include "G4SystemOfUnits.hh"
4 #include "HGCal/TBStandaloneSimulator/interface/SamplingSection.hh"
5 
6 using namespace std;
7 
8 int SamplingSection::sens_layer_count = 0;
9 
10 SamplingSection::SamplingSection(std::vector<TBGeometry::Element>&
11  elements_)
12  : elements(elements_)
13 {
14  G4cout << " -- BEGIN(sampling section)" << G4endl;
15 
16  Total_thick = 0;
17  n_sens_elements = 0;
18  n_elements = 0;
19  ele_name.clear();
20  ele_X0.clear();
21  ele_L0.clear();
22  ele_vol.clear();
23  sens_layer.clear();
24  hasScintillator = false;
25  n_elements = elements.size();
26 
27  for(size_t c = 0; c < elements.size(); c++) {
28  TBGeometry::Element& e = elements[c];
29  G4double units = mm;
30  string theunits = e.smap["units"];
31  if ( theunits == "m" )
32  units = m;
33  else if ( theunits == "cm" )
34  units = cm;
35 
36  bool isSensitive = static_cast<bool>(e.imap["sensitive"]);
37  string material = e.smap["material"];
38  double thickness = e.dmap["thickness"] * units;
39  double zpos = e.dmap["z"] * units;
40 
41  if ( material == "Scintillator" ) hasScintillator = true;
42 
43  ele_name.push_back(material);
44  ele_thick.push_back(thickness);
45  ele_X0.push_back(0);
46  ele_dEdx.push_back(0);
47  ele_L0.push_back(0);
48  ele_vol.push_back(0);
49 
50  Total_thick += thickness;
51 
52  if ( isSensitive ) {
53  G4SiHitVec lVec;
54  sens_HitVec.push_back(lVec);
55  sens_layer.push_back(sens_layer_count);
56  sens_layer_count++;
57  }
58 
59  cout << "\tmaterial: " << material
60  << "\tthickness: " << thickness << theunits
61  << "\tz: " << zpos;
62  if ( isSensitive ) cout << " <=== sensitive";
63  cout << endl;
64  }
65  n_sens_elements = sens_HitVec.size();
66 
67  sens_HitVec_size_max = 0;
68  resetCounters();
69 
70  cout << " number of elements: " << n_elements << endl
71  << " number of sensitive elements: " << n_sens_elements << endl
72  << " -- END(sampling section)" << endl
73  << endl;
74 }
75 
76 //
77 void SamplingSection::add(G4double den, G4double dl,
78  G4double globalTime, G4int pdgId,
79  G4VPhysicalVolume* vol,
80  const G4ThreeVector & position,
81  G4int trackID, G4int parentID,
82  G4int layerId)
83 {
84  std::string lstr = vol->GetName();
85  for (size_t ie(0); ie < n_elements; ++ie) {
86  if(ele_vol[ie] && lstr == ele_vol[ie]->GetName()) {
87  size_t eleidx = ie;
88  ele_den[eleidx] += den;
89  ele_dl[eleidx] += dl;
90  if (isSensitiveElement(eleidx)) {
91  size_t idx = getSensitiveLayerIndex(lstr);
92  sens_time[idx] += den * globalTime;
93 
94  //discriminate further by particle type
95  if(abs(pdgId) == 22) sens_gFlux[idx] += den;
96  else if(abs(pdgId) == 11) sens_eFlux[idx] += den;
97  else if(abs(pdgId) == 13) sens_muFlux[idx] += den;
98  else if (abs(pdgId) == 2112) sens_neutronFlux[idx] += den;
99  else {
100  sens_hadFlux[idx] += den;
101  }
102 
103  //add hit
104  G4SiHit lHit;
105  lHit.energy = den;
106  lHit.time = globalTime;
107  lHit.pdgId = pdgId;
108  lHit.layer = layerId;
109  lHit.hit_x = position.x();
110  lHit.hit_y = position.y();
111  lHit.hit_z = position.z();
112  lHit.trackId = trackID;
113  lHit.parentId = parentID;
114  sens_HitVec[idx].push_back(lHit);
115  }//if Si
116  }//if in right material
117 
118  }//loop on available materials
119 }
120 
121 //
122 void SamplingSection::report(bool header)
123 {
124  if(header) G4cout << "E/[MeV]\t Si\tAbsorber\tTotal\tSi g frac\tSi e frac\tSi mu frac\tSi had frac\tSi <t> \t nG4SiHits" << G4endl;
125  G4cout << std::setprecision(3) << "\t " << getMeasuredEnergy(false) << "\t" << getAbsorbedEnergy() << "\t\t" << getTotalEnergy() << "\t"
126  << getPhotonFraction() << "\t" << getElectronFraction() << "\t" << getMuonFraction() << "\t" << getHadronicFraction() << "\t"
127  << getAverageTime() << "\t"
128  << getTotalSensHits() << "\t"
129  << G4endl;
130 }
131 
132 G4int SamplingSection::getTotalSensHits()
133 {
134  G4int tot = 0;
135  for (unsigned ie(0); ie < n_sens_elements; ++ie) {
136  tot += sens_HitVec[ie].size();
137  }
138  return tot;
139 }
140 
141 G4double SamplingSection::getTotalSensE()
142 {
143  double etot = 0;
144  for (unsigned ie(0); ie < n_elements; ++ie) {
145  if (isSensitiveElement(ie)) etot += ele_den[ie];
146  }
147  return etot;
148 }
149 
150 G4double SamplingSection::getAverageTime()
151 {
152  double etot = getTotalSensE();
153  double time = 0;
154  for (unsigned ie(0); ie < n_sens_elements; ++ie) {
155  time += sens_time[ie];
156  }
157  return etot > 0 ? time / etot : 0 ;
158 }
159 
160 //
161 G4double SamplingSection::getPhotonFraction()
162 {
163  double etot = getTotalSensE();
164  double val = 0;
165  for (unsigned ie(0); ie < n_sens_elements; ++ie) {
166  val += sens_gFlux[ie];
167  }
168  return etot > 0 ? val / etot : 0 ;
169 }
170 
171 //
172 G4double SamplingSection::getElectronFraction()
173 {
174  double etot = getTotalSensE();
175  double val = 0;
176  for (unsigned ie(0); ie < n_sens_elements; ++ie) {
177  val += sens_eFlux[ie];
178  }
179  return etot > 0 ? val / etot : 0 ;
180 }
181 
182 //
183 G4double SamplingSection::getMuonFraction()
184 {
185  double etot = getTotalSensE();
186  double val = 0;
187  for (unsigned ie(0); ie < n_sens_elements; ++ie) {
188  val += sens_muFlux[ie];
189  }
190  return etot > 0 ? val / etot : 0 ;
191 }
192 
193 //
194 G4double SamplingSection::getNeutronFraction()
195 {
196  double etot = getTotalSensE();
197  double val = 0;
198  for (unsigned ie(0); ie < n_sens_elements; ++ie) {
199  val += sens_neutronFlux[ie];
200  }
201  return etot > 0 ? val / etot : 0 ;
202 }
203 
204 //
205 G4double SamplingSection::getHadronicFraction()
206 {
207  double etot = getTotalSensE();
208  double val = 0;
209  for (unsigned ie(0); ie < n_sens_elements; ++ie) {
210  val += sens_hadFlux[ie];
211  }
212  return etot > 0 ? val / etot : 0 ;
213 }
214 
215 //
216 G4double SamplingSection::getMeasuredEnergy(bool weighted)
217 {
218  G4double weight = (weighted ? getAbsorberX0() : 1.0);
219  return weight * getTotalSensE();
220 }
221 
222 //
223 G4double SamplingSection::getAbsorberX0()
224 {
225  double val = 0;
226  for (unsigned ie(0); ie < n_elements; ++ie) {
227  if (isAbsorberElement(ie))
228  if (ele_X0[ie] > 0) val += ele_thick[ie] / ele_X0[ie];
229  }
230  return val;
231 }
232 //
233 G4double SamplingSection::getAbsorberdEdx()
234 {
235  double val = 0;
236  for (unsigned ie(0); ie < n_elements; ++ie) {
237  if (isAbsorberElement(ie))
238  val += ele_thick[ie] * ele_dEdx[ie];
239  }
240  return val;
241 }
242 
243 //
244 G4double SamplingSection::getAbsorberLambda()
245 {
246  double val = 0;
247  for (unsigned ie(0); ie < n_elements; ++ie) {
248  if (isAbsorberElement(ie) && ele_L0[ie] > 0)
249  val += ele_thick[ie] / ele_L0[ie];
250  }
251  return val;
252 }
253 
254 //
255 G4double SamplingSection::getAbsorbedEnergy()
256 {
257  double val = 0;
258  for (unsigned ie(0); ie < n_elements; ++ie) {
259  if (isAbsorberElement(ie)) val += ele_den[ie];
260  }
261  return val;
262 }
263 
264 //
265 G4double SamplingSection::getTotalEnergy()
266 {
267  double val = 0;
268  for (unsigned ie(0); ie < n_elements; ++ie) {
269  val += ele_den[ie];
270  }
271  return val;
272 }
273 
274 const G4SiHitVec & SamplingSection::getSiHitVec(const unsigned & idx) const
275 {
276  assert(sens_HitVec.size() > 0);
277  return sens_HitVec[idx];
278 }
279 
280 void SamplingSection::trackParticleHistory(const unsigned & idx,
281  const G4SiHitVec & incoming)
282 {
283  for (unsigned iP(0); iP < sens_HitVec[idx].size(); ++iP) { //loop on g4hits
284  G4int parId = sens_HitVec[idx][iP].parentId;
285  for (unsigned iI(0); iI < incoming.size(); ++iI) { //loop on previous layer
286  G4int trId = incoming[iI].trackId;
287  if (trId == parId)
288  sens_HitVec[idx][iP].parentId = incoming[iI].parentId;
289  }//loop on previous layer
290  }//loop on g4hits
291 }
std::map< std::string, double > dmap
Definition: TBGeometry.h:13
std::map< std::string, int > imap
Definition: TBGeometry.h:14
std::map< std::string, std::string > smap
Definition: TBGeometry.h:12