Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4EdepNtuple.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file G4EdepNtuple.cc
1 #include "G4EdepNtuple.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 <TNtuple.h>
13 
14 #include <sstream>
15 #include <utility> // for pair
16 
17 using namespace std;
18 
20  : SubsysReco(name)
21  , nblocks(0)
22  , hm(nullptr)
23  , _filename(filename)
24  , ntup(nullptr)
25  , outfile(nullptr)
26 {
27 }
28 
30 {
31  delete hm;
32 }
33 
35 {
36  hm = new Fun4AllHistoManager(Name());
37  outfile = new TFile(_filename.c_str(), "RECREATE");
38  ntup = new TNtuple("edepntup", "G4Edeps", "detid:layer:edep");
39  return 0;
40 }
41 
43 {
44  ostringstream nodename;
45  set<string>::const_iterator iter;
46  map<int, double> layer_edep_map;
47  map<int, double>::const_iterator edepiter;
48  for (iter = _node_postfix.begin(); iter != _node_postfix.end(); ++iter)
49  {
50  layer_edep_map.clear();
51  int detid = (_detid.find(*iter))->second;
52  nodename.str("");
53  nodename << "G4HIT_" << *iter;
54  PHG4HitContainer *hits = findNode::getClass<PHG4HitContainer>(topNode, nodename.str());
55  if (hits)
56  {
57  double esum = 0;
58  // double numhits = hits->size();
59  // nhits[i]->Fill(numhits);
60  PHG4HitContainer::ConstRange hit_range = hits->getHits();
61  for (PHG4HitContainer::ConstIterator hit_iter = hit_range.first; hit_iter != hit_range.second; hit_iter++)
62 
63  {
64  layer_edep_map[hit_iter->second->get_layer()] += hit_iter->second->get_edep();
65  esum += hit_iter->second->get_edep();
66  }
67  for (edepiter = layer_edep_map.begin(); edepiter != layer_edep_map.end(); ++edepiter)
68  {
69  ntup->Fill(detid, edepiter->first, edepiter->second);
70  }
71  ntup->Fill(detid, -1, esum); // fill sum over all layers for each detector
72  }
73  }
74  return 0;
75 }
76 
78 {
79  outfile->cd();
80  ntup->Write();
81  outfile->Write();
82  outfile->Close();
83  delete outfile;
84  hm->dumpHistos(_filename, "UPDATE");
85  return 0;
86 }
87 
88 void G4EdepNtuple::AddNode(const std::string &name, const int detid)
89 {
90  _node_postfix.insert(name);
91  _detid[name] = detid;
92  return;
93 }