ATLAS Offline Software
Loading...
Searching...
No Matches
HitSummaryDataUtils.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4#ifndef ACTSTRK_HITSUMMARYDATAUTILS_H
5#define ACTSTRK_HITSUMMARYDATAUTILS_H
6
12
13#include <vector>
14#include <cmath>
15#include <array>
16#include <tuple>
17#include <type_traits>
18
19namespace ActsTrk::detail {
20 enum class HitCategory: std::uint8_t {
24 };
25
29 public:
46
54
55 constexpr static unsigned short LAYER_REGION_MASK = 0xFF; // bits 0-7
56 constexpr static unsigned short REGION_BITS = 4; // bits 0-3
57 constexpr static unsigned short REGION_MASK = 0xF; // 4 bits
58 constexpr static unsigned short LAYER_BITS = 4; // bits 4-7
59 constexpr static unsigned short LAYER_MASK = 0xF; // 4 bits
60 constexpr static unsigned short SIGNED_ETA_MOD_BITS = 7; // bits 8-13 + 14(sign)
61 constexpr static unsigned short SIGNED_ETA_MOD_MASK = 0x7F; // 6 + 1(sign) bit
62
68 constexpr static unsigned short makeKey(unsigned short region, unsigned short layer, int eta_mod) {
69 // 3 bits region : 0-7 : pixelBarrelFlat - unknown
70 // 6 bits layer : 0-63
71 // 1+6 bits eta_mod : +- 0-63
72 // @TODO endcap side A/C ?
73 assert(region < (1<<REGION_BITS) );
74 assert(layer < (1<<LAYER_BITS));
75 assert( std::abs(eta_mod) < (1<<(SIGNED_ETA_MOD_BITS-1)) );
76 if (region != stripBarrel && region != pixelBarrelFlat) {
77 layer |= (static_cast<uint8_t>(eta_mod) & SIGNED_ETA_MOD_MASK) << LAYER_BITS ;
78 }
79 return static_cast<uint8_t>(region) | (layer<<REGION_BITS);
80 }
81 constexpr static unsigned short makeKey(unsigned short region) {
82 assert(region < (1<<REGION_BITS) );
83 return static_cast<uint8_t>(region);
84 }
85
88 constexpr static DetectorRegion regionFromKey(unsigned short key) {
89 return static_cast<DetectorRegion>(key & REGION_MASK); // bits 0-2
90 }
91
94 constexpr static uint8_t layerFromKey(unsigned short key) {
95 return (key>>REGION_BITS) & LAYER_MASK; // bits 3-8
96 }
97
98 // To select, hits, outliers, and/or shared hits.
106
109 void reset() {
110 m_stat.clear();
111 for(unsigned int i=0;i<static_cast<unsigned int>(CountType::NCountTypes); ++i) {
112 std::fill(m_hits[i].begin(),m_hits[i].end(), 0u);
113 }
114 std::fill(m_layers.begin(),m_layers.end(), 0u);
115 }
116
126 EHitSelection hit_selection) {
127 DetectorRegion region = unknown;
128 uint8_t layer = 255;
129 int eta_module = 0;
130 Identifier id(detEl ? detEl->identify() : Identifier() );
131 switch (det_type) {
133 if (!detEl) { return false; }
134 assert(detEl ->getIdHelper()->is_pixel(id));
137 else if(type==InDetDD::PixelBarrel) region = pixelBarrelFlat;
138 else region = pixelEndcap;
139
140 const PixelID* pixel_id = static_cast<const PixelID *>(detEl->getIdHelper());
141 layer = pixel_id->layer_disk(detEl->identify());
142 eta_module = pixel_id->eta_module(detEl->identify());
143 break;
144 }
146 if (!detEl) { return false; }
147 assert(detEl ->getIdHelper()->is_sct(id));
148 const SCT_ID* sct_id = static_cast<const SCT_ID *>(detEl->getIdHelper());
149 region = (sct_id->barrel_ec(id)==0 ? stripBarrel : stripEndcap);
150
151 layer = sct_id->layer_disk(detEl->identify());
152 eta_module = sct_id->eta_module(detEl->identify());
153 break;
154 }
156 if (!detEl) { return false; }
157 assert(detEl ->getIdHelper()->is_hgtd(id));
158 const HGTD_ID* hgtd_id = static_cast<const HGTD_ID *>(detEl->getIdHelper());
159 region=hgtdTotal;
160 layer=hgtd_id->layer(id);
161 break;
162 }
163 default: {
164 return false;
165 }
166 }
167
168 unsigned short key = makeKey(region, layer, eta_module);
169 for (auto &[stat_key, stat_hits, stat_outlier_hits, stat_shared_hits, stat_split_hits] : m_stat) {
170 if (stat_key == key) {
171 stat_hits += ((hit_selection & HitSummaryData::HitFlag)!=0);
172 stat_outlier_hits += ((hit_selection & HitSummaryData::OutlierFlag)!=0);
173 stat_shared_hits += ((hit_selection & HitSummaryData::SharedHitFlag)!=0);
174 stat_split_hits += ((hit_selection & HitSummaryData::SplitHitFlag)!=0);
175 return true;
176 }
177 }
178 m_stat.emplace_back( std::make_tuple(key,
179 ((hit_selection & HitSummaryData::HitFlag)!=0),
180 ((hit_selection & HitSummaryData::OutlierFlag)!=0),
181 ((hit_selection & HitSummaryData::SharedHitFlag)!=0),
182 ((hit_selection & HitSummaryData::SplitHitFlag)!=0) ));
183 return true;
184 }
185
190 for (const auto &[stat_key, stat_hits, stat_outlier_hits, stat_shared_hits, stat_split_hits] : m_stat) {
191 unsigned short region=regionFromKey(stat_key);
192 m_hits[static_cast<unsigned int>(CountType::Hit)].at(region) += stat_hits;
193 m_hits[static_cast<unsigned int>(CountType::Outlier)].at(region) += stat_outlier_hits;
194 m_hits[static_cast<unsigned int>(CountType::SharedHit)].at(region) += stat_shared_hits;
195 m_hits[static_cast<unsigned int>(CountType::SplitHit)].at(region) += stat_split_hits;
196 ++m_layers.at(region);
197 }
198 for (unsigned int region_i=0; region_i<unknown+1; ++region_i) {
199 for (unsigned int count_type_i=0; count_type_i<static_cast<unsigned int>(CountType::NCountTypes);++count_type_i) {
200 m_hits[count_type_i].at(s_type.at(region_i)) += m_hits[count_type_i][region_i];
201 m_hits[count_type_i].at(Total) += m_hits[count_type_i][region_i];
202 }
203 m_layers.at(s_type.at(region_i)) += m_layers[region_i];
204 m_layers.at(Total) += m_layers[region_i];
205 }
207 for (unsigned int count_type_i=0; count_type_i<static_cast<unsigned int>(CountType::NCountTypes);++count_type_i) {
208 assert( pixelBarrelFlat < m_hits[count_type_i].size() && pixelBarrelInclined < m_hits[count_type_i].size());
209 m_hits[count_type_i].at(pixelBarrel) = m_hits[count_type_i][pixelBarrelFlat]+m_hits[count_type_i][pixelBarrelInclined];
210 }
211 }
212
217 uint8_t contributingLayers(DetectorRegion region) const {
218 return m_layers.at(region);
219 }
220
225 uint8_t contributingHits(DetectorRegion region, CountType hit_type=CountType::Hit) const {
226 assert( static_cast<unsigned int>(hit_type) < static_cast<unsigned int>(CountType::NCountTypes));
227 return m_hits[static_cast<unsigned int>(hit_type)].at(region);
228 }
229
235 return contributingHits(region, CountType::Outlier);
236 }
237
244 }
245
246
251 template <unsigned short HIT_SELECTION>
252 uint8_t sum(DetectorRegion region, uint8_t layer) const {
253 uint8_t total=0u;
254 unsigned short key = makeKey(region, layer, 0);
255 for (const auto &[stat_key, stat_hits, stat_outlier_hits, stat_shared_hits, stat_split_hits] : m_stat) {
256 if ((stat_key & LAYER_REGION_MASK) == key) {
257 if constexpr(HIT_SELECTION & HitSummaryData::HitFlag) {
258 total += stat_hits;
259 }
260 if constexpr(HIT_SELECTION & HitSummaryData::OutlierFlag) {
261 total += stat_outlier_hits;
262 }
263 if constexpr(HIT_SELECTION & HitSummaryData::SharedHitFlag) {
264 total += stat_shared_hits;
265 }
266 if constexpr(HIT_SELECTION & HitSummaryData::SplitHitFlag) {
267 total += stat_split_hits;
268 }
269 }
270 }
271 return total;
272 }
273
277 std::array<uint8_t,4> sumPerCountType(DetectorRegion region, uint8_t layer) const {
278 std::array<uint8_t,4> total{};
279 unsigned short key = makeKey(region, layer, 0);
280 for (const auto &[stat_key, stat_hits, stat_outlier_hits, stat_shared_hits, stat_split_hits] : m_stat) {
281 if ((stat_key & LAYER_REGION_MASK) == key) {
282 total[static_cast<unsigned int>(CountType::Hit)] += stat_hits;
283 total[static_cast<unsigned int>(CountType::Outlier)] += stat_outlier_hits;
284 total[static_cast<unsigned int>(CountType::SharedHit)] += stat_shared_hits;
285 total[static_cast<unsigned int>(CountType::SplitHit)] += stat_split_hits;
286 }
287 }
288 return total;
289 }
290
296 unsigned int layerPattern(DetectorRegion region, bool include_outlier) const {
297 unsigned int layer_pattern=0u;
298 unsigned short key = makeKey(region);
299 std::array<uint8_t,2> stat_mask{ 0xff, static_cast<uint8_t>(include_outlier ? 0xff : 0) };
300 for (const auto &[stat_key, stat_hits, stat_outlier_hits, stat_shared_hits, stat_split_hits] : m_stat) {
301 if ((stat_key & REGION_MASK) == key) {
302 if ((stat_hits & stat_mask[0])+(stat_outlier_hits & stat_mask[1]) >0u) {
303 layer_pattern |= 1u<<layerFromKey(stat_key);
304 }
305 }
306 }
307 return layer_pattern;
308 }
309
310 private:
311 std::vector< std::tuple<unsigned short, uint8_t, uint8_t, uint8_t, uint8_t> > m_stat;
312 std::array<std::array<uint8_t, Total+1>,static_cast<unsigned int>(CountType::NCountTypes)> m_hits{};
313 std::array<uint8_t, Total+1> m_layers{};
314 static constexpr std::array<uint8_t, unknown+1> s_type
316 };
317
321 private:
322 double m_sum = 0.;
323 double m_sum2 = 0.;
324 unsigned int m_n =0u;
325 public:
326 void reset() {
327 m_sum=0.;
328 m_sum2=0.;
329 m_n=0u;
330 }
331 void add(double value) {
332 m_sum += value;
333 m_sum2 += value * value;
334 ++m_n;
335 }
336 std::array<double,2> meanAndBiasedVariance() const {
337 double inv_n = m_n>0 ? 1/m_n : 0 ;
338 double mean = m_sum * inv_n;
339 return std::array<double, 2> { mean, (m_sum2 - m_sum * mean) * inv_n };
340 }
341 double biasedVariance() const {
342 double inv_n = m_n>0 ? 1./m_n : 0 ;
343 return (m_sum2 - m_sum * m_sum *inv_n) * inv_n;
344 }
345 };
346
347 struct TimeInfo {
348 float mean;
350 double chi2;
351 };
352
362 void gatherTrackSummaryData(const typename ActsTrk::TrackContainer::ConstTrackProxy &track,
363 const std::array<unsigned short,Acts::toUnderlying(xAOD::UncalibMeasType::nTypes)>
364 &measurement_to_summary_type,
365 SumOfValues &chi2_stat_out,
366 HitSummaryData &hit_info_out,
367 std::vector<ActsTrk::TrackStateBackend::ConstTrackStateProxy::IndexType > &param_state_idx_out,
368 std::array<std::array<uint8_t,Acts::toUnderlying(HitCategory::N)>,
369 Acts::toUnderlying(xAOD::UncalibMeasType::nTypes)> &special_hit_counts_out,
370 TimeInfo &time_info);
371
372}
373#endif
This is an Identifier helper class for the Pixel subdetector.
This is an Identifier helper class for the SCT subdetector.
size_t size() const
Number of registered mappings.
Helper class to gather hit summary information for e.g.
std::vector< std::tuple< unsigned short, uint8_t, uint8_t, uint8_t, uint8_t > > m_stat
unsigned int layerPattern(DetectorRegion region, bool include_outlier) const
Get a bit pattern with one bit set per layer which is set if the layer has a hit or optionally an out...
void reset()
reset all summary counters to zero.
static constexpr unsigned short REGION_MASK
std::array< uint8_t, Total+1 > m_layers
static constexpr DetectorRegion regionFromKey(unsigned short key)
extract the region index from the given key.
DetectorRegion
Regions for which hit counts are computed.
std::array< uint8_t, 4 > sumPerCountType(DetectorRegion region, uint8_t layer) const
return the total number of hits, outliers, shared hits and split hits in the given detector region an...
static constexpr unsigned short LAYER_BITS
static constexpr std::array< uint8_t, unknown+1 > s_type
std::array< std::array< uint8_t, Total+1 >, static_cast< unsigned int >(CountType::NCountTypes)> m_hits
uint8_t contributingSharedHits(DetectorRegion region) const
return the number of shared hits in a certain detector region.
static constexpr unsigned short SIGNED_ETA_MOD_MASK
void computeSummaries()
Compute the varius summaries.
static constexpr unsigned short REGION_BITS
static constexpr unsigned short LAYER_MASK
uint8_t contributingHits(DetectorRegion region, CountType hit_type=CountType::Hit) const
return the number of hits in a certain detector region.
static constexpr uint8_t layerFromKey(unsigned short key)
extract the layer index from the given key.
static constexpr unsigned short makeKey(unsigned short region)
static constexpr unsigned short makeKey(unsigned short region, unsigned short layer, int eta_mod)
Compute a counter key for the given region, layer and module eta module index.
uint8_t sum(DetectorRegion region, uint8_t layer) const
return the total number of hits, outliers, shared hits and/or split hits in the given detector region...
uint8_t contributingOutlierHits(DetectorRegion region) const
return the number of outliers in a certain detector region.
static constexpr unsigned short LAYER_REGION_MASK
static constexpr unsigned short SIGNED_ETA_MOD_BITS
bool addHit(xAOD::UncalibMeasType det_type, const InDetDD::SolidStateDetectorElementBase *detEl, EHitSelection hit_selection)
update summaries to take the given hit into account.
uint8_t contributingLayers(DetectorRegion region) const
return the number of layers contributing to the hit collection in the given detector region.
Helper class to gather statistics and compute the biased variance.
std::array< double, 2 > meanAndBiasedVariance() const
This is an Identifier helper class for the HGTD subdetector.
Definition HGTD_ID.h:47
int layer(const Identifier &id) const
Definition HGTD_ID.h:477
virtual DetectorType type() const
Type of element.
Class to hold geometrical description of a solid state detector element.
virtual const DetectorDesign & design() const
access to the local description (inline):
virtual Identifier identify() const override final
identifier of this detector element (inline)
const AtlasDetectorID * getIdHelper() const
Returns the id helper (inline).
This is an Identifier helper class for the Pixel subdetector.
Definition PixelID.h:69
int layer_disk(const Identifier &id) const
Definition PixelID.h:602
int eta_module(const Identifier &id) const
Definition PixelID.h:627
This is an Identifier helper class for the SCT subdetector.
Definition SCT_ID.h:68
int layer_disk(const Identifier &id) const
Definition SCT_ID.h:687
int barrel_ec(const Identifier &id) const
Values of different levels (failure returns 0).
Definition SCT_ID.h:681
int eta_module(const Identifier &id) const
Definition SCT_ID.h:699
void mean(std::vector< double > &bins, std::vector< double > &values, const std::vector< std::string > &files, const std::string &histname, const std::string &tplotname, const std::string &label="")
Hash functions to pack the source link into unordered_maps / unordered_sets.
void gatherTrackSummaryData(const typename ActsTrk::TrackContainer::ConstTrackProxy &track, const std::array< unsigned short, Acts::toUnderlying(xAOD::UncalibMeasType::nTypes)> &measurement_to_summary_type, SumOfValues &chi2_stat_out, HitSummaryData &hit_info_out, std::vector< ActsTrk::TrackStateBackend::ConstTrackStateProxy::IndexType > &param_state_idx_out, std::array< std::array< uint8_t, Acts::toUnderlying(HitCategory::N)>, Acts::toUnderlying(xAOD::UncalibMeasType::nTypes)> &special_hit_counts_out, TimeInfo &time_info)
Helper to gather track summary information from the track states of the specified track.
UncalibMeasType
Define the type of the uncalibrated measurement.