R3BROOT
R3B analysis software
Loading...
Searching...
No Matches
R3BNeulandDigitizer.cxx
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#include "R3BNeulandDigitizer.h"
15#include "FairRunAna.h"
16#include "FairRuntimeDb.h"
18#include "NeulandSimCalData.h"
19#include "R3BDataMonitor.h"
21#include "R3BDigitizingEngine.h"
22#include "R3BDigitizingPaddle.h"
26#include "R3BDigitizingTamex.h"
27#include "R3BException.h"
29#include "R3BNeulandCommon.h"
30#include "R3BNeulandGeoPar.h"
31#include "R3BNeulandHit.h"
32#include <FairTask.h>
33#include <Math/Vector3Dfwd.h>
35#include <R3BShared.h>
36#include <RtypesCore.h>
37#include <TFile.h>
38#include <TH1.h>
39#include <TVector3.h>
40#include <algorithm>
41#include <array>
42#include <fairlogger/Logger.h>
43#include <fmt/core.h>
44#include <functional>
45#include <map>
46#include <memory>
47#include <range/v3/view/zip.hpp>
48#include <string>
49#include <string_view>
50#include <utility>
51#include <vector>
52
53constexpr auto MAX_SIZE_BIN = 20;
54
55namespace R3B::Neuland
56{
57 template <typename Type>
59 template <typename Type>
61 namespace
62 {
63 inline auto CreateDigiEngine(const DigiTaskOptions& option, std::string_view hit_par_name, FairRun* run)
64 {
65 namespace Digitizing = R3B::Digitizing;
66 using NeulandPaddle = Digitizing::Neuland::Paddle;
67 namespace Tamex = Digitizing::Neuland::Tamex;
68 using TamexChannel = Tamex::Channel;
71 using MockPaddle = Digitizing::Neuland::MockPaddle;
72 using MockChannel = Digitizing::Neuland::MockChannel;
73 using TacquilaChannel = Digitizing::Neuland::TacQuila::Channel;
74
75 auto pileup_strategy = option.pileup_strategy;
76 const auto& tamex_par = option.tamex_par;
77 Cal2HitPar* cal_to_hit_par{ nullptr };
78 if (option.enable_hit_par)
79 {
80 LOGP(info, "cal_to_hit_par is used in digitization task!");
81 cal_to_hit_par = std::make_unique<Cal2HitPar>(hit_par_name).release();
82 run->GetRuntimeDb()->addContainer(cal_to_hit_par);
83 }
84 else
85 {
86 LOGP(info, "cal_to_hit_par is not used in digitization task!");
87 }
88 return std::map<std::pair<const std::string, const std::string>,
89 std::function<std::unique_ptr<Digitizing::EngineInterface>()>>{
90 { { "neuland", "tamex" },
91 [&tamex_par, pileup_strategy, cal_to_hit_par, enable_sim_cal = option.enable_sim_cal]() -> auto
92 {
94 UsePaddle<NeulandPaddle>(cal_to_hit_par),
95 UseChannel<TamexChannel>(pileup_strategy, tamex_par, cal_to_hit_par, enable_sim_cal));
96 } },
97 { { "neuland", "tacquila" },
98 [cal_to_hit_par]() -> auto
99 {
102 } },
103 { { "mock", "tamex" },
104 [&tamex_par, pileup_strategy, cal_to_hit_par, enable_sim_cal = option.enable_sim_cal]() -> auto
105 {
108 UseChannel<TamexChannel>(pileup_strategy, tamex_par, cal_to_hit_par, enable_sim_cal));
109 } },
110 { { "neuland", "mock" },
111 [cal_to_hit_par]() -> auto
112 {
115 } },
116 { { "mock", "mock" },
117 []() -> auto
119 };
120 }
121
122 } // namespace
123
128
129 Digitizer::~Digitizer() = default;
130
131 Digitizer::Digitizer(std::unique_ptr<Digitizing::EngineInterface> engine,
132 std::string_view points_name,
133 std::string_view hits_name,
134 std::string_view cal_hits_name)
135 : FairTask("R3BNeulandDigitizer")
136 , neuland_points_{ points_name }
137 , neuland_hits_{ hits_name }
138 , neuland_cal_hits_{ cal_hits_name }
139 , digitizing_engine_(std::move(engine))
140 {
141 }
142
143 void Digitizer::SetEngine(std::unique_ptr<Digitizing::EngineInterface> engine)
144 {
145 digitizing_engine_ = std::move(engine);
146 }
147
149 {
150 FairRunAna* run = FairRunAna::Instance();
151 if (run == nullptr)
152 {
153 throw R3B::runtime_error("R3BNeulandDigitizer::SetParContainers: No analysis run");
154 }
155
156 FairRuntimeDb* rtdb = run->GetRuntimeDb();
157 if (rtdb == nullptr)
158 {
159 throw R3B::runtime_error("R3BNeulandDigitizer::SetParContainers: No runtime database");
160 }
161
162 neuland_geo_par_ = dynamic_cast<R3BNeulandGeoPar*>(rtdb->getContainer("R3BNeulandGeoPar"));
163 if (neuland_geo_par_ == nullptr)
164 {
165 throw R3B::runtime_error("R3BNeulandDigitizer::SetParContainers: No R3BNeulandGeoPar");
166 }
167 }
168
169 auto Digitizer::Init() -> InitStatus
170 {
171 if (neuland_geo_par_ == nullptr)
172 {
173 throw R3B::runtime_error("neuland_geo_par is still nullptr during the initialization!");
174 }
176 neuland_points_.init();
177 neuland_hits_.init();
178 if (has_cal_output_)
179 {
180 neuland_cal_hits_.init();
181 }
183
184 return kSUCCESS;
185 }
186
188 {
189 // Initialize control histograms
190 auto const paddle_size = neuland_geo_par_->GetNumberOfModules();
191
192 hist_multi_one_ = data_monitor_.add_hist<TH1I>(
193 "MultiplicityOne", "Paddle multiplicity: only one PMT per paddle", paddle_size, 0, paddle_size);
194
195 hist_multi_two_ = data_monitor_.add_hist<TH1I>(
196 "MultiplicityTwo", "Paddle multiplicity: both PMTs of a paddle", paddle_size, 0, paddle_size);
197 auto const timeBinSize = 200;
199 data_monitor_.add_hist<TH1F>("hRLTimeToTrig", "right/left-time-triggerTime", timeBinSize, -100., 100.);
201 {
202 hist_paddle_hit_size_ = data_monitor_.add_hist<TH1D>(
203 "paddle_hit_size", "Size of paddle hit per paddle", MAX_SIZE_BIN, 0.5, MAX_SIZE_BIN + 0.5);
205 "channel_signal_size", "Size of channel signals per channel", MAX_SIZE_BIN, 0.5, MAX_SIZE_BIN + 0.5);
206 hist_channel_hit_size_ = data_monitor_.add_hist<TH1D>(
207 "channel_hit_size", "Size of channel hits per channel", MAX_SIZE_BIN, 0.5, MAX_SIZE_BIN + 0.5);
208
209 hist_point_size_ = data_monitor_.add_hist<TH1D>(
210 "filtered_point_size", "Size of points per paddle", MAX_SIZE_BIN, 0.5, MAX_SIZE_BIN + 0.5);
211
212 // init point size checker
213 for (int paddle_id{}; paddle_id < paddle_size; ++paddle_id)
214 {
215 point_size_tracker_.emplace(paddle_id + 1, 0);
216 }
217 }
218 }
219
220 void Digitizer::Exec(Option_t* /*option*/)
221 {
222 neuland_hits_.clear();
223 if (has_cal_output_)
224 {
225 neuland_cal_hits_.clear();
226 }
227 digitizing_engine_->Reset();
228
230
231 digitizing_engine_->Construct();
232
234
235 auto paddle_action = [this](const Digitizing::AbstractPaddle& paddle) -> void
236 {
237 if (!paddle.HasFired())
238 {
239 return;
240 }
241
243 {
244 fill_size_histograms(paddle);
245 }
246
247 if (has_cal_output_)
248 {
249 fill_cal_data(paddle);
250 }
251 fill_hit_data(paddle);
252 };
253
254 digitizing_engine_->DoEachPaddle(paddle_action);
255
256 LOGP(debug, "Produced {} hits", neuland_hits_.size());
257 }
258
260 {
261
262 using R3B::Side;
263
264 const auto& paddle_hits = paddle.GetHits();
265 hist_paddle_hit_size_->Fill(static_cast<double>(paddle_hits.size()));
266
267 auto sides = std::array<Side, 2>{ Side::left, Side::right };
268 for (const auto side : sides)
269 {
270 const auto& channel = paddle.GetChannel(side);
271 hist_channel_hit_size_->Fill(static_cast<double>(channel.GetHits().size()));
272 hist_channel_signal_size_->Fill(static_cast<double>(channel.GetSignalSize()));
273 }
274 }
275
277 {
278 static constexpr auto GeVToMeVFac = 1000.;
279
280 // Look at each Land Point, if it deposited energy in the scintillator, store it with reference to the bar
281 for (const auto& point : neuland_points_)
282 {
284 (not neuland_point_filter_.IsPointAllowed(point)))
285 {
286 continue;
287 }
288 if (point.GetEnergyLoss() > 0.)
289 {
290 const auto paddle_ID = point.GetPaddle();
291
292 // Convert position of point to paddle-coordinates, including any rotation or translation
293 const auto& position = point.GetPosition();
294 const auto converted_position = neuland_geo_par_->ConvertToLocalCoordinates(position, paddle_ID);
295 LOGP(debug2, "Point in paddle: {}, with global position XYZ: {}", paddle_ID, position);
296 LOGP(debug2, "Converted to local position XYZ: {}", converted_position);
297
298 // Within the paddle frame, the relevant distance of the light from the pmt is always given by the
299 // X-Coordinate
300 const auto dist = converted_position.X();
301 digitizing_engine_->DepositLight(paddle_ID, point.GetTime(), point.GetLightYield() * GeVToMeVFac, dist);
303 {
304 ++(point_size_tracker_.at(paddle_ID));
305 }
306 } // energy loss
307 } // points
308 }
309
311 {
312 // Fill control histograms
313 const auto triggerTime = digitizing_engine_->GetTriggerTime();
314
315 hist_rl_time_to_trig_->Fill(triggerTime);
316 hist_multi_one_->Fill(digitizing_engine_->DoAllPaddles(
317 [](auto paddles_view) -> double
318 {
319 return static_cast<double>(std::count_if(paddles_view.begin(),
320 paddles_view.end(),
321 [](const auto& paddle) -> bool
322 { return paddle.HasHalfFired(); }));
323 }));
324
325 hist_multi_two_->Fill(digitizing_engine_->DoAllPaddles(
326 [](auto paddles_view) -> double
327 {
328 return static_cast<double>(std::count_if(paddles_view.begin(),
329 paddles_view.end(),
330 [](const auto& paddle) -> bool { return paddle.HasFired(); }));
331 }));
332 if (has_size_monitor_)
333 {
334 for (const auto [key, value] : point_size_tracker_)
335 {
336 if (value == 0)
337 {
338 continue;
339 }
340 hist_point_size_->Fill(value);
341 }
342 }
343 }
344
345 auto Digitizer::Create(const DigiTaskOptions& option, FairRun* run) -> std::unique_ptr<Digitizer>
346 {
347 auto read_branch_names = std::vector<std::string>{};
348 auto write_branch_names = std::vector<std::string>{};
349
350 parse_io_branch_names(option, read_branch_names, 2, write_branch_names, 2); // NOLINT
351 auto engine_map = CreateDigiEngine(option, read_branch_names.at(1), run);
352 auto engine_gen = engine_map.at({ option.paddle, option.channel });
353 auto task = std::make_unique<Digitizer>(
354 engine_gen(), read_branch_names.at(0), write_branch_names.at(0), write_branch_names.at(1));
355 task->EnableCalDataOutput(option.enable_sim_cal);
356 task->EnableSizeMonitor(option.enable_size_monitor);
357 task->SetName(option.name.c_str());
358 auto point_filter = ParticleFilter::Create(option.point_filter);
359 LOG(info) << point_filter.Print();
360 task->SetPointFilter(std::move(point_filter));
361 return task;
362 }
363
365 {
366 const auto& signals = paddle.GetHits();
367
368 for (const auto& signal : signals)
369 {
370 const auto hitPositionLocal = ROOT::Math::XYZVector(signal.position, 0., 0.);
371 const auto hitPositionGlobal =
372 neuland_geo_par_->ConvertToGlobalCoordinates(hitPositionLocal, paddle.GetPaddleID());
373 const auto hitPixel = neuland_geo_par_->ConvertGlobalToPixel(hitPositionGlobal);
374
375 auto hit = R3BNeulandHit{ paddle.GetPaddleID(),
376 signal.left_channel_hit->tdc,
377 signal.right_channel_hit->tdc,
378 signal.time,
379 signal.left_channel_hit->qdcUnSat,
380 signal.right_channel_hit->qdcUnSat,
381 signal.energy,
382 hitPositionGlobal,
383 hitPixel };
384
385 if (hit_filters_.IsValid(hit))
386 {
387 neuland_hits_.get().emplace_back(std::move(hit));
388 LOGP(debug, "Adding neuland hit: {}", hit);
389 }
390 } // loop over all hits for each paddle
391 }
392
394 {
395 auto& cal_hits = neuland_cal_hits_.get();
396
397 const auto paddle_ID = paddle.GetPaddleID();
398 const auto& left_channel = paddle.GetLeftChannelRef();
399 const auto& right_channel = paddle.GetRightChannelRef();
400
401 const auto& left_channel_signals = left_channel.GetCalSignals();
402 const auto& right_channel_signals = right_channel.GetCalSignals();
403
404 if (left_channel_signals.size() != right_channel_signals.size())
405 {
406 return;
407 }
408
409 for (const auto& [left, right] : ranges::zip_view(left_channel_signals, right_channel_signals))
410 {
411
412 auto cal_data = SimCalData{ paddle_ID, left.tot, right.tot, left.tle, right.tle };
413
414 if (cal_hit_filter_.IsValid(cal_data))
415 {
416 cal_hits.push_back(std::move(cal_data));
417 LOGP(debug2, "Adding sim cal with {}", cal_data);
418 }
419 } // loop over signals
420 }
421
422 void Digitizer::FinishTask() { data_monitor_.save_to_sink(); }
423
425 {
427 {
428 for (auto& [key, value] : point_size_tracker_)
429 {
430 value = 0;
431 }
432 }
433 }
434
435} // namespace R3B::Neuland
constexpr auto MAX_SIZE_BIN
auto GetRightChannelRef() const -> auto &
auto GetLeftChannelRef() const -> auto &
auto GetChannel(R3B::Side side) const -> const Digitizing::AbstractChannel &
auto GetHits() const -> const std::vector< Hit > &
Digitizing::Neuland::TacQuila::Channel TacquilaChannel
void Exec(Option_t *) override
static auto Create(const R3B::Neuland::DigiTaskOptions &option, FairRun *run) -> std::unique_ptr< Digitizer >
Generator of the digitizing class.
Filterable< R3BNeulandHit & > hit_filters_
Digitizer()
Constructor with no input parameters.
R3B::Neuland::ParticleFilter neuland_point_filter_
R3BNeulandGeoPar * neuland_geo_par_
std::unique_ptr< Digitizing::EngineInterface > digitizing_engine_
void fill_size_histograms(const Digitizing::AbstractPaddle &paddle)
std::unordered_map< int, int > point_size_tracker_
R3B::OutputVectorConnector< R3BNeulandHit > neuland_hits_
auto Init() -> InitStatus override
void fill_hit_data(const R3B::Digitizing::AbstractPaddle &paddle)
Digitizing::Neuland::Paddle NeulandPaddle
void fill_cal_data(const R3B::Digitizing::AbstractPaddle &paddle)
void SetEngine(std::unique_ptr< Digitizing::EngineInterface > engine)
Setter of the internal engine.
Filterable< R3B::Neuland::SimCalData & > cal_hit_filter_
R3B::InputVectorConnector< R3BNeulandPoint > neuland_points_
R3B::OutputVectorConnector< R3B::Neuland::SimCalData > neuland_cal_hits_
static auto Create(const Options &options) -> ParticleFilter
Generator for the filter object from the configuration.
NeuLAND geometry parameter storage.
NeuLAND digitizing finder task.
auto CreateEngine(Args &&... args) -> std::unique_ptr< decltype(Engine{ std::forward< Args >(args)... })>
Simulation of NeuLAND Bar/Paddle.
constexpr auto BarsPerPlane
Digitizing::UsePaddle< Type > UsePaddle
Digitizing::UseChannel< Type > UseChannel
Configuration struct for R3B::Neuland::Digitizer used in R3B::Neuland::AnalysisApplication.
Digitizing::Neuland::Tamex::PeakPileUpStrategy pileup_strategy
bool enable_sim_cal
Flag to enable the simulated cal level data output.
bool enable_hit_par
Flag to enable the usage of cal_to_hit parameter.
R3B::Digitizing::Neuland::Tamex::Params tamex_par