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 "RooCategory.h"
37
38#include <iomanip>
39#include <iostream>
40
41using std::map, std::list, std::string;
42
43
44
45////////////////////////////////////////////////////////////////////////////////
46
50
51
52
53////////////////////////////////////////////////////////////////////////////////
54/// Convert map by label to map by index for more efficient internal use
55
59 RooAbsDataStore(name,title,RooArgSet(vars,indexCat)), _indexCat(&indexCat)
60{
61 for (const auto& iter : inputData) {
62 const RooAbsCategory::value_type idx = indexCat.lookupIndex(iter.first);
63 _dataMap[idx] = iter.second;
64 }
65}
66
67
68////////////////////////////////////////////////////////////////////////////////
69/// Convert map by label to map by index for more efficient internal use
70
72 RooAbsDataStore(other,newname), _indexCat(other._indexCat), _curStore(other._curStore), _curIndex(other._curIndex), _ownComps(true)
73{
74 for (const auto& item : other._dataMap) {
75 RooAbsDataStore* clonedata = item.second->clone() ;
76 _dataMap[item.first] = clonedata ;
77 }
78}
79
80
81////////////////////////////////////////////////////////////////////////////////
82/// Update index category pointer, if it is contained in input argument vars
83
85 RooAbsDataStore(other,vars,newname), _indexCat(other._indexCat), _curStore(other._curStore), _curIndex(other._curIndex), _ownComps(true)
86{
87 RooCategory* newIdx = static_cast<RooCategory*>(vars.find(other._indexCat->GetName())) ;
88 if (newIdx) {
90 }
91
92 // Convert map by label to map by index for more efficient internal use
93 for (const auto& item : other._dataMap) {
94 RooAbsDataStore* clonedata = item.second->clone(vars) ;
95 _dataMap[item.first] = clonedata ;
96 }
97}
98
99
100
101
102////////////////////////////////////////////////////////////////////////////////
103/// Destructor
104
106{
107 if (_ownComps) {
108 for (auto& item : _dataMap) {
109 delete item.second;
110 }
111 }
112}
113
114
115std::unique_ptr<RooAbsDataStore> RooCompositeDataStore::reduce(
116 RooStringView name, RooStringView title, const RooArgSet& vars, const RooFormulaVar* cutVar,
117 const char* cutRange, std::size_t nStart, std::size_t nStop)
118{
119 // for the components, we need the set of variables without the index category
121 if (RooAbsArg * indexCat = varsNoIndex.find(*_indexCat)) {
122 varsNoIndex.remove(*indexCat,true) ;
123 }
124
125 // create an empty RooCompositeDataStore
126 auto out = std::make_unique<RooCompositeDataStore>(name, title, varsNoIndex, *_indexCat, std::map<std::string,RooAbsDataStore*>{});
127
128 RooCategory* indexCatForCut = nullptr;
130 if (cutVar) {
131 // find _indexCat in the formula recursive servers, if present
133 cutVar->getObservables(&_vars, formulaObservables);
135 if (indexCatForCut) {
136 // save initial index to restore it later
137 initialIndex = indexCatForCut->getCurrentIndex();
138 }
139 }
140
141 // fill it with reduced versions of components
142 for (const auto& item : _dataMap) {
143 if (indexCatForCut) indexCatForCut->setIndex(item.first);
144 out->_dataMap[item.first] = item.second->reduce(name, title, varsNoIndex, cutVar, cutRange, nStart, nStop).release();
145 }
146 if (indexCatForCut) {
147 // restore initial index
148 indexCatForCut->setIndex(initialIndex);
149 }
150
151 // indiceate component ownership and return
152 out->_ownComps = true;
153 return out;
154}
155
156
157////////////////////////////////////////////////////////////////////////////////
158/// Forward recalculate request to all subsets
159
166
167
168////////////////////////////////////////////////////////////////////////////////
169
171{
172 bool ret(false) ;
173 for (auto const& item : _dataMap) {
174 ret |= item.second->hasFilledCache() ;
175 }
176 return ret ;
177}
178
179
180////////////////////////////////////////////////////////////////////////////////
181
183{
184 for (auto const& item : _dataMap) {
185 item.second->forceCacheUpdate() ;
186 }
187}
188
189
190
191////////////////////////////////////////////////////////////////////////////////
192/// Forward fill request to appropriate subset
193
195{
197 const_cast<RooArgSet*>((subset->get()))->assignValueOnly(_vars) ;
198 return subset->fill() ;
199}
200
201
202
203////////////////////////////////////////////////////////////////////////////////
204/// Forward fill request to appropriate subset
205
207{
208 double sum(0) ;
209
210 for (auto const& item : _dataMap) {
211 sum+= item.second->sumEntries() ;
212 }
213 return sum ;
214}
215
216
217
218////////////////////////////////////////////////////////////////////////////////
219/// Load the n-th data point (n='idx') in memory
220/// and return a pointer to the internal RooArgSet
221/// holding its coordinates.
222
224{
225 Int_t offset(0) ;
226 for (auto const& item : _dataMap) {
227 if (idx>=(offset+item.second->numEntries())) {
228 offset += item.second->numEntries() ;
229 continue ;
230 }
231 _vars.assign(*item.second->get(idx-offset)) ;
232
233 _indexCat->setIndex(item.first) ;
234 _curStore = item.second ;
235 _curIndex = idx-offset ;
236
237 return &_vars ;
238 }
239 return nullptr ;
240}
241
242
243
244////////////////////////////////////////////////////////////////////////////////
245
247{
248 if (!_curStore) get(0) ;
249 // coverity[FORWARD_NULL]
250 return _curStore->weight(_curIndex) ;
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
268void RooCompositeDataStore::weightError(double& lo, double& hi, RooAbsData::ErrorType etype) const
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 for (auto const& item : _dataMap) {
283 if (item.second->isWeighted()) return true ;
284 }
285 return false ;
286}
287
288
289////////////////////////////////////////////////////////////////////////////////
290
291void RooCompositeDataStore::loadValues(const RooAbsDataStore*, const RooFormulaVar*, const char*, std::size_t, std::size_t)
292{
293 throw(std::runtime_error("RooCompositeDataSore::loadValues() NOT IMPLEMENTED")) ;
294}
295
296
297
298////////////////////////////////////////////////////////////////////////////////
299/// Change name of internal observable named 'from' into 'to'
300
301bool RooCompositeDataStore::changeObservableName(const char* from, const char* to)
302{
303
304 // Find observable to be changed
305 RooAbsArg* var = _vars.find(from) ;
306
307 // Check that we found it
308 if (!var) {
309 coutE(InputArguments) << "RooCompositeDataStore::changeObservableName(" << GetName() << " no observable " << from << " in this dataset" << std::endl ;
310 return true ;
311 }
312
313 // Process name change
314 var->SetName(to) ;
315
316 // Forward name change request to component datasets
317 bool ret(false) ;
318 for (auto const& item : _dataMap) {
319 ret |= item.second->changeObservableName(from,to) ;
320 }
321
322 return ret ;
323}
324
325
326
327////////////////////////////////////////////////////////////////////////////////
328/// WVE ownership issue here!! Caller (a RooAbsData) should take ownership of all
329/// arguments, but only does for the first one here...
330
332{
333 RooAbsArg* ret(nullptr) ;
334 for (auto const& item : _dataMap) {
335 ret = item.second->addColumn(newVar,adjustRange) ;
336 }
337 if (ret) {
338 _vars.add(*ret) ;
339 }
340 return ret ;
341}
342
343
344////////////////////////////////////////////////////////////////////////////////
345
347{
348 throw string("RooCompositeDataStore::merge() is not implemented yet") ;
349}
350
351
352
353
354
355////////////////////////////////////////////////////////////////////////////////
356
358{
359 Int_t nevt = other.numEntries() ;
360 for (int i=0 ; i<nevt ; i++) {
361 _vars.assign(*other.get(i)) ;
362 fill() ;
363 }
364}
365
366
367
368////////////////////////////////////////////////////////////////////////////////
369
371{
372 Int_t n(0) ;
373 for (auto const& item : _dataMap) {
374 n += item.second->numEntries() ;
375 }
376 return n ;
377}
378
379
380
381
382////////////////////////////////////////////////////////////////////////////////
383
385{
386 for (auto const& item : _dataMap) {
387 item.second->reset() ;
388 }
389}
390
391
392
393////////////////////////////////////////////////////////////////////////////////
394
396{
397 for (auto const& item : _dataMap) {
398 item.second->cacheArgs(owner,newVarSet,nset,skipZeroWeights) ;
399 }
400}
401
402
403
404////////////////////////////////////////////////////////////////////////////////
405
407{
408 for (auto const& item : _dataMap) {
410 set.selectCommon(*item.second->get(), subset);
411 item.second->setArgStatus(subset,active) ;
412 }
413 return ;
414}
415
416
417
418////////////////////////////////////////////////////////////////////////////////
419/// Initialize cache of dataset: attach variables of cache ArgSet
420/// to the corresponding TTree branches
421
423{
424 for (auto const& item : _dataMap) {
425 item.second->attachCache(newOwner,inCachedVars) ;
426 }
427 return ;
428}
429
430
431
432////////////////////////////////////////////////////////////////////////////////
433
435{
436 for (auto const& item : _dataMap) {
437 item.second->resetCache() ;
438 }
439 return ;
440}
441
442
443
444////////////////////////////////////////////////////////////////////////////////
445
447{
448 for (auto const& item : _dataMap) {
449 item.second->attachBuffers(extObs);
450 }
451 return ;
452}
453
454
455
456////////////////////////////////////////////////////////////////////////////////
457
459{
460 for (auto const& item : _dataMap) {
461 item.second->resetBuffers();
462 }
463 return ;
464}
465
466
467////////////////////////////////////////////////////////////////////////////////
468
470{
471 std::cout << "RooCompositeDataStore::dump()" << std::endl ;
472 for (auto const& item : _dataMap) {
473 std::cout << "state number " << item.first << " has store " << item.second->ClassName() << " with variables " << *item.second->get() ;
474 if (item.second->isWeighted()) std::cout << " and is weighted " ;
475 std::cout << std::endl ;
476 }
477}
478
479
480////////////////////////////////////////////////////////////////////////////////
481/// Get the weights of the events in the range [first, first+len).
482/// This implementation will fill a vector with every event retrieved one by one
483/// (even if the weight is constant). Then, it returns a span.
484std::span<const double> RooCompositeDataStore::getWeightBatch(std::size_t first, std::size_t len) const {
485 if (!_weightBuffer) {
486 _weightBuffer = std::make_unique<std::vector<double>>();
487 _weightBuffer->reserve(len);
488
489 for (std::size_t i = 0; i < static_cast<std::size_t>(numEntries()); ++i) {
490 _weightBuffer->push_back(weight(i));
491 }
492 }
493
494 return {_weightBuffer->data() + first, len};
495}
#define coutE(a)
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
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:148
#define hi
Common abstract base class for objects that represent a value and a "shape" in RooFit.
Definition RooAbsArg.h:76
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.
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 * find(const char *name) const
Find object with given name in list.
Abstract base class for a data collection.
virtual double weightError(RooAbsData::ErrorType etype=RooAbsData::Poisson) const =0
virtual double weight() const =0
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:24
RooArgSet * selectCommon(const RooAbsCollection &refColl) const
Use RooAbsCollection::selecCommon(), but return as RooArgSet.
Definition RooArgSet.h:154
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:49
const Int_t n
Definition legend1.C:16
static uint64_t sum(uint64_t i)
Definition Factory.cxx:2335