Logo ROOT  
Reference Guide
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), _owner(owner), _defValueServer(other.defValueServer()),
60 {
61 _owner->registerProxy(*this);
62 }
63
64 /// Initializes this RooCollection proxy from another proxy. Should not be
65 /// considered part of the public interface, only to be used by IO.
66 template <class Other_t>
67 void initializeAfterIOConstructor(RooAbsArg *owner, const Other_t &other)
68 {
69
70 // Copy all attributes from "other"
71 _owner = owner;
72 _defValueServer = other.defValueServer();
73 _defShapeServer = other.defShapeServer();
74 RooCollection_t::setName(other.GetName());
75
76 // Add the elements. This should not be done with
77 // RooCollectionProxy::add(), but with the base class method. Otherwise,
78 // _owner->addServer() is called when adding each element, which we don't
79 // want for IO because the list of servers are already filled when
80 // reading the server list of the object in the file.
81 RooCollection_t::add(other);
82
83 // Don't do _owner->registerProxy(*this) here! The proxy list will also be copied separately.
84 }
85
86 // Assignment is deleted because it is not clear how it should behave.
87 // Should default assignment be used? But then, it will use the assignment
88 // operators of the RooFit collections, which actually don't do assignment,
89 // but value syncronization! Should it be re-implemented to be actual
90 // assignment? That would be inconsistent with the base class! So it's
91 // better to not support it at all.
94
96 {
97 if (_owner)
98 _owner->unRegisterProxy(*this);
99 }
100
101 const char *name() const override { return RooCollection_t::GetName(); }
102
103 // List content management (modified for server hooks)
105 bool add(const RooAbsArg &var, bool valueServer, bool shapeServer, bool silent);
106
107 /// Overloaded RooCollection_t::add() method inserts 'var' into set
108 /// and registers 'var' as server to owner with default value
109 /// and shape dirty flag propagation.
110 bool add(const RooAbsArg &var, bool silent = false) override
111 {
112 return add(var, _defValueServer, _defShapeServer, silent);
113 }
114
116 bool addOwned(RooAbsArg &var, bool silent = false) override;
117
119 RooAbsArg *addClone(const RooAbsArg &var, bool silent = false) override;
120
121 bool replace(const RooAbsArg &var1, const RooAbsArg &var2) override;
122 bool remove(const RooAbsArg &var, bool silent = false, bool matchByNameOnly = false) override;
123
124 /// Remove each argument in the input list from our list using remove(const RooAbsArg&).
125 /// and remove each argument as server to owner
126 bool remove(const RooAbsCollection &list, bool silent = false, bool matchByNameOnly = false)
127 {
128 bool result(false);
129 for (auto const &arg : list) {
130 result |= remove(*arg, silent, matchByNameOnly);
131 }
132 return result;
133 }
134
135 void removeAll() override;
136
137 void print(std::ostream &os, bool addContents = false) const override;
138
139 /// Assign values of arguments on other set to arguments in this set.
141 {
143 return *this;
144 }
145
146 bool defValueServer() const { return _defValueServer; }
147 bool defShapeServer() const { return _defShapeServer; }
148
149private:
150 RooAbsArg *_owner = nullptr;
151 bool _defValueServer = false;
152 bool _defShapeServer = false;
153
154 bool
155 changePointer(const RooAbsCollection &newServerSet, bool nameChange = false, bool factoryInitMode = false) override;
156
157 void checkValid() const
158 {
159 if (!_owner) {
160 throw std::runtime_error(
161 "Attempt to add elements to a RooSetProxy or RooListProxy without owner!"
162 " Please avoid using the RooListProxy default constructor, which should only be used by IO.");
163 }
164 }
165
167};
168
169////////////////////////////////////////////////////////////////////////////////
170/// Overloaded RooCollection_t::add() method insert object into set
171/// and registers object as server to owner with given value
172/// and shape dirty flag propagation requests
173
174template <class RooCollection_t>
175bool RooCollectionProxy<RooCollection_t>::add(const RooAbsArg &var, bool valueServer, bool shapeServer, bool silent)
176{
177 checkValid();
178 bool ret = RooCollection_t::add(var, silent);
179 if (ret) {
180 _owner->addServer((RooAbsArg &)var, valueServer, shapeServer);
181 }
182 return ret;
183}
184
185////////////////////////////////////////////////////////////////////////////////
186/// Overloaded RooCollection_t::addOwned() method insert object into owning set
187/// and registers object as server to owner with default value
188/// and shape dirty flag propagation
189
190template <class RooCollection_t>
192{
193 checkValid();
194 bool ret = RooCollection_t::addOwned(var, silent);
195 if (ret) {
196 _owner->addServer((RooAbsArg &)var, _defValueServer, _defShapeServer);
197 }
198 return ret;
199}
200
201////////////////////////////////////////////////////////////////////////////////
202/// Overloaded RooCollection_t::addClone() method insert clone of object into owning set
203/// and registers cloned object as server to owner with default value
204/// and shape dirty flag propagation
205
206template <class RooCollection_t>
208{
209 checkValid();
210 RooAbsArg *ret = RooCollection_t::addClone(var, silent);
211 if (ret) {
212 _owner->addServer((RooAbsArg &)var, _defValueServer, _defShapeServer);
213 }
214 return ret;
215}
216
217////////////////////////////////////////////////////////////////////////////////
218/// Replace object 'var1' in set with 'var2'. Deregister var1 as
219/// server from owner and register var2 as server to owner with
220/// default value and shape dirty propagation flags
221
222template <class RooCollection_t>
224{
225 bool ret = RooCollection_t::replace(var1, var2);
226 if (ret) {
227 if (!RooCollection_t::isOwning())
228 _owner->removeServer((RooAbsArg &)var1);
229 _owner->addServer((RooAbsArg &)var2, _owner->isValueServer(var1), _owner->isShapeServer(var2));
230 }
231 return ret;
232}
233
234////////////////////////////////////////////////////////////////////////////////
235/// Remove object 'var' from set and deregister 'var' as server to owner.
236
237template <class RooCollection_t>
238bool RooCollectionProxy<RooCollection_t>::remove(const RooAbsArg &var, bool silent, bool matchByNameOnly)
239{
240 bool ret = RooCollection_t::remove(var, silent, matchByNameOnly);
241 if (ret && !RooCollection_t::isOwning()) {
242 _owner->removeServer((RooAbsArg &)var);
243 }
244 return ret;
245}
246
247////////////////////////////////////////////////////////////////////////////////
248/// Remove all argument inset using remove(const RooAbsArg&).
249/// and remove each argument as server to owner
250
251template <class RooCollection_t>
253{
254 if (!RooCollection_t::isOwning()) {
255 for (auto const &arg : *this) {
256 if (!RooCollection_t::isOwning()) {
257 _owner->removeServer(*arg);
258 }
259 }
260 }
261
262 RooCollection_t::removeAll();
263}
264
265////////////////////////////////////////////////////////////////////////////////
266/// Process server change operation on owner. Replace elements in set with equally
267/// named objects in 'newServerList'
268
269template <class RooCollection_t>
271 bool factoryInitMode)
272{
273 if (RooCollection_t::empty()) {
274 if (factoryInitMode) {
275 for (const auto arg : newServerList) {
276 if (arg != _owner) {
277 add(*arg, true);
278 }
279 }
280 } else {
281 return true;
282 }
283 }
284
285 bool error(false);
286 for (auto const &arg : *this) {
287 RooAbsArg *newArg = arg->findNewServer(newServerList, nameChange);
288 if (newArg && newArg != _owner)
289 error |= !RooCollection_t::replace(*arg, *newArg);
290 }
291 return !error;
292}
293
294////////////////////////////////////////////////////////////////////////////////
295/// Printing name of proxy on ostream. If addContents is true
296/// also print names of objects in set
297
298template <class RooCollection_t>
299void RooCollectionProxy<RooCollection_t>::print(std::ostream &os, bool addContents) const
300{
301 if (!addContents) {
302 os << name() << "=";
303 RooCollection_t::printStream(os, RooPrintable::kValue, RooPrintable::kInline);
304 } else {
305 os << name() << "=(";
306 bool first2(true);
307 for (auto const &arg : *this) {
308 if (first2) {
309 first2 = false;
310 } else {
311 os << ",";
312 }
314 }
315 os << ")";
316 }
317}
318
320
321#endif
#define ClassDefOverride(name, id)
Definition: Rtypes.h:339
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
Binding & operator=(OUT(*fun)(void))
RooAbsArg is the common abstract base class for objects that represent a value and a "shape" in RooFi...
Definition: RooAbsArg.h:72
void registerProxy(RooArgProxy &proxy)
Register an RooArgProxy in the proxy list.
Definition: RooAbsArg.cxx:1237
void unRegisterProxy(RooArgProxy &proxy)
Remove proxy from proxy list.
Definition: RooAbsArg.cxx:1266
bool isValueServer(const RooAbsArg &arg) const
Check if this is serving values to arg.
Definition: RooAbsArg.h:216
RooAbsArg * findNewServer(const RooAbsCollection &newSet, bool nameChange) const
Find the new server in the specified set that matches the old server.
Definition: RooAbsArg.cxx:1144
RooAbsCollection is an abstract container object that can hold multiple RooAbsArg objects.
virtual bool addOwned(RooAbsArg &var, bool silent=false)
Add an argument and transfer the ownership to the collection.
virtual bool add(const RooAbsArg &var, bool silent=false)
Add the specified argument to list.
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:30
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.
bool defShapeServer() const
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.
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&).
~RooCollectionProxy() override
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'.
bool defValueServer() const
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...