EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CaloTruthEval.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file CaloTruthEval.cc
1 #include "CaloTruthEval.h"
2 
3 #include <phool/getClass.h>
4 
6 #include <g4main/PHG4HitDefs.h>
7 #include <g4main/PHG4Shower.h>
9 
10 #include <cassert>
11 #include <cmath>
12 #include <iostream>
13 #include <map>
14 #include <set>
15 #include <utility>
16 
17 using namespace std;
18 
19 CaloTruthEval::CaloTruthEval(PHCompositeNode* topNode, const std::string& caloname)
20  : _basetrutheval(topNode)
21  , _caloname(caloname)
22  , _caloid(PHG4HitDefs::get_volume_id(caloname))
23  , _truthinfo(nullptr)
24  , _g4hits(nullptr)
25  , _g4hit_container_id(-1)
26  , _strict(false)
27  , _verbosity(1)
28  , _errors(0)
29  , _do_cache(true)
30  , _cache_get_shower_energy_deposit()
31  , _cache_all_truth_hits_g4shower()
32  , _cache_all_truth_hits_g4particle()
33  , _cache_get_primary_particle_g4hit()
34  , _cache_get_shower_hits_from_primary()
35 {
36  get_node_pointers(topNode);
37 }
38 
40 {
41  if (_verbosity > 0)
42  {
43  if ((_errors > 0) || (_verbosity > 1))
44  {
45  cout << "CaloTruthEval::~CaloTruthEval() - Error Count: " << _errors << endl;
46  }
47  }
48 }
49 
51 {
57 
58  _basetrutheval.next_event(topNode);
59 
60  get_node_pointers(topNode);
61 }
62 
64 {
65  if (!_basetrutheval.has_reduced_node_pointers()) return false;
66 
67  if (_strict)
68  assert(_truthinfo);
69  else if (!_truthinfo)
70  return false;
71 
72  return true;
73 }
74 
76 {
77  return _basetrutheval.get_primary_shower(shower);
78 }
79 
81 {
82  return _basetrutheval.get_primary_shower(particle);
83 }
84 
86 {
87  return _basetrutheval.get_primary_particle(shower);
88 }
89 
91 {
92  return _basetrutheval.get_primary_particle(particle);
93 }
94 
96 {
97  return _basetrutheval.get_embed(particle);
98 }
99 
101 {
102  return _basetrutheval.get_vertex(particle);
103 }
104 
106 {
107  return _basetrutheval.are_same_shower(s1, s2);
108 }
109 
111 {
112  return _basetrutheval.are_same_particle(p1, p2);
113 }
114 
116 {
117  return _basetrutheval.are_same_vertex(vtx1, vtx2);
118 }
119 
121 {
122  return _basetrutheval.is_primary(shower);
123 }
124 
126 {
127  return _basetrutheval.is_primary(particle);
128 }
129 
131 {
133  {
134  ++_errors;
135  return NAN;
136  }
137 
138  if (_strict)
139  {
140  assert(primary);
141  }
142  else if (!primary)
143  {
144  ++_errors;
145  return NAN;
146  }
147 
148  if (!is_primary(primary)) return NAN;
149 
150  primary = get_primary_particle(primary);
151 
152  if (_strict)
153  {
154  assert(primary);
155  }
156  else if (!primary)
157  {
158  ++_errors;
159  return NAN;
160  }
161 
162  if (_do_cache)
163  {
164  std::map<PHG4Particle*, float>::iterator iter =
165  _cache_get_shower_energy_deposit.find(primary);
166  if (iter != _cache_get_shower_energy_deposit.end())
167  {
168  return iter->second;
169  }
170  }
171 
172  float shower_e = 0.0;
173  PHG4Shower* shower = get_primary_shower(primary);
174  if (shower) shower_e = shower->get_edep(_g4hit_container_id);
175 
176  if (_do_cache) _cache_get_shower_energy_deposit.insert(make_pair(primary, shower_e));
177 
178  return shower_e;
179 }
180 
182 {
183  if (!has_full_node_pointers())
184  {
185  ++_errors;
186  return NAN;
187  }
188 
189  if (_strict)
190  {
191  assert(primary);
192  }
193  else if (!primary)
194  {
195  ++_errors;
196  return NAN;
197  }
198 
199  if (!is_primary(primary)) return NAN;
200 
201  PHG4Shower* shower = get_primary_shower(primary);
202  if (!shower) return 0.0;
203 
204  float ratio = shower->get_eh_ratio(get_caloid());
205 
206  return ratio;
207 }
208 
210 {
211  if (!_basetrutheval.has_full_node_pointers()) return false;
212 
213  if (_strict)
214  assert(_truthinfo);
215  else if (!_truthinfo)
216  return false;
217 
218  if (_strict) assert(_g4hits);
219  if (!_g4hits) return false;
220 
221  return true;
222 }
223 
224 std::set<PHG4Hit*> CaloTruthEval::all_truth_hits(PHG4Shower* shower)
225 {
226  if (!has_full_node_pointers())
227  {
228  ++_errors;
229  return std::set<PHG4Hit*>();
230  }
231 
232  if (_strict)
233  {
234  assert(shower);
235  }
236  else if (!shower)
237  {
238  ++_errors;
239  return std::set<PHG4Hit*>();
240  }
241  else if (!_g4hits)
242  {
243  ++_errors;
244  return std::set<PHG4Hit*>();
245  }
246 
247  if (_do_cache)
248  {
249  std::map<PHG4Shower*, std::set<PHG4Hit*> >::iterator iter =
250  _cache_all_truth_hits_g4shower.find(shower);
251  if (iter != _cache_all_truth_hits_g4shower.end())
252  {
253  return iter->second;
254  }
255  }
256 
257  std::set<PHG4Hit*> truth_hits;
258 
259  // loop over all g4hits on the shower
261  if (iter != shower->end_g4hit_id()) return truth_hits;
262 
263  for (std::set<PHG4HitDefs::keytype>::iterator jter = iter->second.begin();
264  jter != iter->second.end();
265  ++jter)
266  {
267  PHG4Hit* g4hit = _g4hits->findHit(*jter);
268 
269  if (_strict)
270  assert(g4hit);
271  else if (!g4hit)
272  {
273  ++_errors;
274  }
275 
276  if (g4hit) truth_hits.insert(g4hit);
277  }
278 
279  if (_do_cache) _cache_all_truth_hits_g4shower.insert(make_pair(shower, truth_hits));
280 
281  return truth_hits;
282 }
283 
285 {
286  if (!has_full_node_pointers())
287  {
288  ++_errors;
289  return std::set<PHG4Hit*>();
290  }
291 
292  if (_strict)
293  {
294  assert(particle);
295  }
296  else if (!particle)
297  {
298  ++_errors;
299  return std::set<PHG4Hit*>();
300  }
301  if (!_g4hits)
302  {
303  ++_errors;
304  return std::set<PHG4Hit*>();
305  }
306 
307  if (_do_cache)
308  {
309  std::map<PHG4Particle*, std::set<PHG4Hit*> >::iterator iter =
310  _cache_all_truth_hits_g4particle.find(particle);
311  if (iter != _cache_all_truth_hits_g4particle.end())
312  {
313  return iter->second;
314  }
315  }
316 
317  std::set<PHG4Hit*> truth_hits;
318 
319  // loop over all the g4hits
320  for (PHG4HitContainer::ConstIterator g4iter = _g4hits->getHits().first;
321  g4iter != _g4hits->getHits().second;
322  ++g4iter)
323  {
324  PHG4Hit* g4hit = g4iter->second;
325  if (is_g4hit_from_particle(g4hit, particle)) continue;
326  truth_hits.insert(g4hit);
327  }
328 
329  if (_do_cache) _cache_all_truth_hits_g4particle.insert(make_pair(particle, truth_hits));
330 
331  return truth_hits;
332 }
333 
335 {
336  return _basetrutheval.get_particle(g4hit);
337 }
338 
340 {
341  if (!has_full_node_pointers())
342  {
343  ++_errors;
344  return nullptr;
345  }
346 
347  if (_strict)
348  {
349  assert(g4hit);
350  }
351  else if (!g4hit)
352  {
353  ++_errors;
354  return nullptr;
355  }
356 
357  if (_do_cache)
358  {
359  std::map<PHG4Hit*, PHG4Particle*>::iterator iter =
361  if (iter != _cache_get_primary_particle_g4hit.end())
362  {
363  return iter->second;
364  }
365  }
366 
368 
369  if (_do_cache) _cache_get_primary_particle_g4hit.insert(make_pair(g4hit, primary));
370 
371  if (_strict)
372  {
373  assert(primary);
374  }
375  else if (!primary)
376  {
377  ++_errors;
378  }
379 
380  return primary;
381 }
382 
384 {
385  return _basetrutheval.is_g4hit_from_particle(g4hit, particle);
386 }
387 
389 {
390  if (!has_full_node_pointers())
391  {
392  ++_errors;
393  return std::set<PHG4Hit*>();
394  }
395 
396  if (_strict)
397  {
398  assert(primary);
399  }
400  else if (!primary)
401  {
402  ++_errors;
403  return std::set<PHG4Hit*>();
404  }
405 
406  if (!is_primary(primary)) return std::set<PHG4Hit*>();
407 
408  primary = get_primary_particle(primary);
409 
410  if (_strict)
411  {
412  assert(primary);
413  }
414  else if (!primary)
415  {
416  ++_errors;
417  return std::set<PHG4Hit*>();
418  }
419 
420  if (_do_cache)
421  {
422  std::map<PHG4Particle*, std::set<PHG4Hit*> >::iterator iter =
424  if (iter != _cache_get_shower_hits_from_primary.end())
425  {
426  return iter->second;
427  }
428  }
429 
430  std::set<PHG4Hit*> truth_hits;
431 
432  PHG4Shower* shower = get_primary_shower(primary);
433  if (shower) truth_hits = all_truth_hits(shower);
434 
435  if (_do_cache) _cache_get_shower_hits_from_primary.insert(make_pair(primary, truth_hits));
436 
437  return truth_hits;
438 }
439 
441 {
442  // need things off of the DST...
443  _truthinfo = findNode::getClass<PHG4TruthInfoContainer>(topNode, "G4TruthInfo");
444 
445  std::string name = "G4HIT_" + _caloname;
446  _g4hits = findNode::getClass<PHG4HitContainer>(topNode, name.c_str());
448 
449  return;
450 }