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