ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/benhoob/HWW/compareClassApp.C
Revision: 1.1
Committed: Mon Feb 14 12:39:14 2011 UTC (14 years, 3 months ago) by benhoob
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Log Message:
Initial commit

File Contents

# User Rev Content
1 benhoob 1.1 #include "tmvaglob.C"
2    
3     enum HistType { MVAType = 0, ProbaType = 1, RarityType = 2 };
4    
5     #define CheckDerivedPlots 0
6     //TString DerivedPlotName = "Proba";
7     TString DerivedPlotName = "Rarity";
8    
9     void compareClassApp( TString finAn = "TMVA.root", TString finApp = "TMVApp.root",
10     HistType htype = MVAType, bool useTMVAStyle=kTRUE )
11     {
12     cout << "=== Compare histograms of two files ===" << endl;
13     cout << " File-1: " << finAn << endl;
14     cout << " File-2: " << finApp << endl;
15    
16     // set style and remove existing canvas'
17     TMVAGlob::Initialize( useTMVAStyle );
18    
19     // switches
20     const Bool_t Draw_CFANN_Logy = kFALSE;
21     const Bool_t Save_Images = kTRUE;
22    
23     TFile* file = TMVAGlob::OpenFile( finAn );
24     TFile* fileApp = new TFile( finApp );
25     file->cd();
26    
27     // define Canvas layout here!
28     const Int_t width = 600; // size of canvas
29    
30     // counter variables
31     Int_t countCanvas = 0;
32     char fname[200];
33    
34     TList methods;
35     UInt_t nm = TMVAGlob::GetListOfMethods( methods );
36     TIter next(&methods);
37     TKey *key, *hkey;
38     while ((key = (TKey*)next())) {
39    
40     TString dirname = ((TDirectory*)key->ReadObj())->GetName();
41     if (dirname.Contains( "Cuts" )) {
42     cout << "--- Found directory: " << dirname << " --> ignoring" << endl;
43     continue;
44     }
45     cout << "--- Found directory: " << dirname
46     << " --> going in" << endl;
47    
48     TString methodName;
49     TMVAGlob::GetMethodName(methodName,key);
50    
51     cout << "--- Method: " << methodName << endl;
52    
53     TDirectory* mDir = (TDirectory*)key->ReadObj();
54     TList titles;
55     UInt_t ninst = TMVAGlob::GetListOfTitles(mDir,titles);
56     TIter nextTitle(&titles);
57     TKey *titkey;
58     TDirectory *titDir;
59     while ((titkey = TMVAGlob::NextKey(nextTitle,"TDirectory"))) {
60    
61     titDir = (TDirectory *)titkey->ReadObj();
62     TString methodTitle;
63     TMVAGlob::GetMethodTitle(methodTitle,titDir);
64     TString hname = "MVA_" + methodTitle;
65     if (CheckDerivedPlots) hname += TString("_") + DerivedPlotName;
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     Float_t xmin = TMath::Max( TMath::Min(sig->GetMean() - nrms*sig->GetRMS(),
104     bgd->GetMean() - nrms*bgd->GetRMS() ),
105     sig->GetXaxis()->GetXmin() );
106     Float_t xmax = TMath::Min( TMath::Max(sig->GetMean() + nrms*sig->GetRMS(),
107     bgd->GetMean() + nrms*bgd->GetRMS() ),
108     sig->GetXaxis()->GetXmax() );
109     Float_t ymin = 0;
110     Float_t ymax = TMath::Max( sig->GetMaximum(), bgd->GetMaximum() )*1.2 ;
111    
112     if (Draw_CFANN_Logy && mvaName[imva] == "CFANN") ymin = 0.01;
113    
114     // build a frame
115     Int_t nb = 500;
116     TH2F* frame = new TH2F( TString("frame") + methodTitle, sig->GetTitle(),
117     nb, xmin, xmax, nb, ymin, ymax );
118     frame->GetXaxis()->SetTitle(methodTitle);
119     if (htype == ProbaType ) frame->GetXaxis()->SetTitle( "Signal probability" );
120     else if (htype == RarityType) frame->GetXaxis()->SetTitle( "Signal rarity" );
121     frame->GetYaxis()->SetTitle("Normalized");
122     TMVAGlob::SetFrameStyle( frame );
123    
124     // eventually: draw the frame
125     frame->Draw();
126    
127     c->GetPad(0)->SetLeftMargin( 0.105 );
128     frame->GetYaxis()->SetTitleOffset( 1.2 );
129    
130     if (Draw_CFANN_Logy && mvaName[imva] == "CFANN") c->SetLogy();
131    
132     // Draw legend
133     TLegend *legend= new TLegend( c->GetLeftMargin(), 1 - c->GetTopMargin() - 0.12,
134     c->GetLeftMargin() + 0.3, 1 - c->GetTopMargin() );
135     legend->SetFillStyle( 1 );
136     legend->AddEntry(sig,"Signal","F");
137     legend->AddEntry(bgd,"Background","F");
138     legend->SetBorderSize(1);
139     legend->SetMargin( 0.3 );
140     legend->Draw("same");
141    
142     // overlay signal and background histograms
143     sig->Draw("samehist");
144     bgd->Draw("samehist");
145    
146     // retrieve corresponding histogram from TMVApp.root
147     TString hStem(hname);
148     cout << "--- Searching for histogram: " << hStem.Data() << " in application file" << endl;
149    
150     TH1* testHist = (TH1*)fileApp->Get( hStem );
151     if (testHist != 0) {
152     cout << "--> Found application histogram: " << testHist->GetName() << " --> superimpose it" << endl;
153     // compute normalisation factor
154     TMVAGlob::NormalizeHists( testHist );
155     testHist->SetLineWidth( 3 );
156     testHist->SetLineColor( 1 );
157     testHist->Draw("samehist");
158     }
159    
160     // redraw axes
161     frame->Draw("sameaxis");
162    
163     // text for overflows
164     Int_t nbin = sig->GetNbinsX();
165     Double_t dxu = sig->GetBinWidth(0);
166     Double_t dxo = sig->GetBinWidth(nbin+1);
167     TString uoflow = Form( "U/O-flow (S,B): (%.1f, %.1f)%% / (%.1f, %.1f)%%",
168     sig->GetBinContent(0)*dxu*100, bgd->GetBinContent(0)*dxu*100,
169     sig->GetBinContent(nbin+1)*dxo*100, bgd->GetBinContent(nbin+1)*dxo*100 );
170     TText* t = new TText( 0.975, 0.115, uoflow );
171     t->SetNDC();
172     t->SetTextSize( 0.030 );
173     t->SetTextAngle( 90 );
174     t->AppendPad();
175    
176     // save canvas to file
177     c->Update();
178     TMVAGlob::plot_logo();
179     if (Save_Images) {
180     if (htype == MVAType) TMVAGlob::imgconv( c, Form("plots/mva_%s", methodTitle.Data()) );
181     else if (htype == ProbaType) TMVAGlob::imgconv( c, Form("plots/proba_%s", methodTitle.Data()) );
182     else TMVAGlob::imgconv( c, Form("plots/rarity_%s", methodTitle.Data()) );
183     }
184     countCanvas++;
185     }
186     }
187     }