R3BROOT
R3B analysis software
Loading...
Searching...
No Matches
R3BDigitizingEngine.h
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) 2019 GSI Helmholtzzentrum für Schwerionenforschung GmbH *
3 * Copyright (C) 2019-2025 Members of R3B Collaboration *
4 * *
5 * This software is distributed under the terms of the *
6 * GNU General Public Licence (GPL) version 3, *
7 * copied verbatim in the file "LICENSE". *
8 * *
9 * In applying this license GSI does not waive the privileges and immunities *
10 * granted to it by virtue of its status as an Intergovernmental Organization *
11 * or submit itself to any jurisdiction. *
12 ******************************************************************************/
13
14#ifndef NEULAND_DIGITIZING_ENGINE_H
15#define NEULAND_DIGITIZING_ENGINE_H
16
18#include "R3BDigitizingPaddle.h"
19#include <cmath>
20#include <functional>
21#include <map>
22#include <memory>
23
24namespace R3B::Digitizing
25{
27 {
28 public:
30 // rule of 5
31 virtual ~DigitizingEngineInterface() = default;
36
37 virtual void DepositLight(int paddle_id, double time, double light, double dist) = 0;
38 [[nodiscard]] virtual auto GetTriggerTime() const -> double = 0;
39 virtual auto ExtractPaddles() -> std::map<int, std::unique_ptr<Paddle>> = 0;
40 virtual void Init() = 0;
41 };
42
43 // factory classes for paddle and channel:
44 template <typename ChannelClass, typename = std::enable_if_t<std::is_base_of_v<Channel, ChannelClass>>>
46 {
47 template <typename... Args>
48 explicit UseChannel(const Args&... args)
49 : BuildChannel([=](ChannelSide side) { return std::make_unique<ChannelClass>(side, args...); })
50 {
51 }
52 std::function<std::unique_ptr<ChannelClass>(ChannelSide)> BuildChannel;
53 };
54
55 template <typename PaddleClass, typename = std::enable_if_t<std::is_base_of_v<Paddle, PaddleClass>>>
56 struct UsePaddle
57 {
58 template <typename... Args>
59 explicit UsePaddle(const Args&... args)
60 : BuildPaddle([=](int paddleID) { return std::make_unique<PaddleClass>(paddleID, args...); })
61 {
62 }
63 std::function<std::unique_ptr<PaddleClass>(int)> BuildPaddle;
64 };
65
66 template <typename PaddleClass, typename ChannelClass, typename InitFunc = std::function<void()>>
68 {
69 private:
70 UsePaddle<PaddleClass> paddleClass_;
71 UseChannel<ChannelClass> channelClass_;
72 std::map<int, std::unique_ptr<Paddle>> paddles;
73 InitFunc initFunc_;
74
75 public:
77 const UsePaddle<PaddleClass>& p_paddleClass,
78 const UseChannel<ChannelClass>& p_channelClass,
79 InitFunc initFunc = []() {})
80 : paddleClass_{ p_paddleClass }
81 , channelClass_{ p_channelClass }
83 , initFunc_{ initFunc }
84 {
85 }
86
87 void DepositLight(int paddle_id, double time, double light, double dist) override
88 {
89 if (paddles.find(paddle_id) == paddles.end())
90 {
91 auto newPaddle = paddleClass_.BuildPaddle(paddle_id);
92
93 newPaddle->SetChannel(channelClass_.BuildChannel(Digitizing::ChannelSide::left));
94 newPaddle->SetChannel(channelClass_.BuildChannel(Digitizing::ChannelSide::right));
95
96 paddles[paddle_id] = std::move(newPaddle);
97 }
98 paddles.at(paddle_id)->DepositLight({ time, light, dist });
99 }
100
101 [[nodiscard]] auto GetTriggerTime() const -> double override
102 {
103 auto min_element = std::min_element(paddles.begin(),
104 paddles.end(),
105 [](const auto& left, const auto& right)
106 { return left.second->GetTrigTime() < right.second->GetTrigTime(); });
107 return (min_element == paddles.end()) ? NAN : min_element->second->GetTrigTime();
108 }
109
110 [[nodiscard]] auto ExtractPaddles() -> std::map<int, std::unique_ptr<Paddle>> override
111 {
112 return std::move(paddles);
113 }
114
115 void Init() override { initFunc_(); }
116 void SetInit(const InitFunc& initFunc) { initFunc_ = initFunc; }
117 };
118
119 // helper to create owning digitizingEngine:
120 template <typename... Args>
121 [[nodiscard]] auto CreateEngine(Args&&... args)
122 -> std::unique_ptr<decltype(DigitizingEngine{ std::forward<Args>(args)... })>
123 {
124 return std::make_unique<decltype(DigitizingEngine{ std::forward<Args>(args)... })>(std::forward<Args>(args)...);
125 }
126
127} // namespace R3B::Digitizing
128
129#endif // NEULAND_DIGITIZING_ENGINE_H
void DepositLight(int paddle_id, double time, double light, double dist) override
void SetInit(const InitFunc &initFunc)
DigitizingEngine(const UsePaddle< PaddleClass > &p_paddleClass, const UseChannel< ChannelClass > &p_channelClass, InitFunc initFunc=[]() {})
auto GetTriggerTime() const -> double override
auto ExtractPaddles() -> std::map< int, std::unique_ptr< Paddle > > override
virtual auto GetTriggerTime() const -> double=0
auto operator=(const DigitizingEngineInterface &other) -> DigitizingEngineInterface &=delete
virtual auto ExtractPaddles() -> std::map< int, std::unique_ptr< Paddle > >=0
DigitizingEngineInterface(DigitizingEngineInterface &&other)=default
auto operator=(DigitizingEngineInterface &&other) -> DigitizingEngineInterface &=delete
virtual void DepositLight(int paddle_id, double time, double light, double dist)=0
DigitizingEngineInterface(const DigitizingEngineInterface &other)=delete
STL class.
auto CreateEngine(Args &&... args) -> std::unique_ptr< decltype(DigitizingEngine{ std::forward< Args >(args)... })>
UseChannel(const Args &... args)
std::function< std::unique_ptr< Type >(ChannelSide)> BuildChannel
std::function< std::unique_ptr< Type >(int)> BuildPaddle
UsePaddle(const Args &... args)