Belle II Software  release-08-01-10
DQMHistAutoCanvas.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 : DQMHistAutoCanvas.cc
10 // Description : This module auto-plots each histogram in its canvas
11 //-
12 
13 #include <dqm/analysis/modules/DQMHistAutoCanvas.h>
14 
15 using namespace std;
16 using namespace Belle2;
17 
18 //-----------------------------------------------------------------
19 // Register the Module
20 //-----------------------------------------------------------------
21 REG_MODULE(DQMHistAutoCanvas);
22 
23 //-----------------------------------------------------------------
24 // Implementation
25 //-----------------------------------------------------------------
26 
27 DQMHistAutoCanvasModule::DQMHistAutoCanvasModule()
29 {
30  //Parameter definition
31  addParam("AutoCanvasFolders", m_acfolders, "List of histograms to automatically create canvases, empty for all",
32  std::vector<std::string>());
33  addParam("ExcludeFolders", m_exclfolders, "List of folders to exclude from create canvases, empty for none, \"all\" for all",
34  std::vector<std::string>());
35  B2DEBUG(1, "DQMHistAutoCanvas: Constructor done.");
36 }
37 
39 {
40  // There may be a more clever way instead of doing the iteration my myself here
41  // This only works because the getHistList is not const anymore
42  for (auto& it : getHistList()) {
43  bool give_canvas = false;
44  TString histoname = it.first;
45 
46  // the following lines prevent any histogram outside a directory to be processed
47  auto split_result = StringSplit(histoname.Data(), '/');
48  if (split_result.size() <= 1) continue;
49  auto dirname = split_result.at(0); // extract dirname, get hist name is in histogram itself
50  if (m_exclfolders.size() == 0) { //If none specified, canvases for all histograms
51  give_canvas = true;
52  } else {
53  bool in_excl_folder = false;
54  if (m_exclfolders.size() == 1 && m_exclfolders[0] == "all") {
55  in_excl_folder = true;
56  } else {
57  for (auto& excl_folder : m_exclfolders) {
58  if (excl_folder == dirname) {
59  in_excl_folder = true;
60  break;
61  }
62  }
63  }
64 
65  if (in_excl_folder) {
66  for (auto& wanted_folder : m_acfolders) {
67  B2DEBUG(1, "==" << wanted_folder << "==" << dirname << "==");
68  if (wanted_folder == std::string(histoname)) {
69  give_canvas = true;
70  break;
71  }
72  }
73  } else {
74  give_canvas = true;
75  }
76  }
77 
78  if (give_canvas) {
79  B2DEBUG(1, "Auto Hist->Canvas for " << histoname);
80  histoname.ReplaceAll("/", "_");
81  std::string name = histoname.Data();
82  if (m_cs.find(name) == m_cs.end()) {
83  // no canvas exists yet, create one
84  if (split_result.size() > 1) {
85  std::string hname = split_result.at(1);
86  if ((dirname + "/" + hname) == "softwaretrigger/skim") hname = "skim_hlt";
87  TCanvas* c = new TCanvas((dirname + "/c_" + hname).c_str(), ("c_" + hname).c_str());
88  m_cs.insert(std::pair<std::string, TCanvas*>(name, c));
89  } else {
90  // but this case is explicity excluded above?
91  std::string hname = histoname.Data();
92  TCanvas* c = new TCanvas(("c_" + hname).c_str(), ("c_" + hname).c_str());
93  m_cs.insert(std::pair<std::string, TCanvas*>(name, c));
94  }
95  }
96  TCanvas* c = m_cs[name]; // access already created canvas
97  B2DEBUG(1, "DQMHistAnalysisInput: new canvas " << c->GetName());
98  c->cd();
99 
100  // not so nice as we actually touch the histogram by iterator
101  // we could use findHist function, but then we do another lookup within iteration
102  auto hist = it.second.getHist();
103  if (hist) {
104  if (hist->GetDimension() == 1) {
105  // assume users are expecting non-0-suppressed axis
106  if (hist->GetMinimum() > 0) hist->SetMinimum(0);
107  hist->Draw("hist");
108  } else if (hist->GetDimension() == 2) {
109  // ... but not in 2d
110  hist->Draw("colz");
111  }
112  }
113 
114  // set Canvas "name" update flag if histo was updated
115  // UpdateCanvas(name, findHist(histoname.Data()) != nullptr);
116  // we already have the HistObject, so we can directly get it
117  UpdateCanvas(name, it.second.isUpdated());
118 
119  // Mark Canvas as repaint needed, but is this needed?
120  c->Update();
121  }
122  }
123 }
The base class for the histogram analysis module.
std::vector< std::string > StringSplit(const std::string &s, const char delim)
Helper function for string token split.
static HistList & getHistList()
Get the list of the histograms.
void UpdateCanvas(std::string name, bool updated=true)
Mark canvas as updated (or not)
void event() override final
This method is called for each event.
std::map< std::string, TCanvas * > m_cs
The map of histogram names to canvas pointers for output.
std::vector< std::string > m_acfolders
The list of folders for which automatically generate canvases.
std::vector< std::string > m_exclfolders
The list of folders which are excluded from automatically generate canvases.
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.