2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
5#include "Identifier/Identifier.h"
6#include "AtlasDetDescr/AtlasDetectorID.h"
7#include "EventContainers/IdentifiableContainerBase.h"
8#include "InDetRawData/InDetRawDataCollection.h"
9#include "InDetRawData/SCT3_RawData.h"
10#include "InDetRawData/Pixel1RawData.h"
11#include "InDetReadoutGeometry/SiDetectorElement.h"
13#include "AthenaKernel/RNGWrapper.h"
14#include "CLHEP/Random/RandPoisson.h"
15#include "CLHEP/Random/RandFlat.h"
17#include "StoreGate/WriteHandle.h"
26 template <class T_RDO_Container>
27 bool DefectsEmulatorAlg<T_RDO_Container>::setModuleData(const ActsDetectorElement &acts_detector_element,
28 ModuleIdentifierMatchUtil::ModuleData_t &module_data) const {
29 // It is assumed that a module is unambiguously identified by the detector type and the id hash,
30 // and all modules with the same detector type are relevant for the given RDO container.
31 if (acts_detector_element.detectorType() == DETECTOR_TYPE) {
32 const auto*si_detector_element = dynamic_cast<const InDetDD::SiDetectorElement*>(acts_detector_element.upstreamDetectorElement());
33 if(si_detector_element != nullptr) {
34 const T_ModuleDesign &moduleDesign = dynamic_cast<const T_ModuleDesign &>(si_detector_element->design());
35 ModuleIdentifierMatchUtil::setModuleData(*m_idHelper,
36 acts_detector_element.identify(),
45 template <class T_RDO_Container>
46 StatusCode DefectsEmulatorAlg<T_RDO_Container>::initialize(){
47 ATH_CHECK( m_emulatedDefects.initialize() );
48 ATH_CHECK( m_rdoOutContainerKey.initialize() );
49 ATH_CHECK( m_origRdoContainerKey.initialize() );
50 ATH_CHECK( detStore()->retrieve(m_idHelper, m_idHelperName.value()) );
52 return DefectsEmulatorBase::initializeBase(m_idHelper->wafer_hash_max());
55 template <class T_RDO_Container>
56 StatusCode DefectsEmulatorAlg<T_RDO_Container>::execute(const EventContext& ctx) const {
57 SG::ReadCondHandle<T_DefectsData> emulatedDefects(m_emulatedDefects,ctx);
58 ATH_CHECK(emulatedDefects.isValid());
59 SG::ReadHandle<T_RDO_Container> origRdoContainer(m_origRdoContainerKey, ctx);
60 ATH_CHECK(origRdoContainer.isValid());
61 SG::WriteHandle<T_RDO_Container> rdoOutContainer(m_rdoOutContainerKey, ctx);
62 ATH_CHECK( rdoOutContainer.record (std::make_unique<T_RDO_Container>(origRdoContainer->size(), EventContainers::Mode::OfflineFast)) );
64 CLHEP::HepRandomEngine *rndmEngine{};
65 if (!m_noiseProbability.value().empty()){
66 m_rndmSvc->getEngine(this, m_rngName);
67 ATHRNG::RNGWrapper* rngWrapper = m_rndmSvc->getEngine(this, m_rngName);
68 rngWrapper->setSeed( m_rngName, ctx );
69 rndmEngine = rngWrapper->getEngine(ctx);
72 unsigned int n_rejected=0u;
73 unsigned int n_split_rdos=0u;
75 T_ID_Adapter id_helper(m_idHelper);
76 unsigned int n_added_noise_hits=0u;
77 unsigned int n_defect_noise_hits=0u;
79 std::vector<typename T_ModuleHelper::KEY_TYPE> sorted_keys;
80 for(const InDetRawDataCollection<T_RDORawData>* collection : *origRdoContainer ) {
81 const IdentifierHash idHash = collection->identifyHash();
82 const auto &det_ele=emulatedDefects->getDetectorElement(idHash);
83 T_ModuleHelper module_helper( det_ele.design() );
84 std::unique_ptr<InDetRawDataCollection<T_RDORawData> >
85 clone = std::make_unique<InDetRawDataCollection<T_RDORawData> >(idHash);
86 clone->setIdentifier(collection->identify());
87 unsigned int rejected_per_mod=0u;
88 unsigned int noise_per_mod=0u;
89 if (!emulatedDefects->isModuleDefect(idHash)) {
90 unsigned int n_cells = module_helper.nCells();
91 unsigned int n_noise_hits=rndmEngine && m_noiseParamIdx.at(idHash)<m_noiseProbability.size()
92 ? CLHEP::RandPoisson::shoot(rndmEngine,
93 n_cells * m_noiseProbability.value()[ m_noiseParamIdx[idHash] ] )
95 clone->reserve( collection->size() + n_noise_hits);
97 ATH_MSG_ERROR( "Not module design for " << idHash);
98 return StatusCode::FAILURE;
100 TH2 *h2_rejected_hits=nullptr;
101 TH2 *h2_noise_hits=nullptr;
102 TH1 *h1_noise_shape=nullptr;
103 if (m_histogrammingEnabled) {
104 std::lock_guard<std::mutex> lock(m_histMutex);
105 std::tuple<TH2 *, TH2 *, TH1 *> hists = findHist(module_helper.nSensorRows(), module_helper.nSensorColumns());
106 h2_rejected_hits=std::get<0>(hists);
107 h2_noise_hits=std::get<1>(hists);
108 h1_noise_shape=std::get<2>(hists);
112 sorted_keys.reserve( collection->size()+n_noise_hits);
113 for(const auto *const rdo : *collection) {
114 const Identifier rdoID = rdo->identify();
116 auto row_idx = id_helper.row_index(rdoID);
117 auto col_idx = id_helper.col_index(rdoID);
119 unsigned int n_new_hits = id_helper.cloneOrRejectHit( module_helper, *(emulatedDefects.cptr()), idHash, row_idx, col_idx, *rdo, *clone);
121 n_split_rdos += (n_new_hits-1);
122 for (unsigned int hit_i=n_new_hits; hit_i>0; --hit_i) {
123 auto new_hit = clone->at(clone->size()-hit_i);
125 const Identifier rdoID = new_hit->identify();
126 auto row_idx = id_helper.row_index(rdoID);
127 auto col_idx = id_helper.col_index(rdoID);
128 unsigned int nConsecutiveRows = id_helper.nConsecutiveRows(*rdo);
129 for (unsigned int row_seq_i=0; row_seq_i<nConsecutiveRows; ++row_seq_i) {
130 unsigned int key = module_helper.hardwareCoordinates(row_idx+row_seq_i,col_idx);
131 auto iter = std::lower_bound(sorted_keys.begin(), sorted_keys.end(), key);
132 if (iter == sorted_keys.end()) {
133 sorted_keys.push_back(key);
137 sorted_keys.insert(iter, key);
144 if (m_histogrammingEnabled && h2_rejected_hits) {
145 std::lock_guard<std::mutex> lock(m_histMutex);
146 h2_rejected_hits->Fill(col_idx, row_idx);
147 if (m_posHist[kZR]) {
148 InDetDD::SiCellId si_cell_id = det_ele.cellIdFromIdentifier(rdoID);
149 auto local_pos = det_ele.design().localPositionOfCell(si_cell_id);
150 Amg::Vector3D global_pos(det_ele.globalPosition(Amg::Vector3D{local_pos.xPhi(),local_pos.xEta(),0.}));
151 double r=std::sqrt(std::pow(global_pos(0,0),2)+ std::pow(global_pos(1,0),2));
152 m_posHist[kZR]->Fill(global_pos(2,0),r);
153 m_posHist[kXY]->Fill(global_pos(0,0),global_pos(1,0));
161 // add random noise hits
162 for (unsigned int i=0; i<n_noise_hits; ++i) {
163 for (unsigned int attempt_i=0; attempt_i<10; ++attempt_i) {
164 unsigned int cell_idx=CLHEP::RandFlat::shoot(rndmEngine,n_cells); // %pixels;
165 unsigned int row_idx = cell_idx % module_helper.rows();
166 unsigned int col_idx = cell_idx / module_helper.rows();
167 unsigned int key = module_helper.hardwareCoordinates(row_idx,col_idx);
168 if (emulatedDefects->isDefect(module_helper, idHash, row_idx, col_idx)) {
169 ++n_defect_noise_hits;
172 auto iter = std::lower_bound(sorted_keys.begin(), sorted_keys.end(), key);
173 if (iter == sorted_keys.end()) {
174 sorted_keys.push_back(key);
178 sorted_keys.insert(iter, key);
184 float prob = CLHEP::RandFlat::shoot(rndmEngine,1.);
185 assert( m_noiseParamIdx[idHash] < m_noiseShapeCummulative.size() );
186 const std::vector<float> &noise_shape = m_noiseShapeCummulative[ m_noiseParamIdx[idHash] ];
187 unsigned int noise_value=0;
188 for (noise_value=0; noise_value<noise_shape.size(); ++noise_value) {
189 if (noise_shape[noise_value]>prob) {
194 clone->push_back( id_helper.createNoiseHit( module_helper, collection->identify(), cell_idx, noise_value).release() );
195 unsigned int nConsecutiveRows = id_helper.nConsecutiveRows(*clone->back());
196 if (nConsecutiveRows!=1u) {
197 ATH_MSG_ERROR("Noise hit has group size larger than 1. This may create duplicate hits.");
200 if (m_histogrammingEnabled && h2_noise_hits) {
201 unsigned int row_idx = cell_idx % module_helper.rows();
202 unsigned int col_idx = cell_idx / module_helper.rows();
204 std::lock_guard<std::mutex> lock(m_histMutex);
205 h2_noise_hits->Fill(col_idx, row_idx);
206 h1_noise_shape->Fill(noise_value);
216 rejected_per_mod += collection->size();
218 if (m_histogrammingEnabled) {
219 std::lock_guard<std::mutex> lock(m_histMutex);
220 unsigned int bin_i=m_moduleHist[kRejectedHits]->GetBin( idHash%100+1, idHash/100+1);
221 assert(m_moduleHist[kRejectedHits]->GetNbinsX() == m_moduleHist[kNoiseHits]->GetNbinsX());
222 assert(m_moduleHist[kRejectedHits]->GetNbinsY() == m_moduleHist[kNoiseHits]->GetNbinsY());
223 m_moduleHist[kRejectedHits]->SetBinContent(bin_i, rejected_per_mod );
224 m_moduleHist[kNoiseHits]->SetBinContent(bin_i, noise_per_mod );
226 n_added_noise_hits += noise_per_mod;
227 n_rejected += rejected_per_mod;
228 n_new += clone->size();
229 rdoOutContainer->addCollection( clone.release(), idHash).ignore();
231 ATH_MSG_DEBUG("rejected " << n_rejected << ", copied " << n_new << " RDOs"
232 << " noise " << n_added_noise_hits << " (+" << n_defect_noise_hits << ")"
233 << " split hits: " << n_split_rdos);
234 m_rejectedRDOs += n_rejected;
235 m_totalRDOs += n_new;
236 m_totalNoise += n_added_noise_hits;
237 m_splitRDOs += n_split_rdos;
239 return StatusCode::SUCCESS;