ATLAS Offline Software
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 #ifndef ACTSTRK_HITSUMMARYDATAUTILS_H
5 #define ACTSTRK_HITSUMMARYDATAUTILS_H 1
6 
10 
11 #include <vector>
12 #include <cmath>
13 #include <array>
14 #include <tuple>
15 
16 namespace InDetDD {
17  class SiDetectorElementCollection;
18 }
19 
20 namespace ActsTrk {
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 
28  namespace HitCategory {
32  N
33  };
34  }
35 
39  public:
48  unknown = 5,
52  Total = 9
53  };
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 or both.
96  Hit = 1,
97  Outlier = 2,
98  HitAndOutlier = 3
99  };
100 
103  void reset() {
104  m_stat.clear();
105  std::fill(m_hits.begin(),m_hits.end(), 0u);
106  std::fill(m_outlierHits.begin(),m_outlierHits.end(), 0u);
107  std::fill(m_layers.begin(),m_layers.end(), 0u);
108  }
109 
117  bool addHit(const InDetDD::SiDetectorElementCollection *detector_elements, unsigned int id_hash, EHitSelection hit_selection) {
118  if (!detector_elements || id_hash>=detector_elements->size() || !(*detector_elements)[id_hash]) {
119  return false;
120  }
121  const InDetDD::SiDetectorElement *detEl=(*detector_elements)[id_hash];
122  DetectorRegion region = unknown;
123  uint8_t layer = 255;
124  int eta_module = 0;
125  if (detEl->isPixel()) {
126  InDetDD::DetectorType type = detEl->design().type();
128  else if(type==InDetDD::PixelBarrel) region = pixelBarrelFlat;
129  else region = pixelEndcap;
130 
131  const PixelID* pixel_id = static_cast<const PixelID *>(detEl->getIdHelper());
132  layer = pixel_id->layer_disk(detEl->identify());
133  eta_module = pixel_id->eta_module(detEl->identify());
134  }
135  else if (detEl->isSCT()) {
136  region = (detEl->isBarrel() ? stripBarrel : stripEndcap);
137 
138  const SCT_ID* sct_id = static_cast<const SCT_ID *>(detEl->getIdHelper());
139  layer = sct_id->layer_disk(detEl->identify());
140  eta_module = sct_id->eta_module(detEl->identify());
141  }
142 
143  unsigned short key = makeKey(region, layer, eta_module);
144  for (auto &[stat_key, stat_hits, stat_outlier_hits] : m_stat) {
145  if (stat_key == key) {
146  stat_hits += ((hit_selection & HitSummaryData::Hit)!=0);
147  stat_outlier_hits += ((hit_selection & HitSummaryData::Outlier)!=0);
148  return true;
149  }
150  }
151  m_stat.emplace_back( std::make_tuple(key,
152  ((hit_selection & HitSummaryData::Hit)!=0),
153  ((hit_selection & HitSummaryData::Outlier)!=0)) );
154  return true;
155  }
156 
161  for (const auto &[stat_key, stat_hits, stat_outlier_hits] : m_stat) {
162  unsigned short region=regionFromKey(stat_key);
163  m_hits.at(region) += stat_hits;
164  m_outlierHits.at(region) += stat_outlier_hits;
165  ++m_layers.at(region);
166  }
167  for (unsigned int region_i=0; region_i<unknown+1; ++region_i) {
168  m_hits.at(s_type.at(region_i)) += m_hits[region_i];
169  m_outlierHits.at(s_type.at(region_i)) += m_outlierHits[region_i];
170  m_layers.at(s_type.at(region_i)) += m_layers[region_i];
171  m_hits.at(Total) += m_hits[region_i];
172  m_outlierHits.at(Total) += m_outlierHits[region_i];
173  m_layers.at(Total) += m_layers[region_i];
174  }
175  }
176 
182  return m_layers.at(region);
183  }
184 
190  return m_hits.at(region);
191  }
192 
198  return m_outlierHits.at(region);
199  }
200 
201 
206  template <unsigned short HIT_SELECTION>
208  uint8_t total=0u;
209  unsigned short key = makeKey(region, layer, 0);
210  for (auto &[stat_key, stat_hits, stat_outlier_hits] : m_stat) {
211  if ((stat_key & LAYER_REGION_MASK) == key) {
212  if constexpr(HIT_SELECTION & HitSummaryData::Hit) {
213  total += stat_hits;
214  }
215  if constexpr(HIT_SELECTION & HitSummaryData::Outlier) {
216  total += stat_outlier_hits;
217  }
218  }
219  }
220  return total;
221  }
222 
223  private:
224  std::vector< std::tuple<unsigned short, uint8_t, uint8_t> > m_stat;
225  std::array<uint8_t, Total+1> m_hits;
226  std::array<uint8_t, Total+1> m_outlierHits;
227  std::array<uint8_t, Total+1> m_layers;
228  static constexpr std::array<uint8_t, unknown+1> s_type
230  };
231 
234  class SumOfValues {
235  private:
236  double m_sum = 0.;
237  double m_sum2 = 0.;
238  unsigned int m_n =0u;
239  public:
240  void reset() {
241  m_sum=0.;
242  m_sum2=0.;
243  m_n=0u;
244  }
245  void add(double value) {
246  m_sum += value;
247  m_sum2 += value * value;
248  ++m_n;
249  }
250  std::array<double,2> meanAndBiasedVariance() const {
251  double inv_n = m_n>0 ? 1/m_n : 0 ;
252  double mean = m_sum * inv_n;
253  return std::array<double, 2> { mean, (m_sum2 - m_sum * mean) * inv_n };
254  }
255  double biasedVariance() const {
256  double inv_n = m_n>0 ? 1./m_n : 0 ;
257  return (m_sum2 - m_sum * m_sum *inv_n) * inv_n;
258  }
259  };
260 
271  void gatherTrackSummaryData(const ActsTrk::TrackContainer &tracksContainer,
272  const typename ActsTrk::TrackContainer::ConstTrackProxy &track,
276  &measurement_to_summary_type,
277  SumOfValues &chi2_stat_out,
278  HitSummaryData &hit_info_out,
279  std::vector<ActsTrk::TrackStateBackend::ConstTrackStateProxy::IndexType > &param_state_idx_out,
281  to_underlying(xAOD::UncalibMeasType::nTypes)> &special_hit_counts_out);
282 
283 }
284 #endif
PixelID.h
This is an Identifier helper class for the Pixel subdetector. This class is a factory for creating co...
ActsTrk::HitSummaryData::LAYER_MASK
constexpr static unsigned short LAYER_MASK
Definition: HitSummaryDataUtils.h:59
ActsTrk::HitSummaryData::unknownTotal
@ unknownTotal
Definition: HitSummaryDataUtils.h:51
SCT_ID.h
This is an Identifier helper class for the SCT subdetector. This class is a factory for creating comp...
ActsTrk::TrackContainer
Definition: TrackContainer.h:31
mean
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="")
Definition: dependence.cxx:254
InDetDD::SiDetectorElementCollection
Definition: SiDetectorElementCollection.h:30
xAOD::uint8_t
uint8_t
Definition: Muon_v1.cxx:575
ActsTrk::HitSummaryData::LAYER_REGION_MASK
constexpr static unsigned short LAYER_REGION_MASK
Definition: HitSummaryDataUtils.h:55
ActsTrk::HitSummaryData::SIGNED_ETA_MOD_MASK
constexpr static unsigned short SIGNED_ETA_MOD_MASK
Definition: HitSummaryDataUtils.h:61
ActsTrk::SumOfValues
Helper class to gather statistics and compute the biased variance.
Definition: HitSummaryDataUtils.h:234
ActsTrk::HitSummaryData::Hit
@ Hit
Definition: HitSummaryDataUtils.h:96
ActsTrk::SumOfValues::add
void add(double value)
Definition: HitSummaryDataUtils.h:245
ActsTrk::HitSummaryData::m_hits
std::array< uint8_t, Total+1 > m_hits
Definition: HitSummaryDataUtils.h:225
ActsTrk::gatherTrackSummaryData
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.
Definition: HitSummaryDataUtils.cxx:16
ActsTrk::HitSummaryData::stripEndcap
@ stripEndcap
Definition: HitSummaryDataUtils.h:47
ActsTrk::HitSummaryData::HitAndOutlier
@ HitAndOutlier
Definition: HitSummaryDataUtils.h:98
ActsTrk::SumOfValues::m_sum
double m_sum
Definition: HitSummaryDataUtils.h:236
athena.value
value
Definition: athena.py:122
ActsTrk::HitSummaryData::pixelBarrelInclined
@ pixelBarrelInclined
Definition: HitSummaryDataUtils.h:44
ActsTrk::HitCategory::DeadSensor
@ DeadSensor
Definition: HitSummaryDataUtils.h:30
ActsTrk::HitSummaryData::pixelTotal
@ pixelTotal
Definition: HitSummaryDataUtils.h:49
ActsTrk::HitSummaryData::unknown
@ unknown
Definition: HitSummaryDataUtils.h:48
ActsTrk::SumOfValues::m_sum2
double m_sum2
Definition: HitSummaryDataUtils.h:237
ActsTrk::to_underlying
constexpr std::underlying_type< T_EnumClass >::type to_underlying(T_EnumClass an_enum)
Helper to convert class enum into an integer.
Definition: HitSummaryDataUtils.h:24
Trk::u
@ u
Enums for curvilinear frames.
Definition: ParamDefs.h:83
ActsTrk::HitSummaryData::sum
uint8_t sum(DetectorRegion region, uint8_t layer) const
return the total number of hits, outliers or hits+outliers in the givrn detector region and layer.
Definition: HitSummaryDataUtils.h:207
InDetDD::SolidStateDetectorElementBase::getIdHelper
const AtlasDetectorID * getIdHelper() const
Returns the id helper (inline)
InDetDD::PixelInclined
@ PixelInclined
Definition: DetectorDesign.h:46
ActsTrk::HitSummaryData::reset
void reset()
reset all summary counters to zero.
Definition: HitSummaryDataUtils.h:103
ActsTrk::HitSummaryData::contributingHits
uint8_t contributingHits(DetectorRegion region) const
return the number of hits in a certain detector region.
Definition: HitSummaryDataUtils.h:189
ActsTrk::HitSummaryData::stripTotal
@ stripTotal
Definition: HitSummaryDataUtils.h:50
Hit
Definition: Simulation/ISF/ISF_FastCaloSim/ISF_FastCaloGpu/ISF_FastCaloGpu/Hit.h:16
ActsTrk::HitSummaryData::contributingOutlierHits
uint8_t contributingOutlierHits(DetectorRegion region) const
return the number of outliers in a certain detector region.
Definition: HitSummaryDataUtils.h:197
ActsTrk::HitSummaryData::REGION_MASK
constexpr static unsigned short REGION_MASK
Definition: HitSummaryDataUtils.h:57
InDetDD::DetectorType
DetectorType
Definition: DetectorDesign.h:45
ActsTrk::HitSummaryData::DetectorRegion
DetectorRegion
Regions for which hit counts are computed.
Definition: HitSummaryDataUtils.h:42
ActsTrk::HitSummaryData::addHit
bool addHit(const InDetDD::SiDetectorElementCollection *detector_elements, unsigned int id_hash, EHitSelection hit_selection)
update summaries to take the given hit into account.
Definition: HitSummaryDataUtils.h:117
ActsTrk::HitSummaryData::SIGNED_ETA_MOD_BITS
constexpr static unsigned short SIGNED_ETA_MOD_BITS
Definition: HitSummaryDataUtils.h:60
TRT::Hit::layer
@ layer
Definition: HitInfo.h:79
ActsTrk::SumOfValues::meanAndBiasedVariance
std::array< double, 2 > meanAndBiasedVariance() const
Definition: HitSummaryDataUtils.h:250
InDetDD::DetectorDesign::type
virtual DetectorType type() const
Type of element.
Definition: DetectorDesign.cxx:101
ActsTrk::HitSummaryData::m_outlierHits
std::array< uint8_t, Total+1 > m_outlierHits
Definition: HitSummaryDataUtils.h:226
InDetDD::SiDetectorElement::isPixel
bool isPixel() const
lumiFormat.array
array
Definition: lumiFormat.py:98
PixelID::layer_disk
int layer_disk(const Identifier &id) const
Definition: PixelID.h:626
xAOD::UncalibMeasType::nTypes
@ nTypes
PixelID::eta_module
int eta_module(const Identifier &id) const
Definition: PixelID.h:651
ActsTrk::SumOfValues::reset
void reset()
Definition: HitSummaryDataUtils.h:240
ActsTrk::HitSummaryData::makeKey
constexpr static 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.
Definition: HitSummaryDataUtils.h:68
InDetDD::PixelBarrel
@ PixelBarrel
Definition: DetectorDesign.h:46
SCT_ID::layer_disk
int layer_disk(const Identifier &id) const
Definition: SCT_ID.h:734
InDetDD::SiDetectorElement
Definition: SiDetectorElement.h:109
InDetDD::SiDetectorElement::isBarrel
bool isBarrel() const
ActsTrk::HitSummaryData::computeSummaries
void computeSummaries()
Compute the varius summaries.
Definition: HitSummaryDataUtils.h:160
ActsTrk::HitSummaryData::pixelEndcap
@ pixelEndcap
Definition: HitSummaryDataUtils.h:45
python.Dumpers.typename
def typename(t)
Definition: Dumpers.py:194
SiDetectorElement.h
ActsTrk::HitSummaryData::stripBarrel
@ stripBarrel
Definition: HitSummaryDataUtils.h:46
ActsTrk::HitSummaryData::m_layers
std::array< uint8_t, Total+1 > m_layers
Definition: HitSummaryDataUtils.h:227
ActsTrk::HitSummaryData::EHitSelection
EHitSelection
Definition: HitSummaryDataUtils.h:95
SCT_ID
Definition: SCT_ID.h:68
lumiFormat.fill
fill
Definition: lumiFormat.py:111
InDetDD
Message Stream Member.
Definition: FakeTrackBuilder.h:8
python.CaloScaleNoiseConfig.type
type
Definition: CaloScaleNoiseConfig.py:78
SCT_ID::eta_module
int eta_module(const Identifier &id) const
Definition: SCT_ID.h:746
ActsTrk::HitSummaryData::pixelBarrelFlat
@ pixelBarrelFlat
Definition: HitSummaryDataUtils.h:43
ActsTrk::HitSummaryData::s_type
static constexpr std::array< uint8_t, unknown+1 > s_type
Definition: HitSummaryDataUtils.h:229
ActsTrk
The AlignStoreProviderAlg loads the rigid alignment corrections and pipes them through the readout ge...
Definition: MuonDetectorBuilderTool.cxx:34
ActsTrk::SumOfValues::biasedVariance
double biasedVariance() const
Definition: HitSummaryDataUtils.h:255
InDetDD::SiDetectorElement::isSCT
bool isSCT() const
ActsTrk::HitSummaryData::layerFromKey
constexpr static uint8_t layerFromKey(unsigned short key)
extract the layer index from the given key.
Definition: HitSummaryDataUtils.h:90
ActsTrk::HitSummaryData::Outlier
@ Outlier
Definition: HitSummaryDataUtils.h:97
xAOD::track
@ track
Definition: TrackingPrimitives.h:512
ActsTrk::HitSummaryData::contributingLayers
uint8_t contributingLayers(DetectorRegion region) const
return the number of layers contributing to the hit collection in the given detector region.
Definition: HitSummaryDataUtils.h:181
ActsTrk::SumOfValues::m_n
unsigned int m_n
Definition: HitSummaryDataUtils.h:238
ActsTrk::HitSummaryData::REGION_BITS
constexpr static unsigned short REGION_BITS
Definition: HitSummaryDataUtils.h:56
ActsTrk::HitSummaryData::Total
@ Total
Definition: HitSummaryDataUtils.h:52
InDetDD::SiDetectorElement::design
virtual const SiDetectorDesign & design() const override final
access to the local description (inline):
PixelID
Definition: PixelID.h:67
ActsTrk::HitCategory::ESpecialHitCategories
ESpecialHitCategories
Definition: HitSummaryDataUtils.h:29
ActsTrk::HitSummaryData::regionFromKey
constexpr static DetectorRegion regionFromKey(unsigned short key)
extract the region index from the given key.
Definition: HitSummaryDataUtils.h:84
InDetDD::SolidStateDetectorElementBase::identify
virtual Identifier identify() const override final
identifier of this detector element (inline)
ActsTrk::HitCategory::N
@ N
Definition: HitSummaryDataUtils.h:32
ActsTrk::HitCategory::Hole
@ Hole
Definition: HitSummaryDataUtils.h:31
ActsTrk::HitSummaryData::LAYER_BITS
constexpr static unsigned short LAYER_BITS
Definition: HitSummaryDataUtils.h:58
ActsTrk::HitSummaryData
Helper class to gather hit summary information for e.g.
Definition: HitSummaryDataUtils.h:38
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37
ActsTrk::HitSummaryData::m_stat
std::vector< std::tuple< unsigned short, uint8_t, uint8_t > > m_stat
Definition: HitSummaryDataUtils.h:224