File indexing completed on 2026-12-02 10:19:04
0001
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
0015 class Event;
0016
0017
0018
0019
0020
0021
0022 class ProjectionApplier {
0023 public:
0024
0025
0026
0027
0028
0029 ProjectionApplier();
0030
0031
0032 virtual ~ProjectionApplier();
0033
0034
0035
0036
0037
0038 virtual std::string name() const = 0;
0039
0040
0041
0042
0043
0044
0045 std::set<ConstProjectionPtr> getProjections() const {
0046 return getProjHandler().getChildProjections(*this, ProjectionHandler::DEEP);
0047 }
0048
0049
0050 std::set<ConstProjectionPtr> getImmediateChildProjections() const {
0051 return getProjHandler().getChildProjections(*this, ProjectionHandler::SHALLOW);
0052 }
0053
0054
0055 bool hasProjection(const std::string& name) const {
0056 if (_projhandler != nullptr) {
0057
0058 return getProjHandler().hasProjection(*this, name);
0059 }
0060 else {
0061
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
0070
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
0082
0083 template <typename PROJ>
0084 const PROJ& get(const std::string& name) const {
0085 return getProjection<PROJ>(name);
0086 }
0087
0088
0089
0090 const Projection& getProjection(const std::string& name) const {
0091 return getProjHandler().getProjection(*this, name);
0092 }
0093
0094
0095
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
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
0116
0117
0118 public:
0119
0120
0121
0122
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
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
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
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
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
0171 ProjectionHandler& getProjHandler() const {
0172 return *_projhandler;
0173 }
0174
0175
0176 private:
0177
0178
0179
0180
0181
0182
0183
0184
0185
0186
0187
0188
0189
0190
0191
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
0203
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
0211
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
0221 const Projection& _declareProjection(const Projection& proj, const std::string& name) const;
0222
0223
0224
0225
0226
0227
0228 const Projection& _apply(const Event& evt, const std::string& name) const;
0229
0230
0231
0232 const Projection& _apply(const Event& evt, const Projection& proj) const;
0233
0234
0235
0236 void setProjectionHandler(ProjectionHandler& projectionHandler) const;
0237
0238
0239 bool _allowProjReg;
0240
0241
0242 private:
0243
0244
0245 mutable bool _owned;
0246
0247
0248
0249
0250 mutable ProjectionHandler* _projhandler;
0251
0252
0253
0254
0255
0256 mutable std::deque<pair<std::shared_ptr<Projection>, string>> _declQueue;
0257
0258 protected:
0259
0260
0261
0262
0263
0264 void _syncDeclQueue() const;
0265 };
0266 }
0267
0268 #endif