68 #if __cplusplus > 199711L
69 std::atomic<TMVA::Tools*> TMVA::Tools::fgTools{0};
71 TMVA::Tools* TMVA::Tools::fgTools = 0;
74 TMVA::Tools& TMVA::gTools() {
return TMVA::Tools::Instance(); }
75 TMVA::Tools& TMVA::Tools::Instance() {
76 #if __cplusplus > 199711L
78 Tools* tmp =
new Tools();
80 if(! fgTools.compare_exchange_strong(expected,tmp)) {
87 return fgTools?*(fgTools): *(fgTools =
new Tools());
90 void TMVA::Tools::DestroyInstance() {
93 #if __cplusplus > 199711L
94 if (fgTools != 0) {
delete fgTools.load(); fgTools=0; }
96 if (fgTools != 0) {
delete fgTools; fgTools=0; }
103 TMVA::Tools::Tools() :
104 fRegexp(
"$&|!%^&()'<>?= "),
105 fLogger(new MsgLogger(
"Tools")),
106 fXMLEngine(new TXMLEngine())
113 TMVA::Tools::~Tools()
122 Double_t TMVA::Tools::NormVariable( Double_t x, Double_t xmin, Double_t xmax )
124 return 2*(x - xmin)/(xmax - xmin) - 1.0;
133 Double_t TMVA::Tools::GetSeparation( TH1* S, TH1* B )
const
135 Double_t separation = 0;
140 if ((S->GetNbinsX() != B->GetNbinsX()) || (S->GetNbinsX() <= 0)) {
141 Log() << kFATAL <<
"<GetSeparation> signal and background"
142 <<
" histograms have different number of bins: "
143 << S->GetNbinsX() <<
" : " << B->GetNbinsX() << Endl;
146 if (S->GetXaxis()->GetXmin() != B->GetXaxis()->GetXmin() ||
147 S->GetXaxis()->GetXmax() != B->GetXaxis()->GetXmax() ||
148 S->GetXaxis()->GetXmax() <= S->GetXaxis()->GetXmin()) {
149 Log() << kINFO << S->GetXaxis()->GetXmin() <<
" " << B->GetXaxis()->GetXmin()
150 <<
" " << S->GetXaxis()->GetXmax() <<
" " << B->GetXaxis()->GetXmax()
151 <<
" " << S->GetXaxis()->GetXmax() <<
" " << S->GetXaxis()->GetXmin() << Endl;
152 Log() << kFATAL <<
"<GetSeparation> signal and background"
153 <<
" histograms have different or invalid dimensions:" << Endl;
156 Int_t nstep = S->GetNbinsX();
157 Double_t intBin = (S->GetXaxis()->GetXmax() - S->GetXaxis()->GetXmin())/nstep;
158 Double_t nS = S->GetSumOfWeights()*intBin;
159 Double_t nB = B->GetSumOfWeights()*intBin;
161 if (nS > 0 && nB > 0) {
162 for (Int_t bin=0; bin<nstep; bin++) {
163 Double_t s = S->GetBinContent( bin+1 )/Double_t(nS);
164 Double_t b = B->GetBinContent( bin+1 )/Double_t(nB);
166 if (s + b > 0) separation += (s - b)*(s - b)/(s + b);
168 separation *= (0.5*intBin);
171 Log() << kWARNING <<
"<GetSeparation> histograms with zero entries: "
172 << nS <<
" : " << nB <<
" cannot compute separation"
186 Double_t TMVA::Tools::GetSeparation(
const PDF& pdfS,
const PDF& pdfB )
const
188 Double_t xmin = pdfS.GetXmin();
189 Double_t xmax = pdfS.GetXmax();
191 if (xmin != pdfB.GetXmin() || xmax != pdfB.GetXmax()) {
192 Log() << kFATAL <<
"<GetSeparation> Mismatch in PDF limits: "
193 << xmin <<
" " << pdfB.GetXmin() << xmax <<
" " << pdfB.GetXmax() << Endl;
196 Double_t separation = 0;
198 Double_t intBin = (xmax - xmin)/Double_t(nstep);
199 for (Int_t bin=0; bin<nstep; bin++) {
200 Double_t x = (bin + 0.5)*intBin + xmin;
201 Double_t s = pdfS.GetVal( x );
202 Double_t b = pdfB.GetVal( x );
204 if (s + b > 0) separation += (s - b)*(s - b)/(s + b);
206 separation *= (0.5*intBin);
214 void TMVA::Tools::ComputeStat(
const std::vector<TMVA::Event*>& events, std::vector<Float_t>* valVec,
215 Double_t& meanS, Double_t& meanB,
216 Double_t& rmsS, Double_t& rmsB,
217 Double_t& xmin, Double_t& xmax,
218 Int_t signalClass, Bool_t norm )
221 Log() << kFATAL <<
"<Tools::ComputeStat> value vector is zero pointer" << Endl;
223 if ( events.size() != valVec->size() )
224 Log() << kWARNING <<
"<Tools::ComputeStat> event and value vector have different lengths "
225 << events.size() <<
"!=" << valVec->size() << Endl;
227 Long64_t entries = valVec->size();
230 Double_t* varVecS =
new Double_t[entries];
231 Double_t* varVecB =
new Double_t[entries];
232 Double_t* wgtVecS =
new Double_t[entries];
233 Double_t* wgtVecB =
new Double_t[entries];
236 Long64_t nEventsS = 0;
237 Long64_t nEventsB = 0;
238 Double_t xmin_ = 0, xmax_ = 0;
241 xmin_ = *std::min( valVec->begin(), valVec->end() );
242 xmax_ = *std::max( valVec->begin(), valVec->end() );
245 for (Int_t ievt=0; ievt<entries; ievt++) {
246 Double_t theVar = (*valVec)[ievt];
247 if (norm) theVar = Tools::NormVariable( theVar, xmin_, xmax_ );
249 if (Int_t(events[ievt]->GetClass()) == signalClass ){
250 wgtVecS[nEventsS] = events[ievt]->GetWeight();
251 varVecS[nEventsS++] = theVar;
254 wgtVecB[nEventsB] = events[ievt]->GetWeight();
255 varVecB[nEventsB++] = theVar;
258 if (theVar > xmax) xmax = theVar;
259 if (theVar < xmin) xmin = theVar;
267 meanS = TMVA::Tools::Mean( nEventsS, varVecS, wgtVecS );
268 meanB = TMVA::Tools::Mean( nEventsB, varVecB, wgtVecB );
269 rmsS = TMVA::Tools::RMS ( nEventsS, varVecS, wgtVecS );
270 rmsB = TMVA::Tools::RMS ( nEventsB, varVecB, wgtVecB );
283 TMatrixD* TMVA::Tools::GetSQRootMatrix( TMatrixDSym* symMat )
285 Int_t n = symMat->GetNrows();
288 TMatrixDSymEigen* eigen =
new TMatrixDSymEigen( *symMat );
291 TMatrixD* si =
new TMatrixD( eigen->GetEigenVectors() );
292 TMatrixD* s =
new TMatrixD( *si );
293 si->Transpose( *si );
296 TMatrixD* d =
new TMatrixD( n, n);
297 d->Mult( (*si), (*symMat) ); (*d) *= (*s);
301 Double_t epsilon = 1.0e-8;
302 for (i=0; i<n; i++) {
303 for (j=0; j<n; j++) {
304 if ((i != j && TMath::Abs((*d)(i,j))/TMath::Sqrt((*d)(i,i)*(*d)(j,j)) > epsilon) ||
305 (i == j && (*d)(i,i) < 0)) {
307 Log() << kWARNING <<
"<GetSQRootMatrix> error in matrix diagonalization; printed S and B" << Endl;
313 for (i=0; i<n; i++)
for (j=0; j<n; j++)
if (j != i) (*d)(i,j) = 0;
316 for (i=0; i<n; i++) (*d)(i,i) = TMath::Sqrt((*d)(i,i));
318 TMatrixD* sqrtMat =
new TMatrixD( n, n );
319 sqrtMat->Mult( (*s), (*d) );
336 const TMatrixD* TMVA::Tools::GetCorrelationMatrix(
const TMatrixD* covMat )
339 if (covMat == 0)
return 0;
341 Int_t nvar = covMat->GetNrows();
342 if (nvar != covMat->GetNcols())
343 Log() << kFATAL <<
"<GetCorrelationMatrix> input matrix not quadratic" << Endl;
346 TMatrixD* corrMat =
new TMatrixD( nvar, nvar );
347 for (Int_t ivar=0; ivar<nvar; ivar++) {
348 for (Int_t jvar=0; jvar<nvar; jvar++) {
350 Double_t d = (*covMat)(ivar, ivar)*(*covMat)(jvar, jvar);
352 (*corrMat)(ivar, jvar) = (*covMat)(ivar, jvar)/TMath::Sqrt(d);
354 Log() <<
"<GetCorrelationMatrix> zero variances for variables "
355 <<
"(" << ivar <<
", " << jvar <<
")" << Endl;
356 (*corrMat)(ivar, jvar) = 0;
358 if (TMath::Abs( (*corrMat)(ivar,jvar)) > 1){
360 <<
" Element corr("<<ivar<<
","<<ivar<<
")=" << (*corrMat)(ivar,jvar)
362 <<
" cov("<<ivar<<
","<<ivar<<
")=" <<(*covMat)(ivar, ivar)
363 <<
" cov("<<jvar<<
","<<jvar<<
")=" <<(*covMat)(jvar, jvar)
368 else (*corrMat)(ivar, ivar) = 1.0;
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 )
385 TH1* hist =
new TH1F( name, name, nbins, xmin, xmax );
387 theTree->Project( name, theVarName, cut );
395 Double_t TMVA::Tools::NormHist( TH1* theHist, Double_t norm )
397 if (!theHist)
return 0;
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 );
413 TList* TMVA::Tools::ParseFormatLine( TString formatString,
const char* sep )
415 TList* labelList =
new TList();
416 labelList->SetOwner();
417 while (formatString.First(sep)==0) formatString.Remove(0,1);
419 while (formatString.Length()>0) {
420 if (formatString.First(sep) == -1) {
421 labelList->Add(
new TObjString(formatString.Data()));
426 Ssiz_t posSep = formatString.First(sep);
427 labelList->Add(
new TObjString(TString(formatString(0,posSep)).Data()));
428 formatString.Remove(0,posSep+1);
430 while (formatString.First(sep)==0) formatString.Remove(0,1);
455 vector<Int_t>* TMVA::Tools::ParseANNOptionString( TString theOptions, Int_t nvar,
456 vector<Int_t>* nodes )
458 TList* list = TMVA::Tools::ParseFormatLine( theOptions,
":" );
462 if (list->GetSize() < 1) {
463 Log() << kFATAL <<
"<ParseANNOptionString> unrecognized option string: " << theOptions << Endl;
467 nodes->push_back( atoi( ((TObjString*)list->At(0))->GetString() ) );
470 if (list->GetSize() > 1) {
471 for (Int_t i=1; i<list->GetSize(); i++) {
472 TString s = ((TObjString*)list->At(i))->GetString();
475 if (s.Length() > 1) nodes->push_back( nvar + atoi(&s[1]) );
476 else nodes->push_back( nvar );
478 else if ((a = atoi( s )) > 0) nodes->push_back( atoi(s ) );
480 Log() << kFATAL <<
"<ParseANNOptionString> unrecognized option string: " << theOptions << Endl;
491 Bool_t TMVA::Tools::CheckSplines(
const TH1* theHist,
const TSpline* theSpline )
493 const Double_t sanityCrit = 0.01;
495 Bool_t retval = kTRUE;
496 for (Int_t ibin=1; ibin<=theHist->GetNbinsX(); ibin++) {
497 Double_t x = theHist->GetBinCenter( ibin );
498 Double_t yh = theHist->GetBinContent( ibin );
499 Double_t ys = theSpline->Eval( x );
502 Double_t dev = 0.5*(ys - yh)/(ys + yh);
503 if (TMath::Abs(dev) > sanityCrit) {
504 Log() << kFATAL <<
"<CheckSplines> Spline failed sanity criterion; "
505 <<
" relative deviation from histogram: " << dev
506 <<
" in (bin, value): (" << ibin <<
", " << x <<
")" << Endl;
518 std::vector<Double_t> TMVA::Tools::MVADiff( std::vector<Double_t>& a, std::vector<Double_t>& b )
520 if (a.size() != b.size()) {
523 vector<Double_t> result(a.size());
524 for (UInt_t i=0; i<a.size();i++) result[i]=a[i]-b[i];
531 void TMVA::Tools::Scale( std::vector<Double_t>& v, Double_t f )
533 for (UInt_t i=0; i<v.size();i++) v[i]*=f;
539 void TMVA::Tools::Scale( std::vector<Float_t>& v, Float_t f )
541 for (UInt_t i=0; i<v.size();i++) v[i]*=f;
550 void TMVA::Tools::UsefulSortAscending( std::vector<vector<Double_t> >& v, std::vector<TString>* vs ){
551 UInt_t nArrays=v.size();
554 UInt_t sizeofarray=v[0].size();
555 for (UInt_t i=0; i<sizeofarray; i++) {
556 for (UInt_t j=sizeofarray-1; j>i; j--) {
557 if (v[0][j-1] > v[0][j]) {
558 for (UInt_t k=0; k< nArrays; k++) {
559 temp = v[k][j-1]; v[k][j-1] = v[k][j]; v[k][j] = temp;
562 TString temps = (*vs)[j-1]; (*vs)[j-1] = (*vs)[j]; (*vs)[j] = temps;
576 void TMVA::Tools::UsefulSortDescending( std::vector<std::vector<Double_t> >& v, std::vector<TString>* vs )
578 UInt_t nArrays=v.size();
581 UInt_t sizeofarray=v[0].size();
582 for (UInt_t i=0; i<sizeofarray; i++) {
583 for (UInt_t j=sizeofarray-1; j>i; j--) {
584 if (v[0][j-1] < v[0][j]) {
585 for (UInt_t k=0; k< nArrays; k++) {
586 temp = v[k][j-1]; v[k][j-1] = v[k][j]; v[k][j] = temp;
589 TString temps = (*vs)[j-1]; (*vs)[j-1] = (*vs)[j]; (*vs)[j] = temps;
601 Double_t TMVA::Tools::GetMutualInformation(
const TH2F& h_ )
603 Double_t hi = h_.Integral();
604 if (hi == 0)
return -1;
611 Double_t mutualInfo = 0.;
612 Int_t maxBinX = h.GetNbinsX();
613 Int_t maxBinY = h.GetNbinsY();
614 for (Int_t x = 1; x <= maxBinX; x++) {
615 for (Int_t y = 1; y <= maxBinY; y++) {
616 Double_t p_xy = h.GetBinContent(x,y)/hi;
617 Double_t p_x = h.Integral(x,x,1,maxBinY)/hi;
618 Double_t p_y = h.Integral(1,maxBinX,y,y)/hi;
619 if (p_x > 0. && p_y > 0. && p_xy > 0.){
620 mutualInfo += p_xy*TMath::Log(p_xy / (p_x * p_y));
632 Double_t TMVA::Tools::GetCorrelationRatio(
const TH2F& h_ )
634 Double_t hi = h_.Integral();
635 if (hi == 0.)
return -1;
642 Double_t corrRatio = 0.;
643 Double_t y_mean = h.ProjectionY()->GetMean();
644 for (Int_t ix=1; ix<=h.GetNbinsX(); ix++) {
645 corrRatio += (h.Integral(ix,ix,1,h.GetNbinsY())/hi)*pow((GetYMean_binX(h,ix)-y_mean),2);
647 corrRatio /= pow(h.ProjectionY()->GetRMS(),2);
654 Double_t TMVA::Tools::GetYMean_binX(
const TH2& h, Int_t bin_x )
656 if (h.Integral(bin_x,bin_x,1,h.GetNbinsY()) == 0.) {
return 0;}
657 Double_t y_bin_mean = 0.;
658 TH1* py = h.ProjectionY();
659 for (Int_t y = 1; y <= h.GetNbinsY(); y++){
660 y_bin_mean += h.GetBinContent(bin_x,y)*py->GetBinCenter(y);
662 y_bin_mean /= h.Integral(bin_x,bin_x,1,h.GetNbinsY());
669 TH2F* TMVA::Tools::TransposeHist(
const TH2F& h )
672 if (h.GetNbinsX() != h.GetNbinsY()) {
673 Log() << kFATAL <<
"<TransposeHist> cannot transpose non-quadratic histogram" << Endl;
676 TH2F *transposedHisto =
new TH2F( h );
677 for (Int_t ix=1; ix <= h.GetNbinsX(); ix++){
678 for (Int_t iy=1; iy <= h.GetNbinsY(); iy++){
679 transposedHisto->SetBinContent(iy,ix,h.GetBinContent(ix,iy));
684 Double_t stats_old[7];
685 Double_t stats_new[7];
687 h.GetStats(stats_old);
688 stats_new[0] = stats_old[0];
689 stats_new[1] = stats_old[1];
690 stats_new[2] = stats_old[4];
691 stats_new[3] = stats_old[5];
692 stats_new[4] = stats_old[2];
693 stats_new[5] = stats_old[3];
694 stats_new[6] = stats_old[6];
695 transposedHisto->PutStats(stats_new);
697 return transposedHisto;
703 Bool_t TMVA::Tools::CheckForSilentOption(
const TString& cs )
const
705 Bool_t isSilent = kFALSE;
709 s.ReplaceAll(
" ",
"");
710 if (s.Contains(
"silent") && !s.Contains(
"silent=f")) {
711 if (!s.Contains(
"!silent") || s.Contains(
"silent=t")) isSilent = kTRUE;
720 Bool_t TMVA::Tools::CheckForVerboseOption(
const TString& cs )
const
722 Bool_t isVerbose = kFALSE;
726 s.ReplaceAll(
" ",
"");
727 std::vector<TString> v = SplitString( s,
':' );
728 for (std::vector<TString>::iterator it = v.begin(); it != v.end(); ++it) {
729 if ((*it ==
"v" || *it ==
"verbose") && !it->Contains(
"!")) isVerbose = kTRUE;
738 void TMVA::Tools::UsefulSortDescending( std::vector<Double_t>& v )
740 vector< vector<Double_t> > vtemp;
742 UsefulSortDescending(vtemp);
749 void TMVA::Tools::UsefulSortAscending( std::vector<Double_t>& v )
751 vector<vector<Double_t> > vtemp;
753 UsefulSortAscending(vtemp);
760 Int_t TMVA::Tools::GetIndexMaxElement( std::vector<Double_t>& v )
762 if (v.empty())
return -1;
764 Int_t pos=0; Double_t mx=v[0];
765 for (UInt_t i=0; i<v.size(); i++){
777 Int_t TMVA::Tools::GetIndexMinElement( std::vector<Double_t>& v )
779 if (v.empty())
return -1;
781 Int_t pos=0; Double_t mn=v[0];
782 for (UInt_t i=0; i<v.size(); i++){
796 Bool_t TMVA::Tools::ContainsRegularExpression(
const TString& s )
798 Bool_t regular = kFALSE;
799 for (Int_t i = 0; i < Tools::fRegexp.Length(); i++)
800 if (s.Contains( Tools::fRegexp[i] )) { regular = kTRUE;
break; }
810 TString TMVA::Tools::ReplaceRegularExpressions(
const TString& s,
const TString& r )
813 for (Int_t i = 0; i < Tools::fRegexp.Length(); i++)
814 snew.ReplaceAll( Tools::fRegexp[i], r );
816 snew.ReplaceAll(
"::", r );
817 snew.ReplaceAll(
"$",
"_S_" );
818 snew.ReplaceAll(
"&",
"_A_" );
819 snew.ReplaceAll(
"%",
"_MOD_" );
820 snew.ReplaceAll(
"|",
"_O_" );
821 snew.ReplaceAll(
"*",
"_T_" );
822 snew.ReplaceAll(
"/",
"_D_" );
823 snew.ReplaceAll(
"+",
"_P_" );
824 snew.ReplaceAll(
"-",
"_M_" );
825 snew.ReplaceAll(
" ",
"_" );
826 snew.ReplaceAll(
"[",
"_" );
827 snew.ReplaceAll(
"]",
"_" );
828 snew.ReplaceAll(
"=",
"_E_" );
829 snew.ReplaceAll(
">",
"_GT_" );
830 snew.ReplaceAll(
"<",
"_LT_" );
831 snew.ReplaceAll(
"(",
"_" );
832 snew.ReplaceAll(
")",
"_" );
840 const TString& TMVA::Tools::Color(
const TString& c )
842 static const TString gClr_none =
"" ;
843 static const TString gClr_white =
"\033[1;37m";
844 static const TString gClr_black =
"\033[30m";
845 static const TString gClr_blue =
"\033[34m";
846 static const TString gClr_red =
"\033[1;31m" ;
847 static const TString gClr_yellow =
"\033[1;33m";
848 static const TString gClr_darkred =
"\033[31m";
849 static const TString gClr_darkgreen =
"\033[32m";
850 static const TString gClr_darkyellow =
"\033[33m";
852 static const TString gClr_bold =
"\033[1m" ;
853 static const TString gClr_black_b =
"\033[30m" ;
854 static const TString gClr_lblue_b =
"\033[1;34m" ;
855 static const TString gClr_cyan_b =
"\033[0;36m" ;
856 static const TString gClr_lgreen_b =
"\033[1;32m";
858 static const TString gClr_blue_bg =
"\033[44m";
859 static const TString gClr_red_bg =
"\033[1;41m";
860 static const TString gClr_whiteonblue =
"\033[1;44m";
861 static const TString gClr_whiteongreen =
"\033[1;42m";
862 static const TString gClr_grey_bg =
"\033[47m";
864 static const TString gClr_reset =
"\033[0m";
866 if (!gConfig().UseColor())
return gClr_none;
868 if (c ==
"white" )
return gClr_white;
869 if (c ==
"blue" )
return gClr_blue;
870 if (c ==
"black" )
return gClr_black;
871 if (c ==
"lightblue")
return gClr_cyan_b;
872 if (c ==
"yellow")
return gClr_yellow;
873 if (c ==
"red" )
return gClr_red;
874 if (c ==
"dred" )
return gClr_darkred;
875 if (c ==
"dgreen")
return gClr_darkgreen;
876 if (c ==
"lgreenb")
return gClr_lgreen_b;
877 if (c ==
"dyellow")
return gClr_darkyellow;
879 if (c ==
"bold")
return gClr_bold;
880 if (c ==
"bblack")
return gClr_black_b;
882 if (c ==
"blue_bgd")
return gClr_blue_bg;
883 if (c ==
"red_bgd" )
return gClr_red_bg;
885 if (c ==
"white_on_blue" )
return gClr_whiteonblue;
886 if (c ==
"white_on_green")
return gClr_whiteongreen;
888 if (c ==
"reset")
return gClr_reset;
890 std::cout <<
"Unknown color " << c << std::endl;
899 void TMVA::Tools::FormattedOutput(
const std::vector<Double_t>& values,
const std::vector<TString>& V,
900 const TString titleVars,
const TString titleValues, MsgLogger& logger,
904 UInt_t nvar = V.size();
905 if ((UInt_t)values.size() != nvar) {
906 logger << kFATAL <<
"<FormattedOutput> fatal error with dimensions: "
907 << values.size() <<
" OR " <<
" != " << nvar << Endl;
912 std::vector<UInt_t> vLengths;
913 for (UInt_t ivar=0; ivar<nvar; ivar++) maxL = TMath::Max( (UInt_t)V[ivar].Length(), maxL );
914 maxL = TMath::Max( (UInt_t)titleVars.Length(), maxL );
918 maxV = TMath::Max( (UInt_t)titleValues.Length() + 1, maxL );
921 UInt_t clen = maxL + maxV + 3;
924 for (UInt_t i=0; i<clen; i++) logger <<
"-";
928 logger << setw(maxL) << titleVars <<
":";
929 logger << setw(maxV+1) << titleValues <<
":";
931 for (UInt_t i=0; i<clen; i++) logger <<
"-";
935 for (UInt_t irow=0; irow<nvar; irow++) {
936 logger << setw(maxL) << V[irow] <<
":";
937 logger << setw(maxV+1) << Form( format.Data(), values[irow] );
942 for (UInt_t i=0; i<clen; i++) logger <<
"-";
949 void TMVA::Tools::FormattedOutput(
const TMatrixD& M,
const std::vector<TString>& V, MsgLogger& logger )
952 UInt_t nvar = V.size();
953 if ((UInt_t)M.GetNcols() != nvar || (UInt_t)M.GetNrows() != nvar) {
954 logger << kFATAL <<
"<FormattedOutput> fatal error with dimensions: "
955 << M.GetNcols() <<
" OR " << M.GetNrows() <<
" != " << nvar <<
" ==> abort" << Endl;
961 std::vector<UInt_t> vLengths;
962 for (UInt_t ivar=0; ivar<nvar; ivar++) {
963 vLengths.push_back(TMath::Max( (UInt_t)V[ivar].Length(), minL ));
964 maxL = TMath::Max( vLengths.back(), maxL );
968 UInt_t clen = maxL+1;
969 for (UInt_t icol=0; icol<nvar; icol++) clen += vLengths[icol]+1;
972 for (UInt_t i=0; i<clen; i++) logger <<
"-";
976 logger << setw(maxL+1) <<
" ";
977 for (UInt_t icol=0; icol<nvar; icol++) logger << setw(vLengths[icol]+1) << V[icol];
981 for (UInt_t irow=0; irow<nvar; irow++) {
982 logger << setw(maxL) << V[irow] <<
":";
983 for (UInt_t icol=0; icol<nvar; icol++) {
984 logger << setw(vLengths[icol]+1) << Form(
"%+1.3f", M(irow,icol) );
990 for (UInt_t i=0; i<clen; i++) logger <<
"-";
997 void TMVA::Tools::FormattedOutput(
const TMatrixD& M,
998 const std::vector<TString>& vert,
const std::vector<TString>& horiz,
1002 UInt_t nvvar = vert.size();
1003 UInt_t nhvar = horiz.size();
1008 std::vector<UInt_t> vLengths;
1009 for (UInt_t ivar=0; ivar<nvvar; ivar++) {
1010 vLengths.push_back(TMath::Max( (UInt_t)vert[ivar].Length(), minL ));
1011 maxL = TMath::Max( vLengths.back(), maxL );
1016 UInt_t maxLh = minLh;
1017 std::vector<UInt_t> hLengths;
1018 for (UInt_t ivar=0; ivar<nhvar; ivar++) {
1019 hLengths.push_back(TMath::Max( (UInt_t)horiz[ivar].Length(), minL ));
1020 maxLh = TMath::Max( hLengths.back(), maxLh );
1023 UInt_t clen = maxLh+1;
1024 for (UInt_t icol=0; icol<nhvar; icol++) clen += hLengths[icol]+1;
1027 for (UInt_t i=0; i<clen; i++) logger <<
"-";
1031 logger << setw(maxL+1) <<
" ";
1032 for (UInt_t icol=0; icol<nhvar; icol++) logger << setw(hLengths[icol]+1) << horiz[icol];
1036 for (UInt_t irow=0; irow<nvvar; irow++) {
1037 logger << setw(maxL) << vert[irow] <<
":";
1038 for (UInt_t icol=0; icol<nhvar; icol++) {
1039 logger << setw(hLengths[icol]+1) << Form(
"%+1.3f", M(irow,icol) );
1045 for (UInt_t i=0; i<clen; i++) logger <<
"-";
1052 TString TMVA::Tools::GetXTitleWithUnit(
const TString& title,
const TString& unit )
1054 return ( unit ==
"" ? title : ( title +
" [" + unit +
"]" ) );
1060 TString TMVA::Tools::GetYTitleWithUnit(
const TH1& h,
const TString& unit, Bool_t normalised )
1062 TString retval = ( normalised ?
"(1/N) " :
"" );
1063 retval += Form(
"dN_{ }/^{ }%.3g %s", h.GetXaxis()->GetBinWidth(1), unit.Data() );
1070 void TMVA::Tools::WriteFloatArbitraryPrecision( Float_t val, ostream& os )
1072 os << val <<
" :: ";
1074 for (
int i=0; i<4; i++) {
1075 Int_t ic = *((
char*)c+i)-
'\0';
1085 void TMVA::Tools::ReadFloatArbitraryPrecision( Float_t& val, istream& is )
1093 for (
int i=0; i<4; i++) {
1095 *((
char*)ap+i) =
'\0'+c[i];
1106 Bool_t TMVA::Tools::HasAttr(
void* node,
const char* attrname )
1108 return xmlengine().HasAttr(node, attrname);
1114 void TMVA::Tools::ReadAttr(
void* node,
const char* attrname, TString& value )
1116 if (!HasAttr(node, attrname)) {
1117 const char * nodename = xmlengine().GetNodeName(node);
1118 Log() << kFATAL <<
"Trying to read non-existing attribute '" << attrname <<
"' from xml node '" << nodename <<
"'" << Endl;
1120 const char* val = xmlengine().GetAttr(node, attrname);
1121 value = TString(val);
1127 void TMVA::Tools::AddAttr(
void* node,
const char* attrname,
const char* value )
1129 if( node == 0 )
return;
1130 gTools().xmlengine().NewAttr(node, 0, attrname, value );
1136 void* TMVA::Tools::AddChild(
void* parent,
const char* childname,
const char* content,
bool isRootNode )
1138 if( !isRootNode && parent == 0 )
return 0;
1139 return gTools().xmlengine().NewChild(parent, 0, childname, content);
1144 Bool_t TMVA::Tools::AddComment(
void* node,
const char* comment ) {
1145 if( node == 0 )
return kFALSE;
1146 return gTools().xmlengine().AddComment(node, comment);
1152 void* TMVA::Tools::GetParent(
void* child)
1154 void* par = xmlengine().GetParent(child);
1162 void* TMVA::Tools::GetChild(
void* parent,
const char* childname )
1164 void* ch = xmlengine().GetChild(parent);
1165 if (childname != 0) {
1166 while (ch!=0 && strcmp(xmlengine().GetNodeName(ch),childname) != 0) ch = xmlengine().GetNext(ch);
1174 void* TMVA::Tools::GetNextChild(
void* prevchild,
const char* childname )
1176 void* ch = xmlengine().GetNext(prevchild);
1177 if (childname != 0) {
1178 while (ch!=0 && strcmp(xmlengine().GetNodeName(ch),childname)!=0) ch = xmlengine().GetNext(ch);
1186 const char* TMVA::Tools::GetContent(
void* node )
1188 return xmlengine().GetNodeContent(node);
1194 const char* TMVA::Tools::GetName(
void* node )
1196 return xmlengine().GetNodeName(node);
1202 Bool_t TMVA::Tools::AddRawLine(
void* node,
const char * raw )
1204 return xmlengine().AddRawLine( node, raw );
1211 std::vector<TString> TMVA::Tools::SplitString(
const TString& theOpt,
const char separator )
const
1213 std::vector<TString> splitV;
1214 TString splitOpt(theOpt);
1215 splitOpt.ReplaceAll(
"\n",
" ");
1216 splitOpt = splitOpt.Strip(TString::kBoth,separator);
1217 while (splitOpt.Length()>0) {
1218 if ( !splitOpt.Contains(separator) ) {
1219 splitV.push_back(splitOpt);
1223 TString toSave = splitOpt(0,splitOpt.First(separator));
1224 splitV.push_back(toSave);
1225 splitOpt = splitOpt(splitOpt.First(separator),splitOpt.Length());
1227 splitOpt = splitOpt.Strip(TString::kLeading,separator);
1235 TString TMVA::Tools::StringFromInt( Long_t i )
1237 std::stringstream s;
1239 return TString(s.str().c_str());
1245 TString TMVA::Tools::StringFromDouble( Double_t d )
1247 std::stringstream s;
1248 s << Form(
"%5.8e", d );
1249 return TString(s.str().c_str());
1255 void TMVA::Tools::WriteTMatrixDToXML(
void* node,
const char* name, TMatrixD* mat )
1257 void* matnode = xmlengine().NewChild(node, 0, name);
1258 xmlengine().NewAttr(matnode,0,
"Rows", StringFromInt(mat->GetNrows()) );
1259 xmlengine().NewAttr(matnode,0,
"Columns", StringFromInt(mat->GetNcols()) );
1260 std::stringstream s;
1261 for (Int_t row = 0; row<mat->GetNrows(); row++) {
1262 for (Int_t col = 0; col<mat->GetNcols(); col++) {
1263 s << Form(
"%5.15e ", (*mat)[row][col] );
1266 xmlengine().AddRawLine( matnode, s.str().c_str() );
1271 void TMVA::Tools::WriteTVectorDToXML(
void* node,
const char* name, TVectorD* vec )
1273 TMatrixD mat(1,vec->GetNoElements(),&((*vec)[0]));
1274 WriteTMatrixDToXML( node, name, &mat );
1279 void TMVA::Tools::ReadTVectorDFromXML(
void* node,
const char* name, TVectorD* vec )
1281 TMatrixD mat(1,vec->GetNoElements(),&((*vec)[0]));
1282 ReadTMatrixDFromXML( node, name, &mat );
1283 for (
int i=0;i<vec->GetNoElements();++i) (*vec)[i] = mat[0][i];
1288 void TMVA::Tools::ReadTMatrixDFromXML(
void* node,
const char* name, TMatrixD* mat )
1290 if (strcmp(xmlengine().GetNodeName(node),name)!=0){
1291 Log() << kWARNING <<
"Possible Error: Name of matrix in weight file"
1292 <<
" does not match name of matrix passed as argument!" << Endl;
1295 ReadAttr( node,
"Rows", nrows );
1296 ReadAttr( node,
"Columns", ncols );
1297 if (mat->GetNrows() != nrows || mat->GetNcols() != ncols){
1298 Log() << kWARNING <<
"Possible Error: Dimension of matrix in weight file"
1299 <<
" does not match dimension of matrix passed as argument!" << Endl;
1300 mat->ResizeTo(nrows,ncols);
1302 const char* content = xmlengine().GetNodeContent(node);
1303 std::stringstream s(content);
1304 for (Int_t row = 0; row<nrows; row++) {
1305 for (Int_t col = 0; col<ncols; col++) {
1306 s >> (*mat)[row][col];
1314 void TMVA::Tools::TMVAWelcomeMessage()
1316 std::cout << std::endl;
1317 std::cout << Color(
"bold") <<
"TMVA -- Toolkit for Multivariate Data Analysis" << Color(
"reset") << std::endl;
1318 std::cout <<
" " <<
"Version " << TMVA_RELEASE <<
", " << TMVA_RELEASE_DATE << std::endl;
1319 std::cout <<
" " <<
"Copyright (C) 2005-2010 CERN, MPI-K Heidelberg, Us of Bonn and Victoria" << std::endl;
1320 std::cout <<
" " <<
"Home page: http://tmva.sf.net" << std::endl;
1321 std::cout <<
" " <<
"Citation info: http://tmva.sf.net/citeTMVA.html" << std::endl;
1322 std::cout <<
" " <<
"License: http://tmva.sf.net/LICENSE" << std::endl << std::endl;
1328 void TMVA::Tools::TMVAVersionMessage( MsgLogger& logger )
1330 logger <<
"___________TMVA Version " << TMVA_RELEASE <<
", " << TMVA_RELEASE_DATE
1337 void TMVA::Tools::ROOTVersionMessage( MsgLogger& logger )
1339 static const char *
const months[] = {
"Jan",
"Feb",
"Mar",
"Apr",
"May",
1340 "Jun",
"Jul",
"Aug",
"Sep",
"Oct",
1342 Int_t idatqq = gROOT->GetVersionDate();
1343 Int_t iday = idatqq%100;
1344 Int_t imonth = (idatqq/100)%100;
1345 Int_t iyear = (idatqq/10000);
1346 TString versionDate = Form(
"%s %d, %4d",months[imonth-1],iday,iyear);
1349 logger <<
"You are running ROOT Version: " << gROOT->GetVersion() <<
", " << versionDate << Endl;
1356 void TMVA::Tools::TMVAWelcomeMessage( MsgLogger& logger, EWelcomeMessage msgType )
1360 case kStandardWelcomeMsg:
1361 logger << Color(
"white") <<
"TMVA -- Toolkit for Multivariate Analysis" << Color(
"reset") << Endl;
1362 logger <<
"Copyright (C) 2005-2006 CERN, LAPP & MPI-K Heidelberg and Victoria U." << Endl;
1363 logger <<
"Home page http://tmva.sourceforge.net" << Endl;
1364 logger <<
"All rights reserved, please read http://tmva.sf.net/license.txt" << Endl << Endl;
1367 case kIsometricWelcomeMsg:
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;
1378 logger <<
" \\/__/ \\/__/ " << Endl << Endl;
1381 case kBlockWelcomeMsg:
1383 logger <<
"_|_|_|_|_| _| _| _| _| _|_| " << Endl;
1384 logger <<
" _| _|_| _|_| _| _| _| _| " << Endl;
1385 logger <<
" _| _| _| _| _| _| _|_|_|_| " << Endl;
1386 logger <<
" _| _| _| _| _| _| _| " << Endl;
1387 logger <<
" _| _| _| _| _| _| " << Endl << Endl;
1390 case kLeanWelcomeMsg:
1392 logger <<
"_/_/_/_/_/ _/ _/ _/ _/ _/_/ " << Endl;
1393 logger <<
" _/ _/_/ _/_/ _/ _/ _/ _/ " << Endl;
1394 logger <<
" _/ _/ _/ _/ _/ _/ _/_/_/_/ " << Endl;
1395 logger <<
" _/ _/ _/ _/ _/ _/ _/ " << Endl;
1396 logger <<
"_/ _/ _/ _/ _/ _/ " << Endl << Endl;
1399 case kLogoWelcomeMsg:
1401 logger <<
"_/_/_/_/_/ _| _| _| _| _|_| " << Endl;
1402 logger <<
" _/ _|_| _|_| _| _| _| _| " << Endl;
1403 logger <<
" _/ _| _| _| _| _| _|_|_|_| " << Endl;
1404 logger <<
" _/ _| _| _| _| _| _| " << Endl;
1405 logger <<
"_/ _| _| _| _| _| " << Endl << Endl;
1408 case kSmall1WelcomeMsg:
1409 logger <<
" _____ __ ____ ___ " << Endl;
1410 logger <<
"|_ _| \\/ \\ \\ / /_\\ " << Endl;
1411 logger <<
" | | | |\\/| |\\ V / _ \\ " << Endl;
1412 logger <<
" |_| |_| |_| \\_/_/ \\_\\" << Endl << Endl;
1415 case kSmall2WelcomeMsg:
1416 logger <<
" _____ __ ____ ___ " << Endl;
1417 logger <<
"|_ _| \\/ \\ \\ / / \\ " << Endl;
1418 logger <<
" | | | |\\/| |\\ \\ / / _ \\ " << Endl;
1419 logger <<
" | | | | | | \\ V / ___ \\ " << Endl;
1420 logger <<
" |_| |_| |_| \\_/_/ \\_\\ " << Endl << Endl;
1423 case kOriginalWelcomeMsgColor:
1424 logger << kINFO <<
"" << Color(
"red")
1425 <<
"_______________________________________" << Color(
"reset") << Endl;
1426 logger << kINFO <<
"" << Color(
"blue")
1427 << Color(
"red_bgd") << Color(
"bwhite") <<
" // " << Color(
"reset")
1428 << Color(
"white") << Color(
"blue_bgd")
1429 <<
"|\\ /|| \\ // /\\\\\\\\\\\\\\\\\\\\\\\\ \\ \\ \\ " << Color(
"reset") << Endl;
1430 logger << kINFO <<
""<< Color(
"blue")
1431 << Color(
"red_bgd") << Color(
"white") <<
"// " << Color(
"reset")
1432 << Color(
"white") << Color(
"blue_bgd")
1433 <<
"| \\/ || \\// /--\\\\\\\\\\\\\\\\\\\\\\\\ \\ \\ \\" << Color(
"reset") << Endl;
1436 case kOriginalWelcomeMsgBW:
1437 logger << kINFO <<
""
1438 <<
"_______________________________________" << Endl;
1439 logger << kINFO <<
" // "
1440 <<
"|\\ /|| \\ // /\\\\\\\\\\\\\\\\\\\\\\\\ \\ \\ \\ " << Endl;
1441 logger << kINFO <<
"// "
1442 <<
"| \\/ || \\// /--\\\\\\\\\\\\\\\\\\\\\\\\ \\ \\ \\" << Endl;
1446 logger << kFATAL <<
"unknown message type: " << msgType << Endl;
1453 void TMVA::Tools::TMVACitation( MsgLogger& logger, ECitation citType )
1458 logger <<
"A. Hoecker, P. Speckmayer, J. Stelzer, J. Therhaag, E. von Toerne, H. Voss" << Endl;
1459 logger <<
"\"TMVA - Toolkit for Multivariate Data Analysis\" PoS ACAT:040,2007. e-Print: physics/0703039" << Endl;
1463 logger <<
"@Article{TMVA2007," << Endl;
1464 logger <<
" author = \"Hoecker, Andreas and Speckmayer, Peter and Stelzer, Joerg " << Endl;
1465 logger <<
" and Therhaag, Jan and von Toerne, Eckhard and Voss, Helge\"," << Endl;
1466 logger <<
" title = \"{TMVA: Toolkit for multivariate data analysis}\"," << Endl;
1467 logger <<
" journal = \"PoS\"," << Endl;
1468 logger <<
" volume = \"ACAT\"," << Endl;
1469 logger <<
" year = \"2007\"," << Endl;
1470 logger <<
" pages = \"040\"," << Endl;
1471 logger <<
" eprint = \"physics/0703039\"," << Endl;
1472 logger <<
" archivePrefix = \"arXiv\"," << Endl;
1473 logger <<
" SLACcitation = \"%%CITATION = PHYSICS/0703039;%%\"" << Endl;
1474 logger <<
"}" << Endl;
1478 logger <<
"%\\cite{TMVA2007}" << Endl;
1479 logger <<
"\\bibitem{TMVA2007}" << Endl;
1480 logger <<
" A.~Hoecker, P.~Speckmayer, J.~Stelzer, J.~Therhaag, E.~von Toerne, H.~Voss" << Endl;
1481 logger <<
" %``TMVA: Toolkit for multivariate data analysis,''" << Endl;
1482 logger <<
" PoS A {\\bf CAT} (2007) 040" << Endl;
1483 logger <<
" [arXiv:physics/0703039]." << Endl;
1484 logger <<
" %%CITATION = POSCI,ACAT,040;%%" << Endl;
1489 logger << kHEADER << gTools().Color(
"bold")
1490 <<
"Thank you for using TMVA!" << gTools().Color(
"reset") << Endl;
1491 logger << kINFO << gTools().Color(
"bold")
1492 <<
"For citation information, please visit: http://tmva.sf.net/citeTMVA.html"
1493 << gTools().Color(
"reset") << Endl;
1499 Bool_t TMVA::Tools::HistoHasEquidistantBins(
const TH1& h)
1501 return !(h.GetXaxis()->GetXbins()->fN);
1506 std::vector<TMatrixDSym*>*
1507 TMVA::Tools::CalcCovarianceMatrices(
const std::vector<const Event*>& events, Int_t maxCls, VariableTransformBase* transformBase )
1509 std::vector<Event*> eventVector;
1510 for (std::vector<const Event*>::const_iterator it = events.begin(), itEnd = events.end(); it != itEnd; ++it)
1512 eventVector.push_back (
new Event(*(*it)));
1514 std::vector<TMatrixDSym*>* returnValue = CalcCovarianceMatrices (eventVector, maxCls, transformBase);
1515 for (std::vector<Event*>::const_iterator it = eventVector.begin(), itEnd = eventVector.end(); it != itEnd; ++it)
1525 std::vector<TMatrixDSym*>*
1526 TMVA::Tools::CalcCovarianceMatrices(
const std::vector<Event*>& events, Int_t maxCls, VariableTransformBase* transformBase )
1528 if (events.empty()) {
1529 Log() << kWARNING <<
" Asked to calculate a covariance matrix for an empty event vectors.. sorry cannot do that -> return NULL"<<Endl;
1533 UInt_t nvars=0, ntgts=0, nspcts=0;
1535 transformBase->CountVariableTypes( nvars, ntgts, nspcts );
1537 nvars =events.at(0)->GetNVariables ();
1538 ntgts =events.at(0)->GetNTargets ();
1539 nspcts=events.at(0)->GetNSpectators();
1544 Int_t matNum = maxCls;
1545 if (maxCls > 1 ) matNum++;
1547 std::vector<TVectorD*>* vec =
new std::vector<TVectorD*>(matNum);
1548 std::vector<TMatrixD*>* mat2 =
new std::vector<TMatrixD*>(matNum);
1549 std::vector<Double_t> count(matNum);
1550 count.assign(matNum,0);
1555 UInt_t ivar=0, jvar=0;
1556 for (cls = 0; cls < matNum ; cls++) {
1557 vec->at(cls) =
new TVectorD(nvars);
1558 mat2->at(cls) =
new TMatrixD(nvars,nvars);
1562 for (ivar=0; ivar<nvars; ivar++) {
1564 for (jvar=0; jvar<nvars; jvar++) {
1565 (*m)(ivar, jvar) = 0;
1571 for (UInt_t i=0; i<events.size(); i++) {
1574 const Event * ev = events[i];
1575 cls = ev->GetClass();
1576 Double_t weight = ev->GetWeight();
1578 std::vector<Float_t> input;
1579 std::vector<Char_t> mask;
1581 if (transformBase) {
1582 transformBase->GetInput (ev, input, mask);
1584 for (ivar=0; ivar<nvars; ++ivar) {
1585 input.push_back (ev->GetValue(ivar));
1590 v = vec->at(matNum-1);
1591 m = mat2->at(matNum-1);
1593 count.at(matNum-1)+=weight;
1594 for (ivar=0; ivar<nvars; ivar++) {
1596 Double_t xi = input.at (ivar);
1597 (*v)(ivar) += xi*weight;
1598 (*m)(ivar, ivar) += (xi*xi*weight);
1600 for (jvar=ivar+1; jvar<nvars; jvar++) {
1601 Double_t xj = input.at (jvar);
1602 (*m)(ivar, jvar) += (xi*xj*weight);
1603 (*m)(jvar, ivar) = (*m)(ivar, jvar);
1608 count.at(cls)+=weight;
1611 for (ivar=0; ivar<nvars; ivar++) {
1612 Double_t xi = input.at (ivar);
1613 (*v)(ivar) += xi*weight;
1614 (*m)(ivar, ivar) += (xi*xi*weight);
1616 for (jvar=ivar+1; jvar<nvars; jvar++) {
1617 Double_t xj = input.at (jvar);
1618 (*m)(ivar, jvar) += (xi*xj*weight);
1619 (*m)(jvar, ivar) = (*m)(ivar, jvar);
1625 std::vector<TMatrixDSym*>* mat =
new std::vector<TMatrixDSym*>(matNum);
1626 for (cls = 0; cls < matNum; cls++) {
1630 mat->at(cls) =
new TMatrixDSym(nvars);
1632 Double_t n = count.at(cls);
1633 for (ivar=0; ivar<nvars; ivar++) {
1634 for (jvar=0; jvar<nvars; jvar++) {
1635 (*(mat->at(cls)))(ivar, jvar) = (*m)(ivar, jvar)/n - (*v)(ivar)*(*v)(jvar)/(n*n);
1653 template <
typename Iterator,
typename WeightIterator>
1654 Double_t TMVA::Tools::Mean ( Iterator first, Iterator last, WeightIterator w)
1661 while ( first != last )
1673 ::Error(
"TMVA::Tools::Mean",
"sum of weights <= 0 ?! that's a bit too much of negative event weights :) ");
1679 while ( first != last )
1685 sum += (*w) * (*first);
1692 ::Error(
"TMVA::Tools::Mean",
"sum of weights <= 0 ?! that's a bit too much of negative event weights :) ");
1702 template <
typename T>
1703 Double_t TMVA::Tools::Mean(Long64_t n,
const T *a,
const Double_t *w)
1706 return TMVA::Tools::Mean(a, a+n, w);
1708 return TMath::Mean(a, a+n);
1717 template <
typename Iterator,
typename WeightIterator>
1718 Double_t TMVA::Tools::RMS(Iterator first, Iterator last, WeightIterator w)
1728 while ( first != last ) {
1729 adouble=Double_t(*first);
1731 sum2 += adouble*adouble;
1738 while ( first != last ) {
1739 adouble=Double_t(*first);
1740 sum += adouble * (*w);
1741 sum2 += adouble*adouble * (*w);
1747 Double_t norm = 1./sumw;
1748 Double_t mean = sum*norm;
1749 Double_t rms = TMath::Sqrt(TMath::Abs(sum2*norm -mean*mean));
1758 template <
typename T>
1759 Double_t TMVA::Tools::RMS(Long64_t n,
const T *a,
const Double_t *w)
1763 return TMVA::Tools::RMS(a, a+n, w);
1765 return TMath::RMS(a, a+n);
1772 TH1* TMVA::Tools::GetCumulativeDist( TH1* h)
1774 TH1* cumulativeDist= (TH1*) h->Clone(Form(
"%sCumul",h->GetTitle()));
1777 Float_t partialSum = 0;
1778 Float_t inverseSum = 0.;
1781 for (Int_t ibinEnd=1, ibin=cumulativeDist->GetNbinsX(); ibin >=ibinEnd ; ibin--){
1782 val = cumulativeDist->GetBinContent(ibin);
1783 if (val>0) inverseSum += val;
1785 inverseSum = 1/inverseSum;
1787 for (Int_t ibinEnd=1, ibin=cumulativeDist->GetNbinsX(); ibin >=ibinEnd ; ibin--){
1788 val = cumulativeDist->GetBinContent(ibin);
1789 if (val>0) partialSum += val;
1790 cumulativeDist->SetBinContent(ibin,partialSum*inverseSum);
1792 return cumulativeDist;
1795 void TMVA::Tools::ReadAttr(
void *node,
const char *attrname,
float &value)
1798 const char *val = xmlengine().GetAttr(node, attrname);
1799 if (val ==
nullptr) {
1800 const char *nodename = xmlengine().GetNodeName(node);
1801 Log() << kFATAL <<
"Trying to read non-existing attribute '" << attrname <<
"' from xml node '" << nodename <<
"'"
1807 void TMVA::Tools::ReadAttr(
void *node,
const char *attrname,
int &value)
1810 const char *val = xmlengine().GetAttr(node, attrname);
1811 if (val ==
nullptr) {
1812 const char *nodename = xmlengine().GetNodeName(node);
1813 Log() << kFATAL <<
"Trying to read non-existing attribute '" << attrname <<
"' from xml node '" << nodename <<
"'"
1819 void TMVA::Tools::ReadAttr(
void *node,
const char *attrname,
short &value)
1822 const char *val = xmlengine().GetAttr(node, attrname);
1823 if (val ==
nullptr) {
1824 const char *nodename = xmlengine().GetNodeName(node);
1825 Log() << kFATAL <<
"Trying to read non-existing attribute '" << attrname <<
"' from xml node '" << nodename <<
"'"