Belle II Software development
eclCrystalEnergy.cc
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8
9/* ECL headers. */
10#include <ecl/dataobjects/ECLElementNumbers.h>
11#include <ecl/dbobjects/ECLCrystalCalib.h>
12
13/* Basf2 headers. */
14#include <framework/database/Configuration.h>
15#include <framework/database/DBImportObjPtr.h>
16#include <framework/database/DBObjPtr.h>
17#include <framework/database/DBStore.h>
18#include <framework/dataobjects/EventMetaData.h>
19#include <framework/datastore/DataStore.h>
20#include <framework/datastore/StoreObjPtr.h>
21
22/* ROOT headers. */
23#include <TFile.h>
24#include <TH1F.h>
25
26/* C++ headers. */
27#include <iostream>
28
29using namespace Belle2;
30
32namespace {
33 void setupDatabase(int exp, int run, int eventNr = 1)
34 {
36 // simulate the initialize() phase where we can register objects in the DataStore
38 evtPtr.registerInDataStore();
40 // now construct the event metadata
41 evtPtr.construct(eventNr, run, exp);
42 // and update the database contents
43 DBStore& dbstore = DBStore::Instance();
44 dbstore.update();
45 // this is only needed it the payload might be intra-run dependent,
46 // that is if it might change during one run as well
47 dbstore.updateEvent();
48 }
49}
50
51
52int main(int argc, char** argv)
53{
54 //------------------------------------------------------------------------
55 //..Check the arguments
56 if (argc < 3 || argc > 5) {
57 std::cout << "incorrect number of arguments for eclCrystalEnergy" << std::endl;
58 return -1;
59 }
60 int experiment = std::stoi(argv[1]);
61 int run = std::stoi(argv[2]);
62 std::string gtName = "ECL_energy_calibrations";
63 if (argc >= 4) {gtName = argv[3];}
64 bool writeOutput = true;
65 if (argc == 5) {writeOutput = std::stoi(argv[4]);}
66
67 std::cout << "eclCrystalEnergy called with arguments exp = " << experiment << " run = " << run << " GT = " << gtName <<
68 " writeOutput = " << writeOutput << std::endl;
69
70
71 //------------------------------------------------------------------------
74 conf.prependGlobalTag(gtName);
75 conf.prependTestingPayloadLocation("localdb/database.txt");
76
78 DBObjPtr<Belle2::ECLCrystalCalib> GammaGamma("ECLCrystalEnergyGammaGamma");
79 DBObjPtr<Belle2::ECLCrystalCalib> Existing("ECLCrystalEnergy");
80
82 setupDatabase(experiment, run);
83
84 //------------------------------------------------------------------------
86 std::vector<float> GammaGammaCalib;
87 std::vector<float> GammaGammaCalibUnc;
88 GammaGammaCalib = GammaGamma->getCalibVector();
89 GammaGammaCalibUnc = GammaGamma->getCalibUncVector();
90
91 std::vector<float> ExistingCalib;
92 std::vector<float> ExistingCalibUnc;
93 ExistingCalib = Existing->getCalibVector();
94 ExistingCalibUnc = Existing->getCalibUncVector();
95
96 //------------------------------------------------------------------------
98 //..For now, use Gamma Gamma if available; otherwise, use existing value
99 std::vector<float> NewCalib;
100 std::vector<float> NewCalibUnc;
101 NewCalib.resize(ECLElementNumbers::c_NCrystals);
102 NewCalibUnc.resize(ECLElementNumbers::c_NCrystals);
103 for (int ic = 0; ic < ECLElementNumbers::c_NCrystals; ic++) {
104 if (GammaGammaCalib[ic] > 0.) {
105 NewCalib[ic] = GammaGammaCalib[ic];
106 NewCalibUnc[ic] = GammaGammaCalibUnc[ic];
107 } else {
108 NewCalib[ic] = ExistingCalib[ic];
109 NewCalibUnc[ic] = ExistingCalibUnc[ic];
110 }
111 }
112
113 //------------------------------------------------------------------------
115 for (int ic = 0; ic < 9000; ic += 1000) {
116 std::cout << ic + 1 << " " << ExistingCalib[ic] << " " << ExistingCalibUnc[ic] << " " << GammaGammaCalib[ic] << " " <<
117 GammaGammaCalibUnc[ic] << " " << NewCalib[ic] << " " << NewCalibUnc[ic] << std::endl;
118 }
119
120 //------------------------------------------------------------------------
121 //..Histograms of existing calibration, new calibration, and ratio new/old
122 TString payloadTitle = "ECLCrystalEnergy";
123 payloadTitle += "_";
124 payloadTitle += experiment;
125 payloadTitle += "_";
126 payloadTitle += run;
127 TString fname = payloadTitle;
128 fname += ".root";
129 TFile hfile(fname, "recreate");
130
131 TString htitle = payloadTitle;
132 htitle += " existing values;cellID";
133 TH1F* existingPayload = new TH1F("existingPayload", htitle, ECLElementNumbers::c_NCrystals, 1, 8737);
134
135 htitle = payloadTitle;
136 htitle += " new values;cellID";
137 TH1F* newPayload = new TH1F("newPayload", htitle, ECLElementNumbers::c_NCrystals, 1, 8737);
138
139 htitle = payloadTitle;
140 htitle += " ratio new/old;cellID";
141 TH1F* payloadRatioVsCellID = new TH1F("payloadRatioVsCellID", htitle, ECLElementNumbers::c_NCrystals, 1, 8737);
142
143 htitle = payloadTitle;
144 htitle += " ratio new/old";
145 TH1F* payloadRatio = new TH1F("payloadRatio", htitle, 200, 0.95, 1.05);
146
147 for (int cellID = 1; cellID <= ECLElementNumbers::c_NCrystals; cellID++) {
148 existingPayload->SetBinContent(cellID, ExistingCalib[cellID - 1]);
149 existingPayload->SetBinError(cellID, ExistingCalibUnc[cellID - 1]);
150
151 newPayload->SetBinContent(cellID, NewCalib[cellID - 1]);
152 newPayload->SetBinError(cellID, NewCalibUnc[cellID - 1]);
153
154 float ratio = 1.;
155 float ratioUnc = 0.;
156 if (abs(ExistingCalib[cellID - 1]) > 1.0e-12) {
157 ratio = NewCalib[cellID - 1] / ExistingCalib[cellID - 1];
158 float rUnc0 = ExistingCalibUnc[cellID - 1] / ExistingCalib[cellID - 1];
159 float rUnc1 = 0.;
160 if (abs(NewCalib[cellID - 1]) > 1.0e-12) {rUnc1 = NewCalibUnc[cellID - 1] / NewCalib[cellID - 1];}
161 ratioUnc = ratio * sqrt(rUnc0 * rUnc0 + rUnc1 * rUnc1);
162 }
163
164 payloadRatioVsCellID->SetBinContent(cellID, ratio);
165 payloadRatioVsCellID->SetBinError(cellID, ratioUnc);
166
167 payloadRatio->Fill(ratio);
168 }
169
170 hfile.cd();
171 hfile.Write();
172 hfile.Close();
173 std::cout << std::endl << "Values written to " << fname << std::endl;
174
175 //------------------------------------------------------------------------
176 //..Write new payload to localdb if requested
177 if (writeOutput) {
178 std::cout << "Creating importer" << std::endl;
179 Belle2::DBImportObjPtr<Belle2::ECLCrystalCalib> importer("ECLCrystalEnergy");
180 importer.construct();
181 importer->setCalibVector(NewCalib, NewCalibUnc);
182 importer.import(Belle2::IntervalOfValidity(experiment, run, -1, -1));
183 std::cout << "Successfully wrote payload ECLCrystalEnergy with iov " << experiment << "," << run << ",-1,-1" << std::endl;
184 } else {
185 std::cout << "Payload not written to database" << std::endl;
186 }
187}
static Configuration & getInstance()
Get a reference to the instance which will be used when the Database is initialized.
Class for importing a single object to the database.
Class for accessing objects in the database.
Definition: DBObjPtr.h:21
Singleton class to cache database objects.
Definition: DBStore.h:31
static DataStore & Instance()
Instance of singleton Store.
Definition: DataStore.cc:54
void setInitializeActive(bool active)
Setter for m_initializeActive.
Definition: DataStore.cc:94
A class that describes the interval of experiments/runs for which an object in the database is valid.
bool registerInDataStore(DataStore::EStoreFlags storeFlags=DataStore::c_WriteOut)
Register the object/array in the DataStore.
Type-safe access to single objects in the data store.
Definition: StoreObjPtr.h:96
bool construct(Args &&... params)
Construct an object of type T in this StoreObjPtr, using the provided constructor arguments.
Definition: StoreObjPtr.h:119
static DBStore & Instance()
Instance of a singleton DBStore.
Definition: DBStore.cc:28
void updateEvent()
Updates all intra-run dependent objects.
Definition: DBStore.cc:142
void update()
Updates all objects that are outside their interval of validity.
Definition: DBStore.cc:79
double sqrt(double a)
sqrt for double
Definition: beamHelpers.h:28
const int c_NCrystals
Number of crystals.
Abstract base class for different kinds of events.