Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
JetEnergies.C
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file JetEnergies.C
1 
2 #include "JetEnergies.h"
3 
4 #include <cassert>
5 
6 #include <g4eval/JetEvalStack.h>
7 #include <g4eval/JetRecoEval.h>
8 
10 #include <phool/getClass.h>
11 #include <fun4all/SubsysReco.h>
12 #include <phool/PHCompositeNode.h>
13 #include <g4jets/JetMap.h>
14 #include <g4jets/Jet.h>
15 
17 
18 #include <TNtuple.h>
19 #include <TFile.h>
20 
21 #include <iostream>
22 #include <cmath>
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(NULL),
35  _strict(false),
36  _errors(0),
37  _do_recojet_eval(true),
38  _do_truthjet_eval(true),
39  _ntp_recojet(NULL),
40  _ntp_truthjet(NULL),
41  _filename(filename),
42  _tfile(NULL),
43  _FluxReturn_plus_hit_container(NULL),
44  _FluxReturn_minus_hit_container(NULL),
45  _BH_1_hit_container(NULL),
46  _BH_Forward_hit_container(NULL),
47  _BH_Negative_hit_container(NULL)
48 {
49  verbosity = 0;
50 }
51 
53 
54  _ievent = 0;
55 
56  _tfile = new TFile(_filename.c_str(), "RECREATE");
57 
58  if (_do_recojet_eval) _ntp_recojet = new TNtuple("ntp_recojet","reco jet => max truth jet",
59  "event:id:ncomp:eta:phi:e:pt:"
60  "gid:gncomp:geta:gphi:ge:gpt:"
61  "efromtruth");
62 
63  if (_do_truthjet_eval) _ntp_truthjet = new TNtuple("ntp_truthjet","truth jet => best reco jet",
64  "event:gid:gncomp:geta:gphi:ge:gpt:"
65  "id:ncomp:eta:phi:e:pt:"
66  "efromtruth:e_FR_plus:e_FR_minus:e_BH1:e_BH_plus:e_BH_minus");
67 
68 
70 }
71 
72 //Added 'InitRun(...) since the hit container for the Flux Return was not being intialized in 'Init(...){...}'
74 {
75  //cout << "In InitRun" << endl;
76  //Here I add the container for the Plug Door hits
77  _FluxReturn_plus_hit_container = findNode::getClass<PHG4HitContainer>(topNode, "G4HIT_FLUXRET_ETA_PLUS");
78  _FluxReturn_minus_hit_container = findNode::getClass<PHG4HitContainer>(topNode, "G4HIT_FLUXRET_ETA_MINUS");
79 
80  _BH_1_hit_container = findNode::getClass<PHG4HitContainer>(topNode,"G4HIT_BH_1");
81  _BH_Forward_hit_container = findNode::getClass<PHG4HitContainer>(topNode,"G4HIT_BH_FORWARD_PLUS");
82  _BH_Negative_hit_container = findNode::getClass<PHG4HitContainer>(topNode,"G4HIT_BH_FORWARD_NEG");
83 
84 
85  //Used for debugging
86  /*
87  if( _FluxReturn_hit_container )
88  {
89  cout << "Correct name" << endl;
90  }
91  */
92 
94 
95 }
96 
98 
99  //cout << "In process Event" << endl;
100 
101  if (!_jetevalstack) {
105  } else {
106  _jetevalstack->next_event(topNode);
107  }
108 
109  //-----------------------------------
110  // print what is coming into the code
111  //-----------------------------------
112 
113  printInputInfo(topNode);
114 
115  //---------------------------
116  // fill the Evaluator NTuples
117  //---------------------------
118 
119  fillOutputNtuples(topNode);
120 
121  //--------------------------------------------------
122  // Print out the ancestry information for this event
123  //--------------------------------------------------
124 
125  printOutputInfo(topNode);
126 
127  ++_ievent;
128 
130 }
131 
133 
134  _tfile->cd();
135 
136  if (_do_recojet_eval) _ntp_recojet->Write();
137  if (_do_truthjet_eval) _ntp_truthjet->Write();
138 
139  _tfile->Close();
140 
141  delete _tfile;
142 
143  if (verbosity > 0) {
144  cout << "========================== JetEnergies::End() ============================" << endl;
145  cout << " " << _ievent << " events of output written to: " << _filename << endl;
146  cout << "===========================================================================" << endl;
147  }
148 
149  delete _jetevalstack;
150 
152 }
153 
155  // to be implemented later if needed
156  return;
157 }
158 
160  // to be implemented later if needed
161  return;
162 }
163 
165 
166  if (verbosity > 2) cout << "JetEnergies::fillOutputNtuples() entered" << endl;
167 
168  float e_FR_plus = GetTotalEnergy( _FluxReturn_plus_hit_container );
169  float e_FR_minus = GetTotalEnergy( _FluxReturn_minus_hit_container );
170  float e_BH1 = GetTotalEnergy( _BH_1_hit_container );
171  float e_BH_plus = GetTotalEnergy( _BH_Forward_hit_container );
172  float e_BH_minus = GetTotalEnergy( _BH_Negative_hit_container );
173 
174  JetRecoEval* recoeval = _jetevalstack->get_reco_eval();
175  //JetTruthEval* trutheval = _jetevalstack->get_truth_eval();
176 
177  //-------------------------
178  // fill the reco jet ntuple
179  //-------------------------
180 
181  if (_do_recojet_eval) {
182  if (verbosity > 1) cout << "JetEnergies::filling recojet ntuple..." << endl;
183 
184  JetMap* recojets = findNode::getClass<JetMap>(topNode,_recojetname.c_str());
185  if (!recojets) {
186  cerr << PHWHERE << " ERROR: Can't find " << _recojetname << endl;
187  exit(-1);
188  }
189 
190  // for every recojet
191  for (JetMap::Iter iter = recojets->begin();
192  iter != recojets->end();
193  ++iter) {
194  Jet* recojet = iter->second;
195  Jet* truthjet = recoeval->max_truth_jet_by_energy(recojet);
196 
197  float id = recojet->get_id();
198  float ncomp = recojet->size_comp();
199  float eta = recojet->get_eta();
200  float phi = recojet->get_phi();
201  float e = recojet->get_e();
202  float pt = recojet->get_pt();
203 
204  float gid = NAN;
205  float gncomp = NAN;
206  float geta = NAN;
207  float gphi = NAN;
208  float ge = NAN;
209  float gpt = NAN;
210  float efromtruth = NAN;
211 
212  if (truthjet) {
213  gid = truthjet->get_id();
214  gncomp = truthjet->size_comp();
215  geta = truthjet->get_eta();
216  gphi = truthjet->get_phi();
217  ge = truthjet->get_e();
218  gpt = truthjet->get_pt();
219  efromtruth = recoeval->get_energy_contribution(recojet,truthjet);
220  }
221 
222  float recojet_data[14] = {(float) _ievent,
223  id,
224  ncomp,
225  eta,
226  phi,
227  e,
228  pt,
229  gid,
230  gncomp,
231  geta,
232  gphi,
233  ge,
234  gpt,
235  efromtruth
236  };
237 
238  _ntp_recojet->Fill(recojet_data);
239  }
240  }
241 
242  //-------------------------
243  // fill the truth jet ntuple
244  //-------------------------
245 
246  if (_do_truthjet_eval) {
247  if (verbosity > 1) cout << "JetEnergies::filling truthjet ntuple..." << endl;
248 
249  JetMap* truthjets = findNode::getClass<JetMap>(topNode,_truthjetname.c_str());
250  if (!truthjets) {
251  cerr << PHWHERE << " ERROR: Can't find " << _truthjetname << endl;
252  exit(-1);
253  }
254 
255  // for every truthjet
256  for (JetMap::Iter iter = truthjets->begin();
257  iter != truthjets->end();
258  ++iter) {
259  Jet* truthjet = iter->second;
260  Jet* recojet = recoeval->best_jet_from(truthjet);
261 
262  float gid = truthjet->get_id();
263  float gncomp = truthjet->size_comp();
264  float geta = truthjet->get_eta();
265  float gphi = truthjet->get_phi();
266  float ge = truthjet->get_e();
267  float gpt = truthjet->get_pt();
268 
269  float id = NAN;
270  float ncomp = NAN;
271  float eta = NAN;
272  float phi = NAN;
273  float e = NAN;
274  float pt = NAN;
275  float efromtruth = NAN;
276 
277  if (recojet) {
278  id = recojet->get_id();
279  ncomp = recojet->size_comp();
280  eta = recojet->get_eta();
281  phi = recojet->get_phi();
282  e = recojet->get_e();
283  pt = recojet->get_pt();
284  efromtruth = recoeval->get_energy_contribution(recojet,truthjet);
285  }
286 
287  float truthjet_data[19] = {(float) _ievent,
288  gid,
289  gncomp,
290  geta,
291  gphi,
292  ge,
293  gpt,
294  id,
295  ncomp,
296  eta,
297  phi,
298  e,
299  pt,
300  efromtruth,
301  e_FR_plus,
302  e_FR_minus,
303  e_BH1,
304  e_BH_plus,
305  e_BH_minus
306  };
307 
308  _ntp_truthjet->Fill(truthjet_data);
309  }
310  }
311 
312  return;
313 }
314 
315 
316 //Function that iterates over all the hits for a given object and returns the total energy deposited in that object requires that the object is set as active in the G4_Setup file otherwise may not work
318 {
319  //cout << "Executing Modified part of code" << endl;
320  float e_object = 0;//Holds Energy deposited in object of interest
321  //Check if hit container exists mostly used for debugging
322  if( hit_object )
323  {
324  //cout << "Execute my code" << endl;
325  PHG4HitContainer::ConstRange object_hit_range = hit_object->getHits();
326  //For loop that will store the total energy deposited in the object in 'e_object'
327  //cout << "Starting for loop" << endl;
328  for (PHG4HitContainer::ConstIterator hit_iter = object_hit_range.first; hit_iter != object_hit_range.second; hit_iter++)
329  {
330  PHG4Hit *this_hit = hit_iter->second;
331  assert(this_hit);
332  e_object += this_hit->get_edep();
333  }
334 
335  //cout << "End of for loop" << endl;
336  //cout << "Value of e_FR: " << e_FR << endl;
337 
338  }
339  //cout << "Executing Rest of original code" << endl;
340  return e_object;
341 
342 }