variables.C

Go to the documentation of this file.
00001 #include "tmvaglob.C"
00002 
00003 // this macro plots the distributions of the different input variables
00004 // used in TMVA (e.g. running TMVAnalysis.C).  Signal and Background are overlayed.
00005 
00006 // input: - Input file (result from TMVA),
00007 //        - normal/decorrelated/PCA
00008 //        - use of TMVA plotting TStyle
00009 void variables( TString fin = "TMVA.root", TString dirName = "InputVariables_Id", TString title = "TMVA Input Variables",
00010                 Bool_t isRegression = kFALSE, Bool_t useTMVAStyle = kTRUE )
00011 {
00012    TString outfname = dirName;
00013    outfname.ToLower(); outfname.ReplaceAll( "input", ""  );
00014 
00015    // set style and remove existing canvas'
00016    TMVAGlob::Initialize( useTMVAStyle );
00017 
00018    // obtain shorter histogram title 
00019    TString htitle = title; 
00020    htitle.ReplaceAll("variables ","variable");
00021    htitle.ReplaceAll("and target(s)","");
00022    htitle.ReplaceAll("(training sample)","");
00023 
00024    // checks if file with name "fin" is already open, and if not opens one
00025    TFile* file = TMVAGlob::OpenFile( fin );
00026 
00027    TDirectory* dir = (TDirectory*)file->Get( dirName );
00028    if (dir==0) {
00029       cout << "No information about " << title << " available in directory " << dirName << " of file " << fin << endl;
00030       return;
00031    }
00032    dir->cd();
00033 
00034    // how many plots are in the directory?
00035    Int_t noPlots = TMVAGlob::GetNumberOfInputVariables( dir ) +
00036       TMVAGlob::GetNumberOfTargets( dir );
00037 
00038    // define Canvas layout here!
00039    // default setting
00040    Int_t xPad;  // no of plots in x
00041    Int_t yPad;  // no of plots in y
00042    Int_t width; // size of canvas
00043    Int_t height;
00044    switch (noPlots) {
00045    case 1:
00046       xPad = 1; yPad = 1; width = 550; height = 0.90*width; break;
00047    case 2:
00048       xPad = 2; yPad = 1; width = 600; height = 0.50*width; break;
00049    case 3:
00050       xPad = 3; yPad = 1; width = 900; height = 0.4*width; break;
00051    case 4:
00052       xPad = 2; yPad = 2; width = 600; height = width; break;
00053    default:
00054       xPad = 3; yPad = 2; width = 800; height = 0.55*width; break;
00055    }
00056 
00057    Int_t noPadPerCanv = xPad * yPad ;
00058 
00059    // counter variables
00060    Int_t countCanvas = 0;
00061    Int_t countPad    = 0;
00062 
00063    // loop over all objects in directory
00064    TCanvas* canv = 0;
00065    TKey*    key  = 0;
00066    Bool_t   createNewFig = kFALSE;
00067    TIter next(dir->GetListOfKeys());
00068    while ((key = (TKey*)next())) {
00069       if (key->GetCycle() != 1) continue;
00070 
00071       if (!TString(key->GetName()).Contains("__Signal") && 
00072           !(isRegression && TString(key->GetName()).Contains("__Regression"))) continue;
00073 
00074       // make sure, that we only look at histograms
00075       TClass *cl = gROOT->GetClass(key->GetClassName());
00076       if (!cl->InheritsFrom("TH1")) continue;
00077       TH1 *sig = (TH1*)key->ReadObj();
00078       TString hname(sig->GetName());
00079 
00080       // create new canvas
00081       if (countPad%noPadPerCanv==0) {
00082          ++countCanvas;
00083          canv = new TCanvas( Form("canvas%d", countCanvas), title,
00084                              countCanvas*50+50, countCanvas*20, width, height );
00085          canv->Divide(xPad,yPad);
00086          canv->Draw();
00087       }
00088 
00089       TPad* cPad = (TPad*)canv->cd(countPad++%noPadPerCanv+1);
00090       
00091       // find the corredponding backgrouns histo
00092       TString bgname = hname;
00093       bgname.ReplaceAll("__Signal","__Background");
00094       TH1 *bgd = (TH1*)dir->Get(bgname);
00095       if (bgd == NULL) {
00096          cout << "ERROR!!! couldn't find background histo for" << hname << endl;
00097          exit;
00098       }
00099 
00100       // this is set but not stored during plot creation in MVA_Factory
00101       TMVAGlob::SetSignalAndBackgroundStyle( sig, (isRegression ? 0 : bgd) );            
00102 
00103       sig->SetTitle( TString( htitle ) + ": " + sig->GetTitle() );
00104       TMVAGlob::SetFrameStyle( sig, 1.2 );
00105 
00106       // normalise both signal and background
00107       if (!isRegression) TMVAGlob::NormalizeHists( sig, bgd );
00108       else {
00109          // change histogram title for target
00110          TString nme = sig->GetName();
00111          if (nme.Contains( "_target" )) {
00112             TString tit = sig->GetTitle();
00113             sig->SetTitle( tit.ReplaceAll("Input variable", "Regression target" ) );
00114          }
00115       }
00116 
00117       // finally plot and overlay
00118       Float_t sc = 1.1;
00119       if (countPad == 1) sc = 1.3;
00120       sig->SetMaximum( TMath::Max( sig->GetMaximum(), bgd->GetMaximum() )*sc );
00121       sig->Draw( "hist" );
00122       cPad->SetLeftMargin( 0.17 );
00123 
00124       sig->GetYaxis()->SetTitleOffset( 1.70 );
00125       if (!isRegression) {
00126          bgd->Draw("histsame");
00127          TString ytit = TString("(1/N) ") + sig->GetYaxis()->GetTitle();
00128          sig->GetYaxis()->SetTitle( ytit ); // histograms are normalised
00129       }
00130 
00131       // Draw legend
00132       if (countPad == 1 && !isRegression) {
00133          TLegend *legend= new TLegend( cPad->GetLeftMargin(), 
00134                                        1-cPad->GetTopMargin()-.15, 
00135                                        cPad->GetLeftMargin()+.4, 
00136                                        1-cPad->GetTopMargin() );
00137          legend->SetFillStyle(1);
00138          legend->AddEntry(sig,"Signal","F");
00139          legend->AddEntry(bgd,"Background","F");
00140          legend->SetBorderSize(1);
00141          legend->SetMargin( 0.3 );
00142          legend->Draw("same");
00143       } 
00144 
00145       // redraw axes
00146       sig->Draw("sameaxis");
00147 
00148       // text for overflows
00149       Int_t    nbin = sig->GetNbinsX();
00150       Double_t dxu  = sig->GetBinWidth(0);
00151       Double_t dxo  = sig->GetBinWidth(nbin+1);
00152       TString uoflow = "";
00153       if (isRegression) {
00154          uoflow = Form( "U/O-flow: %.1f%% / %.1f%%", 
00155                         sig->GetBinContent(0)*dxu*100, sig->GetBinContent(nbin+1)*dxo*100 );
00156       }
00157       else {
00158          uoflow = Form( "U/O-flow (S,B): (%.1f, %.1f)%% / (%.1f, %.1f)%%", 
00159                         sig->GetBinContent(0)*dxu*100, bgd->GetBinContent(0)*dxu*100,
00160                         sig->GetBinContent(nbin+1)*dxo*100, bgd->GetBinContent(nbin+1)*dxo*100 );
00161       }
00162   
00163       TText* t = new TText( 0.98, 0.14, uoflow );
00164       t->SetNDC();
00165       t->SetTextSize( 0.040 );
00166       t->SetTextAngle( 90 );
00167       t->AppendPad();    
00168 
00169       // save canvas to file
00170       if (countPad%noPadPerCanv==0) {
00171          TString fname = Form( "plots/%s_c%i", outfname.Data(), countCanvas );
00172          TMVAGlob::plot_logo();
00173          TMVAGlob::imgconv( canv, fname );
00174          createNewFig = kFALSE;
00175       }
00176       else {
00177          createNewFig = kTRUE;
00178       }
00179    }
00180    
00181    if (createNewFig) {
00182       TString fname = Form( "plots/%s_c%i", outfname.Data(), countCanvas );
00183       TMVAGlob::plot_logo();
00184       TMVAGlob::imgconv( canv, fname );
00185       createNewFig = kFALSE;
00186    }
00187 
00188    return;
00189 }

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