Belle II Software development
DQMHistAnalysisOutputFile.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#include <dqm/analysis/modules/DQMHistAnalysisOutputFile.h>
10#include <TROOT.h>
11#include <TObject.h>
12#include "TKey.h"
13#include "TFile.h"
14#include <sstream>
15#include <iomanip>
16
17using namespace std;
18using namespace Belle2;
19
20//-----------------------------------------------------------------
21// Register the Module
22//-----------------------------------------------------------------
23
24REG_MODULE(DQMHistAnalysisOutputFile);
25
26//-----------------------------------------------------------------
27// Implementation
28//-----------------------------------------------------------------
29
30
33{
34
35 setDescription("Module to save histograms from DQMHistAnalysisModules");
36 //Parameter definition
37
38 addParam("OutputFolder", m_folder, "Output file path", std::string(""));
39 addParam("FilePrefix", m_prefix,
40 "prefix of the output filename {prefix}dqm_e####r######.root is generated (unless Filename is set)", std::string(""));
41 addParam("Filename", m_filename, "name of the output file (default is {prefix}dqm_e####r######.root)", std::string(""));
42 addParam("SaveHistos", m_saveHistos, "Save Histos (not default)", false);
43 addParam("SaveCanvases", m_saveCanvases, "Save Canvases (default)", true);
44 addParam("SavePerEvent", m_savePerEvent, "Whether save to file for each event", true);
45 addParam("SavePerRun", m_savePerRun, "Whether save to file for each run (not usable in online analysis!)", false);
46 B2DEBUG(20, "DQMHistAnalysisOutputFile: Constructor done.");
47}
48
49
51
53{
54 m_eventMetaDataPtr.isOptional();
55 B2DEBUG(20, "DQMHistAnalysisOutputFile: initialized.");
56}
57
59{
60 B2DEBUG(20, "DQMHistAnalysisOutputFile: event called.");
62}
63
65{
66 B2INFO("DQMHistAnalysisOutputFile: endRun called");
68}
69
70
72{
73 B2INFO("DQMHistAnalysisOutputFile: terminate called");
74}
75
77{
78
79 std::stringstream ss;
80 ss << m_folder << "/";
81 if (m_filename != "") ss << m_filename;
82 else {
83 int exp = 0;
84 int run = 0;
86 exp = m_eventMetaDataPtr->getExperiment();
87 run = m_eventMetaDataPtr->getRun();
88 }
89 ss << m_prefix << "dqm_e";
90 ss << std::setfill('0') << std::setw(4) << exp;
91 ss << "r" << std::setfill('0') << std::setw(6) << run;
92 ss << ".root";
93 }
94
95 TFile f(ss.str().data(), "recreate");
96
97 if (f.IsOpen()) {
98 if (m_saveCanvases) {
99 TSeqCollection* seq;
100 seq = gROOT->GetListOfCanvases() ;
101 if (seq) {
102 B2INFO("found canvases");
103 TIter next(seq) ;
104 TObject* obj ;
105 while ((obj = (TObject*)next())) {
106 if (obj->InheritsFrom("TCanvas")) {
107 B2DEBUG(1, "Saving canvas " << obj->GetName());
108 obj->Write();
109 }
110 }
111 }
112 }
113
114 if (m_saveHistos) {
115 TSeqCollection* files;
116 files = gROOT->GetListOfFiles() ;
117 if (files) {
118 B2INFO("found keys");
119 TIter nextfile(files) ;
120 TObject* file ;
121
122 while ((file = (TObject*)nextfile())) {
123 if (file->InheritsFrom("TFile")) {
124 B2INFO("File name: " << file->GetName() << " title " << file->GetTitle());
125 if (file == &f || file->GetName() == m_filename) continue;
126
127 TList* list = ((TFile*)file)->GetListOfKeys() ;
128 if (list) {
129 TIter next(list) ;
130 TKey* key ;
131 TObject* obj ;
132
133 while ((key = (TKey*)next())) {
134 TString skey(key->GetClassName());
135 if (skey.BeginsWith(TString("Belle2::"))) continue;
136 TClass clkey(key->GetClassName());
137 if (clkey.InheritsFrom("TH1")) {
138 obj = key->ReadObj() ;
139 B2INFO("Histo name: " << obj->GetName() << " title " << obj->GetTitle());
140 TDirectory* old, *d;
141 d = old = gDirectory;
142 TString myl = obj->GetName();
143 TString tok;
144 Ssiz_t from = 0;
145 while (myl.Tokenize(tok, from, "/")) {
146 TString dummy;
147 Ssiz_t fr;
148 fr = from;
149 if (myl.Tokenize(dummy, fr,
150 "/")) { // check if its the last one
151 auto e = d->GetDirectory(tok);
152 if (e) {
153 d = e;
154 d->cd();
155 } else {
156 d->mkdir(tok);
157 d->cd(tok);
158 d = gDirectory;
159 }
160 } else {
161 break;
162 }
163 }
164 ((TH1*)obj)->SetName(tok);
165 obj->Write();
166 old->cd();
167 }
168 }
169 }
170 } else {
171 B2INFO("Others name: " << file->GetName() << " title " << file->GetTitle());
172 }
173 }
174 }
175 }
176
177 f.Write();
178 f.Close();
179 }
180}
The base class for the histogram analysis module.
bool m_saveCanvases
Write all Canvases to file.
void initialize() override final
Initializer.
bool m_savePerEvent
Whether save to file per event.
std::string m_prefix
prefix for the output file name
std::string m_folder
output folder to save root file
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 endRun() override final
This method is called if the current run ends.
std::string m_filename
file name of root file
StoreObjPtr< EventMetaData > m_eventMetaDataPtr
event metadata object
void save_to_file()
Opens the root file and saves the content.
bool m_savePerRun
Whether save to file per run.
void setDescription(const std::string &description)
Sets the description of the module.
Definition: Module.cc:214
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.