Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RawTowerCalibration.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file RawTowerCalibration.cc
1 #include "RawTowerCalibration.h"
2 
3 #include <calobase/RawTower.h>
4 #include <calobase/RawTowerContainer.h>
5 #include <calobase/RawTowerDefs.h>
6 #include <calobase/RawTowerGeom.h>
7 #include <calobase/RawTowerGeomContainer.h>
8 #include <calobase/RawTowerv2.h>
9 #include <calobase/TowerInfo.h>
10 #include <calobase/TowerInfoContainer.h>
11 #include <calobase/TowerInfoContainerv1.h>
12 #include <calobase/TowerInfov1.h>
13 
14 #include <phparameter/PHParameters.h>
15 
16 #include <cdbobjects/CDBTTree.h>
17 
19 
21 #include <fun4all/SubsysReco.h>
22 
23 #include <phool/PHCompositeNode.h>
24 #include <phool/PHIODataNode.h>
25 #include <phool/PHNode.h>
26 #include <phool/PHNodeIterator.h>
27 #include <phool/PHObject.h>
28 #include <phool/getClass.h>
29 #include <phool/phool.h>
30 
31 #include <TSystem.h>
32 
33 #include <cassert>
34 #include <cstdlib>
35 #include <exception>
36 #include <fstream>
37 #include <iostream>
38 #include <map>
39 #include <stdexcept>
40 #include <string>
41 #include <utility>
42 
44  : SubsysReco(name)
45  , _calib_algorithm(kNo_calibration)
46  , m_Detector("NONE")
47  , _calib_tower_node_prefix("CALIB")
48  , _raw_tower_node_prefix("RAW")
49  , _tower_calib_params(name)
50 {
51  //_tower_type = -1;
52 }
53 
55 {
56  delete m_CDBTTree;
57 }
58 
60 {
61  PHNodeIterator iter(topNode);
62 
63  // Looking for the DST node
64  PHCompositeNode *dstNode;
65  dstNode = dynamic_cast<PHCompositeNode *>(iter.findFirst("PHCompositeNode",
66  "DST"));
67  if (!dstNode)
68  {
69  std::cout << Name() << "::" << m_Detector << "::" << __PRETTY_FUNCTION__
70  << "DST Node missing, doing nothing." << std::endl;
71  exit(1);
72  }
73 
74  try
75  {
76  CreateNodes(topNode);
77  }
78  catch (std::exception &e)
79  {
80  std::cout << e.what() << std::endl;
82  }
83 
85  {
86  std::cout << Name() << "::" << m_Detector << "::" << __PRETTY_FUNCTION__
87  << "kDbfile_tbt_gain_corr chosen but not implemented"
88  << std::endl;
90  }
91 
93 }
94 
96 {
97  if (Verbosity())
98  {
99  std::cout << Name() << "::" << m_Detector << "::" << __PRETTY_FUNCTION__
100  << "Process event entered" << std::endl;
101  }
102 
103  if (m_UseTowerInfo != 1)
104  {
107 
108  for (rtiter = begin_end.first; rtiter != begin_end.second; ++rtiter)
109  {
110  const RawTowerDefs::keytype key = rtiter->first;
111 
112  const RawTower *raw_tower = rtiter->second;
113  assert(raw_tower);
114 
115  RawTowerGeom *raw_tower_geom = rawtowergeom->get_tower_geometry(
116  raw_tower->get_id());
117  assert(raw_tower_geom);
118 
119  if (_tower_type >= 0)
120  {
121  // Skip towers that don't match the type we are supposed to calibrate
122  if (_tower_type != raw_tower_geom->get_tower_type())
123  {
124  continue;
125  }
126  }
127 
129  {
130  _calib_towers->AddTower(key, new RawTowerv2(*raw_tower));
131  }
133  {
134  const double raw_energy = raw_tower->get_energy();
135  const double calib_energy = (raw_energy - _pedstal_ADC) * _calib_const_GeV_ADC;
136 
137  RawTower *calib_tower = new RawTowerv2(*raw_tower);
138  calib_tower->set_energy(calib_energy);
139  _calib_towers->AddTower(key, calib_tower);
140  }
142  {
144  const int eta = raw_tower->get_bineta();
145  const int phi = raw_tower->get_binphi();
146 
147  double tower_by_tower_calib = 1.;
148  if (caloid == RawTowerDefs::LFHCAL)
149  {
150  const int l = raw_tower->get_binl();
151  const std::string calib_const_name("calib_const_eta" + std::to_string(eta) + "_phi" + std::to_string(phi) + "_l" + std::to_string(l));
152 
153  tower_by_tower_calib = _tower_calib_params.get_double_param(calib_const_name);
154 
155  if (_pedestal_file == true)
156  {
157  const std::string pedstal_name("PedCentral_ADC_eta" + std::to_string(eta) + "_phi" + std::to_string(phi) + "_l" + std::to_string(l));
158  _pedstal_ADC =
160  }
161 
162  if (_GeV_ADC_file == true)
163  {
164  const std::string GeVperADCname("GeVperADC_eta" + std::to_string(eta) + "_phi" + std::to_string(phi) + "_l" + std::to_string(l));
166  _tower_calib_params.get_double_param(GeVperADCname);
167  }
168  }
169  else
170  {
171  const std::string calib_const_name("calib_const_eta" + std::to_string(eta) + "_phi" + std::to_string(phi));
172 
173  tower_by_tower_calib = _tower_calib_params.get_double_param(calib_const_name);
174 
175  if (_pedestal_file == true)
176  {
177  const std::string pedstal_name("PedCentral_ADC_eta" + std::to_string(eta) + "_phi" + std::to_string(phi));
178  _pedstal_ADC =
180  }
181 
182  if (_GeV_ADC_file == true)
183  {
184  const std::string GeVperADCname("GeVperADC_eta" + std::to_string(eta) + "_phi" + std::to_string(phi));
186  _tower_calib_params.get_double_param(GeVperADCname);
187  }
188  }
189  const double raw_energy = raw_tower->get_energy();
190  const double calib_energy = (raw_energy - _pedstal_ADC) * _calib_const_GeV_ADC * tower_by_tower_calib;
191 
192  RawTower *calib_tower = new RawTowerv2(*raw_tower);
193  calib_tower->set_energy(calib_energy);
194  _calib_towers->AddTower(key, calib_tower);
195  }
196  // else if // eventally this will be done exclusively of tow_by_tow
198  {
199  if (m_Detector.c_str()[0] == 'H')
200  {
201  std::string url = CDBInterface::instance()->getUrl("HCALTBYTCORR");
202  if (url.empty())
203  {
204  std::cout << PHWHERE << " Could not get Hcal Calibration for domain HCALTBYTCORR" << std::endl;
205  gSystem->Exit(1);
206  exit(1);
207  }
208 
209  m_CDBTTree = new CDBTTree(url);
210  }
211  else if (m_Detector.c_str()[0] == 'C')
212  {
213  std::string url = CDBInterface::instance()->getUrl("CEMCTBYTCORR");
214  if (url.empty())
215  {
216  std::cout << PHWHERE << " Could not get Cemc Calibration for domain CEMCTBYTCORR" << std::endl;
217  gSystem->Exit(1);
218  exit(1);
219  }
220 
221  m_CDBTTree = new CDBTTree(url);
222  }
223  if (!m_CDBTTree)
224  {
225  std::cout << Name() << "::" << m_Detector << "::" << __PRETTY_FUNCTION__
226  << "kDbfile_tbt_gain_corr chosen but no file loaded" << std::endl;
228  }
229 
230  float gain_factor = -888;
231  // gain_factor = _cal_dbfile->getCorr(key);
232 
233  const int eta = raw_tower->get_bineta();
234  const int phi = raw_tower->get_binphi();
235  unsigned int etaphikey = phi;
236  etaphikey = (etaphikey << 16U) + eta;
237 
238  gain_factor = m_CDBTTree->GetFloatValue(etaphikey,"etaphi");
239 
240  const double raw_energy = raw_tower->get_energy();
241  RawTower *calib_tower = new RawTowerv2(*raw_tower);
242 
243  // still include separate _calib_const_GeV_ADC factor
244  // for global shifts.
245 
246  float corr_energy = raw_energy * gain_factor * _calib_const_GeV_ADC;
247  calib_tower->set_energy(corr_energy);
248  _calib_towers->AddTower(key, calib_tower);
249  }
250  else
251  {
252  std::cout << Name() << "::" << m_Detector << "::" << __PRETTY_FUNCTION__
253  << " invalid calibration algorithm #" << _calib_algorithm
254  << std::endl;
255 
257  }
258  }
259  }
260 
261  if (m_UseTowerInfo > 0)
262  {
263  unsigned int ntowers = _raw_towerinfos->size();
264  for (unsigned int channel = 0; channel < ntowers; channel++)
265  {
266  // unsigned int key =rtiter->first;
267  unsigned int key = _raw_towerinfos->encode_key(channel);
268 
270  assert(raw_tower);
271 
272  TowerInfo *calib_tower = new TowerInfov1();
273 
275  {
276  calib_tower->set_energy(0);
277  }
278 
280  {
281  const double raw_energy = raw_tower->get_energy();
282  const double calib_energy = (raw_energy - _pedstal_ADC) * _calib_const_GeV_ADC;
283  calib_tower->set_energy(calib_energy);
284  }
286  {
287  const int eta = _raw_towerinfos->getTowerEtaBin(key);
288  const int phi = _raw_towerinfos->getTowerPhiBin(key);
289  double tower_by_tower_calib = 1.;
290  const std::string calib_const_name("calib_const_eta" + std::to_string(eta) + "_phi" + std::to_string(phi));
291  tower_by_tower_calib = _tower_calib_params.get_double_param(calib_const_name);
292  if (_pedestal_file == true)
293  {
294  const std::string pedstal_name("PedCentral_ADC_eta" + std::to_string(eta) + "_phi" + std::to_string(phi));
295  _pedstal_ADC =
297  }
298  if (_GeV_ADC_file == true)
299  {
300  const std::string GeVperADCname("GeVperADC_eta" + std::to_string(eta) + "_phi" + std::to_string(phi));
302  _tower_calib_params.get_double_param(GeVperADCname);
303  }
304  const double raw_energy = raw_tower->get_energy();
305  const double calib_energy = (raw_energy - _pedstal_ADC) * _calib_const_GeV_ADC * tower_by_tower_calib;
306  calib_tower->set_energy(calib_energy);
307  }
309  {
310  if (!m_CDBTTree)
311  {
312  std::cout << Name() << "::" << m_Detector << "::" << __PRETTY_FUNCTION__
313  << "kDbfile_tbt_gain_corr chosen but no file loaded" << std::endl;
315  }
316 
317  float gain_factor = -888;
318  const int eta = _raw_towerinfos->getTowerEtaBin(key);
319  const int phi = _raw_towerinfos->getTowerPhiBin(key);
320  unsigned int etaphikey = phi;
321  etaphikey = (etaphikey << 16U) + eta;
322 
323  gain_factor = m_CDBTTree->GetFloatValue(etaphikey,"etaphi");
324  const double raw_energy = raw_tower->get_energy();
325  float corr_energy = raw_energy * gain_factor * _calib_const_GeV_ADC;
326  calib_tower->set_energy(corr_energy);
327  }
328  else
329  {
330  std::cout << Name() << "::" << m_Detector << "::" << __PRETTY_FUNCTION__
331  << " invalid calibration algorithm #" << _calib_algorithm
332  << std::endl;
333 
335  }
336 
337  TowerInfo *calibrated_towerinfo = _calib_towerinfos->get_tower_at_channel(channel);
338  calibrated_towerinfo->set_energy(calib_tower->get_energy());
339  delete calib_tower;
340  }
341  }
342 
343  // for (rtiter = begin_end.first; rtiter != begin_end.second; ++rtiter)
344 
345  /*
346  int towcount =0;
347  RawTowerContainer::ConstRange begin_end2 = _calib_towers->getTowers();
348  RawTowerContainer::ConstIterator rtiter2;
349  std::cout << "Etowers ---------------------================---------"
350  << std::endl;
351 
352  for (rtiter2 = begin_end2.first; rtiter2 != begin_end2.second; ++rtiter2)
353  {
354  const RawTowerDefs::keytype key = rtiter2->first;
355  const RawTower *aftcal_tower = rtiter2->second;
356 
357  if (towcount++ < 10)
358  {
359  std::cout << "E tow: " << key << " " << aftcal_tower->get_energy()
360  << std::endl;
361  }
362 
363 
364  }
365 
366  */
367 
368  if (Verbosity())
369  {
370  std::cout << Name() << "::" << m_Detector << "::" << __PRETTY_FUNCTION__
371  << "input sum energy = " << _raw_towers->getTotalEdep()
372  << ", output sum digitalized value = "
373  << _calib_towers->getTotalEdep() << std::endl;
374  }
376 }
377 
379 {
381 }
382 
384 {
385  PHNodeIterator iter(topNode);
386  PHCompositeNode *runNode = static_cast<PHCompositeNode *>(iter.findFirst(
387  "PHCompositeNode", "RUN"));
388  if (!runNode)
389  {
390  std::cout << Name() << "::" << m_Detector << "::" << __PRETTY_FUNCTION__
391  << "Run Node missing, doing nothing." << std::endl;
392  throw std::runtime_error(
393  "Failed to find Run node in RawTowerCalibration::CreateNodes");
394  }
395 
396  TowerGeomNodeName = "TOWERGEOM_" + m_Detector;
397  rawtowergeom = findNode::getClass<RawTowerGeomContainer>(topNode,
399  if (!rawtowergeom)
400  {
401  std::cout << Name() << "::" << m_Detector << "::" << __PRETTY_FUNCTION__
402  << " " << TowerGeomNodeName << " Node missing, doing bail out!"
403  << std::endl;
404  throw std::runtime_error(
405  "Failed to find " + TowerGeomNodeName + " node in RawTowerCalibration::CreateNodes");
406  }
407 
408  if (Verbosity() >= 1)
409  {
411  }
412 
413  PHCompositeNode *dstNode = dynamic_cast<PHCompositeNode *>(iter.findFirst(
414  "PHCompositeNode", "DST"));
415  if (!dstNode)
416  {
417  std::cout << Name() << "::" << m_Detector << "::" << __PRETTY_FUNCTION__
418  << "DST Node missing, doing nothing." << std::endl;
419  throw std::runtime_error(
420  "Failed to find DST node in RawTowerCalibration::CreateNodes");
421  }
422 
423  if (m_UseTowerInfo != 1)
424  {
426  _raw_towers = findNode::getClass<RawTowerContainer>(dstNode,
428  if (!_raw_towers)
429  {
430  std::cout << Name() << "::" << m_Detector << "::" << __PRETTY_FUNCTION__
431  << " " << RawTowerNodeName << " Node missing, doing bail out!"
432  << std::endl;
433  throw std::runtime_error(
434  "Failed to find " + RawTowerNodeName + " node in RawTowerCalibration::CreateNodes");
435  }
436  }
437  if (m_UseTowerInfo > 0)
438  {
439  RawTowerInfoNodeName = "TOWERINFO_" + _raw_tower_node_prefix + "_" + m_Detector;
440  _raw_towerinfos = findNode::getClass<TowerInfoContainerv1>(dstNode, RawTowerInfoNodeName);
441  if (!_raw_towerinfos)
442  {
443  std::cout << Name() << "::" << m_Detector << "::" << __PRETTY_FUNCTION__
444  << " " << RawTowerInfoNodeName << " Node missing, doing bail out!"
445  << std::endl;
446  throw std::runtime_error(
447  "Failed to find " + RawTowerInfoNodeName + " node in RawTowerCalibration::CreateNodes");
448  }
449  }
450 
451  // Create the tower nodes on the tree
452  PHNodeIterator dstiter(dstNode);
453  PHCompositeNode *DetNode = dynamic_cast<PHCompositeNode *>(dstiter.findFirst(
454  "PHCompositeNode", m_Detector));
455  if (!DetNode)
456  {
457  DetNode = new PHCompositeNode(m_Detector);
458  dstNode->addNode(DetNode);
459  }
460 
461  // Be careful as a previous calibrator may have been registered for this detector
462  if (m_UseTowerInfo != 1)
463  {
465  _calib_towers = findNode::getClass<RawTowerContainer>(DetNode,
467  if (!_calib_towers)
468  {
471  DetNode->addNode(towerNode);
472  }
473  }
474  if (m_UseTowerInfo > 0)
475  {
477  _calib_towerinfos = findNode::getClass<TowerInfoContainerv1>(DetNode, CaliTowerInfoNodeName);
478  if (!_calib_towerinfos)
479  {
481  if (m_Detector == "CEMC")
482  {
483  detec = TowerInfoContainer::DETECTOR::EMCAL;
484  }
485  else if (m_Detector == "HCALIN" || m_Detector == "HCALOUT")
486  {
487  detec = TowerInfoContainer::DETECTOR::HCAL;
488  }
489  else
490  {
491  std::cout << PHWHERE << "Detector not implemented into the TowerInfoContainer object, defaulting to HCal implementation." << std::endl;
492  detec = TowerInfoContainer::DETECTOR::HCAL;
493  }
496  DetNode->addNode(towerinfoNode);
497  }
498  }
499 
500  return;
501 }