rivet is hosted by Hepforge, IPPP Durham
Rivet 4.1.0
ProjectionApplier.hh
1// -*- C++ -*-
2#ifndef RIVET_ProjectionApplier_HH
3#define RIVET_ProjectionApplier_HH
4
5#include <deque>
6#include "Rivet/Config/RivetCommon.hh"
7#include "Rivet/Projection.fhh"
8#include "Rivet/ProjectionHandler.hh"
9#include "Rivet/Tools/Logging.hh"
10
11namespace Rivet {
12
13
14 // Forward declarations
15 class Event;
16
17
23 public:
24
25 // The proj handler needs access to reset the _allowProjReg flag before calling a.init()
26 // friend class ProjectionHandler;
27
30
31 // Virtual destructor: ensure that inheritance is possible.
32 virtual ~ProjectionApplier();
33
34
38 virtual std::string name() const = 0;
40
43
45 std::set<ConstProjectionPtr> getProjections() const {
46 return getProjHandler().getChildProjections(*this, ProjectionHandler::DEEP);
47 }
48
50 std::set<ConstProjectionPtr> getImmediateChildProjections() const {
51 return getProjHandler().getChildProjections(*this, ProjectionHandler::SHALLOW);
52 }
53
55 bool hasProjection(const std::string& name) const {
56 if (_projhandler != nullptr) {
57 // If we have a registered proj handler, check its registered names for this PA
58 return getProjHandler().hasProjection(*this, name);
59 } else {
60 // If we don't have a registered proj handler yet, check names against the queue
61 for (const auto& ptr_name_pair : _declQueue) {
62 if (ptr_name_pair.second == name) return true;
63 }
64 return false;
65 }
66 }
67
70 template <typename PROJ>
71 const PROJ& getProjection(const std::string& name) const {
72 if (_projhandler != nullptr) {
73 const Projection& p = getProjHandler().getProjection(*this, name);
74 return pcast<PROJ>(p);
75 }
76 else {
77 return getProjectionFromDeclQueue<PROJ>(name);
78 }
79 }
82 template <typename PROJ>
83 const PROJ& get(const std::string& name) const { return getProjection<PROJ>(name); }
84
87 const Projection& getProjection(const std::string& name) const {
88 return getProjHandler().getProjection(*this, name);
89 }
90
93 template <typename PROJ>
94 const PROJ& getProjectionFromDeclQueue(const std::string name) const {
95 auto it = std::find_if(_declQueue.begin(), _declQueue.end(),
96 [&name](const std::pair<std::shared_ptr<Projection>, std::string> &Qmember) {return Qmember.second == name;});
97 if (it != _declQueue.end()){
98 return dynamic_cast<PROJ&>(*(it->first));
99 }
100 else {
101 //If projection isn't found, deal with it properly.
102 MSG_ERROR("Projection " << name << " not found in declQueue of " << this << " (" << this->name() << ")");
103 throw RangeError("Projection lookup failed in getProjectionFromDeclQueue");
104 }
105 }
107
108
111
112 public:
113
115
117 template <typename PROJ=Projection>
118 typename std::enable_if_t<std::is_base_of<Projection, PROJ>::value, const PROJ&>
119 apply(const Event& evt, const Projection& proj) const { return pcast<PROJ>(_apply(evt, proj)); }
120
122 template <typename PROJ=Projection>
123 typename std::enable_if_t<std::is_base_of<Projection, PROJ>::value, const PROJ&>
124 apply(const Event& evt, const PROJ& proj) const { return pcast<PROJ>(_apply(evt, proj)); }
125
127 template <typename PROJ=Projection>
128 typename std::enable_if_t<std::is_base_of<Projection, PROJ>::value, const PROJ&>
129 apply(const Event& evt, const std::string& name) const { return pcast<PROJ>(_apply(evt, name)); }
130
132 template <typename PROJ=Projection>
133 typename std::enable_if_t<std::is_base_of<Projection, PROJ>::value, const PROJ&>
134 apply(const std::string& name, const Event& evt) const { return pcast<PROJ>(_apply(evt, name)); }
135
137
138
140 void markAsOwned() const { _owned = true; }
141
142
143 protected:
144
145 Log& getLog() const {
146 return Log::getLog("Rivet.ProjectionHandler");
147 }
148
149
152 return *_projhandler;
153 }
154
155
156 private:
159
171 template <typename PROJ>
172 const PROJ& declareProjection(const PROJ& proj, const std::string& name) const {
173 const Projection& reg = _declareProjection(proj, name);
174 const PROJ& rtn = dynamic_cast<const PROJ&>(reg);
175 rtn.setProjectionHandler(getProjHandler());
176 return rtn;
177 }
178
179 protected:
180
183 template <typename PROJ>
184 const PROJ& declare(const PROJ& proj, const std::string& name) const {
185 std::shared_ptr<Projection> projClone = proj.clone();
186 _declQueue.push_back(make_pair(projClone, name));
187 return (dynamic_cast<PROJ&>(*projClone));
188 }
191 template <typename PROJ>
192 const PROJ& declare(const std::string& name, const PROJ& proj) const {
193 std::shared_ptr<Projection> projClone = proj.clone();
194 _declQueue.push_back(make_pair(projClone, name));
195 return (dynamic_cast<PROJ&>(*projClone));
196 }
197
198
200 const Projection& _declareProjection(const Projection& proj, const std::string& name) const;
201
203
204
207 const Projection& _apply(const Event& evt, const std::string& name) const;
208
211 const Projection& _apply(const Event& evt, const Projection& proj) const;
212
213
215 void setProjectionHandler(ProjectionHandler& projectionHandler) const;
216
218 bool _allowProjReg;
219
220
221 private:
222
224 mutable bool _owned;
225
229 mutable ProjectionHandler* _projhandler;
230
235 mutable std::deque<pair<std::shared_ptr<Projection>, string>> _declQueue;
236
237protected:
238
243 void _syncDeclQueue() const;
244
245 };
246}
247
248#endif
Representation of a HepMC event, and enabler of Projection caching.
Definition Event.hh:22
Logging system for controlled & formatted writing to stdout.
Definition Logging.hh:10
static Log & getLog(const std::string &name)
Common base class for Projection and Analysis, used for internal polymorphism.
Definition ProjectionApplier.hh:22
const Projection & getProjection(const std::string &name) const
Definition ProjectionApplier.hh:87
const PROJ & get(const std::string &name) const
Definition ProjectionApplier.hh:83
void setProjectionHandler(ProjectionHandler &projectionHandler) const
const PROJ & getProjection(const std::string &name) const
Definition ProjectionApplier.hh:71
std::set< ConstProjectionPtr > getProjections() const
Get the contained projections, including recursion.
Definition ProjectionApplier.hh:45
std::enable_if_t< std::is_base_of< Projection, PROJ >::value, const PROJ & > apply(const std::string &name, const Event &evt) const
Apply the supplied projection on event evt (convenience arg-reordering alias).
Definition ProjectionApplier.hh:134
ProjectionHandler & getProjHandler() const
Get a reference to the ProjectionHandler for this thread.
Definition ProjectionApplier.hh:151
std::enable_if_t< std::is_base_of< Projection, PROJ >::value, const PROJ & > apply(const Event &evt, const Projection &proj) const
Apply the supplied projection on event evt.
Definition ProjectionApplier.hh:119
const PROJ & declare(const PROJ &proj, const std::string &name) const
Register a contained projection (user-facing version)
Definition ProjectionApplier.hh:184
std::enable_if_t< std::is_base_of< Projection, PROJ >::value, const PROJ & > apply(const Event &evt, const PROJ &proj) const
Apply the supplied projection on event evt (user-facing alias).
Definition ProjectionApplier.hh:124
bool hasProjection(const std::string &name) const
Does this applier have a projection registered under the name name?
Definition ProjectionApplier.hh:55
void markAsOwned() const
Mark this object as owned by a proj-handler.
Definition ProjectionApplier.hh:140
std::set< ConstProjectionPtr > getImmediateChildProjections() const
Get the contained projections, excluding recursion.
Definition ProjectionApplier.hh:50
std::enable_if_t< std::is_base_of< Projection, PROJ >::value, const PROJ & > apply(const Event &evt, const std::string &name) const
Apply the supplied projection on event evt (user-facing alias).
Definition ProjectionApplier.hh:129
const PROJ & getProjectionFromDeclQueue(const std::string name) const
Definition ProjectionApplier.hh:94
ProjectionApplier()
Constructor.
const PROJ & declare(const std::string &name, const PROJ &proj) const
Register a contained projection (user-facing, arg-reordered version)
Definition ProjectionApplier.hh:192
The projection handler is a central repository for projections to be used in a Rivet analysis run.
Definition ProjectionHandler.hh:43
const Projection & getProjection(const ProjectionApplier &parent, const string &name) const
set< const Projection * > getChildProjections(const ProjectionApplier &parent, ProjDepth depth=SHALLOW) const
bool hasProjection(const ProjectionApplier &parent, const string &name) const
Check if there is a name projection registered by parent.
Base class for all Rivet projections.
Definition Projection.hh:29
#define MSG_ERROR(x)
Highest level messaging for serious problems, using MSG_LVL.
Definition Logging.hh:189
Definition MC_CENT_PPB_Projections.hh:10
Error for e.g. use of invalid bin ranges.
Definition Exceptions.hh:22