ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
JetEvaluator.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file JetEvaluator.cc
1 #include "JetEvaluator.h"
2 
3 #include "JetEvalStack.h"
4 #include "JetRecoEval.h"
5 
6 #include <g4jets/Jet.h>
7 #include <g4jets/JetMap.h>
8 
10 #include <fun4all/SubsysReco.h>
11 
12 #include <phool/getClass.h>
13 #include <phool/phool.h>
14 
15 #include <TFile.h>
16 #include <TNtuple.h>
17 
18 #include <cstdlib>
19 #include <cmath>
20 #include <iostream>
21 #include <map>
22 #include <utility>
23 
24 using namespace std;
25 
27  const string &recojetname,
28  const string &truthjetname,
29  const string &filename)
30  : SubsysReco(name)
31  , _recojetname(recojetname)
32  , _truthjetname(truthjetname)
33  , _ievent(0)
34  , _jetevalstack(nullptr)
35  , _strict(false)
36  , _do_recojet_eval(true)
37  , _do_truthjet_eval(true)
38  , _ntp_recojet(nullptr)
39  , _ntp_truthjet(nullptr)
40  , _filename(filename)
41  , _tfile(nullptr)
42 {
43 }
44 
46 {
47  _ievent = 0;
48 
49  _tfile = new TFile(_filename.c_str(), "RECREATE");
50 
51  if (_do_recojet_eval) _ntp_recojet = new TNtuple("ntp_recojet", "reco jet => max truth jet",
52  "event:id:ncomp:eta:phi:e:pt:"
53  "gid:gncomp:geta:gphi:ge:gpt:"
54  "efromtruth");
55 
56  if (_do_truthjet_eval) _ntp_truthjet = new TNtuple("ntp_truthjet", "truth jet => best reco jet",
57  "event:gid:gncomp:geta:gphi:ge:gpt:"
58  "id:ncomp:eta:phi:e:pt:"
59  "efromtruth");
60 
62 }
63 
65 {
66  if (!_jetevalstack)
67  {
71  }
72  else
73  {
74  _jetevalstack->next_event(topNode);
75  }
76 
77  //-----------------------------------
78  // print what is coming into the code
79  //-----------------------------------
80 
81  printInputInfo(topNode);
82 
83  //---------------------------
84  // fill the Evaluator NTuples
85  //---------------------------
86 
87  fillOutputNtuples(topNode);
88 
89  //--------------------------------------------------
90  // Print out the ancestry information for this event
91  //--------------------------------------------------
92 
93  printOutputInfo(topNode);
94 
95  ++_ievent;
96 
98 }
99 
101 {
102  _tfile->cd();
103 
104  if (_do_recojet_eval) _ntp_recojet->Write();
105  if (_do_truthjet_eval) _ntp_truthjet->Write();
106 
107  _tfile->Close();
108 
109  delete _tfile;
110 
111  if (Verbosity() > 0)
112  {
113  cout << "========================== JetEvaluator::End() ============================" << endl;
114  cout << " " << _ievent << " events of output written to: " << _filename << endl;
115  cout << "===========================================================================" << endl;
116  }
117 
118  delete _jetevalstack;
119 
121 }
122 
124 {
125  // to be implemented later if needed
126  return;
127 }
128 
130 {
131  // to be implemented later if needed
132  return;
133 }
134 
136 {
137  if (Verbosity() > 2) cout << "JetEvaluator::fillOutputNtuples() entered" << endl;
138 
139  JetRecoEval *recoeval = _jetevalstack->get_reco_eval();
140  //JetTruthEval* trutheval = _jetevalstack->get_truth_eval();
141 
142  //-------------------------
143  // fill the reco jet ntuple
144  //-------------------------
145 
146  if (_do_recojet_eval)
147  {
148  if (Verbosity() > 1) cout << "JetEvaluator::filling recojet ntuple..." << endl;
149 
150  JetMap *recojets = findNode::getClass<JetMap>(topNode, _recojetname.c_str());
151  if (!recojets)
152  {
153  cerr << PHWHERE << " ERROR: Can't find " << _recojetname << endl;
154  exit(-1);
155  }
156 
157  // for every recojet
158  for (JetMap::Iter iter = recojets->begin();
159  iter != recojets->end();
160  ++iter)
161  {
162  Jet *recojet = iter->second;
163  Jet *truthjet = recoeval->max_truth_jet_by_energy(recojet);
164 
165  float id = recojet->get_id();
166  float ncomp = recojet->size_comp();
167  float eta = recojet->get_eta();
168  float phi = recojet->get_phi();
169  float e = recojet->get_e();
170  float pt = recojet->get_pt();
171 
172  float gid = NAN;
173  float gncomp = NAN;
174  float geta = NAN;
175  float gphi = NAN;
176  float ge = NAN;
177  float gpt = NAN;
178  float efromtruth = NAN;
179 
180  if (truthjet)
181  {
182  gid = truthjet->get_id();
183  gncomp = truthjet->size_comp();
184  geta = truthjet->get_eta();
185  gphi = truthjet->get_phi();
186  ge = truthjet->get_e();
187  gpt = truthjet->get_pt();
188  efromtruth = recoeval->get_energy_contribution(recojet, truthjet);
189  }
190 
191  float recojet_data[14] = {(float) _ievent,
192  id,
193  ncomp,
194  eta,
195  phi,
196  e,
197  pt,
198  gid,
199  gncomp,
200  geta,
201  gphi,
202  ge,
203  gpt,
204  efromtruth};
205 
206  _ntp_recojet->Fill(recojet_data);
207  }
208  }
209 
210  //-------------------------
211  // fill the truth jet ntuple
212  //-------------------------
213 
214  if (_do_truthjet_eval)
215  {
216  if (Verbosity() > 1) cout << "JetEvaluator::filling truthjet ntuple..." << endl;
217 
218  JetMap *truthjets = findNode::getClass<JetMap>(topNode, _truthjetname.c_str());
219  if (!truthjets)
220  {
221  cerr << PHWHERE << " ERROR: Can't find " << _truthjetname << endl;
222  exit(-1);
223  }
224 
225  // for every truthjet
226  for (JetMap::Iter iter = truthjets->begin();
227  iter != truthjets->end();
228  ++iter)
229  {
230  Jet *truthjet = iter->second;
231  Jet *recojet = recoeval->best_jet_from(truthjet);
232 
233  float gid = truthjet->get_id();
234  float gncomp = truthjet->size_comp();
235  float geta = truthjet->get_eta();
236  float gphi = truthjet->get_phi();
237  float ge = truthjet->get_e();
238  float gpt = truthjet->get_pt();
239 
240  float id = NAN;
241  float ncomp = NAN;
242  float eta = NAN;
243  float phi = NAN;
244  float e = NAN;
245  float pt = NAN;
246  float efromtruth = NAN;
247 
248  if (recojet)
249  {
250  id = recojet->get_id();
251  ncomp = recojet->size_comp();
252  eta = recojet->get_eta();
253  phi = recojet->get_phi();
254  e = recojet->get_e();
255  pt = recojet->get_pt();
256  efromtruth = recoeval->get_energy_contribution(recojet, truthjet);
257  }
258 
259  float truthjet_data[14] = {(float) _ievent,
260  gid,
261  gncomp,
262  geta,
263  gphi,
264  ge,
265  gpt,
266  id,
267  ncomp,
268  eta,
269  phi,
270  e,
271  pt,
272  efromtruth};
273 
274  _ntp_truthjet->Fill(truthjet_data);
275  }
276  }
277 
278  return;
279 }