likelihoodrefs.C

Go to the documentation of this file.
00001 #include <vector>
00002 #include <string>
00003 #include "tmvaglob.C"
00004 
00005 
00006 // this macro plots the reference distribuions for the Likelihood
00007 // methods for the various input variables used in TMVA (e.g. running
00008 // TMVAnalysis.C).  Signal and Background are plotted separately
00009 
00010 // input: - Input file (result from TMVA),
00011 //        - use of TMVA plotting TStyle
00012 void likelihoodrefs( TString fin = "TMVA.root", Bool_t useTMVAStyle = kTRUE )
00013 {
00014    // set style and remove existing canvas'
00015    TMVAGlob::Initialize( useTMVAStyle );
00016   
00017    // checks if file with name "fin" is already open, and if not opens one
00018    TFile* file = TMVAGlob::OpenFile( fin );  
00019 
00020    // get all titles of the method likelihood
00021    TList titles;
00022    UInt_t ninst = TMVAGlob::GetListOfTitles("Method_Likelihood",titles);
00023    if (ninst==0) {
00024       cout << "Could not locate directory 'Method_Likelihood' in file " << fin << endl;
00025       return;
00026    }
00027    // loop over all titles
00028    TIter keyIter(&titles);
00029    TDirectory *lhdir;
00030    TKey *key;
00031    while ((key = TMVAGlob::NextKey(keyIter,"TDirectory"))) {
00032       lhdir = (TDirectory *)key->ReadObj();
00033       likelihoodrefs( lhdir );
00034    }
00035 }
00036 
00037 void likelihoodrefs( TDirectory *lhdir ) {
00038    Bool_t newCanvas = kTRUE;
00039    
00040    const UInt_t maxCanvas = 200;
00041    TCanvas** c = new TCanvas*[maxCanvas];
00042    Int_t width  = 670;
00043    Int_t height = 380;
00044 
00045    // avoid duplicated printing
00046    std::vector<std::string> hasBeenUsed;
00047    const TString titName = lhdir->GetName();
00048    UInt_t ic = -1;   
00049 
00050    TIter next(lhdir->GetListOfKeys());
00051    TKey *key;
00052    while ((key = TMVAGlob::NextKey(next,"TH1"))) { // loop over all TH1
00053       TH1 *h = (TH1*)key->ReadObj();
00054       TH1F *b( 0 );
00055       TString hname( h->GetName() );
00056 
00057       // avoid duplicated plotting
00058       Bool_t found = kFALSE;
00059       for (UInt_t j = 0; j < hasBeenUsed.size(); j++) {
00060          if (hasBeenUsed[j] == hname.Data()) found = kTRUE;
00061       }
00062       if (!found) {
00063 
00064          // draw original plots
00065          if (hname.EndsWith("_sig_nice")) {
00066 
00067             if (newCanvas) {
00068                char cn[20];
00069                sprintf( cn, "cv%d_%s", ic+1, titName.Data() );
00070                ++ic;
00071                TString n = hname;         
00072                c[ic] = new TCanvas( cn, Form( "%s reference for variable: %s", 
00073                                               titName.Data(),(n.ReplaceAll("_sig","")).Data() ), 
00074                                     ic*50+50, ic*20, width, height ); 
00075                c[ic]->Divide(2,1);
00076                newCanvas = kFALSE;
00077             }      
00078 
00079             // signal
00080             Int_t color = 4; 
00081             TPad * cPad = (TPad*)c[ic]->cd(1);
00082             TString plotname = hname;
00083 
00084             h->SetMaximum(h->GetMaximum()*1.3);
00085             h->SetMinimum( 0 );
00086             h->SetMarkerColor(color);
00087             h->SetMarkerSize( 0.7 );
00088             h->SetMarkerStyle( 24 );
00089             h->SetLineWidth(1);
00090             h->SetLineColor(color);
00091             color++;
00092             h->Draw("e1");
00093             Double_t hSscale = 1.0/(h->GetSumOfWeights()*h->GetBinWidth(1));
00094 
00095             TLegend *legS= new TLegend( cPad->GetLeftMargin(), 
00096                                         1-cPad->GetTopMargin()-.14, 
00097                                         cPad->GetLeftMargin()+.77, 
00098                                         1-cPad->GetTopMargin() );
00099             legS->SetBorderSize(1);
00100             legS->AddEntry(h,"Input data (signal)","p");
00101 
00102             // background
00103             TString bname( hname );     
00104             b = (TH1F*)lhdir->Get( bname.ReplaceAll("_sig","_bgd") );
00105             cPad = (TPad*)c[ic]->cd(2);
00106             color = 2;
00107             b->SetMaximum(b->GetMaximum()*1.3);
00108             b->SetMinimum( 0 );
00109             b->SetLineWidth(1);
00110             b->SetLineColor(color);
00111             b->SetMarkerColor(color);
00112             b->SetMarkerSize( 0.7 );
00113             b->SetMarkerStyle( 24 );
00114             b->Draw("e1");       
00115             Double_t hBscale = 1.0/(b->GetSumOfWeights()*b->GetBinWidth(1));
00116             TLegend *legB= new TLegend( cPad->GetLeftMargin(), 
00117                                         1-cPad->GetTopMargin()-.14, 
00118                                         cPad->GetLeftMargin()+.77, 
00119                                         1-cPad->GetTopMargin() );
00120             legB->SetBorderSize(1);
00121             legB->AddEntry(b,"Input data (backgr.)","p");
00122 
00123             // register
00124             hasBeenUsed.push_back( bname.Data() );
00125 
00126             // the PDFs --------------
00127 
00128             // check for splines
00129             h = 0;
00130             b = 0;
00131             TString pname = hname; pname.ReplaceAll("_nice","");            
00132             for (int i=0; i<= 5; i++) {
00133                TString hspline = pname + Form( "_smoothed_hist_from_spline%i", i );
00134                h = (TH1F*)lhdir->Get( hspline );
00135                if (h) {
00136                   b = (TH1F*)lhdir->Get( hspline.ReplaceAll("_sig","_bgd") );
00137                   break;
00138                }
00139             }
00140 
00141             // check for KDE
00142             if (h == 0 && b == 0) {
00143                TString hspline = pname + Form( "_smoothed_hist_from_KDE", i );
00144                h = (TH1F*)lhdir->Get( hspline );
00145                if (h) {
00146                   b = (TH1F*)lhdir->Get( hspline.ReplaceAll("_sig","_bgd") );
00147                }
00148             }
00149                
00150             // found something ?
00151             if (h == 0 || b == 0) {
00152                cout << "--- likelihoodrefs.C: did not find spline for histogram: " << pname.Data() << endl;
00153             }
00154             else {
00155                
00156                Double_t pSscale = 1.0/(h->GetSumOfWeights()*h->GetBinWidth(1));
00157                h->Scale( pSscale/hSscale );
00158                color = 4;
00159                c[ic]->cd(1);
00160                h->SetLineWidth(2);
00161                h->SetLineColor(color);
00162                legS->AddEntry(h,"Estimated PDF (norm. signal)","l");
00163                h->Draw("histsame");
00164                legS->Draw();
00165           
00166                Double_t pBscale = 1.0/(b->GetSumOfWeights()*b->GetBinWidth(1));
00167                b->Scale( pBscale/hBscale );
00168                color = 2;
00169                c[ic]->cd(2);
00170                b->SetLineColor(color);
00171                b->SetLineWidth(2);
00172                legB->AddEntry(b,"Estimated PDF (norm. backgr.)","l");
00173                b->Draw("histsame");
00174 
00175                // draw the legends
00176                legB->Draw();
00177           
00178                hasBeenUsed.push_back( pname.Data() );
00179             }     
00180 
00181             c[ic]->Update();
00182 
00183             // write to file
00184             TString fname = Form( "plots/%s_refs_c%i", titName.Data(), ic+1 );
00185             TMVAGlob::imgconv( c[ic], fname );
00186             //  c[ic]->Update();
00187 
00188             newCanvas = kTRUE;
00189             hasBeenUsed.push_back( hname.Data() );
00190          }
00191       }
00192    }
00193 }
00194 

Generated on Tue Jul 5 15:25:44 2011 for ROOT_528-00b_version by  doxygen 1.5.1