File indexing completed on 2025-01-18 10:11:20
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
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
0043 RooCollectionProxy() {}
0044
0045
0046
0047
0048 RooCollectionProxy(const char *inName, const char * , 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
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
0067
0068 template <class Other_t>
0069 void initializeAfterIOConstructor(RooAbsArg *owner, const Other_t &other)
0070 {
0071
0072
0073 _owner = owner;
0074 _defValueServer = other.defValueServer();
0075 _defShapeServer = other.defShapeServer();
0076 RooCollection_t::setName(other.GetName());
0077
0078
0079
0080
0081
0082
0083 RooCollection_t::add(other);
0084
0085
0086 }
0087
0088
0089
0090
0091
0092
0093
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
0106 using RooAbsCollection::add;
0107 bool add(const RooAbsArg &var, bool valueServer, bool shapeServer, bool silent);
0108
0109
0110
0111
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
0120
0121
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
0137
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
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
0185
0186
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
0201
0202
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
0217
0218
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
0233
0234
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
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
0263
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
0281
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
0324
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