Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
jetrtrack.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file jetrtrack.cc
1 #include "jetrtrack.h"
2 #include <fun4all/SubsysReco.h> // for SubsysReco
3 
5 #include <TFile.h>
6 #include <TH1F.h>
8 
10 #include <g4jets/JetMap.h>
11 #include <fun4all/Fun4AllServer.h>
12 #include <fun4all/PHTFileServer.h>
13 #include <g4main/PHG4VtxPoint.h>
14 #include <TTree.h>
15 #include <phool/PHCompositeNode.h>
16 
17 #include <phool/PHCompositeNode.h>
18 #include <phool/PHIODataNode.h> // for PHIODataNode
19 #include <phool/PHNode.h> // for PHNode
20 #include <phool/PHNodeIterator.h> // for PHNodeIterator
21 #include <phool/PHObject.h> // for PHObject
22 #include <phool/getClass.h>
23 
24 #include <iostream>
25 #include <sstream>
26 #include <string>
27 #include <phool/phool.h> // for PHWHERE
28 
29 #include <g4main/PHG4Particle.h>
32 
33 #include <trackbase/TrkrCluster.h>
36 #include <trackbase/TrkrHit.h>
38 #include <trackbase/TrkrDefs.h>
39 #include <trackbase/ActsGeometry.h>
41 
42 #include <trackbase/TpcDefs.h>
43 
45 #include <trackbase/TrkrHitSet.h>
48 
51 #include <trackbase_historic/SvtxVertex.h>
52 #include <trackbase_historic/SvtxVertexMap.h>
55 #include <g4main/PHG4Particle.h>
56 
58 
59 
60 
61 //____________________________________________________________________________..
63  SubsysReco(name)
64 {
65  std::cout << "jetrtrack::jetrtrack(const std::string &name) Calling ctor" << std::endl;
66 }
67 
68 //____________________________________________________________________________..
70 {
71  std::cout << "jetrtrack::~jetrtrack() Calling dtor" << std::endl;
72 }
73 //____________________________________________________________________________..
74 
76 {
77  //-------------------------------------------------------------
78  //print the list of nodes available in memory
79  //-------------------------------------------------------------
80  topNode->print();
82 }
83 
84 Fun4AllHistoManager *jetrtrack::get_HistoManager() //This enables the handling of the output object and eventual writing of it to file
85 {
87  Fun4AllHistoManager *hm = se->getHistoManager("jetrtrack_HISTOS");
88 
89  if (not hm)
90  {
91  std:: cout
92  << "jetrtrack::get_HistoManager - Making Fun4AllHistoManager jetrtrack_HISTOS"
93  << std::endl;
94  hm = new Fun4AllHistoManager("jetrtrack_HISTOS");
95  se->registerHistoManager(hm);
96  }
97  assert(hm);
98  return hm;
99 }
100 
101 //____________________________________________________________________________..
103 {
104  std::cout << "jetrtrack::InitRun(PHCompositeNode *topNode) Initializing for Run XXX" << std::endl;
105 
106  //-------------------------------
107  //Create the output file
108  //-------------------------------
109  PHTFileServer::get().open(m_outfilename.c_str(), "RECREATE");
110  // PHTFileServer::get().open("jettree.root", "RECREATE");
111 
112 
114  assert(hm);
115 
116  //------------------------------------------------------------------------------------------------
117  // create the output tree where jets, truth particles, and tracks will
118  // be stored and register it to the histogram manager
119  //------------------------------------------------------------------------------------------------
120 
121  TTree* tree= new TTree("tree","tree");
122  tree->Print();
123 
124  // Truth jet kinematics
125  tree->Branch("tjet_pt",&m_tjet_pt);
126  tree->Branch("tjet_phi",&m_tjet_phi);
127  tree->Branch("tjet_eta",&m_tjet_eta);
128  tree->Branch("tjet_jcpt",&m_tjet_jcpt);
129 
130  //Reconstructed jet kinematics
131  tree->Branch("rjet_pt",&m_rjet_pt);
132  tree->Branch("rjet_phi",&m_rjet_phi);
133  tree->Branch("rjet_eta",&m_rjet_eta);
134 
135  //Reconstructed track kinematics
136  tree->Branch("trk_pt",&m_trk_pt);
137  tree->Branch("trk_eta",&m_trk_eta);
138  tree->Branch("trk_phi",&m_trk_phi);
139  tree->Branch("trk_quality",&m_trk_qual);
140  tree->Branch("nmvtx_hits", &m_nmvtxhits);
141 
142  //global variables
143  tree->Branch("cent", &m_centrality);
144  tree->Branch("zvtx", &m_zvtx);
145 
146  //truth charged particle information
147  tree->Branch("tp_pt",&m_tp_pt);
148  tree->Branch("tp_px",&m_tp_px);
149  tree->Branch("tp_py",&m_tp_py);
150  tree->Branch("tp_pz",&m_tp_pz);
151  tree->Branch("tp_phi",&m_tp_phi);
152  tree->Branch("tp_eta",&m_tp_eta);
153  tree->Branch("tp_pid",&m_tp_pid);
154 
155  //Jet constituent information for truth jets
156  tree->Branch("jc_pt",&m_jc_pt);
157  tree->Branch("jc_phi",&m_jc_phi);
158  tree->Branch("jc_eta",&m_jc_eta);
159  tree->Branch("jc_tjet_index",&m_jc_index);
160 
161  hm->registerHisto(tree);
162 
164 }
165 
166 //____________________________________________________________________________..
168 {
169  double pi = TMath::Pi();
170 
171  //-----------------------------------------------------------------------------------------------------------------
172  // load in the containers which hold the truth and reconstructed information
173  //-----------------------------------------------------------------------------------------------------------------
174 
175  JetMap* tjets = findNode::getClass<JetMap>(topNode, "AntiKt_Truth_r04");
176  if (!tjets)
177  {
178  std::cout
179  << "Jetrtrack::process_event - Error can not find DST JetMap node "
180  << std::endl;
181  exit(-1);
182  }
183 
184 
185  JetMap* rjets = findNode::getClass<JetMap>(topNode, "AntiKt_Tower_r04_Sub1");
186  if (!rjets)
187  {
188  std::cout
189  << "Jetrtrack::process_event - Error can not find DST JetMap node "
190  << std::endl;
191  exit(-1);
192  }
193 
194  SvtxTrackMap* trackmap = findNode::getClass<SvtxTrackMap>(topNode, "SvtxTrackMap");
195  if (!trackmap)
196  {
197  std::cout
198  << "Jetrtrack::process_event - Error can not find DST SvtxTrackMap node "
199  << std::endl;
200  exit(-1);
201  }
202 
203 
204  CentralityInfo* cent_node = findNode::getClass<CentralityInfo>(topNode, "CentralityInfo");
205  if (!cent_node)
206  {
207  std::cout
208  << "Jetrtrack::process_event - Error can not find centrality node "
209  << std::endl;
210  exit(-1);
211  }
212 
213  m_centrality = cent_node->get_centile(CentralityInfo::PROP::bimp); //Centrality as defined by the impact parameter
214 
215 
216 
217  SvtxVertexMap* vertexmap = findNode::getClass<SvtxVertexMap>(topNode, "SvtxVertexMap");
218  if (!vertexmap)
219  {
220  std::cout
221  << "Jetrtrack::process_event - Error can not find vertex map node "
222  << std::endl;
223  exit(-1);
224  }
225 
226  PHG4TruthInfoContainer *truthinfo = findNode::getClass<PHG4TruthInfoContainer>(topNode, "G4TruthInfo");
227  if (!truthinfo)
228  {
229  std::cout
230  << "Jetrtrack::process_event - Error can not find G4TruthInfo map node "
231  << std::endl;
232  exit(-1);
233  }
234 
235 
236 
237  //-------------------------------------------------------------
238  //Call the histogram manager in order to
239  //pull the tree into the perview of the event
240  //-------------------------------------------------------------
241 
243  assert(hm);
244 
245 
246 
247  //----------------------------------------------------
248  // loop over vertex information and
249  // extract z-vertex
250  //----------------------------------------------------
251 
252  if (vertexmap)
253  {
254  if (!vertexmap->empty())
255  {
256  SvtxVertex* vertex = (vertexmap->begin()->second);
257  m_zvtx = vertex->get_z();
258  }
259  }
260 
261 
262 
263  //---------------------------------------------------
264  // loop over all truth jets in the event
265  //---------------------------------------------------
266  int chargedparticlespids[] = {11,211,321,2212}; // corresponds to electrons, charged pions, charged kaons, and protons
267 
268  int jetnumber = 0;
269  //---------------------------------
270  // loop over all truth jets
271  //---------------------------------
272  for (JetMap::Iter iter = tjets->begin(); iter != tjets->end(); ++iter)
273  {
274  Jet* tjet = iter->second;
275  assert(tjet);
276  float tjet_phi = tjet->get_phi();
277  float tjet_eta = tjet->get_eta();
278  float tjet_pt = tjet->get_pt();
279  if (tjet_pt < 5){continue;} //Filter out low pT jets to reduce file size
280 
281 
282  //---------------------------------------------------------
283  //loop over truth jet constituent particles
284  //extract the jet constitutent kinematics
285  // from charged particles
286  //---------------------------------------------------------
287 
288  float sumjcpt = 0;
289  for (Jet::ConstIter comp = tjet->begin_comp(); comp != tjet->end_comp(); ++comp)
290  {
291  PHG4Particle* truth = truthinfo->GetParticle((*comp).second);
292  bool reject_particle = true;
293  for (int k = 0 ; k < 4;k++)
294  {
295  if (abs(truth->get_pid()) == chargedparticlespids[k])
296  {
297  reject_particle = false;
298  }
299  }
300  if (reject_particle) {continue;}
301  float m_truthpx = truth->get_px();
302  float m_truthpy = truth->get_py();
303  float m_truthpz = truth->get_pz();
304  float m_truthenergy = truth->get_e();
305  float m_truthpt = sqrt(m_truthpx * m_truthpx + m_truthpy * m_truthpy);
306  float m_truthphi = atan2(m_truthpy , m_truthpx);
307  float m_trutheta = atanh(m_truthpz / m_truthenergy);
308  m_jc_pt.push_back(m_truthpt);
309  m_jc_phi.push_back(m_truthphi);
310  m_jc_eta.push_back(m_trutheta);
311  m_jc_index.push_back(jetnumber);
312  sumjcpt += m_truthpt;
313  }
314  jetnumber++;
315 
316 
317 
318  float drmin = 0.3;
319  float matched_pt = -999;
320  float matched_eta = -999;
321  float matched_phi = -999;
322  //---------------------------------------------------------------------------------------------------
323  //loop over reconstructed jets and match each truth jet to the closest
324  //reconstructed jet
325  //----------------------------------------------------------------------------------------------------
326  for (JetMap::Iter riter = rjets->begin(); riter != rjets->end(); ++riter)
327  {
328  Jet* rjet = riter->second;
329  float rjet_phi = rjet->get_phi();
330  float rjet_eta = rjet->get_eta();
331  float rjet_pt = rjet->get_pt();
332 
333  float deta = fabs(rjet_eta - tjet_eta);
334  float dphi = fabs(rjet_phi - tjet_phi);
335  if (dphi > pi)
336  {
337  dphi = 2*pi-dphi;
338  }
339 
340  float dr = TMath::Sqrt(dphi*dphi + deta*deta);
341  if (dr < drmin)//truth-reco jet matching
342  {
343  matched_pt = rjet_pt;
344  matched_eta = rjet_eta;
345  matched_phi = rjet_phi;
346  drmin = dr;
347  }
348  }
349  //-------------------------------------------------------------------------
350  //append the truth and matched reconstructed jet
351  // to the end of the corresponding vectors
352  //-------------------------------------------------------------------------
353  if (tjet_pt > 0)
354  {
355 
356  m_tjet_pt.push_back(tjet_pt);
357  m_tjet_jcpt.push_back(sumjcpt);
358  m_tjet_phi.push_back(tjet_phi);
359  m_tjet_eta.push_back(tjet_eta);
360  m_rjet_pt.push_back(matched_pt);
361  m_rjet_phi.push_back(matched_phi);
362  m_rjet_eta.push_back(matched_eta);
363  m_dr.push_back(drmin);
364  }
365  }
366 
367  //-------------------------------------------------------------
368  //loop over reconstructed tracks to extract
369  //reconstructed track information
370  //-------------------------------------------------------------
371 
372  if (trackmap)
373  {
374  for (SvtxTrackMap::Iter iter = trackmap->begin();
375  iter != trackmap->end();
376  ++iter)
377  {
378  SvtxTrack* track = iter->second;
379  float quality = track->get_quality();
380  auto silicon_seed = track->get_silicon_seed();
381  int nmvtxhits = 0;
382  if (silicon_seed)
383  {
384  nmvtxhits = silicon_seed->size_cluster_keys();
385  }
386  if (quality < 10) //select on some minimal track quality as reccomended by tracking group
387  {
388  if (track->get_pt() < 1) {continue;} //exclude low pT tracks to reduce file size
389  m_trk_pt.push_back(track->get_pt());
390  m_trk_eta.push_back(track->get_eta());
391  m_trk_phi.push_back(track->get_phi());
392  m_trk_qual.push_back(quality);
393  m_nmvtxhits.push_back(nmvtxhits);
394  }
395  }
396  }
397 
398 
399  //------------------------------------------------------------------------------------------------------
400  // loop over primary truth particles to extract truth particle information
401  //------------------------------------------------------------------------------------------------------
402  if (truthinfo)
403  {
406  for (PHG4TruthInfoContainer::ConstIterator iter = range.first;
407  iter != range.second;
408  ++iter)
409  {
411  const PHG4Particle *truth = iter->second;
412 
413 
415  float m_truthpx = truth->get_px();
416  float m_truthpy = truth->get_py();
417  float m_truthpz = truth->get_pz();
418  float m_truthenergy = truth->get_e();
419 
420  float m_truthpt = sqrt(m_truthpx * m_truthpx + m_truthpy * m_truthpy);
421  float m_truthphi = atan2(m_truthpy , m_truthpx);
422  float m_trutheta = atanh(m_truthpz / m_truthenergy);
423  int m_truthpid = truth->get_pid();
424 
425  if (m_truthpt < 1){continue;}
426  //--------------------------------------------------------------------------------------------------
427  //Filter truth particles to select those which are charged and stablish
428  //--------------------------------------------------------------------------------------------------
429  bool reject_particle = true;
430  for (int k = 0 ; k < 4;k++)
431  {
432  if (abs(truth->get_pid()) == chargedparticlespids[k])
433  {
434  reject_particle = false;
435  }
436  }
437  if (reject_particle) {continue;}
438 
439  m_tp_pt.push_back(m_truthpt);
440  m_tp_px.push_back(m_truthpx);
441  m_tp_py.push_back(m_truthpy);
442  m_tp_pz.push_back(m_truthpz);
443  m_tp_phi.push_back(m_truthphi);
444  m_tp_eta.push_back(m_trutheta);
445  m_tp_pid.push_back(m_truthpid);
446  }
447  }
448  //----------------------------------------------------------
449  //Record the vectors of jet kinematic info
450  //to the ttree.
451  //----------------------------------------------------------
452 
453  TTree *tree = dynamic_cast<TTree *>(hm->getHisto("tree"));
454  assert(tree);
455  tree->Fill();
456 
457 
459 }
460 
461 //____________________________________________________________________________..
463 {
464  //-----------------------------------------------------
465  //Clear every vector inbetween events
466  // this is CRITICAL!! Otherwise you will
467  // have runaway behaviors
468  //------------------------------------------------------
469  m_tjet_pt.clear();
470  m_tjet_jcpt.clear();
471  m_tjet_phi.clear();
472  m_tjet_eta.clear();
473  m_nmvtxhits.clear();
474 
475  m_jc_index.clear();
476  m_tp_pt.clear();
477  m_tp_px.clear();
478  m_tp_py.clear();
479  m_tp_pz.clear();
480  m_tp_phi.clear();
481  m_tp_eta.clear();
482  m_tp_pid.clear();
483 
484 
485  m_jc_pt.clear();
486  m_jc_phi.clear();
487  m_jc_eta.clear();
488 
489  m_rjet_pt.clear();
490  m_rjet_phi.clear();
491  m_rjet_eta.clear();
492  m_dr.clear();
493 
494  m_trk_pt.clear();
495  m_trk_phi.clear();
496  m_trk_eta.clear();
497  m_trk_qual.clear();
498 
500 }
501 
502 //____________________________________________________________________________..
504 {
505  std::cout << "jetrtrack::EndRun(const int runnumber) Ending Run for Run " << runnumber << std::endl;
507 }
508 
509 //____________________________________________________________________________..
511 {
512  std::cout << "jetrtrack::End(PHCompositeNode *topNode) This is the End..." << std::endl;
513  //----------------------------------------------------------------------
514  //Enter the created output file and write the ttree
515  //----------------------------------------------------------------------
516 
518  // PHTFileServer::get().cd("jettree.root");
519 
521  assert(hm);
522 
523  for (unsigned int i = 0; i < hm->nHistos(); i++)
524  hm->getHisto(i)->Write();
525 
526 
528 }
529 
530 //____________________________________________________________________________..
532 {
533  std::cout << "jetrtrack::Reset(PHCompositeNode *topNode) being Reset" << std::endl;
535 }
536 
537 //____________________________________________________________________________..
538 void jetrtrack::Print(const std::string &what) const
539 {
540  std::cout << "jetrtrack::Print(const std::string &what) const Printing info for " << what << std::endl;
541 }