Belle II Software development
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
13using namespace Belle2;
14
15HistDelta::HistDelta(EDeltaType t, int p, unsigned int a)
16{
17 m_type = t;
18 m_parameter = p;
20 m_lastHist = nullptr; // implied
21 m_lastValue = 0; // implied
22}
23
25{
26 m_lastHist = nullptr;
27 m_deltaHists.clear();
28}
29
30void HistDelta::set(EDeltaType t, int p, unsigned int a)
31{
32 m_type = t;
33 m_parameter = p;
35 m_lastHist = nullptr;
36 m_lastValue = 0;
37 m_deltaHists.clear();
38}
39
40void HistDelta::update(const TH1* currentHist)
41{
42 m_updated = false;
43 if (currentHist == nullptr) return; // this won't make sense
44 gROOT->cd(); // make sure we dont accidentally write the histograms to a open file
45 // cover first update after start
46 if (m_lastHist == nullptr) {
47 m_lastHist = std::unique_ptr<TH1> ((TH1*)(currentHist->Clone()));
48 m_lastHist->SetName(TString(currentHist->GetName()) + "_last");
49 m_lastHist->Reset();
50 m_updated = true;
51 }
52 // now check if need to update m_deltaHists
53 bool need_update = false;
54 switch (m_type) {
55 case c_Entries:
56 // default case, look at the entries in the histogram
57 need_update = currentHist->GetEntries() - m_lastHist->GetEntries() >= m_parameter;
58 break;
59 case c_Underflow:
60 // here we misuse underflow as event counter in some histograms, e.g. PXD
61 need_update = currentHist->GetBinContent(0) - m_lastHist->GetBinContent(0) >= m_parameter;
62 break;
63 case c_Events:
64 // use event processed counter
66 if (need_update) m_lastValue = DQMHistAnalysisModule::getEventProcessed(); // update last value
67 break;
68 default:
69 // any unsupported types map to case 0(Disabled), and will disable delta for this hist
70 [[fallthrough]];
71 case c_Disabled:
72 break;
73 }
74
75 if (need_update) {
76 m_updated = true;
77 auto delta = (TH1*)currentHist->Clone();
78 delta->SetName(TString(delta->GetName()) + "_delta");
79 delta->Add(m_lastHist.get(), -1.);
80
81 // we use this as a fifo, but cannot use queue as we need the random access
82 // maybe use deque?
83 m_deltaHists.emplace(m_deltaHists.begin(), delta);
84 if (m_deltaHists.size() > m_amountDeltas) {
85 // remove (and delete) last element
86 m_deltaHists.erase(m_deltaHists.begin() + m_deltaHists.size() - 1);
87 }
88 m_lastHist->Reset();
89 m_lastHist->Add(currentHist);
90 }
98}
99
101{
102 m_deltaHists.clear();
103 if (m_lastHist) m_lastHist->Reset();
104 m_lastValue = 0;
105}
106
107TH1* HistDelta::getDelta(unsigned int n, bool onlyIfUpdated) const
108{
109 if (onlyIfUpdated && !m_updated) return nullptr;// not updated, but requested
110 if (n >= m_deltaHists.size()) return nullptr;
111 return m_deltaHists.at(n).get();
112}
static int getEventProcessed(void)
Get the number of processed events.
void update(const TH1 *hist)
Check if update of delta histogram is necessary.
Definition: HistDelta.cc:40
~HistDelta()
Destructor.
Definition: HistDelta.cc:24
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:30
void reset(void)
Reset histogram and deltas, not the parameters.
Definition: HistDelta.cc:100
std::unique_ptr< TH1 > m_lastHist
Pointer to last histogram state for check.
Definition: HistDelta.h:36
TH1 * getDelta(unsigned int n=0, bool onlyIfUpdated=true) const
Get Delta Histogram.
Definition: HistDelta.cc:107
std::vector< std::unique_ptr< 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)
Constructor.
Definition: HistDelta.cc:15
EDeltaType
enum definition for delta algo Disabled: nothing Entries: use nr histogram entries Underflow: use ent...
Definition: HistDelta.h:32
bool m_updated
if any delta was updated in this event
Definition: HistDelta.h:39
Abstract base class for different kinds of events.