Logo ROOT  
Reference Guide
Monitoring.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "TCanvas.h"
4 #include "TSystem.h"
5 #include "TStyle.h"
6 #include "TH1.h"
7 #include "TH1F.h"
8 #include "TH2F.h"
9 
10 // FIXME: We should not rely on std::cout but on the ROOT printing facilities or
11 // MsgLogger!
12 #include <iostream> // for std::cout
13 #include <map>
14 #include <string>
15 
16 namespace TMVA
17 {
18  class Monitoring
19  {
20 
21  public:
22  /* Monitoring (int argc, char* /\*argv[]*\/) */
23  /* { */
24  /* } */
25 
27  : fCanvas (NULL)
28  {
29  }
30 
32  {
33  delete fCanvas;
34  // delete fApplication;
35  }
36 
37  void Start ()
38  {
39  /* std::cout << "start monitoring" << std::endl; */
40  /* std::cout << " new tapp " << std::endl; */
41  /* fApplication = new TApplication ("TMVA Monitoring", 0, 0); */
42  /* std::cout << " set return from run" << std::endl; */
43  /* // fApplication->SetReturnFromRun (true); */
44 
45  std::cout << " new tcanvas" << std::endl;
46  fCanvas = new TCanvas ("TMVA Monitoring", "Monitoring", 1000, 500);
47  std::cout << " draw" << std::endl;
48  fCanvas->Draw ();
49  std::cout << " update" << std::endl;
50  GetCanvas ()->Update();
51  std::cout << " process events" << std::endl;
52  gSystem->ProcessEvents(); //canvas can be edited during the loop
53  std::cout << " run app" << std::endl;
54  // fApplication->Run ();
55  std::cout << " run app executed" << std::endl;
56 
57  gStyle->SetOptStat (0);
58  }
59 
60 
61  void ProcessEvents ()
62  {
63  GetCanvas ()->Modified();
64  GetCanvas ()->Update();
65  gSystem->ProcessEvents(); //canvas can be edited during the loop
66  }
67 
68  TCanvas* GetCanvas () { return fCanvas; }
69 
70  void pads (int numPads);
71  void create (std::string histoName, int bins, double min, double max);
72  void create (std::string histoName, int bins, double min, double max, int bins2, double min2, double max2);
73  void addPoint (std::string histoName, double x);
74  void addPoint (std::string histoName, double x, double y);
75  void plot (std::string histoName, std::string options = "L", int pad = 0, EColor color = kBlue);
76  void clear (std::string histoName);
77  bool exists (std::string histoName);
78  bool exists (TH1F* dummy, std::string histoName);
79  bool exists (TH2F* dummy, std::string histoName);
80 
81  protected:
82 
83  TH1F* getHistogram (const TH1F* dummy, std::string histoName, int bins = 0, double min = 0, double max = 0);
84  TH2F* getHistogram (const TH2F* dummy, std::string histoName, int bins = 0, double min = 0, double max = 0, int bins2 = 0, double min2 = 0, double max2 = 0);
85 
86 
87  private:
89 
90  // TApplication* fApplication;
91 
92 
93  std::map<std::string, TH1F*> m_histos1D;
94  std::map<std::string, TH2F*> m_histos2D;
95  };
96 
97 
98 
99  inline bool Monitoring::exists (TH1F* /*dummy*/, std::string histoName)
100  {
101  auto it = m_histos1D.find (histoName);
102  if (it != m_histos1D.end ())
103  return true;
104  return false;
105  }
106 
107  inline bool Monitoring::exists (TH2F* /*dummy*/, std::string histoName)
108  {
109  auto it2 = m_histos2D.find (histoName);
110  if (it2 != m_histos2D.end ())
111  return true;
112  return false;
113  }
114 
115 
116  inline bool Monitoring::exists (std::string histoName)
117  {
118  TH1F* dummy1D (NULL);
119  TH2F* dummy2D (NULL);
120  return exists (dummy1D, histoName) || exists (dummy2D, histoName);
121  }
122 
123  inline void Monitoring::pads (int numPads)
124  {
125  TCanvas* canvas = GetCanvas ();
126  canvas->Clear ();
127  std::cout << "divide canvas " << canvas << " into " << numPads << "numPads" << std::endl;
128  GetCanvas ()->DivideSquare (numPads);
129  }
130 
131 
132  inline void Monitoring::create (std::string histoName, int bins, double min, double max)
133  {
134  TH1F* dummy (NULL);
135  getHistogram (dummy, histoName, bins, min, max);
136  }
137 
138  inline void Monitoring::create (std::string histoName, int bins, double min, double max, int bins2, double min2, double max2)
139  {
140  TH2F* dummy (NULL);
141  getHistogram (dummy, histoName, bins, min, max, bins2, min2, max2);
142  }
143 
144 
145 
146  inline TH1F* Monitoring::getHistogram (const TH1F* /*dummy*/, std::string histoName, int bins, double min, double max)
147  {
148  auto it = m_histos1D.find (histoName);
149  if (it != m_histos1D.end ())
150  return it->second;
151  std::cout << "new 1D histogram " << histoName << std::endl;
152  TH1F* histogram = m_histos1D.insert (std::make_pair (histoName, new TH1F (histoName.c_str (), histoName.c_str (), bins, min, max))).first->second;
153  // int numPads = m_histos1D.size () + m_histos2D.size ();
154  return histogram;
155  }
156 
157  inline TH2F* Monitoring::getHistogram (const TH2F* /*dummy*/, std::string histoName, int bins, double min, double max, int bins2, double min2, double max2)
158  {
159  // 2D histogram
160  auto it = m_histos2D.find (histoName);
161  if (it != m_histos2D.end ())
162  return it->second;
163  std::cout << "new 2D histogram " << histoName << std::endl;
164  TH2F* histogram = m_histos2D.insert (std::make_pair (histoName, new TH2F (histoName.c_str (), histoName.c_str (), bins, min, max, bins2, min2, max2))).first->second;
165  // int numPads = m_histos1D.size () + m_histos2D.size ();
166  return histogram;
167  }
168 
169  inline void Monitoring::addPoint (std::string histoName, double x)
170  {
171  TH1F* dummy (NULL);
172  TH1F* hist = getHistogram (dummy, histoName, 100, 0, 1);
173  hist->Fill (x);
174  }
175 
176  inline void Monitoring::addPoint (std::string histoName, double x, double y)
177  {
178  TH2F* dummy (NULL);
179  TH2F* hist = getHistogram (dummy, histoName, 100, 0, 1, 100, 0, 1);
180  hist->Fill (x, y);
181  }
182 
183  inline void Monitoring::clear (std::string histoName)
184  {
185  // std::cout << "clear histo " << histoName << std::endl;
186  if (!exists (histoName))
187  return;
188 
189  // std::cout << "clear histo which exists " << histoName << std::endl;
190  TH1F* hist1D (NULL);
191  TH2F* hist2D (NULL);
192  if (exists (hist1D, histoName))
193  {
194  hist1D = getHistogram (hist1D, histoName, 100, 0,1);
195  hist1D->Reset ();
196  return;
197  }
198 
199  if (exists (hist2D, histoName))
200  {
201  hist2D = getHistogram (hist2D, histoName, 100, 0,1,100,0,1);
202  hist2D->Reset ();
203  }
204  }
205 
206 
207  inline void Monitoring::plot (std::string histoName, std::string options, int pad, EColor color)
208  {
209  TCanvas* canvas = GetCanvas ();
210  canvas->cd (pad);
211  auto it1D = m_histos1D.find (histoName);
212  if (it1D != m_histos1D.end ())
213  {
214  TH1F* dummy (NULL);
215  TH1F* histogram = getHistogram (dummy, histoName);
216  // histogram->SetBit (TH1::kCanRebin);
217  histogram->SetLineColor (color);
218  histogram->SetMarkerColor (color);
219  // std::cout << "draw " << histoName << " 1D on canvas " << canvas << " on pad " << pad << " with options " << options << std::endl;
220  histogram->Draw (options.c_str ());
221  canvas->Modified ();
222  canvas->Update ();
223  return;
224  }
225  auto it2D = m_histos2D.find (histoName);
226  if (it2D != m_histos2D.end ())
227  {
228  TH2F* dummy (NULL);
229  TH2F* histogram = getHistogram (dummy, histoName);
230  // histogram->SetBit (TH1::kCanRebin);
231  histogram->SetLineColor (color);
232  histogram->SetMarkerColor (color);
233  // std::cout << "draw " << histoName << " 2D on canvas " << canvas << " on pad " << pad << " with options " << options << std::endl;
234  histogram->Draw (options.c_str ());
235  canvas->Modified ();
236  canvas->Update ();
237  }
238  }
239 
240 
241 
242 } // namespace TMVA
TMVA::Monitoring::m_histos2D
std::map< std::string, TH2F * > m_histos2D
Definition: Monitoring.h:94
TH1F.h
TMVA::Monitoring::GetCanvas
TCanvas * GetCanvas()
Definition: Monitoring.h:68
TH2F
2-D histogram with a float per channel (see TH1 documentation)}
Definition: TH2.h:251
TMVA::Monitoring::addPoint
void addPoint(std::string histoName, double x)
Definition: Monitoring.h:169
TMVA::Monitoring::getHistogram
TH1F * getHistogram(const TH1F *dummy, std::string histoName, int bins=0, double min=0, double max=0)
Definition: Monitoring.h:146
TMVA::Monitoring::~Monitoring
~Monitoring()
Definition: Monitoring.h:31
TMVA::Monitoring::clear
void clear(std::string histoName)
Definition: Monitoring.h:183
TStyle.h
TMVA::Monitoring::ProcessEvents
void ProcessEvents()
Definition: Monitoring.h:61
x
Double_t x[n]
Definition: legend1.C:17
TMVA::Monitoring
Definition: Monitoring.h:18
TCanvas::Clear
void Clear(Option_t *option="")
Remove all primitives from the canvas.
Definition: TCanvas.cxx:723
TMVA::Monitoring::plot
void plot(std::string histoName, std::string options="L", int pad=0, EColor color=kBlue)
Definition: Monitoring.h:207
TCanvas.h
TMVA::Monitoring::exists
bool exists(std::string histoName)
Definition: Monitoring.h:116
TMVA::Monitoring::Start
void Start()
Definition: Monitoring.h:37
TPad::Modified
void Modified(Bool_t flag=1)
Definition: TPad.h:414
TCanvas::Draw
virtual void Draw(Option_t *option="")
Draw a canvas.
Definition: TCanvas.cxx:841
gStyle
R__EXTERN TStyle * gStyle
Definition: TStyle.h:412
TSystem.h
TCanvas::cd
TVirtualPad * cd(Int_t subpadnumber=0)
Set current canvas & pad.
Definition: TCanvas.cxx:704
TMVA::Monitoring::m_histos1D
std::map< std::string, TH1F * > m_histos1D
Definition: Monitoring.h:93
TMVA::Monitoring::fCanvas
TCanvas * fCanvas
Definition: Monitoring.h:88
TH1::Fill
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition: TH1.cxx:3274
y
Double_t y[n]
Definition: legend1.C:17
TH2F.h
TStyle::SetOptStat
void SetOptStat(Int_t stat=1)
The type of information printed in the histogram statistics box can be selected via the parameter mod...
Definition: TStyle.cxx:1592
TH2::Fill
Int_t Fill(Double_t)
Invalid Fill method.
Definition: TH2.cxx:294
gSystem
R__EXTERN TSystem * gSystem
Definition: TSystem.h:559
TPad::DivideSquare
virtual void DivideSquare(Int_t n, Float_t xmargin=0.01, Float_t ymargin=0.01, Int_t color=0)
"n" is the total number of sub-pads.
Definition: TPad.cxx:1265
TCanvas
Definition: TCanvas.h:23
dummy
static RooMathCoreReg dummy
Definition: RooMathCoreReg.cxx:27
TH1F
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:572
TCanvas::Update
virtual void Update()
Update canvas pad buffers.
Definition: TCanvas.cxx:2500
TMVA::Monitoring::create
void create(std::string histoName, int bins, double min, double max)
Definition: Monitoring.h:132
kBlue
@ kBlue
Definition: Rtypes.h:66
EColor
EColor
Definition: Rtypes.h:65
TH1F::Reset
virtual void Reset(Option_t *option="")
Reset.
Definition: TH1.cxx:9561
TMVA::Monitoring::pads
void pads(int numPads)
Definition: Monitoring.h:123
TMVA::Monitoring::Monitoring
Monitoring()
Definition: Monitoring.h:26
TH1.h
TSystem::ProcessEvents
virtual Bool_t ProcessEvents()
Process pending events (GUI, timers, sockets).
Definition: TSystem.cxx:417
TH2F::Reset
virtual void Reset(Option_t *option="")
Reset this histogram: contents, errors, etc.
Definition: TH2.cxx:3551
TMVA
create variable transformations
Definition: GeneticMinimizer.h:22
TH1::Draw
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2997