ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SimpleNtuple.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file SimpleNtuple.cc
1 #include "SimpleNtuple.h"
2 
3 #include <g4main/PHG4Hit.h>
5 
7 #include <fun4all/SubsysReco.h> // for SubsysReco
8 
9 #include <phool/getClass.h>
10 
11 #include <TFile.h>
12 #include <TH1.h>
13 #include <TNtuple.h>
14 
15 #include <sstream>
16 #include <utility> // for pair
17 
18 using namespace std;
19 
20 SimpleNtuple::SimpleNtuple(const std::string &name, const std::string &filename)
21  : SubsysReco(name)
22  , m_HistoManager(nullptr)
23  , m_Ntup(nullptr)
24  , m_Outfile(nullptr)
25  , m_Filename(filename)
26 {
27 }
28 
30 {
31  delete m_HistoManager;
32 }
33 
35 {
37  m_Outfile = new TFile(m_Filename.c_str(), "RECREATE");
38  m_Ntup = new TNtuple("hitntup", "G4Hits", "absorber:detid:x0:y0:z0:x1:y1:z1:edep");
39  // ntup->SetDirectory(0);
40  TH1 *h1 = new TH1F("edep1GeV", "edep 0-1GeV", 1000, 0, 1);
41  m_ElossVec.push_back(h1);
42  h1 = new TH1F("edep100GeV", "edep 0-100GeV", 1000, 0, 100);
43  m_ElossVec.push_back(h1);
44  return 0;
45 }
46 
48 {
49  ostringstream nodename;
50  set<string>::const_iterator iter;
51  vector<TH1 *>::const_iterator eiter;
52  for (iter = m_NodePostfixSet.begin(); iter != m_NodePostfixSet.end(); ++iter)
53  {
54  int detid = (m_DetIdMap.find(*iter))->second;
55  nodename.str("");
56  nodename << "G4HIT_" << *iter;
57  PHG4HitContainer *hits = findNode::getClass<PHG4HitContainer>(topNode, nodename.str());
58  if (hits)
59  {
60  double esum = 0;
61  PHG4HitContainer::ConstRange hit_range = hits->getHits();
62  for (PHG4HitContainer::ConstIterator hit_iter = hit_range.first; hit_iter != hit_range.second; hit_iter++)
63 
64  {
65  esum += hit_iter->second->get_edep();
66  m_Ntup->Fill(detid,
67  (int) hit_iter->second->get_layer(), // get_layer is unsigned so cast it back to an int for negative values
68  hit_iter->second->get_x(0),
69  hit_iter->second->get_y(0),
70  hit_iter->second->get_z(0),
71  hit_iter->second->get_x(1),
72  hit_iter->second->get_y(1),
73  hit_iter->second->get_z(1),
74  hit_iter->second->get_edep());
75  }
76  for (eiter = m_ElossVec.begin(); eiter != m_ElossVec.end(); ++eiter)
77  {
78  (*eiter)->Fill(esum);
79  }
80  }
81  }
82  return 0;
83 }
84 
86 {
87  m_Outfile->cd();
88  m_Ntup->Write();
89  m_Outfile->Write();
90  m_Outfile->Close();
91  delete m_Outfile;
93  return 0;
94 }
95 
96 void SimpleNtuple::AddNode(const std::string &name, const int detid)
97 {
98  m_NodePostfixSet.insert(name);
99  m_DetIdMap[name] = detid;
100  return;
101 }