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