Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RooCompositeDataStore.cxx
Go to the documentation of this file.
1/*****************************************************************************
2 * Project: RooFit *
3 * Package: RooFitCore *
4 * @(#)root/roofitcore:$Id$
5 * Authors: *
6 * WV, Wouter Verkerke, UC Santa Barbara, verkerke@slac.stanford.edu *
7 * DK, David Kirkby, UC Irvine, dkirkby@uci.edu *
8 * *
9 * Copyright (c) 2000-2005, Regents of the University of California *
10 * and Stanford University. All rights reserved. *
11 * *
12 * Redistribution and use in source and binary forms, *
13 * with or without modification, are permitted according to the terms *
14 * listed in LICENSE (http://roofit.sourceforge.net/license.txt) *
15 *****************************************************************************/
16
17/**
18\file RooCompositeDataStore.cxx
19\class RooCompositeDataStore
20\ingroup Roofitcore
21
22Combines several disjunct datasets into one. This is useful for simultaneous PDFs
23that do not depend on the same observable such as a PDF depending on `x` combined with another one depending
24on `y`.
25The composite storage will store two different datasets, `{x}` and `{y}`, but they can be passed as a single
26dataset to RooFit operations. A category tag will define which dataset has to be passed to which likelihood.
27
28When iterated from start to finish, datasets will be traversed in the order of the category index.
29**/
30
32
33#include "RooMsgService.h"
34#include "RooFormulaVar.h"
35#include "RooRealVar.h"
36#include "RooTrace.h"
37#include "RooCategory.h"
38
39#include <iomanip>
40#include <iostream>
41
42using std::map, std::list, std::string;
43
44
45
46////////////////////////////////////////////////////////////////////////////////
47
52
53
54
55////////////////////////////////////////////////////////////////////////////////
56/// Convert map by label to map by index for more efficient internal use
57
61 RooAbsDataStore(name,title,RooArgSet(vars,indexCat)), _indexCat(&indexCat)
62{
63 for (const auto& iter : inputData) {
64 const RooAbsCategory::value_type idx = indexCat.lookupIndex(iter.first);
65 _dataMap[idx] = iter.second;
66 }
68}
69
70
71////////////////////////////////////////////////////////////////////////////////
72/// Convert map by label to map by index for more efficient internal use
73
75 RooAbsDataStore(other,newname), _indexCat(other._indexCat), _curStore(other._curStore), _curIndex(other._curIndex), _ownComps(true)
76{
77 for (const auto& item : other._dataMap) {
78 RooAbsDataStore* clonedata = item.second->clone() ;
79 _dataMap[item.first] = clonedata ;
80 }
82}
83
84
85////////////////////////////////////////////////////////////////////////////////
86/// Update index category pointer, if it is contained in input argument vars
87
89 RooAbsDataStore(other,vars,newname), _indexCat(other._indexCat), _curStore(other._curStore), _curIndex(other._curIndex), _ownComps(true)
90{
91 RooCategory* newIdx = static_cast<RooCategory*>(vars.find(other._indexCat->GetName())) ;
92 if (newIdx) {
94 }
95
96 // Convert map by label to map by index for more efficient internal use
97 for (const auto& item : other._dataMap) {
98 RooAbsDataStore* clonedata = item.second->clone(vars) ;
99 _dataMap[item.first] = clonedata ;
100 }
102}
103
104
105
106
107////////////////////////////////////////////////////////////////////////////////
108/// Destructor
109
111{
112 if (_ownComps) {
113 for (auto& item : _dataMap) {
114 delete item.second;
115 }
116 }
118}
119
120
121std::unique_ptr<RooAbsDataStore> RooCompositeDataStore::reduce(
122 RooStringView name, RooStringView title, const RooArgSet& vars, const RooFormulaVar* cutVar,
123 const char* cutRange, std::size_t nStart, std::size_t nStop)
124{
125 // for the components, we need the set of variables without the index category
127 if (RooAbsArg * indexCat = varsNoIndex.find(*_indexCat)) {
128 varsNoIndex.remove(*indexCat,true) ;
129 }
130
131 // create an empty RooCompositeDataStore
132 auto out = std::make_unique<RooCompositeDataStore>(name, title, varsNoIndex, *_indexCat, std::map<std::string,RooAbsDataStore*>{});
133
134 RooCategory* indexCatForCut = nullptr;
136 if (cutVar) {
137 // find _indexCat in the formula recursive servers, if present
139 cutVar->getObservables(&_vars, formulaObservables);
141 if (indexCatForCut) {
142 // save initial index to restore it later
143 initialIndex = indexCatForCut->getCurrentIndex();
144 }
145 }
146
147 // fill it with reduced versions of components
148 for (const auto& item : _dataMap) {
149 if (indexCatForCut) indexCatForCut->setIndex(item.first);
150 out->_dataMap[item.first] = item.second->reduce(name, title, varsNoIndex, cutVar, cutRange, nStart, nStop).release();
151 }
152 if (indexCatForCut) {
153 // restore initial index
154 indexCatForCut->setIndex(initialIndex);
155 }
156
157 // indiceate component ownership and return
158 out->_ownComps = true;
159 return out;
160}
161
162
163////////////////////////////////////////////////////////////////////////////////
164/// Forward recalculate request to all subsets
165
167{
168 for (auto const& item : _dataMap) {
169 item.second->recalculateCache(proj,firstEvent,lastEvent,stepSize,skipZeroWeights) ;
170 }
171}
172
173
174////////////////////////////////////////////////////////////////////////////////
175
177{
178 bool ret(false) ;
179 for (auto const& item : _dataMap) {
180 ret |= item.second->hasFilledCache() ;
181 }
182 return ret ;
183}
184
185
186////////////////////////////////////////////////////////////////////////////////
187
189{
190 for (auto const& item : _dataMap) {
191 item.second->forceCacheUpdate() ;
192 }
193}
194
195
196
197////////////////////////////////////////////////////////////////////////////////
198/// Forward fill request to appropriate subset
199
201{
203 const_cast<RooArgSet*>((subset->get()))->assignValueOnly(_vars) ;
204 return subset->fill() ;
205}
206
207
208
209////////////////////////////////////////////////////////////////////////////////
210/// Forward fill request to appropriate subset
211
213{
214 double sum(0) ;
215
216 for (auto const& item : _dataMap) {
217 sum+= item.second->sumEntries() ;
218 }
219 return sum ;
220}
221
222
223
224////////////////////////////////////////////////////////////////////////////////
225/// Load the n-th data point (n='idx') in memory
226/// and return a pointer to the internal RooArgSet
227/// holding its coordinates.
228
230{
231 Int_t offset(0) ;
232 for (auto const& item : _dataMap) {
233 if (idx>=(offset+item.second->numEntries())) {
234 offset += item.second->numEntries() ;
235 continue ;
236 }
237 _vars.assign(*item.second->get(idx-offset)) ;
238
239 _indexCat->setIndex(item.first) ;
240 _curStore = item.second ;
241 _curIndex = idx-offset ;
242
243 return &_vars ;
244 }
245 return nullptr ;
246}
247
248
249
250////////////////////////////////////////////////////////////////////////////////
251
253{
254 if (!_curStore) get(0) ;
255 // coverity[FORWARD_NULL]
256 return _curStore->weight(_curIndex) ;
257}
258
259
260////////////////////////////////////////////////////////////////////////////////
261
263{
264 if (!_curStore) get(0) ;
265 // coverity[FORWARD_NULL]
266 return _curStore->weightError(etype) ;
267}
268
269
270
271
272////////////////////////////////////////////////////////////////////////////////
273
274void RooCompositeDataStore::weightError(double& lo, double& hi, RooAbsData::ErrorType etype) const
275{
276 if (!_curStore) get(0) ;
277 // coverity[FORWARD_NULL]
278 return _curStore->weightError(lo,hi,etype) ;
279}
280
281
282
283
284////////////////////////////////////////////////////////////////////////////////
285
287{
288 for (auto const& item : _dataMap) {
289 if (item.second->isWeighted()) return true ;
290 }
291 return false ;
292}
293
294
295////////////////////////////////////////////////////////////////////////////////
296
297void RooCompositeDataStore::loadValues(const RooAbsDataStore*, const RooFormulaVar*, const char*, std::size_t, std::size_t)
298{
299 throw(std::runtime_error("RooCompositeDataSore::loadValues() NOT IMPLEMENTED")) ;
300}
301
302
303
304////////////////////////////////////////////////////////////////////////////////
305/// Change name of internal observable named 'from' into 'to'
306
307bool RooCompositeDataStore::changeObservableName(const char* from, const char* to)
308{
309
310 // Find observable to be changed
311 RooAbsArg* var = _vars.find(from) ;
312
313 // Check that we found it
314 if (!var) {
315 coutE(InputArguments) << "RooCompositeDataStore::changeObservableName(" << GetName() << " no observable " << from << " in this dataset" << std::endl ;
316 return true ;
317 }
318
319 // Process name change
320 var->SetName(to) ;
321
322 // Forward name change request to component datasets
323 bool ret(false) ;
324 for (auto const& item : _dataMap) {
325 ret |= item.second->changeObservableName(from,to) ;
326 }
327
328 return ret ;
329}
330
331
332
333////////////////////////////////////////////////////////////////////////////////
334/// WVE ownership issue here!! Caller (a RooAbsData) should take ownership of all
335/// arguments, but only does for the first one here...
336
338{
339 RooAbsArg* ret(nullptr) ;
340 for (auto const& item : _dataMap) {
341 ret = item.second->addColumn(newVar,adjustRange) ;
342 }
343 if (ret) {
344 _vars.add(*ret) ;
345 }
346 return ret ;
347}
348
349
350////////////////////////////////////////////////////////////////////////////////
351
353{
354 throw string("RooCompositeDataStore::merge() is not implemented yet") ;
355}
356
357
358
359
360
361////////////////////////////////////////////////////////////////////////////////
362
364{
365 Int_t nevt = other.numEntries() ;
366 for (int i=0 ; i<nevt ; i++) {
367 _vars.assign(*other.get(i)) ;
368 fill() ;
369 }
370}
371
372
373
374////////////////////////////////////////////////////////////////////////////////
375
377{
378 Int_t n(0) ;
379 for (auto const& item : _dataMap) {
380 n += item.second->numEntries() ;
381 }
382 return n ;
383}
384
385
386
387
388////////////////////////////////////////////////////////////////////////////////
389
391{
392 for (auto const& item : _dataMap) {
393 item.second->reset() ;
394 }
395}
396
397
398
399////////////////////////////////////////////////////////////////////////////////
400
402{
403 for (auto const& item : _dataMap) {
404 item.second->cacheArgs(owner,newVarSet,nset,skipZeroWeights) ;
405 }
406}
407
408
409
410////////////////////////////////////////////////////////////////////////////////
411
413{
414 for (auto const& item : _dataMap) {
416 set.selectCommon(*item.second->get(), subset);
417 item.second->setArgStatus(subset,active) ;
418 }
419 return ;
420}
421
422
423
424////////////////////////////////////////////////////////////////////////////////
425/// Initialize cache of dataset: attach variables of cache ArgSet
426/// to the corresponding TTree branches
427
429{
430 for (auto const& item : _dataMap) {
431 item.second->attachCache(newOwner,inCachedVars) ;
432 }
433 return ;
434}
435
436
437
438////////////////////////////////////////////////////////////////////////////////
439
441{
442 for (auto const& item : _dataMap) {
443 item.second->resetCache() ;
444 }
445 return ;
446}
447
448
449
450////////////////////////////////////////////////////////////////////////////////
451
453{
454 for (auto const& item : _dataMap) {
455 item.second->attachBuffers(extObs);
456 }
457 return ;
458}
459
460
461
462////////////////////////////////////////////////////////////////////////////////
463
465{
466 for (auto const& item : _dataMap) {
467 item.second->resetBuffers();
468 }
469 return ;
470}
471
472
473////////////////////////////////////////////////////////////////////////////////
474
476{
477 std::cout << "RooCompositeDataStore::dump()" << std::endl ;
478 for (auto const& item : _dataMap) {
479 std::cout << "state number " << item.first << " has store " << item.second->ClassName() << " with variables " << *item.second->get() ;
480 if (item.second->isWeighted()) std::cout << " and is weighted " ;
481 std::cout << std::endl ;
482 }
483}
484
485
486////////////////////////////////////////////////////////////////////////////////
487/// Get the weights of the events in the range [first, first+len).
488/// This implementation will fill a vector with every event retrieved one by one
489/// (even if the weight is constant). Then, it returns a span.
490std::span<const double> RooCompositeDataStore::getWeightBatch(std::size_t first, std::size_t len) const {
491 if (!_weightBuffer) {
492 _weightBuffer = std::make_unique<std::vector<double>>();
493 _weightBuffer->reserve(len);
494
495 for (std::size_t i = 0; i < static_cast<std::size_t>(numEntries()); ++i) {
496 _weightBuffer->push_back(weight(i));
497 }
498 }
499
500 return {_weightBuffer->data() + first, len};
501}
#define coutE(a)
#define TRACE_DESTROY
Definition RooTrace.h:24
#define TRACE_CREATE
Definition RooTrace.h:23
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
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 offset
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 UChar_t len
char name[80]
Definition TGX11.cxx:110
#define hi
Common abstract base class for objects that represent a value and a "shape" in RooFit.
Definition RooAbsArg.h:77
void SetName(const char *name) override
Set the name of the TNamed.
value_type lookupIndex(const std::string &stateName) const
Find the index number corresponding to the state name.
RooAbsCollection & assignValueOnly(const RooAbsCollection &other, bool forceIfSizeOne=false)
Sets the value of any argument in our set that also appears in the other set.
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...
RooAbsArg * find(const char *name) const
Find object with given name in list.
Abstract base class for a data collection.
virtual double weightError(RooAbsData::ErrorType etype=RooAbsData::Poisson) const =0
virtual double weight() const =0
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:24
RooArgSet * selectCommon(const RooAbsCollection &refColl) const
Use RooAbsCollection::selecCommon(), but return as RooArgSet.
Definition RooArgSet.h:154
Object to represent discrete states.
Definition RooCategory.h:28
bool setIndex(Int_t index, bool printError=true) override
Set value by specifying the index code of the desired state.
value_type getCurrentIndex() const final
Return current index.
Definition RooCategory.h:40
Combines several disjunct datasets into one.
void attachCache(const RooAbsArg *newOwner, const RooArgSet &cachedVars) override
Initialize cache of dataset: attach variables of cache ArgSet to the corresponding TTree branches.
RooAbsArg * addColumn(RooAbsArg &var, bool adjustRange=true) override
WVE ownership issue here!! Caller (a RooAbsData) should take ownership of all arguments,...
std::unique_ptr< std::vector< double > > _weightBuffer
! Buffer for weights in case a batch of values is requested.
Int_t _curIndex
! Index associated with current event
void loadValues(const RooAbsDataStore *tds, const RooFormulaVar *select=nullptr, const char *rangeName=nullptr, std::size_t nStart=0, std::size_t nStop=std::numeric_limits< std::size_t >::max()) override
Int_t numEntries() const 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).
std::map< Int_t, RooAbsDataStore * > _dataMap
RooAbsDataStore * merge(const RooArgSet &allvars, std::list< RooAbsDataStore * > dstoreList) override
bool changeObservableName(const char *from, const char *to) override
Change name of internal observable named 'from' into 'to'.
~RooCompositeDataStore() override
Destructor.
void cacheArgs(const RooAbsArg *owner, RooArgSet &varSet, const RooArgSet *nset=nullptr, bool skipZeroWeights=false) override
void recalculateCache(const RooArgSet *, Int_t, Int_t, Int_t, bool) override
Forward recalculate request to all subsets.
bool isWeighted() const override
void setArgStatus(const RooArgSet &set, bool active) override
bool hasFilledCache() const override
RooAbsDataStore * _curStore
! Datastore associated with current event
double weightError(RooAbsData::ErrorType etype=RooAbsData::Poisson) const override
double sumEntries() const override
Forward fill request to appropriate subset.
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
Int_t fill() override
Forward fill request to appropriate subset.
void attachBuffers(const RooArgSet &extObs) override
double weight() const override
void append(RooAbsDataStore &other) override
virtual const RooArgSet * get() const
A RooFormulaVar is a generic implementation of a real-valued object, which takes a RooArgList of serv...
The RooStringView is a wrapper around a C-style string that can also be constructed from a std::strin...
const char * GetName() const override
Returns name of object.
Definition TNamed.h:49
const Int_t n
Definition legend1.C:16
static uint64_t sum(uint64_t i)
Definition Factory.cxx:2345