Logo ROOT  
Reference Guide
deviations.cxx
Go to the documentation of this file.
1#include "TMVA/deviations.h"
2#include "TMVA/Types.h"
3#include "TH2.h"
4#include "TLine.h"
5#include "TObjString.h"
6
7
8// this macro plots the resulting MVA distributions (Signal and
9// Background overlayed) of different MVA methods run in TMVA
10// (e.g. running TMVAnalysis.C).
11
12
13// input: - Input file (result from TMVA)
14// - use of TMVA plotting TStyle
15void TMVA::deviations(TString dataset, TString fin,
16 HistType htype , Bool_t showTarget, Bool_t useTMVAStyle )
17{
18 // set style and remove existing canvas'
19 TMVAGlob::Initialize( useTMVAStyle );
21
22 // checks if file with name "fin" is already open, and if not opens one
23 TFile* file = TMVAGlob::OpenFile( fin );
24
25 // define Canvas layout here!
26 const Int_t width = 650; // size of canvas
27
28 // this defines how many canvases we need
29 TCanvas* c[100];
30
31 // counter variables
32 Int_t countCanvas = 0;
33
34 // search for the right histograms in full list of keys
35 // TList* methods = new TMap();
36
37 TIter next(file->GetDirectory(dataset.Data())->GetListOfKeys());
38 TKey *key(0);
39 while ((key = (TKey*)next())) {
40
41 if (!TString(key->GetName()).BeginsWith("Method_")) continue;
42 if (!gROOT->GetClass(key->GetClassName())->InheritsFrom("TDirectory")) continue;
43
44 TString methodName;
45 TMVAGlob::GetMethodName(methodName,key);
46 cout << "--- Plotting deviation for method: " << methodName << endl;
47
48 TDirectory* mDir = (TDirectory*)key->ReadObj();
49
50 TList* jobNames = new TList();
51
52 TIter keyIt(mDir->GetListOfKeys());
53 TKey *titkey;
54 while ((titkey = (TKey*)keyIt())) {
55
56 if (!gROOT->GetClass(titkey->GetClassName())->InheritsFrom("TDirectory")) continue;
57
58 TDirectory *titDir = (TDirectory *)titkey->ReadObj();
59
60 TObjString *jN = new TObjString( titDir->GetName() );
61 if (!jobNames->Contains( jN )) jobNames->Add( jN );
62 else delete jN;
63
64 TString methodTitle;
65 TMVAGlob::GetMethodTitle(methodTitle,titDir);
66
67 TString hname = "MVA_" + methodTitle;
68 TIter dirKeyIt( titDir->GetListOfKeys() );
69 TKey* dirKey;
70
71 Int_t countPlots = 0;
72 while ((dirKey = (TKey*)dirKeyIt())){
73 if (dirKey->ReadObj()->InheritsFrom("TH2F")) {
74 TString s(dirKey->ReadObj()->GetName());
75 if (s.Contains("_reg_") &&
76 ( (showTarget && s.Contains("_tgt")) || (!showTarget && !s.Contains("_tgt")) ) &&
77 s.Contains( (htype == kCompareType ? "train" : "test" ))) {
78 c[countCanvas] = new TCanvas( Form("canvas%d", countCanvas+1),
79 Form( "Regression output deviation versus %s for method: %s",
80 (showTarget ? "target" : "input variables"), methodName.Data() ),
81 countCanvas*50+100, (countCanvas+1)*20, width, (Int_t)width*0.72 );
82 c[countCanvas]->SetRightMargin(0.10); // leave space for border
83 TH1* h = (TH1*)dirKey->ReadObj();
84 h->SetTitle( Form("Output deviation for method: %s (%s sample)",
85 hname.Data(), (htype == kCompareType ? "training" : "test" )) );
86 // methodName.Data(), (htype == kCompareType ? "training" : "test" )) );
87 h->Draw("colz");
88 TLine* l = new TLine( h->GetXaxis()->GetXmin(), 0, h->GetXaxis()->GetXmax(), 0 );
89 l->SetLineStyle(2);
90 l->Draw();
91
92 // update and print
93 cout << "plotting logo" << endl;
95 c[countCanvas]->Update();
96
97 TString fname = Form( "%s/plots/deviation_%s_%s_%s_c%i",
98 dataset.Data(),
99 methodName.Data(),
100 (showTarget ? "target" : "vars"),
101 (htype == kCompareType ? "training" : "test" ), countPlots );
102 TMVAGlob::imgconv( c[countCanvas], fname );
103
104 countPlots++;
105 countCanvas++;
106 }
107 }
108 }
109 }
110 }
111}
112
113
114
115
#define c(i)
Definition: RSha256.hxx:101
#define h(i)
Definition: RSha256.hxx:106
bool Bool_t
Definition: RtypesCore.h:63
int Int_t
Definition: RtypesCore.h:45
Option_t Option_t width
#define gROOT
Definition: TROOT.h:404
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
Definition: TString.cxx:2447
R__EXTERN TStyle * gStyle
Definition: TStyle.h:414
virtual void SetLineStyle(Style_t lstyle)
Set the line style.
Definition: TAttLine.h:42
The Canvas class.
Definition: TCanvas.h:23
Bool_t Contains(const char *name) const
Definition: TCollection.h:171
Describe directory structure in memory.
Definition: TDirectory.h:45
virtual TList * GetListOfKeys() const
Definition: TDirectory.h:214
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format.
Definition: TFile.h:54
TH1 is the base class of all histogram classes in ROOT.
Definition: TH1.h:58
Book space in a file, create I/O buffers, to fill them, (un)compress them.
Definition: TKey.h:28
virtual const char * GetClassName() const
Definition: TKey.h:75
virtual TObject * ReadObj()
To read a TObject* from the file.
Definition: TKey.cxx:750
Use the TLine constructor to create a simple line.
Definition: TLine.h:22
A doubly linked list.
Definition: TList.h:38
void Add(TObject *obj) override
Definition: TList.h:81
@ kCompareType
Definition: Types.h:66
const char * GetName() const override
Returns name of object.
Definition: TNamed.h:47
Collectable string class.
Definition: TObjString.h:28
virtual const char * GetName() const
Returns name of object.
Definition: TObject.cxx:359
virtual Bool_t InheritsFrom(const char *classname) const
Returns kTRUE if object inherits from class "classname".
Definition: TObject.cxx:445
virtual void Draw(Option_t *option="")
Default Draw method for all objects.
Definition: TObject.cxx:197
Basic string class.
Definition: TString.h:136
const char * Data() const
Definition: TString.h:369
Bool_t BeginsWith(const char *s, ECaseCompare cmp=kExact) const
Definition: TString.h:615
void SetNumberContours(Int_t number=20)
Set the default number of contour levels when drawing 2-d plots.
Definition: TStyle.cxx:1449
static constexpr double s
void Initialize(Bool_t useTMVAStyle=kTRUE)
Definition: tmvaglob.cxx:176
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
TFile * OpenFile(const TString &fin)
Definition: tmvaglob.cxx:192
void GetMethodName(TString &name, TKey *mkey)
Definition: tmvaglob.cxx:342
void imgconv(TCanvas *c, const TString &fname)
Definition: tmvaglob.cxx:212
void deviations(TString dataset, TString fin="TMVAReg.root", HistType htype=kMVAType, Bool_t showTarget=kTRUE, Bool_t useTMVAStyle=kTRUE)
Definition: file.py:1
TLine l
Definition: textangle.C:4