Loading [MathJax]/extensions/tex2jax.js
ATLAS Offline Software
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 
12 namespace ITk
13 {
14 
15 PixelOfflineCalibCondAlg::PixelOfflineCalibCondAlg(const std::string& name, ISvcLocator* pSvcLocator):
16  ::AthReentrantAlgorithm(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  }
30 
32 
33  return StatusCode::SUCCESS;
34 }
35 
36 
37 
38 StatusCode 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(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
python.PyKernel.retrieve
def retrieve(aClass, aKey=None)
Definition: PyKernel.py:110
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
SG::ReadCondHandle
Definition: ReadCondHandle.h:44
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
PathResolver::find_file
static std::string find_file(const std::string &logical_file_name, const std::string &search_path, SearchType search_type=LocalSearch)
Definition: PathResolver.cxx:251
ITk::PixelClusterErrorData::kPeriod_phi
@ kPeriod_phi
Definition: ITkPixelClusterErrorData.h:29
ITk::PixelOfflineCalibCondAlg::m_writeKey
SG::WriteCondHandleKey< ITk::PixelOfflineCalibData > m_writeKey
Definition: ITkPixelOfflineCalibCondAlg.h:47
mergePhysValFiles.start
start
Definition: DataQuality/DataQualityUtils/scripts/mergePhysValFiles.py:14
ITkPixelOfflineCalibCondAlg.h
ITk::PixelOfflineCalibCondAlg::initialize
virtual StatusCode initialize() override final
Definition: ITkPixelOfflineCalibCondAlg.cxx:21
ITk::PixelOfflineCalibCondAlg::m_inputSource
Gaudi::Property< int > m_inputSource
Definition: ITkPixelOfflineCalibCondAlg.h:35
PixelModuleFeMask_create_db.stop
int stop
Definition: PixelModuleFeMask_create_db.py:76
SG::VarHandleKey::key
const std::string & key() const
Return the StoreGate ID for the referenced object.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:141
SG::WriteCondHandle::record
StatusCode record(const EventIDRange &range, T *t)
record handle, with explicit range DEPRECATED
Definition: WriteCondHandle.h:161
ITk::PixelOfflineCalibCondAlg::m_pixelid
const PixelID * m_pixelid
Definition: ITkPixelOfflineCalibCondAlg.h:49
CondAttrListCollection
This class is a collection of AttributeLists where each one is associated with a channel number....
Definition: CondAttrListCollection.h:52
AthCommonDataStore< AthCommonMsg< Gaudi::Algorithm > >::detStore
const ServiceHandle< StoreGateSvc > & detStore() const
The standard StoreGateSvc/DetectorStore Returns (kind of) a pointer to the StoreGateSvc.
Definition: AthCommonDataStore.h:95
AthReentrantAlgorithm
An algorithm that can be simultaneously executed in multiple threads.
Definition: AthReentrantAlgorithm.h:74
ITk::PixelClusterErrorData::kDelta_x_offset
@ kDelta_x_offset
Definition: ITkPixelClusterErrorData.h:32
python.AthDsoLogger.delimiter
delimiter
Definition: AthDsoLogger.py:71
createCoolChannelIdFile.buffer
buffer
Definition: createCoolChannelIdFile.py:12
FortranAlgorithmOptions.fileName
fileName
Definition: FortranAlgorithmOptions.py:13
SG::WriteCondHandle::fullKey
const DataObjID & fullKey() const
Definition: WriteCondHandle.h:45
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
lumiFormat.i
int i
Definition: lumiFormat.py:85
ITk::PixelClusterErrorData::kError_y
@ kError_y
Definition: ITkPixelClusterErrorData.h:36
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
ITk::PixelOfflineCalibCondAlg::PixelOfflineCalibCondAlg
PixelOfflineCalibCondAlg(const std::string &name, ISvcLocator *pSvcLocator)
Definition: ITkPixelOfflineCalibCondAlg.cxx:15
CxxUtils::atof
double atof(std::string_view str)
Converts a string into a double / float.
Definition: Control/CxxUtils/Root/StringUtils.cxx:91
PathResolver.h
ITk::PixelClusterErrorData::kDelta_y_offset
@ kDelta_y_offset
Definition: ITkPixelClusterErrorData.h:35
ITk
Definition: ITkPixelOfflineCalibCondAlg.cxx:13
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:228
SG::WriteCondHandle::key
const std::string & key() const
Definition: WriteCondHandle.h:44
IdentifierHash.h
SG::CondHandleKey::initialize
StatusCode initialize(bool used=true)
ITk::PixelClusterErrorData::kError_x
@ kError_x
Definition: ITkPixelClusterErrorData.h:33
python.LumiBlobConversion.pos
pos
Definition: LumiBlobConversion.py:18
ITk::PixelOfflineCalibCondAlg::m_dump
Gaudi::Property< int > m_dump
Definition: ITkPixelOfflineCalibCondAlg.h:41
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
ITk::PixelOfflineCalibCondAlg::execute
virtual StatusCode execute(const EventContext &ctx) const override final
Definition: ITkPixelOfflineCalibCondAlg.cxx:38
ITk::PixelClusterErrorData::kPeriod_sinheta
@ kPeriod_sinheta
Definition: ITkPixelClusterErrorData.h:30
LArNewCalib_DelayDump_OFC_Cali.idx
idx
Definition: LArNewCalib_DelayDump_OFC_Cali.py:69
SG::WriteCondHandle::isValid
bool isValid() const
Definition: WriteCondHandle.h:252
ITk::PixelClusterErrorData::kNParam
@ kNParam
Definition: ITkPixelClusterErrorData.h:37
CxxUtils::atoi
int atoi(std::string_view str)
Helper functions to unpack numbers decoded in string into integers and doubles The strings are requir...
Definition: Control/CxxUtils/Root/StringUtils.cxx:85
IdentifierHash
This is a "hash" representation of an Identifier. This encodes a 32 bit index which can be used to lo...
Definition: IdentifierHash.h:25
SG::WriteCondHandle
Definition: WriteCondHandle.h:26
ITk::PixelOfflineCalibCondAlg::m_readKey
SG::ReadCondHandleKey< CondAttrListCollection > m_readKey
Definition: ITkPixelOfflineCalibCondAlg.h:44
ITk::PixelOfflineCalibCondAlg::m_textFileName
Gaudi::Property< std::string > m_textFileName
Definition: ITkPixelOfflineCalibCondAlg.h:38