Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
paracoor.cxx
Go to the documentation of this file.
1#include "TMVA/paracoor.h"
2
3#include "TTree.h"
4#include "TLeaf.h"
5#include "TParallelCoord.h"
6#include "TParallelCoordVar.h"
8
9#include <vector>
10using std::vector;
11
12
13// plot parallel coordinates
14
15void TMVA::paracoor(TString dataset, TString fin , Bool_t useTMVAStyle )
16{
17 // set style and remove existing canvas'
18 TMVAGlob::Initialize( useTMVAStyle );
19
20 // checks if file with name "fin" is already open, and if not opens one
21 TFile* file = TMVAGlob::OpenFile( fin );
22 TTree* tree = (TTree*)file->GetDirectory(dataset.Data())->Get("TestTree");
23 if(!tree) {
24 cout << "--- No TestTree saved in ROOT file. Parallel coordinates will not be plotted" << endl;
25 return;
26 }
27
28 // first get list of leaves in tree
29 TObjArray* leafList = tree->GetListOfLeaves();
30 vector<TString> vars;
31 vector<TString> mvas;
32 for (Int_t iar=0; iar<leafList->GetSize(); iar++) {
33 TLeaf* leaf = (TLeaf*)leafList->At(iar);
34 if (leaf != 0) {
35 TString leafName = leaf->GetName();
36 if (leafName != "type" && leafName != "weight" && leafName != "boostweight" &&
37 leafName != "class" && leafName != "className" && leafName != "classID" &&
38 !leafName.Contains("prob_")) {
39 // is MVA ?
40 if (TMVAGlob::ExistMethodName( leafName,file->GetDirectory(dataset.Data()) )) {
41 mvas.push_back( leafName );
42 }
43 else {
44 vars.push_back( leafName );
45 }
46 }
47 }
48 }
49
50 cout << "--- Found: " << vars.size() << " variables" << endl;
51 cout << "--- Found: " << mvas.size() << " MVA(s)" << endl;
52
53
54 TString type[2] = { "Signal", "Background" };
55 for (UInt_t imva=0; imva<mvas.size(); imva++) {
56 cout << "--- Plotting parallel coordinates for : " << mvas[imva] << " & input variables" << endl;
57
58 for (Int_t itype=0; itype<2; itype++) {
59
60 // create draw option
61 TString varstr = mvas[imva] + ":";
62 for (UInt_t ivar=0; ivar<vars.size(); ivar++) varstr += vars[ivar] + ":";
63 varstr.Resize( varstr.Last( ':' ) );
64
65 // create canvas
66 TString mvashort = mvas[imva]; mvashort.ReplaceAll("MVA_","");
67 auto c1 = new TCanvas( TString::Format( "c1_%i_%s",itype,mvashort.Data() ),
68 TString::Format( "Parallel coordinate representation for %s and input variables (%s events)",
69 mvashort.Data(), type[itype].Data() ),
70 50*(itype), 50*(itype), 750, 500 );
71 tree->Draw( varstr.Data(), TString::Format("classID==%i",1-itype) , "para" );
72 c1->ToggleEditor();
74
75 TParallelCoord* para = (TParallelCoord*)gPad->GetListOfPrimitives()->FindObject( "ParaCoord" );
76 TParallelCoordVar* mvavar = (TParallelCoordVar*)para->GetVarList()->FindObject( mvas[imva] );
77 Double_t minrange = tree->GetMinimum( mvavar->GetName() );
78 Double_t maxrange = tree->GetMaximum( mvavar->GetName() );
79 Double_t width = 0.2*(maxrange - minrange);
80 Double_t x1 = minrange, x2 = x1 + width;
81 TParallelCoordRange* parrange = new TParallelCoordRange( mvavar, x1, x2 );
82 parrange->SetLineColor(4);
83 mvavar->AddRange( parrange );
84
85 para->AddSelection("-1");
86
87 for (Int_t ivar=1; ivar<TMath::Min(Int_t(vars.size()) + 1,3); ivar++) {
88 TParallelCoordVar* var = (TParallelCoordVar*)para->GetVarList()->FindObject( vars[ivar] );
89 minrange = tree->GetMinimum( var->GetName() );
90 maxrange = tree->GetMaximum( var->GetName() );
91 width = 0.2*(maxrange - minrange);
92
93 switch (ivar) {
94 case 0: { x1 = minrange; x2 = x1 + width; break; }
95 case 1: { x1 = 0.5*(maxrange + minrange - width)*0.02; x2 = x1 + width*0.02; break; }
96 case 2: { x1 = maxrange - width; x2 = x1 + width; break; }
97 }
98
99 parrange = new TParallelCoordRange( var, x1, x2 );
100 parrange->SetLineColor( ivar == 0 ? 2 : ivar == 1 ? 5 : 6 );
101 var->AddRange( parrange );
102
103 para->AddSelection( TString::Format("%i",ivar) );
104 }
105
106 c1->Update();
107
108 TString fname = TString::Format( "%s/plots/paracoor_c%i_%s",dataset.Data(), imva, itype == 0 ? "S" : "B" );
109 TMVAGlob::imgconv( c1, fname );
110 }
111 }
112}
113
int Int_t
Definition RtypesCore.h:45
Option_t Option_t TPoint TPoint const char x2
Option_t Option_t TPoint TPoint const char x1
Option_t Option_t width
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t type
R__EXTERN TStyle * gStyle
Definition TStyle.h:433
#define gPad
The Canvas class.
Definition TCanvas.h:23
virtual Int_t GetSize() const
Return the capacity of the collection, i.e.
TDirectory * GetDirectory(const char *apath, Bool_t printError=false, const char *funcname="GetDirectory") override
Find a directory named "apath".
A ROOT file is an on-disk file, usually with extension .root, that stores objects in a file-system-li...
Definition TFile.h:53
A TLeaf describes individual elements of a TBranch See TBranch structure in TTree.
Definition TLeaf.h:57
TObject * FindObject(const char *name) const override
Find an object in this list using its name.
Definition TList.cxx:576
const char * GetName() const override
Returns name of object.
Definition TNamed.h:47
An array of TObjects.
Definition TObjArray.h:31
TObject * At(Int_t idx) const override
Definition TObjArray.h:164
virtual TObject * FindObject(const char *name) const
Must be redefined in derived classes.
Definition TObject.cxx:403
A TParallelCoordRange is a range used for parallel coordinates plots.
void SetLineColor(Color_t col) override
Set the selection line color.
TParallelCoord axes.
void AddRange(TParallelCoordRange *range)
Add a range to the current selection on the axis.
Parallel Coordinates class.
void AddSelection(const char *title)
Add a selection.
TList * GetVarList()
Basic string class.
Definition TString.h:139
const char * Data() const
Definition TString.h:376
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition TString.h:704
void Resize(Ssiz_t n)
Resize the string. Truncate or add blanks as necessary.
Definition TString.cxx:1152
Ssiz_t Last(char c) const
Find last occurrence of a character c.
Definition TString.cxx:931
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString.
Definition TString.cxx:2378
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition TString.h:632
void SetOptTitle(Int_t tit=1)
Definition TStyle.h:332
A TTree represents a columnar dataset.
Definition TTree.h:79
return c1
Definition legend1.C:41
void Initialize(Bool_t useTMVAStyle=kTRUE)
Definition tmvaglob.cxx:176
TFile * OpenFile(const TString &fin)
Definition tmvaglob.cxx:192
Bool_t ExistMethodName(TString name, TDirectory *dir=nullptr)
Definition tmvaglob.cxx:548
void imgconv(TCanvas *c, const TString &fname)
Definition tmvaglob.cxx:212
void paracoor(TString dataset, TString fin="TMVA.root", Bool_t useTMVAStyle=kTRUE)
void mvas(TString dataset, TString fin="TMVA.root", HistType htype=kMVAType, Bool_t useTMVAStyle=kTRUE)
Short_t Min(Short_t a, Short_t b)
Returns the smallest of a and b.
Definition TMathBase.h:198