1 |
|
2 |
#include "TLegend.h"
|
3 |
#include "TText.h"
|
4 |
#include "TH2.h"
|
5 |
|
6 |
#include "tmvaglob.C"
|
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 |
enum HistType { MVAType = 0, ProbaType = 1, RarityType = 2, CompareType = 3 };
|
13 |
|
14 |
// input: - Input file (result from TMVA)
|
15 |
// - use of TMVA plotting TStyle
|
16 |
void deviations( TString fin = "TMVAReg.root",
|
17 |
HistType htype = MVAType, Bool_t showTarget, Bool_t useTMVAStyle = kTRUE )
|
18 |
{
|
19 |
// set style and remove existing canvas'
|
20 |
TMVAGlob::Initialize( useTMVAStyle );
|
21 |
gStyle->SetNumberContours(999);
|
22 |
|
23 |
// switches
|
24 |
const Bool_t Save_Images = kTRUE;
|
25 |
|
26 |
// checks if file with name "fin" is already open, and if not opens one
|
27 |
TFile* file = TMVAGlob::OpenFile( fin );
|
28 |
|
29 |
// define Canvas layout here!
|
30 |
Int_t xPad = 1; // no of plots in x
|
31 |
Int_t yPad = 1; // no of plots in y
|
32 |
Int_t noPad = xPad * yPad ;
|
33 |
const Int_t width = 650; // size of canvas
|
34 |
|
35 |
// this defines how many canvases we need
|
36 |
TCanvas* c[100];
|
37 |
|
38 |
// counter variables
|
39 |
Int_t countCanvas = 0;
|
40 |
|
41 |
// search for the right histograms in full list of keys
|
42 |
// TList* methods = new TMap();
|
43 |
|
44 |
TIter next(file->GetListOfKeys());
|
45 |
TKey *key(0);
|
46 |
while ((key = (TKey*)next())) {
|
47 |
|
48 |
if (!TString(key->GetName()).BeginsWith("Method_")) continue;
|
49 |
if (!gROOT->GetClass(key->GetClassName())->InheritsFrom("TDirectory")) continue;
|
50 |
|
51 |
TString methodName;
|
52 |
TMVAGlob::GetMethodName(methodName,key);
|
53 |
cout << "--- Plotting deviation for method: " << methodName << endl;
|
54 |
|
55 |
TObjString *mN = new TObjString( methodName );
|
56 |
TDirectory* mDir = (TDirectory*)key->ReadObj();
|
57 |
|
58 |
TList* jobNames = new TList();
|
59 |
|
60 |
TIter keyIt(mDir->GetListOfKeys());
|
61 |
TKey *titkey;
|
62 |
while ((titkey = (TKey*)keyIt())) {
|
63 |
|
64 |
if (!gROOT->GetClass(titkey->GetClassName())->InheritsFrom("TDirectory")) continue;
|
65 |
|
66 |
TDirectory *titDir = (TDirectory *)titkey->ReadObj();
|
67 |
|
68 |
TObjString *jN = new TObjString( titDir->GetName() );
|
69 |
if (!jobNames->Contains( jN )) jobNames->Add( jN );
|
70 |
else delete jN;
|
71 |
|
72 |
TString methodTitle;
|
73 |
TMVAGlob::GetMethodTitle(methodTitle,titDir);
|
74 |
|
75 |
TString hname = "MVA_" + methodTitle;
|
76 |
TIter dirKeyIt( titDir->GetListOfKeys() );
|
77 |
TKey* dirKey;
|
78 |
|
79 |
Int_t countPlots = 0;
|
80 |
while ((dirKey = (TKey*)dirKeyIt())){
|
81 |
if (dirKey->ReadObj()->InheritsFrom("TH2F")) {
|
82 |
TString s(dirKey->ReadObj()->GetName());
|
83 |
if (s.Contains("_reg_") &&
|
84 |
( (showTarget && s.Contains("_tgt")) || (!showTarget && !s.Contains("_tgt")) ) &&
|
85 |
s.Contains( (htype == CompareType ? "train" : "test" ))) {
|
86 |
c[countCanvas] = new TCanvas( Form("canvas%d", countCanvas+1),
|
87 |
Form( "Regression output deviation versus %s for method: %s",
|
88 |
(showTarget ? "target" : "input variables"), methodName.Data() ),
|
89 |
countCanvas*50+100, (countCanvas+1)*20, width, (Int_t)width*0.72 );
|
90 |
c[countCanvas]->SetRightMargin(0.10); // leave space for border
|
91 |
TH1* h = (TH1*)dirKey->ReadObj();
|
92 |
h->SetTitle( Form("Output deviation for method: %s (%s sample)",
|
93 |
hname.Data(), (htype == CompareType ? "training" : "test" )) );
|
94 |
// methodName.Data(), (htype == CompareType ? "training" : "test" )) );
|
95 |
h->Draw("colz");
|
96 |
TLine* l = new TLine( h->GetXaxis()->GetXmin(), 0, h->GetXaxis()->GetXmax(), 0 );
|
97 |
l->SetLineStyle(2);
|
98 |
l->Draw();
|
99 |
|
100 |
// update and print
|
101 |
cout << "plotting logo" << endl;
|
102 |
TMVAGlob::plot_logo(1.058);
|
103 |
c[countCanvas]->Update();
|
104 |
|
105 |
TString fname = Form( "plots/deviation_%s_%s_%s_c%i",
|
106 |
methodName.Data(),
|
107 |
(showTarget ? "target" : "vars"),
|
108 |
(htype == CompareType ? "training" : "test" ), countPlots );
|
109 |
TMVAGlob::imgconv( c[countCanvas], fname );
|
110 |
|
111 |
countPlots++;
|
112 |
countCanvas++;
|
113 |
}
|
114 |
}
|
115 |
}
|
116 |
}
|
117 |
}
|
118 |
}
|
119 |
|
120 |
|
121 |
|
122 |
|