rivet is hosted by Hepforge, IPPP Durham
Rivet 4.0.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 return getProjHandler().hasProjection(*this, name);
57 }
58
61 template <typename PROJ>
62 const PROJ& getProjection(const std::string& name) const {
63 if (_projhandler != nullptr){
64 const Projection& p = getProjHandler().getProjection(*this, name);
65 return pcast<PROJ>(p);
66 }
67 else {
68 return getProjectionFromDeclQueue<PROJ>(name);
69 }
70 }
73 template <typename PROJ>
74 const PROJ& get(const std::string& name) const { return getProjection<PROJ>(name); }
75
78 const Projection& getProjection(const std::string& name) const {
79 return getProjHandler().getProjection(*this, name);
80 }
81
84 template <typename PROJ>
85 const PROJ& getProjectionFromDeclQueue(const std::string name) const {
86 auto it = std::find_if(_declQueue.begin(), _declQueue.end(),
87 [&name](const std::pair<std::shared_ptr<Projection>, std::string> &Qmember) {return Qmember.second == name;});
88 if (it != _declQueue.end()){
89 return dynamic_cast<PROJ&>(*(it->first));
90 }
91 else {
92 //If projection isn't found, deal with it properly.
93 MSG_ERROR("Projection " << name << " not found in declQueue of " << this << " (" << this->name() << ")");
94 throw RangeError("Projection lookup failed in getProjectionFromDeclQueue");
95 }
96 }
98
99
102
103 public:
104
106
108 template <typename PROJ=Projection>
109 typename std::enable_if_t<std::is_base_of<Projection, PROJ>::value, const PROJ&>
110 apply(const Event& evt, const Projection& proj) const { return pcast<PROJ>(_apply(evt, proj)); }
111
113 template <typename PROJ=Projection>
114 typename std::enable_if_t<std::is_base_of<Projection, PROJ>::value, const PROJ&>
115 apply(const Event& evt, const PROJ& proj) const { return pcast<PROJ>(_apply(evt, proj)); }
116
118 template <typename PROJ=Projection>
119 typename std::enable_if_t<std::is_base_of<Projection, PROJ>::value, const PROJ&>
120 apply(const Event& evt, const std::string& name) const { return pcast<PROJ>(_apply(evt, name)); }
121
123 template <typename PROJ=Projection>
124 typename std::enable_if_t<std::is_base_of<Projection, PROJ>::value, const PROJ&>
125 apply(const std::string& name, const Event& evt) const { return pcast<PROJ>(_apply(evt, name)); }
126
128
129
131 void markAsOwned() const { _owned = true; }
132
133
134 protected:
135
136 Log& getLog() const {
137 return Log::getLog("Rivet.ProjectionHandler");
138 }
139
140
143 return *_projhandler;
144 }
145
146
147 private:
150
162 template <typename PROJ>
163 const PROJ& declareProjection(const PROJ& proj, const std::string& name) const {
164 const Projection& reg = _declareProjection(proj, name);
165 const PROJ& rtn = dynamic_cast<const PROJ&>(reg);
166 rtn.setProjectionHandler(getProjHandler());
167 return rtn;
168 }
169
170 protected:
171
174 template <typename PROJ>
175 const PROJ& declare(const PROJ& proj, const std::string& name) const {
176 std::shared_ptr<Projection> projClone = proj.clone();
177 _declQueue.push_back(make_pair(projClone, name));
178 return (dynamic_cast<PROJ&>(*projClone));
179 }
182 template <typename PROJ>
183 const PROJ& declare(const std::string& name, const PROJ& proj) const {
184 std::shared_ptr<Projection> projClone = proj.clone();
185 _declQueue.push_back(make_pair(projClone, name));
186 return (dynamic_cast<PROJ&>(*projClone));
187 }
188
189
191 const Projection& _declareProjection(const Projection& proj, const std::string& name) const;
192
194
195
198 const Projection& _apply(const Event& evt, const std::string& name) const;
199
202 const Projection& _apply(const Event& evt, const Projection& proj) const;
203
204
206 void setProjectionHandler(ProjectionHandler& projectionHandler) const;
207
209 bool _allowProjReg;
210
211
212 private:
213
215 mutable bool _owned;
216
219 mutable ProjectionHandler* _projhandler;
222 mutable std::deque<pair<std::shared_ptr<Projection>, string>> _declQueue;
223
224protected:
226 void _syncDeclQueue() const;
227
228 };
229}
230
231#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:78
const PROJ & get(const std::string &name) const
Definition ProjectionApplier.hh:74
void setProjectionHandler(ProjectionHandler &projectionHandler) const
const PROJ & getProjection(const std::string &name) const
Definition ProjectionApplier.hh:62
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:125
ProjectionHandler & getProjHandler() const
Get a reference to the ProjectionHandler for this thread.
Definition ProjectionApplier.hh:142
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:110
const PROJ & declare(const PROJ &proj, const std::string &name) const
Register a contained projection (user-facing version)
Definition ProjectionApplier.hh:175
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:115
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:131
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:120
const PROJ & getProjectionFromDeclQueue(const std::string name) const
Definition ProjectionApplier.hh:85
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:183
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