53 #ifndef ROOT_TMVA_MsgLogger 56 #ifndef ROOT_TMVA_Configurable 59 #ifndef ROOT_TMVA_VariableIdentityTransform 62 #ifndef ROOT_TMVA_VariableDecorrTransform 65 #ifndef ROOT_TMVA_VariablePCATransform 68 #ifndef ROOT_TMVA_DataSet 71 #ifndef ROOT_TMVA_DataSetInfo 74 #ifndef ROOT_TMVA_DataInputHandler 77 #ifndef ROOT_TMVA_Event 93 if (a<b) {
Int_t tmp =
a; a=
b; b=tmp; }
106 fVerboseLevel(TString(
"Info")),
107 fScaleWithPreselEff(0),
120 std::vector<TTreeFormula*>::const_iterator formIt;
160 Log() <<
kDEBUG <<
Form(
"Dataset[%s] : ",dsi.
GetName()) <<
"Build DataSet consisting of one Event with dynamically changing variables" <<
Endl;
170 std::vector<Float_t*>* evdyn =
new std::vector<Float_t*>(0);
174 if (varinfos.empty())
175 Log() <<
kFATAL <<
Form(
"Dataset[%s] : ",dsi.
GetName()) <<
"Dynamic data set cannot be built, since no variable informations are present. Apparently no variables have been set. This should not happen, please contact the TMVA authors." <<
Endl;
177 std::vector<VariableInfo>::iterator it = varinfos.begin(), itEnd=varinfos.end();
178 for (;it!=itEnd;++it) {
181 Log() <<
kDEBUG <<
Form(
"Dataset[%s] : ",dsi.
GetName()) <<
"The link to the external variable is NULL while I am trying to build a dynamic data set. In this case fTmpEvent from MethodBase HAS TO BE USED in the method to get useful values in variables." <<
Endl;
182 else evdyn->push_back (external);
186 it = spectatorinfos.begin();
187 for (;it!=spectatorinfos.end();it++) evdyn->push_back( (
Float_t*)(*it).GetExternalLink() );
189 TMVA::Event * ev =
new Event((
const std::vector<Float_t*>*&)evdyn, varinfos.size());
190 std::vector<Event*>* newEventVector =
new std::vector<Event*>;
191 newEventVector->push_back(ev);
195 ds->SetCurrentEvent( 0 );
213 std::vector< TString >* classList = dataInput.
GetClassList();
214 for (std::vector<TString>::iterator it = classList->begin(); it< classList->end(); it++) {
225 InitOptions( dsi, eventCounts, normMode, splitSeed, splitMode , mixMode );
231 splitMode, mixMode, normMode, splitSeed );
234 if (showCollectedOutput) {
255 const TString& expression,
261 Log() <<
kFATAL <<
"Expression " << expression.Data()
262 <<
" could not be resolved to a valid formula. " <<
Endl;
264 Log() <<
kWARNING <<
"Expression: " << expression.Data()
265 <<
" does not provide data for this event. " 266 <<
"This event is not taken into account. --> please check if you use as a variable " 267 <<
"an entry of an array which is not filled for some events " 268 <<
"(e.g. arr[4] when arr has only 3 elements)." <<
Endl;
269 Log() <<
kWARNING <<
"If you want to take the event into account you can do something like: " 270 <<
"\"Alt$(arr[4],0)\" where in cases where arr doesn't have a 4th element, " 271 <<
" 0 is taken as an alternative." <<
Endl;
274 if( expression.Contains(
"$") )
278 for (
int i = 0, iEnd = ttf->
GetNcodes (); i < iEnd; ++i)
298 TTree *tr = tinfo.
GetTree()->GetTree();
300 tr->SetBranchStatus(
"*",1);
306 std::vector<TTreeFormula*>::const_iterator formIt, formItEnd;
348 for (formIt =
fCutFormulas.begin(), formItEnd =
fCutFormulas.end(); formIt!=formItEnd; formIt++)
if (*formIt)
delete *formIt;
352 const TString tmpCutExp(tmpCut.GetTitle());
381 ttf =
new TTreeFormula(
"FormulaWeight", tmpWeight, tr );
397 tr->SetBranchStatus(
"*",0);
457 for (
UInt_t ivar=0; ivar<nvar ; ivar++) { min[ivar] = FLT_MAX; max[ivar] = -FLT_MAX; }
458 for (
UInt_t ivar=0; ivar<ntgts; ivar++) { tgmin[ivar] = FLT_MAX; tgmax[ivar] = -FLT_MAX; }
459 for (
UInt_t ivar=0; ivar<nvis; ivar++) { vmin[ivar] = FLT_MAX; vmax[ivar] = -FLT_MAX; }
465 for (
UInt_t ivar=0; ivar<nvar; ivar++) {
467 if (v<min[ivar]) min[ivar] =
v;
468 if (v>max[ivar]) max[ivar] =
v;
470 for (
UInt_t itgt=0; itgt<ntgts; itgt++) {
472 if (v<tgmin[itgt]) tgmin[itgt] =
v;
473 if (v>tgmax[itgt]) tgmax[itgt] =
v;
475 for (
UInt_t ivis=0; ivis<nvis; ivis++) {
477 if (v<vmin[ivis]) vmin[ivis] =
v;
478 if (v>vmax[ivis]) vmax[ivis] =
v;
482 for (
UInt_t ivar=0; ivar<nvar; ivar++) {
485 if(
TMath::Abs(max[ivar]-min[ivar]) <= FLT_MIN )
488 for (
UInt_t ivar=0; ivar<ntgts; ivar++) {
491 if(
TMath::Abs(tgmax[ivar]-tgmin[ivar]) <= FLT_MIN )
494 for (
UInt_t ivar=0; ivar<nvis; ivar++) {
521 for (ivar=0; ivar<nvar; ivar++) {
522 for (jvar=0; jvar<nvar; jvar++) {
524 Double_t d = (*mat)(ivar, ivar)*(*mat)(jvar, jvar);
525 if (d > 0) (*mat)(ivar, jvar) /=
sqrt(d);
528 <<
"(" << ivar <<
", " << jvar <<
") = " << d
530 (*mat)(ivar, jvar) = 0;
536 for (ivar=0; ivar<nvar; ivar++) (*mat)(ivar, ivar) = 1.0;
547 UInt_t ivar = 0, jvar = 0;
554 for (ivar=0; ivar<nvar; ivar++) {
556 for (jvar=0; jvar<nvar; jvar++) mat2(ivar, jvar) = 0;
564 if (ev->
GetClass() != classNumber )
continue;
569 for (ivar=0; ivar<nvar; ivar++) {
572 vec(ivar) += xi*weight;
573 mat2(ivar, ivar) += (xi*xi*weight);
575 for (jvar=ivar+1; jvar<nvar; jvar++) {
577 mat2(ivar, jvar) += (xi*xj*weight);
582 for (ivar=0; ivar<nvar; ivar++)
583 for (jvar=ivar+1; jvar<nvar; jvar++)
584 mat2(jvar, ivar) = mat2(ivar, jvar);
588 for (ivar=0; ivar<nvar; ivar++) {
589 for (jvar=0; jvar<nvar; jvar++) {
590 (*mat)(ivar, jvar) = mat2(ivar, jvar)/ic - vec(ivar)*vec(jvar)/(ic*ic);
612 splitSpecs.SetConfigDescription(
"Configuration options given in the \"PrepareForTrainingAndTesting\" call; these options define the creation of the data sets used for training and expert validation by TMVA" );
614 splitMode =
"Random";
615 splitSpecs.DeclareOptionRef( splitMode,
"SplitMode",
616 "Method of picking training and testing events (default: random)" );
617 splitSpecs.AddPreDefVal(TString(
"Random"));
618 splitSpecs.AddPreDefVal(TString(
"Alternate"));
619 splitSpecs.AddPreDefVal(TString(
"Block"));
621 mixMode =
"SameAsSplitMode";
622 splitSpecs.DeclareOptionRef( mixMode,
"MixMode",
623 "Method of mixing events of differnt classes into one dataset (default: SameAsSplitMode)" );
624 splitSpecs.AddPreDefVal(TString(
"SameAsSplitMode"));
625 splitSpecs.AddPreDefVal(TString(
"Random"));
626 splitSpecs.AddPreDefVal(TString(
"Alternate"));
627 splitSpecs.AddPreDefVal(TString(
"Block"));
630 splitSpecs.DeclareOptionRef( splitSeed,
"SplitSeed",
631 "Seed for random event shuffling" );
633 normMode =
"EqualNumEvents";
634 splitSpecs.DeclareOptionRef( normMode,
"NormMode",
635 "Overall renormalisation of event-by-event weights used in the training (NumEvents: average weight of 1 per event, independently for signal and background; EqualNumEvents: average weight of 1 per event for signal, and sum of weights for background equal to sum of weights for signal)" );
636 splitSpecs.AddPreDefVal(TString(
"None"));
637 splitSpecs.AddPreDefVal(TString(
"NumEvents"));
638 splitSpecs.AddPreDefVal(TString(
"EqualNumEvents"));
640 splitSpecs.DeclareOptionRef(
fScaleWithPreselEff=
kFALSE,
"ScaleWithPreselEff",
"Scale the number of requested events by the eff. of the preselection cuts (or not)" );
647 TString titleTrain = TString().Format(
"Number of training events of class %s (default: 0 = all)",clName.Data()).
Data();
648 TString titleTest = TString().Format(
"Number of test events of class %s (default: 0 = all)",clName.Data()).
Data();
649 TString titleSplit = TString().Format(
"Split in training and test events of class %s (default: 0 = deactivated)",clName.Data()).
Data();
651 splitSpecs.DeclareOptionRef( nEventRequests.at(cl).nTrainingEventsRequested, TString(
"nTrain_")+clName, titleTrain );
652 splitSpecs.DeclareOptionRef( nEventRequests.at(cl).nTestingEventsRequested , TString(
"nTest_")+clName , titleTest );
653 splitSpecs.DeclareOptionRef( nEventRequests.at(cl).TrainTestSplitRequested , TString(
"TrainTestSplit_")+clName , titleTest );
656 splitSpecs.DeclareOptionRef(
fVerbose,
"V",
"Verbosity (default: true)" );
658 splitSpecs.DeclareOptionRef(
fVerboseLevel=TString(
"Info"),
"VerboseLevel",
"VerboseLevel (Debug/Verbose/Info)" );
659 splitSpecs.AddPreDefVal(TString(
"Debug"));
660 splitSpecs.AddPreDefVal(TString(
"Verbose"));
661 splitSpecs.AddPreDefVal(TString(
"Info"));
663 splitSpecs.ParseOptions();
664 splitSpecs.CheckForUnusedOptions();
673 splitMode.ToUpper(); mixMode.ToUpper(); normMode.ToUpper();
676 <<
"\tSplitmode is: \"" << splitMode <<
"\" the mixmode is: \"" << mixMode <<
"\"" <<
Endl;
677 if (mixMode==
"SAMEASSPLITMODE") mixMode = splitMode;
678 else if (mixMode!=splitMode)
680 <<
" differs from mixmode="<<mixMode<<Endl;
705 for (
size_t i=0; i<nclasses; i++) {
706 eventCounts[i].varAvLength =
new Float_t[nvars];
707 for (
UInt_t ivar=0; ivar<nvars; ivar++)
708 eventCounts[i].varAvLength[ivar] = 0;
716 for (
UInt_t cl=0; cl<nclasses; cl++) {
720 EventStats& classEventCounts = eventCounts[cl];
728 TString currentFileName(
"");
734 std::vector<Float_t> vars(nvars);
735 std::vector<Float_t> tgts(ntgts);
736 std::vector<Float_t> vis(nvis);
743 Bool_t isChain = (TString(
"TChain") == currentInfo.
GetTree()->ClassName());
744 currentInfo.
GetTree()->LoadTree(0);
752 for (
Long64_t evtIdx = 0; evtIdx < nEvts; evtIdx++) {
753 currentInfo.
GetTree()->LoadTree(evtIdx);
757 if (currentInfo.
GetTree()->GetTree()->GetDirectory()->GetFile()->GetName() != currentFileName) {
758 currentFileName = currentInfo.
GetTree()->GetTree()->GetDirectory()->GetFile()->GetName();
762 currentInfo.
GetTree()->GetEntry(evtIdx);
763 Int_t sizeOfArrays = 1;
764 Int_t prevArrExpr = 0;
769 for (
UInt_t ivar=0; ivar<nvars; ivar++) {
772 if (ndata == 1)
continue;
774 varIsArray[ivar] =
kTRUE;
775 if (sizeOfArrays == 1) {
776 sizeOfArrays =
ndata;
779 else if (sizeOfArrays!=ndata) {
780 Log() <<
kERROR <<
Form(
"Dataset[%s] : ",dsi.
GetName())<<
"ERROR while preparing training and testing trees:" <<
Endl;
781 Log() <<
Form(
"Dataset[%s] : ",dsi.
GetName())<<
" multiple array-type expressions of different length were encountered" <<
Endl;
782 Log() <<
Form(
"Dataset[%s] : ",dsi.
GetName())<<
" location of error: event " << evtIdx
783 <<
" in tree " << currentInfo.
GetTree()->GetName()
784 <<
" of file " << currentInfo.
GetTree()->GetCurrentFile()->GetName() <<
Endl;
786 <<
Form(
"Dataset[%s] : ",dsi.
GetName()) << ndata <<
" entries, while" <<
Endl;
794 for (
Int_t idata = 0; idata<sizeOfArrays; idata++) {
809 Log() <<
kWARNING <<
Form(
"Dataset[%s] : ",dsi.
GetName())<<
"Cut expression resolves to infinite value (NaN): " 815 for (
UInt_t ivar=0; ivar<nvars; ivar++) {
818 vars[ivar] = (ndata == 1 ?
823 Log() <<
kWARNING <<
Form(
"Dataset[%s] : ",dsi.
GetName())<<
"Input expression resolves to infinite value (NaN): " 829 for (
UInt_t itrgt=0; itrgt<ntgts; itrgt++) {
832 tgts[itrgt] = (ndata == 1 ?
837 Log() <<
kWARNING <<
Form(
"Dataset[%s] : ",dsi.
GetName())<<
"Target expression resolves to infinite value (NaN): " 843 for (
UInt_t itVis=0; itVis<nvis; itVis++) {
846 vis[itVis] = (ndata == 1 ?
851 Log() <<
kWARNING <<
Form(
"Dataset[%s] : ",dsi.
GetName())<<
"Spectator expression resolves to infinite value (NaN): " 862 weight *= (ndata == 1 ?
867 Log() <<
kWARNING <<
Form(
"Dataset[%s] : ",dsi.
GetName())<<
"Weight expression resolves to infinite value (NaN): " 879 if (cutVal<0.5)
continue;
900 event_v.push_back(
new Event(vars, tgts , vis, cl , weight));
903 currentInfo.
GetTree()->ResetBranchAddresses();
911 Log() <<
kDEBUG <<
"(after possible flattening of arrays):" <<
Endl;
918 <<
" -- number of events : " 919 << std::setw(5) << eventCounts[cl].nEvBeforeCut
920 <<
" / sum of weights: " << std::setw(5) << eventCounts[cl].nWeEvBeforeCut <<
Endl;
926 <<
" tree -- total number of entries: " 932 <<
"\tPreselection: (will affect number of requested training and testing events)" <<
Endl;
935 <<
"\tPreselection: (will NOT affect number of requested training and testing events)" <<
Endl;
943 <<
" -- number of events passed: " 944 << std::setw(5) << eventCounts[cl].nEvAfterCut
945 <<
" / sum of weights: " << std::setw(5) << eventCounts[cl].nWeEvAfterCut <<
Endl;
948 <<
" -- efficiency : " 949 << std::setw(6) << eventCounts[cl].nWeEvAfterCut/eventCounts[cl].nWeEvBeforeCut <<
Endl;
953 <<
" No preselection cuts applied on event classes" <<
Endl;
967 const TString& splitMode,
968 const TString& mixMode,
969 const TString& normMode,
982 if (splitMode.Contains(
"RANDOM" ) ) {
986 if( ! unspecifiedEvents.empty() ) {
988 << unspecifiedEvents.size()
989 <<
" events of class " << cls
990 <<
" which are not yet associated to testing or training" <<
Endl;
991 std::random_shuffle( unspecifiedEvents.begin(),
992 unspecifiedEvents.end(),
1002 Log() <<
kDEBUG <<
Form(
"Dataset[%s] : ",dsi.
GetName())<<
"check number of training/testing events, requested and available number of events and for class " << cls <<
Endl;
1009 Int_t availableTraining = eventVectorTraining.size();
1010 Int_t availableTesting = eventVectorTesting.size();
1011 Int_t availableUndefined = eventVectorUndefined.size();
1015 presel_scale = eventCounts[cls].cutScaling();
1016 if (presel_scale < 1)
1017 Log() <<
kINFO <<
Form(
"Dataset[%s] : ",dsi.
GetName()) <<
" you have opted for scaling the number of requested training/testing events\n to be scaled by the preselection efficiency"<<
Endl;
1020 if (eventCounts[cls].cutScaling() < 1)
1021 Log() <<
kINFO <<
Form(
"Dataset[%s] : ",dsi.
GetName()) <<
" you have opted for interpreting the requested number of training/testing events\n to be the number of events AFTER your preselection cuts" <<
Endl;
1028 if(eventCounts[cls].TrainTestSplitRequested < 1.0 && eventCounts[cls].TrainTestSplitRequested > 0.0){
1029 eventCounts[cls].nTrainingEventsRequested =
Int_t(eventCounts[cls].TrainTestSplitRequested*(availableTraining+availableTesting+availableUndefined));
1030 eventCounts[cls].nTestingEventsRequested =
Int_t(0);
1032 else if(eventCounts[cls].TrainTestSplitRequested != 0.0)
Log() <<
kFATAL <<
Form(
"The option TrainTestSplit_<class> has to be in range (0, 1] but is set to %f.",eventCounts[cls].TrainTestSplitRequested) <<
Endl;
1033 Int_t requestedTraining =
Int_t(eventCounts[cls].nTrainingEventsRequested * presel_scale);
1034 Int_t requestedTesting =
Int_t(eventCounts[cls].nTestingEventsRequested * presel_scale);
1036 Log() <<
kDEBUG <<
Form(
"Dataset[%s] : ",dsi.
GetName())<<
"events in training trees : " << availableTraining << Endl;
1037 Log() <<
kDEBUG <<
Form(
"Dataset[%s] : ",dsi.
GetName())<<
"events in testing trees : " << availableTesting << Endl;
1038 Log() <<
kDEBUG <<
Form(
"Dataset[%s] : ",dsi.
GetName())<<
"events in unspecified trees : " << availableUndefined << Endl;
1039 Log() <<
kDEBUG <<
Form(
"Dataset[%s] : ",dsi.
GetName())<<
"requested for training : " << requestedTraining << Endl;;
1042 Log() <<
" ( " << eventCounts[cls].nTrainingEventsRequested
1043 <<
" * " << presel_scale <<
" preselection efficiency)" <<
Endl;
1046 Log() <<
kDEBUG <<
"requested for testing : " << requestedTesting;
1048 Log() <<
" ( " << eventCounts[cls].nTestingEventsRequested
1049 <<
" * " << presel_scale <<
" preselection efficiency)" <<
Endl;
1100 Int_t useForTesting(0),useForTraining(0);
1101 Int_t allAvailable(availableUndefined + availableTraining + availableTesting);
1103 if( (requestedTraining == 0) && (requestedTesting == 0)){
1107 if ( availableUndefined >=
TMath::Abs(availableTraining - availableTesting) ) {
1109 useForTraining = useForTesting = allAvailable/2;
1112 useForTraining = availableTraining;
1113 useForTesting = availableTesting;
1114 if (availableTraining < availableTesting)
1115 useForTraining += availableUndefined;
1117 useForTesting += availableUndefined;
1119 requestedTraining = useForTraining;
1120 requestedTesting = useForTesting;
1123 else if (requestedTesting == 0){
1125 useForTraining =
TMath::Max(requestedTraining,availableTraining);
1126 if (allAvailable < useForTraining) {
1127 Log() <<
kFATAL <<
Form(
"Dataset[%s] : ",dsi.
GetName())<<
"More events requested for training (" 1128 << requestedTraining <<
") than available (" 1129 << allAvailable <<
")!" << Endl;
1131 useForTesting = allAvailable - useForTraining;
1132 requestedTesting = useForTesting;
1135 else if (requestedTraining == 0){
1136 useForTesting =
TMath::Max(requestedTesting,availableTesting);
1137 if (allAvailable < useForTesting) {
1138 Log() <<
kFATAL <<
Form(
"Dataset[%s] : ",dsi.
GetName())<<
"More events requested for testing (" 1139 << requestedTesting <<
") than available (" 1140 << allAvailable <<
")!" << Endl;
1142 useForTraining= allAvailable - useForTesting;
1143 requestedTraining = useForTraining;
1152 Int_t stillNeedForTraining =
TMath::Max(requestedTraining-availableTraining,0);
1153 Int_t stillNeedForTesting =
TMath::Max(requestedTesting-availableTesting,0);
1155 int NFree = availableUndefined - stillNeedForTraining - stillNeedForTesting;
1156 if (NFree <0) NFree = 0;
1157 useForTraining =
TMath::Max(requestedTraining,availableTraining) + NFree/2;
1158 useForTesting= allAvailable - useForTraining;
1161 Log() <<
kDEBUG <<
Form(
"Dataset[%s] : ",dsi.
GetName())<<
"determined event sample size to select training sample from="<<useForTraining<<Endl;
1162 Log() <<
kDEBUG <<
Form(
"Dataset[%s] : ",dsi.
GetName())<<
"determined event sample size to select test sample from="<<useForTesting<<Endl;
1167 if( splitMode ==
"ALTERNATE" ){
1169 Int_t nTraining = availableTraining;
1170 Int_t nTesting = availableTesting;
1171 for( EventVector::iterator it = eventVectorUndefined.begin(), itEnd = eventVectorUndefined.end(); it != itEnd; ){
1173 if( nTraining <= requestedTraining ){
1174 eventVectorTraining.insert( eventVectorTraining.end(), (*it) );
1179 eventVectorTesting.insert( eventVectorTesting.end(), (*it) );
1184 Log() <<
kDEBUG <<
Form(
"Dataset[%s] : ",dsi.
GetName())<<
"split '" << splitMode <<
"'" << Endl;
1187 Log() <<
kDEBUG <<
Form(
"Dataset[%s] : ",dsi.
GetName())<<
"availableundefined : " << availableUndefined << Endl;
1188 Log() <<
kDEBUG <<
Form(
"Dataset[%s] : ",dsi.
GetName())<<
"useForTraining : " << useForTraining << Endl;
1189 Log() <<
kDEBUG <<
Form(
"Dataset[%s] : ",dsi.
GetName())<<
"useForTesting : " << useForTesting << Endl;
1190 Log() <<
kDEBUG <<
Form(
"Dataset[%s] : ",dsi.
GetName())<<
"availableTraining : " << availableTraining << Endl;
1191 Log() <<
kDEBUG <<
Form(
"Dataset[%s] : ",dsi.
GetName())<<
"availableTesting : " << availableTesting << Endl;
1193 if( availableUndefined<(useForTraining-availableTraining) ||
1194 availableUndefined<(useForTesting -availableTesting ) ||
1195 availableUndefined<(useForTraining+useForTesting-availableTraining-availableTesting ) ){
1196 Log() <<
kFATAL <<
Form(
"Dataset[%s] : ",dsi.
GetName())<<
"More events requested than available!" << Endl;
1200 if (useForTraining>availableTraining){
1201 eventVectorTraining.insert( eventVectorTraining.end() , eventVectorUndefined.begin(), eventVectorUndefined.begin()+ useForTraining- availableTraining );
1202 eventVectorUndefined.erase( eventVectorUndefined.begin(), eventVectorUndefined.begin() + useForTraining- availableTraining);
1204 if (useForTesting>availableTesting){
1205 eventVectorTesting.insert( eventVectorTesting.end() , eventVectorUndefined.begin(), eventVectorUndefined.begin()+ useForTesting- availableTesting );
1208 eventVectorUndefined.clear();
1211 if (splitMode.Contains(
"RANDOM" )){
1212 UInt_t sizeTraining = eventVectorTraining.size();
1213 if( sizeTraining >
UInt_t(requestedTraining) ){
1214 std::vector<UInt_t> indicesTraining( sizeTraining );
1218 std::random_shuffle( indicesTraining.begin(), indicesTraining.end(), rndm );
1220 indicesTraining.erase( indicesTraining.begin()+sizeTraining-
UInt_t(requestedTraining), indicesTraining.end() );
1222 for( std::vector<UInt_t>::iterator it = indicesTraining.begin(), itEnd = indicesTraining.end(); it != itEnd; ++it ){
1223 delete eventVectorTraining.at( (*it) );
1224 eventVectorTraining.at( (*it) ) =
NULL;
1227 eventVectorTraining.erase( std::remove( eventVectorTraining.begin(), eventVectorTraining.end(), (
void*)
NULL ), eventVectorTraining.end() );
1230 UInt_t sizeTesting = eventVectorTesting.size();
1231 if( sizeTesting >
UInt_t(requestedTesting) ){
1232 std::vector<UInt_t> indicesTesting( sizeTesting );
1236 std::random_shuffle( indicesTesting.begin(), indicesTesting.end(), rndm );
1238 indicesTesting.erase( indicesTesting.begin()+sizeTesting-
UInt_t(requestedTesting), indicesTesting.end() );
1240 for( std::vector<UInt_t>::iterator it = indicesTesting.begin(), itEnd = indicesTesting.end(); it != itEnd; ++it ){
1241 delete eventVectorTesting.at( (*it) );
1242 eventVectorTesting.at( (*it) ) =
NULL;
1245 eventVectorTesting.erase( std::remove( eventVectorTesting.begin(), eventVectorTesting.end(), (
void*)
NULL ), eventVectorTesting.end() );
1249 if( eventVectorTraining.size() <
UInt_t(requestedTraining) )
1250 Log() <<
kWARNING <<
Form(
"Dataset[%s] : ",dsi.
GetName())<<
"DataSetFactory/requested number of training samples larger than size of eventVectorTraining.\n" 1251 <<
"There is probably an issue. Please contact the TMVA developers." << Endl;
1252 std::for_each( eventVectorTraining.begin()+requestedTraining, eventVectorTraining.end(), DeleteFunctor<Event>() );
1253 eventVectorTraining.erase(eventVectorTraining.begin()+requestedTraining,eventVectorTraining.end());
1255 if( eventVectorTesting.size() <
UInt_t(requestedTesting) )
1256 Log() <<
kWARNING <<
Form(
"Dataset[%s] : ",dsi.
GetName())<<
"DataSetFactory/requested number of testing samples larger than size of eventVectorTesting.\n" 1257 <<
"There is probably an issue. Please contact the TMVA developers." << Endl;
1258 std::for_each( eventVectorTesting.begin()+requestedTesting, eventVectorTesting.end(), DeleteFunctor<Event>() );
1259 eventVectorTesting.erase(eventVectorTesting.begin()+requestedTesting,eventVectorTesting.end());
1265 Int_t trainingSize = 0;
1266 Int_t testingSize = 0;
1280 trainingEventVector->reserve( trainingSize );
1281 testingEventVector->reserve( testingSize );
1289 if( mixMode ==
"ALTERNATE" ){
1294 Log() <<
kINFO <<
Form(
"Dataset[%s] : ",dsi.
GetName()) <<
"Training sample: You are trying to mix events in alternate mode although the classes have different event numbers. This works but the alternation stops at the last event of the smaller class."<<Endl;
1297 Log() <<
kINFO <<
Form(
"Dataset[%s] : ",dsi.
GetName()) <<
"Testing sample: You are trying to mix events in alternate mode although the classes have different event numbers. This works but the alternation stops at the last event of the smaller class."<<Endl;
1300 typedef EventVector::iterator EvtVecIt;
1301 EvtVecIt itEvent, itEventEnd;
1304 Log() <<
kDEBUG <<
"insert class 0 into training and test vector" <<
Endl;
1306 testingEventVector->insert( testingEventVector->end(), tmpEventVector[
Types::kTesting].at(0).begin(), tmpEventVector[
Types::kTesting].at(0).end() );
1313 itTarget = trainingEventVector->begin() - 1;
1315 for( itEvent = tmpEventVector[
Types::kTraining].at(cls).begin(), itEventEnd = tmpEventVector[
Types::kTraining].at(cls).end(); itEvent != itEventEnd; ++itEvent ){
1317 if( (trainingEventVector->end() - itTarget) <
Int_t(cls+1) ) {
1318 itTarget = trainingEventVector->end();
1319 trainingEventVector->insert( itTarget, itEvent, itEventEnd );
1323 trainingEventVector->insert( itTarget, (*itEvent) );
1327 itTarget = testingEventVector->begin() - 1;
1329 for( itEvent = tmpEventVector[
Types::kTesting].at(cls).begin(), itEventEnd = tmpEventVector[
Types::kTesting].at(cls).end(); itEvent != itEventEnd; ++itEvent ){
1331 if( ( testingEventVector->end() - itTarget ) <
Int_t(cls+1) ) {
1332 itTarget = testingEventVector->end();
1333 testingEventVector->insert( itTarget, itEvent, itEventEnd );
1337 testingEventVector->insert( itTarget, (*itEvent) );
1354 trainingEventVector->insert( trainingEventVector->end(), tmpEventVector[
Types::kTraining].at(cls).begin(), tmpEventVector[
Types::kTraining].at(cls).end() );
1355 testingEventVector->insert ( testingEventVector->end(), tmpEventVector[
Types::kTesting].at(cls).begin(), tmpEventVector[
Types::kTesting].at(cls).end() );
1371 if (mixMode ==
"RANDOM") {
1374 std::random_shuffle( trainingEventVector->begin(), trainingEventVector->end(), rndm );
1375 std::random_shuffle( testingEventVector->begin(), testingEventVector->end(), rndm );
1378 Log() <<
kDEBUG <<
Form(
"Dataset[%s] : ",dsi.
GetName())<<
"trainingEventVector " << trainingEventVector->size() <<
Endl;
1379 Log() <<
kDEBUG <<
Form(
"Dataset[%s] : ",dsi.
GetName())<<
"testingEventVector " << testingEventVector->size() <<
Endl;
1391 Log() <<
kFATAL <<
"Dataset " << std::string(dsi.
GetName()) <<
" does not have any training events, I better stop here and let you fix that one first " << Endl;
1395 Log() <<
kERROR <<
"Dataset " << std::string(dsi.
GetName()) <<
" does not have any testing events, guess that will cause problems later..but for now, I continue " << Endl;
1417 const TString& normMode )
1424 Int_t trainingSize = 0;
1425 Int_t testingSize = 0;
1433 Double_t trainingSumSignalWeights = 0;
1434 Double_t trainingSumBackgrWeights = 0;
1435 Double_t testingSumSignalWeights = 0;
1436 Double_t testingSumBackgrWeights = 0;
1441 trainingSizePerClass.at(cls) = tmpEventVector[
Types::kTraining].at(cls).size();
1442 testingSizePerClass.at(cls) = tmpEventVector[
Types::kTesting].at(cls).size();
1444 trainingSize += trainingSizePerClass.back();
1445 testingSize += testingSizePerClass.back();
1459 trainingSumWeightsPerClass.at(cls) = std::accumulate( tmpEventVector[
Types::kTraining].at(cls).begin(),
1466 testingSumWeightsPerClass.at(cls) = std::accumulate( tmpEventVector[
Types::kTesting].at(cls).begin(),
1474 trainingSumSignalWeights += trainingSumWeightsPerClass.at(cls);
1475 testingSumSignalWeights += testingSumWeightsPerClass.at(cls);
1477 trainingSumBackgrWeights += trainingSumWeightsPerClass.at(cls);
1478 testingSumBackgrWeights += testingSumWeightsPerClass.at(cls);
1498 if (normMode ==
"NONE") {
1499 Log() <<
kINFO <<
Form(
"Dataset[%s] : ",dsi.
GetName()) <<
"No weight renormalisation applied: use original global and event weights" <<
Endl;
1505 else if (normMode ==
"NUMEVENTS") {
1507 <<
"\tWeight renormalisation mode: \"NumEvents\": renormalises all event classes " <<
Endl;
1509 <<
" such that the effective (weighted) number of events in each class equals the respective " <<
Endl;
1511 <<
" number of events (entries) that you demanded in PrepareTrainingAndTestTree(\"\",\"nTrain_Signal=.. )" <<
Endl;
1513 <<
" ... i.e. such that Sum[i=1..N_j]{w_i} = N_j, j=0,1,2..." <<
Endl;
1515 <<
" ... (note that N_j is the sum of TRAINING events (nTrain_j...with j=Signal,Background.." <<
Endl;
1517 <<
" ..... Testing events are not renormalised nor included in the renormalisation factor! )"<<
Endl;
1523 renormFactor.at(cls) = ((
Float_t)trainingSizePerClass.at(cls) )/
1524 (trainingSumWeightsPerClass.at(cls)) ;
1527 else if (normMode ==
"EQUALNUMEVENTS") {
1533 Log() <<
kINFO <<
Form(
"Dataset[%s] : ",dsi.
GetName()) <<
"Weight renormalisation mode: \"EqualNumEvents\": renormalises all event classes ..." <<
Endl;
1534 Log() <<
kINFO <<
Form(
"Dataset[%s] : ",dsi.
GetName()) <<
" such that the effective (weighted) number of events in each class is the same " <<
Endl;
1535 Log() <<
kINFO <<
Form(
"Dataset[%s] : ",dsi.
GetName()) <<
" (and equals the number of events (entries) given for class=0 )" <<
Endl;
1536 Log() <<
kINFO <<
Form(
"Dataset[%s] : ",dsi.
GetName()) <<
"... i.e. such that Sum[i=1..N_j]{w_i} = N_classA, j=classA, classB, ..." <<
Endl;
1537 Log() <<
kINFO <<
Form(
"Dataset[%s] : ",dsi.
GetName()) <<
"... (note that N_j is the sum of TRAINING events" <<
Endl;
1538 Log() <<
kINFO <<
Form(
"Dataset[%s] : ",dsi.
GetName()) <<
" ..... Testing events are not renormalised nor included in the renormalisation factor!)" <<
Endl;
1541 UInt_t referenceClass = 0;
1543 renormFactor.at(cls) =
Float_t(trainingSizePerClass.at(referenceClass))/
1544 (trainingSumWeightsPerClass.at(cls));
1548 Log() <<
kFATAL <<
Form(
"Dataset[%s] : ",dsi.
GetName())<<
"<PrepareForTrainingAndTesting> Unknown NormMode: " << normMode <<
Endl;
1556 <<
"--> Rescale " << setiosflags(ios::left) << std::setw(maxL)
1558 for (EventVector::iterator it = tmpEventVector[
Types::kTraining].at(cls).begin(),
1559 itEnd = tmpEventVector[
Types::kTraining].at(cls).end(); it != itEnd; ++it){
1560 (*it)->SetWeight ((*it)->GetWeight() * renormFactor.at(cls));
1571 <<
"Number of training and testing events" <<
Endl;
1574 <<
"---------------------------------------------------------------------------" <<
Endl;
1576 trainingSumSignalWeights = 0;
1577 trainingSumBackgrWeights = 0;
1578 testingSumSignalWeights = 0;
1579 testingSumBackgrWeights = 0;
1583 trainingSumWeightsPerClass.at(cls) = (std::accumulate( tmpEventVector[
Types::kTraining].at(cls).begin(),
1590 testingSumWeightsPerClass.at(cls) = std::accumulate( tmpEventVector[
Types::kTesting].at(cls).begin(),
1599 trainingSumSignalWeights += trainingSumWeightsPerClass.at(cls);
1600 testingSumSignalWeights += testingSumWeightsPerClass.at(cls);
1602 trainingSumBackgrWeights += trainingSumWeightsPerClass.at(cls);
1603 testingSumBackgrWeights += testingSumWeightsPerClass.at(cls);
1609 << setiosflags(ios::left) << std::setw(maxL)
1611 <<
"training events : " << trainingSizePerClass.at(cls) <<
Endl;
1612 Log() <<
kDEBUG <<
"\t(sum of weights: " << trainingSumWeightsPerClass.at(cls) <<
")" 1613 <<
" - requested were " << eventCounts[cls].nTrainingEventsRequested <<
" events" <<
Endl;
1615 << setiosflags(ios::left) << std::setw(maxL)
1617 <<
"testing events : " << testingSizePerClass.at(cls) <<
Endl;
1618 Log() <<
kDEBUG <<
"\t(sum of weights: " << testingSumWeightsPerClass.at(cls) <<
")" 1619 <<
" - requested were " << eventCounts[cls].nTestingEventsRequested <<
" events" <<
Endl;
1621 << setiosflags(ios::left) << std::setw(maxL)
1623 <<
"training and testing events: " 1624 << (trainingSizePerClass.at(cls)+testingSizePerClass.at(cls)) << Endl;
1625 Log() <<
kDEBUG <<
"\t(sum of weights: " 1626 << (trainingSumWeightsPerClass.at(cls)+testingSumWeightsPerClass.at(cls)) <<
")" << Endl;
1627 if(eventCounts[cls].nEvAfterCut<eventCounts[cls].nEvBeforeCut) {
1628 Log() <<
kINFO <<
Form(
"Dataset[%s] : ",dsi.
GetName()) << setiosflags(ios::left) << std::setw(maxL)
1630 <<
"due to the preselection a scaling factor has been applied to the numbers of requested events: " 1631 << eventCounts[cls].cutScaling() <<
Endl;
virtual const char * GetName() const
Returns name of object.
A TLeaf describes individual elements of a TBranch See TBranch structure in TTree.
UInt_t GetNVariables() const
std::vector< EventVector > EventVectorOfClasses
void SetTrainingSumBackgrWeights(Double_t trainingSumBackgrWeights)
MsgLogger & Endl(MsgLogger &ml)
const TString & GetInternalName() const
std::vector< VariableInfo > & GetSpectatorInfos()
std::vector< TTreeFormula * > fInputFormulas
std::vector< TTreeFormula * > fCutFormulas
std::vector< Double_t > ValuePerClass
UInt_t GetNVariables() const
access the number of variables through the datasetinfo
void SetTrainingSumSignalWeights(Double_t trainingSumSignalWeights)
void SetTestingSumBackgrWeights(Double_t testingSumBackgrWeights)
void BuildEventVector(DataSetInfo &dsi, DataInputHandler &dataInput, EventVectorOfClassesOfTreeType &eventsmap, EvtStatsPerClass &eventCounts)
build empty event vectors distributes events between kTraining/kTesting/kMaxTreeType ...
void generate(R &r, TH1D *h)
UInt_t GetNClasses() const
void CalcMinMax(DataSet *, DataSetInfo &dsi)
compute covariance matrix
const TString & GetExpression() const
std::vector< int > NumberPerClass
std::map< Types::ETreeType, EventVectorOfClasses > EventVectorOfClassesOfTreeType
void InitOptions(DataSetInfo &dsi, EvtStatsPerClass &eventsmap, TString &normMode, UInt_t &splitSeed, TString &splitMode, TString &mixMode)
the dataset splitting
DataSet * BuildDynamicDataSet(DataSetInfo &)
UInt_t GetNSpectators() const
access the number of targets through the datasetinfo
MsgLogger & Log() const
message logger
std::vector< TTreeFormula * > fWeightFormula
void SetTestingSumSignalWeights(Double_t testingSumSignalWeights)
std::vector< std::vector< double > > Data
void PrintCorrelationMatrix(const TString &className)
calculates the correlation matrices for signal and background, prints them to standard output...
void SetMinType(EMsgType minType)
void RenormEvents(DataSetInfo &dsi, EventVectorOfClassesOfTreeType &eventsmap, const EvtStatsPerClass &eventCounts, const TString &normMode)
renormalisation of the TRAINING event weights -none (kind of obvious) .
Double_t GetWeight() const
return the event weight - depending on whether the flag IgnoreNegWeightsInTraining is or not...
Long64_t GetNTrainingEvents() const
void ChangeToNewTree(TreeInfo &, const DataSetInfo &)
While the data gets copied into the local training and testing trees, the input tree can change (for ...
TMatrixT< Double_t > TMatrixD
void SetCorrelationMatrix(const TString &className, TMatrixD *matrix)
DataSetFactory()
constructor
TMatrixD * CalcCorrelationMatrix(DataSet *, const UInt_t classNumber)
computes correlation matrix for variables "theVars" in tree; "theType" defines the required event "ty...
Float_t GetTarget(UInt_t itgt) const
Int_t LargestCommonDivider(Int_t a, Int_t b)
UInt_t GetNTargets() const
Bool_t fScaleWithPreselEff
Types::ETreeType GetTreeType() const
ClassInfo * GetClassInfo(Int_t clNum) const
std::vector< TTreeFormula * > fSpectatorFormulas
DataSet * CreateDataSet(DataSetInfo &, DataInputHandler &)
steering the creation of a new dataset
VariableInfo & GetTargetInfo(Int_t i)
char * Form(const char *fmt,...)
UInt_t GetNSpectators(bool all=kTRUE) const
UInt_t GetSignalClassIndex()
std::vector< TTreeFormula * > fTargetFormulas
std::vector< Event *> EventVector
Long64_t GetNTestEvents() const
Float_t GetValue(UInt_t ivar) const
return value of i'th variable
DataSet * BuildInitialDataSet(DataSetInfo &, TMVA::DataInputHandler &)
if no entries, than create a DataSet with one Event which uses dynamic variables (pointers to variabl...
~DataSetFactory()
destructor
const TString & GetClassName() const
VariableInfo & GetSpectatorInfo(Int_t i)
compose_binary_t< F, G, H > compose_binary(const F &_f, const G &_g, const H &_h)
void SetEventCollection(std::vector< Event *> *, Types::ETreeType, Bool_t deleteEvents=true)
Sets the event collection (by DataSetFactory)
VariableInfo & GetVariableInfo(Int_t i)
ClassInfo * AddClass(const TString &className)
Int_t GetClassNameMaxLength() const
virtual const char * GetName() const
Returns name of object.
Long64_t GetNClassEvents(Int_t type, UInt_t classNumber)
void SetConfigName(const char *n)
virtual const char * GetTitle() const
Returns title of object.
Abstract ClassifierFactory template that handles arbitrary types.
const TCut & GetCut() const
std::vector< EventStats > EvtStatsPerClass
const TString & GetSplitOptions() const
Short_t Max(Short_t a, Short_t b)
Double_t GetOriginalWeight() const
Bool_t CheckTTreeFormula(TTreeFormula *ttf, const TString &expression, Bool_t &hasDollar)
checks a TTreeFormula for problems
void SetNumber(const UInt_t index)
you should not use this method at all Int_t Int_t Double_t Double_t Double_t Int_t Double_t Double_t Double_t Double_t b
Long64_t GetNEvents(Types::ETreeType type=Types::kMaxTreeType) const
const TString & GetWeight() const
DataSet * MixEvents(DataSetInfo &dsi, EventVectorOfClassesOfTreeType &eventsmap, EvtStatsPerClass &eventCounts, const TString &splitMode, const TString &mixMode, const TString &normMode, UInt_t splitSeed)
Select and distribute unassigned events to kTraining and kTesting Bool_t emptyUndefined = kTRUE;...
TBranch * GetBranch() const
virtual const char * GetName() const
Returns name of object.
virtual Bool_t IsOnTerminalBranch() const
Double_t GetWeight() const
UInt_t GetNTargets() const
access the number of targets through the datasetinfo
Float_t GetSpectator(UInt_t ivar) const
return spectator content
void SetNormalization(const TString &norm)
TMatrixD * CalcCovarianceMatrix(DataSet *, const UInt_t classNumber)
compute covariance matrix
const Event * GetEvent() const
std::vector< VariableInfo > & GetVariableInfos()
virtual const char * GetTitle() const
Returns title of object.