Belle II Software  release-08-01-10
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 // File : DQMHistAnalysisOutputFile.cc
10 // Description : DQM Analysis, dump histograms to file (as reference histograms)
11 //-
12 
13 
14 #include <dqm/analysis/modules/DQMHistAnalysisOutputFile.h>
15 #include <TROOT.h>
16 #include <TObject.h>
17 #include "TKey.h"
18 #include "TFile.h"
19 
20 using namespace std;
21 using namespace Belle2;
22 
23 //-----------------------------------------------------------------
24 // Register the Module
25 //-----------------------------------------------------------------
26 REG_MODULE(DQMHistAnalysisOutputFile);
27 
28 //-----------------------------------------------------------------
29 // Implementation
30 //-----------------------------------------------------------------
31 
32 DQMHistAnalysisOutputFileModule::DQMHistAnalysisOutputFileModule()
34 {
35  setDescription("Module to save histograms from DQMHistAnalysisModules");
36 
37  //Parameter definition
38  addParam("HistoFile", m_filename, "Output Histogram Filename", std::string("histo.root"));
39  addParam("histogramDirectoryName", m_histogramDirectoryName, "Name of the directory where histograms will be placed",
40  std::string("ref"));
41  addParam("SaveHistos", m_saveHistos, "Save Histos (default)", true);
42  addParam("SaveCanvases", m_saveCanvases, "Save Canvases (not default)", false);
43  B2DEBUG(20, "DQMHistAnalysisOutputFile: Constructor done.");
44 }
45 
46 
48 
50 {
51  B2DEBUG(20, "DQMHistAnalysisOutputFile: initialized.");
52 }
53 
54 
56 {
57  B2DEBUG(20, "DQMHistAnalysisOutputFile: beginRun called.");
58 }
59 
60 
62 {
63  B2DEBUG(20, "DQMHistAnalysisOutputFile: event called.");
64 }
65 
67 {
68  B2INFO("DQMHistAnalysisOutputFile: endRun called");
69 
70  // Attention, we can not do that in Terminate, as then the memFile is already closed by previous task!
71  B2INFO("open file");
72  TFile f(m_filename.data(), "recreate");
73  if (f.IsOpen()) {
74  TDirectory* oldDir = gDirectory;
75  oldDir->mkdir(m_histogramDirectoryName.c_str());
76  oldDir->cd(m_histogramDirectoryName.c_str());
77 
78  if (m_saveCanvases) {
79  // we could loop over histos ... but i think the second one is better
80  TSeqCollection* seq;
81  seq = gROOT->GetListOfCanvases() ;
82  if (seq) {
83  B2INFO("found canvases");
84  TIter next(seq) ;
85  TObject* obj ;
86 
87  while ((obj = (TObject*)next())) {
88  if (obj->InheritsFrom("TCanvas")) {
89  B2INFO("Canvas name: " << obj->GetName() << " title " << obj->GetTitle());
90  obj->Write();
91  } else {
92  B2INFO("Others name: " << obj->GetName() << " title " << obj->GetTitle());
93  }
94  }
95  }
96  }
97  if (m_saveHistos) {
98  TSeqCollection* files;
99  files = gROOT->GetListOfFiles() ;
100  if (files) {
101  B2INFO("found keys");
102  TIter nextfile(files) ;
103  TObject* file ;
104 
105  while ((file = (TObject*)nextfile())) {
106  if (file->InheritsFrom("TFile")) {
107  B2INFO("File name: " << file->GetName() << " title " << file->GetTitle());
108  if (file == &f || file->GetName() == m_filename) continue;
109 
110  TList* list = ((TFile*)file)->GetListOfKeys() ;
111  if (list) {
112  TIter next(list) ;
113  TKey* key ;
114  TObject* obj ;
115 
116  while ((key = (TKey*)next())) {
117  TString skey(key->GetClassName());
118  if (skey.BeginsWith(TString("Belle2::"))) continue;
119  TClass clkey(key->GetClassName());
120  if (clkey.InheritsFrom("TH1")) {
121  obj = key->ReadObj() ;
122  B2INFO("Histo name: " << obj->GetName() << " title " << obj->GetTitle());
123  TDirectory* old, *d;
124  d = old = gDirectory;
125  TString myl = obj->GetName();
126  TString tok;
127 // workaround for changes in histmanager/Input module ... TODO remove if not needed anymore
128 // std::string a=myl.Data();
129 // a.erase(std::remove(a.begin(), a.end(), ':'), a.end());
130 // myl=a.data();
131  Ssiz_t from = 0;
132  while (myl.Tokenize(tok, from, "/")) {
133  TString dummy;
134  Ssiz_t fr;
135  fr = from;
136  if (myl.Tokenize(dummy, fr, "/")) { // check if its the last one
137  auto e = d->GetDirectory(tok);
138  if (e) {
139  d = e;
140  d->cd();
141  } else {
142  d->mkdir(tok);
143  d->cd(tok);
144  d = gDirectory;
145  }
146  } else {
147  break;
148  }
149  }
150  ((TH1*)obj)->SetName(tok);
151  obj->Write();
152  old->cd();
153  }
154  }
155  }
156  } else {
157  B2INFO("Others name: " << file->GetName() << " title " << file->GetTitle());
158  }
159  }
160  }
161  }
162 
163 
164  f.Write();
165  f.Close();
166  }
167 }
168 
169 
171 {
172  B2INFO("DQMHistAnalysisOutputFile: terminate called");
173 // Attention, we can not do that in Terminate, as then the memFile is already closed by previous task!
174 }
175 
The base class for the histogram analysis module.
bool m_saveCanvases
Write all Canvases to file.
void initialize() override final
Initializer.
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_histogramDirectoryName
Directory name within root file where to place things.
void endRun() override final
This method is called if the current run ends.
std::string m_filename
file name of root file
void beginRun() override final
Called when entering a new 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.