Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CaloGeomMapping.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file CaloGeomMapping.cc
1 #include "CaloGeomMapping.h"
2 
3 #include <cdbobjects/CDBTTree.h>
4 
6 
8 #include <fun4all/SubsysReco.h> // for SubsysReco
9 
10 #include <phool/PHCompositeNode.h>
11 #include <phool/PHIODataNode.h> // for PHIODataNode
12 #include <phool/PHNode.h>
13 #include <phool/PHNodeIterator.h> // for PHNodeIterator
14 #include <phool/PHObject.h>
15 #include <phool/getClass.h>
16 #include <phool/phool.h> // for PHWHERE
17 
18 #include <calobase/RawTowerDefs.h> // for encode_towerid
19 #include <calobase/RawTowerGeom.h> // for RawTowerGeom
20 #include <calobase/RawTowerGeomContainer.h> // for RawTowerGeomC...
21 #include <calobase/RawTowerGeomContainer_Cylinderv1.h>
22 #include <calobase/RawTowerGeomv1.h>
23 
24 #include <cstdlib> // for exit
25 #include <exception> // for exception
26 #include <iostream> // for operator<<, endl
27 #include <cmath> // for fabs, atan, cos
28 #include <stdexcept> // for runtime_error
29 #include <utility> // for pair
30 
31 //____________________________________________________________________________..
33  SubsysReco(name),
34  m_Detector("CEMC"),
35  m_RawTowerGeomContainer(nullptr)
36 {
37  std::cout << "CaloGeomMapping::CaloGeomMapping(const std::string &name) Calling ctor" << std::endl;
38 }
39 
40 //____________________________________________________________________________..
42 {
43  std::cout << "CaloGeomMapping::~CaloGeomMapping() Calling dtor" << std::endl;
44 }
45 
46 //____________________________________________________________________________..
48 {
49  std::cout << "CaloGeomMapping::Init(PHCompositeNode *topNode) Initializing" << std::endl;
50 
51  /* std::cout << "Printing node tree before new node creation:" << std::endl; */
52  /* topNode->print(); */
53  try
54  {
55  CreateGeomNode(topNode);
56  }
57  catch (std::exception &e)
58  {
59  std::cout << e.what() << std::endl;
60  exit(1);
61  }
62  /* std::cout << "Printing node tree after new node creation:" << std::endl; */
63  /* topNode->print(); */
65 }
66 
67 //____________________________________________________________________________..
68 /*
69 int CaloGeomMapping::InitRun(PHCompositeNode *topNode)
70 {
71  std::cout << "CaloGeomMapping::InitRun(PHCompositeNode *topNode) Initializing for Run XXX" << std::endl;
72  return Fun4AllReturnCodes::EVENT_OK;
73 }
74 
75 //____________________________________________________________________________..
76 int CaloGeomMapping::process_event(PHCompositeNode *topNode)
77 {
78  std::cout << "CaloGeomMapping::process_event(PHCompositeNode *topNode) Processing Event" << std::endl;
79  return Fun4AllReturnCodes::EVENT_OK;
80 }
81 
82 //____________________________________________________________________________..
83 int CaloGeomMapping::ResetEvent(PHCompositeNode *topNode)
84 {
85  std::cout << "CaloGeomMapping::ResetEvent(PHCompositeNode *topNode) Resetting internal structures, prepare for next event" << std::endl;
86  return Fun4AllReturnCodes::EVENT_OK;
87 }
88 
89 //____________________________________________________________________________..
90 int CaloGeomMapping::EndRun(const int runnumber)
91 {
92  std::cout << "CaloGeomMapping::EndRun(const int runnumber) Ending Run for Run " << runnumber << std::endl;
93  return Fun4AllReturnCodes::EVENT_OK;
94 }
95 
96 //____________________________________________________________________________..
97 int CaloGeomMapping::End(PHCompositeNode *topNode)
98 {
99  std::cout << "CaloGeomMapping::End(PHCompositeNode *topNode) This is the End..." << std::endl;
100  return Fun4AllReturnCodes::EVENT_OK;
101 }
102 
103 //____________________________________________________________________________..
104 int CaloGeomMapping::Reset(PHCompositeNode *topNode)
105 {
106  std::cout << "CaloGeomMapping::Reset(PHCompositeNode *topNode) being Reset" << std::endl;
107  return Fun4AllReturnCodes::EVENT_OK;
108 }
109 
110 //____________________________________________________________________________..
111 void CaloGeomMapping::Print(const std::string &what) const
112 {
113  std::cout << "CaloGeomMapping::Print(const std::string &what) const Printing info for " << what << std::endl;
114 }
115 */
116 
118 {
119  PHNodeIterator iter(topNode);
120  PHCompositeNode *runNode = dynamic_cast<PHCompositeNode *>(iter.findFirst("PHCompositeNode", "RUN"));
121  if (!runNode)
122  {
123  std::cout << PHWHERE << "Run Node missing, doing nothing." << std::endl;
124  throw std::runtime_error("Failed to find Run node in CaloGeomMapping::CreateGeomNode");
125  }
126 
127  PHNodeIterator runIter(runNode);
128  PHCompositeNode *RunDetNode = dynamic_cast<PHCompositeNode *>(runIter.findFirst("PHCompositeNode", m_Detector));
129  if (!RunDetNode)
130  {
131  RunDetNode = new PHCompositeNode(m_Detector);
132  runNode->addNode(RunDetNode);
133  }
134 
136  m_TowerGeomNodeName = "TOWERGEOM_" + m_Detector;
137  m_RawTowerGeomContainer = findNode::getClass<RawTowerGeomContainer>(topNode, m_TowerGeomNodeName);
139  {
141  // add it to the node tree
143  RunDetNode->addNode(newNode);
144  }
145 
146  // Get the geometry mapping file from the Conditions Database
147  std::string inName=CDBInterface::instance()->getUrl("CALO_TOWER_GEOMETRY");
148  CDBTTree * cdbttree = new CDBTTree(inName);
149  cdbttree->LoadCalibrations();
150 
151  std::string parName;
152  std::string parBase;
153  // Set the radius, thickness, number of eta and phi bins
154  if (m_Detector == "CEMC")
155  {
156  parBase = "cemc";
161  // m_RawTowerGeomContainer->set_phistep(m_PhiStep);
162  // m_RawTowerGeomContainer->set_phimin(m_PhiMin);
163  }
164  if (m_Detector == "HCALIN")
165  {
166  parBase = "hcalin";
171  // m_RawTowerGeomContainer->set_phistep(m_PhiStep);
172  // m_RawTowerGeomContainer->set_phimin(m_PhiMin);
173  }
174  if (m_Detector == "HCALOUT")
175  {
176  parBase = "hcalout";
181  // m_RawTowerGeomContainer->set_phistep(m_PhiStep);
182  // m_RawTowerGeomContainer->set_phimin(m_PhiMin);
183  }
184 
185  // Set the eta and phi bounds of each bin
186  for (int ibin = 0; ibin < m_RawTowerGeomContainer->get_etabins(); ibin++)
187  {
188  parName = parBase + "_eta_";
189  double first, second;
190  first = cdbttree->GetDoubleValue(ibin, parName + "first");
191  second = cdbttree->GetDoubleValue(ibin, parName + "second");
192  const std::pair<double, double> range(first, second);
194  /* std::cout << "Setting eta bounds for bin " << ibin << ", range is " << first << " - " << second << "\n"; */
195  }
196  for (int ibin = 0; ibin < m_RawTowerGeomContainer->get_phibins(); ibin++)
197  {
198  parName = parBase + "_phi_";
199  double first, second;
200  first = cdbttree->GetDoubleValue(ibin, parName + "first");
201  second = cdbttree->GetDoubleValue(ibin, parName + "second");
202  const std::pair<double, double> range(first, second);
204  /* std::cout << "Setting phi bounds for bin " << ibin << ", range is " << first << " - " << second << "\n"; */
205  }
206 
207  // Populate container with RawTowerGeom objects
208  for (int ieta=0; ieta<m_RawTowerGeomContainer->get_etabins(); ieta++)
209  {
210  for (int iphi=0; iphi<m_RawTowerGeomContainer->get_phibins(); iphi++)
211  {
212  // build tower geom here
213  const RawTowerDefs::keytype key =
214  RawTowerDefs::encode_towerid(caloid, ieta, iphi);
215 
217  const double x(r * cos(m_RawTowerGeomContainer->get_phicenter(iphi)));
218  const double y(r * sin(m_RawTowerGeomContainer->get_phicenter(iphi)));
219  /* const double z(r / tan(PHG4Utils::get_theta(m_RawTowerGeomContainer->get_etacenter(ieta)))); */
220  const double z(r / tan(2*atan(exp(-1*m_RawTowerGeomContainer->get_etacenter(ieta)))));
221  /* const double x(0); */
222  /* const double y(0); */
223  /* const double z(0); */
224 
226  if (tg)
227  {
228  if (Verbosity() > 0)
229  {
230  std::cout << "CaloGeomMapping::CreateGeomNode - Tower geometry " << key << " already exists" << std::endl;
231  }
232 
233  if (fabs(tg->get_center_x() - x) > 1e-4)
234  {
235  std::cout << "CaloGeomMapping::CreateGeomNode - Fatal Error - duplicated Tower geometry " << key << " with existing x = " << tg->get_center_x() << " and expected x = " << x
236  << std::endl;
237 
238  exit(1);
239  }
240  if (fabs(tg->get_center_y() - y) > 1e-4)
241  {
242  std::cout << "CaloGeomMapping::CreateGeomNode - Fatal Error - duplicated Tower geometry " << key << " with existing y = " << tg->get_center_y() << " and expected y = " << y
243  << std::endl;
244  exit(1);
245  }
246  if (fabs(tg->get_center_z() - z) > 1e-4)
247  {
248  std::cout << "CaloGeomMapping::CreateGeomNode - Fatal Error - duplicated Tower geometry " << key << " with existing z= " << tg->get_center_z() << " and expected z = " << z
249  << std::endl;
250  exit(1);
251  }
252  }
253  else
254  {
255  if (Verbosity() > 0)
256  {
257  std::cout << "CaloGeomMapping::CreateGeomNode - building tower geometry " << key << "" << std::endl;
258  }
259 
260  tg = new RawTowerGeomv1(key);
261 
262  tg->set_center_x(x);
263  tg->set_center_y(y);
264  tg->set_center_z(z);
266  /* std::cout << "Added new RawTowerGeom " << tg << "; more details:\n"; */
267  /* tg->identify(); */
268  }
269  }
270  } // end loop over eta, phi bins
271 } // end of building RawTowerGeomContainer
272 
274 {
275  m_Detector = name;
276 }
277 
279 {
280  return m_Detector;
281 }