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