Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TimingNtuple.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file TimingNtuple.cc
1 #include "TimingNtuple.h"
2 
4 #include <g4main/PHG4Hit.h>
5 
7 #include <g4main/PHG4Particle.h>
8 #include <g4main/PHG4VtxPoint.h>
9 
11 
12 #include <phool/getClass.h>
13 
14 #include <TFile.h>
15 #include <TH1.h>
16 #include <TH2.h>
17 #include <TNtuple.h>
18 
19 #include <boost/foreach.hpp>
20 
21 #include<sstream>
22 
23 using namespace std;
24 
26  SubsysReco( name ),
27  nblocks(0),
28  hm(NULL),
29  _filename(filename),
30  ntup(NULL),
31  outfile(NULL)
32 {}
33 
35 {
36  // delete ntup;
37  delete hm;
38 }
39 
40 
41 int
43 {
44  ostringstream hname, htit;
45  hm = new Fun4AllHistoManager(Name());
46  outfile = new TFile(_filename.c_str(), "RECREATE");
47  //ntup = new TNtuple("hitntup", "G4Hits", "detid:layer:x0:y0:z0:x1:y1:z1:edep");
48  ntupt = new TNtuple("tnt", "G4Hits", "ID:t:dtotal");
49 // ntupavet = new TNtuple("time", "G4Hits", "ID:avt");
50  // ntup->SetDirectory(0);
51  TH1 *h1 = new TH1F("edep1GeV","edep 0-1GeV",1000,0,1);
52  eloss.push_back(h1);
53  h1 = new TH1F("edep100GeV","edep 0-100GeV",1000,0,100);
54  eloss.push_back(h1);
55  return 0;
56 }
57 
58 int
60 {
61  ostringstream nodename;
62 
63  map<int, PHG4Particle*>::const_iterator particle_iter;
64  PHG4TruthInfoContainer *_truth_container = findNode::getClass<PHG4TruthInfoContainer>(topNode, "G4TruthInfo");
65 
67  _truth_container->GetPrimaryParticleRange();
68  float ntvars[5] = {0};
69  for (PHG4TruthInfoContainer::ConstIterator particle_iter = primary_range.first;
70  particle_iter != primary_range.second; ++particle_iter)
71  {
72  PHG4Particle *particle = particle_iter->second;
73  ntvars[0] = atan2(particle->get_py(), particle->get_px());
74  ntvars[1] = atan(sqrt(particle->get_py()*particle->get_py() +
75  particle->get_px()*particle->get_px()) /
76  particle->get_pz());
77  if (ntvars[1] < 0)
78  {
79  ntvars[1]+=M_PI;
80  }
81  ntvars[2] = 0.5*log((particle->get_e()+particle->get_pz())/
82  (particle->get_e()-particle->get_pz()));
83  ntvars[3] = particle->get_e();
84  ntvars[4] = particle->get_pz();
85  }
86 
87  set<string>::const_iterator iter;
88  vector<TH1 *>::const_iterator eiter;
89 
90  PHG4HitContainer *hits = nullptr;
91 
92  hits = findNode::getClass<PHG4HitContainer>(topNode,"G4HIT_CEMC");
93 
94  if (hits)
95  {
96  // double numhits = hits->size();
97  // nhits[i]->Fill(numhits);
98  PHG4HitContainer::ConstRange hit_range = hits->getHits();
99  for ( PHG4HitContainer::ConstIterator hit_iter = hit_range.first ; hit_iter != hit_range.second; hit_iter++ )
100 
101  {
102  double dtotal = get_dtotal(hit_iter->second, ntvars[0],ntvars[1]);
103  ntupt->Fill(0,hit_iter->second->get_avg_t(),dtotal);
104  }
105  }
106 
107 
108  hits = findNode::getClass<PHG4HitContainer>(topNode,"G4HIT_HCALIN");
109 
110  if (hits)
111  {
112  // double numhits = hits->size();
113  // nhits[i]->Fill(numhits);
114  PHG4HitContainer::ConstRange hit_range = hits->getHits();
115  for ( PHG4HitContainer::ConstIterator hit_iter = hit_range.first ; hit_iter != hit_range.second; hit_iter++ )
116 
117  {
118  double dtotal = get_dtotal(hit_iter->second, ntvars[0],ntvars[1]);
119  ntupt->Fill(1,hit_iter->second->get_avg_t(),dtotal);
120  }
121  }
122 
123  hits = findNode::getClass<PHG4HitContainer>(topNode,"G4HIT_HCALOUT");
124 
125  if (hits)
126  {
127  // double numhits = hits->size();
128  // nhits[i]->Fill(numhits);
129  PHG4HitContainer::ConstRange hit_range = hits->getHits();
130  for ( PHG4HitContainer::ConstIterator hit_iter = hit_range.first ; hit_iter != hit_range.second; hit_iter++ )
131 
132  {
133  double dtotal = get_dtotal(hit_iter->second, ntvars[0],ntvars[1]);
134  ntupt->Fill(2,hit_iter->second->get_avg_t(),dtotal);
135  }
136  }
137 
138  return 0;
139 }
140 
141 int
143 {
144  outfile->cd();
145  // ntup->Write();
146  ntupt->Write();
147  outfile->Write();
148  outfile->Close();
149  delete outfile;
150  hm->dumpHistos(_filename, "UPDATE");
151  return 0;
152 }
153 
154 void
155 TimingNtuple::AddNode(const std::string &name, const int detid)
156 {
157  _node_postfix.insert(name);
158  _detid[name] = detid;
159  return;
160 }
161 
162 double
163 TimingNtuple::get_dtotal(const PHG4Hit *hit, const double phi, const double theta)
164 {
165  double phi_hit = atan2(hit->get_avg_y(),hit->get_avg_x());
166  double theta_hit = atan(sqrt(hit->get_avg_x()*hit->get_avg_x()+
167  hit->get_avg_y()*hit->get_avg_y()) /
168  hit->get_avg_z());
169  double diffphi = phi_hit - phi;
170  if (diffphi > M_PI)
171  {
172  diffphi -= 2*M_PI;
173  }
174  else if (diffphi < - M_PI)
175  {
176  diffphi += 2*M_PI;
177  }
178  if (theta_hit < 0)
179  {
180  theta_hit += M_PI;
181  }
182  double difftheta = theta_hit-theta;
183  double deltasqrt = sqrt(diffphi*diffphi+difftheta*difftheta);
184  return deltasqrt;
185 }