Logo ROOT   6.10/09
Reference Guide
TMVAGui.cxx
Go to the documentation of this file.
1 #include "TMVA/TMVAGui.h"
2 #include "TMVA/Types.h"
3 #include <iostream>
4 #include <vector>
5 
6 #include "TList.h"
7 #include "TROOT.h"
8 #include "TKey.h"
9 #include "TString.h"
10 #include "TControlBar.h"
11 #include "TObjString.h"
12 #include "TClass.h"
13 
14 
15 
16 // some global lists
18 static std::vector<TString> TMVAGui_inactiveButtons;
19 
20 TList* TMVA::GetKeyList( const TString& pattern )
21 {
22  TList* list = new TList();
23 
24  TIter next( TMVAGui_keyContent );
25  TKey* key(0);
26  while ((key = (TKey*)next())) {
27  if (TString(key->GetName()).Contains( pattern )) { list->Add( new TObjString( key->GetName() ) ); }
28  }
29  return list;
30 }
31 
32 // utility function
33 void TMVA::ActionButton( TControlBar* cbar,
34  const TString& title, const TString& macro, const TString& comment,
35  const TString& buttonType, TString requiredKey )
36 {
37  cbar->AddButton( title, macro, comment, buttonType );
38 
39  // search
40  if (requiredKey != "") {
41  Bool_t found = kFALSE;
42  TIter next( TMVAGui_keyContent );
43  TKey* key(0);
44  while ((key = (TKey*)next())) {
45  if (TString(key->GetName()).Contains( requiredKey )) { found = kTRUE; break; }
46  }
47  if (!found) TMVAGui_inactiveButtons.push_back( title );
48  }
49 }
50 
51 // main GUI
52 void TMVA::TMVAGui( const char* fName , TString dataset)
53 {
54  // Use this script in order to run the various individual macros
55  // that plot the output of TMVA (e.g. running TMVAClassification.C),
56  // stored in the file "TMVA.root"
57 
58  TString curMacroPath(gROOT->GetMacroPath());
59  // uncomment next line for macros submitted to next root version
60  gROOT->SetMacroPath(curMacroPath+":./:$ROOTSYS/tmva/test/:");
61 
62  // for the sourceforge version, including $ROOTSYS/tmva/test in the
63  // macro path is a mistake, especially if "./" was not part of path
64  // add ../macros to the path (comment out next line for the ROOT version of TMVA)
65  // gROOT->SetMacroPath(curMacroPath+":../macros:");
66 
67  TString curIncludePath=gSystem->GetIncludePath();
68  TString newIncludePath=TString("-I../ ")+curIncludePath;
69  gSystem->SetIncludePath(newIncludePath);
70 
71  cout << "--- Launch TMVA GUI to view input file: " << fName << endl;
72 
73  // init
74  TMVAGui_inactiveButtons.clear();
75 
76  // check if file exist
77  TFile* file = TFile::Open( fName );
78  if (!file) {
79  cout << "==> Abort TMVAGui, please verify filename" << endl;
80  return;
81  }
82 
83  if(file->GetListOfKeys()->GetEntries()<=0)
84  {
85  cout << "==> Abort TMVAGui, please verify if dataset exist" << endl;
86  return;
87  }
88  if( (dataset==""||dataset.IsWhitespace()) && (file->GetListOfKeys()->GetEntries()==1))
89  {
90  TKey *key=(TKey*)file->GetListOfKeys()->At(0);
91  dataset=key->GetName();
92  }else if((dataset==""||dataset.IsWhitespace()) && (file->GetListOfKeys()->GetEntries()>=1))
93  {
94  gROOT->Reset();
95  gStyle->SetScreenFactor(2); // if you have a large screen, select 1,2 or 1.4
96 
97  TControlBar *bar=new TControlBar("vertical","Select dataset", 0, 0);
98  bar->SetButtonWidth(300);
99  for(int i=0;i<file->GetListOfKeys()->GetEntries();i++)
100  {
101  TKey *key=(TKey*)file->GetListOfKeys()->At(i);
102  dataset=key->GetName();
103  bar->AddButton(dataset.Data(),Form("TMVA::TMVAGui(\"%s\",\"%s\")",fName,dataset.Data()),dataset.Data());
104  }
105 
106  bar->AddSeparator();
107  bar->AddButton( "Quit", ".q", "Quit", "button");
108 
109  // set the style
110  bar->SetTextColor("black");
111  bar->Show();
112  gROOT->SaveContext();
113  return ;
114  }
115 
116  // find all references
117  TMVAGui_keyContent = (TList*)file->GetDirectory(dataset.Data())->GetListOfKeys()->Clone();
118 
119  // close file
120  file->Close();
121 
122  TString defaultRequiredClassifier = "";
123 
124  // gROOT->Reset();
125  // gStyle->SetScreenFactor(2); // if you have a large screen, select 1,2 or 1.4
126 
127  // create the control bar
128  TControlBar* cbar = new TControlBar( "vertical", "TMVA Plotting Macros for Classification", 0, 0 );
129 
130  const TString buttonType( "button" );
131 
132  // configure buttons
133  Int_t ic = 1;
134 
135  // find all input variables types
136  TList* keylist = GetKeyList( "InputVariables" );
137  TListIter it( keylist );
138  TObjString* str = 0;
139  char ch = 'a';
140  while ((str = (TObjString*)it())) {
141  TString tmp = str->GetString();
142  TString title = Form( "Input variables '%s'-transformed (training sample)",
143  tmp.ReplaceAll("InputVariables_","").Data() );
144  if (tmp.Contains( "Id" )) title = "Input variables (training sample)";
145  ActionButton( cbar,
146  Form( "(%i%c) %s", ic, ch++, title.Data() ),
147  Form( "TMVA::variables(\"%s\",\"%s\",\"%s\",\"%s\")",dataset.Data(), fName, str->GetString().Data(), title.Data() ),
148  Form( "Plots all '%s'-transformed input variables (macro variables(...))", str->GetString().Data() ),
149  buttonType, str->GetString() );
150  }
151  ic++;
152 
153  // correlation scatter plots
154  it.Reset(); ch = 'a';
155  while ((str = (TObjString*)it())) {
156  TString tmp = str->GetString();
157  TString title = Form( "Input variable correlations '%s'-transformed (scatter profiles)",
158  tmp.ReplaceAll("InputVariables_","").Data() );
159  if (tmp.Contains( "Id" )) title = "Input variable correlations (scatter profiles)";
160  ActionButton( cbar,
161  Form( "(%i%c) %s", ic, ch++, title.Data() ),
162  Form( "TMVA::CorrGui(\"%s\",\"%s\",\"%s\",\"%s\")",dataset.Data(), fName, str->GetString().Data(), title.Data() ),
163  Form( "Plots all correlation profiles between '%s'-transformed input variables (macro CorrGui(...))",
164  str->GetString().Data() ),
165  buttonType, str->GetString() );
166  }
167 
168  TString title;
169  // coefficients
170  title =Form( "(%i) Input Variable Linear Correlation Coefficients", ++ic );
171  ActionButton( cbar,
172  title,
173  Form( "TMVA::correlations(\"%s\",\"%s\")",dataset.Data(), fName ),
174  "Plots signal and background correlation summaries for all input variables (macro correlations.C)",
175  buttonType );
176 
177  title =Form( "(%ia) Classifier Output Distributions (test sample)", ++ic );
178  ActionButton( cbar,
179  title,
180  Form( "TMVA::mvas(\"%s\",\"%s\", TMVA::kMVAType)",dataset.Data(), fName ),
181  "Plots the output of each classifier for the test data (macro mvas(...,0))",
182  buttonType, defaultRequiredClassifier );
183 
184  title =Form( "(%ib) Classifier Output Distributions (test and training samples superimposed)", ic );
185  ActionButton( cbar,
186  title,
187  Form( "TMVA::mvas(\"%s\",\"%s\", TMVA::kCompareType )",dataset.Data(), fName),
188  "Plots the output of each classifier for the test (histograms) and training (dots) data (macro mvas(...,3))",
189  buttonType, defaultRequiredClassifier );
190 
191  title = Form( "(%ic) Classifier Probability Distributions (test sample)", ic );
192  ActionButton( cbar,
193  title,
194  Form( "TMVA::mvas(\"%s\",\"%s\", TMVA::kProbaType)",dataset.Data(), fName ),
195  "Plots the probability of each classifier for the test data (macro mvas(...,1))",
196  buttonType, defaultRequiredClassifier );
197 
198  title =Form( "(%id) Classifier Rarity Distributions (test sample)", ic );
199  ActionButton( cbar,
200  title,
201  Form( "TMVA::mvas(\"%s\",\"%s\", TMVA::kRarityType)",dataset.Data(), fName ),
202  "Plots the Rarity of each classifier for the test data (macro mvas(...,2)) - background distribution should be uniform",
203  buttonType, defaultRequiredClassifier );
204 
205  title =Form( "(%ia) Classifier Cut Efficiencies", ++ic );
206  ActionButton( cbar,
207  title,
208  Form( "TMVA::mvaeffs(\"%s\",\"%s\")",dataset.Data(), fName ),
209  "Plots signal and background efficiencies versus cut on classifier output (macro mvaeffs.cxx)",
210  buttonType, defaultRequiredClassifier );
211 
212  title = Form( "(%ib) Classifier Background Rejection vs Signal Efficiency (ROC curve)", ic );
213  ActionButton( cbar,
214  title,
215  Form( "TMVA::efficiencies(\"%s\",\"%s\")",dataset.Data(), fName ),
216  "Plots background rejection vs signal efficiencies (macro efficiencies.cxx) [\"ROC\" stands for \"Receiver Operation Characteristics\"]",
217  buttonType, defaultRequiredClassifier );
218 
219  title = Form( "(%ib) Classifier 1/(Backgr. Efficiency) vs Signal Efficiency (ROC curve)", ic );
220  ActionButton( cbar,
221  title,
222  Form( "TMVA::efficiencies(\"%s\",\"%s\",%d)",dataset.Data(), fName, 3 ),
223  "Plots 1/(background eff.) vs signal efficiencies (macro efficiencies.cxx) [\"ROC\" stands for \"Receiver Operation Characteristics\"]",
224  buttonType, defaultRequiredClassifier );
225 
226  title = Form( "(%i) Parallel Coordinates (requires ROOT-version >= 5.17)", ++ic );
227  ActionButton( cbar,
228  title,
229  Form( "TMVA::paracoor(\"%s\",\"%s\")",dataset.Data(), fName ),
230  "Plots parallel coordinates for classifiers and input variables (macro paracoor.cxx, requires ROOT >= 5.17)",
231  buttonType, defaultRequiredClassifier );
232 
233  // parallel coordinates only exist since ROOT 5.17
234 #if ROOT_VERSION_CODE < ROOT_VERSION(5,17,0)
235  TMVAGui_inactiveButtons.push_back( title );
236 #endif
237 
238  title =Form( "(%i) PDFs of Classifiers (requires \"CreateMVAPdfs\" option set)", ++ic );
239  ActionButton( cbar,
240  title,
241  Form( "TMVA::probas(\"%s\",\"%s\")",dataset.Data(), fName ),
242  "Plots the PDFs of the classifier output distributions for signal and background - if requested (macro probas.cxx)",
243  buttonType, defaultRequiredClassifier );
244 
245  title = Form( "(%i) Likelihood Reference Distributiuons", ++ic);
246  ActionButton( cbar,
247  title,
248  Form( "TMVA::likelihoodrefs(\"%s\",\"%s\")",dataset.Data(), fName ),
249  "Plots to verify the likelihood reference distributions (macro likelihoodrefs.cxx)",
250  buttonType, "Likelihood" );
251 
252  title = Form( "(%ia) Network Architecture (MLP)", ++ic );
253  TString call = Form( "TMVA::network(\"%s\",\"%s\")", dataset.Data(),fName );
254  ActionButton( cbar,
255  title,
256  call,
257  "Plots the MLP weights (macro network.cxx)",
258  buttonType, "MLP" );
259 
260  title = Form( "(%ib) Network Convergence Test (MLP)", ic );
261  ActionButton( cbar,
262  title,
263  Form( "TMVA::annconvergencetest(\"%s\",\"%s\")",dataset.Data(), fName ),
264  "Plots error estimator versus training epoch for training and test samples (macro annconvergencetest.C)",
265  buttonType, "MLP" );
266 
267  title = Form( "(%i) Decision Trees (BDT)", ++ic );
268  ActionButton( cbar,
269  title,
270  Form( "TMVA::BDT(\"%s\",\"%s\")",dataset.Data() , fName ),
271  "Plots the Decision Trees trained by BDT algorithms (macro BDT(itree,...))",
272  buttonType, "BDT" );
273 
274  title = Form( "(%i) Decision Tree Control Plots (BDT)", ++ic );
275  ActionButton( cbar,
276  title,
277  Form( "TMVA::BDTControlPlots(\"%s\",\"%s\")",dataset.Data() , fName ),
278  "Plots to monitor boosting and pruning of decision trees (macro BDTControlPlots.cxx)",
279  buttonType, "BDT" );
280  // ActionButton( cbar,
281  // Form( "(%i) Rule Ensemble Importance Plots (RuleFit)", ++ic ),
282  // Form( "TMVA::rulevis(\"%s\",0)", fName ),
283  // "Plots all input variables with rule ensemble weights, including linear terms (macro rulevis.cxx)",
284  // buttonType, "RuleFit" );
285 
286  title = Form( "(%i) Plot Foams (PDEFoam)", ++ic );
287  ActionButton( cbar,
288  title,
289  Form("TMVA::PlotFoams(\"%s/weights/TMVAClassification_PDEFoam.weights_foams.root\")",dataset.Data()),
290  "Plot Foams (macro PlotFoams.cxx)",
291  buttonType, "PDEFoam" );
292 
293  title = Form( "(%i) General Boost Control Plots", ++ic );
294  ActionButton( cbar,
295  title,
296  Form( "TMVA::BoostControlPlots(\"%s\",\"%s\")",dataset.Data() , fName ),
297  "Plots to monitor boosting of general classifiers (macro BoostControlPlots)",
298  buttonType, "Boost" );
299 
300  cbar->AddSeparator();
301 
302  cbar->AddButton( Form( "(%i) Quit", ++ic ), ".q", "Quit", buttonType );
303 
304  // set the style
305  cbar->SetTextColor("black");
306 
307  // there seems to be a bug in ROOT: font jumps back to default after pressing on >2 different buttons
308  // cbar->SetFont("-adobe-helvetica-bold-r-*-*-12-*-*-*-*-*-iso8859-1");
309 
310  // draw
311  cbar->Show();
312 
313  // indicate inactive buttons
314  for (UInt_t i=0; i<TMVAGui_inactiveButtons.size(); i++) cbar->SetButtonState(TMVAGui_inactiveButtons[i], 3 );
315  if (TMVAGui_inactiveButtons.size() > 0) {
316  cout << "=== Note: inactive buttons indicate classifiers that were not trained, ===" << endl;
317  cout << "=== or functionalities that were not invoked during the training ===" << endl;
318  }
319 
320  gROOT->SaveContext();
321 }
void Show()
Show control bar.
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
void AddButton(TControlBarButton *button)
Add button.
A Control Bar is a fully user configurable tool which provides fast access to frequently used operati...
Definition: TControlBar.h:22
void SetTextColor(const char *colorName)
Sets text color for control bar buttons, e.g.
Collectable string class.
Definition: TObjString.h:28
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition: TString.h:640
R__EXTERN TStyle * gStyle
Definition: TStyle.h:402
void TMVAGui(const char *fName="TMVA.root", TString dataset="")
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:46
virtual Int_t GetEntries() const
Definition: TCollection.h:86
#define gROOT
Definition: TROOT.h:375
UInt_t GetListOfKeys(TList &keys, TString inherits, TDirectory *dir=0)
Definition: tmvaglob.cxx:375
Basic string class.
Definition: TString.h:129
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
Iterator of linked list.
Definition: TList.h:183
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=1, Int_t netopt=0)
Create / open a file.
Definition: TFile.cxx:3909
Book space in a file, create I/O buffers, to fill them, (un)compress them.
Definition: TKey.h:24
void SetButtonWidth(UInt_t width)
Sets the width in pixels for control bar button.
A doubly linked list.
Definition: TList.h:43
const TString & GetString() const
Definition: TObjString.h:47
void ActionButton(TControlBar *cbar, const TString &title, const TString &macro, const TString &comment, const TString &buttonType, TString requiredKey="")
R__EXTERN TSystem * gSystem
Definition: TSystem.h:539
void SetScreenFactor(Float_t factor=1)
Definition: TStyle.h:294
unsigned int UInt_t
Definition: RtypesCore.h:42
char * Form(const char *fmt,...)
virtual TObject * At(Int_t idx) const
Returns the object at position idx. Returns 0 if idx is out of range.
Definition: TList.cxx:315
virtual TDirectory * GetDirectory(const char *apath, Bool_t printError=false, const char *funcname="GetDirectory")
Find a directory named "apath".
TList * GetKeyList(const TString &pattern)
const Bool_t kFALSE
Definition: RtypesCore.h:92
virtual const char * GetIncludePath()
Get the list of include path.
Definition: TSystem.cxx:3805
static std::vector< TString > TMVAGui_inactiveButtons
Definition: TMVAGui.cxx:18
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition: TString.h:572
virtual void Add(TObject *obj)
Definition: TList.h:77
Definition: file.py:1
virtual TList * GetListOfKeys() const
static TList * TMVAGui_keyContent
Definition: TMVAGui.cxx:17
void AddSeparator()
Add separator.
void Reset()
Reset list iterator.
Definition: TList.cxx:981
void SetButtonState(const char *label, Int_t state=0)
Sets a state for control bar button &#39;label&#39;; possible states are 0-kButtonUp, 1-kButtonDown, 2-kButtonEngaged, 3-kButtonDisabled,.
Bool_t IsWhitespace() const
Definition: TString.h:386
const Bool_t kTRUE
Definition: RtypesCore.h:91
virtual void SetIncludePath(const char *includePath)
IncludePath should contain the list of compiler flags to indicate where to find user defined header f...
Definition: TSystem.cxx:4007
virtual void Close(Option_t *option="")
Close a file.
Definition: TFile.cxx:904
const char * Data() const
Definition: TString.h:347