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