Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
JetRecoEval.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file JetRecoEval.cc
1 #include "JetRecoEval.h"
2 
3 #include "CaloEvalStack.h"
4 #include "CaloRawClusterEval.h"
5 #include "CaloRawTowerEval.h" // for CaloRawTowerEval
6 #include "JetTruthEval.h"
7 #include "SvtxEvalStack.h"
8 #include "SvtxTrackEval.h"
9 
10 #include <calobase/RawCluster.h>
11 #include <calobase/RawClusterContainer.h>
12 #include <calobase/RawTower.h>
13 #include <calobase/RawTowerContainer.h>
14 
15 #include <jetbase/Jet.h>
16 #include <jetbase/JetContainer.h>
17 
18 #include <g4main/PHG4Particle.h>
19 
22 
23 #include <phool/getClass.h>
24 #include <phool/phool.h>
25 
26 #include <cassert>
27 #include <cfloat>
28 #include <cmath>
29 #include <cstdlib>
30 #include <iostream>
31 #include <map>
32 #include <set>
33 #include <string>
34 
36  const std::string& recojetname,
37  const std::string& truthjetname)
38  : _jettrutheval(topNode, truthjetname)
39  , _recojetname(recojetname)
40  , _truthjetname(truthjetname)
41 {
42  get_node_pointers(topNode);
43 }
44 
46 {
47  if (_verbosity > 0)
48  {
49  if ((_errors > 0) || (_verbosity > 1))
50  {
51  std::cout << "JetRecoEval::~JetRecoEval() - Error Count: " << _errors << std::endl;
52  }
53  }
54 }
55 
57 {
60  _cache_all_truth_jets.clear();
62  _cache_all_jets_from.clear();
63  _cache_best_jet_from.clear();
66  _cache_all_truth_hits.clear();
67 
68  _jettrutheval.next_event(topNode);
69 
70  get_node_pointers(topNode);
71 }
72 
74 {
77 }
78 
79 std::set<PHG4Shower*> JetRecoEval::all_truth_showers(Jet* recojet)
80 {
81  if (_strict)
82  {
83  assert(recojet);
84  }
85  else if (!recojet)
86  {
87  ++_errors;
88  return std::set<PHG4Shower*>();
89  }
90 
91  if (_do_cache)
92  {
93  std::map<Jet*, std::set<PHG4Shower*> >::iterator iter =
94  _cache_all_truth_showers.find(recojet);
95  if (iter != _cache_all_truth_showers.end())
96  {
97  return iter->second;
98  }
99  }
100 
101  std::set<PHG4Shower*> truth_showers;
102 
103  // loop over all the jet constituents, backtrack each reco object to the
104  // truth hits and combine with other consituents
105  for (auto jter : recojet->get_comp_vec())
106  {
107  Jet::SRC source = jter.first;
108  unsigned int index = jter.second;
109 
110  std::set<PHG4Shower*> new_showers;
111 
112  if (source == Jet::TRACK)
113  {
114  if (!_trackmap)
115  {
116  std::cout << PHWHERE << "ERROR: can't find SvtxTrackMap" << std::endl;
117  exit(-1);
118  }
119 
120  // SvtxTrack* track = _trackmap->get(index);
121 
122  // if (_strict) {assert(track);}
123  // else if (!track) {++_errors; continue;}
124 
125  // new_showers = get_svtx_eval_stack()->get_track_eval()->all_truth_showers(track);
126  }
127 
128  else if (source == Jet::CEMC_TOWER)
129  {
130  if (!_cemctowers)
131  {
132  std::cout << PHWHERE << "ERROR: can't find TOWER_CEMC" << std::endl;
133  exit(-1);
134  }
135 
136  RawTower* tower = _cemctowers->getTower(index);
137 
138  if (_strict)
139  {
140  assert(tower);
141  }
142  else if (!tower)
143  {
144  ++_errors;
145  continue;
146  }
147 
149  }
150  else if (source == Jet::CEMC_CLUSTER)
151  {
152  if (!_cemcclusters)
153  {
154  std::cout << PHWHERE << "ERROR: can't find CLUSTER_CEMC" << std::endl;
155  exit(-1);
156  }
157 
158  RawCluster* cluster = _cemcclusters->getCluster(index);
159 
160  if (_strict)
161  {
162  assert(cluster);
163  }
164  else if (!cluster)
165  {
166  ++_errors;
167  continue;
168  }
169 
171  }
172  else if (source == Jet::EEMC_TOWER)
173  {
174  if (!_eemctowers)
175  {
176  std::cout << PHWHERE << "ERROR: can't find TOWER_EEMC" << std::endl;
177  exit(-1);
178  }
179 
180  RawTower* tower = _eemctowers->getTower(index);
181 
182  if (_strict)
183  {
184  assert(tower);
185  }
186  else if (!tower)
187  {
188  ++_errors;
189  continue;
190  }
191 
193  }
194  else if (source == Jet::EEMC_CLUSTER)
195  {
196  if (!_eemcclusters)
197  {
198  std::cout << PHWHERE << "ERROR: can't find CLUSTER_EEMC" << std::endl;
199  exit(-1);
200  }
201 
202  RawCluster* cluster = _eemcclusters->getCluster(index);
203 
204  if (_strict)
205  {
206  assert(cluster);
207  }
208  else if (!cluster)
209  {
210  ++_errors;
211  continue;
212  }
213 
215  }
216  else if (source == Jet::HCALIN_TOWER)
217  {
218  if (!_hcalintowers)
219  {
220  std::cout << PHWHERE << "ERROR: can't find TOWER_HCALIN" << std::endl;
221  exit(-1);
222  }
223 
225 
226  if (_strict)
227  {
228  assert(tower);
229  }
230  else if (!tower)
231  {
232  ++_errors;
233  continue;
234  }
235 
237  }
238  else if (source == Jet::HCALIN_CLUSTER)
239  {
240  if (!_hcalinclusters)
241  {
242  std::cout << PHWHERE << "ERROR: can't find CLUSTER_HCALIN" << std::endl;
243  exit(-1);
244  }
245 
246  RawCluster* cluster = _hcalinclusters->getCluster(index);
247 
248  if (_strict)
249  {
250  assert(cluster);
251  }
252  else if (!cluster)
253  {
254  ++_errors;
255  continue;
256  }
257 
259  }
260  else if (source == Jet::HCALOUT_TOWER)
261  {
262  if (!_hcalouttowers)
263  {
264  std::cout << PHWHERE << "ERROR: can't find TOWER_HCALOUT" << std::endl;
265  exit(-1);
266  }
267 
269 
270  if (_strict)
271  {
272  assert(tower);
273  }
274  else if (!tower)
275  {
276  ++_errors;
277  continue;
278  }
279 
281  }
282  else if (source == Jet::HCALOUT_CLUSTER)
283  {
284  if (!_hcaloutclusters)
285  {
286  std::cout << PHWHERE << "ERROR: can't find CLUSTER_HCALOUT" << std::endl;
287  exit(-1);
288  }
289 
290  RawCluster* cluster = _hcaloutclusters->getCluster(index);
291 
292  if (_strict)
293  {
294  assert(cluster);
295  }
296  else if (!cluster)
297  {
298  ++_errors;
299  continue;
300  }
301 
303  }
304  else if (source == Jet::FEMC_TOWER)
305  {
306  if (!_femctowers)
307  {
308  std::cout << PHWHERE << "ERROR: can't find TOWER_FEMC" << std::endl;
309  exit(-1);
310  }
311 
312  RawTower* tower = _femctowers->getTower(index);
313 
314  if (_strict)
315  {
316  assert(tower);
317  }
318  else if (!tower)
319  {
320  ++_errors;
321  continue;
322  }
323 
325  }
326  else if (source == Jet::FEMC_CLUSTER)
327  {
328  if (!_femcclusters)
329  {
330  std::cout << PHWHERE << "ERROR: can't find CLUSTER_FEMC" << std::endl;
331  exit(-1);
332  }
333 
334  RawCluster* cluster = _femcclusters->getCluster(index);
335 
336  if (_strict)
337  {
338  assert(cluster);
339  }
340  else if (!cluster)
341  {
342  ++_errors;
343  continue;
344  }
345 
347  }
348  else if (source == Jet::FHCAL_TOWER)
349  {
350  if (!_fhcaltowers)
351  {
352  std::cout << PHWHERE << "ERROR: can't find TOWER_FHCAL" << std::endl;
353  exit(-1);
354  }
355 
356  RawTower* tower = _fhcaltowers->getTower(index);
357 
358  if (_strict)
359  {
360  assert(tower);
361  }
362  else if (!tower)
363  {
364  ++_errors;
365  continue;
366  }
367 
369  }
370  else if (source == Jet::FHCAL_CLUSTER)
371  {
372  if (!_fhcalclusters)
373  {
374  std::cout << PHWHERE << "ERROR: can't find CLUSTER_FHCAL" << std::endl;
375  exit(-1);
376  }
377 
378  RawCluster* cluster = _fhcalclusters->getCluster(index);
379 
380  if (_strict)
381  {
382  assert(cluster);
383  }
384  else if (!cluster)
385  {
386  ++_errors;
387  continue;
388  }
389 
391  }
392 
393  for (auto new_shower : new_showers)
394  {
395  truth_showers.insert(new_shower);
396  }
397  }
398 
399  if (_do_cache)
400  {
401  _cache_all_truth_showers.insert(std::make_pair(recojet, truth_showers));
402  }
403 
404  return truth_showers;
405 }
406 
407 std::set<PHG4Particle*> JetRecoEval::all_truth_particles(Jet* recojet)
408 {
409  if (_strict)
410  {
411  assert(recojet);
412  }
413  else if (!recojet)
414  {
415  ++_errors;
416  return std::set<PHG4Particle*>();
417  }
418 
419  if (_do_cache)
420  {
421  std::map<Jet*, std::set<PHG4Particle*> >::iterator iter =
422  _cache_all_truth_particles.find(recojet);
423  if (iter != _cache_all_truth_particles.end())
424  {
425  return iter->second;
426  }
427  }
428 
429  std::set<PHG4Particle*> truth_particles;
430 
431  // loop over all the jet constituents, backtrack each reco object to the
432  // truth hits and combine with other consituents
433  for (auto jter : recojet->get_comp_vec())
434  {
435  Jet::SRC source = jter.first;
436  unsigned int index = jter.second;
437 
438  std::set<PHG4Particle*> new_particles;
439 
440  if (source == Jet::TRACK)
441  {
442  if (!_trackmap)
443  {
444  std::cout << PHWHERE << "ERROR: can't find TrackMap" << std::endl;
445  exit(-1);
446  }
447 
448  SvtxTrack* track = _trackmap->get(index);
449 
450  if (_strict)
451  {
452  assert(track);
453  }
454  else if (!track)
455  {
456  ++_errors;
457  continue;
458  }
459 
460  new_particles = get_svtx_eval_stack()->get_track_eval()->all_truth_particles(track);
461  }
462  else if (source == Jet::CEMC_TOWER)
463  {
464  if (!_cemctowers)
465  {
466  std::cout << PHWHERE << "ERROR: can't find TOWER_CEMC" << std::endl;
467  exit(-1);
468  }
469 
470  RawTower* tower = _cemctowers->getTower(index);
471 
472  if (_strict)
473  {
474  assert(tower);
475  }
476  else if (!tower)
477  {
478  ++_errors;
479  continue;
480  }
481 
483  }
484  else if (source == Jet::CEMC_CLUSTER)
485  {
486  if (!_cemcclusters)
487  {
488  std::cout << PHWHERE << "ERROR: can't find CLUSTER_CEMC" << std::endl;
489  exit(-1);
490  }
491 
492  RawCluster* cluster = _cemcclusters->getCluster(index);
493 
494  if (_strict)
495  {
496  assert(cluster);
497  }
498  else if (!cluster)
499  {
500  ++_errors;
501  continue;
502  }
503 
505  }
506  else if (source == Jet::EEMC_TOWER)
507  {
508  if (!_eemctowers)
509  {
510  std::cout << PHWHERE << "ERROR: can't find TOWER_EEMC" << std::endl;
511  exit(-1);
512  }
513 
514  RawTower* tower = _eemctowers->getTower(index);
515 
516  if (_strict)
517  {
518  assert(tower);
519  }
520  else if (!tower)
521  {
522  ++_errors;
523  continue;
524  }
525 
527  }
528  else if (source == Jet::EEMC_CLUSTER)
529  {
530  if (!_eemcclusters)
531  {
532  std::cout << PHWHERE << "ERROR: can't find CLUSTER_EEMC" << std::endl;
533  exit(-1);
534  }
535 
536  RawCluster* cluster = _eemcclusters->getCluster(index);
537 
538  if (_strict)
539  {
540  assert(cluster);
541  }
542  else if (!cluster)
543  {
544  ++_errors;
545  continue;
546  }
547 
549  }
550  else if (source == Jet::HCALIN_TOWER)
551  {
552  if (!_hcalintowers)
553  {
554  std::cout << PHWHERE << "ERROR: can't find TOWER_HCALIN" << std::endl;
555  exit(-1);
556  }
557 
559 
560  if (_strict)
561  {
562  assert(tower);
563  }
564  else if (!tower)
565  {
566  ++_errors;
567  continue;
568  }
569 
571  }
572  else if (source == Jet::HCALIN_CLUSTER)
573  {
574  if (!_hcalinclusters)
575  {
576  std::cout << PHWHERE << "ERROR: can't find CLUSTER_HCALIN" << std::endl;
577  exit(-1);
578  }
579 
580  RawCluster* cluster = _hcalinclusters->getCluster(index);
581 
582  if (_strict)
583  {
584  assert(cluster);
585  }
586  else if (!cluster)
587  {
588  ++_errors;
589  continue;
590  }
591 
593  }
594  else if (source == Jet::HCALOUT_TOWER)
595  {
596  if (!_hcalouttowers)
597  {
598  std::cout << PHWHERE << "ERROR: can't find TOWER_HCALOUT" << std::endl;
599  exit(-1);
600  }
601 
603 
604  if (_strict)
605  {
606  assert(tower);
607  }
608  else if (!tower)
609  {
610  ++_errors;
611  continue;
612  }
613 
615  }
616  else if (source == Jet::HCALOUT_CLUSTER)
617  {
618  if (!_hcaloutclusters)
619  {
620  std::cout << PHWHERE << "ERROR: can't find CLUSTER_HCALOUT" << std::endl;
621  exit(-1);
622  }
623 
624  RawCluster* cluster = _hcaloutclusters->getCluster(index);
625 
626  if (_strict)
627  {
628  assert(cluster);
629  }
630  else if (!cluster)
631  {
632  ++_errors;
633  continue;
634  }
635 
637  }
638  else if (source == Jet::FEMC_TOWER)
639  {
640  if (!_femctowers)
641  {
642  std::cout << PHWHERE << "ERROR: can't find TOWER_FEMC" << std::endl;
643  exit(-1);
644  }
645 
646  RawTower* tower = _femctowers->getTower(index);
647 
648  if (_strict)
649  {
650  assert(tower);
651  }
652  else if (!tower)
653  {
654  ++_errors;
655  continue;
656  }
657 
659  }
660  else if (source == Jet::FEMC_CLUSTER)
661  {
662  if (!_femcclusters)
663  {
664  std::cout << PHWHERE << "ERROR: can't find CLUSTER_FEMC" << std::endl;
665  exit(-1);
666  }
667 
668  RawCluster* cluster = _femcclusters->getCluster(index);
669 
670  if (_strict)
671  {
672  assert(cluster);
673  }
674  else if (!cluster)
675  {
676  ++_errors;
677  continue;
678  }
679 
681  }
682  else if (source == Jet::FHCAL_TOWER)
683  {
684  if (!_fhcaltowers)
685  {
686  std::cout << PHWHERE << "ERROR: can't find TOWER_FHCAL" << std::endl;
687  exit(-1);
688  }
689 
690  RawTower* tower = _fhcaltowers->getTower(index);
691 
692  if (_strict)
693  {
694  assert(tower);
695  }
696  else if (!tower)
697  {
698  ++_errors;
699  continue;
700  }
701 
703  }
704  else if (source == Jet::FHCAL_CLUSTER)
705  {
706  if (!_fhcalclusters)
707  {
708  std::cout << PHWHERE << "ERROR: can't find CLUSTER_FHCAL" << std::endl;
709  exit(-1);
710  }
711 
712  RawCluster* cluster = _fhcalclusters->getCluster(index);
713 
714  if (_strict)
715  {
716  assert(cluster);
717  }
718  else if (!cluster)
719  {
720  ++_errors;
721  continue;
722  }
723 
725  }
726 
727  for (auto new_particle : new_particles)
728  {
729  truth_particles.insert(new_particle);
730  }
731  }
732 
733  if (_do_cache)
734  {
735  _cache_all_truth_particles.insert(std::make_pair(recojet, truth_particles));
736  }
737 
738  return truth_particles;
739 }
740 
741 std::set<Jet*> JetRecoEval::all_truth_jets(Jet* recojet)
742 {
743  if (_strict)
744  {
745  assert(recojet);
746  }
747  else if (!recojet)
748  {
749  ++_errors;
750  return std::set<Jet*>();
751  }
752 
753  if (_do_cache)
754  {
755  std::map<Jet*, std::set<Jet*> >::iterator iter =
756  _cache_all_truth_jets.find(recojet);
757  if (iter != _cache_all_truth_jets.end())
758  {
759  return iter->second;
760  }
761  }
762 
763  std::set<Jet*> truth_jets;
764 
765  // get all truth particles (this can include muons and other truth excludes)...
766  std::set<PHG4Particle*> particles = all_truth_particles(recojet);
767 
768  // backtrack from the truth particles to the truth jets...
769  for (auto particle : particles)
770  {
771  if (_strict)
772  {
773  assert(particle);
774  }
775  else if (!particle)
776  {
777  ++_errors;
778  continue;
779  }
780 
781  Jet* truth_jet = _jettrutheval.get_truth_jet(particle);
782  if (!truth_jet)
783  {
784  continue;
785  }
786 
787  truth_jets.insert(truth_jet);
788  }
789 
790  if (_do_cache)
791  {
792  _cache_all_truth_jets.insert(std::make_pair(recojet, truth_jets));
793  }
794 
795  return truth_jets;
796 }
797 
799 {
800  if (_strict)
801  {
802  assert(recojet);
803  }
804  else if (!recojet)
805  {
806  ++_errors;
807  return nullptr;
808  }
809 
810  if (_do_cache)
811  {
812  std::map<Jet*, Jet*>::iterator iter =
813  _cache_max_truth_jet_by_energy.find(recojet);
814  if (iter != _cache_max_truth_jet_by_energy.end())
815  {
816  return iter->second;
817  }
818  }
819 
820  Jet* truthjet = nullptr;
821  float max_energy = FLT_MAX * -1.0;
822 
823  std::set<Jet*> truthjets = all_truth_jets(recojet);
824  for (auto candidate : truthjets)
825  {
826  if (_strict)
827  {
828  assert(candidate);
829  }
830  else if (!candidate)
831  {
832  ++_errors;
833  continue;
834  }
835 
836  float energy = get_energy_contribution(recojet, candidate);
837  if (energy > max_energy)
838  {
839  truthjet = candidate;
840  max_energy = energy;
841  }
842  }
843 
844  if (_do_cache)
845  {
846  _cache_max_truth_jet_by_energy.insert(std::make_pair(recojet, truthjet));
847  }
848 
849  return truthjet;
850 }
851 
852 std::set<Jet*> JetRecoEval::all_jets_from(Jet* truthjet)
853 {
854  if (_strict)
855  {
856  assert(truthjet);
857  }
858  else if (!truthjet)
859  {
860  ++_errors;
861  return std::set<Jet*>();
862  }
863 
864  if (_do_cache)
865  {
866  std::map<Jet*, std::set<Jet*> >::iterator iter =
867  _cache_all_jets_from.find(truthjet);
868  if (iter != _cache_all_jets_from.end())
869  {
870  return iter->second;
871  }
872  }
873 
874  std::set<Jet*> recojets;
875 
876  // loop over all reco jets
877  for (auto recojet : *_recojets)
878  {
879  /* Jet* recojet = _recojet.second; */
880 
881  // if this jet back tracks to the truth jet
882  std::set<Jet*> truthcandidates = all_truth_jets(recojet);
883  for (auto truthcandidate : truthcandidates)
884  {
885  if (_strict)
886  {
887  assert(truthcandidate);
888  }
889  else if (!truthcandidate)
890  {
891  ++_errors;
892  continue;
893  }
894 
895  if (truthcandidate->get_id() == truthjet->get_id())
896  {
897  recojets.insert(recojet);
898  }
899  }
900  }
901 
902  if (_do_cache)
903  {
904  _cache_all_jets_from.insert(std::make_pair(truthjet, recojets));
905  }
906 
907  return recojets;
908 }
909 
911 {
912 
913  if (_strict)
914  {
915  assert(truthjet);
916  }
917  else if (!truthjet)
918  {
919  ++_errors;
920  return nullptr;
921  }
922 
923  if (_do_cache)
924  {
925  std::map<Jet*, Jet*>::iterator iter =
926  _cache_best_jet_from.find(truthjet);
927  if (iter != _cache_best_jet_from.end())
928  {
929  return iter->second;
930  }
931  }
932 
933  Jet* bestrecojet = nullptr;
934  float max_energy = FLT_MAX * -1.0;
935 
936  std::set<Jet*> recojets = all_jets_from(truthjet);
937  for (auto recojet : recojets)
938  {
939  if (_strict)
940  {
941  assert(recojet);
942  }
943  else if (!recojet)
944  {
945  ++_errors;
946  continue;
947  }
948 
949  float energy = get_energy_contribution(recojet, truthjet);
950  if (energy > max_energy)
951  {
952  bestrecojet = recojet;
953  max_energy = energy;
954  }
955  }
956 
957  if (_do_cache)
958  {
959  _cache_best_jet_from.insert(std::make_pair(truthjet, bestrecojet));
960  }
961  return bestrecojet;
962 }
963 
965 {
966  if (_strict)
967  {
968  assert(truthjet);
969  }
970  else if (!truthjet)
971  {
972  ++_errors;
973  return nullptr;
974  }
975 
976  Jet* recojet = best_jet_from(truthjet);
977 
978  if (recojet)
979  {
980  Jet* back_matching = max_truth_jet_by_energy(recojet);
981 
982  if (back_matching->get_id() == truthjet->get_id())
983  {
984  return recojet; // uniquely matched
985  }
986  else
987  {
988  return nullptr;
989  }
990  }
991  else
992  {
993  return nullptr;
994  }
995 }
996 
998 {
999  if (_strict)
1000  {
1001  assert(recojet);
1002  }
1003  else if (!recojet)
1004  {
1005  ++_errors;
1006  return nullptr;
1007  }
1008 
1009  Jet* truthjet = max_truth_jet_by_energy(recojet);
1010 
1011  if (truthjet)
1012  {
1013  Jet* back_matching = best_jet_from(truthjet);
1014 
1015  if (back_matching->get_id() == recojet->get_id())
1016  {
1017  return truthjet; // uniquely matched
1018  }
1019  else
1020  {
1021  return nullptr;
1022  }
1023  }
1024  else
1025  {
1026  return nullptr;
1027  }
1028 }
1029 
1030 // overlap calculations
1032 {
1033  if (_strict)
1034  {
1035  assert(recojet);
1036  assert(truthjet);
1037  }
1038  else if (!recojet || !truthjet)
1039  {
1040  ++_errors;
1041  return NAN;
1042  }
1043 
1044  if (_do_cache)
1045  {
1046  std::map<std::pair<Jet*, Jet*>, float>::iterator iter =
1047  _cache_get_energy_contribution.find(std::make_pair(recojet, truthjet));
1048  if (iter != _cache_get_energy_contribution.end())
1049  {
1050  return iter->second;
1051  }
1052  }
1053 
1054  float energy_contribution = 0.0;
1055 
1056  std::set<PHG4Particle*> truthjetcomp = get_truth_eval()->all_truth_particles(truthjet);
1057  // loop over all truthjet constituents
1058  for (auto truthparticle : truthjetcomp)
1059  {
1060  if (_strict)
1061  {
1062  assert(truthparticle);
1063  }
1064  else if (!truthparticle)
1065  {
1066  ++_errors;
1067  continue;
1068  }
1069 
1070  for (auto jter : recojet->get_comp_vec())
1071  {
1072  Jet::SRC source = jter.first;
1073  unsigned int index = jter.second;
1074 
1075  float energy = 0.0;
1076 
1077  if (source == Jet::TRACK)
1078  {
1079  SvtxTrack* track = _trackmap->get(index);
1080 
1081  if (_strict)
1082  {
1083  assert(track);
1084  }
1085  else if (!track)
1086  {
1087  ++_errors;
1088  continue;
1089  }
1090 
1092 
1093  if (maxtruthparticle == nullptr)
1094  {
1095  // in extreme rare cases, noise hits can make a track with no maxtruthparticle matched
1096  energy = 0;
1097  }
1098  else if (maxtruthparticle->get_track_id() == truthparticle->get_track_id())
1099  {
1100  energy = track->get_p();
1101  }
1102  }
1103  else if (source == Jet::CEMC_TOWER)
1104  {
1105  RawTower* tower = _cemctowers->getTower(index);
1106 
1107  if (_strict)
1108  {
1109  assert(tower);
1110  }
1111  else if (!tower)
1112  {
1113  ++_errors;
1114  continue;
1115  }
1116 
1117  energy = get_cemc_eval_stack()->get_rawtower_eval()->get_energy_contribution(tower, truthparticle);
1118  }
1119  else if (source == Jet::CEMC_CLUSTER)
1120  {
1121  RawCluster* cluster = _cemcclusters->getCluster(index);
1122 
1123  if (_strict)
1124  {
1125  assert(cluster);
1126  }
1127  else if (!cluster)
1128  {
1129  ++_errors;
1130  continue;
1131  }
1132 
1133  energy = get_cemc_eval_stack()->get_rawcluster_eval()->get_energy_contribution(cluster, truthparticle);
1134  }
1135  else if (source == Jet::EEMC_TOWER)
1136  {
1137  RawTower* tower = _eemctowers->getTower(index);
1138 
1139  if (_strict)
1140  {
1141  assert(tower);
1142  }
1143  else if (!tower)
1144  {
1145  ++_errors;
1146  continue;
1147  }
1148 
1149  energy = get_eemc_eval_stack()->get_rawtower_eval()->get_energy_contribution(tower, truthparticle);
1150  }
1151  else if (source == Jet::EEMC_CLUSTER)
1152  {
1153  RawCluster* cluster = _eemcclusters->getCluster(index);
1154 
1155  if (_strict)
1156  {
1157  assert(cluster);
1158  }
1159  else if (!cluster)
1160  {
1161  ++_errors;
1162  continue;
1163  }
1164 
1165  energy = get_eemc_eval_stack()->get_rawcluster_eval()->get_energy_contribution(cluster, truthparticle);
1166  }
1167  else if (source == Jet::HCALIN_TOWER)
1168  {
1169  RawTower* tower = _hcalintowers->getTower(index);
1170 
1171  if (_strict)
1172  {
1173  assert(tower);
1174  }
1175  else if (!tower)
1176  {
1177  ++_errors;
1178  continue;
1179  }
1180 
1181  energy = get_hcalin_eval_stack()->get_rawtower_eval()->get_energy_contribution(tower, truthparticle);
1182  }
1183  else if (source == Jet::HCALIN_CLUSTER)
1184  {
1185  RawCluster* cluster = _hcalinclusters->getCluster(index);
1186 
1187  if (_strict)
1188  {
1189  assert(cluster);
1190  }
1191  else if (!cluster)
1192  {
1193  ++_errors;
1194  continue;
1195  }
1196 
1197  energy = get_hcalin_eval_stack()->get_rawcluster_eval()->get_energy_contribution(cluster, truthparticle);
1198  }
1199  else if (source == Jet::HCALOUT_TOWER)
1200  {
1202 
1203  if (_strict)
1204  {
1205  assert(tower);
1206  }
1207  else if (!tower)
1208  {
1209  ++_errors;
1210  continue;
1211  }
1212 
1213  energy = get_hcalout_eval_stack()->get_rawtower_eval()->get_energy_contribution(tower, truthparticle);
1214  }
1215  else if (source == Jet::HCALOUT_CLUSTER)
1216  {
1217  RawCluster* cluster = _hcaloutclusters->getCluster(index);
1218 
1219  if (_strict)
1220  {
1221  assert(cluster);
1222  }
1223  else if (!cluster)
1224  {
1225  ++_errors;
1226  continue;
1227  }
1228 
1229  energy = get_hcalout_eval_stack()->get_rawcluster_eval()->get_energy_contribution(cluster, truthparticle);
1230  }
1231  else if (source == Jet::FEMC_TOWER)
1232  {
1233  RawTower* tower = _femctowers->getTower(index);
1234 
1235  if (_strict)
1236  {
1237  assert(tower);
1238  }
1239  else if (!tower)
1240  {
1241  ++_errors;
1242  continue;
1243  }
1244 
1245  energy = get_femc_eval_stack()->get_rawtower_eval()->get_energy_contribution(tower, truthparticle);
1246  }
1247  else if (source == Jet::FEMC_CLUSTER)
1248  {
1249  RawCluster* cluster = _femcclusters->getCluster(index);
1250 
1251  if (_strict)
1252  {
1253  assert(cluster);
1254  }
1255  else if (!cluster)
1256  {
1257  ++_errors;
1258  continue;
1259  }
1260 
1261  energy = get_femc_eval_stack()->get_rawcluster_eval()->get_energy_contribution(cluster, truthparticle);
1262  }
1263  else if (source == Jet::FHCAL_TOWER)
1264  {
1265  RawTower* tower = _fhcaltowers->getTower(index);
1266 
1267  if (_strict)
1268  {
1269  assert(tower);
1270  }
1271  else if (!tower)
1272  {
1273  ++_errors;
1274  continue;
1275  }
1276 
1277  energy = get_fhcal_eval_stack()->get_rawtower_eval()->get_energy_contribution(tower, truthparticle);
1278  }
1279  else if (source == Jet::FHCAL_CLUSTER)
1280  {
1281  RawCluster* cluster = _fhcalclusters->getCluster(index);
1282 
1283  if (_strict)
1284  {
1285  assert(cluster);
1286  }
1287  else if (!cluster)
1288  {
1289  ++_errors;
1290  continue;
1291  }
1292 
1293  energy = get_fhcal_eval_stack()->get_rawcluster_eval()->get_energy_contribution(cluster, truthparticle);
1294  }
1295 
1296  energy_contribution += energy;
1297  }
1298  }
1299 
1300  if (_do_cache)
1301  {
1302  _cache_get_energy_contribution.insert(std::make_pair(std::make_pair(recojet, truthjet), energy_contribution));
1303  }
1304 
1305  return energy_contribution;
1306 }
1307 
1308 // overlap calculations
1310 {
1311  if (_strict)
1312  {
1313  assert(recojet);
1314  }
1315  else if (!recojet)
1316  {
1317  ++_errors;
1318  return NAN;
1319  }
1320 
1321  if (_do_cache)
1322  {
1323  std::map<std::pair<Jet*, Jet::SRC>, float>::iterator iter =
1324  _cache_get_energy_contribution_src.find(std::make_pair(recojet, src));
1325  if (iter != _cache_get_energy_contribution_src.end())
1326  {
1327  return iter->second;
1328  }
1329  }
1330 
1331  float energy = 0.0;
1332 
1333  // loop over all recojet constituents
1334  for (Jet::ITER_comp_vec jter = recojet->comp_begin(src);
1335  jter != recojet->comp_end(src);
1336  ++jter)
1337  {
1338  Jet::SRC source = jter->first;
1339  assert(source == src); // jet container consistency check
1340  unsigned int index = jter->second;
1341 
1342  if (source == Jet::TRACK)
1343  {
1344  SvtxTrack* track = _trackmap->get(index);
1345  energy += track->get_p();
1346  }
1347  else if (source == Jet::CEMC_TOWER)
1348  {
1349  RawTower* tower = _cemctowers->getTower(index);
1350 
1351  if (_strict)
1352  {
1353  assert(tower);
1354  }
1355  else if (!tower)
1356  {
1357  ++_errors;
1358  continue;
1359  }
1360 
1361  energy += tower->get_energy();
1362  }
1363  else if (source == Jet::CEMC_CLUSTER)
1364  {
1365  RawCluster* cluster = _cemcclusters->getCluster(index);
1366 
1367  if (_strict)
1368  {
1369  assert(cluster);
1370  }
1371  else if (!cluster)
1372  {
1373  ++_errors;
1374  continue;
1375  }
1376 
1377  energy += cluster->get_energy();
1378  }
1379  else if (source == Jet::EEMC_TOWER)
1380  {
1381  RawTower* tower = _eemctowers->getTower(index);
1382 
1383  if (_strict)
1384  {
1385  assert(tower);
1386  }
1387  else if (!tower)
1388  {
1389  ++_errors;
1390  continue;
1391  }
1392 
1393  energy += tower->get_energy();
1394  }
1395  else if (source == Jet::EEMC_CLUSTER)
1396  {
1397  RawCluster* cluster = _eemcclusters->getCluster(index);
1398 
1399  if (_strict)
1400  {
1401  assert(cluster);
1402  }
1403  else if (!cluster)
1404  {
1405  ++_errors;
1406  continue;
1407  }
1408 
1409  energy += cluster->get_energy();
1410  }
1411  else if (source == Jet::HCALIN_TOWER)
1412  {
1413  RawTower* tower = _hcalintowers->getTower(index);
1414 
1415  if (_strict)
1416  {
1417  assert(tower);
1418  }
1419  else if (!tower)
1420  {
1421  ++_errors;
1422  continue;
1423  }
1424 
1425  energy += tower->get_energy();
1426  }
1427  else if (source == Jet::HCALIN_CLUSTER)
1428  {
1429  RawCluster* cluster = _hcalinclusters->getCluster(index);
1430 
1431  if (_strict)
1432  {
1433  assert(cluster);
1434  }
1435  else if (!cluster)
1436  {
1437  ++_errors;
1438  continue;
1439  }
1440 
1441  energy += cluster->get_energy();
1442  }
1443  else if (source == Jet::HCALOUT_TOWER)
1444  {
1446 
1447  if (_strict)
1448  {
1449  assert(tower);
1450  }
1451  else if (!tower)
1452  {
1453  ++_errors;
1454  continue;
1455  }
1456 
1457  energy += tower->get_energy();
1458  }
1459  else if (source == Jet::HCALOUT_CLUSTER)
1460  {
1461  RawCluster* cluster = _hcaloutclusters->getCluster(index);
1462 
1463  if (_strict)
1464  {
1465  assert(cluster);
1466  }
1467  else if (!cluster)
1468  {
1469  ++_errors;
1470  continue;
1471  }
1472 
1473  energy += cluster->get_energy();
1474  }
1475  else if (source == Jet::FEMC_TOWER)
1476  {
1477  RawTower* tower = _femctowers->getTower(index);
1478 
1479  if (_strict)
1480  {
1481  assert(tower);
1482  }
1483  else if (!tower)
1484  {
1485  ++_errors;
1486  continue;
1487  }
1488 
1489  energy += tower->get_energy();
1490  }
1491  else if (source == Jet::FEMC_CLUSTER)
1492  {
1493  RawCluster* cluster = _femcclusters->getCluster(index);
1494 
1495  if (_strict)
1496  {
1497  assert(cluster);
1498  }
1499  else if (!cluster)
1500  {
1501  ++_errors;
1502  continue;
1503  }
1504 
1505  energy += cluster->get_energy();
1506  }
1507  else if (source == Jet::FHCAL_TOWER)
1508  {
1509  RawTower* tower = _fhcaltowers->getTower(index);
1510 
1511  if (_strict)
1512  {
1513  assert(tower);
1514  }
1515  else if (!tower)
1516  {
1517  ++_errors;
1518  continue;
1519  }
1520 
1521  energy += tower->get_energy();
1522  }
1523  else if (source == Jet::FHCAL_CLUSTER)
1524  {
1525  RawCluster* cluster = _fhcalclusters->getCluster(index);
1526 
1527  if (_strict)
1528  {
1529  assert(cluster);
1530  }
1531  else if (!cluster)
1532  {
1533  ++_errors;
1534  continue;
1535  }
1536 
1537  energy += cluster->get_energy();
1538  } // else if (source == Jet::FHCAL_CLUSTER)
1539 
1540  } // for (Jet::ConstIter jter = recojet->lower_bound_comp(src);
1541 
1542  if (_do_cache)
1543  {
1544  _cache_get_energy_contribution_src.insert(std::make_pair(std::make_pair(recojet, src), energy));
1545  }
1546 
1547  return energy;
1548 }
1549 
1550 std::set<PHG4Hit*> JetRecoEval::all_truth_hits(Jet* recojet)
1551 {
1552  if (_strict)
1553  {
1554  assert(recojet);
1555  }
1556  else if (!recojet)
1557  {
1558  ++_errors;
1559  return std::set<PHG4Hit*>();
1560  }
1561 
1562  if (_do_cache)
1563  {
1564  std::map<Jet*, std::set<PHG4Hit*> >::iterator iter =
1565  _cache_all_truth_hits.find(recojet);
1566  if (iter != _cache_all_truth_hits.end())
1567  {
1568  return iter->second;
1569  }
1570  }
1571 
1572  std::set<PHG4Hit*> truth_hits;
1573 
1574  // loop over all the jet constituents, backtrack each reco object to the
1575  // truth hits and combine with other consituents
1576 
1577  for (auto jter : recojet->get_comp_vec())
1578  {
1579  Jet::SRC source = jter.first;
1580  unsigned int index = jter.second;
1581 
1582  std::set<PHG4Hit*> new_hits;
1583 
1584  if (source == Jet::TRACK)
1585  {
1586  if (!_trackmap)
1587  {
1588  std::cout << PHWHERE << "ERROR: can't find SvtxTrackMap" << std::endl;
1589  exit(-1);
1590  }
1591 
1592  SvtxTrack* track = _trackmap->get(index);
1593 
1594  if (_strict)
1595  {
1596  assert(track);
1597  }
1598  else if (!track)
1599  {
1600  ++_errors;
1601  continue;
1602  }
1603 
1604  new_hits = get_svtx_eval_stack()->get_track_eval()->all_truth_hits(track);
1605  }
1606  else if (source == Jet::CEMC_TOWER)
1607  {
1608  if (!_cemctowers)
1609  {
1610  std::cout << PHWHERE << "ERROR: can't find TOWER_CEMC" << std::endl;
1611  exit(-1);
1612  }
1613 
1614  RawTower* tower = _cemctowers->getTower(index);
1615 
1616  if (_strict)
1617  {
1618  assert(tower);
1619  }
1620  else if (!tower)
1621  {
1622  ++_errors;
1623  continue;
1624  }
1625 
1626  new_hits = get_cemc_eval_stack()->get_rawtower_eval()->all_truth_hits(tower);
1627  }
1628  else if (source == Jet::CEMC_CLUSTER)
1629  {
1630  if (!_cemcclusters)
1631  {
1632  std::cout << PHWHERE << "ERROR: can't find CLUSTER_CEMC" << std::endl;
1633  exit(-1);
1634  }
1635 
1636  RawCluster* cluster = _cemcclusters->getCluster(index);
1637 
1638  if (_strict)
1639  {
1640  assert(cluster);
1641  }
1642  else if (!cluster)
1643  {
1644  ++_errors;
1645  continue;
1646  }
1647 
1648  new_hits = get_cemc_eval_stack()->get_rawcluster_eval()->all_truth_hits(cluster);
1649  }
1650  else if (source == Jet::EEMC_TOWER)
1651  {
1652  if (!_eemctowers)
1653  {
1654  std::cout << PHWHERE << "ERROR: can't find TOWER_EEMC" << std::endl;
1655  exit(-1);
1656  }
1657 
1658  RawTower* tower = _eemctowers->getTower(index);
1659 
1660  if (_strict)
1661  {
1662  assert(tower);
1663  }
1664  else if (!tower)
1665  {
1666  ++_errors;
1667  continue;
1668  }
1669 
1670  new_hits = get_eemc_eval_stack()->get_rawtower_eval()->all_truth_hits(tower);
1671  }
1672  else if (source == Jet::EEMC_CLUSTER)
1673  {
1674  if (!_eemcclusters)
1675  {
1676  std::cout << PHWHERE << "ERROR: can't find CLUSTER_EEMC" << std::endl;
1677  exit(-1);
1678  }
1679 
1680  RawCluster* cluster = _eemcclusters->getCluster(index);
1681 
1682  if (_strict)
1683  {
1684  assert(cluster);
1685  }
1686  else if (!cluster)
1687  {
1688  ++_errors;
1689  continue;
1690  }
1691 
1692  new_hits = get_eemc_eval_stack()->get_rawcluster_eval()->all_truth_hits(cluster);
1693  }
1694  else if (source == Jet::HCALIN_TOWER)
1695  {
1696  if (!_hcalintowers)
1697  {
1698  std::cout << PHWHERE << "ERROR: can't find TOWER_HCALIN" << std::endl;
1699  exit(-1);
1700  }
1701 
1702  RawTower* tower = _hcalintowers->getTower(index);
1703 
1704  if (_strict)
1705  {
1706  assert(tower);
1707  }
1708  else if (!tower)
1709  {
1710  ++_errors;
1711  continue;
1712  }
1713 
1714  new_hits = get_hcalin_eval_stack()->get_rawtower_eval()->all_truth_hits(tower);
1715  }
1716  else if (source == Jet::HCALIN_CLUSTER)
1717  {
1718  if (!_hcalinclusters)
1719  {
1720  std::cout << PHWHERE << "ERROR: can't find CLUSTER_HCALIN" << std::endl;
1721  exit(-1);
1722  }
1723 
1724  RawCluster* cluster = _hcalinclusters->getCluster(index);
1725 
1726  if (_strict)
1727  {
1728  assert(cluster);
1729  }
1730  else if (!cluster)
1731  {
1732  ++_errors;
1733  continue;
1734  }
1735 
1736  new_hits = get_hcalin_eval_stack()->get_rawcluster_eval()->all_truth_hits(cluster);
1737  }
1738  else if (source == Jet::HCALOUT_TOWER)
1739  {
1740  if (!_hcalouttowers)
1741  {
1742  std::cout << PHWHERE << "ERROR: can't find TOWER_HCALOUT" << std::endl;
1743  exit(-1);
1744  }
1745 
1747 
1748  if (_strict)
1749  {
1750  assert(tower);
1751  }
1752  else if (!tower)
1753  {
1754  ++_errors;
1755  continue;
1756  }
1757 
1759  }
1760  else if (source == Jet::HCALOUT_CLUSTER)
1761  {
1762  if (!_hcaloutclusters)
1763  {
1764  std::cout << PHWHERE << "ERROR: can't find CLUSTER_HCALOUT" << std::endl;
1765  exit(-1);
1766  }
1767 
1768  RawCluster* cluster = _hcaloutclusters->getCluster(index);
1769 
1770  if (_strict)
1771  {
1772  assert(cluster);
1773  }
1774  else if (!cluster)
1775  {
1776  ++_errors;
1777  continue;
1778  }
1779 
1780  new_hits = get_hcalout_eval_stack()->get_rawcluster_eval()->all_truth_hits(cluster);
1781  }
1782  else if (source == Jet::FEMC_TOWER)
1783  {
1784  if (!_femctowers)
1785  {
1786  std::cout << PHWHERE << "ERROR: can't find TOWER_FEMC" << std::endl;
1787  exit(-1);
1788  }
1789 
1790  RawTower* tower = _femctowers->getTower(index);
1791 
1792  if (_strict)
1793  {
1794  assert(tower);
1795  }
1796  else if (!tower)
1797  {
1798  ++_errors;
1799  continue;
1800  }
1801 
1802  new_hits = get_femc_eval_stack()->get_rawtower_eval()->all_truth_hits(tower);
1803  }
1804  else if (source == Jet::FEMC_CLUSTER)
1805  {
1806  if (!_femcclusters)
1807  {
1808  std::cout << PHWHERE << "ERROR: can't find CLUSTER_FEMC" << std::endl;
1809  exit(-1);
1810  }
1811 
1812  RawCluster* cluster = _femcclusters->getCluster(index);
1813 
1814  if (_strict)
1815  {
1816  assert(cluster);
1817  }
1818  else if (!cluster)
1819  {
1820  ++_errors;
1821  continue;
1822  }
1823 
1824  new_hits = get_femc_eval_stack()->get_rawcluster_eval()->all_truth_hits(cluster);
1825  }
1826  else if (source == Jet::FHCAL_TOWER)
1827  {
1828  if (!_fhcaltowers)
1829  {
1830  std::cout << PHWHERE << "ERROR: can't find TOWER_FHCAL" << std::endl;
1831  exit(-1);
1832  }
1833 
1834  RawTower* tower = _fhcaltowers->getTower(index);
1835 
1836  if (_strict)
1837  {
1838  assert(tower);
1839  }
1840  else if (!tower)
1841  {
1842  ++_errors;
1843  continue;
1844  }
1845 
1846  new_hits = get_fhcal_eval_stack()->get_rawtower_eval()->all_truth_hits(tower);
1847  }
1848  else if (source == Jet::FHCAL_CLUSTER)
1849  {
1850  if (!_fhcalclusters)
1851  {
1852  std::cout << PHWHERE << "ERROR: can't find CLUSTER_FHCAL" << std::endl;
1853  exit(-1);
1854  }
1855 
1856  RawCluster* cluster = _fhcalclusters->getCluster(index);
1857 
1858  if (_strict)
1859  {
1860  assert(cluster);
1861  }
1862  else if (!cluster)
1863  {
1864  ++_errors;
1865  continue;
1866  }
1867 
1868  new_hits = get_fhcal_eval_stack()->get_rawcluster_eval()->all_truth_hits(cluster);
1869  }
1870 
1871  for (auto new_hit : new_hits)
1872  {
1873  truth_hits.insert(new_hit);
1874  }
1875  }
1876 
1877  if (_do_cache)
1878  {
1879  _cache_all_truth_hits.insert(std::make_pair(recojet, truth_hits));
1880  }
1881 
1882  return truth_hits;
1883 }
1884 
1886 {
1887  // need things off of the DST...
1888  _recojets = findNode::getClass<JetContainer>(topNode, _recojetname.c_str());
1889  if (!_recojets)
1890  {
1891  std::cout << PHWHERE << " ERROR: Can't find " << _recojetname << std::endl;
1892  exit(-1);
1893  }
1894 
1895  _truthjets = findNode::getClass<JetContainer>(topNode, _truthjetname.c_str());
1896  if (!_truthjets)
1897  {
1898  std::cout << PHWHERE << " ERROR: Can't find " << _truthjetname << std::endl;
1899  exit(-1);
1900  }
1901 
1902  _trackmap = findNode::getClass<SvtxTrackMap>(topNode, m_TrackNodeName);
1903  if (!_trackmap)
1904  {
1905  _trackmap = findNode::getClass<SvtxTrackMap>(topNode, "TrackMap");
1906  }
1907  _cemctowers = findNode::getClass<RawTowerContainer>(topNode, "TOWER_CALIB_CEMC");
1908  _hcalintowers = findNode::getClass<RawTowerContainer>(topNode, "TOWER_CALIB_HCALIN");
1909  _hcalouttowers = findNode::getClass<RawTowerContainer>(topNode, "TOWER_CALIB_HCALOUT");
1910  _femctowers = findNode::getClass<RawTowerContainer>(topNode, "TOWER_CALIB_FEMC");
1911  _fhcaltowers = findNode::getClass<RawTowerContainer>(topNode, "TOWER_CALIB_FHCAL");
1912  _eemctowers = findNode::getClass<RawTowerContainer>(topNode, "TOWER_CALIB_EEMC");
1913  _cemcclusters = findNode::getClass<RawClusterContainer>(topNode, "CLUSTER_CEMC");
1914  _hcalinclusters = findNode::getClass<RawClusterContainer>(topNode, "CLUSTER_HCALIN");
1915  _hcaloutclusters = findNode::getClass<RawClusterContainer>(topNode, "CLUSTER_HCALOUT");
1916  _femcclusters = findNode::getClass<RawClusterContainer>(topNode, "CLUSTER_FEMC");
1917  _fhcalclusters = findNode::getClass<RawClusterContainer>(topNode, "CLUSTER_FHCAL");
1918  _eemcclusters = findNode::getClass<RawClusterContainer>(topNode, "CLUSTER_EEMC");
1919 
1920  return;
1921 }