12 #include <top/calibration/TOPDatabaseImporter.h>
13 #include <top/geometry/TOPGeometryPar.h>
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>
23 #include <framework/logging/Logger.h>
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>
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>
48 #include <top/dbobjects/TOPFrontEndSetting.h>
60 float phind_lambda_(
float*);
73 void TOPDatabaseImporter::importSampleTimeCalibration(
string fNames,
74 int firstExp,
int firstRun,
75 int lastExp,
int lastRun)
80 vector<string> fileNames;
81 stringstream ss(fNames);
84 fileNames.push_back(fName);
89 const auto* geo = TOPGeometryPar::Instance()->getGeometry();
90 auto syncTimeBase = geo->getNominalTDC().getSyncTimeBase();
99 for (
const auto& fileName : fileNames) {
100 TFile* file = TFile::Open(fileName.c_str(),
"r");
102 B2ERROR(
"openFile: " << fileName <<
" *** failed to open");
105 B2INFO(fileName <<
": open for reading");
107 TH1F* hsuccess = (TH1F*) file->Get(
"success");
109 B2ERROR(
"Fit status histogram '" << hsuccess <<
"' not found");
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;
119 string hname =
"sampleTimes_ch" + to_string(channel);
121 TH1F* hsampleTimes = (TH1F*) file->Get(hname.c_str());
123 B2ERROR(
"Histogram '" << hname <<
"' with calibration constants not found");
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 <<
"'");
135 int len = ichannel - iscrod;
137 B2ERROR(
"Unsuccessful parsing of scrodID from '" << title <<
"'");
140 int scrodID = stoi(title.substr(iscrod, len));
141 scrodIDs.insert(scrodID);
144 if (hsampleTimes->GetBinContent(257) > 0)
145 rescale = 2 * syncTimeBase / hsampleTimes->GetBinContent(257);
147 vector<double> sampleTimes;
148 for (
int isamp = 0; isamp < 256; isamp++) {
149 sampleTimes.push_back(hsampleTimes->GetBinContent(isamp + 1) * rescale);
153 timeBase->append(scrodID, channel, sampleTimes);
157 B2INFO(
"--> number of calibrated channels: " << goodChannels);
158 B2INFO(
"file closed");
163 B2INFO(
"set constants for uncalibrated channels using nearest calibrated channel within an ASIC");
164 for (
auto scrodID : scrodIDs) {
166 for (
int as = 0; as < nasic; as++) {
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());
177 B2INFO(
"No calibration available for ASIC " << as <<
" of scrodID " << scrodID);
188 int nall = timeBase->getSampleTimes().size();
190 for (
const auto& sampleTimes : timeBase->getSampleTimes()) {
191 if (sampleTimes.isCalibrated()) ncal++;
194 B2RESULT(
"Sample time calibration constants imported to database, calibrated channels: "
195 << ncal <<
"/" << nall);
199 void TOPDatabaseImporter::importLocalT0Calibration(
string fNames,
200 int firstExp,
int firstRun,
201 int lastExp,
int lastRun)
203 vector<string> fileNames;
204 stringstream ss(fNames);
206 while (ss >> fName) {
207 fileNames.push_back(fName);
210 const auto* geo = TOPGeometryPar::Instance()->getGeometry();
217 for (
const auto& fileName : fileNames) {
218 TFile* file = TFile::Open(fileName.c_str(),
"r");
219 B2INFO(
"--> Opening constants file " << fileName);
222 B2ERROR(
"openFile: " << fileName <<
" *** failed to open");
225 B2INFO(
"--> " << fileName <<
": open for reading");
227 TTree* treeCal = (TTree*)file->Get(
"chT0");
230 B2ERROR(
"openFile: no tree named chT0 found in " << fileName);
236 double t0CalErr = 0.;
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);
247 B2INFO(
"--> importing constats");
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.");
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.");
263 channelT0->setT0(slotID, channelID, t0Cal, t0CalErr);
264 if (fitStatus == 0) {
267 channelT0->setUnusable(slotID, channelID);
272 B2INFO(
"--> Input file closed");
274 channelT0->suppressAverage();
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];
289 B2RESULT(
"Channel T0 calibration constants imported to database, calibrated channels: "
290 << nCalTot <<
"/ 8192");
294 void TOPDatabaseImporter::importChannelT0(std::string fileName,
295 int expNo,
int firstRun,
int lastRun)
302 TFile* file = TFile::Open(fileName.c_str(),
"r");
304 B2ERROR(
"openFile: " << fileName <<
" *** failed to open");
307 B2INFO(fileName <<
": open for reading");
310 int nModules = TOPGeometryPar::Instance()->getGeometry()->getNumModules();
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());
318 B2ERROR(
"Histogram with name '" + name +
"' not found");
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);
328 channelT0->setUnusable(moduleID, channel);
334 channelT0->suppressAverage();
340 B2INFO(
"Channel T0 for exp " << expNo <<
" run " << firstRun <<
" to " << lastRun
341 <<
" imported. Calibrated channels: " << count <<
"/" << nModules * 512);
346 void TOPDatabaseImporter::importAsicShifts_BS13d(
double s0,
double s1,
double s2,
double s3,
347 int expNo,
int firstRun,
int lastRun)
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);
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]);
371 B2INFO(
"ASIC shifts of BS13d imported for exp " << expNo <<
" run " << firstRun <<
376 void TOPDatabaseImporter::importOfflineCommonT0Calibration(
string fileName,
377 int firstExp,
int firstRun,
378 int lastExp,
int lastRun)
380 TFile* file = TFile::Open(fileName.c_str(),
"r");
381 B2INFO(
"--> Opening constants file " << fileName);
384 B2ERROR(
"openFile: " << fileName <<
" *** failed to open");
387 B2INFO(
"--> " << fileName <<
": open for reading");
389 TTree* treeCal = (TTree*)file->Get(
"tree");
392 B2ERROR(
"openFile: no tree named tree found in " << fileName);
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);
413 treeCal->GetEntry(0);
415 if (lastRun == -1 and firstRun == -1) {
418 B2INFO(
"Using the run number from the tree ");
420 B2INFO(
"Using the run numbers passed to the importer");
422 B2INFO(
"IOV = (" << firstExp <<
", " << firstRun <<
", "
423 << lastExp <<
", " << lastRun <<
")");
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);
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();
446 B2INFO(
"--> constants imported");
450 void TOPDatabaseImporter::importCommonT0(
double value,
double error,
451 int expNo,
int firstRun,
int lastRun,
452 bool roughlyCalibrated)
456 if (roughlyCalibrated) commonT0->setRoughlyCalibrated();
461 B2INFO(
"--> constants for exp = " << expNo
462 <<
" run = " << firstRun <<
" to " << lastRun <<
" imported");
465 void TOPDatabaseImporter::importModuleT0Calibration(
string fileName,
466 int firstExp,
int firstRun,
467 int lastExp,
int lastRun)
475 ifstream inFile(fileName);
476 B2INFO(
"--> Opening constants file " << fileName);
479 B2ERROR(
"openFile: " << fileName <<
" *** failed to open");
482 B2INFO(
"--> " << fileName <<
": open for reading");
485 B2INFO(
"--> importing constants");
487 while (!inFile.eof()) {
493 inFile >> slot >> dummy >> T0 >> T0_err;
494 if (slot < 1 or slot > 16) {
495 B2ERROR(
"Module ID is not valid. Skipping the entry.");
498 moduleT0->setT0(slot, T0, T0_err);
502 B2INFO(
"--> Input file closed");
504 moduleT0->suppressAverage();
510 for (
int iSlot = 1; iSlot < 17; iSlot++) {
511 B2INFO(
"--> Time offset of Slot " << iSlot <<
" = " << moduleT0->getT0(iSlot));
518 void TOPDatabaseImporter::importModuleT0(std::string fileName,
519 int expNo,
int firstRun,
int lastRun)
527 TFile* file = TFile::Open(fileName.c_str(),
"r");
529 B2ERROR(
"openFile: " << fileName <<
" *** failed to open");
532 B2INFO(fileName <<
": open for reading");
535 auto* h = (TH1F*) file->Get(
"moduleT0");
537 B2ERROR(
"no histogram 'moduleT0' found in the file, nothing imported");
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);
548 moduleT0->setUnusable(slot);
553 moduleT0->suppressAverage();
559 B2INFO(
"Module T0 for exp " << expNo <<
" run " << firstRun <<
" to " << lastRun
560 <<
" imported. Calibrated modules: " << count <<
"/" << 16);
565 void TOPDatabaseImporter::getSampleTimeCalibrationInfo()
569 B2ERROR(
"No time base calibration available");
573 const auto* geo = TOPGeometryPar::Instance()->getGeometry();
574 int numModules = geo->getNumModules();
575 auto& feMapper = TOPGeometryPar::Instance()->getFrontEndMapper();
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);
584 B2ERROR(
"No FrontEnd map available for boardstack " << bs <<
" of module " << moduleID);
587 scrodID[bs] = femap->getScrodID();
588 for (
int channel = 0; channel < 128; channel++) {
589 if (timeBase->isAvailable(scrodID[bs], channel)) ncal[bs]++;
592 if (ncal[0] + ncal[1] + ncal[2] + ncal[3] == 0)
continue;
594 cout <<
"Slot " << moduleID << endl;
595 for (
int bs = 0; bs < 4; bs++) {
596 cout <<
" scrodID " << scrodID[bs] <<
": " << ncal[bs] <<
"/128" << endl;
604 void TOPDatabaseImporter::printSampleTimeCalibration()
609 B2ERROR(
"No time base calibration available");
613 for (
const auto& sampleTimes : timeBase->getSampleTimes()) {
614 cout << sampleTimes.getScrodID() <<
" " << sampleTimes.getChannel() << endl;
615 for (
const auto& time : sampleTimes.getTimeAxis()) {
624 void TOPDatabaseImporter::importChannelMask(std::string fileName,
625 int expNo,
int firstRun,
int lastRun)
632 TFile* file = TFile::Open(fileName.c_str(),
"r");
634 B2ERROR(
"openFile: " << fileName <<
" *** failed to open");
637 B2INFO(fileName <<
": open for reading");
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());
646 B2ERROR(
"Histogram with name '" + name +
"' not found");
649 for (
int channel = 0; channel < h->GetNbinsX(); channel++) {
650 int value = h->GetBinContent(channel + 1);
652 channelMask->setActive(moduleID, channel);
654 }
else if (value == 1) {
655 channelMask->setDead(moduleID, channel);
658 channelMask->setNoisy(moduleID, channel);
669 B2INFO(
"Channel mask for exp " << expNo <<
" run " << firstRun <<
" to " << lastRun
670 <<
" imported. Active channels: " << active <<
", dead: " << dead
671 <<
", noisy: " << noisy);
676 void TOPDatabaseImporter::generateFakeChannelMask(
double fractionDead,
678 int firstExp,
int firstRun,
679 int lastExp,
int lastRun)
686 auto& chMapper = TOPGeometryPar::Instance()->getChannelMapper();
687 const size_t nModules = TOPGeometryPar::Instance()->getGeometry()->getNumModules();
692 for (
size_t moduleID = 1; moduleID <= nModules; moduleID++) {
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);
702 if (gRandom->Rndm() < fractionDead) {
703 channelMask->setDead(moduleID, channel);
706 if (gRandom->Rndm() < fractionHot) {
707 channelMask->setNoisy(moduleID, channel);
720 B2RESULT(
"Generated and imported a fake channel mask to database for testing: "
721 << ncall <<
"/" << nall);
726 void TOPDatabaseImporter::importPmtQEData(
string fileName,
string treeName,
727 int firstExp,
int firstRun,
728 int lastExp,
int lastRun)
734 static const int nChann = 16;
735 std::string* serialNum = 0;
736 std::vector<float>* QE_data[nChann];
737 float lambdaFirst, lambdaStep, collEff0, collEff;
739 TBranch* bQE_data[nChann];
742 TFile* file = TFile::Open(fileName.c_str(),
"r");
744 B2ERROR(
"Cannot open the file " << fileName);
747 TTree* tQeData = (TTree*)file->Get(treeName.c_str());
749 B2ERROR(
"No TTree with name " << treeName <<
" in file " << fileName);
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);
760 for (
int ic = 0; ic < nChann; ic++) {
762 QE_data[ic] =
new std::vector<float>;
763 bQE_data[ic] =
new TBranch();
765 TString cString =
"QE_ch";
767 tQeData->SetBranchAddress(cString, &QE_data[ic], &bQE_data[ic]);
773 for (
int ient = 0; ient < tQeData->GetEntries(); ient++) {
775 tQeData->GetEntry(ient);
777 auto* pmtQE = pmtQEs.
appendNew(*serialNum, lambdaFirst, lambdaStep, collEff0, collEff);
779 for (
int ic = 0; ic < nChann; ic++) {
780 int tEntry = tQeData->LoadTree(ient);
781 bQE_data[ic]->GetEntry(tEntry);
783 pmtQE->setQE(ic + 1, *QE_data[ic]);
793 B2RESULT(
"PMT QE data imported to database for " << countPMTs <<
" PMT's.");
799 void TOPDatabaseImporter::importPmtGainData(
string fileName,
string treeName,
800 int firstExp,
int firstRun,
801 int lastExp,
int lastRun)
807 static const int nChann = 16;
808 std::string* serialNum = 0;
809 float gain_const[nChann], gain_slope[nChann], gain_ratio[nChann];
813 TFile* file = TFile::Open(fileName.c_str(),
"r");
815 B2ERROR(
"Cannot open the file " << fileName);
818 TTree* tGainData = (TTree*)file->Get(treeName.c_str());
820 B2ERROR(
"No TTree with name " << treeName <<
" in file " << fileName);
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);
836 for (
int ient = 0; ient < tGainData->GetEntries(); ient++) {
837 tGainData->GetEntry(ient);
838 auto* pmtGain = pmtGains.
appendNew(*serialNum);
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));
852 B2RESULT(
"PMT gain data imported to database for " << countPMTs <<
" PMT's.");
858 void TOPDatabaseImporter::importPmtInstallationData(
string fileName,
string treeName ,
859 int firstExp,
int firstRun,
860 int lastExp,
int lastRun)
866 std::string* serialNum = 0;
867 int moduleCNum, slotNum, arrayNum, PMTposition;
871 TFile* file = TFile::Open(fileName.c_str(),
"r");
873 B2ERROR(
"Cannot open the file " << fileName);
876 TTree* tInstData = (TTree*)file->Get(treeName.c_str());
878 B2ERROR(
"No TTree with name " << treeName <<
" in file " << fileName);
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);
893 for (
int ient = 0; ient < tInstData->GetEntries(); ient++) {
894 tInstData->GetEntry(ient);
895 pmtInst.
appendNew(*serialNum, moduleCNum, slotNum, arrayNum, PMTposition, type);
903 B2RESULT(
"PMT installation data imported to database for " << countPMTs <<
" PMT's.");
908 void TOPDatabaseImporter::importPmtObsoleteData(
string fileName,
string treeName,
909 int firstExp,
int firstRun,
910 int lastExp,
int lastRun)
916 std::string* serialNum = 0;
917 std::string* cathode = 0;
918 float hv_spec, dark_spec, qe380_spec;
922 TFile* file = TFile::Open(fileName.c_str(),
"r");
924 B2ERROR(
"Cannot open the file " << fileName);
927 TTree* tObsData = (TTree*)file->Get(treeName.c_str());
929 B2ERROR(
"No TTree with name " << treeName <<
" in file " << fileName);
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);
944 for (
int ient = 0; ient < tObsData->GetEntries(); ient++) {
945 tObsData->GetEntry(ient);
948 hv_spec = -fabs(hv_spec);
950 pmtObsData.
appendNew(*serialNum, type, *cathode, hv_spec, dark_spec, qe380_spec);
957 B2RESULT(
"PMT obsolete data imported to database for " << countPMTs <<
" PMT's.");
968 void TOPDatabaseImporter::importPmtTTSPar(
string fileName,
string treeName,
969 int firstExp,
int firstRun,
970 int lastExp,
int lastRun)
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];
982 TBranch* bGFrac[nChann];
983 TBranch* bGMean[nChann];
984 TBranch* bGSigma[nChann];
988 TFile* file = TFile::Open(fileName.c_str(),
"r");
990 B2ERROR(
"Cannot open the file " << fileName);
993 TTree* tTtsPar = (TTree*)file->Get(treeName.c_str());
995 B2ERROR(
"No TTree with name " << treeName <<
" in file " << fileName);
1000 tTtsPar->SetBranchAddress(
"serialNum", &serialNum);
1001 for (
int ic = 0; ic < nChann; ic++) {
1003 gausFrac[ic] =
new std::vector<float>;
1004 gausMean[ic] =
new std::vector<float>;
1005 gausSigma[ic] =
new std::vector<float>;
1007 bGFrac[ic] =
new TBranch();
1008 bGMean[ic] =
new TBranch();
1009 bGSigma[ic] =
new TBranch();
1012 TString cStringF =
"gausFrac_ch";
1013 TString cStringM =
"gausMean_ch";
1014 TString cStringS =
"gausSigma_ch";
1020 tTtsPar->SetBranchAddress(cStringF, &gausFrac[ic], &bGFrac[ic]);
1021 tTtsPar->SetBranchAddress(cStringM, &gausMean[ic], &bGMean[ic]);
1022 tTtsPar->SetBranchAddress(cStringS, &gausSigma[ic], &bGSigma[ic]);
1028 for (
int ient = 0; ient < tTtsPar->GetEntries(); ient++) {
1030 tTtsPar->GetEntry(ient);
1032 auto* pmtTtsPar = pmtTtsPars.
appendNew(*serialNum);
1034 for (
int ic = 0; ic < nChann; ic++) {
1036 int tEntry = tTtsPar->LoadTree(ient);
1037 bGFrac[ic]->GetEntry(tEntry);
1038 bGMean[ic]->GetEntry(tEntry);
1039 bGSigma[ic]->GetEntry(tEntry);
1042 if ((gausFrac[ic]->size() != gausMean[ic]->size()) ||
1043 (gausFrac[ic]->size() != gausSigma[ic]->size())) {
1045 B2ERROR(
"The TTSPar vectors for PMT " << serialNum <<
", channel " << ic + 1 <<
" have different sizes! Skipping channel...");
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));
1063 B2RESULT(
"PMT TTS parameters imported to database for " << countPMTs <<
" PMT's.");
1069 void TOPDatabaseImporter::importPmtTTSHisto(
string fileName,
1071 int firstExp,
int firstRun,
1072 int lastExp,
int lastRun)
1078 static const int nChann = 16;
1079 std::string* serialNum = 0;
1081 TH1F* histo[nChann] = {0};
1084 TFile* file = TFile::Open(fileName.c_str(),
"r");
1086 B2ERROR(
"Cannot open the file " << fileName);
1089 TTree* tTtsHisto = (TTree*)file->Get(treeName.c_str());
1091 B2ERROR(
"No TTree with name " << treeName <<
" in file " << fileName);
1096 tTtsHisto->SetBranchAddress(
"serialNum", &serialNum);
1097 tTtsHisto->SetBranchAddress(
"hv", &hv);
1098 for (
int ic = 0; ic < nChann; ic++) {
1099 TString hString =
"hist_ch";
1101 tTtsHisto->SetBranchAddress(hString, &histo[ic]);
1107 for (
int ient = 0; ient < tTtsHisto->GetEntries(); ient++) {
1109 tTtsHisto->GetEntry(ient);
1114 B2INFO(
"Saving TTS histograms for PMT " << *serialNum <<
", HV = " << hv);
1116 auto* pmtTtsHisto = pmtTtsHistos.
appendNew(*serialNum, hv);
1117 for (
int ic = 0; ic < nChann; ic++) {
1118 pmtTtsHisto->setHistogram(ic + 1, histo[ic]);
1125 pmtTtsHistos.
import(iov);
1127 B2RESULT(
"Imported " << countHists <<
" sets of TTS histograms from " << fileName <<
" file.");
1132 void TOPDatabaseImporter::importPmtPulseHeightFitResult(std::string fileName,
1133 int firstExp,
int firstRun,
1134 int lastExp,
int lastRun)
1142 TFile* file = TFile::Open(fileName.c_str());
1144 B2ERROR(
"openFile: " << fileName <<
" *** failed to open");
1147 TTree* tr = (TTree*)file->Get(
"tree");
1149 B2ERROR(
"No TTree with name tree found in " << fileName);
1159 float threshold = -1;
1160 float efficiency = -1;
1161 float chisquare = -1;
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);
1173 const auto& channelMapper = TOPGeometryPar::Instance()->getChannelMapper();
1174 if (!channelMapper.isValid()) {
1175 B2ERROR(
"No valid channel mapper found");
1180 long nEntries = tr->GetEntries();
1181 std::map<short, float> reducedChisqMap;
1182 for (
long iEntry = 0 ; iEntry < nEntries ; iEntry++) {
1183 tr->GetEntry(iEntry);
1185 if (efficiency < 0)
continue;
1187 if (!channelMapper.isPixelIDValid(pixelId)) {
1188 B2ERROR(
"invalid pixelID" << pixelId);
1191 auto channel = channelMapper.getChannel(pixelId);
1192 short globalChannelNumber = slotId * 1000 + channel;
1193 float redChisq = chisquare / ndf;
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);
1203 if (redChisq > 10.) {
1204 calChannelPulseHeight->setUnusable(slotId, channel);
1205 calChannelThresholdEff->setUnusable(slotId, channel);
1212 calChannelPulseHeight.
import(iov);
1213 calChannelThresholdEff.
import(iov);
1215 B2RESULT(
"Imported channel-by-channel gain and efficiency data from fitting of pulse height distribution for "
1216 << reducedChisqMap.size() <<
" channels from " << fileName <<
" file.");
1222 void TOPDatabaseImporter::exportPmtTTSHisto(
string outFileName)
1228 TFile file(outFileName.c_str(),
"recreate");
1231 for (
const auto& element : elements) {
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();
1245 void TOPDatabaseImporter::importFrontEndSettings(
int lookback,
int readoutWin,
1246 int extraWin,
int offset,
1247 int expNo,
int firstRun,
int lastRun)
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);
1259 feSetting->setWriteDepths(writeDepths);
1260 feSetting->setLookbackWindows(lookback);
1261 feSetting->setReadoutWindows(readoutWin);
1262 feSetting->setExtraWindows(extraWin);
1263 feSetting->setOffset(offset);
1266 std::vector<int> shifts = {0, 0, 1, 1, 1, 2};
1267 feSetting->setWindowShifts(shifts);
1272 B2INFO(
"Front-end settings imported for exp " << expNo <<
" run " << firstRun <<
1277 void TOPDatabaseImporter::importDummyCalModuleAlignment(
int firstExp,
int firstRun,
1278 int lastExp,
int lastRun)
1283 moduleAlignment.
import(iov);
1284 B2INFO(
"Dummy TOPCalModuleAlignment imported");
1288 void TOPDatabaseImporter::importDummyCalModuleT0(
int firstExp,
int firstRun,
1289 int lastExp,
int lastRun)
1295 B2INFO(
"Dummy TOPCalModuleT0 imported");
1299 void TOPDatabaseImporter::importDummyCalChannelT0(
int firstExp,
int firstRun,
1300 int lastExp,
int lastRun)
1306 B2INFO(
"Dummy TOPCalChannelT0 imported");
1310 void TOPDatabaseImporter::importDummyCalTimebase(
int firstExp,
int firstRun,
1311 int lastExp,
int lastRun)
1317 B2INFO(
"Dummy TOPCalTimebase imported");
1321 void TOPDatabaseImporter::importDummyCalChannelNoise(
int firstExp,
int firstRun,
1322 int lastExp,
int lastRun)
1327 channelNoise.
import(iov);
1328 B2INFO(
"Dummy TOPCalChannelNoise imported");
1332 void TOPDatabaseImporter::importDummyCalChannelPulseHeight(
int firstExp,
int firstRun,
1333 int lastExp,
int lastRun)
1339 B2INFO(
"Dummy TOPCalChannelPulseHeight imported");
1343 void TOPDatabaseImporter::importDummyCalChannelRQE(
int firstExp,
int firstRun,
1344 int lastExp,
int lastRun)
1350 B2INFO(
"Dummy TOPCalChannelRQE imported");
1354 void TOPDatabaseImporter::importDummyCalChannelThresholdEff(
int firstExp,
int firstRun,
1355 int lastExp,
int lastRun)
1360 channelThresholdEff.
import(iov);
1361 B2INFO(
"Dummy TOPCalChannelThresholdEff imported");
1365 void TOPDatabaseImporter::importDummyCalChannelThreshold(
int firstExp,
int firstRun,
1366 int lastExp,
int lastRun)
1371 channelThreshold.
import(iov);
1372 B2INFO(
"Dummy TOPCalChannelThreshold imported");
1376 void TOPDatabaseImporter::importDummyCalCommonT0(
int firstExp,
int firstRun,
1377 int lastExp,
int lastRun)
1383 B2INFO(
"Dummy TOPCalCommonT0 imported");
1387 void TOPDatabaseImporter::importDummyCalIntegratedCharge(
int firstExp,
int firstRun,
1388 int lastExp,
int lastRun)
1393 integratedCharge.
import(iov);
1394 B2INFO(
"Dummy TOPCalIntegratedCharge imported");
1398 void TOPDatabaseImporter::importDummyCalAsicShift(
int firstExp,
int firstRun,
1399 int lastExp,
int lastRun)
1405 B2INFO(
"Dummy TOPCalAsicShift imported");
1408 void TOPDatabaseImporter::correctTOPPmtQE()
1410 B2ERROR(
"Function disabled since the corrected payload TOPPmtQEs already imported");
1416 for (
const auto& pmt : pmtQEData) {
1417 auto* pmtCorr = pmtQECorrected.
appendNew(pmt.getSerialNumber(),
1418 pmt.getLambdaFirst(),
1419 pmt.getLambdaStep(),
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);
1428 double reflectance = pow((n - 1) / (n + 1), 2);
1429 qe /= (1 - reflectance);
1432 pmtCorr->setQE(pmtPixel, qeData);
1437 pmtQECorrected.
import(iov);
1439 B2RESULT(
"Corrected PMT QE data imported to database for "
1440 << pmtQECorrected.
getEntries() <<
" PMT's.");
1445 void TOPDatabaseImporter::importTimeWalk(PyObject* list,
double a,
double b,
1446 int firstExp,
int firstRun,
1447 int lastExp,
int lastRun)
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());
1458 B2ERROR(
"Input Python object is not a list");
1464 timeWalk->set(params, a, b);
1469 B2RESULT(
"Time-walk constants imported");
1476 void TOPDatabaseImporter::importTest(
int runNumber,
double syncTimeBase)
1480 vector<double> timeAxis;
1481 for (
int i = 0; i < 256; i++) {
1482 timeAxis.push_back(syncTimeBase / 128.0 * i);
1487 for (
unsigned scrodID = 0; scrodID < 64; scrodID++) {
1488 for (
unsigned channel = 0; channel < 128; channel++) {
1489 timeBase->append(scrodID, channel, timeAxis);
1493 if (runNumber == 3) {
1496 for (
unsigned scrodID = 0; scrodID < 64; scrodID++) {
1497 for (
unsigned channel = 0; channel < 128; channel++) {
1498 timeBase->append(scrodID, channel, timeAxis);
1509 void TOPDatabaseImporter::importTest()
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);
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);
1526 for (
const auto& gain : pmtGains) gain.print();