59 :
TNamed(dsi.GetName(),
"DataSet"),
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 &) {
174 ClassInfo* ci = fdsi->GetClassInfo( classNumber );
175 Log() << kFATAL <<
TString::Format(
"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 <<
TString::Format(
"Dataset[%s] : ",fdsi->GetName()) <<
"ERROR/CAUGHT : DataSet/GetNClassEvents, .. unknown error" <<
Endl;
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);
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() );
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;
323 Log()<<kFATAL<<
TString::Format(
"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 <<
TString::Format(
"Dataset[%s] : ",fdsi->GetName()) <<
" Delete Results previous existing result:" << resultsName
332 resultsForType.erase(it->first);
335 Log() << kINFO <<
TString::Format(
"Dataset[%s] : ",fdsi->GetName()) <<
"could not fine Result class of " << resultsName
336 <<
" of type " <<
type <<
" which I should have deleted" <<
Endl;
346 if (fResults.empty())
return;
349 Log()<<kFATAL<<
TString::Format(
"Dataset[%s] : ",fdsi->GetName()) <<
"you asked for an Treetype (training/testing/...)"
350 <<
" whose index " <<
type <<
" does not exist " <<
Endl;
353 std::map<TString, Results *> & resultsForType = fResults[
UInt_t(
type)];
355 for (
auto && it : resultsForType) {
356 auto & resultsName = it.first;
359 <<
" DeleteAllResults previous existing result: "
360 << resultsName <<
" of type " <<
type <<
Endl;
365 resultsForType.clear();
375 if (fBlockBelongToTraining.size() == blockNum)
return;
377 if (fBlockBelongToTraining.size() == 1) {
378 if (fEventCollection[tOrg].
size() == 0)
379 fEventCollection[tOrg].resize(fEventCollection[tTrn].
size());
380 fEventCollection[tOrg].clear();
381 for (
UInt_t i=0; i<fEventCollection[tTrn].size(); i++)
382 fEventCollection[tOrg].push_back(fEventCollection[tTrn][i]);
383 fClassEvents[tOrg] = fClassEvents[tTrn];
386 fBlockBelongToTraining.clear();
387 for (
UInt_t i=0 ; i < blockNum ; i++) fBlockBelongToTraining.push_back(
kTRUE);
389 ApplyTrainingSetDivision();
398 fEventCollection[tTrn].clear();
399 if (fEventCollection[tVld].
size()==0)
400 fEventCollection[tVld].resize(fEventCollection[tOrg].
size());
401 fEventCollection[tVld].clear();
404 for (
UInt_t i=0; i<fEventCollection[tOrg].size(); i++) {
405 if (fBlockBelongToTraining[i % fBlockBelongToTraining.size()])
406 fEventCollection[tTrn].push_back(fEventCollection[tOrg][i]);
408 fEventCollection[tVld].push_back(fEventCollection[tOrg][i]);
418 fBlockBelongToTraining[blockInd]=
kFALSE;
420 fBlockBelongToTraining[blockInd]=
kTRUE;
421 if (applyChanges) ApplyTrainingSetDivision();
429 return GetNClassEvents(
Types::kTesting, fdsi->GetClassInfo(
"Signal")->GetNumber() );
437 return GetNClassEvents(
Types::kTesting, fdsi->GetClassInfo(
"Background")->GetNumber() );
445 return GetNClassEvents(
Types::kTraining, fdsi->GetClassInfo(
"Signal")->GetNumber() );
453 return GetNClassEvents(
Types::kTraining, fdsi->GetClassInfo(
"Background")->GetNumber() );
462 if (fSamplingRandom == 0 ) fSamplingRandom =
new TRandom3( seed );
465 std::vector< std::pair< Float_t, Long64_t >* > evtList;
467 Int_t treeIdx = TreeIndex( GetCurrentType() );
469 if (fSamplingEventList.size() <
UInt_t(treeIdx+1) ) fSamplingEventList.resize(treeIdx+1);
470 if (fSamplingSelected.size() <
UInt_t(treeIdx+1) ) fSamplingSelected.resize(treeIdx+1);
472 fSamplingEventList.at(treeIdx).clear();
473 fSamplingSelected.at(treeIdx).clear();
475 if (fSampling.size() <
UInt_t(treeIdx+1) ) fSampling.resize(treeIdx+1);
476 if (fSamplingNEvents.size() <
UInt_t(treeIdx+1) ) fSamplingNEvents.resize(treeIdx+1);
477 if (fSamplingWeight.size() <
UInt_t(treeIdx+1) ) fSamplingWeight.resize(treeIdx+1);
479 if (fraction > 0.999999 || fraction < 0.0000001) {
480 fSampling.at( treeIdx ) =
false;
481 fSamplingNEvents.at( treeIdx ) = 0;
482 fSamplingWeight.at( treeIdx ) = 1.0;
487 fSampling.at( treeIdx ) =
false;
489 fSamplingNEvents.at( treeIdx ) =
Int_t(fraction*GetNEvents());
490 fSamplingWeight.at( treeIdx ) = weight;
493 fSamplingEventList.at( treeIdx ).reserve( nEvts );
494 fSamplingSelected.at( treeIdx ).reserve( fSamplingNEvents.at(treeIdx) );
495 for (
Long64_t ievt=0; ievt<nEvts; ievt++) {
496 std::pair<Float_t,Long64_t>
p(1.0,ievt);
497 fSamplingEventList.at( treeIdx ).push_back(
p );
501 fSampling.at( treeIdx ) =
true;
510 Int_t treeIdx = TreeIndex( GetCurrentType() );
512 if (!fSampling.at(treeIdx) )
return;
514 if (fSamplingRandom == 0 )
516 <<
"no random generator present for creating a random/importance sampling (initialized?)" <<
Endl;
519 fSamplingSelected.at(treeIdx).clear();
522 std::vector< std::pair< Float_t, Long64_t > > evtList;
523 std::vector< std::pair< Float_t, Long64_t > >::iterator evtListIt;
529 evtList.assign( fSamplingEventList.at(treeIdx).begin(), fSamplingEventList.at(treeIdx).end() );
532 for (evtListIt = evtList.begin(); evtListIt != evtList.end(); ++evtListIt) {
533 sumWeights += (*evtListIt).first;
535 evtListIt = evtList.begin();
538 std::vector< Float_t > rnds;
539 rnds.reserve(fSamplingNEvents.at(treeIdx));
542 for (
Int_t i = 0; i < fSamplingNEvents.at(treeIdx); i++) {
543 pos = fSamplingRandom->Rndm()*sumWeights;
544 rnds.push_back( pos );
548 std::sort(rnds.begin(),rnds.end());
551 std::vector< Float_t >::iterator rndsIt = rnds.begin();
552 Float_t runningSum = 0.000000001;
553 for (evtListIt = evtList.begin(); evtListIt != evtList.end();) {
554 runningSum += (*evtListIt).first;
555 if (runningSum >= (*rndsIt)) {
556 fSamplingSelected.at(treeIdx).push_back( (*evtListIt) );
557 evtListIt = evtList.erase( evtListIt );
560 if (rndsIt == rnds.end() )
break;
575 if (!fSampling.at(fCurrentTreeIdx))
return;
576 if (fSamplingWeight.at(fCurrentTreeIdx) > 0.99999999999)
return;
579 Long64_t stop = fSamplingEventList.at(fCurrentTreeIdx).size() -1;
580 if (evtNumber >= 0) {
584 for (
Long64_t iEvt = start; iEvt <= stop; iEvt++ ){
585 if (
Long64_t(fSamplingEventList.at(fCurrentTreeIdx).size()) < iEvt) {
586 Log() << kWARNING <<
TString::Format(
"Dataset[%s] : ",fdsi->GetName()) <<
"event number (" << iEvt
587 <<
") larger than number of sampled events ("
588 << fSamplingEventList.at(fCurrentTreeIdx).size() <<
" of tree " << fCurrentTreeIdx <<
")" <<
Endl;
591 Float_t weight = fSamplingEventList.at(fCurrentTreeIdx).at( iEvt ).first;
594 weight /= fSamplingWeight.at(fCurrentTreeIdx);
595 if (weight > 1.0 ) weight = 1.0;
599 weight *= fSamplingWeight.at(fCurrentTreeIdx);
601 fSamplingEventList.at(fCurrentTreeIdx).at( iEvt ).first = weight;
619 SetCurrentType(
type);
621 if (fResults.size() <= t) {
623 <<
" found. Size=" << fResults.size() <<
Endl;
643 for(
UInt_t i=0; i<fResults.at(t).
size(); i++ )
644 metVals[i] =
new Float_t[fdsi->GetNTargets()+fdsi->GetNClasses()];
647 tree->Branch(
"classID", &cls,
"classID/I" );
648 tree->Branch(
"className", className,
"className/C" );
652 Int_t ivar_array = 0;
653 Int_t arraySize = -1;
654 for (std::vector<VariableInfo>::const_iterator itVars = fdsi->GetVariableInfos().begin();
655 itVars != fdsi->GetVariableInfos().end(); ++itVars) {
659 tree->Branch( (*itVars).GetInternalName(), &varVals[
n], (*itVars).GetInternalName()+
TString(
"/F") );
662 if (ivar_array == 0) {
665 arraySize = fdsi->GetVarArraySize((*itVars).GetExpression());
667 Log() << kDEBUG <<
"creating branch for array " <<
name <<
" with size " << arraySize <<
Endl;
670 if (ivar_array == arraySize)
677 for (std::vector<VariableInfo>::const_iterator itTgts = fdsi->GetTargetInfos().begin();
678 itTgts != fdsi->GetTargetInfos().end(); ++itTgts) {
680 tree->Branch( (*itTgts).GetInternalName(), &tgtVals[
n], (*itTgts).GetInternalName()+
TString(
"/F") );
685 for (std::vector<VariableInfo>::const_iterator itVis = fdsi->GetSpectatorInfos().begin();
686 itVis != fdsi->GetSpectatorInfos().end(); ++itVis) {
688 tree->Branch( (*itVis).GetInternalName(), &visVals[
n], (*itVis).GetInternalName()+
TString(
"/F") );
692 tree->Branch(
"weight", &weight,
"weight/F" );
696 for (std::map< TString, Results* >::iterator itMethod = fResults.at(t).begin();
697 itMethod != fResults.at(t).end(); ++itMethod) {
701 <<
"analysis type: " << (itMethod->second->GetAnalysisType()==
Types::kRegression ?
"Regression" :
706 tree->Branch( itMethod->first, &(metVals[
n][0]), itMethod->first +
"/F" );
711 for (
UInt_t iCls = 0; iCls < fdsi->GetNClasses(); iCls++) {
712 if (iCls > 0) leafList.
Append(
":" );
713 leafList.
Append( fdsi->GetClassInfo( iCls )->GetName() );
716 Log() << kDEBUG <<
TString::Format(
"Dataset[%s] : ",fdsi->GetName()) <<
"itMethod->first " << itMethod->first <<
" LEAFLIST: "
717 << leafList <<
" itMethod->second " << itMethod->second <<
Endl;
718 tree->Branch( itMethod->first, (metVals[
n]), leafList );
723 for (
UInt_t iTgt = 0; iTgt < fdsi->GetNTargets(); iTgt++) {
724 if (iTgt > 0) leafList.
Append(
":" );
725 leafList.
Append( fdsi->GetTargetInfo( iTgt ).GetInternalName() );
729 Log() << kDEBUG <<
TString::Format(
"Dataset[%s] : ",fdsi->GetName()) <<
"itMethod->first " << itMethod->first <<
" LEAFLIST: "
730 << leafList <<
" itMethod->second " << itMethod->second <<
Endl;
731 tree->Branch( itMethod->first, (metVals[
n]), leafList );
734 Log() << kWARNING <<
TString::Format(
"Dataset[%s] : ",fdsi->GetName()) <<
"Unknown analysis type for result found when writing TestTree." <<
Endl;
741 for (
auto && itMethod : fResults.at(t)) {
742 auto numEvents = GetNEvents(
type);
743 auto results = itMethod.second;
744 auto resultsName = itMethod.first;
747 auto analysisType = results->GetAnalysisType();
755 Log() << kFATAL <<
"Unexpected analysisType." <<
Endl;
758 if (numEventsResults != numEvents) {
759 Log() << kFATAL <<
"An error occurred in DataSet::GetTree. "
760 "Inconsistent size of result for result with name '"
761 << resultsName <<
"'."
762 <<
" Size is '" << std::to_string(numEventsResults)
764 <<
" Expected '" << numEvents <<
"'." <<
Endl;
769 for (
Long64_t iEvt = 0; iEvt < GetNEvents(
type ); iEvt++) {
771 const Event* ev = GetEvent( iEvt );
775 strlcpy(className, fdsi->GetClassInfo( cls )->GetName(),
sizeof(className));
785 for (
auto && itMethod : fResults.at(t)) {
786 auto & results = *itMethod.second;
787 auto analysisType = results.GetAnalysisType();
789 auto const & vals = results[iEvt];
792 metVals[iMethod][0] = vals[0];
794 for (
UInt_t nCls = 0; nCls < fdsi->GetNClasses(); nCls++) {
796 metVals[iMethod][nCls] = val;
799 for (
UInt_t nTgts = 0; nTgts < fdsi->GetNTargets(); nTgts++) {
801 metVals[iMethod][nTgts] = val;
811 <<
"Created tree '" << tree->GetName() <<
"' with " << tree->GetEntries() <<
" events" <<
Endl <<
Endl;
813 SetCurrentType(savedType);
819 for(
UInt_t i=0; i<fResults.at(t).
size(); i++ )
823 tree->ResetBranchAddresses();
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
winID h TVirtualViewer3D TVirtualGLPainter p
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t dest
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t type
Class that contains all the information of a class.
Class that contains all the data information.
void DivideTrainingSet(UInt_t blockNum)
divide training set
void AddEvent(Event *, Types::ETreeType)
add event to event list after which the event is owned by the dataset
Long64_t GetNEvtSigTest()
return number of signal test events in dataset
std::vector< Char_t > fSampling
random or importance sampling (not all events are taken) !! Bool_t are stored ( no std::vector<bool> ...
std::vector< Float_t > fSamplingWeight
weight change factor [weight is indicating if sampling is random (1.0) or importance (<1....
UInt_t GetNTargets() const
access the number of targets through the datasetinfo
void ClearNClassEvents(Int_t type)
Long64_t GetNEvtSigTrain()
return number of signal training events in dataset
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...
void SetEventCollection(std::vector< Event * > *, Types::ETreeType, Bool_t deleteEvents=true)
Sets the event collection (by DataSetFactory)
TTree * GetTree(Types::ETreeType type)
create the test/trainings tree with all the variables, the weights, the classes, the targets,...
const Event * GetEvent() const
returns event without transformations
Results * GetResults(const TString &, Types::ETreeType type, Types::EAnalysisType analysistype)
Long64_t GetNClassEvents(Int_t type, UInt_t classNumber)
std::vector< Char_t > fBlockBelongToTraining
when dividing the dataset to blocks, sets whether the certain block is in the Training set or else in...
UInt_t GetNSpectators() const
access the number of targets through the datasetinfo
void MoveTrainingBlock(Int_t blockInd, Types::ETreeType dest, Bool_t applyChanges=kTRUE)
move training block
UInt_t GetNVariables() const
access the number of variables through the datasetinfo
std::vector< Int_t > fSamplingNEvents
number of events which should be sampled
virtual ~DataSet()
destructor
std::vector< std::vector< Long64_t > > fClassEvents
number of events of class 0,1,2,... in training[0] and testing[1] (+validation, trainingoriginal)
void DeleteAllResults(Types::ETreeType type, Types::EAnalysisType analysistype)
Deletes all results currently in the dataset.
void InitSampling(Float_t fraction, Float_t weight, UInt_t seed=0)
initialize random or importance sampling
void IncrementNClassEvents(Int_t type, UInt_t classNumber)
void DeleteResults(const TString &, Types::ETreeType type, Types::EAnalysisType analysistype)
delete the results stored for this particular Method instance.
void CreateSampling() const
create an event sampling (random or importance sampling)
TRandom3 * fSamplingRandom
-> random generator for sampling
Long64_t GetNEvtBkgdTrain()
return number of background training events in dataset
void DestroyCollection(Types::ETreeType type, Bool_t deleteEvents)
destroys the event collection (events + vector)
void ApplyTrainingSetDivision()
apply division of data set
Long64_t GetNEvtBkgdTest()
return number of background test events in dataset
Float_t GetValue(UInt_t ivar) const
return value of i'th variable
UInt_t GetNSpectators() const
accessor to the number of spectators
UInt_t GetNVariables() const
accessor to the number of variables
UInt_t GetNTargets() const
accessor to the number of targets
Double_t GetWeight() const
return the event weight - depending on whether the flag IgnoreNegWeightsInTraining is or not.
Float_t GetSpectator(UInt_t ivar) const
return spectator content
Float_t GetTarget(UInt_t itgt) const
ostringstream derivative to redirect and format output
Class that is the base-class for a vector of result.
Class which takes the results of a multiclass classification.
Class that is the base-class for a vector of result.
Class that is the base-class for a vector of result.
void SetTreeType(Types::ETreeType type)
@ kTrainingOriginal
ever needed
@ kValidation
these are placeholders... currently not used, but could be moved "forward" if
The TNamed class is the base class for all named ROOT classes.
const char * GetName() const override
Returns name of object.
Random number generator class based on M.
TString & ReplaceAll(const TString &s1, const TString &s2)
TString & Append(const char *cs)
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString.
A TTree represents a columnar dataset.
MsgLogger & Endl(MsgLogger &ml)