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
53
54 constexpr static unsigned short LAYER_REGION_MASK = 0xFF; // bits 0-7
55 constexpr static unsigned short REGION_BITS = 4; // bits 0-3
56 constexpr static unsigned short REGION_MASK = 0xF; // 4 bits
57 constexpr static unsigned short LAYER_BITS = 4; // bits 4-7
58 constexpr static unsigned short LAYER_MASK = 0xF; // 4 bits
59 constexpr static unsigned short SIGNED_ETA_MOD_BITS = 7; // bits 8-13 + 14(sign)
60 constexpr static unsigned short SIGNED_ETA_MOD_MASK = 0x7F; // 6 + 1(sign) bit
61
67 constexpr static unsigned short makeKey(unsigned short region, unsigned short layer, int eta_mod) {
68 // 3 bits region : 0-7 : pixelBarrelFlat - unknown
69 // 6 bits layer : 0-63
70 // 1+6 bits eta_mod : +- 0-63
71 // @TODO endcap side A/C ?
72 assert(region < (1<<REGION_BITS) );
73 assert(layer < (1<<LAYER_BITS));
74 assert( std::abs(eta_mod) < (1<<(SIGNED_ETA_MOD_BITS-1)) );
75 if (region != stripBarrel && region != pixelBarrelFlat) {
76 layer |= (static_cast<uint8_t>(eta_mod) & SIGNED_ETA_MOD_MASK) << LAYER_BITS ;
77 }
78 return static_cast<uint8_t>(region) | (layer<<REGION_BITS);
79 }
80 constexpr static unsigned short makeKey(unsigned short region) {
81 assert(region < (1<<REGION_BITS) );
82 return static_cast<uint8_t>(region);
83 }
84
87 constexpr static DetectorRegion regionFromKey(unsigned short key) {
88 return static_cast<DetectorRegion>(key & REGION_MASK); // bits 0-2
89 }
90
93 constexpr static uint8_t layerFromKey(unsigned short key) {
94 return (key>>REGION_BITS) & LAYER_MASK; // bits 3-8
95 }
96
97 // To select, hits, outliers, and/or shared hits.
104
107 void reset() {
108 m_stat.clear();
109 for(unsigned int i=0;i<static_cast<unsigned int>(CountType::NCountTypes); ++i) {
110 std::fill(m_hits[i].begin(),m_hits[i].end(), 0u);
111 }
112 std::fill(m_layers.begin(),m_layers.end(), 0u);
113 }
114
124 EHitSelection hit_selection) {
125 DetectorRegion region = unknown;
126 uint8_t layer = 255;
127 int eta_module = 0;
128 Identifier id(detEl ? detEl->identify() : Identifier() );
129 switch (det_type) {
131 if (!detEl) { return false; }
132 assert(detEl ->getIdHelper()->is_pixel(id));
135 else if(type==InDetDD::PixelBarrel) region = pixelBarrelFlat;
136 else region = pixelEndcap;
137
138 const PixelID* pixel_id = static_cast<const PixelID *>(detEl->getIdHelper());
139 layer = pixel_id->layer_disk(detEl->identify());
140 eta_module = pixel_id->eta_module(detEl->identify());
141 break;
142 }
144 if (!detEl) { return false; }
145 assert(detEl ->getIdHelper()->is_sct(id));
146 const SCT_ID* sct_id = static_cast<const SCT_ID *>(detEl->getIdHelper());
147 region = (sct_id->barrel_ec(id)==0 ? stripBarrel : stripEndcap);
148
149 layer = sct_id->layer_disk(detEl->identify());
150 eta_module = sct_id->eta_module(detEl->identify());
151 break;
152 }
154 if (!detEl) { return false; }
155 assert(detEl ->getIdHelper()->is_hgtd(id));
156 const HGTD_ID* hgtd_id = static_cast<const HGTD_ID *>(detEl->getIdHelper());
157 region=hgtdTotal;
158 layer=hgtd_id->layer(id);
159 break;
160 }
161 default: {
162 return false;
163 }
164 }
165
166 unsigned short key = makeKey(region, layer, eta_module);
167 for (auto &[stat_key, stat_hits, stat_outlier_hits, stat_shared_hits] : m_stat) {
168 if (stat_key == key) {
169 stat_hits += ((hit_selection & HitSummaryData::Hit)!=0);
170 stat_outlier_hits += ((hit_selection & HitSummaryData::Outlier)!=0);
171 stat_shared_hits += ((hit_selection & HitSummaryData::SharedHit)!=0);
172 return true;
173 }
174 }
175 m_stat.emplace_back( std::make_tuple(key,
176 ((hit_selection & HitSummaryData::Hit)!=0),
177 ((hit_selection & HitSummaryData::Outlier)!=0),
178 ((hit_selection & HitSummaryData::SharedHit)!=0)) );
179 return true;
180 }
181
186 for (const auto &[stat_key, stat_hits, stat_outlier_hits, stat_shared_hits] : m_stat) {
187 unsigned short region=regionFromKey(stat_key);
188 m_hits[static_cast<unsigned int>(CountType::Hit)].at(region) += stat_hits;
189 m_hits[static_cast<unsigned int>(CountType::Outlier)].at(region) += stat_outlier_hits;
190 m_hits[static_cast<unsigned int>(CountType::SharedHit)].at(region) += stat_shared_hits;
191 ++m_layers.at(region);
192 }
193 for (unsigned int region_i=0; region_i<unknown+1; ++region_i) {
194 for (unsigned int count_type_i=0; count_type_i<static_cast<unsigned int>(CountType::NCountTypes);++count_type_i) {
195 m_hits[count_type_i].at(s_type.at(region_i)) += m_hits[count_type_i][region_i];
196 m_hits[count_type_i].at(Total) += m_hits[count_type_i][region_i];
197 }
198 m_layers.at(s_type.at(region_i)) += m_layers[region_i];
199 m_layers.at(Total) += m_layers[region_i];
200 }
202 for (unsigned int count_type_i=0; count_type_i<static_cast<unsigned int>(CountType::NCountTypes);++count_type_i) {
203 assert( pixelBarrelFlat < m_hits[count_type_i].size() && pixelBarrelInclined < m_hits[count_type_i].size());
204 m_hits[count_type_i].at(pixelBarrel) = m_hits[count_type_i][pixelBarrelFlat]+m_hits[count_type_i][pixelBarrelInclined];
205 }
206 }
207
212 uint8_t contributingLayers(DetectorRegion region) const {
213 return m_layers.at(region);
214 }
215
220 uint8_t contributingHits(DetectorRegion region, CountType hit_type=CountType::Hit) const {
221 assert( static_cast<unsigned int>(hit_type) < static_cast<unsigned int>(CountType::NCountTypes));
222 return m_hits[static_cast<unsigned int>(hit_type)].at(region);
223 }
224
230 return contributingHits(region, CountType::Outlier);
231 }
232
239 }
240
241
246 template <unsigned short HIT_SELECTION>
247 uint8_t sum(DetectorRegion region, uint8_t layer) const {
248 uint8_t total=0u;
249 unsigned short key = makeKey(region, layer, 0);
250 for (const auto &[stat_key, stat_hits, stat_outlier_hits, stat_shared_hits] : m_stat) {
251 if ((stat_key & LAYER_REGION_MASK) == key) {
252 if constexpr(HIT_SELECTION & HitSummaryData::Hit) {
253 total += stat_hits;
254 }
255 if constexpr(HIT_SELECTION & HitSummaryData::Outlier) {
256 total += stat_outlier_hits;
257 }
258 if constexpr(HIT_SELECTION & HitSummaryData::SharedHit) {
259 total += stat_shared_hits;
260 }
261 }
262 }
263 return total;
264 }
265 unsigned int layerPattern(DetectorRegion region, bool include_outlier) const {
266 unsigned int layer_pattern=0u;
267 unsigned short key = makeKey(region);
268 std::array<uint8_t,2> stat_mask{ 0xff, static_cast<uint8_t>(include_outlier ? 0xff : 0) };
269 for (const auto &[stat_key, stat_hits, stat_outlier_hits, stat_shared_hits] : m_stat) {
270 if ((stat_key & REGION_MASK) == key) {
271 if ((stat_hits & stat_mask[0])+(stat_outlier_hits & stat_mask[1]) >0u) {
272 layer_pattern |= 1u<<layerFromKey(stat_key);
273 }
274 }
275 }
276 return layer_pattern;
277 }
278
279 private:
280 std::vector< std::tuple<unsigned short, uint8_t, uint8_t, uint8_t> > m_stat;
281 std::array<std::array<uint8_t, Total+1>,static_cast<unsigned int>(CountType::NCountTypes)> m_hits{};
282 std::array<uint8_t, Total+1> m_layers{};
283 static constexpr std::array<uint8_t, unknown+1> s_type
285 };
286
290 private:
291 double m_sum = 0.;
292 double m_sum2 = 0.;
293 unsigned int m_n =0u;
294 public:
295 void reset() {
296 m_sum=0.;
297 m_sum2=0.;
298 m_n=0u;
299 }
300 void add(double value) {
301 m_sum += value;
302 m_sum2 += value * value;
303 ++m_n;
304 }
305 std::array<double,2> meanAndBiasedVariance() const {
306 double inv_n = m_n>0 ? 1/m_n : 0 ;
307 double mean = m_sum * inv_n;
308 return std::array<double, 2> { mean, (m_sum2 - m_sum * mean) * inv_n };
309 }
310 double biasedVariance() const {
311 double inv_n = m_n>0 ? 1./m_n : 0 ;
312 return (m_sum2 - m_sum * m_sum *inv_n) * inv_n;
313 }
314 };
315
316 struct TimeInfo {
317 float mean;
319 double chi2;
320 };
321
331 void gatherTrackSummaryData(const typename ActsTrk::TrackContainer::ConstTrackProxy &track,
332 const std::array<unsigned short,Acts::toUnderlying(xAOD::UncalibMeasType::nTypes)>
333 &measurement_to_summary_type,
334 SumOfValues &chi2_stat_out,
335 HitSummaryData &hit_info_out,
336 std::vector<ActsTrk::TrackStateBackend::ConstTrackStateProxy::IndexType > &param_state_idx_out,
337 std::array<std::array<uint8_t,Acts::toUnderlying(HitCategory::N)>,
338 Acts::toUnderlying(xAOD::UncalibMeasType::nTypes)> &special_hit_counts_out,
339 TimeInfo &time_info);
340
341}
342#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.
unsigned int layerPattern(DetectorRegion region, bool include_outlier) const
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.
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, and/or shared hits in the givrn detector region and layer.
uint8_t contributingOutlierHits(DetectorRegion region) const
return the number of outliers in a certain detector region.
std::vector< std::tuple< unsigned short, uint8_t, uint8_t, uint8_t > > m_stat
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.