Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MyJetAnalysis.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file MyJetAnalysis.cc
1 #include "MyJetAnalysis.h"
2 
4 
5 #include <jetbase/JetMap.h>
6 
10 
11 #include <phool/PHCompositeNode.h>
12 #include <phool/getClass.h>
13 
14 #include <TFile.h>
15 #include <TH1F.h>
16 #include <TH2F.h>
17 #include <TString.h>
18 #include <TTree.h>
19 #include <TVector3.h>
20 
21 #include <algorithm>
22 #include <cassert>
23 #include <cmath>
24 #include <iostream>
25 #include <limits>
26 #include <stdexcept>
27 
28 MyJetAnalysis::MyJetAnalysis(const std::string& recojetname, const std::string& truthjetname, const std::string& outputfilename)
29  : SubsysReco("MyJetAnalysis_" + recojetname + "_" + truthjetname)
30  , m_recoJetName(recojetname)
31  , m_truthJetName(truthjetname)
32  , m_outputFileName(outputfilename)
33  , m_etaRange(-1, 1)
34  , m_ptRange(5, 100)
35  , m_trackJetMatchingRadius(.7)
36 {
37  m_trackdR.fill(std::numeric_limits<float>::signaling_NaN());
38  m_trackpT.fill(std::numeric_limits<float>::signaling_NaN());
39 }
40 
42 {
44  std::cout << "MyJetAnalysis::Init - Outoput to " << m_outputFileName << std::endl;
45 
47 
48  // Histograms
49  m_hInclusiveE = new TH1F(
50  "hInclusive_E", //
51  TString(m_recoJetName) + " inclusive jet E;Total jet energy (GeV)", 100, 0, 100);
52 
54  new TH1F(
55  "hInclusive_eta", //
56  TString(m_recoJetName) + " inclusive jet #eta;#eta;Jet energy density", 50, -1, 1);
58  new TH1F(
59  "hInclusive_phi", //
60  TString(m_recoJetName) + " inclusive jet #phi;#phi;Jet energy density", 50, -M_PI, M_PI);
61 
62  //Trees
63  m_T = new TTree("T", "MyJetAnalysis Tree");
64 
65  // int m_event;
66  m_T->Branch("m_event", &m_event, "event/I");
67  // int m_id;
68  m_T->Branch("id", &m_id, "id/I");
69  // int m_nComponent;
70  m_T->Branch("nComponent", &m_nComponent, "nComponent/I");
71  // float m_eta;
72  m_T->Branch("eta", &m_eta, "eta/F");
73  // float m_phi;
74  m_T->Branch("phi", &m_phi, "phi/F");
75  // float m_e;
76  m_T->Branch("e", &m_e, "e/F");
77  // float m_pt;
78  m_T->Branch("pt", &m_pt, "pt/F");
79  //
80  // int m_truthID;
81  m_T->Branch("truthID", &m_truthID, "truthID/I");
82  // int m_truthNComponent;
83  m_T->Branch("truthNComponent", &m_truthNComponent, "truthNComponent/I");
84  // float m_truthEta;
85  m_T->Branch("truthEta", &m_truthEta, "truthEta/F");
86  // float m_truthPhi;
87  m_T->Branch("truthPhi", &m_truthPhi, "truthPhi/F");
88  // float m_truthE;
89  m_T->Branch("truthE", &m_truthE, "truthE/F");
90  // float m_truthPt;
91  m_T->Branch("truthPt", &m_truthPt, "truthPt/F");
92  //
93  // //! number of matched tracks
94  // int m_nMatchedTrack;
95  m_T->Branch("nMatchedTrack", &m_nMatchedTrack, "nMatchedTrack/I");
96  // std::array<float, kMaxMatchedTrack> m_trackdR;
97  m_T->Branch("id", m_trackdR.data(), "trackdR[nMatchedTrack]/F");
98  // std::array<float, kMaxMatchedTrack> m_trackpT;
99  m_T->Branch("id", m_trackpT.data(), "trackpT[nMatchedTrack]/F");
100 
102 }
103 
105 {
106  std::cout << "MyJetAnalysis::End - Output to " << m_outputFileName << std::endl;
108 
109  m_hInclusiveE->Write();
110  m_hInclusiveEta->Write();
111  m_hInclusivePhi->Write();
112  m_T->Write();
113 
115 }
116 
118 {
119  m_jetEvalStack = std::shared_ptr<JetEvalStack>(new JetEvalStack(topNode, m_recoJetName, m_truthJetName));
120  m_jetEvalStack->get_stvx_eval_stack()->set_use_initial_vertex(initial_vertex);
122 }
123 
125 {
127  std::cout << "MyJetAnalysis::process_event() entered" << std::endl;
128 
129  m_jetEvalStack->next_event(topNode);
130  JetRecoEval* recoeval = m_jetEvalStack->get_reco_eval();
131  ++m_event;
132 
133  // interface to jets
134  JetMap* jets = findNode::getClass<JetMap>(topNode, m_recoJetName);
135  if (!jets)
136  {
137  std::cout
138  << "MyJetAnalysis::process_event - Error can not find DST JetMap node "
139  << m_recoJetName << std::endl;
140  exit(-1);
141  }
142 
143  // interface to tracks
144  SvtxTrackMap* trackmap = findNode::getClass<SvtxTrackMap>(topNode, "SvtxTrackMap");
145  if (!trackmap)
146  {
147  trackmap = findNode::getClass<SvtxTrackMap>(topNode, "TrackMap");
148  if (!trackmap)
149  {
150  std::cout
151  << "MyJetAnalysis::process_event - Error can not find DST trackmap node SvtxTrackMap" << std::endl;
152  exit(-1);
153  }
154  }
155  for (JetMap::Iter iter = jets->begin(); iter != jets->end(); ++iter)
156  {
157  Jet* jet = iter->second;
158  assert(jet);
159 
160  bool eta_cut = (jet->get_eta() >= m_etaRange.first) and (jet->get_eta() <= m_etaRange.second);
161  bool pt_cut = (jet->get_pt() >= m_ptRange.first) and (jet->get_pt() <= m_ptRange.second);
162  if ((not eta_cut) or (not pt_cut))
163  {
165  {
166  std::cout << "MyJetAnalysis::process_event() - jet failed acceptance cut: ";
167  std::cout << "eta cut: " << eta_cut << ", ptcut: " << pt_cut << std::endl;
168  std::cout << "jet eta: " << jet->get_eta() << ", jet pt: " << jet->get_pt() << std::endl;
169  jet->identify();
170  }
171  continue;
172  }
173 
174  // fill histograms
176  m_hInclusiveE->Fill(jet->get_e());
178  m_hInclusiveEta->Fill(jet->get_eta());
180  m_hInclusivePhi->Fill(jet->get_phi());
181 
182  // fill trees - jet spectrum
183  Jet* truthjet = recoeval->max_truth_jet_by_energy(jet);
184 
185  m_id = jet->get_id();
186  m_nComponent = jet->size_comp();
187  m_eta = jet->get_eta();
188  m_phi = jet->get_phi();
189  m_e = jet->get_e();
190  m_pt = jet->get_pt();
191 
192  m_truthID = -1;
193  m_truthNComponent = -1;
194  m_truthEta = NAN;
195  m_truthPhi = NAN;
196  m_truthE = NAN;
197  m_truthPt = NAN;
198 
199  if (truthjet)
200  {
201  m_truthID = truthjet->get_id();
202  m_truthNComponent = truthjet->size_comp();
203  m_truthEta = truthjet->get_eta();
204  m_truthPhi = truthjet->get_phi();
205  m_truthE = truthjet->get_e();
206  m_truthPt = truthjet->get_pt();
207  }
208 
209  // fill trees - jet track matching
210  m_nMatchedTrack = 0;
211 
212  for (SvtxTrackMap::Iter iter = trackmap->begin();
213  iter != trackmap->end();
214  ++iter)
215  {
216  SvtxTrack* track = iter->second;
217 
218  TVector3 v(track->get_px(), track->get_py(), track->get_pz());
219  const double dEta = v.Eta() - m_eta;
220  const double dPhi = v.Phi() - m_phi;
221  const double dR = sqrt(dEta * dEta + dPhi * dPhi);
222 
223  if (dR < m_trackJetMatchingRadius)
224  {
225  //matched track to jet
226 
228 
230  m_trackpT[m_nMatchedTrack] = v.Perp();
231 
232  ++m_nMatchedTrack;
233  }
234 
236  {
237  std::cout << "MyJetAnalysis::process_event() - reached max track that matching a jet. Quit iterating tracks" << std::endl;
238  break;
239  }
240 
241  } // for (SvtxTrackMap::Iter iter = trackmap->begin();
242 
243  m_T->Fill();
244  } // for (JetMap::Iter iter = jets->begin(); iter != jets->end(); ++iter)
245 
247 }