Belle II Software  release-06-00-14
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 : DQM histgram 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::ParamTypeList DQMHistAnalysisModule::g_parname;
32 DQMHistAnalysisModule::IntValueList DQMHistAnalysisModule::g_vint;
33 DQMHistAnalysisModule::FloatValueList DQMHistAnalysisModule::g_vfloat;
34 DQMHistAnalysisModule::TextList DQMHistAnalysisModule::g_text;
35 DQMHistAnalysisModule::HistList DQMHistAnalysisModule::g_hist;
36 DQMHistAnalysisModule::MonObjList DQMHistAnalysisModule::g_monObj;
37 
38 DQMHistAnalysisModule::DQMHistAnalysisModule() : Module()
39 {
40  //Set module properties
41  setDescription("Histgram Analysis module");
42 }
43 
44 
45 DQMHistAnalysisModule::~DQMHistAnalysisModule()
46 {
47 
48 }
49 
50 TCanvas* DQMHistAnalysisModule::find_canvas(TString canvas_name)
51 {
52  TIter nextkey(gROOT->GetListOfCanvases());
53  TObject* obj{};
54 
55  while ((obj = dynamic_cast<TObject*>(nextkey()))) {
56  if (obj->IsA()->InheritsFrom("TCanvas")) {
57  if (obj->GetName() == canvas_name)
58  return dynamic_cast<TCanvas*>(obj);
59  }
60  }
61  return nullptr;
62 }
63 
64 void DQMHistAnalysisModule::addHist(const std::string& dirname, const std::string& histname, TH1* h)
65 {
66  if (dirname.size() > 0) {
67  g_hist.insert(HistList::value_type(dirname + "/" + histname, h));
68  } else {
69  g_hist.insert(HistList::value_type(histname, h));
70  }
71 }
72 
74 {
75  if (g_monObj.find(objName) != g_monObj.end()) {
76  if (g_monObj[objName]) {
77  return g_monObj[objName];
78  } else {
79  B2WARNING("MonitoringObject " << objName << " listed as being in memfile but points to nowhere. New Object will be made.");
80  g_monObj.erase(objName);
81  }
82  }
83 
84  MonitoringObject* obj = new MonitoringObject(objName);
85  g_monObj.insert(MonObjList::value_type(objName, obj));
86  return obj;
87 }
88 
89 
91 {
92  return g_hist;
93 }
94 
96 {
97  return g_monObj;
98 }
99 
100 
101 TH1* DQMHistAnalysisModule::findHist(const std::string& histname)
102 {
103  if (g_hist.find(histname) != g_hist.end()) {
104  if (g_hist[histname]) {
105  //Want to search elsewhere if null-pointer saved in map
106  return g_hist[histname];
107  } else {
108  B2ERROR("Histogram " << histname << " listed as being in memfile but points to nowhere.");
109  }
110  }
111  B2INFO("Histogram " << histname << " not in memfile.");
112 
113  //Histogram not in list, search in memory for it
114  gROOT->cd();
115 
116  //Following the path to the histogram
117  TDirectory* d = gROOT;
118  TString myl = histname;
119  TString tok;
120  Ssiz_t from = 0;
121  while (myl.Tokenize(tok, from, "/")) {
122  TString dummy;
123  Ssiz_t f;
124  f = from;
125  if (myl.Tokenize(dummy, f, "/")) { // check if its the last one
126  auto e = d->GetDirectory(tok);
127  if (e) {
128  B2INFO("Cd Dir " << tok);
129  d = e;
130  }
131  d->cd();
132  } else {
133  break;
134  }
135  }
136 
137  // This code assumes that the histograms address does NOT change between initialization and any later event
138  // This assumption seems to be reasonable for TFiles and in-memory objects
139  // BUT this means => Analysis moules MUST NEVER create a histogram with already existing name NOR delete any histogram
140  TH1* found_hist = findHist(d, tok);
141  if (found_hist) {
142  g_hist[histname] = found_hist;//Can't use addHist as we want to overwrite invalid entries
143  }
144  return found_hist;
145 
146 }
147 
148 TH1* DQMHistAnalysisModule::findHist(const std::string& dirname, const std::string& histname)
149 {
150  if (dirname.size() > 0) {
151  return findHist(dirname + "/" + histname);
152  }
153  return findHist(histname);
154 }
155 
156 
157 TH1* DQMHistAnalysisModule::findHist(const TDirectory* histdir, const TString& histname)
158 {
159  TObject* obj = histdir->FindObject(histname);
160  if (obj != NULL) {
161  if (obj->IsA()->InheritsFrom("TH1")) {
162  B2INFO("Histogram " << histname << " found in mem");
163  return (TH1*)obj;
164  }
165  } else {
166  B2INFO("Histogram " << histname << " NOT found in mem");
167  }
168  return NULL;
169 }
170 
172 {
173  if (g_monObj.find(objName) != g_monObj.end()) {
174  if (g_monObj[objName]) {
175  //Want to search elsewhere if null-pointer saved in map
176  return g_monObj[objName];
177  } else {
178  B2ERROR("MonitoringObject " << objName << " listed as being in memfile but points to nowhere.");
179  }
180  }
181  B2INFO("MonitoringObject " << objName << " not in memfile.");
182  return NULL;
183 }
184 
185 
186 
187 void DQMHistAnalysisModule::setIntValue(const std::string& parname, int vint)
188 {
189  if (g_parname.find(parname) == g_parname.end() && g_vint.find(parname) == g_vint.end()) {
190  g_parname.insert(ParamTypeList::value_type(parname, c_ParamINT));
191  g_vint.insert(IntValueList::value_type(parname, vint));
192  } else if (g_vint.find(parname) == g_vint.end()) {
193  B2ERROR(parname + " is already registered as non-int data type");
194  } else {
195  g_vint[parname] = vint;
196  }
197 }
198 
199 void DQMHistAnalysisModule::setFloatValue(const std::string& parname, float vfloat)
200 {
201  if (g_parname.find(parname) == g_parname.end() && g_vfloat.find(parname) == g_vfloat.end()) {
202  g_parname.insert(ParamTypeList::value_type(parname, c_ParamFLOAT));
203  g_vfloat.insert(FloatValueList::value_type(parname, vfloat));
204  } else if (g_vfloat.find(parname) == g_vfloat.end()) {
205  B2ERROR(parname + " is already registered as non-float data type");
206  } else {
207  g_vfloat[parname] = vfloat;
208  }
209 }
210 
211 void DQMHistAnalysisModule::setText(const std::string& parname, const std::string& text)
212 {
213  if (g_parname.find(parname) == g_parname.end() && g_text.find(parname) == g_text.end()) {
214  g_parname.insert(ParamTypeList::value_type(parname, c_ParamTEXT));
215  g_text.insert(TextList::value_type(parname, text));
216  } else if (g_text.find(parname) == g_text.end()) {
217  B2ERROR(parname + " is already registered as non-text data type");
218  } else {
219  g_text[parname] = text;
220  }
221 }
222 
static void setText(const std::string &parname, const std::string &text)
Set the string value of the parameter.
static const HistList & getHistList()
Get the list of the histograms.
TCanvas * find_canvas(TString cname)
Find canvas by name.
static void setIntValue(const std::string &parname, int vint)
Set the integer value of the parameter.
std::map< std::string, int > IntValueList
The type of list of integer module parameter.
static MonitoringObject * findMonitoringObject(const std::string &objName)
Find MonitoringObject.
static void addHist(const std::string &dirname, const std::string &histname, TH1 *h)
Add histogram.
std::map< std::string, float > FloatValueList
The type of list of float module parameter.
static ParamTypeList g_parname
The list of module parameter types.
static HistList g_hist
The list of histograms.
static TH1 * findHist(const std::string &histname)
Find histogram.
std::map< std::string, EParamType > ParamTypeList
The type of list of module parameter types.
std::map< std::string, MonitoringObject * > MonObjList
The type of list of MonitoringObjects.
static const MonObjList & getMonObjList()
Get the list of MonitoringObjects.
static TextList g_text
The list of string module parameter.
static IntValueList g_vint
The list of integer module parameter.
static MonObjList g_monObj
The list of MonitoringObjects.
static void setFloatValue(const std::string &parname, float vfloat)
Set the float value of the parameter.
static MonitoringObject * getMonitoringObject(const std::string &histname)
Get MonitoringObject with given name (new object is created if non-existing)
static FloatValueList g_vfloat
The list of float module parameter.
std::map< std::string, std::string > TextList
The type of list of string module parameter.
std::map< std::string, TH1 * > HistList
The type of list of histograms.
@ c_ParamTEXT
The string type for module parameter.
@ c_ParamINT
The integer type for module parameter.
@ c_ParamFLOAT
The float type for module parameter.
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.