ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/LJMet/MultivariateAnalysis/macros/mvas.C
Revision: 1.3
Committed: Sat Jul 4 13:29:08 2009 UTC (15 years, 10 months ago) by kukartse
Content type: text/plain
Branch: MAIN
CVS Tags: V00-03-01, ZMorph_BASE_20100408, gak040610_morphing, V00-02-02, gak011410, gak010310, ejterm2010_25nov2009, V00-02-01, V00-02-00, gak112409, CMSSW_22X_branch_base, segala101609, HEAD
Branch point for: ZMorph-V00-03-01, CMSSW_22X_branch
Changes since 1.2: +7 -4 lines
Log Message:
*** empty log message ***

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 kukartse 1.2 void mvas( TString fin = "TMVA.root", HistType htype = MVAType, Bool_t useTMVAStyle = kFALSE )
12 kukartse 1.1 {
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 kukartse 1.3 //sig->SetTitle( Form("TMVA output for classifier: %s", methodTitle.Data()) );
74     sig->SetTitle( "" );
75 kukartse 1.1 if (htype == ProbaType)
76     sig->SetTitle( Form("TMVA probability for classifier: %s", methodTitle.Data()) );
77     else if (htype == RarityType)
78     sig->SetTitle( Form("TMVA Rarity for classifier: %s", methodTitle.Data()) );
79    
80     // create new canvas
81     TString ctitle = ((htype == MVAType) ?
82     Form("TMVA output %s",methodTitle.Data()) :
83     (htype == ProbaType) ?
84     Form("TMVA probability %s",methodTitle.Data()) :
85     Form("TMVA rarity %s",methodTitle.Data()));
86    
87     TString cname = ((htype == MVAType) ?
88     Form("output_%s",methodTitle.Data()) :
89     (htype == ProbaType) ?
90     Form("probability_%s",methodTitle.Data()) :
91     Form("rarity_%s",methodTitle.Data()));
92    
93     c = new TCanvas( Form("canvas%d", countCanvas+1), ctitle,
94     countCanvas*50+200, countCanvas*20, width, width*0.78 );
95    
96     // set the histogram style
97     TMVAGlob::SetSignalAndBackgroundStyle( sig, bgd );
98    
99     // normalise both signal and background
100     TMVAGlob::NormalizeHists( sig, bgd );
101    
102     // frame limits (choose judicuous x range)
103     Float_t nrms = 4;
104     cout << "--- mean and RMS (S): " << sig->GetMean() << ", " << sig->GetRMS() << endl;
105     cout << "--- mean and RMS (B): " << bgd->GetMean() << ", " << bgd->GetRMS() << endl;
106     Float_t xmin = TMath::Max( TMath::Min(sig->GetMean() - nrms*sig->GetRMS(),
107     bgd->GetMean() - nrms*bgd->GetRMS() ),
108     sig->GetXaxis()->GetXmin() );
109     Float_t xmax = TMath::Min( TMath::Max(sig->GetMean() + nrms*sig->GetRMS(),
110     bgd->GetMean() + nrms*bgd->GetRMS() ),
111     sig->GetXaxis()->GetXmax() );
112     Float_t ymin = 0;
113     Float_t ymax = TMath::Max( sig->GetMaximum(), bgd->GetMaximum() )*1.2 ;
114    
115     if (Draw_CFANN_Logy && mvaName[imva] == "CFANN") ymin = 0.01;
116    
117     // build a frame
118     Int_t nb = 500;
119     TH2F* frame = new TH2F( TString("frame") + methodTitle, sig->GetTitle(),
120     nb, xmin, xmax, nb, ymin, ymax );
121 kukartse 1.3 //frame->GetXaxis()->SetTitle(methodTitle);
122     frame->GetXaxis()->SetTitle("");
123 kukartse 1.1 if (htype == ProbaType ) frame->GetXaxis()->SetTitle( "Signal probability" );
124     else if (htype == RarityType) frame->GetXaxis()->SetTitle( "Signal rarity" );
125 kukartse 1.3 //frame->GetYaxis()->SetTitle("Normalized");
126     frame->GetYaxis()->SetTitle("");
127 kukartse 1.1 TMVAGlob::SetFrameStyle( frame );
128    
129     // eventually: draw the frame
130     frame->Draw();
131    
132     c->GetPad(0)->SetLeftMargin( 0.105 );
133     frame->GetYaxis()->SetTitleOffset( 1.2 );
134    
135     if (Draw_CFANN_Logy && mvaName[imva] == "CFANN") c->SetLogy();
136    
137     // Draw legend
138     TLegend *legend= new TLegend( c->GetLeftMargin(), 1 - c->GetTopMargin() - 0.12,
139     c->GetLeftMargin() + 0.3, 1 - c->GetTopMargin() );
140     legend->SetFillStyle( 1 );
141     legend->AddEntry(sig,"Signal","F");
142     legend->AddEntry(bgd,"Background","F");
143     legend->SetBorderSize(1);
144     legend->SetMargin( 0.3 );
145     legend->Draw("same");
146    
147     // overlay signal and background histograms
148     // DEBUG signal color
149     sig->SetLineColor(4);
150     sig->Draw("samehist");
151     bgd->Draw("samehist");
152    
153     // redraw axes
154     frame->Draw("sameaxis");
155    
156     // text for overflows
157     Int_t nbin = sig->GetNbinsX();
158     TString uoflow = Form( "U/O-flow (S,B): (%.1f, %.1f)% / (%.1f, %.1f)%",
159     sig->GetBinContent(0)*100, bgd->GetBinContent(0)*100,
160     sig->GetBinContent(nbin+1)*100, bgd->GetBinContent(nbin+1)*100 );
161     TText* t = new TText( 0.975, 0.115, uoflow );
162     t->SetNDC();
163     t->SetTextSize( 0.030 );
164     t->SetTextAngle( 90 );
165 kukartse 1.3 //t->AppendPad();
166 kukartse 1.1
167     // save canvas to file
168     c->Update();
169     TMVAGlob::plot_logo();
170     if (Save_Images) {
171     if (htype == MVAType) TMVAGlob::imgconv( c, Form("plots/mva_%s", methodTitle.Data()) );
172     else if (htype == ProbaType) TMVAGlob::imgconv( c, Form("plots/proba_%s", methodTitle.Data()) );
173     else TMVAGlob::imgconv( c, Form("plots/rarity_%s", methodTitle.Data()) );
174     }
175     countCanvas++;
176     }
177     }
178     }
179