Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-12-02 10:19:04

0001 // -*- C++ -*-
0002 #ifndef RIVET_ProjectionApplier_HH
0003 #define RIVET_ProjectionApplier_HH
0004 
0005 #include "Rivet/Projection.fhh"
0006 #include "Rivet/ProjectionHandler.hh"
0007 #include "Rivet/Config/RivetCommon.hh"
0008 #include "Rivet/Tools/Logging.hh"
0009 #include <deque>
0010 
0011 namespace Rivet {
0012 
0013 
0014   // Forward declarations
0015   class Event;
0016 
0017 
0018   /// @brief Common base class for Projection and Analysis, used for internal polymorphism
0019   ///
0020   /// Empty interface used for storing Projection and Analysis pointers in the
0021   /// same container (used by the ProjectionHandler)
0022   class ProjectionApplier {
0023   public:
0024 
0025     // The proj handler needs access to reset the _allowProjReg flag before calling a.init()
0026     // friend class ProjectionHandler;
0027 
0028     /// Constructor
0029     ProjectionApplier();
0030 
0031     // Virtual destructor: ensure that inheritance is possible.
0032     virtual ~ProjectionApplier();
0033 
0034 
0035     /// @name Metadata functions
0036     /// @{
0037     /// Get the name of this Projection or Analysis class
0038     virtual std::string name() const = 0;
0039     /// @}
0040 
0041     /// @name Projection "getting" functions
0042     /// @{
0043 
0044     /// Get the contained projections, including recursion.
0045     std::set<ConstProjectionPtr> getProjections() const {
0046       return getProjHandler().getChildProjections(*this, ProjectionHandler::DEEP);
0047     }
0048 
0049     /// Get the contained projections, excluding recursion.
0050     std::set<ConstProjectionPtr> getImmediateChildProjections() const {
0051       return getProjHandler().getChildProjections(*this, ProjectionHandler::SHALLOW);
0052     }
0053 
0054     /// Does this applier have a projection registered under the name @a name?
0055     bool hasProjection(const std::string& name) const {
0056       if (_projhandler != nullptr) {
0057         // If we have a registered proj handler, check its registered names for this PA
0058         return getProjHandler().hasProjection(*this, name);
0059       }
0060       else {
0061         // If we don't have a registered proj handler yet, check names against the queue
0062         for (const auto& ptr_name_pair : _declQueue) {
0063           if (ptr_name_pair.second == name) return true;
0064         }
0065         return false;
0066       }
0067     }
0068 
0069     /// Get the named projection, specifying return type via a template argument.
0070     /// @todo Add SFINAE to require that PROJ inherit from Projection
0071     template <typename PROJ>
0072     const PROJ& getProjection(const std::string& name) const {
0073       if (_projhandler != nullptr) {
0074         const Projection& p = getProjHandler().getProjection(*this, name);
0075         return pcast<PROJ>(p);
0076       }
0077       else {
0078         return getProjectionFromDeclQueue<PROJ>(name);
0079       }
0080     }
0081     /// Get the named projection, specifying return type via a template argument (user-facing alias).
0082     /// @todo Add SFINAE to require that PROJ inherit from Projection
0083     template <typename PROJ>
0084     const PROJ& get(const std::string& name) const {
0085       return getProjection<PROJ>(name);
0086     }
0087 
0088     /// Get the named projection (non-templated, so returns as a reference to a
0089     /// Projection base class).
0090     const Projection& getProjection(const std::string& name) const {
0091       return getProjHandler().getProjection(*this, name);
0092     }
0093 
0094     ///Get a named projection from this projection appliers declqueue
0095     ///TODO for TP: Recursion?
0096     template <typename PROJ>
0097     const PROJ& getProjectionFromDeclQueue(const std::string name) const {
0098       auto it = std::find_if(_declQueue.begin(), _declQueue.end(),
0099                              [&name](const std::pair<std::shared_ptr<Projection>, std::string>& Qmember) {
0100                                return Qmember.second == name;
0101                              });
0102       if (it != _declQueue.end()) {
0103         return dynamic_cast<PROJ&>(*(it->first));
0104       }
0105       else {
0106         //If projection isn't found, deal with it properly.
0107         MSG_ERROR("Projection " << name << " not found in declQueue of " << this << " (" << this->name()
0108                                 << ")");
0109         throw RangeError("Projection lookup failed in getProjectionFromDeclQueue");
0110       }
0111     }
0112     ///@}
0113 
0114 
0115     /// @name Projection applying functions
0116     /// @{
0117 
0118   public:
0119 
0120     /// Apply the supplied projection on event @a evt.
0121 
0122     /// Apply the supplied projection on event @a evt (user-facing alias).
0123     template <typename PROJ = Projection>
0124     typename std::enable_if_t<std::is_base_of<Projection, PROJ>::value, const PROJ&> apply(
0125         const Event& evt,
0126         const Projection& proj) const {
0127       return pcast<PROJ>(_apply(evt, proj));
0128     }
0129 
0130     /// Apply the supplied projection on event @a evt (user-facing alias).
0131     template <typename PROJ = Projection>
0132     typename std::enable_if_t<std::is_base_of<Projection, PROJ>::value, const PROJ&> apply(
0133         const Event& evt,
0134         const PROJ& proj) const {
0135       return pcast<PROJ>(_apply(evt, proj));
0136     }
0137 
0138     /// Apply the supplied projection on event @a evt (user-facing alias).
0139     template <typename PROJ = Projection>
0140     typename std::enable_if_t<std::is_base_of<Projection, PROJ>::value, const PROJ&> apply(
0141         const Event& evt,
0142         const std::string& name) const {
0143       return pcast<PROJ>(_apply(evt, name));
0144     }
0145 
0146     /// Apply the supplied projection on event @a evt (convenience arg-reordering alias).
0147     template <typename PROJ = Projection>
0148     typename std::enable_if_t<std::is_base_of<Projection, PROJ>::value, const PROJ&> apply(
0149         const std::string& name,
0150         const Event& evt) const {
0151       return pcast<PROJ>(_apply(evt, name));
0152     }
0153 
0154     /// @}
0155 
0156 
0157     /// Mark this object as owned by a proj-handler
0158     void markAsOwned() const {
0159       _owned = true;
0160     }
0161 
0162 
0163   protected:
0164 
0165     Log& getLog() const {
0166       return Log::getLog("Rivet.ProjectionHandler");
0167     }
0168 
0169 
0170     /// Get a reference to the ProjectionHandler for this thread.
0171     ProjectionHandler& getProjHandler() const {
0172       return *_projhandler;
0173     }
0174 
0175 
0176   private:
0177 
0178     /// @name Projection registration functions
0179     /// @{
0180 
0181     /// @brief Register a contained projection
0182     ///
0183     /// The type of the argument is used to instantiate a new projection
0184     /// internally: this new object is applied to events rather than the
0185     /// argument object. Hence you are advised to only use locally-scoped
0186     /// Projection objects in your Projection and Analysis constructors, and to
0187     /// avoid polymorphism (e.g. handling @c ConcreteProjection via a pointer or
0188     /// reference to type @c Projection) since this will screw up the internal
0189     /// type management.
0190     ///
0191     /// @todo Add SFINAE to require that PROJ inherit from Projection
0192     template <typename PROJ>
0193     const PROJ& declareProjection(const PROJ& proj, const std::string& name) const {
0194       const Projection& reg = _declareProjection(proj, name);
0195       const PROJ& rtn = dynamic_cast<const PROJ&>(reg);
0196       rtn.setProjectionHandler(getProjHandler());
0197       return rtn;
0198     }
0199 
0200   protected:
0201 
0202     /// @brief Register a contained projection (user-facing version)
0203     /// @todo Add SFINAE to require that PROJ inherit from Projection
0204     template <typename PROJ>
0205     const PROJ& declare(const PROJ& proj, const std::string& name) const {
0206       std::shared_ptr<Projection> projClone = proj.clone();
0207       _declQueue.push_back(make_pair(projClone, name));
0208       return (dynamic_cast<PROJ&>(*projClone));
0209     }
0210     /// @brief Register a contained projection (user-facing, arg-reordered version)
0211     /// @todo Add SFINAE to require that PROJ inherit from Projection
0212     template <typename PROJ>
0213     const PROJ& declare(const std::string& name, const PROJ& proj) const {
0214       std::shared_ptr<Projection> projClone = proj.clone();
0215       _declQueue.push_back(make_pair(projClone, name));
0216       return (dynamic_cast<PROJ&>(*projClone));
0217     }
0218 
0219 
0220     /// Untemplated function to do the work...
0221     const Projection& _declareProjection(const Projection& proj, const std::string& name) const;
0222 
0223     /// @}
0224 
0225 
0226     /// Non-templated version of string-based apply, to work around
0227     /// header dependency issue.
0228     const Projection& _apply(const Event& evt, const std::string& name) const;
0229 
0230     /// Non-templated version of proj-based apply, to work around
0231     /// header dependency issue.
0232     const Projection& _apply(const Event& evt, const Projection& proj) const;
0233 
0234 
0235     /// @todo AB: Add Doxygen comment, follow surrounding coding style
0236     void setProjectionHandler(ProjectionHandler& projectionHandler) const;
0237 
0238     /// Flag to forbid projection registration in analyses until the init phase
0239     bool _allowProjReg;
0240 
0241 
0242   private:
0243 
0244     /// Mark object as owned by the _projhandler
0245     mutable bool _owned;
0246 
0247     /// @brief Pointer to projection handler
0248     ///
0249     /// @todo TP: Would we prefer a smart pointer?
0250     mutable ProjectionHandler* _projhandler;
0251 
0252     /// @brief Queue storing child projections to be properly declared later
0253     ///
0254     /// Declaration receives reference to a Projection and a name, so we
0255     /// need to store both Projection and name for each queued child.
0256     mutable std::deque<pair<std::shared_ptr<Projection>, string>> _declQueue;
0257 
0258   protected:
0259 
0260     /// @brief Synchronise the declaration queue once a ProjectionHandler is attached
0261     ///
0262     /// If this applier is owned, recursively flush the declQueues of the
0263     /// child projections, registering _them_ to our projection handler.
0264     void _syncDeclQueue() const;
0265   };
0266 }
0267 
0268 #endif