149#include <unordered_map>
169 , fSigToBkgFraction(0)
174 , fBaggedGradBoost(
kFALSE)
178 , fMinNodeSizeS(
"5%")
181 , fMinLinCorrForFisher(.8)
182 , fUseExclusiveVars(0)
184 , fNodePurityLimit(0)
189 , fFValidationEvents(0)
191 , fRandomisedTrees(
kFALSE)
193 , fUsePoissonNvars(0)
194 , fUseNTrainEvents(0)
195 , fBaggedSampleFraction(0)
196 , fNoNegWeightsInTraining(
kFALSE)
197 , fInverseBoostNegWeights(
kFALSE)
198 , fPairNegWeightsGlobal(
kFALSE)
199 , fTrainWithNegWeights(
kFALSE)
209 , fSkipNormalization(
kFALSE)
224 , fSigToBkgFraction(0)
229 , fBaggedGradBoost(
kFALSE)
233 , fMinNodeSizeS(
"5%")
236 , fMinLinCorrForFisher(.8)
237 , fUseExclusiveVars(0)
239 , fNodePurityLimit(0)
244 , fFValidationEvents(0)
246 , fRandomisedTrees(
kFALSE)
248 , fUsePoissonNvars(0)
249 , fUseNTrainEvents(0)
250 , fBaggedSampleFraction(0)
251 , fNoNegWeightsInTraining(
kFALSE)
252 , fInverseBoostNegWeights(
kFALSE)
253 , fPairNegWeightsGlobal(
kFALSE)
254 , fTrainWithNegWeights(
kFALSE)
264 , fSkipNormalization(
kFALSE)
334 DeclareOptionRef(fNTrees,
"NTrees",
"Number of trees in the forest");
335 if (DoRegression()) {
336 DeclareOptionRef(fMaxDepth=50,
"MaxDepth",
"Max depth of the decision tree allowed");
338 DeclareOptionRef(fMaxDepth=3,
"MaxDepth",
"Max depth of the decision tree allowed");
341 TString tmp=
"5%";
if (DoRegression()) tmp=
"0.2%";
342 DeclareOptionRef(fMinNodeSizeS=tmp,
"MinNodeSize",
"Minimum percentage of training events required in a leaf node (default: Classification: 5%, Regression: 0.2%)");
344 DeclareOptionRef(fNCuts,
"nCuts",
"Number of grid points in variable range used in finding optimal cut in node splitting");
346 DeclareOptionRef(fBoostType,
"BoostType",
"Boosting type for the trees in the forest (note: AdaCost is still experimental)");
348 AddPreDefVal(
TString(
"AdaBoost"));
349 AddPreDefVal(
TString(
"RealAdaBoost"));
350 AddPreDefVal(
TString(
"AdaCost"));
351 AddPreDefVal(
TString(
"Bagging"));
353 AddPreDefVal(
TString(
"AdaBoostR2"));
355 if (DoRegression()) {
356 fBoostType =
"AdaBoostR2";
358 fBoostType =
"AdaBoost";
360 DeclareOptionRef(fAdaBoostR2Loss=
"Quadratic",
"AdaBoostR2Loss",
"Type of Loss function in AdaBoostR2");
361 AddPreDefVal(
TString(
"Linear"));
362 AddPreDefVal(
TString(
"Quadratic"));
363 AddPreDefVal(
TString(
"Exponential"));
365 DeclareOptionRef(fBaggedBoost=
kFALSE,
"UseBaggedBoost",
"Use only a random subsample of all events for growing the trees in each boost iteration.");
366 DeclareOptionRef(fShrinkage = 1.0,
"Shrinkage",
"Learning rate for BoostType=Grad algorithm");
367 DeclareOptionRef(fAdaBoostBeta=.5,
"AdaBoostBeta",
"Learning rate for AdaBoost algorithm");
368 DeclareOptionRef(fRandomisedTrees,
"UseRandomisedTrees",
"Determine at each node splitting the cut variable only as the best out of a random subset of variables (like in RandomForests)");
369 DeclareOptionRef(fUseNvars,
"UseNvars",
"Size of the subset of variables used with RandomisedTree option");
370 DeclareOptionRef(fUsePoissonNvars,
"UsePoissonNvars",
"Interpret \"UseNvars\" not as fixed number but as mean of a Poisson distribution in each split with RandomisedTree option");
371 DeclareOptionRef(fBaggedSampleFraction=.6,
"BaggedSampleFraction",
"Relative size of bagged event sample to original size of the data sample (used whenever bagging is used (i.e. UseBaggedBoost, Bagging,)" );
373 DeclareOptionRef(fUseYesNoLeaf=
kTRUE,
"UseYesNoLeaf",
374 "Use Sig or Bkg categories, or the purity=S/(S+B) as classification of the leaf node -> Real-AdaBoost");
375 if (DoRegression()) {
379 DeclareOptionRef(fNegWeightTreatment=
"InverseBoostNegWeights",
"NegWeightTreatment",
"How to treat events with negative weights in the BDT training (particular the boosting) : IgnoreInTraining; Boost With inverse boostweight; Pair events with negative and positive weights in training sample and *annihilate* them (experimental!)");
380 AddPreDefVal(
TString(
"InverseBoostNegWeights"));
381 AddPreDefVal(
TString(
"IgnoreNegWeightsInTraining"));
382 AddPreDefVal(
TString(
"NoNegWeightsInTraining"));
383 AddPreDefVal(
TString(
"PairNegWeightsGlobal"));
388 DeclareOptionRef(fCss=1.,
"Css",
"AdaCost: cost of true signal selected signal");
389 DeclareOptionRef(fCts_sb=1.,
"Cts_sb",
"AdaCost: cost of true signal selected bkg");
390 DeclareOptionRef(fCtb_ss=1.,
"Ctb_ss",
"AdaCost: cost of true bkg selected signal");
391 DeclareOptionRef(fCbb=1.,
"Cbb",
"AdaCost: cost of true bkg selected bkg ");
393 DeclareOptionRef(fNodePurityLimit=0.5,
"NodePurityLimit",
"In boosting/pruning, nodes with purity > NodePurityLimit are signal; background otherwise.");
396 DeclareOptionRef(fSepTypeS,
"SeparationType",
"Separation criterion for node splitting");
397 AddPreDefVal(
TString(
"CrossEntropy"));
398 AddPreDefVal(
TString(
"GiniIndex"));
399 AddPreDefVal(
TString(
"GiniIndexWithLaplace"));
400 AddPreDefVal(
TString(
"MisClassificationError"));
401 AddPreDefVal(
TString(
"SDivSqrtSPlusB"));
402 AddPreDefVal(
TString(
"RegressionVariance"));
403 if (DoRegression()) {
404 fSepTypeS =
"RegressionVariance";
406 fSepTypeS =
"GiniIndex";
409 DeclareOptionRef(fRegressionLossFunctionBDTGS =
"Huber",
"RegressionLossFunctionBDTG",
"Loss function for BDTG regression.");
410 AddPreDefVal(
TString(
"Huber"));
411 AddPreDefVal(
TString(
"AbsoluteDeviation"));
412 AddPreDefVal(
TString(
"LeastSquares"));
414 DeclareOptionRef(fHuberQuantile = 0.7,
"HuberQuantile",
"In the Huber loss function this is the quantile that separates the core from the tails in the residuals distribution.");
416 DeclareOptionRef(fDoBoostMonitor=
kFALSE,
"DoBoostMonitor",
"Create control plot with ROC integral vs tree number");
418 DeclareOptionRef(fUseFisherCuts=
kFALSE,
"UseFisherCuts",
"Use multivariate splits using the Fisher criterion");
419 DeclareOptionRef(fMinLinCorrForFisher=.8,
"MinLinCorrForFisher",
"The minimum linear correlation between two variables demanded for use in Fisher criterion in node splitting");
420 DeclareOptionRef(fUseExclusiveVars=
kFALSE,
"UseExclusiveVars",
"Variables already used in fisher criterion are not anymore analysed individually for node splitting");
423 DeclareOptionRef(fDoPreselection=
kFALSE,
"DoPreselection",
"and and apply automatic pre-selection for 100% efficient signal (bkg) cuts prior to training");
426 DeclareOptionRef(fSigToBkgFraction=1,
"SigToBkgFraction",
"Sig to Bkg ratio used in Training (similar to NodePurityLimit, which cannot be used in real adaboost");
428 DeclareOptionRef(fPruneMethodS,
"PruneMethod",
"Note: for BDTs use small trees (e.g.MaxDepth=3) and NoPruning: Pruning: Method used for pruning (removal) of statistically insignificant branches ");
429 AddPreDefVal(
TString(
"NoPruning"));
430 AddPreDefVal(
TString(
"ExpectedError"));
431 AddPreDefVal(
TString(
"CostComplexity"));
433 DeclareOptionRef(fPruneStrength,
"PruneStrength",
"Pruning strength");
435 DeclareOptionRef(fFValidationEvents=0.5,
"PruningValFraction",
"Fraction of events to use for optimizing automatic pruning.");
437 DeclareOptionRef(fSkipNormalization=
kFALSE,
"SkipNormalization",
"Skip normalization at initialization, to keep expectation value of BDT output according to the fraction of events");
440 DeclareOptionRef(fMinNodeEvents=0,
"nEventsMin",
"deprecated: Use MinNodeSize (in % of training events) instead");
442 DeclareOptionRef(fBaggedGradBoost=
kFALSE,
"UseBaggedGrad",
"deprecated: Use *UseBaggedBoost* instead: Use only a random subsample of all events for growing the trees in each iteration.");
443 DeclareOptionRef(fBaggedSampleFraction,
"GradBaggingFraction",
"deprecated: Use *BaggedSampleFraction* instead: Defines the fraction of events to be used in each iteration, e.g. when UseBaggedGrad=kTRUE. ");
444 DeclareOptionRef(fUseNTrainEvents,
"UseNTrainEvents",
"deprecated: Use *BaggedSampleFraction* instead: Number of randomly picked training events used in randomised (and bagged) trees");
445 DeclareOptionRef(fNNodesMax,
"NNodesMax",
"deprecated: Use MaxDepth instead to limit the tree size" );
457 DeclareOptionRef(fHistoricBool=
kTRUE,
"UseWeightedTrees",
458 "Use weighted trees or simple average in classification from the forest");
459 DeclareOptionRef(fHistoricBool=
kFALSE,
"PruneBeforeBoost",
"Flag to prune the tree before applying boosting algorithm");
460 DeclareOptionRef(fHistoricBool=
kFALSE,
"RenormByClass",
"Individually re-normalize each event class to the original size after boosting");
462 AddPreDefVal(
TString(
"NegWeightTreatment"),
TString(
"IgnoreNegWeights"));
473 else if (fSepTypeS ==
"giniindex") fSepType =
new GiniIndex();
475 else if (fSepTypeS ==
"crossentropy") fSepType =
new CrossEntropy();
476 else if (fSepTypeS ==
"sdivsqrtsplusb") fSepType =
new SdivSqrtSplusB();
477 else if (fSepTypeS ==
"regressionvariance") fSepType = NULL;
479 Log() << kINFO << GetOptions() <<
Endl;
480 Log() << kFATAL <<
"<ProcessOptions> unknown Separation Index option " << fSepTypeS <<
" called" <<
Endl;
483 if(!(fHuberQuantile >= 0.0 && fHuberQuantile <= 1.0)){
484 Log() << kINFO << GetOptions() <<
Endl;
485 Log() << kFATAL <<
"<ProcessOptions> Huber Quantile must be in range [0,1]. Value given, " << fHuberQuantile <<
", does not match this criteria" <<
Endl;
489 fRegressionLossFunctionBDTGS.ToLower();
490 if (fRegressionLossFunctionBDTGS ==
"huber") fRegressionLossFunctionBDTG =
new HuberLossFunctionBDT(fHuberQuantile);
494 Log() << kINFO << GetOptions() <<
Endl;
495 Log() << kFATAL <<
"<ProcessOptions> unknown Regression Loss Function BDT option " << fRegressionLossFunctionBDTGS <<
" called" <<
Endl;
498 fPruneMethodS.ToLower();
503 Log() << kINFO << GetOptions() <<
Endl;
504 Log() << kFATAL <<
"<ProcessOptions> unknown PruneMethod " << fPruneMethodS <<
" option called" <<
Endl;
510 <<
"Sorry automatic pruning strength determination is not implemented yet for ExpectedErrorPruning" <<
Endl;
514 if (fMinNodeEvents > 0){
515 fMinNodeSize =
Double_t(fMinNodeEvents*100.) / Data()->GetNTrainingEvents();
516 Log() << kWARNING <<
"You have explicitly set ** nEventsMin = " << fMinNodeEvents<<
" ** the min absolute number \n"
517 <<
"of events in a leaf node. This is DEPRECATED, please use the option \n"
518 <<
"*MinNodeSize* giving the relative number as percentage of training \n"
519 <<
"events instead. \n"
520 <<
"nEventsMin="<<fMinNodeEvents<<
"--> MinNodeSize="<<fMinNodeSize<<
"%"
522 Log() << kWARNING <<
"Note also that explicitly setting *nEventsMin* so far OVERWRITES the option recommended \n"
523 <<
" *MinNodeSize* = " << fMinNodeSizeS <<
" option !!" <<
Endl ;
527 SetMinNodeSize(fMinNodeSizeS);
531 fAdaBoostR2Loss.ToLower();
533 if (fBoostType==
"Grad") {
535 if (fNegWeightTreatment==
"InverseBoostNegWeights"){
536 Log() << kINFO <<
"the option NegWeightTreatment=InverseBoostNegWeights does"
537 <<
" not exist for BoostType=Grad" <<
Endl;
538 Log() << kINFO <<
"--> change to new default NegWeightTreatment=Pray" <<
Endl;
539 Log() << kDEBUG <<
"i.e. simply keep them as if which should work fine for Grad Boost" <<
Endl;
540 fNegWeightTreatment=
"Pray";
541 fNoNegWeightsInTraining=
kFALSE;
543 }
else if (fBoostType==
"RealAdaBoost"){
544 fBoostType =
"AdaBoost";
546 }
else if (fBoostType==
"AdaCost"){
550 if (fFValidationEvents < 0.0) fFValidationEvents = 0.0;
551 if (fAutomatic && fFValidationEvents > 0.5) {
552 Log() << kWARNING <<
"You have chosen to use more than half of your training sample "
553 <<
"to optimize the automatic pruning algorithm. This is probably wasteful "
554 <<
"and your overall results will be degraded. Are you sure you want this?"
559 if (this->Data()->HasNegativeEventWeights()){
560 Log() << kINFO <<
" You are using a Monte Carlo that has also negative weights. "
561 <<
"That should in principle be fine as long as on average you end up with "
562 <<
"something positive. For this you have to make sure that the minimal number "
563 <<
"of (un-weighted) events demanded for a tree node (currently you use: MinNodeSize="
564 << fMinNodeSizeS <<
" ("<< fMinNodeSize <<
"%)"
565 <<
", (or the deprecated equivalent nEventsMin) you can set this via the "
566 <<
"BDT option string when booking the "
567 <<
"classifier) is large enough to allow for reasonable averaging!!! "
568 <<
" If this does not help.. maybe you want to try the option: IgnoreNegWeightsInTraining "
569 <<
"which ignores events with negative weight in the training. " <<
Endl
570 <<
Endl <<
"Note: You'll get a WARNING message during the training if that should ever happen" <<
Endl;
573 if (DoRegression()) {
574 if (fUseYesNoLeaf && !IsConstructedFromWeightFile()){
575 Log() << kWARNING <<
"Regression Trees do not work with fUseYesNoLeaf=TRUE --> I will set it to FALSE" <<
Endl;
579 if (fSepType != NULL){
580 Log() << kWARNING <<
"Regression Trees do not work with Separation type other than <RegressionVariance> --> I will use it instead" <<
Endl;
584 Log() << kWARNING <<
"Sorry, UseFisherCuts is not available for regression analysis, I will ignore it!" <<
Endl;
588 Log() << kWARNING <<
"Sorry, the option of nCuts<0 using a more elaborate node splitting algorithm " <<
Endl;
589 Log() << kWARNING <<
"is not implemented for regression analysis ! " <<
Endl;
590 Log() << kWARNING <<
"--> I switch do default nCuts = 20 and use standard node splitting"<<
Endl;
594 if (fRandomisedTrees){
595 Log() << kINFO <<
" Randomised trees use no pruning" <<
Endl;
600 if (fUseFisherCuts) {
601 Log() << kWARNING <<
"When using the option UseFisherCuts, the other option nCuts<0 (i.e. using" <<
Endl;
602 Log() <<
" a more elaborate node splitting algorithm) is not implemented. " <<
Endl;
609 Log() << kERROR <<
" Zero Decision Trees demanded... that does not work !! "
610 <<
" I set it to 1 .. just so that the program does not crash"
615 fNegWeightTreatment.ToLower();
616 if (fNegWeightTreatment ==
"ignorenegweightsintraining") fNoNegWeightsInTraining =
kTRUE;
617 else if (fNegWeightTreatment ==
"nonegweightsintraining") fNoNegWeightsInTraining =
kTRUE;
618 else if (fNegWeightTreatment ==
"inverseboostnegweights") fInverseBoostNegWeights =
kTRUE;
619 else if (fNegWeightTreatment ==
"pairnegweightsglobal") fPairNegWeightsGlobal =
kTRUE;
620 else if (fNegWeightTreatment ==
"pray") Log() << kDEBUG <<
"Yes, good luck with praying " <<
Endl;
622 Log() << kINFO << GetOptions() <<
Endl;
623 Log() << kFATAL <<
"<ProcessOptions> unknown option for treating negative event weights during training " << fNegWeightTreatment <<
" requested" <<
Endl;
626 if (fNegWeightTreatment ==
"pairnegweightsglobal")
627 Log() << kWARNING <<
" you specified the option NegWeightTreatment=PairNegWeightsGlobal : This option is still considered EXPERIMENTAL !! " <<
Endl;
634 while (tmp < fNNodesMax){
638 Log() << kWARNING <<
"You have specified a deprecated option *NNodesMax="<<fNNodesMax
639 <<
"* \n this has been translated to MaxDepth="<<fMaxDepth<<
Endl;
643 if (fUseNTrainEvents>0){
644 fBaggedSampleFraction = (
Double_t) fUseNTrainEvents/Data()->GetNTrainingEvents();
645 Log() << kWARNING <<
"You have specified a deprecated option *UseNTrainEvents="<<fUseNTrainEvents
646 <<
"* \n this has been translated to BaggedSampleFraction="<<fBaggedSampleFraction<<
"(%)"<<
Endl;
649 if (fBoostType==
"Bagging") fBaggedBoost =
kTRUE;
650 if (fBaggedGradBoost){
651 fBaggedBoost =
kTRUE;
652 Log() << kWARNING <<
"You have specified a deprecated option *UseBaggedGrad* --> please use *UseBaggedBoost* instead" <<
Endl;
660 if (sizeInPercent > 0 && sizeInPercent < 50){
661 fMinNodeSize=sizeInPercent;
664 Log() << kFATAL <<
"you have demanded a minimal node size of "
665 << sizeInPercent <<
"% of the training events.. \n"
666 <<
" that somehow does not make sense "<<
Endl;
674 sizeInPercent.ReplaceAll(
"%",
"");
675 sizeInPercent.ReplaceAll(
" ",
"");
676 if (sizeInPercent.IsFloat()) SetMinNodeSize(sizeInPercent.Atof());
678 Log() << kFATAL <<
"I had problems reading the option MinNodeEvents, which "
679 <<
"after removing a possible % sign now reads " << sizeInPercent <<
Endl;
691 fBoostType =
"AdaBoost";
692 if(DataInfo().GetNClasses()!=0)
696 fBoostType =
"AdaBoostR2";
697 fAdaBoostR2Loss =
"Quadratic";
698 if(DataInfo().GetNClasses()!=0)
704 fPruneMethodS =
"NoPruning";
708 fFValidationEvents = 0.5;
709 fRandomisedTrees =
kFALSE;
712 fUsePoissonNvars =
kTRUE;
717 SetSignalReferenceCut( 0 );
730 for (
UInt_t i=0; i<fForest.size(); i++)
delete fForest[i];
733 fBoostWeights.clear();
734 if (fMonitorNtuple) { fMonitorNtuple->Delete(); fMonitorNtuple=NULL; }
735 fVariableImportance.clear();
737 fLossFunctionEventInfo.clear();
741 if (Data()) Data()->DeleteResults(GetMethodName(),
Types::kTraining, GetAnalysisType());
742 Log() << kDEBUG <<
" successfully(?) reset the method " <<
Endl;
754 for (
UInt_t i=0; i<fForest.size(); i++)
delete fForest[i];
762 if (!HasTrainingTree()) Log() << kFATAL <<
"<Init> Data().TrainingTree() is zero pointer" <<
Endl;
764 if (fEventSample.size() > 0) {
766 for (
UInt_t iev=0; iev<fEventSample.size(); iev++) fEventSample[iev]->SetBoostWeight(1.);
769 UInt_t nevents = Data()->GetNTrainingEvents();
771 std::vector<const TMVA::Event*> tmpEventSample;
772 for (
Long64_t ievt=0; ievt<nevents; ievt++) {
774 Event*
event =
new Event( *GetTrainingEvent(ievt) );
775 tmpEventSample.push_back(
event);
778 if (!DoRegression()) DeterminePreselectionCuts(tmpEventSample);
779 else fDoPreselection =
kFALSE;
781 for (
UInt_t i=0; i<tmpEventSample.size(); i++)
delete tmpEventSample[i];
786 for (
Long64_t ievt=0; ievt<nevents; ievt++) {
789 Event*
event =
new Event( *GetTrainingEvent(ievt) );
790 if (fDoPreselection){
797 if (
event->GetWeight() < 0 && (IgnoreEventsWithNegWeightsInTraining() || fNoNegWeightsInTraining)){
798 if (firstNegWeight) {
799 Log() << kWARNING <<
" Note, you have events with negative event weight in the sample, but you've chosen to ignore them" <<
Endl;
803 }
else if (
event->GetWeight()==0){
804 if (firstZeroWeight) {
806 Log() <<
"Events with weight == 0 are going to be simply ignored " <<
Endl;
810 if (
event->GetWeight() < 0) {
811 fTrainWithNegWeights=
kTRUE;
814 if (fPairNegWeightsGlobal){
815 Log() << kWARNING <<
"Events with negative event weights are found and "
816 <<
" will be removed prior to the actual BDT training by global "
817 <<
" paring (and subsequent annihilation) with positiv weight events"
820 Log() << kWARNING <<
"Events with negative event weights are USED during "
821 <<
"the BDT training. This might cause problems with small node sizes "
822 <<
"or with the boosting. Please remove negative events from training "
823 <<
"using the option *IgnoreEventsWithNegWeightsInTraining* in case you "
824 <<
"observe problems with the boosting"
831 Double_t modulo = 1.0/(fFValidationEvents);
832 Int_t imodulo =
static_cast<Int_t>( fmod(modulo,1.0) > 0.5 ? ceil(modulo) : floor(modulo) );
833 if (ievt % imodulo == 0) fValidationSample.push_back(
event );
834 else fEventSample.push_back(
event );
837 fEventSample.push_back(
event);
843 Log() << kINFO <<
"<InitEventSample> Internally I use " << fEventSample.size()
844 <<
" for Training and " << fValidationSample.size()
845 <<
" for Pruning Validation (" << ((
Float_t)fValidationSample.size())/((
Float_t)fEventSample.size()+fValidationSample.size())*100.0
846 <<
"% of training used for validation)" <<
Endl;
850 if (fPairNegWeightsGlobal) PreProcessNegativeEventWeights();
853 if (DoRegression()) {
855 }
else if (DoMulticlass()) {
857 }
else if (!fSkipNormalization) {
859 Log() << kDEBUG <<
"\t<InitEventSample> For classification trees, "<<
Endl;
860 Log() << kDEBUG <<
" \tthe effective number of backgrounds is scaled to match "<<
Endl;
861 Log() << kDEBUG <<
" \tthe signal. Otherwise the first boosting step would do 'just that'!"<<
Endl;
875 Double_t nevents = fEventSample.size();
877 Int_t sumSig=0, sumBkg=0;
878 for (
UInt_t ievt=0; ievt<fEventSample.size(); ievt++) {
879 if ((DataInfo().IsSignal(fEventSample[ievt])) ) {
880 sumSigW += fEventSample[ievt]->GetWeight();
883 sumBkgW += fEventSample[ievt]->GetWeight();
887 if (sumSigW && sumBkgW){
888 Double_t normSig = nevents/((1+fSigToBkgFraction)*sumSigW)*fSigToBkgFraction;
889 Double_t normBkg = nevents/((1+fSigToBkgFraction)*sumBkgW); ;
890 Log() << kDEBUG <<
"\tre-normalise events such that Sig and Bkg have respective sum of weights = "
891 << fSigToBkgFraction <<
Endl;
892 Log() << kDEBUG <<
" \tsig->sig*"<<normSig <<
"ev. bkg->bkg*"<<normBkg <<
"ev." <<
Endl;
893 Log() << kHEADER <<
"#events: (reweighted) sig: "<< sumSigW*normSig <<
" bkg: " << sumBkgW*normBkg <<
Endl;
894 Log() << kINFO <<
"#events: (unweighted) sig: "<< sumSig <<
" bkg: " << sumBkg <<
Endl;
895 for (
Long64_t ievt=0; ievt<nevents; ievt++) {
896 if ((DataInfo().IsSignal(fEventSample[ievt])) ) fEventSample[ievt]->SetBoostWeight(normSig);
897 else fEventSample[ievt]->SetBoostWeight(normBkg);
900 Log() << kINFO <<
"--> could not determine scaling factors as either there are " <<
Endl;
901 Log() << kINFO <<
" no signal events (sumSigW="<<sumSigW<<
") or no bkg ev. (sumBkgW="<<sumBkgW<<
")"<<
Endl;
906 fTrainSample = &fEventSample;
908 GetBaggedSubSample(fEventSample);
909 fTrainSample = &fSubSample;
935 std::vector<const Event*> negEvents;
936 for (
UInt_t iev = 0; iev < fEventSample.size(); iev++){
937 if (fEventSample[iev]->GetWeight() < 0) {
938 totalNegWeights += fEventSample[iev]->GetWeight();
939 negEvents.push_back(fEventSample[iev]);
941 totalPosWeights += fEventSample[iev]->GetWeight();
943 totalWeights += fEventSample[iev]->GetWeight();
945 if (totalNegWeights == 0 ) {
946 Log() << kINFO <<
"no negative event weights found .. no preprocessing necessary" <<
Endl;
949 Log() << kINFO <<
"found a total of " << totalNegWeights <<
" of negative event weights which I am going to try to pair with positive events to annihilate them" <<
Endl;
950 Log() << kINFO <<
"found a total of " << totalPosWeights <<
" of events with positive weights" <<
Endl;
951 Log() << kINFO <<
"--> total sum of weights = " << totalWeights <<
" = " << totalNegWeights+totalPosWeights <<
Endl;
958 for (
Int_t i=0; i<2; i++){
959 invCov = ((*cov)[i]);
960 if (
TMath::Abs(invCov->Determinant()) < 10E-24 ) {
961 std::cout <<
"<MethodBDT::PreProcessNeg...> matrix is almost singular with determinant="
963 <<
" did you use the variables that are linear combinations or highly correlated?"
966 if (
TMath::Abs(invCov->Determinant()) < 10E-120 ) {
967 std::cout <<
"<MethodBDT::PreProcessNeg...> matrix is singular with determinant="
969 <<
" did you use the variables that are linear combinations?"
978 Log() << kINFO <<
"Found a total of " << totalNegWeights <<
" in negative weights out of " << fEventSample.size() <<
" training events " <<
Endl;
979 Timer timer(negEvents.size(),
"Negative Event paired");
980 for (
UInt_t nev = 0; nev < negEvents.size(); nev++){
981 timer.DrawProgressBar( nev );
982 Double_t weight = negEvents[nev]->GetWeight();
983 UInt_t iClassID = negEvents[nev]->GetClass();
984 invCov = ((*cov)[iClassID]);
990 for (
UInt_t iev = 0; iev < fEventSample.size(); iev++){
991 if (iClassID==fEventSample[iev]->GetClass() && fEventSample[iev]->GetWeight() > 0){
993 for (
UInt_t ivar=0; ivar < GetNvar(); ivar++){
994 for (
UInt_t jvar=0; jvar<GetNvar(); jvar++){
995 dist += (negEvents[nev]->GetValue(ivar)-fEventSample[iev]->GetValue(ivar))*
996 (*invCov)[ivar][jvar]*
997 (negEvents[nev]->GetValue(jvar)-fEventSample[iev]->GetValue(jvar));
1000 if (dist < minDist) { iMin=iev; minDist=dist;}
1006 Double_t newWeight = (negEvents[nev]->GetWeight() + fEventSample[iMin]->GetWeight());
1008 negEvents[nev]->SetBoostWeight( 0 );
1009 fEventSample[iMin]->SetBoostWeight( newWeight/fEventSample[iMin]->GetOriginalWeight() );
1011 negEvents[nev]->SetBoostWeight( newWeight/negEvents[nev]->GetOriginalWeight() );
1012 fEventSample[iMin]->SetBoostWeight( 0 );
1015 }
else Log() << kFATAL <<
"preprocessing didn't find event to pair with the negative weight ... probably a bug" <<
Endl;
1016 weight = negEvents[nev]->GetWeight();
1019 Log() << kINFO <<
"<Negative Event Pairing> took: " << timer.GetElapsedTime()
1023 totalNegWeights = 0;
1024 totalPosWeights = 0;
1031 std::vector<const Event*> newEventSample;
1033 for (
UInt_t iev = 0; iev < fEventSample.size(); iev++){
1034 if (fEventSample[iev]->GetWeight() < 0) {
1035 totalNegWeights += fEventSample[iev]->GetWeight();
1036 totalWeights += fEventSample[iev]->GetWeight();
1038 totalPosWeights += fEventSample[iev]->GetWeight();
1039 totalWeights += fEventSample[iev]->GetWeight();
1041 if (fEventSample[iev]->GetWeight() > 0) {
1042 newEventSample.push_back(
new Event(*fEventSample[iev]));
1043 if (fEventSample[iev]->GetClass() == fSignalClass){
1044 sigWeight += fEventSample[iev]->GetWeight();
1047 bkgWeight += fEventSample[iev]->GetWeight();
1052 if (totalNegWeights < 0) Log() << kFATAL <<
" compensation of negative event weights with positive ones did not work " << totalNegWeights <<
Endl;
1054 for (
UInt_t i=0; i<fEventSample.size(); i++)
delete fEventSample[i];
1055 fEventSample = newEventSample;
1057 Log() << kINFO <<
" after PreProcessing, the Event sample is left with " << fEventSample.size() <<
" events (unweighted), all with positive weights, adding up to " << totalWeights <<
Endl;
1058 Log() << kINFO <<
" nSig="<<nSig <<
" sigWeight="<<sigWeight <<
" nBkg="<<nBkg <<
" bkgWeight="<<bkgWeight <<
Endl;
1070 std::map<TString,TMVA::Interval*> tuneParameters;
1071 std::map<TString,Double_t> tunedParameters;
1080 tuneParameters.insert(std::pair<TString,Interval*>(
"NTrees",
new Interval(10,1000,5)));
1081 tuneParameters.insert(std::pair<TString,Interval*>(
"MaxDepth",
new Interval(2,4,3)));
1082 tuneParameters.insert(std::pair<TString,Interval*>(
"MinNodeSize",
new LogInterval(1,30,30)));
1087 if (fBoostType==
"AdaBoost"){
1088 tuneParameters.insert(std::pair<TString,Interval*>(
"AdaBoostBeta",
new Interval(.2,1.,5)));
1090 }
else if (fBoostType==
"Grad"){
1091 tuneParameters.insert(std::pair<TString,Interval*>(
"Shrinkage",
new Interval(0.05,0.50,5)));
1093 }
else if (fBoostType==
"Bagging" && fRandomisedTrees){
1096 tuneParameters.insert(std::pair<TString,Interval*>(
"UseNvars",
new Interval(min_var,max_var,4)));
1100 Log()<<kINFO <<
" the following BDT parameters will be tuned on the respective *grid*\n"<<
Endl;
1101 std::map<TString,TMVA::Interval*>::iterator it;
1102 for(it=tuneParameters.begin(); it!= tuneParameters.end(); ++it){
1103 Log() << kWARNING << it->first <<
Endl;
1104 std::ostringstream oss;
1105 (it->second)->Print(oss);
1111 tunedParameters=optimize.
optimize();
1113 return tunedParameters;
1122 std::map<TString,Double_t>::iterator it;
1123 for(it=tuneParameters.begin(); it!= tuneParameters.end(); ++it){
1124 Log() << kWARNING << it->first <<
" = " << it->second <<
Endl;
1125 if (it->first ==
"MaxDepth" ) SetMaxDepth ((
Int_t)it->second);
1126 else if (it->first ==
"MinNodeSize" ) SetMinNodeSize (it->second);
1127 else if (it->first ==
"NTrees" ) SetNTrees ((
Int_t)it->second);
1128 else if (it->first ==
"NodePurityLimit") SetNodePurityLimit (it->second);
1129 else if (it->first ==
"AdaBoostBeta" ) SetAdaBoostBeta (it->second);
1130 else if (it->first ==
"Shrinkage" ) SetShrinkage (it->second);
1131 else if (it->first ==
"UseNvars" ) SetUseNvars ((
Int_t)it->second);
1132 else if (it->first ==
"BaggedSampleFraction" ) SetBaggedSampleFraction (it->second);
1133 else Log() << kFATAL <<
" SetParameter for " << it->first <<
" not yet implemented " <<
Endl;
1151 Log() << kERROR <<
" Zero Decision Trees demanded... that does not work !! "
1152 <<
" I set it to 1 .. just so that the program does not crash"
1160 if (IsNormalised()) Log() << kFATAL <<
"\"Normalise\" option cannot be used with BDT; "
1161 <<
"please remove the option from the configuration string, or "
1162 <<
"use \"!Normalise\""
1166 Log() << kINFO <<
"Regression Loss Function: "<< fRegressionLossFunctionBDTG->Name() <<
Endl;
1168 Log() << kINFO <<
"Training "<< fNTrees <<
" Decision Trees ... patience please" <<
Endl;
1170 Log() << kDEBUG <<
"Training with maximal depth = " <<fMaxDepth
1171 <<
", MinNodeEvents=" << fMinNodeEvents
1172 <<
", NTrees="<<fNTrees
1173 <<
", NodePurityLimit="<<fNodePurityLimit
1174 <<
", AdaBoostBeta="<<fAdaBoostBeta
1180 TString hname =
"AdaBooost weight distribution";
1186 if (DoRegression()) {
1190 hname=
"Boost event weights distribution";
1196 TH1* nodesBeforePruningVsTree =
new TH1I(
TString::Format(
"%s_NodesBeforePruning",DataInfo().GetName()).Data(),
"nodes before pruning",fNTrees,0,fNTrees);
1197 TH1* nodesAfterPruningVsTree =
new TH1I(
TString::Format(
"%s_NodesAfterPruning",DataInfo().GetName()).Data(),
"nodes after pruning",fNTrees,0,fNTrees);
1200 if(!DoMulticlass()){
1203 h->SetXTitle(
"boost weight");
1204 results->
Store(
h,
"BoostWeights");
1208 if (fDoBoostMonitor){
1209 TH2* boostMonitor =
new TH2F(
"BoostMonitor",
"ROC Integral Vs iTree",2,0,fNTrees,2,0,1.05);
1210 boostMonitor->SetXTitle(
"#tree");
1211 boostMonitor->SetYTitle(
"ROC Integral");
1212 results->Store(boostMonitor,
"BoostMonitor");
1214 boostMonitorGraph->SetName(
"BoostMonitorGraph");
1215 boostMonitorGraph->SetTitle(
"ROCIntegralVsNTrees");
1216 results->Store(boostMonitorGraph,
"BoostMonitorGraph");
1220 h =
new TH1F(
"BoostWeightVsTree",
"Boost weights vs tree",fNTrees,0,fNTrees);
1221 h->SetXTitle(
"#tree");
1222 h->SetYTitle(
"boost weight");
1223 results->Store(
h,
"BoostWeightsVsTree");
1226 h =
new TH1F(
"ErrFractHist",
"error fraction vs tree number",fNTrees,0,fNTrees);
1227 h->SetXTitle(
"#tree");
1228 h->SetYTitle(
"error fraction");
1229 results->Store(
h,
"ErrorFrac");
1232 nodesBeforePruningVsTree->SetXTitle(
"#tree");
1233 nodesBeforePruningVsTree->SetYTitle(
"#tree nodes");
1234 results->Store(nodesBeforePruningVsTree);
1237 nodesAfterPruningVsTree->SetXTitle(
"#tree");
1238 nodesAfterPruningVsTree->SetYTitle(
"#tree nodes");
1239 results->Store(nodesAfterPruningVsTree);
1243 fMonitorNtuple=
new TTree(
"MonitorNtuple",
"BDT variables");
1244 fMonitorNtuple->Branch(
"iTree",&fITree,
"iTree/I");
1245 fMonitorNtuple->Branch(
"boostWeight",&fBoostWeight,
"boostWeight/D");
1246 fMonitorNtuple->Branch(
"errorFraction",&fErrorFraction,
"errorFraction/D");
1248 Timer timer( fNTrees, GetName() );
1249 Int_t nNodesBeforePruningCount = 0;
1250 Int_t nNodesAfterPruningCount = 0;
1252 Int_t nNodesBeforePruning = 0;
1253 Int_t nNodesAfterPruning = 0;
1255 if(fBoostType==
"Grad"){
1256 InitGradBoost(fEventSample);
1263 while (itree < fNTrees && continueBoost){
1264 timer.DrawProgressBar( itree );
1276 if (fBoostType!=
"Grad"){
1277 Log() << kFATAL <<
"Multiclass is currently only supported by gradient boost. "
1278 <<
"Please change boost option accordingly (BoostType=Grad)." <<
Endl;
1281 UInt_t nClasses = DataInfo().GetNClasses();
1282 for (
UInt_t i=0;i<nClasses;i++){
1286 fForest.push_back(
new DecisionTree( fSepType, fMinNodeSize, fNCuts, &(DataInfo()), i,
1287 fRandomisedTrees, fUseNvars, fUsePoissonNvars, fMaxDepth,
1288 itree*nClasses+i, fNodePurityLimit, itree*nClasses+1));
1289 fForest.back()->SetNVars(GetNvar());
1290 if (fUseFisherCuts) {
1291 fForest.back()->SetUseFisherCuts();
1292 fForest.back()->SetMinLinCorrForFisher(fMinLinCorrForFisher);
1293 fForest.back()->SetUseExclusiveVars(fUseExclusiveVars);
1297 nNodesBeforePruning = fForest.back()->BuildTree(*fTrainSample);
1298 Double_t bw = this->Boost(*fTrainSample, fForest.back(),i);
1300 fBoostWeights.push_back(bw);
1302 fBoostWeights.push_back(0);
1303 Log() << kWARNING <<
"stopped boosting at itree="<<itree <<
Endl;
1312 fRandomisedTrees, fUseNvars, fUsePoissonNvars, fMaxDepth,
1313 itree, fNodePurityLimit, itree);
1315 fForest.push_back(dt);
1316 fForest.back()->SetNVars(GetNvar());
1317 if (fUseFisherCuts) {
1318 fForest.back()->SetUseFisherCuts();
1319 fForest.back()->SetMinLinCorrForFisher(fMinLinCorrForFisher);
1320 fForest.back()->SetUseExclusiveVars(fUseExclusiveVars);
1323 nNodesBeforePruning = fForest.back()->BuildTree(*fTrainSample);
1325 if (fUseYesNoLeaf && !DoRegression() && fBoostType!=
"Grad") {
1326 nNodesBeforePruning = fForest.back()->CleanTree();
1329 nNodesBeforePruningCount += nNodesBeforePruning;
1330 nodesBeforePruningVsTree->SetBinContent(itree+1,nNodesBeforePruning);
1332 fForest.back()->SetPruneMethod(fPruneMethod);
1333 fForest.back()->SetPruneStrength(fPruneStrength);
1335 std::vector<const Event*> * validationSample = NULL;
1336 if(fAutomatic) validationSample = &fValidationSample;
1337 Double_t bw = this->Boost(*fTrainSample, fForest.back());
1339 fBoostWeights.push_back(bw);
1341 fBoostWeights.push_back(0);
1342 Log() << kWARNING <<
"stopped boosting at itree="<<itree <<
Endl;
1351 if (fUseYesNoLeaf && !DoRegression() && fBoostType!=
"Grad"){
1352 fForest.back()->CleanTree();
1354 nNodesAfterPruning = fForest.back()->GetNNodes();
1355 nNodesAfterPruningCount += nNodesAfterPruning;
1356 nodesAfterPruningVsTree->SetBinContent(itree+1,nNodesAfterPruning);
1359 fMonitorNtuple->Fill();
1360 if (fDoBoostMonitor){
1361 if (! DoRegression() ){
1362 if ( itree==fNTrees-1 || (!(itree%500)) ||
1363 (!(itree%250) && itree <1000)||
1364 (!(itree%100) && itree < 500)||
1365 (!(itree%50) && itree < 250)||
1366 (!(itree%25) && itree < 150)||
1367 (!(itree%10) && itree < 50)||
1368 (!(itree%5) && itree < 20)
1369 ) BoostMonitor(itree);
1377 Log() << kDEBUG <<
"\t<Train> elapsed time: " << timer.GetElapsedTime()
1380 Log() << kDEBUG <<
"\t<Train> average number of nodes (w/o pruning) : "
1381 << nNodesBeforePruningCount/GetNTrees() <<
Endl;
1384 Log() << kDEBUG <<
"\t<Train> average number of nodes before/after pruning : "
1385 << nNodesBeforePruningCount/GetNTrees() <<
" / "
1386 << nNodesAfterPruningCount/GetNTrees()
1394 Log() << kDEBUG <<
"Now I delete the privat data sample"<<
Endl;
1395 for (
UInt_t i=0; i<fEventSample.size(); i++)
delete fEventSample[i];
1396 for (
UInt_t i=0; i<fValidationSample.size(); i++)
delete fValidationSample[i];
1397 fEventSample.clear();
1398 fValidationSample.clear();
1409 for (
UInt_t itree=0; itree<nTrees; itree++) {
1414 return 2.0/(1.0+exp(-2.0*
sum))-1;
1422 if (DoMulticlass()) {
1423 UInt_t nClasses = DataInfo().GetNClasses();
1424 Bool_t isLastClass = (cls == nClasses - 1);
1436 std::map<const TMVA::Event *, std::vector<double>> & residuals = this->fResiduals;
1439 auto update_residuals = [&residuals, &lastTree, cls](
const TMVA::Event *
e) {
1443 auto update_residuals_last = [&residuals, &lastTree, cls, nClasses](
const TMVA::Event *
e) {
1444 residuals[
e].at(cls) += lastTree.CheckEvent(
e,
kFALSE);
1446 auto &residualsThisEvent = residuals[
e];
1448 std::vector<Double_t> expCache(nClasses, 0.0);
1449 std::transform(residualsThisEvent.begin(),
1450 residualsThisEvent.begin() + nClasses,
1451 expCache.begin(), [](
Double_t d) { return exp(d); });
1453 Double_t exp_sum = std::accumulate(expCache.begin(),
1454 expCache.begin() + nClasses,
1457 for (
UInt_t i = 0; i < nClasses; i++) {
1458 Double_t p_cls = expCache[i] / exp_sum;
1460 Double_t res = (
e->GetClass() == i) ? (1.0 - p_cls) : (-p_cls);
1467 .Foreach(update_residuals_last, eventSample);
1470 .Foreach(update_residuals, eventSample);
1476 std::vector<Double_t> expCache;
1478 expCache.resize(nClasses);
1481 for (
auto e : eventSample) {
1482 fResiduals[
e].at(cls) += fForest.back()->CheckEvent(
e,
kFALSE);
1484 auto &residualsThisEvent = fResiduals[
e];
1485 std::transform(residualsThisEvent.begin(),
1486 residualsThisEvent.begin() + nClasses,
1487 expCache.begin(), [](
Double_t d) { return exp(d); });
1489 Double_t exp_sum = std::accumulate(expCache.begin(),
1490 expCache.begin() + nClasses,
1493 for (
UInt_t i = 0; i < nClasses; i++) {
1494 Double_t p_cls = expCache[i] / exp_sum;
1496 Double_t res = (
e->GetClass() == i) ? (1.0 - p_cls) : (-p_cls);
1503 std::map<const TMVA::Event *, std::vector<double>> & residuals = this->fResiduals;
1506 UInt_t signalClass = DataInfo().GetSignalClassIndex();
1509 auto update_residuals = [&residuals, &lastTree, signalClass](
const TMVA::Event *
e) {
1510 double & residualAt0 = residuals[
e].at(0);
1511 residualAt0 += lastTree.CheckEvent(
e,
kFALSE);
1513 Double_t p_sig = 1.0 / (1.0 + exp(-2.0 * residualAt0));
1514 Double_t res = ((
e->GetClass() == signalClass) ? (1.0 - p_sig) : (-p_sig));
1520 .Foreach(update_residuals, eventSample);
1522 for (
auto e : eventSample) {
1523 double & residualAt0 = residuals[
e].at(0);
1524 residualAt0 += lastTree.CheckEvent(
e,
kFALSE);
1526 Double_t p_sig = 1.0 / (1.0 + exp(-2.0 * residualAt0));
1527 Double_t res = ((
e->GetClass() == signalClass) ? (1.0 - p_sig) : (-p_sig));
1550 auto f = [
this, &nPartitions](
UInt_t partition = 0) ->
Int_t {
1551 Int_t start = 1.0 * partition / nPartitions * this->fEventSample.size();
1552 Int_t end = (partition + 1.0) / nPartitions * this->fEventSample.size();
1554 for (
Int_t i = start; i < end; ++i) {
1573 fRegressionLossFunctionBDTG->SetTargets(eventSample, fLossFunctionEventInfo);
1587 std::unordered_map<TMVA::DecisionTreeNode*, LeafInfo> leaves;
1588 for (
auto e : eventSample) {
1591 auto &
v = leaves[node];
1592 auto target =
e->GetTarget(cls);
1593 v.sumWeightTarget +=
target * weight;
1596 for (
auto &iLeave : leaves) {
1597 constexpr auto minValue = 1
e-30;
1598 if (iLeave.second.sum2 < minValue) {
1599 iLeave.second.sum2 = minValue;
1601 const Double_t K = DataInfo().GetNClasses();
1602 iLeave.first->SetResponse(fShrinkage * (K - 1) / K * iLeave.second.sumWeightTarget / iLeave.second.sum2);
1607 DoMulticlass() ? UpdateTargets(fEventSample, cls) : UpdateTargets(fEventSample);
1619 std::map<TMVA::DecisionTreeNode*,vector< TMVA::LossFunctionEventInfo > > leaves;
1620 for (std::vector<const TMVA::Event*>::const_iterator
e=eventSample.begin();
e!=eventSample.end();++
e) {
1622 (leaves[node]).push_back(fLossFunctionEventInfo[*
e]);
1629 for (std::map<
TMVA::DecisionTreeNode*,vector< TMVA::LossFunctionEventInfo > >::iterator iLeave=leaves.begin();
1630 iLeave!=leaves.end();++iLeave){
1631 Double_t fit = fRegressionLossFunctionBDTG->Fit(iLeave->second);
1632 (iLeave->first)->SetResponse(fShrinkage*fit);
1635 UpdateTargetsRegression(*fTrainSample);
1650 for (std::vector<const TMVA::Event*>::const_iterator
e=eventSample.begin();
e!=eventSample.end();++
e) {
1654 fRegressionLossFunctionBDTG->Init(fLossFunctionEventInfo, fBoostWeights);
1655 UpdateTargetsRegression(*fTrainSample,
kTRUE);
1659 else if(DoMulticlass()){
1660 UInt_t nClasses = DataInfo().GetNClasses();
1661 for (std::vector<const TMVA::Event*>::const_iterator
e=eventSample.begin();
e!=eventSample.end();++
e) {
1662 for (
UInt_t i=0;i<nClasses;i++){
1664 Double_t r = (*e)->GetClass()==i?(1-1.0/nClasses):(-1.0/nClasses);
1666 fResiduals[*
e].push_back(0);
1671 for (std::vector<const TMVA::Event*>::const_iterator
e=eventSample.begin();
e!=eventSample.end();++
e) {
1672 Double_t r = (DataInfo().IsSignal(*
e)?1:0)-0.5;
1674 fResiduals[*
e].push_back(0);
1685 for (
UInt_t ievt=0; ievt<fValidationSample.size(); ievt++) {
1686 Bool_t isSignalType= (dt->CheckEvent(fValidationSample[ievt]) > fNodePurityLimit ) ? 1 : 0;
1688 if (isSignalType == (DataInfo().IsSignal(fValidationSample[ievt])) ) {
1689 ncorrect += fValidationSample[ievt]->GetWeight();
1692 nfalse += fValidationSample[ievt]->GetWeight();
1696 return ncorrect / (ncorrect + nfalse);
1707 if (fBoostType==
"AdaBoost") returnVal = this->AdaBoost (eventSample, dt);
1708 else if (fBoostType==
"AdaCost") returnVal = this->AdaCost (eventSample, dt);
1709 else if (fBoostType==
"Bagging") returnVal = this->Bagging ( );
1710 else if (fBoostType==
"RegBoost") returnVal = this->RegBoost (eventSample, dt);
1711 else if (fBoostType==
"AdaBoostR2") returnVal = this->AdaBoostR2(eventSample, dt);
1712 else if (fBoostType==
"Grad"){
1714 returnVal = this->GradBoostRegression(eventSample, dt);
1715 else if(DoMulticlass())
1716 returnVal = this->GradBoost (eventSample, dt, cls);
1718 returnVal = this->GradBoost (eventSample, dt);
1721 Log() << kINFO << GetOptions() <<
Endl;
1722 Log() << kFATAL <<
"<Boost> unknown boost option " << fBoostType<<
" called" <<
Endl;
1726 GetBaggedSubSample(fEventSample);
1741 TH1F *tmpS =
new TH1F(
"tmpS",
"", 100 , -1., 1.00001 );
1742 TH1F *tmpB =
new TH1F(
"tmpB",
"", 100 , -1., 1.00001 );
1746 UInt_t signalClassNr = DataInfo().GetClassInfo(
"Signal")->GetNumber();
1756 UInt_t nevents = Data()->GetNTestEvents();
1757 for (
UInt_t iev=0; iev < nevents; iev++){
1758 const Event*
event = GetTestingEvent(iev);
1760 if (
event->GetClass() == signalClassNr) {tmp=tmpS;}
1762 tmp->Fill(PrivateGetMvaValue(
event),
event->GetWeight());
1766 std::vector<TH1F*> hS;
1767 std::vector<TH1F*> hB;
1768 for (
UInt_t ivar=0; ivar<GetNvar(); ivar++){
1769 hS.push_back(
new TH1F(
TString::Format(
"SigVar%dAtTree%d",ivar,iTree).Data(),
TString::Format(
"SigVar%dAtTree%d",ivar,iTree).Data(),100,DataInfo().GetVariableInfo(ivar).GetMin(),DataInfo().GetVariableInfo(ivar).GetMax()));
1770 hB.push_back(
new TH1F(
TString::Format(
"BkgVar%dAtTree%d",ivar,iTree).Data(),
TString::Format(
"BkgVar%dAtTree%d",ivar,iTree).Data(),100,DataInfo().GetVariableInfo(ivar).GetMin(),DataInfo().GetVariableInfo(ivar).GetMax()));
1771 results->Store(hS.back(),hS.back()->GetTitle());
1772 results->Store(hB.back(),hB.back()->GetTitle());
1776 for (
UInt_t iev=0; iev < fEventSample.size(); iev++){
1777 if (fEventSample[iev]->GetBoostWeight() > max) max = 1.01*fEventSample[iev]->GetBoostWeight();
1781 results->Store(tmpBoostWeightsS,tmpBoostWeightsS->GetTitle());
1782 results->Store(tmpBoostWeightsB,tmpBoostWeightsB->GetTitle());
1784 TH1F *tmpBoostWeights;
1785 std::vector<TH1F*> *
h;
1787 for (
UInt_t iev=0; iev < fEventSample.size(); iev++){
1788 if (fEventSample[iev]->GetClass() == signalClassNr) {
1789 tmpBoostWeights=tmpBoostWeightsS;
1792 tmpBoostWeights=tmpBoostWeightsB;
1795 tmpBoostWeights->
Fill(fEventSample[iev]->GetBoostWeight());
1796 for (
UInt_t ivar=0; ivar<GetNvar(); ivar++){
1797 (*h)[ivar]->Fill(fEventSample[iev]->GetValue(ivar),fEventSample[iev]->GetWeight());
1806 TGraph*
gr=results->GetGraph(
"BoostMonitorGraph");
1833 Double_t err=0, sumGlobalw=0, sumGlobalwfalse=0, sumGlobalwfalse2=0;
1835 std::vector<Double_t> sumw(DataInfo().GetNClasses(),0);
1838 for (std::vector<const TMVA::Event*>::const_iterator
e=eventSample.begin();
e!=eventSample.end();++
e) {
1841 UInt_t iclass=(*e)->GetClass();
1844 if ( DoRegression() ) {
1846 sumGlobalwfalse +=
w * tmpDev;
1847 sumGlobalwfalse2 +=
w * tmpDev*tmpDev;
1848 if (tmpDev > maxDev) maxDev = tmpDev;
1852 Bool_t isSignalType = (dt->CheckEvent(*
e,fUseYesNoLeaf) > fNodePurityLimit );
1853 if (!(isSignalType == DataInfo().IsSignal(*
e))) {
1854 sumGlobalwfalse+=
w;
1857 Double_t dtoutput = (dt->CheckEvent(*
e,fUseYesNoLeaf) - 0.5)*2.;
1859 if (DataInfo().IsSignal(*
e)) trueType = 1;
1861 sumGlobalwfalse+=
w*trueType*dtoutput;
1866 err = sumGlobalwfalse/sumGlobalw ;
1867 if ( DoRegression() ) {
1869 if (fAdaBoostR2Loss==
"linear"){
1870 err = sumGlobalwfalse/maxDev/sumGlobalw ;
1872 else if (fAdaBoostR2Loss==
"quadratic"){
1873 err = sumGlobalwfalse2/maxDev/maxDev/sumGlobalw ;
1875 else if (fAdaBoostR2Loss==
"exponential"){
1877 for (std::vector<const TMVA::Event*>::const_iterator
e=eventSample.begin();
e!=eventSample.end();++
e) {
1880 err +=
w * (1 - exp (-tmpDev/maxDev)) / sumGlobalw;
1885 Log() << kFATAL <<
" you've chosen a Loss type for Adaboost other than linear, quadratic or exponential "
1886 <<
" namely " << fAdaBoostR2Loss <<
"\n"
1887 <<
"and this is not implemented... a typo in the options ??" <<
Endl;
1891 Log() << kDEBUG <<
"BDT AdaBoos wrong/all: " << sumGlobalwfalse <<
"/" << sumGlobalw <<
Endl;
1895 std::vector<Double_t> newSumw(sumw.size(),0);
1898 if (err >= 0.5 && fUseYesNoLeaf) {
1901 if (dt->GetNNodes() == 1){
1902 Log() << kERROR <<
" YOUR tree has only 1 Node... kind of a funny *tree*. I cannot "
1903 <<
"boost such a thing... if after 1 step the error rate is == 0.5"
1905 <<
"please check why this happens, maybe too many events per node requested ?"
1909 Log() << kERROR <<
" The error rate in the BDT boosting is > 0.5. ("<< err
1910 <<
") That should not happen, please check your code (i.e... the BDT code), I "
1911 <<
" stop boosting here" <<
Endl;
1915 }
else if (err < 0) {
1916 Log() << kERROR <<
" The error rate in the BDT boosting is < 0. That can happen"
1917 <<
" due to improper treatment of negative weights in a Monte Carlo.. (if you have"
1918 <<
" an idea on how to do it in a better way, please let me know (Helge.Voss@cern.ch)"
1919 <<
" for the time being I set it to its absolute value.. just to continue.." <<
Endl;
1923 boostWeight =
TMath::Log((1.-err)/err)*fAdaBoostBeta;
1925 boostWeight =
TMath::Log((1.+err)/(1-err))*fAdaBoostBeta;
1928 Log() << kDEBUG <<
"BDT AdaBoos wrong/all: " << sumGlobalwfalse <<
"/" << sumGlobalw <<
" 1-err/err="<<boostWeight<<
" log.."<<
TMath::Log(boostWeight)<<
Endl;
1933 for (std::vector<const TMVA::Event*>::const_iterator
e=eventSample.begin();
e!=eventSample.end();++
e) {
1935 if (fUseYesNoLeaf||DoRegression()){
1936 if ((!( (dt->CheckEvent(*
e,fUseYesNoLeaf) > fNodePurityLimit ) == DataInfo().IsSignal(*
e))) || DoRegression()) {
1940 if ( (*e)->GetWeight() > 0 ){
1941 (*e)->SetBoostWeight( (*e)->GetBoostWeight() * boostfactor);
1943 if (DoRegression()) results->GetHist(
"BoostWeights")->Fill(boostfactor);
1945 if ( fInverseBoostNegWeights )(*e)->ScaleBoostWeight( 1. / boostfactor);
1946 else (*e)->SetBoostWeight( (*e)->GetBoostWeight() * boostfactor);
1952 Double_t dtoutput = (dt->CheckEvent(*
e,fUseYesNoLeaf) - 0.5)*2.;
1954 if (DataInfo().IsSignal(*
e)) trueType = 1;
1958 if ( (*e)->GetWeight() > 0 ){
1959 (*e)->SetBoostWeight( (*e)->GetBoostWeight() * boostfactor);
1961 if (DoRegression()) results->GetHist(
"BoostWeights")->Fill(boostfactor);
1963 if ( fInverseBoostNegWeights )(*e)->ScaleBoostWeight( 1. / boostfactor);
1964 else (*e)->SetBoostWeight( (*e)->GetBoostWeight() * boostfactor);
1967 newSumGlobalw+=(*e)->GetWeight();
1968 newSumw[(*e)->GetClass()] += (*e)->GetWeight();
1974 Log() << kDEBUG <<
"new Nsig="<<newSumw[0]*globalNormWeight <<
" new Nbkg="<<newSumw[1]*globalNormWeight <<
Endl;
1977 for (std::vector<const TMVA::Event*>::const_iterator
e=eventSample.begin();
e!=eventSample.end();++
e) {
1981 if (DataInfo().IsSignal(*
e))(*e)->ScaleBoostWeight( globalNormWeight * fSigToBkgFraction );
1982 else (*e)->ScaleBoostWeight( globalNormWeight );
1985 if (!(DoRegression()))results->GetHist(
"BoostWeights")->Fill(boostWeight);
1986 results->GetHist(
"BoostWeightsVsTree")->SetBinContent(fForest.size(),boostWeight);
1987 results->GetHist(
"ErrorFrac")->SetBinContent(fForest.size(),err);
1989 fBoostWeight = boostWeight;
1990 fErrorFraction = err;
2016 Double_t err=0, sumGlobalWeights=0, sumGlobalCost=0;
2018 std::vector<Double_t> sumw(DataInfo().GetNClasses(),0);
2020 for (vector<const TMVA::Event*>::const_iterator
e=eventSample.begin();
e!=eventSample.end();++
e) {
2022 sumGlobalWeights +=
w;
2023 UInt_t iclass=(*e)->GetClass();
2027 if ( DoRegression() ) {
2028 Log() << kFATAL <<
" AdaCost not implemented for regression"<<
Endl;
2031 Double_t dtoutput = (dt->CheckEvent(*
e,
false) - 0.5)*2.;
2033 Bool_t isTrueSignal = DataInfo().IsSignal(*
e);
2034 Bool_t isSelectedSignal = (dtoutput>0);
2035 if (isTrueSignal) trueType = 1;
2039 if (isTrueSignal && isSelectedSignal) cost=Css;
2040 else if (isTrueSignal && !isSelectedSignal) cost=Cts_sb;
2041 else if (!isTrueSignal && isSelectedSignal) cost=Ctb_ss;
2042 else if (!isTrueSignal && !isSelectedSignal) cost=Cbb;
2043 else Log() << kERROR <<
"something went wrong in AdaCost" <<
Endl;
2045 sumGlobalCost+=
w*trueType*dtoutput*cost;
2050 if ( DoRegression() ) {
2051 Log() << kFATAL <<
" AdaCost not implemented for regression"<<
Endl;
2056 sumGlobalCost /= sumGlobalWeights;
2061 vector<Double_t> newSumClassWeights(sumw.size(),0);
2063 Double_t boostWeight =
TMath::Log((1+sumGlobalCost)/(1-sumGlobalCost)) * fAdaBoostBeta;
2067 for (vector<const TMVA::Event*>::const_iterator
e=eventSample.begin();
e!=eventSample.end();++
e) {
2068 Double_t dtoutput = (dt->CheckEvent(*
e,
false) - 0.5)*2.;
2070 Bool_t isTrueSignal = DataInfo().IsSignal(*
e);
2071 Bool_t isSelectedSignal = (dtoutput>0);
2072 if (isTrueSignal) trueType = 1;
2076 if (isTrueSignal && isSelectedSignal) cost=Css;
2077 else if (isTrueSignal && !isSelectedSignal) cost=Cts_sb;
2078 else if (!isTrueSignal && isSelectedSignal) cost=Ctb_ss;
2079 else if (!isTrueSignal && !isSelectedSignal) cost=Cbb;
2080 else Log() << kERROR <<
"something went wrong in AdaCost" <<
Endl;
2083 if (DoRegression())Log() << kFATAL <<
" AdaCost not implemented for regression"<<
Endl;
2084 if ( (*e)->GetWeight() > 0 ){
2085 (*e)->SetBoostWeight( (*e)->GetBoostWeight() * boostfactor);
2087 if (DoRegression())Log() << kFATAL <<
" AdaCost not implemented for regression"<<
Endl;
2089 if ( fInverseBoostNegWeights )(*e)->ScaleBoostWeight( 1. / boostfactor);
2092 newSumGlobalWeights+=(*e)->GetWeight();
2093 newSumClassWeights[(*e)->GetClass()] += (*e)->GetWeight();
2098 Double_t globalNormWeight=
Double_t(eventSample.size())/newSumGlobalWeights;
2099 Log() << kDEBUG <<
"new Nsig="<<newSumClassWeights[0]*globalNormWeight <<
" new Nbkg="<<newSumClassWeights[1]*globalNormWeight <<
Endl;
2102 for (std::vector<const TMVA::Event*>::const_iterator
e=eventSample.begin();
e!=eventSample.end();++
e) {
2105 if (DataInfo().IsSignal(*
e))(*e)->ScaleBoostWeight( globalNormWeight * fSigToBkgFraction );
2106 else (*e)->ScaleBoostWeight( globalNormWeight );
2110 if (!(DoRegression()))results->GetHist(
"BoostWeights")->Fill(boostWeight);
2111 results->GetHist(
"BoostWeightsVsTree")->SetBinContent(fForest.size(),boostWeight);
2112 results->GetHist(
"ErrorFrac")->SetBinContent(fForest.size(),err);
2114 fBoostWeight = boostWeight;
2115 fErrorFraction = err;
2142 if (!fSubSample.empty()) fSubSample.clear();
2144 for (std::vector<const TMVA::Event*>::const_iterator
e=eventSample.begin();
e!=eventSample.end();++
e) {
2145 n = trandom->PoissonD(fBaggedSampleFraction);
2146 for (
Int_t i=0;i<
n;i++) fSubSample.push_back(*
e);
2180 if ( !DoRegression() ) Log() << kFATAL <<
"Somehow you chose a regression boost method for a classification job" <<
Endl;
2182 Double_t err=0, sumw=0, sumwfalse=0, sumwfalse2=0;
2184 for (std::vector<const TMVA::Event*>::const_iterator
e=eventSample.begin();
e!=eventSample.end();++
e) {
2189 sumwfalse +=
w * tmpDev;
2190 sumwfalse2 +=
w * tmpDev*tmpDev;
2191 if (tmpDev > maxDev) maxDev = tmpDev;
2195 if (fAdaBoostR2Loss==
"linear"){
2196 err = sumwfalse/maxDev/sumw ;
2198 else if (fAdaBoostR2Loss==
"quadratic"){
2199 err = sumwfalse2/maxDev/maxDev/sumw ;
2201 else if (fAdaBoostR2Loss==
"exponential"){
2203 for (std::vector<const TMVA::Event*>::const_iterator
e=eventSample.begin();
e!=eventSample.end();++
e) {
2206 err +=
w * (1 - exp (-tmpDev/maxDev)) / sumw;
2211 Log() << kFATAL <<
" you've chosen a Loss type for Adaboost other than linear, quadratic or exponential "
2212 <<
" namely " << fAdaBoostR2Loss <<
"\n"
2213 <<
"and this is not implemented... a typo in the options ??" <<
Endl;
2220 if (dt->GetNNodes() == 1){
2221 Log() << kERROR <<
" YOUR tree has only 1 Node... kind of a funny *tree*. I cannot "
2222 <<
"boost such a thing... if after 1 step the error rate is == 0.5"
2224 <<
"please check why this happens, maybe too many events per node requested ?"
2228 Log() << kERROR <<
" The error rate in the BDT boosting is > 0.5. ("<< err
2229 <<
") That should not happen, but is possible for regression trees, and"
2230 <<
" should trigger a stop for the boosting. please check your code (i.e... the BDT code), I "
2231 <<
" stop boosting " <<
Endl;
2235 }
else if (err < 0) {
2236 Log() << kERROR <<
" The error rate in the BDT boosting is < 0. That can happen"
2237 <<
" due to improper treatment of negative weights in a Monte Carlo.. (if you have"
2238 <<
" an idea on how to do it in a better way, please let me know (Helge.Voss@cern.ch)"
2239 <<
" for the time being I set it to its absolute value.. just to continue.." <<
Endl;
2243 Double_t boostWeight = err / (1.-err);
2248 for (std::vector<const TMVA::Event*>::const_iterator
e=eventSample.begin();
e!=eventSample.end();++
e) {
2250 results->GetHist(
"BoostWeights")->Fill(boostfactor);
2252 if ( (*e)->GetWeight() > 0 ){
2253 Float_t newBoostWeight = (*e)->GetBoostWeight() * boostfactor;
2254 Float_t newWeight = (*e)->GetWeight() * (*e)->GetBoostWeight() * boostfactor;
2255 if (newWeight == 0) {
2256 Log() << kINFO <<
"Weight= " << (*e)->GetWeight() <<
Endl;
2257 Log() << kINFO <<
"BoostWeight= " << (*e)->GetBoostWeight() <<
Endl;
2258 Log() << kINFO <<
"boostweight="<<boostWeight <<
" err= " <<err <<
Endl;
2259 Log() << kINFO <<
"NewBoostWeight= " << newBoostWeight <<
Endl;
2260 Log() << kINFO <<
"boostfactor= " << boostfactor <<
Endl;
2261 Log() << kINFO <<
"maxDev = " << maxDev <<
Endl;
2262 Log() << kINFO <<
"tmpDev = " <<
TMath::Abs(dt->CheckEvent(*
e,
kFALSE) - (*e)->GetTarget(0) ) <<
Endl;
2263 Log() << kINFO <<
"target = " << (*e)->GetTarget(0) <<
Endl;
2264 Log() << kINFO <<
"estimate = " << dt->CheckEvent(*
e,
kFALSE) <<
Endl;
2266 (*e)->SetBoostWeight( newBoostWeight );
2269 (*e)->SetBoostWeight( (*e)->GetBoostWeight() / boostfactor);
2271 newSumw+=(*e)->GetWeight();
2275 Double_t normWeight = sumw / newSumw;
2276 for (std::vector<const TMVA::Event*>::const_iterator
e=eventSample.begin();
e!=eventSample.end();++
e) {
2279 (*e)->SetBoostWeight( (*e)->GetBoostWeight() * normWeight );
2283 results->GetHist(
"BoostWeightsVsTree")->SetBinContent(fForest.size(),1./boostWeight);
2284 results->GetHist(
"ErrorFrac")->SetBinContent(fForest.size(),err);
2286 fBoostWeight = boostWeight;
2287 fErrorFraction = err;
2299 if (fDoPreselection){
2300 for (
UInt_t ivar=0; ivar<GetNvar(); ivar++){
2314 gTools().
AddAttr( wght,
"AnalysisType", fForest.back()->GetAnalysisType() );
2316 for (
UInt_t i=0; i< fForest.size(); i++) {
2317 void* trxml = fForest[i]->AddXMLTo(wght);
2328 for (i=0; i<fForest.size(); i++)
delete fForest[i];
2330 fBoostWeights.clear();
2338 fIsLowBkgCut.resize(GetNvar());
2339 fLowBkgCut.resize(GetNvar());
2340 fIsLowSigCut.resize(GetNvar());
2341 fLowSigCut.resize(GetNvar());
2342 fIsHighBkgCut.resize(GetNvar());
2343 fHighBkgCut.resize(GetNvar());
2344 fIsHighSigCut.resize(GetNvar());
2345 fHighSigCut.resize(GetNvar());
2349 for (
UInt_t ivar=0; ivar<GetNvar(); ivar++){
2351 fIsLowBkgCut[ivar]=tmpBool;
2353 fLowBkgCut[ivar]=tmpDouble;
2355 fIsLowSigCut[ivar]=tmpBool;
2357 fLowSigCut[ivar]=tmpDouble;
2359 fIsHighBkgCut[ivar]=tmpBool;
2361 fHighBkgCut[ivar]=tmpDouble;
2363 fIsHighSigCut[ivar]=tmpBool;
2365 fHighSigCut[ivar]=tmpDouble;
2372 if(
gTools().HasAttr(parent,
"TreeType")) {
2383 fForest.back()->SetTreeID(i++);
2385 fBoostWeights.push_back(boostWeight);
2397 Int_t analysisType(0);
2400 istr >> dummy >> fNTrees;
2401 Log() << kINFO <<
"Read " << fNTrees <<
" Decision trees" <<
Endl;
2403 for (
UInt_t i=0;i<fForest.size();i++)
delete fForest[i];
2405 fBoostWeights.clear();
2408 for (
int i=0;i<fNTrees;i++) {
2409 istr >> dummy >> iTree >> dummy >> boostWeight;
2411 fForest.back()->Print( std::cout );
2412 Log() << kFATAL <<
"Error while reading weight file; mismatch iTree="
2413 << iTree <<
" i=" << i
2414 <<
" dummy " << dummy
2415 <<
" boostweight " << boostWeight
2420 fForest.back()->SetTreeID(i);
2421 fForest.back()->
Read(istr, GetTrainingTMVAVersionCode());
2422 fBoostWeights.push_back(boostWeight);
2429 return this->GetMvaValue( err, errUpper, 0 );
2439 const Event* ev = GetEvent();
2440 if (fDoPreselection) {
2441 Double_t val = ApplyPreselectionCuts(ev);
2444 return PrivateGetMvaValue(ev, err, errUpper, useNTrees);
2456 NoErrorCalc(err, errUpper);
2460 UInt_t nTrees = fForest.size();
2462 if (useNTrees > 0 ) nTrees = useNTrees;
2464 if (fBoostType==
"Grad")
return GetGradBoostMVA(ev,nTrees);
2468 for (
UInt_t itree=0; itree<nTrees; itree++) {
2470 myMVA += fBoostWeights[itree] * fForest[itree]->CheckEvent(ev,fUseYesNoLeaf);
2471 norm += fBoostWeights[itree];
2473 return ( norm > std::numeric_limits<double>::epsilon() ) ? myMVA /= norm : 0 ;
2483 if (fMulticlassReturnVal == NULL) fMulticlassReturnVal =
new std::vector<Float_t>();
2484 fMulticlassReturnVal->clear();
2486 UInt_t nClasses = DataInfo().GetNClasses();
2487 std::vector<Double_t> temp(nClasses);
2488 auto forestSize = fForest.size();
2491 std::vector<TMVA::DecisionTree *> forest = fForest;
2492 auto get_output = [&
e, &forest, &temp, forestSize, nClasses](
UInt_t iClass) {
2493 for (
UInt_t itree = iClass; itree < forestSize; itree += nClasses) {
2494 temp[iClass] += forest[itree]->CheckEvent(
e,
kFALSE);
2504 for (
UInt_t itree = 0; itree < forestSize; ++itree) {
2505 temp[classOfTree] += fForest[itree]->CheckEvent(
e,
kFALSE);
2506 if (++classOfTree == nClasses) classOfTree = 0;
2512 std::transform(temp.begin(), temp.end(), temp.begin(), [](
Double_t d){return exp(d);});
2514 Double_t exp_sum = std::accumulate(temp.begin(), temp.end(), 0.0);
2516 for (
UInt_t i = 0; i < nClasses; i++) {
2517 Double_t p_cls = temp[i] / exp_sum;
2518 (*fMulticlassReturnVal).push_back(p_cls);
2521 return *fMulticlassReturnVal;
2530 if (fRegressionReturnVal == NULL) fRegressionReturnVal =
new std::vector<Float_t>();
2531 fRegressionReturnVal->clear();
2533 const Event * ev = GetEvent();
2538 if (fBoostType==
"AdaBoostR2") {
2549 vector< Double_t > response(fForest.size());
2550 vector< Double_t > weight(fForest.size());
2553 for (
UInt_t itree=0; itree<fForest.size(); itree++) {
2554 response[itree] = fForest[itree]->CheckEvent(ev,
kFALSE);
2555 weight[itree] = fBoostWeights[itree];
2556 totalSumOfWeights += fBoostWeights[itree];
2559 std::vector< std::vector<Double_t> > vtemp;
2560 vtemp.push_back( response );
2561 vtemp.push_back( weight );
2566 while (sumOfWeights <= totalSumOfWeights/2.) {
2567 sumOfWeights += vtemp[1][t];
2579 evT->SetTarget(0, rVal/
Double_t(count) );
2581 else if(fBoostType==
"Grad"){
2582 for (
UInt_t itree=0; itree<fForest.size(); itree++) {
2583 myMVA += fForest[itree]->CheckEvent(ev,
kFALSE);
2586 evT->SetTarget(0, myMVA+fBoostWeights[0] );
2589 for (
UInt_t itree=0; itree<fForest.size(); itree++) {
2591 myMVA += fBoostWeights[itree] * fForest[itree]->CheckEvent(ev,
kFALSE);
2592 norm += fBoostWeights[itree];
2595 evT->SetTarget(0, ( norm > std::numeric_limits<double>::epsilon() ) ? myMVA /= norm : 0 );
2600 const Event* evT2 = GetTransformationHandler().InverseTransform( evT );
2601 fRegressionReturnVal->push_back( evT2->GetTarget(0) );
2606 return *fRegressionReturnVal;
2615 Log() << kDEBUG <<
"\tWrite monitoring histograms to file: " << BaseDir()->GetPath() <<
Endl;
2619 fMonitorNtuple->
Write();
2630 fVariableImportance.resize(GetNvar());
2631 for (
UInt_t ivar = 0; ivar < GetNvar(); ivar++) {
2632 fVariableImportance[ivar]=0;
2635 for (
UInt_t itree = 0; itree < GetNTrees(); itree++) {
2636 std::vector<Double_t> relativeImportance(fForest[itree]->GetVariableImportance());
2637 for (
UInt_t i=0; i< relativeImportance.size(); i++) {
2638 fVariableImportance[i] += fBoostWeights[itree] * relativeImportance[i];
2642 for (
UInt_t ivar=0; ivar< fVariableImportance.size(); ivar++){
2643 fVariableImportance[ivar] =
TMath::Sqrt(fVariableImportance[ivar]);
2644 sum += fVariableImportance[ivar];
2646 for (
UInt_t ivar=0; ivar< fVariableImportance.size(); ivar++) fVariableImportance[ivar] /=
sum;
2648 return fVariableImportance;
2658 std::vector<Double_t> relativeImportance = this->GetVariableImportance();
2659 if (ivar < (
UInt_t)relativeImportance.size())
return relativeImportance[ivar];
2660 else Log() << kFATAL <<
"<GetVariableImportance> ivar = " << ivar <<
" is out of range " <<
Endl;
2671 fRanking =
new Ranking( GetName(),
"Variable Importance" );
2672 vector< Double_t> importance(this->GetVariableImportance());
2674 for (
UInt_t ivar=0; ivar<GetNvar(); ivar++) {
2676 fRanking->AddRank(
Rank( GetInputLabel(ivar), importance[ivar] ) );
2690 Log() <<
"Boosted Decision Trees are a collection of individual decision" <<
Endl;
2691 Log() <<
"trees which form a multivariate classifier by (weighted) majority " <<
Endl;
2692 Log() <<
"vote of the individual trees. Consecutive decision trees are " <<
Endl;
2693 Log() <<
"trained using the original training data set with re-weighted " <<
Endl;
2694 Log() <<
"events. By default, the AdaBoost method is employed, which gives " <<
Endl;
2695 Log() <<
"events that were misclassified in the previous tree a larger " <<
Endl;
2696 Log() <<
"weight in the training of the following tree." <<
Endl;
2698 Log() <<
"Decision trees are a sequence of binary splits of the data sample" <<
Endl;
2699 Log() <<
"using a single discriminant variable at a time. A test event " <<
Endl;
2700 Log() <<
"ending up after the sequence of left-right splits in a final " <<
Endl;
2701 Log() <<
"(\"leaf\") node is classified as either signal or background" <<
Endl;
2702 Log() <<
"depending on the majority type of training events in that node." <<
Endl;
2706 Log() <<
"By the nature of the binary splits performed on the individual" <<
Endl;
2707 Log() <<
"variables, decision trees do not deal well with linear correlations" <<
Endl;
2708 Log() <<
"between variables (they need to approximate the linear split in" <<
Endl;
2709 Log() <<
"the two dimensional space by a sequence of splits on the two " <<
Endl;
2710 Log() <<
"variables individually). Hence decorrelation could be useful " <<
Endl;
2711 Log() <<
"to optimise the BDT performance." <<
Endl;
2715 Log() <<
"The two most important parameters in the configuration are the " <<
Endl;
2716 Log() <<
"minimal number of events requested by a leaf node as percentage of the " <<
Endl;
2717 Log() <<
" number of training events (option \"MinNodeSize\" replacing the actual number " <<
Endl;
2718 Log() <<
" of events \"nEventsMin\" as given in earlier versions" <<
Endl;
2719 Log() <<
"If this number is too large, detailed features " <<
Endl;
2720 Log() <<
"in the parameter space are hard to be modelled. If it is too small, " <<
Endl;
2721 Log() <<
"the risk to overtrain rises and boosting seems to be less effective" <<
Endl;
2722 Log() <<
" typical values from our current experience for best performance " <<
Endl;
2723 Log() <<
" are between 0.5(%) and 10(%) " <<
Endl;
2725 Log() <<
"The default minimal number is currently set to " <<
Endl;
2726 Log() <<
" max(20, (N_training_events / N_variables^2 / 10)) " <<
Endl;
2727 Log() <<
"and can be changed by the user." <<
Endl;
2729 Log() <<
"The other crucial parameter, the pruning strength (\"PruneStrength\")," <<
Endl;
2730 Log() <<
"is also related to overtraining. It is a regularisation parameter " <<
Endl;
2731 Log() <<
"that is used when determining after the training which splits " <<
Endl;
2732 Log() <<
"are considered statistically insignificant and are removed. The" <<
Endl;
2733 Log() <<
"user is advised to carefully watch the BDT screen output for" <<
Endl;
2734 Log() <<
"the comparison between efficiencies obtained on the training and" <<
Endl;
2735 Log() <<
"the independent test sample. They should be equal within statistical" <<
Endl;
2736 Log() <<
"errors, in order to minimize statistical fluctuations in different samples." <<
Endl;
2748 fout <<
" std::vector<"<<nodeName<<
"*> fForest; // i.e. root nodes of decision trees" << std::endl;
2749 fout <<
" std::vector<double> fBoostWeights; // the weights applied in the individual boosts" << std::endl;
2750 fout <<
"};" << std::endl << std::endl;
2753 fout <<
"std::vector<double> ReadBDTG::GetMulticlassValues__( const std::vector<double>& inputValues ) const" << std::endl;
2754 fout <<
"{" << std::endl;
2755 fout <<
" uint nClasses = " << DataInfo().GetNClasses() <<
";" << std::endl;
2756 fout <<
" std::vector<double> fMulticlassReturnVal;" << std::endl;
2757 fout <<
" fMulticlassReturnVal.reserve(nClasses);" << std::endl;
2759 fout <<
" std::vector<double> temp(nClasses);" << std::endl;
2760 fout <<
" auto forestSize = fForest.size();" << std::endl;
2761 fout <<
" // trees 0, nClasses, 2*nClasses, ... belong to class 0" << std::endl;
2762 fout <<
" // trees 1, nClasses+1, 2*nClasses+1, ... belong to class 1 and so forth" << std::endl;
2763 fout <<
" uint classOfTree = 0;" << std::endl;
2764 fout <<
" for (uint itree = 0; itree < forestSize; ++itree) {" << std::endl;
2765 fout <<
" BDTGNode *current = fForest[itree];" << std::endl;
2766 fout <<
" while (current->GetNodeType() == 0) { //intermediate node" << std::endl;
2767 fout <<
" if (current->GoesRight(inputValues)) current=(BDTGNode*)current->GetRight();" << std::endl;
2768 fout <<
" else current=(BDTGNode*)current->GetLeft();" << std::endl;
2769 fout <<
" }" << std::endl;
2770 fout <<
" temp[classOfTree] += current->GetResponse();" << std::endl;
2771 fout <<
" if (++classOfTree == nClasses) classOfTree = 0; // cheap modulo" << std::endl;
2772 fout <<
" }" << std::endl;
2774 fout <<
" // we want to calculate sum of exp(temp[j] - temp[i]) for all i,j (i!=j)" << std::endl;
2775 fout <<
" // first calculate exp(), then replace minus with division." << std::endl;
2776 fout <<
" std::transform(temp.begin(), temp.end(), temp.begin(), [](double d){return exp(d);});" << std::endl;
2778 fout <<
" for(uint iClass=0; iClass<nClasses; iClass++){" << std::endl;
2779 fout <<
" double norm = 0.0;" << std::endl;
2780 fout <<
" for(uint j=0;j<nClasses;j++){" << std::endl;
2781 fout <<
" if(iClass!=j)" << std::endl;
2782 fout <<
" norm += temp[j] / temp[iClass];" << std::endl;
2783 fout <<
" }" << std::endl;
2784 fout <<
" fMulticlassReturnVal.push_back(1.0/(1.0+norm));" << std::endl;
2785 fout <<
" }" << std::endl;
2787 fout <<
" return fMulticlassReturnVal;" << std::endl;
2788 fout <<
"}" << std::endl;
2790 fout <<
"double " << className <<
"::GetMvaValue__( const std::vector<double>& inputValues ) const" << std::endl;
2791 fout <<
"{" << std::endl;
2792 fout <<
" double myMVA = 0;" << std::endl;
2793 if (fDoPreselection){
2794 for (
UInt_t ivar = 0; ivar< fIsLowBkgCut.size(); ivar++){
2795 if (fIsLowBkgCut[ivar]){
2796 fout <<
" if (inputValues["<<ivar<<
"] < " << fLowBkgCut[ivar] <<
") return -1; // is background preselection cut" << std::endl;
2798 if (fIsLowSigCut[ivar]){
2799 fout <<
" if (inputValues["<<ivar<<
"] < "<< fLowSigCut[ivar] <<
") return 1; // is signal preselection cut" << std::endl;
2801 if (fIsHighBkgCut[ivar]){
2802 fout <<
" if (inputValues["<<ivar<<
"] > "<<fHighBkgCut[ivar] <<
") return -1; // is background preselection cut" << std::endl;
2804 if (fIsHighSigCut[ivar]){
2805 fout <<
" if (inputValues["<<ivar<<
"] > "<<fHighSigCut[ivar]<<
") return 1; // is signal preselection cut" << std::endl;
2810 if (fBoostType!=
"Grad"){
2811 fout <<
" double norm = 0;" << std::endl;
2813 fout <<
" for (unsigned int itree=0; itree<fForest.size(); itree++){" << std::endl;
2814 fout <<
" "<<nodeName<<
" *current = fForest[itree];" << std::endl;
2815 fout <<
" while (current->GetNodeType() == 0) { //intermediate node" << std::endl;
2816 fout <<
" if (current->GoesRight(inputValues)) current=("<<nodeName<<
"*)current->GetRight();" << std::endl;
2817 fout <<
" else current=("<<nodeName<<
"*)current->GetLeft();" << std::endl;
2818 fout <<
" }" << std::endl;
2819 if (fBoostType==
"Grad"){
2820 fout <<
" myMVA += current->GetResponse();" << std::endl;
2822 if (fUseYesNoLeaf) fout <<
" myMVA += fBoostWeights[itree] * current->GetNodeType();" << std::endl;
2823 else fout <<
" myMVA += fBoostWeights[itree] * current->GetPurity();" << std::endl;
2824 fout <<
" norm += fBoostWeights[itree];" << std::endl;
2826 fout <<
" }" << std::endl;
2827 if (fBoostType==
"Grad"){
2828 fout <<
" return 2.0/(1.0+exp(-2.0*myMVA))-1.0;" << std::endl;
2830 else fout <<
" return myMVA /= norm;" << std::endl;
2831 fout <<
"}" << std::endl << std::endl;
2834 fout <<
"void " << className <<
"::Initialize()" << std::endl;
2835 fout <<
"{" << std::endl;
2836 fout <<
" double inf = std::numeric_limits<double>::infinity();" << std::endl;
2837 fout <<
" double nan = std::numeric_limits<double>::quiet_NaN();" << std::endl;
2839 for (
UInt_t itree=0; itree<GetNTrees(); itree++) {
2840 fout <<
" // itree = " << itree << std::endl;
2841 fout <<
" fBoostWeights.push_back(" << fBoostWeights[itree] <<
");" << std::endl;
2842 fout <<
" fForest.push_back( " << std::endl;
2843 this->MakeClassInstantiateNode((
DecisionTreeNode*)fForest[itree]->GetRoot(), fout, className);
2844 fout <<
" );" << std::endl;
2846 fout <<
" return;" << std::endl;
2847 fout <<
"};" << std::endl;
2849 fout <<
"// Clean up" << std::endl;
2850 fout <<
"inline void " << className <<
"::Clear() " << std::endl;
2851 fout <<
"{" << std::endl;
2852 fout <<
" for (unsigned int itree=0; itree<fForest.size(); itree++) { " << std::endl;
2853 fout <<
" delete fForest[itree]; " << std::endl;
2854 fout <<
" }" << std::endl;
2855 fout <<
"}" << std::endl;
2867 fout <<
"#include <algorithm>" << std::endl;
2868 fout <<
"#include <limits>" << std::endl;
2871 fout <<
"#define NN new "<<nodeName << std::endl;
2874 fout <<
"#ifndef "<<nodeName<<
"__def" << std::endl;
2875 fout <<
"#define "<<nodeName<<
"__def" << std::endl;
2877 fout <<
"class "<<nodeName<<
" {" << std::endl;
2879 fout <<
"public:" << std::endl;
2881 fout <<
" // constructor of an essentially \"empty\" node floating in space" << std::endl;
2882 fout <<
" "<<nodeName<<
" ( "<<nodeName<<
"* left,"<<nodeName<<
"* right," << std::endl;
2883 if (fUseFisherCuts){
2884 fout <<
" int nFisherCoeff," << std::endl;
2885 for (
UInt_t i=0;i<GetNVariables()+1;i++){
2886 fout <<
" double fisherCoeff"<<i<<
"," << std::endl;
2889 fout <<
" int selector, double cutValue, bool cutType, " << std::endl;
2890 fout <<
" int nodeType, double purity, double response ) :" << std::endl;
2891 fout <<
" fLeft ( left )," << std::endl;
2892 fout <<
" fRight ( right )," << std::endl;
2893 if (fUseFisherCuts) fout <<
" fNFisherCoeff ( nFisherCoeff )," << std::endl;
2894 fout <<
" fSelector ( selector )," << std::endl;
2895 fout <<
" fCutValue ( cutValue )," << std::endl;
2896 fout <<
" fCutType ( cutType )," << std::endl;
2897 fout <<
" fNodeType ( nodeType )," << std::endl;
2898 fout <<
" fPurity ( purity )," << std::endl;
2899 fout <<
" fResponse ( response ){" << std::endl;
2900 if (fUseFisherCuts){
2901 for (
UInt_t i=0;i<GetNVariables()+1;i++){
2902 fout <<
" fFisherCoeff.push_back(fisherCoeff"<<i<<
");" << std::endl;
2905 fout <<
" }" << std::endl << std::endl;
2906 fout <<
" virtual ~"<<nodeName<<
"();" << std::endl << std::endl;
2907 fout <<
" // test event if it descends the tree at this node to the right" << std::endl;
2908 fout <<
" virtual bool GoesRight( const std::vector<double>& inputValues ) const;" << std::endl;
2909 fout <<
" "<<nodeName<<
"* GetRight( void ) {return fRight; };" << std::endl << std::endl;
2910 fout <<
" // test event if it descends the tree at this node to the left " << std::endl;
2911 fout <<
" virtual bool GoesLeft ( const std::vector<double>& inputValues ) const;" << std::endl;
2912 fout <<
" "<<nodeName<<
"* GetLeft( void ) { return fLeft; }; " << std::endl << std::endl;
2913 fout <<
" // return S/(S+B) (purity) at this node (from training)" << std::endl << std::endl;
2914 fout <<
" double GetPurity( void ) const { return fPurity; } " << std::endl;
2915 fout <<
" // return the node type" << std::endl;
2916 fout <<
" int GetNodeType( void ) const { return fNodeType; }" << std::endl;
2917 fout <<
" double GetResponse(void) const {return fResponse;}" << std::endl << std::endl;
2918 fout <<
"private:" << std::endl << std::endl;
2919 fout <<
" "<<nodeName<<
"* fLeft; // pointer to the left daughter node" << std::endl;
2920 fout <<
" "<<nodeName<<
"* fRight; // pointer to the right daughter node" << std::endl;
2921 if (fUseFisherCuts){
2922 fout <<
" int fNFisherCoeff; // =0 if this node doesn't use fisher, else =nvar+1 " << std::endl;
2923 fout <<
" std::vector<double> fFisherCoeff; // the fisher coeff (offset at the last element)" << std::endl;
2925 fout <<
" int fSelector; // index of variable used in node selection (decision tree) " << std::endl;
2926 fout <<
" double fCutValue; // cut value applied on this node to discriminate bkg against sig" << std::endl;
2927 fout <<
" bool fCutType; // true: if event variable > cutValue ==> signal , false otherwise" << std::endl;
2928 fout <<
" int fNodeType; // Type of node: -1 == Bkg-leaf, 1 == Signal-leaf, 0 = internal " << std::endl;
2929 fout <<
" double fPurity; // Purity of node from training"<< std::endl;
2930 fout <<
" double fResponse; // Regression response value of node" << std::endl;
2931 fout <<
"}; " << std::endl;
2933 fout <<
"//_______________________________________________________________________" << std::endl;
2934 fout <<
" "<<nodeName<<
"::~"<<nodeName<<
"()" << std::endl;
2935 fout <<
"{" << std::endl;
2936 fout <<
" if (fLeft != NULL) delete fLeft;" << std::endl;
2937 fout <<
" if (fRight != NULL) delete fRight;" << std::endl;
2938 fout <<
"}; " << std::endl;
2940 fout <<
"//_______________________________________________________________________" << std::endl;
2941 fout <<
"bool "<<nodeName<<
"::GoesRight( const std::vector<double>& inputValues ) const" << std::endl;
2942 fout <<
"{" << std::endl;
2943 fout <<
" // test event if it descends the tree at this node to the right" << std::endl;
2944 fout <<
" bool result;" << std::endl;
2945 if (fUseFisherCuts){
2946 fout <<
" if (fNFisherCoeff == 0){" << std::endl;
2947 fout <<
" result = (inputValues[fSelector] >= fCutValue );" << std::endl;
2948 fout <<
" }else{" << std::endl;
2949 fout <<
" double fisher = fFisherCoeff.at(fFisherCoeff.size()-1);" << std::endl;
2950 fout <<
" for (unsigned int ivar=0; ivar<fFisherCoeff.size()-1; ivar++)" << std::endl;
2951 fout <<
" fisher += fFisherCoeff.at(ivar)*inputValues.at(ivar);" << std::endl;
2952 fout <<
" result = fisher > fCutValue;" << std::endl;
2953 fout <<
" }" << std::endl;
2955 fout <<
" result = (inputValues[fSelector] >= fCutValue );" << std::endl;
2957 fout <<
" if (fCutType == true) return result; //the cuts are selecting Signal ;" << std::endl;
2958 fout <<
" else return !result;" << std::endl;
2959 fout <<
"}" << std::endl;
2961 fout <<
"//_______________________________________________________________________" << std::endl;
2962 fout <<
"bool "<<nodeName<<
"::GoesLeft( const std::vector<double>& inputValues ) const" << std::endl;
2963 fout <<
"{" << std::endl;
2964 fout <<
" // test event if it descends the tree at this node to the left" << std::endl;
2965 fout <<
" if (!this->GoesRight(inputValues)) return true;" << std::endl;
2966 fout <<
" else return false;" << std::endl;
2967 fout <<
"}" << std::endl;
2969 fout <<
"#endif" << std::endl;
2979 Log() << kFATAL <<
"MakeClassInstantiateNode: started with undefined node" <<
Endl;
2982 fout <<
"NN("<<std::endl;
2983 if (
n->GetLeft() != NULL){
2984 this->MakeClassInstantiateNode( (
DecisionTreeNode*)
n->GetLeft() , fout, className);
2989 fout <<
", " <<std::endl;
2990 if (
n->GetRight() != NULL){
2991 this->MakeClassInstantiateNode( (
DecisionTreeNode*)
n->GetRight(), fout, className );
2996 fout <<
", " << std::endl
2997 << std::setprecision(6);
2998 if (fUseFisherCuts){
2999 fout <<
n->GetNFisherCoeff() <<
", ";
3000 for (
UInt_t i=0; i< GetNVariables()+1; i++) {
3001 if (
n->GetNFisherCoeff() == 0 ){
3004 fout <<
n->GetFisherCoeff(i) <<
", ";
3008 fout <<
n->GetSelector() <<
", "
3009 <<
n->GetCutValue() <<
", "
3010 <<
n->GetCutType() <<
", "
3011 <<
n->GetNodeType() <<
", "
3012 <<
n->GetPurity() <<
","
3013 <<
n->GetResponse() <<
") ";
3025 std::vector<TMVA::BDTEventWrapper> bdtEventSample;
3027 fIsLowSigCut.assign(GetNvar(),
kFALSE);
3028 fIsLowBkgCut.assign(GetNvar(),
kFALSE);
3029 fIsHighSigCut.assign(GetNvar(),
kFALSE);
3030 fIsHighBkgCut.assign(GetNvar(),
kFALSE);
3032 fLowSigCut.assign(GetNvar(),0.);
3033 fLowBkgCut.assign(GetNvar(),0.);
3034 fHighSigCut.assign(GetNvar(),0.);
3035 fHighBkgCut.assign(GetNvar(),0.);
3040 for( std::vector<const TMVA::Event*>::const_iterator it = eventSample.begin(); it != eventSample.end(); ++it ) {
3041 if (DataInfo().IsSignal(*it)){
3042 nTotS += (*it)->GetWeight();
3045 nTotB += (*it)->GetWeight();
3050 for(
UInt_t ivar = 0; ivar < GetNvar(); ivar++ ) {
3052 std::sort( bdtEventSample.begin(),bdtEventSample.end() );
3054 Double_t bkgWeightCtr = 0.0, sigWeightCtr = 0.0;
3055 std::vector<TMVA::BDTEventWrapper>::iterator it = bdtEventSample.begin(), it_end = bdtEventSample.end();
3056 for( ; it != it_end; ++it ) {
3057 if (DataInfo().IsSignal(**it))
3058 sigWeightCtr += (**it)->GetWeight();
3060 bkgWeightCtr += (**it)->GetWeight();
3062 it->SetCumulativeWeight(
false,bkgWeightCtr);
3063 it->SetCumulativeWeight(
true,sigWeightCtr);
3068 Double_t dVal = (DataInfo().GetVariableInfo(ivar).GetMax() - DataInfo().GetVariableInfo(ivar).GetMin())/100. ;
3069 Double_t nSelS, nSelB, effS=0.05, effB=0.05, rejS=0.05, rejB=0.05;
3070 Double_t tmpEffS, tmpEffB, tmpRejS, tmpRejB;
3075 for(
UInt_t iev = 1; iev < bdtEventSample.size(); iev++) {
3078 nSelS = bdtEventSample[iev].GetCumulativeWeight(
true);
3079 nSelB = bdtEventSample[iev].GetCumulativeWeight(
false);
3081 tmpEffS=nSelS/nTotS;
3082 tmpEffB=nSelB/nTotB;
3085 if (nSelS==0 && tmpEffB>effB) {effB=tmpEffB; fLowBkgCut[ivar] = bdtEventSample[iev].GetVal() - dVal; fIsLowBkgCut[ivar]=
kTRUE;}
3086 else if (nSelB==0 && tmpEffS>effS) {effS=tmpEffS; fLowSigCut[ivar] = bdtEventSample[iev].GetVal() - dVal; fIsLowSigCut[ivar]=
kTRUE;}
3087 else if (nSelB==nTotB && tmpRejS>rejS) {rejS=tmpRejS; fHighSigCut[ivar] = bdtEventSample[iev].GetVal() + dVal; fIsHighSigCut[ivar]=
kTRUE;}
3088 else if (nSelS==nTotS && tmpRejB>rejB) {rejB=tmpRejB; fHighBkgCut[ivar] = bdtEventSample[iev].GetVal() + dVal; fIsHighBkgCut[ivar]=
kTRUE;}
3093 Log() << kDEBUG <<
" \tfound and suggest the following possible pre-selection cuts " <<
Endl;
3094 if (fDoPreselection) Log() << kDEBUG <<
"\tthe training will be done after these cuts... and GetMVA value returns +1, (-1) for a signal (bkg) event that passes these cuts" <<
Endl;
3095 else Log() << kDEBUG <<
"\tas option DoPreselection was not used, these cuts however will not be performed, but the training will see the full sample"<<
Endl;
3096 for (
UInt_t ivar=0; ivar < GetNvar(); ivar++ ) {
3097 if (fIsLowBkgCut[ivar]){
3098 Log() << kDEBUG <<
" \tfound cut: Bkg if var " << ivar <<
" < " << fLowBkgCut[ivar] <<
Endl;
3100 if (fIsLowSigCut[ivar]){
3101 Log() << kDEBUG <<
" \tfound cut: Sig if var " << ivar <<
" < " << fLowSigCut[ivar] <<
Endl;
3103 if (fIsHighBkgCut[ivar]){
3104 Log() << kDEBUG <<
" \tfound cut: Bkg if var " << ivar <<
" > " << fHighBkgCut[ivar] <<
Endl;
3106 if (fIsHighSigCut[ivar]){
3107 Log() << kDEBUG <<
" \tfound cut: Sig if var " << ivar <<
" > " << fHighSigCut[ivar] <<
Endl;
3122 for (
UInt_t ivar=0; ivar < GetNvar(); ivar++ ) {
3123 if (fIsLowBkgCut[ivar]){
3124 if (ev->GetValue(ivar) < fLowBkgCut[ivar])
result = -1;
3126 if (fIsLowSigCut[ivar]){
3127 if (ev->GetValue(ivar) < fLowSigCut[ivar])
result = 1;
3129 if (fIsHighBkgCut[ivar]){
3130 if (ev->GetValue(ivar) > fHighBkgCut[ivar])
result = -1;
3132 if (fIsHighSigCut[ivar]){
3133 if (ev->GetValue(ivar) > fHighSigCut[ivar])
result = 1;
#define REGISTER_METHOD(CLASS)
for example
unsigned int UInt_t
Unsigned integer 4 bytes (unsigned int)
float Float_t
Float 4 bytes (float)
double Double_t
Double 8 bytes.
long long Long64_t
Portable signed long integer 8 bytes.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t target
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t r
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t result
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t type
A pseudo container class which is a generator of indices.
A TGraph is an object made of two arrays X and Y with npoints each.
virtual void SetPoint(Int_t i, Double_t x, Double_t y)
Set x and y values for point number i.
virtual void Set(Int_t n)
Set number of points in the graph Existing coordinates are preserved New coordinates above fNpoints a...
1-D histogram with a float per channel (see TH1 documentation)
1-D histogram with an int per channel (see TH1 documentation)
TH1 is the base class of all histogram classes in ROOT.
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
2-D histogram with a float per channel (see TH1 documentation)
Service class for 2-D histogram classes.
Absolute Deviation BDT Loss Function.
static void SetVarIndex(Int_t iVar)
static Config & Instance()
static function: returns TMVA instance
Implementation of the CrossEntropy as separation criterion.
Class that contains all the data information.
static void SetIsTraining(bool on)
Implementation of a Decision Tree.
static DecisionTree * CreateFromXML(void *node, UInt_t tmva_Version_Code=262657)
re-create a new tree (decision tree or search tree) from XML
Double_t CheckEvent(const TMVA::Event *, Bool_t UseYesNoLeaf=kFALSE) const
the event e is put into the decision tree (starting at the root node) and the output is NodeType (sig...
void SetTarget(UInt_t itgt, Float_t value)
set the target value (dimension itgt) to value
Implementation of the GiniIndex With Laplace correction as separation criterion.
Implementation of the GiniIndex as separation criterion.
The TMVA::Interval Class.
Least Squares BDT Loss Function.
The TMVA::Interval Class.
Double_t GetMvaValue(Double_t *err=nullptr, Double_t *errUpper=nullptr) override
const std::vector< Float_t > & GetMulticlassValues() override
Get the multiclass MVA response for the BDT classifier.
void Init(void) override
Common initialisation with defaults for the BDT-Method.
void MakeClassSpecificHeader(std::ostream &, const TString &) const override
Specific class header.
void AddWeightsXMLTo(void *parent) const override
Write weights to XML.
static const Int_t fgDebugLevel
debug level determining some printout/control plots etc.
MethodBDT(const TString &jobName, const TString &methodTitle, DataSetInfo &theData, const TString &theOption="")
The standard constructor for the "boosted decision trees".
void BoostMonitor(Int_t iTree)
Fills the ROCIntegral vs Itree from the testSample for the monitoring plots during the training .
Double_t AdaBoostR2(std::vector< const TMVA::Event * > &, DecisionTree *dt)
Adaption of the AdaBoost to regression problems (see H.Drucker 1997).
Double_t PrivateGetMvaValue(const TMVA::Event *ev, Double_t *err=nullptr, Double_t *errUpper=nullptr, UInt_t useNTrees=0)
Return the MVA value (range [-1;1]) that classifies the event according to the majority vote from the...
void SetTuneParameters(std::map< TString, Double_t > tuneParameters) override
Set the tuning parameters according to the argument.
std::map< TString, Double_t > OptimizeTuningParameters(TString fomType="ROCIntegral", TString fitType="FitGA") override
Call the Optimizer with the set of parameters and ranges that are meant to be tuned.
Bool_t HasAnalysisType(Types::EAnalysisType type, UInt_t numberClasses, UInt_t numberTargets) override
BDT can handle classification with multiple classes and regression with one regression-target.
LossFunctionBDT * fRegressionLossFunctionBDTG
void DeterminePreselectionCuts(const std::vector< const TMVA::Event * > &eventSample)
Find useful preselection cuts that will be applied before and Decision Tree training.
Double_t GradBoost(std::vector< const TMVA::Event * > &, DecisionTree *dt, UInt_t cls=0)
Calculate the desired response value for each region.
Double_t AdaCost(std::vector< const TMVA::Event * > &, DecisionTree *dt)
The AdaCost boosting algorithm takes a simple cost Matrix (currently fixed for all events....
void Train(void) override
BDT training.
Double_t Boost(std::vector< const TMVA::Event * > &, DecisionTree *dt, UInt_t cls=0)
Apply the boosting algorithm (the algorithm is selecte via the "option" given in the constructor.
Double_t TestTreeQuality(DecisionTree *dt)
Test the tree quality.. in terms of Misclassification.
Double_t Bagging()
Call it boot-strapping, re-sampling or whatever you like, in the end it is nothing else but applying ...
void ReadWeightsFromStream(std::istream &istr) override
Read the weights (BDT coefficients).
void MakeClassSpecific(std::ostream &, const TString &) const override
Make ROOT-independent C++ class for classifier response (classifier-specific implementation).
void UpdateTargets(std::vector< const TMVA::Event * > &, UInt_t cls=0)
Calculate residual for all events.
void UpdateTargetsRegression(std::vector< const TMVA::Event * > &, Bool_t first=kFALSE)
Calculate residuals for all events and update targets for next iter.
Double_t GradBoostRegression(std::vector< const TMVA::Event * > &, DecisionTree *dt)
Implementation of M_TreeBoost using any loss function as described by Friedman 1999.
virtual ~MethodBDT(void)
Destructor.
Double_t GetGradBoostMVA(const TMVA::Event *e, UInt_t nTrees)
Returns MVA value: -1 for background, 1 for signal.
Double_t RegBoost(std::vector< const TMVA::Event * > &, DecisionTree *dt)
A special boosting only for Regression (not implemented).
void InitEventSample()
Initialize the event sample (i.e. reset the boost-weights... etc).
void DeclareCompatibilityOptions() override
Options that are used ONLY for the READER to ensure backward compatibility.
void WriteMonitoringHistosToFile(void) const override
Here we could write some histograms created during the processing to the output file.
void DeclareOptions() override
Define the options (their key words).
Double_t ApplyPreselectionCuts(const Event *ev)
Apply the preselection cuts before even bothering about any Decision Trees in the GetMVA .
void SetMinNodeSize(Double_t sizeInPercent)
void PreProcessNegativeEventWeights()
O.k.
void ReadWeightsFromXML(void *parent) override
Reads the BDT from the xml file.
void Reset(void) override
Reset the method, as if it had just been instantiated (forget all training etc.).
void GetHelpMessage() const override
Get help message text.
void MakeClassInstantiateNode(DecisionTreeNode *n, std::ostream &fout, const TString &className) const
Recursively descends a tree and writes the node instance to the output stream.
Double_t AdaBoost(std::vector< const TMVA::Event * > &, DecisionTree *dt)
The AdaBoost implementation.
TTree * fMonitorNtuple
monitoring ntuple
std::vector< Double_t > GetVariableImportance()
Return the relative variable importance, normalized to all variables together having the importance 1...
void InitGradBoost(std::vector< const TMVA::Event * > &)
Initialize targets for first tree.
const std::vector< Float_t > & GetRegressionValues() override
Get the regression value generated by the BDTs.
void GetBaggedSubSample(std::vector< const TMVA::Event * > &)
Fills fEventSample with fBaggedSampleFraction*NEvents random training events.
const Ranking * CreateRanking() override
Compute ranking of input variables.
SeparationBase * fSepType
the separation used in node splitting
void ProcessOptions() override
The option string is decoded, for available options see "DeclareOptions".
Virtual base Class for all MVA method.
virtual void DeclareCompatibilityOptions()
options that are used ONLY for the READER to ensure backward compatibility they are hence without any...
Implementation of the MisClassificationError as separation criterion.
std::map< TString, Double_t > optimize()
PDF wrapper for histograms; uses user-defined spline interpolation.
Ranking for variables in method (implementation)
Class that is the base-class for a vector of result.
void Store(TObject *obj, const char *alias=nullptr)
Implementation of the SdivSqrtSplusB as separation criterion.
Timing information for training and evaluation of MVA methods.
Singleton class for Global types used by TMVA.
virtual Int_t Write(const char *name=nullptr, Int_t option=0, Int_t bufsize=0)
Write this object to the current directory.
virtual Int_t Read(const char *name)
Read contents of object with specified name from the current directory.
Random number generator class based on M.
TString & ReplaceAll(const TString &s1, const TString &s2)
TString & Append(const char *cs)
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString.
A TTree represents a columnar dataset.
TSeq< unsigned int > TSeqU
create variable transformations
MsgLogger & Endl(MsgLogger &ml)
Short_t Max(Short_t a, Short_t b)
Returns the largest of a and b.
Double_t Exp(Double_t x)
Returns the base-e exponential function of x, which is e raised to the power x.
Int_t FloorNint(Double_t x)
Returns the nearest integer of TMath::Floor(x).
Double_t Log(Double_t x)
Returns the natural logarithm of x.
Double_t Sqrt(Double_t x)
Returns the square root of x.
LongDouble_t Power(LongDouble_t x, LongDouble_t y)
Returns x raised to the power y.
Int_t CeilNint(Double_t x)
Returns the nearest integer of TMath::Ceil(x).
Short_t Min(Short_t a, Short_t b)
Returns the smallest of a and b.
Short_t Abs(Short_t d)
Returns the absolute value of parameter Short_t d.
static uint64_t sum(uint64_t i)