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]()
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]() {
101 } },
102 { { "mock", "tamex" },
103 [&tamex_par, pileup_strategy, cal_to_hit_par, enable_sim_cal = option.enable_sim_cal]()
104 {
107 UseChannel<TamexChannel>(pileup_strategy, tamex_par, cal_to_hit_par, enable_sim_cal));
108 } },
109 { { "neuland", "mock" },
110 [cal_to_hit_par]() {
113 } },
114 { { "mock", "mock" },
116 };
117 }
118
119 } // namespace
120
125
126 Digitizer::~Digitizer() = default;
127
128 Digitizer::Digitizer(std::unique_ptr<Digitizing::EngineInterface> engine,
129 std::string_view points_name,
130 std::string_view hits_name,
131 std::string_view cal_hits_name)
132 : FairTask("R3BNeulandDigitizer")
133 , neuland_points_{ points_name }
134 , neuland_hits_{ hits_name }
135 , neuland_cal_hits_{ cal_hits_name }
136 , digitizing_engine_(std::move(engine))
137 {
138 }
139
140 void Digitizer::SetEngine(std::unique_ptr<Digitizing::EngineInterface> engine)
141 {
142 digitizing_engine_ = std::move(engine);
143 }
144
146 {
147 FairRunAna* run = FairRunAna::Instance();
148 if (run == nullptr)
149 {
150 throw R3B::runtime_error("R3BNeulandDigitizer::SetParContainers: No analysis run");
151 }
152
153 FairRuntimeDb* rtdb = run->GetRuntimeDb();
154 if (rtdb == nullptr)
155 {
156 throw R3B::runtime_error("R3BNeulandDigitizer::SetParContainers: No runtime database");
157 }
158
159 neuland_geo_par_ = dynamic_cast<R3BNeulandGeoPar*>(rtdb->getContainer("R3BNeulandGeoPar"));
160 if (neuland_geo_par_ == nullptr)
161 {
162 throw R3B::runtime_error("R3BNeulandDigitizer::SetParContainers: No R3BNeulandGeoPar");
163 }
164 }
165
166 auto Digitizer::Init() -> InitStatus
167 {
168 if (neuland_geo_par_ == nullptr)
169 {
170 throw R3B::runtime_error("neuland_geo_par is still nullptr during the initialization!");
171 }
173 neuland_points_.init();
174 neuland_hits_.init();
175 if (has_cal_output_)
176 {
177 neuland_cal_hits_.init();
178 }
180
181 return kSUCCESS;
182 }
183
185 {
186 // Initialize control histograms
187 auto const paddle_size = neuland_geo_par_->GetNumberOfModules();
188
189 hist_multi_one_ = data_monitor_.add_hist<TH1I>(
190 "MultiplicityOne", "Paddle multiplicity: only one PMT per paddle", paddle_size, 0, paddle_size);
191
192 hist_multi_two_ = data_monitor_.add_hist<TH1I>(
193 "MultiplicityTwo", "Paddle multiplicity: both PMTs of a paddle", paddle_size, 0, paddle_size);
194 auto const timeBinSize = 200;
196 data_monitor_.add_hist<TH1F>("hRLTimeToTrig", "R/Ltime-triggerTime", timeBinSize, -100., 100.);
198 {
199 hist_paddle_hit_size_ = data_monitor_.add_hist<TH1D>(
200 "paddle_hit_size", "Size of paddle hit per paddle", MAX_SIZE_BIN, 0.5, MAX_SIZE_BIN + 0.5);
202 "channel_signal_size", "Size of channel signals per channel", MAX_SIZE_BIN, 0.5, MAX_SIZE_BIN + 0.5);
203 hist_channel_hit_size_ = data_monitor_.add_hist<TH1D>(
204 "channel_hit_size", "Size of channel hits per channel", MAX_SIZE_BIN, 0.5, MAX_SIZE_BIN + 0.5);
205
206 hist_point_size_ = data_monitor_.add_hist<TH1D>(
207 "filtered_point_size", "Size of points per paddle", MAX_SIZE_BIN, 0.5, MAX_SIZE_BIN + 0.5);
208
209 // init point size checker
210 for (int paddle_id{}; paddle_id < paddle_size; ++paddle_id)
211 {
212 point_size_tracker_.emplace(paddle_id + 1, 0);
213 }
214 }
215 }
216
217 void Digitizer::Exec(Option_t* /*option*/)
218 {
219 neuland_hits_.clear();
220 if (has_cal_output_)
221 {
222 neuland_cal_hits_.clear();
223 }
224 digitizing_engine_->Reset();
225
227
228 digitizing_engine_->Construct();
229
231
232 auto paddle_action = [this](const Digitizing::AbstractPaddle& paddle)
233 {
234 if (!paddle.HasFired())
235 {
236 return;
237 }
238
240 {
241 fill_size_histograms(paddle);
242 }
243
244 if (has_cal_output_)
245 {
246 fill_cal_data(paddle);
247 }
248 fill_hit_data(paddle);
249 };
250
251 digitizing_engine_->DoEachPaddle(paddle_action);
252
253 LOG(debug) << fmt::format("Produced {} hits", neuland_hits_.size());
254 }
255
257 {
258
259 using R3B::Side;
260
261 const auto& paddle_hits = paddle.GetHits();
262 hist_paddle_hit_size_->Fill(static_cast<double>(paddle_hits.size()));
263
264 auto sides = std::array<Side, 2>{ Side::left, Side::right };
265 for (const auto side : sides)
266 {
267 const auto& channel = paddle.GetChannel(side);
268 hist_channel_hit_size_->Fill(static_cast<double>(channel.GetHits().size()));
269 hist_channel_signal_size_->Fill(static_cast<double>(channel.GetSignalSize()));
270 }
271 }
272
274 {
275 static constexpr auto GeVToMeVFac = 1000.;
276
277 // Look at each Land Point, if it deposited energy in the scintillator, store it with reference to the bar
278 for (const auto& point : neuland_points_)
279 {
281 (not neuland_point_filter_.IsPointAllowed(point)))
282 {
283 continue;
284 }
285 if (point.GetEnergyLoss() > 0.)
286 {
287 const auto paddle_ID = point.GetPaddle();
288
289 // Convert position of point to paddle-coordinates, including any rotation or translation
290 const auto& position = point.GetPosition();
291 const auto converted_position = neuland_geo_par_->ConvertToLocalCoordinates(position, paddle_ID);
292 LOGP(debug2, "Point in paddle: {}, with global position XYZ: {}", paddle_ID, position);
293 LOGP(debug2, "Converted to local position XYZ: {}", converted_position);
294
295 // Within the paddle frame, the relevant distance of the light from the pmt is always given by the
296 // X-Coordinate
297 const auto dist = converted_position.X();
298 digitizing_engine_->DepositLight(paddle_ID, point.GetTime(), point.GetLightYield() * GeVToMeVFac, dist);
300 {
301 ++(point_size_tracker_.at(paddle_ID));
302 }
303 } // eloss
304 } // points
305 }
306
308 {
309 // Fill control histograms
310 const auto triggerTime = digitizing_engine_->GetTriggerTime();
311
312 hist_rl_time_to_trig_->Fill(triggerTime);
313 hist_multi_one_->Fill(digitizing_engine_->DoAllPaddles(
314 [](auto paddles_view)
315 {
316 return static_cast<double>(std::count_if(paddles_view.begin(),
317 paddles_view.end(),
318 [](const auto& paddle) { return paddle.HasHalfFired(); }));
319 }));
320
321 hist_multi_two_->Fill(digitizing_engine_->DoAllPaddles(
322 [](auto paddles_view)
323 {
324 return static_cast<double>(std::count_if(
325 paddles_view.begin(), paddles_view.end(), [](const auto& paddle) { return paddle.HasFired(); }));
326 }));
327 if (has_size_monitor_)
328 {
329 for (const auto [key, value] : point_size_tracker_)
330 {
331 if (value == 0)
332 {
333 continue;
334 }
335 hist_point_size_->Fill(value);
336 }
337 }
338 }
339
340 auto Digitizer::Create(const DigiTaskOptions& option, FairRun* run) -> std::unique_ptr<Digitizer>
341 {
342 auto read_branch_names = std::vector<std::string>{};
343 auto write_branch_names = std::vector<std::string>{};
344
345 parse_io_branch_names(option, read_branch_names, 2, write_branch_names, 2); // NOLINT
346 auto engine_map = CreateDigiEngine(option, read_branch_names.at(1), run);
347 auto engine_gen = engine_map.at({ option.paddle, option.channel });
348 auto task = std::make_unique<Digitizer>(
349 engine_gen(), read_branch_names.at(0), write_branch_names.at(0), write_branch_names.at(1));
350 task->EnableCalDataOutput(option.enable_sim_cal);
351 task->EnableSizeMonitor(option.enable_size_monitor);
352 task->SetName(option.name.c_str());
353 auto point_filter = ParticleFilter::Create(option.point_filter);
354 LOG(info) << point_filter.Print();
355 task->SetPointFilter(std::move(point_filter));
356 return task;
357 }
358
360 {
361 const auto& signals = paddle.GetHits();
362
363 for (const auto& signal : signals)
364 {
365 const auto hitPositionLocal = ROOT::Math::XYZVector(signal.position, 0., 0.);
366 const auto hitPositionGlobal =
367 neuland_geo_par_->ConvertToGlobalCoordinates(hitPositionLocal, paddle.GetPaddleID());
368 const auto hitPixel = neuland_geo_par_->ConvertGlobalToPixel(hitPositionGlobal);
369
370 auto hit = R3BNeulandHit{ paddle.GetPaddleID(),
371 signal.left_channel_hit->tdc,
372 signal.right_channel_hit->tdc,
373 signal.time,
374 signal.left_channel_hit->qdcUnSat,
375 signal.right_channel_hit->qdcUnSat,
376 signal.energy,
377 hitPositionGlobal,
378 hitPixel };
379
380 if (hit_filters_.IsValid(hit))
381 {
382 neuland_hits_.get().emplace_back(std::move(hit));
383 LOGP(debug, "Adding neuland hit: {}", hit);
384 }
385 } // loop over all hits for each paddle
386 }
387
389 {
390 auto& cal_hits = neuland_cal_hits_.get();
391
392 const auto paddle_ID = paddle.GetPaddleID();
393 const auto& left_channel = paddle.GetLeftChannelRef();
394 const auto& right_channel = paddle.GetRightChannelRef();
395
396 const auto& left_channel_signals = left_channel.GetCalSignals();
397 const auto& right_channel_signals = right_channel.GetCalSignals();
398
399 if (left_channel_signals.size() != right_channel_signals.size())
400 {
401 return;
402 }
403
404 for (const auto& [left, right] : ranges::zip_view(left_channel_signals, right_channel_signals))
405 {
406
407 auto cal_data = SimCalData{ paddle_ID, left.tot, right.tot, left.tle, right.tle };
408
409 if (cal_hit_filter_.IsValid(cal_data))
410 {
411 cal_hits.push_back(std::move(cal_data));
412 LOGP(debug2, "Adding sim cal with {}", cal_data);
413 }
414 } // loop over signals
415 }
416
417 void Digitizer::FinishTask() { data_monitor_.save_to_sink(); }
418
420 {
422 {
423 for (auto& [key, value] : point_size_tracker_)
424 {
425 value = 0;
426 }
427 }
428 }
429
430} // 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
void parse_io_branch_names(const Option &option, std::vector< std::string > &read, int read_num, std::vector< std::string > &write, int write_num)
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