rulevisHists.C

Go to the documentation of this file.
00001 #include "tmvaglob.C"
00002 
00003 // This macro plots the distributions of the different input variables overlaid on
00004 // the sum of importance per bin.
00005 // The scale goes from violett (no importance) to red (high importance).
00006 // Areas where many important rules are active, will thus be very red.
00007 //
00008 // input: - Input file (result from TMVA),
00009 //        - normal/decorrelated/PCA
00010 //        - use of TMVA plotting TStyle
00011 void rulevisHists( TString fin = "TMVA.root", TMVAGlob::TypeOfPlot type = TMVAGlob::kNorm, bool useTMVAStyle=kTRUE )
00012 {
00013    // set style and remove existing canvas'
00014    TMVAGlob::Initialize( useTMVAStyle );
00015    
00016    // checks if file with name "fin" is already open, and if not opens one
00017    TFile *file = TMVAGlob::OpenFile( fin );
00018 
00019    // get all titles of the method rulefit
00020    TList titles;
00021    UInt_t ninst = TMVAGlob::GetListOfTitles("Method_RuleFit",titles);
00022    if (ninst==0) return;
00023 
00024    // get top dir containing all hists of the variables
00025    TDirectory* vardir = TMVAGlob::GetInputVariablesDir( type );
00026    if (vardir==0) return;
00027 
00028    TDirectory* corrdir = TMVAGlob::GetCorrelationPlotsDir( type, vardir );
00029    if (corrdir==0) return;
00030 
00031    // loop over all titles
00032    TIter keyIter(&titles);
00033    TDirectory *rfdir;
00034    TKey *rfkey;
00035    while ((rfkey = TMVAGlob::NextKey(keyIter,"TDirectory"))) {
00036       rfdir = (TDirectory *)rfkey->ReadObj();
00037       rulevisHists( rfdir, vardir, corrdir, type );
00038    }
00039 }
00040 
00041 void rulevisHists( TDirectory *rfdir, TDirectory *vardir, TDirectory *corrdir, TMVAGlob::TypeOfPlot type) {
00042    //
00043    if (rfdir==0)   return;
00044    if (vardir==0)  return;
00045    if (corrdir==0) return;
00046    //
00047    const TString rfName    = rfdir->GetName();
00048    const TString maintitle = rfName + " : Rule Importance";
00049    const TString rfNameOpt = "_RF2D_";
00050    const TString outfname[TMVAGlob::kNumOfMethods] = { "rulevisHists",
00051                                                        "rulevisHists_decorr",
00052                                                        "rulevisCorr_pca",
00053                                                        "rulevisCorr_gaussdecorr" };
00054 
00055    const TString outputName = outfname[type]+"_"+rfdir->GetName();
00056    //
00057    TIter rfnext(rfdir->GetListOfKeys());
00058    TKey *rfkey;
00059    Double_t rfmax;
00060    Double_t rfmin;
00061    Bool_t allEmpty=kTRUE;
00062    Bool_t first=kTRUE;
00063    while ((rfkey = (TKey*)rfnext())) {
00064       // make sure, that we only look at histograms
00065       TClass *cl = gROOT->GetClass(rfkey->GetClassName());
00066       if (!cl->InheritsFrom("TH2F")) continue;
00067       TH2F *hrf = (TH2F*)rfkey->ReadObj();
00068       TString hname= hrf->GetName();
00069       if (hname.Contains("__RF_")){ // found a new RF plot
00070          Double_t valmin = hrf->GetMinimum();
00071          Double_t valmax = hrf->GetMaximum();
00072          if (first) {
00073             rfmin=valmin;
00074             rfmax=valmax;
00075             first = kFALSE;
00076          } else {
00077             if (valmax>rfmax) rfmax=valmax;
00078             if (valmin<rfmin) rfmin=valmin;
00079          }
00080          if (hrf->GetEntries()>0) allEmpty=kFALSE;
00081       }
00082    }
00083    if (first) {
00084       cout << "ERROR: no RF plots found..." << endl;
00085       return;
00086    }
00087 
00088    const Int_t nContours = 100;
00089    Double_t contourLevels[nContours];
00090    Double_t dcl = (rfmax-rfmin)/Double_t(nContours-1);
00091    //
00092    for (Int_t i=0; i<nContours; i++) {
00093       contourLevels[i] = rfmin+dcl*Double_t(i);
00094    }
00095 
00096    ///////////////////////////
00097    vardir->cd();
00098  
00099    // how many plots are in the directory?
00100    Int_t noPlots = ((vardir->GetListOfKeys())->GetEntries()) / 2;
00101  
00102    // define Canvas layout here!
00103    // default setting
00104    Int_t xPad;  // no of plots in x
00105    Int_t yPad;  // no of plots in y
00106    Int_t width; // size of canvas
00107    Int_t height;
00108    switch (noPlots) {
00109    case 1:
00110       xPad = 1; yPad = 1; width = 500; height = 0.7*width; break;
00111    case 2:
00112       xPad = 2; yPad = 1; width = 600; height = 0.7*width; break;
00113    case 3:
00114       xPad = 3; yPad = 1; width = 900; height = 0.4*width; break;
00115    case 4:
00116       xPad = 2; yPad = 2; width = 600; height = width; break;
00117    default:
00118       xPad = 3; yPad = 2; width = 800; height = 0.7*width; break;
00119    }
00120    Int_t noPad = xPad * yPad ;   
00121 
00122    // this defines how many canvases we need
00123    const Int_t noCanvas = 1 + (Int_t)((noPlots - 0.001)/noPad);
00124    TCanvas **c = new TCanvas*[noCanvas];
00125    for (Int_t ic=0; ic<noCanvas; ic++) c[ic] = 0;
00126 
00127    // counter variables
00128    Int_t countCanvas = 0;
00129    Int_t countPad    = 1;
00130 
00131    // loop over all objects in directory
00132    TIter next(vardir->GetListOfKeys());
00133    TKey *key;
00134    TH1F *sigCpy=0;
00135    TH1F *bgdCpy=0;
00136    //
00137    Bool_t first = kTRUE;
00138 
00139    while ((key = (TKey*)next())) {
00140 
00141       // make sure, that we only look at histograms
00142       TClass *cl = gROOT->GetClass(key->GetClassName());
00143       if (!cl->InheritsFrom("TH1")) continue;
00144       sig = (TH1F*)key->ReadObj();
00145       TString hname= sig->GetName();
00146 
00147       // check for all signal histograms
00148       if (hname.Contains("__S")){ // found a new signal plot
00149          //         sigCpy = new TH1F(*sig);
00150          // create new canvas
00151          if ((c[countCanvas]==NULL) || (countPad>noPad)) {
00152             char cn[20];
00153             sprintf( cn, "rulehist%d_", countCanvas+1 );
00154             TString cname(cn);
00155             cname += rfdir->GetName();
00156             c[countCanvas] = new TCanvas( cname, maintitle,
00157                                           countCanvas*50+200, countCanvas*20, width, height ); 
00158             // style
00159             c[countCanvas]->Divide(xPad,yPad);
00160             countPad = 1;
00161          }       
00162 
00163          // save canvas to file
00164          TPad *cPad = (TPad *)(c[countCanvas]->GetPad(countPad));
00165          c[countCanvas]->cd(countPad);
00166          countPad++;
00167 
00168          // find the corredponding background histo
00169          TString bgname = hname;
00170          bgname.ReplaceAll("__S","__B");
00171          hkey = vardir->GetKey(bgname);
00172          bgd = (TH1F*)hkey->ReadObj();
00173          if (bgd == NULL) {
00174             cout << "ERROR!!! couldn't find backgroung histo for" << hname << endl;
00175             exit;
00176          }
00177 
00178          TString rfname = hname;
00179          rfname.ReplaceAll("__S","__RF");
00180          TKey *hrfkey = rfdir->GetKey(rfname);
00181          TH2F *hrf = (TH2F*)hrfkey->ReadObj();
00182          Double_t wv = hrf->GetMaximum();
00183          //         if (rfmax>0.0)
00184          //            hrf->Scale(1.0/rfmax);
00185          hrf->SetMinimum(rfmin); // make sure it's zero  -> for palette axis
00186          hrf->SetMaximum(rfmax); // make sure max is 1.0 -> idem
00187          hrf->SetContour(nContours,&contourLevels[0]);
00188 
00189          // this is set but not stored during plot creation in MVA_Factory
00190          //         TMVAGlob::SetSignalAndBackgroundStyle( sigK, bgd );
00191          sig->SetFillStyle(3002);
00192          sig->SetFillColor(1);
00193          sig->SetLineColor(1);
00194          sig->SetLineWidth(2);
00195 
00196          bgd->SetFillStyle(3554);
00197          bgd->SetFillColor(1);
00198          bgd->SetLineColor(1);
00199          bgd->SetLineWidth(2);
00200 
00201          // chop off "signal" 
00202          TString title(hrf->GetTitle());
00203          title.ReplaceAll("signal","");
00204 
00205          // finally plot and overlay       
00206          Float_t sc = 1.1;
00207          if (countPad==2) sc = 1.3;
00208          sig->SetMaximum( TMath::Max( sig->GetMaximum(), bgd->GetMaximum() )*sc );
00209          Double_t smax = sig->GetMaximum();
00210 
00211          if (first) {
00212             hrf->SetTitle( maintitle );
00213             first = kFALSE;
00214          } else {
00215             hrf->SetTitle( "" );
00216          }
00217          hrf->Draw("colz ah");
00218          TMVAGlob::SetFrameStyle( hrf, 1.2 );
00219 
00220          sig->Draw("same ah");
00221          bgd->Draw("same ah");
00222          // draw axis using range [0,smax]
00223          hrf->GetXaxis()->SetTitle( title );
00224          hrf->GetYaxis()->SetTitleOffset( 1.30 );
00225          hrf->GetYaxis()->SetTitle("Events");
00226          hrf->GetYaxis()->SetLimits(0,smax);
00227          hrf->Draw("same axis");
00228 
00229          cPad->SetRightMargin(0.13);
00230          cPad->Update();
00231 
00232          // Draw legend
00233          if (countPad==2){
00234             TLegend *legend= new TLegend( cPad->GetLeftMargin(), 
00235                                           1-cPad->GetTopMargin()-.18, 
00236                                           cPad->GetLeftMargin()+.4, 
00237                                           1-cPad->GetTopMargin() );
00238             legend->AddEntry(sig,"Signal","F");
00239             legend->AddEntry(bgd,"Background","F");
00240             legend->Draw("same");
00241             legend->SetBorderSize(1);
00242             legend->SetMargin( 0.3 );
00243             legend->SetFillColor(19);
00244             legend->SetFillStyle(1);
00245          } 
00246 
00247          // save canvas to file
00248          if (countPad > noPad) {
00249             c[countCanvas]->Update();
00250             TString fname = Form( "plots/%s_c%i", outputName.Data(), countCanvas+1 );
00251             TMVAGlob::imgconv( c[countCanvas], fname );
00252             //        TMVAGlob::plot_logo(); // don't understand why this doesn't work ... :-(
00253             countCanvas++;
00254          }
00255       }
00256    }
00257 
00258    if (countPad <= noPad) {
00259       c[countCanvas]->Update();
00260       TString fname = Form( "plots/%s_c%i", outputName.Data(), countCanvas+1 );
00261       TMVAGlob::imgconv( c[countCanvas], fname );
00262    }
00263 }

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