EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SamplingFractionReco.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file SamplingFractionReco.cc
1 
2 #include "SamplingFractionReco.h"
3 
4 #include <g4main/PHG4Hit.h>
6 #include <g4main/PHG4Particle.h>
8 
10 
11 #include <phool/PHCompositeNode.h>
12 #include <phool/getClass.h>
13 
14 #include <TFile.h>
15 #include <TNtuple.h>
16 #include <TSystem.h>
17 
18 #include <iostream> // for operator<<, endl, basic_ost...
19 
20 //____________________________________________________________________________..
21 SamplingFractionReco::SamplingFractionReco(const std::string &name, const std::string &filename)
22  : SubsysReco(name)
23  , outfilename(filename)
24 {
25 }
26 
27 //____________________________________________________________________________..
29 {
30 }
31 
32 //____________________________________________________________________________..
34 {
35  if (m_Detector.empty())
36  {
37  std::cout << "Detector not set via Detector(<name>) method" << std::endl;
38  std::cout << "(it is the name appended to the G4HIT_<name> nodename)" << std::endl;
39  std::cout << "you do not want to run like this, exiting now" << std::endl;
40  gSystem->Exit(1);
41  }
42  outfile = new TFile(outfilename.c_str(), "RECREATE");
43  std::string title = "Sampling Fraction " + m_Detector;
44  ntup = new TNtuple("sfntup", title.c_str(), "theta:phi:eta:p:escin:eabs:eion:light:esum");
46 }
47 
48 //____________________________________________________________________________..
50 {
52 }
53 
54 //____________________________________________________________________________..
56 {
57  PHG4TruthInfoContainer *truthinfo = findNode::getClass<PHG4TruthInfoContainer>(topNode, "G4TruthInfo");
58  double phi = NAN;
59  double eta = NAN;
60  double theta = NAN;
61  double mom = NAN;
62  if (truthinfo)
63  {
65  int justone = 0;
66 
67  for (PHG4TruthInfoContainer::ConstIterator iter = range.first;
68  iter != range.second;
69  ++iter)
70  {
71  justone++;
72  PHG4Particle *primary = iter->second;
73  double gpx = primary->get_px();
74  double gpy = primary->get_py();
75  phi = atan2(gpy, gpx) * 180. / M_PI;
76  double gpz = primary->get_pz();
77  double gpt = std::sqrt(gpx * gpx + gpy * gpy);
78  mom = std::sqrt(gpx * gpx + gpy * gpy + gpz * gpz);
79  if (gpt > 0)
80  {
81  eta = asinh(gpz / gpt);
82  }
83  }
84  if (justone > 1)
85  {
86  std::cout << "this only works for single particle events"
87  << " here I see " << justone << " primaries" << std::endl;
88  gSystem->Exit(1);
89  }
90  }
91  // add hits
92  PHG4HitContainer *g4hits = findNode::getClass<PHG4HitContainer>(topNode, m_HitNodeName);
93 
94  double escin = 0;
95  double eabs = 0.;
96  double eion = 0.;
97  double light = 0.;
98  double esum = 0.;
99  if (g4hits)
100  {
101  PHG4HitContainer::ConstRange hit_range = g4hits->getHits();
102  for (PHG4HitContainer::ConstIterator hit_iter = hit_range.first; hit_iter != hit_range.second; hit_iter++)
103  {
104  escin += hit_iter->second->get_edep();
105  eion += hit_iter->second->get_eion();
106  light += hit_iter->second->get_light_yield();
107  }
108  }
109  else
110  {
111  std::cout << "could not find " << m_HitNodeName << std::endl;
112  }
113  g4hits = findNode::getClass<PHG4HitContainer>(topNode, m_AbsorberNodeName);
114  if (g4hits)
115  {
116  PHG4HitContainer::ConstRange hit_range = g4hits->getHits();
117  for (PHG4HitContainer::ConstIterator hit_iter = hit_range.first; hit_iter != hit_range.second; hit_iter++)
118  {
119  eabs += hit_iter->second->get_edep();
120  }
121  }
122  else
123  {
124  std::cout << "could not find " << m_AbsorberNodeName << std::endl;
125  }
126  if (m_SupportFlag)
127  {
128  g4hits = findNode::getClass<PHG4HitContainer>(topNode, m_SupportNodeName);
129  if (g4hits)
130  {
131  PHG4HitContainer::ConstRange hit_range = g4hits->getHits();
132  for (PHG4HitContainer::ConstIterator hit_iter = hit_range.first; hit_iter != hit_range.second; hit_iter++)
133  {
134  eabs += hit_iter->second->get_edep();
135  }
136  }
137  else
138  {
139  std::cout << "could not find " << m_SupportNodeName << std::endl;
140  }
141  }
142  esum = escin + eabs;
143  ntup->Fill(theta, phi, eta, mom, escin, eabs, eion, light, esum);
145 }
146 
147 //____________________________________________________________________________..
149 {
151 }
152 
153 //____________________________________________________________________________..
154 int SamplingFractionReco::EndRun(const int runnumber)
155 {
157 }
158 
159 //____________________________________________________________________________..
161 {
162  outfile->cd();
163  ntup->Write();
164  outfile->Write();
165  outfile->Close();
166  delete outfile;
168 }
169 
170 //____________________________________________________________________________..
172 {
174 }
175 
176 //____________________________________________________________________________..
177 void SamplingFractionReco::Print(const std::string &what) const
178 {
179  std::cout << "SamplingFractionReco::Print(const std::string &what) const Printing info for " << what << std::endl;
180 }
181 
182 void SamplingFractionReco::Detector(const std::string &name)
183 {
184  m_Detector = name;
185  m_HitNodeName = "G4HIT_" + name;
186  m_AbsorberNodeName = "G4HIT_ABSORBER_" + name;
187  m_SupportNodeName = "G4HIT_SUPPORT_" + name;
188 }