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