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 Belle2;
18
19//-----------------------------------------------------------------
20// Register the Module
21//-----------------------------------------------------------------
22
23REG_MODULE(DQMHistAnalysisOutputFile);
24
25//-----------------------------------------------------------------
26// Implementation
27//-----------------------------------------------------------------
28
29
32{
33
34 setDescription("Module to save canvas/histograms from analysis to a single root file");
35 // This module CAN NOT be run in parallel!
36
37 //Parameter definition
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
68{
69 std::stringstream ss;
70 ss << m_folder << "/";
71 if (m_filename != "") ss << m_filename;
72 else {
73 int exp = 0;
74 int run = 0;
76 exp = m_eventMetaDataPtr->getExperiment();
77 run = m_eventMetaDataPtr->getRun();
78 }
79 ss << m_prefix << "dqm_canvas_e";
80 ss << std::setfill('0') << std::setw(4) << exp;
81 ss << "r" << std::setfill('0') << std::setw(6) << run;
82 ss << ".root";
83 }
84
85 TFile f(ss.str().data(), "recreate");
86
87 if (f.IsOpen()) {
88 if (m_saveCanvases) {
89 TSeqCollection* seq;
90 seq = gROOT->GetListOfCanvases() ;
91 if (seq) {
92 B2INFO("found canvases");
93 TIter next(seq) ;
94 TObject* obj ;
95 while ((obj = next())) {
96 if (obj->InheritsFrom("TCanvas")) {
97 B2DEBUG(1, "Saving canvas " << obj->GetName());
98 obj->Write();
99 }
100 }
101 }
102 }
103
104 if (m_saveHistos) {
105 TSeqCollection* files;
106 files = gROOT->GetListOfFiles() ;
107 if (files) {
108 B2INFO("found keys");
109 TIter nextfile(files) ;
110 TObject* file ;
111
112 while ((file = dynamic_cast<TObject*>(nextfile()))) {
113 if (file->InheritsFrom("TFile")) {
114 B2INFO("File name: " << file->GetName() << " title " << file->GetTitle());
115 if (file == &f || file->GetName() == m_filename) continue;
116
117 TList* list = (static_cast<TFile*>(file))->GetListOfKeys() ;
118 if (list) {
119 TIter next(list) ;
120 TKey* key ;
121 TObject* obj ;
122
123 while ((key = dynamic_cast<TKey*>(next()))) {
124 TString skey(key->GetClassName());
125 if (skey.BeginsWith(TString("Belle2::"))) continue;
126 TClass clkey(key->GetClassName());
127 if (clkey.InheritsFrom("TH1")) {
128 obj = key->ReadObj() ;
129 B2INFO("Histo name: " << obj->GetName() << " title " << obj->GetTitle());
130 TDirectory* old, *d;
131 d = old = gDirectory;
132 TString myl = obj->GetName();
133 TString tok;
134 Ssiz_t from = 0;
135 while (myl.Tokenize(tok, from, "/")) {
136 TString dummy;
137 Ssiz_t fr;
138 fr = from;
139 if (myl.Tokenize(dummy, fr,
140 "/")) { // check if its the last one
141 auto e = d->GetDirectory(tok);
142 if (e) {
143 d = e;
144 d->cd();
145 } else {
146 d->mkdir(tok);
147 d->cd(tok);
148 d = gDirectory;
149 }
150 } else {
151 break;
152 }
153 }
154 auto obj2 = dynamic_cast<TH1*>(obj); // actually inheritance checked above, so it should be a TH1
155 if (obj2) {
156 obj2->SetName(tok);
157 obj2->Write();
158 }
159 old->cd();
160 }
161 }
162 }
163 } else {
164 B2INFO("Others name: " << file->GetName() << " title " << file->GetTitle());
165 }
166 }
167 }
168 }
169
170 f.Write();
171 f.Close();
172 }
173}
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 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.