ATLAS Offline Software
Loading...
Searching...
No Matches
DefectsEmulatorCondAlgImpl.icc
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
5#include "Identifier/Identifier.h"
6#include "AthenaKernel/RNGWrapper.h"
7
8#include "StoreGate/WriteHandle.h"
9
10
11#include "CLHEP/Random/RandFlat.h"
12
13#include "ModuleIdentifierMatchUtil.h"
14#include "ConnectedModulesUtil.h"
15#include "EmulatedDefectsBuilder.h"
16
17#include "nlohmann/json.hpp"
18
19#include "TVirtualRWMutex.h"
20#include "ROOT/RNTupleWriter.hxx"
21#include "ROOT/RNTupleReader.hxx"
22#include "ROOT/RNTupleModel.hxx"
23#include "TFile.h"
24
25#include <cstdlib>
26#include <ranges>
27#include <type_traits>
28#include <array>
29#include <span>
30#include <algorithm>
31#include <optional>
32
33
34namespace {
35
36 template <typename T_ID>
37 concept IdentifierHelperWithOtherSideConecpt = requires(T_ID id_helper) { id_helper.get_other_side(); };
38
39 template <class T_ModuleHelper>
40 unsigned int getSensorColumn(const T_ModuleHelper &helper, unsigned int cell_idx) {
41 return cell_idx % helper.nSensorColumns();
42 }
43 template <class T_ModuleHelper>
44 unsigned int getSensorRow(const T_ModuleHelper &helper, unsigned int cell_idx) {
45 return cell_idx / helper.nSensorColumns();
46 }
47
48 unsigned int makeCheckerboard(unsigned int cell_idx, unsigned int n_rows, unsigned int n_columns,
49 bool odd_row_toggle,
50 bool odd_col_toggle
51 ) {
52 unsigned int row_i=cell_idx / n_columns;
53 unsigned int col_i = cell_idx % n_columns;
54 unsigned int row_odd = row_i < n_rows/2 || !odd_row_toggle ? 1 : 0;
55 unsigned int div_row = n_rows/7;
56 unsigned int div_cols = n_columns/7;
57 row_i = std::min((((row_i / div_row ) & (0xffff-1)) + row_odd)*div_row + (row_i%div_row), n_rows-1);
58 unsigned int col_odd = col_i < n_columns/2 || !odd_col_toggle ? 1 : 0;
59 col_i = std::min((((col_i / div_cols ) & (0xffff-1)) + col_odd)*div_cols + (col_i%div_cols),n_columns-1);
60 cell_idx= row_i * n_columns + (col_i % n_columns);
61 return cell_idx;
62 }
63
64 /** helper to consistently lock and unlock when leaving the scope.
65 * similar to std::lock_guard, but locking when not needed for the scope
66 * can be disabled.
67 */
68 class MyLockGuard {
69 public:
70 MyLockGuard(std::mutex &a_mutex, bool disable)
71 : m_mutex( !disable ? &a_mutex : nullptr)
72 {
73 if (m_mutex) {m_mutex->lock(); }
74 }
75 ~MyLockGuard() {
76 if (m_mutex) {m_mutex->unlock(); }
77 }
78 private:
79 std::mutex *m_mutex;
80 };
81
82
83 template <class T_ModuleHelper>
84 void histogramDefects(const T_ModuleHelper &helper,
85 const typename std::pair<typename T_ModuleHelper::KEY_TYPE,typename T_ModuleHelper::KEY_TYPE> &key,
86 TH2 *h2) {
87 std::array<unsigned int,4> ranges_row_col = helper.offlineRange(key);
88 for (unsigned int row_i=ranges_row_col[0]; row_i<ranges_row_col[1]; ++row_i) {
89 for (unsigned int col_i=ranges_row_col[2]; col_i<ranges_row_col[3]; ++col_i) {
90 h2->Fill(col_i, row_i);
91 }
92 }
93 }
94
95 // create an array containing all possible combinations of M-tuples of numbers of 1 to N
96 // The M numbers are unique per M-tuple, and the order has no significance.
97 template <typename T, std::size_t N_MAX, std::size_t M>
98 constexpr std::array<T, N_MAX> packCombinations(unsigned int N) {
99 std::array<T, N_MAX> packed_permutations{};
100 unsigned int idx=0;
101 std::array<unsigned int,M> last_val;
102 for (unsigned int i=0; i<N; ++i) {
103 last_val[i]=i;
104 }
105 for (;;) {
106 for (unsigned int i=0; i<N; ++i) {
107 packed_permutations[idx]|= static_cast<T>(1u<<last_val[i]);
108 }
109 ++idx;
110 unsigned int i=N;
111 for(; i-->0; ) {
112 if (++last_val[i]<=M-(N-i)) {
113 for (;++i<N;) {
114 last_val[i]=last_val[i-1]+1;
115 }
116 i=0;
117 break;
118 }
119 }
120 if (i>=N) break;
121 }
122 return packed_permutations;
123 }
124
125 // count the number of elements up the first which is zero if it is not the first element.
126 template <typename T, std::size_t N_MAX>
127 constexpr unsigned int findLastNonEmpty(const std::array<T, N_MAX> &arr) {
128 unsigned int i=0;
129 while (++i<arr.size() && arr[i]!=0);
130 return i;
131 }
132
133 // create an arry which contains the number of combinations for the array of combinations.
134 template <typename T, typename T2, std::size_t N_MAX, std::size_t N>
135 constexpr std::array<unsigned char,N> makeCombinationCounts(const std::array<std::array<T2, N_MAX>, N> &arr) {
136 std::array<unsigned char,N> ret;
137 unsigned int idx=0;
138 for (const std::array<T2, N_MAX> &sub_arr : arr) {
139 ret[idx++]=static_cast<T>(findLastNonEmpty(sub_arr));
140 }
141 return ret;
142 }
143
144 // create the possible combinations of defect corners for 1 to 4 defect corners
145 static constexpr std::array< std::array<unsigned char,6>, 4> corner_combinations {
146 packCombinations<unsigned char,6,4>(1),
147 packCombinations<unsigned char,6,4>(2),
148 packCombinations<unsigned char,6,4>(3),
149 packCombinations<unsigned char,6,4>(4),
150 };
151
152 // count the number of possible combinations;
153 static constexpr std::array<unsigned char,4> n_corner_combinations (makeCombinationCounts<unsigned char>(corner_combinations) );
154
155 template <typename T>
156 inline T sqr(T a) {
157 return a*a;
158 }
159
160 bool hasExtensions(const std::string &name, const std::string_view &ext) {
161 return name.size()>=ext.size() && name.substr(name.size()-ext.size(),ext.size())==ext;
162 }
163
164 template <typename T_EmulatedDefects>
165 void toJson(const T_EmulatedDefects &defects, const std::string &name) {
166 nlohmann::json data;
167 for (unsigned int id_hash = 0; id_hash < defects.size(); ++id_hash) {
168 if (defects.isModuleDefect(id_hash) || !defects[id_hash].empty()) {
169 nlohmann::json module_data;
170 if (defects.isModuleDefect(id_hash)) {
171 module_data["isDefect"]=true;
172 }
173 else {
174 module_data["defects"]=defects[id_hash];
175 }
176
177 std::stringstream id_hash_name;
178 id_hash_name << id_hash;
179 data[id_hash_name.str()]=module_data;
180 }
181 }
182 std::ofstream out(name.c_str());
183 out << data;
184 }
185
186 template <typename T_EmulatedDefects>
187 void toRoot(const T_EmulatedDefects &defects, const std::string &name) {
188 ROOT::TWriteLockGuard lock (ROOT::gCoreMutex);
189
190 auto model=ROOT::RNTupleModel::Create();
191 auto nt_defects=model->MakeField< std::vector<typename T_EmulatedDefects::KEY_TYPE> >("defects");
192 auto nt_is_defect=model->MakeField<bool>("isDefect");
193 ROOT::RNTupleWriteOptions options;
194 options.SetCompression(ROOT::RCompressionSetting::EAlgorithm::kZSTD, ROOT::RCompressionSetting::ELevel::kDefaultZSTD);
195 auto writer = ROOT::RNTupleWriter::Recreate(std::move(model), "defects", name.c_str(), options);
196 for (unsigned int id_hash = 0; id_hash < defects.size(); ++id_hash) {
197 nt_defects->clear();
198 if (defects.isModuleDefect(id_hash)) {
199 *nt_is_defect =true;
200 }
201 else {
202 *nt_is_defect =false;
203 *nt_defects = defects[id_hash];
204 }
205 writer->Fill();
206 }
207 }
208
209 template <typename KEY_TYPE>
210 class JsonAdapter {
211 public:
212 bool open(const std::string &name) {
213 std::ifstream input_file(name);
214 if (!input_file) {
215 return false;
216 }
217 input_file >> m_jsDefects;
218 return true;
219 }
220 auto items() {
221 return m_jsDefects.items();
222 }
223 template <typename T>
224 static std::size_t moduleHash(const T &element) {
225 char *ptr;
226 return strtol(element.key().c_str(),&ptr, 10);
227 }
228 template <typename T>
229 static nlohmann::json value(const T &element) {
230 return element.value();
231 }
232 static bool isDefect(const nlohmann::json &value) {
233 return !value.contains("isDefect") ? false : value["isDefect"].get<bool>();
234 }
235 static std::vector<KEY_TYPE> moduleDefects(const nlohmann::json &value) {
236 return !value.contains("defects") ? std::vector<KEY_TYPE>() : value["defects"].get< std::vector<KEY_TYPE> >();
237 }
238 private:
239 nlohmann::json m_jsDefects;
240 };
241
242 template <typename KEY_TYPE>
243 class RootAdapter {
244 public:
245 bool open(const std::string &name) {
246 auto model=ROOT::RNTupleModel::Create();
247 m_defects=model->MakeField< std::vector<KEY_TYPE> >("defects");
248 m_isDefect=model->MakeField<bool>("isDefect");
249 m_reader = ROOT::RNTupleReader::Open(std::move(model), "defects", name.c_str());
250 return m_reader.get() != nullptr;
251 }
252 ROOT::RNTupleReader &items() {
253 return *m_reader;
254 }
255 static auto moduleHash( std::size_t entry_i) {
256 return entry_i;
257 }
258 auto value(std::size_t entry_i) {
259 m_reader->LoadEntry(entry_i);
260 return entry_i;
261 }
262 bool isDefect([[maybe_unused]] std::size_t entry_i) {
263 return *m_isDefect;
264 }
265 const std::vector<KEY_TYPE> &moduleDefects([[maybe_unused]] std::size_t entry_i) {
266 return *m_defects;
267 }
268 private:
269 std::unique_ptr< ROOT::RNTupleReader > m_reader;
270 std::shared_ptr< bool > m_isDefect;
271 std::shared_ptr<std::vector<KEY_TYPE> > m_defects;
272 };
273
274 template <typename T_ModuleHelper, typename T_ReaderAdapter, typename T_EmulatedDefects>
275 void fromFile(const InDetDD::SiDetectorElementCollection &det_ele_coll, T_EmulatedDefects &defects, const std::string &name) {
276 T_ReaderAdapter reader;
277 if (!reader.open(name)) {
278 std::stringstream amsg;
279 amsg << "Failed to read \"" << name << "\"";
280 throw std::runtime_error(amsg.str());
281 }
282
283 for (auto entry_i : reader.items()) {
284 auto module_i = reader.moduleHash(entry_i);
285 assert( module_i < det_ele_coll.size());
286 auto value = reader.value(entry_i);
287 if (reader.isDefect(value)) {
288 defects.setModuleDefect(module_i);
289 }
290 else if (!defects.isModuleDefect(module_i)) {
291 std::vector<typename T_EmulatedDefects::KEY_TYPE> &module_defects = defects.at(module_i);
292 if (module_defects.empty()) {
293 module_defects = reader.moduleDefects(value); // move ?
294 }
295 else {
296 InDetDD::SiDetectorElementCollection::const_value_type det_ele = det_ele_coll[module_i];
297 T_ModuleHelper helper(det_ele->design());
298 auto input_defects = reader.moduleDefects(value);
299 for (typename std::vector<typename T_EmulatedDefects::KEY_TYPE>::const_reverse_iterator iter = input_defects.rbegin();
300 iter != input_defects.rend();
301 ++iter) {
302 auto key = *iter;
303 if (T_ModuleHelper::isRangeKey(*iter)) {
304 ++iter;
305 if (iter == input_defects.rend()) {
306 break;
307 }
308 EmulatedDefectsBuilder::insertKeyRange(helper,
309 module_defects,
310 std::make_pair(key, *iter),
311 T_ModuleHelper::getDefectTypeComponent(key));
312 if (T_ModuleHelper::getDefectTypeComponent(key) != T_ModuleHelper::getDefectTypeComponent(*iter)) {
313 auto [end_key_iter, end_iter] = InDet::EmulatedDefects<T_ModuleHelper>::lower_bound(module_defects, *iter);
314 if (end_key_iter != end_iter && T_ModuleHelper::makeBaseKey(*iter) == T_ModuleHelper::makeBaseKey(*end_key_iter)) {
315 *end_key_iter |= T_ModuleHelper::getDefectTypeComponent(*iter);
316 }
317 }
318 }
319 else {
320 EmulatedDefectsBuilder::insertKey(helper,
321 module_defects,
322 key,
323 T_ModuleHelper::getDefectTypeComponent(key));
324 }
325 }
326 }
327 }
328 }
329 }
330
331
332 template <typename T_ModuleHelper, typename T_EmulatedDefects>
333 void fromJson(const InDetDD::SiDetectorElementCollection &det_ele_coll, T_EmulatedDefects &defects, const std::string &name) {
334 fromFile<T_ModuleHelper, JsonAdapter<typename T_ModuleHelper::KEY_TYPE> >(det_ele_coll, defects, name);
335 }
336
337 template <typename T_ModuleHelper, typename T_EmulatedDefects>
338 void fromRoot(const InDetDD::SiDetectorElementCollection &det_ele_coll, T_EmulatedDefects &defects, const std::string &name) {
339 ROOT::TReadLockGuard lock (ROOT::gCoreMutex);
340 fromFile<T_ModuleHelper, RootAdapter<typename T_ModuleHelper::KEY_TYPE> >(det_ele_coll, defects, name);
341 }
342
343}
344
345namespace InDet{
346
347
348 template<class T_Derived>
349 StatusCode DefectsEmulatorCondAlgImpl<T_Derived>::initialize(){
350 ATH_CHECK(m_detEleCollKey.initialize());
351 ATH_CHECK(m_writeKey.initialize());
352 ATH_CHECK(detStore()->retrieve(m_idHelper,derived().IDName()));
353 if constexpr( T_ModuleHelper::ROW_BITS==0 || T_ModuleHelper::COL_BITS==0) {
354 if (!m_cornerDefectParamsPerPattern.empty() || !m_cornerDefectNCornerFractionsPerPattern.empty()) {
355 ATH_MSG_ERROR("Corner defects are not supported in this instance but are configured.");
356 return StatusCode::FAILURE;
357 }
358 }
359
360 return initializeBase(T_ModuleHelper::nMasks(), m_idHelper->wafer_hash_max());
361 }
362
363 template<class T_Derived>
364 StatusCode DefectsEmulatorCondAlgImpl<T_Derived>::execute(const EventContext& ctx) const {
365 SG::WriteCondHandle<T_EmulatedDefects> defectsOut(m_writeKey, ctx);
366 if (defectsOut.isValid()) {
367 return StatusCode::SUCCESS;
368 }
369 SG::ReadCondHandle<InDetDD::SiDetectorElementCollection> detEleColl(m_detEleCollKey,ctx);
370 ATH_CHECK(detEleColl.isValid());
371 defectsOut.addDependency(detEleColl);
372
373 std::unique_ptr<T_EmulatedDefects> defects = std::make_unique<T_EmulatedDefects>(*(detEleColl.cptr()));
374
375 for (const std::string &input_file : m_inputFiles.value()) {
376 if (hasExtensions(input_file,".json")) {
377 fromJson<T_ModuleHelper>(*(detEleColl.cptr()),*defects, input_file);
378 }
379 else if (hasExtensions(input_file,".root")) {
380 fromRoot<T_ModuleHelper>(*(detEleColl.cptr()), *defects, input_file);
381 }
382 }
383
384 // last counts (+1) are counts for entire modules being defect
385 std::vector<std::array<unsigned int,kNCounts> > counts(T_ModuleHelper::nMasks()+1, std::array<unsigned int,kNCounts>{});
386
387 std::size_t n_error=0u;
388 unsigned int n_defects_total=0;
389 unsigned int module_without_matching_pattern=0u;
390 {
391 auto connected_modules = derived().getModuleConnectionMap(*(detEleColl.cptr()));
392
393 std::array<CLHEP::HepRandomEngine *, kMaskDefects + T_ModuleHelper::nMasks() > rndmEngine;
394 if (this->m_rngPerDefectType) {
395 assert (m_rngName.size() == rndmEngine.size());
396 unsigned int idx=0;
397 for (const std::string &a_rngName : m_rngName) {
398 ATHRNG::RNGWrapper* rngWrapper = m_rndmSvc->getEngine(this, a_rngName );
399 rngWrapper->setSeed( a_rngName, ctx );
400 assert( idx < rndmEngine.size());
401 rndmEngine[idx++]=rngWrapper->getEngine(ctx);
402 }
403 }
404 else{
405 assert( !m_rngName.empty());
406 ATHRNG::RNGWrapper* rngWrapper = m_rndmSvc->getEngine(this, m_rngName[0]);
407 rngWrapper->setSeed( m_rngName[0], ctx );
408 CLHEP::HepRandomEngine *a_rndmEngine = rngWrapper->getEngine(ctx);
409 for (CLHEP::HepRandomEngine *&elm : rndmEngine) {
410 elm = a_rndmEngine;
411 }
412 }
413
414 ModuleIdentifierMatchUtil::ModuleData_t module_data;
415 std::vector<unsigned int> module_pattern_idx;
416 module_pattern_idx.reserve( m_modulePattern.size() );
417 std::vector<double> cumulative_prob_dist;
418 cumulative_prob_dist.reserve( m_modulePattern.size() );
419 std::vector<unsigned int> n_mask_defects;
420 n_mask_defects.reserve( T_ModuleHelper::nMasks());
421
422 std::size_t det_ele_sz = detEleColl->size();
423 for (unsigned int module_i=0u; module_i < det_ele_sz; ++module_i) {
424 typename T_DetectorElementCollection::const_value_type det_ele = (*detEleColl.cptr())[module_i];
425 if (defects->isModuleDefect(module_i)) continue;
426 if (derived().isModuleDefect(ctx, module_i)) {
427 defects->setModuleDefect(module_i);
428 if (m_histogrammingEnabled && !m_moduleHist.empty()) {
429 // Always fill externally provided defects as if the module would
430 // match the first pattern, since there is no pattern associated to
431 // the external defects.
432 std::lock_guard<std::mutex> lock(m_histMutex);
433 histogramDefectModule(0, 0, module_i, det_ele->center());
434 }
435 continue;
436 }
437
438 T_ModuleHelper helper(det_ele->design());
439
440 if (!helper) {
441 ++n_error;
442 continue;
443 }
444 ++(counts.back()[ kNElements ]); // module defects
445
446 // find pattern matching this module
447 const T_ModuleDesign &moduleDesign = dynamic_cast<const T_ModuleDesign &>(det_ele->design());
448 ModuleIdentifierMatchUtil::setModuleData(*m_idHelper,
449 det_ele->identify(),
450 moduleDesign,
451 module_data);
452 ModuleIdentifierMatchUtil::moduleMatches(m_modulePattern.value(), module_data, module_pattern_idx);
453 if (module_pattern_idx.empty()) {
454 ++module_without_matching_pattern;
455 continue;
456 }
457
458 // it is possible that multiple patterns match a module
459 // For module defects a pattern is selected from all matching patterns randomly. This matters, because
460 // patterns control whether a defect is propagated to all modules connected to the same physical sensor
461 // or not.
462 makeCumulativeProbabilityDist(module_pattern_idx,kModuleDefectProb, cumulative_prob_dist);
463 float prob=(cumulative_prob_dist.empty() || cumulative_prob_dist.back()<=0.) ? 1. : CLHEP::RandFlat::shoot(rndmEngine[kModuleDefects],1.);
464
465 unsigned int match_i=module_pattern_idx.size();
466 assert (match_i>0);
467 for (; match_i-->0; ) {
468 assert( match_i < cumulative_prob_dist.size());
469 if (prob > cumulative_prob_dist[match_i]) break;
470 }
471 ++match_i;
472 // module defects
473 if (match_i < module_pattern_idx.size()) {
474 unsigned int hist_pattern_i = (m_fillHistogramsPerPattern ? module_pattern_idx[match_i] : 0 );
475 // mark entire module as defect;
476 (counts.back()[ kNDefects ])
477 += 1-defects->isModuleDefect(module_i); // do not double count
478 defects->setModuleDefect(module_i);
479 // if configured accordingly also mark the other "modules" as defect which
480 // are part of the same physical module
481 ATH_MSG_VERBOSE( "Add module defect "
482 << " hash=" << m_idHelper->wafer_hash(det_ele->identify())
483 << " barel_ec=" << m_idHelper->barrel_ec(det_ele->identify())
484 << " layer_disk=" << m_idHelper->layer_disk(det_ele->identify())
485 << " phi_module=" << m_idHelper->phi_module(det_ele->identify())
486 << " eta_module=" << m_idHelper->eta_module(det_ele->identify())
487 << " side=" << ModuleIdentifierMatchUtil::detail::getZeroOrSide(*m_idHelper,det_ele->identify())
488 << " columns_strips=" << module_data[4]);
489
490 // if histogramming is enabled lock also for the connected modules
491 // to avoid frequent lock/unlocks.
492 // Anyway the algorithm should run only once per job
493 MyLockGuard lock(m_histMutex, m_histogrammingEnabled);
494 if (m_histogrammingEnabled) {
495 histogramDefectModule(module_pattern_idx[match_i], hist_pattern_i, module_i, det_ele->center());
496 }
497
498 if constexpr(IdentifierHelperWithOtherSideConecpt<decltype(*m_idHelper)>) {
499 // propagate module defects to modules connected to the same physical sensor
500 // if configured accordingly.
501 assert( module_pattern_idx[match_i] < m_modulePattern.size());
502 ConnectedModulesUtil::visitMatchingConnectedModules(
503 *m_idHelper,
504 m_modulePattern[module_pattern_idx[match_i]],
505 *(detEleColl.cptr()),
506 module_i,
507 connected_modules,
508 [defects_ptr=defects.get(),
509 &counts,
510 module_pattern_i=module_pattern_idx[match_i],
511 hist_pattern_i,
512 this](unsigned int child_id_hash,const InDetDD::SiDetectorElement &connected_det_ele) {
513
514 (counts.back()[ kNDefects ])
515 += 1-defects_ptr->isModuleDefect(child_id_hash); // do not double count
516 defects_ptr->setModuleDefect(child_id_hash);
517
518 if (m_histogrammingEnabled) {
519 histogramDefectModule(module_pattern_i, hist_pattern_i, child_id_hash, connected_det_ele.center());
520 }
521
522 ATH_MSG_VERBOSE( "Propagate module defect to other split modules "
523 << " hash=" << m_idHelper->wafer_hash(connected_det_ele.identify())
524 << " barel_ec=" << m_idHelper->barrel_ec(connected_det_ele.identify())
525 << " layer_disk=" << m_idHelper->layer_disk(connected_det_ele.identify())
526 << " phi_module=" << m_idHelper->phi_module(connected_det_ele.identify())
527 << " eta_module=" << m_idHelper->eta_module(connected_det_ele.identify())
528 << " side=" << ModuleIdentifierMatchUtil::detail::getZeroOrSide(*m_idHelper, connected_det_ele.identify()) );
529 });
530 }
531 continue;
532 }
533 unsigned int hist_pattern_i = (m_fillHistogramsPerPattern ? module_pattern_idx.front() : 0 );
534
535 // throw number of defects of all defect types excluding module defects which are already handled
536 // e.g. chip-defects, core-column defects, single pixel or strip defects
537 unsigned int cells = helper.nCells();
538 std::vector<typename T_ModuleHelper::KEY_TYPE> &module_defects=(*defects).at(module_i);
539 module_defects.reserve( throwNumberOfDefects(std::span(&rndmEngine.data()[kMaskDefects], rndmEngine.size()-kMaskDefects),
540 module_pattern_idx,
541 T_ModuleHelper::nMasks(),
542 cells,
543 n_mask_defects) );
544
545 // if histogramming is enabled lock for the entire loop, since it would
546 // be rather inefficient to lock for each defect pixel or strip separately and
547 // this algorithm should run only a single time per job anyway.
548 MyLockGuard lock(m_histMutex, m_histogrammingEnabled);
549 auto [matrix_histogram_index, matrix_index]=(m_histogrammingEnabled
550 ? findHist(hist_pattern_i, helper.nSensorRows(), helper.nSensorColumns())
551 : std::make_pair(0u,0u));
552
553 // create defects starting from the mask (or group) defect covering the largest area (assuming
554 // that masks are in ascending order) e.g. defect chips, core-column defects, individual pixel
555 // or strip defects
556 auto masks = helper.masks();
557 for (unsigned int mask_i = masks.size(); mask_i-->0; ) {
558
559 assert( mask_i < n_mask_defects.size());
560 counts[mask_i][kNElements] += helper.nElements(mask_i);
561 unsigned int n_defects = n_mask_defects[mask_i];
562 if (n_defects>0) {
563 // module with mask (or group) defects i.e. core-column defects, defect chips, ...
564 assert( mask_i < counts.size());
565 counts[mask_i][kMaxDefectsPerModule] = std::max(counts[mask_i][kMaxDefectsPerModule],n_defects);
566 unsigned int current_defects = module_defects.size();
567 assert( !m_histogrammingEnabled || ( hist_pattern_i < m_hist.size() && matrix_histogram_index < m_hist[hist_pattern_i].size()) );
568 TH2 *h2 = (m_histogrammingEnabled ? m_hist.at(hist_pattern_i).at(matrix_histogram_index) : nullptr);
569
570 for (unsigned int defect_i=0; defect_i < n_defects; ++defect_i) {
571 // retry if a random position has been chosen already, but at most m_maxAttempts times
572 unsigned int attempt_i=0;
573 for (attempt_i=0; attempt_i<m_maxAttempts.value(); ++attempt_i) {
574 // chose random pixel or strip which defines the defect location
575 assert(kMaskDefects+mask_i < rndmEngine.size() );
576 unsigned int cell_idx=CLHEP::RandFlat::shoot(rndmEngine[kMaskDefects+mask_i],cells);
577
578 if (m_checkerBoardToggle) {
579 // for debugging
580 // restrict defects on checker board
581 cell_idx=makeCheckerboard(cell_idx,
582 helper.nSensorRows(),
583 helper.nSensorColumns(),
584 m_oddRowToggle.value(),
585 m_oddColToggle.value() );
586 }
587
588 std::pair<typename T_ModuleHelper::KEY_TYPE, typename T_ModuleHelper::KEY_TYPE> key_range;
589 if (mask_i==0 && m_perPatternAndMaskFractions[match_i].at(mask_i).size()>1) {
590 // for cell defects throw a group size,
591 // the fractions are actual fractions for group sizes of 1,2,... where the first element
592 // is the fraction for a group size of 1.
593 float group_size_prob=CLHEP::RandFlat::shoot(rndmEngine[kMaskDefects+mask_i],1.);
594 unsigned int group_size=m_perPatternAndMaskFractions.at(match_i).at(mask_i).size();
595 for (; group_size-->0 && group_size_prob <= m_perPatternAndMaskFractions[match_i][mask_i][group_size];);
596 ++group_size;
597 // squeeze the random row index into the number of rows reduced by the group size.
598 unsigned int sensor_row = std::min(static_cast<unsigned int>(getSensorRow(helper, cell_idx) %
599 (helper.nSensorRows() - group_size -1)),
600 static_cast<unsigned int>(helper.nSensorRows() - group_size - 1));
601 unsigned int sensor_col = getSensorColumn(helper, cell_idx);
602 key_range = std::make_pair( helper.hardwareCoordinates(sensor_row,
603 sensor_col),
604 helper.hardwareCoordinates(sensor_row+group_size,
605 sensor_col));
606 }
607 else {
608 key_range= T_ModuleHelper::makeRangeForMask(helper.hardwareCoordinates(getSensorRow(helper, cell_idx),
609 getSensorColumn(helper, cell_idx)),
610 masks[mask_i]);
611 }
612
613 if (EmulatedDefectsBuilder::insertKeyRange(helper, module_defects, key_range, T_ModuleHelper::makeDefectTypeKey(mask_i))) {
614 if (h2) {
615 histogramDefects(helper,key_range,h2);
616 }
617 break;
618 }
619
620 assert( mask_i+1 < counts.size());
621 ++counts[mask_i][ kNRetries ];
622 }
623 assert( mask_i+1 < counts.size());
624 counts[mask_i][kNMaxRtriesExceeded] += attempt_i >= m_maxAttempts.value();
625 }
626 unsigned int new_defects = module_defects.size() - current_defects;
627 assert( mask_i+1 < counts.size());
628 counts[mask_i][ kNDefects ] += new_defects;
629 if (new_defects>0) {
630 ++(counts[mask_i][kNModulesWithDefects]);
631 }
632 }
633 }
634
635 // Create corner defects only for modules with columns and rows.
636 if constexpr( T_ModuleHelper::ROW_BITS>0 && T_ModuleHelper::COL_BITS>0) {
637 if (!m_cornerDefectParamsPerPattern.empty() && !m_cornerDefectParamsPerPattern[module_pattern_idx.front()].empty()) {
638 if (module_pattern_idx.size()!=1) {
639 ATH_MSG_WARNING("Multiple module pattern match module " << module_i
640 << ", but for corner defects only the parameters are used which are associated to the first pattern" );
641 }
642 // create defects at certain corners outside of a circle which crosses the sensor edges at certain positions
643 // chosen in a random range. The edge crossing at the particular corner defines a sagitta of a certain value
644 // chosen in a random range
645
646 // throw the number of corners with corner defects.
647 float defect_prob=CLHEP::RandFlat::shoot(rndmEngine[kCornerDefects],1.);
648 unsigned int n_corners=m_perPatternCornerDefectNCornerCummulativeProb[module_pattern_idx.front() ].size();
649 for (;
650 n_corners>0 && defect_prob<m_perPatternCornerDefectNCornerCummulativeProb[module_pattern_idx.front() ][n_corners-1];
651 --n_corners);
652 unsigned int n_current_defects =module_defects.size();
653 if (n_corners<m_perPatternCornerDefectNCornerCummulativeProb[module_pattern_idx.front() ].size()) {
654 assert( !m_histogrammingEnabled || ( hist_pattern_i < m_hist.size() && matrix_histogram_index < m_hist[hist_pattern_i].size()) );
655 TH2 *h2 = (m_histogrammingEnabled ? m_hist.at(hist_pattern_i).at(matrix_histogram_index) : nullptr);
656
657 assert( n_corners < n_corner_combinations.size() );
658 // chose the corners which have defects randomly
659 unsigned int the_combination =CLHEP::RandFlat::shoot(rndmEngine[kCornerDefects],n_corner_combinations[n_corners] );
660 unsigned char corner_mask = corner_combinations[n_corners][the_combination];
661 // columns, rows and their pitch are already in direction of the hardware columns and rows
662 unsigned int hwa_columns = helper.columns();
663 unsigned int hwa_rows = helper.rows();
664 float hwa_columnPitch = helper.columnPitch();
665 float hwa_rowPitch = helper.rowPitch();
666
667 for (unsigned int corner_i=0; corner_i<4; ++corner_i) {
668 if (corner_mask & (1u << corner_i)) {
669 // chose random positions at which the circle crosses the edges at the particular corner
670 // and chose a random sagitta defined by the corssings.
671 assert( m_cornerDefectParamsPerPattern[module_pattern_idx.front()].size()==kNCornerDefectParams);
672 float rx=m_cornerDefectParamsPerPattern[module_pattern_idx.front()][kCornerDefectWidthColumnDirectionOffset]
673 +CLHEP::RandFlat::shoot(rndmEngine[kCornerDefects],m_cornerDefectParamsPerPattern[module_pattern_idx.front() ][kCornerDefectWidthColumnDirection]);
674 float ry=m_cornerDefectParamsPerPattern[module_pattern_idx.front() ][kCornerDefectkWidthRowDirectionOffset]
675 +CLHEP::RandFlat::shoot(rndmEngine[kCornerDefects],m_cornerDefectParamsPerPattern[module_pattern_idx.front() ][kCornerDefectkWidthRowDirection]);
676
677 float sagitta;
678 {
679 // make sure that the sagitta is small enough that the circle can actually cross the bounds.
680 // conditions: the circle radius Rc has to be larger than rx and ry. If the circle radius
681 // is equal to rx or ry, the circle will just touch the border.
682 // This leads to quadratic equations x^2 + 2*hp1/2 * x + q solved by x1/2,1/2 = -hp1/2 +- sqrt(hp1/2^2-q)
683 float r2=rx*rx+ry*ry;
684 float hp1=0.5*sqrt(r2)*rx/ry;
685 float hp2=0.5*sqrt(r2)*ry/rx;
686 float q=-0.25*r2;
687 float sagitta_max = std::min( (-hp1 + sqrt( hp1*hp1 -q )),(-hp2 + sqrt( hp2*hp2 -q )) );
688 float sagitta_range = m_cornerDefectParamsPerPattern[module_pattern_idx.front() ][kCornerDefectkSagitta];
689 float sagitta_offset = m_cornerDefectParamsPerPattern[module_pattern_idx.front() ][kCornerDefectkSagittaOffset];
690 sagitta_offset=std::min(sagitta_offset,sagitta_max);
691 sagitta_range=std::min( sagitta_range, sagitta_max - sagitta_offset);
692 if (sagitta_range != m_cornerDefectParamsPerPattern[module_pattern_idx.front() ][kCornerDefectkSagitta]
693 || sagitta_offset != m_cornerDefectParamsPerPattern[module_pattern_idx.front() ][kCornerDefectkSagittaOffset]) {
694 ATH_MSG_VERBOSE("limited sagitta range from "
695 << m_cornerDefectParamsPerPattern[module_pattern_idx.front() ][kCornerDefectkSagittaOffset]
696 << ".."
697 << (m_cornerDefectParamsPerPattern[module_pattern_idx.front() ][kCornerDefectkSagittaOffset]
698 + m_cornerDefectParamsPerPattern[module_pattern_idx.front() ][kCornerDefectkSagitta])
699 << " to " << sagitta_offset << ".." << (sagitta_offset+sagitta_range));
700 }
701 sagitta=sagitta_offset+CLHEP::RandFlat::shoot(rndmEngine[kCornerDefects],sagitta_range);
702 }
703
704 // compute column range with defects
705 double scale_rx=(corner_i==1 || corner_i==2) ? -1.f : 1.f;
706 double scale_ry=(corner_i<=1) ? -1.f : 1.f;
707 std::array<unsigned int,2> row_range{0,hwa_rows};
708 unsigned int row_var_idx=(corner_i<=1) ? 0 : 1;
709 unsigned int col_start=(corner_i==1 || corner_i==2) ? hwa_columns : 0;
710
711 Amg::Vector2D corner_pos{col_start*hwa_columnPitch,row_range[row_var_idx^1]*hwa_rowPitch};
712
713 unsigned int col_end = std::min(static_cast<unsigned int>(std::max(0,static_cast<int>(col_start)
714 + static_cast<int>(rx*scale_rx / hwa_columnPitch +.5))),
715 hwa_columns);
716 if (col_end < col_start) {
717 std::swap(col_start, col_end);
718 }
719
720 // compute circle parameters
721 Amg::Vector2D c0 = corner_pos + Amg::Vector2D{0.f,ry*scale_ry};
722 Amg::Vector2D c1 = corner_pos + Amg::Vector2D{rx*scale_rx,0.f};
723 double rho2=sqr(rx) + sqr(ry);
724 double radius = rho2/(8*sagitta)+0.5*sagitta;
725 Amg::Vector2D Rc = c0+(c1-c0)*.5 - (scale_ry*scale_rx) * (radius-sagitta)
726 /sqrt(rho2)
727 *Amg::Vector2D{-ry*scale_ry,-rx*scale_rx};
728
729 double ymin=std::min(c0[1],c1[1]);
730 double ymax=std::max(c0[1],c1[1]);
731 for (unsigned int col_i=col_start; col_i< col_end; ++col_i) {
732 //compute row range with defects per column i.e. the row at which the circle crosses the column
733 // and the corresponding edge row (top or bottom)
734 double q = sqr(col_i*hwa_columnPitch-Rc[0]) + sqr(Rc[1]) - sqr(radius);
735 double ph = -Rc[1];
736 double Q=sqr(ph)-q;
737 if (Q>=0.) {
738 double sQ=sqrt(Q);
739 double cx = (-ph+sQ );
740 if (cx<ymin || cx>=ymax) {
741 double cx2=(-ph-sQ );
742 // pick the physical solution and make sure that
743 // that the solution remains in the allowed range despite
744 // limited precision
745 if (cx2<ymin || cx2 >=ymax) {
746 double d1=std::min(std::abs(cx-ymin),std::abs(cx-ymax));
747 double d2=std::min(std::abs(cx2-ymin),std::abs(cx2-ymax));
748 if (d2<d1) {
749 cx=cx2;
750 }
751 cx = std::clamp( cx, ymin, ymax - hwa_rowPitch*.25);
752 }
753 else {
754 cx=cx2;
755 }
756 }
757 if (cx>=ymin && cx<ymax) {
758 row_range[row_var_idx]=static_cast<unsigned int>( std::max(0.,cx / hwa_rowPitch + .5));
759
760 if (row_range[0] < row_range[1] && row_range[0] <hwa_rows) {
761 // create corner defects for one column
762 std::pair<typename T_ModuleHelper::KEY_TYPE, typename T_ModuleHelper::KEY_TYPE> key_range{
763 helper.swapOfflineRowsColumns() ? helper.hardwareCoordinates(col_i, std::min(row_range[0],helper.nSensorColumns()-1u))
764 : helper.hardwareCoordinates(std::min(row_range[0],helper.nSensorRows()-1u),col_i),
765 helper.swapOfflineRowsColumns() ? helper.hardwareCoordinates(col_i, std::min(row_range[1]-1, helper.nSensorColumns()-1u))
766 : helper.hardwareCoordinates(std::min(row_range[1]-1,helper.nSensorRows()-1u),col_i)
767 };
768
769 if (EmulatedDefectsBuilder::insertKeyRange(helper, module_defects, key_range, T_ModuleHelper::makeDefectTypeKey(masks.size()>1 ? 1 : 0))) {
770 if (h2) {
771 histogramDefects(helper,key_range,h2);
772 }
773 }
774 }
775 }
776 }
777 }
778 }
779 }
780 // add corner defects to random pixel defects for histogramming
781 n_mask_defects[0] += module_defects.size() - n_current_defects;
782 }
783 }
784 }
785 if (m_histogrammingEnabled) {
786 fillPerModuleHistograms(module_pattern_idx.front(),hist_pattern_i,matrix_histogram_index, matrix_index, module_i,
787 T_ModuleHelper::nMasks(), n_mask_defects, det_ele->center());
788 }
789 n_defects_total+=module_defects.size();
790 }
791 }
792
793 {
794 // count defects
795 std::size_t det_ele_sz = detEleColl->size();
796 std::size_t module_defects=0ul;
797 std::size_t single_defects=0ul;
798 for (unsigned int module_i=0u; module_i < det_ele_sz; ++module_i) {
799 if (defects->isModuleDefect(module_i)) {
800 ++module_defects;
801 }
802 else {
803 std::optional<typename T_EmulatedDefects::KEY_TYPE> last;
804 for (auto key : defects->at(module_i)) {
805 if (T_ModuleHelper::isRangeKey(key) && last.has_value()) {
806 auto base_key = T_ModuleHelper::makeBaseKey(key);
807 auto last_base_key = T_ModuleHelper::makeBaseKey(*last);
808 // may overestimate the number of single defects since it will not respect row and or column
809 // limits
810 single_defects += std::max(base_key,last_base_key) - std::min(base_key,last_base_key) + 1;
811 }
812 else {
813 ++single_defects;
814 }
815 last=key;
816 }
817 }
818 }
819 ATH_MSG_INFO("Total module defects: " << module_defects <<
820 " , total single defects including ranges (ranges may exagerate defect count): " << single_defects);
821 }
822
823 m_modulesWithoutDefectParameters += module_without_matching_pattern;
824 if (!m_outputFile.value().empty()) {
825 if (hasExtensions(m_outputFile.value(),".json")) {
826 toJson(*defects, m_outputFile.value());
827 }
828 else if (hasExtensions(m_outputFile.value(),".root")) {
829 toRoot(*defects, m_outputFile.value());
830 }
831 }
832 ATH_CHECK( defectsOut.record (std::move(defects)) );
833
834 if (msgLvl(MSG::INFO)) {
835 printSummaryOfDefectGeneration(T_ModuleHelper::nMasks(), n_error, n_defects_total,counts);
836 }
837
838 return StatusCode::SUCCESS;
839 }
840
841}