Belle II Software  release-08-01-10
ARICHHitRateCounter.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 // Own header.
10 #include <background/modules/BeamBkgHitRateMonitor/ARICHHitRateCounter.h>
11 
12 // framework aux
13 #include <framework/logging/Logger.h>
14 
15 using namespace std;
16 
17 namespace Belle2 {
22  namespace Background {
23 
24  void ARICHHitRateCounter::initialize(TTree* tree)
25  {
26  // register collection(s) as optional, your detector might be excluded in DAQ
27  m_hits.isOptional();
28 
29  // set branch address
30  tree->Branch("arich", &m_rates, "segmentRates[18]/F:averageRate/F:numEvents/I:valid/O");
31 
32  // make map of modules to 18 segments
33  // (first hapd ring is 6 segments (by sector), for the rest, each segment merges 3 hapd rings (again by sector))
34  const int nModInRing[7] = {0, 42, 90, 144, 204, 270, 342};
35  int iRing = 0;
36  for (int i = 0; i < 420; i++) {
37  if (i == nModInRing[iRing + 1]) iRing++;
38  int segment = (i - nModInRing[iRing]) / (7 + iRing);
39  if (iRing > 0) segment += 6;
40  if (iRing > 3) segment += 6;
41  m_segmentMap[i] = segment;
42  }
43 
44  // set fractions of active channels
45  setActiveHapds();
46  }
47 
48  void ARICHHitRateCounter::clear()
49  {
50  m_buffer.clear();
51  }
52 
53  void ARICHHitRateCounter::accumulate(unsigned timeStamp)
54  {
55  // check if data are available
56  if (not m_hits.isValid()) return;
57 
58  // get buffer element
59  auto& rates = m_buffer[timeStamp];
60 
61  // increment event counter
62  rates.numEvents++;
63 
64  // count and weight hits accoring to channel efficiecny
65  for (const auto& hit : m_hits) {
66  if (hit.getModule() < 1 || hit.getModule() > 420) continue;
67  auto effi = m_modulesInfo->getChannelQE(hit.getModule(), hit.getChannel());
68  float wt = std::min(1.0 / effi, 100.);
69  rates.segmentRates[m_segmentMap[hit.getModule() - 1]] += wt;
70  rates.averageRate += wt;
71  }
72 
73  // set flag to true to indicate the rates are valid
74  rates.valid = true;
75 
76  }
77 
78  void ARICHHitRateCounter::normalize(unsigned timeStamp)
79  {
80  // copy buffer element
81  m_rates = m_buffer[timeStamp];
82 
83  if (not m_rates.valid) return;
84 
85  // normalize
86  m_rates.normalize();
87 
88  // correct rates for masked-out channels
89  if (m_channelMask.hasChanged()) setActiveHapds();
90 
91  for (int iSegment = 0; iSegment < 18; iSegment++) {
92  double nHapds = m_activeHapds[iSegment];
93  if (nHapds > 0) m_rates.segmentRates[iSegment] /= nHapds;
94  else m_rates.segmentRates[iSegment] = 0;
95  }
96  m_rates.averageRate /= m_activeTotal;
97  }
98 
99  void ARICHHitRateCounter::setActiveHapds()
100  {
101  for (auto& nactive : m_activeHapds) nactive = 0;
102 
103  if (not m_channelMask.isValid()) {
104  for (unsigned imod = 1; imod < 421; imod++) m_activeHapds[m_segmentMap[imod - 1]] += 1.;
105  m_activeTotal = 420;
106  B2WARNING("ARICHHitRateCounter: no valid channel mask - all HAPDs set to active");
107  return;
108  }
109 
110  int nactiveTotal = 0;
111  for (unsigned imod = 1; imod < 421; imod++) {
112  int nactive = 0;
113  for (unsigned ichn = 0; ichn < 144; ichn++) {
114  if (m_channelMask->isActive(imod, ichn)) nactive++;
115  }
116  nactiveTotal += nactive;
117  m_activeHapds[m_segmentMap[imod - 1]] += (float)nactive / 144.;
118  }
119  m_activeTotal = (float)nactiveTotal / 144.;
120  }
121 
122  } // Background namespace
124 } // Belle2 namespace
125 
Abstract base class for different kinds of events.