Belle II Software  release-05-02-19
eclMergingCrystalEAlgorithm.cc
1 #include <ecl/calibration/eclMergingCrystalEAlgorithm.h>
2 #include <ecl/dbobjects/ECLCrystalCalib.h>
3 #include "TH1F.h"
4 #include "TString.h"
5 #include "TFile.h"
6 #include "TDirectory.h"
7 
8 using namespace std;
9 using namespace Belle2;
10 using namespace ECL;
11 using namespace Calibration;
12 
14 eclMergingCrystalEAlgorithm::eclMergingCrystalEAlgorithm(): CalibrationAlgorithm("DummyCollector"),
15  m_existingGammaGamma("ECLCrystalEnergyGammaGamma"), m_existingMuMu("ECLCrystalEnergyMuMu"),
16  m_existing5x5("ECLCrystalEnergyee5x5"), m_existing(m_payloadName)
17 {
19  "Perform energy calibration of ecl crystals by combining previous values from the DB for different calibrations."
20  );
21 }
22 
24 {
25  //------------------------------------------------------------------------
26  // Get the input run list (should be only 1) for us to use to update the DBObjectPtrs
27  auto runs = getRunList();
28  // Take the first run
29  ExpRun chosenRun = runs.front();
30  B2INFO("merging using the ExpRun (" << chosenRun.second << "," << chosenRun.first << ")");
31  // After here your DBObjPtrs are correct
32  updateDBObjPtrs(1, chosenRun.second, chosenRun.first);
33 
34  //------------------------------------------------------------------------
35  // Test the DBObjects we want to exist and fail if not all of them do.
36  bool allObjectsFound = true;
37  // Test that the DBObjects are valid
38  if (!m_existingGammaGamma) {
39  allObjectsFound = false;
40  B2ERROR("No valid DBObject found for 'ECLCrystalEnergyGammaGamma'");
41  }
42  if (!m_existingMuMu) {
43  allObjectsFound = false;
44  B2ERROR("No valid DBObject found for 'ECLCrystalEnergyMuMu'");
45  }
46  if (!m_existing5x5) {
47  allObjectsFound = false;
48  B2ERROR("No valid DBObject found for 'ECLCrystalEnergyee5x5'");
49  }
50  if (!m_existing) {
51  allObjectsFound = false;
52  B2ERROR("No valid DBObject found for 'ECLCrystalEnergy'");
53  }
54 
55  if (allObjectsFound) {
56  B2INFO("Valid objects found for both 'ECLCrystalEnergy' and 'ECLCrystalEnergyGammaGamma'");
57  } else {
58  B2INFO("Exiting with failure");
59  return c_Failure;
60  }
61 
62  //------------------------------------------------------------------------
64  vector<float> gammaGammaCalib = m_existingGammaGamma->getCalibVector();
65  vector<float> gammaGammaCalibUnc = m_existingGammaGamma->getCalibUncVector();
66 
67  vector<float> existingCalib = m_existing->getCalibVector();
68  vector<float> existingCalibUnc = m_existing->getCalibUncVector();
69 
70  //------------------------------------------------------------------------
72  //..For now, use Gamma Gamma if available; otherwise, use existing value
73  vector<float> newCalib(m_numCrystals);
74  vector<float> newCalibUnc(m_numCrystals);
75 
76  for (int ic = 0; ic < m_numCrystals; ic++) {
77  if (gammaGammaCalib[ic] > 0.) {
78  newCalib[ic] = gammaGammaCalib[ic];
79  newCalibUnc[ic] = gammaGammaCalibUnc[ic];
80  } else {
81  newCalib[ic] = existingCalib[ic];
82  newCalibUnc[ic] = existingCalibUnc[ic];
83  }
84  }
85 
86  //------------------------------------------------------------------------
88  for (int ic = 0; ic < 9000; ic += 1000) {
89  B2INFO(ic + 1 << " " << existingCalib[ic] << " " << existingCalibUnc[ic] << " "
90  << gammaGammaCalib[ic] << " " << gammaGammaCalibUnc[ic] << " "
91  << newCalib[ic] << " " << newCalibUnc[ic]);
92  }
93 
94  //------------------------------------------------------------------------
95  //..Histograms of existing calibration, new calibration, and ratio new/old
96 
97  // Just in case, we remember the current TDirectory so we can return to it
98  TDirectory* executeDir = gDirectory;
99 
100  TString fname = m_payloadName;
101  fname += ".root";
102  TFile hfile(fname, "recreate");
103 
104  TString htitle = m_payloadName;
105  htitle += " existing values;cellID";
106  TH1F* existingPayload = new TH1F("existingPayload", htitle, m_numCrystals, 1, 8737);
107 
108  htitle = m_payloadName;
109  htitle += " new values;cellID";
110  TH1F* newPayload = new TH1F("newPayload", htitle, m_numCrystals, 1, 8737);
111 
112  htitle = m_payloadName;
113  htitle += " ratio new/old;cellID";
114  TH1F* payloadRatioVsCellID = new TH1F("payloadRatioVsCellID", htitle, m_numCrystals, 1, 8737);
115 
116  htitle = m_payloadName;
117  htitle += " ratio new/old";
118  TH1F* payloadRatio = new TH1F("payloadRatio", htitle, 200, 0.95, 1.05);
119 
120  for (int cellID = 1; cellID <= m_numCrystals; cellID++) {
121  existingPayload->SetBinContent(cellID, existingCalib[cellID - 1]);
122  existingPayload->SetBinError(cellID, existingCalibUnc[cellID - 1]);
123 
124  newPayload->SetBinContent(cellID, newCalib[cellID - 1]);
125  newPayload->SetBinError(cellID, newCalibUnc[cellID - 1]);
126 
127  float ratio = 1.;
128  float ratioUnc = 0.;
129  if (abs(existingCalib[cellID - 1]) > 1.0e-12) {
130  ratio = newCalib[cellID - 1] / existingCalib[cellID - 1];
131  float rUnc0 = existingCalibUnc[cellID - 1] / existingCalib[cellID - 1];
132  float rUnc1 = 0.;
133  if (abs(newCalib[cellID - 1]) > 1.0e-12) {rUnc1 = newCalibUnc[cellID - 1] / newCalib[cellID - 1];}
134  ratioUnc = ratio * sqrt(rUnc0 * rUnc0 + rUnc1 * rUnc1);
135  }
136 
137  payloadRatioVsCellID->SetBinContent(cellID, ratio);
138  payloadRatioVsCellID->SetBinError(cellID, ratioUnc);
139 
140  payloadRatio->Fill(ratio);
141  }
142  hfile.cd();
143  hfile.Write();
144  hfile.Close();
145  B2INFO("Debugging histograms written to " << fname);
146  // Go back to original TDirectory
147  executeDir->cd();
148 
149  ECLCrystalCalib* newCrystalEnergy = new ECLCrystalCalib();
150  newCrystalEnergy->setCalibVector(newCalib, newCalibUnc);
151  saveCalibration(newCrystalEnergy, m_payloadName);
152  return c_OK;
153 }
Belle2::ECL::eclMergingCrystalEAlgorithm::calibrate
virtual EResult calibrate() override
..Run algorithm
Definition: eclMergingCrystalEAlgorithm.cc:23
Belle2::ECL::eclMergingCrystalEAlgorithm::m_existingMuMu
DBObjPtr< ECLCrystalCalib > m_existingMuMu
ECLCrystalEnergyMuMu payloads that we want to read from the DB.
Definition: eclMergingCrystalEAlgorithm.h:63
Belle2::CalibrationAlgorithm::saveCalibration
void saveCalibration(TClonesArray *data, const std::string &name)
Store DBArray payload with given name with default IOV.
Definition: CalibrationAlgorithm.cc:290
Belle2::CalibrationAlgorithm::c_OK
@ c_OK
Finished successfuly =0 in Python.
Definition: CalibrationAlgorithm.h:51
Belle2::ECLCrystalCalib
General DB object to store one calibration number per ECL crystal.
Definition: ECLCrystalCalib.h:34
Belle2::ECL::eclMergingCrystalEAlgorithm::m_existing
DBObjPtr< ECLCrystalCalib > m_existing
ECLCrystalEnergy payloads that we want to read from the DB.
Definition: eclMergingCrystalEAlgorithm.h:69
Belle2::CalibrationAlgorithm::setDescription
void setDescription(const std::string &description)
Set algorithm description (in constructor)
Definition: CalibrationAlgorithm.h:331
Belle2::CalibrationAlgorithm::updateDBObjPtrs
void updateDBObjPtrs(const unsigned int event, const int run, const int experiment)
Updates any DBObjPtrs by calling update(event) for DBStore.
Definition: CalibrationAlgorithm.cc:398
Belle2::ECL::eclMergingCrystalEAlgorithm::m_existingGammaGamma
DBObjPtr< ECLCrystalCalib > m_existingGammaGamma
ECLCrystalEnergyGammaGamma payloads that we want to read from the DB.
Definition: eclMergingCrystalEAlgorithm.h:60
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::CalibrationAlgorithm::c_Failure
@ c_Failure
Failed =3 in Python.
Definition: CalibrationAlgorithm.h:54
Belle2::ExpRun
Struct containing exp number and run number.
Definition: Splitter.h:55
Belle2::ECL::eclMergingCrystalEAlgorithm::m_payloadName
const std::string m_payloadName
The output payload name.
Definition: eclMergingCrystalEAlgorithm.h:57
Belle2::CalibrationAlgorithm::EResult
EResult
The result of calibration.
Definition: CalibrationAlgorithm.h:50
Belle2::CalibrationAlgorithm::getRunList
const std::vector< Calibration::ExpRun > & getRunList() const
Get the list of runs for which calibration is called.
Definition: CalibrationAlgorithm.h:276
Belle2::ECLCrystalCalib::setCalibVector
void setCalibVector(const std::vector< float > &CalibConst, const std::vector< float > &CalibConstUnc)
Set vector of constants with uncertainties.
Definition: ECLCrystalCalib.h:48
Belle2::CalibrationAlgorithm
Base class for calibration algorithms.
Definition: CalibrationAlgorithm.h:47
Belle2::ECL::eclMergingCrystalEAlgorithm::m_existing5x5
DBObjPtr< ECLCrystalCalib > m_existing5x5
ECLCrystalEnergyee5x5 payloads that we want to read from the DB.
Definition: eclMergingCrystalEAlgorithm.h:66
Belle2::ECL::eclMergingCrystalEAlgorithm::m_numCrystals
const int m_numCrystals
Number of Crystals expected.
Definition: eclMergingCrystalEAlgorithm.h:72