File indexing completed on 2025-04-19 09:06:57
0001
0002 #ifndef RIVET_ProjectionApplier_HH
0003 #define RIVET_ProjectionApplier_HH
0004
0005 #include <deque>
0006 #include "Rivet/Config/RivetCommon.hh"
0007 #include "Rivet/Projection.fhh"
0008 #include "Rivet/ProjectionHandler.hh"
0009 #include "Rivet/Tools/Logging.hh"
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 return getProjHandler().hasProjection(*this, name);
0057 }
0058
0059
0060
0061 template <typename PROJ>
0062 const PROJ& getProjection(const std::string& name) const {
0063 if (_projhandler != nullptr){
0064 const Projection& p = getProjHandler().getProjection(*this, name);
0065 return pcast<PROJ>(p);
0066 }
0067 else {
0068 return getProjectionFromDeclQueue<PROJ>(name);
0069 }
0070 }
0071
0072
0073 template <typename PROJ>
0074 const PROJ& get(const std::string& name) const { return getProjection<PROJ>(name); }
0075
0076
0077
0078 const Projection& getProjection(const std::string& name) const {
0079 return getProjHandler().getProjection(*this, name);
0080 }
0081
0082
0083
0084 template <typename PROJ>
0085 const PROJ& getProjectionFromDeclQueue(const std::string name) const {
0086 auto it = std::find_if(_declQueue.begin(), _declQueue.end(),
0087 [&name](const std::pair<std::shared_ptr<Projection>, std::string> &Qmember) {return Qmember.second == name;});
0088 if (it != _declQueue.end()){
0089 return dynamic_cast<PROJ&>(*(it->first));
0090 }
0091 else {
0092
0093 MSG_ERROR("Projection " << name << " not found in declQueue of " << this << " (" << this->name() << ")");
0094 throw RangeError("Projection lookup failed in getProjectionFromDeclQueue");
0095 }
0096 }
0097
0098
0099
0100
0101
0102
0103 public:
0104
0105
0106
0107
0108 template <typename PROJ=Projection>
0109 typename std::enable_if_t<std::is_base_of<Projection, PROJ>::value, const PROJ&>
0110 apply(const Event& evt, const Projection& proj) const { return pcast<PROJ>(_apply(evt, proj)); }
0111
0112
0113 template <typename PROJ=Projection>
0114 typename std::enable_if_t<std::is_base_of<Projection, PROJ>::value, const PROJ&>
0115 apply(const Event& evt, const PROJ& proj) const { return pcast<PROJ>(_apply(evt, proj)); }
0116
0117
0118 template <typename PROJ=Projection>
0119 typename std::enable_if_t<std::is_base_of<Projection, PROJ>::value, const PROJ&>
0120 apply(const Event& evt, const std::string& name) const { return pcast<PROJ>(_apply(evt, name)); }
0121
0122
0123 template <typename PROJ=Projection>
0124 typename std::enable_if_t<std::is_base_of<Projection, PROJ>::value, const PROJ&>
0125 apply(const std::string& name, const Event& evt) const { return pcast<PROJ>(_apply(evt, name)); }
0126
0127
0128
0129
0130
0131 void markAsOwned() const { _owned = true; }
0132
0133
0134 protected:
0135
0136 Log& getLog() const {
0137 return Log::getLog("Rivet.ProjectionHandler");
0138 }
0139
0140
0141
0142 ProjectionHandler& getProjHandler() const {
0143 return *_projhandler;
0144 }
0145
0146
0147 private:
0148
0149
0150
0151
0152
0153
0154
0155
0156
0157
0158
0159
0160
0161
0162 template <typename PROJ>
0163 const PROJ& declareProjection(const PROJ& proj, const std::string& name) const {
0164 const Projection& reg = _declareProjection(proj, name);
0165 const PROJ& rtn = dynamic_cast<const PROJ&>(reg);
0166 rtn.setProjectionHandler(getProjHandler());
0167 return rtn;
0168 }
0169
0170 protected:
0171
0172
0173
0174 template <typename PROJ>
0175 const PROJ& declare(const PROJ& proj, const std::string& name) const {
0176 std::shared_ptr<Projection> projClone = proj.clone();
0177 _declQueue.push_back(make_pair(projClone, name));
0178 return (dynamic_cast<PROJ&>(*projClone));
0179 }
0180
0181
0182 template <typename PROJ>
0183 const PROJ& declare(const std::string& name, const PROJ& proj) const {
0184 std::shared_ptr<Projection> projClone = proj.clone();
0185 _declQueue.push_back(make_pair(projClone, name));
0186 return (dynamic_cast<PROJ&>(*projClone));
0187 }
0188
0189
0190
0191 const Projection& _declareProjection(const Projection& proj, const std::string& name) const;
0192
0193
0194
0195
0196
0197
0198 const Projection& _apply(const Event& evt, const std::string& name) const;
0199
0200
0201
0202 const Projection& _apply(const Event& evt, const Projection& proj) const;
0203
0204
0205
0206 void setProjectionHandler(ProjectionHandler& projectionHandler) const;
0207
0208
0209 bool _allowProjReg;
0210
0211
0212 private:
0213
0214
0215 mutable bool _owned;
0216
0217
0218
0219 mutable ProjectionHandler* _projhandler;
0220
0221
0222 mutable std::deque<pair<std::shared_ptr<Projection>, string>> _declQueue;
0223
0224 protected:
0225
0226 void _syncDeclQueue() const;
0227
0228 };
0229 }
0230
0231 #endif