Belle II Software  release-06-00-14
b2klm-execute-masking.cc
1 /**************************************************************************
2  * basf2 (Belle II Analysis Software Framework) *
3  * Author: The Belle II Collaboration *
4  * *
5  * See git log for contributors and copyright holders. *
6  * This file is licensed under LGPL-3.0, see LICENSE.md. *
7  **************************************************************************/
8 
9 /* KLM headers. */
10 #include <klm/dataobjects/KLMChannelArrayIndex.h>
11 #include <klm/dataobjects/KLMChannelIndex.h>
12 #include <klm/dataobjects/KLMElementNumbers.h>
13 #include <klm/dataobjects/KLMSectorArrayIndex.h>
14 
15 /* Belle 2 headers. */
16 #include <framework/logging/Logger.h>
17 
18 /* ROOT headers. */
19 #include <TFile.h>
20 #include <TH1.h>
21 #include <TSystem.h>
22 
23 /* C++ headers. */
24 #include <cstdlib>
25 #include <iostream>
26 #include <string>
27 
28 using namespace Belle2;
29 
30 int main(int argc, char* argv[])
31 {
32  /* Print the usage message if --help or -h are used. */
33  if (argc == 1 or std::string(argv[1]) == "--help" or std::string(argv[1]) == "-h") {
34  std::cout << "Usage: " << argv[0] << " [INPUT_FILE] [CHANNEL1] [CHANNEL2] ... [CHANNELN]\n\n"
35  " This tool masks the given channels of the KLM DQM reference plots stored\n"
36  " in the given input file (here 'channel' means 'channel number').\n\n"
37  " The plots on which this tool acts are:\n"
38  " KLM/masked_channels;\n"
39  " KLM/strip_hits_subdetector_<X>_section_<Y>_sector_<W>_<Z>.\n\n"
40  " This tool is not intended to be run standalone, since it is executed\n"
41  " by 'b2klm-mask-dqm', which automatically detects the channels to be masked.\n";
42  return 0;
43  }
44  /* Print error messages when needed. */
45  int nChannels = argc - 2;
46  if (nChannels == 0) {
47  B2ERROR("There are no channels to mask!");
48  return 0;
49  }
50  std::string inputFileName(argv[1]);
51  if (inputFileName.find(".root") == std::string::npos) {
52  B2ERROR("The input file is not a .root file!");
53  return 0;
54  }
55  if (gSystem->AccessPathName(inputFileName.c_str())) {
56  B2ERROR("The input file does not exist!");
57  return 0;
58  }
59  TFile* inputFile = new TFile(inputFileName.c_str(), "UPDATE");
60  if (!inputFile or inputFile->IsZombie()) {
61  B2ERROR("The input file is not working!");
62  return 0;
63  }
64  /* Now we can safely execute the masking. */
65  inputFile->cd();
66  const KLMElementNumbers* elementNumbers = &(KLMElementNumbers::Instance());
67  const KLMChannelArrayIndex* channelArrayIndex = &(KLMChannelArrayIndex::Instance());
68  const KLMSectorArrayIndex* sectorArrayIndex = &(KLMSectorArrayIndex::Instance());
69  TH1* histoSummary = (TH1*)inputFile->Get("KLM/masked_channels");
70  if (!histoSummary) {
71  B2ERROR("The histogram KLM/masked_channels is not found!");
72  return 0;
73  }
74  for (int i = 2; i <= nChannels + 1; ++i) {
75  KLMChannelNumber channelNumber = std::atoi(argv[i]);
76  int subdetector, section, sector, layer, plane, strip;
77  elementNumbers->channelNumberToElementNumbers(
78  channelNumber, &subdetector, &section, &sector, &layer, &plane, &strip);
79  /* First: mask the channel in occupancy plot. */
80  KLMChannelNumber channelIndex = channelArrayIndex->getIndex(channelNumber);
81  int nHistoOccupancy;
82  if (subdetector == KLMElementNumbers::c_BKLM)
83  nHistoOccupancy = 2;
84  else
85  nHistoOccupancy = 3;
86  for (int j = 0; j < nHistoOccupancy; ++j) {
87  std::string histoOccupancyName = "KLM/strip_hits_subdetector_" + std::to_string(subdetector) +
88  "_section_" + std::to_string(section) +
89  "_sector_" + std::to_string(sector) +
90  "_" + std::to_string(j);
91  TH1* histoOccupancy = (TH1*)inputFile->Get(histoOccupancyName.c_str());
92  if (!histoOccupancy) {
93  B2ERROR("The histogram " << histoOccupancyName << " is not found!");
94  return 0;
95  }
96  TAxis* xAxis = histoOccupancy->GetXaxis();
97  double xMin = xAxis->GetXmin();
98  double xMax = xAxis->GetXmax();
99  if ((channelIndex >= xMin) and (channelIndex < xMax)) {
100  int bin = xAxis->FindBin(channelIndex);
101  histoOccupancy->SetBinContent(bin, 0);
102  inputFile->Write("", TObject::kOverwrite);
103  }
104  }
105  /* Second: add the masked channel to the summary plot. */
106  KLMSectorNumber sectorNumber;
107  if (subdetector == KLMElementNumbers::c_BKLM)
108  sectorNumber = elementNumbers->sectorNumberBKLM(section, sector);
109  else
110  sectorNumber = elementNumbers->sectorNumberEKLM(section, sector);
111  uint16_t sectorIndex = sectorArrayIndex->getIndex(sectorNumber);
112  histoSummary->Fill(sectorIndex);
113  }
114  inputFile->Write("", TObject::kOverwrite);
115  inputFile->Close();
116  delete inputFile;
117  B2INFO("Masking complete: the reference file " << inputFileName << " is now ready.");
118  return 0;
119 }
KLM channel array index.
static const KLMChannelArrayIndex & Instance()
Instantiation.
uint16_t getIndex(uint16_t number) const
Get element index.
KLM element numbers.
KLMSectorNumber sectorNumberEKLM(int section, int sector) const
Get sector number for EKLM.
static const KLMElementNumbers & Instance()
Instantiation.
void channelNumberToElementNumbers(KLMChannelNumber channel, int *subdetector, int *section, int *sector, int *layer, int *plane, int *strip) const
Get element numbers by channel number.
KLMSectorNumber sectorNumberBKLM(int section, int sector) const
Get sector number for BKLM.
KLM sector array index.
static const KLMSectorArrayIndex & Instance()
Instantiation.
uint16_t KLMSectorNumber
Sector number.
uint16_t KLMChannelNumber
Channel number.
Abstract base class for different kinds of events.
int main(int argc, char **argv)
Run all tests.
Definition: test_main.cc:75