Belle II Software development
DQMHistDeltaHisto.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// File : DQMHistDeltaHisto.cc
10// Description : DQM Histogram analysis module to generate delta histograms
11//-
12
13
14#include <framework/core/ModuleParam.templateDetails.h>
15#include <dqm/analysis/modules/DQMHistDeltaHisto.h>
16#include <TROOT.h>
17#include <TClass.h>
18
19using namespace std;
20using namespace Belle2;
21
22//-----------------------------------------------------------------
23// Register the Module
24//-----------------------------------------------------------------
25REG_MODULE(DQMHistDeltaHisto);
26
27//-----------------------------------------------------------------
28// Implementation
29//-----------------------------------------------------------------
30
33{
34 addParam("Interval", m_interval, "Interval time for diff histos [s]", 180);
35 addParam("MonitoredHistos", m_monitoredHistos, "List of histograms to monitor", vector<string>());
36 B2DEBUG(20, "DQMHistDeltaHisto: Constructor done.");
37}
38
39
41
43{
44 gROOT->cd();
45 B2DEBUG(20, "DQMHistDeltaHisto: initialized.");
46 for (auto& histoname : m_monitoredHistos) {
47 queue<SSNODE*> hq;
48 m_histosQueues[histoname] = hq;
49 }
50 m_evtMetaDataPtr.isRequired();
51}
52
53
55{
56 B2DEBUG(20, "DQMHistDeltaHisto: beginRun called.");
57 for (auto& histoname : m_monitoredHistos) {
58 queue<SSNODE*>& hq = m_histosQueues[histoname];
59 while (!hq.empty()) {
60 SSNODE* nn = hq.front();
61 clear_node(nn);
62 delete nn;
63 hq.pop();
64 }
65 }
66}
67
69{
70 delete n->histo;
71 delete n->diff_histo;
72}
73
75{
76
77 B2DEBUG(20, "DQMHistDeltaHisto: event called.");
78 if (!m_evtMetaDataPtr.isValid()) {
79 B2ERROR("No valid EventMetaData.");
80 return;
81 }
82 time_t cur_mtime = m_evtMetaDataPtr->getTime();
83
84 for (auto& histoname : m_monitoredHistos) {
85 TH1* hh = findHist(histoname.c_str());
86 if (hh == nullptr) continue;
87 if (hh->GetDimension() != 1) continue;
88 queue<SSNODE*>& hq = m_histosQueues[histoname];
89 if (hq.empty()) {
90 SSNODE* n = new SSNODE;
91 n->histo = (TH1*)hh->Clone();
92 n->diff_histo = (TH1*)hh->Clone();
93 n->time_modified = cur_mtime;
94 hq.push(n);
95 } else {
96 while (!hq.empty()) {
97 SSNODE* nn = hq.front();
98 if ((cur_mtime - nn->time_modified < m_interval) || (hq.size() == 1)) {
99 if (hq.back()->time_modified == cur_mtime) {
100 break;
101 }
102 SSNODE* n = new SSNODE;
103 n->histo = (TH1*)hh->Clone();
104 n->diff_histo = (TH1*)hh->Clone();
105 n->diff_histo->Add(nn->histo, -1);
106 n->time_modified = cur_mtime;
107 hq.push(n);
108 break;
109 } else {
110 clear_node(nn);
111 delete nn;
112 hq.pop();
113 }
114 }
115 }
116 auto s = StringSplit(histoname, '/');
117 auto dirname = s.at(0);
118 auto hname = s.at(1);
119 std::string canvas_name = dirname + "/c_" + hname;
120 TCanvas* c = findCanvas(canvas_name);
121 if (c == nullptr) continue;
122 TH1* h_diff = hq.back()->diff_histo;
123 h_diff->SetName((histoname + "_diff").data());
124 if (h_diff->Integral() != 0) h_diff->Scale(hh->Integral() / h_diff->Integral());
125 c->cd();
126 h_diff->SetLineColor(kRed);
127 h_diff->SetLineStyle(kDotted);
128 h_diff->SetStats(kFALSE);
129 h_diff->Draw("hist,same");
130 c->Modified();
131 c->Update();
132 }
133
134}
135
137{
138 B2DEBUG(20, "DQMHistDeltaHisto: endRun called");
139}
140
141
143{
144 B2DEBUG(20, "DQMHistDeltaHisto: terminate called");
145}
The base class for the histogram analysis module.
TCanvas * findCanvas(TString cname)
Find canvas by name.
static TH1 * findHist(const std::string &histname, bool onlyIfUpdated=false)
Get histogram from list (no other search).
std::vector< std::string > StringSplit(const std::string &s, const char delim)
Helper function for string token split.
void initialize() override final
Initializer.
StoreObjPtr< EventMetaData > m_evtMetaDataPtr
The metadata for each event.
void terminate() override final
This method is called at the end of the event processing.
void event() override final
This method is called for each event.
void clear_node(SSNODE *n)
Clear content of SSNODE.
void endRun() override final
This method is called if the current run ends.
int m_interval
Interval between checks in second.
std::map< std::string, std::queue< SSNODE * > > m_histosQueues
Map of histogram names to queues of monitoring objects.
void beginRun() override final
Called when entering a new run.
std::vector< std::string > m_monitoredHistos
Names of the histograms that should be monitored.
void addParam(const std::string &name, T &paramVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module.
Definition: Module.h:560
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:650
Abstract base class for different kinds of events.
STL namespace.
The struct for the snapshots.
TH1 * histo
The histogram for snapshot.
time_t time_modified
Whether the histogram is not updated for a long time.