Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:11:20

0001 /*****************************************************************************
0002  * Project: RooFit                                                           *
0003  * Package: RooFitCore                                                       *
0004  *    File: $Id: RooSetProxy.h,v 1.21 2007/07/13 21:24:36 wouter Exp $
0005  * Authors:                                                                  *
0006  *   WV, Wouter Verkerke, UC Santa Barbara, verkerke@slac.stanford.edu       *
0007  *   DK, David Kirkby,    UC Irvine,         dkirkby@uci.edu                 *
0008  *                                                                           *
0009  * Copyright (c) 2000-2005, Regents of the University of California          *
0010  *                          and Stanford University. All rights reserved.    *
0011  *                                                                           *
0012  * Redistribution and use in source and binary forms,                        *
0013  * with or without modification, are permitted according to the terms        *
0014  * listed in LICENSE (http://roofit.sourceforge.net/license.txt)             *
0015  *****************************************************************************/
0016 
0017 /**
0018 \class RooCollectionProxy
0019 \ingroup Roofitcore
0020 Concrete proxy for RooArgSet or RooArgList objects.
0021 A RooCollectionProxy is the general mechanism to store a RooArgSet or RooArgList
0022 with RooAbsArgs in a RooAbsArg.
0023 Creating a RooCollectionProxy adds all members of the proxied RooArgSet to the proxy owners
0024 server list (thus receiving value/shape dirty flags from it) and
0025 registers itself with the owning class. The latter allows the
0026 owning class to update the pointers of RooArgSet or RooArgList contents to reflect
0027 the serverRedirect changes.
0028 **/
0029 
0030 #ifndef roofit_roofitcore_RooFit_RooCollectionProxy_h
0031 #define roofit_roofitcore_RooFit_RooCollectionProxy_h
0032 
0033 #include <RooAbsProxy.h>
0034 #include <RooAbsArg.h>
0035 #include <RooArgSet.h>
0036 
0037 #include <exception>
0038 
0039 template <class RooCollection_t>
0040 class RooCollectionProxy final : public RooCollection_t, public RooAbsProxy {
0041 public:
0042    // Constructors, assignment etc.
0043    RooCollectionProxy() {}
0044 
0045    /// Construct proxy with given name and description, with given owner
0046    /// The default value and shape dirty propagation of the set contents
0047    /// to the set owner is controlled by flags defValueServer and defShapeServer.
0048    RooCollectionProxy(const char *inName, const char * /*desc*/, RooAbsArg *owner, bool defValueServer = true,
0049                       bool defShapeServer = false)
0050       : RooCollection_t(inName), _owner(owner), _defValueServer(defValueServer), _defShapeServer(defShapeServer)
0051    {
0052       _owner->registerProxy(*this);
0053    }
0054 
0055    /// Copy constructor.
0056    template <class Other_t>
0057    RooCollectionProxy(const char *inName, RooAbsArg *owner, const Other_t &other)
0058       : RooCollection_t(other, inName),
0059         _owner(owner),
0060         _defValueServer(other.defValueServer()),
0061         _defShapeServer(other.defShapeServer())
0062    {
0063       _owner->registerProxy(*this);
0064    }
0065 
0066    /// Initializes this RooCollection proxy from another proxy. Should not be
0067    /// considered part of the public interface, only to be used by IO.
0068    template <class Other_t>
0069    void initializeAfterIOConstructor(RooAbsArg *owner, const Other_t &other)
0070    {
0071 
0072       // Copy all attributes from "other"
0073       _owner = owner;
0074       _defValueServer = other.defValueServer();
0075       _defShapeServer = other.defShapeServer();
0076       RooCollection_t::setName(other.GetName());
0077 
0078       // Add the elements. This should not be done with
0079       // RooCollectionProxy::add(), but with the base class method. Otherwise,
0080       // _owner->addServer() is called when adding each element, which we don't
0081       // want for IO because the list of servers are already filled when
0082       // reading the server list of the object in the file.
0083       RooCollection_t::add(other);
0084 
0085       // Don't do _owner->registerProxy(*this) here! The proxy list will also be copied separately.
0086    }
0087 
0088    // Assignment is deleted because it is not clear how it should behave.
0089    // Should default assignment be used? But then, it will use the assignment
0090    // operators of the RooFit collections, which actually don't do assignment,
0091    // but value synchronization! Should it be re-implemented to be actual
0092    // assignment? That would be inconsistent with the base class! So it's
0093    // better to not support it at all.
0094    RooCollectionProxy &operator=(RooCollectionProxy const &other) = delete;
0095    RooCollectionProxy &operator=(RooCollectionProxy &&other) = delete;
0096 
0097    ~RooCollectionProxy() override
0098    {
0099       if (_owner)
0100          _owner->unRegisterProxy(*this);
0101    }
0102 
0103    const char *name() const override { return RooCollection_t::GetName(); }
0104 
0105    // List content management (modified for server hooks)
0106    using RooAbsCollection::add;
0107    bool add(const RooAbsArg &var, bool valueServer, bool shapeServer, bool silent);
0108 
0109    /// Overloaded RooCollection_t::add() method inserts 'var' into set
0110    /// and registers 'var' as server to owner with default value
0111    /// and shape dirty flag propagation.
0112    bool add(const RooAbsArg &var, bool silent = false) override
0113    {
0114       return add(var, _defValueServer, _defShapeServer, silent);
0115    }
0116 
0117    using RooAbsCollection::addOwned;
0118 
0119 // The following function is not memory safe, because it takes ownership of var
0120 // without moving it. It is not publicly available in the memory safe
0121 // interfaces mode.
0122 #ifdef ROOFIT_MEMORY_SAFE_INTERFACES
0123 protected:
0124 #endif
0125    bool addOwned(RooAbsArg &var, bool silent = false) override;
0126 #ifdef ROOFIT_MEMORY_SAFE_INTERFACES
0127 public:
0128 #endif
0129 
0130    using RooAbsCollection::addClone;
0131    RooAbsArg *addClone(const RooAbsArg &var, bool silent = false) override;
0132 
0133    bool replace(const RooAbsArg &var1, const RooAbsArg &var2) override;
0134    bool remove(const RooAbsArg &var, bool silent = false, bool matchByNameOnly = false) override;
0135 
0136    /// Remove each argument in the input list from our list using remove(const RooAbsArg&).
0137    /// and remove each argument as server to owner
0138    bool remove(const RooAbsCollection &list, bool silent = false, bool matchByNameOnly = false)
0139    {
0140       bool result(false);
0141       for (auto const &arg : list) {
0142          result |= remove(*arg, silent, matchByNameOnly);
0143       }
0144       return result;
0145    }
0146 
0147    void removeAll() override;
0148 
0149    void print(std::ostream &os, bool addContents = false) const override;
0150 
0151    /// Assign values of arguments on other set to arguments in this set.
0152    RooCollectionProxy &operator=(const RooCollection_t &other)
0153    {
0154       RooCollection_t::operator=(other);
0155       return *this;
0156    }
0157 
0158    bool defValueServer() const { return _defValueServer; }
0159    bool defShapeServer() const { return _defShapeServer; }
0160 
0161 private:
0162    RooAbsArg *_owner = nullptr;
0163    bool _defValueServer = false;
0164    bool _defShapeServer = false;
0165 
0166    bool
0167    changePointer(const RooAbsCollection &newServerSet, bool nameChange = false, bool factoryInitMode = false) override;
0168 
0169    bool changePointer(std::unordered_map<RooAbsArg *, RooAbsArg *> const &replacements) override;
0170 
0171    void checkValid() const
0172    {
0173       if (!_owner) {
0174          throw std::runtime_error(
0175             "Attempt to add elements to a RooSetProxy or RooListProxy without owner!"
0176             " Please avoid using the RooListProxy default constructor, which should only be used by IO.");
0177       }
0178    }
0179 
0180    ClassDefOverride(RooCollectionProxy, 1)
0181 };
0182 
0183 ////////////////////////////////////////////////////////////////////////////////
0184 /// Overloaded RooCollection_t::add() method insert object into set
0185 /// and registers object as server to owner with given value
0186 /// and shape dirty flag propagation requests
0187 
0188 template <class RooCollection_t>
0189 bool RooCollectionProxy<RooCollection_t>::add(const RooAbsArg &var, bool valueServer, bool shapeServer, bool silent)
0190 {
0191    checkValid();
0192    bool ret = RooCollection_t::add(var, silent);
0193    if (ret) {
0194       _owner->addServer((RooAbsArg &)var, valueServer, shapeServer);
0195    }
0196    return ret;
0197 }
0198 
0199 ////////////////////////////////////////////////////////////////////////////////
0200 /// Overloaded RooCollection_t::addOwned() method insert object into owning set
0201 /// and registers object as server to owner with default value
0202 /// and shape dirty flag propagation
0203 
0204 template <class RooCollection_t>
0205 bool RooCollectionProxy<RooCollection_t>::addOwned(RooAbsArg &var, bool silent)
0206 {
0207    checkValid();
0208    bool ret = RooCollection_t::addOwned(var, silent);
0209    if (ret) {
0210       _owner->addServer((RooAbsArg &)var, _defValueServer, _defShapeServer);
0211    }
0212    return ret;
0213 }
0214 
0215 ////////////////////////////////////////////////////////////////////////////////
0216 /// Overloaded RooCollection_t::addClone() method insert clone of object into owning set
0217 /// and registers cloned object as server to owner with default value
0218 /// and shape dirty flag propagation
0219 
0220 template <class RooCollection_t>
0221 RooAbsArg *RooCollectionProxy<RooCollection_t>::addClone(const RooAbsArg &var, bool silent)
0222 {
0223    checkValid();
0224    RooAbsArg *ret = RooCollection_t::addClone(var, silent);
0225    if (ret) {
0226       _owner->addServer((RooAbsArg &)var, _defValueServer, _defShapeServer);
0227    }
0228    return ret;
0229 }
0230 
0231 ////////////////////////////////////////////////////////////////////////////////
0232 /// Replace object 'var1' in set with 'var2'. Deregister var1 as
0233 /// server from owner and register var2 as server to owner with
0234 /// default value and shape dirty propagation flags
0235 
0236 template <class RooCollection_t>
0237 bool RooCollectionProxy<RooCollection_t>::replace(const RooAbsArg &var1, const RooAbsArg &var2)
0238 {
0239    bool ret = RooCollection_t::replace(var1, var2);
0240    if (ret) {
0241       if (!RooCollection_t::isOwning())
0242          _owner->removeServer((RooAbsArg &)var1);
0243       _owner->addServer((RooAbsArg &)var2, _owner->isValueServer(var1), _owner->isShapeServer(var2));
0244    }
0245    return ret;
0246 }
0247 
0248 ////////////////////////////////////////////////////////////////////////////////
0249 /// Remove object 'var' from set and deregister 'var' as server to owner.
0250 
0251 template <class RooCollection_t>
0252 bool RooCollectionProxy<RooCollection_t>::remove(const RooAbsArg &var, bool silent, bool matchByNameOnly)
0253 {
0254    bool ret = RooCollection_t::remove(var, silent, matchByNameOnly);
0255    if (ret && !RooCollection_t::isOwning()) {
0256       _owner->removeServer((RooAbsArg &)var);
0257    }
0258    return ret;
0259 }
0260 
0261 ////////////////////////////////////////////////////////////////////////////////
0262 /// Remove all argument inset using remove(const RooAbsArg&).
0263 /// and remove each argument as server to owner
0264 
0265 template <class RooCollection_t>
0266 void RooCollectionProxy<RooCollection_t>::removeAll()
0267 {
0268    if (!RooCollection_t::isOwning()) {
0269       for (auto const &arg : *this) {
0270          if (!RooCollection_t::isOwning()) {
0271             _owner->removeServer(*arg);
0272          }
0273       }
0274    }
0275 
0276    RooCollection_t::removeAll();
0277 }
0278 
0279 ////////////////////////////////////////////////////////////////////////////////
0280 /// Process server change operation on owner. Replace elements in set with equally
0281 /// named objects in 'newServerList'
0282 
0283 template <class RooCollection_t>
0284 bool RooCollectionProxy<RooCollection_t>::changePointer(const RooAbsCollection &newServerList, bool nameChange,
0285                                                         bool factoryInitMode)
0286 {
0287    if (RooCollection_t::empty()) {
0288       if (factoryInitMode) {
0289          for (const auto arg : newServerList) {
0290             if (arg != _owner) {
0291                add(*arg, true);
0292             }
0293          }
0294       } else {
0295          return true;
0296       }
0297    }
0298 
0299    bool error(false);
0300    for (auto const &arg : *this) {
0301       RooAbsArg *newArg = arg->findNewServer(newServerList, nameChange);
0302       if (newArg && newArg != _owner)
0303          error |= !RooCollection_t::replace(*arg, *newArg);
0304    }
0305    return !error;
0306 }
0307 
0308 template <class RooCollection_t>
0309 bool RooCollectionProxy<RooCollection_t>::changePointer(
0310    std::unordered_map<RooAbsArg *, RooAbsArg *> const &replacements)
0311 {
0312    bool error(false);
0313    for (auto const &arg : *this) {
0314       auto newArgFound = replacements.find(arg);
0315       if (newArgFound != replacements.end()) {
0316          error |= !RooCollection_t::replace(*arg, *newArgFound->second);
0317       }
0318    }
0319    return !error;
0320 }
0321 
0322 ////////////////////////////////////////////////////////////////////////////////
0323 /// Printing name of proxy on ostream. If addContents is true
0324 /// also print names of objects in set
0325 
0326 template <class RooCollection_t>
0327 void RooCollectionProxy<RooCollection_t>::print(std::ostream &os, bool addContents) const
0328 {
0329    if (!addContents) {
0330       os << name() << "=";
0331       RooCollection_t::printStream(os, RooPrintable::kValue, RooPrintable::kInline);
0332    } else {
0333       os << name() << "=(";
0334       bool first2(true);
0335       for (auto const &arg : *this) {
0336          if (first2) {
0337             first2 = false;
0338          } else {
0339             os << ",";
0340          }
0341          arg->printStream(os, RooPrintable::kValue | RooPrintable::kName, RooPrintable::kInline);
0342       }
0343       os << ")";
0344    }
0345 }
0346 
0347 using RooSetProxy = RooCollectionProxy<RooArgSet>;
0348 
0349 #endif