63using std::endl, std::list, std::string;
112 if (selExpr && *selExpr) {
131 if (selExpr && *selExpr) {
147 const char *wgtVarName)
159 std::unique_ptr<RooFormulaVar> cloneVar;
162 cloneVar->attachDataStore(tds) ;
174 loadValues(&tds,cloneVar.get(),cutRange,nStart,nStop);
180 std::size_t nStart, std::size_t nStop) {
186 return std::make_unique<RooTreeDataStore>(
name, title, *
this, tmp, cutVar, cutRange, nStart, nStop, wgtVarName);
200 ret.remove(*wgt,
true,
true) ;
331 _tree->SetDirectory(
nullptr);
337 bool notInMemNow= (pwd!=memDir) ;
372 std::unique_ptr<
TTree,
decltype(deleter)> tClone(
static_cast<TTree*
>(t->
Clone()), deleter);
377 _varsww.snapshot(sourceArgSet,
false);
380 bool missingBranches =
false;
381 for (
const auto var : sourceArgSet) {
382 if (!tClone->GetBranch(var->GetName())) {
383 missingBranches =
true;
384 coutE(InputArguments) <<
"Didn't find a branch in Tree '" << tClone->GetName() <<
"' to read variable '"
385 << var->GetName() <<
"' from."
386 <<
"\n\tNote: Name the RooFit variable the same as the branch." << std::endl;
389 if (missingBranches) {
390 coutE(InputArguments) <<
"Cannot import data from TTree '" << tClone->GetName()
391 <<
"' because some branches are missing !" << std::endl;
396 for (
const auto sourceArg : sourceArgSet) {
401 std::unique_ptr<RooFormulaVar> selectClone;
403 selectClone.reset(
static_cast<RooFormulaVar*
>(select->cloneTree()) );
404 selectClone->recursiveRedirectServers(sourceArgSet) ;
409 Int_t numInvalid(0) ;
410 const Long64_t nevent = tClone->GetEntries();
411 for(
Long64_t i=0; i < nevent; ++i) {
412 const auto entryNumber = tClone->GetEntryNumber(i);
413 if (entryNumber<0)
break;
414 tClone->GetEntry(entryNumber,1);
418 for (
unsigned int j=0; j < sourceArgSet.
size(); ++j) {
420 const auto sourceArg = sourceArgSet[j];
422 destArg->copyCache(sourceArg) ;
423 sourceArg->copyCache(destArg) ;
424 if (!destArg->isValid()) {
427 if (numInvalid < 5) {
428 auto& log =
coutI(DataHandling);
429 log <<
"RooTreeDataStore::loadValues(" <<
GetName() <<
") Skipping event #" << i <<
" because " << destArg->GetName()
430 <<
" cannot accommodate the value ";
431 if(sourceArg->isCategory()) {
432 log << static_cast<RooAbsCategory*>(sourceArg)->getCurrentIndex();
434 log << static_cast<RooAbsReal*>(sourceArg)->getVal();
437 }
else if (numInvalid == 5) {
438 coutI(DataHandling) <<
"RooTreeDataStore::loadValues(" <<
GetName() <<
") Skipping ..." << std::endl;
445 if (!allOK || (selectClone && selectClone->getVal()==0)) {
453 coutW(DataHandling) <<
"RooTreeDataStore::loadValues(" <<
GetName() <<
") Ignored " << numInvalid <<
" out-of-range events" << std::endl ;
470 const char* rangeName, std::size_t nStart, std::size_t nStop)
473 std::unique_ptr<RooFormulaVar> selectClone;
475 selectClone.reset(
static_cast<RooFormulaVar*
>(select->cloneTree()) );
476 selectClone->recursiveRedirectServers(*ads->
get()) ;
484 const auto numEntr =
static_cast<std::size_t
>(ads->
numEntries());
485 std::size_t nevent = nStop < numEntr ? nStop : numEntr;
492 std::vector<std::string> ranges;
497 for (
auto i=nStart; i < nevent ; ++i) {
501 if (selectClone && selectClone->getVal()==0) {
507 _varsww.assignValueOnly(TDS->_varsww) ;
513 bool allValid =
true;
514 for (
const auto arg :
_varsww) {
515 allValid = arg->isValid() && (ranges.empty() || std::any_of(ranges.begin(), ranges.end(),
516 [arg](
const std::string& range){return arg->inRange(range.c_str());}) );
542 return _tree->Fill() ;
559 if(!
ret)
return nullptr;
563 for (
auto var :
_vars) {
564 var->setValueDirty();
568 var->setValueDirty();
569 var->clearValueDirty();
631 return (
_wgtVar->getAsymErrorHi() -
_wgtVar->getAsymErrorLo() ) / 2 ;
654 throw string(
Form(
"RooDataHist::weightError(%s) error type Auto not allowed here",
GetName())) ;
658 throw string(
Form(
"RooDataHist::weightError(%s) error type Expected not allowed here",
GetName())) ;
693 lo =
_wgtVar->getAsymErrorLo() ;
719 coutE(InputArguments) <<
"RooTreeDataStore::changeObservableName(" <<
GetName() <<
" no observable " << from <<
" in this dataset" << std::endl ;
728 if (
_tree->GetBranch(oldBranchName.
Data())) {
737 if (
_tree->GetBranch(
Form(
"%s_aerr_lo",oldBranchName.
Data()))) {
740 if (
_tree->GetBranch(
Form(
"%s_aerr_hi",oldBranchName.
Data()))) {
787 auto valHolder = std::unique_ptr<RooAbsArg>{newVar.
createFundamental()}.release();
789 if(!valHolder->isFundamental()) {
790 coutE(InputArguments) <<
GetName() <<
"::addColumn: holder argument is not fundamental: \""
791 << valHolder->GetName() <<
"\"" << std::endl;
799 std::unique_ptr<RooAbsArg> newVarClone{newVar.
cloneTree()};
800 newVarClone->recursiveRedirectServers(
_vars,
false) ;
804 _vars.add(*valHolder) ;
809 for (
int i=0 ; i <
_tree->GetEntries() ; i++) {
812 newVarClone->syncCache(&
_vars) ;
813 valHolder->copyCache(newVarClone.get());
814 valHolder->fillTreeBranch(*
_tree) ;
844 Int_t nevt = dstoreList.front()->numEntries() ;
845 for (
int i=0 ; i<nevt ; i++) {
851 for (list<RooAbsDataStore*>::iterator iter = dstoreList.begin() ; iter!=dstoreList.end() ; ++iter) {
856 mergedStore->
fill() ;
870 for (
int i=0 ; i<nevt ; i++) {
890 for (
int i=0 ; i<nevt ; i++) {
893 double y =
_wgtVar->getVal() - carry;
895 carry = (t -
sum) -
y;
905 for (
int i=0 ; i<nevt ; i++) {
909 carry = (t -
sum) -
y;
928 return _tree->GetEntries() ;
956 std::unique_ptr<RooArgSet> constExprVarSet{newVarSet.
selectByAttrib(
"ConstantExpression",
true)};
960 for (
RooAbsArg * arg : *constExprVarSet) {
971 for (
int i=0 ; i <
_tree->GetEntries() ; i++) {
975 for (
RooAbsArg * arg : *constExprVarSet) {
976 arg->setValueDirty() ;
977 arg->syncCache(nset) ;
1004 coutE(InputArguments) <<
"RooTreeDataStore::setArgStatus(" <<
GetName()
1005 <<
") dataset doesn't contain variable " << arg->GetName() << std::endl ;
1039 for (
const auto arg :
_varsww) {
1042 if (arg->getAttribute(
"StoreError")) {
1045 if (arg->getAttribute(
"StoreAsymError")) {
1061 arg->attachToTree(*
_tree) ;
1072 arg->attachToTree(*
_tree) ;
1114 if (
_tree && parent) {
1118 auto tmpDir =
_tree->GetDirectory();
1120 _tree->SetDirectory(parent);
1121 _tree->FlushBaskets(
false);
1123 _tree->SetDirectory(tmpDir);
1137 std::replace(title.begin(), title.end(),
' ',
'_');
1138 std::replace(title.begin(), title.end(),
'-',
'_');
1139 return std::string(
"RooTreeDataStore_") +
GetName() +
"_" + title;
1157 for (
int i = 0; i <
_tree->GetEntries(); ++i) {
int Int_t
Signed integer 4 bytes (int).
short Version_t
Class version identifier (short).
unsigned int UInt_t
Unsigned integer 4 bytes (unsigned int).
long long Long64_t
Portable signed long integer 8 bytes.
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
Common abstract base class for objects that represent a value and a "shape" in RooFit.
void SetName(const char *name) override
Set the name of the TNamed.
virtual RooFit::OwningPtr< RooAbsArg > createFundamental(const char *newname=nullptr) const =0
Create a fundamental-type object that stores our type of value.
virtual void attachToTree(TTree &t, Int_t bufSize=32000)=0
Overloadable function for derived classes to implement attachment as branch to a TTree.
virtual void setTreeBranchStatus(TTree &t, bool active)=0
virtual RooAbsArg * cloneTree(const char *newname=nullptr) const
Clone tree expression of objects.
TString cleanBranchName() const
Construct a mangled name from the actual name that is free of any math symbols that might be interpre...
void setAttribute(const Text_t *name, bool value=true)
Set (default) or clear a named boolean attribute of this object.
bool contains(const char *name) const
Check if collection contains an argument with a specific name.
Storage_t const & get() const
Const access to the underlying stl container.
virtual bool add(const RooAbsArg &var, bool silent=false)
Add the specified argument to list.
void assign(const RooAbsCollection &other) const
Sets the value, cache and constant attribute of any argument in our set that also appears in the othe...
Storage_t::size_type size() const
RooAbsArg * find(const char *name) const
Find object with given name in list.
virtual const RooArgSet * get(Int_t index) const =0
bool _doDirtyProp
Switch do (de)activate dirty state propagation when loading a data point.
virtual double weight() const =0
virtual Int_t numEntries() const =0
RooArgSet is a container object that can hold multiple RooAbsArg objects.
RooArgSet * selectByAttrib(const char *name, bool value) const
Use RooAbsCollection::selectByAttrib(), but return as RooArgSet.
static const RooHistError & instance()
Return a reference to a singleton object that is created the first time this method is called.
bool getPoissonInterval(Int_t n, double &mu1, double &mu2, double nSigma=1) const
Calculate a confidence interval for the expected number of events given n observed (unweighted) event...
Variable that can be changed from the outside.
The RooStringView is a wrapper around a C-style string that can also be constructed from a std::strin...
void initialize()
One-time initialization common to all constructor forms.
double _curWgtErr
Weight of current event.
double weightError(RooAbsData::ErrorType etype=RooAbsData::Poisson) const override
void resetBuffers() override
double _curWgt
Weight of current event.
void attachCache(const RooAbsArg *newOwner, const RooArgSet &cachedVars) override
Initialize cache of dataset: attach variables of cache ArgSet to the corresponding TTree branches.
double _curWgtErrHi
Weight of current event.
Int_t numEntries() const override
std::string makeTreeName() const
Generate a name for the storage tree from the name and title of this instance.
RooArgSet varsNoWeight(const RooArgSet &allVars, const char *wgtName=nullptr)
Utility function for constructors Return RooArgSet that is copy of allVars minus variable matching wg...
~RooTreeDataStore() override
Destructor.
void createTree(RooStringView name, RooStringView title)
Create TTree object that lives in memory, independent of current location of gDirectory.
const double * _extWgtErrHiArray
! External weight array - high error
void attachBuffers(const RooArgSet &extObs) override
TTree * _cacheTree
! TTree holding the cached function values
RooAbsDataStore * merge(const RooArgSet &allvars, std::list< RooAbsDataStore * > dstoreList) override
Merge columns of supplied data set(s) with this data set.
static Int_t _defTreeBufSize
RooArgSet _attachedBuffers
! Currently attached buffers (if different from _varsww)
Int_t fill() override
Interface function to TTree::Fill.
double sumEntries() const override
std::unique_ptr< RooAbsDataStore > reduce(RooStringView name, RooStringView title, const RooArgSet &vars, const RooFormulaVar *cutVar, const char *cutRange, std::size_t nStart, std::size_t nStop) override
bool _defCtor
! Was object constructed with default ctor?
RooAbsArg * addColumn(RooAbsArg &var, bool adjustRange=true) override
Add a new column to the data set which holds the pre-calculated values of 'newVar'.
double weight() const override
Return the weight of the n-th data point (n='index') in memory.
void restoreAlternateBuffers()
void loadValues(const TTree *t, const RooFormulaVar *select=nullptr, const char *rangeName=nullptr, Int_t nStart=0, Int_t nStop=2000000000)
Load values from tree 't' into this data collection, optionally selecting events using the RooFormula...
void append(RooAbsDataStore &other) override
std::span< const double > getWeightBatch(std::size_t first, std::size_t len) const override
Get the weights of the events in the range [first, first+len).
const double * _extWgtErrLoArray
! External weight array - low error
void checkInit() const override
std::unique_ptr< std::vector< double > > _weightBuffer
! Buffer for weights in case a batch of values is requested.
const double * _extSumW2Array
! External sum of weights array
void Streamer(TBuffer &) override
Stream an object of class RooTreeDataStore.
RooRealVar * weightVar(const RooArgSet &allVars, const char *wgtName=nullptr)
Utility function for constructors Return pointer to weight variable if it is defined.
const double * _extWgtArray
! External weight array
double _curWgtErrLo
Weight of current event.
bool changeObservableName(const char *from, const char *to) override
Change name of internal observable named 'from' into 'to'.
void resetCache() override
Remove tree with values of cached observables and clear list of cached observables.
void setArgStatus(const RooArgSet &set, bool active) override
Activate or deactivate the branch status of the TTree branch associated with the given set of dataset...
void cacheArgs(const RooAbsArg *owner, RooArgSet &varSet, const RooArgSet *nset=nullptr, bool skipZeroWeights=false) override
Cache given RooAbsArgs with this tree: The tree is given direct write access of the args internal cac...
virtual const RooArgSet * get() const
const RooAbsArg * _cacheOwner
! Object owning cache contents
Buffer base class used for serializing objects.
virtual Version_t ReadVersion(UInt_t *start=nullptr, UInt_t *bcnt=nullptr, const TClass *cl=nullptr)=0
TObject * GetParent() const
Return pointer to parent of this buffer.
virtual Int_t ReadClassBuffer(const TClass *cl, void *pointer, const TClass *onfile_class=nullptr)=0
virtual Int_t WriteClassBuffer(const TClass *cl, void *pointer)=0
Describe directory structure in memory.
void GetObject(const char *namecycle, T *&ptr)
Get an object with proper type checking.
A file, usually with extension .root, that stores data and code in the form of serialized objects in ...
TObject * Clone(const char *newname="") const override
Make a clone of an object using the Streamer facility.
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
const char * GetName() const override
Returns name of object.
const char * GetTitle() const override
Returns title of object.
@ kCanDelete
if object in a list can be deleted
@ kMustCleanup
if object destructor must call RecursiveRemove()
const char * Data() const
TString & Append(const char *cs)
A TTree represents a columnar dataset.
TDirectory * GetDirectory() const
virtual void SetDirectory(TDirectory *dir)
Change the tree's directory.
std::vector< std::string > Split(std::string_view str, std::string_view delims, bool skipEmpty=false)
Splits a string at each character in delims.
static uint64_t sum(uint64_t i)