fire v0.19.0
Framework for sImulation and Reconstruction of Events
Processor.h
1#ifndef FIRE_PROCESSOR_H
2#define FIRE_PROCESSOR_H
3
4#include "fire/Conditions.h"
5#include "fire/Event.h"
6#include "fire/RunHeader.h"
7#include "fire/StorageControl.h"
8#include "fire/config/Parameters.h"
9#include "fire/exception/Exception.h"
10#include "fire/factory/Factory.h"
11#include "fire/logging/Logger.h"
12
13namespace fire {
14
22class Process;
23
57class Processor {
58 public:
66 public:
67 AbortEventException() noexcept : std::exception() {}
68 };
69
70 public:
85
89 virtual ~Processor() = default;
90
100 virtual void beforeNewRun(RunHeader &header) {}
101
113 virtual void onNewRun(const RunHeader &runHeader) {}
114
123 virtual void onFileOpen(const std::string &file_name) {}
124
133 virtual void onFileClose(const std::string &file_name) {}
134
139 virtual void onProcessStart() {}
140
146 virtual void onProcessEnd() {}
147
151 const std::string &getName() const { return name_; }
152
153
162 virtual void process(Event &event) = 0;
163
172 virtual void attach(Process *p) final { process_ = p; }
173
195 template <class T>
196 const T &getCondition(const std::string &condition_name) {
197 return getConditions().get<T>(condition_name);
198 }
199 public:
219 class Factory {
220 public:
225
233
234 public:
244 static Factory& get() {
245 static Factory the_factory;
246 return the_factory;
247 }
248
267 template<typename DerivedType>
268 uint64_t declare() {
269 std::string full_name{boost::core::demangle(typeid(DerivedType).name())};
270 library_[full_name] = &maker<DerivedType>;
271 return reinterpret_cast<std::uintptr_t>(&library_);
272 }
273
292 PrototypePtr make(const std::string& full_name,
293 const config::Parameters& ps,
294 Process& p) {
295 auto lib_it{library_.find(full_name)};
296 if (lib_it == library_.end()) {
297 throw Exception("Factory","A processor of class " + full_name +
298 " has not been declared.",false);
299 }
300 return lib_it->second(ps,p);
301 }
302
304 Factory(Factory const&) = delete;
305
307 void operator=(Factory const&) = delete;
308
309 private:
321 template <typename DerivedType>
325 // new type
326 ptr = std::make_unique<DerivedType>(parameters);
327 ptr->attach(&process);
328 } else {
329 // old type
330 ptr = std::make_unique<DerivedType>(parameters.get<std::string>("name"), process);
331 dynamic_cast<DerivedType*>(ptr.get())->configure(const_cast<config::Parameters&>(parameters));
332 }
333 return ptr;
334 }
335
337 Factory() = default;
338
341 }; // Factory
342
343 protected:
353 const std::string &purpose = "") const;
354
355
361 void abortEvent() { throw AbortEventException(); }
362
368 inline void fatalError(const std::string &msg) { throw Exception(name_, msg); }
369
377
378 private:
382 Conditions &getConditions() const;
383
386
388 Process *process_{nullptr};
389};
390
391} // namespace fire
392
413#define DECLARE_PROCESSOR(CLASS) \
414 namespace { \
415 auto v = fire::Processor::Factory::get().declare<CLASS>(); \
416 }
417
418#endif
Container and cache for conditions and conditions providers.
Definition: Conditions.h:66
const ConditionType & get(const std::string &condition_name)
Primary request action for a conditions object.
Definition: Conditions.h:114
Event class for interfacing with processors.
Definition: Event.h:28
Standard base exception class with some useful output information.
Definition: Exception.h:18
The central object managing the data processing.
Definition: Process.h:16
Specific exception used to abort an event.
Definition: Processor.h:65
The special factory used to create processors.
Definition: Processor.h:219
PrototypePtr make(const std::string &full_name, const config::Parameters &ps, Process &p)
make a new object by name
Definition: Processor.h:292
std::unique_ptr< Processor > PrototypePtr
The base pointer for processors.
Definition: Processor.h:224
Factory()=default
private constructor to prevent creation
Factory(Factory const &)=delete
delete the copy constructor
uint64_t declare()
register a new object to be constructible
Definition: Processor.h:268
static PrototypePtr maker(const config::Parameters &parameters, Process &process)
make a DerivedType returning a PrototypePtr
Definition: Processor.h:322
static Factory & get()
get the factory instance
Definition: Processor.h:244
void operator=(Factory const &)=delete
delete the assignment operator
std::unordered_map< std::string, PrototypeMaker > library_
library of possible objects to create
Definition: Processor.h:340
Base class for all event processing components.
Definition: Processor.h:57
virtual void attach(Process *p) final
Attach the current process to this processor.
Definition: Processor.h:172
virtual void onFileOpen(const std::string &file_name)
Callback for the Processor to take any necessary action when a new input event file is opened.
Definition: Processor.h:123
Conditions & getConditions() const
Internal getter for conditions without exposing all of Process.
Definition: Processor.cxx:17
virtual void onProcessStart()
Callback for the Processor to take any necessary action when the processing of events starts.
Definition: Processor.h:139
virtual void onFileClose(const std::string &file_name)
Callback for the Processor to take any necessary action when a event input file is closed.
Definition: Processor.h:133
void setStorageHint(StorageControl::Hint hint, const std::string &purpose="") const
Mark the current event as having the given storage control hint from this processor and the given pur...
Definition: Processor.cxx:11
void abortEvent()
Abort the event immediately.
Definition: Processor.h:361
logging::logger theLog_
The logger for this Processor.
Definition: Processor.h:376
void fatalError(const std::string &msg)
End processing due to a fatal runtime error.
Definition: Processor.h:368
virtual void process(Event &event)=0
have the derived processors do what they need to do
Process * process_
Handle to current process.
Definition: Processor.h:388
virtual void onProcessEnd()
Callback for the Processor to take any necessary action when the processing of events finishes,...
Definition: Processor.h:146
Processor(const config::Parameters &ps)
Configure the processor upon construction.
Definition: Processor.cxx:7
const std::string & getName() const
Get the processor name.
Definition: Processor.h:151
virtual ~Processor()=default
virtual default destructor so derived classes can be destructed
virtual void onNewRun(const RunHeader &runHeader)
Callback for the Processor to take any necessary action when the run being processed changes.
Definition: Processor.h:113
const T & getCondition(const std::string &condition_name)
Access a conditions object for the current event.
Definition: Processor.h:196
std::string name_
The name of the Processor.
Definition: Processor.h:385
virtual void beforeNewRun(RunHeader &header)
Handle allowing processors to modify run headers before the run begins.
Definition: Processor.h:100
Container for run parameters.
Definition: RunHeader.h:27
Hint
Hints that can be provided by processors to the storage controller.
Definition: StorageControl.h:30
Class encapsulating parameters for configuring a processor.
Definition: Parameters.h:28
const T & get(const std::string &name) const
Retrieve the parameter of the given name.
Definition: Parameters.h:76
Interface to Boost.Logging.
T get(T... args)
log::sources::severity_channel_logger_mt< level, std::string > logger
Short names for boost::log.
Definition: Logger.h:84
fire::Process Process
alias Process into this namespace
Definition: EventProcessor.h:105