64 fHasNegativeEventWeights(
kFALSE),
81 for (
Int_t treeIdx = 0; treeIdx < treeNum; treeIdx++) {
96 fHasNegativeEventWeights(
kFALSE),
113 for (
Int_t treeIdx = 0; treeIdx < treeNum; treeIdx++) {
130 fBlockBelongToTraining.clear();
132 for (std::vector< std::map< TString, Results* > >::iterator it = fResults.begin(); it != fResults.end(); it++) {
133 for (std::map< TString, Results* >::iterator itMap = (*it).begin(); itMap != (*it).end(); itMap++) {
134 delete itMap->second;
139 if (fSamplingRandom != 0 )
delete fSamplingRandom;
153 if (fClassEvents.size()<(
UInt_t)(type+1)) fClassEvents.resize( type+1 );
154 if (fClassEvents.at( type ).size() < classNumber+1) fClassEvents.at( type ).resize( classNumber+1 );
155 fClassEvents.at( type ).at( classNumber ) += 1;
162 if (fClassEvents.size()<(
UInt_t)(type+1)) fClassEvents.resize( type+1 );
163 fClassEvents.at( type ).clear();
171 return fClassEvents.at(type).at(classNumber);
173 catch (std::out_of_range excpt) {
174 ClassInfo* ci = fdsi->GetClassInfo( classNumber );
175 Log() << kFATAL <<
Form(
"Dataset[%s] : ",fdsi->GetName()) <<
"No " << (type==0?
"training":(type==1?
"testing":
"_unknown_type_"))
176 <<
" events for class " << (ci==
NULL?
"_no_name_known_":ci->
GetName()) <<
" (index # "<<classNumber<<
")"
177 <<
" available. Check if all class names are spelled correctly and if events are"
178 <<
" passing the selection cuts." <<
Endl;
181 Log() << kFATAL <<
Form(
"Dataset[%s] : ",fdsi->GetName()) <<
"ERROR/CAUGHT : DataSet/GetNClassEvents, .. unknown error" <<
Endl;
191 UInt_t i = TreeIndex(type);
192 if (i>=fEventCollection.size() || fEventCollection[i].size()==0)
return;
195 for (
UInt_t j=0; j<fEventCollection[i].size(); j++)
delete fEventCollection[i][j];
197 fEventCollection[i].clear();
204 if (fSampling.size() >
UInt_t(fCurrentTreeIdx) && fSampling.at(fCurrentTreeIdx)) {
205 Long64_t iEvt = fSamplingSelected.at(fCurrentTreeIdx).at( fCurrentEventIdx ).second;
206 return ((fEventCollection.at(fCurrentTreeIdx))).at(iEvt);
209 return ((fEventCollection.at(fCurrentTreeIdx))).at(fCurrentEventIdx);
218 return fdsi->GetNVariables();
226 return fdsi->GetNTargets();
234 return fdsi->GetNSpectators();
243 fEventCollection.at(
Int_t(type)).push_back(ev);
252 DestroyCollection(type,deleteEvents);
254 const Int_t t = TreeIndex(type);
255 ClearNClassEvents( type );
257 fEventCollection.at(t) = *events;
258 for (std::vector<Event*>::iterator it = fEventCollection.at(t).begin(); it < fEventCollection.at(t).end(); it++) {
259 IncrementNClassEvents( t, (*it)->GetClass() );
269 UInt_t t = TreeIndex(type);
270 if (t<fResults.size()) {
271 const std::map< TString, Results* >& resultsForType = fResults[t];
272 std::map< TString, Results* >::const_iterator it = resultsForType.find(resultsName);
273 if (it!=resultsForType.end()) {
279 fResults.resize(t+1);
285 switch(analysistype) {
305 fResults[t][resultsName] = newresults;
320 if (fResults.empty())
return;
322 if (
UInt_t(type) > fResults.size()){
323 Log()<<kFATAL<<
Form(
"Dataset[%s] : ",fdsi->GetName()) <<
"you asked for an Treetype (training/testing/...)"
324 <<
" whose index " << type <<
" does not exist " <<
Endl;
326 std::map< TString, Results* >& resultsForType = fResults[
UInt_t(type)];
327 std::map< TString, Results* >::iterator it = resultsForType.find(resultsName);
328 if (it!=resultsForType.end()) {
329 Log() << kDEBUG <<
Form(
"Dataset[%s] : ",fdsi->GetName()) <<
" Delete Results previous existing result:" << resultsName
330 <<
" of type " << type <<
Endl;
332 resultsForType.erase(it->first);
335 Log() << kINFO <<
Form(
"Dataset[%s] : ",fdsi->GetName()) <<
"could not fine Result class of " << resultsName
336 <<
" of type " << type <<
" which I should have deleted" <<
Endl;
346 if (fBlockBelongToTraining.size() == blockNum)
return;
348 if (fBlockBelongToTraining.size() == 1) {
349 if (fEventCollection[tOrg].size() == 0)
350 fEventCollection[tOrg].resize(fEventCollection[tTrn].size());
351 fEventCollection[tOrg].clear();
352 for (
UInt_t i=0; i<fEventCollection[tTrn].size(); i++)
353 fEventCollection[tOrg].push_back(fEventCollection[tTrn][i]);
354 fClassEvents[tOrg] = fClassEvents[tTrn];
357 fBlockBelongToTraining.clear();
358 for (
UInt_t i=0 ; i < blockNum ; i++) fBlockBelongToTraining.push_back(
kTRUE);
360 ApplyTrainingSetDivision();
369 fEventCollection[tTrn].clear();
370 if (fEventCollection[tVld].size()==0)
371 fEventCollection[tVld].resize(fEventCollection[tOrg].size());
372 fEventCollection[tVld].clear();
375 for (
UInt_t i=0; i<fEventCollection[tOrg].size(); i++) {
376 if (fBlockBelongToTraining[i % fBlockBelongToTraining.size()])
377 fEventCollection[tTrn].push_back(fEventCollection[tOrg][i]);
379 fEventCollection[tVld].push_back(fEventCollection[tOrg][i]);
389 fBlockBelongToTraining[blockInd]=
kFALSE;
391 fBlockBelongToTraining[blockInd]=
kTRUE;
392 if (applyChanges) ApplyTrainingSetDivision();
400 return GetNClassEvents(
Types::kTesting, fdsi->GetClassInfo(
"Signal")->GetNumber() );
408 return GetNClassEvents(
Types::kTesting, fdsi->GetClassInfo(
"Background")->GetNumber() );
416 return GetNClassEvents(
Types::kTraining, fdsi->GetClassInfo(
"Signal")->GetNumber() );
424 return GetNClassEvents(
Types::kTraining, fdsi->GetClassInfo(
"Background")->GetNumber() );
433 if (fSamplingRandom == 0 ) fSamplingRandom =
new TRandom3( seed );
436 std::vector< std::pair< Float_t, Long64_t >* > evtList;
437 std::vector< std::pair< Float_t, Long64_t >* >::iterator it;
439 Int_t treeIdx = TreeIndex( GetCurrentType() );
441 if (fSamplingEventList.size() <
UInt_t(treeIdx+1) ) fSamplingEventList.resize(treeIdx+1);
442 if (fSamplingSelected.size() <
UInt_t(treeIdx+1) ) fSamplingSelected.resize(treeIdx+1);
444 fSamplingEventList.at(treeIdx).clear();
445 fSamplingSelected.at(treeIdx).clear();
447 if (fSampling.size() <
UInt_t(treeIdx+1) ) fSampling.resize(treeIdx+1);
448 if (fSamplingNEvents.size() <
UInt_t(treeIdx+1) ) fSamplingNEvents.resize(treeIdx+1);
449 if (fSamplingWeight.size() <
UInt_t(treeIdx+1) ) fSamplingWeight.resize(treeIdx+1);
451 if (fraction > 0.999999 || fraction < 0.0000001) {
452 fSampling.at( treeIdx ) =
false;
453 fSamplingNEvents.at( treeIdx ) = 0;
454 fSamplingWeight.at( treeIdx ) = 1.0;
459 fSampling.at( treeIdx ) =
false;
461 fSamplingNEvents.at( treeIdx ) =
Int_t(fraction*GetNEvents());
462 fSamplingWeight.at( treeIdx ) = weight;
465 fSamplingEventList.at( treeIdx ).reserve( nEvts );
466 fSamplingSelected.at( treeIdx ).reserve( fSamplingNEvents.at(treeIdx) );
467 for (
Long64_t ievt=0; ievt<nEvts; ievt++) {
468 std::pair<Float_t,Long64_t> p(1.0,ievt);
469 fSamplingEventList.at( treeIdx ).push_back( p );
473 fSampling.at( treeIdx ) =
true;
482 Int_t treeIdx = TreeIndex( GetCurrentType() );
484 if (!fSampling.at(treeIdx) )
return;
486 if (fSamplingRandom == 0 )
487 Log() << kFATAL<<
Form(
"Dataset[%s] : ",fdsi->GetName())
488 <<
"no random generator present for creating a random/importance sampling (initialized?)" <<
Endl;
491 fSamplingSelected.at(treeIdx).clear();
494 std::vector< std::pair< Float_t, Long64_t > > evtList;
495 std::vector< std::pair< Float_t, Long64_t > >::iterator evtListIt;
501 evtList.assign( fSamplingEventList.at(treeIdx).begin(), fSamplingEventList.at(treeIdx).end() );
504 for (evtListIt = evtList.begin(); evtListIt != evtList.end(); evtListIt++) {
505 sumWeights += (*evtListIt).first;
507 evtListIt = evtList.begin();
510 std::vector< Float_t > rnds;
511 rnds.reserve(fSamplingNEvents.at(treeIdx));
514 for (
Int_t i = 0; i < fSamplingNEvents.at(treeIdx); i++) {
515 pos = fSamplingRandom->Rndm()*sumWeights;
516 rnds.push_back( pos );
520 std::sort(rnds.begin(),rnds.end());
523 std::vector< Float_t >::iterator rndsIt = rnds.begin();
524 Float_t runningSum = 0.000000001;
525 for (evtListIt = evtList.begin(); evtListIt != evtList.end();) {
526 runningSum += (*evtListIt).first;
527 if (runningSum >= (*rndsIt)) {
528 fSamplingSelected.at(treeIdx).push_back( (*evtListIt) );
529 evtListIt = evtList.erase( evtListIt );
532 if (rndsIt == rnds.end() )
break;
547 if (!fSampling.at(fCurrentTreeIdx))
return;
548 if (fSamplingWeight.at(fCurrentTreeIdx) > 0.99999999999)
return;
551 Long64_t stop = fSamplingEventList.at(fCurrentTreeIdx).size() -1;
552 if (evtNumber >= 0) {
556 for (
Long64_t iEvt = start; iEvt <= stop; iEvt++ ){
557 if (
Long64_t(fSamplingEventList.at(fCurrentTreeIdx).size()) < iEvt) {
558 Log() << kWARNING <<
Form(
"Dataset[%s] : ",fdsi->GetName()) <<
"event number (" << iEvt
559 <<
") larger than number of sampled events ("
560 << fSamplingEventList.at(fCurrentTreeIdx).size() <<
" of tree " << fCurrentTreeIdx <<
")" <<
Endl;
563 Float_t weight = fSamplingEventList.at(fCurrentTreeIdx).at( iEvt ).first;
566 weight /= fSamplingWeight.at(fCurrentTreeIdx);
567 if (weight > 1.0 ) weight = 1.0;
571 weight *= fSamplingWeight.at(fCurrentTreeIdx);
573 fSamplingEventList.at(fCurrentTreeIdx).at( iEvt ).first = weight;
583 Log() << kDEBUG <<
Form(
"Dataset[%s] : ",fdsi->GetName()) <<
"GetTree(" << ( type==
Types::kTraining ?
"training" :
"testing" ) <<
")" <<
Endl;
591 SetCurrentType(type);
592 const UInt_t t = TreeIndex(type);
593 if (fResults.size() <= t) {
594 Log() << kWARNING <<
Form(
"Dataset[%s] : ",fdsi->GetName()) <<
"No results for treetype " << ( type==
Types::kTraining ?
"training" :
"testing" )
595 <<
" found. Size=" << fResults.size() <<
Endl;
615 for(
UInt_t i=0; i<fResults.at(t).size(); i++ )
616 metVals[i] =
new Float_t[fdsi->GetNTargets()+fdsi->GetNClasses()];
619 tree->
Branch(
"classID", &cls,
"classID/I" );
620 tree->
Branch(
"className", className,
"className/C" );
624 for (std::vector<VariableInfo>::const_iterator itVars = fdsi->GetVariableInfos().begin();
625 itVars != fdsi->GetVariableInfos().end(); itVars++) {
628 tree->
Branch( (*itVars).GetInternalName(), &varVals[
n], (*itVars).GetInternalName()+
TString(
"/F") );
633 for (std::vector<VariableInfo>::const_iterator itTgts = fdsi->GetTargetInfos().begin();
634 itTgts != fdsi->GetTargetInfos().end(); itTgts++) {
636 tree->
Branch( (*itTgts).GetInternalName(), &tgtVals[
n], (*itTgts).GetInternalName()+
TString(
"/F") );
641 for (std::vector<VariableInfo>::const_iterator itVis = fdsi->GetSpectatorInfos().begin();
642 itVis != fdsi->GetSpectatorInfos().end(); itVis++) {
644 tree->
Branch( (*itVis).GetInternalName(), &visVals[
n], (*itVis).GetInternalName()+
TString(
"/F") );
648 tree->
Branch(
"weight", &weight,
"weight/F" );
652 for (std::map< TString, Results* >::iterator itMethod = fResults.at(t).begin();
653 itMethod != fResults.at(t).end(); itMethod++) {
656 Log() << kDEBUG <<
Form(
"Dataset[%s] : ",fdsi->GetName()) <<
"analysis type: " << (itMethod->second->GetAnalysisType()==
Types::kRegression ?
"Regression" :
661 tree->
Branch( itMethod->first, &(metVals[n][0]), itMethod->first +
"/F" );
666 for (
UInt_t iCls = 0; iCls < fdsi->GetNClasses(); iCls++) {
667 if (iCls > 0) leafList.
Append(
":" );
668 leafList.
Append( fdsi->GetClassInfo( iCls )->GetName() );
671 Log() << kDEBUG <<
Form(
"Dataset[%s] : ",fdsi->GetName()) <<
"itMethod->first " << itMethod->first <<
" LEAFLIST: "
672 << leafList <<
" itMethod->second " << itMethod->second << Endl;
673 tree->
Branch( itMethod->first, (metVals[n]), leafList );
678 for (
UInt_t iTgt = 0; iTgt < fdsi->GetNTargets(); iTgt++) {
679 if (iTgt > 0) leafList.
Append(
":" );
680 leafList.
Append( fdsi->GetTargetInfo( iTgt ).GetInternalName() );
684 Log() << kDEBUG <<
Form(
"Dataset[%s] : ",fdsi->GetName()) <<
"itMethod->first " << itMethod->first <<
" LEAFLIST: "
685 << leafList <<
" itMethod->second " << itMethod->second << Endl;
686 tree->
Branch( itMethod->first, (metVals[n]), leafList );
689 Log() << kWARNING <<
Form(
"Dataset[%s] : ",fdsi->GetName()) <<
"Unknown analysis type for result found when writing TestTree." << Endl;
696 for (
Long64_t iEvt = 0; iEvt < GetNEvents( type ); iEvt++) {
698 const Event* ev = GetEvent( iEvt );
702 strlcpy(className, fdsi->GetClassInfo( cls )->GetName(),
sizeof(className));
712 for (std::map<TString, Results*>::iterator itMethod = fResults.at(t).begin();
713 itMethod != fResults.at(t).end(); itMethod++) {
714 Results* results = itMethod->second;
716 const std::vector< Float_t >& vals = results->operator[](iEvt);
720 metVals[
n][0] = vals[0];
724 for (
UInt_t nCls = 0, nClsEnd=fdsi->GetNClasses(); nCls < nClsEnd; nCls++) {
726 metVals[
n][nCls] = val;
731 for (
UInt_t nTgts = 0; nTgts < fdsi->GetNTargets(); nTgts++) {
733 metVals[
n][nTgts] = val;
743 <<
"Created tree '" << tree->
GetName() <<
"' with " << tree->
GetEntries() <<
" events" << Endl <<
Endl;
745 SetCurrentType(savedType);
751 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)
Class that is the base-class for a vector of result.
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
Class that contains all the information of a class.
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
Class that contains all the data information.
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)
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 particular Method instance.
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
Class which takes the results of a multiclass classification.
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)
ostringstream derivative to redirect and format output
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)
Class that is the base-class for a vector of result.
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
Class that is the base-class for a vector of result.