Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
JetTruthEval.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file JetTruthEval.cc
1 #include "JetTruthEval.h"
2 
3 #include "CaloTruthEval.h"
4 #include "SvtxTruthEval.h"
5 
6 #include <jetbase/Jet.h>
7 #include <jetbase/JetContainer.h>
8 
10 
11 #include <phool/getClass.h>
12 #include <phool/phool.h>
13 
14 #include <cassert>
15 #include <cstdlib>
16 #include <iostream>
17 #include <map>
18 #include <utility>
19 
21  const std::string& truthjetname)
22  : _truthjetname(truthjetname)
23  , _svtxevalstack(topNode)
24  , _cemcevalstack(topNode, "CEMC")
25  , _hcalinevalstack(topNode, "HCALIN")
26  , _hcaloutevalstack(topNode, "HCALOUT")
27  , _femcevalstack(topNode, "FEMC")
28  , _fhcalevalstack(topNode, "FHCAL")
29  , _eemcevalstack(topNode, "EEMC")
30 {
31  get_node_pointers(topNode);
32 }
33 
35 {
36  if (_verbosity > 0)
37  {
38  if ((_errors > 0) || (_verbosity > 1))
39  {
40  std::cout << "JetTruthEval::~JetTruthEval() - Error Count: " << _errors << std::endl;
41  }
42  }
43 }
44 
46 {
47  _svtxevalstack.next_event(topNode);
48  _cemcevalstack.next_event(topNode);
51  _femcevalstack.next_event(topNode);
52  _fhcalevalstack.next_event(topNode);
53 
56  _cache_all_truth_hits.clear();
57  _cache_get_truth_jet.clear();
58 
59  get_node_pointers(topNode);
60 }
61 
62 std::set<PHG4Particle*> JetTruthEval::all_truth_particles(Jet* truthjet)
63 {
64  if (_strict)
65  {
66  assert(truthjet);
67  }
68  else if (!truthjet)
69  {
70  ++_errors;
71  return std::set<PHG4Particle*>();
72  }
73 
74  if (_do_cache)
75  {
76  std::map<Jet*, std::set<PHG4Particle*> >::iterator iter =
77  _cache_all_truth_particles.find(truthjet);
78  if (iter != _cache_all_truth_particles.end())
79  {
80  return iter->second;
81  }
82  }
83 
84  std::set<PHG4Particle*> truth_particles;
85 
86  // loop over all the entries in the truthjet
87  /* for (Jet::ConstIter iter = truthjet->comp_begin(); */
88  /* iter != truthjet->comp_end(); */
89  /* ++iter) */
90  for (const auto& iter : truthjet->get_comp_vec()) // vector of pair<Jet::SRC, unsigned int>
91  {
92  Jet::SRC source = iter.first;
93  unsigned int index = iter.second;
94  if (source != Jet::PARTICLE)
95  {
96  std::cout << PHWHERE << " truth jet contains something other than particles!" << std::endl;
97  exit(-1);
98  }
99 
100  PHG4Particle* truth_particle = _truthinfo->GetParticle(index);
101 
102  if (_strict)
103  {
104  assert(truth_particle);
105  }
106  else if (!truth_particle)
107  {
108  ++_errors;
109  continue;
110  }
111 
112  truth_particles.insert(truth_particle);
113  }
114 
115  if (_do_cache)
116  {
117  _cache_all_truth_particles.insert(std::make_pair(truthjet, truth_particles));
118  }
119 
120  return truth_particles;
121 }
122 
123 std::set<PHG4Shower*> JetTruthEval::all_truth_showers(Jet* truthjet)
124 {
125  if (_strict)
126  {
127  assert(truthjet);
128  }
129  else if (!truthjet)
130  {
131  ++_errors;
132  return std::set<PHG4Shower*>();
133  }
134 
135  if (_do_cache)
136  {
137  std::map<Jet*, std::set<PHG4Shower*> >::iterator iter =
138  _cache_all_truth_showers.find(truthjet);
139  if (iter != _cache_all_truth_showers.end())
140  {
141  return iter->second;
142  }
143  }
144 
145  std::set<PHG4Shower*> truth_showers;
146 
147  std::set<PHG4Particle*> truth_particles = all_truth_particles(truthjet);
148 
149  // loop over all the entries in the truthjet
150  for (auto particle : truth_particles)
151  {
152  if (_strict)
153  {
154  assert(particle);
155  }
156  else if (!particle)
157  {
158  ++_errors;
159  continue;
160  }
161 
162  // any calo truth eval module would work here...
163  CaloTruthEval* cemc_truth_eval = _cemcevalstack.get_truth_eval();
164  PHG4Shower* shower = cemc_truth_eval->get_primary_shower(particle);
165  if (shower)
166  {
167  truth_showers.insert(shower);
168  }
169  }
170 
171  if (_do_cache)
172  {
173  _cache_all_truth_showers.insert(std::make_pair(truthjet, truth_showers));
174  }
175 
176  return truth_showers;
177 }
178 
179 std::set<PHG4Hit*> JetTruthEval::all_truth_hits(Jet* truthjet)
180 {
181  if (_strict)
182  {
183  assert(truthjet);
184  }
185  else if (!truthjet)
186  {
187  ++_errors;
188  return std::set<PHG4Hit*>();
189  }
190 
191  if (_do_cache)
192  {
193  std::map<Jet*, std::set<PHG4Hit*> >::iterator iter =
194  _cache_all_truth_hits.find(truthjet);
195  if (iter != _cache_all_truth_hits.end())
196  {
197  return iter->second;
198  }
199  }
200 
201  std::set<PHG4Hit*> truth_hits;
202 
203  std::set<PHG4Particle*> truth_particles = all_truth_particles(truthjet);
204 
205  // loop over all the entries in the truthjet
206  for (auto particle : truth_particles)
207  {
208  if (_strict)
209  {
210  assert(particle);
211  }
212  else if (!particle)
213  {
214  ++_errors;
215  continue;
216  }
217 
218  // ask the svtx truth eval to backtrack the particles to g4hits
219  SvtxTruthEval* svtx_truth_eval = _svtxevalstack.get_truth_eval();
220  std::set<PHG4Hit*> svtx_g4hits = svtx_truth_eval->all_truth_hits(particle);
221 
222  for (auto g4hit : svtx_g4hits)
223  {
224  if (_strict)
225  {
226  assert(g4hit);
227  }
228  else if (!g4hit)
229  {
230  ++_errors;
231  continue;
232  }
233 
234  truth_hits.insert(g4hit);
235  }
236  }
237 
238  if (_do_cache)
239  {
240  _cache_all_truth_hits.insert(std::make_pair(truthjet, truth_hits));
241  }
242 
243  return truth_hits;
244 }
245 
247 {
248  if (_strict)
249  {
250  assert(particle);
251  }
252  else if (!particle)
253  {
254  ++_errors;
255  return nullptr;
256  }
257 
258  if (_do_cache)
259  {
260  std::map<PHG4Particle*, Jet*>::iterator iter =
261  _cache_get_truth_jet.find(particle);
262  if (iter != _cache_get_truth_jet.end())
263  {
264  return iter->second;
265  }
266  }
267 
268  Jet* truth_jet = nullptr;
269 
270  // loop over all jets and look for this particle...
271  for (auto candidate : *_truthjets)
272  {
273  /* Jet* candidate = _truthjet.second; */
274 
275  // loop over all consituents and look for this particle
276  for (const std::pair<Jet::SRC, unsigned int>& jter
277  : candidate->get_comp_vec())
278  {
279  unsigned int index = jter.second;
280 
281  PHG4Particle* constituent = _truthinfo->GetParticle(index);
282  if (_strict)
283  {
284  assert(constituent);
285  }
286  else if (!constituent)
287  {
288  ++_errors;
289  continue;
290  }
291 
292  if (get_svtx_eval_stack()->get_truth_eval()->are_same_particle(constituent, particle))
293  {
294  truth_jet = candidate;
295  break;
296  }
297  }
298 
299  if (truth_jet)
300  {
301  break;
302  }
303  }
304 
305  if (_do_cache)
306  {
307  _cache_get_truth_jet.insert(std::make_pair(particle, truth_jet));
308  }
309 
310  return truth_jet;
311 }
312 
314 {
315  _truthinfo = findNode::getClass<PHG4TruthInfoContainer>(topNode, "G4TruthInfo");
316  if (!_truthinfo)
317  {
318  std::cout << PHWHERE << " ERROR: Can't find G4TruthInfo" << std::endl;
319  exit(-1);
320  }
321 
322  _truthjets = findNode::getClass<JetContainer>(topNode, _truthjetname);
323  if (!_truthjets)
324  {
325  std::cout << PHWHERE << " ERROR: Can't find " << _truthjetname << std::endl;
326  exit(-1);
327  }
328 
329  return;
330 }