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) {
262 <<
" could not be resolved to a valid formula. " <<
Endl;
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;
278 for (
int i = 0, iEnd = ttf->
GetNcodes (); i < iEnd; ++i)
306 std::vector<TTreeFormula*>::const_iterator formIt, formItEnd;
348 for (formIt =
fCutFormulas.begin(), formItEnd =
fCutFormulas.end(); formIt!=formItEnd; formIt++)
if (*formIt)
delete *formIt;
381 ttf =
new TTreeFormula(
"FormulaWeight", tmpWeight, tr );
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)" );
650 splitSpecs.DeclareOptionRef( nEventRequests.at(cl).nTrainingEventsRequested,
TString(
"nTrain_")+clName, titleTrain );
651 splitSpecs.DeclareOptionRef( nEventRequests.at(cl).nTestingEventsRequested ,
TString(
"nTest_")+clName , titleTest );
654 splitSpecs.DeclareOptionRef(
fVerbose,
"V",
"Verbosity (default: true)" );
656 splitSpecs.DeclareOptionRef(
fVerboseLevel=
TString(
"Info"),
"VerboseLevel",
"VerboseLevel (Debug/Verbose/Info)" );
657 splitSpecs.AddPreDefVal(
TString(
"Debug"));
658 splitSpecs.AddPreDefVal(
TString(
"Verbose"));
659 splitSpecs.AddPreDefVal(
TString(
"Info"));
661 splitSpecs.ParseOptions();
662 splitSpecs.CheckForUnusedOptions();
674 <<
"\tSplitmode is: \"" << splitMode <<
"\" the mixmode is: \"" << mixMode <<
"\"" <<
Endl;
675 if (mixMode==
"SAMEASSPLITMODE") mixMode = splitMode;
676 else if (mixMode!=splitMode)
678 <<
" differs from mixmode="<<mixMode<<Endl;
703 for (
size_t i=0; i<nclasses; i++) {
704 eventCounts[i].varAvLength =
new Float_t[nvars];
705 for (
UInt_t ivar=0; ivar<nvars; ivar++)
706 eventCounts[i].varAvLength[ivar] = 0;
714 for (
UInt_t cl=0; cl<nclasses; cl++) {
718 EventStats& classEventCounts = eventCounts[cl];
732 std::vector<Float_t> vars(nvars);
733 std::vector<Float_t> tgts(ntgts);
734 std::vector<Float_t> vis(nvis);
750 for (
Long64_t evtIdx = 0; evtIdx < nEvts; evtIdx++) {
761 Int_t sizeOfArrays = 1;
762 Int_t prevArrExpr = 0;
767 for (
UInt_t ivar=0; ivar<nvars; ivar++) {
770 if (ndata == 1)
continue;
772 varIsArray[ivar] =
kTRUE;
773 if (sizeOfArrays == 1) {
774 sizeOfArrays =
ndata;
777 else if (sizeOfArrays!=ndata) {
778 Log() <<
kERROR <<
Form(
"Dataset[%s] : ",dsi.
GetName())<<
"ERROR while preparing training and testing trees:" <<
Endl;
779 Log() <<
Form(
"Dataset[%s] : ",dsi.
GetName())<<
" multiple array-type expressions of different length were encountered" <<
Endl;
780 Log() <<
Form(
"Dataset[%s] : ",dsi.
GetName())<<
" location of error: event " << evtIdx
784 <<
Form(
"Dataset[%s] : ",dsi.
GetName()) << ndata <<
" entries, while" <<
Endl;
792 for (
Int_t idata = 0; idata<sizeOfArrays; idata++) {
807 Log() <<
kWARNING <<
Form(
"Dataset[%s] : ",dsi.
GetName())<<
"Cut expression resolves to infinite value (NaN): " 813 for (
UInt_t ivar=0; ivar<nvars; ivar++) {
816 vars[ivar] = (ndata == 1 ?
821 Log() <<
kWARNING <<
Form(
"Dataset[%s] : ",dsi.
GetName())<<
"Input expression resolves to infinite value (NaN): " 827 for (
UInt_t itrgt=0; itrgt<ntgts; itrgt++) {
830 tgts[itrgt] = (ndata == 1 ?
835 Log() <<
kWARNING <<
Form(
"Dataset[%s] : ",dsi.
GetName())<<
"Target expression resolves to infinite value (NaN): " 841 for (
UInt_t itVis=0; itVis<nvis; itVis++) {
844 vis[itVis] = (ndata == 1 ?
849 Log() <<
kWARNING <<
Form(
"Dataset[%s] : ",dsi.
GetName())<<
"Spectator expression resolves to infinite value (NaN): " 860 weight *= (ndata == 1 ?
865 Log() <<
kWARNING <<
Form(
"Dataset[%s] : ",dsi.
GetName())<<
"Weight expression resolves to infinite value (NaN): " 877 if (cutVal<0.5)
continue;
898 event_v.push_back(
new Event(vars, tgts , vis, cl , weight));
909 Log() <<
kDEBUG <<
"(after possible flattening of arrays):" <<
Endl;
916 <<
" -- number of events : " 917 << std::setw(5) << eventCounts[cl].nEvBeforeCut
918 <<
" / sum of weights: " << std::setw(5) << eventCounts[cl].nWeEvBeforeCut <<
Endl;
924 <<
" tree -- total number of entries: " 930 <<
"\tPreselection: (will affect number of requested training and testing events)" <<
Endl;
933 <<
"\tPreselection: (will NOT affect number of requested training and testing events)" <<
Endl;
941 <<
" -- number of events passed: " 942 << std::setw(5) << eventCounts[cl].nEvAfterCut
943 <<
" / sum of weights: " << std::setw(5) << eventCounts[cl].nWeEvAfterCut <<
Endl;
946 <<
" -- efficiency : " 947 << std::setw(6) << eventCounts[cl].nWeEvAfterCut/eventCounts[cl].nWeEvBeforeCut <<
Endl;
951 <<
" No preselection cuts applied on event classes" <<
Endl;
980 if (splitMode.
Contains(
"RANDOM" ) ) {
984 if( ! unspecifiedEvents.empty() ) {
986 << unspecifiedEvents.size()
987 <<
" events of class " << cls
988 <<
" which are not yet associated to testing or training" <<
Endl;
989 std::random_shuffle( unspecifiedEvents.begin(),
990 unspecifiedEvents.end(),
1000 Log() <<
kDEBUG <<
Form(
"Dataset[%s] : ",dsi.
GetName())<<
"check number of training/testing events, requested and available number of events and for class " << cls <<
Endl;
1007 Int_t availableTraining = eventVectorTraining.size();
1008 Int_t availableTesting = eventVectorTesting.size();
1009 Int_t availableUndefined = eventVectorUndefined.size();
1013 presel_scale = eventCounts[cls].cutScaling();
1014 if (presel_scale < 1)
1015 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;
1018 if (eventCounts[cls].cutScaling() < 1)
1019 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;
1022 Int_t requestedTraining =
Int_t(eventCounts[cls].nTrainingEventsRequested * presel_scale);
1023 Int_t requestedTesting =
Int_t(eventCounts[cls].nTestingEventsRequested * presel_scale);
1025 Log() <<
kDEBUG <<
Form(
"Dataset[%s] : ",dsi.
GetName())<<
"events in training trees : " << availableTraining <<
Endl;
1026 Log() <<
kDEBUG <<
Form(
"Dataset[%s] : ",dsi.
GetName())<<
"events in testing trees : " << availableTesting <<
Endl;
1027 Log() <<
kDEBUG <<
Form(
"Dataset[%s] : ",dsi.
GetName())<<
"events in unspecified trees : " << availableUndefined <<
Endl;
1028 Log() <<
kDEBUG <<
Form(
"Dataset[%s] : ",dsi.
GetName())<<
"requested for training : " << requestedTraining;
1031 Log() <<
" ( " << eventCounts[cls].nTrainingEventsRequested
1032 <<
" * " << presel_scale <<
" preselection efficiency)" <<
Endl;
1035 Log() <<
kDEBUG <<
"requested for testing : " << requestedTesting;
1037 Log() <<
" ( " << eventCounts[cls].nTestingEventsRequested
1038 <<
" * " << presel_scale <<
" preselection efficiency)" <<
Endl;
1089 Int_t useForTesting(0),useForTraining(0);
1090 Int_t allAvailable(availableUndefined + availableTraining + availableTesting);
1092 if( (requestedTraining == 0) && (requestedTesting == 0)){
1096 if ( availableUndefined >=
TMath::Abs(availableTraining - availableTesting) ) {
1098 useForTraining = useForTesting = allAvailable/2;
1101 useForTraining = availableTraining;
1102 useForTesting = availableTesting;
1103 if (availableTraining < availableTesting)
1104 useForTraining += availableUndefined;
1106 useForTesting += availableUndefined;
1108 requestedTraining = useForTraining;
1109 requestedTesting = useForTesting;
1112 else if (requestedTesting == 0){
1114 useForTraining =
TMath::Max(requestedTraining,availableTraining);
1115 if (allAvailable < useForTraining) {
1116 Log() <<
kFATAL <<
Form(
"Dataset[%s] : ",dsi.
GetName())<<
"More events requested for training (" 1117 << requestedTraining <<
") than available (" 1118 << allAvailable <<
")!" << Endl;
1120 useForTesting = allAvailable - useForTraining;
1121 requestedTesting = useForTesting;
1124 else if (requestedTraining == 0){
1125 useForTesting =
TMath::Max(requestedTesting,availableTesting);
1126 if (allAvailable < useForTesting) {
1127 Log() <<
kFATAL <<
Form(
"Dataset[%s] : ",dsi.
GetName())<<
"More events requested for testing (" 1128 << requestedTesting <<
") than available (" 1129 << allAvailable <<
")!" << Endl;
1131 useForTraining= allAvailable - useForTesting;
1132 requestedTraining = useForTraining;
1141 Int_t stillNeedForTraining =
TMath::Max(requestedTraining-availableTraining,0);
1142 Int_t stillNeedForTesting =
TMath::Max(requestedTesting-availableTesting,0);
1144 int NFree = availableUndefined - stillNeedForTraining - stillNeedForTesting;
1145 if (NFree <0) NFree = 0;
1146 useForTraining =
TMath::Max(requestedTraining,availableTraining) + NFree/2;
1147 useForTesting= allAvailable - useForTraining;
1150 Log() <<
kDEBUG <<
Form(
"Dataset[%s] : ",dsi.
GetName())<<
"determined event sample size to select training sample from="<<useForTraining<<Endl;
1151 Log() <<
kDEBUG <<
Form(
"Dataset[%s] : ",dsi.
GetName())<<
"determined event sample size to select test sample from="<<useForTesting<<Endl;
1156 if( splitMode ==
"ALTERNATE" ){
1158 Int_t nTraining = availableTraining;
1159 Int_t nTesting = availableTesting;
1160 for( EventVector::iterator it = eventVectorUndefined.begin(), itEnd = eventVectorUndefined.end(); it != itEnd; ){
1162 if( nTraining <= requestedTraining ){
1163 eventVectorTraining.insert( eventVectorTraining.end(), (*it) );
1168 eventVectorTesting.insert( eventVectorTesting.end(), (*it) );
1173 Log() <<
kDEBUG <<
Form(
"Dataset[%s] : ",dsi.
GetName())<<
"split '" << splitMode <<
"'" << Endl;
1176 Log() <<
kDEBUG <<
Form(
"Dataset[%s] : ",dsi.
GetName())<<
"availableundefined : " << availableUndefined << Endl;
1177 Log() <<
kDEBUG <<
Form(
"Dataset[%s] : ",dsi.
GetName())<<
"useForTraining : " << useForTraining << Endl;
1178 Log() <<
kDEBUG <<
Form(
"Dataset[%s] : ",dsi.
GetName())<<
"useForTesting : " << useForTesting << Endl;
1179 Log() <<
kDEBUG <<
Form(
"Dataset[%s] : ",dsi.
GetName())<<
"availableTraining : " << availableTraining << Endl;
1180 Log() <<
kDEBUG <<
Form(
"Dataset[%s] : ",dsi.
GetName())<<
"availableTesting : " << availableTesting << Endl;
1182 if( availableUndefined<(useForTraining-availableTraining) ||
1183 availableUndefined<(useForTesting -availableTesting ) ||
1184 availableUndefined<(useForTraining+useForTesting-availableTraining-availableTesting ) ){
1185 Log() <<
kFATAL <<
Form(
"Dataset[%s] : ",dsi.
GetName())<<
"More events requested than available!" << Endl;
1189 if (useForTraining>availableTraining){
1190 eventVectorTraining.insert( eventVectorTraining.end() , eventVectorUndefined.begin(), eventVectorUndefined.begin()+ useForTraining- availableTraining );
1191 eventVectorUndefined.erase( eventVectorUndefined.begin(), eventVectorUndefined.begin() + useForTraining- availableTraining);
1193 if (useForTesting>availableTesting){
1194 eventVectorTesting.insert( eventVectorTesting.end() , eventVectorUndefined.begin(), eventVectorUndefined.begin()+ useForTesting- availableTesting );
1197 eventVectorUndefined.clear();
1200 if (splitMode.
Contains(
"RANDOM" )){
1201 UInt_t sizeTraining = eventVectorTraining.size();
1202 if( sizeTraining >
UInt_t(requestedTraining) ){
1203 std::vector<UInt_t> indicesTraining( sizeTraining );
1207 std::random_shuffle( indicesTraining.begin(), indicesTraining.end(), rndm );
1209 indicesTraining.erase( indicesTraining.begin()+sizeTraining-
UInt_t(requestedTraining), indicesTraining.end() );
1211 for( std::vector<UInt_t>::iterator it = indicesTraining.begin(), itEnd = indicesTraining.end(); it != itEnd; ++it ){
1212 delete eventVectorTraining.at( (*it) );
1213 eventVectorTraining.at( (*it) ) =
NULL;
1216 eventVectorTraining.erase( std::remove( eventVectorTraining.begin(), eventVectorTraining.end(), (
void*)
NULL ), eventVectorTraining.end() );
1219 UInt_t sizeTesting = eventVectorTesting.size();
1220 if( sizeTesting >
UInt_t(requestedTesting) ){
1221 std::vector<UInt_t> indicesTesting( sizeTesting );
1225 std::random_shuffle( indicesTesting.begin(), indicesTesting.end(), rndm );
1227 indicesTesting.erase( indicesTesting.begin()+sizeTesting-
UInt_t(requestedTesting), indicesTesting.end() );
1229 for( std::vector<UInt_t>::iterator it = indicesTesting.begin(), itEnd = indicesTesting.end(); it != itEnd; ++it ){
1230 delete eventVectorTesting.at( (*it) );
1231 eventVectorTesting.at( (*it) ) =
NULL;
1234 eventVectorTesting.erase( std::remove( eventVectorTesting.begin(), eventVectorTesting.end(), (
void*)
NULL ), eventVectorTesting.end() );
1238 if( eventVectorTraining.size() <
UInt_t(requestedTraining) )
1239 Log() <<
kWARNING <<
Form(
"Dataset[%s] : ",dsi.
GetName())<<
"DataSetFactory/requested number of training samples larger than size of eventVectorTraining.\n" 1240 <<
"There is probably an issue. Please contact the TMVA developers." << Endl;
1241 std::for_each( eventVectorTraining.begin()+requestedTraining, eventVectorTraining.end(), DeleteFunctor<Event>() );
1242 eventVectorTraining.erase(eventVectorTraining.begin()+requestedTraining,eventVectorTraining.end());
1244 if( eventVectorTesting.size() <
UInt_t(requestedTesting) )
1245 Log() <<
kWARNING <<
Form(
"Dataset[%s] : ",dsi.
GetName())<<
"DataSetFactory/requested number of testing samples larger than size of eventVectorTesting.\n" 1246 <<
"There is probably an issue. Please contact the TMVA developers." << Endl;
1247 std::for_each( eventVectorTesting.begin()+requestedTesting, eventVectorTesting.end(), DeleteFunctor<Event>() );
1248 eventVectorTesting.erase(eventVectorTesting.begin()+requestedTesting,eventVectorTesting.end());
1254 Int_t trainingSize = 0;
1255 Int_t testingSize = 0;
1269 trainingEventVector->reserve( trainingSize );
1270 testingEventVector->reserve( testingSize );
1278 if( mixMode ==
"ALTERNATE" ){
1283 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;
1286 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;
1289 typedef EventVector::iterator EvtVecIt;
1290 EvtVecIt itEvent, itEventEnd;
1293 Log() <<
kDEBUG <<
"insert class 0 into training and test vector" <<
Endl;
1295 testingEventVector->insert( testingEventVector->end(), tmpEventVector[
Types::kTesting].at(0).begin(), tmpEventVector[
Types::kTesting].at(0).end() );
1302 itTarget = trainingEventVector->begin() - 1;
1304 for( itEvent = tmpEventVector[
Types::kTraining].at(cls).begin(), itEventEnd = tmpEventVector[
Types::kTraining].at(cls).end(); itEvent != itEventEnd; ++itEvent ){
1306 if( (trainingEventVector->end() - itTarget) <
Int_t(cls+1) ) {
1307 itTarget = trainingEventVector->end();
1308 trainingEventVector->insert( itTarget, itEvent, itEventEnd );
1312 trainingEventVector->insert( itTarget, (*itEvent) );
1316 itTarget = testingEventVector->begin() - 1;
1318 for( itEvent = tmpEventVector[
Types::kTesting].at(cls).begin(), itEventEnd = tmpEventVector[
Types::kTesting].at(cls).end(); itEvent != itEventEnd; ++itEvent ){
1320 if( ( testingEventVector->end() - itTarget ) <
Int_t(cls+1) ) {
1321 itTarget = testingEventVector->end();
1322 testingEventVector->insert( itTarget, itEvent, itEventEnd );
1326 testingEventVector->insert( itTarget, (*itEvent) );
1343 trainingEventVector->insert( trainingEventVector->end(), tmpEventVector[
Types::kTraining].at(cls).begin(), tmpEventVector[
Types::kTraining].at(cls).end() );
1344 testingEventVector->insert ( testingEventVector->end(), tmpEventVector[
Types::kTesting].at(cls).begin(), tmpEventVector[
Types::kTesting].at(cls).end() );
1360 if (mixMode ==
"RANDOM") {
1363 std::random_shuffle( trainingEventVector->begin(), trainingEventVector->end(), rndm );
1364 std::random_shuffle( testingEventVector->begin(), testingEventVector->end(), rndm );
1367 Log() <<
kDEBUG <<
Form(
"Dataset[%s] : ",dsi.
GetName())<<
"trainingEventVector " << trainingEventVector->size() <<
Endl;
1368 Log() <<
kDEBUG <<
Form(
"Dataset[%s] : ",dsi.
GetName())<<
"testingEventVector " << testingEventVector->size() <<
Endl;
1380 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;
1384 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;
1413 Int_t trainingSize = 0;
1414 Int_t testingSize = 0;
1422 Double_t trainingSumSignalWeights = 0;
1423 Double_t trainingSumBackgrWeights = 0;
1424 Double_t testingSumSignalWeights = 0;
1425 Double_t testingSumBackgrWeights = 0;
1430 trainingSizePerClass.at(cls) = tmpEventVector[
Types::kTraining].at(cls).size();
1431 testingSizePerClass.at(cls) = tmpEventVector[
Types::kTesting].at(cls).size();
1433 trainingSize += trainingSizePerClass.back();
1434 testingSize += testingSizePerClass.back();
1448 trainingSumWeightsPerClass.at(cls) = std::accumulate( tmpEventVector[
Types::kTraining].at(cls).begin(),
1455 testingSumWeightsPerClass.at(cls) = std::accumulate( tmpEventVector[
Types::kTesting].at(cls).begin(),
1463 trainingSumSignalWeights += trainingSumWeightsPerClass.at(cls);
1464 testingSumSignalWeights += testingSumWeightsPerClass.at(cls);
1466 trainingSumBackgrWeights += trainingSumWeightsPerClass.at(cls);
1467 testingSumBackgrWeights += testingSumWeightsPerClass.at(cls);
1487 if (normMode ==
"NONE") {
1488 Log() <<
kINFO <<
Form(
"Dataset[%s] : ",dsi.
GetName()) <<
"No weight renormalisation applied: use original global and event weights" <<
Endl;
1494 else if (normMode ==
"NUMEVENTS") {
1496 <<
"\tWeight renormalisation mode: \"NumEvents\": renormalises all event classes " <<
Endl;
1498 <<
" such that the effective (weighted) number of events in each class equals the respective " <<
Endl;
1500 <<
" number of events (entries) that you demanded in PrepareTrainingAndTestTree(\"\",\"nTrain_Signal=.. )" <<
Endl;
1502 <<
" ... i.e. such that Sum[i=1..N_j]{w_i} = N_j, j=0,1,2..." <<
Endl;
1504 <<
" ... (note that N_j is the sum of TRAINING events (nTrain_j...with j=Signal,Background.." <<
Endl;
1506 <<
" ..... Testing events are not renormalised nor included in the renormalisation factor! )"<<
Endl;
1512 renormFactor.at(cls) = ((
Float_t)trainingSizePerClass.at(cls) )/
1513 (trainingSumWeightsPerClass.at(cls)) ;
1516 else if (normMode ==
"EQUALNUMEVENTS") {
1522 Log() <<
kINFO <<
Form(
"Dataset[%s] : ",dsi.
GetName()) <<
"Weight renormalisation mode: \"EqualNumEvents\": renormalises all event classes ..." <<
Endl;
1523 Log() <<
kINFO <<
Form(
"Dataset[%s] : ",dsi.
GetName()) <<
" such that the effective (weighted) number of events in each class is the same " <<
Endl;
1524 Log() <<
kINFO <<
Form(
"Dataset[%s] : ",dsi.
GetName()) <<
" (and equals the number of events (entries) given for class=0 )" <<
Endl;
1525 Log() <<
kINFO <<
Form(
"Dataset[%s] : ",dsi.
GetName()) <<
"... i.e. such that Sum[i=1..N_j]{w_i} = N_classA, j=classA, classB, ..." <<
Endl;
1526 Log() <<
kINFO <<
Form(
"Dataset[%s] : ",dsi.
GetName()) <<
"... (note that N_j is the sum of TRAINING events" <<
Endl;
1527 Log() <<
kINFO <<
Form(
"Dataset[%s] : ",dsi.
GetName()) <<
" ..... Testing events are not renormalised nor included in the renormalisation factor!)" <<
Endl;
1530 UInt_t referenceClass = 0;
1532 renormFactor.at(cls) =
Float_t(trainingSizePerClass.at(referenceClass))/
1533 (trainingSumWeightsPerClass.at(cls));
1537 Log() <<
kFATAL <<
Form(
"Dataset[%s] : ",dsi.
GetName())<<
"<PrepareForTrainingAndTesting> Unknown NormMode: " << normMode <<
Endl;
1545 <<
"--> Rescale " << setiosflags(ios::left) << std::setw(maxL)
1547 for (EventVector::iterator it = tmpEventVector[
Types::kTraining].at(cls).begin(),
1548 itEnd = tmpEventVector[
Types::kTraining].at(cls).end(); it != itEnd; ++it){
1549 (*it)->SetWeight ((*it)->GetWeight() * renormFactor.at(cls));
1560 <<
"Number of training and testing events" <<
Endl;
1563 <<
"---------------------------------------------------------------------------" <<
Endl;
1565 trainingSumSignalWeights = 0;
1566 trainingSumBackgrWeights = 0;
1567 testingSumSignalWeights = 0;
1568 testingSumBackgrWeights = 0;
1572 trainingSumWeightsPerClass.at(cls) = (std::accumulate( tmpEventVector[
Types::kTraining].at(cls).begin(),
1579 testingSumWeightsPerClass.at(cls) = std::accumulate( tmpEventVector[
Types::kTesting].at(cls).begin(),
1588 trainingSumSignalWeights += trainingSumWeightsPerClass.at(cls);
1589 testingSumSignalWeights += testingSumWeightsPerClass.at(cls);
1591 trainingSumBackgrWeights += trainingSumWeightsPerClass.at(cls);
1592 testingSumBackgrWeights += testingSumWeightsPerClass.at(cls);
1598 << setiosflags(ios::left) << std::setw(maxL)
1600 <<
"training events : " << trainingSizePerClass.at(cls) <<
Endl;
1601 Log() <<
kDEBUG <<
"\t(sum of weights: " << trainingSumWeightsPerClass.at(cls) <<
")" 1602 <<
" - requested were " << eventCounts[cls].nTrainingEventsRequested <<
" events" <<
Endl;
1604 << setiosflags(ios::left) << std::setw(maxL)
1606 <<
"testing events : " << testingSizePerClass.at(cls) <<
Endl;
1607 Log() <<
kDEBUG <<
"\t(sum of weights: " << testingSumWeightsPerClass.at(cls) <<
")" 1608 <<
" - requested were " << eventCounts[cls].nTestingEventsRequested <<
" events" <<
Endl;
1610 << setiosflags(ios::left) << std::setw(maxL)
1612 <<
"training and testing events: " 1613 << (trainingSizePerClass.at(cls)+testingSizePerClass.at(cls)) << Endl;
1614 Log() <<
kDEBUG <<
"\t(sum of weights: " 1615 << (trainingSumWeightsPerClass.at(cls)+testingSumWeightsPerClass.at(cls)) <<
")" << Endl;
1616 if(eventCounts[cls].nEvAfterCut<eventCounts[cls].nEvBeforeCut) {
1617 Log() <<
kINFO <<
Form(
"Dataset[%s] : ",dsi.
GetName()) << setiosflags(ios::left) << std::setw(maxL)
1619 <<
"due to the preselection a scaling factor has been applied to the numbers of requested events: " 1620 << eventCounts[cls].cutScaling() <<
Endl;
virtual const char * GetTitle() const
Returns title of object.
UInt_t GetNSpectators() const
access the number of targets through the datasetinfo
A TLeaf describes individual elements of a TBranch See TBranch structure in TTree.
std::vector< EventVector > EventVectorOfClasses
Long64_t GetNTestEvents() const
void SetTrainingSumBackgrWeights(Double_t trainingSumBackgrWeights)
MsgLogger & Endl(MsgLogger &ml)
virtual void SetBranchStatus(const char *bname, Bool_t status=1, UInt_t *found=0)
Set branch status to Process or DoNotProcess.
std::vector< VariableInfo > & GetSpectatorInfos()
std::vector< TTreeFormula * > fInputFormulas
Float_t GetSpectator(UInt_t ivar) const
return spectator content
const TString & GetExpression() const
Int_t GetClassNameMaxLength() const
std::vector< TTreeFormula * > fCutFormulas
UInt_t GetNClasses() const
std::vector< Double_t > ValuePerClass
Types::ETreeType GetTreeType() const
UInt_t GetNTargets() const
void ToUpper()
Change string to upper case.
void SetTrainingSumSignalWeights(Double_t trainingSumSignalWeights)
virtual Int_t GetEntry(Long64_t entry=0, Int_t getall=0)
Read all branches of entry and return total number of bytes read.
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 ...
const TString & GetWeight() const
void generate(R &r, TH1D *h)
TBranch * GetBranch() const
void CalcMinMax(DataSet *, DataSetInfo &dsi)
compute covariance matrix
TFile * GetCurrentFile() const
Return pointer to the current file.
Double_t GetWeight() const
return the event weight - depending on whether the flag IgnoreNegWeightsInTraining is or not...
UInt_t GetNVariables() const
Float_t GetValue(UInt_t ivar) const
return value of i'th variable
std::vector< int > NumberPerClass
const TString & GetClassName() const
const TString & GetInternalName() const
virtual const char * GetName() const
Returns name of object.
const char * Data() const
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 &)
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString...
virtual Bool_t IsOnTerminalBranch() const
virtual Long64_t LoadTree(Long64_t entry)
Set current entry.
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
Double_t GetOriginalWeight() const
void ChangeToNewTree(TreeInfo &, const DataSetInfo &)
While the data gets copied into the local training and testing trees, the input tree can change (for ...
A specialized string object used for TTree selections.
virtual TFile * GetFile() const
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...
Int_t LargestCommonDivider(Int_t a, Int_t b)
Bool_t fScaleWithPreselEff
std::vector< TTreeFormula * > fSpectatorFormulas
DataSet * CreateDataSet(DataSetInfo &, DataInputHandler &)
steering the creation of a new dataset
TDirectory * GetDirectory() const
virtual const char * ClassName() const
Returns name of class to which the object belongs.
VariableInfo & GetTargetInfo(Int_t i)
ClassInfo * GetClassInfo(Int_t clNum) const
char * Form(const char *fmt,...)
virtual const char * GetName() const
Returns name of object.
UInt_t GetSignalClassIndex()
std::vector< TTreeFormula * > fTargetFormulas
void SetEventCollection(std::vector< Event * > *, Types::ETreeType, Bool_t deleteEvents=true)
Sets the event collection (by DataSetFactory)
const TCut & GetCut() const
std::vector< Event * > EventVector
const TString & GetSplitOptions() const
DataSet * BuildInitialDataSet(DataSetInfo &, TMVA::DataInputHandler &)
if no entries, than create a DataSet with one Event which uses dynamic variables (pointers to variabl...
const Event * GetEvent() const
~DataSetFactory()
destructor
virtual const char * GetName() const
Returns name of object.
virtual void ResetBranchAddresses()
Tell all of our branches to drop their current objects and allocate new ones.
Long64_t GetNEvents(Types::ETreeType type=Types::kMaxTreeType) const
UInt_t GetNTargets() const
access the number of targets through the datasetinfo
UInt_t GetNVariables() const
access the number of variables through the datasetinfo
VariableInfo & GetSpectatorInfo(Int_t i)
compose_binary_t< F, G, H > compose_binary(const F &_f, const G &_g, const H &_h)
virtual TTree * GetTree() const
VariableInfo & GetVariableInfo(Int_t i)
ClassInfo * AddClass(const TString &className)
Long64_t GetNClassEvents(Int_t type, UInt_t classNumber)
void SetConfigName(const char *n)
Float_t GetTarget(UInt_t itgt) const
Abstract ClassifierFactory template that handles arbitrary types.
std::vector< EventStats > EvtStatsPerClass
Short_t Max(Short_t a, Short_t b)
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
UInt_t GetNSpectators(bool all=kTRUE) 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
virtual Long64_t GetEntries() const
Long64_t GetNTrainingEvents() const
A TTree object has a header with a name and a title.
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;...
virtual const char * GetTitle() const
Returns title of object.
void SetNormalization(const TString &norm)
TMatrixD * CalcCovarianceMatrix(DataSet *, const UInt_t classNumber)
compute covariance matrix
std::vector< VariableInfo > & GetVariableInfos()
int CompareTo(const char *cs, ECaseCompare cmp=kExact) const
Compare a string to char *cs2.