Belle II Software  release-05-01-25
CDCHitRateCounter.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2019 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Marko Staric *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 // Own include
12 #include <background/modules/BeamBkgHitRateMonitor/CDCHitRateCounter.h>
13 
14 // framework aux
15 #include <framework/gearbox/Unit.h>
16 #include <framework/gearbox/Const.h>
17 #include <framework/logging/Logger.h>
18 
19 #include <tracking/trackFindingCDC/rootification/StoreWrappedObjPtr.h>
20 #include <tracking/trackFindingCDC/eventdata/hits/CDCWireHit.h>
21 
22 #include <cdc/geometry/CDCGeometryPar.h>
23 #include <cdc/dataobjects/WireID.h>
24 
25 using namespace std;
26 
27 namespace Belle2 {
33  using TrackFindingCDC::StoreWrappedObjPtr;
34  using TrackFindingCDC::CDCWireHit;
35 
36  namespace Background {
37 
38  void CDCHitRateCounter::initialize(TTree* tree)
39  {
40  // register collection(s) as optional, your detector might be excluded in DAQ
41  m_digits.isOptional();
42 
43  // set branch address
44  //tree->Branch("cdc", &m_rates, "averageRate/F:numEvents/I:valid/O");
45  stringstream leafList;
46  leafList
47  << "layerHitRate[" << f_nLayer << "]/F:"
48  << "superLayerHitRate[" << f_nSuperLayer << "]/F:"
49  << "averageRate/F:"
50  << "timeWindowForSmallCell/I:"
51  << "timeWindowForNormalCell/I:"
52  << "nActiveWireInLayer[" << f_nLayer << "]/I:"
53  << "nActiveWireInSuperLayer[" << f_nSuperLayer << "]/I:"
54  << "nActiveWireInTotal/I:"
55  << "numEvents/I:"
56  << "valid/O";
57  tree->Branch("cdc", &m_rates, leafList.str().c_str());
58 
59  if (m_enableBadWireTreatment) {
60  countActiveWires();
61  } else {
62  countActiveWires_countAll();
63  }
64  }
65 
66  void CDCHitRateCounter::clear()
67  {
68  m_buffer.clear();
69  }
70 
71  void CDCHitRateCounter::accumulate(unsigned timeStamp)
72  {
73  // check if data are available
74  if (not m_digits.isValid()) return;
75 
76  // get buffer element
77  auto& rates = m_buffer[timeStamp];
78 
79  // increment event counter
80  rates.numEvents++;
81 
83  std::map<const CDCHit*, bool> CDCHitToBackgroundFlag;
84  if (m_enableBackgroundHitFilter) {
85  StoreWrappedObjPtr<std::vector<CDCWireHit>> storeVector("CDCWireHitVector");
86  if (not storeVector) {
87  B2FATAL("CDCWireHitVector is unaccessible in DataStore."
88  "Need TFCDC_WireHitParameter module before.");
89  }
90  const std::vector<CDCWireHit>& cdcWireHitVector = *storeVector;
91  for (const auto& cdcWireHit : cdcWireHitVector) {
92  const CDCHit* cdcHit = cdcWireHit.getHit();
93  CDCHitToBackgroundFlag[cdcHit] = cdcWireHit->hasBackgroundFlag();
94  }
95  }
96 
97  CDC::CDCGeometryPar& geometryPar = CDC::CDCGeometryPar::Instance();
98 
106  for (CDCHit& hit : m_digits) {
107  const WireID wireID(hit.getID());
108  if (m_enableBadWireTreatment && geometryPar.isBadWire(wireID))
109  continue;
110 
111  const int iLayer = hit.getICLayer();
112  const int iSuperLayer = hit.getISuperLayer();
113  const unsigned short adc = hit.getADCCount();
114  const short tdc = hit.getTDCCount();
115 
116  if (m_enableBackgroundHitFilter) {
117  if (CDCHitToBackgroundFlag[&hit]) {
118  if (m_enableMarkBackgroundHit) {
119  unsigned short newStatus = (hit.getStatus() | 0x100);
120  hit.setStatus(newStatus);
121  }
122  continue;
123  }
124  } else {
125  if (iSuperLayer == 0 && adc < 15)
126  continue;
127  if (iSuperLayer != 0 && adc < 18)
128  continue;
129  }
130 
131  if (not isInTimeWindow(iSuperLayer, tdc))
132  continue;
133 
134 
135  rates.layerHitRate[iLayer] += 1;
136  rates.superLayerHitRate[iSuperLayer] += 1;
137  rates.averageRate += 1;
138  }
139 
140  // set flag to true to indicate the rates are valid
141  rates.valid = true;
142  }
143 
144  void CDCHitRateCounter::normalize(unsigned timeStamp)
145  {
146  // copy buffer element
147  m_rates = m_buffer[timeStamp];
148 
149  if (not m_rates.valid) return;
150 
151  m_rates.timeWindowForSmallCell = m_timeWindowUpperEdge_smallCell - m_timeWindowLowerEdge_smallCell;
152  m_rates.timeWindowForNormalCell = m_timeWindowUpperEdge_normalCell - m_timeWindowLowerEdge_normalCell;
153 
154  // normalize : hit rate in the time stamp in kHz
155  m_rates.normalize();
156 
159  if (m_nActiveWireInTotal == 0) {
160  m_rates.averageRate = 0;
161  } else {
162  m_rates.averageRate /= m_nActiveWireInTotal;
163  }
165  for (int iSL = 0 ; iSL < f_nSuperLayer ; ++iSL)
166  if (m_nActiveWireInSuperLayer[iSL] == 0) {
167  m_rates.superLayerHitRate[iSL] = 0;
168  } else {
169  m_rates.superLayerHitRate[iSL] /= m_nActiveWireInSuperLayer[iSL];
170  }
172  for (int iLayer = 0 ; iLayer < f_nLayer ; ++iLayer)
173  if (m_nActiveWireInLayer[iLayer] == 0) {
174  m_rates.layerHitRate[iLayer] = 0;
175  } else {
176  m_rates.layerHitRate[iLayer] /= m_nActiveWireInLayer[iLayer];
177  }
178  for (int i = 0 ; i < f_nLayer ; ++i)
179  m_rates.nActiveWireInLayer[i] = m_nActiveWireInLayer[i];
180  for (int i = 0 ; i < f_nSuperLayer ; ++i)
181  m_rates.nActiveWireInSuperLayer[i] = m_nActiveWireInSuperLayer[i];
182  m_rates.nActiveWireInTotal = m_nActiveWireInTotal;
183  }
184 
185 
186  void CDCHitRateCounter::countActiveWires_countAll()
187  {
188  const int nlayer_in_SL[f_nSuperLayer] = { 8, 6, 6,
189  6, 6, 6,
190  6, 6, 6
191  };
192  const int nwire_in_layer[f_nSuperLayer] = { 160, 160, 192,
193  224, 256, 288,
194  320, 352, 384
195  };
196 
197  m_nActiveWireInTotal = 0;
198  int contLayerID = 0;
199  for (int iSL = 0 ; iSL < f_nSuperLayer ; ++iSL) {
200  m_nActiveWireInSuperLayer[iSL] = 0;
201  for (int i = 0 ; i < nlayer_in_SL[iSL] ; ++i) {
202  m_nActiveWireInLayer[contLayerID] = nwire_in_layer[iSL];
203  m_nActiveWireInSuperLayer[iSL] += nwire_in_layer[iSL];
204  m_nActiveWireInTotal += nwire_in_layer[iSL];
205  ++contLayerID;
206  }
207  }
208  }
209 
210 
211 
212  void CDCHitRateCounter::countActiveWires()
213  {
214  const int nlayer_in_SL[f_nSuperLayer] = { 8, 6, 6,
215  6, 6, 6,
216  6, 6, 6
217  };
218  const int nwire_in_layer[f_nSuperLayer] = { 160, 160, 192,
219  224, 256, 288,
220  320, 352, 384
221  };
222 
223  CDC::CDCGeometryPar& geometryPar = CDC::CDCGeometryPar::Instance();
224 
225  m_nActiveWireInTotal = 0;
226  int contLayerID = 0;
227 
228  for (int iSL = 0 ; iSL < f_nSuperLayer ; ++iSL) {
229  m_nActiveWireInSuperLayer[iSL] = 0;
230  for (int iL = 0 ; iL < nlayer_in_SL[iSL] ; ++iL) {
231  m_nActiveWireInLayer[contLayerID] = 0;
232  for (int i = 0 ; i < nwire_in_layer[iSL] ; ++i) {
233  WireID wireID(iSL, iL, i);
234  if (not geometryPar.isBadWire(wireID))
235  ++m_nActiveWireInLayer[contLayerID];
236  }
237  m_nActiveWireInSuperLayer[iSL] += m_nActiveWireInLayer[contLayerID];
238  ++contLayerID;
239  }
240  m_nActiveWireInTotal += m_nActiveWireInSuperLayer[iSL];
241  }
242 
243 
244  //B2INFO("CDC, # of Active wires / # of total wires");
245  std::cout << "CDC, # of Active wires / # of total wires" << std::endl;
246  int contLayerID_2 = 0;
247  for (int iSL = 0 ; iSL < f_nSuperLayer ; ++iSL) {
248  for (int iL = 0 ; iL < nlayer_in_SL[iSL] ; ++iL) {
249  //B2INFO("Layer "<< contLayerID_2 << ": "
250  std::cout << "Layer " << contLayerID_2 << ": "
251  << m_nActiveWireInLayer[contLayerID_2] << " / "
252  //<< nwire_in_layer[iSL] );
253  << nwire_in_layer[iSL] << std::endl;
254  ++contLayerID_2;
255  }
256  }
257 
258  }
259 
260 
261  } // Background namespace
263 } // Belle2 namespace
264 
Belle2::WireID
Class to identify a wire inside the CDC.
Definition: WireID.h:44
Belle2::CDCHit
Class containing the result of the unpacker in raw data and the result of the digitizer in simulation...
Definition: CDCHit.h:51
Belle2::TrackFindingCDC::StoreWrappedObjPtr
This class is for convenience access and registration of objects, that are stored inside the StoreWra...
Definition: StoreWrappedObjPtr.h:40
Belle2::CDC::CDCGeometryPar
The Class for CDC Geometry Parameters.
Definition: CDCGeometryPar.h:75
Belle2::CDC::CDCGeometryPar::isBadWire
bool isBadWire(const WireID &wid)
Inquire if the wire is totally-dead.
Definition: CDCGeometryPar.h:840
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19