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 namespace std;
43
45
46
47////////////////////////////////////////////////////////////////////////////////
48
50{
52}
53
54
55
56////////////////////////////////////////////////////////////////////////////////
57/// Convert map by label to map by index for more efficient internal use
58
61 const RooArgSet& vars, RooCategory& indexCat,map<std::string,RooAbsDataStore*> const& inputData) :
62 RooAbsDataStore(name,title,RooArgSet(vars,indexCat)), _indexCat(&indexCat)
63{
64 for (const auto& iter : inputData) {
65 const RooAbsCategory::value_type idx = indexCat.lookupIndex(iter.first);
66 _dataMap[idx] = iter.second;
67 }
69}
70
71
72////////////////////////////////////////////////////////////////////////////////
73/// Convert map by label to map by index for more efficient internal use
74
76 RooAbsDataStore(other,newname), _indexCat(other._indexCat), _curStore(other._curStore), _curIndex(other._curIndex), _ownComps(true)
77{
78 for (const auto& item : other._dataMap) {
79 RooAbsDataStore* clonedata = item.second->clone() ;
80 _dataMap[item.first] = clonedata ;
81 }
83}
84
85
86////////////////////////////////////////////////////////////////////////////////
87/// Update index category pointer, if it is contained in input argument vars
88
89RooCompositeDataStore::RooCompositeDataStore(const RooCompositeDataStore& other, const RooArgSet& vars, const char* newname) :
90 RooAbsDataStore(other,vars,newname), _indexCat(other._indexCat), _curStore(other._curStore), _curIndex(other._curIndex), _ownComps(true)
91{
92 RooCategory* newIdx = static_cast<RooCategory*>(vars.find(other._indexCat->GetName())) ;
93 if (newIdx) {
94 _indexCat = newIdx ;
95 }
96
97 // Convert map by label to map by index for more efficient internal use
98 for (const auto& item : other._dataMap) {
99 RooAbsDataStore* clonedata = item.second->clone(vars) ;
100 _dataMap[item.first] = clonedata ;
101 }
103}
104
105
106
107
108////////////////////////////////////////////////////////////////////////////////
109/// Destructor
110
112{
113 if (_ownComps) {
114 for (auto& item : _dataMap) {
115 delete item.second;
116 }
117 }
119}
120
121
122std::unique_ptr<RooAbsDataStore> RooCompositeDataStore::reduce(
123 RooStringView name, RooStringView title, const RooArgSet& vars, const RooFormulaVar* cutVar,
124 const char* cutRange, std::size_t nStart, std::size_t nStop)
125{
126 // for the components, we need the set of variables without the index category
127 RooArgSet varsNoIndex{vars};
128 if (RooAbsArg * indexCat = varsNoIndex.find(*_indexCat)) {
129 varsNoIndex.remove(*indexCat,true) ;
130 }
131
132 // create an empty RooCompositeDataStore
133 auto out = std::make_unique<RooCompositeDataStore>(name, title, varsNoIndex, *_indexCat, std::map<std::string,RooAbsDataStore*>{});
134
135 // fill it with reduced versions of components
136 for (const auto& item : _dataMap) {
137 out->_dataMap[item.first] = item.second->reduce(name, title, varsNoIndex, cutVar, cutRange, nStart, nStop).release();
138 }
139
140 // indiceate component ownership and return
141 out->_ownComps = true;
142 return out;
143}
144
145
146////////////////////////////////////////////////////////////////////////////////
147/// Forward recalculate request to all subsets
148
149void RooCompositeDataStore::recalculateCache(const RooArgSet* proj, Int_t firstEvent, Int_t lastEvent, Int_t stepSize, bool skipZeroWeights)
150{
151 for (auto const& item : _dataMap) {
152 item.second->recalculateCache(proj,firstEvent,lastEvent,stepSize,skipZeroWeights) ;
153 }
154}
155
156
157////////////////////////////////////////////////////////////////////////////////
158
160{
161 bool ret(false) ;
162 for (auto const& item : _dataMap) {
163 ret |= item.second->hasFilledCache() ;
164 }
165 return ret ;
166}
167
168
169////////////////////////////////////////////////////////////////////////////////
170
172{
173 for (auto const& item : _dataMap) {
174 item.second->forceCacheUpdate() ;
175 }
176}
177
178
179
180////////////////////////////////////////////////////////////////////////////////
181/// Forward fill request to appropriate subset
182
184{
186 const_cast<RooArgSet*>((subset->get()))->assignValueOnly(_vars) ;
187 return subset->fill() ;
188}
189
190
191
192////////////////////////////////////////////////////////////////////////////////
193/// Forward fill request to appropriate subset
194
196{
197 double sum(0) ;
198
199 for (auto const& item : _dataMap) {
200 sum+= item.second->sumEntries() ;
201 }
202 return sum ;
203}
204
205
206
207////////////////////////////////////////////////////////////////////////////////
208/// Load the n-th data point (n='idx') in memory
209/// and return a pointer to the internal RooArgSet
210/// holding its coordinates.
211
213{
214 Int_t offset(0) ;
215 for (auto const& item : _dataMap) {
216 if (idx>=(offset+item.second->numEntries())) {
217 offset += item.second->numEntries() ;
218 continue ;
219 }
220 _vars.assign(*item.second->get(idx-offset)) ;
221
222 _indexCat->setIndex(item.first) ;
223 _curStore = item.second ;
224 _curIndex = idx-offset ;
225
226 return &_vars ;
227 }
228 return nullptr ;
229}
230
231
232
233////////////////////////////////////////////////////////////////////////////////
234
236{
237 if (!_curStore) get(0) ;
238 // coverity[FORWARD_NULL]
239 return _curStore->weight(_curIndex) ;
240}
241
242
243////////////////////////////////////////////////////////////////////////////////
244
246{
247 if (!_curStore) get(0) ;
248 // coverity[FORWARD_NULL]
249 return _curStore->weightError(etype) ;
250}
251
252
253
254
255////////////////////////////////////////////////////////////////////////////////
256
257void RooCompositeDataStore::weightError(double& lo, double& hi, RooAbsData::ErrorType etype) const
258{
259 if (!_curStore) get(0) ;
260 // coverity[FORWARD_NULL]
261 return _curStore->weightError(lo,hi,etype) ;
262}
263
264
265
266
267////////////////////////////////////////////////////////////////////////////////
268
270{
271 for (auto const& item : _dataMap) {
272 if (item.second->isWeighted()) return true ;
273 }
274 return false ; ;
275}
276
277
278////////////////////////////////////////////////////////////////////////////////
279
280void RooCompositeDataStore::loadValues(const RooAbsDataStore*, const RooFormulaVar*, const char*, std::size_t, std::size_t)
281{
282 throw(std::runtime_error("RooCompositeDataSore::loadValues() NOT IMPLEMENTED")) ;
283}
284
285
286
287////////////////////////////////////////////////////////////////////////////////
288/// Change name of internal observable named 'from' into 'to'
289
290bool RooCompositeDataStore::changeObservableName(const char* from, const char* to)
291{
292
293 // Find observable to be changed
294 RooAbsArg* var = _vars.find(from) ;
295
296 // Check that we found it
297 if (!var) {
298 coutE(InputArguments) << "RooCompositeDataStore::changeObservableName(" << GetName() << " no observable " << from << " in this dataset" << endl ;
299 return true ;
300 }
301
302 // Process name change
303 var->SetName(to) ;
304
305 // Forward name change request to component datasets
306 bool ret(false) ;
307 for (auto const& item : _dataMap) {
308 ret |= item.second->changeObservableName(from,to) ;
309 }
310
311 return ret ;
312}
313
314
315
316////////////////////////////////////////////////////////////////////////////////
317/// WVE ownership issue here!! Caller (a RooAbsData) should take ownership of all
318/// arguments, but only does for the first one here...
319
321{
322 RooAbsArg* ret(nullptr) ;
323 for (auto const& item : _dataMap) {
324 ret = item.second->addColumn(newVar,adjustRange) ;
325 }
326 if (ret) {
327 _vars.add(*ret) ;
328 }
329 return ret ;
330}
331
332
333////////////////////////////////////////////////////////////////////////////////
334
335RooAbsDataStore* RooCompositeDataStore::merge(const RooArgSet& /*allVars*/, list<RooAbsDataStore*> /*dstoreList*/)
336{
337 throw string("RooCompositeDataStore::merge() is not implemented yet") ;
338}
339
340
341
342
343
344////////////////////////////////////////////////////////////////////////////////
345
347{
348 Int_t nevt = other.numEntries() ;
349 for (int i=0 ; i<nevt ; i++) {
350 _vars.assign(*other.get(i)) ;
351 fill() ;
352 }
353}
354
355
356
357////////////////////////////////////////////////////////////////////////////////
358
360{
361 Int_t n(0) ;
362 for (auto const& item : _dataMap) {
363 n += item.second->numEntries() ;
364 }
365 return n ;
366}
367
368
369
370
371////////////////////////////////////////////////////////////////////////////////
372
374{
375 for (auto const& item : _dataMap) {
376 item.second->reset() ;
377 }
378}
379
380
381
382////////////////////////////////////////////////////////////////////////////////
383
384void RooCompositeDataStore::cacheArgs(const RooAbsArg* owner, RooArgSet& newVarSet, const RooArgSet* nset, bool skipZeroWeights)
385{
386 for (auto const& item : _dataMap) {
387 item.second->cacheArgs(owner,newVarSet,nset,skipZeroWeights) ;
388 }
389}
390
391
392
393////////////////////////////////////////////////////////////////////////////////
394
396{
397 for (auto const& item : _dataMap) {
398 RooArgSet* subset = static_cast<RooArgSet*>(set.selectCommon(*item.second->get())) ;
399 item.second->setArgStatus(*subset,active) ;
400 delete subset ;
401 }
402 return ;
403}
404
405
406
407////////////////////////////////////////////////////////////////////////////////
408/// Initialize cache of dataset: attach variables of cache ArgSet
409/// to the corresponding TTree branches
410
411void RooCompositeDataStore::attachCache(const RooAbsArg* newOwner, const RooArgSet& inCachedVars)
412{
413 for (auto const& item : _dataMap) {
414 item.second->attachCache(newOwner,inCachedVars) ;
415 }
416 return ;
417}
418
419
420
421////////////////////////////////////////////////////////////////////////////////
422
424{
425 for (auto const& item : _dataMap) {
426 item.second->resetCache() ;
427 }
428 return ;
429}
430
431
432
433////////////////////////////////////////////////////////////////////////////////
434
436{
437 for (auto const& item : _dataMap) {
438 item.second->attachBuffers(extObs);
439 }
440 return ;
441}
442
443
444
445////////////////////////////////////////////////////////////////////////////////
446
448{
449 for (auto const& item : _dataMap) {
450 item.second->resetBuffers();
451 }
452 return ;
453}
454
455
456////////////////////////////////////////////////////////////////////////////////
457
459{
460 cout << "RooCompositeDataStore::dump()" << endl ;
461 for (auto const& item : _dataMap) {
462 cout << "state number " << item.first << " has store " << item.second->ClassName() << " with variables " << *item.second->get() ;
463 if (item.second->isWeighted()) cout << " and is weighted " ;
464 cout << endl ;
465 }
466}
467
468
469////////////////////////////////////////////////////////////////////////////////
470/// Get the weights of the events in the range [first, first+len).
471/// This implementation will fill a vector with every event retrieved one by one
472/// (even if the weight is constant). Then, it returns a span.
473std::span<const double> RooCompositeDataStore::getWeightBatch(std::size_t first, std::size_t len) const {
474 if (!_weightBuffer) {
475 _weightBuffer = std::make_unique<std::vector<double>>();
476 _weightBuffer->reserve(len);
477
478 for (std::size_t i = 0; i < static_cast<std::size_t>(numEntries()); ++i) {
479 _weightBuffer->push_back(weight(i));
480 }
481 }
482
483 return {_weightBuffer->data() + first, len};
484}
#define coutE(a)
#define TRACE_DESTROY
Definition RooTrace.h:24
#define TRACE_CREATE
Definition RooTrace.h:23
#define ClassImp(name)
Definition Rtypes.h:377
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.
virtual bool remove(const RooAbsArg &var, bool silent=false, bool matchByNameOnly=false)
Remove the specified argument from our list.
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 * first() const
bool selectCommon(const RooAbsCollection &refColl, RooAbsCollection &outColl) const
Create a subset of the current collection, consisting only of those elements that are contained as we...
RooAbsArg * find(const char *name) const
Find object with given name in list.
Abstract base class for a data collection.
virtual const RooArgSet * get(Int_t index) const =0
virtual RooAbsDataStore * clone(const char *newname=nullptr) const =0
virtual Int_t fill()=0
virtual double weightError(RooAbsData::ErrorType etype=RooAbsData::Poisson) const =0
virtual double weight() const =0
virtual Int_t numEntries() const =0
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:55
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:47
virtual const char * ClassName() const
Returns name of class to which the object belongs.
Definition TObject.cxx:207
const Int_t n
Definition legend1.C:16
static uint64_t sum(uint64_t i)
Definition Factory.cxx:2345