Logo ROOT   6.08/07
Reference Guide
paracoor.cxx
Go to the documentation of this file.
1 #include "TMVA/paracoor.h"
2 
3 #include "TROOT.h"
4 #include "TTree.h"
5 #include "TLeaf.h"
6 #include "TParallelCoord.h"
7 #include "TParallelCoordVar.h"
8 #include "TParallelCoordRange.h"
9 
10 #include <vector>
11 using std::vector;
12 
13 
14 // plot parallel coordinates
15 
16 void TMVA::paracoor(TString dataset, TString fin , Bool_t useTMVAStyle )
17 {
18  // set style and remove existing canvas'
19  TMVAGlob::Initialize( useTMVAStyle );
20 
21  // checks if file with name "fin" is already open, and if not opens one
22  TFile* file = TMVAGlob::OpenFile( fin );
23  TTree* tree = (TTree*)file->GetDirectory(dataset.Data())->Get("TestTree");
24  if(!tree) {
25  cout << "--- No TestTree saved in ROOT file. Parallel coordinates will not be plotted" << endl;
26  return;
27  }
28 
29  // first get list of leaves in tree
30  TObjArray* leafList = tree->GetListOfLeaves();
31  vector<TString> vars;
32  vector<TString> mvas;
33  for (Int_t iar=0; iar<leafList->GetSize(); iar++) {
34  TLeaf* leaf = (TLeaf*)leafList->At(iar);
35  if (leaf != 0) {
36  TString leafName = leaf->GetName();
37  if (leafName != "type" && leafName != "weight" && leafName != "boostweight" &&
38  leafName != "class" && leafName != "className" && leafName != "classID" &&
39  !leafName.Contains("prob_")) {
40  // is MVA ?
41  if (TMVAGlob::ExistMethodName( leafName,file->GetDirectory(dataset.Data()) )) {
42  mvas.push_back( leafName );
43  }
44  else {
45  vars.push_back( leafName );
46  }
47  }
48  }
49  }
50 
51  cout << "--- Found: " << vars.size() << " variables" << endl;
52  cout << "--- Found: " << mvas.size() << " MVA(s)" << endl;
53 
54 
55  TString type[2] = { "Signal", "Background" };
56  const UInt_t nmva = mvas.size();
57  TCanvas* csig[nmva];
58  TCanvas* cbkg[nmva];
59  for (UInt_t imva=0; imva<mvas.size(); imva++) {
60  cout << "--- Plotting parallel coordinates for : " << mvas[imva] << " & input variables" << endl;
61 
62  for (Int_t itype=0; itype<2; itype++) {
63 
64  // create draw option
65  TString varstr = mvas[imva] + ":";
66  for (UInt_t ivar=0; ivar<vars.size(); ivar++) varstr += vars[ivar] + ":";
67  varstr.Resize( varstr.Last( ':' ) );
68 
69  // create canvas
70  TString mvashort = mvas[imva]; mvashort.ReplaceAll("MVA_","");
71  TCanvas* c1 = (itype == 0) ? csig[imva] : cbkg[imva];
72  c1 = new TCanvas( Form( "c1_%i_%s",itype,mvashort.Data() ),
73  Form( "Parallel coordinate representation for %s and input variables (%s events)",
74  mvashort.Data(), type[itype].Data() ),
75  50*(itype), 50*(itype), 750, 500 );
76  tree->Draw( varstr.Data(), Form("classID==%i",1-itype) , "para" );
77  c1->ToggleEditor();
78  gStyle->SetOptTitle(0);
79 
80  TParallelCoord* para = (TParallelCoord*)gPad->GetListOfPrimitives()->FindObject( "ParaCoord" );
81  TParallelCoordVar* mvavar = (TParallelCoordVar*)para->GetVarList()->FindObject( mvas[imva] );
82  Double_t minrange = tree->GetMinimum( mvavar->GetName() );
83  Double_t maxrange = tree->GetMaximum( mvavar->GetName() );
84  Double_t width = 0.2*(maxrange - minrange);
85  Double_t x1 = minrange, x2 = x1 + width;
86  TParallelCoordRange* parrange = new TParallelCoordRange( mvavar, x1, x2 );
87  parrange->SetLineColor(4);
88  mvavar->AddRange( parrange );
89 
90  para->AddSelection("-1");
91 
92  for (Int_t ivar=1; ivar<TMath::Min(Int_t(vars.size()) + 1,3); ivar++) {
93  TParallelCoordVar* var = (TParallelCoordVar*)para->GetVarList()->FindObject( vars[ivar] );
94  minrange = tree->GetMinimum( var->GetName() );
95  maxrange = tree->GetMaximum( var->GetName() );
96  width = 0.2*(maxrange - minrange);
97 
98  switch (ivar) {
99  case 0: { x1 = minrange; x2 = x1 + width; break; }
100  case 1: { x1 = 0.5*(maxrange + minrange - width)*0.02; x2 = x1 + width*0.02; break; }
101  case 2: { x1 = maxrange - width; x2 = x1 + width; break; }
102  }
103 
104  parrange = new TParallelCoordRange( var, x1, x2 );
105  parrange->SetLineColor( ivar == 0 ? 2 : ivar == 1 ? 5 : 6 );
106  var->AddRange( parrange );
107 
108  para->AddSelection( Form("%i",ivar) );
109  }
110 
111  c1->Update();
112 
113  TString fname = Form( "%s/plots/paracoor_c%i_%s",dataset.Data(), imva, itype == 0 ? "S" : "B" );
114  TMVAGlob::imgconv( c1, fname );
115  }
116  }
117 }
118 
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:51
A TLeaf describes individual elements of a TBranch See TBranch structure in TTree.
Definition: TLeaf.h:37
An array of TObjects.
Definition: TObjArray.h:39
void imgconv(TCanvas *c, const TString &fname)
Definition: tmvaglob.cxx:212
void paracoor(TString dataset, TString fin="TMVA.root", Bool_t useTMVAStyle=kTRUE)
Definition: paracoor.cxx:16
return c1
Definition: legend1.C:41
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition: TString.h:635
R__EXTERN TStyle * gStyle
Definition: TStyle.h:418
TFile * OpenFile(const TString &fin)
Definition: tmvaglob.cxx:192
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:50
Bool_t ExistMethodName(TString name, TDirectory *dir=0)
Definition: tmvaglob.cxx:539
Basic string class.
Definition: TString.h:137
A TParallelCoordRange is a range used for parallel coordinates plots.
Short_t Min(Short_t a, Short_t b)
Definition: TMathBase.h:170
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
void mvas(TString dataset, TString fin="TMVA.root", HistType htype=kMVAType, Bool_t useTMVAStyle=kTRUE)
Definition: mvas.cxx:16
TObject * At(Int_t idx) const
Definition: TObjArray.h:167
virtual TObject * FindObject(const char *name) const
Find an object in this list using its name.
Definition: TList.cxx:497
virtual Double_t GetMinimum(const char *columname)
Return minimum of column with name columname.
Definition: TTree.cxx:5830
TParallelCoord axes.
static const double x2[5]
TList * GetVarList()
virtual Double_t GetMaximum(const char *columname)
Return maximum of column with name columname.
Definition: TTree.cxx:5791
virtual void SetLineColor(Color_t col)
Set the selection line color.
void AddSelection(const char *title)
Add a selection.
void Initialize(Bool_t useTMVAStyle=kTRUE)
Definition: tmvaglob.cxx:176
unsigned int UInt_t
Definition: RtypesCore.h:42
char * Form(const char *fmt,...)
virtual void ToggleEditor()
Toggle editor.
Definition: TCanvas.cxx:2147
virtual TDirectory * GetDirectory(const char *apath, Bool_t printError=false, const char *funcname="GetDirectory")
Find a directory named "apath".
The Canvas class.
Definition: TCanvas.h:41
static const double x1[5]
double Double_t
Definition: RtypesCore.h:55
Ssiz_t Last(char c) const
Find last occurrence of a character c.
Definition: TString.cxx:865
int type
Definition: TGX11.cxx:120
virtual void Draw(Option_t *opt)
Default Draw method for all objects.
Definition: TTree.h:367
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition: TString.h:567
Parallel Coordinates class.
Definition: file.py:1
void SetOptTitle(Int_t tit=1)
Definition: TStyle.h:325
#define gPad
Definition: TVirtualPad.h:289
Definition: tree.py:1
A TTree object has a header with a name and a title.
Definition: TTree.h:98
virtual void Update()
Update canvas pad buffers.
Definition: TCanvas.cxx:2183
virtual Int_t GetSize() const
Definition: TCollection.h:95
void AddRange(TParallelCoordRange *range)
Add a range to the current selection on the axis.
void Resize(Ssiz_t n)
Resize the string. Truncate or add blanks as necessary.
Definition: TString.cxx:1059
virtual TObjArray * GetListOfLeaves()
Definition: TTree.h:418
const char * Data() const
Definition: TString.h:349