Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-16 10:30:35

0001 /*****************************************************************************
0002  * Project: RooFit                                                           *
0003  * Package: RooFitCore                                                       *
0004  *    File: $Id: RooArgSet.h,v 1.45 2007/08/09 19:55:47 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 #ifndef ROO_ARG_SET
0017 #define ROO_ARG_SET
0018 
0019 #include "RooAbsCollection.h"
0020 
0021 class RooAbsArg ;
0022 class RooArgList ;
0023 
0024 class RooArgSet : public RooAbsCollection {
0025 public:
0026 
0027   // Constructors, assignment etc.
0028   RooArgSet();
0029 
0030   /// Construct a (non-owning) RooArgSet from one or more
0031   /// RooFit objects. The set will not own its contents.
0032   /// \tparam Ts Parameter pack of objects that derive from RooAbsArg or RooFit collections; or a name.
0033   /// \param arg A RooFit object.
0034   ///            Note that you can also pass a `double` as first argument
0035   ///            when constructing a RooArgSet, and another templated
0036   ///            constructor will be used where a RooConstVar is implicitly
0037   ///            created from the `double` value.
0038   /// \param moreArgsOrName Arbitrary number of
0039   /// - Further RooFit objects that derive from RooAbsArg
0040   /// - RooFit collections of such objects
0041   /// - `double`s from which a RooConstVar is implicitly created via `RooFit::RooConst`.
0042   /// - A name for the set. Given multiple names, the last-given name prevails.
0043   template<typename... Args_t>
0044   RooArgSet(const RooAbsArg& arg, Args_t &&... moreArgsOrName)
0045   /*NB: Making this a delegating constructor led to linker errors with MSVC*/
0046   {
0047     // This constructor should cause a failed static_assert if any of the input
0048     // arguments is a temporary (r-value reference), which will be checked in
0049     // processArg. This works statically because of the universal reference
0050     // mechanism with templated functions.
0051     // Unfortunately, we can't check the first arg, because it's type can't be
0052     // a template parameter and hence a universal reference can't be used.
0053     // This problem is solved by introducing another templated constructor below,
0054     // which accepts a RooAbsArg && as the first argument which is forwarded to
0055     // be the second argument for this constructor.
0056     processArgs(arg, std::forward<Args_t>(moreArgsOrName)...);
0057   }
0058 
0059   /// This constructor will provoke a `static_assert`, because passing a
0060   /// RooAbsArg as r-value reference is not allowed.
0061   template<typename... Args_t>
0062   RooArgSet(RooAbsArg && arg, Args_t &&... moreArgsOrName)
0063     : RooArgSet{arg, std::move(arg), std::forward<Args_t>(moreArgsOrName)...} {}
0064 
0065   template<typename... Args_t>
0066   explicit RooArgSet(double arg, Args_t &&... moreArgsOrName) {
0067     processArgs(arg, std::forward<Args_t>(moreArgsOrName)...);
0068   }
0069 
0070   /// Construct a (non-owning) RooArgSet from iterators.
0071   /// \tparam Iterator_t An iterator pointing to RooFit objects or to pointers/references of those.
0072   /// \param beginIt Iterator to first element to add.
0073   /// \param endIt Iterator to end of range to be added.
0074   /// \param name Optional name of the collection.
0075   template<typename Iterator_t,
0076       typename value_type = typename std::remove_pointer<typename std::iterator_traits<Iterator_t>::value_type>::type,
0077       typename = std::enable_if<std::is_convertible<const value_type*, const RooAbsArg*>::value> >
0078   RooArgSet(Iterator_t beginIt, Iterator_t endIt, const char* name="") :
0079   RooArgSet(name) {
0080     for (auto it = beginIt; it != endIt; ++it) {
0081       processArg(*it);
0082     }
0083   }
0084 
0085   /// Construct a non-owning RooArgSet from a vector of RooAbsArg pointers.
0086   /// This constructor is mainly intended for pyROOT. With cppyy, a Python list
0087   /// or tuple can be implicitly converted to an std::vector, and by enabling
0088   /// implicit construction of a RooArgSet from a std::vector, we indirectly
0089   /// enable implicit conversion from a Python list/tuple to RooArgSets.
0090   /// \param vec A vector with pointers to the arguments or doubles for RooFit::RooConst().
0091   RooArgSet(std::vector<RooAbsArgPtrOrDouble> const& vec) {
0092     for(auto const& arg : vec) {
0093       if(arg.hasPtr) processArg(arg.ptr);
0094       else processArg(arg.val);
0095     }
0096   }
0097 
0098   RooArgSet(const RooArgSet& other, const char *name="");
0099   /// Move constructor.
0100   RooArgSet(RooArgSet && other) : RooAbsCollection(std::move(other)) {}
0101 
0102   RooArgSet(const RooArgSet& set1, const RooArgSet& set2,
0103             const char *name="");
0104 
0105   RooArgSet(const RooAbsCollection& coll) ;
0106   RooArgSet(const RooAbsCollection& collection, const RooAbsArg* var1);
0107   explicit RooArgSet(const TCollection& tcoll, const char* name="") ;
0108   explicit RooArgSet(const char *name);
0109 
0110   ~RooArgSet() override;
0111   TObject* clone(const char* newname=nullptr) const override { return new RooArgSet(*this,newname); }
0112   TObject* create(const char* newname) const override { return new RooArgSet(newname); }
0113   RooArgSet& operator=(const RooArgSet& other) { RooAbsCollection::operator=(other) ; return *this ;}
0114 
0115   using RooAbsCollection::operator[];
0116   RooAbsArg& operator[](const TString& str) const;
0117 
0118 
0119   /// Shortcut for readFromStream(std::istream&, bool, const char*, const char*, bool), setting
0120   /// `flagReadAtt` and `section` to 0.
0121   virtual bool readFromStream(std::istream& is, bool compact, bool verbose=false) {
0122     // I/O streaming interface (machine readable)
0123     return readFromStream(is, compact, nullptr, nullptr, verbose) ;
0124   }
0125   bool readFromStream(std::istream& is, bool compact, const char* flagReadAtt, const char* section, bool verbose=false) ;
0126   virtual void writeToStream(std::ostream& os, bool compact, const char* section=nullptr) const;
0127   void writeToFile(const char* fileName) const ;
0128   bool readFromFile(const char* fileName, const char* flagReadAtt=nullptr, const char* section=nullptr, bool verbose=false) ;
0129 
0130 
0131   /// Check if this exact instance is in this collection.
0132   bool containsInstance(const RooAbsArg& var) const override {
0133     return find(var) == &var;
0134   }
0135 
0136   static void cleanup() ;
0137 
0138   bool isInRange(const char* rangeSpec) ;
0139 
0140   using RooAbsCollection::selectCommon;
0141   using RooAbsCollection::snapshot;
0142 
0143   /// Use RooAbsCollection::selectByAttrib(), but return as RooArgSet.
0144   RooArgSet* selectByAttrib(const char* name, bool value) const {
0145     return static_cast<RooArgSet*>(RooAbsCollection::selectByAttrib(name, value));
0146   }
0147 
0148   /// Use RooAbsCollection::selectByName(), but return as RooArgSet.
0149   inline RooArgSet* selectByName(const char* nameList, bool verbose=false) const {
0150     return static_cast<RooArgSet*>(RooAbsCollection::selectByName(nameList, verbose));
0151   }
0152 
0153   /// Use RooAbsCollection::selecCommon(), but return as RooArgSet.
0154   inline RooArgSet* selectCommon(const RooAbsCollection& refColl) const {
0155     return static_cast<RooArgSet*>(RooAbsCollection::selectCommon(refColl));
0156   }
0157 
0158   /// Use RooAbsCollection::snapshot(), but return as RooArgSet.
0159   inline RooArgSet * snapshot(bool deepCopy = true) const {
0160     return static_cast<RooArgSet*>(RooAbsCollection::snapshot(deepCopy));
0161   }
0162 
0163 protected:
0164   bool checkForDup(const RooAbsArg& arg, bool silent) const ;
0165   bool canBeAdded(const RooAbsArg& arg, bool silent) const override {
0166     return !checkForDup(arg, silent);
0167   }
0168 
0169 private:
0170 
0171   template<typename... Args_t>
0172   void processArgs(Args_t &&... args) {
0173     // Expand parameter pack in C++ 11 way:
0174     int dummy[] = { 0, (processArg(std::forward<Args_t>(args)), 0) ... };
0175     (void)dummy;
0176   }
0177   void processArg(const RooAbsArg& arg) { add(arg); }
0178   void processArg(const RooAbsArg* arg) { add(*arg); }
0179   void processArg(RooAbsArg* var) { add(*var); }
0180   template<class Arg_t>
0181   void processArg(Arg_t && arg) {
0182     assert_is_no_temporary(std::forward<Arg_t>(arg));
0183     add(arg);
0184   }
0185   void processArg(const char* name) { _name = name; }
0186   void processArg(double value);
0187   void processArg(const RooAbsCollection& coll) { add(coll); if (_name.Length() == 0) _name = coll.GetName(); }
0188   // this overload with r-value references is needed so we don't trigger the
0189   // templated function with the failing static_assert for r-value references
0190   void processArg(RooAbsCollection && coll) { processArg(coll); }
0191   void processArg(const RooArgList& list);
0192 
0193   ClassDefOverride(RooArgSet,1) // Set of RooAbsArg objects
0194 };
0195 
0196 
0197 namespace RooFitShortHand {
0198 
0199 template<class... Args_t>
0200 RooArgSet S(Args_t&&... args) {
0201   return {std::forward<Args_t>(args)...};
0202 }
0203 
0204 } // namespace RooFitShortHand
0205 
0206 
0207 #endif