ATLAS Offline Software
Loading...
Searching...
No Matches
PixelChargeCalibCondData.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
4
8
9
10#include <stdexcept>
11#include <sstream>
12#include <cfloat> //for FLT_MAX
13
14
15
16using namespace PixelChargeCalib;
17using InDetDD::enum2uint; //in PixelReadoutDefinitions.h; will use ADL anyway,but make it explicit
18
19namespace{
20 const std::out_of_range
21 generateError(const char * functionName, InDetDD::PixelDiodeType type, unsigned int moduleHash, unsigned int FE){
22 std::stringstream error;
23 error << "PixelChargeCalibCondData::"<< functionName << "("<<enum2uint(type,__func__)<< ", " << moduleHash << ", " << FE << "): out of bounds";
24 return std::out_of_range(error.str());
25 }
26 const std::out_of_range
27 generateError(const char * functionName, unsigned int moduleHash, unsigned int FE, unsigned int val){
28 std::stringstream error;
29 error << "PixelChargeCalibCondData::"<< functionName << "(" << moduleHash << ", " << FE << ", "<< val << "): out of bounds";
30 return std::out_of_range(error.str());
31 }
32 const std::out_of_range
33 generateError(const char * functionName, unsigned int moduleHash, unsigned int FE){
34 std::stringstream error;
35 error << "PixelChargeCalibCondData::"<< functionName << "(" << moduleHash << ", " << FE << "): out of bounds";
36 return std::out_of_range(error.str());
37 }
38 const std::out_of_range
39 generateError(const char * functionName, unsigned int moduleHash){
40 std::stringstream error;
41 error << "PixelChargeCalibCondData::"<< functionName << "(" << moduleHash << "): out of bounds";
42 return std::out_of_range(error.str());
43 }
44}
45
46PixelChargeCalibCondData::PixelChargeCalibCondData(std::size_t max_module_hash) : m_sizeOfHashVector(max_module_hash){
47 //nop
48}
49
50void
52 //calibration strategy
53 setCalibrationStrategy(moduleHash, b.calibrationType);
54 // Normal pixel
55 setThresholds(InDetDD::PixelDiodeType::NORMAL, moduleHash, b.threshold);
58 setTotResolutions(moduleHash, b.totRes);
59 // Long pixel
60 setThresholds(InDetDD::PixelDiodeType::LONG, moduleHash, b.thresholdLong);
63 // Ganged/large pixel
64 setThresholds(InDetDD::PixelDiodeType::GANGED, moduleHash, b.thresholdGanged);
67}
68
69void
70PixelChargeCalibCondData::setAllFromConfigData(unsigned int moduleHash, const PixelModuleData * configData, const std::pair<int, int> &becLayer, unsigned int numFE){
71 const auto & [barrel_ec, layer] = becLayer;
72 for (size_t i{}; i != s_NPixelDiodes; ++i) {
73 const auto t = static_cast<InDetDD::PixelDiodeType>(i);
74 const Thresholds thresholds{configData->getDefaultAnalogThreshold(barrel_ec, layer), configData->getDefaultAnalogThresholdSigma(barrel_ec, layer),
75 configData->getDefaultAnalogThresholdNoise(barrel_ec, layer), configData->getDefaultInTimeThreshold(barrel_ec, layer)};
76 setThresholds(t, moduleHash, std::vector<Thresholds>(numFE, thresholds));
77 //
78 const LegacyFitParameters defaultParams{configData->getDefaultQ2TotA(), configData->getDefaultQ2TotE(), configData->getDefaultQ2TotC(), LegacyFitParameters::defaultOverflow};
79 setLegacyFitParameters(t, moduleHash, std::vector<LegacyFitParameters>(numFE, defaultParams));
80 //
81 const auto zeroLinFit = PixelChargeCalib::LinearFitParameters();
82 setLinearFitParameters(t, moduleHash, std::vector<PixelChargeCalib::LinearFitParameters>(numFE, zeroLinFit));
83 }
84 const auto zeroResolution = PixelChargeCalib::Resolutions();
85 setTotResolutions(moduleHash, std::vector<PixelChargeCalib::Resolutions>(numFE, zeroResolution));
86}
87
88void
89PixelChargeCalibCondData::setThresholds(InDetDD::PixelDiodeType type, unsigned int moduleHash, const std::vector<PixelChargeCalib::Thresholds> & thresholds){
90 if (moduleHash >= m_sizeOfHashVector) throw generateError(__func__, moduleHash);
91 auto & hashIndexedVector = m_thresholds[enum2uint(type,__func__)];
92 if (hashIndexedVector.size()<=moduleHash){
93 hashIndexedVector.resize(m_sizeOfHashVector);
94 }
95 hashIndexedVector[moduleHash] = thresholds;
96}
97
99PixelChargeCalibCondData::getThresholds(InDetDD::PixelDiodeType type, unsigned int moduleHash, unsigned int FE) const{
100 auto idx = enum2uint(type,__func__);
101 const auto & hashIndexedVector = m_thresholds[idx];
102 const auto &feIndexedVector = hashIndexedVector.at(moduleHash);
103 if (FE < feIndexedVector.size()) {
104 return feIndexedVector[FE];
105 }
106 throw generateError(__func__, type, moduleHash, FE);
107}
108
109void
110PixelChargeCalibCondData::setLegacyFitParameters(InDetDD::PixelDiodeType type, unsigned int moduleHash, const std::vector<PixelChargeCalib::LegacyFitParameters> &parameters){
111 if (moduleHash >= m_sizeOfHashVector) throw generateError(__func__, moduleHash);
112 auto & thisVector = m_legacyFit.at(enum2uint(type,__func__));
113 if (thisVector.size()<=moduleHash){
114 thisVector.resize(m_sizeOfHashVector);
115 }
116 thisVector[moduleHash] = parameters;
117}
118
121 if (moduleHash >= m_sizeOfHashVector) throw generateError(__func__, type, moduleHash, FE);
122 auto idx = enum2uint(type,__func__);
123 const auto & thisVector = m_legacyFit.at(idx);
124 const auto &v = thisVector.at(moduleHash);
125 if (FE < v.size()) {
126 return v[FE];
127 }
128 throw generateError(__func__, type, moduleHash, FE);
129}
130
131void
132PixelChargeCalibCondData::setLinearFitParameters(InDetDD::PixelDiodeType type, unsigned int moduleHash, const std::vector<PixelChargeCalib::LinearFitParameters> &parameters){
133 if (moduleHash >= m_sizeOfHashVector) throw generateError(__func__, moduleHash);
134 auto & thisVector = m_linFit.at(enum2uint(type,__func__));
135 if ( thisVector.size()<=moduleHash){
136 thisVector.resize(m_sizeOfHashVector);
137 }
138 thisVector[moduleHash] = parameters;
139}
140
141
144 if (type==InDetDD::PixelDiodeType::NONE) throw generateError(__func__, type, moduleHash, FE);
145 auto idx = enum2uint(type,__func__);
146 const auto &typeMap = m_linFit.at(idx);
147 //
148 const auto &linFit = typeMap.at(moduleHash);
149 if (FE < linFit.size()) {
150 return typeMap.at(moduleHash).at(FE);
151 }
152 throw generateError(__func__, type, moduleHash, FE);
153}
154
155void
156PixelChargeCalibCondData::setTotResolutions(unsigned int moduleHash, const std::vector<PixelChargeCalib::Resolutions> &value){
157 if (moduleHash >= m_sizeOfHashVector) throw generateError(__func__, moduleHash);
158 if (moduleHash >= m_totRes.size()) m_totRes.resize(m_sizeOfHashVector);
159 m_totRes[moduleHash] = value;
160}
161
162float
163PixelChargeCalibCondData::getTotRes(unsigned int moduleHash, unsigned int FE, float Q) const{
165 if (const auto &res = m_totRes.at(moduleHash); FE < res.size()) {
166 r = res[FE];
167 } else {
168 throw generateError(__func__, moduleHash, FE);
169 }
170 return r.total(Q);
171}
172
173float
174PixelChargeCalibCondData::getToT(InDetDD::PixelDiodeType type, unsigned int moduleHash, unsigned int FE, float Q) const{
176 return getToTLUTFEI4(moduleHash, FE, Q);
177 }
178 if (type == InDetDD::PixelDiodeType::NONE) return 0.f;
179 const LegacyFitParameters & legacy = getLegacyFitParameters(type, moduleHash, FE);
180 float tot = legacy.ToT(Q);
181
182 // Protection for large charge
183 float exth = 1e5f; // the calibration function is analytically connected at threshold exth.
184 if (Q>exth && getCalibrationStrategy(moduleHash)==CalibrationStrategy::RUN3PIX) {
185 const LinearFitParameters & lin = getLinearFitParameters(type, moduleHash, FE);
186 if ( float tot1 = lin.ToT(Q); tot1 != 0.f) return tot1;
187 }
188 return tot;
189}
190
191
192float
194 const CalibrationStrategy calibStrategy,
195 unsigned int moduleHash,
196 unsigned int FE,
197 float ToT) const{
198
199 if (calibStrategy == CalibrationStrategy::LUTFEI4) {
200 return getChargeLUTFEI4(moduleHash, FE, ToT);
201 }
202
203 if (type == InDetDD::PixelDiodeType::NONE) return 0.f;
204 const LegacyFitParameters & legacy = getLegacyFitParameters(type, moduleHash, FE);
205 float charge = legacy.Q(ToT);
206 // Protection for small charge
207 const auto & thresholds = getThresholds(type,moduleHash,FE);
208 const auto analogueThreshold = thresholds.value;
209 if (charge<analogueThreshold && calibStrategy==CalibrationStrategy::RUN3PIX) { charge=analogueThreshold; }
210 // Protection for large charge
211 float exth = 1e5f; // the calibration function is analytically connected at threshold exth.
212 if (charge>exth && calibStrategy==CalibrationStrategy::RUN3PIX) {
213 const LinearFitParameters & lin = getLinearFitParameters(type, moduleHash, FE);
214 if (float charge1 = lin.Q(ToT); charge1 != 0.f) return charge1;
215 }
216
217 return charge;
218
219}
220
221
222float
223PixelChargeCalibCondData::getCharge(InDetDD::PixelDiodeType type, unsigned int moduleHash, unsigned int FE, float ToT) const{
225 return getChargeLUTFEI4(moduleHash, FE, ToT);
226 }
227 if (type == InDetDD::PixelDiodeType::NONE) return 0.f;
228 const LegacyFitParameters & legacy = getLegacyFitParameters(type, moduleHash, FE);
229
230 float charge = legacy.Q(ToT);
231
232 // Protection for small charge
233 const auto & thresholds = getThresholds(type,moduleHash,FE);
234 const auto analogueThreshold = thresholds.value;
235 if (charge<analogueThreshold && getCalibrationStrategy(moduleHash)==CalibrationStrategy::RUN3PIX) { charge=analogueThreshold; }
236 // Protection for large charge
237 float exth = 1e5f; // the calibration function is analytically connected at threshold exth.
239 const LinearFitParameters & lin = getLinearFitParameters(type, moduleHash, FE);
240 if (float charge1 = lin.Q(ToT); charge1 != 0.f) return charge1;
241 }
242 return charge;
243}
244
245void
247 if (moduleHash >= m_sizeOfHashVector) {
248 throw generateError(__func__, moduleHash, static_cast<int>(strategy));
249 }
250 m_calibrationStrategy[moduleHash] = strategy;
251}
252
255 if (moduleHash >= m_sizeOfHashVector){
256 throw generateError(__func__, moduleHash);
257 }
258 auto itr = m_calibrationStrategy.find(moduleHash);
259 if (itr != m_calibrationStrategy.end()) {
260 return itr->second;
261 }
263}
264
265void
266PixelChargeCalibCondData::setTot2Charges(unsigned int moduleHash, IBLModule charges){
267 if (moduleHash >= m_sizeOfHashVector){
268 throw generateError(__func__, moduleHash);
269 }
270 m_tot2Charges[moduleHash] = std::move(charges);
271}
272
274PixelChargeCalibCondData::getTot2Charges(unsigned int moduleHash, unsigned int FE) const{
275 auto it = m_tot2Charges.find(moduleHash);
276 if (it != m_tot2Charges.end() && FE < it->second.size()) {
277 return it->second.at(FE);
278 }
279 throw generateError(__func__, moduleHash,FE);
280}
281
282float
283PixelChargeCalibCondData::getChargeLUTFEI4(unsigned int moduleHash, unsigned int FE, unsigned int ToT) const{
284 if (ToT < 1 || ToT > IBLCalibrationSize) {
285 throw generateError(__func__, moduleHash,FE, ToT);
286 }
287 const IBLCalibration &charges = getTot2Charges(moduleHash,FE);
288 return charges[ToT - 1];
289}
290
291float
292PixelChargeCalibCondData::getToTLUTFEI4(unsigned int moduleHash, unsigned int FE, float Q) const{
293 int tot = -1;
294 float minDiff = FLT_MAX;
295 for (size_t t = 0; t < IBLCalibrationSize; t++) {
296 float charge = getChargeLUTFEI4(moduleHash, FE, t + 1);
297 float diff = std::fabs(charge - Q);
298 if (diff < minDiff) {
299 minDiff = diff;
300 tot = t + 1;
301 }
302 }
303 return tot;
304}
double charge(const T &p)
Definition AtlasPID.h:997
Struct for holding vectors of charge calibration constants, with utility methods.
constexpr std::size_t enum2uint(T n, std::string_view callingFunctionName="")
Convert an enum class to size_t for use as an array index.
std::pair< std::vector< unsigned int >, bool > res
constexpr std::size_t enum2uint(T n, std::string_view callingFunctionName="")
Convert an enum class to size_t for use as an array index.
Store pixel constant parameters in PixelModuleData.
void diff(const Jet &rJet1, const Jet &rJet2, std::map< std::string, double > varDiff)
Difference between jets - Non-Class function required by trigger.
Definition Jet.cxx:631
void setLinearFitParameters(InDetDD::PixelDiodeType type, unsigned int moduleHash, const std::vector< PixelChargeCalib::LinearFitParameters > &parameters)
void setLegacyFitParameters(InDetDD::PixelDiodeType type, unsigned int moduleHash, const std::vector< PixelChargeCalib::LegacyFitParameters > &parameters)
void setTot2Charges(unsigned int moduleHash, IBLModule charges)
PixelChargeCalib::LegacyFitParameters getLegacyFitParameters(InDetDD::PixelDiodeType type, unsigned int moduleHash, unsigned int FE) const
CalibrationStrategy getCalibrationStrategy(unsigned int moduleHash) const
PixelChargeCalibCondData()=default
std::map< int, CalibrationStrategy > m_calibrationStrategy
std::vector< IBLCalibration > IBLModule
float getCharge(InDetDD::PixelDiodeType type, unsigned int moduleHash, unsigned int FE, float ToT) const
std::array< std::vector< std::vector< PixelChargeCalib::LinearFitParameters > >, s_NPixelDiodes > m_linFit
void setAllFromConfigData(unsigned int moduleHash, const PixelModuleData *configData, const std::pair< int, int > &becLayer, unsigned int numFE)
void setThresholds(InDetDD::PixelDiodeType type, unsigned int moduleHash, const std::vector< PixelChargeCalib::Thresholds > &thresholds)
const IBLCalibration & getTot2Charges(unsigned int moduleHash, unsigned int FE) const
std::vector< std::vector< PixelChargeCalib::Resolutions > > m_totRes
void setCalibrationStrategy(unsigned int moduleHash, CalibrationStrategy strategy)
void setAllFromBundle(unsigned int moduleHash, const PixelChargeCalib::ChargeCalibrationBundle &b)
void setTotResolutions(unsigned int moduleHash, const std::vector< PixelChargeCalib::Resolutions > &value)
static constexpr size_t IBLCalibrationSize
std::array< std::vector< std::vector< PixelChargeCalib::Thresholds > >, s_NPixelDiodes > m_thresholds
std::map< int, IBLModule > m_tot2Charges
PixelChargeCalib::Thresholds getThresholds(InDetDD::PixelDiodeType type, unsigned int moduleHash, unsigned int FE) const
float getToT(InDetDD::PixelDiodeType type, unsigned int moduleHash, unsigned int FE, float Q) const
float getToTLUTFEI4(unsigned int moduleHash, unsigned int FE, float Q) const
static constexpr std::size_t s_NPixelDiodes
float getTotRes(unsigned int moduleHash, unsigned int FE, float Q) const
PixelChargeCalib::LinearFitParameters getLinearFitParameters(InDetDD::PixelDiodeType type, unsigned int moduleHash, unsigned int FE) const
std::array< float, IBLCalibrationSize > IBLCalibration
float getChargeLUTFEI4(unsigned int moduleHash, unsigned int FE, unsigned int ToT) const
std::array< std::vector< std::vector< PixelChargeCalib::LegacyFitParameters > >, s_NPixelDiodes > m_legacyFit
int getDefaultInTimeThreshold(int barrel_ec, int layer) const
int getDefaultAnalogThreshold(int barrel_ec, int layer) const
float getDefaultQ2TotA() const
int getDefaultAnalogThresholdNoise(int barrel_ec, int layer) const
float getDefaultQ2TotC() const
float getDefaultQ2TotE() const
int getDefaultAnalogThresholdSigma(int barrel_ec, int layer) const
int r
Definition globals.cxx:22
constexpr std::size_t enum2uint(T n, std::string_view callingFunctionName="")
Convert an enum class to size_t for use as an array index.
bundles of parameters used together in the PixelChargeCalibCondAlg
float ToT(float Q) const
Return Time-over-threshold given charge Q.