mvaweights.C

Go to the documentation of this file.
00001 #include "tmvaglob.C"
00002 
00003 // input: - Input file (result from TMVA)
00004 //        - use of TMVA plotting TStyle
00005 void mvaweights( TString fin = "TMVA.root", Bool_t useTMVAStyle = kTRUE )
00006 {
00007    // set style and remove existing canvas'
00008    TMVAGlob::Initialize( useTMVAStyle );
00009 
00010    // few modifications
00011    TStyle *TMVAStyle = gROOT->GetStyle("Plain"); // our style is based on Plain
00012    TMVAStyle->SetTitleW(0.94);
00013    TMVAStyle->SetTitleH(.06);
00014 
00015    TString varx = "var3";
00016    TString vary = "var4";
00017 
00018    // switches
00019    const Bool_t Save_Images     = kTRUE;
00020 
00021    // checks if file with name "fin" is already open, and if not opens one   
00022    TFile* file = TMVAGlob::OpenFile( fin );  
00023    if (!file) {
00024       cout << "Cannot open flie: " << fin << endl;
00025       return;
00026    }
00027 
00028    // define Canvas layout here!
00029    const Int_t width = 500;   // size of canvas
00030 
00031    // this defines how many canvases we need
00032    TCanvas *c = 0;
00033 
00034    // counter variables
00035    Int_t countCanvas = 0;
00036    
00037    // retrieve trees
00038    TTree *tree = (TTree*)file->Get( "TestTree" );
00039 
00040    // search for the right histograms in full list of keys
00041    TObjArray* branches = tree->GetListOfBranches();
00042    for (Int_t imva=0; imva<branches->GetEntries(); imva++) {
00043       TBranch* b = (TBranch*)(*branches)[imva];
00044       TString methodS = b->GetName();
00045       cout << "Use MVA output of Method " << methodS <<endl; 
00046       
00047       if (!methodS.BeginsWith("MVA_") || methodS.EndsWith("_Proba")) continue;
00048       if (methodS.Contains("Cuts") ) continue;
00049       
00050       methodS.Remove(0,4);
00051       cout << "--- Found variable: \"" << methodS << "\"" << endl;      
00052       
00053       // create new canvas
00054       TString cname = Form("TMVA output %s",methodS.Data());
00055       c = new TCanvas( Form("canvas%d", countCanvas+1), cname, 
00056                        countCanvas*50+200, countCanvas*20, width, width*1.0 ); 
00057       c->Divide( 1, 1 );
00058           
00059       // set the histogram style
00060       Float_t xmin = tree->GetMinimum( varx );
00061       Float_t xmax = tree->GetMaximum( varx );
00062       Float_t ymin = tree->GetMinimum( vary );
00063       Float_t ymax = tree->GetMaximum( vary );
00064       
00065       Int_t nbin = 100;
00066       TH2F* frame   = new TH2F( "frame",  "frame",  nbin, xmin, xmax, nbin, ymin, ymax );
00067       TH2F* frameS  = new TH2F( "DataS",  "DataS",  nbin, xmin, xmax, nbin, ymin, ymax );
00068       TH2F* frameB  = new TH2F( "DataB",  "DataB",  nbin, xmin, xmax, nbin, ymin, ymax );
00069       TH2F* frameRS = new TH2F( "DataRS", "DataRS", nbin, xmin, xmax, nbin, ymin, ymax );
00070       TH2F* frameRB = new TH2F( "DataRB", "DataRB", nbin, xmin, xmax, nbin, ymin, ymax );
00071 
00072       Int_t nbinC = 20;
00073       TH2F* refS = new TH2F( "RefS", "RefS", nbinC, xmin, xmax, nbinC, ymin, ymax );
00074       TH2F* refB = new TH2F( "RefB", "RefB", nbinC, xmin, xmax, nbinC, ymin, ymax );
00075       
00076       Float_t mvaMin = tree->GetMinimum( Form( "MVA_%s", methodS.Data() ) );
00077       Float_t mvaMax = tree->GetMaximum( Form( "MVA_%s", methodS.Data() ) );
00078 
00079       // project trees
00080       TString expr = Form( "((MVA_%s-(%f))/(%f-(%f)))", methodS.Data(), mvaMin, mvaMax, mvaMin );
00081       cout << "Expression = " << expr << endl;
00082       tree->Project( "DataS", Form( "%s:%s", vary.Data(), varx.Data() ), 
00083                      Form( "%s*(type==1)", expr.Data() ) );
00084       tree->Project( "DataB", Form( "%s:%s", vary.Data(), varx.Data() ), 
00085                      Form( "%s*(type==0)", expr.Data() ) );
00086       tree->Project( "DataRS", Form( "%s:%s", vary.Data(), varx.Data() ), 
00087                      Form( "type==1", methodS.Data() ) );
00088       tree->Project( "DataRB", Form( "%s:%s", vary.Data(), varx.Data() ), 
00089                      Form( "type==0", methodS.Data() ) );
00090       tree->Project( "RefS", Form( "%s:%s", vary.Data(), varx.Data() ), 
00091                      Form( "type==1", methodS.Data() ), "", 500000  );
00092       tree->Project( "RefB", Form( "%s:%s", vary.Data(), varx.Data() ), 
00093                      Form( "type==0", methodS.Data() ), "", 500000, 10000 );
00094 
00095       Float_t zminS = frameS->GetMinimum();
00096       Float_t zmaxS = frameS->GetMaximum();
00097       Float_t zminB = frameB->GetMinimum();
00098       Float_t zmaxB = frameB->GetMaximum();
00099       // normalise      
00100       for (Int_t i=1; i<=nbin; i++) {
00101          for (Int_t j=1; j<=nbin; j++) {
00102             // signal
00103             Float_t z = frameS->GetBinContent( i, j );
00104             z = (z - zminS)/(zmaxS - zminS);
00105             Float_t zr = frameRS->GetBinContent( i, j );
00106             if (zr > 0) z /= zr;
00107             else z = 0.;
00108             frameS->SetBinContent( i, j, z );
00109 
00110             // background
00111             z = frameB->GetBinContent( i, j );
00112             z = (z - zminB)/(zmaxB - zminB);
00113             z = 1 - z;
00114             zr = frameRB->GetBinContent( i, j );
00115             if (zr > 0) z /= zr;
00116             else z = 0.;
00117             frameB->SetBinContent( i, j, z );
00118          }
00119       }
00120       zminS = frameS->GetMinimum();
00121       zmaxS = frameS->GetMaximum();
00122       zminB = frameB->GetMinimum();
00123       zmaxB = frameB->GetMaximum();
00124       // renormalise      
00125       for (Int_t i=1; i<=nbin; i++) {
00126          for (Int_t j=1; j<=nbin; j++) {
00127             // signal
00128             Float_t z = frameS->GetBinContent( i, j );
00129             z = 1*(z - zminS)/(zmaxS - zminS) - 0;
00130             frameS->SetBinContent( i, j, z );
00131 
00132             // background
00133             z = frameB->GetBinContent( i, j );
00134             z = 1*(z - zminB)/(zmaxB - zminB) - 0;
00135             frameB->SetBinContent( i, j, z );
00136          }
00137       }
00138       frame ->SetMinimum( -1.0 );
00139       frame ->SetMaximum( +1.0 );
00140       frameS->SetMinimum( -1.0 );
00141       frameS->SetMaximum( +1.0 );
00142       frameB->SetMinimum( -1.0 );
00143       frameB->SetMaximum( +1.0 );
00144       
00145       // axis labels
00146       frame->SetTitle( Form( "Signal and background distributions weighted by %s output", 
00147                              methodS.Data() ) );
00148       frame->SetTitleSize( 0.08 );
00149       frame->GetXaxis()->SetTitle( varx );
00150       frame->GetYaxis()->SetTitle( vary );
00151 
00152       // style
00153       frame->SetLabelSize( 0.04, "X" );
00154       frame->SetLabelSize( 0.04, "Y" );
00155       frame->SetTitleSize( 0.05, "X" );
00156       frame->SetTitleSize( 0.05, "Y" );
00157       frame->GetYaxis()->SetTitleOffset( 1.05);// label offset on x axis
00158       frame->GetYaxis()->SetTitleOffset( 1.30 );// label offset on x axis
00159 
00160       // now the weighted functions
00161       const Int_t nlevels = 3;
00162       Double_t levelsS[nlevels];
00163       Double_t levelsB[nlevels];
00164       levelsS[0] = 0.3;
00165       levelsS[1] = 0.5;
00166       levelsS[2] = 0.7;
00167       levelsB[0] = -0.3;
00168       levelsB[1] = 0.2;
00169       levelsB[2] = 0.5;
00170       frameS->SetContour( nlevels, levelsS );
00171       frameB->SetContour( nlevels, levelsB );
00172 
00173       frameS->SetLineColor( 104 );
00174       frameS->SetFillColor( 104 );
00175       frameS->SetLineWidth( 3 );
00176       frameB->SetLineColor( 102 );
00177       frameB->SetFillColor( 102 );
00178       frameB->SetLineWidth( 3 );
00179 
00180       // set style
00181       refS->SetMarkerSize( 0.2 );
00182       refS->SetMarkerColor( 104 );
00183       
00184       refB->SetMarkerSize( 0.2 );
00185       refB->SetMarkerColor( 102 );
00186 
00187       const Int_t nlevelsR = 1;
00188       Double_t levelsRS[nlevelsR];
00189       Double_t levelsRB[nlevelsR];
00190       levelsRS[0] = refS->GetMaximum()*0.3;
00191       //      levelsRS[1] = refS->GetMaximum()*0.3;
00192       levelsRB[0] = refB->GetMaximum()*0.3;
00193       //      levelsRB[1] = refB->GetMaximum()*0.3;
00194       refS->SetContour( nlevelsR, levelsRS );
00195       refB->SetContour( nlevelsR, levelsRB );
00196 
00197       refS->SetLineColor( 104 );
00198       refS->SetFillColor( 104 );
00199       refS->SetLineWidth( 3 );
00200       refB->SetLineColor( 102 );
00201       refB->SetFillColor( 102 );
00202       refB->SetLineWidth( 3 );
00203 
00204       // and plot
00205       c->cd(1);
00206       
00207       frame->Draw();
00208       frameS->Draw( "contsame" );
00209       refS->Draw( "cont3same" );
00210       refB->Draw( "cont3same" );  
00211       //      frameB->Draw( "colzsame" );
00212 
00213       // save canvas to file
00214       c->Update();
00215       if (Save_Images) {
00216          TMVAGlob::imgconv( c, Form("plots/mvaweights_%s",   methodS.Data()) );
00217       }
00218       countCanvas++;
00219    }
00220 }

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