35 #include <framework/database/DBImportObjPtr.h>
36 #include <framework/database/DBObjPtr.h>
37 #include <framework/database/DBStore.h>
38 #include <framework/datastore/StoreObjPtr.h>
39 #include <framework/datastore/DataStore.h>
40 #include <framework/dataobjects/EventMetaData.h>
41 #include <framework/database/Configuration.h>
42 #include <framework/logging/LogSystem.h>
43 #include <ecl/dbobjects/ECLCrystalCalib.h>
53 void setupDatabase(
int exp,
int run,
int eventNr = 1)
58 evtPtr.registerInDataStore();
60 std::cout <<
"about to construct EventMetaData, exp = " << exp <<
" run = " << run <<
" eventNr = " << eventNr << std::endl;
69 int main(
int argc,
char** argv)
71 if (argc < 4 || argc > 5) {
72 std::cout <<
"insufficient arguments for eclElectronicsPayloads" << std::endl;
75 std::string payloadName = argv[1];
76 if (payloadName !=
"ECLCrystalElectronics" and payloadName !=
"ECLCrystalElectronicsTime" and payloadName !=
"ECLRefAmplNom"
77 and payloadName !=
"ECLRefTimeNom") {
78 std::cout <<
"First argument must be ECLCrystalElectronics, ECLCrystalElectronicsTime, ECLRefAmplNom, or ECLRefTimeNom" <<
82 int experiment = std::stoi(argv[2]);
83 int run = std::stoi(argv[3]);
84 bool writeOutput =
true;
85 if (argc == 5) writeOutput = std::stoi(argv[4]);
86 std::cout <<
"eclElectronicsPayloads called with arguments " << payloadName <<
" " << experiment <<
" " << run <<
" " << writeOutput
92 auto states = conf.getUsableTagStates();
93 states.insert(
"OPEN");
94 conf.setUsableTagStates(states);
95 conf.prependGlobalTag(
"ECL_localrun_data");
96 conf.prependTestingPayloadLocation(
"localdb/database.txt");
101 logging->setDebugLevel(10);
104 std::cout <<
"calling setupDatabase " << std::endl;
105 setupDatabase(experiment, run);
118 std::cout <<
"Reading ECLRefAmpl, ECLRefAmplNom, ECLRefTime, ECLRefTimeNom, ECLCrystalElectronics, and ECLCrystalElectronicsTime" <<
120 std::cout <<
"Dumping " << payloadName << std::endl;
121 existingObject->Dump();
124 std::vector<float> currentValues = existingObject->getCalibVector();
125 std::vector<float> currentUnc = existingObject->getCalibUncVector();
127 std::vector<float> refAmpl = InputAmpl->getCalibVector();
128 std::vector<float> refAmplUnc = InputAmpl->getCalibUncVector();
130 std::vector<float> refAmplNom = InputAmplNom->getCalibVector();
132 std::vector<float> refTime = InputTime->getCalibVector();
133 std::vector<float> refTimeUnc = InputTime->getCalibUncVector();
135 std::vector<float> refTimeNom = InputTimeNom->getCalibVector();
137 std::vector<float> crysElec = CurrentElec->getCalibVector();
139 std::vector<float> crysTime = CurrentTime->getCalibVector();
143 std::cout << std::endl <<
"Reference amplitudes and times read from database " << std::endl;
144 for (
int ic = 0; ic < 9000; ic += 1000) {
145 std::cout <<
"cellID " << ic + 1 <<
" ref amplitude = " << refAmpl[ic] <<
" +/- " << refAmplUnc[ic] <<
" nom = " << refAmplNom[ic]
146 <<
" ref time = " << refTime[ic]
147 <<
" +/- " << refTimeUnc[ic] <<
" nom = " << refTimeNom[ic] << std::endl;
152 std::vector<float> newValues;
153 std::vector<float> newUnc;
154 for (
int ic = 0; ic < 8736; ic++) {
155 if (payloadName ==
"ECLCrystalElectronics") {
156 newValues.push_back(refAmplNom[ic] / refAmpl[ic]);
157 newUnc.push_back(newValues[ic]*refAmplUnc[ic] / refAmpl[ic]);
158 }
else if (payloadName ==
"ECLCrystalElectronicsTime") {
159 newValues.push_back(refTime[ic] - refTimeNom[ic]);
160 newUnc.push_back(refTimeUnc[ic]);
161 }
else if (payloadName ==
"ECLRefAmplNom") {
162 newValues.push_back(crysElec[ic]*refAmpl[ic]);
163 newUnc.push_back(0.);
164 }
else if (payloadName ==
"ECLRefTimeNom") {
165 newValues.push_back(refTime[ic] - crysTime[ic]);
166 newUnc.push_back(0.);
173 std::cout << std::endl <<
"Comparison of existing and new values for " << payloadName << std::endl;
174 for (
int ic = 0; ic < 9000; ic += 1000) {
175 std::cout <<
"cellID " << ic + 1 <<
" existing = " << currentValues[ic] <<
" +/- " << currentUnc[ic] <<
" new = " << newValues[ic]
176 <<
" +/- " << newUnc[ic] << std::endl;
178 std::cout << std::endl;
180 TString payloadTitle = payloadName;
182 payloadTitle += experiment;
185 TString fname = payloadTitle;
187 TFile hfile(fname,
"recreate");
188 TString htitle = payloadTitle;
189 htitle +=
" existing calibration values;cellID";
190 TH1F* existingCalib =
new TH1F(
"existingCalib", htitle, 8736, 1, 8737);
192 htitle = payloadTitle;
193 htitle +=
" new calibration values;cellID";
194 TH1F* newCalib =
new TH1F(
"newCalib", htitle, 8736, 1, 8737);
196 htitle = payloadTitle;
198 TH1F* calibRatio =
new TH1F(
"calibRatio", htitle, 200, 0.9, 1.1);
200 htitle = payloadTitle;
201 htitle +=
" difference";
202 TH1F* calibDiff =
new TH1F(
"calibDiff", htitle, 200, -100, 100);
204 htitle = payloadTitle;
205 htitle +=
" reference";
206 TH1F* refValues =
new TH1F(
"refValues", htitle, 8736, 1, 8737);
208 htitle = payloadTitle;
209 htitle +=
" ratio vs cellID;cellID;new/old";
210 TH1F* ratioVsCellID =
new TH1F(
"ratioVsCellID", htitle, 8736, 1, 8737);
212 htitle = payloadTitle;
213 htitle +=
" diff vs cellID;cellID;new - old";
214 TH1F* diffVsCellID =
new TH1F(
"diffVsCellID", htitle, 8736, 1, 8737);
216 for (
int cellID = 1; cellID <= 8736; cellID++) {
217 float oldValue = currentValues[cellID - 1];
218 float newValue = newValues[cellID - 1];
220 if (oldValue != 0.) {
221 ratio = newValue / oldValue;
222 }
else if (newValue != 0.) {
223 ratio = ratio * newValue / fabs(newValue);
226 existingCalib->SetBinContent(cellID, oldValue);
227 existingCalib->SetBinError(cellID, currentUnc[cellID - 1]);
228 newCalib->SetBinContent(cellID, newValue);
229 newCalib->SetBinError(cellID, newUnc[cellID - 1]);
230 calibRatio->Fill(ratio);
231 ratioVsCellID->SetBinContent(cellID, ratio);
232 ratioVsCellID->SetBinError(cellID, 0);
233 calibDiff->Fill(newValue - oldValue);
234 diffVsCellID->SetBinContent(cellID, newValue - oldValue);
235 diffVsCellID->SetBinError(cellID, 0);
236 if (payloadName ==
"ECLCrystalElectronics" or payloadName ==
"ECLRefAmplNom") {
237 refValues->SetBinContent(cellID, refAmpl[cellID - 1]);
238 refValues->SetBinError(cellID, refAmplUnc[cellID - 1]);
240 refValues->SetBinContent(cellID, refTime[cellID - 1]);
241 refValues->SetBinError(cellID, refTimeUnc[cellID - 1]);
245 if ((payloadName ==
"ECLCrystalElectronics" or payloadName ==
"ECLRefAmplNom") and (ratio<0.99 or ratio>1.01)) {
246 std::cout <<
"Ratio = " << ratio <<
" for cellID = " << cellID <<
" refAmpl = " << refAmpl[cellID - 1] <<
" refAmplNom = " <<
247 refAmplNom[cellID - 1] << std::endl;
248 }
else if (abs(newValue - oldValue) > 20.) {
249 std::cout <<
"Difference = " << newValue - oldValue <<
" for cellID = " << cellID <<
" refTime = " << refTime[cellID - 1] <<
250 " refTimeNom = " << refTimeNom[cellID - 1] << std::endl;
257 std::cout << std::endl <<
"Comparison of existing and new calibration values written to " << fname << std::endl;
262 std::cout <<
"Creating importer" << std::endl;
264 importer.construct();
265 importer->setCalibVector(newValues, newUnc);
267 std::cout <<
"Successfully wrote payload " << payloadName <<
" with iov " << experiment <<
"," << run <<
",-1,-1" << std::endl;