paracoor.C

Go to the documentation of this file.
00001 #include "tmvaglob.C"
00002 
00003 // plot parallel coordinates
00004 
00005 void paracoor( TString fin = "TMVA.root", Bool_t useTMVAStyle = kTRUE )
00006 {
00007    // set style and remove existing canvas'
00008    TMVAGlob::Initialize( useTMVAStyle );
00009 
00010    // checks if file with name "fin" is already open, and if not opens one
00011    TFile* file = TMVAGlob::OpenFile( fin );  
00012    TTree* tree = (TTree*)file->Get("TestTree");
00013    if(!tree) {
00014       cout << "--- No TestTree saved in ROOT file. Parallel coordinates will not be plotted" << endl;
00015       return;
00016    }
00017 
00018    // first get list of leaves in tree
00019    TObjArray* leafList = tree->GetListOfLeaves();
00020    vector<TString> vars;
00021    vector<TString> mvas;   
00022    for (Int_t iar=0; iar<leafList->GetSize(); iar++) {
00023       TLeaf* leaf = (TLeaf*)leafList->At(iar);
00024       if (leaf != 0) {
00025          TString leafName = leaf->GetName();
00026          if (leafName != "type" && leafName != "weight"  && leafName != "boostweight" &&
00027              leafName != "class" && leafName != "className" && leafName != "classID" && 
00028              !leafName.Contains("prob_")) {
00029             // is MVA ?
00030             if (TMVAGlob::ExistMethodName( leafName )) {
00031                mvas.push_back( leafName );
00032             }
00033             else {
00034                vars.push_back( leafName );
00035             }
00036          }
00037       }
00038    }
00039 
00040    cout << "--- Found: " << vars.size() << " variables" << endl;
00041    cout << "--- Found: " << mvas.size() << " MVA(s)" << endl;
00042    
00043 
00044    TString type[2] = { "Signal", "Background" };
00045    const Int_t nmva = mvas.size();
00046    TCanvas* csig[nmva];
00047    TCanvas* cbkg[nmva];
00048    for (Int_t imva=0; imva<mvas.size(); imva++) {
00049       cout << "--- Plotting parallel coordinates for : " << mvas[imva] << " & input variables" << endl;
00050 
00051       for (Int_t itype=0; itype<2; itype++) {
00052 
00053          // create draw option
00054          TString varstr = mvas[imva] + ":";
00055          for (Int_t ivar=0; ivar<vars.size(); ivar++) varstr += vars[ivar] + ":";
00056          varstr.Resize( varstr.Last( ':' ) );
00057 
00058          // create canvas
00059          TString mvashort = mvas[imva]; mvashort.ReplaceAll("MVA_","");
00060          TCanvas* c1 = (itype == 0) ? csig[imva] : cbkg[imva];
00061          c1 = new TCanvas( Form( "c1_%i",itype ), 
00062                            Form( "Parallel coordinate representation for %s and input variables (%s events)", 
00063                                  mvashort.Data(), type[itype].Data() ), 
00064                            50*(itype), 50*(itype), 750, 500 );      
00065          tree->Draw( varstr.Data(), Form("classID==%i",1-itype) , "para" );
00066          c1->ToggleEditor();
00067          gStyle->SetOptTitle(0);
00068 
00069          TParallelCoord*    para   = (TParallelCoord*)gPad->GetListOfPrimitives()->FindObject( "ParaCoord" );
00070          TParallelCoordVar* mvavar = (TParallelCoordVar*)para->GetVarList()->FindObject( mvas[imva] );
00071          Double_t minrange = tree->GetMinimum( mvavar->GetName() );
00072          Double_t maxrange = tree->GetMaximum( mvavar->GetName() );
00073          Double_t width    = 0.2*(maxrange - minrange);
00074          Double_t x1 = minrange, x2 = x1 + width;
00075          TParallelCoordRange* parrange = new TParallelCoordRange( mvavar, x1, x2 );
00076          parrange->SetLineColor(4);
00077          mvavar->AddRange( parrange );
00078 
00079          para->AddSelection("-1");
00080 
00081          for (Int_t ivar=1; ivar<TMath::Min(Int_t(vars.size()) + 1,3); ivar++) {
00082             TParallelCoordVar* var = (TParallelCoordVar*)para->GetVarList()->FindObject( vars[ivar] );
00083             minrange = tree->GetMinimum( var->GetName() );
00084             maxrange = tree->GetMaximum( var->GetName() );
00085             width    = 0.2*(maxrange - minrange);
00086 
00087             switch (ivar) {
00088             case 0: { x1 = minrange; x2 = x1 + width; break; }
00089             case 1: { x1 = 0.5*(maxrange + minrange - width)*0.02; x2 = x1 + width*0.02; break; }
00090             case 2: { x1 = maxrange - width; x2 = x1 + width; break; }
00091             }
00092 
00093             parrange = new TParallelCoordRange( var, x1, x2 );
00094             parrange->SetLineColor( ivar == 0 ? 2 : ivar == 1 ? 5 : 6 );
00095             var->AddRange( parrange );
00096 
00097             para->AddSelection( Form("%i",ivar) );
00098          }
00099 
00100          c1->Update();
00101 
00102          TString fname = Form( "plots/paracoor_c%i_%s", imva, itype == 0 ? "S" : "B" );
00103          TMVAGlob::imgconv( c1, fname );
00104       }
00105    }
00106 }
00107 

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