ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/benhoob/HWW/likelihoodrefs.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 <vector>
2     #include <string>
3     #include "tmvaglob.C"
4    
5    
6     // this macro plots the reference distribuions for the Likelihood
7     // methods for the various input variables used in TMVA (e.g. running
8     // TMVAnalysis.C). Signal and Background are plotted separately
9    
10     // input: - Input file (result from TMVA),
11     // - use of TMVA plotting TStyle
12     void likelihoodrefs( TString fin = "TMVA.root", Bool_t useTMVAStyle = kTRUE )
13     {
14     // set style and remove existing canvas'
15     TMVAGlob::Initialize( useTMVAStyle );
16    
17     // checks if file with name "fin" is already open, and if not opens one
18     TFile* file = TMVAGlob::OpenFile( fin );
19    
20     // get all titles of the method likelihood
21     TList titles;
22     UInt_t ninst = TMVAGlob::GetListOfTitles("Method_Likelihood",titles);
23     if (ninst==0) {
24     cout << "Could not locate directory 'Method_Likelihood' in file " << fin << endl;
25     return;
26     }
27     // loop over all titles
28     TIter keyIter(&titles);
29     TDirectory *lhdir;
30     TKey *key;
31     while ((key = TMVAGlob::NextKey(keyIter,"TDirectory"))) {
32     lhdir = (TDirectory *)key->ReadObj();
33     likelihoodrefs( lhdir );
34     }
35     }
36    
37     void likelihoodrefs( TDirectory *lhdir ) {
38     Bool_t newCanvas = kTRUE;
39    
40     const UInt_t maxCanvas = 200;
41     TCanvas** c = new TCanvas*[maxCanvas];
42     Int_t width = 670;
43     Int_t height = 380;
44    
45     // avoid duplicated printing
46     std::vector<std::string> hasBeenUsed;
47     const TString titName = lhdir->GetName();
48     UInt_t ic = -1;
49    
50     TIter next(lhdir->GetListOfKeys());
51     TKey *key;
52     while ((key = TMVAGlob::NextKey(next,"TH1"))) { // loop over all TH1
53     TH1 *h = (TH1*)key->ReadObj();
54     TH1F *b( 0 );
55     TString hname( h->GetName() );
56    
57     // avoid duplicated plotting
58     Bool_t found = kFALSE;
59     for (UInt_t j = 0; j < hasBeenUsed.size(); j++) {
60     if (hasBeenUsed[j] == hname.Data()) found = kTRUE;
61     }
62     if (!found) {
63    
64     // draw original plots
65     if (hname.EndsWith("_sig_nice")) {
66    
67     if (newCanvas) {
68     char cn[20];
69     sprintf( cn, "cv%d_%s", ic+1, titName.Data() );
70     ++ic;
71     TString n = hname;
72     c[ic] = new TCanvas( cn, Form( "%s reference for variable: %s",
73     titName.Data(),(n.ReplaceAll("_sig","")).Data() ),
74     ic*50+50, ic*20, width, height );
75     c[ic]->Divide(2,1);
76     newCanvas = kFALSE;
77     }
78    
79     // signal
80     Int_t color = 4;
81     TPad * cPad = (TPad*)c[ic]->cd(1);
82     TString plotname = hname;
83    
84     h->SetMaximum(h->GetMaximum()*1.3);
85     h->SetMinimum( 0 );
86     h->SetMarkerColor(color);
87     h->SetMarkerSize( 0.7 );
88     h->SetMarkerStyle( 24 );
89     h->SetLineWidth(1);
90     h->SetLineColor(color);
91     color++;
92     h->Draw("e1");
93     Double_t hSscale = 1.0/(h->GetSumOfWeights()*h->GetBinWidth(1));
94    
95     TLegend *legS= new TLegend( cPad->GetLeftMargin(),
96     1-cPad->GetTopMargin()-.14,
97     cPad->GetLeftMargin()+.77,
98     1-cPad->GetTopMargin() );
99     legS->SetBorderSize(1);
100     legS->AddEntry(h,"Input data (signal)","p");
101    
102     // background
103     TString bname( hname );
104     b = (TH1F*)lhdir->Get( bname.ReplaceAll("_sig","_bgd") );
105     cPad = (TPad*)c[ic]->cd(2);
106     color = 2;
107     b->SetMaximum(b->GetMaximum()*1.3);
108     b->SetMinimum( 0 );
109     b->SetLineWidth(1);
110     b->SetLineColor(color);
111     b->SetMarkerColor(color);
112     b->SetMarkerSize( 0.7 );
113     b->SetMarkerStyle( 24 );
114     b->Draw("e1");
115     Double_t hBscale = 1.0/(b->GetSumOfWeights()*b->GetBinWidth(1));
116     TLegend *legB= new TLegend( cPad->GetLeftMargin(),
117     1-cPad->GetTopMargin()-.14,
118     cPad->GetLeftMargin()+.77,
119     1-cPad->GetTopMargin() );
120     legB->SetBorderSize(1);
121     legB->AddEntry(b,"Input data (backgr.)","p");
122    
123     // register
124     hasBeenUsed.push_back( bname.Data() );
125    
126     // the PDFs --------------
127    
128     // check for splines
129     h = 0;
130     b = 0;
131     TString pname = hname; pname.ReplaceAll("_nice","");
132     for (int i=0; i<= 5; i++) {
133     TString hspline = pname + Form( "_smoothed_hist_from_spline%i", i );
134     h = (TH1F*)lhdir->Get( hspline );
135     if (h) {
136     b = (TH1F*)lhdir->Get( hspline.ReplaceAll("_sig","_bgd") );
137     break;
138     }
139     }
140    
141     // check for KDE
142     if (h == 0 && b == 0) {
143     TString hspline = pname + Form( "_smoothed_hist_from_KDE", i );
144     h = (TH1F*)lhdir->Get( hspline );
145     if (h) {
146     b = (TH1F*)lhdir->Get( hspline.ReplaceAll("_sig","_bgd") );
147     }
148     }
149    
150     // found something ?
151     if (h == 0 || b == 0) {
152     cout << "--- likelihoodrefs.C: did not find spline for histogram: " << pname.Data() << endl;
153     }
154     else {
155    
156     Double_t pSscale = 1.0/(h->GetSumOfWeights()*h->GetBinWidth(1));
157     h->Scale( pSscale/hSscale );
158     color = 4;
159     c[ic]->cd(1);
160     h->SetLineWidth(2);
161     h->SetLineColor(color);
162     legS->AddEntry(h,"Estimated PDF (norm. signal)","l");
163     h->Draw("histsame");
164     legS->Draw();
165    
166     Double_t pBscale = 1.0/(b->GetSumOfWeights()*b->GetBinWidth(1));
167     b->Scale( pBscale/hBscale );
168     color = 2;
169     c[ic]->cd(2);
170     b->SetLineColor(color);
171     b->SetLineWidth(2);
172     legB->AddEntry(b,"Estimated PDF (norm. backgr.)","l");
173     b->Draw("histsame");
174    
175     // draw the legends
176     legB->Draw();
177    
178     hasBeenUsed.push_back( pname.Data() );
179     }
180    
181     c[ic]->Update();
182    
183     // write to file
184     TString fname = Form( "plots/%s_refs_c%i", titName.Data(), ic+1 );
185     TMVAGlob::imgconv( c[ic], fname );
186     // c[ic]->Update();
187    
188     newCanvas = kTRUE;
189     hasBeenUsed.push_back( hname.Data() );
190     }
191     }
192     }
193     }
194