ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/LJMet/MultivariateAnalysis/macros/compareanapp.C
Revision: 1.1
Committed: Thu Nov 20 22:34:49 2008 UTC (16 years, 5 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, V00-01-15, V00-01-14, V00-01-13, 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, HEAD
Branch point for: ZMorph-V00-03-01, CMSSW_22X_branch
Log Message:
created /macros with TMVA scripts in it

File Contents

# User Rev Content
1 kukartse 1.1 #include "tmvaglob.C"
2    
3     enum HistType { MVAType = 0, ProbaType = 1, RarityType = 2 };
4    
5     void compareanapp( TString finAn = "TMVA.root", TString finApp = "TMVApp.root",
6     HistType htype = MVAType, bool useTMVAStyle=kTRUE )
7     {
8     // set style and remove existing canvas'
9     TMVAGlob::Initialize( useTMVAStyle );
10    
11     // switches
12     const Bool_t Draw_CFANN_Logy = kFALSE;
13     const Bool_t Save_Images = kTRUE;
14    
15     TFile* file = TMVAGlob::OpenFile( finAn );
16     TFile* fileApp = new TFile( finApp );
17     file->cd();
18    
19     // define Canvas layout here!
20     const Int_t width = 600; // size of canvas
21    
22     // counter variables
23     Int_t countCanvas = 0;
24     char fname[200];
25    
26     TList methods;
27     UInt_t nm = TMVAGlob::GetListOfMethods( methods );
28     TIter next(&methods);
29     TKey *key, *hkey;
30     while ((key = (TKey*)next())) {
31    
32     cout << "--- Found directory: " << ((TDirectory*)key->ReadObj())->GetName()
33     << " --> going in" << endl;
34    
35     TString methodName;
36     TMVAGlob::GetMethodName(methodName,key);
37    
38     cout << "--- Method: " << methodName << endl;
39    
40     TDirectory* mDir = (TDirectory*)key->ReadObj();
41     TList titles;
42     UInt_t ninst = TMVAGlob::GetListOfTitles(mDir,titles);
43     TIter nextTitle(&titles);
44     TKey *titkey;
45     TDirectory *titDir;
46     while ((titkey = TMVAGlob::NextKey(nextTitle,"TDirectory"))) {
47    
48     titDir = (TDirectory *)titkey->ReadObj();
49     TString methodTitle;
50     TMVAGlob::GetMethodTitle(methodTitle,titDir);
51     TString hname = "MVA_" + methodTitle;
52    
53     TH1* sig = dynamic_cast<TH1*>(titDir->Get( hname + "_S" ));
54     TH1* bgd = dynamic_cast<TH1*>(titDir->Get( hname + "_B" ));
55    
56     if (sig==0 || bgd==0) continue;
57    
58     // chop off useless stuff
59     sig->SetTitle( Form("TMVA output for classifier: %s", methodTitle.Data()) );
60     if (htype == ProbaType)
61     sig->SetTitle( Form("TMVA probability for classifier: %s", methodTitle.Data()) );
62     else if (htype == RarityType)
63     sig->SetTitle( Form("TMVA Rarity for classifier: %s", methodTitle.Data()) );
64    
65     // create new canvas
66     TString ctitle = ((htype == MVAType) ?
67     Form("TMVA output %s",methodTitle.Data()) :
68     (htype == ProbaType) ?
69     Form("TMVA probability %s",methodTitle.Data()) :
70     Form("TMVA rarity %s",methodTitle.Data()));
71    
72     TString cname = ((htype == MVAType) ?
73     Form("output_%s",methodTitle.Data()) :
74     (htype == ProbaType) ?
75     Form("probability_%s",methodTitle.Data()) :
76     Form("rarity_%s",methodTitle.Data()));
77    
78     c = new TCanvas( Form("canvas%d", countCanvas+1), ctitle,
79     countCanvas*50+200, countCanvas*20, width, width*0.78 );
80    
81     // set the histogram style
82     TMVAGlob::SetSignalAndBackgroundStyle( sig, bgd );
83    
84     // normalise both signal and background
85     TMVAGlob::NormalizeHists( sig, bgd );
86    
87     // frame limits (choose judicuous x range)
88     Float_t nrms = 4;
89     cout << "--- mean and RMS (S): " << sig->GetMean() << ", " << sig->GetRMS() << endl;
90     cout << "--- mean and RMS (B): " << bgd->GetMean() << ", " << bgd->GetRMS() << endl;
91     Float_t xmin = TMath::Max( TMath::Min(sig->GetMean() - nrms*sig->GetRMS(),
92     bgd->GetMean() - nrms*bgd->GetRMS() ),
93     sig->GetXaxis()->GetXmin() );
94     Float_t xmax = TMath::Min( TMath::Max(sig->GetMean() + nrms*sig->GetRMS(),
95     bgd->GetMean() + nrms*bgd->GetRMS() ),
96     sig->GetXaxis()->GetXmax() );
97     Float_t ymin = 0;
98     Float_t ymax = TMath::Max( sig->GetMaximum(), bgd->GetMaximum() )*1.2 ;
99    
100     if (Draw_CFANN_Logy && mvaName[imva] == "CFANN") ymin = 0.01;
101    
102     // build a frame
103     Int_t nb = 500;
104     TH2F* frame = new TH2F( TString("frame") + methodTitle, sig->GetTitle(),
105     nb, xmin, xmax, nb, ymin, ymax );
106     frame->GetXaxis()->SetTitle(methodTitle);
107     if (htype == ProbaType ) frame->GetXaxis()->SetTitle( "Signal probability" );
108     else if (htype == RarityType) frame->GetXaxis()->SetTitle( "Signal rarity" );
109     frame->GetYaxis()->SetTitle("Normalized");
110     TMVAGlob::SetFrameStyle( frame );
111    
112     // eventually: draw the frame
113     frame->Draw();
114    
115     c->GetPad(0)->SetLeftMargin( 0.105 );
116     frame->GetYaxis()->SetTitleOffset( 1.2 );
117    
118     if (Draw_CFANN_Logy && mvaName[imva] == "CFANN") c->SetLogy();
119    
120     // Draw legend
121     TLegend *legend= new TLegend( c->GetLeftMargin(), 1 - c->GetTopMargin() - 0.12,
122     c->GetLeftMargin() + 0.3, 1 - c->GetTopMargin() );
123     legend->SetFillStyle( 1 );
124     legend->AddEntry(sig,"Signal","F");
125     legend->AddEntry(bgd,"Background","F");
126     legend->SetBorderSize(1);
127     legend->SetMargin( 0.3 );
128     legend->Draw("same");
129    
130     // overlay signal and background histograms
131     sig->Draw("samehist");
132     bgd->Draw("samehist");
133    
134     // retrieve corresponding histogram from TMVApp.root
135     TString hStem(hname);
136     cout << "--- Searching for histogram: " << hStem.Data() << " in application file" << endl;
137    
138     TH1* testHist = (TH1*)fileApp->Get( hStem );
139     if (testHist != 0) {
140     cout << "--> Found application histogram: " << testHist->GetName() << " --> superimpose it" << endl;
141     // compute normalisation factor
142     TMVAGlob::NormalizeHists( testHist );
143     testHist->SetLineWidth( 3 );
144     testHist->SetLineColor( 1 );
145     testHist->Draw("samehist");
146     }
147    
148     // redraw axes
149     frame->Draw("sameaxis");
150    
151     // text for overflows
152     Int_t nbin = sig->GetNbinsX();
153     TString uoflow = Form( "U/O-flow (S,B): (%.1f, %.1f)% / (%.1f, %.1f)%",
154     sig->GetBinContent(0)*100, bgd->GetBinContent(0)*100,
155     sig->GetBinContent(nbin+1)*100, bgd->GetBinContent(nbin+1)*100 );
156     TText* t = new TText( 0.975, 0.115, uoflow );
157     t->SetNDC();
158     t->SetTextSize( 0.030 );
159     t->SetTextAngle( 90 );
160     t->AppendPad();
161    
162     // save canvas to file
163     c->Update();
164     TMVAGlob::plot_logo();
165     if (Save_Images) {
166     if (htype == MVAType) TMVAGlob::imgconv( c, Form("plots/mva_%s", methodTitle.Data()) );
167     else if (htype == ProbaType) TMVAGlob::imgconv( c, Form("plots/proba_%s", methodTitle.Data()) );
168     else TMVAGlob::imgconv( c, Form("plots/rarity_%s", methodTitle.Data()) );
169     }
170     countCanvas++;
171     }
172     }
173     }