Logo ROOT   6.18/05
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
31#include "RooFit.h"
32#include "RooMsgService.h"
34
35#include "Riostream.h"
36#include "TTree.h"
37#include "TChain.h"
38#include "TDirectory.h"
39#include "TROOT.h"
40#include "RooFormulaVar.h"
41#include "RooRealVar.h"
42#include "RooTrace.h"
43#include "RooCategory.h"
44#include <iomanip>
45using namespace std ;
46
48;
49
50
51////////////////////////////////////////////////////////////////////////////////
52
53RooCompositeDataStore::RooCompositeDataStore() : _indexCat(0), _curStore(0), _curIndex(0), _ownComps(kFALSE)
54{
56}
57
58
59
60////////////////////////////////////////////////////////////////////////////////
61/// Convert map by label to map by index for more efficient internal use
62
63RooCompositeDataStore::RooCompositeDataStore(const char* name, const char* title, const RooArgSet& vars, RooCategory& indexCat,map<std::string,RooAbsDataStore*> inputData) :
64 RooAbsDataStore(name,title,RooArgSet(vars,indexCat)), _indexCat(&indexCat), _curStore(0), _curIndex(0), _ownComps(kFALSE)
65{
66 for (map<string,RooAbsDataStore*>::iterator iter=inputData.begin() ; iter!=inputData.end() ; ++iter) {
67 _dataMap[indexCat.lookupType(iter->first.c_str())->getVal()] = iter->second ;
68 }
70}
71
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 (map<Int_t,RooAbsDataStore*>::const_iterator iter=other._dataMap.begin() ; iter!=other._dataMap.end() ; ++iter) {
82 RooAbsDataStore* clonedata = iter->second->clone() ;
83 _dataMap[iter->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 (map<Int_t,RooAbsDataStore*>::const_iterator iter=other._dataMap.begin() ; iter!=other._dataMap.end() ; ++iter) {
102 RooAbsDataStore* clonedata = iter->second->clone(vars) ;
103 _dataMap[iter->first] = clonedata ;
104 }
106}
107
108
109
110
111////////////////////////////////////////////////////////////////////////////////
112/// Destructor
113
115{
116 if (_ownComps) {
117 map<int,RooAbsDataStore*>::const_iterator iter ;
118 for (iter = _dataMap.begin() ; iter!=_dataMap.end() ; ++iter) {
119 delete iter->second ;
120 }
121 }
123}
124
125
126////////////////////////////////////////////////////////////////////////////////
127/// Return true if currently loaded coordinate is considered valid within
128/// the current range definitions of all observables
129
131{
132 return kTRUE ;
133}
134
135
136
137
138////////////////////////////////////////////////////////////////////////////////
139/// Forward recalculate request to all subsets
140
141void RooCompositeDataStore::recalculateCache(const RooArgSet* proj, Int_t firstEvent, Int_t lastEvent, Int_t stepSize, Bool_t skipZeroWeights)
142{
143 map<int,RooAbsDataStore*>::const_iterator iter ;
144 for (iter = _dataMap.begin() ; iter!=_dataMap.end() ; ++iter) {
145 iter->second->recalculateCache(proj,firstEvent,lastEvent,stepSize,skipZeroWeights) ;
146 }
147}
148
149
150////////////////////////////////////////////////////////////////////////////////
151
153{
154 Bool_t ret(kFALSE) ;
155 map<int,RooAbsDataStore*>::const_iterator iter ;
156 for (iter = _dataMap.begin() ; iter!=_dataMap.end() ; ++iter) {
157 ret |= iter->second->hasFilledCache() ;
158 }
159 return ret ;
160}
161
162
163////////////////////////////////////////////////////////////////////////////////
164
166{
167 map<int,RooAbsDataStore*>::const_iterator iter ;
168 for (iter = _dataMap.begin() ; iter!=_dataMap.end() ; ++iter) {
169 iter->second->forceCacheUpdate() ;
170 }
171}
172
173
174
175////////////////////////////////////////////////////////////////////////////////
176/// Forward fill request to appropriate subset
177
179{
181 const_cast<RooArgSet*>((subset->get()))->assignValueOnly(_vars) ;
182 return subset->fill() ;
183}
184
185
186
187////////////////////////////////////////////////////////////////////////////////
188/// Forward fill request to appropriate subset
189
191{
192 Double_t sum(0) ;
193
194 map<int,RooAbsDataStore*>::const_iterator iter ;
195 for (iter = _dataMap.begin() ; iter!=_dataMap.end() ; ++iter) {
196 sum+= iter->second->sumEntries() ;
197 }
198 return sum ;
199}
200
201
202
203////////////////////////////////////////////////////////////////////////////////
204/// Load the n-th data point (n='idx') in memory
205/// and return a pointer to the internal RooArgSet
206/// holding its coordinates.
207
209{
210 Int_t offset(0) ;
211 map<int,RooAbsDataStore*>::const_iterator iter ;
212 for (iter = _dataMap.begin() ; iter!=_dataMap.end() ; ++iter) {
213 if (idx>=(offset+iter->second->numEntries())) {
214 offset += iter->second->numEntries() ;
215 continue ;
216 }
217 const_cast<RooCompositeDataStore*>(this)->_vars = (*iter->second->get(idx-offset)) ;
218
219 _indexCat->setIndex(iter->first) ;
220 _curStore = iter->second ;
221 _curIndex = idx-offset ;
222
223 return &_vars ;
224 }
225 return 0 ;
226}
227
228
229
230////////////////////////////////////////////////////////////////////////////////
231
233{
234 if (!_curStore) get(0) ;
235 // coverity[FORWARD_NULL]
236 return _curStore->weight(_curIndex) ;
237}
238
239
240
241
242
243////////////////////////////////////////////////////////////////////////////////
244
246{
247 get(idx) ;
248 return weight() ;
249}
250
251
252
253
254////////////////////////////////////////////////////////////////////////////////
255
257{
258 if (!_curStore) get(0) ;
259 // coverity[FORWARD_NULL]
260 return _curStore->weightError(etype) ;
261}
262
263
264
265
266////////////////////////////////////////////////////////////////////////////////
267
269{
270 if (!_curStore) get(0) ;
271 // coverity[FORWARD_NULL]
272 return _curStore->weightError(lo,hi,etype) ;
273}
274
275
276
277
278////////////////////////////////////////////////////////////////////////////////
279
281{
282 map<int,RooAbsDataStore*>::const_iterator iter ;
283 for (iter = _dataMap.begin() ; iter!=_dataMap.end() ; ++iter) {
284 if (iter->second->isWeighted()) return kTRUE ;
285 }
286 return kFALSE ; ;
287}
288
289
290////////////////////////////////////////////////////////////////////////////////
291
293{
294 throw(std::string("RooCompositeDataSore::loadValues() NOT IMPLEMENTED")) ;
295}
296
297
298
299////////////////////////////////////////////////////////////////////////////////
300/// Change name of internal observable named 'from' into 'to'
301
302Bool_t RooCompositeDataStore::changeObservableName(const char* from, const char* to)
303{
304
305 // Find observable to be changed
306 RooAbsArg* var = _vars.find(from) ;
307
308 // Check that we found it
309 if (!var) {
310 coutE(InputArguments) << "RooCompositeDataStore::changeObservableName(" << GetName() << " no observable " << from << " in this dataset" << endl ;
311 return kTRUE ;
312 }
313
314 // Process name change
315 var->SetName(to) ;
316
317 // Forward name change request to component datasets
318 Bool_t ret(kFALSE) ;
319 map<int,RooAbsDataStore*>::const_iterator iter ;
320 for (iter = _dataMap.begin() ; iter!=_dataMap.end() ; ++iter) {
321 ret |= iter->second->changeObservableName(from,to) ;
322 }
323
324 return ret ;
325}
326
327
328
329////////////////////////////////////////////////////////////////////////////////
330/// WVE ownership issue here!! Caller (a RooAbsData) should take ownership of all
331/// arguments, but only does for the first one here...
332
334{
335 RooAbsArg* ret(0) ;
336 map<int,RooAbsDataStore*>::const_iterator iter ;
337 for (iter = _dataMap.begin() ; iter!=_dataMap.end() ; ++iter) {
338 ret = iter->second->addColumn(newVar,adjustRange) ;
339 }
340 if (ret) {
341 _vars.add(*ret) ;
342 }
343 return ret ;
344}
345
346
347
348////////////////////////////////////////////////////////////////////////////////
349/// WVE ownership issue here!! Caller (a RooAbsData) should take ownership of all
350/// arguments, but only does for the first one here...
351
353{
354 RooArgSet* ret(0) ;
355 map<int,RooAbsDataStore*>::const_iterator iter ;
356 for (iter = _dataMap.begin() ; iter!=_dataMap.end() ; ++iter) {
357 ret = iter->second->addColumns(varList) ;
358 }
359 if (ret) {
360 _vars.add(*ret) ;
361 }
362 return ret ;
363}
364
365
366
367
368////////////////////////////////////////////////////////////////////////////////
369
370RooAbsDataStore* RooCompositeDataStore::merge(const RooArgSet& /*allVars*/, list<RooAbsDataStore*> /*dstoreList*/)
371{
372 throw string("RooCompositeDataStore::merge() is not implemented yet") ;
373}
374
375
376
377
378
379////////////////////////////////////////////////////////////////////////////////
380
382{
383 Int_t nevt = other.numEntries() ;
384 for (int i=0 ; i<nevt ; i++) {
385 _vars = *other.get(i) ;
386 fill() ;
387 }
388}
389
390
391
392////////////////////////////////////////////////////////////////////////////////
393
395{
396 Int_t n(0) ;
397 map<int,RooAbsDataStore*>::const_iterator iter ;
398 for (iter = _dataMap.begin() ; iter!=_dataMap.end() ; ++iter) {
399 n += iter->second->numEntries() ;
400 }
401 return n ;
402}
403
404
405
406
407////////////////////////////////////////////////////////////////////////////////
408
410{
411 map<int,RooAbsDataStore*>::const_iterator iter ;
412 for (iter = _dataMap.begin() ; iter!=_dataMap.end() ; ++iter) {
413 iter->second->reset() ;
414 }
415}
416
417
418
419////////////////////////////////////////////////////////////////////////////////
420
421void RooCompositeDataStore::cacheArgs(const RooAbsArg* owner, RooArgSet& newVarSet, const RooArgSet* nset, Bool_t skipZeroWeights)
422{
423 map<int,RooAbsDataStore*>::const_iterator iter ;
424 for (iter = _dataMap.begin() ; iter!=_dataMap.end() ; ++iter) {
425 iter->second->cacheArgs(owner,newVarSet,nset,skipZeroWeights) ;
426 }
427}
428
429
430
431////////////////////////////////////////////////////////////////////////////////
432
434{
435 map<int,RooAbsDataStore*>::const_iterator iter ;
436 for (iter = _dataMap.begin() ; iter!=_dataMap.end() ; ++iter) {
437 RooArgSet* subset = (RooArgSet*) set.selectCommon(*iter->second->get()) ;
438 iter->second->setArgStatus(*subset,active) ;
439 delete subset ;
440 }
441 return ;
442}
443
444
445
446////////////////////////////////////////////////////////////////////////////////
447/// Initialize cache of dataset: attach variables of cache ArgSet
448/// to the corresponding TTree branches
449
450void RooCompositeDataStore::attachCache(const RooAbsArg* newOwner, const RooArgSet& inCachedVars)
451{
452 map<int,RooAbsDataStore*>::const_iterator iter ;
453 for (iter = _dataMap.begin() ; iter!=_dataMap.end() ; ++iter) {
454 iter->second->attachCache(newOwner,inCachedVars) ;
455 }
456 return ;
457}
458
459
460
461////////////////////////////////////////////////////////////////////////////////
462
464{
465 map<int,RooAbsDataStore*>::const_iterator iter ;
466 for (iter = _dataMap.begin() ; iter!=_dataMap.end() ; ++iter) {
467 iter->second->resetCache() ;
468 }
469 return ;
470}
471
472
473
474////////////////////////////////////////////////////////////////////////////////
475
477{
478 map<int,RooAbsDataStore*>::const_iterator iter ;
479 for (iter = _dataMap.begin() ; iter!=_dataMap.end() ; ++iter) {
480 iter->second->attachBuffers(extObs);
481 }
482 return ;
483}
484
485
486
487////////////////////////////////////////////////////////////////////////////////
488
490{
491 map<int,RooAbsDataStore*>::const_iterator iter ;
492 for (iter = _dataMap.begin() ; iter!=_dataMap.end() ; ++iter) {
493 iter->second->resetBuffers();
494 }
495 return ;
496}
497
498
499////////////////////////////////////////////////////////////////////////////////
500
502{
503 cout << "RooCompositeDataStore::dump()" << endl ;
504 map<int,RooAbsDataStore*>::const_iterator iter ;
505 for (iter = _dataMap.begin() ; iter!=_dataMap.end() ; ++iter) {
506 cout << "state number " << iter->first << " has store " << iter->second->IsA()->GetName() << " with variables " << *iter->second->get() ;
507 if (iter->second->isWeighted()) cout << " and is weighted " ;
508 cout << endl ;
509 }
510}
511
512
#define coutE(a)
Definition: RooMsgService.h:34
#define TRACE_DESTROY
Definition: RooTrace.h:23
#define TRACE_CREATE
Definition: RooTrace.h:22
int Int_t
Definition: RtypesCore.h:41
const Bool_t kFALSE
Definition: RtypesCore.h:88
bool Bool_t
Definition: RtypesCore.h:59
double Double_t
Definition: RtypesCore.h:55
const Bool_t kTRUE
Definition: RtypesCore.h:87
#define ClassImp(name)
Definition: Rtypes.h:365
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:70
void SetName(const char *name)
Set the name of the TNamed.
Definition: RooAbsArg.cxx:2341
const RooCatType * lookupType(Int_t index, Bool_t printError=kFALSE) const
Find our type corresponding to the specified index, or return 0 for no match.
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
Int_t getVal() const
Definition: RooCatType.h:79
RooCategory represents a fundamental (non-derived) discrete value object.
Definition: RooCategory.h:24
virtual Int_t getIndex() const
Return index number of current state.
Definition: RooCategory.h:34
virtual Bool_t setIndex(Int_t index, Bool_t printError=kTRUE)
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.
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.
void loadValues(const RooAbsDataStore *tds, const RooFormulaVar *select=0, const char *rangeName=0, Int_t nStart=0, Int_t nStop=2000000000)
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:27
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:58
static long int sum(long int i)
Definition: Factory.cxx:2258