probas.C

Go to the documentation of this file.
00001 #include "tmvaglob.C"
00002 
00003 // this macro plots the MVA probability distributions (Signal and
00004 // Background overlayed) of different MVA methods run in TMVA
00005 // (e.g. running TMVAnalysis.C).
00006 
00007 // input: - Input file (result from TMVA)
00008 //        - use of TMVA plotting TStyle
00009 void probas( TString fin = "TMVA.root", Bool_t useTMVAStyle = kTRUE )
00010 {
00011    // set style and remove existing canvas'
00012    TMVAGlob::Initialize( useTMVAStyle );
00013 
00014    // switches
00015    const Bool_t Draw_CFANN_Logy = kFALSE;
00016    const Bool_t Save_Images     = kTRUE;
00017 
00018    // checks if file with name "fin" is already open, and if not opens one
00019    TFile* file = TMVAGlob::OpenFile( fin );  
00020 
00021    // define Canvas layout here!
00022    Int_t xPad = 1; // no of plots in x
00023    Int_t yPad = 1; // no of plots in y
00024    Int_t noPad = xPad * yPad ; 
00025    const Int_t width = 600;   // size of canvas
00026 
00027    // this defines how many canvases we need
00028    TCanvas *c = 0;
00029 
00030    // counter variables
00031    Int_t countCanvas = 0;
00032 
00033    // list of existing MVAs
00034    const Int_t nveto = 1;
00035    TString suffixSig = "_tr_S";
00036    TString suffixBgd = "_tr_B";
00037 
00038    // search for the right histograms in full list of keys
00039    TList methods;
00040    UInt_t nmethods = TMVAGlob::GetListOfMethods( methods );
00041    if (nmethods==0) {
00042       cout << "--- Probas.C: no methods found!" << endl;
00043       return;
00044    }
00045    TIter next(&methods);
00046    TKey *key, *hkey;
00047    char fname[200];
00048    TH1* sig(0);
00049    TH1* bgd(0);
00050    while ((key = (TKey*)next())) {
00051       TDirectory * mDir = (TDirectory*)key->ReadObj();
00052       TList titles;
00053       UInt_t ni = TMVAGlob::GetListOfTitles( mDir, titles );
00054       TString methodName;
00055       TMVAGlob::GetMethodName(methodName,key);
00056       if (ni==0) {
00057          cout << "+++ No titles found for classifier: " << methodName << endl;
00058          return;
00059       }
00060       TIter nextTitle(&titles);
00061       TKey *instkey;
00062       TDirectory *instDir;
00063       
00064       // iterate over all classifiers
00065       while ((instkey = (TKey *)nextTitle())) {
00066          instDir = (TDirectory *)instkey->ReadObj();
00067          TString instName = instkey->GetName();
00068          TList h1hists;
00069          UInt_t nhists = TMVAGlob::GetListOfKeys( h1hists, "TH1", instDir );
00070          if (nhists==0) cout << "*** No histograms found!" << endl;
00071          TIter nextInDir(&h1hists);
00072          TString methodTitle;
00073          TMVAGlob::GetMethodTitle(methodTitle,instDir);
00074          Bool_t found = kFALSE;
00075          while (hkey = (TKey*)nextInDir()) {
00076             TH1 *th1 = (TH1*)hkey->ReadObj();
00077             TString hname= th1->GetName();
00078             if (hname.Contains( suffixSig ) && !hname.Contains( "Cut") && 
00079                 !hname.Contains("original") && !hname.Contains("smoothed")) {
00080                // retrieve corresponding signal and background histograms   
00081                TString hnameS = hname;
00082                TString hnameB = hname; hnameB.ReplaceAll("_S","_B");
00083 
00084                sig = (TH1*)instDir->Get( hnameS );
00085                bgd = (TH1*)instDir->Get( hnameB );
00086 
00087                if (sig == 0 || bgd == 0) {
00088                   cout << "*** probas.C: big troubles in probas.... histogram: " << hname << " not found" << endl;
00089                   return;
00090                }
00091 
00092                TH1* sigF(0);
00093                TH1* bkgF(0);
00094                
00095                for (int i=0; i<= 5; i++) {
00096                   TString hspline = hnameS + Form("_smoothed_hist_from_spline%i",i);
00097                   sigF = (TH1*)instDir->Get( hspline );
00098             
00099                   if (sigF) {
00100                      bkgF = (TH1*)instDir->Get( hspline.ReplaceAll("_tr_S","_tr_B") );
00101                      break;
00102                   }
00103                }
00104                if (!sigF){
00105                   TString hspline = hnameS + TString("_smoothed_hist_from_KDE");
00106                   sigF = (TH1*)instDir->Get( hspline );
00107                   
00108                   if (sigF) {
00109                      bkgF = (TH1*)instDir->Get( hspline.ReplaceAll("_tr_S","_tr_B") );
00110                   }
00111                }
00112               
00113                if ((sigF == NULL || bkgF == NULL) &&!hname.Contains("hist") ) {
00114                   cout << "*** probas.C: big troubles - did not find probability histograms" << endl;
00115                   return;
00116                }
00117                else  {
00118                     // remove the signal suffix
00119 
00120                   // check that exist
00121                   if (NULL != sigF && NULL != bkgF && NULL!=sig && NULL!=bgd) {
00122           
00123                      TString hname = sig->GetName();
00124                      found = kTRUE;
00125                      // chop off useless stuff
00126                      sig->SetTitle( TString("TMVA output for classifier: ") + methodTitle );
00127             
00128                      // create new canvas
00129                      cout << "--- Book canvas no: " << countCanvas << endl;
00130                      char cn[20];
00131                      sprintf( cn, "canvas%d", countCanvas+1 );
00132                      c = new TCanvas( cn, Form("TMVA Output Fit Variables %s",methodTitle.Data()), 
00133                                       countCanvas*50+200, countCanvas*20, width, width*0.78 ); 
00134             
00135                      // set the histogram style
00136                      TMVAGlob::SetSignalAndBackgroundStyle( sig, bgd );
00137                      TMVAGlob::SetSignalAndBackgroundStyle( sigF, bkgF );
00138             
00139                      // frame limits (choose judicuous x range)
00140                      Float_t nrms = 4;
00141                      Float_t xmin = TMath::Max( TMath::Min(sig->GetMean() - nrms*sig->GetRMS(), 
00142                                                            bgd->GetMean() - nrms*bgd->GetRMS() ),
00143                                                 sig->GetXaxis()->GetXmin() );
00144                      Float_t xmax = TMath::Min( TMath::Max(sig->GetMean() + nrms*sig->GetRMS(), 
00145                                                            bgd->GetMean() + nrms*bgd->GetRMS() ),
00146                                                 sig->GetXaxis()->GetXmax() );
00147                      Float_t ymin = 0;
00148                      Float_t ymax = TMath::Max( sig->GetMaximum(), bgd->GetMaximum() )*1.5;
00149             
00150                      if (Draw_CFANN_Logy && mvaName[imva] == "CFANN") ymin = 0.01;
00151             
00152                      // build a frame
00153                      Int_t nb = 500;
00154                      TH2F* frame = new TH2F( TString("frame") + sig->GetName() + "_proba", sig->GetTitle(), 
00155                                              nb, xmin, xmax, nb, ymin, ymax );
00156                      frame->GetXaxis()->SetTitle(methodTitle);
00157                      frame->GetYaxis()->SetTitle("Normalized");
00158                      TMVAGlob::SetFrameStyle( frame );
00159             
00160                      // eventually: draw the frame
00161                      frame->Draw();  
00162             
00163                      if (Draw_CFANN_Logy && mvaName[imva] == "CFANN") c->SetLogy();
00164             
00165                      // overlay signal and background histograms
00166                      sig->SetMarkerColor( TMVAGlob::c_SignalLine );
00167                      sig->SetMarkerSize( 0.7 );
00168                      sig->SetMarkerStyle( 20 );
00169                      sig->SetLineWidth(1);
00170 
00171                      bgd->SetMarkerColor( TMVAGlob::c_BackgroundLine );
00172                      bgd->SetMarkerSize( 0.7 );
00173                      bgd->SetMarkerStyle( 24 );
00174                      bgd->SetLineWidth(1);
00175 
00176                      sig->Draw("samee");
00177                      bgd->Draw("samee");
00178             
00179                      sigF->SetFillStyle( 0 );
00180                      bkgF->SetFillStyle( 0 );
00181                      sigF->Draw("samehist");
00182                      bkgF->Draw("samehist");
00183             
00184                      // redraw axes
00185                      frame->Draw("sameaxis");
00186             
00187                      // Draw legend               
00188                      TLegend *legend= new TLegend( c->GetLeftMargin(), 1 - c->GetTopMargin() - 0.2, 
00189                                                    c->GetLeftMargin() + 0.4, 1 - c->GetTopMargin() );
00190                      legend->AddEntry(sig,"Signal data","P");
00191                      legend->AddEntry(sigF,"Signal PDF","L");
00192                      legend->AddEntry(bgd,"Background data","P");
00193                      legend->AddEntry(bkgF,"Background PDF","L");
00194                      legend->Draw("same");
00195                      legend->SetBorderSize(1);
00196                      legend->SetMargin( 0.3 );
00197             
00198                      // save canvas to file
00199                      c->Update();
00200                      TMVAGlob::plot_logo();
00201                      sprintf( fname, "plots/mva_pdf_%s_c%i", methodTitle.Data(), countCanvas+1 );
00202                      if (Save_Images) TMVAGlob::imgconv( c, fname );
00203                      countCanvas++;
00204                   }
00205                }
00206             }
00207             
00208          }
00209          if(!found){
00210             cout << "--- No PDFs found for method " << methodTitle << ". Did you request \"CreateMVAPdfs\" in the option string?" << endl;
00211          }
00212       }    
00213    }
00214 }

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