Rivet API documentation

Rivet 4.1.3
ProjectionApplier.hh
1// -*- C++ -*-
2#ifndef RIVET_ProjectionApplier_HH
3#define RIVET_ProjectionApplier_HH
4
5#include "Rivet/Projection.fhh"
6#include "Rivet/ProjectionHandler.hh"
7#include "Rivet/Config/RivetCommon.hh"
8#include "Rivet/Tools/Logging.hh"
9#include <deque>
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 }
60 else {
61 // If we don't have a registered proj handler yet, check names against the queue
62 for (const auto& ptr_name_pair : _declQueue) {
63 if (ptr_name_pair.second == name) return true;
64 }
65 return false;
66 }
67 }
68
71 template <typename PROJ>
72 const PROJ& getProjection(const std::string& name) const {
73 if (_projhandler != nullptr) {
74 const Projection& p = getProjHandler().getProjection(*this, name);
75 return pcast<PROJ>(p);
76 }
77 else {
79 }
80 }
81
83 template <typename PROJ>
84 const PROJ& get(const std::string& name) const {
85 return getProjection<PROJ>(name);
86 }
87
90 const Projection& getProjection(const std::string& name) const {
91 return getProjHandler().getProjection(*this, name);
92 }
93
96 template <typename PROJ>
97 const PROJ& getProjectionFromDeclQueue(const std::string name) const {
98 auto it = std::find_if(_declQueue.begin(), _declQueue.end(),
99 [&name](const std::pair<std::shared_ptr<Projection>, std::string>& Qmember) {
100 return Qmember.second == name;
101 });
102 if (it != _declQueue.end()) {
103 return dynamic_cast<PROJ&>(*(it->first));
104 }
105 else {
106 //If projection isn't found, deal with it properly.
107 MSG_ERROR("Projection " << name << " not found in declQueue of " << this << " (" << this->name()
108 << ")");
109 throw RangeError("Projection lookup failed in getProjectionFromDeclQueue");
110 }
111 }
112
113
114
117
118 public:
119
121
123 template <typename PROJ = Projection>
124 typename std::enable_if_t<std::is_base_of<Projection, PROJ>::value, const PROJ&> apply(
125 const Event& evt,
126 const Projection& proj) const {
127 return pcast<PROJ>(_apply(evt, proj));
128 }
129
131 template <typename PROJ = Projection>
132 typename std::enable_if_t<std::is_base_of<Projection, PROJ>::value, const PROJ&> apply(
133 const Event& evt,
134 const PROJ& proj) const {
135 return pcast<PROJ>(_apply(evt, proj));
136 }
137
139 template <typename PROJ = Projection>
140 typename std::enable_if_t<std::is_base_of<Projection, PROJ>::value, const PROJ&> apply(
141 const Event& evt,
142 const std::string& name) const {
143 return pcast<PROJ>(_apply(evt, name));
144 }
145
147 template <typename PROJ = Projection>
148 typename std::enable_if_t<std::is_base_of<Projection, PROJ>::value, const PROJ&> apply(
149 const std::string& name,
150 const Event& evt) const {
151 return pcast<PROJ>(_apply(evt, name));
152 }
153
155
156
158 void markAsOwned() const {
159 _owned = true;
160 }
161
162
163 protected:
164
165 Log& getLog() const {
166 return Log::getLog("Rivet.ProjectionHandler");
167 }
168
169
172 return *_projhandler;
173 }
174
175
176 private:
177
180
192 template <typename PROJ>
193 const PROJ& declareProjection(const PROJ& proj, const std::string& name) const {
194 const Projection& reg = _declareProjection(proj, name);
195 const PROJ& rtn = dynamic_cast<const PROJ&>(reg);
196 rtn.setProjectionHandler(getProjHandler());
197 return rtn;
198 }
199
200 protected:
201
204 template <typename PROJ>
205 const PROJ& declare(const PROJ& proj, const std::string& name) const {
206 std::shared_ptr<Projection> projClone = proj.clone();
207 _declQueue.push_back(make_pair(projClone, name));
208 return (dynamic_cast<PROJ&>(*projClone));
209 }
210
212 template <typename PROJ>
213 const PROJ& declare(const std::string& name, const PROJ& proj) const {
214 std::shared_ptr<Projection> projClone = proj.clone();
215 _declQueue.push_back(make_pair(projClone, name));
216 return (dynamic_cast<PROJ&>(*projClone));
217 }
218
219
221 const Projection& _declareProjection(const Projection& proj, const std::string& name) const;
222
224
225
228 const Projection& _apply(const Event& evt, const std::string& name) const;
229
232 const Projection& _apply(const Event& evt, const Projection& proj) const;
233
234
236 void setProjectionHandler(ProjectionHandler& projectionHandler) const;
237
239 bool _allowProjReg;
240
241
242 private:
243
245 mutable bool _owned;
246
250 mutable ProjectionHandler* _projhandler;
251
256 mutable std::deque<pair<std::shared_ptr<Projection>, string>> _declQueue;
257
258 protected:
259
264 void _syncDeclQueue() const;
265 };
266}
267
268#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)
const Projection & getProjection(const std::string &name) const
Definition ProjectionApplier.hh:90
const PROJ & get(const std::string &name) const
Definition ProjectionApplier.hh:84
void setProjectionHandler(ProjectionHandler &projectionHandler) const
const PROJ & getProjection(const std::string &name) const
Definition ProjectionApplier.hh:72
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:148
ProjectionHandler & getProjHandler() const
Get a reference to the ProjectionHandler for this thread.
Definition ProjectionApplier.hh:171
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:124
const PROJ & declare(const PROJ &proj, const std::string &name) const
Register a contained projection (user-facing version).
Definition ProjectionApplier.hh:205
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:132
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:158
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:140
const PROJ & getProjectionFromDeclQueue(const std::string name) const
Definition ProjectionApplier.hh:97
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:213
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:202
double p(const ParticleBase &p)
Unbound function access to p.
Definition ParticleBaseUtils.hh:819
Definition LHCbCommon.hh:9
Error for e.g. use of invalid bin ranges.
Definition Exceptions.hh:23