epic-drich-beam-test-analysis
ePIC dRICH beam test analysis framework
Loading...
Searching...
No Matches
alcor_finedata.h
1#pragma once
2
3#include <mist/ring_finding/hough_transform.h>
4#include <sstream>
5#include <cmath>
6#include <iostream>
7#include <fstream>
8#include <unordered_map>
9#include <array>
10#include "TH2F.h"
11#include "TF1.h"
12#include "TCanvas.h"
13#include "alcor_data.h"
14
24{
27
29 uint32_t rollover;
30
32 uint16_t coarse;
33
35 uint8_t fine;
36
38 float hit_x;
39
41 float hit_y;
42
44
47
49 uint32_t global_index;
50
52 uint32_t hit_mask;
53
55
58
62 alcor_finedata_struct(uint32_t rollover_,
63 uint16_t coarse_,
64 uint8_t fine_,
65 float hit_x_,
66 float hit_y_,
67 uint32_t global_index_,
68 uint32_t hit_mask_)
69 : rollover(rollover_),
70 coarse(coarse_),
71 fine(fine_),
72 hit_x(hit_x_),
73 hit_y(hit_y_),
74 global_index(global_index_),
75 hit_mask(hit_mask_)
76 {
77 }
78
84};
85
86// =============================================================================
87
103{
104public:
105 // -------------------------------------------------------------------------
106 // Constructors
107 // -------------------------------------------------------------------------
108
111
117
123
129
130 // -------------------------------------------------------------------------
131 // Getters — Raw fields
132 // -------------------------------------------------------------------------
133
136
138 alcor_finedata_struct get_data_struct() const { return internal_data; }
139
141 uint32_t get_global_index() const { return internal_data.global_index; }
142
144 uint32_t get_rollover() const { return internal_data.rollover; }
145
147 uint16_t get_coarse() const { return internal_data.coarse; }
148
150 uint8_t get_fine() const { return internal_data.fine; }
151
153 float get_hit_x() const { return internal_data.hit_x; }
154
156 float get_hit_y() const { return internal_data.hit_y; }
157
159 uint32_t get_mask() const { return internal_data.hit_mask; }
160
162
163 // -------------------------------------------------------------------------
164 // Getters — Derived timing
165 // -------------------------------------------------------------------------
166
169
174 float get_phase() const;
175
182 float get_time() const
183 {
184 return static_cast<float>(_ALCOR_ROLLOVER_TO_CC_) * static_cast<float>(get_rollover()) + static_cast<float>(get_coarse()) - get_phase();
185 }
186
190 float get_time_ns() const { return _ALCOR_CC_TO_NS_ * get_time(); }
191
193
194 // -------------------------------------------------------------------------
195 // Getters — Derived channel address
196 // -------------------------------------------------------------------------
197
200
202 int get_tdc() const { return get_global_index() % 4; }
203
205 int get_device() const { return 192 + (get_global_channel_index() / 256); }
206
208 int get_fifo() const { return (get_global_channel_index() % 256) / 8; }
209
211 int get_chip() const { return (get_global_channel_index() % 256) / 32; }
212
214 int get_eo_channel() const { return (get_global_channel_index() % 256) % 32 + 32 * (get_chip() % 2); }
215
217 int get_column() const { return ((get_global_channel_index() % 256) % 32) / 4; }
218
220 int get_pixel() const { return ((get_global_channel_index() % 256) % 32) % 4; }
221
223 int get_device_index() const { return get_eo_channel() + 64 * (get_chip() / 2); }
224
226 int get_global_channel_index() const { return get_global_index() / 4; }
227
229
230 // -------------------------------------------------------------------------
231 // Getters — Spatial (randomised)
232 // -------------------------------------------------------------------------
233
236
238 float get_hit_x_rnd() const { return internal_data.hit_x + (_rnd_(_global_gen_) * 3.0f - 1.5f); }
239
241 float get_hit_y_rnd() const { return internal_data.hit_y + (_rnd_(_global_gen_) * 3.0f - 1.5f); }
242
244 float get_hit_r_rnd() const { return get_hit_r_rnd({0.f, 0.f}); }
245
247 float get_hit_phi_rnd() const { return get_hit_phi_rnd({0.f, 0.f}); }
248
253 float get_hit_r_rnd(std::array<float, 2> v) const;
254
259 float get_hit_phi_rnd(std::array<float, 2> v) const;
260
262
263 // -------------------------------------------------------------------------
264 // Getters — Hit masks
265 // -------------------------------------------------------------------------
266
269
274 void add_mask_bit(hit_mask bit) { internal_data.hit_mask |= (1u << bit); }
275
280 void clear_mask_bit(hit_mask bit) { internal_data.hit_mask &= ~(1u << bit); }
281
287 bool has_mask_bit(hit_mask bit) const { return (internal_data.hit_mask >> bit) & 1u; }
288
290 bool is_ring_tag_first() const { return has_mask_bit(_HITMASK_ring_tag_first); }
291
293 bool is_ring_tag_second() const { return has_mask_bit(_HITMASK_ring_tag_second); }
294
296 bool is_cross_talk() const { return has_mask_bit(_HITMASK_cross_talk); }
297
299 bool is_afterpulse() const { return has_mask_bit(_HITMASK_afterpulse); }
300
302 bool is_part_lane() const { return has_mask_bit(_HITMASK_part_lane); }
303
305 bool is_dead_lane() const { return has_mask_bit(_HITMASK_dead_lane); }
306
308
309 // -------------------------------------------------------------------------
310 // Setters — Raw fields
311 // -------------------------------------------------------------------------
312
315
320 void set_data_struct(const alcor_finedata_struct &d) { internal_data = d; }
321
326 void set_global_index(uint32_t calib) { internal_data.global_index = calib; }
327
332 void set_rollover(uint32_t r) { internal_data.rollover = r; }
333
338 void set_coarse(uint16_t c) { internal_data.coarse = c; }
339
344 void set_fine(uint8_t f) { internal_data.fine = f; }
345
350 void set_mask(uint32_t mask) { internal_data.hit_mask = mask; }
351
353 void set_streaming_ring_trigger_mask() { add_mask_bit(_HITMASK_streaming_ring_trigger_); }
354
356
357 // -------------------------------------------------------------------------
358 // Setters — Calibration parameters
359 // -------------------------------------------------------------------------
360
363
369 static void set_param0(int global_tdc_index, float value) { calibration_parameters[global_tdc_index][0] = value; }
370
376 static void set_param1(int global_tdc_index, float value) { calibration_parameters[global_tdc_index][1] = value; }
377
383 static void set_param2(int global_tdc_index, float value) { calibration_parameters[global_tdc_index][2] = value; }
384
386
387 // -------------------------------------------------------------------------
388 // Comparison operators
389 // -------------------------------------------------------------------------
390
394
396 bool operator<(const alcor_finedata &v) const { return get_time() < v.get_time(); }
397
399 bool operator<=(const alcor_finedata &v) const { return get_time() <= v.get_time(); }
400
402 bool operator>(const alcor_finedata &v) const { return get_time() > v.get_time(); }
403
405 bool operator>=(const alcor_finedata &v) const { return get_time() >= v.get_time(); }
406
408
409 // -------------------------------------------------------------------------
410 // Calibration I/O
411 // -------------------------------------------------------------------------
412
415
427 void generate_calibration(TH2F *calibration_histogram, bool overwrite_calibration);
428
434 void update_calibration(TH2F *h) { generate_calibration(h, false); }
435
440 void write_calib_to_file(const std::string &filename);
441
448 void read_calib_from_file(const std::string &filename, bool clear_first = true, bool overwrites = true);
449
451
452 // -------------------------------------------------------------------------
453 // Finding rings algorithms
454 // -------------------------------------------------------------------------
455
458
484 static std::vector<mist::ring_finding::ring_result> alcor_find_rings_hough(
485 mist::ring_finding::hough_transform &ht,
486 std::vector<alcor_finedata> &alcor_hits,
487 float threshold_fraction,
488 int min_hits,
489 int min_active,
490 int max_rings = 2,
491 float collection_radius = 6.f);
492
494
495private:
496 // -------------------------------------------------------------------------
497 // Private data
498 // -------------------------------------------------------------------------
499
501 alcor_finedata_struct internal_data;
502
509 inline static std::unordered_map<int, std::array<float, 3>> calibration_parameters = {};
510
511 // -------------------------------------------------------------------------
512 // Private helpers
513 // -------------------------------------------------------------------------
514
516 void set_standard_function();
517};
Data structures and utilities for ALCOR hit-level data handling.
hit_mask
Bit positions used inside alcor_data_struct::hit_mask.
Definition alcor_data.h:97
Represents a single calibrated ALCOR TDC hit with fine-time correction.
Definition alcor_finedata.h:103
alcor_finedata()
Default constructor. Initialises all fields to zero.
Definition alcor_finedata.cxx:23
alcor_finedata_struct get_data_struct() const
Returns a copy of the underlying alcor_finedata_struct.
Definition alcor_finedata.h:138
int get_fifo() const
Returns the FIFO number decoded from the calibration index.
Definition alcor_finedata.h:208
void read_calib_from_file(const std::string &filename, bool clear_first=true, bool overwrites=true)
Loads calibration parameters from a plain-text file.
Definition alcor_finedata.cxx:85
float get_hit_x() const
Returns the x-axis position from mapping.
Definition alcor_finedata.h:153
void set_data_struct(const alcor_finedata_struct &d)
Replaces the underlying data struct.
Definition alcor_finedata.h:320
bool has_mask_bit(hit_mask bit) const
Checks whether a single bit is set in the hit mask.
Definition alcor_finedata.h:287
float get_hit_y() const
Returns the y-axis position from mapping.
Definition alcor_finedata.h:156
void add_mask_bit(hit_mask bit)
Sets a single bit in the hit mask.
Definition alcor_finedata.h:274
uint8_t get_fine() const
Returns the fine timestamp (TDC bin within a clock cycle).
Definition alcor_finedata.h:150
uint32_t get_mask() const
Returns the hit bitmask.
Definition alcor_finedata.h:159
bool is_cross_talk() const
Checks whether the hit is flagged as cross-talk.
Definition alcor_finedata.h:296
void set_global_index(uint32_t calib)
Sets the calibration index.
Definition alcor_finedata.h:326
int get_global_channel_index() const
Returns the global channel index stripped of TDC info.
Definition alcor_finedata.h:226
void set_rollover(uint32_t r)
Sets the rollover counter.
Definition alcor_finedata.h:332
int get_column() const
Returns the column address decoded from the calibration index.
Definition alcor_finedata.h:217
int get_tdc() const
Returns the TDC index decoded from the calibration index.
Definition alcor_finedata.h:202
bool operator<(const alcor_finedata &v) const
Less-than comparison against an alcor_finedata hit.
Definition alcor_finedata.h:396
void generate_calibration(TH2F *calibration_histogram, bool overwrite_calibration)
Derives calibration parameters from a 2D fine-time histogram.
Definition alcor_finedata.cxx:105
bool is_ring_tag_second() const
Checks whether the hit is tagged as part of a ring (second pass).
Definition alcor_finedata.h:293
int get_device_index() const
Returns the per-device TDC index.
Definition alcor_finedata.h:223
float get_hit_y_rnd() const
Returns the pixel-randomised y-coordinate, uniform within ±1.5 mm of the hit position.
Definition alcor_finedata.h:241
bool is_ring_tag_first() const
Checks whether the hit is tagged as part of a ring (first pass).
Definition alcor_finedata.h:290
bool operator>(const alcor_finedata &v) const
Greater-than comparison against an alcor_finedata hit.
Definition alcor_finedata.h:402
uint32_t get_global_index() const
Returns the calibration index identifying the TDC channel.
Definition alcor_finedata.h:141
bool is_dead_lane() const
Checks whether the hit originates from a dead lane.
Definition alcor_finedata.h:305
static void set_param2(int global_tdc_index, float value)
Sets calibration parameter 2 for a given TDC channel.
Definition alcor_finedata.h:383
void set_streaming_ring_trigger_mask()
Flags the hit as a streaming ring trigger participant.
Definition alcor_finedata.h:353
static std::vector< mist::ring_finding::ring_result > alcor_find_rings_hough(mist::ring_finding::hough_transform &ht, std::vector< alcor_finedata > &alcor_hits, float threshold_fraction, int min_hits, int min_active, int max_rings=2, float collection_radius=6.f)
Convert a vector of alcor_finedata hits into hough_hit, run the Hough-transform ring finder,...
Definition alcor_finedata.cxx:181
int get_eo_channel() const
Returns the even/odd channel index decoded from the calibration index.
Definition alcor_finedata.h:214
bool operator>=(const alcor_finedata &v) const
Greater-than-or-equal comparison against an alcor_finedata hit.
Definition alcor_finedata.h:405
float get_time_ns() const
Returns the calibrated hit time in nanoseconds.
Definition alcor_finedata.h:190
uint16_t get_coarse() const
Returns the coarse timestamp (clock-cycle count).
Definition alcor_finedata.h:147
int get_chip() const
Returns the chip ID decoded from the calibration index.
Definition alcor_finedata.h:211
void update_calibration(TH2F *h)
Updates calibration without overwriting existing entries. Alias for generate_calibration with overwri...
Definition alcor_finedata.h:434
void set_coarse(uint16_t c)
Sets the coarse timestamp.
Definition alcor_finedata.h:338
float get_hit_phi_rnd() const
Returns the azimuthal angle from the origin using a freshly randomised position [rad].
Definition alcor_finedata.h:247
bool operator<=(const alcor_finedata &v) const
Less-than-or-equal comparison against an alcor_finedata hit.
Definition alcor_finedata.h:399
int get_device() const
Returns the readout device ID decoded from the calibration index.
Definition alcor_finedata.h:205
int get_pixel() const
Returns the pixel address decoded from the calibration index.
Definition alcor_finedata.h:220
float get_hit_r_rnd() const
Returns the radial distance from the origin using a freshly randomised position.
Definition alcor_finedata.h:244
bool is_afterpulse() const
Checks whether the hit is flagged as an afterpulse.
Definition alcor_finedata.h:299
void write_calib_to_file(const std::string &filename)
Writes the current calibration parameters to a plain-text file.
Definition alcor_finedata.cxx:76
void set_fine(uint8_t f)
Sets the fine timestamp.
Definition alcor_finedata.h:344
float get_hit_x_rnd() const
Returns the pixel-randomised x-coordinate, uniform within ±1.5 mm of the hit position.
Definition alcor_finedata.h:238
bool is_part_lane() const
Checks whether the hit originates from a partially active lane.
Definition alcor_finedata.h:302
float get_time() const
Returns the calibrated hit time in clock cycles. Combines rollover, coarse, and the fine-time phase c...
Definition alcor_finedata.h:182
static void set_param0(int global_tdc_index, float value)
Sets calibration parameter 0 for a given TDC channel.
Definition alcor_finedata.h:369
static void set_param1(int global_tdc_index, float value)
Sets calibration parameter 1 for a given TDC channel.
Definition alcor_finedata.h:376
uint32_t get_rollover() const
Returns the rollover counter.
Definition alcor_finedata.h:144
void set_mask(uint32_t mask)
Sets the hit bitmask.
Definition alcor_finedata.h:350
float get_phase() const
Returns the calibrated fine-time phase in clock cycles. Computed from the 3-parameter calibration sto...
Definition alcor_finedata.cxx:38
void clear_mask_bit(hit_mask bit)
Clears a single bit in the hit mask.
Definition alcor_finedata.h:280
Plain data container for a single ALCOR hit.
Definition alcor_data.h:60
Raw decoded hit data from an ALCOR TDC channel.
Definition alcor_finedata.h:24
uint16_t coarse
Coarse timestamp (clock-cycle counter).
Definition alcor_finedata.h:32
uint8_t fine
Fine timestamp (TDC interpolation bin within a clock cycle).
Definition alcor_finedata.h:35
alcor_finedata_struct()=default
Default constructor.
float hit_x
X-axis position from mapping.
Definition alcor_finedata.h:38
uint32_t hit_mask
Bitmask encoding hit channel/pixel information.
Definition alcor_finedata.h:52
alcor_finedata_struct(uint32_t rollover_, uint16_t coarse_, uint8_t fine_, float hit_x_, float hit_y_, uint32_t global_index_, uint32_t hit_mask_)
Constructor from individual values.
Definition alcor_finedata.h:62
float hit_y
Y-axis position from mapping.
Definition alcor_finedata.h:41
uint32_t rollover
Rollover counter (most-significant timing word).
Definition alcor_finedata.h:29
uint32_t global_index
Global calibration index identifying the TDC channel.
Definition alcor_finedata.h:49
std::uniform_real_distribution _rnd_(0.0, 1.0)
Uniform float distribution in [0, 1).
std::mt19937 _global_gen_(_global_rd_())
Mersenne-Twister 19937 engine seeded from _global_rd_.