Belle II Software  release-08-01-10
TOPASICPedestals.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/dbobjects/TOPASICPedestals.h>
10 #include <framework/logging/Logger.h>
11 
12 #include <set>
13 #include <iostream>
14 
15 using namespace std;
16 
17 namespace Belle2 {
23  int TOPASICPedestals::setPedestals(const TProfile* profile, double average)
24  {
25  int bad = c_WindowSize;
26 
27  if (!profile) return bad;
28 
29  if (profile->GetNbinsX() != c_WindowSize) {
30  B2ERROR("TOPASICPedestals::setPedestals: TProfile with wrong number of bins");
31  return bad;
32  }
33 
34  if (profile->GetEntries() == 0) {
35  B2WARNING("TOPASICPedestals::setPedestals: TProfile with no entries");
36  return bad;
37  }
38 
39  std::vector<unsigned> values;
40  std::vector<unsigned> errors;
41  for (int i = 0; i < c_WindowSize; i++) {
42  values.push_back(int(profile->GetBinContent(i + 1) + average + 0.5));
43  errors.push_back(int(profile->GetBinError(i + 1) + 0.5));
44  }
45 
46  unsigned maxDif = 1 << c_Bits; maxDif--;
47  unsigned maxErr = 1 << (sizeof(unsigned short) * 8 - c_Bits); maxErr--;
48  m_offset = getOptimizedOffset(values, errors, maxDif, maxErr);
49 
50  for (int i = 0; i < c_WindowSize; i++) {
51  unsigned dif = values[i] - m_offset;
52  if (dif > maxDif) continue;
53  unsigned err = errors[i];
54  if (err > maxErr) continue;
55  if (err == 0) err = 1;
56  m_pedestals[i] = dif + (err << c_Bits);
57  bad--;
58  }
59 
60  return bad;
61 
62  }
63 
64 
65  unsigned TOPASICPedestals::getOptimizedOffset(const std::vector<unsigned>& values,
66  const std::vector<unsigned>& errors,
67  unsigned maxDif,
68  unsigned maxErr)
69  {
70 
71  std::set<unsigned> sortedValues;
72  for (unsigned i = 0; i < values.size(); i++) {
73  if (errors[i] > maxErr) continue;
74  sortedValues.insert(values[i]);
75  }
76 
77  unsigned nprev = sortedValues.size();
78  unsigned nl = 0;
79  for (auto it = sortedValues.begin(); it != sortedValues.end(); ++it) {
80  unsigned nr = 0;
81  for (auto back = sortedValues.crbegin(); *back - *it > maxDif; ++back) {
82  nr++;
83  }
84  unsigned nbad = nl + nr;
85  if (nbad == 0) return *it;
86  if (nbad >= nprev) return *(--it);
87  nprev = nbad;
88  nl++;
89  }
90 
91  return *sortedValues.begin();
92 
93  }
94 
96 } // end Belle2 namespace
Abstract base class for different kinds of events.