Belle II Software development
DQMHistAnalysisOutputMonObj.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
10#include <dqm/analysis/modules/DQMHistAnalysisOutputMonObj.h>
11#include <framework/dataobjects/EventMetaData.h>
12#include <framework/datastore/DataStore.h>
13#include <framework/datastore/StoreObjPtr.h>
14#include "TFile.h"
15#include "TTree.h"
16#include "TString.h"
17#include <time.h>
18
19
20using namespace std;
21using namespace Belle2;
22
23//-----------------------------------------------------------------
24// Register the Module
25//-----------------------------------------------------------------
26REG_MODULE(DQMHistAnalysisOutputMonObj);
27
28//-----------------------------------------------------------------
29// Implementation
30//-----------------------------------------------------------------
31
34{
35 setDescription("Module to process run information.");
36
37 //Parameter definition
38 addParam("Filename", m_filename, "Output root filename (if not set mon_e{exp}r{run}.root is used", std::string(""));
39 addParam("TreeFile", m_treeFile, "If set, entry to run summary TTree from TreeFile is made", std::string(""));
40 addParam("ProcID", m_procID, "Processing id (online,proc10, etc.)", std::string("online"));
41 addParam("run", m_run, "Run number", 0);
42 addParam("exp", m_exp, "Experiment number", 0);
43 addParam("nevt", m_nevt, "Number of events", 0);
44 addParam("runtype", m_runtype, "Run type", std::string(""));
45
46 B2DEBUG(20, "DQMHistAnalysisOutputMonObj: Constructor done.");
47}
48
49
51
53{
54 B2DEBUG(20, "DQMHistAnalysisOutputMonObj: initialized.");
55 // create file metadata
58}
59
60
62{
63 B2DEBUG(20, "DQMHistAnalysisOutputMonObj: beginRun called.");
64}
65
66
68{
69 B2DEBUG(20, "DQMHistAnalysisOutputMonObj: event called.");
70}
71
73{
74 B2INFO("DQMHistAnalysisOutputMonObj: endRun called");
75
77
78 B2INFO("open file");
79 TH1* hrun = findHist("DQMInfo/runno");
80 TH1* hexp = findHist("DQMInfo/expno");
81
82 int run = hrun ? std::stoi(hrun->GetTitle()) : m_run;
83 int exp = hexp ? std::stoi(hexp->GetTitle()) : m_exp;
84 TString fname;
85 if (m_filename.length()) fname = m_filename;
86 else fname = TString::Format("mon_e%04dr%06d_%s.root", exp, run, m_procID.c_str());
87
88 TH1* runtype = findHist("DQMInfo/rtype");
89 if (runtype) m_metaData->setRunType(std::string(runtype->GetTitle()));
91 TH1* hnevt = findHist("DAQ/Nevent");
92 if (hnevt) m_metaData->setNEvents(hnevt->GetEntries());
94
95 TFile* f = new TFile(fname, "NEW");
96 int exist = 0;
97 if (f->IsZombie()) {
98 B2WARNING("File " << LogVar("MonitoringObject file",
99 fname) << " already exists additional data will be appended! previous metadata is kept.");
100 f = new TFile(fname, "UPDATE");
101 exist = 1;
102 }
103
104 // set and write meta data info if first input
105 if (!exist) {
106 m_metaData->setExperimentRun(exp, run);
107 time_t ts = lastEvtMeta->getTime() / 1e9;
108 struct tm* timeinfo;
109 timeinfo = localtime(&ts);
110 char buf[50];
111 m_metaData->setRunDate(asctime_r(timeinfo, buf));
112 m_metaData->Write();
113 }
114
115 // get list of existing monitoring objects
116 const MonObjList& objts = getMonObjList();
117 // write them to the output file
118 for (const auto& obj : objts) {
119 // of object already exists rewrite it
120 if (exist) f->Delete(obj.second.GetName() + TString(";*"));
121 obj.second.Write();
122 }
123 f->Close();
124
125 if (m_treeFile.length() > 0) addTreeEntry();
126
127}
128
130{
131
132 TFile* treeFile = new TFile(m_treeFile.c_str(), "update");
133 auto* tree = (TTree*)treeFile->Get("tree");
134
135 if (tree == NULL) tree = new TTree("tree", "tree");
136
137 int run = m_metaData->getRun();
138 int expe = m_metaData->getExperiment();
139 int nevt = m_metaData->getNEvents();
140 //int rune = 0;
141 //int expee = 0;
142 char* rel = const_cast<char*>(m_metaData->getRelease().c_str());
143 char* db = const_cast<char*>(m_metaData->getDatabaseGlobalTag().c_str());
144 char* date = const_cast<char*>(m_metaData->getRunDate().c_str());
145 char* rtype = const_cast<char*>(m_metaData->getRunType().c_str());
146 char* procID = const_cast<char*>(m_metaData->getProcessingID().c_str());
147
148 auto b_run = tree->GetBranch("run");
149 auto b_exp = tree->GetBranch("exp");
150 auto b_release = tree->GetBranch("release");
151 auto b_gt = tree->GetBranch("gt");
152 auto b_datetime = tree->GetBranch("datetime");
153 auto b_rtype = tree->GetBranch("rtype");
154 auto b_procID = tree->GetBranch("procID");
155 auto b_nevt = tree->GetBranch("nevt");
156
157 // this still needs to be sorted out
158 /*if(b_run){
159 b_run->SetAddress(&rune);
160 b_exp->SetAddress(&expee);
161 for(int ie = 0; ie<b_run->GetEntries(); ie++){
162 b_run->GetEntry(ir);
163 b_exp->GetEntry(ir);
164 if(rune == run && expee = expe){
165
166 }
167 }
168 }*/
169
170
171 if (!b_run) tree->Branch("run", &run, "run/I");
172 else b_run->SetAddress(&run);
173 if (!b_exp) tree->Branch("exp", &expe, "exp/I");
174 else b_exp->SetAddress(&expe);
175 if (!b_nevt) tree->Branch("nevt", &nevt, "nevt/I");
176 else b_nevt->SetAddress(&nevt);
177 if (!b_release) tree->Branch("release", rel, "release/C");
178 else b_release->SetAddress(rel);
179 if (!b_gt) tree->Branch("gt", db, "gt/C");
180 else b_gt->SetAddress(db);
181 if (!b_datetime) tree->Branch("datetime", date, "datetime/C");
182 else b_datetime->SetAddress(date);
183 if (!b_rtype) tree->Branch("rtype", rtype, "rtype/C");
184 else b_rtype->SetAddress(rtype);
185 if (!b_procID) tree->Branch("procID", procID, "procID/C");
186 else b_procID->SetAddress(procID);
187
188
189 auto& objts = getMonObjList();
190 // write them to the output file
191 for (auto& obj : objts) {
192 auto& vars = const_cast<std::map<std::string, float>&>(obj.second.getVariables());
193 auto& upErr = const_cast<std::map<std::string, float>&>(obj.second.getUpError());
194 auto& lowErr = const_cast<std::map<std::string, float>&>(obj.second.getLowError());
195 auto& strVars = obj.second.getStringVariables();
196
197 for (auto& var : vars) {
198 std::string brname = obj.first + "_" + var.first;
199 auto branch = tree->GetBranch((brname).c_str());
200 if (!branch) {
201 branch = tree->Branch((brname).c_str(), &(var.second));
202 fillBranch(branch);
203 } else branch->SetAddress(&(var.second));
204
205 auto vvE1 = upErr.find(var.first);
206 auto vvE2 = lowErr.find(var.first);
207
208 if (vvE1 != upErr.end() && vvE2 == lowErr.end()) {
209 auto errBranch = tree->GetBranch((brname).c_str() + TString("_err"));
210 if (!errBranch) {
211 errBranch = tree->Branch((brname).c_str() + TString("_err"), &(vvE1->second));
212 fillBranch(errBranch);
213 } else errBranch->SetAddress(&(vvE1->second));
214 }
215
216 if (vvE1 != upErr.end() && vvE2 != lowErr.end()) {
217 auto errBranch1 = tree->GetBranch((brname).c_str() + TString("_upErr"));
218 if (!errBranch1) {
219 errBranch1 = tree->Branch((brname).c_str() + TString("_upErr"), &(vvE1->second));
220 fillBranch(errBranch1);
221 } else errBranch1->SetAddress(&(vvE1->second));
222
223 auto errBranch2 = tree->GetBranch((brname).c_str() + TString("_dwErr"));
224 if (!errBranch2) {
225 errBranch2 = tree->Branch((brname).c_str() + TString("_dwErr"), &(vvE2->second));
226 fillBranch(errBranch2);
227 } else errBranch2->SetAddress(&(vvE2->second));
228
229 }
230 }
231
232 for (auto& var : strVars) {
233 std::string brname = obj.first + "_" + var.first;
234 char* cc = const_cast<char*>((var.second).c_str());
235 auto branch = tree->GetBranch((brname).c_str());
236 if (!branch) {
237 std::string ty = brname + "/C";
238 branch = tree->Branch((brname).c_str(), cc, ty.c_str());
239 fillBranch(branch);
240 } else branch->SetAddress(cc);
241 }
242 }
243
244 tree->Fill();
245 tree->Write(0, TObject::kWriteDelete, 0);
246 treeFile->Close();
247
248}
249
251{
252 TTree* tree = (TTree*)branch->GetTree();
253 int nentr = tree->GetEntries();
254 for (int i = 0; i < nentr; i++) {
255 tree->GetEntry(i);
256 branch->Fill();
257 }
258}
259
260
261
263{
264 B2INFO("DQMHistAnalysisOutputMonObj: terminate called");
265// Attention, we can not do that in Terminate, as then the memFile is already closed by previous task!
266
267}
268
Metadata information about a DQM file.
const std::string & getDatabaseGlobalTag() const
Get the database global tag used when creating this file.
const std::string & getRelease() const
Software release version getter.
const std::string & getRunType() const
Run type getter.
void setExperimentRun(int experiment, int run)
Set experiment and run.
unsigned int getNEvents() const
Number of events getter.
void setRunType(const std::string &rtype)
set run type
int getRun() const
run number getter.
const std::string & getRunDate() const
Run date getter (UTC)
void setProcessingID(const std::string &procID)
set processing ID
void setRunDate(const std::string &date)
set run date
int getExperiment() const
experiment number getter.
void setNEvents(unsigned int nEvents)
Number of events setter.
const std::string & getProcessingID() const
Processing ID getter.
The base class for the histogram analysis module.
std::map< std::string, MonitoringObject > MonObjList
The type of list of MonitoringObjects.
static TH1 * findHist(const std::string &histname, bool onlyIfUpdated=false)
Get histogram from list (no other search).
static MonObjList & getMonObjList()
Get the list of MonitoringObjects.
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.
std::string m_procID
processing id (online,proc10,etc)
void fillBranch(TBranch *br)
if new variables is added to run summary TTree this files
void endRun() override final
This method is called if the current run ends.
void beginRun() override final
Called when entering a new run.
void addTreeEntry()
Makes entry with monitoring variables in the run summary TTree.
DQMFileMetaData * m_metaData
output file meta data
std::string m_treeFile
if set, entry with monitoring variables is made in the run summary TTree
void setDescription(const std::string &description)
Sets the description of the module.
Definition: Module.cc:214
Type-safe access to single objects in the data store.
Definition: StoreObjPtr.h:96
Class to store variables with their name which were sent to the logging service.
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.