variablesMultiClass.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 variablesMultiClass( 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    TString tmp = dirName;
00014    tmp.ReplaceAll("InputVariables_","");
00015    outfname.ToLower(); outfname.ReplaceAll( "input", ""  );
00016 
00017    // set style and remove existing canvas'
00018    TMVAGlob::Initialize( useTMVAStyle );
00019 
00020    // obtain shorter histogram title 
00021    TString htitle = title; 
00022    htitle.ReplaceAll("variables ","variable");
00023    htitle.ReplaceAll("and target(s)","");
00024    htitle.ReplaceAll("(training sample)","");
00025 
00026    // checks if file with name "fin" is already open, and if not opens one
00027    TFile* file = TMVAGlob::OpenFile( fin );
00028 
00029    TDirectory* dir = (TDirectory*)file->Get( dirName );
00030    if (dir==0) {
00031       cout << "No information about " << title << " available in directory " << dirName << " of file " << fin << endl;
00032       return;
00033    }
00034    dir->cd();
00035 
00036    // how many plots are in the directory?
00037    Int_t noPlots = TMVAGlob::GetNumberOfInputVariables( dir );
00038 
00039    // define Canvas layout here!
00040    // default setting
00041    Int_t xPad;  // no of plots in x
00042    Int_t yPad;  // no of plots in y
00043    Int_t width; // size of canvas
00044    Int_t height;
00045    switch (noPlots) {
00046    case 1:
00047       xPad = 1; yPad = 1; width = 550; height = 0.90*width; break;
00048    case 2:
00049       xPad = 2; yPad = 1; width = 600; height = 0.50*width; break;
00050    case 3:
00051       xPad = 3; yPad = 1; width = 900; height = 0.4*width; break;
00052    case 4:
00053       xPad = 2; yPad = 2; width = 600; height = width; break;
00054    default:
00055       xPad = 3; yPad = 2; width = 800; height = 0.55*width; break;
00056    }
00057 
00058    Int_t noPadPerCanv = xPad * yPad ;
00059 
00060    // counter variables
00061    Int_t countCanvas = 0;
00062    Int_t countPad    = 0;
00063 
00064    // loop over all objects in directory
00065    TCanvas* canv = 0;
00066    TKey*    key  = 0;
00067    Bool_t   createNewFig = kFALSE;
00068    TIter next(dir->GetListOfKeys());
00069     
00070    std::vector<TString> varnames(TMVAGlob::GetInputVariableNames(dir));
00071    std::vector<TString> classnames(TMVAGlob::GetClassNames(dir));
00072 
00073 
00074    std::vector<TString>::iterator variter = varnames.begin();
00075    std::vector<TString>::iterator classiter = classnames.begin();
00076 
00077    /*
00078    std::vector<TString>::const_iterator variter = varnames.begin();
00079    std::cout << "Available variables:" << std::endl;
00080    while(variter != varnames.end()){
00081       std::cout << *variter << std::endl;
00082       variter++;
00083    }
00084    
00085    std::vector<TString>::const_iterator classiter = classnames.begin();
00086    std::cout << "Available classes:" << std::endl;
00087    while(classiter != classnames.end()){
00088       std::cout << *classiter << std::endl;
00089       classiter++;
00090    }
00091    */
00092    
00093    variter = varnames.begin();
00094    for(; variter!=varnames.end(); ++variter){
00095       
00096       //create new canvas
00097       if (countPad%noPadPerCanv==0) {
00098          ++countCanvas;
00099          canv = new TCanvas( Form("canvas%d", countCanvas), title,
00100                              countCanvas*50+50, countCanvas*20, width, height );
00101          canv->Divide(xPad,yPad);
00102          canv->Draw();
00103       }
00104       TPad* cPad = (TPad*)canv->cd(countPad++%noPadPerCanv+1);
00105       classiter = classnames.begin();
00106       
00107       TObjArray hists;
00108       for(; classiter!=classnames.end(); ++classiter){
00109          //assemble histogram names
00110          TString hname(*variter + "__" + *classiter + "_" + tmp);
00111          TH1 *hist = (TH1*)dir->Get(hname);
00112          //cout << "Looking for histgram " << hname << endl;
00113          if (hist == NULL) {
00114             cout << "ERROR!!! couldn't find " << *variter << " histogram for class " << *classiter << endl;
00115             exit;
00116          }
00117          hists.Add(hist);
00118       }
00119       
00120       // this is set but not stored during plot creation in MVA_Factory  
00121       //TMVAGlob::SetSignalAndBackgroundStyle(((TH1*)hists[0]), ((TH1*)hists[1]));            
00122       TMVAGlob::SetMultiClassStyle( &hists ); 
00123       
00124       ((TH1*)hists.First())->SetTitle( TString( htitle ) + ": " + *variter );
00125 
00126       TMVAGlob::SetFrameStyle( ((TH1*)hists.First()), 1.2 );
00127       
00128       // normalise all histograms and find maximum
00129       Float_t histmax = -1;
00130       for(Int_t i=0; i<hists.GetEntriesFast(); ++i){
00131          TMVAGlob::NormalizeHist((TH1*)hists[i] );
00132          if(((TH1*)hists[i])->GetMaximum() > histmax)
00133             histmax = ((TH1*)hists[i])->GetMaximum();
00134       }
00135       
00136       // finally plot and overlay
00137       Float_t sc = 1.1;
00138       if (countPad == 1) sc = 1.3;
00139       ((TH1*)hists.First())->SetMaximum( histmax*sc );
00140       
00141       ((TH1*)hists.First())->Draw( "hist" );
00142       cPad->SetLeftMargin( 0.17 );
00143       ((TH1*)hists.First())->GetYaxis()->SetTitleOffset( 1.70 );
00144       
00145       for(Int_t i=1; i<hists.GetEntriesFast(); ++i){
00146 
00147          ((TH1*)hists[i])->Draw("histsame");
00148          TString ytit = TString("(1/N) ") + ((TH1*)hists[i])->GetYaxis()->GetTitle();
00149          ((TH1*)hists[i])->GetYaxis()->SetTitle( ytit ); // histograms are normalised
00150       
00151       }
00152            
00153       // Draw legend
00154       if (countPad == 1) {
00155          TLegend *legend= new TLegend( cPad->GetLeftMargin(), 
00156                                        1-cPad->GetTopMargin()-.15, 
00157                                        cPad->GetLeftMargin()+.4, 
00158                                        1-cPad->GetTopMargin() );
00159          legend->SetFillStyle(1);
00160          
00161          classiter = classnames.begin();
00162 
00163          for(Int_t i=0; i<hists.GetEntriesFast(); ++i, ++classiter){
00164             legend->AddEntry(((TH1*)hists[i]),*classiter,"F");
00165          }
00166 
00167          legend->SetBorderSize(1);
00168          legend->SetMargin( 0.3 );
00169          legend->Draw("same");
00170       } 
00171 
00172       // redraw axes
00173       ((TH1*)hists.First())->Draw("sameaxis");
00174 
00175       
00176       // text for overflows
00177       Int_t    nbin = ((TH1*)hists.First())->GetNbinsX();
00178       Double_t dxu  = ((TH1*)hists.First())->GetBinWidth(0);
00179       Double_t dxo  = ((TH1*)hists.First())->GetBinWidth(nbin+1);
00180       TString uoflow = "";
00181            
00182       classiter = classnames.begin();
00183       for(Int_t i=0; i<hists.GetEntriesFast(); ++i, ++classiter){
00184          if(((TH1*)hists[i])->GetBinContent(0)!=0 || ((TH1*)hists[i])->GetBinContent(nbin+1)!=0){
00185             uoflow += *classiter;
00186             uoflow += Form( " U/O-flow:  %.1f / %.1f %%", 
00187                             ((TH1*)hists[i])->GetBinContent(0)*dxu*100, ((TH1*)hists[i])->GetBinContent(nbin+1)*dxo*100);
00188          }
00189       }
00190       
00191       TText* t = new TText( 0.98, 0.14, uoflow );
00192       t->SetNDC();
00193       t->SetTextSize( 0.040 );
00194       t->SetTextAngle( 90 );
00195       t->AppendPad();    
00196       
00197       
00198       // save canvas to file
00199       if (countPad%noPadPerCanv==0) {
00200          TString fname = Form( "plots/%s_c%i", outfname.Data(), countCanvas );
00201          TMVAGlob::plot_logo();
00202          TMVAGlob::imgconv( canv, fname );
00203          createNewFig = kFALSE;
00204       }
00205       else {
00206          createNewFig = kTRUE;
00207       }
00208    } 
00209    
00210    if (createNewFig) {
00211       TString fname = Form( "plots/%s_c%i", outfname.Data(), countCanvas );
00212       TMVAGlob::plot_logo();
00213       TMVAGlob::imgconv( canv, fname );
00214       createNewFig = kFALSE;
00215    }
00216 
00217    return;
00218 }

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