Belle II Software  release-08-01-10
HistDelta.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 #include <dqm/analysis/HistDelta.h>
9 #include <dqm/core/DQMHistAnalysis.h>
10 #include <string>
11 #include <TROOT.h>
12 
13 using namespace Belle2;
14 
15 HistDelta::HistDelta(EDeltaType t, int p, unsigned int a)
16 {
17  m_type = t;
18  m_parameter = p;
19  m_amountDeltas = a;
20  m_lastHist = nullptr;
21  m_lastValue = 0;
22 };
23 
24 void HistDelta::set(EDeltaType t, int p, unsigned int a)
25 {
26  m_type = t;
27  m_parameter = p;
28  m_amountDeltas = a;
29 };
30 
31 void HistDelta::update(TH1* currentHist)
32 {
33  m_updated = false;
34  if (currentHist == nullptr) return; // this wont make sense
35  gROOT->cd(); // make sure we dont accidentally write the histograms to a open file
36  // cover first update after start
37  if (m_lastHist == nullptr) {
38  m_lastHist = (TH1*)currentHist->Clone();
39  m_lastHist->SetName(TString(currentHist->GetName()) + "_last");
40  m_lastHist->Reset();
41  m_updated = true;
42  }
43  // now check if need to update m_deltaHists
44  bool need_update = false;
45  switch (m_type) {
46  case c_Entries:
47  // default case, look at the entries in the histogram
48  need_update = currentHist->GetEntries() - m_lastHist->GetEntries() >= m_parameter;
49  break;
50  case c_Underflow:
51  // here we misuse underflow as event counter in some histograms, e.g. PXD
52  need_update = currentHist->GetBinContent(0) - m_lastHist->GetBinContent(0) >= m_parameter;
53  break;
54  case c_Events:
55  // use event processed counter
57  if (need_update) m_lastValue = DQMHistAnalysisModule::getEventProcessed(); // update last value
58  break;
59  default:
60  // any unsupported types map to case 0(Disabled), and will disable delta for this hist
61  [[fallthrough]];
62  case c_Disabled:
63  break;
64  }
65 
66  if (need_update) {
67  m_updated = true;
68  TH1* delta = (TH1*)currentHist->Clone();
69  delta->Add(m_lastHist, -1.);
70 
71  // we use this as a fifo, but cannot use queue as we need the random access
72  // maybe use deque?
73  m_deltaHists.emplace(m_deltaHists.begin(), delta);
74  if (m_deltaHists.size() > m_amountDeltas) {
75  // remove (and delete) last element
76  auto h = m_deltaHists.back();
77  m_deltaHists.erase(m_deltaHists.begin() + m_deltaHists.size() - 1);
78  if (h) delete h;
79  }
80  m_lastHist->Reset();
81  m_lastHist->Add(currentHist);
82  }
90 }
91 
92 void HistDelta::reset(void)
93 {
94  // m_deltaHists.clear(); // loop and delete? to be checked what is left in memory
95  // by intention, we may not want to delete old m_deltaHists, thus having them from m_lastHist run? tbd
96  if (m_lastHist) m_lastHist->Reset();
97  m_updated = true;
98 }
99 
100 TH1* HistDelta::getDelta(unsigned int n, bool onlyIfUpdated)
101 {
102  if (onlyIfUpdated && !m_updated) return nullptr;// not updated, but requested
103  if (n >= m_deltaHists.size()) return nullptr;
104  return m_deltaHists.at(n);
105 }
static int getEventProcessed(void)
Get the number of processed events.
EDeltaType m_type
type of delta algo
Definition: HistDelta.h:33
void set(EDeltaType t, int p, unsigned int a)
Parameter setter.
Definition: HistDelta.cc:24
void reset(void)
Reset histogram and deltas, not the parameters.
Definition: HistDelta.cc:92
TH1 * getDelta(unsigned int n=0, bool onlyIfUpdated=true)
Get Delta Histogram.
Definition: HistDelta.cc:100
std::vector< TH1 * > m_deltaHists
vector of histograms (max m_amountDeltas)
Definition: HistDelta.h:38
int m_lastValue
last value for comparison, depending on type
Definition: HistDelta.h:37
unsigned int m_amountDeltas
amount of past histograms, at least 1
Definition: HistDelta.h:35
int m_parameter
parameter depending on algo, e.g.
Definition: HistDelta.h:34
HistDelta(EDeltaType t=c_Disabled, int p=0, unsigned int a=0)
Construktor.
Definition: HistDelta.cc:15
EDeltaType
enum definition for delta algo Disabled: nothing Entries: use nr histogram entries Underflow: use ent...
Definition: HistDelta.h:32
void update(TH1 *hist)
Check if update of delta histogram is necessary.
Definition: HistDelta.cc:31
TH1 * m_lastHist
Pointer to last histogram state for check.
Definition: HistDelta.h:36
bool m_updated
if any delta was updated in this event
Definition: HistDelta.h:39
Abstract base class for different kinds of events.