ATLAS Offline Software
Loading...
Searching...
No Matches
HitSummaryDataUtils.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3*/
4
5#ifndef ACTSTRK_HITSUMMARYDATAUTILS_H
6#define ACTSTRK_HITSUMMARYDATAUTILS_H
7
13
14#include <vector>
15#include <cmath>
16#include <array>
17#include <tuple>
18#include <type_traits>
19
20namespace ActsTrk::detail {
23 template <typename T_EnumClass >
24 constexpr typename std::underlying_type<T_EnumClass>::type to_underlying(T_EnumClass an_enum) {
25 return static_cast<typename std::underlying_type<T_EnumClass>::type>(an_enum);
26 }
27
35
39 public:
54
55 constexpr static unsigned short LAYER_REGION_MASK = 0x1FF; // bits 0-8
56 constexpr static unsigned short REGION_BITS = 3; // bits 0-2
57 constexpr static unsigned short REGION_MASK = 0x7; // 3 bits
58 constexpr static unsigned short LAYER_BITS = 6; // bits 3-8
59 constexpr static unsigned short LAYER_MASK = 0x3F; // 6 bits
60 constexpr static unsigned short SIGNED_ETA_MOD_BITS = 7; // bits 9-14 + 15(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
84 constexpr static DetectorRegion regionFromKey(unsigned short key) {
85 return static_cast<DetectorRegion>(key & REGION_MASK); // bits 0-2
86 }
87
90 constexpr static uint8_t layerFromKey(unsigned short key) {
91 return (key>>REGION_BITS) & LAYER_MASK; // bits 3-8
92 }
93
94 // To select, hits, outliers, and/or shared hits.
101
104 void reset() {
105 m_stat.clear();
106 std::fill(m_hits.begin(),m_hits.end(), 0u);
107 std::fill(m_outlierHits.begin(),m_outlierHits.end(), 0u);
108 std::fill(m_sharedHits.begin(),m_sharedHits.end(), 0u);
109 std::fill(m_layers.begin(),m_layers.end(), 0u);
110 }
111
119 bool addHit(const InDetDD::SiDetectorElementCollection *detector_elements, unsigned int id_hash, EHitSelection hit_selection) {
120 if (!detector_elements || id_hash>=detector_elements->size() || !(*detector_elements)[id_hash]) {
121 return false;
122 }
123 const InDetDD::SiDetectorElement *detEl=(*detector_elements)[id_hash];
124 DetectorRegion region = unknown;
125 uint8_t layer = 255;
126 int eta_module = 0;
127 if (detEl->isPixel()) {
130 else if(type==InDetDD::PixelBarrel) region = pixelBarrelFlat;
131 else region = pixelEndcap;
132
133 const PixelID* pixel_id = static_cast<const PixelID *>(detEl->getIdHelper());
134 layer = pixel_id->layer_disk(detEl->identify());
135 eta_module = pixel_id->eta_module(detEl->identify());
136 }
137 else if (detEl->isSCT()) {
138 region = (detEl->isBarrel() ? stripBarrel : stripEndcap);
139
140 const SCT_ID* sct_id = static_cast<const SCT_ID *>(detEl->getIdHelper());
141 layer = sct_id->layer_disk(detEl->identify());
142 eta_module = sct_id->eta_module(detEl->identify());
143 }
144
145 unsigned short key = makeKey(region, layer, eta_module);
146 for (auto &[stat_key, stat_hits, stat_outlier_hits, stat_shared_hits] : m_stat) {
147 if (stat_key == key) {
148 stat_hits += ((hit_selection & HitSummaryData::Hit)!=0);
149 stat_outlier_hits += ((hit_selection & HitSummaryData::Outlier)!=0);
150 stat_shared_hits += ((hit_selection & HitSummaryData::SharedHit)!=0);
151 return true;
152 }
153 }
154 m_stat.emplace_back( std::make_tuple(key,
155 ((hit_selection & HitSummaryData::Hit)!=0),
156 ((hit_selection & HitSummaryData::Outlier)!=0),
157 ((hit_selection & HitSummaryData::SharedHit)!=0)) );
158 return true;
159 }
160
165 for (const auto &[stat_key, stat_hits, stat_outlier_hits, stat_shared_hits] : m_stat) {
166 unsigned short region=regionFromKey(stat_key);
167 m_hits.at(region) += stat_hits;
168 m_outlierHits.at(region) += stat_outlier_hits;
169 m_sharedHits.at(region) += stat_shared_hits;
170 ++m_layers.at(region);
171 }
172 for (unsigned int region_i=0; region_i<unknown+1; ++region_i) {
173 m_hits.at(s_type.at(region_i)) += m_hits[region_i];
174 m_outlierHits.at(s_type.at(region_i)) += m_outlierHits[region_i];
175 m_sharedHits.at(s_type.at(region_i)) += m_sharedHits[region_i];
176 m_layers.at(s_type.at(region_i)) += m_layers[region_i];
177 m_hits.at(Total) += m_hits[region_i];
178 m_outlierHits.at(Total) += m_outlierHits[region_i];
179 m_sharedHits.at(Total) += m_sharedHits[region_i];
180 m_layers.at(Total) += m_layers[region_i];
181 }
182 }
183
188 uint8_t contributingLayers(DetectorRegion region) const {
189 return m_layers.at(region);
190 }
191
196 uint8_t contributingHits(DetectorRegion region) const {
197 return m_hits.at(region);
198 }
199
205 return m_outlierHits.at(region);
206 }
207
213 return m_sharedHits.at(region);
214 }
215
216
221 template <unsigned short HIT_SELECTION>
222 uint8_t sum(DetectorRegion region, uint8_t layer) const {
223 uint8_t total=0u;
224 unsigned short key = makeKey(region, layer, 0);
225 for (const auto &[stat_key, stat_hits, stat_outlier_hits, stat_shared_hits] : m_stat) {
226 if ((stat_key & LAYER_REGION_MASK) == key) {
227 if constexpr(HIT_SELECTION & HitSummaryData::Hit) {
228 total += stat_hits;
229 }
230 if constexpr(HIT_SELECTION & HitSummaryData::Outlier) {
231 total += stat_outlier_hits;
232 }
233 if constexpr(HIT_SELECTION & HitSummaryData::SharedHit) {
234 total += stat_shared_hits;
235 }
236 }
237 }
238 return total;
239 }
240
241 private:
242 std::vector< std::tuple<unsigned short, uint8_t, uint8_t, uint8_t> > m_stat;
243 std::array<uint8_t, Total+1> m_hits;
244 std::array<uint8_t, Total+1> m_outlierHits;
245 std::array<uint8_t, Total+1> m_sharedHits;
246 std::array<uint8_t, Total+1> m_layers;
247 static constexpr std::array<uint8_t, unknown+1> s_type
249 };
250
254 private:
255 double m_sum = 0.;
256 double m_sum2 = 0.;
257 unsigned int m_n =0u;
258 public:
259 void reset() {
260 m_sum=0.;
261 m_sum2=0.;
262 m_n=0u;
263 }
264 void add(double value) {
265 m_sum += value;
266 m_sum2 += value * value;
267 ++m_n;
268 }
269 std::array<double,2> meanAndBiasedVariance() const {
270 double inv_n = m_n>0 ? 1/m_n : 0 ;
271 double mean = m_sum * inv_n;
272 return std::array<double, 2> { mean, (m_sum2 - m_sum * mean) * inv_n };
273 }
274 double biasedVariance() const {
275 double inv_n = m_n>0 ? 1./m_n : 0 ;
276 return (m_sum2 - m_sum * m_sum *inv_n) * inv_n;
277 }
278 };
279
290 void gatherTrackSummaryData(const ActsTrk::TrackContainer &tracksContainer,
291 const typename ActsTrk::TrackContainer::ConstTrackProxy &track,
292 const std::array<const InDetDD::SiDetectorElementCollection *,
294 const std::array<unsigned short,to_underlying(xAOD::UncalibMeasType::nTypes)>
295 &measurement_to_summary_type,
296 SumOfValues &chi2_stat_out,
297 HitSummaryData &hit_info_out,
298 std::vector<ActsTrk::TrackStateBackend::ConstTrackStateProxy::IndexType > &param_state_idx_out,
299 std::array<std::array<uint8_t,to_underlying(HitCategory::N)>,
300 to_underlying(xAOD::UncalibMeasType::nTypes)> &special_hit_counts_out);
301
302}
303#endif
This is an Identifier helper class for the Pixel subdetector.
This is an Identifier helper class for the SCT subdetector.
Helper class to gather hit summary information for e.g.
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
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
bool addHit(const InDetDD::SiDetectorElementCollection *detector_elements, unsigned int id_hash, EHitSelection hit_selection)
update summaries to take the given hit into account.
std::array< uint8_t, Total+1 > m_hits
void computeSummaries()
Compute the varius summaries.
static constexpr unsigned short REGION_BITS
static constexpr unsigned short LAYER_MASK
static constexpr uint8_t layerFromKey(unsigned short key)
extract the layer index from the given key.
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.
uint8_t contributingHits(DetectorRegion region) const
return the number of hits in a certain detector region.
std::vector< std::tuple< unsigned short, uint8_t, uint8_t, uint8_t > > m_stat
std::array< uint8_t, Total+1 > m_outlierHits
static constexpr unsigned short LAYER_REGION_MASK
static constexpr unsigned short SIGNED_ETA_MOD_BITS
std::array< uint8_t, Total+1 > m_sharedHits
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
size_type size() const noexcept
Returns the number of elements in the collection.
virtual DetectorType type() const
Type of element.
Class to hold the SiDetectorElement objects to be put in the detector store.
Class to hold geometrical description of a silicon detector element.
virtual const SiDetectorDesign & design() const override final
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:67
int layer_disk(const Identifier &id) const
Definition PixelID.h:607
int eta_module(const Identifier &id) const
Definition PixelID.h:632
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 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="")
Athena definition of the Eigen plugin.
void gatherTrackSummaryData(const ActsTrk::TrackContainer &tracksContainer, const typename ActsTrk::TrackContainer::ConstTrackProxy &track, const std::array< const InDetDD::SiDetectorElementCollection *, to_underlying(xAOD::UncalibMeasType::nTypes)> &siDetEleColl, const std::array< unsigned short, to_underlying(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, to_underlying(HitCategory::N)>, to_underlying(xAOD::UncalibMeasType::nTypes)> &special_hit_counts_out)
Helper to gather track summary information from the track states of the specified track.
constexpr std::underlying_type< T_EnumClass >::type to_underlying(T_EnumClass an_enum)
Helper to convert class enum into an integer.