33 #ifndef ROOT_TMVA_DataSetInfo
36 #ifndef ROOT_TMVA_DataSet
39 #ifndef ROOT_TMVA_Event
42 #ifndef ROOT_TMVA_MsgLogger
45 #ifndef ROOT_TMVA_ResultsRegression
48 #ifndef ROOT_TMVA_ResultsClassification
51 #ifndef ROOT_TMVA_ResultsMulticlass
54 #ifndef ROOT_TMVA_Configurable
63 fEventCollection(4,(
std::vector<
Event*>*)0),
66 fHasNegativeEventWeights(
kFALSE),
70 for (
UInt_t i=0; i<4; i++) fEventCollection[i] = new std::vector<Event*>;
84 for (
Int_t treeIdx = 0; treeIdx < treeNum; treeIdx++) {
101 fBlockBelongToTraining.clear();
103 for (std::vector< std::map< TString, Results* > >::iterator it = fResults.begin(); it != fResults.end(); it++) {
104 for (std::map< TString, Results* >::iterator itMap = (*it).begin(); itMap != (*it).end(); itMap++) {
105 delete itMap->second;
110 if (fSamplingRandom != 0 )
delete fSamplingRandom;
112 std::vector< std::pair< Float_t, Long64_t >* >::iterator itEv;
113 std::vector< std::vector<std::pair< Float_t, Long64_t >* > >::iterator treeIt;
114 for (treeIt = fSamplingEventList.begin(); treeIt != fSamplingEventList.end(); treeIt++ ) {
115 for (itEv = (*treeIt).begin(); itEv != (*treeIt).end(); itEv++) {
131 if (fClassEvents.size()<(
UInt_t)(type+1)) fClassEvents.resize( type+1 );
132 if (fClassEvents.at( type ).size() < classNumber+1) fClassEvents.at( type ).resize( classNumber+1 );
133 fClassEvents.at( type ).at( classNumber ) += 1;
140 if (fClassEvents.size()<(
UInt_t)(type+1)) fClassEvents.resize( type+1 );
141 fClassEvents.at( type ).clear();
149 return fClassEvents.at(type).at(classNumber);
151 catch (std::out_of_range excpt) {
152 ClassInfo* ci = fdsi.GetClassInfo( classNumber );
153 Log() <<
kFATAL <<
"No " << (type==0?
"training":(type==1?
"testing":
"_unknown_type_"))
154 <<
" events for class " << (ci==
NULL?
"_no_name_known_":ci->
GetName().
Data()) <<
" (index # "<<classNumber<<
")"
155 <<
" available. Check if all class names are spelled correctly and if events are"
156 <<
" passing the selection cuts." <<
Endl;
159 Log() <<
kFATAL <<
"ERROR/CAUGHT : DataSet/GetNClassEvents, .. unknown error" <<
Endl;
169 UInt_t i = TreeIndex(type);
170 if (i>=fEventCollection.size() || fEventCollection[i]==0)
return;
172 for (
UInt_t j=0; j<fEventCollection[i]->size(); j++)
delete (*fEventCollection[i])[j];
174 delete fEventCollection[i];
175 fEventCollection[i]=0;
182 if (fSampling.size() >
UInt_t(fCurrentTreeIdx) && fSampling.at(fCurrentTreeIdx)) {
183 Long64_t iEvt = fSamplingSelected.at(fCurrentTreeIdx).at( fCurrentEventIdx )->second;
184 return (*(fEventCollection.at(fCurrentTreeIdx))).at(iEvt);
187 return (*(fEventCollection.at(fCurrentTreeIdx))).at(fCurrentEventIdx);
196 return fdsi.GetNVariables();
204 return fdsi.GetNTargets();
212 return fdsi.GetNSpectators();
221 fEventCollection.at(
Int_t(type))->push_back(ev);
223 fEvtCollIt=fEventCollection.at(fCurrentTreeIdx)->begin();
231 Bool_t deleteEvents =
true;
232 DestroyCollection(type,deleteEvents);
234 const Int_t t = TreeIndex(type);
235 ClearNClassEvents( type );
236 fEventCollection.at(t) = events;
237 for (std::vector<Event*>::iterator it = fEventCollection.at(t)->begin(); it < fEventCollection.at(t)->end(); it++) {
238 IncrementNClassEvents( t, (*it)->GetClass() );
240 fEvtCollIt=fEventCollection.at(fCurrentTreeIdx)->begin();
261 UInt_t t = TreeIndex(type);
262 if (t<fResults.size()) {
263 const std::map< TString, Results* >& resultsForType = fResults[t];
264 std::map< TString, Results* >::const_iterator it = resultsForType.find(resultsName);
265 if (it!=resultsForType.end()) {
271 fResults.resize(t+1);
277 switch(analysistype) {
297 fResults[t][resultsName] = newresults;
312 if (fResults.empty())
return;
314 if (
UInt_t(type) > fResults.size()){
315 Log()<<
kFATAL<<
"you asked for an Treetype (training/testing/...)"
316 <<
" whose index " << type <<
" does not exist " <<
Endl;
318 std::map< TString, Results* >& resultsForType = fResults[
UInt_t(type)];
319 std::map< TString, Results* >::iterator it = resultsForType.find(resultsName);
320 if (it!=resultsForType.end()) {
321 Log() <<
kDEBUG <<
" Delete Results previous existing result:" << resultsName
322 <<
" of type " << type <<
Endl;
324 resultsForType.erase(it->first);
327 Log() <<
kINFO <<
"could not fine Result class of " << resultsName
328 <<
" of type " << type <<
" which I should have deleted" <<
Endl;
338 if (fBlockBelongToTraining.size() == blockNum)
return;
340 if (fBlockBelongToTraining.size() == 1) {
341 if (fEventCollection[tOrg] == 0)
342 fEventCollection[tOrg]=
new std::vector<TMVA::Event*>(fEventCollection[tTrn]->size());
343 fEventCollection[tOrg]->clear();
344 for (
UInt_t i=0; i<fEventCollection[tTrn]->size(); i++)
345 fEventCollection[tOrg]->push_back((*fEventCollection[tTrn])[i]);
346 fClassEvents[tOrg] = fClassEvents[tTrn];
349 fBlockBelongToTraining.clear();
350 for (
UInt_t i=0 ; i < blockNum ; i++) fBlockBelongToTraining.push_back(
kTRUE);
352 ApplyTrainingSetDivision();
361 fEventCollection[tTrn]->clear();
362 if (fEventCollection[tVld]==0)
363 fEventCollection[tVld] =
new std::vector<TMVA::Event*>(fEventCollection[tOrg]->size());
364 fEventCollection[tVld]->clear();
367 for (
UInt_t i=0; i<fEventCollection[tOrg]->size(); i++) {
368 if (fBlockBelongToTraining[i % fBlockBelongToTraining.size()])
369 fEventCollection[tTrn]->push_back((*fEventCollection[tOrg])[i]);
371 fEventCollection[tVld]->push_back((*fEventCollection[tOrg])[i]);
381 fBlockBelongToTraining[blockInd]=
kFALSE;
383 fBlockBelongToTraining[blockInd]=
kTRUE;
384 if (applyChanges) ApplyTrainingSetDivision();
392 return GetNClassEvents(
Types::kTesting, fdsi.GetClassInfo(
"Signal")->GetNumber() );
400 return GetNClassEvents(
Types::kTesting, fdsi.GetClassInfo(
"Background")->GetNumber() );
408 return GetNClassEvents(
Types::kTraining, fdsi.GetClassInfo(
"Signal")->GetNumber() );
416 return GetNClassEvents(
Types::kTraining, fdsi.GetClassInfo(
"Background")->GetNumber() );
425 if (fSamplingRandom == 0 ) fSamplingRandom =
new TRandom3( seed );
428 std::vector< std::pair< Float_t, Long64_t >* > evtList;
429 std::vector< std::pair< Float_t, Long64_t >* >::iterator it;
431 Int_t treeIdx = TreeIndex( GetCurrentType() );
433 if (fSamplingEventList.size() <
UInt_t(treeIdx+1) ) fSamplingEventList.resize(treeIdx+1);
434 if (fSamplingSelected.size() <
UInt_t(treeIdx+1) ) fSamplingSelected.resize(treeIdx+1);
435 for (it = fSamplingEventList.at(treeIdx).begin(); it != fSamplingEventList.at(treeIdx).end(); it++ )
delete (*it);
436 fSamplingEventList.at(treeIdx).clear();
437 fSamplingSelected.at(treeIdx).clear();
439 if (fSampling.size() <
UInt_t(treeIdx+1) ) fSampling.resize(treeIdx+1);
440 if (fSamplingNEvents.size() <
UInt_t(treeIdx+1) ) fSamplingNEvents.resize(treeIdx+1);
441 if (fSamplingWeight.size() <
UInt_t(treeIdx+1) ) fSamplingWeight.resize(treeIdx+1);
443 if (fraction > 0.999999 || fraction < 0.0000001) {
444 fSampling.at( treeIdx ) =
false;
445 fSamplingNEvents.at( treeIdx ) = 0;
446 fSamplingWeight.at( treeIdx ) = 1.0;
451 fSampling.at( treeIdx ) =
false;
453 fSamplingNEvents.at( treeIdx ) =
Int_t(fraction*GetNEvents());
454 fSamplingWeight.at( treeIdx ) = weight;
457 fSamplingEventList.at( treeIdx ).reserve( nEvts );
458 fSamplingSelected.at( treeIdx ).reserve( fSamplingNEvents.at(treeIdx) );
459 for (
Long64_t ievt=0; ievt<nEvts; ievt++) {
460 std::pair<Float_t,Long64_t> *p =
new std::pair<Float_t,Long64_t>(1.0,ievt);
461 fSamplingEventList.at( treeIdx ).push_back( p );
465 fSampling.at( treeIdx ) =
true;
474 Int_t treeIdx = TreeIndex( GetCurrentType() );
476 if (!fSampling.at(treeIdx) )
return;
478 if (fSamplingRandom == 0 )
480 <<
"no random generator present for creating a random/importance sampling (initialized?)" <<
Endl;
483 fSamplingSelected.at(treeIdx).clear();
486 std::vector< std::pair< Float_t, Long64_t >* > evtList;
487 std::vector< std::pair< Float_t, Long64_t >* >::iterator evtListIt;
493 evtList.assign( fSamplingEventList.at(treeIdx).begin(), fSamplingEventList.at(treeIdx).end() );
496 for (evtListIt = evtList.begin(); evtListIt != evtList.end(); evtListIt++) {
497 sumWeights += (*evtListIt)->first;
499 evtListIt = evtList.begin();
502 std::vector< Float_t > rnds;
503 rnds.reserve(fSamplingNEvents.at(treeIdx));
506 for (
Int_t i = 0; i < fSamplingNEvents.at(treeIdx); i++) {
507 pos = fSamplingRandom->Rndm()*sumWeights;
508 rnds.push_back( pos );
512 std::sort(rnds.begin(),rnds.end());
515 std::vector< Float_t >::iterator rndsIt = rnds.begin();
516 Float_t runningSum = 0.000000001;
517 for (evtListIt = evtList.begin(); evtListIt != evtList.end();) {
518 runningSum += (*evtListIt)->first;
519 if (runningSum >= (*rndsIt)) {
520 fSamplingSelected.at(treeIdx).push_back( (*evtListIt) );
521 evtListIt = evtList.erase( evtListIt );
524 if (rndsIt == rnds.end() )
break;
539 if (!fSampling.at(fCurrentTreeIdx))
return;
540 if (fSamplingWeight.at(fCurrentTreeIdx) > 0.99999999999)
return;
543 Long64_t stop = fSamplingEventList.at(fCurrentTreeIdx).size() -1;
544 if (evtNumber >= 0) {
548 for (
Long64_t iEvt = start; iEvt <= stop; iEvt++ ){
549 if (
Long64_t(fSamplingEventList.at(fCurrentTreeIdx).size()) < iEvt) {
551 <<
") larger than number of sampled events ("
552 << fSamplingEventList.at(fCurrentTreeIdx).size() <<
" of tree " << fCurrentTreeIdx <<
")" <<
Endl;
555 Float_t weight = fSamplingEventList.at(fCurrentTreeIdx).at( iEvt )->first;
558 weight /= fSamplingWeight.at(fCurrentTreeIdx);
559 if (weight > 1.0 ) weight = 1.0;
563 weight *= fSamplingWeight.at(fCurrentTreeIdx);
565 fSamplingEventList.at(fCurrentTreeIdx).at( iEvt )->first = weight;
583 SetCurrentType(type);
584 const UInt_t t = TreeIndex(type);
585 if (fResults.size() <= t) {
587 <<
" found. Size=" << fResults.size() <<
Endl;
601 char *className =
new char[40];
607 for(
UInt_t i=0; i<fResults.at(t).size(); i++ )
608 metVals[i] =
new Float_t[fdsi.GetNTargets()+fdsi.GetNClasses()];
611 tree->
Branch(
"classID", &cls,
"classID/I" );
612 tree->
Branch(
"className",(
void*)className,
"className/C" );
616 for (std::vector<VariableInfo>::const_iterator itVars = fdsi.GetVariableInfos().begin();
617 itVars != fdsi.GetVariableInfos().end(); itVars++) {
620 tree->
Branch( (*itVars).GetInternalName(), &varVals[
n], (*itVars).GetInternalName()+
TString(
"/F") );
625 for (std::vector<VariableInfo>::const_iterator itTgts = fdsi.GetTargetInfos().begin();
626 itTgts != fdsi.GetTargetInfos().end(); itTgts++) {
628 tree->
Branch( (*itTgts).GetInternalName(), &tgtVals[
n], (*itTgts).GetInternalName()+
TString(
"/F") );
633 for (std::vector<VariableInfo>::const_iterator itVis = fdsi.GetSpectatorInfos().begin();
634 itVis != fdsi.GetSpectatorInfos().end(); itVis++) {
636 tree->
Branch( (*itVis).GetInternalName(), &visVals[
n], (*itVis).GetInternalName()+
TString(
"/F") );
640 tree->
Branch(
"weight", &weight,
"weight/F" );
644 for (std::map< TString, Results* >::iterator itMethod = fResults.at(t).begin();
645 itMethod != fResults.at(t).end(); itMethod++) {
653 tree->
Branch( itMethod->first, &(metVals[n][0]), itMethod->first +
"/F" );
658 for (
UInt_t iCls = 0; iCls < fdsi.GetNClasses(); iCls++) {
659 if (iCls > 0) leafList.
Append(
":" );
660 leafList.
Append( fdsi.GetClassInfo( iCls )->GetName() );
663 Log() <<
kDEBUG <<
"itMethod->first " << itMethod->first <<
" LEAFLIST: "
664 << leafList <<
" itMethod->second " << itMethod->second <<
Endl;
665 tree->
Branch( itMethod->first, (metVals[n]), leafList );
670 for (
UInt_t iTgt = 0; iTgt < fdsi.GetNTargets(); iTgt++) {
671 if (iTgt > 0) leafList.
Append(
":" );
672 leafList.
Append( fdsi.GetTargetInfo( iTgt ).GetInternalName() );
676 Log() <<
kDEBUG <<
"itMethod->first " << itMethod->first <<
" LEAFLIST: "
677 << leafList <<
" itMethod->second " << itMethod->second <<
Endl;
678 tree->
Branch( itMethod->first, (metVals[n]), leafList );
681 Log() <<
kWARNING <<
"Unknown analysis type for result found when writing TestTree." <<
Endl;
688 for (
Long64_t iEvt = 0; iEvt < GetNEvents( type ); iEvt++) {
690 const Event* ev = GetEvent( iEvt );
694 TString tmp = fdsi.GetClassInfo( cls )->GetName();
695 for (
Int_t itmp = 0; itmp < tmp.
Sizeof(); itmp++) {
696 className[itmp] = tmp(itmp);
697 className[itmp+1] = 0;
708 for (std::map<TString, Results*>::iterator itMethod = fResults.at(t).begin();
709 itMethod != fResults.at(t).end(); itMethod++) {
710 Results* results = itMethod->second;
712 const std::vector< Float_t >& vals = results->operator[](iEvt);
716 metVals[
n][0] = vals[0];
720 for (
UInt_t nCls = 0, nClsEnd=fdsi.GetNClasses(); nCls < nClsEnd; nCls++) {
722 metVals[
n][nCls] = val;
727 for (
UInt_t nTgts = 0; nTgts < fdsi.GetNTargets(); nTgts++) {
729 metVals[
n][nTgts] = val;
740 SetCurrentType(savedType);
746 for(
UInt_t i=0; i<fResults.at(t).size(); i++ )
UInt_t GetNSpectators() const
access the number of targets through the datasetinfo
void SetEventCollection(std::vector< Event * > *, Types::ETreeType)
Sets the event collection (by DataSetFactory)
Random number generator class based on M.
MsgLogger & Endl(MsgLogger &ml)
void AddEvent(Event *, Types::ETreeType)
add event to event list after which the event is owned by the dataset
UInt_t GetNTargets() const
accessor to the number of targets
Float_t GetSpectator(UInt_t ivar) const
return spectator content
virtual Int_t Fill()
Fill all branches.
TRandom3 * fSamplingRandom
void SetTreeType(Types::ETreeType type)
UInt_t GetNSpectators() const
accessor to the number of spectators
std::vector< Char_t > fBlockBelongToTraining
void ClearNClassEvents(Int_t type)
Double_t GetWeight() const
return the event weight - depending on whether the flag IgnoreNegWeightsInTraining is or not...
Float_t GetValue(UInt_t ivar) const
return value of i'th variable
Long64_t GetNEvtBkgdTrain()
return number of background training events in dataset
const char * Data() const
virtual ~DataSet()
destructor
TTree * GetTree(Types::ETreeType type)
create the test/trainings tree with all the variables, the weights, the classes, the targets...
TString & Append(const char *cs)
std::vector< std::vector< double > > Data
UInt_t GetNVariables() const
accessor to the number of variables
void MoveTrainingBlock(Int_t blockInd, Types::ETreeType dest, Bool_t applyChanges=kTRUE)
move training block
void ApplyTrainingSetDivision()
apply division of data set
Results * GetResults(const TString &, Types::ETreeType type, Types::EAnalysisType analysistype)
TString info(resultsName+"/"); switch(type) { case Types::kTraining: info += "kTraining/"; break; cas...
std::vector< std::vector< Long64_t > > fClassEvents
Long64_t GetNEvtSigTest()
return number of signal test events in dataset
void DeleteResults(const TString &, Types::ETreeType type, Types::EAnalysisType analysistype)
delete the results stored for this particulary Method instance (here appareantly called resultsName i...
void DivideTrainingSet(UInt_t blockNum)
divide training set
void DestroyCollection(Types::ETreeType type, Bool_t deleteEvents)
destroys the event collection (events + vector)
const TString & GetName() const
virtual const char * GetName() const
Returns name of object.
Long64_t GetNEvtBkgdTest()
return number of background test events in dataset
void CreateSampling() const
create an event sampling (random or importance sampling)
void IncrementNClassEvents(Int_t type, UInt_t classNumber)
const Event * GetEvent() const
std::vector< Char_t > fSampling
void EventResult(Bool_t successful, Long64_t evtNumber=-1)
increase the importance sampling weight of the event when not successful and decrease it when success...
std::vector< Float_t > fSamplingWeight
Long64_t GetNEvtSigTrain()
return number of signal training events in dataset
UInt_t GetNTargets() const
access the number of targets through the datasetinfo
UInt_t GetNVariables() const
access the number of variables through the datasetinfo
std::vector< Int_t > fSamplingNEvents
Long64_t GetNClassEvents(Int_t type, UInt_t classNumber)
Float_t GetTarget(UInt_t itgt) const
virtual Int_t Branch(TCollection *list, Int_t bufsize=32000, Int_t splitlevel=99, const char *name="")
Create one branch for each element in the collection.
#define dest(otri, vertexptr)
virtual Int_t Sizeof() const
Returns size string will occupy on I/O buffer.
virtual Long64_t GetEntries() const
A TTree object has a header with a name and a title.
void InitSampling(Float_t fraction, Float_t weight, UInt_t seed=0)
initialize random or importance sampling