Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PHG4Prototype2HcalCellReco.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file PHG4Prototype2HcalCellReco.cc
3 #include <g4detectors/PHG4ScintillatorSlat.h> // for PHG4ScintillatorSlat
5 #include <g4detectors/PHG4ScintillatorSlatDefs.h> // for genkey, keytype
6 
7 #include <g4main/PHG4Hit.h>
9 
10 #include <phparameter/PHParameterInterface.h> // for PHParameterInterface
11 
13 #include <fun4all/Fun4AllServer.h>
14 #include <fun4all/SubsysReco.h> // for SubsysReco
15 
16 #include <phool/PHCompositeNode.h>
17 #include <phool/PHIODataNode.h>
18 #include <phool/PHNode.h> // for PHNode
19 #include <phool/PHNodeIterator.h>
20 #include <phool/PHObject.h> // for PHObject
21 #include <phool/getClass.h>
22 #include <phool/phool.h> // for PHWHERE
23 
24 #include <TSystem.h>
25 
26 #include <array> // for array, array<>::value_...
27 #include <cmath>
28 #include <cstdlib>
29 #include <iostream>
30 #include <map> // for _Rb_tree_const_iterator
31 #include <sstream>
32 #include <utility> // for pair
33 
34 using namespace std;
35 // for hcal dimension
36 // prototype outer hcal, 1st and 2nd prototype inner hcal, 3rd prototype inner hcal is 17
37 #define ROWDIM 21
38 // 12 scintillator tiles per row (index starting at 1)
39 #define COLUMNDIM 13
40 static array<array<PHG4ScintillatorSlat *, COLUMNDIM>, ROWDIM> slatarray = {{{nullptr}}};
41 
43  : SubsysReco(name)
44  , PHParameterInterface(name)
45  , m_CheckEnergyConservationFlag(0)
46  , m_Tmin(NAN)
47  , m_Tmax(NAN)
48 {
50 }
51 
53 {
54  PHNodeIterator iter(topNode);
55 
56  // Looking for the DST node
57  PHCompositeNode *dstNode;
58  dstNode = dynamic_cast<PHCompositeNode *>(iter.findFirst("PHCompositeNode", "DST"));
59  if (!dstNode)
60  {
61  std::cout << PHWHERE << "DST Node missing, doing nothing." << std::endl;
62  gSystem->Exit(1);
63  exit(1);
64  }
65  PHCompositeNode *runNode = dynamic_cast<PHCompositeNode *>(iter.findFirst("PHCompositeNode", "RUN"));
66  PHCompositeNode *parNode = dynamic_cast<PHCompositeNode *>(iter.findFirst("PHCompositeNode", "PAR"));
67 
68  string paramnodename = "G4CELLPARAM_" + m_Detector;
69  string geonodename = "G4CELLGEO_" + m_Detector;
70 
71  m_HitNodeName = "G4HIT_" + m_Detector;
72  PHG4HitContainer *g4hit = findNode::getClass<PHG4HitContainer>(topNode, m_HitNodeName);
73  if (!g4hit)
74  {
75  cout << "Could not locate g4 hit node " << m_HitNodeName << endl;
77  se->Print("NODETREE");
78  gSystem->Exit(1);
79  exit(1);
80  }
81  m_CellNodeName = "G4CELL_" + m_Detector;
82  PHG4ScintillatorSlatContainer *slats = findNode::getClass<PHG4ScintillatorSlatContainer>(topNode, m_CellNodeName);
83  if (!slats)
84  {
85  PHNodeIterator dstiter(dstNode);
86  PHCompositeNode *DetNode = dynamic_cast<PHCompositeNode *>(dstiter.findFirst("PHCompositeNode", m_Detector));
87  if (!DetNode)
88  {
89  DetNode = new PHCompositeNode(m_Detector);
90  dstNode->addNode(DetNode);
91  }
92  slats = new PHG4ScintillatorSlatContainer();
93  PHIODataNode<PHObject> *newNode = new PHIODataNode<PHObject>(slats, m_CellNodeName, "PHObject");
94  DetNode->addNode(newNode);
95  }
97  PHNodeIterator runIter(runNode);
98  PHCompositeNode *RunDetNode = dynamic_cast<PHCompositeNode *>(runIter.findFirst("PHCompositeNode", m_Detector));
99  if (!RunDetNode)
100  {
101  RunDetNode = new PHCompositeNode(m_Detector);
102  runNode->addNode(RunDetNode);
103  }
104  SaveToNodeTree(RunDetNode, paramnodename);
105  // save this to the parNode for use
106  PHNodeIterator parIter(parNode);
107  PHCompositeNode *ParDetNode = dynamic_cast<PHCompositeNode *>(parIter.findFirst("PHCompositeNode", m_Detector));
108  if (!ParDetNode)
109  {
110  ParDetNode = new PHCompositeNode(m_Detector);
111  parNode->addNode(ParDetNode);
112  }
113  PutOnParNode(ParDetNode, geonodename);
114 
115  m_Tmin = get_double_param("tmin");
116  m_Tmax = get_double_param("tmax");
118 }
119 
121 {
122  PHG4HitContainer *g4hit = findNode::getClass<PHG4HitContainer>(topNode, m_HitNodeName);
123  if (!g4hit)
124  {
125  cout << "Could not locate g4 hit node " << m_HitNodeName << endl;
126  exit(1);
127  }
128  PHG4ScintillatorSlatContainer *slats = findNode::getClass<PHG4ScintillatorSlatContainer>(topNode, m_CellNodeName);
129  if (!slats)
130  {
131  cout << "could not locate cell node " << m_CellNodeName << endl;
132  exit(1);
133  }
134 
136  pair<PHG4HitContainer::LayerIter, PHG4HitContainer::LayerIter> layer_begin_end = g4hit->getLayers();
137  for (layer = layer_begin_end.first; layer != layer_begin_end.second; ++layer)
138  {
140  PHG4HitContainer::ConstRange hit_begin_end = g4hit->getHits(*layer);
141  for (hiter = hit_begin_end.first; hiter != hit_begin_end.second; ++hiter)
142  {
143  if (hiter->second->get_t(0) > m_Tmax) continue;
144  if (hiter->second->get_t(1) < m_Tmin) continue;
145  short icolumn = hiter->second->get_scint_id();
146  short irow = hiter->second->get_row();
147  if (irow >= ROWDIM || irow < 0)
148  {
149  cout << "row " << irow
150  << " exceed array size: " << ROWDIM
151  << " adjust ROWDIM and recompile" << endl;
152  gSystem->Exit(1);
153  }
154 
155  if (icolumn >= COLUMNDIM || icolumn < 0)
156  {
157  cout << "column: " << icolumn
158  << " exceed array size: " << COLUMNDIM
159  << " adjust COLUMNDIM and recompile" << endl;
160  gSystem->Exit(1);
161  }
162 
163  if (!slatarray[irow][icolumn])
164  {
165  slatarray[irow][icolumn] = new PHG4ScintillatorSlatv1();
166  }
167  slatarray[irow][icolumn]->add_edep(hiter->second->get_edep(),
168  hiter->second->get_eion(),
169  hiter->second->get_light_yield());
170  slatarray[irow][icolumn]->add_hit_key(hiter->first);
171  // cout << "row: " << hiter->second->get_row()
172  // << ", column: " << hiter->second->get_scint_id() << endl;
173  // checking ADC timing integration window cut
174  } // end loop over g4hits
175  int nslathits = 0;
176  for (int irow = 0; irow < ROWDIM; irow++)
177  {
178  for (int icolumn = 0; icolumn < COLUMNDIM; icolumn++)
179  {
180  if (slatarray[irow][icolumn])
181  {
183  slats->AddScintillatorSlat(key, slatarray[irow][icolumn]);
184  slatarray[irow][icolumn] = nullptr;
185  nslathits++;
186  }
187  }
188  }
189  if (Verbosity() > 0)
190  {
191  cout << Name() << ": found " << nslathits << " slats with energy deposition" << endl;
192  }
193  }
194 
196  {
197  CheckEnergy(topNode);
198  }
200 }
201 
203 {
204  PHG4HitContainer *g4hit = findNode::getClass<PHG4HitContainer>(topNode, m_HitNodeName);
205  PHG4ScintillatorSlatContainer *slats = findNode::getClass<PHG4ScintillatorSlatContainer>(topNode, m_CellNodeName);
206  double sum_energy_g4hit = 0.;
207  double sum_energy_cells = 0.;
208  PHG4HitContainer::ConstRange hit_begin_end = g4hit->getHits();
210  for (hiter = hit_begin_end.first; hiter != hit_begin_end.second; ++hiter)
211  {
212  sum_energy_g4hit += hiter->second->get_edep();
213  }
214  PHG4ScintillatorSlatContainer::ConstRange cell_begin_end = slats->getScintillatorSlats();
216  for (citer = cell_begin_end.first; citer != cell_begin_end.second; ++citer)
217  {
218  sum_energy_cells += citer->second->get_edep();
219  }
220  // the fractional eloss for particles traversing eta bins leads to minute rounding errors
221  if (fabs(sum_energy_cells - sum_energy_g4hit) / sum_energy_g4hit > 1e-6)
222  {
223  cout << "energy mismatch between cells: " << sum_energy_cells
224  << " and hits: " << sum_energy_g4hit
225  << " diff sum(cells) - sum(hits): " << sum_energy_cells - sum_energy_g4hit
226  << endl;
227  return -1;
228  }
229  else
230  {
231  if (Verbosity() > 0)
232  {
233  cout << Name() << ": total energy for this event: " << sum_energy_g4hit << " GeV" << endl;
234  }
235  }
236  return 0;
237 }
238 
240 {
241  set_default_double_param("tmax", 60.0);
242  set_default_double_param("tmin", 0.0);
243  return;
244 }
245 
246 void PHG4Prototype2HcalCellReco::set_timing_window(const double tmi, const double tma)
247 {
248  set_double_param("tmin", tmi);
249  set_double_param("tmax", tma);
250 }