Belle II Software  release-06-02-00
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 #include <framework/database/DBImportObjPtr.h>
10 #include <framework/database/DBObjPtr.h>
11 #include <framework/database/DBStore.h>
12 #include <framework/datastore/StoreObjPtr.h>
13 #include <framework/datastore/DataStore.h>
14 #include <framework/dataobjects/EventMetaData.h>
15 #include <framework/database/Configuration.h>
16 #include <ecl/dbobjects/ECLCrystalCalib.h>
17 #include <iostream>
18 #include <TFile.h>
19 #include <TH1F.h>
20 
21 using namespace Belle2;
22 
24 namespace {
25  void setupDatabase(int exp, int run, int eventNr = 1)
26  {
28  // simulate the initialize() phase where we can register objects in the DataStore
30  evtPtr.registerInDataStore();
32  // now construct the event metadata
33  evtPtr.construct(eventNr, run, exp);
34  // and update the database contents
35  DBStore& dbstore = DBStore::Instance();
36  dbstore.update();
37  // this is only needed it the payload might be intra-run dependent,
38  // that is if it might change during one run as well
39  dbstore.updateEvent();
40  }
41 }
42 
43 
44 int main(int argc, char** argv)
45 {
46  //------------------------------------------------------------------------
47  //..Check the arguments
48  if (argc < 3 || argc > 5) {
49  std::cout << "incorrect number of arguments for eclCrystalEnergy" << std::endl;
50  return -1;
51  }
52  int experiment = std::stoi(argv[1]);
53  int run = std::stoi(argv[2]);
54  std::string gtName = "ECL_energy_calibrations";
55  if (argc >= 4) {gtName = argv[3];}
56  bool writeOutput = true;
57  if (argc == 5) {writeOutput = std::stoi(argv[4]);}
58 
59  std::cout << "eclCrystalEnergy called with arguments exp = " << experiment << " run = " << run << " GT = " << gtName <<
60  " writeOutput = " << writeOutput << std::endl;
61 
62 
63  //------------------------------------------------------------------------
66  conf.prependGlobalTag(gtName);
67  conf.prependTestingPayloadLocation("localdb/database.txt");
68 
70  DBObjPtr<Belle2::ECLCrystalCalib> GammaGamma("ECLCrystalEnergyGammaGamma");
71  DBObjPtr<Belle2::ECLCrystalCalib> Existing("ECLCrystalEnergy");
72 
74  setupDatabase(experiment, run);
75 
76  //------------------------------------------------------------------------
78  std::vector<float> GammaGammaCalib;
79  std::vector<float> GammaGammaCalibUnc;
80  GammaGammaCalib = GammaGamma->getCalibVector();
81  GammaGammaCalibUnc = GammaGamma->getCalibUncVector();
82 
83  std::vector<float> ExistingCalib;
84  std::vector<float> ExistingCalibUnc;
85  ExistingCalib = Existing->getCalibVector();
86  ExistingCalibUnc = Existing->getCalibUncVector();
87 
88  //------------------------------------------------------------------------
90  //..For now, use Gamma Gamma if available; otherwise, use existing value
91  std::vector<float> NewCalib;
92  std::vector<float> NewCalibUnc;
93  NewCalib.resize(8736);
94  NewCalibUnc.resize(8736);
95  for (int ic = 0; ic < 8736; ic++) {
96  if (GammaGammaCalib[ic] > 0.) {
97  NewCalib[ic] = GammaGammaCalib[ic];
98  NewCalibUnc[ic] = GammaGammaCalibUnc[ic];
99  } else {
100  NewCalib[ic] = ExistingCalib[ic];
101  NewCalibUnc[ic] = ExistingCalibUnc[ic];
102  }
103  }
104 
105  //------------------------------------------------------------------------
107  for (int ic = 0; ic < 9000; ic += 1000) {
108  std::cout << ic + 1 << " " << ExistingCalib[ic] << " " << ExistingCalibUnc[ic] << " " << GammaGammaCalib[ic] << " " <<
109  GammaGammaCalibUnc[ic] << " " << NewCalib[ic] << " " << NewCalibUnc[ic] << std::endl;
110  }
111 
112  //------------------------------------------------------------------------
113  //..Histograms of existing calibration, new calibration, and ratio new/old
114  TString payloadTitle = "ECLCrystalEnergy";
115  payloadTitle += "_";
116  payloadTitle += experiment;
117  payloadTitle += "_";
118  payloadTitle += run;
119  TString fname = payloadTitle;
120  fname += ".root";
121  TFile hfile(fname, "recreate");
122 
123  TString htitle = payloadTitle;
124  htitle += " existing values;cellID";
125  TH1F* existingPayload = new TH1F("existingPayload", htitle, 8736, 1, 8737);
126 
127  htitle = payloadTitle;
128  htitle += " new values;cellID";
129  TH1F* newPayload = new TH1F("newPayload", htitle, 8736, 1, 8737);
130 
131  htitle = payloadTitle;
132  htitle += " ratio new/old;cellID";
133  TH1F* payloadRatioVsCellID = new TH1F("payloadRatioVsCellID", htitle, 8736, 1, 8737);
134 
135  htitle = payloadTitle;
136  htitle += " ratio new/old";
137  TH1F* payloadRatio = new TH1F("payloadRatio", htitle, 200, 0.95, 1.05);
138 
139  for (int cellID = 1; cellID <= 8736; cellID++) {
140  existingPayload->SetBinContent(cellID, ExistingCalib[cellID - 1]);
141  existingPayload->SetBinError(cellID, ExistingCalibUnc[cellID - 1]);
142 
143  newPayload->SetBinContent(cellID, NewCalib[cellID - 1]);
144  newPayload->SetBinError(cellID, NewCalibUnc[cellID - 1]);
145 
146  float ratio = 1.;
147  float ratioUnc = 0.;
148  if (abs(ExistingCalib[cellID - 1]) > 1.0e-12) {
149  ratio = NewCalib[cellID - 1] / ExistingCalib[cellID - 1];
150  float rUnc0 = ExistingCalibUnc[cellID - 1] / ExistingCalib[cellID - 1];
151  float rUnc1 = 0.;
152  if (abs(NewCalib[cellID - 1]) > 1.0e-12) {rUnc1 = NewCalibUnc[cellID - 1] / NewCalib[cellID - 1];}
153  ratioUnc = ratio * sqrt(rUnc0 * rUnc0 + rUnc1 * rUnc1);
154  }
155 
156  payloadRatioVsCellID->SetBinContent(cellID, ratio);
157  payloadRatioVsCellID->SetBinError(cellID, ratioUnc);
158 
159  payloadRatio->Fill(ratio);
160  }
161 
162  hfile.cd();
163  hfile.Write();
164  hfile.Close();
165  std::cout << std::endl << "Values written to " << fname << std::endl;
166 
167  //------------------------------------------------------------------------
168  //..Write new payload to localdb if requested
169  if (writeOutput) {
170  std::cout << "Creating importer" << std::endl;
171  Belle2::DBImportObjPtr<Belle2::ECLCrystalCalib> importer("ECLCrystalEnergy");
172  importer.construct();
173  importer->setCalibVector(NewCalib, NewCalibUnc);
174  importer.import(Belle2::IntervalOfValidity(experiment, run, -1, -1));
175  std::cout << "Successfully wrote payload ECLCrystalEnergy with iov " << experiment << "," << run << ",-1,-1" << std::endl;
176  } else {
177  std::cout << "Payload not written to database" << std::endl;
178  }
179 }
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.
Singleton class to cache database objects.
Definition: DBStore.h:32
static DataStore & Instance()
Instance of singleton Store.
Definition: DataStore.cc:52
void setInitializeActive(bool active)
Setter for m_initializeActive.
Definition: DataStore.cc:92
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:95
bool construct(Args &&... params)
Construct an object of type T in this StoreObjPtr, using the provided constructor arguments.
Definition: StoreObjPtr.h:118
static DBStore & Instance()
Instance of a singleton DBStore.
Definition: DBStore.cc:26
void updateEvent()
Updates all intra-run dependent objects.
Definition: DBStore.cc:140
void update()
Updates all objects that are outside their interval of validity.
Definition: DBStore.cc:77
Abstract base class for different kinds of events.
int main(int argc, char **argv)
Run all tests.
Definition: test_main.cc:75