Belle II Software  release-06-02-00
DQMHistAnalysis.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 : DQMHistAnalysisModule.cc
10 // Description : Baseclass for DQM histogram analysis module
11 //-
12 
13 #include <dqm/analysis/modules/DQMHistAnalysis.h>
14 #include <TROOT.h>
15 #include <TClass.h>
16 
17 
18 
19 using namespace std;
20 using namespace Belle2;
21 
22 //-----------------------------------------------------------------
23 // Register the Module
24 //-----------------------------------------------------------------
25 REG_MODULE(DQMHistAnalysis)
26 
27 //-----------------------------------------------------------------
28 // Implementation
29 //-----------------------------------------------------------------
30 
31 DQMHistAnalysisModule::HistList DQMHistAnalysisModule::g_hist;
32 DQMHistAnalysisModule::MonObjList DQMHistAnalysisModule::g_monObj;
33 
34 DQMHistAnalysisModule::DQMHistAnalysisModule() : Module()
35 {
36  //Set module properties
37  setDescription("Histgram Analysis module");
38 }
39 
40 
41 DQMHistAnalysisModule::~DQMHistAnalysisModule()
42 {
43 
44 }
45 
46 void DQMHistAnalysisModule::addHist(const std::string& dirname, const std::string& histname, TH1* h)
47 {
48  if (dirname.size() > 0) {
49  g_hist.insert(HistList::value_type(dirname + "/" + histname, h));
50  } else {
51  g_hist.insert(HistList::value_type(histname, h));
52  }
53 }
54 
56 {
57  if (g_monObj.find(objName) != g_monObj.end()) {
58  if (g_monObj[objName]) {
59  return g_monObj[objName];
60  } else {
61  B2WARNING("MonitoringObject " << objName << " listed as being in memfile but points to nowhere. New Object will be made.");
62  g_monObj.erase(objName);
63  }
64  }
65 
66  MonitoringObject* obj = new MonitoringObject(objName);
67  g_monObj.insert(MonObjList::value_type(objName, obj));
68  return obj;
69 }
70 
71 TCanvas* DQMHistAnalysisModule::findCanvas(TString canvas_name)
72 {
73  TIter nextkey(gROOT->GetListOfCanvases());
74  TObject* obj{};
75 
76  while ((obj = dynamic_cast<TObject*>(nextkey()))) {
77  if (obj->IsA()->InheritsFrom("TCanvas")) {
78  if (obj->GetName() == canvas_name)
79  return dynamic_cast<TCanvas*>(obj);
80  }
81  }
82  return nullptr;
83 }
84 
85 TH1* DQMHistAnalysisModule::findHist(const std::string& histname)
86 {
87  if (g_hist.find(histname) != g_hist.end()) {
88  if (g_hist[histname]) {
89  //Want to search elsewhere if null-pointer saved in map
90  return g_hist[histname];
91  } else {
92  B2ERROR("Histogram " << histname << " listed as being in memfile but points to nowhere.");
93  }
94  }
95  B2INFO("Histogram " << histname << " not in memfile.");
96 
97  //Histogram not in list, search in memory for it
98  gROOT->cd();
99 
100  //Following the path to the histogram
101  TDirectory* d = gROOT;
102  TString myl = histname;
103  TString tok;
104  Ssiz_t from = 0;
105  while (myl.Tokenize(tok, from, "/")) {
106  TString dummy;
107  Ssiz_t f;
108  f = from;
109  if (myl.Tokenize(dummy, f, "/")) { // check if its the last one
110  auto e = d->GetDirectory(tok);
111  if (e) {
112  B2INFO("Cd Dir " << tok);
113  d = e;
114  }
115  d->cd();
116  } else {
117  break;
118  }
119  }
120 
121  // This code assumes that the histograms address does NOT change between initialization and any later event
122  // This assumption seems to be reasonable for TFiles and in-memory objects
123  // BUT this means => Analysis moules MUST NEVER create a histogram with already existing name NOR delete any histogram
124  TH1* found_hist = findHist(d, tok);
125  if (found_hist) {
126  g_hist[histname] = found_hist;//Can't use addHist as we want to overwrite invalid entries
127  }
128  return found_hist;
129 
130 }
131 
132 TH1* DQMHistAnalysisModule::findHist(const std::string& dirname, const std::string& histname)
133 {
134  if (dirname.size() > 0) {
135  return findHist(dirname + "/" + histname);
136  }
137  return findHist(histname);
138 }
139 
140 TH1* DQMHistAnalysisModule::findHist(const TDirectory* histdir, const TString& histname)
141 {
142  TObject* obj = histdir->FindObject(histname);
143  if (obj != NULL) {
144  if (obj->IsA()->InheritsFrom("TH1")) {
145  B2INFO("Histogram " << histname << " found in mem");
146  return (TH1*)obj;
147  }
148  } else {
149  B2INFO("Histogram " << histname << " NOT found in mem");
150  }
151  return NULL;
152 }
153 
155 {
156  if (g_monObj.find(objName) != g_monObj.end()) {
157  if (g_monObj[objName]) {
158  //Want to search elsewhere if null-pointer saved in map
159  return g_monObj[objName];
160  } else {
161  B2ERROR("MonitoringObject " << objName << " listed as being in memfile but points to nowhere.");
162  }
163  }
164  B2INFO("MonitoringObject " << objName << " not in memfile.");
165  return NULL;
166 }
TCanvas * findCanvas(TString cname)
Find canvas by name.
static MonitoringObject * findMonitoringObject(const std::string &objName)
Find MonitoringObject.
static void addHist(const std::string &dirname, const std::string &histname, TH1 *h)
Add histogram.
static HistList g_hist
The list of Histograms.
static TH1 * findHist(const std::string &histname)
Find histogram.
std::map< std::string, MonitoringObject * > MonObjList
The type of list of MonitoringObjects.
static MonObjList g_monObj
The list of MonitoringObjects.
static MonitoringObject * getMonitoringObject(const std::string &histname)
Get MonitoringObject with given name (new object is created if non-existing)
std::map< std::string, TH1 * > HistList
The type of list of histograms.
Base class for Modules.
Definition: Module.h:72
void setDescription(const std::string &description)
Sets the description of the module.
Definition: Module.cc:214
MonitoringObject is a basic object to hold data for the run-dependency monitoring Run summary TCanvas...
#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.