ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
EICG4RPHitTree.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file EICG4RPHitTree.cc
1 // - 08/10/2021 TTree production for RP G4 hits
2 //
3 #include "EICG4RPHitTree.h"
4 
5 #include <g4main/PHG4Hit.h>
7 
9 #include <fun4all/SubsysReco.h> // for SubsysReco
10 
11 #include <phool/getClass.h>
12 
13 #include <TFile.h>
14 #include <TH1.h>
15 #include <TTree.h>
16 
17 #include <sstream>
18 #include <utility>
19 
20 EICG4RPHitTree::EICG4RPHitTree(const std::string &name, const std::string &filename)
21  : SubsysReco(name)
22  , _filename(filename)
23  , tree(nullptr)
24  , outfile(nullptr)
25  , Nhit(0)
26 {
27 }
28 
30 {
31 }
32 
34 {
35  outfile = new TFile(_filename.c_str(), "RECREATE");
36  tree = new TTree("rphit", "Collection of EICG4 RP G4Hits");
37 
38  tree->Branch("Nhit", &Nhit, "Nhit/I");
39  tree->Branch("layerType", &layerType);
40  tree->Branch("layerID", &layerID);
41  tree->Branch("xID", &xID);
42  tree->Branch("yID", &yID);
43  tree->Branch("x0", &x0);
44  tree->Branch("y0", &y0);
45  tree->Branch("z0", &z0);
46  tree->Branch("x1", &x1);
47  tree->Branch("y1", &y1);
48  tree->Branch("z1", &z1);
49  tree->Branch("time0", &time0);
50  tree->Branch("time1", &time1);
51  tree->Branch("edep", &edep);
52 
53  return 0;
54 }
55 
57 {
58  std::ostringstream nodename;
59  std::set<std::string>::const_iterator iter;
60  for (iter = _node_postfix.begin(); iter != _node_postfix.end(); ++iter)
61  {
62  nodename.str("");
63  nodename << "G4HIT_" << *iter;
64  PHG4HitContainer *hits = findNode::getClass<PHG4HitContainer>(topNode, nodename.str());
65  if (!hits) return 0;
66 
67  Nhit = hits->size();
68 
69  PHG4HitContainer::ConstRange hit_range = hits->getHits();
70  for (PHG4HitContainer::ConstIterator hit_iter = hit_range.first; hit_iter != hit_range.second; hit_iter++)
71  {
72  // cout << " ---> !!! hit_iter->first = " << hit_iter->first << " hit_iter->second->get_hit_type() = " << hit_iter->second->get_hit_type() << endl;
73  // hit_iter->second->identify();
74  // hit_iter->second->print();
75  if (hit_iter->second->get_hit_type() < 0) continue;
76 
77  layerType.push_back(hit_iter->second->get_hit_type());
78  layerID.push_back(hit_iter->second->get_layer());
79  xID.push_back(hit_iter->second->get_index_i());
80  yID.push_back(hit_iter->second->get_index_j());
81  x0.push_back(hit_iter->second->get_x(0));
82  y0.push_back(hit_iter->second->get_y(0));
83  z0.push_back(hit_iter->second->get_z(0));
84  x1.push_back(hit_iter->second->get_x(1));
85  y1.push_back(hit_iter->second->get_y(1));
86  z1.push_back(hit_iter->second->get_z(1));
87  time0.push_back(hit_iter->second->get_t(0));
88  time1.push_back(hit_iter->second->get_t(1));
89  edep.push_back(hit_iter->second->get_edep());
90  }
91 
92  tree->Fill();
93 
94  layerType.clear();
95  layerID.clear();
96  xID.clear();
97  yID.clear();
98  x0.clear();
99  y0.clear();
100  z0.clear();
101  x1.clear();
102  y1.clear();
103  z1.clear();
104  time0.clear();
105  time1.clear();
106  edep.clear();
107  }
108  return 0;
109 }
110 
112 {
113  outfile->cd();
114  tree->Write();
115  outfile->Write();
116  outfile->Close();
117  delete outfile;
118  return 0;
119 }
120 
121 void EICG4RPHitTree::AddNode(const std::string &name, const int detid)
122 {
123  _node_postfix.insert(name);
124  _detid[name] = detid;
125  return;
126 }