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_canvas_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_canvas_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
50{
51 m_eventMetaDataPtr.isOptional();
52 B2DEBUG(20, "DQMHistAnalysisOutputFile: initialized.");
53}
54
56{
57 B2DEBUG(20, "DQMHistAnalysisOutputFile: event called.");
59}
60
62{
63 B2INFO("DQMHistAnalysisOutputFile: endRun called");
65}
66
67
69{
70 B2INFO("DQMHistAnalysisOutputFile: terminate called");
71}
72
74{
75
76 std::stringstream ss;
77 ss << m_folder << "/";
78 if (m_filename != "") ss << m_filename;
79 else {
80 int exp = 0;
81 int run = 0;
83 exp = m_eventMetaDataPtr->getExperiment();
84 run = m_eventMetaDataPtr->getRun();
85 }
86 ss << m_prefix << "dqm_canvas_e";
87 ss << std::setfill('0') << std::setw(4) << exp;
88 ss << "r" << std::setfill('0') << std::setw(6) << run;
89 ss << ".root";
90 }
91
92 TFile f(ss.str().data(), "recreate");
93
94 if (f.IsOpen()) {
95 if (m_saveCanvases) {
96 TSeqCollection* seq;
97 seq = gROOT->GetListOfCanvases() ;
98 if (seq) {
99 B2INFO("found canvases");
100 TIter next(seq) ;
101 TObject* obj ;
102 while ((obj = (TObject*)next())) {
103 if (obj->InheritsFrom("TCanvas")) {
104 B2DEBUG(1, "Saving canvas " << obj->GetName());
105 obj->Write();
106 }
107 }
108 }
109 }
110
111 if (m_saveHistos) {
112 TSeqCollection* files;
113 files = gROOT->GetListOfFiles() ;
114 if (files) {
115 B2INFO("found keys");
116 TIter nextfile(files) ;
117 TObject* file ;
118
119 while ((file = (TObject*)nextfile())) {
120 if (file->InheritsFrom("TFile")) {
121 B2INFO("File name: " << file->GetName() << " title " << file->GetTitle());
122 if (file == &f || file->GetName() == m_filename) continue;
123
124 TList* list = ((TFile*)file)->GetListOfKeys() ;
125 if (list) {
126 TIter next(list) ;
127 TKey* key ;
128 TObject* obj ;
129
130 while ((key = (TKey*)next())) {
131 TString skey(key->GetClassName());
132 if (skey.BeginsWith(TString("Belle2::"))) continue;
133 TClass clkey(key->GetClassName());
134 if (clkey.InheritsFrom("TH1")) {
135 obj = key->ReadObj() ;
136 B2INFO("Histo name: " << obj->GetName() << " title " << obj->GetTitle());
137 TDirectory* old, *d;
138 d = old = gDirectory;
139 TString myl = obj->GetName();
140 TString tok;
141 Ssiz_t from = 0;
142 while (myl.Tokenize(tok, from, "/")) {
143 TString dummy;
144 Ssiz_t fr;
145 fr = from;
146 if (myl.Tokenize(dummy, fr,
147 "/")) { // check if its the last one
148 auto e = d->GetDirectory(tok);
149 if (e) {
150 d = e;
151 d->cd();
152 } else {
153 d->mkdir(tok);
154 d->cd(tok);
155 d = gDirectory;
156 }
157 } else {
158 break;
159 }
160 }
161 ((TH1*)obj)->SetName(tok);
162 obj->Write();
163 old->cd();
164 }
165 }
166 }
167 } else {
168 B2INFO("Others name: " << file->GetName() << " title " << file->GetTitle());
169 }
170 }
171 }
172 }
173
174 f.Write();
175 f.Close();
176 }
177}
DQMHistAnalysisModule()
Constructor / Destructor.
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:559
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition Module.h:649
Abstract base class for different kinds of events.
STL namespace.