ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/benhoob/HWW/paracoor.C
Revision: 1.1
Committed: Mon Feb 14 12:39:14 2011 UTC (14 years, 3 months ago) by benhoob
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Log Message:
Initial commit

File Contents

# User Rev Content
1 benhoob 1.1 #include "tmvaglob.C"
2    
3     // plot parallel coordinates
4    
5     void paracoor( TString fin = "TMVA.root", Bool_t useTMVAStyle = kTRUE )
6     {
7     // set style and remove existing canvas'
8     TMVAGlob::Initialize( useTMVAStyle );
9    
10     // checks if file with name "fin" is already open, and if not opens one
11     TFile* file = TMVAGlob::OpenFile( fin );
12     TTree* tree = (TTree*)file->Get("TestTree");
13     if(!tree) {
14     cout << "--- No TestTree saved in ROOT file. Parallel coordinates will not be plotted" << endl;
15     return;
16     }
17    
18     // first get list of leaves in tree
19     TObjArray* leafList = tree->GetListOfLeaves();
20     vector<TString> vars;
21     vector<TString> mvas;
22     for (Int_t iar=0; iar<leafList->GetSize(); iar++) {
23     TLeaf* leaf = (TLeaf*)leafList->At(iar);
24     if (leaf != 0) {
25     TString leafName = leaf->GetName();
26     if (leafName != "type" && leafName != "weight" && leafName != "boostweight" &&
27     leafName != "class" && leafName != "className" && leafName != "classID" &&
28     !leafName.Contains("prob_")) {
29     // is MVA ?
30     if (TMVAGlob::ExistMethodName( leafName )) {
31     mvas.push_back( leafName );
32     }
33     else {
34     vars.push_back( leafName );
35     }
36     }
37     }
38     }
39    
40     cout << "--- Found: " << vars.size() << " variables" << endl;
41     cout << "--- Found: " << mvas.size() << " MVA(s)" << endl;
42    
43    
44     TString type[2] = { "Signal", "Background" };
45     const Int_t nmva = mvas.size();
46     TCanvas* csig[nmva];
47     TCanvas* cbkg[nmva];
48     for (Int_t imva=0; imva<mvas.size(); imva++) {
49     cout << "--- Plotting parallel coordinates for : " << mvas[imva] << " & input variables" << endl;
50    
51     for (Int_t itype=0; itype<2; itype++) {
52    
53     // create draw option
54     TString varstr = mvas[imva] + ":";
55     for (Int_t ivar=0; ivar<vars.size(); ivar++) varstr += vars[ivar] + ":";
56     varstr.Resize( varstr.Last( ':' ) );
57    
58     // create canvas
59     TString mvashort = mvas[imva]; mvashort.ReplaceAll("MVA_","");
60     TCanvas* c1 = (itype == 0) ? csig[imva] : cbkg[imva];
61     c1 = new TCanvas( Form( "c1_%i",itype ),
62     Form( "Parallel coordinate representation for %s and input variables (%s events)",
63     mvashort.Data(), type[itype].Data() ),
64     50*(itype), 50*(itype), 750, 500 );
65     tree->Draw( varstr.Data(), Form("classID==%i",1-itype) , "para" );
66     c1->ToggleEditor();
67     gStyle->SetOptTitle(0);
68    
69     TParallelCoord* para = (TParallelCoord*)gPad->GetListOfPrimitives()->FindObject( "ParaCoord" );
70     TParallelCoordVar* mvavar = (TParallelCoordVar*)para->GetVarList()->FindObject( mvas[imva] );
71     Double_t minrange = tree->GetMinimum( mvavar->GetName() );
72     Double_t maxrange = tree->GetMaximum( mvavar->GetName() );
73     Double_t width = 0.2*(maxrange - minrange);
74     Double_t x1 = minrange, x2 = x1 + width;
75     TParallelCoordRange* parrange = new TParallelCoordRange( mvavar, x1, x2 );
76     parrange->SetLineColor(4);
77     mvavar->AddRange( parrange );
78    
79     para->AddSelection("-1");
80    
81     for (Int_t ivar=1; ivar<TMath::Min(Int_t(vars.size()) + 1,3); ivar++) {
82     TParallelCoordVar* var = (TParallelCoordVar*)para->GetVarList()->FindObject( vars[ivar] );
83     minrange = tree->GetMinimum( var->GetName() );
84     maxrange = tree->GetMaximum( var->GetName() );
85     width = 0.2*(maxrange - minrange);
86    
87     switch (ivar) {
88     case 0: { x1 = minrange; x2 = x1 + width; break; }
89     case 1: { x1 = 0.5*(maxrange + minrange - width)*0.02; x2 = x1 + width*0.02; break; }
90     case 2: { x1 = maxrange - width; x2 = x1 + width; break; }
91     }
92    
93     parrange = new TParallelCoordRange( var, x1, x2 );
94     parrange->SetLineColor( ivar == 0 ? 2 : ivar == 1 ? 5 : 6 );
95     var->AddRange( parrange );
96    
97     para->AddSelection( Form("%i",ivar) );
98     }
99    
100     c1->Update();
101    
102     TString fname = Form( "plots/paracoor_c%i_%s", imva, itype == 0 ? "S" : "B" );
103     TMVAGlob::imgconv( c1, fname );
104     }
105     }
106     }
107