Belle II Software  release-05-02-19
TOPChannelMaskAlgorithm.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2021 - Belle II Collaboration *
4  * *
5  * *
6  * Author: The Belle II Collaboration *
7  * Contributors: Marko Staric *
8  * *
9  * This software is provided "as is" without any warranty. *
10  **************************************************************************/
11 
12 #include <top/calibration/TOPChannelMaskAlgorithm.h>
13 #include <framework/logging/Logger.h>
14 #include "TROOT.h"
15 #include <TH1F.h>
16 #include <TH2F.h>
17 #include <string>
18 #include <vector>
19 
20 using namespace std;
21 
22 namespace Belle2 {
27  namespace TOP {
28 
29  TOPChannelMaskAlgorithm::TOPChannelMaskAlgorithm():
30  CalibrationAlgorithm("TOPChannelMaskCollector")
31  {
32  setDescription("Calibration algorithm for masking of dead and hot channels");
33  }
34 
36  {
37  gROOT->SetBatch();
38 
39  // check if the statistics is sufficient
40 
41  auto nhits = getObjectPtr<TH1F>("nhits");
42  if (not nhits) return c_NotEnoughData;
43  double averageChannelHits = nhits->GetEntries() * nhits->GetMean() / 16 / 512;
44  B2INFO("Average number of good hits per channel: " << averageChannelHits);
45  if (averageChannelHits < m_minHits) return c_NotEnoughData;
46 
47  // construct file name and open output root file
48 
49  const auto& expRun = getRunList();
50  string expNo = to_string(expRun[0].first);
51  while (expNo.length() < 4) expNo.insert(0, "0");
52  string runNo = to_string(expRun[0].second);
53  while (runNo.length() < 5) runNo.insert(0, "0");
54  string outputFileName = "channelMask-e" + expNo + "-r" + runNo + ".root";
55  m_file = TFile::Open(outputFileName.c_str(), "recreate");
56 
57  nhits->Write();
58 
59  // create payload
60 
62 
63  // dead and hot channels
64 
65  auto meanHits = new TH1F("meanHits", "Average number of hits per channel; slot number; average", 16, 0.5, 16.5);
66  for (int slot = 1; slot <= 16; slot++) {
67  string name = "hits_" + to_string(slot);
68  auto h = getObjectPtr<TH1F>(name);
69  if (not h) continue;
70  h->Write();
71  double mean = 0;
72  int n = 0;
73  for (int chan = 0; chan < h->GetNbinsX(); chan++) {
74  double y = h->GetBinContent(chan + 1);
75  if (y > 0) {
76  mean += y;
77  n++;
78  }
79  }
80  if (n > 0) mean /= n;
81  meanHits->SetBinContent(slot, mean);
82  double deadCut = mean / 10;
83  double hotCut = mean * 10;
84  for (int chan = 0; chan < h->GetNbinsX(); chan++) {
85  double y = h->GetBinContent(chan + 1);
86  if (y <= deadCut) {
87  m_channelMask->setDead(slot, chan);
88  } else if (y > hotCut) {
89  m_channelMask->setNoisy(slot, chan);
90  }
91  }
92  }
93 
94  // ASICs with window corruption
95 
96  for (int slot = 1; slot <= 16; slot++) {
97  string name = "window_vs_asic_" + to_string(slot);
98  auto h = getObjectPtr<TH2F>(name);
99  if (not h) continue;
100  h->Write();
101  auto h0 = h->ProjectionX();
102  auto h1 = h->ProjectionX("_tmp", m_minWindow, m_maxWindow);
103  for (int asic = 0; asic < h->GetNbinsX(); asic++) {
104  double r = 1 - h1->GetBinContent(asic + 1) / h0->GetBinContent(asic + 1);
105  if (r > 0.20) {
106  for (int chan = 0; chan < 8; chan++) m_channelMask->setNoisy(slot, chan + asic * 8);
107  }
108  }
109  delete h0;
110  delete h1;
111  }
112 
113  // write the results and close the file
114 
115  auto dead = new TH1F("numDead", "Number of dead channels; slot number; dead channels", 16, 0.5, 16.5);
116  auto hot = new TH1F("numHot", "Number of noisy channels; slot number; noisy channels", 16, 0.5, 16.5);
117  auto active = new TH1F("activeFract", "Fraction of active channels; slot number; active fraction", 16, 0.5, 16.5);
118  for (int slot = 1; slot <= 16; slot++) {
119  dead->SetBinContent(slot, m_channelMask->getNumOfDeadChannels(slot));
120  hot->SetBinContent(slot, m_channelMask->getNumOfNoisyChannels(slot));
121  active->SetBinContent(slot, m_channelMask->getActiveFraction(slot));
122  }
123  m_file->Write();
124  m_file->Close();
125 
126  // import payload to DB
127 
129 
130  return c_OK;
131  }
132 
133 
134  } // end namespace TOP
136 } // end namespace Belle2
137 
Belle2::TOPCalChannelMask::getNumOfNoisyChannels
int getNumOfNoisyChannels() const
Returns number of noisy channels.
Definition: TOPCalChannelMask.h:123
Belle2::TOPCalChannelMask::setNoisy
void setNoisy(int moduleID, unsigned channel)
Sets a specific channel as noisy.
Definition: TOPCalChannelMask.cc:53
Belle2::TOP::TOPChannelMaskAlgorithm::m_minHits
double m_minHits
minimal number of hits per channel needed for calibration
Definition: TOPChannelMaskAlgorithm.h:71
Belle2::CalibrationAlgorithm::saveCalibration
void saveCalibration(TClonesArray *data, const std::string &name)
Store DBArray payload with given name with default IOV.
Definition: CalibrationAlgorithm.cc:290
Belle2::TOPCalChannelMask
Channel status for all 512 channels of 16 modules.
Definition: TOPCalChannelMask.h:39
Belle2::CalibrationAlgorithm::c_OK
@ c_OK
Finished successfuly =0 in Python.
Definition: CalibrationAlgorithm.h:51
Belle2::CalibrationAlgorithm::setDescription
void setDescription(const std::string &description)
Set algorithm description (in constructor)
Definition: CalibrationAlgorithm.h:331
Belle2::TOP::TOPChannelMaskAlgorithm::m_minWindow
int m_minWindow
band lower limit in window_vs_asic
Definition: TOPChannelMaskAlgorithm.h:72
Belle2::TOP::TOPChannelMaskAlgorithm::m_file
TFile * m_file
root file
Definition: TOPChannelMaskAlgorithm.h:76
Belle2::TOPCalChannelMask::setDead
void setDead(int moduleID, unsigned channel)
Sets a specific channel as dead.
Definition: TOPCalChannelMask.cc:43
Belle2::TOP::TOPChannelMaskAlgorithm::calibrate
virtual EResult calibrate() final
algorithm implementation
Definition: TOPChannelMaskAlgorithm.cc:35
Belle2::TOPCalChannelMask::getActiveFraction
double getActiveFraction() const
Returns fraction of active channels.
Definition: TOPCalChannelMask.h:128
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::TOP::TOPChannelMaskAlgorithm::m_channelMask
TOPCalChannelMask * m_channelMask
masks of dead and hot channels
Definition: TOPChannelMaskAlgorithm.h:79
Belle2::TOP::TOPChannelMaskAlgorithm::m_maxWindow
int m_maxWindow
band upper limit in window_vs_asic
Definition: TOPChannelMaskAlgorithm.h:73
Belle2::TOPCalChannelMask::getNumOfDeadChannels
int getNumOfDeadChannels() const
Returns number of dead channels.
Definition: TOPCalChannelMask.h:118
Belle2::CalibrationAlgorithm::EResult
EResult
The result of calibration.
Definition: CalibrationAlgorithm.h:50
Belle2::CalibrationAlgorithm::getRunList
const std::vector< Calibration::ExpRun > & getRunList() const
Get the list of runs for which calibration is called.
Definition: CalibrationAlgorithm.h:276
Belle2::CalibrationAlgorithm::c_NotEnoughData
@ c_NotEnoughData
Needs more data =2 in Python.
Definition: CalibrationAlgorithm.h:53
Belle2::CalibrationAlgorithm
Base class for calibration algorithms.
Definition: CalibrationAlgorithm.h:47