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 "RooFit.h"
34#include "RooMsgService.h"
35#include "Riostream.h"
36#include "RooFormulaVar.h"
37#include "RooRealVar.h"
38#include "RooTrace.h"
39#include "RooCategory.h"
40
41#include "TTree.h"
42#include "TChain.h"
43#include "TDirectory.h"
44#include "TROOT.h"
45
46#include <iomanip>
47#include <iostream>
48
49using namespace std;
50
52;
53
54
55////////////////////////////////////////////////////////////////////////////////
56
57RooCompositeDataStore::RooCompositeDataStore() : _indexCat(0), _curStore(0), _curIndex(0), _ownComps(kFALSE)
58{
60}
61
62
63
64////////////////////////////////////////////////////////////////////////////////
65/// Convert map by label to map by index for more efficient internal use
66
67RooCompositeDataStore::RooCompositeDataStore(const char* name, const char* title, const RooArgSet& vars, RooCategory& indexCat,map<std::string,RooAbsDataStore*> inputData) :
68 RooAbsDataStore(name,title,RooArgSet(vars,indexCat)), _indexCat(&indexCat), _curStore(0), _curIndex(0), _ownComps(kFALSE)
69{
70 for (const auto& iter : inputData) {
71 const RooAbsCategory::value_type idx = indexCat.lookupIndex(iter.first);
72 _dataMap[idx] = iter.second;
73 }
75}
76
77
78
79
80////////////////////////////////////////////////////////////////////////////////
81/// Convert map by label to map by index for more efficient internal use
82
84 RooAbsDataStore(other,newname), _indexCat(other._indexCat), _curStore(other._curStore), _curIndex(other._curIndex), _ownComps(kTRUE)
85{
86 for (map<Int_t,RooAbsDataStore*>::const_iterator iter=other._dataMap.begin() ; iter!=other._dataMap.end() ; ++iter) {
87 RooAbsDataStore* clonedata = iter->second->clone() ;
88 _dataMap[iter->first] = clonedata ;
89 }
91}
92
93
94////////////////////////////////////////////////////////////////////////////////
95/// Update index category pointer, if it is contained in input argument vars
96
97RooCompositeDataStore::RooCompositeDataStore(const RooCompositeDataStore& other, const RooArgSet& vars, const char* newname) :
98 RooAbsDataStore(other,vars,newname), _indexCat(other._indexCat), _curStore(other._curStore), _curIndex(other._curIndex), _ownComps(kTRUE)
99{
100 RooCategory* newIdx = (RooCategory*) vars.find(other._indexCat->GetName()) ;
101 if (newIdx) {
102 _indexCat = newIdx ;
103 }
104
105 // Convert map by label to map by index for more efficient internal use
106 for (map<Int_t,RooAbsDataStore*>::const_iterator iter=other._dataMap.begin() ; iter!=other._dataMap.end() ; ++iter) {
107 RooAbsDataStore* clonedata = iter->second->clone(vars) ;
108 _dataMap[iter->first] = clonedata ;
109 }
111}
112
113
114
115
116////////////////////////////////////////////////////////////////////////////////
117/// Destructor
118
120{
121 if (_ownComps) {
122 map<int,RooAbsDataStore*>::const_iterator iter ;
123 for (iter = _dataMap.begin() ; iter!=_dataMap.end() ; ++iter) {
124 delete iter->second ;
125 }
126 }
128}
129
130
131////////////////////////////////////////////////////////////////////////////////
132/// Return true if currently loaded coordinate is considered valid within
133/// the current range definitions of all observables
134
136{
137 return kTRUE ;
138}
139
140
141
142
143////////////////////////////////////////////////////////////////////////////////
144/// Forward recalculate request to all subsets
145
146void RooCompositeDataStore::recalculateCache(const RooArgSet* proj, Int_t firstEvent, Int_t lastEvent, Int_t stepSize, Bool_t skipZeroWeights)
147{
148 map<int,RooAbsDataStore*>::const_iterator iter ;
149 for (iter = _dataMap.begin() ; iter!=_dataMap.end() ; ++iter) {
150 iter->second->recalculateCache(proj,firstEvent,lastEvent,stepSize,skipZeroWeights) ;
151 }
152}
153
154
155////////////////////////////////////////////////////////////////////////////////
156
158{
159 Bool_t ret(kFALSE) ;
160 map<int,RooAbsDataStore*>::const_iterator iter ;
161 for (iter = _dataMap.begin() ; iter!=_dataMap.end() ; ++iter) {
162 ret |= iter->second->hasFilledCache() ;
163 }
164 return ret ;
165}
166
167
168////////////////////////////////////////////////////////////////////////////////
169
171{
172 map<int,RooAbsDataStore*>::const_iterator iter ;
173 for (iter = _dataMap.begin() ; iter!=_dataMap.end() ; ++iter) {
174 iter->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_t sum(0) ;
198
199 map<int,RooAbsDataStore*>::const_iterator iter ;
200 for (iter = _dataMap.begin() ; iter!=_dataMap.end() ; ++iter) {
201 sum+= iter->second->sumEntries() ;
202 }
203 return sum ;
204}
205
206
207
208////////////////////////////////////////////////////////////////////////////////
209/// Load the n-th data point (n='idx') in memory
210/// and return a pointer to the internal RooArgSet
211/// holding its coordinates.
212
214{
215 Int_t offset(0) ;
216 map<int,RooAbsDataStore*>::const_iterator iter ;
217 for (iter = _dataMap.begin() ; iter!=_dataMap.end() ; ++iter) {
218 if (idx>=(offset+iter->second->numEntries())) {
219 offset += iter->second->numEntries() ;
220 continue ;
221 }
222 const_cast<RooCompositeDataStore*>(this)->_vars = (*iter->second->get(idx-offset)) ;
223
224 _indexCat->setIndex(iter->first) ;
225 _curStore = iter->second ;
226 _curIndex = idx-offset ;
227
228 return &_vars ;
229 }
230 return 0 ;
231}
232
233
234
235////////////////////////////////////////////////////////////////////////////////
236
238{
239 if (!_curStore) get(0) ;
240 // coverity[FORWARD_NULL]
241 return _curStore->weight(_curIndex) ;
242}
243
244
245
246
247
248////////////////////////////////////////////////////////////////////////////////
249
251{
252 get(idx) ;
253 return weight() ;
254}
255
256
257
258
259////////////////////////////////////////////////////////////////////////////////
260
262{
263 if (!_curStore) get(0) ;
264 // coverity[FORWARD_NULL]
265 return _curStore->weightError(etype) ;
266}
267
268
269
270
271////////////////////////////////////////////////////////////////////////////////
272
274{
275 if (!_curStore) get(0) ;
276 // coverity[FORWARD_NULL]
277 return _curStore->weightError(lo,hi,etype) ;
278}
279
280
281
282
283////////////////////////////////////////////////////////////////////////////////
284
286{
287 map<int,RooAbsDataStore*>::const_iterator iter ;
288 for (iter = _dataMap.begin() ; iter!=_dataMap.end() ; ++iter) {
289 if (iter->second->isWeighted()) return kTRUE ;
290 }
291 return kFALSE ; ;
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_t 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" << endl ;
316 return kTRUE ;
317 }
318
319 // Process name change
320 var->SetName(to) ;
321
322 // Forward name change request to component datasets
323 Bool_t ret(kFALSE) ;
324 map<int,RooAbsDataStore*>::const_iterator iter ;
325 for (iter = _dataMap.begin() ; iter!=_dataMap.end() ; ++iter) {
326 ret |= iter->second->changeObservableName(from,to) ;
327 }
328
329 return ret ;
330}
331
332
333
334////////////////////////////////////////////////////////////////////////////////
335/// WVE ownership issue here!! Caller (a RooAbsData) should take ownership of all
336/// arguments, but only does for the first one here...
337
339{
340 RooAbsArg* ret(0) ;
341 map<int,RooAbsDataStore*>::const_iterator iter ;
342 for (iter = _dataMap.begin() ; iter!=_dataMap.end() ; ++iter) {
343 ret = iter->second->addColumn(newVar,adjustRange) ;
344 }
345 if (ret) {
346 _vars.add(*ret) ;
347 }
348 return ret ;
349}
350
351
352
353////////////////////////////////////////////////////////////////////////////////
354/// WVE ownership issue here!! Caller (a RooAbsData) should take ownership of all
355/// arguments, but only does for the first one here...
356
358{
359 RooArgSet* ret(0) ;
360 map<int,RooAbsDataStore*>::const_iterator iter ;
361 for (iter = _dataMap.begin() ; iter!=_dataMap.end() ; ++iter) {
362 ret = iter->second->addColumns(varList) ;
363 }
364 if (ret) {
365 _vars.add(*ret) ;
366 }
367 return ret ;
368}
369
370
371
372
373////////////////////////////////////////////////////////////////////////////////
374
375RooAbsDataStore* RooCompositeDataStore::merge(const RooArgSet& /*allVars*/, list<RooAbsDataStore*> /*dstoreList*/)
376{
377 throw string("RooCompositeDataStore::merge() is not implemented yet") ;
378}
379
380
381
382
383
384////////////////////////////////////////////////////////////////////////////////
385
387{
388 Int_t nevt = other.numEntries() ;
389 for (int i=0 ; i<nevt ; i++) {
390 _vars = *other.get(i) ;
391 fill() ;
392 }
393}
394
395
396
397////////////////////////////////////////////////////////////////////////////////
398
400{
401 Int_t n(0) ;
402 map<int,RooAbsDataStore*>::const_iterator iter ;
403 for (iter = _dataMap.begin() ; iter!=_dataMap.end() ; ++iter) {
404 n += iter->second->numEntries() ;
405 }
406 return n ;
407}
408
409
410
411
412////////////////////////////////////////////////////////////////////////////////
413
415{
416 map<int,RooAbsDataStore*>::const_iterator iter ;
417 for (iter = _dataMap.begin() ; iter!=_dataMap.end() ; ++iter) {
418 iter->second->reset() ;
419 }
420}
421
422
423
424////////////////////////////////////////////////////////////////////////////////
425
426void RooCompositeDataStore::cacheArgs(const RooAbsArg* owner, RooArgSet& newVarSet, const RooArgSet* nset, Bool_t skipZeroWeights)
427{
428 map<int,RooAbsDataStore*>::const_iterator iter ;
429 for (iter = _dataMap.begin() ; iter!=_dataMap.end() ; ++iter) {
430 iter->second->cacheArgs(owner,newVarSet,nset,skipZeroWeights) ;
431 }
432}
433
434
435
436////////////////////////////////////////////////////////////////////////////////
437
439{
440 map<int,RooAbsDataStore*>::const_iterator iter ;
441 for (iter = _dataMap.begin() ; iter!=_dataMap.end() ; ++iter) {
442 RooArgSet* subset = (RooArgSet*) set.selectCommon(*iter->second->get()) ;
443 iter->second->setArgStatus(*subset,active) ;
444 delete subset ;
445 }
446 return ;
447}
448
449
450
451////////////////////////////////////////////////////////////////////////////////
452/// Initialize cache of dataset: attach variables of cache ArgSet
453/// to the corresponding TTree branches
454
455void RooCompositeDataStore::attachCache(const RooAbsArg* newOwner, const RooArgSet& inCachedVars)
456{
457 map<int,RooAbsDataStore*>::const_iterator iter ;
458 for (iter = _dataMap.begin() ; iter!=_dataMap.end() ; ++iter) {
459 iter->second->attachCache(newOwner,inCachedVars) ;
460 }
461 return ;
462}
463
464
465
466////////////////////////////////////////////////////////////////////////////////
467
469{
470 map<int,RooAbsDataStore*>::const_iterator iter ;
471 for (iter = _dataMap.begin() ; iter!=_dataMap.end() ; ++iter) {
472 iter->second->resetCache() ;
473 }
474 return ;
475}
476
477
478
479////////////////////////////////////////////////////////////////////////////////
480
482{
483 map<int,RooAbsDataStore*>::const_iterator iter ;
484 for (iter = _dataMap.begin() ; iter!=_dataMap.end() ; ++iter) {
485 iter->second->attachBuffers(extObs);
486 }
487 return ;
488}
489
490
491
492////////////////////////////////////////////////////////////////////////////////
493
495{
496 map<int,RooAbsDataStore*>::const_iterator iter ;
497 for (iter = _dataMap.begin() ; iter!=_dataMap.end() ; ++iter) {
498 iter->second->resetBuffers();
499 }
500 return ;
501}
502
503
504////////////////////////////////////////////////////////////////////////////////
505
507{
508 cout << "RooCompositeDataStore::dump()" << endl ;
509 map<int,RooAbsDataStore*>::const_iterator iter ;
510 for (iter = _dataMap.begin() ; iter!=_dataMap.end() ; ++iter) {
511 cout << "state number " << iter->first << " has store " << iter->second->IsA()->GetName() << " with variables " << *iter->second->get() ;
512 if (iter->second->isWeighted()) cout << " and is weighted " ;
513 cout << endl ;
514 }
515}
516
517
#define coutE(a)
Definition: RooMsgService.h:33
#define TRACE_DESTROY
Definition: RooTrace.h:23
#define TRACE_CREATE
Definition: RooTrace.h:22
int Int_t
Definition: RtypesCore.h:43
const Bool_t kFALSE
Definition: RtypesCore.h:90
bool Bool_t
Definition: RtypesCore.h:61
double Double_t
Definition: RtypesCore.h:57
const Bool_t kTRUE
Definition: RtypesCore.h:89
#define ClassImp(name)
Definition: Rtypes.h:361
char name[80]
Definition: TGX11.cxx:109
float type_of_call hi(const int &, const int &)
RooAbsArg is the common abstract base class for objects that represent a value (of arbitrary type) an...
Definition: RooAbsArg.h:73
void SetName(const char *name)
Set the name of the TNamed.
Definition: RooAbsArg.cxx:2216
int value_type
The type used to denote a specific category state.
value_type lookupIndex(const std::string &stateName) const
Find the index number corresponding to the state name.
RooAbsCollection * selectCommon(const RooAbsCollection &refColl) const
Create a subset of the current collection, consisting only of those elements that are contained as we...
RooAbsCollection & assignValueOnly(const RooAbsCollection &other, Bool_t oneSafe=kFALSE)
The assignment operator sets the value of any argument in our set that also appears in the other set.
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
RooArgList is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgList.h:21
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgSet.h:28
virtual Bool_t add(const RooAbsCollection &col, Bool_t silent=kFALSE)
Add a collection of arguments to this collection by calling add() for each element in the source coll...
Definition: RooArgSet.h:88
RooCategory is an object to represent discrete states.
Definition: RooCategory.h:23
virtual value_type getCurrentIndex() const override final
Return current index.
Definition: RooCategory.h:35
virtual Bool_t setIndex(Int_t index, bool printError=true) override
Set value by specifying the index code of the desired state.
RooCompositeDataStore combines several disjunct datasets into one.
virtual RooAbsArg * addColumn(RooAbsArg &var, Bool_t adjustRange=kTRUE)
WVE ownership issue here!! Caller (a RooAbsData) should take ownership of all arguments,...
Int_t _curIndex
Datastore associated with current event.
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())
virtual Bool_t changeObservableName(const char *from, const char *to)
Change name of internal observable named 'from' into 'to'.
virtual Bool_t valid() const
Return true if currently loaded coordinate is considered valid within the current range definitions o...
std::map< Int_t, RooAbsDataStore * > _dataMap
virtual void attachBuffers(const RooArgSet &extObs)
void attachCache(const RooAbsArg *newOwner, const RooArgSet &cachedVars)
Initialize cache of dataset: attach variables of cache ArgSet to the corresponding TTree branches.
virtual Double_t weightError(RooAbsData::ErrorType etype=RooAbsData::Poisson) const
Bool_t _ownComps
Index associated with current event.
virtual Double_t sumEntries() const
Forward fill request to appropriate subset.
virtual void setArgStatus(const RooArgSet &set, Bool_t active)
virtual RooArgSet * addColumns(const RooArgList &varList)
WVE ownership issue here!! Caller (a RooAbsData) should take ownership of all arguments,...
virtual Int_t numEntries() const
virtual void append(RooAbsDataStore &other)
virtual Bool_t isWeighted() const
virtual Double_t weight() const
virtual void cacheArgs(const RooAbsArg *owner, RooArgSet &varSet, const RooArgSet *nset=0, Bool_t skipZeroWeights=kFALSE)
virtual ~RooCompositeDataStore()
Destructor.
RooAbsDataStore * merge(const RooArgSet &allvars, std::list< RooAbsDataStore * > dstoreList)
virtual Bool_t hasFilledCache() const
virtual void recalculateCache(const RooArgSet *, Int_t, Int_t, Int_t, Bool_t)
Forward recalculate request to all subsets.
virtual const RooArgSet * get() const
virtual Int_t fill()
Forward fill request to appropriate subset.
A RooFormulaVar is a generic implementation of a real-valued object, which takes a RooArgList of serv...
Definition: RooFormulaVar.h:29
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
const Int_t n
Definition: legend1.C:16
@ InputArguments
Definition: RooGlobalFunc.h:68
static long int sum(long int i)
Definition: Factory.cxx:2275