Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
compareanapp.cxx
Go to the documentation of this file.
1#include "TMVA/compareanapp.h"
2#include "TMVA/Types.h"
3#include "TH2.h"
4
5
6#define CheckDerivedPlots 0
7//TString DerivedPlotName = "Proba";
9
10void TMVA::compareanapp( TString finAn , TString finApp ,
11 HistType htype , bool useTMVAStyle )
12{
13 cout << "=== Compare histograms of two files ===" << endl;
14 cout << " File-1: " << finAn << endl;
15 cout << " File-2: " << finApp << endl;
16
17 // set style and remove existing canvas'
18 TMVAGlob::Initialize( useTMVAStyle );
19
20 // switches
21 const Bool_t Draw_CFANN_Logy = kFALSE;
22 const Bool_t Save_Images = kTRUE;
23
24 TFile* file = TMVAGlob::OpenFile( finAn );
25 TFile* fileApp = new TFile( finApp );
26 file->cd();
27
28 // define Canvas layout here!
29 const Int_t width = 600; // size of canvas
30
31 // counter variables
32 Int_t countCanvas = 0;
33
34 TList methods;
35 TIter next(&methods);
36 TKey *key;
37 while ( (key = (TKey*)next()) ) {
38
39 TString dirname = ((TDirectory*)key->ReadObj())->GetName();
40 if (dirname.Contains( "Cuts" )) {
41 cout << "--- Found directory: " << dirname << " --> ignoring" << endl;
42 continue;
43 }
44 cout << "--- Found directory: " << dirname
45 << " --> going in" << endl;
46
47 TString methodName;
48 TMVAGlob::GetMethodName(methodName,key);
49
50 cout << "--- Method: " << methodName << endl;
51
52 //TDirectory* mDir = (TDirectory*)
53 key->ReadObj();
54 TList titles;
55 // UInt_t ninst = TMVAGlob::GetListOfTitles(mDir,titles);
56 TIter nextTitle(&titles);
57 TKey *titkey;
58 TDirectory *titDir;
59 while ( (titkey = TMVAGlob::NextKey(nextTitle,"TDirectory")) ) {
60
61 titDir = (TDirectory *)titkey->ReadObj();
62 TString methodTitle;
63 TMVAGlob::GetMethodTitle(methodTitle,titDir);
64 TString hname = "MVA_" + methodTitle;
65 if (CheckDerivedPlots) hname += TString("_") + DerivedPlotName;
66
67 TH1* sig = dynamic_cast<TH1*>(titDir->Get( hname + "_S" ));
68 TH1* bgd = dynamic_cast<TH1*>(titDir->Get( hname + "_B" ));
69
70 if (sig==0 || bgd==0) continue;
71
72 // chop off useless stuff
73 sig->SetTitle( TString::Format("TMVA output for classifier: %s", methodTitle.Data()) );
74 if (htype == kProbaType)
75 sig->SetTitle( TString::Format("TMVA probability for classifier: %s", methodTitle.Data()) );
76 else if (htype == kRarityType)
77 sig->SetTitle( TString::Format("TMVA Rarity for classifier: %s", methodTitle.Data()) );
78
79 // create new canvas
80 TString ctitle = ((htype == TMVA::kMVAType) ?
81 TString::Format("TMVA output %s",methodTitle.Data()) :
82 (htype == kProbaType) ?
83 TString::Format("TMVA probability %s",methodTitle.Data()) :
84 TString::Format("TMVA rarity %s",methodTitle.Data()));
85
86 TString cname = ((htype == TMVA::kMVAType) ?
87 TString::Format("output_%s",methodTitle.Data()) :
88 (htype == kProbaType) ?
89 TString::Format("probability_%s",methodTitle.Data()) :
90 TString::Format("rarity_%s",methodTitle.Data()));
91
92 TCanvas* c = new TCanvas( TString::Format("canvas%d", countCanvas+1), ctitle,
93 countCanvas*50+200, countCanvas*20, width, width*0.78 );
94
95 // set the histogram style
97
98 // normalise both signal and background
99 TMVAGlob::NormalizeHists( sig, bgd );
100
101 // frame limits (choose judicuous x range)
102 Float_t nrms = 4;
103 Float_t xmin = TMath::Max( TMath::Min(sig->GetMean() - nrms*sig->GetRMS(),
104 bgd->GetMean() - nrms*bgd->GetRMS() ),
105 sig->GetXaxis()->GetXmin() );
106 Float_t xmax = TMath::Min( TMath::Max(sig->GetMean() + nrms*sig->GetRMS(),
107 bgd->GetMean() + nrms*bgd->GetRMS() ),
108 sig->GetXaxis()->GetXmax() );
109 Float_t ymin = 0;
110 Float_t ymax = TMath::Max( sig->GetMaximum(), bgd->GetMaximum() )*1.2 ;
111
112 if (Draw_CFANN_Logy && methodName == "CFANN") ymin = 0.01;
113
114 // build a frame
115 Int_t nb = 500;
116 TH2F* frame = new TH2F( TString("frame") + methodTitle, sig->GetTitle(),
117 nb, xmin, xmax, nb, ymin, ymax );
118 frame->GetXaxis()->SetTitle(methodTitle);
119 if (htype == kProbaType ) frame->GetXaxis()->SetTitle( "Signal probability" );
120 else if (htype == kRarityType) frame->GetXaxis()->SetTitle( "Signal rarity" );
121 frame->GetYaxis()->SetTitle("Normalized");
123
124 // eventually: draw the frame
125 frame->Draw();
126
127 c->GetPad(0)->SetLeftMargin( 0.105 );
128 frame->GetYaxis()->SetTitleOffset( 1.2 );
129
130 if (Draw_CFANN_Logy && methodName == "CFANN") c->SetLogy();
131
132 // Draw legend
133 TLegend *legend= new TLegend( c->GetLeftMargin(), 1 - c->GetTopMargin() - 0.12,
134 c->GetLeftMargin() + 0.3, 1 - c->GetTopMargin() );
135 legend->SetFillStyle( 1 );
136 legend->AddEntry(sig,"Signal","F");
137 legend->AddEntry(bgd,"Background","F");
138 legend->SetBorderSize(1);
139 legend->SetMargin( 0.3 );
140 legend->Draw("same");
141
142 // overlay signal and background histograms
143 sig->Draw("samehist");
144 bgd->Draw("samehist");
145
146 // retrieve corresponding histogram from TMVApp.root
147 TString hStem(hname);
148 cout << "--- Searching for histogram: " << hStem.Data() << " in application file" << endl;
149
150 TH1* testHist = (TH1*)fileApp->Get( hStem );
151 if (testHist != 0) {
152 cout << "--> Found application histogram: " << testHist->GetName() << " --> superimpose it" << endl;
153 // compute normalisation factor
154 TMVAGlob::NormalizeHists( testHist );
155 testHist->SetLineWidth( 3 );
156 testHist->SetLineColor( 1 );
157 testHist->Draw("samehist");
158 }
159
160 // redraw axes
161 frame->Draw("sameaxis");
162
163 // text for overflows
164 Int_t nbin = sig->GetNbinsX();
165 Double_t dxu = sig->GetBinWidth(0);
166 Double_t dxo = sig->GetBinWidth(nbin+1);
167 TString uoflow = TString::Format( "U/O-flow (S,B): (%.1f, %.1f)%% / (%.1f, %.1f)%%",
168 sig->GetBinContent(0)*dxu*100, bgd->GetBinContent(0)*dxu*100,
169 sig->GetBinContent(nbin+1)*dxo*100, bgd->GetBinContent(nbin+1)*dxo*100 );
170 TText* t = new TText( 0.975, 0.115, uoflow );
171 t->SetNDC();
172 t->SetTextSize( 0.030 );
173 t->SetTextAngle( 90 );
174 t->AppendPad();
175
176 // save canvas to file
177 c->Update();
179 if (Save_Images) {
180 if (htype == TMVA::kMVAType) TMVAGlob::imgconv( c, TString::Format("plots/mva_%s", methodTitle.Data()) );
181 else if (htype == TMVA::kProbaType) TMVAGlob::imgconv( c, TString::Format("plots/proba_%s", methodTitle.Data()) );
182 else TMVAGlob::imgconv( c, TString::Format("plots/rarity_%s", methodTitle.Data()) );
183 }
184 countCanvas++;
185 }
186 }
187}
#define c(i)
Definition RSha256.hxx:101
bool Bool_t
Definition RtypesCore.h:63
int Int_t
Definition RtypesCore.h:45
float Float_t
Definition RtypesCore.h:57
constexpr Bool_t kFALSE
Definition RtypesCore.h:101
double Double_t
Definition RtypesCore.h:59
constexpr Bool_t kTRUE
Definition RtypesCore.h:100
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char cname
Option_t Option_t width
float xmin
float ymin
float xmax
float ymax
virtual void SetTitleOffset(Float_t offset=1)
Set distance between the axis and the axis title.
Definition TAttAxis.cxx:298
virtual void SetFillStyle(Style_t fstyle)
Set the fill area style.
Definition TAttFill.h:39
virtual void SetLineWidth(Width_t lwidth)
Set the line width.
Definition TAttLine.h:43
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition TAttLine.h:40
virtual void SetTextAngle(Float_t tangle=0)
Set the text angle.
Definition TAttText.h:43
virtual void SetTextSize(Float_t tsize=1)
Set the text size.
Definition TAttText.h:47
Double_t GetXmax() const
Definition TAxis.h:140
Double_t GetXmin() const
Definition TAxis.h:139
The Canvas class.
Definition TCanvas.h:23
Bool_t cd() override
Change current directory to "this" directory.
TObject * Get(const char *namecycle) override
Return pointer to object identified by namecycle.
Describe directory structure in memory.
Definition TDirectory.h:45
virtual TObject * Get(const char *namecycle)
Return pointer to object identified by namecycle.
A ROOT file is an on-disk file, usually with extension .root, that stores objects in a file-system-li...
Definition TFile.h:53
TH1 is the base class of all histogram classes in ROOT.
Definition TH1.h:59
void SetTitle(const char *title) override
Change/set the title.
Definition TH1.cxx:6686
virtual Double_t GetMean(Int_t axis=1) const
For axis = 1,2 or 3 returns the mean value of the histogram along X,Y or Z axis.
Definition TH1.cxx:7503
TAxis * GetXaxis()
Definition TH1.h:324
virtual Double_t GetMaximum(Double_t maxval=FLT_MAX) const
Return maximum value smaller than maxval of bins in the range, unless the value has been overridden b...
Definition TH1.cxx:8513
virtual Int_t GetNbinsX() const
Definition TH1.h:297
TAxis * GetYaxis()
Definition TH1.h:325
void Draw(Option_t *option="") override
Draw this histogram with options.
Definition TH1.cxx:3066
Double_t GetRMS(Int_t axis=1) const
This function returns the Standard Deviation (Sigma) of the distribution not the Root Mean Square (RM...
Definition TH1.h:319
virtual Double_t GetBinContent(Int_t bin) const
Return content of bin number bin.
Definition TH1.cxx:5029
virtual Double_t GetBinWidth(Int_t bin) const
Return bin width for 1D histogram.
Definition TH1.cxx:9131
2-D histogram with a float per channel (see TH1 documentation)
Definition TH2.h:307
Book space in a file, create I/O buffers, to fill them, (un)compress them.
Definition TKey.h:28
virtual TObject * ReadObj()
To read a TObject* from the file.
Definition TKey.cxx:758
This class displays a legend box (TPaveText) containing several legend entries.
Definition TLegend.h:23
TLegendEntry * AddEntry(const TObject *obj, const char *label="", Option_t *option="lpf")
Add a new entry to this legend.
Definition TLegend.cxx:317
void Draw(Option_t *option="") override
Draw this legend with its current attributes.
Definition TLegend.cxx:422
void SetMargin(Float_t margin)
Definition TLegend.h:69
A doubly linked list.
Definition TList.h:38
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
Definition TNamed.cxx:164
const char * GetName() const override
Returns name of object.
Definition TNamed.h:47
const char * GetTitle() const override
Returns title of object.
Definition TNamed.h:48
virtual void AppendPad(Option_t *option="")
Append graphics object to current pad.
Definition TObject.cxx:184
virtual void SetBorderSize(Int_t bordersize=4)
Sets the border size of the TPave box and shadow.
Definition TPave.h:77
Basic string class.
Definition TString.h:139
const char * Data() const
Definition TString.h:376
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString.
Definition TString.cxx:2378
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition TString.h:632
Base class for several text objects.
Definition TText.h:22
virtual void SetNDC(Bool_t isNDC=kTRUE)
Set NDC mode on if isNDC = kTRUE, off otherwise.
Definition TText.cxx:823
TString DerivedPlotName
#define CheckDerivedPlots
Definition compareanapp.h:8
void Initialize(Bool_t useTMVAStyle=kTRUE)
Definition tmvaglob.cxx:176
TKey * NextKey(TIter &keyIter, TString className)
Definition tmvaglob.cxx:364
void GetMethodTitle(TString &name, TKey *ikey)
Definition tmvaglob.cxx:348
void plot_logo(Float_t v_scale=1.0, Float_t skew=1.0)
Definition tmvaglob.cxx:270
void SetSignalAndBackgroundStyle(TH1 *sig, TH1 *bkg, TH1 *all=nullptr)
Definition tmvaglob.cxx:8
TFile * OpenFile(const TString &fin)
Definition tmvaglob.cxx:192
void SetFrameStyle(TH1 *frame, Float_t scale=1.0)
Definition tmvaglob.cxx:77
void NormalizeHists(TH1 *sig, TH1 *bkg=nullptr)
Definition tmvaglob.cxx:324
void GetMethodName(TString &name, TKey *mkey)
Definition tmvaglob.cxx:342
void imgconv(TCanvas *c, const TString &fname)
Definition tmvaglob.cxx:212
void compareanapp(TString finAn="TMVA.root", TString finApp="TMVApp.root", HistType htype=kMVAType, bool useTMVAStyle=kTRUE)
Short_t Max(Short_t a, Short_t b)
Returns the largest of a and b.
Definition TMathBase.h:250
Short_t Min(Short_t a, Short_t b)
Returns the smallest of a and b.
Definition TMathBase.h:198