efficiencies.C

Go to the documentation of this file.
00001 #include "tmvaglob.C"
00002 
00003 void plot_efficiencies( TFile* file, Int_t type = 2, TDirectory* BinDir)
00004 {
00005    // input:   - Input file (result from TMVA),
00006    //          - type = 1 --> plot efficiency(B) versus eff(S)
00007    //                 = 2 --> plot rejection (B) versus efficiency (S)
00008 
00009    Bool_t __PLOT_LOGO__  = kTRUE;
00010    Bool_t __SAVE_IMAGE__ = kTRUE;
00011 
00012    // the coordinates
00013    Float_t x1 = 0;
00014    Float_t x2 = 1;
00015    Float_t y1 = 0;
00016    Float_t y2 = 0.8;
00017 
00018    // reverse order if "rejection"
00019    if (type == 2) {
00020       Float_t z = y1;
00021       y1 = 1 - y2;
00022       y2 = 1 - z;
00023       //      cout << "--- type==2: plot background rejection versus signal efficiency" << endl;
00024    }
00025    else {
00026       //  cout << "--- type==1: plot background efficiency versus signal efficiency" << endl;
00027    }
00028    // create canvas
00029    TCanvas* c = new TCanvas( "c", "the canvas", 200, 0, 650, 500 );
00030 
00031    // global style settings
00032    c->SetGrid();
00033    c->SetTicks();
00034 
00035    // legend
00036    Float_t x0L = 0.107,     y0H = 0.899;
00037    Float_t dxL = 0.457-x0L, dyH = 0.22;
00038    if (type == 2) {
00039       x0L = 0.15;
00040       y0H = 1 - y0H + dyH + 0.07;
00041    }
00042    TLegend *legend = new TLegend( x0L, y0H-dyH, x0L+dxL, y0H );
00043    //legend->SetTextSize( 0.05 );
00044    legend->SetHeader( "MVA Method:" );
00045    legend->SetMargin( 0.4 );
00046 
00047    TString xtit = "Signal efficiency";
00048    TString ytit = "Background efficiency";
00049    if (type == 2) ytit = "Background rejection";
00050    TString ftit = ytit + " versus " + xtit;
00051 
00052    if (TString(BinDir->GetName()).Contains("multicut")){
00053       ftit += "  Bin: ";
00054       ftit += (BinDir->GetTitle());
00055    }
00056 
00057    // draw empty frame
00058    if(gROOT->FindObject("frame")!=0) gROOT->FindObject("frame")->Delete();
00059    TH2F* frame = new TH2F( "frame", ftit, 500, x1, x2, 500, y1, y2 );
00060    frame->GetXaxis()->SetTitle( xtit );
00061    frame->GetYaxis()->SetTitle( ytit );
00062    TMVAGlob::SetFrameStyle( frame, 1.0 );
00063 
00064    frame->Draw();
00065 
00066    Int_t color = 1;
00067    Int_t nmva  = 0;
00068    TKey *key, *hkey;
00069 
00070    TString hNameRef = "effBvsS";
00071    if (type == 2) hNameRef = "rejBvsS";
00072 
00073    TList hists;
00074    TList methods;
00075    UInt_t nm = TMVAGlob::GetListOfMethods( methods );
00076    //   TIter next(file->GetListOfKeys());
00077    TIter next(&methods);
00078 
00079    // loop over all methods
00080    while (key = (TKey*)next()) {
00081       TDirectory * mDir = (TDirectory*)key->ReadObj();
00082       TList titles;
00083       UInt_t ninst = TMVAGlob::GetListOfTitles(mDir,titles);
00084       TIter nextTitle(&titles);
00085       TKey *titkey;
00086       TDirectory *titDir;
00087       while ((titkey = TMVAGlob::NextKey(nextTitle,"TDirectory"))) {
00088          titDir = (TDirectory *)titkey->ReadObj();
00089          TString methodTitle;
00090          TMVAGlob::GetMethodTitle(methodTitle,titDir);
00091          TIter nextKey( titDir->GetListOfKeys() );
00092          while ((hkey = TMVAGlob::NextKey(nextKey,"TH1"))) {
00093             TH1 *h = (TH1*)hkey->ReadObj();
00094             TString hname = h->GetName();
00095             if (hname.Contains( hNameRef ) && hname.BeginsWith( "MVA_" )) {
00096                h->SetLineWidth(3);
00097                h->SetLineColor(color);
00098                color++; if (color == 5 || color == 10 || color == 11) color++;
00099                h->Draw("csame");
00100                hists.Add(h);
00101                nmva++;
00102             }
00103          }
00104       }
00105    }
00106 
00107    while (hists.GetSize()) {
00108       TListIter hIt(&hists);
00109       TH1* hist(0);
00110       Double_t largestInt=-1;
00111       TH1* histWithLargestInt(0);
00112       while ((hist = (TH1*)hIt())!=0) {
00113          Double_t integral = hist->Integral(1,hist->FindBin(0.9999));
00114          if (integral>largestInt) {
00115             largestInt = integral;
00116             histWithLargestInt = hist;
00117          }
00118       }
00119       if (histWithLargestInt == 0) {
00120          cout << "ERROR - unknown hist \"histWithLargestInt\" --> serious problem in ROOT file" << endl;
00121          break;
00122       }
00123       legend->AddEntry(histWithLargestInt,TString(histWithLargestInt->GetTitle()).ReplaceAll("MVA_",""),"l");
00124       hists.Remove(histWithLargestInt);
00125    }
00126 
00127    // rescale legend box size
00128    // current box size has been tuned for 3 MVAs + 1 title
00129    if (type == 1) {
00130       dyH *= (1.0 + Float_t(nmva - 3.0)/4.0);
00131       legend->SetY1( y0H - dyH );
00132    }
00133    else {
00134       dyH *= (Float_t(TMath::Min(10,nmva) - 3.0)/4.0);
00135       legend->SetY2( y0H + dyH);
00136    }
00137 
00138    // redraw axes
00139    frame->Draw("sameaxis");
00140    legend->Draw("same");
00141 
00142    // ============================================================
00143 
00144    if (__PLOT_LOGO__) TMVAGlob::plot_logo();
00145 
00146    // ============================================================
00147 
00148    c->Update();
00149 
00150    TString fname = "plots/" + hNameRef;
00151    if (TString(BinDir->GetName()).Contains("multicut")){
00152       TString fprepend(BinDir->GetName());
00153       fprepend.ReplaceAll("multicutMVA_","");
00154       fname = "plots/" + fprepend + "_" + hNameRef;
00155    }
00156    if (__SAVE_IMAGE__) TMVAGlob::imgconv( c, fname );
00157 
00158    return;
00159 }
00160 
00161 void efficiencies( TString fin = "TMVA.root", Int_t type = 2, Bool_t useTMVAStyle = kTRUE )
00162 {
00163    // argument: type = 1 --> plot efficiency(B) versus eff(S)
00164    //           type = 2 --> plot rejection (B) versus efficiency (S)
00165 
00166    // set style and remove existing canvas'
00167    TMVAGlob::Initialize( useTMVAStyle );
00168 
00169    // checks if file with name "fin" is already open, and if not opens one
00170    TFile* file = TMVAGlob::OpenFile( fin );
00171 
00172    // check if multi-cut MVA or only one set of MVAs
00173    Bool_t multiMVA=kFALSE;
00174    TIter nextDir(file->GetListOfKeys());
00175    TKey *key;
00176    // loop over all directories and check if
00177    // one contains the key word 'multicutMVA'
00178    while ((key = (TKey*)nextDir())) {
00179       TClass *cl = gROOT->GetClass(key->GetClassName());
00180       if (!cl->InheritsFrom("TDirectory")) continue;
00181       TDirectory *d = (TDirectory*)key->ReadObj();
00182       TString path(d->GetPath());
00183       if (path.Contains("multicutMVA")){
00184          multiMVA=kTRUE;
00185          plot_efficiencies( file, type, d );
00186       }
00187    }
00188    plot_efficiencies( file, type, gDirectory );
00189 
00190    return;
00191 }
00192 

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