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