ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Tools.cxx
Go to the documentation of this file.
1 // @(#)root/tmva $Id$
2 // Author: Andreas Hoecker, Peter Speckmayer, Joerg Stelzer, Helge Voss, Jan Therhaag
3 
4 /**********************************************************************************
5  * Project: TMVA - a Root-integrated toolkit for multivariate data analysis *
6  * Package: TMVA *
7  * Class : Tools *
8  * Web : http://tmva.sourceforge.net *
9  * *
10  * Description: *
11  * Implementation (see header for description) *
12  * *
13  * Authors (alphabetical): *
14  * Andreas Hoecker <Andreas.Hocker@cern.ch> - CERN, Switzerland *
15  * Peter Speckmayer <Peter.Speckmayer@cern.ch> - CERN, Switzerland *
16  * Jan Therhaag <Jan.Therhaag@cern.ch> - U of Bonn, Germany *
17  * Helge Voss <Helge.Voss@cern.ch> - MPI-K Heidelberg, Germany *
18  * Kai Voss <Kai.Voss@cern.ch> - U. of Victoria, Canada *
19  * *
20  * Copyright (c) 2005-2011: *
21  * CERN, Switzerland *
22  * U. of Victoria, Canada *
23  * MPI-K Heidelberg, Germany *
24  * U. of Bonn, Germany *
25  * *
26  * Redistribution and use in source and binary forms, with or without *
27  * modification, are permitted according to the terms listed in LICENSE *
28  * (http://ttmva.sourceforge.net/LICENSE) *
29  **********************************************************************************/
30 
31 #include "TMVA/Tools.h"
32 
33 #ifndef ROOT_TMVA_Config
34 #include "TMVA/Config.h"
35 #endif
36 #ifndef ROOT_TMVA_Event
37 #include "TMVA/Event.h"
38 #endif
39 #ifndef ROOT_TMVA_Version
40 #include "TMVA/Version.h"
41 #endif
42 #ifndef ROOT_TMVA_PDF
43 #include "TMVA/PDF.h"
44 #endif
45 #ifndef ROOT_TMVA_MsgLogger
46 #include "TMVA/MsgLogger.h"
47 #endif
48 #include "TMVA/Types.h"
49 
50 #include "TObjString.h"
51 #include "TMath.h"
52 #include "TString.h"
53 #include "TTree.h"
54 #include "TLeaf.h"
55 #include "TH1.h"
56 #include "TH2.h"
57 #include "TList.h"
58 #include "TSpline.h"
59 #include "TVector.h"
60 #include "TMatrixD.h"
61 #include "TMatrixDSymEigen.h"
62 #include "TVectorD.h"
63 #include "TTreeFormula.h"
64 #include "TXMLEngine.h"
65 #include "TROOT.h"
66 #include "TMatrixDSymEigen.h"
67 
68 #include <algorithm>
69 #include <cstdlib>
70 
71 using namespace std;
72 
73 #if __cplusplus > 199711L
74 std::atomic<TMVA::Tools*> TMVA::Tools::fgTools{0};
75 #else
77 #endif
78 
81 #if __cplusplus > 199711L
82  if(!fgTools) {
83  Tools* tmp = new Tools();
84  Tools* expected = 0;
85  if(! fgTools.compare_exchange_strong(expected,tmp)) {
86  //another thread beat us
87  delete tmp;
88  }
89  }
90  return *fgTools;
91 #else
92  return fgTools?*(fgTools): *(fgTools = new Tools());
93 #endif
94 }
96  //NOTE: there is no thread safe way to do this so
97  // one must only call this method ones in an executable
98 #if __cplusplus > 199711L
99  if (fgTools != 0) { delete fgTools.load(); fgTools=0; }
100 #else
101  if (fgTools != 0) { delete fgTools; fgTools=0; }
102 #endif
103 }
104 
105 ////////////////////////////////////////////////////////////////////////////////
106 /// constructor
107 
109  fRegexp("$&|!%^&()'<>?= "),
110  fLogger(new MsgLogger("Tools")),
111  fXMLEngine(new TXMLEngine())
112 {
113 }
114 
115 ////////////////////////////////////////////////////////////////////////////////
116 /// destructor
117 
119 {
120  delete fLogger;
121  delete fXMLEngine;
122 }
123 
124 ////////////////////////////////////////////////////////////////////////////////
125 /// normalise to output range: [-1, 1]
126 
128 {
129  return 2*(x - xmin)/(xmax - xmin) - 1.0;
130 }
131 
132 ////////////////////////////////////////////////////////////////////////////////
133 /// compute "separation" defined as
134 /// <s2> = (1/2) Int_-oo..+oo { (S(x) - B(x))^2/(S(x) + B(x)) dx }
135 
137 {
138  Double_t separation = 0;
139 
140  // sanity checks
141  // signal and background histograms must have same number of bins and
142  // same limits
143  if ((S->GetNbinsX() != B->GetNbinsX()) || (S->GetNbinsX() <= 0)) {
144  Log() << kFATAL << "<GetSeparation> signal and background"
145  << " histograms have different number of bins: "
146  << S->GetNbinsX() << " : " << B->GetNbinsX() << Endl;
147  }
148 
149  if (S->GetXaxis()->GetXmin() != B->GetXaxis()->GetXmin() ||
150  S->GetXaxis()->GetXmax() != B->GetXaxis()->GetXmax() ||
151  S->GetXaxis()->GetXmax() <= S->GetXaxis()->GetXmin()) {
152  Log() << kINFO << S->GetXaxis()->GetXmin() << " " << B->GetXaxis()->GetXmin()
153  << " " << S->GetXaxis()->GetXmax() << " " << B->GetXaxis()->GetXmax()
154  << " " << S->GetXaxis()->GetXmax() << " " << S->GetXaxis()->GetXmin() << Endl;
155  Log() << kFATAL << "<GetSeparation> signal and background"
156  << " histograms have different or invalid dimensions:" << Endl;
157  }
158 
159  Int_t nstep = S->GetNbinsX();
160  Double_t intBin = (S->GetXaxis()->GetXmax() - S->GetXaxis()->GetXmin())/nstep;
161  Double_t nS = S->GetSumOfWeights()*intBin;
162  Double_t nB = B->GetSumOfWeights()*intBin;
163 
164  if (nS > 0 && nB > 0) {
165  for (Int_t bin=0; bin<nstep; bin++) {
166  Double_t s = S->GetBinContent( bin+1 )/Double_t(nS);
167  Double_t b = B->GetBinContent( bin+1 )/Double_t(nB);
168  // separation
169  if (s + b > 0) separation += 0.5*(s - b)*(s - b)/(s + b);
170  }
171  separation *= intBin;
172  }
173  else {
174  Log() << kWARNING << "<GetSeparation> histograms with zero entries: "
175  << nS << " : " << nB << " cannot compute separation"
176  << Endl;
177  separation = 0;
178  }
179 
180  return separation;
181 }
182 
183 ////////////////////////////////////////////////////////////////////////////////
184 /// compute "separation" defined as
185 /// <s2> = (1/2) Int_-oo..+oo { (S(x) - B(x))2/(S(x) + B(x)) dx }
186 
187 Double_t TMVA::Tools::GetSeparation( const PDF& pdfS, const PDF& pdfB ) const
188 {
189  Double_t xmin = pdfS.GetXmin();
190  Double_t xmax = pdfS.GetXmax();
191  // sanity check
192  if (xmin != pdfB.GetXmin() || xmax != pdfB.GetXmax()) {
193  Log() << kFATAL << "<GetSeparation> Mismatch in PDF limits: "
194  << xmin << " " << pdfB.GetXmin() << xmax << " " << pdfB.GetXmax() << Endl;
195  }
196 
197  Double_t separation = 0;
198  Int_t nstep = 100;
199  Double_t intBin = (xmax - xmin)/Double_t(nstep);
200  for (Int_t bin=0; bin<nstep; bin++) {
201  Double_t x = (bin + 0.5)*intBin + xmin;
202  Double_t s = pdfS.GetVal( x );
203  Double_t b = pdfB.GetVal( x );
204  // separation
205  if (s + b > 0) separation += (s - b)*(s - b)/(s + b);
206  }
207  separation *= (0.5*intBin);
208 
209  return separation;
210 }
211 
212 ////////////////////////////////////////////////////////////////////////////////
213 /// sanity check
214 
215 void TMVA::Tools::ComputeStat( const std::vector<TMVA::Event*>& events, std::vector<Float_t>* valVec,
216  Double_t& meanS, Double_t& meanB,
217  Double_t& rmsS, Double_t& rmsB,
219  Int_t signalClass, Bool_t norm )
220 {
221  if (0 == valVec)
222  Log() << kFATAL << "<Tools::ComputeStat> value vector is zero pointer" << Endl;
223 
224  if ( events.size() != valVec->size() )
225  Log() << kWARNING << "<Tools::ComputeStat> event and value vector have different lengths "
226  << events.size() << "!=" << valVec->size() << Endl;
227 
228  Long64_t entries = valVec->size();
229 
230  // first fill signal and background in arrays before analysis
231  Double_t* varVecS = new Double_t[entries];
232  Double_t* varVecB = new Double_t[entries];
233  Double_t* wgtVecS = new Double_t[entries];
234  Double_t* wgtVecB = new Double_t[entries];
235  xmin = +DBL_MAX;
236  xmax = -DBL_MAX;
237  Long64_t nEventsS = 0;
238  Long64_t nEventsB = 0;
239  Double_t xmin_ = 0, xmax_ = 0;
240 
241  if (norm) {
242  xmin_ = *std::min( valVec->begin(), valVec->end() );
243  xmax_ = *std::max( valVec->begin(), valVec->end() );
244  }
245 
246  for (Int_t ievt=0; ievt<entries; ievt++) {
247  Double_t theVar = (*valVec)[ievt];
248  if (norm) theVar = Tools::NormVariable( theVar, xmin_, xmax_ );
249 
250  if (Int_t(events[ievt]->GetClass()) == signalClass ){
251  wgtVecS[nEventsS] = events[ievt]->GetWeight(); // this is signal
252  varVecS[nEventsS++] = theVar; // this is signal
253  }
254  else {
255  wgtVecB[nEventsB] = events[ievt]->GetWeight(); // this is signal
256  varVecB[nEventsB++] = theVar; // this is background
257  }
258 
259  if (theVar > xmax) xmax = theVar;
260  if (theVar < xmin) xmin = theVar;
261  }
262  // ++nEventsS;
263  // ++nEventsB;
264 
265  // basic statistics
266  // !!! TMath::Mean allows for weights, but NOT for negative weights
267  // and TMath::RMS doesn't allow for weights all together...
268  meanS = TMVA::Tools::Mean( nEventsS, varVecS, wgtVecS );
269  meanB = TMVA::Tools::Mean( nEventsB, varVecB, wgtVecB );
270  rmsS = TMVA::Tools::RMS ( nEventsS, varVecS, wgtVecS );
271  rmsB = TMVA::Tools::RMS ( nEventsB, varVecB, wgtVecB );
272 
273  delete [] varVecS;
274  delete [] varVecB;
275  delete [] wgtVecS;
276  delete [] wgtVecB;
277 }
278 
279 ////////////////////////////////////////////////////////////////////////////////
280 /// square-root of symmetric matrix
281 /// of course the resulting sqrtMat is also symmetric, but it's easier to
282 /// treat it as a general matrix
283 
285 {
286  Int_t n = symMat->GetNrows();
287 
288  // compute eigenvectors
289  TMatrixDSymEigen* eigen = new TMatrixDSymEigen( *symMat );
290 
291  // D = ST C S
292  TMatrixD* si = new TMatrixD( eigen->GetEigenVectors() );
293  TMatrixD* s = new TMatrixD( *si ); // copy
294  si->Transpose( *si ); // invert (= transpose)
295 
296  // diagonal matrices
297  TMatrixD* d = new TMatrixD( n, n);
298  d->Mult( (*si), (*symMat) ); (*d) *= (*s);
299 
300  // sanity check: matrix must be diagonal and positive definit
301  Int_t i, j;
302  Double_t epsilon = 1.0e-8;
303  for (i=0; i<n; i++) {
304  for (j=0; j<n; j++) {
305  if ((i != j && TMath::Abs((*d)(i,j))/TMath::Sqrt((*d)(i,i)*(*d)(j,j)) > epsilon) ||
306  (i == j && (*d)(i,i) < 0)) {
307  //d->Print();
308  Log() << kWARNING << "<GetSQRootMatrix> error in matrix diagonalization; printed S and B" << Endl;
309  }
310  }
311  }
312 
313  // make exactly diagonal
314  for (i=0; i<n; i++) for (j=0; j<n; j++) if (j != i) (*d)(i,j) = 0;
315 
316  // compute the square-root C' of covariance matrix: C = C'*C'
317  for (i=0; i<n; i++) (*d)(i,i) = TMath::Sqrt((*d)(i,i));
318 
319  TMatrixD* sqrtMat = new TMatrixD( n, n );
320  sqrtMat->Mult( (*s), (*d) );
321  (*sqrtMat) *= (*si);
322 
323  // invert square-root matrices
324  sqrtMat->Invert();
325 
326  delete eigen;
327  delete s;
328  delete si;
329  delete d;
330 
331  return sqrtMat;
332 }
333 
334 ////////////////////////////////////////////////////////////////////////////////
335 /// turns covariance into correlation matrix
336 
338 {
339  if (covMat == 0) return 0;
340 
341  // sanity check
342  Int_t nvar = covMat->GetNrows();
343  if (nvar != covMat->GetNcols())
344  Log() << kFATAL << "<GetCorrelationMatrix> input matrix not quadratic" << Endl;
345 
346  TMatrixD* corrMat = new TMatrixD( nvar, nvar );
347 
348  for (Int_t ivar=0; ivar<nvar; ivar++) {
349  for (Int_t jvar=0; jvar<nvar; jvar++) {
350  if (ivar != jvar) {
351  Double_t d = (*covMat)(ivar, ivar)*(*covMat)(jvar, jvar);
352  if (d > 1E-20) (*corrMat)(ivar, jvar) = (*covMat)(ivar, jvar)/TMath::Sqrt(d);
353  else {
354  Log() << kWARNING << "<GetCorrelationMatrix> zero variances for variables "
355  << "(" << ivar << ", " << jvar << ")" << Endl;
356  (*corrMat)(ivar, jvar) = 0;
357  }
358  if (TMath::Abs( (*corrMat)(ivar,jvar)) > 1){
359  Log() << kWARNING
360  << " Element corr("<<ivar<<","<<ivar<<")=" << (*corrMat)(ivar,jvar)
361  << " sigma2="<<d
362  << " cov("<<ivar<<","<<ivar<<")=" <<(*covMat)(ivar, ivar)
363  << " cov("<<jvar<<","<<jvar<<")=" <<(*covMat)(jvar, jvar)
364  << Endl;
365 
366  }
367  }
368  else (*corrMat)(ivar, ivar) = 1.0;
369  }
370  }
371 
372  return corrMat;
373 }
374 
375 ////////////////////////////////////////////////////////////////////////////////
376 /// projects variable from tree into normalised histogram
377 
378 TH1* TMVA::Tools::projNormTH1F( TTree* theTree, const TString& theVarName,
379  const TString& name, Int_t nbins,
380  Double_t xmin, Double_t xmax, const TString& cut )
381 {
382  // needed because of ROOT bug (feature) that excludes events that have value == xmax
383  xmax += 0.00001;
384 
385  TH1* hist = new TH1F( name, name, nbins, xmin, xmax );
386  hist->Sumw2(); // enable quadratic errors
387  theTree->Project( name, theVarName, cut );
388  NormHist( hist );
389  return hist;
390 }
391 
392 ////////////////////////////////////////////////////////////////////////////////
393 /// normalises histogram
394 
396 {
397  if (!theHist) return 0;
398 
399  if (theHist->GetSumw2N() == 0) theHist->Sumw2();
400  if (theHist->GetSumOfWeights() != 0) {
401  Double_t w = ( theHist->GetSumOfWeights()
402  *(theHist->GetXaxis()->GetXmax() - theHist->GetXaxis()->GetXmin())/theHist->GetNbinsX() );
403  if (w > 0) theHist->Scale( norm/w );
404  return w;
405  }
406 
407  return 1.0;
408 }
409 
410 ////////////////////////////////////////////////////////////////////////////////
411 /// Parse the string and cut into labels separated by ":"
412 
413 TList* TMVA::Tools::ParseFormatLine( TString formatString, const char* sep )
414 {
415  TList* labelList = new TList();
416  labelList->SetOwner();
417  while (formatString.First(sep)==0) formatString.Remove(0,1); // remove initial separators
418 
419  while (formatString.Length()>0) {
420  if (formatString.First(sep) == -1) { // no more separator
421  labelList->Add(new TObjString(formatString.Data()));
422  formatString="";
423  break;
424  }
425 
426  Ssiz_t posSep = formatString.First(sep);
427  labelList->Add(new TObjString(TString(formatString(0,posSep)).Data()));
428  formatString.Remove(0,posSep+1);
429 
430  while (formatString.First(sep)==0) formatString.Remove(0,1); // remove additional separators
431 
432  }
433  return labelList;
434 }
435 
436 ////////////////////////////////////////////////////////////////////////////////
437 /// parse option string for ANN methods
438 /// default settings (should be defined in theOption string)
439 
440 vector<Int_t>* TMVA::Tools::ParseANNOptionString( TString theOptions, Int_t nvar,
441  vector<Int_t>* nodes )
442 {
443  TList* list = TMVA::Tools::ParseFormatLine( theOptions, ":" );
444 
445  // format and syntax of option string: "3000:N:N+2:N-3:6"
446  //
447  // where:
448  // 3000 - number of training cycles (epochs)
449  // N - number of nodes in first hidden layer, where N is the number
450  // of discriminating variables used (note that the first ANN
451  // layer necessarily has N nodes, and hence is not given).
452  // N+2 - number of nodes in 2nd hidden layer (2 nodes more than
453  // number of variables)
454  // N-3 - number of nodes in 3rd hidden layer (3 nodes less than
455  // number of variables)
456  // 6 - 6 nodes in last (4th) hidden layer (note that the last ANN
457  // layer in MVA has 2 nodes, each one for signal and background
458  // classes)
459 
460  // sanity check
461  if (list->GetSize() < 1) {
462  Log() << kFATAL << "<ParseANNOptionString> unrecognized option string: " << theOptions << Endl;
463  }
464 
465  // add number of cycles
466  nodes->push_back( atoi( ((TObjString*)list->At(0))->GetString() ) );
467 
468  Int_t a;
469  if (list->GetSize() > 1) {
470  for (Int_t i=1; i<list->GetSize(); i++) {
471  TString s = ((TObjString*)list->At(i))->GetString();
472  s.ToUpper();
473  if (s(0) == 'N') {
474  if (s.Length() > 1) nodes->push_back( nvar + atoi(&s[1]) );
475  else nodes->push_back( nvar );
476  }
477  else if ((a = atoi( s )) > 0) nodes->push_back( atoi(s ) );
478  else {
479  Log() << kFATAL << "<ParseANNOptionString> unrecognized option string: " << theOptions << Endl;
480  }
481  }
482  }
483 
484  return nodes;
485 }
486 
487 Bool_t TMVA::Tools::CheckSplines( const TH1* theHist, const TSpline* theSpline )
488 {
489  // check quality of splining by comparing splines and histograms in each bin
490  const Double_t sanityCrit = 0.01; // relative deviation
491 
492  Bool_t retval = kTRUE;
493  for (Int_t ibin=1; ibin<=theHist->GetNbinsX(); ibin++) {
494  Double_t x = theHist->GetBinCenter( ibin );
495  Double_t yh = theHist->GetBinContent( ibin ); // the histogram output
496  Double_t ys = theSpline->Eval( x ); // the spline output
497 
498  if (ys + yh > 0) {
499  Double_t dev = 0.5*(ys - yh)/(ys + yh);
500  if (TMath::Abs(dev) > sanityCrit) {
501  Log() << kFATAL << "<CheckSplines> Spline failed sanity criterion; "
502  << " relative deviation from histogram: " << dev
503  << " in (bin, value): (" << ibin << ", " << x << ")" << Endl;
504  retval = kFALSE;
505  }
506  }
507  }
508 
509  return retval;
510 }
511 
512 ////////////////////////////////////////////////////////////////////////////////
513 /// computes difference between two vectors
514 
515 std::vector<Double_t> TMVA::Tools::MVADiff( std::vector<Double_t>& a, std::vector<Double_t>& b )
516 {
517  if (a.size() != b.size()) {
518  throw;
519  }
520  vector<Double_t> result(a.size());
521  for (UInt_t i=0; i<a.size();i++) result[i]=a[i]-b[i];
522  return result;
523 }
524 
525 ////////////////////////////////////////////////////////////////////////////////
526 /// scales double vector
527 
528 void TMVA::Tools::Scale( std::vector<Double_t>& v, Double_t f )
529 {
530  for (UInt_t i=0; i<v.size();i++) v[i]*=f;
531 }
532 
533 ////////////////////////////////////////////////////////////////////////////////
534 /// scales float vector
535 
536 void TMVA::Tools::Scale( std::vector<Float_t>& v, Float_t f )
537 {
538  for (UInt_t i=0; i<v.size();i++) v[i]*=f;
539 }
540 
541 ////////////////////////////////////////////////////////////////////////////////
542 /// sort 2D vector (AND in parallel a TString vector) in such a way
543 /// that the "first vector is sorted" and the other vectors are reshuffled
544 /// in the same way as necessary to have the first vector sorted.
545 /// I.e. the correlation between the elements is kept.
546 
547 void TMVA::Tools::UsefulSortAscending( std::vector<vector<Double_t> >& v, std::vector<TString>* vs ){
548  UInt_t nArrays=v.size();
549  Double_t temp;
550  if (nArrays > 0) {
551  UInt_t sizeofarray=v[0].size();
552  for (UInt_t i=0; i<sizeofarray; i++) {
553  for (UInt_t j=sizeofarray-1; j>i; j--) {
554  if (v[0][j-1] > v[0][j]) {
555  for (UInt_t k=0; k< nArrays; k++) {
556  temp = v[k][j-1]; v[k][j-1] = v[k][j]; v[k][j] = temp;
557  }
558  if (NULL != vs) {
559  TString temps = (*vs)[j-1]; (*vs)[j-1] = (*vs)[j]; (*vs)[j] = temps;
560  }
561  }
562  }
563  }
564  }
565 }
566 
567 ////////////////////////////////////////////////////////////////////////////////
568 /// sort 2D vector (AND in parallel a TString vector) in such a way
569 /// that the "first vector is sorted" and the other vectors are reshuffled
570 /// in the same way as necessary to have the first vector sorted.
571 /// I.e. the correlation between the elements is kept.
572 
573 void TMVA::Tools::UsefulSortDescending( std::vector<std::vector<Double_t> >& v, std::vector<TString>* vs )
574 {
575  UInt_t nArrays=v.size();
576  Double_t temp;
577  if (nArrays > 0) {
578  UInt_t sizeofarray=v[0].size();
579  for (UInt_t i=0; i<sizeofarray; i++) {
580  for (UInt_t j=sizeofarray-1; j>i; j--) {
581  if (v[0][j-1] < v[0][j]) {
582  for (UInt_t k=0; k< nArrays; k++) {
583  temp = v[k][j-1]; v[k][j-1] = v[k][j]; v[k][j] = temp;
584  }
585  if (NULL != vs) {
586  TString temps = (*vs)[j-1]; (*vs)[j-1] = (*vs)[j]; (*vs)[j] = temps;
587  }
588  }
589  }
590  }
591  }
592 }
593 
594 ////////////////////////////////////////////////////////////////////////////////
595 /// Mutual Information method for non-linear correlations estimates in 2D histogram
596 /// Author: Moritz Backes, Geneva (2009)
597 
599 {
600  Double_t hi = h_.Integral();
601  if (hi == 0) return -1;
602 
603  // copy histogram and rebin to speed up procedure
604  TH2F h( h_ );
605  h.RebinX(2);
606  h.RebinY(2);
607 
608  Double_t mutualInfo = 0.;
609  Int_t maxBinX = h.GetNbinsX();
610  Int_t maxBinY = h.GetNbinsY();
611  for (Int_t x = 1; x <= maxBinX; x++) {
612  for (Int_t y = 1; y <= maxBinY; y++) {
613  Double_t p_xy = h.GetBinContent(x,y)/hi;
614  Double_t p_x = h.Integral(x,x,1,maxBinY)/hi;
615  Double_t p_y = h.Integral(1,maxBinX,y,y)/hi;
616  if (p_x > 0. && p_y > 0. && p_xy > 0.){
617  mutualInfo += p_xy*TMath::Log(p_xy / (p_x * p_y));
618  }
619  }
620  }
621 
622  return mutualInfo;
623 }
624 
625 ////////////////////////////////////////////////////////////////////////////////
626 /// Compute Correlation Ratio of 2D histogram to estimate functional dependency between two variables
627 /// Author: Moritz Backes, Geneva (2009)
628 
630 {
631  Double_t hi = h_.Integral();
632  if (hi == 0.) return -1;
633 
634  // copy histogram and rebin to speed up procedure
635  TH2F h( h_ );
636  h.RebinX(2);
637  h.RebinY(2);
638 
639  Double_t corrRatio = 0.;
640  Double_t y_mean = h.ProjectionY()->GetMean();
641  for (Int_t ix=1; ix<=h.GetNbinsX(); ix++) {
642  corrRatio += (h.Integral(ix,ix,1,h.GetNbinsY())/hi)*pow((GetYMean_binX(h,ix)-y_mean),2);
643  }
644  corrRatio /= pow(h.ProjectionY()->GetRMS(),2);
645  return corrRatio;
646 }
647 
648 ////////////////////////////////////////////////////////////////////////////////
649 /// Compute the mean in Y for a given bin X of a 2D histogram
650 
652 {
653  if (h.Integral(bin_x,bin_x,1,h.GetNbinsY()) == 0.) {return 0;}
654  Double_t y_bin_mean = 0.;
655  TH1* py = h.ProjectionY();
656  for (Int_t y = 1; y <= h.GetNbinsY(); y++){
657  y_bin_mean += h.GetBinContent(bin_x,y)*py->GetBinCenter(y);
658  }
659  y_bin_mean /= h.Integral(bin_x,bin_x,1,h.GetNbinsY());
660  return y_bin_mean;
661 }
662 
663 ////////////////////////////////////////////////////////////////////////////////
664 /// Transpose quadratic histogram
665 
666 TH2F* TMVA::Tools::TransposeHist( const TH2F& h )
667 {
668  // sanity check
669  if (h.GetNbinsX() != h.GetNbinsY()) {
670  Log() << kFATAL << "<TransposeHist> cannot transpose non-quadratic histogram" << Endl;
671  }
672 
673  TH2F *transposedHisto = new TH2F( h );
674  for (Int_t ix=1; ix <= h.GetNbinsX(); ix++){
675  for (Int_t iy=1; iy <= h.GetNbinsY(); iy++){
676  transposedHisto->SetBinContent(iy,ix,h.GetBinContent(ix,iy));
677  }
678  }
679 
680  // copy stats (thanks to Swagato Banerjee for pointing out the missing stats information)
681  Double_t stats_old[7];
682  Double_t stats_new[7];
683 
684  h.GetStats(stats_old);
685  stats_new[0] = stats_old[0];
686  stats_new[1] = stats_old[1];
687  stats_new[2] = stats_old[4];
688  stats_new[3] = stats_old[5];
689  stats_new[4] = stats_old[2];
690  stats_new[5] = stats_old[3];
691  stats_new[6] = stats_old[6];
692  transposedHisto->PutStats(stats_new);
693 
694  return transposedHisto; // ownership returned
695 }
696 
697 ////////////////////////////////////////////////////////////////////////////////
698 /// check for "silence" option in configuration option string
699 
701 {
702  Bool_t isSilent = kFALSE;
703 
704  TString s( cs );
705  s.ToLower();
706  s.ReplaceAll(" ","");
707  if (s.Contains("silent") && !s.Contains("silent=f")) {
708  if (!s.Contains("!silent") || s.Contains("silent=t")) isSilent = kTRUE;
709  }
710 
711  return isSilent;
712 }
713 
714 ////////////////////////////////////////////////////////////////////////////////
715 /// check if verbosity "V" set in option
716 
718 {
719  Bool_t isVerbose = kFALSE;
720 
721  TString s( cs );
722  s.ToLower();
723  s.ReplaceAll(" ","");
724  std::vector<TString> v = SplitString( s, ':' );
725  for (std::vector<TString>::iterator it = v.begin(); it != v.end(); it++) {
726  if ((*it == "v" || *it == "verbose") && !it->Contains("!")) isVerbose = kTRUE;
727  }
728 
729  return isVerbose;
730 }
731 
732 ////////////////////////////////////////////////////////////////////////////////
733 /// sort vector
734 
735 void TMVA::Tools::UsefulSortDescending( std::vector<Double_t>& v )
736 {
737  vector< vector<Double_t> > vtemp;
738  vtemp.push_back(v);
739  UsefulSortDescending(vtemp);
740  v = vtemp[0];
741 }
742 
743 ////////////////////////////////////////////////////////////////////////////////
744 /// sort vector
745 
746 void TMVA::Tools::UsefulSortAscending( std::vector<Double_t>& v )
747 {
748  vector<vector<Double_t> > vtemp;
749  vtemp.push_back(v);
750  UsefulSortAscending(vtemp);
751  v = vtemp[0];
752 }
753 
754 ////////////////////////////////////////////////////////////////////////////////
755 /// find index of maximum entry in vector
756 
757 Int_t TMVA::Tools::GetIndexMaxElement( std::vector<Double_t>& v )
758 {
759  if (v.empty()) return -1;
760 
761  Int_t pos=0; Double_t mx=v[0];
762  for (UInt_t i=0; i<v.size(); i++){
763  if (v[i] > mx){
764  mx=v[i];
765  pos=i;
766  }
767  }
768  return pos;
769 }
770 
771 ////////////////////////////////////////////////////////////////////////////////
772 /// find index of minimum entry in vector
773 
774 Int_t TMVA::Tools::GetIndexMinElement( std::vector<Double_t>& v )
775 {
776  if (v.empty()) return -1;
777 
778  Int_t pos=0; Double_t mn=v[0];
779  for (UInt_t i=0; i<v.size(); i++){
780  if (v[i] < mn){
781  mn=v[i];
782  pos=i;
783  }
784  }
785  return pos;
786 }
787 
788 
789 ////////////////////////////////////////////////////////////////////////////////
790 /// check if regular expression
791 /// helper function to search for "$!%^&()'<>?= " in a string
792 
794 {
795  Bool_t regular = kFALSE;
796  for (Int_t i = 0; i < Tools::fRegexp.Length(); i++)
797  if (s.Contains( Tools::fRegexp[i] )) { regular = kTRUE; break; }
798 
799  return regular;
800 }
801 
802 ////////////////////////////////////////////////////////////////////////////////
803 /// replace regular expressions
804 /// helper function to remove all occurences "$!%^&()'<>?= " from a string
805 /// and replace all ::,$,*,/,+,- with _M_,_S_,_T_,_D_,_P_,_M_ respectively
806 
808 {
809  TString snew = s;
810  for (Int_t i = 0; i < Tools::fRegexp.Length(); i++)
811  snew.ReplaceAll( Tools::fRegexp[i], r );
812 
813  snew.ReplaceAll( "::", r );
814  snew.ReplaceAll( "$", "_S_" );
815  snew.ReplaceAll( "&", "_A_" );
816  snew.ReplaceAll( "%", "_MOD_" );
817  snew.ReplaceAll( "|", "_O_" );
818  snew.ReplaceAll( "*", "_T_" );
819  snew.ReplaceAll( "/", "_D_" );
820  snew.ReplaceAll( "+", "_P_" );
821  snew.ReplaceAll( "-", "_M_" );
822  snew.ReplaceAll( " ", "_" );
823  snew.ReplaceAll( "[", "_" );
824  snew.ReplaceAll( "]", "_" );
825  snew.ReplaceAll( "=", "_E_" );
826  snew.ReplaceAll( ">", "_GT_" );
827  snew.ReplaceAll( "<", "_LT_" );
828  snew.ReplaceAll( "(", "_" );
829  snew.ReplaceAll( ")", "_" );
830 
831  return snew;
832 }
833 
834 ////////////////////////////////////////////////////////////////////////////////
835 /// human readable color strings
836 
838 {
839  static const TString gClr_none = "" ;
840  static const TString gClr_white = "\033[1;37m"; // white
841  static const TString gClr_black = "\033[30m"; // black
842  static const TString gClr_blue = "\033[34m"; // blue
843  static const TString gClr_red = "\033[1;31m" ; // red
844  static const TString gClr_yellow = "\033[1;33m"; // yellow
845  static const TString gClr_darkred = "\033[31m"; // dark red
846  static const TString gClr_darkgreen = "\033[32m"; // dark green
847  static const TString gClr_darkyellow = "\033[33m"; // dark yellow
848 
849  static const TString gClr_bold = "\033[1m" ; // bold
850  static const TString gClr_black_b = "\033[30m" ; // bold black
851  static const TString gClr_lblue_b = "\033[1;34m" ; // bold light blue
852  static const TString gClr_cyan_b = "\033[0;36m" ; // bold cyan
853  static const TString gClr_lgreen_b = "\033[1;32m"; // bold light green
854 
855  static const TString gClr_blue_bg = "\033[44m"; // blue background
856  static const TString gClr_red_bg = "\033[1;41m"; // white on red background
857  static const TString gClr_whiteonblue = "\033[1;44m"; // white on blue background
858  static const TString gClr_whiteongreen = "\033[1;42m"; // white on green background
859  static const TString gClr_grey_bg = "\033[47m"; // grey background
860 
861  static const TString gClr_reset = "\033[0m"; // reset
862 
863  if (!gConfig().UseColor()) return gClr_none;
864 
865  if (c == "white" ) return gClr_white;
866  if (c == "blue" ) return gClr_blue;
867  if (c == "black" ) return gClr_black;
868  if (c == "lightblue") return gClr_cyan_b;
869  if (c == "yellow") return gClr_yellow;
870  if (c == "red" ) return gClr_red;
871  if (c == "dred" ) return gClr_darkred;
872  if (c == "dgreen") return gClr_darkgreen;
873  if (c == "lgreenb") return gClr_lgreen_b;
874  if (c == "dyellow") return gClr_darkyellow;
875 
876  if (c == "bold") return gClr_bold;
877  if (c == "bblack") return gClr_black_b;
878 
879  if (c == "blue_bgd") return gClr_blue_bg;
880  if (c == "red_bgd" ) return gClr_red_bg;
881 
882  if (c == "white_on_blue" ) return gClr_whiteonblue;
883  if (c == "white_on_green") return gClr_whiteongreen;
884 
885  if (c == "reset") return gClr_reset;
886 
887  std::cout << "Unknown color " << c << std::endl;
888  exit(1);
889 
890  return gClr_none;
891 }
892 
893 ////////////////////////////////////////////////////////////////////////////////
894 /// formatted output of simple table
895 
896 void TMVA::Tools::FormattedOutput( const std::vector<Double_t>& values, const std::vector<TString>& V,
897  const TString titleVars, const TString titleValues, MsgLogger& logger,
898  TString format )
899 {
900  // sanity check
901  UInt_t nvar = V.size();
902  if ((UInt_t)values.size() != nvar) {
903  logger << kFATAL << "<FormattedOutput> fatal error with dimensions: "
904  << values.size() << " OR " << " != " << nvar << Endl;
905  }
906 
907  // find maximum length in V (and column title)
908  UInt_t maxL = 7;
909  std::vector<UInt_t> vLengths;
910  for (UInt_t ivar=0; ivar<nvar; ivar++) maxL = TMath::Max( (UInt_t)V[ivar].Length(), maxL );
911  maxL = TMath::Max( (UInt_t)titleVars.Length(), maxL );
912 
913  // column length
914  UInt_t maxV = 7;
915  maxV = TMath::Max( (UInt_t)titleValues.Length() + 1, maxL );
916 
917  // full column length
918  UInt_t clen = maxL + maxV + 3;
919 
920  // bar line
921  for (UInt_t i=0; i<clen; i++) logger << "-";
922  logger << Endl;
923 
924  // title bar
925  logger << setw(maxL) << titleVars << ":";
926  logger << setw(maxV+1) << titleValues << ":";
927  logger << Endl;
928  for (UInt_t i=0; i<clen; i++) logger << "-";
929  logger << Endl;
930 
931  // the numbers
932  for (UInt_t irow=0; irow<nvar; irow++) {
933  logger << setw(maxL) << V[irow] << ":";
934  logger << setw(maxV+1) << Form( format.Data(), values[irow] );
935  logger << Endl;
936  }
937 
938  // bar line
939  for (UInt_t i=0; i<clen; i++) logger << "-";
940  logger << Endl;
941 }
942 
943 ////////////////////////////////////////////////////////////////////////////////
944 /// formatted output of matrix (with labels)
945 
946 void TMVA::Tools::FormattedOutput( const TMatrixD& M, const std::vector<TString>& V, MsgLogger& logger )
947 {
948  // sanity check: matrix must be quadratic
949  UInt_t nvar = V.size();
950  if ((UInt_t)M.GetNcols() != nvar || (UInt_t)M.GetNrows() != nvar) {
951  logger << kFATAL << "<FormattedOutput> fatal error with dimensions: "
952  << M.GetNcols() << " OR " << M.GetNrows() << " != " << nvar << " ==> abort" << Endl;
953  }
954 
955  // get length of each variable, and maximum length
956  UInt_t minL = 7;
957  UInt_t maxL = minL;
958  std::vector<UInt_t> vLengths;
959  for (UInt_t ivar=0; ivar<nvar; ivar++) {
960  vLengths.push_back(TMath::Max( (UInt_t)V[ivar].Length(), minL ));
961  maxL = TMath::Max( vLengths.back(), maxL );
962  }
963 
964  // count column length
965  UInt_t clen = maxL+1;
966  for (UInt_t icol=0; icol<nvar; icol++) clen += vLengths[icol]+1;
967 
968  // bar line
969  for (UInt_t i=0; i<clen; i++) logger << "-";
970  logger << Endl;
971 
972  // title bar
973  logger << setw(maxL+1) << " ";
974  for (UInt_t icol=0; icol<nvar; icol++) logger << setw(vLengths[icol]+1) << V[icol];
975  logger << Endl;
976 
977  // the numbers
978  for (UInt_t irow=0; irow<nvar; irow++) {
979  logger << setw(maxL) << V[irow] << ":";
980  for (UInt_t icol=0; icol<nvar; icol++) {
981  logger << setw(vLengths[icol]+1) << Form( "%+1.3f", M(irow,icol) );
982  }
983  logger << Endl;
984  }
985 
986  // bar line
987  for (UInt_t i=0; i<clen; i++) logger << "-";
988  logger << Endl;
989 }
990 
991 ////////////////////////////////////////////////////////////////////////////////
992 /// formatted output of matrix (with labels)
993 
995  const std::vector<TString>& vert, const std::vector<TString>& horiz,
996  MsgLogger& logger )
997 {
998  // sanity check: matrix must be quadratic
999  UInt_t nvvar = vert.size();
1000  UInt_t nhvar = horiz.size();
1001 
1002  // get length of each variable, and maximum length
1003  UInt_t minL = 7;
1004  UInt_t maxL = minL;
1005  std::vector<UInt_t> vLengths;
1006  for (UInt_t ivar=0; ivar<nvvar; ivar++) {
1007  vLengths.push_back(TMath::Max( (UInt_t)vert[ivar].Length(), minL ));
1008  maxL = TMath::Max( vLengths.back(), maxL );
1009  }
1010 
1011  // count column length
1012  UInt_t minLh = 7;
1013  UInt_t maxLh = minLh;
1014  std::vector<UInt_t> hLengths;
1015  for (UInt_t ivar=0; ivar<nhvar; ivar++) {
1016  hLengths.push_back(TMath::Max( (UInt_t)horiz[ivar].Length(), minL ));
1017  maxLh = TMath::Max( hLengths.back(), maxLh );
1018  }
1019 
1020  UInt_t clen = maxLh+1;
1021  for (UInt_t icol=0; icol<nhvar; icol++) clen += hLengths[icol]+1;
1022 
1023  // bar line
1024  for (UInt_t i=0; i<clen; i++) logger << "-";
1025  logger << Endl;
1026 
1027  // title bar
1028  logger << setw(maxL+1) << " ";
1029  for (UInt_t icol=0; icol<nhvar; icol++) logger << setw(hLengths[icol]+1) << horiz[icol];
1030  logger << Endl;
1031 
1032  // the numbers
1033  for (UInt_t irow=0; irow<nvvar; irow++) {
1034  logger << setw(maxL) << vert[irow] << ":";
1035  for (UInt_t icol=0; icol<nhvar; icol++) {
1036  logger << setw(hLengths[icol]+1) << Form( "%+1.3f", M(irow,icol) );
1037  }
1038  logger << Endl;
1039  }
1040 
1041  // bar line
1042  for (UInt_t i=0; i<clen; i++) logger << "-";
1043  logger << Endl;
1044 }
1045 
1046 ////////////////////////////////////////////////////////////////////////////////
1047 /// histogramming utility
1048 
1050 {
1051  return ( unit == "" ? title : ( title + " [" + unit + "]" ) );
1052 }
1053 
1054 ////////////////////////////////////////////////////////////////////////////////
1055 /// histogramming utility
1056 
1057 TString TMVA::Tools::GetYTitleWithUnit( const TH1& h, const TString& unit, Bool_t normalised )
1058 {
1059  TString retval = ( normalised ? "(1/N) " : "" );
1060  retval += Form( "dN_{ }/^{ }%.3g %s", h.GetXaxis()->GetBinWidth(1), unit.Data() );
1061  return retval;
1062 }
1063 
1064 ////////////////////////////////////////////////////////////////////////////////
1065 /// writes a float value with the available precision to a stream
1066 
1068 {
1069  os << val << " :: ";
1070  void * c = &val;
1071  for (int i=0; i<4; i++) {
1072  Int_t ic = *((char*)c+i)-'\0';
1073  if (ic<0) ic+=256;
1074  os << ic << " ";
1075  }
1076  os << ":: ";
1077 }
1078 
1079 ////////////////////////////////////////////////////////////////////////////////
1080 /// reads a float value with the available precision from a stream
1081 
1083 {
1084  Float_t a = 0;
1085  is >> a;
1086  TString dn;
1087  is >> dn;
1088  Int_t c[4];
1089  void * ap = &a;
1090  for (int i=0; i<4; i++) {
1091  is >> c[i];
1092  *((char*)ap+i) = '\0'+c[i];
1093  }
1094  is >> dn;
1095  val = a;
1096 }
1097 
1098 
1099 // XML file reading/writing helper functions
1100 
1101 ////////////////////////////////////////////////////////////////////////////////
1102 /// add attribute from xml
1103 
1104 Bool_t TMVA::Tools::HasAttr( void* node, const char* attrname )
1105 {
1106  return xmlengine().HasAttr(node, attrname);
1107 }
1108 
1109 ////////////////////////////////////////////////////////////////////////////////
1110 /// add attribute from xml
1111 
1112 void TMVA::Tools::ReadAttr( void* node, const char* attrname, TString& value )
1113 {
1114  if (!HasAttr(node, attrname)) {
1115  const char * nodename = xmlengine().GetNodeName(node);
1116  Log() << kFATAL << "Trying to read non-existing attribute '" << attrname << "' from xml node '" << nodename << "'" << Endl;
1117  }
1118  const char* val = xmlengine().GetAttr(node, attrname);
1119  value = TString(val);
1120 }
1121 
1122 ////////////////////////////////////////////////////////////////////////////////
1123 /// add attribute to node
1124 
1125 void TMVA::Tools::AddAttr( void* node, const char* attrname, const char* value )
1126 {
1127  if( node == 0 ) return;
1128  gTools().xmlengine().NewAttr(node, 0, attrname, value );
1129 }
1130 
1131 ////////////////////////////////////////////////////////////////////////////////
1132 /// add child node
1133 
1134 void* TMVA::Tools::AddChild( void* parent, const char* childname, const char* content, bool isRootNode )
1135 {
1136  if( !isRootNode && parent == 0 ) return 0;
1137  return gTools().xmlengine().NewChild(parent, 0, childname, content);
1138 }
1139 
1140 ////////////////////////////////////////////////////////////////////////////////
1141 
1142 Bool_t TMVA::Tools::AddComment( void* node, const char* comment ) {
1143  if( node == 0 ) return kFALSE;
1144  return gTools().xmlengine().AddComment(node, comment);
1145 }
1146 ////////////////////////////////////////////////////////////////////////////////
1147 /// get parent node
1148 
1149 void* TMVA::Tools::GetParent( void* child)
1150 {
1151  void* par = xmlengine().GetParent(child);
1152 
1153  return par;
1154 }
1155 ////////////////////////////////////////////////////////////////////////////////
1156 /// get child node
1157 
1158 void* TMVA::Tools::GetChild( void* parent, const char* childname )
1159 {
1160  void* ch = xmlengine().GetChild(parent);
1161  if (childname != 0) {
1162  while (ch!=0 && strcmp(xmlengine().GetNodeName(ch),childname) != 0) ch = xmlengine().GetNext(ch);
1163  }
1164  return ch;
1165 }
1166 
1167 ////////////////////////////////////////////////////////////////////////////////
1168 /// XML helpers
1169 
1170 void* TMVA::Tools::GetNextChild( void* prevchild, const char* childname )
1171 {
1172  void* ch = xmlengine().GetNext(prevchild);
1173  if (childname != 0) {
1174  while (ch!=0 && strcmp(xmlengine().GetNodeName(ch),childname)!=0) ch = xmlengine().GetNext(ch);
1175  }
1176  return ch;
1177 }
1178 
1179 ////////////////////////////////////////////////////////////////////////////////
1180 /// XML helpers
1181 
1182 const char* TMVA::Tools::GetContent( void* node )
1183 {
1184  return xmlengine().GetNodeContent(node);
1185 }
1186 
1187 ////////////////////////////////////////////////////////////////////////////////
1188 /// XML helpers
1189 
1190 const char* TMVA::Tools::GetName( void* node )
1191 {
1192  return xmlengine().GetNodeName(node);
1193 }
1194 
1195 ////////////////////////////////////////////////////////////////////////////////
1196 /// XML helpers
1197 
1198 Bool_t TMVA::Tools::AddRawLine( void* node, const char * raw )
1199 {
1200  return xmlengine().AddRawLine( node, raw );
1201 }
1202 
1203 ////////////////////////////////////////////////////////////////////////////////
1204 /// splits the option string at 'separator' and fills the list
1205 /// 'splitV' with the primitive strings
1206 
1207 std::vector<TString> TMVA::Tools::SplitString(const TString& theOpt, const char separator ) const
1208 {
1209  std::vector<TString> splitV;
1210  TString splitOpt(theOpt);
1211  splitOpt.ReplaceAll("\n"," ");
1212  splitOpt = splitOpt.Strip(TString::kBoth,separator);
1213  while (splitOpt.Length()>0) {
1214  if ( !splitOpt.Contains(separator) ) {
1215  splitV.push_back(splitOpt);
1216  break;
1217  }
1218  else {
1219  TString toSave = splitOpt(0,splitOpt.First(separator));
1220  splitV.push_back(toSave);
1221  splitOpt = splitOpt(splitOpt.First(separator),splitOpt.Length());
1222  }
1223  splitOpt = splitOpt.Strip(TString::kLeading,separator);
1224  }
1225  return splitV;
1226 }
1227 
1228 ////////////////////////////////////////////////////////////////////////////////
1229 /// string tools
1230 
1232 {
1233  std::stringstream s;
1234  s << i;
1235  return TString(s.str().c_str());
1236 }
1237 
1238 ////////////////////////////////////////////////////////////////////////////////
1239 /// string tools
1240 
1242 {
1243  std::stringstream s;
1244  s << Form( "%5.8e", d );
1245  return TString(s.str().c_str());
1246 }
1247 
1248 ////////////////////////////////////////////////////////////////////////////////
1249 /// XML helpers
1250 
1251 void TMVA::Tools::WriteTMatrixDToXML( void* node, const char* name, TMatrixD* mat )
1252 {
1253  void* matnode = xmlengine().NewChild(node, 0, name);
1254  xmlengine().NewAttr(matnode,0,"Rows", StringFromInt(mat->GetNrows()) );
1255  xmlengine().NewAttr(matnode,0,"Columns", StringFromInt(mat->GetNcols()) );
1256  std::stringstream s;
1257  for (Int_t row = 0; row<mat->GetNrows(); row++) {
1258  for (Int_t col = 0; col<mat->GetNcols(); col++) {
1259  s << Form( "%5.15e ", (*mat)[row][col] );
1260  }
1261  }
1262  xmlengine().AddRawLine( matnode, s.str().c_str() );
1263 }
1264 
1265 ////////////////////////////////////////////////////////////////////////////////
1266 
1267 void TMVA::Tools::WriteTVectorDToXML( void* node, const char* name, TVectorD* vec )
1268 {
1269  TMatrixD mat(1,vec->GetNoElements(),&((*vec)[0]));
1270  WriteTMatrixDToXML( node, name, &mat );
1271 }
1272 
1273 ////////////////////////////////////////////////////////////////////////////////
1274 
1275 void TMVA::Tools::ReadTVectorDFromXML( void* node, const char* name, TVectorD* vec )
1276 {
1277  TMatrixD mat(1,vec->GetNoElements(),&((*vec)[0]));
1278  ReadTMatrixDFromXML( node, name, &mat );
1279  for (int i=0;i<vec->GetNoElements();++i) (*vec)[i] = mat[0][i];
1280 }
1281 
1282 ////////////////////////////////////////////////////////////////////////////////
1283 
1284 void TMVA::Tools::ReadTMatrixDFromXML( void* node, const char* name, TMatrixD* mat )
1285 {
1286  if (strcmp(xmlengine().GetNodeName(node),name)!=0){
1287  Log() << kWARNING << "Possible Error: Name of matrix in weight file"
1288  << " does not match name of matrix passed as argument!" << Endl;
1289  }
1290  Int_t nrows, ncols;
1291  ReadAttr( node, "Rows", nrows );
1292  ReadAttr( node, "Columns", ncols );
1293  if (mat->GetNrows() != nrows || mat->GetNcols() != ncols){
1294  Log() << kWARNING << "Possible Error: Dimension of matrix in weight file"
1295  << " does not match dimension of matrix passed as argument!" << Endl;
1296  mat->ResizeTo(nrows,ncols);
1297  }
1298  const char* content = xmlengine().GetNodeContent(node);
1299  std::stringstream s(content);
1300  for (Int_t row = 0; row<nrows; row++) {
1301  for (Int_t col = 0; col<ncols; col++) {
1302  s >> (*mat)[row][col];
1303  }
1304  }
1305 }
1306 
1307 ////////////////////////////////////////////////////////////////////////////////
1308 /// direct output, eg, when starting ROOT session -> no use of Logger here
1309 
1311 {
1312  std::cout << std::endl;
1313  std::cout << Color("bold") << "TMVA -- Toolkit for Multivariate Data Analysis" << Color("reset") << std::endl;
1314  std::cout << " " << "Version " << TMVA_RELEASE << ", " << TMVA_RELEASE_DATE << std::endl;
1315  std::cout << " " << "Copyright (C) 2005-2010 CERN, MPI-K Heidelberg, Us of Bonn and Victoria" << std::endl;
1316  std::cout << " " << "Home page: http://tmva.sf.net" << std::endl;
1317  std::cout << " " << "Citation info: http://tmva.sf.net/citeTMVA.html" << std::endl;
1318  std::cout << " " << "License: http://tmva.sf.net/LICENSE" << std::endl << std::endl;
1319 }
1320 
1321 ////////////////////////////////////////////////////////////////////////////////
1322 /// prints the TMVA release number and date
1323 
1325 {
1326  logger << "___________TMVA Version " << TMVA_RELEASE << ", " << TMVA_RELEASE_DATE
1327  << "" << Endl;
1328 }
1329 
1330 ////////////////////////////////////////////////////////////////////////////////
1331 /// prints the ROOT release number and date
1332 
1334 {
1335  static const char * const months[] = { "Jan","Feb","Mar","Apr","May",
1336  "Jun","Jul","Aug","Sep","Oct",
1337  "Nov","Dec" };
1338  Int_t idatqq = gROOT->GetVersionDate();
1339  Int_t iday = idatqq%100;
1340  Int_t imonth = (idatqq/100)%100;
1341  Int_t iyear = (idatqq/10000);
1342  TString versionDate = Form("%s %d, %4d",months[imonth-1],iday,iyear);
1343 
1344  logger << "You are running ROOT Version: " << gROOT->GetVersion() << ", " << versionDate << Endl;
1345 }
1346 
1347 ////////////////////////////////////////////////////////////////////////////////
1348 /// various kinds of welcome messages
1349 /// ASCII text generated by this site: http://www.network-science.de/ascii/
1350 
1352 {
1353  switch (msgType) {
1354 
1355  case kStandardWelcomeMsg:
1356  logger << Color("white") << "TMVA -- Toolkit for Multivariate Analysis" << Color("reset") << Endl;
1357  logger << "Copyright (C) 2005-2006 CERN, LAPP & MPI-K Heidelberg and Victoria U." << Endl;
1358  logger << "Home page http://tmva.sourceforge.net" << Endl;
1359  logger << "All rights reserved, please read http://tmva.sf.net/license.txt" << Endl << Endl;
1360  break;
1361 
1362  case kIsometricWelcomeMsg:
1363  logger << " ___ ___ ___ ___ " << Endl;
1364  logger << " /\\ \\ /\\__\\ /\\__\\ /\\ \\ " << Endl;
1365  logger << " \\:\\ \\ /::| | /:/ / /::\\ \\ " << Endl;
1366  logger << " \\:\\ \\ /:|:| | /:/ / /:/\\:\\ \\ " << Endl;
1367  logger << " /::\\ \\ /:/|:|__|__ /:/__/ ___ /::\\~\\:\\ \\ " << Endl;
1368  logger << " /:/\\:\\__\\ /:/ |::::\\__\\ |:| | /\\__\\ /:/\\:\\ \\:\\__\\ " << Endl;
1369  logger << " /:/ \\/__/ \\/__/~~/:/ / |:| |/:/ / \\/__\\:\\/:/ / " << Endl;
1370  logger << "/:/ / /:/ / |:|__/:/ / \\::/ / " << Endl;
1371  logger << "\\/__/ /:/ / \\::::/__/ /:/ / " << Endl;
1372  logger << " /:/ / ~~~~ /:/ / " << Endl;
1373  logger << " \\/__/ \\/__/ " << Endl << Endl;
1374  break;
1375 
1376  case kBlockWelcomeMsg:
1377  logger << Endl;
1378  logger << "_|_|_|_|_| _| _| _| _| _|_| " << Endl;
1379  logger << " _| _|_| _|_| _| _| _| _| " << Endl;
1380  logger << " _| _| _| _| _| _| _|_|_|_| " << Endl;
1381  logger << " _| _| _| _| _| _| _| " << Endl;
1382  logger << " _| _| _| _| _| _| " << Endl << Endl;
1383  break;
1384 
1385  case kLeanWelcomeMsg:
1386  logger << Endl;
1387  logger << "_/_/_/_/_/ _/ _/ _/ _/ _/_/ " << Endl;
1388  logger << " _/ _/_/ _/_/ _/ _/ _/ _/ " << Endl;
1389  logger << " _/ _/ _/ _/ _/ _/ _/_/_/_/ " << Endl;
1390  logger << " _/ _/ _/ _/ _/ _/ _/ " << Endl;
1391  logger << "_/ _/ _/ _/ _/ _/ " << Endl << Endl;
1392  break;
1393 
1394  case kLogoWelcomeMsg:
1395  logger << Endl;
1396  logger << "_/_/_/_/_/ _| _| _| _| _|_| " << Endl;
1397  logger << " _/ _|_| _|_| _| _| _| _| " << Endl;
1398  logger << " _/ _| _| _| _| _| _|_|_|_| " << Endl;
1399  logger << " _/ _| _| _| _| _| _| " << Endl;
1400  logger << "_/ _| _| _| _| _| " << Endl << Endl;
1401  break;
1402 
1403  case kSmall1WelcomeMsg:
1404  logger << " _____ __ ____ ___ " << Endl;
1405  logger << "|_ _| \\/ \\ \\ / /_\\ " << Endl;
1406  logger << " | | | |\\/| |\\ V / _ \\ " << Endl;
1407  logger << " |_| |_| |_| \\_/_/ \\_\\" << Endl << Endl;
1408  break;
1409 
1410  case kSmall2WelcomeMsg:
1411  logger << " _____ __ ____ ___ " << Endl;
1412  logger << "|_ _| \\/ \\ \\ / / \\ " << Endl;
1413  logger << " | | | |\\/| |\\ \\ / / _ \\ " << Endl;
1414  logger << " | | | | | | \\ V / ___ \\ " << Endl;
1415  logger << " |_| |_| |_| \\_/_/ \\_\\ " << Endl << Endl;
1416  break;
1417 
1418  case kOriginalWelcomeMsgColor:
1419  logger << kINFO << "" << Color("red")
1420  << "_______________________________________" << Color("reset") << Endl;
1421  logger << kINFO << "" << Color("blue")
1422  << Color("red_bgd") << Color("bwhite") << " // " << Color("reset")
1423  << Color("white") << Color("blue_bgd")
1424  << "|\\ /|| \\ // /\\\\\\\\\\\\\\\\\\\\\\\\ \\ \\ \\ " << Color("reset") << Endl;
1425  logger << kINFO << ""<< Color("blue")
1426  << Color("red_bgd") << Color("white") << "// " << Color("reset")
1427  << Color("white") << Color("blue_bgd")
1428  << "| \\/ || \\// /--\\\\\\\\\\\\\\\\\\\\\\\\ \\ \\ \\" << Color("reset") << Endl;
1429  break;
1430 
1431  case kOriginalWelcomeMsgBW:
1432  logger << kINFO << ""
1433  << "_______________________________________" << Endl;
1434  logger << kINFO << " // "
1435  << "|\\ /|| \\ // /\\\\\\\\\\\\\\\\\\\\\\\\ \\ \\ \\ " << Endl;
1436  logger << kINFO << "// "
1437  << "| \\/ || \\// /--\\\\\\\\\\\\\\\\\\\\\\\\ \\ \\ \\" << Endl;
1438  break;
1439 
1440  default:
1441  logger << kFATAL << "unknown message type: " << msgType << Endl;
1442  }
1443 }
1444 
1445 ////////////////////////////////////////////////////////////////////////////////
1446 /// kinds of TMVA citation
1447 
1449 {
1450  switch (citType) {
1451 
1452  case kPlainText:
1453  logger << "A. Hoecker, P. Speckmayer, J. Stelzer, J. Therhaag, E. von Toerne, H. Voss" << Endl;
1454  logger << "\"TMVA - Toolkit for Multivariate Data Analysis\" PoS ACAT:040,2007. e-Print: physics/0703039" << Endl;
1455  break;
1456 
1457  case kBibTeX:
1458  logger << "@Article{TMVA2007," << Endl;
1459  logger << " author = \"Hoecker, Andreas and Speckmayer, Peter and Stelzer, Joerg " << Endl;
1460  logger << " and Therhaag, Jan and von Toerne, Eckhard and Voss, Helge\"," << Endl;
1461  logger << " title = \"{TMVA: Toolkit for multivariate data analysis}\"," << Endl;
1462  logger << " journal = \"PoS\"," << Endl;
1463  logger << " volume = \"ACAT\"," << Endl;
1464  logger << " year = \"2007\"," << Endl;
1465  logger << " pages = \"040\"," << Endl;
1466  logger << " eprint = \"physics/0703039\"," << Endl;
1467  logger << " archivePrefix = \"arXiv\"," << Endl;
1468  logger << " SLACcitation = \"%%CITATION = PHYSICS/0703039;%%\"" << Endl;
1469  logger << "}" << Endl;
1470  break;
1471 
1472  case kLaTeX:
1473  logger << "%\\cite{TMVA2007}" << Endl;
1474  logger << "\\bibitem{TMVA2007}" << Endl;
1475  logger << " A.~Hoecker, P.~Speckmayer, J.~Stelzer, J.~Therhaag, E.~von Toerne, H.~Voss" << Endl;
1476  logger << " %``TMVA: Toolkit for multivariate data analysis,''" << Endl;
1477  logger << " PoS A {\\bf CAT} (2007) 040" << Endl;
1478  logger << " [arXiv:physics/0703039]." << Endl;
1479  logger << " %%CITATION = POSCI,ACAT,040;%%" << Endl;
1480  break;
1481 
1482  case kHtmlLink:
1483  logger << kINFO << " " << Endl;
1484  logger << kINFO << gTools().Color("bold")
1485  << "Thank you for using TMVA!" << gTools().Color("reset") << Endl;
1486  logger << kINFO << gTools().Color("bold")
1487  << "For citation information, please visit: http://tmva.sf.net/citeTMVA.html"
1488  << gTools().Color("reset") << Endl;
1489  }
1490 }
1491 
1492 ////////////////////////////////////////////////////////////////////////////////
1493 
1495 {
1496  return !(h.GetXaxis()->GetXbins()->fN);
1497 }
1498 
1499 ////////////////////////////////////////////////////////////////////////////////
1500 
1501 std::vector<TMatrixDSym*>*
1502 TMVA::Tools::CalcCovarianceMatrices( const std::vector<const Event*>& events, Int_t maxCls, VariableTransformBase* transformBase )
1503 {
1504  std::vector<Event*> eventVector;
1505  for (std::vector<const Event*>::const_iterator it = events.begin(), itEnd = events.end(); it != itEnd; ++it)
1506  {
1507  eventVector.push_back (new Event(*(*it)));
1508  }
1509  std::vector<TMatrixDSym*>* returnValue = CalcCovarianceMatrices (eventVector, maxCls, transformBase);
1510  for (std::vector<Event*>::const_iterator it = eventVector.begin(), itEnd = eventVector.end(); it != itEnd; ++it)
1511  {
1512  delete (*it);
1513  }
1514  return returnValue;
1515 }
1516 
1517 ////////////////////////////////////////////////////////////////////////////////
1518 /// compute covariance matrices
1519 
1520 std::vector<TMatrixDSym*>*
1521 TMVA::Tools::CalcCovarianceMatrices( const std::vector<Event*>& events, Int_t maxCls, VariableTransformBase* transformBase )
1522 {
1523  if (events.empty()) {
1524  Log() << kWARNING << " Asked to calculate a covariance matrix for an empty event vectors.. sorry cannot do that -> return NULL"<<Endl;
1525  return 0;
1526  }
1527 
1528  UInt_t nvars=0, ntgts=0, nspcts=0;
1529  if (transformBase)
1530  transformBase->CountVariableTypes( nvars, ntgts, nspcts );
1531  else {
1532  nvars =events.at(0)->GetNVariables ();
1533  ntgts =events.at(0)->GetNTargets ();
1534  nspcts=events.at(0)->GetNSpectators();
1535  }
1536 
1537 
1538  // init matrices
1539  Int_t matNum = maxCls;
1540  if (maxCls > 1 ) matNum++; // if more than one classes, then produce one matrix for all events as well (beside the matrices for each class)
1541 
1542  std::vector<TVectorD*>* vec = new std::vector<TVectorD*>(matNum);
1543  std::vector<TMatrixD*>* mat2 = new std::vector<TMatrixD*>(matNum);
1544  std::vector<Double_t> count(matNum);
1545  count.assign(matNum,0);
1546 
1547  Int_t cls = 0;
1548  TVectorD* v;
1549  TMatrixD* m;
1550  UInt_t ivar=0, jvar=0;
1551  for (cls = 0; cls < matNum ; cls++) {
1552  vec->at(cls) = new TVectorD(nvars);
1553  mat2->at(cls) = new TMatrixD(nvars,nvars);
1554  v = vec->at(cls);
1555  m = mat2->at(cls);
1556 
1557  for (ivar=0; ivar<nvars; ivar++) {
1558  (*v)(ivar) = 0;
1559  for (jvar=0; jvar<nvars; jvar++) {
1560  (*m)(ivar, jvar) = 0;
1561  }
1562  }
1563  }
1564 
1565  // perform event loop
1566  for (UInt_t i=0; i<events.size(); i++) {
1567 
1568  // fill the event
1569  const Event * ev = events[i];
1570  cls = ev->GetClass();
1571  Double_t weight = ev->GetWeight();
1572 
1573  std::vector<Float_t> input;
1574  std::vector<Char_t> mask; // entries with kTRUE must not be transformed
1575  // Bool_t hasMaskedEntries = kFALSE;
1576  if (transformBase) {
1577  /* hasMaskedEntries = */ transformBase->GetInput (ev, input, mask);
1578  } else {
1579  for (ivar=0; ivar<nvars; ++ivar) {
1580  input.push_back (ev->GetValue(ivar));
1581  }
1582  }
1583 
1584  if (maxCls > 1) {
1585  v = vec->at(matNum-1);
1586  m = mat2->at(matNum-1);
1587 
1588  count.at(matNum-1)+=weight; // count used events
1589  for (ivar=0; ivar<nvars; ivar++) {
1590 
1591  Double_t xi = input.at (ivar);
1592  (*v)(ivar) += xi*weight;
1593  (*m)(ivar, ivar) += (xi*xi*weight);
1594 
1595  for (jvar=ivar+1; jvar<nvars; jvar++) {
1596  Double_t xj = input.at (jvar);
1597  (*m)(ivar, jvar) += (xi*xj*weight);
1598  (*m)(jvar, ivar) = (*m)(ivar, jvar); // symmetric matrix
1599  }
1600  }
1601  }
1602 
1603  count.at(cls)+=weight; // count used events
1604  v = vec->at(cls);
1605  m = mat2->at(cls);
1606  for (ivar=0; ivar<nvars; ivar++) {
1607  Double_t xi = input.at (ivar);
1608  (*v)(ivar) += xi*weight;
1609  (*m)(ivar, ivar) += (xi*xi*weight);
1610 
1611  for (jvar=ivar+1; jvar<nvars; jvar++) {
1612  Double_t xj = input.at (jvar);
1613  (*m)(ivar, jvar) += (xi*xj*weight);
1614  (*m)(jvar, ivar) = (*m)(ivar, jvar); // symmetric matrix
1615  }
1616  }
1617  }
1618 
1619  // variance-covariance
1620  std::vector<TMatrixDSym*>* mat = new std::vector<TMatrixDSym*>(matNum);
1621  for (cls = 0; cls < matNum; cls++) {
1622  v = vec->at(cls);
1623  m = mat2->at(cls);
1624 
1625  mat->at(cls) = new TMatrixDSym(nvars);
1626 
1627  Double_t n = count.at(cls);
1628  for (ivar=0; ivar<nvars; ivar++) {
1629  for (jvar=0; jvar<nvars; jvar++) {
1630  (*(mat->at(cls)))(ivar, jvar) = (*m)(ivar, jvar)/n - (*v)(ivar)*(*v)(jvar)/(n*n);
1631  }
1632  }
1633  delete v;
1634  delete m;
1635  }
1636 
1637  delete mat2;
1638  delete vec;
1639 
1640  return mat;
1641 }
1642 
1643 template <typename Iterator, typename WeightIterator>
1644 Double_t TMVA::Tools::Mean ( Iterator first, Iterator last, WeightIterator w)
1645 {
1646  // Return the weighted mean of an array defined by the first and
1647  // last iterators. The w iterator should point to the first element
1648  // of a vector of weights of the same size as the main array.
1649 
1650  Double_t sum = 0;
1651  Double_t sumw = 0;
1652  int i = 0;
1653  if (w==NULL)
1654  {
1655  while ( first != last )
1656  {
1657  // if ( *w < 0) {
1658  // ::Error("TMVA::Tools::Mean","w[%d] = %.4e < 0 ?!",i,*w);
1659  // return 0;
1660  // } // SURE, why wouldn't you allow for negative event weights here ?? :)
1661  sum += (*first);
1662  sumw += 1.0 ;
1663  ++first;
1664  ++i;
1665  }
1666  if (sumw <= 0) {
1667  ::Error("TMVA::Tools::Mean","sum of weights <= 0 ?! that's a bit too much of negative event weights :) ");
1668  return 0;
1669  }
1670  }
1671  else
1672  {
1673  while ( first != last )
1674  {
1675  // if ( *w < 0) {
1676  // ::Error("TMVA::Tools::Mean","w[%d] = %.4e < 0 ?!",i,*w);
1677  // return 0;
1678  // } // SURE, why wouldn't you allow for negative event weights here ?? :)
1679  sum += (*w) * (*first);
1680  sumw += (*w) ;
1681  ++w;
1682  ++first;
1683  ++i;
1684  }
1685  if (sumw <= 0) {
1686  ::Error("TMVA::Tools::Mean","sum of weights <= 0 ?! that's a bit too much of negative event weights :) ");
1687  return 0;
1688  }
1689  }
1690  return sum/sumw;
1691 }
1692 
1693 template <typename T>
1695 {
1696  // Return the weighted mean of an array a with length n.
1697 
1698  if (w) {
1699  return TMVA::Tools::Mean(a, a+n, w);
1700  } else {
1701  return TMath::Mean(a, a+n);
1702  }
1703 }
1704 
1705 template <typename Iterator, typename WeightIterator>
1706 Double_t TMVA::Tools::RMS(Iterator first, Iterator last, WeightIterator w)
1707 {
1708  // Return the Standard Deviation of an array defined by the iterators.
1709  // Note that this function returns the sigma(standard deviation) and
1710  // not the root mean square of the array.
1711 
1712  Double_t sum = 0;
1713  Double_t sum2 = 0;
1714  Double_t sumw = 0;
1715 
1716  Double_t adouble;
1717  if (w==NULL)
1718  {
1719  while ( first != last ) {
1720  adouble=Double_t(*first);
1721  sum += adouble;
1722  sum2 += adouble*adouble;
1723  sumw += 1.0;
1724  ++first;
1725  }
1726  }
1727  else
1728  {
1729  while ( first != last ) {
1730  adouble=Double_t(*first);
1731  sum += adouble * (*w);
1732  sum2 += adouble*adouble * (*w);
1733  sumw += (*w);
1734  ++first;
1735  ++w;
1736  }
1737  }
1738  Double_t norm = 1./sumw;
1739  Double_t mean = sum*norm;
1740  Double_t rms = TMath::Sqrt(TMath::Abs(sum2*norm -mean*mean));
1741  return rms;
1742 }
1743 
1744 template <typename T>
1746 {
1747  // Return the Standard Deviation of an array a with length n.
1748  // Note that this function returns the sigma(standard deviation) and
1749  // not the root mean square of the array.
1750 
1751  if (w) {
1752  return TMVA::Tools::RMS(a, a+n, w);
1753  } else {
1754  return TMath::RMS(a, a+n);
1755  }
1756 }
1757 
1758 
1760 {
1761  // get the cumulative distribution of a histogram
1762  TH1* cumulativeDist= (TH1*) h->Clone(Form("%sCumul",h->GetTitle()));
1763  //cumulativeDist->Smooth(5); // with this, I get less beautiful ROC curves, hence out!
1764 
1765  Float_t partialSum = 0;
1766  Float_t inverseSum = 0.;
1767 
1768  Float_t val;
1769  for (Int_t ibinEnd=1, ibin=cumulativeDist->GetNbinsX(); ibin >=ibinEnd ; ibin--){
1770  val = cumulativeDist->GetBinContent(ibin);
1771  if (val>0) inverseSum += val;
1772  }
1773  inverseSum = 1/inverseSum; // as I learned multiplications are much faster than division, and later I need one per bin. Well, not that it would really matter here I guess :)
1774 
1775  for (Int_t ibinEnd=1, ibin=cumulativeDist->GetNbinsX(); ibin >=ibinEnd ; ibin--){
1776  val = cumulativeDist->GetBinContent(ibin);
1777  if (val>0) partialSum += val;
1778  cumulativeDist->SetBinContent(ibin,partialSum*inverseSum);
1779  }
1780  return cumulativeDist;
1781 }
tuple row
Definition: mrt.py:26
virtual void CountVariableTypes(UInt_t &nvars, UInt_t &ntgts, UInt_t &nspcts) const
count variables, targets and spectators
Bool_t ContainsRegularExpression(const TString &s)
check if regular expression helper function to search for "$!%^&()'<>?= " in a string ...
Definition: Tools.cxx:793
virtual const char * GetTitle() const
Returns title of object.
Definition: TNamed.h:52
double par[1]
Definition: unuranDistr.cxx:38
void Scale(std::vector< Double_t > &, Double_t)
scales double vector
Definition: Tools.cxx:528
virtual void Scale(Double_t c1=1, Option_t *option="")
Multiply this histogram by a constant c1.
Definition: TH1.cxx:6174
static double B[]
static Tools & Instance()
Definition: Tools.cxx:80
TXMLEngine & xmlengine()
Definition: Tools.h:277
void UsefulSortDescending(std::vector< std::vector< Double_t > > &, std::vector< TString > *vs=0)
sort 2D vector (AND in parallel a TString vector) in such a way that the "first vector is sorted" and...
Definition: Tools.cxx:573
float xmin
Definition: THbookFile.cxx:93
Double_t RMS(Long64_t n, const T *a, const Double_t *w=0)
Definition: Tools.cxx:1745
static Vc_ALWAYS_INLINE int_v min(const int_v &x, const int_v &y)
Definition: vector.h:433
MsgLogger & Endl(MsgLogger &ml)
Definition: MsgLogger.h:162
TMatrixT< Element > & Transpose(const TMatrixT< Element > &source)
Transpose matrix source.
Definition: TMatrixT.cxx:1461
long long Long64_t
Definition: RtypesCore.h:69
virtual Double_t GetBinContent(Int_t bin) const
Return content of bin number bin.
Definition: TH1.cxx:4629
Double_t Log(Double_t x)
Definition: TMath.h:526
Ssiz_t Length() const
Definition: TString.h:390
Collectable string class.
Definition: TObjString.h:32
float Float_t
Definition: RtypesCore.h:53
return c
static const std::string comment("comment")
Double_t GetXmin() const
Definition: PDF.h:112
void ROOTVersionMessage(MsgLogger &logger)
prints the ROOT release number and date
Definition: Tools.cxx:1333
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition: TString.h:635
std::vector< TMatrixDSym * > * CalcCovarianceMatrices(const std::vector< Event * > &events, Int_t maxCls, VariableTransformBase *transformBase=0)
compute covariance matrices
Definition: Tools.cxx:1521
virtual void SetOwner(Bool_t enable=kTRUE)
Set whether this collection is the owner (enable==true) of its content.
Config & gConfig()
TH1 * h
Definition: legend2.C:5
Base class for spline implementation containing the Draw/Paint methods //.
Definition: TSpline.h:22
std::vector< double > values
Definition: TwoHistoFit2D.C:32
void ToUpper()
Change string to upper case.
Definition: TString.cxx:1088
#define gROOT
Definition: TROOT.h:344
Bool_t CheckForSilentOption(const TString &) const
check for "silence" option in configuration option string
Definition: Tools.cxx:700
Basic string class.
Definition: TString.h:137
Double_t GetMutualInformation(const TH2F &)
Mutual Information method for non-linear correlations estimates in 2D histogram Author: Moritz Backes...
Definition: Tools.cxx:598
void ToLower()
Change string to lower-case.
Definition: TString.cxx:1075
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
void WriteFloatArbitraryPrecision(Float_t val, std::ostream &os)
writes a float value with the available precision to a stream
Definition: Tools.cxx:1067
TArc * a
Definition: textangle.C:12
const Bool_t kFALSE
Definition: Rtypes.h:92
virtual Double_t GetBinWidth(Int_t bin) const
Return bin width.
Definition: TAxis.cxx:511
Double_t GetWeight() const
return the event weight - depending on whether the flag IgnoreNegWeightsInTraining is or not...
Definition: Event.cxx:376
Bool_t AddComment(XMLNodePointer_t parent, const char *comment)
Adds comment line to the node.
Definition: TXMLEngine.cxx:734
int nbins[3]
virtual Int_t GetNbinsX() const
Definition: TH1.h:296
static std::string format(double x, double y, int digits, int width)
Float_t py
Definition: hprod.C:33
virtual TMatrixTBase< Element > & ResizeTo(Int_t nrows, Int_t ncols, Int_t=-1)
Set size of the matrix to nrows x ncols New dynamic elements are created, the overlapping part of the...
Definition: TMatrixT.cxx:1202
void AddAttr(void *node, const char *, const T &value, Int_t precision=16)
Definition: Tools.h:308
Bool_t AddComment(void *node, const char *comment)
Definition: Tools.cxx:1142
void * AddChild(void *parent, const char *childname, const char *content=0, bool isRootNode=false)
add child node
Definition: Tools.cxx:1134
Short_t Abs(Short_t d)
Definition: TMathBase.h:110
virtual TObject * At(Int_t idx) const
Returns the object at position idx. Returns 0 if idx is out of range.
Definition: TList.cxx:311
TFile * f
TObject * Clone(const char *newname=0) const
Make a complete copy of the underlying object.
Definition: TH1.cxx:2565
void ReadTMatrixDFromXML(void *node, const char *name, TMatrixD *mat)
Definition: Tools.cxx:1284
TTree * T
const char * Data() const
Definition: TString.h:349
Double_t RMS(Long64_t n, const T *a, const Double_t *w=0)
Definition: TMath.h:916
void * GetParent(void *child)
get parent node
Definition: Tools.cxx:1149
TClass * GetClass(T *)
Definition: TClass.h:554
Tools & gTools()
Definition: Tools.cxx:79
Bool_t CheckForVerboseOption(const TString &) const
check if verbosity "V" set in option
Definition: Tools.cxx:717
std::vector< Double_t > MVADiff(std::vector< Double_t > &, std::vector< Double_t > &)
computes difference between two vectors
Definition: Tools.cxx:515
Double_t x[n]
Definition: legend1.C:17
~Tools()
destructor
Definition: Tools.cxx:118
static const std::string separator("@@@")
RooCmdArg Color(Color_t color)
int d
Definition: tornado.py:11
Bool_t CheckSplines(const TH1 *, const TSpline *)
Definition: Tools.cxx:487
void * GetChild(void *parent, const char *childname=0)
get child node
Definition: Tools.cxx:1158
TString StringFromInt(Long_t i)
string tools
Definition: Tools.cxx:1231
double pow(double, double)
std::vector< std::vector< double > > Data
Double_t NormHist(TH1 *theHist, Double_t norm=1.0)
normalises histogram
Definition: Tools.cxx:395
std::vector< Int_t > * ParseANNOptionString(TString theOptions, Int_t nvar, std::vector< Int_t > *nodes)
parse option string for ANN methods default settings (should be defined in theOption string) ...
Definition: Tools.cxx:440
TH1D * ProjectionY(const char *name="_py", Int_t firstxbin=0, Int_t lastxbin=-1, Option_t *option="") const
Project a 2-D histogram into a 1-D histogram along Y.
Definition: TH2.cxx:2563
Definition: PDF.h:71
#define TMVA_RELEASE
Definition: Version.h:44
Double_t GetXmin() const
Definition: TAxis.h:137
void Error(const char *location, const char *msgfmt,...)
TVectorT< Double_t > TVectorD
Definition: TVectorDfwd.h:24
Bool_t AddRawLine(void *node, const char *raw)
XML helpers.
Definition: Tools.cxx:1198
TMatrixT< Element > & Invert(Double_t *det=0)
Invert the matrix and calculate its determinant.
Definition: TMatrixT.cxx:1388
Int_t GetNrows() const
Definition: TMatrixTBase.h:134
virtual Double_t GetBinContent(Int_t bin) const
Return content of bin number bin.
Definition: TH2.h:90
A doubly linked list.
Definition: TList.h:47
static Tools * fgTools
Definition: Tools.h:247
Double_t Mean(Long64_t n, const T *a, const Double_t *w=0)
Definition: Tools.cxx:1694
TMatrixT< Double_t > TMatrixD
Definition: TMatrixDfwd.h:24
static void DestroyInstance()
Definition: Tools.cxx:95
Int_t fN
Definition: TArray.h:40
virtual Double_t GetBinCenter(Int_t bin) const
return bin center for 1D historam Better to use h1.GetXaxis().GetBinCenter(bin)
Definition: TH1.cxx:8470
EWelcomeMessage
Definition: Tools.h:213
TString StringFromDouble(Double_t d)
string tools
Definition: Tools.cxx:1241
ROOT::R::TRInterface & r
Definition: Object.C:4
Service class for 2-Dim histogram classes.
Definition: TH2.h:36
TString ReplaceRegularExpressions(const TString &s, const TString &replace="+")
replace regular expressions helper function to remove all occurences "$!%^&()'<>?= " from a string and replace all ::,$,*,/,+,- with M,S,T,D,P,M respectively
Definition: Tools.cxx:807
SVector< double, 2 > v
Definition: Dict.h:5
void TMVAWelcomeMessage()
direct output, eg, when starting ROOT session -> no use of Logger here
Definition: Tools.cxx:1310
TPaveLabel title(3, 27.1, 15, 28.7,"ROOT Environment and Tools")
Int_t GetIndexMaxElement(std::vector< Double_t > &)
find index of maximum entry in vector
Definition: Tools.cxx:757
virtual void SetBinContent(Int_t bin, Double_t content)
Set bin content see convention for numbering bins in TH1::GetBin In case the bin number is greater th...
Definition: TH1.cxx:8543
Bool_t HistoHasEquidistantBins(const TH1 &h)
Definition: Tools.cxx:1494
Double_t Mean(Long64_t n, const T *a, const Double_t *w=0)
Definition: TMath.h:824
unsigned int UInt_t
Definition: RtypesCore.h:42
TMarker * m
Definition: textangle.C:8
char * Form(const char *fmt,...)
Double_t GetSeparation(TH1 *S, TH1 *B) const
compute "separation" defined as <s2> = (1/2) Int_-oo..+oo { (S(x) - B(x))^2/(S(x) + B(x)) dx } ...
Definition: Tools.cxx:136
TString GetYTitleWithUnit(const TH1 &h, const TString &unit, Bool_t normalised)
histogramming utility
Definition: Tools.cxx:1057
bool first
Definition: line3Dfit.C:48
virtual Double_t GetSumOfWeights() const
Return the sum of weights excluding under/overflows.
Definition: TH1.cxx:7354
Double_t E()
Definition: TMath.h:54
tuple w
Definition: qtexample.py:51
Tools()
constructor
Definition: Tools.cxx:108
Bool_t HasAttr(void *node, const char *attrname)
add attribute from xml
Definition: Tools.cxx:1104
virtual Int_t GetSumw2N() const
Definition: TH1.h:314
const char * GetContent(void *node)
XML helpers.
Definition: Tools.cxx:1182
TSubString Strip(EStripType s=kTrailing, char c= ' ') const
Return a substring of self stripped at beginning and/or end.
Definition: TString.cxx:1056
virtual Double_t Eval(Double_t x) const =0
void ReadAttr(void *node, const char *, T &value)
Definition: Tools.h:295
float xmax
Definition: THbookFile.cxx:93
TH1 * GetCumulativeDist(TH1 *h)
Definition: Tools.cxx:1759
REAL epsilon
Definition: triangle.c:617
virtual Double_t Integral(Option_t *option="") const
Return integral of bin contents.
Definition: TH2.cxx:1182
TString GetXTitleWithUnit(const TString &title, const TString &unit)
histogramming utility
Definition: Tools.cxx:1049
TString & Remove(Ssiz_t pos)
Definition: TString.h:616
long Long_t
Definition: RtypesCore.h:50
int Ssiz_t
Definition: RtypesCore.h:63
const TString fRegexp
Definition: Tools.h:241
XMLAttrPointer_t NewAttr(XMLNodePointer_t xmlnode, XMLNsPointer_t, const char *name, const char *value)
creates new attribute for xmlnode, namespaces are not supported for attributes
Definition: TXMLEngine.cxx:488
virtual Int_t GetSize() const
Definition: TCollection.h:95
TH1 * projNormTH1F(TTree *theTree, const TString &theVarName, const TString &name, Int_t nbins, Double_t xmin, Double_t xmax, const TString &cut)
projects variable from tree into normalised histogram
Definition: Tools.cxx:378
Double_t GetVal(Double_t x) const
returns value PDF(x)
Definition: PDF.cxx:698
double Double_t
Definition: RtypesCore.h:55
const TMatrixD & GetEigenVectors() const
const TMatrixD * GetCorrelationMatrix(const TMatrixD *covMat)
turns covariance into correlation matrix
Definition: Tools.cxx:337
static const float S
Definition: mandel.cpp:113
TMatrixTSym< Double_t > TMatrixDSym
Int_t GetNcols() const
Definition: TMatrixTBase.h:137
void WriteTVectorDToXML(void *node, const char *name, TVectorD *vec)
Definition: Tools.cxx:1267
Double_t GetXmax() const
Definition: TAxis.h:138
void * GetNextChild(void *prevchild, const char *childname=0)
XML helpers.
Definition: Tools.cxx:1170
Float_t GetValue(UInt_t ivar) const
return value of i'th variable
Definition: Event.cxx:231
Double_t y[n]
Definition: legend1.C:17
#define TMVA_RELEASE_DATE
Definition: Version.h:45
The TH1 histogram class.
Definition: TH1.h:80
void UsefulSortAscending(std::vector< std::vector< Double_t > > &, std::vector< TString > *vs=0)
sort 2D vector (AND in parallel a TString vector) in such a way that the "first vector is sorted" and...
Definition: Tools.cxx:547
void ReadFloatArbitraryPrecision(Float_t &val, std::istream &is)
reads a float value with the available precision from a stream
Definition: Tools.cxx:1082
UInt_t GetClass() const
Definition: Event.h:86
void TMVAVersionMessage(MsgLogger &logger)
prints the TMVA release number and date
Definition: Tools.cxx:1324
virtual Bool_t GetInput(const Event *event, std::vector< Float_t > &input, std::vector< Char_t > &mask, Bool_t backTransform=kFALSE) const
select the values from the event
static Vc_ALWAYS_INLINE int_v max(const int_v &x, const int_v &y)
Definition: vector.h:440
#define name(a, b)
Definition: linkTestLib0.cpp:5
void FormattedOutput(const std::vector< Double_t > &, const std::vector< TString > &, const TString titleVars, const TString titleValues, MsgLogger &logger, TString format="%+1.3f")
formatted output of simple table
Definition: Tools.cxx:896
const TString & Color(const TString &)
human readable color strings
Definition: Tools.cxx:837
void WriteTMatrixDToXML(void *node, const char *name, TMatrixD *mat)
XML helpers.
Definition: Tools.cxx:1251
virtual Int_t GetNbinsY() const
Definition: TH1.h:297
Int_t GetNoElements() const
Definition: TVectorT.h:82
Double_t GetYMean_binX(const TH2 &, Int_t bin_x)
Compute the mean in Y for a given bin X of a 2D histogram.
Definition: Tools.cxx:651
virtual void Add(TObject *obj)
Definition: TList.h:81
Short_t Max(Short_t a, Short_t b)
Definition: TMathBase.h:202
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition: TString.h:567
XMLNodePointer_t NewChild(XMLNodePointer_t parent, XMLNsPointer_t ns, const char *name, const char *content=0)
create new child element for parent node
Definition: TXMLEngine.cxx:614
virtual void Sumw2(Bool_t flag=kTRUE)
Create structure to store sum of squares of weights.
Definition: TH1.cxx:8350
TList * ParseFormatLine(TString theString, const char *sep=":")
Parse the string and cut into labels separated by ":".
Definition: Tools.cxx:413
#define NULL
Definition: Rtypes.h:82
ClassImp(TSlaveInfo) Int_t TSlaveInfo const TSlaveInfo * si
Used to sort slaveinfos by ordinal.
Definition: TProof.cxx:183
const char * GetName(void *node)
XML helpers.
Definition: Tools.cxx:1190
TString()
TString default ctor.
Definition: TString.cxx:87
A TTree object has a header with a name and a title.
Definition: TTree.h:98
float type_of_call hi(const int &, const int &)
double result[121]
const TArrayD * GetXbins() const
Definition: TAxis.h:134
Double_t NormVariable(Double_t x, Double_t xmin, Double_t xmax)
normalise to output range: [-1, 1]
Definition: Tools.cxx:127
void TMVACitation(MsgLogger &logger, ECitation citType=kPlainText)
kinds of TMVA citation
Definition: Tools.cxx:1448
TMatrixD * GetSQRootMatrix(TMatrixDSym *symMat)
square-root of symmetric matrix of course the resulting sqrtMat is also symmetric, but it's easier to treat it as a general matrix
Definition: Tools.cxx:284
void Mult(const TMatrixT< Element > &a, const TMatrixT< Element > &b)
General matrix multiplication. Create a matrix C such that C = A * B.
Definition: TMatrixT.cxx:640
Double_t Sqrt(Double_t x)
Definition: TMath.h:464
void ReadTVectorDFromXML(void *node, const char *name, TVectorD *vec)
Definition: Tools.cxx:1275
const Bool_t kTRUE
Definition: Rtypes.h:91
TH2F * TransposeHist(const TH2F &)
Transpose quadratic histogram.
Definition: Tools.cxx:666
float value
Definition: math.cpp:443
double norm(double *x, double *p)
Definition: unuranDistr.cxx:40
void ComputeStat(const std::vector< TMVA::Event * > &, std::vector< Float_t > *, Double_t &, Double_t &, Double_t &, Double_t &, Double_t &, Double_t &, Int_t signalClass, Bool_t norm=kFALSE)
sanity check
Definition: Tools.cxx:215
const Int_t n
Definition: legend1.C:16
virtual Long64_t Project(const char *hname, const char *varexp, const char *selection="", Option_t *option="", Long64_t nentries=kMaxEntries, Long64_t firstentry=0)
Make a projection of a tree using selections.
Definition: TTree.cxx:6735
Double_t GetXmax() const
Definition: PDF.h:113
std::vector< TString > SplitString(const TString &theOpt, const char separator) const
splits the option string at 'separator' and fills the list 'splitV' with the primitive strings ...
Definition: Tools.cxx:1207
Int_t GetIndexMinElement(std::vector< Double_t > &)
find index of minimum entry in vector
Definition: Tools.cxx:774
Double_t GetCorrelationRatio(const TH2F &)
Compute Correlation Ratio of 2D histogram to estimate functional dependency between two variables Aut...
Definition: Tools.cxx:629
Definition: math.cpp:60
Ssiz_t First(char c) const
Find first occurrence of a character c.
Definition: TString.cxx:453
TAxis * GetXaxis()
Definition: TH1.h:319