ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/LJMet/MultivariateAnalysis/macros/mvas.C
Revision: 1.1
Committed: Thu Nov 20 22:34:50 2008 UTC (16 years, 5 months ago) by kukartse
Content type: text/plain
Branch: MAIN
CVS Tags: V00-01-12, V00-01-11, V00-01-10, gak031009, gak030509, gak022309, gak021209, gak040209, gak012809, V00-01-09, V00-01-08, V00-01-07, V00-01-06, V00-01-05, V00-01-04, V00-00-07, V00-00-06, V00-00-05, V00-00-04, V00-01-03, V00-00-02, V00-00-01
Log Message:
created /macros with TMVA scripts in it

File Contents

# User Rev Content
1 kukartse 1.1 #include "tmvaglob.C"
2    
3     // this macro plots the resulting MVA distributions (Signal and
4     // Background overlayed) of different MVA methods run in TMVA
5     // (e.g. running TMVAnalysis.C).
6    
7     enum HistType { MVAType = 0, ProbaType = 1, RarityType = 2 };
8    
9     // input: - Input file (result from TMVA)
10     // - use of TMVA plotting TStyle
11     void mvas( TString fin = "TMVA.root", HistType htype = MVAType, Bool_t useTMVAStyle = kTRUE )
12     {
13     // set style and remove existing canvas'
14     TMVAGlob::Initialize( useTMVAStyle );
15    
16     // switches
17     const Bool_t Draw_CFANN_Logy = kFALSE;
18     const Bool_t Save_Images = kTRUE;
19    
20     // checks if file with name "fin" is already open, and if not opens one
21     TFile* file = TMVAGlob::OpenFile( fin );
22    
23     cout << "--- plotting type: " << htype << endl;
24    
25     // define Canvas layout here!
26     Int_t xPad = 1; // no of plots in x
27     Int_t yPad = 1; // no of plots in y
28     Int_t noPad = xPad * yPad ;
29     const Int_t width = 600; // size of canvas
30    
31     // this defines how many canvases we need
32     TCanvas *c = 0;
33    
34     // counter variables
35     Int_t countCanvas = 0;
36    
37     // search for the right histograms in full list of keys
38     TList methods;
39     UInt_t nm = TMVAGlob::GetListOfMethods( methods );
40     TIter next(&methods);
41     TKey *key, *hkey;
42     while ((key = (TKey*)next())) {
43    
44     cout << "--- Found directory: " << ((TDirectory*)key->ReadObj())->GetName()
45     << " --> going in" << endl;
46    
47     TString methodName;
48     TMVAGlob::GetMethodName(methodName,key);
49    
50     cout << "--- Method: " << methodName << endl;
51    
52     TDirectory* mDir = (TDirectory*)key->ReadObj();
53     TList titles;
54     UInt_t ninst = TMVAGlob::GetListOfTitles(mDir,titles);
55     TIter nextTitle(&titles);
56     TKey *titkey;
57     TDirectory *titDir;
58     while ((titkey = TMVAGlob::NextKey(nextTitle,"TDirectory"))) {
59    
60     titDir = (TDirectory *)titkey->ReadObj();
61     TString methodTitle;
62     TMVAGlob::GetMethodTitle(methodTitle,titDir);
63     TString hname = "MVA_" + methodTitle;
64     if (htype == ProbaType ) hname += "_Proba";
65     else if (htype == RarityType) hname += "_Rarity";
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( Form("TMVA output for classifier: %s", methodTitle.Data()) );
74     if (htype == ProbaType)
75     sig->SetTitle( Form("TMVA probability for classifier: %s", methodTitle.Data()) );
76     else if (htype == RarityType)
77     sig->SetTitle( Form("TMVA Rarity for classifier: %s", methodTitle.Data()) );
78    
79     // create new canvas
80     TString ctitle = ((htype == MVAType) ?
81     Form("TMVA output %s",methodTitle.Data()) :
82     (htype == ProbaType) ?
83     Form("TMVA probability %s",methodTitle.Data()) :
84     Form("TMVA rarity %s",methodTitle.Data()));
85    
86     TString cname = ((htype == MVAType) ?
87     Form("output_%s",methodTitle.Data()) :
88     (htype == ProbaType) ?
89     Form("probability_%s",methodTitle.Data()) :
90     Form("rarity_%s",methodTitle.Data()));
91    
92     c = new TCanvas( Form("canvas%d", countCanvas+1), ctitle,
93     countCanvas*50+200, countCanvas*20, width, width*0.78 );
94    
95     // set the histogram style
96     TMVAGlob::SetSignalAndBackgroundStyle( sig, bgd );
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     cout << "--- mean and RMS (S): " << sig->GetMean() << ", " << sig->GetRMS() << endl;
104     cout << "--- mean and RMS (B): " << bgd->GetMean() << ", " << bgd->GetRMS() << endl;
105     Float_t xmin = TMath::Max( TMath::Min(sig->GetMean() - nrms*sig->GetRMS(),
106     bgd->GetMean() - nrms*bgd->GetRMS() ),
107     sig->GetXaxis()->GetXmin() );
108     Float_t xmax = TMath::Min( TMath::Max(sig->GetMean() + nrms*sig->GetRMS(),
109     bgd->GetMean() + nrms*bgd->GetRMS() ),
110     sig->GetXaxis()->GetXmax() );
111     Float_t ymin = 0;
112     Float_t ymax = TMath::Max( sig->GetMaximum(), bgd->GetMaximum() )*1.2 ;
113    
114     if (Draw_CFANN_Logy && mvaName[imva] == "CFANN") ymin = 0.01;
115    
116     // build a frame
117     Int_t nb = 500;
118     TH2F* frame = new TH2F( TString("frame") + methodTitle, sig->GetTitle(),
119     nb, xmin, xmax, nb, ymin, ymax );
120     frame->GetXaxis()->SetTitle(methodTitle);
121     if (htype == ProbaType ) frame->GetXaxis()->SetTitle( "Signal probability" );
122     else if (htype == RarityType) frame->GetXaxis()->SetTitle( "Signal rarity" );
123     frame->GetYaxis()->SetTitle("Normalized");
124     TMVAGlob::SetFrameStyle( frame );
125    
126     // eventually: draw the frame
127     frame->Draw();
128    
129     c->GetPad(0)->SetLeftMargin( 0.105 );
130     frame->GetYaxis()->SetTitleOffset( 1.2 );
131    
132     if (Draw_CFANN_Logy && mvaName[imva] == "CFANN") c->SetLogy();
133    
134     // Draw legend
135     TLegend *legend= new TLegend( c->GetLeftMargin(), 1 - c->GetTopMargin() - 0.12,
136     c->GetLeftMargin() + 0.3, 1 - c->GetTopMargin() );
137     legend->SetFillStyle( 1 );
138     legend->AddEntry(sig,"Signal","F");
139     legend->AddEntry(bgd,"Background","F");
140     legend->SetBorderSize(1);
141     legend->SetMargin( 0.3 );
142     legend->Draw("same");
143    
144     // overlay signal and background histograms
145     // DEBUG signal color
146     sig->SetLineColor(4);
147     sig->Draw("samehist");
148     bgd->Draw("samehist");
149    
150     // redraw axes
151     frame->Draw("sameaxis");
152    
153     // text for overflows
154     Int_t nbin = sig->GetNbinsX();
155     TString uoflow = Form( "U/O-flow (S,B): (%.1f, %.1f)% / (%.1f, %.1f)%",
156     sig->GetBinContent(0)*100, bgd->GetBinContent(0)*100,
157     sig->GetBinContent(nbin+1)*100, bgd->GetBinContent(nbin+1)*100 );
158     TText* t = new TText( 0.975, 0.115, uoflow );
159     t->SetNDC();
160     t->SetTextSize( 0.030 );
161     t->SetTextAngle( 90 );
162     t->AppendPad();
163    
164     // save canvas to file
165     c->Update();
166     TMVAGlob::plot_logo();
167     if (Save_Images) {
168     if (htype == MVAType) TMVAGlob::imgconv( c, Form("plots/mva_%s", methodTitle.Data()) );
169     else if (htype == ProbaType) TMVAGlob::imgconv( c, Form("plots/proba_%s", methodTitle.Data()) );
170     else TMVAGlob::imgconv( c, Form("plots/rarity_%s", methodTitle.Data()) );
171     }
172     countCanvas++;
173     }
174     }
175     }
176