ATLAS Offline Software
Loading...
Searching...
No Matches
ITkPixelOfflineCalibCondAlg.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
5#include "Identifier/Identifier.h"
7#include "GaudiKernel/EventIDRange.h"
9#include <memory>
10#include <sstream>
11
12namespace ITk
13{
14
15PixelOfflineCalibCondAlg::PixelOfflineCalibCondAlg(const std::string& name, ISvcLocator* pSvcLocator):
16 ::AthCondAlgorithm(name, pSvcLocator)
17{
18}
19
20
22 ATH_MSG_DEBUG("ITkPixelOfflineCalibCondAlg::initialize()");
23
24 ATH_CHECK(detStore()->retrieve(m_pixelid, "PixelID")) ;
25
26 if (m_inputSource==2 && m_readKey.key().empty()) {
27 ATH_MSG_ERROR("The database is set to be input source (2) but the ReadKey is empty.");
28 }
29 ATH_CHECK(m_readKey.initialize());
30
31 ATH_CHECK(m_writeKey.initialize());
32
33 return StatusCode::SUCCESS;
34}
35
36
37
38StatusCode PixelOfflineCalibCondAlg::execute(const EventContext& ctx) const {
39 ATH_MSG_DEBUG("ITkPixelOfflineCalibCondAlg::execute()");
40
42 if (writeHandle.isValid()) {
43 ATH_MSG_DEBUG("CondHandle " << writeHandle.fullKey() << " is already valid.. In theory this should not be called, but may happen if multiple concurrent events are being processed out of order.");
44 return StatusCode::SUCCESS;
45 }
46
47 // Construct the output Cond Object and fill it in
48 std::unique_ptr<PixelOfflineCalibData> writeCdo(std::make_unique<PixelOfflineCalibData>());
49
50 if (m_inputSource==0) {
51 ATH_MSG_WARNING("So far do nothing!! return StatusCode::FAILURE");
52 return StatusCode::FAILURE;
53 }
54 else if (m_inputSource==1) {
55 ATH_MSG_WARNING("Pixel ITk constants read from text file. Only supported for local developments and debugging!");
56
57 auto calibData = std::make_unique<PixelOfflineCalibData>();
58
59 // Find and open the text file
60 ATH_MSG_INFO("Load ITkPixelErrorData constants from text file");
61 std::string fileName = PathResolver::find_file(m_textFileName, "DATAPATH");
62 if (fileName.empty()) { ATH_MSG_WARNING("Input file " << fileName << " not found! Default (hardwired) values to be used!"); }
63 else {
64 unsigned int n_entries = calibData->getClusterErrorData()->load(fileName);
65 ATH_MSG_DEBUG("Loaded " << n_entries << " from " << fileName << ".");
66 }
67
68 const EventIDBase start{EventIDBase::UNDEFNUM, EventIDBase::UNDEFEVT, 0, 0, EventIDBase::UNDEFNUM, EventIDBase::UNDEFNUM};
69 const EventIDBase stop {EventIDBase::UNDEFNUM, EventIDBase::UNDEFEVT, EventIDBase::UNDEFNUM-1, EventIDBase::UNDEFNUM-1, EventIDBase::UNDEFNUM, EventIDBase::UNDEFNUM};
70 const EventIDRange rangeW{start, stop};
71
72 ATH_MSG_DEBUG("Range of input is " << rangeW);
73
74 if (writeHandle.record(rangeW, std::move(writeCdo)).isFailure()) {
75 ATH_MSG_FATAL("Could not record PixelCalib::ITkPixelOfflineCalibData " << writeHandle.key() << " with EventRange " << rangeW << " into Conditions Store");
76 return StatusCode::FAILURE;
77 }
78 ATH_MSG_DEBUG("recorded new CDO " << writeHandle.key() << " with range " << rangeW << " into Conditions Store");
79
80 if (m_dump!=0) {
81 ATH_MSG_DEBUG("Dump the constants to file");
82 calibData->dump();
83 }
84 }
85
86 else if (m_inputSource==2) {
87
89 const CondAttrListCollection* readCdo{*readHandle};
90 if (readCdo==nullptr) {
91 ATH_MSG_FATAL("Null pointer to the read conditions object");
92 return StatusCode::FAILURE;
93 }
94
95 // Get the validitiy range
96 EventIDRange rangeW;
97 if (not readHandle.range(rangeW)) {
98 ATH_MSG_FATAL("Failed to retrieve validity range for " << readHandle.key());
99 return StatusCode::FAILURE;
100 }
101
102 ATH_MSG_DEBUG("Range of input is " << rangeW);
103
104 for(const auto & attrList : *readCdo){
105
106 std::ostringstream attrStr;
107 attrList.second.toOutputStream(attrStr);
108 ATH_MSG_DEBUG( "ChanNum " << attrList.first << " Attribute list " << attrStr.str() );
109
110 std::string stringData = attrList.second["data_array"].data<std::string>();
111 std::string delimiter = "],";
112 size_t pos = 0;
113 std::vector<std::string> component;
114 std::string buffer;
115 while ((pos = stringData.find(delimiter)) != std::string::npos) {
116 buffer = stringData.substr(0,pos);
117 component.push_back(buffer);
118 stringData.erase(0, pos + delimiter.length());
119 }
120 component.push_back(stringData);
121 ATH_MSG_INFO("Last component="<<stringData);
122
123 for (auto & i : component) {
124 std::string checkModule = i;
125 std::vector<std::string> moduleString;
126 delimiter = ":[";
127 pos = 0;
128 while ((pos = checkModule.find(delimiter)) != std::string::npos) {
129 buffer = checkModule.substr(0,pos);
130 moduleString.push_back(buffer);
131 checkModule.erase(0, pos + delimiter.length());
132 }
133 moduleString.push_back(std::move(checkModule));
134
135 if (moduleString.size()!=2) {
136 ATH_MSG_FATAL("String size (moduleString) is not 2. " << moduleString.size() << " in " << i << " channel " << attrList.first << " read from " << readHandle.fullKey());
137 return StatusCode::FAILURE;
138 }
139
140 std::stringstream checkModuleHash(moduleString[0]);
141 std::vector<std::string> moduleStringHash;
142 while (std::getline(checkModuleHash,buffer,'"')) { moduleStringHash.push_back(buffer); }
143
144 int waferHash = std::atoi(moduleStringHash[1].c_str());
145 IdentifierHash waferID_hash(waferHash);
146
147 std::stringstream moduleConstants(moduleString[1]);
148 std::vector<double> moduleConstantsVec;
149 while (std::getline(moduleConstants,buffer,',')) { moduleConstantsVec.emplace_back(std::atof(buffer.c_str())); }
150
151
152 std::array<double, ITk::PixelClusterErrorData::kNParam> param{};
153 std::span<double> param_src(param);
154
155 // Format v1 with no incident angle dependance
156 if(moduleConstantsVec.size()==4){
157 param[ITk::PixelClusterErrorData::kDelta_x_offset]= moduleConstantsVec[0]; // delta_x_offset
158 param[ITk::PixelClusterErrorData::kError_x] = moduleConstantsVec[1]; // delta_error_x
159 param[ITk::PixelClusterErrorData::kDelta_y_offset] = moduleConstantsVec[2]; // delta_y_offset
160 param[ITk::PixelClusterErrorData::kError_y] = moduleConstantsVec[3]; // delta_error_y
161 }
162
163 else if(moduleConstantsVec.size()==7){
164 assert( moduleConstantsVec.size()+1 == ITk::PixelClusterErrorData::kNParam);
167 param[0] = moduleConstantsVec[0]; // first value used for kPeriod_phi and kPeriod_sinheta
168 for (unsigned int idx=0; idx<moduleConstantsVec.size(); ++idx) {
169 param[idx+1] = moduleConstantsVec[idx];
170 }
171 }
172
173 // Format v3 with incident angle dependance + different eta-phi periods
174 else if(moduleConstantsVec.size()==8){
175 param_src = std::span<double>(moduleConstantsVec.begin(), moduleConstantsVec.end());
176 }
177 assert(writeCdo->getClusterErrorData());
178 writeCdo->getClusterErrorData()->setDeltaError(waferID_hash,param_src);
179 }
180
181 }
182
183
184 if (writeHandle.record(rangeW, std::move(writeCdo)).isFailure()) {
185 ATH_MSG_FATAL("Could not record PixelCalib::ITkPixelOfflineCalibData " << writeHandle.key() << " with EventRange " << rangeW << " into Conditions Store");
186 return StatusCode::FAILURE;
187 }
188 ATH_MSG_DEBUG("recorded new CDO " << writeHandle.key() << " with range " << rangeW << " into Conditions Store");
189
190 }
191
192 return StatusCode::SUCCESS;
193}
194
195} // namespace ITk
#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)
const ServiceHandle< StoreGateSvc > & detStore() const
Base class for conditions algorithms.
This class is a collection of AttributeLists where each one is associated with a channel number.
PixelOfflineCalibCondAlg(const std::string &name, ISvcLocator *pSvcLocator)
Gaudi::Property< std::string > m_textFileName
SG::ReadCondHandleKey< CondAttrListCollection > m_readKey
virtual StatusCode execute(const EventContext &ctx) const override final
SG::WriteCondHandleKey< ITk::PixelOfflineCalibData > m_writeKey
virtual StatusCode initialize() override final
This is a "hash" representation of an Identifier.
static std::string find_file(const std::string &logical_file_name, const std::string &search_path)
bool range(EventIDRange &r)
const std::string & key() const
const DataObjID & fullKey() const
const std::string & key() const
StatusCode record(const EventIDRange &range, T *t)
record handle, with explicit range DEPRECATED
const DataObjID & fullKey() const