Logo ROOT  
Reference Guide
 
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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
20Concrete 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 <RooAbsArg.h>
34#include <RooAbsProxy.h>
35#include <RooArgSet.h>
36#include <RooMsgService.h>
37
38#include <ROOT/RConfig.hxx> // for R__SUGGEST_ALTERNATIVE
39
40#include <exception>
41
42template <class RooCollection_t>
44public:
45 // Constructors, assignment etc.
47
48 /// Construct proxy with given name and description, with given owner
49 /// The default value and shape dirty propagation of the set contents
50 /// to the set owner is controlled by flags defValueServer and defShapeServer.
51 RooCollectionProxy(const char *inName, const char * /*desc*/, RooAbsArg *owner, bool defValueServer = true,
52 bool defShapeServer = false)
54 {
55 _owner->registerProxy(*this);
56 }
57
58 /// Copy constructor.
59 /// \note Copying a collection proxy and giving it a name different from the
60 /// original proxy doesn't make sense in the context of how this class is
61 /// used. The copy constructor that doesn't take a name as the first
62 /// parameter should be preferred.
63 template <class Other_t>
64 RooCollectionProxy(const char *inName, RooAbsArg *owner, const Other_t &other)
65 R__SUGGEST_ALTERNATIVE("Copying a collection proxy and giving it a name different from the original proxy "
66 "doesn't make sense in the context of how this class is used. The copy constructor that "
67 "doesn't take a name as the first parameter should be preferred.")
69 _owner(owner),
72 {
73 _owner->registerProxy(*this);
74 }
75
76 /// Copy constructor.
77 template <class Other_t>
86
87 /// Initializes this RooCollection proxy from another proxy. Should not be
88 /// considered part of the public interface, only to be used by IO.
89 template <class Other_t>
91 {
92
93 // Copy all attributes from "other"
94 _owner = owner;
95 _defValueServer = other.defValueServer();
96 _defShapeServer = other.defShapeServer();
97 RooCollection_t::setName(other.GetName());
98
99 // Add the elements. This should not be done with
100 // RooCollectionProxy::add(), but with the base class method. Otherwise,
101 // _owner->addServer() is called when adding each element, which we don't
102 // want for IO because the list of servers are already filled when
103 // reading the server list of the object in the file.
104 RooCollection_t::add(other);
105
106 // Don't do _owner->registerProxy(*this) here! The proxy list will also be copied separately.
107 }
108
109 // Assignment is deleted because it is not clear how it should behave.
110 // Should default assignment be used? But then, it will use the assignment
111 // operators of the RooFit collections, which actually don't do assignment,
112 // but value synchronization! Should it be re-implemented to be actual
113 // assignment? That would be inconsistent with the base class! So it's
114 // better to not support it at all.
117
119 {
120 if (_owner)
121 _owner->unRegisterProxy(*this);
122 }
123
124 const char *name() const override { return RooCollection_t::GetName(); }
125
126 // List content management (modified for server hooks)
128 bool add(const RooAbsArg &var, bool valueServer, bool shapeServer, bool silent);
129
130 /// Overloaded RooCollection_t::add() method inserts 'var' into set
131 /// and registers 'var' as server to owner with default value
132 /// and shape dirty flag propagation.
133 bool add(const RooAbsArg &var, bool silent = false) override
134 {
136 }
137
139
140// The following function is not memory safe, because it takes ownership of var
141// without moving it. It is not publicly available in the memory safe
142// interfaces mode.
143#ifdef ROOFIT_MEMORY_SAFE_INTERFACES
144protected:
145#endif
146 bool addOwned(RooAbsArg &var, bool silent = false) override;
147#ifdef ROOFIT_MEMORY_SAFE_INTERFACES
148public:
149#endif
150
152 RooAbsArg *addClone(const RooAbsArg &var, bool silent = false) override;
153
154 bool replace(const RooAbsArg &var1, const RooAbsArg &var2) override;
155 bool remove(const RooAbsArg &var, bool silent = false, bool matchByNameOnly = false) override;
156
157 /// Remove each argument in the input list from our list using remove(const RooAbsArg&).
158 /// and remove each argument as server to owner
159 bool remove(const RooAbsCollection &list, bool silent = false, bool matchByNameOnly = false)
160 {
161 bool result(false);
162 for (auto const &arg : list) {
164 }
165 return result;
166 }
167
168 void removeAll() override;
169
170 void print(std::ostream &os, bool addContents = false) const override;
171
172 /// Assign values of arguments on other set to arguments in this set.
174 {
175 RooCollection_t::operator=(other);
176 return *this;
177 }
178
179 bool defValueServer() const { return _defValueServer; }
180 bool defShapeServer() const { return _defShapeServer; }
181
182private:
183 RooAbsArg *_owner = nullptr;
184 bool _defValueServer = false;
185 bool _defShapeServer = false;
186
187 bool
188 changePointer(const RooAbsCollection &newServerSet, bool nameChange = false, bool factoryInitMode = false) override;
189
190 bool changePointer(std::unordered_map<RooAbsArg *, RooAbsArg *> const &replacements) override;
191
192 void checkValid() const
193 {
194 if (!_owner) {
195 throw std::runtime_error(
196 "Attempt to add elements to a RooSetProxy or RooListProxy without owner!"
197 " Please avoid using the RooListProxy default constructor, which should only be used by IO.");
198 }
199 }
200
202};
203
204////////////////////////////////////////////////////////////////////////////////
205/// Overloaded RooCollection_t::add() method insert object into set
206/// and registers object as server to owner with given value
207/// and shape dirty flag propagation requests
208
209template <class RooCollection_t>
211{
212 checkValid();
213 bool ret = RooCollection_t::add(var, silent);
214 if (ret) {
215 _owner->addServer((RooAbsArg &)var, valueServer, shapeServer);
216 }
217 return ret;
218}
219
220////////////////////////////////////////////////////////////////////////////////
221/// Overloaded RooCollection_t::addOwned() method insert object into owning set
222/// and registers object as server to owner with default value
223/// and shape dirty flag propagation
224
225template <class RooCollection_t>
227{
228 checkValid();
229 bool ret = RooCollection_t::addOwned(var, silent);
230 if (ret) {
231 _owner->addServer((RooAbsArg &)var, _defValueServer, _defShapeServer);
232 }
233 return ret;
234}
235
236////////////////////////////////////////////////////////////////////////////////
237/// Overloaded RooCollection_t::addClone() method insert clone of object into owning set
238/// and registers cloned object as server to owner with default value
239/// and shape dirty flag propagation
240
241template <class RooCollection_t>
243{
244 checkValid();
245 RooAbsArg *ret = RooCollection_t::addClone(var, silent);
246 if (ret) {
247 _owner->addServer((RooAbsArg &)var, _defValueServer, _defShapeServer);
248 }
249 return ret;
250}
251
252////////////////////////////////////////////////////////////////////////////////
253/// Replace object 'var1' in set with 'var2'. Deregister var1 as
254/// server from owner and register var2 as server to owner with
255/// default value and shape dirty propagation flags
256
257template <class RooCollection_t>
259{
260 bool ret = RooCollection_t::replace(var1, var2);
261 if (ret) {
262 if (!RooCollection_t::isOwning())
263 _owner->removeServer((RooAbsArg &)var1);
264 _owner->addServer((RooAbsArg &)var2, _owner->isValueServer(var1), _owner->isShapeServer(var2));
265 }
266 return ret;
267}
268
269////////////////////////////////////////////////////////////////////////////////
270/// Remove object 'var' from set and deregister 'var' as server to owner.
271
272template <class RooCollection_t>
274{
275 bool ret = RooCollection_t::remove(var, silent, matchByNameOnly);
276 if (ret && !RooCollection_t::isOwning()) {
277 _owner->removeServer((RooAbsArg &)var);
278 }
279 return ret;
280}
281
282////////////////////////////////////////////////////////////////////////////////
283/// Remove all argument inset using remove(const RooAbsArg&).
284/// and remove each argument as server to owner
285
286template <class RooCollection_t>
288{
289 if (!RooCollection_t::isOwning()) {
290 for (auto const &arg : *this) {
291 if (!RooCollection_t::isOwning()) {
292 _owner->removeServer(*arg);
293 }
294 }
295 }
296
297 RooCollection_t::removeAll();
298}
299
300////////////////////////////////////////////////////////////////////////////////
301/// Process server change operation on owner. Replace elements in set with equally
302/// named objects in 'newServerList'
303
304template <class RooCollection_t>
306 bool factoryInitMode)
307{
308 if (RooCollection_t::empty()) {
309 if (factoryInitMode) {
310 for (const auto arg : newServerList) {
311 if (arg != _owner) {
312 add(*arg, true);
313 }
314 }
315 } else {
316 return true;
317 }
318 }
319
320 bool error(false);
321 for (auto const &arg : *this) {
322 RooAbsArg *newArg = arg->findNewServer(newServerList, nameChange);
323 if (newArg && newArg != _owner)
324 error |= !RooCollection_t::replace(*arg, *newArg);
325 }
326 return !error;
327}
328
329template <class RooCollection_t>
331 std::unordered_map<RooAbsArg *, RooAbsArg *> const &replacements)
332{
333 bool error(false);
334 for (auto const &arg : *this) {
335 auto newArgFound = replacements.find(arg);
336 if (newArgFound != replacements.end()) {
337 error |= !RooCollection_t::replace(*arg, *newArgFound->second);
338 }
339 }
340 return !error;
341}
342
343////////////////////////////////////////////////////////////////////////////////
344/// Printing name of proxy on ostream. If addContents is true
345/// also print names of objects in set
346
347template <class RooCollection_t>
349{
350 if (!addContents) {
351 os << name() << "=";
352 RooCollection_t::printStream(os, RooPrintable::kValue, RooPrintable::kInline);
353 } else {
354 os << name() << "=(";
355 bool first2(true);
356 for (auto const &arg : *this) {
357 if (first2) {
358 first2 = false;
359 } else {
360 os << ",";
361 }
363 }
364 os << ")";
365 }
366}
367
369
370#endif
#define R__SUGGEST_ALTERNATIVE(ALTERNATIVE)
Definition RConfig.hxx:528
#define ClassDefOverride(name, id)
Definition Rtypes.h:346
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 result
char name[80]
Definition TGX11.cxx:110
const_iterator end() const
Common abstract base class for objects that represent a value and a "shape" in RooFit.
Definition RooAbsArg.h:77
void registerProxy(RooArgProxy &proxy)
Register an RooArgProxy in the proxy list.
void unRegisterProxy(RooArgProxy &proxy)
Remove proxy from proxy list.
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.
Abstract interface for proxy classes.
Definition RooAbsProxy.h:37
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.
RooCollectionProxy(RooAbsArg *owner, const Other_t &other)
Copy constructor.
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...