Logo ROOT   6.08/07
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://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 
340  if (covMat == 0) return 0;
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  Log() << kWARNING;
347  TMatrixD* corrMat = new TMatrixD( nvar, nvar );
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() << "<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  return corrMat;
372  Log() << Endl;
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 
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 << kHEADER ;
1345  logger << "You are running ROOT Version: " << gROOT->GetVersion() << ", " << versionDate << Endl;
1346 }
1347 
1348 ////////////////////////////////////////////////////////////////////////////////
1349 /// various kinds of welcome messages
1350 /// ASCII text generated by this site: http://www.network-science.de/ascii/
1351 
1353 {
1354  switch (msgType) {
1355 
1356  case kStandardWelcomeMsg:
1357  logger << Color("white") << "TMVA -- Toolkit for Multivariate Analysis" << Color("reset") << Endl;
1358  logger << "Copyright (C) 2005-2006 CERN, LAPP & MPI-K Heidelberg and Victoria U." << Endl;
1359  logger << "Home page http://tmva.sourceforge.net" << Endl;
1360  logger << "All rights reserved, please read http://tmva.sf.net/license.txt" << Endl << Endl;
1361  break;
1362 
1363  case kIsometricWelcomeMsg:
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;
1374  logger << " \\/__/ \\/__/ " << Endl << Endl;
1375  break;
1376 
1377  case kBlockWelcomeMsg:
1378  logger << Endl;
1379  logger << "_|_|_|_|_| _| _| _| _| _|_| " << Endl;
1380  logger << " _| _|_| _|_| _| _| _| _| " << Endl;
1381  logger << " _| _| _| _| _| _| _|_|_|_| " << Endl;
1382  logger << " _| _| _| _| _| _| _| " << Endl;
1383  logger << " _| _| _| _| _| _| " << Endl << Endl;
1384  break;
1385 
1386  case kLeanWelcomeMsg:
1387  logger << Endl;
1388  logger << "_/_/_/_/_/ _/ _/ _/ _/ _/_/ " << Endl;
1389  logger << " _/ _/_/ _/_/ _/ _/ _/ _/ " << Endl;
1390  logger << " _/ _/ _/ _/ _/ _/ _/_/_/_/ " << Endl;
1391  logger << " _/ _/ _/ _/ _/ _/ _/ " << Endl;
1392  logger << "_/ _/ _/ _/ _/ _/ " << Endl << Endl;
1393  break;
1394 
1395  case kLogoWelcomeMsg:
1396  logger << Endl;
1397  logger << "_/_/_/_/_/ _| _| _| _| _|_| " << Endl;
1398  logger << " _/ _|_| _|_| _| _| _| _| " << Endl;
1399  logger << " _/ _| _| _| _| _| _|_|_|_| " << Endl;
1400  logger << " _/ _| _| _| _| _| _| " << Endl;
1401  logger << "_/ _| _| _| _| _| " << Endl << Endl;
1402  break;
1403 
1404  case kSmall1WelcomeMsg:
1405  logger << " _____ __ ____ ___ " << Endl;
1406  logger << "|_ _| \\/ \\ \\ / /_\\ " << Endl;
1407  logger << " | | | |\\/| |\\ V / _ \\ " << Endl;
1408  logger << " |_| |_| |_| \\_/_/ \\_\\" << Endl << Endl;
1409  break;
1410 
1411  case kSmall2WelcomeMsg:
1412  logger << " _____ __ ____ ___ " << Endl;
1413  logger << "|_ _| \\/ \\ \\ / / \\ " << Endl;
1414  logger << " | | | |\\/| |\\ \\ / / _ \\ " << Endl;
1415  logger << " | | | | | | \\ V / ___ \\ " << Endl;
1416  logger << " |_| |_| |_| \\_/_/ \\_\\ " << Endl << Endl;
1417  break;
1418 
1420  logger << kINFO << "" << Color("red")
1421  << "_______________________________________" << Color("reset") << Endl;
1422  logger << kINFO << "" << Color("blue")
1423  << Color("red_bgd") << Color("bwhite") << " // " << Color("reset")
1424  << Color("white") << Color("blue_bgd")
1425  << "|\\ /|| \\ // /\\\\\\\\\\\\\\\\\\\\\\\\ \\ \\ \\ " << Color("reset") << Endl;
1426  logger << kINFO << ""<< Color("blue")
1427  << Color("red_bgd") << Color("white") << "// " << Color("reset")
1428  << Color("white") << Color("blue_bgd")
1429  << "| \\/ || \\// /--\\\\\\\\\\\\\\\\\\\\\\\\ \\ \\ \\" << Color("reset") << Endl;
1430  break;
1431 
1432  case kOriginalWelcomeMsgBW:
1433  logger << kINFO << ""
1434  << "_______________________________________" << Endl;
1435  logger << kINFO << " // "
1436  << "|\\ /|| \\ // /\\\\\\\\\\\\\\\\\\\\\\\\ \\ \\ \\ " << Endl;
1437  logger << kINFO << "// "
1438  << "| \\/ || \\// /--\\\\\\\\\\\\\\\\\\\\\\\\ \\ \\ \\" << Endl;
1439  break;
1440 
1441  default:
1442  logger << kFATAL << "unknown message type: " << msgType << Endl;
1443  }
1444 }
1445 
1446 ////////////////////////////////////////////////////////////////////////////////
1447 /// kinds of TMVA citation
1448 
1450 {
1451  switch (citType) {
1452 
1453  case kPlainText:
1454  logger << "A. Hoecker, P. Speckmayer, J. Stelzer, J. Therhaag, E. von Toerne, H. Voss" << Endl;
1455  logger << "\"TMVA - Toolkit for Multivariate Data Analysis\" PoS ACAT:040,2007. e-Print: physics/0703039" << Endl;
1456  break;
1457 
1458  case kBibTeX:
1459  logger << "@Article{TMVA2007," << Endl;
1460  logger << " author = \"Hoecker, Andreas and Speckmayer, Peter and Stelzer, Joerg " << Endl;
1461  logger << " and Therhaag, Jan and von Toerne, Eckhard and Voss, Helge\"," << Endl;
1462  logger << " title = \"{TMVA: Toolkit for multivariate data analysis}\"," << Endl;
1463  logger << " journal = \"PoS\"," << Endl;
1464  logger << " volume = \"ACAT\"," << Endl;
1465  logger << " year = \"2007\"," << Endl;
1466  logger << " pages = \"040\"," << Endl;
1467  logger << " eprint = \"physics/0703039\"," << Endl;
1468  logger << " archivePrefix = \"arXiv\"," << Endl;
1469  logger << " SLACcitation = \"%%CITATION = PHYSICS/0703039;%%\"" << Endl;
1470  logger << "}" << Endl;
1471  break;
1472 
1473  case kLaTeX:
1474  logger << "%\\cite{TMVA2007}" << Endl;
1475  logger << "\\bibitem{TMVA2007}" << Endl;
1476  logger << " A.~Hoecker, P.~Speckmayer, J.~Stelzer, J.~Therhaag, E.~von Toerne, H.~Voss" << Endl;
1477  logger << " %``TMVA: Toolkit for multivariate data analysis,''" << Endl;
1478  logger << " PoS A {\\bf CAT} (2007) 040" << Endl;
1479  logger << " [arXiv:physics/0703039]." << Endl;
1480  logger << " %%CITATION = POSCI,ACAT,040;%%" << Endl;
1481  break;
1482 
1483  case kHtmlLink:
1484  // logger << kINFO << " " << Endl;
1485  logger << kHEADER << gTools().Color("bold")
1486  << "Thank you for using TMVA!" << gTools().Color("reset") << Endl;
1487  logger << kINFO << gTools().Color("bold")
1488  << "For citation information, please visit: http://tmva.sf.net/citeTMVA.html"
1489  << gTools().Color("reset") << Endl;
1490  }
1491 }
1492 
1493 ////////////////////////////////////////////////////////////////////////////////
1494 
1496 {
1497  return !(h.GetXaxis()->GetXbins()->fN);
1498 }
1499 
1500 ////////////////////////////////////////////////////////////////////////////////
1501 
1502 std::vector<TMatrixDSym*>*
1503 TMVA::Tools::CalcCovarianceMatrices( const std::vector<const Event*>& events, Int_t maxCls, VariableTransformBase* transformBase )
1504 {
1505  std::vector<Event*> eventVector;
1506  for (std::vector<const Event*>::const_iterator it = events.begin(), itEnd = events.end(); it != itEnd; ++it)
1507  {
1508  eventVector.push_back (new Event(*(*it)));
1509  }
1510  std::vector<TMatrixDSym*>* returnValue = CalcCovarianceMatrices (eventVector, maxCls, transformBase);
1511  for (std::vector<Event*>::const_iterator it = eventVector.begin(), itEnd = eventVector.end(); it != itEnd; ++it)
1512  {
1513  delete (*it);
1514  }
1515  return returnValue;
1516 }
1517 
1518 ////////////////////////////////////////////////////////////////////////////////
1519 /// compute covariance matrices
1520 
1521 std::vector<TMatrixDSym*>*
1522 TMVA::Tools::CalcCovarianceMatrices( const std::vector<Event*>& events, Int_t maxCls, VariableTransformBase* transformBase )
1523 {
1524  if (events.empty()) {
1525  Log() << kWARNING << " Asked to calculate a covariance matrix for an empty event vectors.. sorry cannot do that -> return NULL"<<Endl;
1526  return 0;
1527  }
1528 
1529  UInt_t nvars=0, ntgts=0, nspcts=0;
1530  if (transformBase)
1531  transformBase->CountVariableTypes( nvars, ntgts, nspcts );
1532  else {
1533  nvars =events.at(0)->GetNVariables ();
1534  ntgts =events.at(0)->GetNTargets ();
1535  nspcts=events.at(0)->GetNSpectators();
1536  }
1537 
1538 
1539  // init matrices
1540  Int_t matNum = maxCls;
1541  if (maxCls > 1 ) matNum++; // if more than one classes, then produce one matrix for all events as well (beside the matrices for each class)
1542 
1543  std::vector<TVectorD*>* vec = new std::vector<TVectorD*>(matNum);
1544  std::vector<TMatrixD*>* mat2 = new std::vector<TMatrixD*>(matNum);
1545  std::vector<Double_t> count(matNum);
1546  count.assign(matNum,0);
1547 
1548  Int_t cls = 0;
1549  TVectorD* v;
1550  TMatrixD* m;
1551  UInt_t ivar=0, jvar=0;
1552  for (cls = 0; cls < matNum ; cls++) {
1553  vec->at(cls) = new TVectorD(nvars);
1554  mat2->at(cls) = new TMatrixD(nvars,nvars);
1555  v = vec->at(cls);
1556  m = mat2->at(cls);
1557 
1558  for (ivar=0; ivar<nvars; ivar++) {
1559  (*v)(ivar) = 0;
1560  for (jvar=0; jvar<nvars; jvar++) {
1561  (*m)(ivar, jvar) = 0;
1562  }
1563  }
1564  }
1565 
1566  // perform event loop
1567  for (UInt_t i=0; i<events.size(); i++) {
1568 
1569  // fill the event
1570  const Event * ev = events[i];
1571  cls = ev->GetClass();
1572  Double_t weight = ev->GetWeight();
1573 
1574  std::vector<Float_t> input;
1575  std::vector<Char_t> mask; // entries with kTRUE must not be transformed
1576  // Bool_t hasMaskedEntries = kFALSE;
1577  if (transformBase) {
1578  /* hasMaskedEntries = */ transformBase->GetInput (ev, input, mask);
1579  } else {
1580  for (ivar=0; ivar<nvars; ++ivar) {
1581  input.push_back (ev->GetValue(ivar));
1582  }
1583  }
1584 
1585  if (maxCls > 1) {
1586  v = vec->at(matNum-1);
1587  m = mat2->at(matNum-1);
1588 
1589  count.at(matNum-1)+=weight; // count used events
1590  for (ivar=0; ivar<nvars; ivar++) {
1591 
1592  Double_t xi = input.at (ivar);
1593  (*v)(ivar) += xi*weight;
1594  (*m)(ivar, ivar) += (xi*xi*weight);
1595 
1596  for (jvar=ivar+1; jvar<nvars; jvar++) {
1597  Double_t xj = input.at (jvar);
1598  (*m)(ivar, jvar) += (xi*xj*weight);
1599  (*m)(jvar, ivar) = (*m)(ivar, jvar); // symmetric matrix
1600  }
1601  }
1602  }
1603 
1604  count.at(cls)+=weight; // count used events
1605  v = vec->at(cls);
1606  m = mat2->at(cls);
1607  for (ivar=0; ivar<nvars; ivar++) {
1608  Double_t xi = input.at (ivar);
1609  (*v)(ivar) += xi*weight;
1610  (*m)(ivar, ivar) += (xi*xi*weight);
1611 
1612  for (jvar=ivar+1; jvar<nvars; jvar++) {
1613  Double_t xj = input.at (jvar);
1614  (*m)(ivar, jvar) += (xi*xj*weight);
1615  (*m)(jvar, ivar) = (*m)(ivar, jvar); // symmetric matrix
1616  }
1617  }
1618  }
1619 
1620  // variance-covariance
1621  std::vector<TMatrixDSym*>* mat = new std::vector<TMatrixDSym*>(matNum);
1622  for (cls = 0; cls < matNum; cls++) {
1623  v = vec->at(cls);
1624  m = mat2->at(cls);
1625 
1626  mat->at(cls) = new TMatrixDSym(nvars);
1627 
1628  Double_t n = count.at(cls);
1629  for (ivar=0; ivar<nvars; ivar++) {
1630  for (jvar=0; jvar<nvars; jvar++) {
1631  (*(mat->at(cls)))(ivar, jvar) = (*m)(ivar, jvar)/n - (*v)(ivar)*(*v)(jvar)/(n*n);
1632  }
1633  }
1634  delete v;
1635  delete m;
1636  }
1637 
1638  delete mat2;
1639  delete vec;
1640 
1641  return mat;
1642 }
1643 
1644 template <typename Iterator, typename WeightIterator>
1645 Double_t TMVA::Tools::Mean ( Iterator first, Iterator last, WeightIterator w)
1646 {
1647  // Return the weighted mean of an array defined by the first and
1648  // last iterators. The w iterator should point to the first element
1649  // of a vector of weights of the same size as the main array.
1650 
1651  Double_t sum = 0;
1652  Double_t sumw = 0;
1653  int i = 0;
1654  if (w==NULL)
1655  {
1656  while ( first != last )
1657  {
1658  // if ( *w < 0) {
1659  // ::Error("TMVA::Tools::Mean","w[%d] = %.4e < 0 ?!",i,*w);
1660  // return 0;
1661  // } // SURE, why wouldn't you allow for negative event weights here ?? :)
1662  sum += (*first);
1663  sumw += 1.0 ;
1664  ++first;
1665  ++i;
1666  }
1667  if (sumw <= 0) {
1668  ::Error("TMVA::Tools::Mean","sum of weights <= 0 ?! that's a bit too much of negative event weights :) ");
1669  return 0;
1670  }
1671  }
1672  else
1673  {
1674  while ( first != last )
1675  {
1676  // if ( *w < 0) {
1677  // ::Error("TMVA::Tools::Mean","w[%d] = %.4e < 0 ?!",i,*w);
1678  // return 0;
1679  // } // SURE, why wouldn't you allow for negative event weights here ?? :)
1680  sum += (*w) * (*first);
1681  sumw += (*w) ;
1682  ++w;
1683  ++first;
1684  ++i;
1685  }
1686  if (sumw <= 0) {
1687  ::Error("TMVA::Tools::Mean","sum of weights <= 0 ?! that's a bit too much of negative event weights :) ");
1688  return 0;
1689  }
1690  }
1691  return sum/sumw;
1692 }
1693 
1694 template <typename T>
1696 {
1697  // Return the weighted mean of an array a with length n.
1698 
1699  if (w) {
1700  return TMVA::Tools::Mean(a, a+n, w);
1701  } else {
1702  return TMath::Mean(a, a+n);
1703  }
1704 }
1705 
1706 template <typename Iterator, typename WeightIterator>
1707 Double_t TMVA::Tools::RMS(Iterator first, Iterator last, WeightIterator w)
1708 {
1709  // Return the Standard Deviation of an array defined by the iterators.
1710  // Note that this function returns the sigma(standard deviation) and
1711  // not the root mean square of the array.
1712 
1713  Double_t sum = 0;
1714  Double_t sum2 = 0;
1715  Double_t sumw = 0;
1716 
1717  Double_t adouble;
1718  if (w==NULL)
1719  {
1720  while ( first != last ) {
1721  adouble=Double_t(*first);
1722  sum += adouble;
1723  sum2 += adouble*adouble;
1724  sumw += 1.0;
1725  ++first;
1726  }
1727  }
1728  else
1729  {
1730  while ( first != last ) {
1731  adouble=Double_t(*first);
1732  sum += adouble * (*w);
1733  sum2 += adouble*adouble * (*w);
1734  sumw += (*w);
1735  ++first;
1736  ++w;
1737  }
1738  }
1739  Double_t norm = 1./sumw;
1740  Double_t mean = sum*norm;
1741  Double_t rms = TMath::Sqrt(TMath::Abs(sum2*norm -mean*mean));
1742  return rms;
1743 }
1744 
1745 template <typename T>
1747 {
1748  // Return the Standard Deviation of an array a with length n.
1749  // Note that this function returns the sigma(standard deviation) and
1750  // not the root mean square of the array.
1751 
1752  if (w) {
1753  return TMVA::Tools::RMS(a, a+n, w);
1754  } else {
1755  return TMath::RMS(a, a+n);
1756  }
1757 }
1758 
1759 
1761 {
1762  // get the cumulative distribution of a histogram
1763  TH1* cumulativeDist= (TH1*) h->Clone(Form("%sCumul",h->GetTitle()));
1764  //cumulativeDist->Smooth(5); // with this, I get less beautiful ROC curves, hence out!
1765 
1766  Float_t partialSum = 0;
1767  Float_t inverseSum = 0.;
1768 
1769  Float_t val;
1770  for (Int_t ibinEnd=1, ibin=cumulativeDist->GetNbinsX(); ibin >=ibinEnd ; ibin--){
1771  val = cumulativeDist->GetBinContent(ibin);
1772  if (val>0) inverseSum += val;
1773  }
1774  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 :)
1775 
1776  for (Int_t ibinEnd=1, ibin=cumulativeDist->GetNbinsX(); ibin >=ibinEnd ; ibin--){
1777  val = cumulativeDist->GetBinContent(ibin);
1778  if (val>0) partialSum += val;
1779  cumulativeDist->SetBinContent(ibin,partialSum*inverseSum);
1780  }
1781  return cumulativeDist;
1782 }
Bool_t ContainsRegularExpression(const TString &s)
check if regular expression helper function to search for "$!%^&()&#39;<>?= " in a string ...
Definition: Tools.cxx:793
Config & gConfig()
Definition: Config.cxx:43
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:5936
static double B[]
static Tools & Instance()
Definition: Tools.cxx:80
static long int sum(long int i)
Definition: Factory.cxx:1786
TXMLEngine & xmlengine()
Definition: Tools.h:278
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:1746
virtual Double_t GetBinCenter(Int_t bin) const
Return bin center for 1D histogram.
Definition: TH1.cxx:8251
MsgLogger & Endl(MsgLogger &ml)
Definition: MsgLogger.h:162
TMatrixT< Element > & Transpose(const TMatrixT< Element > &source)
Transpose matrix source.
Definition: TMatrixT.cxx:1469
long long Long64_t
Definition: RtypesCore.h:69
Double_t Log(Double_t x)
Definition: TMath.h:526
virtual void PutStats(Double_t *stats)
Replace current statistics with the values in array stats.
Definition: TH2.cxx:2341
Collectable string class.
Definition: TObjString.h:32
float Float_t
Definition: RtypesCore.h:53
return c
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:1333
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:2332
Int_t GetNcols() const
Definition: TMatrixTBase.h:137
virtual TH2 * RebinY(Int_t ngroup=2, const char *newname="")
Rebin only the Y axis see Rebin2D.
Definition: TH2.cxx:1525
virtual void SetOwner(Bool_t enable=kTRUE)
Set whether this collection is the owner (enable==true) of its content.
THist< 1, float, THistStatContent, THistStatUncertainty > TH1F
Definition: THist.hxx:302
TH1 * h
Definition: legend2.C:5
Base class for spline implementation containing the Draw/Paint methods //.
Definition: TSpline.h:22
virtual Double_t GetSumOfWeights() const
Return the sum of weights excluding under/overflows.
Definition: TH1.cxx:7100
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:4638
#define gROOT
Definition: TROOT.h:364
virtual Double_t GetMean(Int_t axis=1) const
For axis = 1,2 or 3 returns the mean value of the histogram along X,Y or Z axis.
Definition: TH1.cxx:6760
XMLNodePointer_t GetNext(XMLNodePointer_t xmlnode, Bool_t realnode=kTRUE)
return next to xmlnode node if realnode==kTRUE, any special nodes in between will be skipped ...
Double_t GetMutualInformation(const TH2F &)
Mutual Information method for non-linear correlations estimates in 2D histogram Author: Moritz Backes...
Definition: Tools.cxx:598
virtual Double_t Integral(Option_t *option="") const
Return integral of bin contents.
Definition: TH2.cxx:1187
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
Double_t GetRMS(Int_t axis=1) const
Definition: TH1.h:320
const Bool_t kFALSE
Definition: Rtypes.h:92
Bool_t AddComment(XMLNodePointer_t parent, const char *comment)
Adds comment line to the node.
Definition: TXMLEngine.cxx:734
int nbins[3]
STL namespace.
static std::string format(double x, double y, int digits, int width)
Bool_t CheckForSilentOption(const TString &) const
check for "silence" option in configuration option string
Definition: Tools.cxx:700
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:1210
MsgLogger & Log() const
Definition: Tools.h:244
void AddAttr(void *node, const char *, const T &value, Int_t precision=16)
Definition: Tools.h:309
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
TMatrixDSymEigen.
const char * GetNodeContent(XMLNodePointer_t xmlnode)
get contents (if any) of xml node
Definition: TXMLEngine.cxx:938
void ReadTMatrixDFromXML(void *node, const char *name, TMatrixD *mat)
Definition: Tools.cxx:1284
Double_t RMS(Long64_t n, const T *a, const Double_t *w=0)
Definition: TMath.h:903
void * GetParent(void *child)
get parent node
Definition: Tools.cxx:1149
TClass * GetClass(T *)
Definition: TClass.h:555
Bool_t CheckForVerboseOption(const TString &) const
check if verbosity "V" set in option
Definition: Tools.cxx:717
Tools & gTools()
Definition: Tools.cxx:79
Double_t GetXmin() const
Definition: TAxis.h:139
std::vector< Double_t > MVADiff(std::vector< Double_t > &, std::vector< Double_t > &)
computes difference between two vectors
Definition: Tools.cxx:515
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:118
static const std::string separator("@@@")
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)
UInt_t GetClass() const
Definition: Event.h:89
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
Definition: PDF.h:71
#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:378
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:1396
const char * GetNodeName(XMLNodePointer_t xmlnode)
returns name of xmlnode
Definition: TXMLEngine.cxx:930
A doubly linked list.
Definition: TList.h:47
static Tools * fgTools
Definition: Tools.h:248
Double_t Mean(Long64_t n, const T *a, const Double_t *w=0)
Definition: Tools.cxx:1695
TMatrixT< Double_t > TMatrixD
Definition: TMatrixDfwd.h:24
static void DestroyInstance()
Definition: Tools.cxx:95
RooArgSet S(const RooAbsArg &v1)
Int_t fN
Definition: TArray.h:40
MsgLogger * fLogger
Definition: Tools.h:243
EWelcomeMessage
Definition: Tools.h:214
TString StringFromDouble(Double_t d)
string tools
Definition: Tools.cxx:1241
Bool_t AddRawLine(XMLNodePointer_t parent, const char *line)
Add just line into xml file Line should has correct xml syntax that later it can be decoded by xml pa...
Definition: TXMLEngine.cxx:769
TRandom2 r(17)
Service class for 2-Dim histogram classes.
Definition: TH2.h:36
virtual void GetStats(Double_t *stats) const
Fill the array stats from the contents of this histogram The array stats must be correctly dimensionn...
Definition: TH2.cxx:1128
TString ReplaceRegularExpressions(const TString &s, const TString &replace="+")
replace regular expressions helper function to remove all occurences "$!%^&()&#39;<>?= " from a string an...
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
TXMLEngine * fXMLEngine
Definition: Tools.h:280
Int_t GetIndexMaxElement(std::vector< Double_t > &)
find index of maximum entry in vector
Definition: Tools.cxx:757
tomato 2-D histogram with a float per channel (see TH1 documentation)}
Definition: TH2.h:255
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:8323
Bool_t HistoHasEquidistantBins(const TH1 &h)
Definition: Tools.cxx:1495
Double_t Mean(Long64_t n, const T *a, const Double_t *w=0)
Definition: TMath.h:811
unsigned int UInt_t
Definition: RtypesCore.h:42
TMarker * m
Definition: textangle.C:8
char * Form(const char *fmt,...)
Ssiz_t Length() const
Definition: TString.h:390
TString GetYTitleWithUnit(const TH1 &h, const TString &unit, Bool_t normalised)
histogramming utility
Definition: Tools.cxx:1057
Double_t E()
Definition: TMath.h:54
Tools()
constructor
Definition: Tools.cxx:108
Double_t GetXmin() const
Definition: PDF.h:112
Bool_t HasAttr(void *node, const char *attrname)
add attribute from xml
Definition: Tools.cxx:1104
Int_t GetNoElements() const
Definition: TVectorT.h:82
const char * GetContent(void *node)
XML helpers.
Definition: Tools.cxx:1182
virtual Double_t Eval(Double_t x) const =0
void ReadAttr(void *node, const char *, T &value)
Definition: Tools.h:296
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:311
TH1 * GetCumulativeDist(TH1 *h)
Definition: Tools.cxx:1760
const TMatrixD & GetEigenVectors() const
REAL epsilon
Definition: triangle.c:617
Int_t GetNrows() const
Definition: TMatrixTBase.h:134
TString GetXTitleWithUnit(const TString &title, const TString &unit)
histogramming utility
Definition: Tools.cxx:1049
Float_t GetValue(UInt_t ivar) const
return value of i&#39;th variable
Definition: Event.cxx:233
Bool_t HasAttr(XMLNodePointer_t xmlnode, const char *name)
checks if node has attribute of specified name
Definition: TXMLEngine.cxx:446
long Long_t
Definition: RtypesCore.h:50
int Ssiz_t
Definition: RtypesCore.h:63
virtual Int_t GetSumw2N() const
Definition: TH1.h:319
const TString fRegexp
Definition: Tools.h:242
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
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
virtual TH2 * RebinX(Int_t ngroup=2, const char *newname="")
Rebin only the X axis see Rebin2D.
Definition: TH2.cxx:1515
double f(double x)
double Double_t
Definition: RtypesCore.h:55
const char * GetAttr(XMLNodePointer_t xmlnode, const char *name)
returns value of attribute for xmlnode
Definition: TXMLEngine.cxx:460
Double_t GetXmax() const
Definition: PDF.h:113
const TMatrixD * GetCorrelationMatrix(const TMatrixD *covMat)
turns covariance into correlation matrix
Definition: Tools.cxx:337
TMatrixTSym< Double_t > TMatrixDSym
void WriteTVectorDToXML(void *node, const char *name, TVectorD *vec)
Definition: Tools.cxx:1267
void * GetNextChild(void *prevchild, const char *childname=0)
XML helpers.
Definition: Tools.cxx:1170
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
void TMVAVersionMessage(MsgLogger &logger)
prints the TMVA release number and date
Definition: Tools.cxx:1324
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
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 Double_t GetBinWidth(Int_t bin) const
Return bin width.
Definition: TAxis.cxx:526
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
XMLNodePointer_t GetChild(XMLNodePointer_t xmlnode, Bool_t realnode=kTRUE)
returns first child of xml node
Definition: TXMLEngine.cxx:993
XMLNodePointer_t GetParent(XMLNodePointer_t xmlnode)
returns parent of xmlnode
Short_t Max(Short_t a, Short_t b)
Definition: TMathBase.h:202
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:8130
TObject * Clone(const char *newname=0) const
Make a complete copy of the underlying object.
Definition: TH1.cxx:2544
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:413
#define NULL
Definition: Rtypes.h:82
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:1207
virtual Double_t GetBinContent(Int_t bin) const
Return content of bin number bin.
Definition: TH2.h:90
const char * GetName(void *node)
XML helpers.
Definition: Tools.cxx:1190
TString()
TString default ctor.
Definition: TString.cxx:88
float type_of_call hi(const int &, const int &)
double result[121]
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:1449
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:284
virtual void SetBinContent(Int_t bin, Double_t content)
Set bin content.
Definition: TH2.cxx:2475
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:648
virtual Int_t GetNbinsX() const
Definition: TH1.h:301
Double_t Sqrt(Double_t x)
Definition: TMath.h:464
std::vector< TMatrixDSym * > * CalcCovarianceMatrices(const std::vector< Event *> &events, Int_t maxCls, VariableTransformBase *transformBase=0)
compute covariance matrices
Definition: Tools.cxx:1522
void ReadTVectorDFromXML(void *node, const char *name, TVectorD *vec)
Definition: Tools.cxx:1275
virtual Int_t GetSize() const
Definition: TCollection.h:95
const Bool_t kTRUE
Definition: Rtypes.h:91
TH2F * TransposeHist(const TH2F &)
Transpose quadratic histogram.
Definition: Tools.cxx:666
THist< 2, float, THistStatContent, THistStatUncertainty > TH2F
Definition: THist.hxx:308
double norm(double *x, double *p)
Definition: unuranDistr.cxx:40
Double_t GetXmax() const
Definition: TAxis.h:140
const Int_t n
Definition: legend1.C:16
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
char name[80]
Definition: TGX11.cxx:109
const TArrayD * GetXbins() const
Definition: TAxis.h:136
TAxis * GetXaxis()
Definition: TH1.h:324
virtual const char * GetTitle() const
Returns title of object.
Definition: TNamed.h:52
virtual Int_t GetNbinsY() const
Definition: TH1.h:302
Double_t GetVal(Double_t x) const
returns value PDF(x)
Definition: PDF.cxx:699
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