File indexing completed on 2025-01-18 10:11:01
0001 #pragma once
0002
0003 #include "TCanvas.h"
0004 #include "TSystem.h"
0005 #include "TStyle.h"
0006 #include "TH1.h"
0007 #include "TH1F.h"
0008 #include "TH2F.h"
0009
0010
0011
0012 #include <iostream> // for std::cout
0013 #include <map>
0014 #include <string>
0015
0016 namespace TMVA
0017 {
0018 class Monitoring
0019 {
0020
0021 public:
0022
0023
0024
0025
0026 Monitoring ()
0027 : fCanvas (nullptr)
0028 {
0029 }
0030
0031 ~Monitoring ()
0032 {
0033 delete fCanvas;
0034
0035 }
0036
0037 void Start ()
0038 {
0039
0040
0041
0042
0043
0044
0045 std::cout << " new tcanvas" << std::endl;
0046 fCanvas = new TCanvas ("TMVA Monitoring", "Monitoring", 1000, 500);
0047 std::cout << " draw" << std::endl;
0048 fCanvas->Draw ();
0049 std::cout << " update" << std::endl;
0050 GetCanvas ()->Update();
0051 std::cout << " process events" << std::endl;
0052 gSystem->ProcessEvents();
0053 std::cout << " run app" << std::endl;
0054
0055 std::cout << " run app executed" << std::endl;
0056
0057 gStyle->SetOptStat (0);
0058 }
0059
0060
0061 void ProcessEvents ()
0062 {
0063 GetCanvas ()->Modified();
0064 GetCanvas ()->Update();
0065 gSystem->ProcessEvents();
0066 }
0067
0068 TCanvas* GetCanvas () { return fCanvas; }
0069
0070 void pads (int numPads);
0071 void create (std::string histoName, int bins, double min, double max);
0072 void create (std::string histoName, int bins, double min, double max, int bins2, double min2, double max2);
0073 void addPoint (std::string histoName, double x);
0074 void addPoint (std::string histoName, double x, double y);
0075 void plot (std::string histoName, std::string options = "L", int pad = 0, EColor color = kBlue);
0076 void clear (std::string histoName);
0077 bool exists (std::string histoName);
0078 bool exists (TH1F* dummy, std::string histoName);
0079 bool exists (TH2F* dummy, std::string histoName);
0080
0081 protected:
0082
0083 TH1F* getHistogram (const TH1F* dummy, std::string histoName, int bins = 0, double min = 0, double max = 0);
0084 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);
0085
0086
0087 private:
0088 TCanvas* fCanvas;
0089
0090
0091
0092
0093 std::map<std::string, TH1F*> m_histos1D;
0094 std::map<std::string, TH2F*> m_histos2D;
0095 };
0096
0097
0098
0099 inline bool Monitoring::exists (TH1F* , std::string histoName)
0100 {
0101 auto it = m_histos1D.find (histoName);
0102 if (it != m_histos1D.end ())
0103 return true;
0104 return false;
0105 }
0106
0107 inline bool Monitoring::exists (TH2F* , std::string histoName)
0108 {
0109 auto it2 = m_histos2D.find (histoName);
0110 if (it2 != m_histos2D.end ())
0111 return true;
0112 return false;
0113 }
0114
0115
0116 inline bool Monitoring::exists (std::string histoName)
0117 {
0118 TH1F* dummy1D = nullptr;
0119 TH2F* dummy2D = nullptr;
0120 return exists (dummy1D, histoName) || exists (dummy2D, histoName);
0121 }
0122
0123 inline void Monitoring::pads (int numPads)
0124 {
0125 TCanvas* canvas = GetCanvas ();
0126 canvas->Clear ();
0127 std::cout << "divide canvas " << canvas << " into " << numPads << "numPads" << std::endl;
0128 GetCanvas ()->DivideSquare (numPads);
0129 }
0130
0131
0132 inline void Monitoring::create (std::string histoName, int bins, double min, double max)
0133 {
0134 TH1F* dummy = nullptr;
0135 getHistogram (dummy, histoName, bins, min, max);
0136 }
0137
0138 inline void Monitoring::create (std::string histoName, int bins, double min, double max, int bins2, double min2, double max2)
0139 {
0140 TH2F* dummy = nullptr;
0141 getHistogram (dummy, histoName, bins, min, max, bins2, min2, max2);
0142 }
0143
0144
0145
0146 inline TH1F* Monitoring::getHistogram (const TH1F* , std::string histoName, int bins, double min, double max)
0147 {
0148 auto it = m_histos1D.find (histoName);
0149 if (it != m_histos1D.end ())
0150 return it->second;
0151 std::cout << "new 1D histogram " << histoName << std::endl;
0152 TH1F* histogram = m_histos1D.insert (std::make_pair (histoName, new TH1F (histoName.c_str (), histoName.c_str (), bins, min, max))).first->second;
0153
0154 return histogram;
0155 }
0156
0157 inline TH2F* Monitoring::getHistogram (const TH2F* , std::string histoName, int bins, double min, double max, int bins2, double min2, double max2)
0158 {
0159
0160 auto it = m_histos2D.find (histoName);
0161 if (it != m_histos2D.end ())
0162 return it->second;
0163 std::cout << "new 2D histogram " << histoName << std::endl;
0164 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;
0165
0166 return histogram;
0167 }
0168
0169 inline void Monitoring::addPoint (std::string histoName, double x)
0170 {
0171 TH1F* dummy = nullptr;
0172 TH1F* hist = getHistogram (dummy, histoName, 100, 0, 1);
0173 hist->Fill (x);
0174 }
0175
0176 inline void Monitoring::addPoint (std::string histoName, double x, double y)
0177 {
0178 TH2F* dummy = nullptr;
0179 TH2F* hist = getHistogram (dummy, histoName, 100, 0, 1, 100, 0, 1);
0180 hist->Fill (x, y);
0181 }
0182
0183 inline void Monitoring::clear (std::string histoName)
0184 {
0185
0186 if (!exists (histoName))
0187 return;
0188
0189
0190 TH1F* hist1D = nullptr;
0191 TH2F* hist2D = nullptr;
0192 if (exists (hist1D, histoName))
0193 {
0194 hist1D = getHistogram (hist1D, histoName, 100, 0,1);
0195 hist1D->Reset ();
0196 return;
0197 }
0198
0199 if (exists (hist2D, histoName))
0200 {
0201 hist2D = getHistogram (hist2D, histoName, 100, 0,1,100,0,1);
0202 hist2D->Reset ();
0203 }
0204 }
0205
0206
0207 inline void Monitoring::plot (std::string histoName, std::string options, int pad, EColor color)
0208 {
0209 TCanvas* canvas = GetCanvas ();
0210 canvas->cd (pad);
0211 auto it1D = m_histos1D.find (histoName);
0212 if (it1D != m_histos1D.end ())
0213 {
0214 TH1F* dummy = nullptr;
0215 TH1F* histogram = getHistogram (dummy, histoName);
0216
0217 histogram->SetLineColor (color);
0218 histogram->SetMarkerColor (color);
0219
0220 histogram->Draw (options.c_str ());
0221 canvas->Modified ();
0222 canvas->Update ();
0223 return;
0224 }
0225 auto it2D = m_histos2D.find (histoName);
0226 if (it2D != m_histos2D.end ())
0227 {
0228 TH2F* dummy = nullptr;
0229 TH2F* histogram = getHistogram (dummy, histoName);
0230
0231 histogram->SetLineColor (color);
0232 histogram->SetMarkerColor (color);
0233
0234 histogram->Draw (options.c_str ());
0235 canvas->Modified ();
0236 canvas->Update ();
0237 }
0238 }
0239
0240
0241
0242 }