Belle II Software release-09-00-00
DQMHistAnalysisInput2.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 : DQMHistAnalysisInput2.cc
10// Description :
11//-
12
13
14#include <dqm/analysis/modules/DQMHistAnalysisInput2.h>
15
16#include <TROOT.h>
17#include <TKey.h>
18
19#include <ctime>
20
21using namespace Belle2;
22
23//-----------------------------------------------------------------
24// Register the Module
25//-----------------------------------------------------------------
26REG_MODULE(DQMHistAnalysisInput2);
27
28//-----------------------------------------------------------------
29// Implementation
30//-----------------------------------------------------------------
31
34{
35 //Parameter definition
36 addParam("HistMemoryPath", m_mempath, "Path to Input Hist memory", std::string(""));
37 addParam("StatFileName", m_statname, "Filename for status export", std::string(""));
38 addParam("RefreshInterval", m_interval, "Refresh interval of histograms", 10);
39 addParam("RemoveEmpty", m_remove_empty, "Remove empty histograms", false);
40 addParam("EnableRunInfo", m_enable_run_info, "Enable Run Info", false);
41 B2DEBUG(1, "DQMHistAnalysisInput2: Constructor done.");
42}
43
45{
47 m_c_info = new TCanvas("DQMInfo/c_info", "");
48 m_c_info->SetTitle("");
49 }
50 m_eventMetaDataPtr.registerInDataStore();
51 B2DEBUG(1, "DQMHistAnalysisInput2: initialized.");
52}
53
54
56{
57 B2DEBUG(1, "DQMHistAnalysisInput2: beginRun called.");
61
62 m_last_beginrun = time(0);
64 m_lasttime -= std::chrono::seconds(1); // just change
65 m_forceChanged = true;
66}
67
69{
70 m_last_event = time(0);
72
73 TH1::AddDirectory(false);
75
76// we create a metadata and set the latest exp/run
77// if do not, execution will be terminate if file is not updated/zombie
78// if we only put dummy exp/run, we trigger begin/endRun ...
79 m_eventMetaDataPtr.create();
80 m_eventMetaDataPtr->setExperiment(m_expno);
82 m_eventMetaDataPtr->setEvent(m_count);
83
84 const std::filesystem::file_time_type ftime = std::filesystem::last_write_time(m_mempath);
85
86 if (m_lasttime == ftime) {
87 B2INFO("File not updated! -> Sleep");
88 sleep(m_interval);
89 setReturnValue(false);
90 return;
91 }
92 m_last_file_update = time(0);
93 m_lasttime = ftime;
94
95 char mbstr[100];
96 time_t now = time(0);
97 strftime(mbstr, sizeof(mbstr), "%F %T", localtime(&now));
98 B2INFO("[" << mbstr << "] before Load File");
99
100 std::unique_ptr<TFile> pFile = std::unique_ptr<TFile> (new TFile(m_mempath.c_str(), "READ"));
101 if (pFile->IsZombie()) {
102 B2WARNING("DQMHistAnalysisInput2: " << m_mempath + " is Zombie -> Sleep");
103 sleep(m_interval);
104 setReturnValue(false);
105 return;
106 }
107
108 now = time(0);
109 strftime(mbstr, sizeof(mbstr), "%F %T", localtime(&now));
110 B2INFO("[" << mbstr << "] after LoadFile");
111
112 const TDatime& mt = pFile->GetModificationDate();
113 TDatime mmt(mt.Convert());
114 std::string expno("UNKNOWN"), runno("UNKNOWN"), rtype("UNKNOWN");
115
116 pFile->cd();
117 TIter next(pFile->GetListOfKeys());
118 TKey* key = nullptr;
119
120 now = time(0);
121 strftime(mbstr, sizeof(mbstr), "%F %T", localtime(&now));
122 B2INFO("[" << mbstr << "] before input loop");
123
124 // check if histograms were updated since last DQM event (based on number of processed events)
125 if (m_nevent != getEventProcessed()) {
126 m_lastChange = std::string(mbstr);
127 }
128 // update number of processed events
130
131 std::vector<TH1*> hs; // temporary histograms storage vector
132
133 while ((key = (TKey*)next())) {
134 auto obj = key->ReadObj(); // I now own this object and have to take care to delete it
135 if (obj == nullptr) continue; // would be strange, but better check
136 if (!obj->IsA()->InheritsFrom("TH1")) {
137 delete obj;
138 continue; // other non supported (yet?)
139 }
140 TH1* h = (TH1*)obj; // we are sure its a TH1
141
142 if (m_remove_empty && h->GetEntries() == 0) {
143 delete obj;
144 continue;
145 }
146 // Remove ":" from folder name, workaround!
147 TString a = h->GetName();
148 a.ReplaceAll(":", "");
149 h->SetName(a);
150 B2DEBUG(1, "DQMHistAnalysisInput2: get histo " << a.Data());
151
152 // the following line prevent any histogram outside a directory to be processed
153 if (StringSplit(a.Data(), '/').size() <= 1) {
154 delete obj;
155 continue;
156 }
157
158 // only Histograms in the hs list will be taken care off
159 hs.push_back(h);
160
161 // the following workaround need to be improved
162 if (std::string(h->GetName()) == std::string("DQMInfo/expno")) expno = h->GetTitle();
163 if (std::string(h->GetName()) == std::string("DQMInfo/runno")) runno = h->GetTitle();
164 if (std::string(h->GetName()) == std::string("DQMInfo/rtype")) rtype = h->GetTitle();
165 }
166
167 now = time(0);
168 strftime(mbstr, sizeof(mbstr), "%F %T", localtime(&now));
169 B2INFO("[" << mbstr << "] before Close File");
170
171 // we are done with reading, so close it
172 pFile->Close();
173
174 now = time(0);
175 strftime(mbstr, sizeof(mbstr), "%F %T", localtime(&now));
176 B2INFO("[" << mbstr << "] before delete File");
177 pFile = nullptr;
178
179 now = time(0);
180 strftime(mbstr, sizeof(mbstr), "%F %T", localtime(&now));
181 B2INFO("[" << mbstr << "] after input loop");
182
183 if (expno == std::string("UNKNOWN") || runno == std::string("UNKNOWN")) {
184 B2WARNING("DQMHistAnalysisInput2: " << m_mempath + ": Exp " + expno + ", Run " + runno + ", RunType " + rtype + ", Last Updated " +
185 mmt.AsString());
186 setReturnValue(false);
187 for (auto& h : hs) delete h;
188 return;
189 } else {
190 if (m_c_info != NULL) m_c_info->SetTitle((m_mempath + ": Exp " + expno + ", Run " + runno + ", RunType " + rtype + ", Last Changed "
191 + m_lastChange + ", Last Updated "
192 + mmt.AsString() + ", Last DQM event " + std::string(mbstr)).c_str());
193 m_expno = std::stoi(expno);
194 m_runno = std::stoi(runno);
195 }
196 B2INFO("DQMHistAnalysisInput2: " << m_mempath + ": Exp " + expno + ", Run " + runno + ", RunType " + rtype + ", Last Updated " +
197 mmt.AsString());
198
199
200 m_count++;
201 // Override with updated values
202 m_eventMetaDataPtr->setExperiment(m_expno);
204 m_eventMetaDataPtr->setEvent(m_count);
205 m_eventMetaDataPtr->setTime(mt.Convert());
206
207 //setExpNr(m_expno); // redundant access from MetaData
208 //setRunNr(m_runno); // redundant access from MetaData
209 // ExtractRunType();// Run Type is processed above already, just take it
210 setRunType(rtype);
211 ExtractEvent(hs);
212
213 // this code must be run after "event processed" has been extracted
214 bool anyupdate = m_forceChanged; // flag if any histogram updated at all
215 m_forceChanged = false;
216 for (auto& h : hs) {
217 anyupdate |= addHist("", h->GetName(), h);
218 B2DEBUG(1, "Found : " << h->GetName() << " : " << h->GetEntries());
219 }
220
221 if (anyupdate) {
222 m_last_content_update = time(0);
223 } else {
224 // if there is no update, sleep a moment
225 sleep(m_interval);
226 }
227
228 // if no histogram was updated, we could stop processing
229 setReturnValue(anyupdate);
230}
231
233{
234 B2DEBUG(1, "DQMHistAnalysisInput2: endRun called");
235}
236
238{
239 B2DEBUG(1, "DQMHistAnalysisInput2: terminate called");
240 if (m_c_info) delete m_c_info;
241 clearlist(); // necessary in the Input Module! Otherwise ROOT may clean before we do
242}
243
245{
246 if (m_statname == "") return;
247 FILE* fh = fopen(m_statname.c_str(), "wt+");
248 if (fh) {
249 char mbstr[100];
250 time_t now = time(0);
251 strftime(mbstr, sizeof(mbstr), "%F %T", localtime(&now));
252 fprintf(fh, "%s,%s,%s,", m_statname.c_str(), m_mempath.c_str(), mbstr);
253 strftime(mbstr, sizeof(mbstr), "%F %T", localtime(&m_last_event));
254 fprintf(fh, "%s,", mbstr);
255 strftime(mbstr, sizeof(mbstr), "%F %T", localtime(&m_last_beginrun));
256 fprintf(fh, "%s,", mbstr);
257 strftime(mbstr, sizeof(mbstr), "%F %T", localtime(&m_last_file_update));
258 fprintf(fh, "%s,", mbstr);
259 strftime(mbstr, sizeof(mbstr), "%F %T", localtime(&m_last_content_update));
260 fprintf(fh, "%s\n", mbstr);
261
262 fclose(fh);
263 }
264}
std::string m_lastChange
last change date/time of shm input file
bool m_remove_empty
Whether to remove empty histograms.
void initialize() override final
Initialize the module.
std::string m_statname
The file name of the analysis for stats.
std::filesystem::file_time_type m_lasttime
Last time input file changes.
bool m_enable_run_info
Whether to enable the run info to be displayed.
void terminate() override final
This method is called at the end of the event processing.
time_t m_last_event
last time event loop entered
void event() override final
This method is called for each event.
bool m_forceChanged
enforce a content change on next event
std::string m_mempath
The name of the shared memory for the histograms.
time_t m_last_file_update
last time input file update detected
time_t m_last_content_update
last time input file content has changed
void endRun() override final
This method is called if the current run ends.
int m_interval
The name of the memory file (HLT or ExpressReco).
void beginRun() override final
Called when entering a new run.
StoreObjPtr< EventMetaData > m_eventMetaDataPtr
The metadata for each event.
TCanvas * m_c_info
The canvas hold the basic DQM info.
int m_nevent
DAQ number of processed events.
time_t m_last_beginrun
last time begin run entered
void write_state(void)
Write stats of analysis.
The base class for the histogram analysis module.
void clearlist(void)
Clear all static global lists.
static void clearHistList(void)
Clears the list of histograms.
std::vector< std::string > StringSplit(const std::string &s, const char delim)
Helper function for string token split.
void setRunType(std::string &t)
Set the Run Type.
static bool addHist(const std::string &dirname, const std::string &histname, TH1 *h)
Add histogram.
void clearCanvases(void)
Clear content of all Canvases.
static int getEventProcessed(void)
Get the number of processed events.
static void initHistListBeforeEvent(void)
Reset the list of histograms.
void ExtractEvent(std::vector< TH1 * > &hs)
Extract event processed from daq histogram, called from input module.
void resetDeltaList(void)
Reset Delta.
void setReturnValue(int value)
Sets the return value for this module as integer.
Definition: Module.cc:220
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.