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
74 fHasNegativeEventWeights(
kFALSE),
91 for (
Int_t treeIdx = 0; treeIdx < treeNum; treeIdx++) {
103 fHasNegativeEventWeights(
kFALSE),
105 fTrainingBlockSize(0)
120 for (
Int_t treeIdx = 0; treeIdx < treeNum; treeIdx++) {
139 fBlockBelongToTraining.clear();
141 for (std::vector< std::map< TString, Results* > >::iterator it = fResults.begin(); it != fResults.end(); it++) {
142 for (std::map< TString, Results* >::iterator itMap = (*it).begin(); itMap != (*it).end(); itMap++) {
143 delete itMap->second;
148 if (fSamplingRandom != 0 )
delete fSamplingRandom;
162 if (fClassEvents.size()<(
UInt_t)(type+1)) fClassEvents.resize( type+1 );
163 if (fClassEvents.at( type ).size() < classNumber+1) fClassEvents.at( type ).resize( classNumber+1 );
164 fClassEvents.at( type ).at( classNumber ) += 1;
171 if (fClassEvents.size()<(
UInt_t)(type+1)) fClassEvents.resize( type+1 );
172 fClassEvents.at( type ).clear();
180 return fClassEvents.at(type).at(classNumber);
182 catch (std::out_of_range excpt) {
183 ClassInfo* ci = fdsi->GetClassInfo( classNumber );
184 Log() <<
kFATAL <<
Form(
"Dataset[%s] : ",fdsi->GetName()) <<
"No " << (type==0?
"training":(type==1?
"testing":
"_unknown_type_"))
185 <<
" events for class " << (ci==
NULL?
"_no_name_known_":ci->
GetName()) <<
" (index # "<<classNumber<<
")"
186 <<
" available. Check if all class names are spelled correctly and if events are"
187 <<
" passing the selection cuts." <<
Endl;
190 Log() <<
kFATAL <<
Form(
"Dataset[%s] : ",fdsi->GetName()) <<
"ERROR/CAUGHT : DataSet/GetNClassEvents, .. unknown error" <<
Endl;
200 UInt_t i = TreeIndex(type);
201 if (i>=fEventCollection.size() || fEventCollection[i].size()==0)
return;
204 for (
UInt_t j=0; j<fEventCollection[i].size(); j++)
delete fEventCollection[i][j];
206 fEventCollection[i].clear();
213 if (fSampling.size() >
UInt_t(fCurrentTreeIdx) && fSampling.at(fCurrentTreeIdx)) {
214 Long64_t iEvt = fSamplingSelected.at(fCurrentTreeIdx).at( fCurrentEventIdx ).second;
215 return ((fEventCollection.at(fCurrentTreeIdx))).at(iEvt);
218 return ((fEventCollection.at(fCurrentTreeIdx))).at(fCurrentEventIdx);
227 return fdsi->GetNVariables();
235 return fdsi->GetNTargets();
243 return fdsi->GetNSpectators();
252 fEventCollection.at(
Int_t(type)).push_back(ev);
261 DestroyCollection(type,deleteEvents);
263 const Int_t t = TreeIndex(type);
264 ClearNClassEvents( type );
266 fEventCollection.at(t) = *events;
267 for (std::vector<Event*>::iterator it = fEventCollection.at(t).begin(); it < fEventCollection.at(t).end(); it++) {
268 IncrementNClassEvents( t, (*it)->GetClass() );
290 UInt_t t = TreeIndex(type);
291 if (t<fResults.size()) {
292 const std::map< TString, Results* >& resultsForType = fResults[t];
293 std::map< TString, Results* >::const_iterator it = resultsForType.find(resultsName);
294 if (it!=resultsForType.end()) {
300 fResults.resize(t+1);
306 switch(analysistype) {
326 fResults[t][resultsName] = newresults;
341 if (fResults.empty())
return;
343 if (
UInt_t(type) > fResults.size()){
344 Log()<<
kFATAL<<
Form(
"Dataset[%s] : ",fdsi->GetName()) <<
"you asked for an Treetype (training/testing/...)"
345 <<
" whose index " << type <<
" does not exist " <<
Endl;
347 std::map< TString, Results* >& resultsForType = fResults[
UInt_t(type)];
348 std::map< TString, Results* >::iterator it = resultsForType.find(resultsName);
349 if (it!=resultsForType.end()) {
350 Log() <<
kDEBUG <<
Form(
"Dataset[%s] : ",fdsi->GetName()) <<
" Delete Results previous existing result:" << resultsName
351 <<
" of type " << type <<
Endl;
353 resultsForType.erase(it->first);
356 Log() <<
kINFO <<
Form(
"Dataset[%s] : ",fdsi->GetName()) <<
"could not fine Result class of " << resultsName
357 <<
" of type " << type <<
" which I should have deleted" <<
Endl;
367 if (fBlockBelongToTraining.size() == blockNum)
return;
369 if (fBlockBelongToTraining.size() == 1) {
370 if (fEventCollection[tOrg].size() == 0)
371 fEventCollection[tOrg].resize(fEventCollection[tTrn].size());
372 fEventCollection[tOrg].clear();
373 for (
UInt_t i=0; i<fEventCollection[tTrn].size(); i++)
374 fEventCollection[tOrg].push_back(fEventCollection[tTrn][i]);
375 fClassEvents[tOrg] = fClassEvents[tTrn];
378 fBlockBelongToTraining.clear();
379 for (
UInt_t i=0 ; i < blockNum ; i++) fBlockBelongToTraining.push_back(
kTRUE);
381 ApplyTrainingSetDivision();
390 fEventCollection[tTrn].clear();
391 if (fEventCollection[tVld].size()==0)
392 fEventCollection[tVld].resize(fEventCollection[tOrg].size());
393 fEventCollection[tVld].clear();
396 for (
UInt_t i=0; i<fEventCollection[tOrg].size(); i++) {
397 if (fBlockBelongToTraining[i % fBlockBelongToTraining.size()])
398 fEventCollection[tTrn].push_back(fEventCollection[tOrg][i]);
400 fEventCollection[tVld].push_back(fEventCollection[tOrg][i]);
410 fBlockBelongToTraining[blockInd]=
kFALSE;
412 fBlockBelongToTraining[blockInd]=
kTRUE;
413 if (applyChanges) ApplyTrainingSetDivision();
421 return GetNClassEvents(
Types::kTesting, fdsi->GetClassInfo(
"Signal")->GetNumber() );
429 return GetNClassEvents(
Types::kTesting, fdsi->GetClassInfo(
"Background")->GetNumber() );
437 return GetNClassEvents(
Types::kTraining, fdsi->GetClassInfo(
"Signal")->GetNumber() );
445 return GetNClassEvents(
Types::kTraining, fdsi->GetClassInfo(
"Background")->GetNumber() );
454 if (fSamplingRandom == 0 ) fSamplingRandom =
new TRandom3( seed );
457 std::vector< std::pair< Float_t, Long64_t >* > evtList;
458 std::vector< std::pair< Float_t, Long64_t >* >::iterator it;
460 Int_t treeIdx = TreeIndex( GetCurrentType() );
462 if (fSamplingEventList.size() <
UInt_t(treeIdx+1) ) fSamplingEventList.resize(treeIdx+1);
463 if (fSamplingSelected.size() <
UInt_t(treeIdx+1) ) fSamplingSelected.resize(treeIdx+1);
465 fSamplingEventList.at(treeIdx).clear();
466 fSamplingSelected.at(treeIdx).clear();
468 if (fSampling.size() <
UInt_t(treeIdx+1) ) fSampling.resize(treeIdx+1);
469 if (fSamplingNEvents.size() <
UInt_t(treeIdx+1) ) fSamplingNEvents.resize(treeIdx+1);
470 if (fSamplingWeight.size() <
UInt_t(treeIdx+1) ) fSamplingWeight.resize(treeIdx+1);
472 if (fraction > 0.999999 || fraction < 0.0000001) {
473 fSampling.at( treeIdx ) =
false;
474 fSamplingNEvents.at( treeIdx ) = 0;
475 fSamplingWeight.at( treeIdx ) = 1.0;
480 fSampling.at( treeIdx ) =
false;
482 fSamplingNEvents.at( treeIdx ) =
Int_t(fraction*GetNEvents());
483 fSamplingWeight.at( treeIdx ) = weight;
486 fSamplingEventList.at( treeIdx ).reserve( nEvts );
487 fSamplingSelected.at( treeIdx ).reserve( fSamplingNEvents.at(treeIdx) );
488 for (
Long64_t ievt=0; ievt<nEvts; ievt++) {
489 std::pair<Float_t,Long64_t> p(1.0,ievt);
490 fSamplingEventList.at( treeIdx ).push_back( p );
494 fSampling.at( treeIdx ) =
true;
503 Int_t treeIdx = TreeIndex( GetCurrentType() );
505 if (!fSampling.at(treeIdx) )
return;
507 if (fSamplingRandom == 0 )
509 <<
"no random generator present for creating a random/importance sampling (initialized?)" <<
Endl;
512 fSamplingSelected.at(treeIdx).clear();
515 std::vector< std::pair< Float_t, Long64_t > > evtList;
516 std::vector< std::pair< Float_t, Long64_t > >::iterator evtListIt;
522 evtList.assign( fSamplingEventList.at(treeIdx).begin(), fSamplingEventList.at(treeIdx).end() );
525 for (evtListIt = evtList.begin(); evtListIt != evtList.end(); evtListIt++) {
526 sumWeights += (*evtListIt).first;
528 evtListIt = evtList.begin();
531 std::vector< Float_t > rnds;
532 rnds.reserve(fSamplingNEvents.at(treeIdx));
535 for (
Int_t i = 0; i < fSamplingNEvents.at(treeIdx); i++) {
536 pos = fSamplingRandom->Rndm()*sumWeights;
537 rnds.push_back( pos );
541 std::sort(rnds.begin(),rnds.end());
544 std::vector< Float_t >::iterator rndsIt = rnds.begin();
545 Float_t runningSum = 0.000000001;
546 for (evtListIt = evtList.begin(); evtListIt != evtList.end();) {
547 runningSum += (*evtListIt).first;
548 if (runningSum >= (*rndsIt)) {
549 fSamplingSelected.at(treeIdx).push_back( (*evtListIt) );
550 evtListIt = evtList.erase( evtListIt );
553 if (rndsIt == rnds.end() )
break;
568 if (!fSampling.at(fCurrentTreeIdx))
return;
569 if (fSamplingWeight.at(fCurrentTreeIdx) > 0.99999999999)
return;
572 Long64_t stop = fSamplingEventList.at(fCurrentTreeIdx).size() -1;
573 if (evtNumber >= 0) {
577 for (
Long64_t iEvt = start; iEvt <= stop; iEvt++ ){
578 if (
Long64_t(fSamplingEventList.at(fCurrentTreeIdx).size()) < iEvt) {
579 Log() <<
kWARNING <<
Form(
"Dataset[%s] : ",fdsi->GetName()) <<
"event number (" << iEvt
580 <<
") larger than number of sampled events ("
581 << fSamplingEventList.at(fCurrentTreeIdx).size() <<
" of tree " << fCurrentTreeIdx <<
")" <<
Endl;
584 Float_t weight = fSamplingEventList.at(fCurrentTreeIdx).at( iEvt ).first;
587 weight /= fSamplingWeight.at(fCurrentTreeIdx);
588 if (weight > 1.0 ) weight = 1.0;
592 weight *= fSamplingWeight.at(fCurrentTreeIdx);
594 fSamplingEventList.at(fCurrentTreeIdx).at( iEvt ).first = weight;
612 SetCurrentType(type);
613 const UInt_t t = TreeIndex(type);
614 if (fResults.size() <= t) {
616 <<
" found. Size=" << fResults.size() <<
Endl;
630 char *className =
new char[40];
636 for(
UInt_t i=0; i<fResults.at(t).size(); i++ )
637 metVals[i] =
new Float_t[fdsi->GetNTargets()+fdsi->GetNClasses()];
640 tree->
Branch(
"classID", &cls,
"classID/I" );
641 tree->
Branch(
"className",(
void*)className,
"className/C" );
645 for (std::vector<VariableInfo>::const_iterator itVars = fdsi->GetVariableInfos().begin();
646 itVars != fdsi->GetVariableInfos().end(); itVars++) {
649 tree->
Branch( (*itVars).GetInternalName(), &varVals[
n], (*itVars).GetInternalName()+
TString(
"/F") );
654 for (std::vector<VariableInfo>::const_iterator itTgts = fdsi->GetTargetInfos().begin();
655 itTgts != fdsi->GetTargetInfos().end(); itTgts++) {
657 tree->
Branch( (*itTgts).GetInternalName(), &tgtVals[
n], (*itTgts).GetInternalName()+
TString(
"/F") );
662 for (std::vector<VariableInfo>::const_iterator itVis = fdsi->GetSpectatorInfos().begin();
663 itVis != fdsi->GetSpectatorInfos().end(); itVis++) {
665 tree->
Branch( (*itVis).GetInternalName(), &visVals[
n], (*itVis).GetInternalName()+
TString(
"/F") );
669 tree->
Branch(
"weight", &weight,
"weight/F" );
673 for (std::map< TString, Results* >::iterator itMethod = fResults.at(t).begin();
674 itMethod != fResults.at(t).end(); itMethod++) {
677 Log() <<
kDEBUG <<
Form(
"Dataset[%s] : ",fdsi->GetName()) <<
"analysis type: " << (itMethod->second->GetAnalysisType()==
Types::kRegression ?
"Regression" :
682 tree->
Branch( itMethod->first, &(metVals[n][0]), itMethod->first +
"/F" );
687 for (
UInt_t iCls = 0; iCls < fdsi->GetNClasses(); iCls++) {
688 if (iCls > 0) leafList.
Append(
":" );
689 leafList.
Append( fdsi->GetClassInfo( iCls )->GetName() );
692 Log() <<
kDEBUG <<
Form(
"Dataset[%s] : ",fdsi->GetName()) <<
"itMethod->first " << itMethod->first <<
" LEAFLIST: "
693 << leafList <<
" itMethod->second " << itMethod->second << Endl;
694 tree->
Branch( itMethod->first, (metVals[n]), leafList );
699 for (
UInt_t iTgt = 0; iTgt < fdsi->GetNTargets(); iTgt++) {
700 if (iTgt > 0) leafList.
Append(
":" );
701 leafList.
Append( fdsi->GetTargetInfo( iTgt ).GetInternalName() );
705 Log() <<
kDEBUG <<
Form(
"Dataset[%s] : ",fdsi->GetName()) <<
"itMethod->first " << itMethod->first <<
" LEAFLIST: "
706 << leafList <<
" itMethod->second " << itMethod->second << Endl;
707 tree->
Branch( itMethod->first, (metVals[n]), leafList );
710 Log() <<
kWARNING <<
Form(
"Dataset[%s] : ",fdsi->GetName()) <<
"Unknown analysis type for result found when writing TestTree." << Endl;
717 for (
Long64_t iEvt = 0; iEvt < GetNEvents( type ); iEvt++) {
719 const Event* ev = GetEvent( iEvt );
723 TString tmp = fdsi->GetClassInfo( cls )->GetName();
724 for (
Int_t itmp = 0; itmp < tmp.
Sizeof(); itmp++) {
725 className[itmp] = tmp(itmp);
726 className[itmp+1] = 0;
737 for (std::map<TString, Results*>::iterator itMethod = fResults.at(t).begin();
738 itMethod != fResults.at(t).end(); itMethod++) {
739 Results* results = itMethod->second;
741 const std::vector< Float_t >& vals = results->operator[](iEvt);
745 metVals[
n][0] = vals[0];
749 for (
UInt_t nCls = 0, nClsEnd=fdsi->GetNClasses(); nCls < nClsEnd; nCls++) {
751 metVals[
n][nCls] = val;
756 for (
UInt_t nTgts = 0; nTgts < fdsi->GetNTargets(); nTgts++) {
758 metVals[
n][nTgts] = val;
768 <<
"Created tree '" << tree->
GetName() <<
"' with " << tree->
GetEntries() <<
" events" << Endl <<
Endl;
770 SetCurrentType(savedType);
776 for(
UInt_t i=0; i<fResults.at(t).size(); i++ )
UInt_t GetNSpectators() const
access the number of targets through the datasetinfo
std::string GetName(const std::string &scope_name)
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
The TNamed class is the base class for all named ROOT classes.
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...
char * Form(const char *fmt,...)
void DivideTrainingSet(UInt_t blockNum)
divide training set
void DestroyCollection(Types::ETreeType type, Bool_t deleteEvents)
destroys the event collection (events + vector)
virtual const char * GetName() const
Returns name of object.
void SetEventCollection(std::vector< Event * > *, Types::ETreeType, Bool_t deleteEvents=true)
Sets the event collection (by DataSetFactory)
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