Belle II Software  release-05-01-25
TOPDatabaseImporter.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2016 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Marko Staric, Umberto Tamponi *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 // Own include
12 #include <top/calibration/TOPDatabaseImporter.h>
13 #include <top/geometry/TOPGeometryPar.h>
14 
15 // framework - Database
16 #include <framework/database/IntervalOfValidity.h>
17 #include <framework/database/DBImportArray.h>
18 #include <framework/database/DBImportObjPtr.h>
19 #include <framework/database/DBArray.h>
20 #include <framework/database/DBObjPtr.h>
21 
22 // framework aux
23 #include <framework/logging/Logger.h>
24 
25 // DB objects
26 #include <top/dbobjects/TOPCalTimebase.h>
27 #include <top/dbobjects/TOPCalChannelT0.h>
28 #include <top/dbobjects/TOPCalModuleT0.h>
29 #include <top/dbobjects/TOPCalChannelMask.h>
30 #include <top/dbobjects/TOPCalChannelPulseHeight.h>
31 #include <top/dbobjects/TOPCalChannelThresholdEff.h>
32 #include <top/dbobjects/TOPCalChannelNoise.h>
33 #include <top/dbobjects/TOPCalChannelRQE.h>
34 #include <top/dbobjects/TOPCalChannelThreshold.h>
35 #include <top/dbobjects/TOPCalCommonT0.h>
36 #include <top/dbobjects/TOPCalIntegratedCharge.h>
37 #include <top/dbobjects/TOPCalModuleAlignment.h>
38 #include <top/dbobjects/TOPCalAsicShift.h>
39 #include <top/dbobjects/TOPCalTimeWalk.h>
40 
41 #include <top/dbobjects/TOPPmtGainPar.h>
42 #include <top/dbobjects/TOPPmtQE.h>
43 #include <top/dbobjects/TOPPmtInstallation.h>
44 #include <top/dbobjects/TOPPmtObsoleteData.h>
45 #include <top/dbobjects/TOPPmtTTSPar.h>
46 #include <top/dbobjects/TOPPmtTTSHisto.h>
47 
48 #include <top/dbobjects/TOPFrontEndSetting.h>
49 
50 #include <iostream>
51 #include <fstream>
52 #include <sstream>
53 #include <set>
54 #include <map>
55 
56 #include "TFile.h"
57 #include "TTree.h"
58 
59 extern "C" {
60  float phind_lambda_(float*); // phase refractive index of quartz (top_geo.F)
61 }
62 
63 using namespace std;
64 
65 namespace Belle2 {
71  using namespace TOP;
72 
73  void TOPDatabaseImporter::importSampleTimeCalibration(string fNames,
74  int firstExp, int firstRun,
75  int lastExp, int lastRun)
76  {
77 
78  // make vector out of files separated with space
79 
80  vector<string> fileNames;
81  stringstream ss(fNames);
82  string fName;
83  while (ss >> fName) {
84  fileNames.push_back(fName);
85  }
86 
87  // prepare what is needed
88 
89  const auto* geo = TOPGeometryPar::Instance()->getGeometry();
90  auto syncTimeBase = geo->getNominalTDC().getSyncTimeBase();
91 
93  timeBase.construct(syncTimeBase);
94 
95  set<int> scrodIDs;
96 
97  // read constants from files and put them to DB object
98 
99  for (const auto& fileName : fileNames) {
100  TFile* file = TFile::Open(fileName.c_str(), "r");
101  if (!file) {
102  B2ERROR("openFile: " << fileName << " *** failed to open");
103  continue;
104  }
105  B2INFO(fileName << ": open for reading");
106 
107  TH1F* hsuccess = (TH1F*) file->Get("success");
108  if (!hsuccess) {
109  B2ERROR("Fit status histogram '" << hsuccess << "' not found");
110  file->Close();
111  continue;
112  }
113 
114  int goodChannels = 0;
115  int numChannels = hsuccess->GetNbinsX();
116  for (int channel = 0; channel < numChannels; channel++) {
117  if (hsuccess->GetBinContent(channel + 1) == 0) continue;
118 
119  string hname = "sampleTimes_ch" + to_string(channel);
120 
121  TH1F* hsampleTimes = (TH1F*) file->Get(hname.c_str());
122  if (!hsampleTimes) {
123  B2ERROR("Histogram '" << hname << "' with calibration constants not found");
124  continue;
125  }
126  // parse scrodID from histogram title
127  string title = hsampleTimes->GetTitle();
128  auto iscrod = title.find("scrod");
129  auto ichannel = title.find("channel");
130  if (iscrod == string::npos or ichannel == string::npos) {
131  B2ERROR("Unsuccessful parsing of scrodID from '" << title << "'");
132  continue;
133  }
134  iscrod += 5;
135  int len = ichannel - iscrod;
136  if (len < 1) {
137  B2ERROR("Unsuccessful parsing of scrodID from '" << title << "'");
138  continue;
139  }
140  int scrodID = stoi(title.substr(iscrod, len));
141  scrodIDs.insert(scrodID);
142 
143  double rescale = 1;
144  if (hsampleTimes->GetBinContent(257) > 0)
145  rescale = 2 * syncTimeBase / hsampleTimes->GetBinContent(257);
146 
147  vector<double> sampleTimes;
148  for (int isamp = 0; isamp < 256; isamp++) {
149  sampleTimes.push_back(hsampleTimes->GetBinContent(isamp + 1) * rescale);
150  }
151  goodChannels++;
152 
153  timeBase->append(scrodID, channel, sampleTimes);
154  }
155 
156  file->Close();
157  B2INFO("--> number of calibrated channels: " << goodChannels);
158  B2INFO("file closed");
159  }
160 
161  // set calibration for missing ones, using previous calibrated channel within asic
162 
163  B2INFO("set constants for uncalibrated channels using nearest calibrated channel within an ASIC");
164  for (auto scrodID : scrodIDs) {
165  int nasic = 128 / 8;
166  for (int as = 0; as < nasic; as++) {
167  const TOPSampleTimes* sampleTimes = 0;
168  for (int ch = 0; ch < 15; ch++) {
169  int channel = as * 8 + (ch % 8);
170  if (timeBase->isAvailable(scrodID, channel)) {
171  sampleTimes = timeBase->getSampleTimes(scrodID, channel);
172  } else if (sampleTimes) {
173  timeBase->append(scrodID, channel, sampleTimes->getTimeAxis());
174  }
175  }
176  if (!sampleTimes) {
177  B2INFO("No calibration available for ASIC " << as << " of scrodID " << scrodID);
178  }
179  }
180  }
181 
182  // import constants
183  IntervalOfValidity iov(firstExp, firstRun, lastExp, lastRun);
184  timeBase.import(iov);
185 
186  // final message
187 
188  int nall = timeBase->getSampleTimes().size();
189  int ncal = 0;
190  for (const auto& sampleTimes : timeBase->getSampleTimes()) {
191  if (sampleTimes.isCalibrated()) ncal++;
192  }
193 
194  B2RESULT("Sample time calibration constants imported to database, calibrated channels: "
195  << ncal << "/" << nall);
196  }
197 
198 
199  void TOPDatabaseImporter::importLocalT0Calibration(string fNames,
200  int firstExp, int firstRun,
201  int lastExp, int lastRun)
202  {
203  vector<string> fileNames;
204  stringstream ss(fNames);
205  string fName;
206  while (ss >> fName) {
207  fileNames.push_back(fName);
208  }
209 
210  const auto* geo = TOPGeometryPar::Instance()->getGeometry();
211 
213  channelT0.construct();
214 
215  int nCal[16] = {0}; // number of calibrated channels per slot
216 
217  for (const auto& fileName : fileNames) {
218  TFile* file = TFile::Open(fileName.c_str(), "r");
219  B2INFO("--> Opening constants file " << fileName);
220 
221  if (!file) {
222  B2ERROR("openFile: " << fileName << " *** failed to open");
223  continue;
224  }
225  B2INFO("--> " << fileName << ": open for reading");
226 
227  TTree* treeCal = (TTree*)file->Get("chT0");
228 
229  if (!treeCal) {
230  B2ERROR("openFile: no tree named chT0 found in " << fileName);
231  file->Close();
232  continue;
233  }
234 
235  double t0Cal = 0.;
236  double t0CalErr = 0.;
237  int channelID = 0; // 0-511
238  int slotID = 0; // 1-16
239  int fitStatus = 0; // fit status, 0 = OK
240 
241  treeCal->SetBranchAddress("channel", &channelID);
242  treeCal->SetBranchAddress("slot", &slotID);
243  treeCal->SetBranchAddress("channelT0", &t0Cal);
244  treeCal->SetBranchAddress("channelT0Err", &t0CalErr);
245  treeCal->SetBranchAddress("fitStatus", &fitStatus);
246 
247  B2INFO("--> importing constats");
248 
249  for (int iCal = 0; iCal < treeCal->GetEntries(); iCal++) {
250  treeCal->GetEntry(iCal);
251  if (!geo->isModuleIDValid(slotID)) {
252  B2ERROR("Slot ID is not valid (fileName = " << fileName
253  << ", SlotID = " << slotID << ", ChannelID = " << channelID <<
254  "). Skipping the entry.");
255  continue;
256  }
257  if (channelID < 0 or channelID > 511) {
258  B2ERROR("Channel ID is not valid (fileName = " << fileName
259  << ", SlotID = " << slotID << ", ChannelID = " << channelID <<
260  "). Skipping the entry.");
261  continue;
262  }
263  channelT0->setT0(slotID, channelID, t0Cal, t0CalErr);
264  if (fitStatus == 0) {
265  nCal[slotID - 1]++;
266  } else {
267  channelT0->setUnusable(slotID, channelID);
268  }
269  }
270 
271  file->Close();
272  B2INFO("--> Input file closed");
273  }
274  channelT0->suppressAverage();
275 
276  IntervalOfValidity iov(firstExp, firstRun, lastExp, lastRun);
277  channelT0.import(iov);
278 
279  short nCalTot = 0;
280  B2INFO("Summary: ");
281  for (int iSlot = 1; iSlot < 17; iSlot++) {
282  B2INFO("--> Number of calibrated channels on Slot " << iSlot << " : " << nCal[iSlot - 1] << "/512");
283  B2INFO("--> Cal on ch 1, 256 and 511: " << channelT0->getT0(iSlot, 0) << ", " << channelT0->getT0(iSlot,
284  257) << ", " << channelT0->getT0(iSlot, 511));
285  nCalTot += nCal[iSlot - 1];
286  }
287 
288 
289  B2RESULT("Channel T0 calibration constants imported to database, calibrated channels: "
290  << nCalTot << "/ 8192");
291  }
292 
293 
294  void TOPDatabaseImporter::importChannelT0(std::string fileName,
295  int expNo, int firstRun, int lastRun)
296  {
297  // declare db object to be imported -- and construct it
299  channelT0.construct();
300 
301  // open the root file
302  TFile* file = TFile::Open(fileName.c_str(), "r");
303  if (!file) {
304  B2ERROR("openFile: " << fileName << " *** failed to open");
305  return;
306  }
307  B2INFO(fileName << ": open for reading");
308 
309  // loop over slots and set channel T0
310  int nModules = TOPGeometryPar::Instance()->getGeometry()->getNumModules();
311  int count = 0; // counter of calibrated constants
312  for (int moduleID = 1; moduleID <= nModules; moduleID++) {
313  std::string name = "channelT0_slot";
314  if (moduleID < 10) name += "0";
315  name += std::to_string(moduleID);
316  auto* h = (TH1F*) file->Get(name.c_str());
317  if (!h) {
318  B2ERROR("Histogram with name '" + name + "' not found");
319  continue;
320  }
321  for (int channel = 0; channel < h->GetNbinsX(); channel++) {
322  double value = h->GetBinContent(channel + 1);
323  double error = h->GetBinError(channel + 1);
324  channelT0->setT0(moduleID, channel, value, error);
325  if (error > 0) {
326  count++;
327  } else {
328  channelT0->setUnusable(moduleID, channel);
329  }
330  }
331  }
332  file->Close();
333 
334  channelT0->suppressAverage();
335 
336  // import to database
337  IntervalOfValidity iov(expNo, firstRun, expNo, lastRun);
338  channelT0.import(iov);
339 
340  B2INFO("Channel T0 for exp " << expNo << " run " << firstRun << " to " << lastRun
341  << " imported. Calibrated channels: " << count << "/" << nModules * 512);
342 
343  }
344 
345 
346  void TOPDatabaseImporter::importAsicShifts_BS13d(double s0, double s1, double s2, double s3,
347  int expNo, int firstRun, int lastRun)
348  {
349 
350  std::vector<double> shifts;
351  shifts.push_back(s0);
352  shifts.push_back(s1);
353  shifts.push_back(s2);
354  shifts.push_back(s3);
355 
357  asicShift.construct();
358 
359  int moduleID = 13;
360  unsigned bs = 3;
361  for (unsigned carrier = 0; carrier < 4; carrier++) {
362  for (unsigned a = 0; a < 4; a++) {
363  unsigned asic = a + carrier * 4 + bs * 16;
364  asicShift->setT0(moduleID, asic, shifts[carrier]);
365  }
366  }
367 
368  IntervalOfValidity iov(expNo, firstRun, expNo, lastRun);
369  asicShift.import(iov);
370 
371  B2INFO("ASIC shifts of BS13d imported for exp " << expNo << " run " << firstRun <<
372  " to " << lastRun);
373  }
374 
375 
376  void TOPDatabaseImporter::importOfflineCommonT0Calibration(string fileName,
377  int firstExp, int firstRun,
378  int lastExp, int lastRun)
379  {
380  TFile* file = TFile::Open(fileName.c_str(), "r");
381  B2INFO("--> Opening constants file " << fileName);
382 
383  if (!file) {
384  B2ERROR("openFile: " << fileName << " *** failed to open");
385  return;
386  }
387  B2INFO("--> " << fileName << ": open for reading");
388 
389  TTree* treeCal = (TTree*)file->Get("tree");
390 
391  if (!treeCal) {
392  B2ERROR("openFile: no tree named tree found in " << fileName);
393  file->Close();
394  return;
395  }
396 
397  float t0 = 0.;
398  float t0Err = 0;
399  float chi2 = 0;
400  float integral = 0;
401  float sigma = 0;
402  int runNum = 0;
403  int fitStatus = 0;
404 
405  treeCal->SetBranchAddress("offset", &t0);
406  treeCal->SetBranchAddress("runNum", &runNum);
407  treeCal->SetBranchAddress("sigma", &sigma);
408  treeCal->SetBranchAddress("offsetErr", &t0Err);
409  treeCal->SetBranchAddress("chi2", &chi2);
410  treeCal->SetBranchAddress("integral", &integral);
411  treeCal->SetBranchAddress("fitStatus", &fitStatus);
412 
413  treeCal->GetEntry(0);
414 
415  if (lastRun == -1 and firstRun == -1) {
416  lastRun = runNum;
417  firstRun = runNum;
418  B2INFO("Using the run number from the tree ");
419  } else {
420  B2INFO("Using the run numbers passed to the importer");
421  }
422  B2INFO("IOV = (" << firstExp << ", " << firstRun << ", "
423  << lastExp << ", " << lastRun << ")");
424 
426  commonT0.construct(t0, t0Err);
427 
428  if (fitStatus == 0 and integral > 10 and sigma > 0.05 and sigma < 0.33) {
429  B2INFO("Good calibration found ");
430  B2INFO("t0 = " << t0 << " +- " << t0Err);
431  B2INFO("sigma = " << sigma);
432  B2INFO("chi2 = " << chi2);
433  } else {
434  B2INFO("BAD calibration found - set calibration to 'unusable'");
435  B2INFO("t0 = " << t0 << " +- " << t0Err);
436  B2INFO("sigma = " << sigma);
437  B2INFO("chi2 = " << chi2);
438  B2INFO("fit status = " << fitStatus);
439  commonT0->setUnusable();
440  }
441 
442  file->Close();
443 
444  IntervalOfValidity iov(firstExp, firstRun, lastExp, lastRun);
445  commonT0.import(iov);
446  B2INFO("--> constants imported");
447  B2INFO(" ");
448  }
449 
450  void TOPDatabaseImporter::importCommonT0(double value, double error,
451  int expNo, int firstRun, int lastRun,
452  bool roughlyCalibrated)
453  {
455  commonT0.construct(value, error);
456  if (roughlyCalibrated) commonT0->setRoughlyCalibrated();
457 
458  IntervalOfValidity iov(expNo, firstRun, expNo, lastRun);
459  commonT0.import(iov);
460 
461  B2INFO("--> constants for exp = " << expNo
462  << " run = " << firstRun << " to " << lastRun << " imported");
463  }
464 
465  void TOPDatabaseImporter::importModuleT0Calibration(string fileName,
466  int firstExp, int firstRun,
467  int lastExp, int lastRun)
468  {
469 
470 
472  moduleT0.construct();
473 
474 
475  ifstream inFile(fileName);
476  B2INFO("--> Opening constants file " << fileName);
477 
478  if (!inFile) {
479  B2ERROR("openFile: " << fileName << " *** failed to open");
480  return;
481  }
482  B2INFO("--> " << fileName << ": open for reading");
483 
484 
485  B2INFO("--> importing constants");
486 
487  while (!inFile.eof()) {
488  int slot = 0;
489  int dummy = 0;
490  double T0 = 0;
491  double T0_err = 0;
492 
493  inFile >> slot >> dummy >> T0 >> T0_err;
494  if (slot < 1 or slot > 16) {
495  B2ERROR("Module ID is not valid. Skipping the entry.");
496  continue;
497  }
498  moduleT0->setT0(slot, T0, T0_err);
499 
500  }
501  inFile.close();
502  B2INFO("--> Input file closed");
503 
504  moduleT0->suppressAverage();
505 
506  IntervalOfValidity iov(firstExp, firstRun, lastExp, lastRun);
507  moduleT0.import(iov);
508 
509  B2INFO("Summary: ");
510  for (int iSlot = 1; iSlot < 17; iSlot++) {
511  B2INFO("--> Time offset of Slot " << iSlot << " = " << moduleT0->getT0(iSlot));
512  }
513 
514 
515  }
516 
517 
518  void TOPDatabaseImporter::importModuleT0(std::string fileName,
519  int expNo, int firstRun, int lastRun)
520  {
521 
522  // construct DB import object
524  moduleT0.construct();
525 
526  // open the root file
527  TFile* file = TFile::Open(fileName.c_str(), "r");
528  if (!file) {
529  B2ERROR("openFile: " << fileName << " *** failed to open");
530  return;
531  }
532  B2INFO(fileName << ": open for reading");
533 
534  // get histogram and set the DB import object
535  auto* h = (TH1F*) file->Get("moduleT0");
536  if (not h) {
537  B2ERROR("no histogram 'moduleT0' found in the file, nothing imported");
538  return;
539  }
540  int count = 0; // counter of calibrated
541  for (int slot = 1; slot <= h->GetNbinsX(); slot++) {
542  double value = h->GetBinContent(slot);
543  double error = h->GetBinError(slot);
544  moduleT0->setT0(slot, value, error);
545  if (error > 0) {
546  count++;
547  } else {
548  moduleT0->setUnusable(slot);
549  }
550  }
551  file->Close();
552 
553  moduleT0->suppressAverage();
554 
555  // import the object
556  IntervalOfValidity iov(expNo, firstRun, expNo, lastRun);
557  moduleT0.import(iov);
558 
559  B2INFO("Module T0 for exp " << expNo << " run " << firstRun << " to " << lastRun
560  << " imported. Calibrated modules: " << count << "/" << 16);
561 
562  }
563 
564 
565  void TOPDatabaseImporter::getSampleTimeCalibrationInfo()
566  {
567  DBObjPtr<TOPCalTimebase> timeBase;
568  if (!timeBase.isValid()) {
569  B2ERROR("No time base calibration available");
570  return;
571  }
572 
573  const auto* geo = TOPGeometryPar::Instance()->getGeometry();
574  int numModules = geo->getNumModules();
575  auto& feMapper = TOPGeometryPar::Instance()->getFrontEndMapper();
576 
577  cout << "Time base calibration: number of calibrated channels in database" << endl << endl;
578  for (int moduleID = 1; moduleID <= numModules; moduleID++) {
579  int ncal[4] = {0, 0, 0, 0};
580  int scrodID[4] = {0, 0, 0, 0};
581  for (int bs = 0; bs < 4; bs++) {
582  auto* femap = feMapper.getMap(moduleID, bs);
583  if (!femap) {
584  B2ERROR("No FrontEnd map available for boardstack " << bs << " of module " << moduleID);
585  continue;
586  }
587  scrodID[bs] = femap->getScrodID();
588  for (int channel = 0; channel < 128; channel++) {
589  if (timeBase->isAvailable(scrodID[bs], channel)) ncal[bs]++;
590  }
591  }
592  if (ncal[0] + ncal[1] + ncal[2] + ncal[3] == 0) continue;
593 
594  cout << "Slot " << moduleID << endl;
595  for (int bs = 0; bs < 4; bs++) {
596  cout << " scrodID " << scrodID[bs] << ": " << ncal[bs] << "/128" << endl;
597  }
598  }
599 
600  cout << endl;
601  }
602 
603 
604  void TOPDatabaseImporter::printSampleTimeCalibration()
605  {
606 
607  DBObjPtr<TOPCalTimebase> timeBase;
608  if (!timeBase.isValid()) {
609  B2ERROR("No time base calibration available");
610  return;
611  }
612 
613  for (const auto& sampleTimes : timeBase->getSampleTimes()) {
614  cout << sampleTimes.getScrodID() << " " << sampleTimes.getChannel() << endl;
615  for (const auto& time : sampleTimes.getTimeAxis()) {
616  cout << time << " ";
617  }
618  cout << endl;
619  }
620 
621  }
622 
623 
624  void TOPDatabaseImporter::importChannelMask(std::string fileName,
625  int expNo, int firstRun, int lastRun)
626  {
627  // declare db object to be imported -- and construct it
629  channelMask.construct();
630 
631  // open the root file
632  TFile* file = TFile::Open(fileName.c_str(), "r");
633  if (!file) {
634  B2ERROR("openFile: " << fileName << " *** failed to open");
635  return;
636  }
637  B2INFO(fileName << ": open for reading");
638 
639  // loop over slots and set channel mask
640  int nModules = TOPGeometryPar::Instance()->getGeometry()->getNumModules();
641  int active = 0, dead = 0, noisy = 0;
642  for (int moduleID = 1; moduleID <= nModules; moduleID++) {
643  std::string name = "slot_" + std::to_string(moduleID);
644  auto* h = (TH1F*) file->Get(name.c_str());
645  if (!h) {
646  B2ERROR("Histogram with name '" + name + "' not found");
647  continue;
648  }
649  for (int channel = 0; channel < h->GetNbinsX(); channel++) {
650  int value = h->GetBinContent(channel + 1);
651  if (value == 0) {
652  channelMask->setActive(moduleID, channel);
653  active++;
654  } else if (value == 1) {
655  channelMask->setDead(moduleID, channel);
656  dead++;
657  } else {
658  channelMask->setNoisy(moduleID, channel);
659  noisy++;
660  }
661  }
662  }
663  file->Close();
664 
665  // import to database
666  IntervalOfValidity iov(expNo, firstRun, expNo, lastRun);
667  channelMask.import(iov);
668 
669  B2INFO("Channel mask for exp " << expNo << " run " << firstRun << " to " << lastRun
670  << " imported. Active channels: " << active << ", dead: " << dead
671  << ", noisy: " << noisy);
672 
673  }
674 
675 
676  void TOPDatabaseImporter::generateFakeChannelMask(double fractionDead,
677  double fractionHot,
678  int firstExp, int firstRun,
679  int lastExp, int lastRun)
680  {
681  // declare db object to be imported -- and construct it
683  channelMask.construct();
684 
685  // set up for loop channel mapper
686  auto& chMapper = TOPGeometryPar::Instance()->getChannelMapper();
687  const size_t nModules = TOPGeometryPar::Instance()->getGeometry()->getNumModules();
688  unsigned ncall = 0;
689  unsigned nall = 0;
690 
691  // loop over module (1-based)
692  for (size_t moduleID = 1; moduleID <= nModules; moduleID++) {
693 
694  // loop over boardStack*carrierBoard*assic*channel to get channel (0 to 512)
695  // TODO: get these loop limits from some sensible enum somewhere
696  for (int boardStack = 0; boardStack < 4; boardStack++) {
697  for (int carrierBoard = 0; carrierBoard < 4; carrierBoard++) {
698  for (int asic = 0; asic < 4; asic++) {
699  for (int chan = 0; chan < 8; chan++) {
700  auto channel = chMapper.getChannel(boardStack, carrierBoard, asic, chan);
701  nall++;
702  if (gRandom->Rndm() < fractionDead) {
703  channelMask->setDead(moduleID, channel);
704  ncall++;
705  }
706  if (gRandom->Rndm() < fractionHot) {
707  channelMask->setNoisy(moduleID, channel);
708  ncall++;
709  }
710  }
711  }
712  }
713  }
714  } // module
715 
716  // declare interval of validity
717  IntervalOfValidity iov(firstExp, firstRun, lastExp, lastRun);
718  channelMask.import(iov);
719 
720  B2RESULT("Generated and imported a fake channel mask to database for testing: "
721  << ncall << "/" << nall);
722  return;
723  }
724 
725 
726  void TOPDatabaseImporter::importPmtQEData(string fileName, string treeName,
727  int firstExp, int firstRun,
728  int lastExp, int lastRun)
729  {
730 
731  // declare db objects to be imported
733 
734  static const int nChann = 16;
735  std::string* serialNum = 0;
736  std::vector<float>* QE_data[nChann];
737  float lambdaFirst, lambdaStep, collEff0, collEff;
738 
739  TBranch* bQE_data[nChann];
740 
741  // open root file and get tree
742  TFile* file = TFile::Open(fileName.c_str(), "r");
743  if (!file) {
744  B2ERROR("Cannot open the file " << fileName);
745  return;
746  }
747  TTree* tQeData = (TTree*)file->Get(treeName.c_str());
748  if (!tQeData) {
749  B2ERROR("No TTree with name " << treeName << " in file " << fileName);
750  file->Close();
751  return;
752  }
753 
754  tQeData->SetBranchAddress("serialNum", &serialNum);
755  tQeData->SetBranchAddress("lambdaFirst", &lambdaFirst);
756  tQeData->SetBranchAddress("lambdaStep", &lambdaStep);
757  tQeData->SetBranchAddress("collEff0", &collEff0);
758  tQeData->SetBranchAddress("collEff", &collEff);
759 
760  for (int ic = 0; ic < nChann; ic++) {
761  // must initialize vectors and branches
762  QE_data[ic] = new std::vector<float>;
763  bQE_data[ic] = new TBranch();
764 
765  TString cString = "QE_ch";
766  cString += ic + 1;
767  tQeData->SetBranchAddress(cString, &QE_data[ic], &bQE_data[ic]);
768  }
769 
770  // loop on input tree entries and construct the pmtQE objects
771  int countPMTs = 0;
772 
773  for (int ient = 0; ient < tQeData->GetEntries(); ient++) {
774 
775  tQeData->GetEntry(ient);
776 
777  auto* pmtQE = pmtQEs.appendNew(*serialNum, lambdaFirst, lambdaStep, collEff0, collEff);
778 
779  for (int ic = 0; ic < nChann; ic++) {
780  int tEntry = tQeData->LoadTree(ient);
781  bQE_data[ic]->GetEntry(tEntry);
782 
783  pmtQE->setQE(ic + 1, *QE_data[ic]);
784  } // end loop on channels
785 
786  countPMTs++;
787  }
788  file->Close();
789 
790  IntervalOfValidity iov(firstExp, firstRun, lastExp, lastRun);
791  pmtQEs.import(iov);
792 
793  B2RESULT("PMT QE data imported to database for " << countPMTs << " PMT's.");
794 
795  return;
796  }
797 
798 
799  void TOPDatabaseImporter::importPmtGainData(string fileName, string treeName,
800  int firstExp, int firstRun,
801  int lastExp, int lastRun)
802  {
803 
804  // declare db objects to be imported
806 
807  static const int nChann = 16;
808  std::string* serialNum = 0;
809  float gain_const[nChann], gain_slope[nChann], gain_ratio[nChann];
810  float hv_op0, hv_op;
811 
812  // open root file and get tree
813  TFile* file = TFile::Open(fileName.c_str(), "r");
814  if (!file) {
815  B2ERROR("Cannot open the file " << fileName);
816  return;
817  }
818  TTree* tGainData = (TTree*)file->Get(treeName.c_str());
819  if (!tGainData) {
820  B2ERROR("No TTree with name " << treeName << " in file " << fileName);
821  file->Close();
822  return;
823  }
824 
825  tGainData->SetBranchAddress("serialNum", &serialNum);
826  tGainData->SetBranchAddress("gain_const", &gain_const);
827  tGainData->SetBranchAddress("gain_slope", &gain_slope);
828  tGainData->SetBranchAddress("gain_ratio", &gain_ratio);
829  tGainData->SetBranchAddress("hv_op0", &hv_op0);
830  tGainData->SetBranchAddress("hv_op", &hv_op);
831 
832 
833  // loop on input tree entries and construct the pmtGain objects
834  int countPMTs = 0;
835 
836  for (int ient = 0; ient < tGainData->GetEntries(); ient++) {
837  tGainData->GetEntry(ient);
838  auto* pmtGain = pmtGains.appendNew(*serialNum);
839 
840  for (int ic = 0; ic < nChann; ic++) {
841  pmtGain->setPmtPixelData(ic + 1, gain_const[ic], gain_slope[ic], gain_ratio[ic]);
842  pmtGain->setNominalHV0(-fabs(hv_op0));
843  pmtGain->setNominalHV(-fabs(hv_op));
844  }
845  countPMTs++;
846  }
847  file->Close();
848 
849  IntervalOfValidity iov(firstExp, firstRun, lastExp, lastRun);
850  pmtGains.import(iov);
851 
852  B2RESULT("PMT gain data imported to database for " << countPMTs << " PMT's.");
853 
854  return;
855  }
856 
857 
858  void TOPDatabaseImporter::importPmtInstallationData(string fileName, string treeName ,
859  int firstExp, int firstRun,
860  int lastExp, int lastRun)
861  {
862 
863  // declare db objects to be imported
865 
866  std::string* serialNum = 0;
867  int moduleCNum, slotNum, arrayNum, PMTposition;
869 
870  // open root file and get tree
871  TFile* file = TFile::Open(fileName.c_str(), "r");
872  if (!file) {
873  B2ERROR("Cannot open the file " << fileName);
874  return;
875  }
876  TTree* tInstData = (TTree*)file->Get(treeName.c_str());
877  if (!tInstData) {
878  B2ERROR("No TTree with name " << treeName << " in file " << fileName);
879  file->Close();
880  return;
881  }
882 
883  tInstData->SetBranchAddress("serialNum", &serialNum);
884  tInstData->SetBranchAddress("moduleCNum", &moduleCNum);
885  tInstData->SetBranchAddress("slotNum", &slotNum);
886  tInstData->SetBranchAddress("arrayNum", &arrayNum);
887  tInstData->SetBranchAddress("PMTposition", &PMTposition);
888  tInstData->SetBranchAddress("type", &type);
889 
890  // loop on input tree entries and construct the pmtInstallation objects
891  int countPMTs = 0;
892 
893  for (int ient = 0; ient < tInstData->GetEntries(); ient++) {
894  tInstData->GetEntry(ient);
895  pmtInst.appendNew(*serialNum, moduleCNum, slotNum, arrayNum, PMTposition, type);
896  countPMTs++;
897  }
898  file->Close();
899 
900  IntervalOfValidity iov(firstExp, firstRun, lastExp, lastRun);
901  pmtInst.import(iov);
902 
903  B2RESULT("PMT installation data imported to database for " << countPMTs << " PMT's.");
904 
905  }
906 
907 
908  void TOPDatabaseImporter::importPmtObsoleteData(string fileName, string treeName,
909  int firstExp, int firstRun,
910  int lastExp, int lastRun)
911  {
912 
913  // declare db objects to be imported
915 
916  std::string* serialNum = 0;
917  std::string* cathode = 0;
918  float hv_spec, dark_spec, qe380_spec;
920 
921  // open root file and get tree
922  TFile* file = TFile::Open(fileName.c_str(), "r");
923  if (!file) {
924  B2ERROR("Cannot open the file " << fileName);
925  return;
926  }
927  TTree* tObsData = (TTree*)file->Get(treeName.c_str());
928  if (!tObsData) {
929  B2ERROR("No TTree with name " << treeName << " in file " << fileName);
930  file->Close();
931  return;
932  }
933 
934  tObsData->SetBranchAddress("serialNum", &serialNum);
935  tObsData->SetBranchAddress("cathode", &cathode);
936  tObsData->SetBranchAddress("hv_spec", &hv_spec);
937  tObsData->SetBranchAddress("dark_spec", &dark_spec);
938  tObsData->SetBranchAddress("qe380_spec", &qe380_spec);
939  tObsData->SetBranchAddress("type", &type);
940 
941  // loop on input tree entries and construct the pmt obsolete data objects
942  int countPMTs = 0;
943 
944  for (int ient = 0; ient < tObsData->GetEntries(); ient++) {
945  tObsData->GetEntry(ient);
946 
947  // make sure the HV from specifications is negative
948  hv_spec = -fabs(hv_spec);
949 
950  pmtObsData.appendNew(*serialNum, type, *cathode, hv_spec, dark_spec, qe380_spec);
951  countPMTs++;
952  }
953 
954  IntervalOfValidity iov(firstExp, firstRun, lastExp, lastRun);
955  pmtObsData.import(iov);
956 
957  B2RESULT("PMT obsolete data imported to database for " << countPMTs << " PMT's.");
958 
959  file->Close();
960 
961  delete serialNum;
962  delete cathode;
963 
964  return;
965  }
966 
967 
968  void TOPDatabaseImporter::importPmtTTSPar(string fileName, string treeName,
969  int firstExp, int firstRun,
970  int lastExp, int lastRun)
971  {
972 
973  // declare db objects to be imported
974  DBImportArray<TOPPmtTTSPar> pmtTtsPars;
975 
976  static const int nChann = 16;
977  std::string* serialNum = 0;
978  std::vector<float>* gausFrac[nChann];
979  std::vector<float>* gausMean[nChann];
980  std::vector<float>* gausSigma[nChann];
981 
982  TBranch* bGFrac[nChann];
983  TBranch* bGMean[nChann];
984  TBranch* bGSigma[nChann];
985 
986 
987  // open root file and get tree
988  TFile* file = TFile::Open(fileName.c_str(), "r");
989  if (!file) {
990  B2ERROR("Cannot open the file " << fileName);
991  return;
992  }
993  TTree* tTtsPar = (TTree*)file->Get(treeName.c_str());
994  if (!tTtsPar) {
995  B2ERROR("No TTree with name " << treeName << " in file " << fileName);
996  file->Close();
997  return;
998  }
999 
1000  tTtsPar->SetBranchAddress("serialNum", &serialNum);
1001  for (int ic = 0; ic < nChann; ic++) {
1002  // must initialize vectors and branches
1003  gausFrac[ic] = new std::vector<float>;
1004  gausMean[ic] = new std::vector<float>;
1005  gausSigma[ic] = new std::vector<float>;
1006 
1007  bGFrac[ic] = new TBranch();
1008  bGMean[ic] = new TBranch();
1009  bGSigma[ic] = new TBranch();
1010 
1011 
1012  TString cStringF = "gausFrac_ch";
1013  TString cStringM = "gausMean_ch";
1014  TString cStringS = "gausSigma_ch";
1015 
1016  cStringF += ic + 1;
1017  cStringM += ic + 1;
1018  cStringS += ic + 1;
1019 
1020  tTtsPar->SetBranchAddress(cStringF, &gausFrac[ic], &bGFrac[ic]);
1021  tTtsPar->SetBranchAddress(cStringM, &gausMean[ic], &bGMean[ic]);
1022  tTtsPar->SetBranchAddress(cStringS, &gausSigma[ic], &bGSigma[ic]);
1023  }
1024 
1025  // loop on input tree entries and construct the pmt tts par objects
1026  int countPMTs = 0;
1027 
1028  for (int ient = 0; ient < tTtsPar->GetEntries(); ient++) {
1029 
1030  tTtsPar->GetEntry(ient);
1031 
1032  auto* pmtTtsPar = pmtTtsPars.appendNew(*serialNum);
1033 
1034  for (int ic = 0; ic < nChann; ic++) {
1035 
1036  int tEntry = tTtsPar->LoadTree(ient);
1037  bGFrac[ic]->GetEntry(tEntry);
1038  bGMean[ic]->GetEntry(tEntry);
1039  bGSigma[ic]->GetEntry(tEntry);
1040 
1041  // check that the vectors have the same size. Otherwise skip the channel
1042  if ((gausFrac[ic]->size() != gausMean[ic]->size()) ||
1043  (gausFrac[ic]->size() != gausSigma[ic]->size())) {
1044 
1045  B2ERROR("The TTSPar vectors for PMT " << serialNum << ", channel " << ic + 1 << " have different sizes! Skipping channel...");
1046  continue;
1047  }
1048 
1049  for (uint iv = 0; iv < gausFrac[ic]->size(); iv++) {
1050  pmtTtsPar->appendGaussian(ic + 1,
1051  gausFrac[ic]->at(iv),
1052  gausMean[ic]->at(iv),
1053  gausSigma[ic]->at(iv));
1054  }
1055  }
1056  countPMTs++;
1057  }
1058  file->Close();
1059 
1060  IntervalOfValidity iov(firstExp, firstRun, lastExp, lastRun);
1061  pmtTtsPars.import(iov);
1062 
1063  B2RESULT("PMT TTS parameters imported to database for " << countPMTs << " PMT's.");
1064 
1065  return;
1066  }
1067 
1068 
1069  void TOPDatabaseImporter::importPmtTTSHisto(string fileName,
1070  string treeName,
1071  int firstExp, int firstRun,
1072  int lastExp, int lastRun)
1073  {
1074 
1075  // declare db objects to be imported
1076  DBImportArray<TOPPmtTTSHisto> pmtTtsHistos;
1077 
1078  static const int nChann = 16;
1079  std::string* serialNum = 0;
1080  float hv = 0;
1081  TH1F* histo[nChann] = {0};
1082 
1083  // open root file and get tree
1084  TFile* file = TFile::Open(fileName.c_str(), "r");
1085  if (!file) {
1086  B2ERROR("Cannot open the file " << fileName);
1087  return;
1088  }
1089  TTree* tTtsHisto = (TTree*)file->Get(treeName.c_str());
1090  if (!tTtsHisto) {
1091  B2ERROR("No TTree with name " << treeName << " in file " << fileName);
1092  file->Close();
1093  return;
1094  }
1095 
1096  tTtsHisto->SetBranchAddress("serialNum", &serialNum);
1097  tTtsHisto->SetBranchAddress("hv", &hv);
1098  for (int ic = 0; ic < nChann; ic++) {
1099  TString hString = "hist_ch";
1100  hString += ic + 1;
1101  tTtsHisto->SetBranchAddress(hString, &histo[ic]);
1102  }
1103 
1104  // loop on input tree entries and construct the pmt tts histo objects
1105  int countHists = 0;
1106 
1107  for (int ient = 0; ient < tTtsHisto->GetEntries(); ient++) {
1108 
1109  tTtsHisto->GetEntry(ient);
1110 
1111  // make sure the HV used in the test is negative
1112  hv = -fabs(hv);
1113 
1114  B2INFO("Saving TTS histograms for PMT " << *serialNum << ", HV = " << hv);
1115 
1116  auto* pmtTtsHisto = pmtTtsHistos.appendNew(*serialNum, hv);
1117  for (int ic = 0; ic < nChann; ic++) {
1118  pmtTtsHisto->setHistogram(ic + 1, histo[ic]);
1119  }
1120  countHists++;
1121  }
1122  file->Close();
1123 
1124  IntervalOfValidity iov(firstExp, firstRun, lastExp, lastRun);
1125  pmtTtsHistos.import(iov);
1126 
1127  B2RESULT("Imported " << countHists << " sets of TTS histograms from " << fileName << " file.");
1128 
1129  return;
1130  }
1131 
1132  void TOPDatabaseImporter::importPmtPulseHeightFitResult(std::string fileName,
1133  int firstExp, int firstRun,
1134  int lastExp, int lastRun)
1135  {
1136  // declare db objects to be imported
1137  DBImportObjPtr<TOPCalChannelPulseHeight> calChannelPulseHeight;
1138  DBImportObjPtr<TOPCalChannelThresholdEff> calChannelThresholdEff;
1139  calChannelPulseHeight.construct();
1140  calChannelThresholdEff.construct();
1141 
1142  TFile* file = TFile::Open(fileName.c_str());
1143  if (!file) {
1144  B2ERROR("openFile: " << fileName << " *** failed to open");
1145  return;
1146  }
1147  TTree* tr = (TTree*)file->Get("tree"); // defined in TOPGainEfficiencyCalculatorModule
1148  if (!tr) {
1149  B2ERROR("No TTree with name tree found in " << fileName);
1150  file->Close();
1151  return;
1152  }
1153 
1154  short slotId = 0;
1155  short pixelId = 0;
1156  float p1 = -1;
1157  float p2 = -1;
1158  float x0 = -1;
1159  float threshold = -1;
1160  float efficiency = -1;
1161  float chisquare = -1;
1162  int ndf = 0;
1163  tr->SetBranchAddress("slotId", &slotId);
1164  tr->SetBranchAddress("pixelId", &pixelId);
1165  tr->SetBranchAddress("p1UseIntegral", &p1);
1166  tr->SetBranchAddress("p2UseIntegral", &p2);
1167  tr->SetBranchAddress("x0UseIntegral", &x0);
1168  tr->SetBranchAddress("thresholdForIntegral", &threshold);
1169  tr->SetBranchAddress("efficiencyUseIntegral", &efficiency);
1170  tr->SetBranchAddress("chisquareUseIntegral", &chisquare);
1171  tr->SetBranchAddress("ndfUseIntegral", &ndf);
1172 
1173  const auto& channelMapper = TOPGeometryPar::Instance()->getChannelMapper();
1174  if (!channelMapper.isValid()) {
1175  B2ERROR("No valid channel mapper found");
1176  file->Close();
1177  return;
1178  }
1179 
1180  long nEntries = tr->GetEntries();
1181  std::map<short, float> reducedChisqMap;
1182  for (long iEntry = 0 ; iEntry < nEntries ; iEntry++) {
1183  tr->GetEntry(iEntry);
1184 
1185  if (efficiency < 0) continue;
1186 
1187  if (!channelMapper.isPixelIDValid(pixelId)) {
1188  B2ERROR("invalid pixelID" << pixelId);
1189  continue;
1190  }
1191  auto channel = channelMapper.getChannel(pixelId);
1192  short globalChannelNumber = slotId * 1000 + channel;
1193  float redChisq = chisquare / ndf;
1194 
1195  //in case entries for the same channel appears multiple time, use data with smaller reduced chisquare
1196  //(This can happen when distribution is fit manually and results are appended for channels with fit failure)
1197  if (reducedChisqMap.count(globalChannelNumber) == 0
1198  or reducedChisqMap[globalChannelNumber] > redChisq) {
1199  reducedChisqMap[globalChannelNumber] = redChisq;
1200  calChannelPulseHeight->setParameters(slotId, channel, x0, p1, p2);
1201  calChannelThresholdEff->setThrEff(slotId, channel, efficiency, (short)threshold);
1202 
1203  if (redChisq > 10.) {
1204  calChannelPulseHeight->setUnusable(slotId, channel);
1205  calChannelThresholdEff->setUnusable(slotId, channel);
1206  }
1207  }
1208  }
1209  file->Close();
1210 
1211  IntervalOfValidity iov(firstExp, firstRun, lastExp, lastRun);
1212  calChannelPulseHeight.import(iov);
1213  calChannelThresholdEff.import(iov);
1214 
1215  B2RESULT("Imported channel-by-channel gain and efficiency data from fitting of pulse height distribution for "
1216  << reducedChisqMap.size() << " channels from " << fileName << " file.");
1217 
1218  return;
1219  }
1220 
1221 
1222  void TOPDatabaseImporter::exportPmtTTSHisto(string outFileName)
1223  {
1224 
1225  // this is just an example on how to retrieve TTS histograms
1226  DBArray<TOPPmtTTSHisto> elements("TOPPmtTTSHistos");
1227 
1228  TFile file(outFileName.c_str(), "recreate");
1229 
1230  // prints serialNum of PMTs and hv setting used, and saves TTS histograms to root file
1231  for (const auto& element : elements) {
1232 
1233  B2INFO("serialNum = " << element.getSerialNumber() << ", HV = " << element.getHV());
1234  for (int ic = 0; ic < element.getNumOfPixels(); ic++) {
1235  const auto* ttsHisto = element.getHistogram(ic + 1);
1236  if (ttsHisto) ttsHisto->Write();
1237  }
1238  }
1239 
1240  file.Close();
1241 
1242  return;
1243  }
1244 
1245  void TOPDatabaseImporter::importFrontEndSettings(int lookback, int readoutWin,
1246  int extraWin, int offset,
1247  int expNo, int firstRun, int lastRun)
1248  {
1250  feSetting.construct();
1251 
1252  // write-window depths (write-window is 128 samples)
1253  std::vector<int> writeDepths;
1254  for (int i = 0; i < 3; i++) {
1255  writeDepths.push_back(214);
1256  writeDepths.push_back(212);
1257  writeDepths.push_back(214);
1258  }
1259  feSetting->setWriteDepths(writeDepths);
1260  feSetting->setLookbackWindows(lookback);
1261  feSetting->setReadoutWindows(readoutWin);
1262  feSetting->setExtraWindows(extraWin);
1263  feSetting->setOffset(offset);
1264 
1265  // window shifts
1266  std::vector<int> shifts = {0, 0, 1, 1, 1, 2};
1267  feSetting->setWindowShifts(shifts);
1268 
1269  IntervalOfValidity iov(expNo, firstRun, expNo, lastRun);
1270  feSetting.import(iov);
1271 
1272  B2INFO("Front-end settings imported for exp " << expNo << " run " << firstRun <<
1273  " to " << lastRun);
1274  }
1275 
1276 
1277  void TOPDatabaseImporter::importDummyCalModuleAlignment(int firstExp, int firstRun,
1278  int lastExp, int lastRun)
1279  {
1280  IntervalOfValidity iov(firstExp, firstRun, lastExp, lastRun);
1281  DBImportObjPtr<TOPCalModuleAlignment> moduleAlignment;
1282  moduleAlignment.construct();
1283  moduleAlignment.import(iov);
1284  B2INFO("Dummy TOPCalModuleAlignment imported");
1285  return;
1286  }
1287 
1288  void TOPDatabaseImporter::importDummyCalModuleT0(int firstExp, int firstRun,
1289  int lastExp, int lastRun)
1290  {
1291  IntervalOfValidity iov(firstExp, firstRun, lastExp, lastRun);
1293  moduleT0.construct();
1294  moduleT0.import(iov);
1295  B2INFO("Dummy TOPCalModuleT0 imported");
1296  return;
1297  }
1298 
1299  void TOPDatabaseImporter::importDummyCalChannelT0(int firstExp, int firstRun,
1300  int lastExp, int lastRun)
1301  {
1302  IntervalOfValidity iov(firstExp, firstRun, lastExp, lastRun);
1304  channelT0.construct();
1305  channelT0.import(iov);
1306  B2INFO("Dummy TOPCalChannelT0 imported");
1307  return;
1308  }
1309 
1310  void TOPDatabaseImporter::importDummyCalTimebase(int firstExp, int firstRun,
1311  int lastExp, int lastRun)
1312  {
1313  IntervalOfValidity iov(firstExp, firstRun, lastExp, lastRun);
1315  timebase.construct();
1316  timebase.import(iov);
1317  B2INFO("Dummy TOPCalTimebase imported");
1318  return;
1319  }
1320 
1321  void TOPDatabaseImporter::importDummyCalChannelNoise(int firstExp, int firstRun,
1322  int lastExp, int lastRun)
1323  {
1324  IntervalOfValidity iov(firstExp, firstRun, lastExp, lastRun);
1326  channelNoise.construct();
1327  channelNoise.import(iov);
1328  B2INFO("Dummy TOPCalChannelNoise imported");
1329  return;
1330  }
1331 
1332  void TOPDatabaseImporter::importDummyCalChannelPulseHeight(int firstExp, int firstRun,
1333  int lastExp, int lastRun)
1334  {
1335  IntervalOfValidity iov(firstExp, firstRun, lastExp, lastRun);
1337  pulseHeight.construct();
1338  pulseHeight.import(iov);
1339  B2INFO("Dummy TOPCalChannelPulseHeight imported");
1340  return;
1341  }
1342 
1343  void TOPDatabaseImporter::importDummyCalChannelRQE(int firstExp, int firstRun,
1344  int lastExp, int lastRun)
1345  {
1346  IntervalOfValidity iov(firstExp, firstRun, lastExp, lastRun);
1348  channelRQE.construct();
1349  channelRQE.import(iov);
1350  B2INFO("Dummy TOPCalChannelRQE imported");
1351  return;
1352  }
1353 
1354  void TOPDatabaseImporter::importDummyCalChannelThresholdEff(int firstExp, int firstRun,
1355  int lastExp, int lastRun)
1356  {
1357  IntervalOfValidity iov(firstExp, firstRun, lastExp, lastRun);
1358  DBImportObjPtr<TOPCalChannelThresholdEff> channelThresholdEff;
1359  channelThresholdEff.construct();
1360  channelThresholdEff.import(iov);
1361  B2INFO("Dummy TOPCalChannelThresholdEff imported");
1362  return;
1363  }
1364 
1365  void TOPDatabaseImporter::importDummyCalChannelThreshold(int firstExp, int firstRun,
1366  int lastExp, int lastRun)
1367  {
1368  IntervalOfValidity iov(firstExp, firstRun, lastExp, lastRun);
1369  DBImportObjPtr<TOPCalChannelThreshold> channelThreshold;
1370  channelThreshold.construct();
1371  channelThreshold.import(iov);
1372  B2INFO("Dummy TOPCalChannelThreshold imported");
1373  return;
1374  }
1375 
1376  void TOPDatabaseImporter::importDummyCalCommonT0(int firstExp, int firstRun,
1377  int lastExp, int lastRun)
1378  {
1379  IntervalOfValidity iov(firstExp, firstRun, lastExp, lastRun);
1381  commonT0.construct();
1382  commonT0.import(iov);
1383  B2INFO("Dummy TOPCalCommonT0 imported");
1384  return;
1385  }
1386 
1387  void TOPDatabaseImporter::importDummyCalIntegratedCharge(int firstExp, int firstRun,
1388  int lastExp, int lastRun)
1389  {
1390  IntervalOfValidity iov(firstExp, firstRun, lastExp, lastRun);
1391  DBImportObjPtr<TOPCalIntegratedCharge> integratedCharge;
1392  integratedCharge.construct();
1393  integratedCharge.import(iov);
1394  B2INFO("Dummy TOPCalIntegratedCharge imported");
1395  return;
1396  }
1397 
1398  void TOPDatabaseImporter::importDummyCalAsicShift(int firstExp, int firstRun,
1399  int lastExp, int lastRun)
1400  {
1401  IntervalOfValidity iov(firstExp, firstRun, lastExp, lastRun);
1403  asicShift.construct();
1404  asicShift.import(iov);
1405  B2INFO("Dummy TOPCalAsicShift imported");
1406  }
1407 
1408  void TOPDatabaseImporter::correctTOPPmtQE()
1409  {
1410  B2ERROR("Function disabled since the corrected payload TOPPmtQEs already imported");
1411  return;
1412 
1413  DBArray<TOPPmtQE> pmtQEData;
1414  DBImportArray<TOPPmtQE> pmtQECorrected;
1415 
1416  for (const auto& pmt : pmtQEData) {
1417  auto* pmtCorr = pmtQECorrected.appendNew(pmt.getSerialNumber(),
1418  pmt.getLambdaFirst(),
1419  pmt.getLambdaStep(),
1420  pmt.getCE(false),
1421  pmt.getCE(true));
1422  for (unsigned pmtPixel = 1; pmtPixel <= TOPPmtQE::c_NumPmtPixels; pmtPixel++) {
1423  auto qeData = pmt.getQE(pmtPixel);
1424  float lambda = pmt.getLambdaFirst();
1425  float step = pmt.getLambdaStep();
1426  for (auto& qe : qeData) {
1427  double n = phind_lambda_(&lambda); // phase refractive index of quartz
1428  double reflectance = pow((n - 1) / (n + 1), 2);
1429  qe /= (1 - reflectance);
1430  lambda += step;
1431  }
1432  pmtCorr->setQE(pmtPixel, qeData);
1433  }
1434  }
1435 
1436  IntervalOfValidity iov(0, 0, -1, -1);
1437  pmtQECorrected.import(iov);
1438 
1439  B2RESULT("Corrected PMT QE data imported to database for "
1440  << pmtQECorrected.getEntries() << " PMT's.");
1441 
1442  }
1443 
1444 
1445  void TOPDatabaseImporter::importTimeWalk(PyObject* list, double a, double b,
1446  int firstExp, int firstRun,
1447  int lastExp, int lastRun)
1448  {
1449 
1450  std::vector<double> params;
1451  if (PyList_Check(list)) {
1452  for (Py_ssize_t i = 0; i < PyList_Size(list); i++) {
1453  PyObject* value = PyList_GetItem(list, i);
1454  params.push_back(PyFloat_AsDouble(value));
1455  B2INFO(i << " " << params.back());
1456  }
1457  } else {
1458  B2ERROR("Input Python object is not a list");
1459  return;
1460  }
1461 
1463  timeWalk.construct();
1464  timeWalk->set(params, a, b);
1465 
1466  IntervalOfValidity iov(firstExp, firstRun, lastExp, lastRun);
1467  timeWalk.import(iov);
1468 
1469  B2RESULT("Time-walk constants imported");
1470  }
1471 
1472 
1473 
1474 //---- for testing only -- will be removed --------------------------------
1475 
1476  void TOPDatabaseImporter::importTest(int runNumber, double syncTimeBase)
1477  {
1478 
1480  vector<double> timeAxis;
1481  for (int i = 0; i < 256; i++) {
1482  timeAxis.push_back(syncTimeBase / 128.0 * i);
1483  }
1484 
1485 
1486  timeBase.construct(syncTimeBase);
1487  for (unsigned scrodID = 0; scrodID < 64; scrodID++) {
1488  for (unsigned channel = 0; channel < 128; channel++) {
1489  timeBase->append(scrodID, channel, timeAxis);
1490  }
1491  }
1492 
1493  if (runNumber == 3) {
1494  timeBase.addEventDependency(10);
1495  timeBase.construct(syncTimeBase + 100);
1496  for (unsigned scrodID = 0; scrodID < 64; scrodID++) {
1497  for (unsigned channel = 0; channel < 128; channel++) {
1498  timeBase->append(scrodID, channel, timeAxis);
1499  }
1500  }
1501  }
1502 
1503  IntervalOfValidity iov(1, runNumber, 1, runNumber);
1504  timeBase.import(iov);
1505 
1506  }
1507 
1508 
1509  void TOPDatabaseImporter::importTest()
1510  {
1511 
1513 
1514  auto* pmtGain = pmtGains.appendNew("JT00123");
1515  pmtGain->setNominalHV(3520);
1516  for (unsigned channel = 1; channel <= 16; channel++) {
1517  pmtGain->setPmtPixelData(channel, -13.77, 0.0042, 0.4);
1518  }
1519 
1520  pmtGain = pmtGains.appendNew("JT02135");
1521  pmtGain->setNominalHV(3450);
1522  for (unsigned channel = 1; channel <= 16; channel++) {
1523  pmtGain->setPmtPixelData(channel, -12.77, 0.0045, 0.4);
1524  }
1525 
1526  for (const auto& gain : pmtGains) gain.print();
1527 
1528  // IntervalOfValidity iov(0, 0, -1, -1); // all experiments and runs
1529  // pmtGains.import(iov);
1530 
1531 
1532  }
1533 
1534 
1536 } // end Belle2 namespace
1537 
Belle2::IntervalOfValidity
A class that describes the interval of experiments/runs for which an object in the database is valid.
Definition: IntervalOfValidity.h:35
Belle2::DBImportBase::addEventDependency
virtual void addEventDependency(unsigned int eventNumber)
add event dependency
Definition: DBImportBase.h:57
Belle2::DBImportArray::appendNew
T * appendNew()
Construct a new T object at the end of the array.
Definition: DBImportArray.h:77
Belle2::DBImportArray::getEntries
int getEntries() const
Returns number of objects in the array.
Definition: DBImportArray.h:64
Belle2::TOPSampleTimes::getTimeAxis
std::vector< double > getTimeAxis() const
Returns time axis (sample times)
Definition: TOPSampleTimes.cc:50
Belle2::DBArray
Class for accessing arrays of objects in the database.
Definition: DBArray.h:36
Belle2::DBImportObjPtr::construct
void construct(Args &&... params)
Construct an object of type T in this DBImportObjPtr using the provided constructor arguments.
Definition: DBImportObjPtr.h:57
Belle2::DBImportBase::import
bool import(const IntervalOfValidity &iov)
Import the object to database.
Definition: DBImportBase.cc:38
Belle2::DBObjPtr
Class for accessing objects in the database.
Definition: DBObjPtr.h:31
Belle2::TOPPmtObsoleteData::EType
EType
enum for PMT types
Definition: TOPPmtObsoleteData.h:41
Belle2::TOPSampleTimes
Calibration constants of a singe ASIC channel: time axis (sample times)
Definition: TOPSampleTimes.h:34
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::DBImportObjPtr
Class for importing a single object to the database.
Definition: DBImportObjPtr.h:33
Belle2::DBAccessorBase::isValid
bool isValid() const
Check whether a valid object was obtained from the database.
Definition: DBAccessorBase.h:75
Belle2::DBImportArray
Class for importing array of objects to the database.
Definition: DBImportArray.h:35