11 #include <framework/core/HistoModule.h>
12 #include <top/modules/TOPTBCComparator/TOPTBCComparatorModule.h>
13 #include <top/geometry/TOPGeometryPar.h>
14 #include <top/geometry/FrontEndMapper.h>
15 #include "TDirectory.h"
16 #include "TSystemDirectory.h"
17 #include "TSystemFile.h"
20 #include <boost/format.hpp>
36 setDescription(
"TOP Time Base Correction monitor module ");
38 addParam(
"inputDirectorList", m_inputDirectoryList,
39 "List of the directories (one per IOV) in which the files with the calibration constants of the SCODS are stored. The format of each line must be: folderpath/ label ",
41 addParam(
"compareToPreviousSet", m_compareToPreviousSet,
42 "If true, each CalSet is compared to the previous one in the order determined by the inputDirectorList file. If false, the reference CalSet is the first of the list",
44 addParam(
"outputFile", m_outputFile,
45 "output file containing the monitoring plots",
string(
"TBCComparisonResults.root"));
46 addParam(
"minCalPulses", m_minCalPulses,
47 "minimum number of calibration pulses need to flag a sample as non-empty",
short(200));
48 addParam(
"numSamples", m_numSamples,
49 "number of samples that have been calibrated",
short(256));
54 void TOPTBCComparatorModule::defineHisto()
57 ifstream inputDirectoryListFile(m_inputDirectoryList.c_str());
60 if (!inputDirectoryListFile) {
61 B2ERROR(
"Unable to open the input file with the list of CalSets to analyze");
65 B2INFO(
"Initializing histograms");
68 std::string inputString;
72 while (std::getline(inputDirectoryListFile, inputString)) {
75 parseInputDirectoryLine(inputString);
77 B2INFO(
"Initializing histograms for Calibration set located at " << m_calSetDirectory <<
" with label " << m_calSetLabel);
90 name = str(format(
"TOPAverageDeltaT_CalSet_%1%") % m_calSetLabel);
91 title = str(format(
"Average value of #Delta T VS global channel number. CalSet %1%") % m_calSetLabel);
92 m_topAverageDeltaT.push_back(
new TH1F(name.c_str(), title.c_str(), 512 * 16, 0., 512 * 16.));
95 name = str(format(
"TOPSigmaDeltaT_CalSet_%1%") % m_calSetLabel);
96 title = str(format(
"Sigma value of #Delta T VS global channel number. CalSet %1%") % m_calSetLabel);
97 m_topSigmaDeltaT.push_back(
new TH1F(name.c_str(), title.c_str(), 512 * 16, 0., 512 * 16.));
100 name = str(format(
"TOPSampleOccupancy_CalSet_%1%") % m_calSetLabel);
101 title = str(format(
"Average number of calpulses per sample VS global channel number. CalSet %1%") % m_calSetLabel);
102 m_topSampleOccupancy.push_back(
new TH1F(name.c_str(), title.c_str(), 512 * 16, 0., 512 * 16.));
106 name = str(format(
"TOPAverageDeltaTComparison_CalSet_%1%") % m_calSetLabel);
107 title = str(format(
"Ratio of the average #Delta T in CalSet %1% over the previous one, over the whole detector") % m_calSetLabel);
108 m_topAverageDeltaTComparison.push_back(
new TH1F(name.c_str(), title.c_str(), 512 * 16, 0., 512 * 16.));
111 name = str(format(
"TOPSigmaDeltaTComparison_CalSet_%1%") % m_calSetLabel);
112 title = str(format(
"Ratio of the st. dev. of #Delta T in CalSet %1% over the previous one, , over the whole detector") %
114 m_topSigmaDeltaTComparison.push_back(
new TH1F(name.c_str(), title.c_str(), 512 * 16, 0., 512 * 16.));
117 name = str(format(
"TOPSampleOccupancyComparison_CalSet_%1%") % m_calSetLabel);
119 format(
"Ratio of the average number of calpulses per sample in CalSet %1% over the previous one, over the whole detector") %
121 m_topSampleOccupancyComparison.push_back(
new TH1F(name.c_str(), title.c_str(), 512 * 16, 0., 512 * 16.));
128 for (
int iSlot = 0; iSlot < 16; iSlot ++) {
131 name = str(format(
"SlotAverageDeltaT_Slot%1%_CalSet_%2%") % (iSlot) % m_calSetLabel);
132 title = str(format(
"Average value of #Delta T VS Channel number. Slot %1%, CalSet %2%") % (iSlot) % m_calSetLabel);
133 m_slotAverageDeltaT[iSlot].push_back(
new TH1F(name.c_str(), title.c_str(), 512, 0., 512.));
135 name = str(format(
"SlotSigmaDeltaT_Slot%1%_CalSet_%2%") % (iSlot) % m_calSetLabel);
136 title = str(format(
"Standard deviation of #Delta T VS Channel number. Slot %1%, CalSet %2%") % (iSlot) % m_calSetLabel);
137 m_slotSigmaDeltaT[iSlot].push_back(
new TH1F(name.c_str(), title.c_str(), 512, 0., 512.));
139 name = str(format(
"SlotAverageDeltaTMap_Slot%1%_CalSet_%2%") % (iSlot) % m_calSetLabel);
140 title = str(format(
"Map of the average value of #Delta T on the 256 samples. Slot %1%, CalSet %2%") % (iSlot) % m_calSetLabel);
141 m_slotAverageDeltaTMap[iSlot].push_back(
new TH2F(name.c_str(), title.c_str(), 64, 0., 64., 8, 0., 8.));
143 name = str(format(
"SlotSigmaDeltaTMap_Slot%1%_CalSet_%2%") % (iSlot) % m_calSetLabel);
144 title = str(format(
"Map of the RMS of #Delta T on the 256 samples . Slot %1%, CalSet %2%") % (iSlot) % m_calSetLabel);
145 m_slotSigmaDeltaTMap[iSlot].push_back(
new TH2F(name.c_str(), title.c_str(), 64, 0., 64., 8, 0., 8.));
149 name = str(format(
"SlotSampleOccupancy_Slot%1%_CalSet_%2%") % (iSlot) % m_calSetLabel);
150 title = str(format(
"Average occupancy per sample. Slot %1%, CalSet %2%") % (iSlot) % m_calSetLabel);
151 m_slotSampleOccupancy[iSlot].push_back(
new TH1F(name.c_str(), title.c_str(), 512, 0., 512.));
153 name = str(format(
"SlotEmptySamples_Slot%1%_CalSet_%2%") % (iSlot) % m_calSetLabel);
154 title = str(format(
"Number samples with less than %3% calpulses per each slot channel. Slot %1%, CalSet %2%") %
155 (iSlot) % m_calSetLabel % m_minCalPulses);
156 m_slotEmptySamples[iSlot].push_back(
new TH1F(name.c_str(), title.c_str(), 512, 0., 512.));
158 name = str(format(
"SlotSampleOccupancyMap_Slot%1%_CalSet_%2%") % (iSlot) % m_calSetLabel);
159 title = str(format(
"Map of the average occupancy per sample. Slot %1%, CalSet %2%") %
160 (iSlot) % m_calSetLabel);
161 m_slotSampleOccupancyMap[iSlot].push_back(
new TH2F(name.c_str(), title.c_str(), 64, 0., 64., 8, 0., 8.));
163 name = str(format(
"SlotEmptySamplesMap_Slot%1%_CalSet_%2%") % (iSlot) % m_calSetLabel);
164 title = str(format(
"Map of the number samples with less than %3% calpulses per each. Slot %1%, CalSet %2%") %
165 (iSlot) % m_calSetLabel % m_minCalPulses);
166 m_slotEmptySamplesMap[iSlot].push_back(
new TH2F(name.c_str(), title.c_str(), 64, 0., 64., 8, 0., 8.));
171 name = str(format(
"SlotAverageDeltaTComparison_Slot%1%_CalSet_%2%") % (iSlot) % m_calSetLabel);
172 title = str(format(
"Ratio of the average #Delta T in CalSet %2% over the previous one. Slot %1%") % (iSlot) % m_calSetLabel);
173 m_slotAverageDeltaTComparison[iSlot].push_back(
new TH1F(name.c_str(), title.c_str(), 512, 0., 512.));
175 name = str(format(
"SlotRMSDeltaTComparison_Slot%1%_CalSet_%2%") % (iSlot) % m_calSetLabel);
176 title = str(format(
"Ratio of the RMS of #Delta T in CalSet %2% over the previous one. Slot %1%") % (iSlot) % m_calSetLabel);
177 m_slotSigmaDeltaTComparison[iSlot].push_back(
new TH1F(name.c_str(), title.c_str(), 512, 0., 512.));
179 name = str(format(
"SlotAverageDeltaTMapComparison_Slot%1%_CalSet_%2%") % (iSlot) % m_calSetLabel);
180 title = str(format(
"Map of the ratio of the average #Delta T in CalSet %2% over the previous one. Slot %1%") %
181 (iSlot) % m_calSetLabel);
182 m_slotAverageDeltaTMapComparison[iSlot].push_back(
new TH2F(name.c_str(), title.c_str(), 64, 0., 64., 8, 0., 8.));
184 name = str(format(
"SlotRMSDeltaTMapComparison_Slot%1%_CalSet_%2%") % (iSlot) % m_calSetLabel);
185 title = str(format(
"Map of the ratio of the RMS of #Delta T in CalSet %2% over the previous one. Slot %1%") %
186 (iSlot) % m_calSetLabel);
187 m_slotSigmaDeltaTMapComparison[iSlot].push_back(
new TH2F(name.c_str(), title.c_str(), 64, 0., 64., 8, 0., 8.));
194 B2INFO(
"Initialization of the histograms for " << m_totCalSets <<
" CalSets done.");
200 int TOPTBCComparatorModule::analyzeCalFile()
206 if (m_slotID < 0 || m_boardstackID < 0 || m_scrodID < 0) {
207 B2WARNING(
"Negative slot, BS or scrod ID found while calling analyzeCalFile(). Looks like they have not been initialized, or that a function re-initialized them");
212 for (
short iChannel = 0; iChannel < 128; iChannel++) {
215 if (!m_calSetFile->Get(str(format(
"timeDiff_ch%1%") % (iChannel)).c_str())) {
225 short hardwareChannel = iChannel + 128 * m_boardstackID;
226 auto& chMapper = TOP::TOPGeometryPar::Instance()->getChannelMapper();
227 short pixelID = chMapper.getPixelID(hardwareChannel);
228 short colNum = (pixelID - 1) % 64 + 1;
229 short rowNum = (pixelID - 1) / 64 + 1 ;
230 short globalChannel = hardwareChannel + 512 * (m_slotID -
239 if (!m_calSetFile->Get(str(format(
"timeDiffcal_ch%1%") % (iChannel)).c_str())) {
240 B2WARNING(
"Error opening " << str(format(
"timeDiffcal_ch%1%") % (iChannel)));
242 TH2F* h_timeDiffcal = (TH2F*)m_calSetFile->Get(str(format(
"timeDiffcal_ch%1%") % (iChannel)).c_str());
243 TH1D* h_projection = h_timeDiffcal->ProjectionY(
"h_projection", 1, m_numSamples);
245 m_slotAverageDeltaT[m_slotID - 1][m_calSetID]->SetBinContent(hardwareChannel + 1, h_projection->GetMean());
246 m_slotAverageDeltaT[m_slotID - 1][m_calSetID]->SetBinError(hardwareChannel + 1,
247 h_projection->GetMeanError());
248 m_slotSigmaDeltaT[m_slotID - 1][m_calSetID]->SetBinContent(hardwareChannel + 1,
249 h_projection->GetRMS());
250 m_slotSigmaDeltaT[m_slotID - 1][m_calSetID]->SetBinError(hardwareChannel + 1,
251 h_projection->GetRMSError());
253 m_slotAverageDeltaTMap[m_slotID - 1][m_calSetID]->SetBinContent(colNum, rowNum, h_projection->GetMean());
254 m_slotSigmaDeltaTMap[m_slotID - 1][m_calSetID]->SetBinContent(colNum, rowNum,
255 h_projection->GetRMS());
257 m_topAverageDeltaT[m_calSetID]->SetBinContent(globalChannel + 1, h_projection->GetMean());
258 m_topSigmaDeltaT[m_calSetID]->SetBinContent(globalChannel + 1, h_projection->GetRMS());
266 if (!m_calSetFile->Get(str(format(
"sampleOccup_ch%1%") % (iChannel)).c_str())) {
267 B2WARNING(
"Error opening " << str(format(
"sampleOccup_ch%1%") % (iChannel)));
269 TH1F* h_sampleOccup = (TH1F*)m_calSetFile->Get(str(format(
"sampleOccup_ch%1%") % (iChannel)).c_str());
273 for (
int iSample = 1; iSample < m_numSamples + 1 ; iSample++) {
274 if (h_sampleOccup->GetBinContent(iSample) < m_minCalPulses) nEmpty++;
277 m_slotSampleOccupancy[m_slotID - 1][m_calSetID]->SetBinContent(hardwareChannel + 1, h_sampleOccup->Integral() / m_numSamples);
278 m_slotEmptySamples[m_slotID - 1][m_calSetID]->SetBinContent(hardwareChannel + 1, nEmpty);
280 m_slotSampleOccupancyMap[m_slotID - 1][m_calSetID]->SetBinContent(colNum, rowNum, h_sampleOccup->Integral() / m_numSamples);
281 m_slotEmptySamplesMap[m_slotID - 1][m_calSetID]->SetBinContent(colNum, rowNum, nEmpty);
283 m_topSampleOccupancy[m_calSetID]->SetBinContent(globalChannel + 1, h_sampleOccup->Integral() / m_numSamples);
294 int TOPTBCComparatorModule::makeComparisons()
298 B2INFO(
"Making comparisons for " << m_totCalSets <<
" sets.");
301 for (
int iSet = 1; iSet < m_totCalSets; iSet++) {
302 if (m_compareToPreviousSet) refSet = iSet - 1;
304 m_topAverageDeltaTComparison[iSet] = calculateHistoRatio(m_topAverageDeltaTComparison[iSet], m_topAverageDeltaT[iSet],
305 m_topAverageDeltaT[refSet]);
306 m_topSigmaDeltaTComparison[iSet] = calculateHistoRatio(m_topSigmaDeltaTComparison[iSet], m_topSigmaDeltaT[iSet],
307 m_topSigmaDeltaT[refSet]);
308 m_topSampleOccupancyComparison[iSet] = calculateHistoRatio(m_topSampleOccupancyComparison[iSet], m_topSampleOccupancy[iSet],
309 m_topSampleOccupancy[refSet]);
312 for (
int iSlot = 0; iSlot < 16; iSlot++) {
313 m_slotAverageDeltaTComparison[iSlot][iSet] = calculateHistoRatio(m_slotAverageDeltaTComparison[iSlot][iSet],
314 m_slotAverageDeltaT[iSlot][iSet], m_slotAverageDeltaT[iSlot][refSet]);
315 m_slotAverageDeltaTMapComparison[iSlot][iSet] = calculateHistoRatio(m_slotAverageDeltaTMapComparison[iSlot][iSet],
316 m_slotAverageDeltaTMap[iSlot][iSet], m_slotAverageDeltaTMap[iSlot][refSet]);
317 m_slotSigmaDeltaTComparison[iSlot][iSet] = calculateHistoRatio(m_slotSigmaDeltaTComparison[iSlot][iSet],
318 m_slotSigmaDeltaT[iSlot][iSet], m_slotSigmaDeltaT[iSlot][refSet]);
319 m_slotSigmaDeltaTMapComparison[iSlot][iSet] = calculateHistoRatio(m_slotSigmaDeltaTMapComparison[iSlot][iSet],
320 m_slotSigmaDeltaTMap[iSlot][iSet], m_slotSigmaDeltaTMap[iSlot][refSet]);
323 B2INFO(
"Comparisons done");
329 void TOPTBCComparatorModule::initialize()
334 void TOPTBCComparatorModule::beginRun()
340 void TOPTBCComparatorModule::event()
349 void TOPTBCComparatorModule::endRun()
352 ifstream inputDirectoryListFile(m_inputDirectoryList.c_str());
355 if (!inputDirectoryListFile) {
356 B2ERROR(
"Unable to open the input file with the list of CalSets to analyze");
360 std::string inputString;
362 while (std::getline(inputDirectoryListFile, inputString)) {
365 parseInputDirectoryLine(inputString);
367 B2INFO(
"Processing the calibration set located in " << m_calSetDirectory <<
" and labelled " << m_calSetLabel);
368 TSystemDirectory calSetDir(m_calSetDirectory.c_str(), (m_calSetDirectory).c_str());
371 TList* calSetDirContent = calSetDir.GetListOfFiles();
372 if (calSetDirContent) {
374 TIter next(calSetDirContent);
375 while ((entry = (TSystemFile*)next())) {
378 std::string entryName = entry->GetName();
381 if (!entry->IsDirectory() && TString(entryName.c_str()).EndsWith(
".root")) {
383 m_calSetFile =
new TFile((m_calSetDirectory + entryName).c_str(),
" ");
385 int parserStatus = parseSlotAndScrodIDfromFileName(entryName);
387 if (parserStatus == 1)
390 m_calSetFile->Close();
397 if (entry->IsDirectory() && entryName !=
"." && entryName !=
"..") {
400 TSystemDirectory calSetSubDir((m_calSetDirectory + entryName).c_str(), (m_calSetDirectory + entryName).c_str());
403 TList* calSetSubDirContent = calSetSubDir.GetListOfFiles();
404 if (calSetSubDirContent) {
406 TIter nextSub(calSetSubDirContent);
407 while ((file = (TSystemFile*)nextSub())) {
409 std::string fileName = file->GetName();
411 if (!file->IsDirectory() && TString(fileName.c_str()).EndsWith(
".root")) {
413 m_calSetFile =
new TFile((m_calSetDirectory + entryName + fileName).c_str(),
" ");
415 int parserStatus = parseSlotAndScrodIDfromFileName(fileName);
417 if (parserStatus == 1)
420 m_calSetFile->Close();
421 }
else if (file->IsDirectory() && fileName !=
"." && fileName !=
"..") {
422 B2WARNING(
"Additional subdirectory " << fileName <<
" found in " << m_calSetDirectory + entryName <<
423 ", where only .root and .log files are expected ");
432 B2WARNING(
"Error in creating the TList form the directory " << m_calSetDirectory);
437 B2INFO(
"Analisys concluded.");
446 void TOPTBCComparatorModule::terminate()
449 B2INFO(
"Creating output file " << m_outputFile);
450 TFile outfile(m_outputFile.c_str(),
"recreate");
452 B2INFO(
"Writing histograms ");
455 ifstream inputDirectoryListFile(m_inputDirectoryList.c_str());
456 std::string inputString;
459 while (std::getline(inputDirectoryListFile, inputString)) {
460 parseInputDirectoryLine(inputString);
461 TDirectory* dirSet = outfile.mkdir(m_calSetLabel.c_str());
464 m_topAverageDeltaT[iSet]->Write();
465 m_topSigmaDeltaT[iSet]->Write();
466 m_topSampleOccupancy[iSet]->Write();
467 m_topAverageDeltaTComparison[iSet]->Write();
468 m_topSigmaDeltaTComparison[iSet]->Write();
469 m_topSampleOccupancyComparison[iSet]->Write();
471 for (
int iSlot = 0; iSlot < 16; iSlot ++) {
472 TDirectory* dirSlot = dirSet->mkdir(str(format(
"slot%1%") % (iSlot + 1)).c_str());
475 m_slotAverageDeltaT[iSlot][iSet]->Write();
476 m_slotSigmaDeltaT[iSlot][iSet]->Write();
477 m_slotAverageDeltaTMap[iSlot][iSet]->Write();
478 m_slotSigmaDeltaTMap[iSlot][iSet]->Write();
479 m_slotSampleOccupancy[iSlot][iSet]->Write();
480 m_slotEmptySamples[iSlot][iSet]->Write();
481 m_slotSampleOccupancyMap[iSlot][iSet]->Write();
482 m_slotEmptySamplesMap[iSlot][iSet]->Write();
484 m_slotAverageDeltaTComparison[iSlot][iSet]->Write();
485 m_slotSigmaDeltaTComparison[iSlot][iSet]->Write();
486 m_slotAverageDeltaTMapComparison[iSlot][iSet]->Write();
487 m_slotSigmaDeltaTMapComparison[iSlot][iSet]->Write();
502 int TOPTBCComparatorModule::parseInputDirectoryLine(std::string inputString)
505 m_calSetDirectory.clear();
506 m_calSetLabel.clear();
509 bool isDirectoryNameChar =
true;
510 bool isAtBeginning =
true;
512 for (std::string::size_type i = 0; i < inputString.size(); ++i) {
513 char c = inputString[i];
515 if (c ==
' ' && isAtBeginning)
continue;
516 if (c ==
' ' && !isAtBeginning) {
517 isDirectoryNameChar =
false;
518 isAtBeginning =
false;
521 if (c !=
' ' && isDirectoryNameChar) {
522 m_calSetDirectory += c;
523 isAtBeginning =
false;
526 if (c !=
' ' && !isDirectoryNameChar) {
528 isAtBeginning =
false;
531 B2WARNING(
"Uncaught exception in parsing the input string. Ending the parsing.");
540 int TOPTBCComparatorModule::parseSlotAndScrodIDfromFileName(std::string inputString)
550 std::string stringSlot =
"";
551 std::string stringBS =
"";
552 std::string stringScrod =
"";
562 if (!(inputString[0] ==
't' && inputString[1] ==
'b' && inputString[2] ==
'c')) {
563 B2WARNING(inputString <<
" is not a valid TBC file. Skipping it.");
566 stringSlot += inputString[7];
567 stringSlot += inputString[8];
568 stringBS += inputString[10];
569 stringScrod += inputString[17];
570 stringScrod += inputString[18];
571 if (inputString[19] !=
'.')
572 stringScrod += inputString[19];
575 m_slotID = std::stoi(stringSlot);
576 m_scrodID = std::stoi(stringScrod);
577 m_boardstackID = std::stoi(stringBS);
584 TH1F* TOPTBCComparatorModule::calculateHistoRatio(TH1F* hRatio, TH1F* hNum, TH1F* hDen)
587 const char* name = hRatio->GetName();
588 const char* title = hRatio->GetTitle();
590 const char* xAxisTitle = hRatio->GetXaxis()->GetTitle();
591 const char* yAxisTitle = hRatio->GetYaxis()->GetTitle();
594 hRatio = (TH1F*)hNum->Clone();
598 hRatio->Divide(hDen);
601 hRatio->SetName(name);
602 hRatio->SetTitle(title);
603 hRatio->GetXaxis()->SetTitle(xAxisTitle);
604 hRatio->GetYaxis()->SetTitle(yAxisTitle);
608 TH2F* TOPTBCComparatorModule::calculateHistoRatio(TH2F* hRatio, TH2F* hNum, TH2F* hDen)
611 const char* name = hRatio->GetName();
612 const char* title = hRatio->GetTitle();
614 const char* xAxisTitle = hRatio->GetXaxis()->GetTitle();
615 const char* yAxisTitle = hRatio->GetYaxis()->GetTitle();
618 hRatio = (TH2F*)hNum->Clone();
622 hRatio->Divide(hDen);
625 hRatio->SetName(name);
626 hRatio->SetTitle(title);
627 hRatio->GetXaxis()->SetTitle(xAxisTitle);
628 hRatio->GetYaxis()->SetTitle(yAxisTitle);