Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RooCollectionProxy.h
Go to the documentation of this file.
1/*****************************************************************************
2 * Project: RooFit *
3 * Package: RooFitCore *
4 * File: $Id: RooSetProxy.h,v 1.21 2007/07/13 21:24:36 wouter Exp $
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\class RooCollectionProxy
19\ingroup Roofitcore
20RooCollectionProxy is the concrete proxy for RooArgSet or RooArgList objects.
21A RooCollectionProxy is the general mechanism to store a RooArgSet or RooArgList
22with RooAbsArgs in a RooAbsArg.
23Creating a RooCollectionProxy adds all members of the proxied RooArgSet to the proxy owners
24server list (thus receiving value/shape dirty flags from it) and
25registers itself with the owning class. The latter allows the
26owning class to update the pointers of RooArgSet or RooArgList contents to reflect
27the serverRedirect changes.
28**/
29
30#ifndef roofit_roofitcore_RooFit_RooCollectionProxy_h
31#define roofit_roofitcore_RooFit_RooCollectionProxy_h
32
33#include <RooAbsProxy.h>
34#include <RooAbsArg.h>
35#include <RooArgSet.h>
36
37#include <exception>
38
39template <class RooCollection_t>
40class RooCollectionProxy final : public RooCollection_t, public RooAbsProxy {
41public:
42 // Constructors, assignment etc.
44
45 /// Construct proxy with given name and description, with given owner
46 /// The default value and shape dirty propagation of the set contents
47 /// to the set owner is controlled by flags defValueServer and defShapeServer.
48 RooCollectionProxy(const char *inName, const char * /*desc*/, RooAbsArg *owner, bool defValueServer = true,
49 bool defShapeServer = false)
51 {
52 _owner->registerProxy(*this);
53 }
54
55 /// Copy constructor.
56 template <class Other_t>
57 RooCollectionProxy(const char *inName, RooAbsArg *owner, const Other_t &other)
58 : RooCollection_t(other, inName),
59 _owner(owner),
62 {
63 _owner->registerProxy(*this);
64 }
65
66 /// Initializes this RooCollection proxy from another proxy. Should not be
67 /// considered part of the public interface, only to be used by IO.
68 template <class Other_t>
69 void initializeAfterIOConstructor(RooAbsArg *owner, const Other_t &other)
70 {
71
72 // Copy all attributes from "other"
73 _owner = owner;
74 _defValueServer = other.defValueServer();
75 _defShapeServer = other.defShapeServer();
76 RooCollection_t::setName(other.GetName());
77
78 // Add the elements. This should not be done with
79 // RooCollectionProxy::add(), but with the base class method. Otherwise,
80 // _owner->addServer() is called when adding each element, which we don't
81 // want for IO because the list of servers are already filled when
82 // reading the server list of the object in the file.
83 RooCollection_t::add(other);
84
85 // Don't do _owner->registerProxy(*this) here! The proxy list will also be copied separately.
86 }
87
88 // Assignment is deleted because it is not clear how it should behave.
89 // Should default assignment be used? But then, it will use the assignment
90 // operators of the RooFit collections, which actually don't do assignment,
91 // but value syncronization! Should it be re-implemented to be actual
92 // assignment? That would be inconsistent with the base class! So it's
93 // better to not support it at all.
96
98 {
99 if (_owner)
100 _owner->unRegisterProxy(*this);
101 }
102
103 const char *name() const override { return RooCollection_t::GetName(); }
104
105 // List content management (modified for server hooks)
107 bool add(const RooAbsArg &var, bool valueServer, bool shapeServer, bool silent);
108
109 /// Overloaded RooCollection_t::add() method inserts 'var' into set
110 /// and registers 'var' as server to owner with default value
111 /// and shape dirty flag propagation.
112 bool add(const RooAbsArg &var, bool silent = false) override
113 {
114 return add(var, _defValueServer, _defShapeServer, silent);
115 }
116
118
119// The following function is not memory safe, because it takes ownership of var
120// without moving it. It is not publically available in the memory safe
121// interfaces mode.
122#ifdef ROOFIT_MEMORY_SAFE_INTERFACES
123protected:
124#endif
125 bool addOwned(RooAbsArg &var, bool silent = false) override;
126#ifdef ROOFIT_MEMORY_SAFE_INTERFACES
127public:
128#endif
129
131 RooAbsArg *addClone(const RooAbsArg &var, bool silent = false) override;
132
133 bool replace(const RooAbsArg &var1, const RooAbsArg &var2) override;
134 bool remove(const RooAbsArg &var, bool silent = false, bool matchByNameOnly = false) override;
135
136 /// Remove each argument in the input list from our list using remove(const RooAbsArg&).
137 /// and remove each argument as server to owner
138 bool remove(const RooAbsCollection &list, bool silent = false, bool matchByNameOnly = false)
139 {
140 bool result(false);
141 for (auto const &arg : list) {
142 result |= remove(*arg, silent, matchByNameOnly);
143 }
144 return result;
145 }
146
147 void removeAll() override;
148
149 void print(std::ostream &os, bool addContents = false) const override;
150
151 /// Assign values of arguments on other set to arguments in this set.
153 {
154 RooCollection_t::operator=(other);
155 return *this;
156 }
157
158 bool defValueServer() const { return _defValueServer; }
159 bool defShapeServer() const { return _defShapeServer; }
160
161private:
162 RooAbsArg *_owner = nullptr;
163 bool _defValueServer = false;
164 bool _defShapeServer = false;
165
166 bool
167 changePointer(const RooAbsCollection &newServerSet, bool nameChange = false, bool factoryInitMode = false) override;
168
169 bool changePointer(std::unordered_map<RooAbsArg *, RooAbsArg *> const &replacements) override;
170
171 void checkValid() const
172 {
173 if (!_owner) {
174 throw std::runtime_error(
175 "Attempt to add elements to a RooSetProxy or RooListProxy without owner!"
176 " Please avoid using the RooListProxy default constructor, which should only be used by IO.");
177 }
178 }
179
181};
182
183////////////////////////////////////////////////////////////////////////////////
184/// Overloaded RooCollection_t::add() method insert object into set
185/// and registers object as server to owner with given value
186/// and shape dirty flag propagation requests
187
188template <class RooCollection_t>
189bool RooCollectionProxy<RooCollection_t>::add(const RooAbsArg &var, bool valueServer, bool shapeServer, bool silent)
190{
191 checkValid();
192 bool ret = RooCollection_t::add(var, silent);
193 if (ret) {
194 _owner->addServer((RooAbsArg &)var, valueServer, shapeServer);
195 }
196 return ret;
197}
198
199////////////////////////////////////////////////////////////////////////////////
200/// Overloaded RooCollection_t::addOwned() method insert object into owning set
201/// and registers object as server to owner with default value
202/// and shape dirty flag propagation
203
204template <class RooCollection_t>
206{
207 checkValid();
208 bool ret = RooCollection_t::addOwned(var, silent);
209 if (ret) {
210 _owner->addServer((RooAbsArg &)var, _defValueServer, _defShapeServer);
211 }
212 return ret;
213}
214
215////////////////////////////////////////////////////////////////////////////////
216/// Overloaded RooCollection_t::addClone() method insert clone of object into owning set
217/// and registers cloned object as server to owner with default value
218/// and shape dirty flag propagation
219
220template <class RooCollection_t>
222{
223 checkValid();
224 RooAbsArg *ret = RooCollection_t::addClone(var, silent);
225 if (ret) {
226 _owner->addServer((RooAbsArg &)var, _defValueServer, _defShapeServer);
227 }
228 return ret;
229}
230
231////////////////////////////////////////////////////////////////////////////////
232/// Replace object 'var1' in set with 'var2'. Deregister var1 as
233/// server from owner and register var2 as server to owner with
234/// default value and shape dirty propagation flags
235
236template <class RooCollection_t>
238{
239 bool ret = RooCollection_t::replace(var1, var2);
240 if (ret) {
241 if (!RooCollection_t::isOwning())
242 _owner->removeServer((RooAbsArg &)var1);
243 _owner->addServer((RooAbsArg &)var2, _owner->isValueServer(var1), _owner->isShapeServer(var2));
244 }
245 return ret;
246}
247
248////////////////////////////////////////////////////////////////////////////////
249/// Remove object 'var' from set and deregister 'var' as server to owner.
250
251template <class RooCollection_t>
252bool RooCollectionProxy<RooCollection_t>::remove(const RooAbsArg &var, bool silent, bool matchByNameOnly)
253{
254 bool ret = RooCollection_t::remove(var, silent, matchByNameOnly);
255 if (ret && !RooCollection_t::isOwning()) {
256 _owner->removeServer((RooAbsArg &)var);
257 }
258 return ret;
259}
260
261////////////////////////////////////////////////////////////////////////////////
262/// Remove all argument inset using remove(const RooAbsArg&).
263/// and remove each argument as server to owner
264
265template <class RooCollection_t>
267{
268 if (!RooCollection_t::isOwning()) {
269 for (auto const &arg : *this) {
270 if (!RooCollection_t::isOwning()) {
271 _owner->removeServer(*arg);
272 }
273 }
274 }
275
276 RooCollection_t::removeAll();
277}
278
279////////////////////////////////////////////////////////////////////////////////
280/// Process server change operation on owner. Replace elements in set with equally
281/// named objects in 'newServerList'
282
283template <class RooCollection_t>
285 bool factoryInitMode)
286{
287 if (RooCollection_t::empty()) {
288 if (factoryInitMode) {
289 for (const auto arg : newServerList) {
290 if (arg != _owner) {
291 add(*arg, true);
292 }
293 }
294 } else {
295 return true;
296 }
297 }
298
299 bool error(false);
300 for (auto const &arg : *this) {
301 RooAbsArg *newArg = arg->findNewServer(newServerList, nameChange);
302 if (newArg && newArg != _owner)
303 error |= !RooCollection_t::replace(*arg, *newArg);
304 }
305 return !error;
306}
307
308template <class RooCollection_t>
310 std::unordered_map<RooAbsArg *, RooAbsArg *> const &replacements)
311{
312 bool error(false);
313 for (auto const &arg : *this) {
314 auto newArgFound = replacements.find(arg);
315 if (newArgFound != replacements.end()) {
316 error |= !RooCollection_t::replace(*arg, *newArgFound->second);
317 }
318 }
319 return !error;
320}
321
322////////////////////////////////////////////////////////////////////////////////
323/// Printing name of proxy on ostream. If addContents is true
324/// also print names of objects in set
325
326template <class RooCollection_t>
327void RooCollectionProxy<RooCollection_t>::print(std::ostream &os, bool addContents) const
328{
329 if (!addContents) {
330 os << name() << "=";
331 RooCollection_t::printStream(os, RooPrintable::kValue, RooPrintable::kInline);
332 } else {
333 os << name() << "=(";
334 bool first2(true);
335 for (auto const &arg : *this) {
336 if (first2) {
337 first2 = false;
338 } else {
339 os << ",";
340 }
342 }
343 os << ")";
344 }
345}
346
348
349#endif
#define ClassDefOverride(name, id)
Definition Rtypes.h:341
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 result
char name[80]
Definition TGX11.cxx:110
RooAbsArg is the common abstract base class for objects that represent a value and a "shape" in RooFi...
Definition RooAbsArg.h:74
void registerProxy(RooArgProxy &proxy)
Register an RooArgProxy in the proxy list.
void unRegisterProxy(RooArgProxy &proxy)
Remove proxy from proxy list.
bool isValueServer(const RooAbsArg &arg) const
Check if this is serving values to arg.
Definition RooAbsArg.h:221
RooAbsArg * findNewServer(const RooAbsCollection &newSet, bool nameChange) const
Find the new server in the specified set that matches the old server.
RooAbsCollection is an abstract container object that can hold multiple RooAbsArg objects.
virtual bool add(const RooAbsArg &var, bool silent=false)
Add the specified argument to list.
virtual bool addOwned(RooAbsArg &var, bool silent=false)
Add an argument and transfer the ownership to the collection.
virtual RooAbsArg * addClone(const RooAbsArg &var, bool silent=false)
Add a clone of the specified argument to list.
RooAbsProxy is the abstact interface for proxy classes.
Definition RooAbsProxy.h:37
RooCollectionProxy is the concrete proxy for RooArgSet or RooArgList objects.
RooCollectionProxy & operator=(RooCollectionProxy &&other)=delete
void removeAll() override
Remove all argument inset using remove(const RooAbsArg&).
bool changePointer(const RooAbsCollection &newServerSet, bool nameChange=false, bool factoryInitMode=false) override
Process server change operation on owner.
void print(std::ostream &os, bool addContents=false) const override
Printing name of proxy on ostream.
RooCollectionProxy & operator=(const RooCollection_t &other)
Assign values of arguments on other set to arguments in this set.
bool changePointer(std::unordered_map< RooAbsArg *, RooAbsArg * > const &replacements) override
void initializeAfterIOConstructor(RooAbsArg *owner, const Other_t &other)
Initializes this RooCollection proxy from another proxy.
bool addOwned(RooAbsArg &var, bool silent=false) override
Overloaded RooCollection_t::addOwned() method insert object into owning set and registers object as s...
RooCollectionProxy & operator=(RooCollectionProxy const &other)=delete
bool remove(const RooAbsCollection &list, bool silent=false, bool matchByNameOnly=false)
Remove each argument in the input list from our list using remove(const RooAbsArg&).
bool add(const RooAbsArg &var, bool silent=false) override
Overloaded RooCollection_t::add() method inserts 'var' into set and registers 'var' as server to owne...
RooAbsArg * addClone(const RooAbsArg &var, bool silent=false) override
Overloaded RooCollection_t::addClone() method insert clone of object into owning set and registers cl...
bool add(const RooAbsArg &var, bool valueServer, bool shapeServer, bool silent)
Overloaded RooCollection_t::add() method insert object into set and registers object as server to own...
RooCollectionProxy(const char *inName, RooAbsArg *owner, const Other_t &other)
Copy constructor.
const char * name() const override
bool remove(const RooAbsArg &var, bool silent=false, bool matchByNameOnly=false) override
Remove object 'var' from set and deregister 'var' as server to owner.
bool replace(const RooAbsArg &var1, const RooAbsArg &var2) override
Replace object 'var1' in set with 'var2'.
RooCollectionProxy(const char *inName, const char *, RooAbsArg *owner, bool defValueServer=true, bool defShapeServer=false)
Construct proxy with given name and description, with given owner The default value and shape dirty p...