16 #ifndef ROO_VECTOR_DATA_STORE
17 #define ROO_VECTOR_DATA_STORE
30 #define VECTOR_BUFFER_SIZE 1024
38 class RooVectorDataStore :
public RooAbsDataStore {
41 RooVectorDataStore() ;
44 RooVectorDataStore(
const char* name,
const char* title,
const RooArgSet& vars,
const char* wgtVarName=0) ;
45 virtual RooAbsDataStore* clone(
const char* newname=0)
const override {
return new RooVectorDataStore(*
this,newname) ; }
46 virtual RooAbsDataStore* clone(
const RooArgSet& vars,
const char* newname=0)
const override {
return new RooVectorDataStore(*
this,vars,newname) ; }
48 RooVectorDataStore(
const RooVectorDataStore& other,
const char* newname=0) ;
49 RooVectorDataStore(
const RooTreeDataStore& other,
const RooArgSet& vars,
const char* newname=0) ;
50 RooVectorDataStore(
const RooVectorDataStore& other,
const RooArgSet& vars,
const char* newname=0) ;
53 RooVectorDataStore(
const char *name,
const char *title, RooAbsDataStore& tds,
54 const RooArgSet& vars,
const RooFormulaVar* cutVar,
const char* cutRange,
55 Int_t nStart, Int_t nStop, Bool_t ,
const char* wgtVarName=0) ;
57 virtual ~RooVectorDataStore() ;
60 RooArgSet varsNoWeight(
const RooArgSet& allVars,
const char* wgtName);
61 RooRealVar* weightVar(
const RooArgSet& allVars,
const char* wgtName);
64 void reserve(Int_t nEvt);
68 virtual Int_t fill()
override;
71 using RooAbsDataStore::get;
72 virtual const RooArgSet*
get(Int_t index)
const override;
74 virtual const RooArgSet* getNative(Int_t index)
const;
76 virtual Double_t weight()
const override;
77 virtual Double_t weightError(RooAbsData::ErrorType etype=RooAbsData::Poisson)
const override;
78 virtual void weightError(Double_t& lo, Double_t& hi, RooAbsData::ErrorType etype=RooAbsData::Poisson)
const override;
79 virtual Double_t weight(Int_t index)
const override;
80 virtual Bool_t isWeighted()
const override {
return (_wgtVar!=0||_extWgtArray!=0) ; }
82 virtual std::vector<RooSpan<const double>> getBatch(std::size_t first, std::size_t last)
const override;
83 virtual RooSpan<const double> getWeightBatch(std::size_t first, std::size_t last)
const override;
86 virtual Bool_t changeObservableName(
const char* from,
const char* to)
override;
89 virtual RooAbsArg* addColumn(RooAbsArg& var, Bool_t adjustRange=kTRUE)
override;
90 virtual RooArgSet* addColumns(
const RooArgList& varList)
override;
93 RooAbsDataStore* merge(
const RooArgSet& allvars, std::list<RooAbsDataStore*> dstoreList)
override;
96 virtual void append(RooAbsDataStore& other)
override;
99 virtual Bool_t valid()
const override;
100 virtual Int_t numEntries()
const override;
101 virtual Double_t sumEntries()
const override {
return _sumWeight ; }
102 virtual void reset()
override;
105 virtual void attachBuffers(
const RooArgSet& extObs)
override;
106 virtual void resetBuffers()
override;
110 virtual const RooAbsArg* cacheOwner()
override {
return _cacheOwner ; }
111 virtual void cacheArgs(
const RooAbsArg* owner, RooArgSet& varSet,
const RooArgSet* nset=0, Bool_t skipZeroWeights=kTRUE)
override;
112 virtual void attachCache(
const RooAbsArg* newOwner,
const RooArgSet& cachedVars)
override;
113 virtual void resetCache()
override;
114 virtual void recalculateCache(
const RooArgSet* , Int_t firstEvent, Int_t lastEvent, Int_t stepSize, Bool_t skipZeroWeights)
override;
116 virtual void setArgStatus(
const RooArgSet& set, Bool_t active)
override;
118 const RooVectorDataStore* cache()
const {
return _cache ; }
120 void loadValues(
const RooAbsDataStore *tds,
const RooFormulaVar* select=0,
const char* rangeName=0, Int_t nStart=0, Int_t nStop=2000000000)
override;
122 void dump()
override;
124 void setExternalWeightArray(
const Double_t* arrayWgt,
const Double_t* arrayWgtErrLo,
125 const Double_t* arrayWgtErrHi,
const Double_t* arraySumW2)
override {
126 _extWgtArray = arrayWgt ;
127 _extWgtErrLoArray = arrayWgtErrLo ;
128 _extWgtErrHiArray = arrayWgtErrHi ;
129 _extSumW2Array = arraySumW2 ;
132 virtual void setDirtyProp(Bool_t flag)
override {
133 _doDirtyProp = flag ;
135 _cache->setDirtyProp(flag) ;
139 const RooArgSet& row() {
return _varsww ; }
144 RealVector(UInt_t initialCapacity=(VECTOR_BUFFER_SIZE /
sizeof(Double_t))) :
145 _nativeReal(0), _real(0), _buf(0), _nativeBuf(0), _tracker(0), _nset(0) {
146 _vec.reserve(initialCapacity);
149 RealVector(RooAbsReal* arg, UInt_t initialCapacity=(VECTOR_BUFFER_SIZE /
sizeof(Double_t))) :
150 _nativeReal(arg), _real(0), _buf(0), _nativeBuf(0), _tracker(0), _nset(0) {
151 _vec.reserve(initialCapacity);
154 virtual ~RealVector() {
156 if (_nset)
delete _nset ;
159 RealVector(
const RealVector& other, RooAbsReal* real=0) :
160 _vec(other._vec), _nativeReal(real?real:other._nativeReal), _real(real?real:other._real), _buf(other._buf), _nativeBuf(other._nativeBuf), _nset(0) {
161 if (other._tracker) {
162 _tracker =
new RooChangeTracker(Form(
"track_%s",_nativeReal->GetName()),
"tracker",other._tracker->parameters()) ;
167 _nset =
new RooArgSet(*other._nset) ;
171 RealVector& operator=(
const RealVector& other) {
172 if (&other==
this)
return *
this;
173 _nativeReal = other._nativeReal;
176 _nativeBuf = other._nativeBuf;
177 if (other._vec.size() <= _vec.capacity() / 2 && _vec.capacity() > (VECTOR_BUFFER_SIZE /
sizeof(Double_t))) {
178 std::vector<Double_t> tmp;
179 tmp.reserve(std::max(other._vec.size(), VECTOR_BUFFER_SIZE /
sizeof(Double_t)));
180 tmp.assign(other._vec.begin(), other._vec.end());
189 void setNset(RooArgSet* newNset) { _nset = newNset ?
new RooArgSet(*newNset) : 0 ; }
191 RooArgSet* nset()
const {
return _nset ; }
193 void setBufArg(RooAbsReal* arg) { _nativeReal = arg ; }
194 const RooAbsReal* bufArg()
const {
return _nativeReal ; }
196 void setBuffer(RooAbsReal* real, Double_t* newBuf) {
204 void setNativeBuffer(Double_t* newBuf=0) {
205 _nativeBuf = newBuf ? newBuf : _buf ;
208 void setDependents(
const RooArgSet& deps) {
212 _tracker =
new RooChangeTracker(Form(
"track_%s",_nativeReal->GetName()),
"tracker",deps) ;
215 Bool_t needRecalc() {
216 if (!_tracker)
return kFALSE ;
217 return _tracker->hasChanged(kTRUE) ;
221 _vec.push_back(*_buf);
224 void write(Int_t i) {
230 std::vector<Double_t> tmp;
234 inline void get(Int_t idx)
const {
235 *_buf = *(_vec.begin() + idx) ;
238 RooSpan<const double> getRange(std::size_t first, std::size_t last)
const {
239 auto beg = std::min(_vec.cbegin() + first, _vec.cend());
240 auto end = std::min(_vec.cbegin() + last, _vec.cend());
242 return RooSpan<const double>(beg, end);
245 inline void getNative(Int_t idx)
const {
246 *_nativeBuf = *(_vec.begin() + idx) ;
249 Int_t size()
const {
return _vec.size() ; }
251 void resize(Int_t siz) {
252 if (siz < Int_t(_vec.capacity()) / 2 && _vec.capacity() > (VECTOR_BUFFER_SIZE /
sizeof(Double_t))) {
254 std::vector<Double_t> tmp;
255 tmp.reserve(std::max(siz, Int_t(VECTOR_BUFFER_SIZE /
sizeof(Double_t))));
257 tmp.assign(_vec.begin(), std::min(_vec.end(), _vec.begin() + siz));
258 if (Int_t(tmp.size()) != siz)
266 void reserve(Int_t siz) {
270 const std::vector<double>& data()
const {
275 std::vector<double> _vec;
278 friend class RooVectorDataStore ;
279 RooAbsReal* _nativeReal ;
282 Double_t* _nativeBuf ;
283 RooChangeTracker* _tracker ;
285 ClassDef(RealVector,1)
289 class RealFullVector :
public RealVector {
291 RealFullVector(UInt_t initialCapacity=(VECTOR_BUFFER_SIZE /
sizeof(Double_t))) : RealVector(initialCapacity),
292 _bufE(0), _bufEL(0), _bufEH(0),
293 _nativeBufE(0), _nativeBufEL(0), _nativeBufEH(0),
294 _vecE(0), _vecEL(0), _vecEH(0) {
297 RealFullVector(RooAbsReal* arg, UInt_t initialCapacity=(VECTOR_BUFFER_SIZE /
sizeof(Double_t))) :
298 RealVector(arg,initialCapacity),
299 _bufE(0), _bufEL(0), _bufEH(0),
300 _nativeBufE(0), _nativeBufEL(0), _nativeBufEH(0),
301 _vecE(0), _vecEL(0), _vecEH(0) {
304 virtual ~RealFullVector() {
305 if (_vecE)
delete _vecE ;
306 if (_vecEL)
delete _vecEL ;
307 if (_vecEH)
delete _vecEH ;
310 RealFullVector(
const RealFullVector& other, RooAbsReal* real=0) : RealVector(other,real),
311 _bufE(other._bufE), _bufEL(other._bufEL), _bufEH(other._bufEH),
312 _nativeBufE(other._nativeBufE), _nativeBufEL(other._nativeBufEL), _nativeBufEH(other._nativeBufEH) {
313 _vecE = (other._vecE) ?
new std::vector<Double_t>(*other._vecE) : 0 ;
314 _vecEL = (other._vecEL) ?
new std::vector<Double_t>(*other._vecEL) : 0 ;
315 _vecEH = (other._vecEH) ?
new std::vector<Double_t>(*other._vecEH) : 0 ;
318 RealFullVector(
const RealVector& other, RooAbsReal* real=0) : RealVector(other,real),
319 _bufE(0), _bufEL(0), _bufEH(0),
320 _nativeBufE(0), _nativeBufEL(0), _nativeBufEH(0) {
326 RealFullVector& operator=(
const RealFullVector& other) {
327 if (&other==
this)
return *
this;
328 RealVector::operator=(other);
330 _bufEL = other._bufEL;
331 _bufEH = other._bufEH;
332 _nativeBufE = other._nativeBufE;
333 _nativeBufEL = other._nativeBufEL;
334 _nativeBufEH = other._nativeBufEH;
335 std::vector<Double_t>* src[3] = { other._vecE, other._vecEL, other._vecEH };
336 std::vector<Double_t>* dst[3] = { _vecE, _vecEL, _vecEH };
337 for (
unsigned i = 0; i < 3; ++i) {
340 if (dst[i]->size() <= src[i]->capacity() / 2 &&
341 src[i]->capacity() > (VECTOR_BUFFER_SIZE /
sizeof(Double_t))) {
342 std::vector<Double_t> tmp;
343 tmp.reserve(std::max(src[i]->size(), VECTOR_BUFFER_SIZE /
sizeof(Double_t)));
344 tmp.assign(src[i]->begin(), src[i]->end());
350 dst[i] =
new std::vector<Double_t>(*src[i]);
360 void setErrorBuffer(Double_t* newBuf) {
363 if (!_vecE) _vecE =
new std::vector<Double_t> ;
364 _vecE->reserve(_vec.capacity()) ;
365 if (!_nativeBufE) _nativeBufE = _bufE ;
367 void setAsymErrorBuffer(Double_t* newBufL, Double_t* newBufH) {
368 _bufEL = newBufL ; _bufEH = newBufH ;
370 _vecEL =
new std::vector<Double_t> ;
371 _vecEH =
new std::vector<Double_t> ;
372 _vecEL->reserve(_vec.capacity()) ;
373 _vecEH->reserve(_vec.capacity()) ;
376 _nativeBufEL = _bufEL ;
377 _nativeBufEH = _bufEH ;
381 inline void getNative(Int_t idx)
const {
382 RealVector::getNative(idx) ;
384 *_nativeBufE = (*_vecE)[idx] ;
387 *_nativeBufEL = (*_vecEL)[idx] ;
388 *_nativeBufEH = (*_vecEH)[idx] ;
394 if (_vecE) _vecE->push_back(*_bufE) ;
395 if (_vecEL) _vecEL->push_back(*_bufEL) ;
396 if (_vecEH) _vecEH->push_back(*_bufEH) ;
399 void write(Int_t i) {
400 RealVector::write(i) ;
401 if (_vecE) (*_vecE)[i] = *_bufE ;
402 if (_vecEL) (*_vecEL)[i] = *_bufEL ;
403 if (_vecEH) (*_vecEH)[i] = *_bufEH ;
409 std::vector<Double_t> tmp;
413 std::vector<Double_t> tmp;
417 std::vector<Double_t> tmp;
422 inline void get(Int_t idx)
const {
423 RealVector::get(idx) ;
424 if (_vecE) *_bufE = (*_vecE)[idx];
425 if (_vecEL) *_bufEL = (*_vecEL)[idx] ;
426 if (_vecEH) *_bufEH = (*_vecEH)[idx] ;
429 void resize(Int_t siz) {
430 RealVector::resize(siz);
431 std::vector<Double_t>* vlist[3] = { _vecE, _vecEL, _vecEH };
432 for (
unsigned i = 0; i < 3; ++i) {
433 if (!vlist[i])
continue;
435 if (siz < Int_t(vlist[i]->capacity()) / 2 && vlist[i]->capacity() > (VECTOR_BUFFER_SIZE /
sizeof(Double_t))) {
437 std::vector<Double_t> tmp;
438 tmp.reserve(std::max(siz, Int_t(VECTOR_BUFFER_SIZE /
sizeof(Double_t))));
439 if (!vlist[i]->empty())
440 tmp.assign(vlist[i]->begin(),
441 std::min(_vec.end(), _vec.begin() + siz));
442 if (Int_t(tmp.size()) != siz)
446 vlist[i]->resize(siz);
452 void reserve(Int_t siz) {
453 RealVector::reserve(siz);
454 if (_vecE) _vecE->reserve(siz);
455 if (_vecEL) _vecEL->reserve(siz);
456 if (_vecEH) _vecEH->reserve(siz);
460 friend class RooVectorDataStore ;
464 Double_t *_nativeBufE ;
465 Double_t *_nativeBufEL ;
466 Double_t *_nativeBufEH ;
467 std::vector<double> *_vecE, *_vecEL, *_vecEH ;
468 ClassDef(RealFullVector,1)
474 CatVector(UInt_t initialCapacity=(VECTOR_BUFFER_SIZE /
sizeof(RooCatType))) :
475 _cat(0), _buf(0), _nativeBuf(0), _vec0(0)
477 _vec.reserve(initialCapacity);
480 CatVector(RooAbsCategory* cat, UInt_t initialCapacity=(VECTOR_BUFFER_SIZE /
sizeof(RooCatType))) :
481 _cat(cat), _buf(0), _nativeBuf(0), _vec0(0)
483 _vec.reserve(initialCapacity);
486 virtual ~CatVector() {
489 CatVector(
const CatVector& other, RooAbsCategory* cat=0) :
490 _cat(cat?cat:other._cat), _buf(other._buf), _nativeBuf(other._nativeBuf), _vec(other._vec)
492 _vec0 = _vec.size()>0 ? &_vec.front() : 0 ;
495 CatVector& operator=(
const CatVector& other) {
496 if (&other==
this)
return *
this;
499 _nativeBuf = other._nativeBuf;
500 if (other._vec.size() <= _vec.capacity() / 2 && _vec.capacity() > (VECTOR_BUFFER_SIZE /
sizeof(RooCatType))) {
501 std::vector<RooCatType> tmp;
502 tmp.reserve(std::max(other._vec.size(), VECTOR_BUFFER_SIZE /
sizeof(RooCatType)));
503 tmp.assign(other._vec.begin(), other._vec.end());
508 _vec0 = _vec.size()>0 ? &_vec.front() : 0;
512 void setBuffer(RooCatType* newBuf) {
514 if (_nativeBuf==0) _nativeBuf=newBuf ;
517 void setNativeBuffer(RooCatType* newBuf=0) {
518 _nativeBuf = newBuf ? newBuf : _buf ;
522 _vec.push_back(*_buf) ;
523 _vec0 = &_vec.front() ;
525 void write(Int_t i) {
530 std::vector<RooCatType> tmp;
534 inline void get(Int_t idx)
const {
535 _buf->assignFast(*(_vec0+idx)) ;
537 inline void getNative(Int_t idx)
const {
538 _nativeBuf->assignFast(*(_vec0+idx)) ;
540 Int_t size()
const {
return _vec.size() ; }
542 void resize(Int_t siz) {
543 if (siz < Int_t(_vec.capacity()) / 2 && _vec.capacity() > (VECTOR_BUFFER_SIZE /
sizeof(RooCatType))) {
545 std::vector<RooCatType> tmp;
546 tmp.reserve(std::max(siz, Int_t(VECTOR_BUFFER_SIZE /
sizeof(RooCatType))));
548 tmp.assign(_vec.begin(), std::min(_vec.end(), _vec.begin() + siz));
549 if (Int_t(tmp.size()) != siz)
555 _vec0 = _vec.size() > 0 ? &_vec.front() : 0;
558 void reserve(Int_t siz) {
560 _vec0 = _vec.size() > 0 ? &_vec.front() : 0;
563 void setBufArg(RooAbsCategory* arg) { _cat = arg; }
564 const RooAbsCategory* bufArg()
const {
return _cat; }
567 friend class RooVectorDataStore ;
568 RooAbsCategory* _cat ;
570 RooCatType* _nativeBuf ;
571 std::vector<RooCatType> _vec ;
573 ClassDef(CatVector,1)
579 friend class RooAbsReal ;
580 friend class RooAbsCategory ;
581 friend class RooRealVar ;
582 std::vector<RealVector*>& realStoreList() {
return _realStoreList ; }
583 std::vector<RealFullVector*>& realfStoreList() {
return _realfStoreList ; }
584 std::vector<CatVector*>& catStoreList() {
return _catStoreList ; }
586 CatVector* addCategory(RooAbsCategory* cat) {
589 for (
auto catVec : _catStoreList) {
590 if (std::string(catVec->bufArg()->GetName())==cat->GetName()) {
596 _catStoreList.push_back(
new CatVector(cat)) ;
600 _firstCat = &_catStoreList.front() ;
602 return _catStoreList.back() ;
605 RealVector* addReal(RooAbsReal* real) {
608 for (
auto realVec : _realStoreList) {
609 if (realVec->bufArg()->namePtr()==real->namePtr()) {
615 for (
auto fullVec : _realfStoreList) {
616 if (fullVec->bufArg()->namePtr()==real->namePtr()) {
623 _realStoreList.push_back(
new RealVector(real)) ;
625 return _realStoreList.back() ;
628 Bool_t isFullReal(RooAbsReal* real) {
631 for (
auto fullVec : _realfStoreList) {
632 if (std::string(fullVec->bufArg()->GetName())==real->GetName()) {
639 Bool_t hasError(RooAbsReal* real) {
642 for (
auto fullVec : _realfStoreList) {
643 if (std::string(fullVec->bufArg()->GetName())==real->GetName()) {
644 return fullVec->_vecE ? kTRUE : kFALSE ;
650 Bool_t hasAsymError(RooAbsReal* real) {
653 for (
auto fullVec : _realfStoreList) {
654 if (std::string(fullVec->bufArg()->GetName())==real->GetName()) {
655 return fullVec->_vecEL ? kTRUE : kFALSE ;
661 RealFullVector* addRealFull(RooAbsReal* real) {
664 for (
auto fullVec : _realfStoreList) {
665 if (std::string(fullVec->bufArg()->GetName())==real->GetName()) {
671 for (
auto realVec : _realStoreList) {
672 if (std::string(realVec->bufArg()->GetName())==real->GetName()) {
675 _realfStoreList.push_back(
new RealFullVector(*realVec,real)) ;
677 _firstRealF = &_realfStoreList.front() ;
680 _realStoreList.erase(std::find(_realStoreList.begin(), _realStoreList.end(), realVec));
683 return _realfStoreList.back() ;
688 _realfStoreList.push_back(
new RealFullVector(real)) ;
692 _firstRealF = &_realfStoreList.front() ;
695 return _realfStoreList.back() ;
698 virtual Bool_t hasFilledCache()
const override {
return _cache ? kTRUE : kFALSE ; }
700 void forceCacheUpdate()
override;
704 RooRealVar* _wgtVar ;
706 std::vector<RealVector*> _realStoreList ;
707 std::vector<RealFullVector*> _realfStoreList ;
708 std::vector<CatVector*> _catStoreList ;
709 std::vector<double> _weights;
711 void setAllBuffersNative() ;
716 RealFullVector** _firstRealF ;
717 CatVector** _firstCat ;
718 Double_t _sumWeight ;
719 Double_t _sumWeightCarry;
721 const Double_t* _extWgtArray ;
722 const Double_t* _extWgtErrLoArray ;
723 const Double_t* _extWgtErrHiArray ;
724 const Double_t* _extSumW2Array ;
726 mutable Double_t _curWgt ;
727 mutable Double_t _curWgtErrLo ;
728 mutable Double_t _curWgtErrHi ;
729 mutable Double_t _curWgtErr ;
731 RooVectorDataStore* _cache ;
732 RooAbsArg* _cacheOwner ;
734 Bool_t _forcedUpdate ;
736 ClassDefOverride(RooVectorDataStore,3)