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