Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4TowerNtuple.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file G4TowerNtuple.cc
1 #include "G4TowerNtuple.h"
2 
3 #include <calobase/TowerInfo.h>
4 #include <calobase/TowerInfoContainerv1.h>
5 #include <calobase/RawTowerGeomContainer.h>
6 
8 #include <fun4all/SubsysReco.h> // for SubsysReco
9 
10 #include <phool/getClass.h>
11 
12 #include <TFile.h>
13 #include <TH1.h>
14 #include <TNtuple.h>
15 
16 #include <cmath> // for isfinite
17 #include <iostream> // for operator<<, basic_ostream
18 #include <sstream>
19 #include <utility> // for pair, make_pair
20 
21 using namespace std;
22 
24  : SubsysReco(name)
25  , nblocks(0)
26  , hm(nullptr)
27  , _filename(filename)
28  , ntup(nullptr)
29  , outfile(nullptr)
30 {
31 }
32 
34 {
35  // delete ntup;
36  delete hm;
37 }
38 
40 {
41  hm = new Fun4AllHistoManager(Name());
42  outfile = new TFile(_filename.c_str(), "RECREATE");
43  ntup = new TNtuple("towerntup", "G4Towers", "detid:phi:eta:energy");
44  // ntup->SetDirectory(0);
45  TH1 *h1 = new TH1F("energy1GeV", "energy 0-1GeV", 1000, 0, 1);
46  eloss.push_back(h1);
47  h1 = new TH1F("energy100GeV", "energy 0-100GeV", 1000, 0, 100);
48  eloss.push_back(h1);
49  return 0;
50 }
51 
53 {
54  ostringstream nodename;
55  ostringstream geonodename;
56  set<string>::const_iterator iter;
57  vector<TH1 *>::const_iterator eiter;
58  for (iter = _node_postfix.begin(); iter != _node_postfix.end(); ++iter)
59  {
60  int detid = (_detid.find(*iter))->second;
61  nodename.str("");
62  nodename << "TOWERINFO_" << _tower_type[*iter];
63  geonodename.str("");
64  geonodename << "TOWERGEOM_" << *iter;
65  RawTowerGeomContainer *towergeom = findNode::getClass<RawTowerGeomContainer>(topNode, geonodename.str());
66  if (!towergeom)
67  {
68  cout << "no geometry node " << geonodename.str() << " for " << *iter << endl;
69  continue;
70  }
71 
72 
73  TowerInfoContainer *towers = findNode::getClass<TowerInfoContainerv1>(topNode, nodename.str());
74  if (towers)
75  {
76  double esum = 0;
77  unsigned int nchannels = towers->size();
78  for (unsigned int channel = 0; channel < nchannels;channel++)
79  {
81  double energy = tower->get_energy();
82  if (!isfinite(energy))
83  {
84  cout << "invalid energy: " << energy << endl;
85  }
86  esum += energy;
87  unsigned int towerkey = towers->encode_key(channel);
88  int etabin = towers->getTowerEtaBin(towerkey);
89  int phibin = towers->getTowerPhiBin(towerkey);
90 
91  // to search the map fewer times, cache the geom object until the layer changes
92  double phi = towergeom->get_phicenter(phibin);
93  double eta = towergeom->get_etacenter(etabin);
94  ntup->Fill(detid,
95  phi,
96  eta,
97  energy);
98  }
99  for (eiter = eloss.begin(); eiter != eloss.end(); ++eiter)
100  {
101  (*eiter)->Fill(esum);
102  }
103  }
104  }
105  return 0;
106 }
107 
109 {
110  outfile->cd();
111  ntup->Write();
112  outfile->Write();
113  outfile->Close();
114  delete outfile;
115  hm->dumpHistos(_filename, "UPDATE");
116  return 0;
117 }
118 
119 void G4TowerNtuple::AddNode(const std::string &name, const std::string &twrtype, const int detid)
120 {
121  ostringstream twrname;
122  twrname << twrtype << "_" << name;
123  _node_postfix.insert(name);
124  _tower_type.insert(make_pair(name, twrname.str()));
125  _detid[name] = detid;
126  return;
127 }