Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RawTowerBuilderByHitIndex.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file RawTowerBuilderByHitIndex.cc
2 
3 #include <calobase/RawTowerContainer.h>
4 #include <calobase/RawTowerv1.h>
5 
6 #include <calobase/RawTower.h> // for RawTower
7 #include <calobase/RawTowerDefs.h> // for convert_name_to_caloid
8 #include <calobase/RawTowerGeom.h> // for RawTowerGeom
9 #include <calobase/RawTowerGeomContainer.h> // for RawTowerGeomContainer
10 #include <calobase/RawTowerGeomContainerv1.h>
11 #include <calobase/RawTowerGeomv3.h>
12 
13 #include <g4main/PHG4Hit.h>
15 
17 #include <fun4all/SubsysReco.h> // for SubsysReco
18 
19 #include <phool/PHCompositeNode.h>
20 #include <phool/PHIODataNode.h>
21 #include <phool/PHNode.h> // for PHNode
22 #include <phool/PHNodeIterator.h>
23 #include <phool/PHObject.h> // for PHObject
24 #include <phool/getClass.h>
25 #include <phool/phool.h> // for PHWHERE
26 
27 #include <TRotation.h>
28 #include <TVector3.h>
29 
30 #include <cstdlib> // for exit
31 #include <exception> // for exception
32 #include <fstream>
33 #include <iostream>
34 #include <map>
35 #include <sstream>
36 #include <stdexcept>
37 #include <utility> // for pair, make_pair
38 
40  : SubsysReco(name)
41 {
42 }
43 
45 {
46  PHNodeIterator iter(topNode);
47 
48  // Looking for the DST node
49  PHCompositeNode *dstNode;
50  dstNode = dynamic_cast<PHCompositeNode *>(iter.findFirst("PHCompositeNode", "DST"));
51  if (!dstNode)
52  {
53  std::cout << PHWHERE << "DST Node missing, doing nothing." << std::endl;
54  exit(1);
55  }
56 
57  try
58  {
59  CreateNodes(topNode);
60  }
61  catch (std::exception &e)
62  {
63  std::cout << e.what() << std::endl;
64  //exit(1);
65  }
66 
67  try
68  {
70  }
71  catch (std::exception &e)
72  {
73  std::cout << e.what() << std::endl;
74  //exit(1);
75  }
76 
78 }
79 
81 {
82  // get hits
83  std::string NodeNameHits = "G4HIT_" + m_Detector;
84  PHG4HitContainer *g4hit = findNode::getClass<PHG4HitContainer>(topNode, NodeNameHits);
85  if (!g4hit)
86  {
87  std::cout << "Could not locate g4 hit node " << NodeNameHits << std::endl;
88  exit(1);
89  }
90 
91  // loop over all hits in the event
93  PHG4HitContainer::ConstRange hit_begin_end = g4hit->getHits();
94 
95  for (hiter = hit_begin_end.first; hiter != hit_begin_end.second; hiter++)
96  {
97  PHG4Hit *g4hit_i = hiter->second;
98 
99  // Don't include hits with zero energy
100  if (g4hit_i->get_edep() <= 0 && g4hit_i->get_edep() != -1) continue;
101 
102  /* encode CaloTowerID from j, k index of tower / hit and calorimeter ID */
104  g4hit_i->get_index_j(),
105  g4hit_i->get_index_k());
106 
107  /* add the energy to the corresponding tower */
108  RawTowerv1 *tower = dynamic_cast<RawTowerv1 *>(m_Towers->getTower(calotowerid));
109  if (!tower)
110  {
111  tower = new RawTowerv1(calotowerid);
112  tower->set_energy(0);
113  m_Towers->AddTower(tower->get_id(), tower);
114  }
115 
116  tower->add_ecell((g4hit_i->get_index_j() << 16U) + g4hit_i->get_index_k(), g4hit_i->get_light_yield());
117  tower->set_energy(tower->get_energy() + g4hit_i->get_light_yield());
118  tower->add_eshower(g4hit_i->get_shower_id(), g4hit_i->get_edep());
119  }
120 
121  float towerE = 0.;
122 
123  if (Verbosity())
124  {
125  towerE = m_Towers->getTotalEdep();
126  std::cout << "towers before compression: " << m_Towers->size() << "\t" << m_Detector << std::endl;
127  }
129  if (Verbosity())
130  {
131  std::cout << "storing towers: " << m_Towers->size() << std::endl;
132  std::cout << "Energy lost by dropping towers with less than " << m_Emin
133  << " energy, lost energy: " << towerE - m_Towers->getTotalEdep() << std::endl;
134  m_Towers->identify();
137  for (iter = begin_end.first; iter != begin_end.second; ++iter)
138  {
139  iter->second->identify();
140  }
141  }
142 
144 }
145 
147 {
149 }
150 
152 {
153  m_Detector = d;
155 }
156 
158 {
159  PHNodeIterator iter(topNode);
160  PHCompositeNode *runNode = static_cast<PHCompositeNode *>(iter.findFirst("PHCompositeNode", "RUN"));
161  if (!runNode)
162  {
163  std::cout << PHWHERE << "Run Node missing, doing nothing." << std::endl;
164  throw std::runtime_error("Failed to find Run node in RawTowerBuilderByHitIndex::CreateNodes");
165  }
166 
167  PHCompositeNode *dstNode = static_cast<PHCompositeNode *>(iter.findFirst("PHCompositeNode", "DST"));
168  if (!dstNode)
169  {
170  std::cout << PHWHERE << "DST Node missing, doing nothing." << std::endl;
171  throw std::runtime_error("Failed to find DST node in RawTowerBuilderByHitIndex::CreateNodes");
172  }
173 
174  // Create the tower geometry node on the tree
176  std::string NodeNameTowerGeometries = "TOWERGEOM_" + m_Detector;
177 
178  PHIODataNode<PHObject> *geomNode = new PHIODataNode<PHObject>(m_Geoms, NodeNameTowerGeometries, "PHObject");
179  runNode->addNode(geomNode);
180 
181  // Find detector node (or create new one if not found)
182  PHNodeIterator dstiter(dstNode);
183  PHCompositeNode *DetNode = dynamic_cast<PHCompositeNode *>(dstiter.findFirst(
184  "PHCompositeNode", m_Detector));
185  if (!DetNode)
186  {
187  DetNode = new PHCompositeNode(m_Detector);
188  dstNode->addNode(DetNode);
189  }
190 
191  // Create the tower nodes on the tree
193  std::string NodeNameTowers;
194  if (m_SimTowerNodePrefix.empty())
195  {
196  // no prefix, consistent with older convension
197  NodeNameTowers = "TOWER_" + m_Detector;
198  }
199  else
200  {
201  NodeNameTowers = "TOWER_" + m_SimTowerNodePrefix + "_" + m_Detector;
202  }
203 
204  PHIODataNode<PHObject> *towerNode = new PHIODataNode<PHObject>(m_Towers, NodeNameTowers, "PHObject");
205  DetNode->addNode(towerNode);
206 
207  return;
208 }
209 
211 {
212  /* Stream to read table from file */
213  std::ifstream istream_mapping;
214 
215  /* Open the datafile, if it won't open return an error */
216  if (!istream_mapping.is_open())
217  {
218  istream_mapping.open(m_MappingTowerFile);
219  if (!istream_mapping)
220  {
221  std::cout << "CaloTowerGeomManager::ReadGeometryFromTable - ERROR Failed to open mapping file " << m_MappingTowerFile << std::endl;
222  exit(1);
223  }
224  }
225 
226  std::string line_mapping;
227 
228  while (getline(istream_mapping, line_mapping))
229  {
230  /* Skip lines starting with / including a '#' */
231  if (line_mapping.find('#') != std::string::npos)
232  {
233  if (Verbosity() > 0)
234  {
235  std::cout << "RawTowerBuilderByHitIndex: SKIPPING line in mapping file: " << line_mapping << std::endl;
236  }
237  continue;
238  }
239 
240  std::istringstream iss(line_mapping);
241 
242  /* If line starts with keyword Tower, add to tower positions */
243  if (line_mapping.find("Tower ") != std::string::npos)
244  {
245  unsigned idx_j, idx_k, idx_l;
246  double pos_x, pos_y, pos_z;
247  double size_x, size_y, size_z;
248  double rot_x, rot_y, rot_z;
249  double type;
250  std::string dummys;
251 
252  /* read string- break if error */
253  if (!(iss >> dummys >> type >> idx_j >> idx_k >> idx_l >> pos_x >> pos_y >> pos_z >> size_x >> size_y >> size_z >> rot_x >> rot_y >> rot_z))
254  {
255  std::cout << "ERROR in RawTowerBuilderByHitIndex: Failed to read line in mapping file " << m_MappingTowerFile << std::endl;
256  exit(1);
257  }
258 
259  /* Construct unique Tower ID */
260  unsigned int temp_id = RawTowerDefs::encode_towerid(m_CaloId, idx_j, idx_k);
261 
262  /* Create tower geometry object */
263  RawTowerGeom *temp_geo = new RawTowerGeomv3(temp_id);
264  temp_geo->set_center_x(pos_x);
265  temp_geo->set_center_y(pos_y);
266  temp_geo->set_center_z(pos_z);
267  temp_geo->set_size_x(size_x);
268  temp_geo->set_size_y(size_y);
269  temp_geo->set_size_z(size_z);
270  temp_geo->set_tower_type((int) type);
271 
272  /* Insert this tower into position map */
273  m_Geoms->add_tower_geometry(temp_geo);
274  }
275  /* If line does not start with keyword Tower, read as parameter */
276  else
277  {
278  /* If this line is not a comment and not a tower, save parameter as string / value. */
279  std::string parname;
280  double parval;
281 
282  /* read string- break if error */
283  if (!(iss >> parname >> parval))
284  {
285  std::cout << "ERROR in RawTowerBuilderByHitIndex: Failed to read line in mapping file " << m_MappingTowerFile << std::endl;
286  exit(1);
287  }
288 
289  m_GlobalParameterMap.insert(std::make_pair(parname, parval));
290  }
291  }
292 
293  /* Update member variables for global parameters based on parsed parameter file */
294  std::map<std::string, double>::iterator parit;
295 
296  parit = m_GlobalParameterMap.find("Gx0");
297  if (parit != m_GlobalParameterMap.end())
298  m_GlobalPlaceInX = parit->second;
299 
300  parit = m_GlobalParameterMap.find("Gy0");
301  if (parit != m_GlobalParameterMap.end())
302  m_GlobalPlaceInY = parit->second;
303 
304  parit = m_GlobalParameterMap.find("Gz0");
305  if (parit != m_GlobalParameterMap.end())
306  m_GlobalPlaceInZ = parit->second;
307 
308  parit = m_GlobalParameterMap.find("Grot_x");
309  if (parit != m_GlobalParameterMap.end())
310  m_RotInX = parit->second;
311 
312  parit = m_GlobalParameterMap.find("Grot_y");
313  if (parit != m_GlobalParameterMap.end())
314  m_RotInY = parit->second;
315 
316  parit = m_GlobalParameterMap.find("Grot_z");
317  if (parit != m_GlobalParameterMap.end())
318  m_RotInZ = parit->second;
319 
320  /* Correct tower geometries for global calorimter translation / rotation
321  * after reading parameters from file */
323 
324  for (RawTowerGeomContainer::ConstIterator it = all_towers.first;
325  it != all_towers.second; ++it)
326  {
327  double x_temp = it->second->get_center_x();
328  double y_temp = it->second->get_center_y();
329  double z_temp = it->second->get_center_z();
330 
331  /* Rotation */
332  TRotation rot;
333  rot.RotateX(m_RotInX);
334  rot.RotateY(m_RotInY);
335  rot.RotateZ(m_RotInZ);
336 
337  TVector3 v_temp_r(x_temp, y_temp, z_temp);
338  v_temp_r.Transform(rot);
339 
340  /* Translation */
341  double x_temp_rt = v_temp_r.X() + m_GlobalPlaceInX;
342  double y_temp_rt = v_temp_r.Y() + m_GlobalPlaceInY;
343  double z_temp_rt = v_temp_r.Z() + m_GlobalPlaceInZ;
344 
345  /* Update tower geometry object */
346  it->second->set_center_x(x_temp_rt);
347  it->second->set_center_y(y_temp_rt);
348  it->second->set_center_z(z_temp_rt);
349 
350  if (Verbosity() > 2)
351  {
352  std::cout << "* Local tower x y z : " << x_temp << " " << y_temp << " " << z_temp << std::endl;
353  std::cout << "* Globl tower x y z : " << x_temp_rt << " " << y_temp_rt << " " << z_temp_rt << std::endl;
354  }
355  }
356 
357  if (Verbosity())
358  {
359  std::cout << "size tower geom container:" << m_Geoms->size() << "\t" << m_Detector << std::endl;
360  }
361  return true;
362 }