ATLAS Offline Software
Loading...
Searching...
No Matches
SCT_ModuleVetoTool.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
4
10
11#include "SCT_ModuleVetoTool.h"
12#include <nlohmann/json.hpp>
13
14//STL includes
15#include <algorithm>
16#include <iterator>
17#include <iostream>
18#include <fstream>
19#include <vector>
20#include <string>
21
22//Athena includes
26
27using json = nlohmann::json;
28
29static const std::string databaseSignature{"database"};
30
31// Constructor
32SCT_ModuleVetoTool::SCT_ModuleVetoTool(const std::string& type, const std::string& name, const IInterface* parent) :
33 base_class(type, name, parent)
34{
35}
36
37//Initialize
38StatusCode
40 if (m_maskLayers and m_layersToMask.size()==0 and m_disksToMask.size()==0) {
41 ATH_MSG_INFO("Layer/Disk masking enabled, but no layer/disk specified!");
42 m_maskLayers = false;
43 }
44
45 if ((not m_maskLayers) and (m_layersToMask.size() or m_disksToMask.size())) {
46 ATH_MSG_INFO("Layer/Disk to mask specified, but masking is disabled!");
47 }
48
49 if ((not m_maskLayers) and m_maskSide!=-1) {
50 ATH_MSG_INFO("Layer/Disk side to mask specified, but masking is disabled!");
51 }
52
53 if (m_maskLayers and m_disksToMask.size() and (std::find(m_disksToMask.begin(), m_disksToMask.end(),0)!=m_disksToMask.end())) {
54 ATH_MSG_WARNING("0th Disk not defined (-N to N) - check your setup!");
55 }
56
57 if (detStore()->retrieve(m_pHelper, "SCT_ID").isFailure()) {
58 ATH_MSG_FATAL("SCT helper failed to retrieve");
59 return StatusCode::FAILURE;
60 } else {
61 ATH_MSG_INFO("Successfully retrieved SCT_ID helper");
62 }
63
64 // If "database" is found in m_badElements, COOL database is used.
65 m_useDatabase=(std::find(m_badElements.value().begin(), m_badElements.value().end(), databaseSignature) != m_badElements.value().end());
66 ATH_MSG_INFO("m_useDatabase is " << m_useDatabase);
67
68 if (not m_useDatabase) {
69 if (fillData().isFailure()) {
70 ATH_MSG_FATAL("Failed to fill data");
71 return StatusCode::FAILURE;
72 }
73 }
74
75 // Read Cond Handle
77
78 const std::string databaseUseString{m_useDatabase ? "" : "not "};
79 ATH_MSG_INFO("Initialized veto service with data, "
80 << (m_badElements.value().size() - static_cast<long unsigned int>(m_useDatabase))
81 << " elements declared bad. Database will " << databaseUseString << "be used.");
82
83 return StatusCode::SUCCESS;
84}
85
86//Finalize
87StatusCode
89 return StatusCode::SUCCESS;
90}
91
92bool
96
97bool
98SCT_ModuleVetoTool::isGood(const Identifier& elementId, const EventContext& ctx, InDetConditions::Hierarchy h) const {
99 if (not canReportAbout(h)) return true;
100
101 const Identifier waferId{m_pHelper->wafer_id(elementId)};
102
103 // Bad wafer in properties
104 if (m_localCondData.isBadWaferId(waferId)) return false;
105 // If database is not used, all wafer IDs here should be good.
106 if (not m_useDatabase) return true;
107
108 const SCT_ModuleVetoCondData* condData{getCondData(ctx)};
109 // If database cannot be retrieved, all wafer IDs are good.
110 if (condData==nullptr) return true;
111
112 // Return the result of database
113 return (not condData->isBadWaferId(waferId));
114}
115
116bool
118 const EventContext& ctx{Gaudi::Hive::currentContext()};
119 return isGood(elementId, ctx, h);
120}
121
122bool
123SCT_ModuleVetoTool::isGood(const IdentifierHash& hashId, const EventContext& ctx) const {
124 Identifier elementId{m_pHelper->wafer_id(hashId)};
125 return isGood(elementId, ctx, InDetConditions::SCT_SIDE);
126}
127
128bool
130 const EventContext& ctx{Gaudi::Hive::currentContext()};
131 return isGood(hashId, ctx);
132}
133
134void
137 std::vector<bool> &status = element_status.getElementStatus();
138 if (status.empty()) {
139 status.resize(m_pHelper->wafer_hash_max(),true);
140 }
141 for (const Identifier &wafer_id: m_localCondData.badWaferIds()) {
142 status.at( m_pHelper->wafer_hash(wafer_id) ) = false;
143 }
144 if (m_useDatabase) {
146 if (not condDataHandle.isValid()) {
147 ATH_MSG_ERROR("Failed to get " << m_condKey.key());
148 return;
149 }
150 const SCT_ModuleVetoCondData* condData{ condDataHandle.cptr() };
151 if (whandle) {
152 whandle->addDependency (condDataHandle);
153 }
154 if (condData) {
155 for (const Identifier &wafer_id: condData->badWaferIds()) {
156 status.at( m_pHelper->wafer_hash(wafer_id) ) = false;
157 }
158 }
159 }
160}
161
162
163StatusCode
165 // Reset SCT_ModuleVetoCondData
166 m_localCondData.clear();
167
168 // @TODO: This part should be changed to use PathResolver before using in production.
169
170 //Read bad module IDs from json file.
171 if(!m_JsonLocation.empty())
172 {
173 std::ifstream json_file(m_JsonLocation);
174 if (!json_file.is_open()) {
175 ATH_MSG_FATAL("Failed to open the json file.");
176 return StatusCode::FAILURE;
177 }
178
179 json data = json::parse(json_file);
180
181 for(const auto& i:data)
182 {
183 std::string id_mod = i["Decimal_ID"];
184 unsigned long long id_cstring = std::stoull(id_mod);
185 m_localCondData.setBadWaferId(Identifier(id_cstring));
186 ATH_MSG_DEBUG("Masking Module ID: " << id_cstring << ".");
187 }
188 return StatusCode::SUCCESS;
189 }
190
191
192 // Fill data based on properties
193 StatusCode sc{StatusCode::SUCCESS};
194 if ((m_badElements.value().size() - static_cast<int>(m_useDatabase)) == 0 and (not m_maskLayers)) {
195 ATH_MSG_INFO("No bad modules in job options.");
196 return sc;
197 }
198
199
200 bool success{true};
201 std::vector<std::string>::const_iterator pId{m_badElements.value().begin()};
202 std::vector<std::string>::const_iterator last{m_badElements.value().end()};
203 for(; pId not_eq last;++pId) {
204 unsigned long long idToWrite{static_cast<unsigned long long>(atoll(pId->c_str()))};
205 if (*pId != databaseSignature) success &= m_localCondData.setBadWaferId(Identifier(idToWrite));
206 }
207
208
209
210 if (m_maskLayers) {
211 ATH_MSG_INFO("Masking " << m_layersToMask.size() << " SCT Layers");
212 ATH_MSG_INFO("Masking " << m_disksToMask.size() << " SCT Disks");
213 for(unsigned int i{0}; i < m_pHelper->wafer_hash_max(); i++) {
214 Identifier mID{m_pHelper->wafer_id(i)};
215 int bec{m_pHelper->barrel_ec(mID)};
216 int side{m_pHelper->side(mID)};
217 int layer_disk{m_pHelper->layer_disk(mID)};
218
219 if ((bec == 0 and (m_maskSide==-1 or side==m_maskSide) and (std::find(m_layersToMask.begin(), m_layersToMask.end(), layer_disk ) != m_layersToMask.end())) or
220 (bec == 2 and (m_maskSide==-1 or side==m_maskSide) and (std::find(m_disksToMask.begin(), m_disksToMask.end(), (layer_disk + 1)) != m_disksToMask.end())) or
221 (bec == -2 and (m_maskSide==-1 or side==m_maskSide) and (std::find(m_disksToMask.begin(), m_disksToMask.end(), -1*(layer_disk + 1)) != m_disksToMask.end()))) {
222 ATH_MSG_DEBUG("Masking ID Hash " << i);
223 m_localCondData.setBadWaferId(mID);
224 }
225 }
226 }
227
228 ATH_MSG_INFO(m_localCondData.size() << " bad wafers are defined via properties.");
229
230 m_localCondData.setFilled();
231 ATH_MSG_DEBUG("Successfully filled bad SCT identifiers list");
232 return (success ? sc : StatusCode::FAILURE);
233}
234
236SCT_ModuleVetoTool::getCondData(const EventContext& ctx) const {
238 return condData.retrieve();
239}
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_FATAL(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
char data[hepevt_bytes_allocation_ATLAS]
Definition HepEvt.cxx:11
nlohmann::json json
static Double_t sc
This is an Identifier helper class for the SCT subdetector.
static const std::string databaseSignature
header file for tool allowing one to declare modules as bad
Header file for AthHistogramAlgorithm.
This is a "hash" representation of an Identifier.
const std::vector< bool > & getElementStatus() const
Class for data object used in SCT_ModuleVetoCondAlg, SCT_LinkMaskingCondAlg, SCT_ModuleVetoTool,...
const std::set< Identifier > & badWaferIds() const
bool isBadWaferId(const Identifier waferId) const
Check if a wafer ID is bad or not.
SG::ReadCondHandleKey< SCT_ModuleVetoCondData > m_condKey
IntegerProperty m_maskSide
StringProperty m_JsonLocation
SCT_ModuleVetoTool(const std::string &type, const std::string &name, const IInterface *parent)
IntegerArrayProperty m_disksToMask
virtual StatusCode finalize() override
virtual bool canReportAbout(InDetConditions::Hierarchy h) const override
Can the service report about the given component? (chip, module...)
StringArrayProperty m_badElements
const SCT_ID * m_pHelper
virtual bool isGood(const Identifier &elementId, InDetConditions::Hierarchy h=InDetConditions::DEFAULT) const override
Is the detector element good?
SCT_ModuleVetoCondData m_localCondData
IntegerArrayProperty m_layersToMask
const SCT_ModuleVetoCondData * getCondData(const EventContext &ctx) const
BooleanProperty m_maskLayers
virtual StatusCode initialize() override
virtual void getDetectorElementStatus(const EventContext &ctx, InDet::SiDetectorElementStatus &element_status, SG::WriteCondHandle< InDet::SiDetectorElementStatus > *whandle) const override
const_pointer_type retrieve()
const_pointer_type cptr()
void addDependency(const EventIDRange &range)