ATLAS Offline Software
PixelCablingCondAlg.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #include "PixelCablingCondAlg.h"
6 #include "GaudiKernel/EventIDRange.h"
8 
9 #include <iostream>
10 #include <fstream>
11 #include <sstream>
12 
14 #include "Identifier/Identifier.h"
15 #include "Identifier/IdContext.h"
16 #include "CoralBase/Blob.h"
17 
18 
19 PixelCablingCondAlg::PixelCablingCondAlg(const std::string& name, ISvcLocator* pSvcLocator):
20  ::AthReentrantAlgorithm(name, pSvcLocator)
21 {
22 }
23 
25  ATH_MSG_DEBUG("PixelCablingCondAlg::initialize()");
26 
27  ATH_CHECK(detStore()->retrieve(m_pixelID,"PixelID"));
28 
32 
34 
35  return StatusCode::SUCCESS;
36 }
37 
38 StatusCode PixelCablingCondAlg::execute(const EventContext& ctx) const {
39  ATH_MSG_DEBUG("PixelCablingCondAlg::execute()");
40 
42  ATH_MSG_DEBUG("Conditions updates every event!!! This should be avoided once RegionSelectorTable is fixed!!");
43  if (writeHandle.isValid()) {
44  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.");
45  return StatusCode::SUCCESS;
46  }
47 
48  // Construct the output Cond Object and fill it in
49  std::unique_ptr<PixelCablingCondData> writeCdo(std::make_unique<PixelCablingCondData>());
50 
51  // Signed values
52  int barrel_ec, eta_module;
53 
54  // Unsigned 32 bit values
55  uint32_t layer_disk, phi_module;
56  uint32_t robid, rodid;
57  uint32_t sl_40_fmt, sl_40_link, sl_80_fmt, sl_80_link;
58 
59  // Unsigned 64-bit values
60  uint64_t onlineId = 0;
61  uint64_t linknumber = 0;
62 
63  // Strings
64  std::string DCSname;
65  std::string line;
66 
68  const PixelModuleData *moduleData = *moduleDataHandle;
69 
70  // For debugging purposes
71  std::ofstream output_mapping_file_raw;
72  if (moduleData->getCablingMapToFile()) { output_mapping_file_raw.open("pixel_cabling_map_raw.txt"); }
73  std::ofstream output_mapping_file_interpreted;
74  if (moduleData->getCablingMapToFile()) { output_mapping_file_interpreted.open("pixel_cabling_map_interpreted.txt"); }
75 
76  std::stringstream instr;
77  if (!m_readKey.empty()) {
79  const AthenaAttributeList* readCdo = *readHandle;
80  if (readCdo==nullptr) {
81  ATH_MSG_FATAL("Null pointer to the read conditions object");
82  return StatusCode::FAILURE;
83  }
84  writeHandle.addDependency(readHandle);
85 
86  ATH_MSG_DEBUG("Size of AthenaAttributeList " << readHandle.fullKey() << " readCdo->size()= " << readCdo->size());
87  ATH_MSG_DEBUG("Range of input is " << readHandle.getRange());
88 
89  const coral::Blob& blob_cabling=(*readCdo)["CablingMapData"].data<coral::Blob>();
90  const char* p_cabling = static_cast<const char*>(blob_cabling.startingAddress());
91 
92  unsigned int len_cabling = blob_cabling.size()/sizeof(char);
93  ATH_MSG_DEBUG("blob_cabling.size() = " << blob_cabling.size() << ", len_cabling = " << len_cabling);
94 
95  instr.str(std::string(p_cabling,blob_cabling.size()));
96  }
97  else {
98  const std::string &cablingFilename = moduleData->getCablingMapFileName();
99  const std::string filename = PathResolverFindCalibFile(cablingFilename);
100  if (filename.empty()) {
101  ATH_MSG_FATAL("Mapping File: " << cablingFilename << " not found!");
102  return StatusCode::FAILURE;
103  }
104  std::ifstream fin(filename.c_str());
105  if (!fin) { return StatusCode::FAILURE; }
106  instr << fin.rdbuf();
107 
108  writeHandle.addDependency(IOVInfiniteRange::infiniteRunLB()); //When reading from file, use infinite IOV
109  ATH_MSG_DEBUG("Refilled pixel cabling from file \"" << cablingFilename << "\"");
110  }
111 
112  // Each entry in the mapping is sepated by a newline.
113  // Loop over all lines and parse the values
114  std::map<uint32_t,bool> rodReadoutMap = SG::ReadCondHandle<PixelReadoutSpeedData>(m_readoutspeedKey, ctx)->getReadoutMap();
115 
116  while (instr.good() && getline(instr, line)) {
117 
118  if (moduleData->getCablingMapToFile()) { output_mapping_file_raw << line << std::endl; }
119 
120  // Skip empty lines and comments (i.e. starting with a hash or a space)
121  if (line.empty()) { continue; }
122  if (line[0]==' ' || line[0]=='#') { continue; }
123 
124  // There are 11 columns, so there must be at least 21 characters in a valid line.
125  if (line.length()<21) { continue; }
126 
127  // If reading from COOL, skip the datestamp
128  if (line.substr(line.length()-3,line.length())=="GMT") { continue; }
129 
130  std::istringstream parse(line);
131  parse >> barrel_ec >> layer_disk >> phi_module >> eta_module >> std::hex >> robid >> rodid >> sl_40_fmt >> sl_40_link >> sl_80_fmt >> sl_80_link >> DCSname;
132 
133  // Debug
134  if (moduleData->getCablingMapToFile()) {
135  output_mapping_file_interpreted << barrel_ec << "\t" << layer_disk << "\t" << phi_module << "\t"
136  << eta_module << "\t" << std::hex << robid << "\t" << rodid << "\t"
137  << sl_40_fmt << "\t" << sl_40_link << "\t" << sl_80_fmt << "\t"
138  << sl_80_link << "\t" << DCSname << std::dec << std::endl;
139  }
140 
141  // Get the offline ID for this module
142  Identifier offlineId = m_pixelID->wafer_id(barrel_ec,layer_disk,phi_module,eta_module);
143 
144  // Set linknumber for IBL / DBM entries
145  if ((robid & 0xFFFFFF)>=0x140000) {
146  linknumber = sl_40_link | (sl_40_fmt<<4) | (sl_80_link<<8) | (sl_80_fmt<<12);
147  }
148  // Set linknumber for pixel entries
149  else {
150  bool readoutSpeed = false;
151  if (rodReadoutMap.find(rodid)!=rodReadoutMap.end()) { readoutSpeed=rodReadoutMap[rodid]; }
152 
153  if (!readoutSpeed) { linknumber=(sl_40_link & 0xF) | ((sl_40_fmt & 0xF)<<4); }
154  else { linknumber=(sl_80_link & 0xF) | ((sl_80_fmt & 0xF)<<4); }
155  }
156 
157  // Compute onlineId
158  onlineId = (robid & 0xFFFFFF) | (linknumber<<24);
159 
160  IdentifierHash hashId;
161 
162  // Do checks to verify consistency
163  IdContext cntxpixel = m_pixelID->wafer_context();
164  if (m_pixelID->get_hash(offlineId, hashId, &cntxpixel)) {
165  ATH_MSG_WARNING("Could not get hash from offlineId");
166  }
167 
168  if (hashId>m_pixelID->wafer_hash_max()) {
169  ATH_MSG_ERROR("IdHash overflow! HashId is 0x" << std::hex << hashId);
170  ATH_MSG_ERROR("not mapped OfflineID: " << std::hex << offlineId << std::dec << " barrel_ec: " << barrel_ec
171  << " layer_disk: " << layer_disk << " phi_module: " << phi_module << " eta_module: " << eta_module);
172  ATH_MSG_ERROR("to OnlineID: 0x" << std::hex << onlineId << " robid: 0x" << robid << " rodid: 0x" << rodid << std::dec
173  << " link: 0x" << std::hex /*<< link*/ << " -> Linknumber: 0x" << linknumber << " HashId: 0x" << hashId << std::dec);
174 
175  // Check if offlineId fail was caused by exceeding eta_module range
176  if (eta_module>m_pixelID->eta_module_max(offlineId) || eta_module<m_pixelID->eta_module_min(offlineId)) {
177  // eta_module_max == -999 indicates the module does not exist
178  if (m_pixelID->eta_module_max(offlineId)==-999 && m_pixelID->eta_module_min(offlineId)==-999) {
179  ATH_MSG_ERROR("Module does not exist in geometry");
180  }
181  else {
182  ATH_MSG_ERROR("eta_module range exceeded: Got eta_module = " << eta_module
183  << ", but allowed range is [" << m_pixelID->eta_module_min(offlineId)
184  << "," << m_pixelID->eta_module_max(offlineId) << "]");
185  ATH_MSG_ERROR("Input geometry tag may not be compatible with mapping file");
186  }
187  }
188  }
189 
190  // Fill the maps
191  writeCdo->add_entry_onoff(onlineId, offlineId);
192  writeCdo->add_entry_offon(offlineId, onlineId);
193  writeCdo->add_entry_offlineList(robid,offlineId);
194  writeCdo->add_entry_offrob(offlineId, robid);
195  writeCdo->add_entry_rodrob(rodid, robid);
196  writeCdo->add_entry_robrod(robid, rodid);
197  writeCdo->add_entry_DCSoffline(DCSname, offlineId);
198 
199  // Debug messages
200  ATH_MSG_DEBUG("Mapped offlineID: " << std::hex << offlineId << " to onlineID: 0x" << onlineId
201  << ", robID: 0x" << robid << ", barrel_ec: " << std::dec << barrel_ec << ", layer_disk: " << layer_disk
202  << ", eta_module: " << eta_module << ", phi_module: " << phi_module << ", linknumber: 0x" << std::hex << linknumber);
203  }
204 
205  if (moduleData->getCablingMapToFile()) {
206  output_mapping_file_raw.close();
207  output_mapping_file_interpreted.close();
208  }
209 
210  ATH_MSG_DEBUG("Size of ROD readoutspeed map: " << rodReadoutMap.size());
211  writeCdo->set_readout_map(rodReadoutMap);
212 
213  if (writeHandle.record(std::move(writeCdo)).isFailure()) {
214  ATH_MSG_FATAL("Could not record PixelCablingCondData " << writeHandle.key() << " with EventRange " << writeHandle.getRange() << " into Conditions Store");
215  return StatusCode::FAILURE;
216  }
217  ATH_MSG_DEBUG("recorded new CDO " << writeHandle.key() << " with range " << writeHandle.getRange() << " into Conditions Store");
218 
219  return StatusCode::SUCCESS;
220 }
221 
python.PyKernel.retrieve
def retrieve(aClass, aKey=None)
Definition: PyKernel.py:110
PixelCablingCondAlg::m_writeKey
SG::WriteCondHandleKey< PixelCablingCondData > m_writeKey
Definition: PixelCablingCondAlg.h:55
PixelID::get_hash
virtual int get_hash(const Identifier &id, IdentifierHash &hash_id, const IdContext *context=0) const override final
Create hash id from compact id (return == 0 for OK)
Definition: PixelID.cxx:899
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
checkFileSG.line
line
Definition: checkFileSG.py:75
PixelCablingCondAlg::PixelCablingCondAlg
PixelCablingCondAlg(const std::string &name, ISvcLocator *pSvcLocator)
Definition: PixelCablingCondAlg.cxx:19
SG::ReadCondHandle::getRange
const EventIDRange & getRange()
Definition: ReadCondHandle.h:242
SG::ReadCondHandle::fullKey
const DataObjID & fullKey() const
Definition: ReadCondHandle.h:60
SG::ReadCondHandle
Definition: ReadCondHandle.h:44
xAOD::uint32_t
setEventNumber uint32_t
Definition: EventInfo_v1.cxx:127
PixelModuleData
Definition: PixelModuleData.h:22
xAOD::char
char
Definition: TrigDecision_v1.cxx:38
SG::WriteCondHandle::getRange
const EventIDRange & getRange() const
Definition: WriteCondHandle.h:89
parse
std::map< std::string, std::string > parse(const std::string &list)
Definition: egammaLayerRecalibTool.cxx:1054
PixelCablingCondData::add_entry_offrob
void add_entry_offrob(const Identifier, const uint32_t)
Definition: PixelCablingCondData.cxx:40
PixelCablingCondData::add_entry_onoff
void add_entry_onoff(const uint64_t, const Identifier)
Definition: PixelCablingCondData.cxx:32
PixelCablingCondData::add_entry_DCSoffline
void add_entry_DCSoffline(const std::string &, const Identifier)
Definition: PixelCablingCondData.cxx:60
python.subdetectors.tile.Blob
Blob
Definition: tile.py:17
PixelCablingCondAlg::m_moduleDataKey
SG::ReadCondHandleKey< PixelModuleData > m_moduleDataKey
Definition: PixelCablingCondAlg.h:46
SG::VarHandleKey::empty
bool empty() const
Test if the key is blank.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:150
SG::WriteCondHandle::record
StatusCode record(const EventIDRange &range, T *t)
record handle, with explicit range DEPRECATED
Definition: WriteCondHandle.h:157
PixelModuleData::getCablingMapFileName
const std::string & getCablingMapFileName() const
Definition: PixelModuleData.cxx:356
PixelID::eta_module_max
int eta_module_max(const Identifier &id) const
Definition: PixelID.cxx:77
PixelID::wafer_id
Identifier wafer_id(int barrel_ec, int layer_disk, int phi_module, int eta_module) const
For a single crystal.
Definition: PixelID.h:364
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:83
SG::WriteCondHandle::fullKey
const DataObjID & fullKey() const
Definition: WriteCondHandle.h:41
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
IdContext.h
PixelCablingCondData::add_entry_robrod
void add_entry_robrod(int robid, int rodid)
Definition: PixelCablingCondData.cxx:50
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
PixelCablingCondAlg::m_pixelID
const PixelID * m_pixelID
Definition: PixelCablingCondAlg.h:43
IOVInfiniteRange::infiniteRunLB
static EventIDRange infiniteRunLB()
Produces an EventIDRange that is infinite in RunLumi and invalid in Time.
Definition: IOVInfiniteRange.h:39
PixelCablingCondAlg::m_readoutspeedKey
SG::ReadCondHandleKey< PixelReadoutSpeedData > m_readoutspeedKey
Definition: PixelCablingCondAlg.h:49
xAOD::uint64_t
uint64_t
Definition: EventInfo_v1.cxx:123
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
AthenaAttributeList
An AttributeList represents a logical row of attributes in a metadata table. The name and type of eac...
Definition: PersistentDataModel/PersistentDataModel/AthenaAttributeList.h:45
PixelCablingCondAlg.h
PixelCablingCondAlg::m_readKey
SG::ReadCondHandleKey< AthenaAttributeList > m_readKey
Definition: PixelCablingCondAlg.h:52
IOVInfiniteRange.h
PathResolver.h
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:228
SG::WriteCondHandle::key
const std::string & key() const
Definition: WriteCondHandle.h:40
PixelCablingCondData::add_entry_rodrob
void add_entry_rodrob(int rodid, int robid)
Definition: PixelCablingCondData.cxx:44
SG::CondHandleKey::initialize
StatusCode initialize(bool used=true)
PixelID::wafer_hash_max
size_type wafer_hash_max(void) const
Definition: PixelID.cxx:831
PixelCablingCondData::set_readout_map
void set_readout_map(std::map< uint32_t, bool > rodReadoutMap)
Definition: PixelCablingCondData.cxx:64
PathResolverFindCalibFile
std::string PathResolverFindCalibFile(const std::string &logical_file_name)
Definition: PathResolver.cxx:431
PixelID::eta_module_min
int eta_module_min(const Identifier &id) const
Definition: PixelID.cxx:94
PixelID::wafer_context
IdContext wafer_context(void) const
Definition: PixelID.h:590
PixelCablingCondAlg::initialize
virtual StatusCode initialize() override final
Definition: PixelCablingCondAlg.cxx:24
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
PixelModuleData::getCablingMapToFile
bool getCablingMapToFile() const
Definition: PixelModuleData.cxx:353
CaloCellTimeCorrFiller.filename
filename
Definition: CaloCellTimeCorrFiller.py:24
SG::WriteCondHandle::isValid
bool isValid() const
Definition: WriteCondHandle.h:248
compute_lumi.fin
fin
Definition: compute_lumi.py:19
PixelCablingCondData::add_entry_offon
void add_entry_offon(const Identifier, const uint64_t)
Definition: PixelCablingCondData.cxx:36
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
PixelCablingCondAlg::execute
virtual StatusCode execute(const EventContext &ctx) const override final
Definition: PixelCablingCondAlg.cxx:38
SG::AllowEmpty
@ AllowEmpty
Definition: StoreGate/StoreGate/VarHandleKey.h:30
IdContext
This class saves the "context" of an expanded identifier (ExpandedIdentifier) for compact or hash ver...
Definition: IdContext.h:26
SG::WriteCondHandle
Definition: WriteCondHandle.h:26
PixelCablingCondData::add_entry_offlineList
void add_entry_offlineList(const uint32_t, const Identifier)
Definition: PixelCablingCondData.cxx:56
SG::WriteCondHandle::addDependency
void addDependency(const EventIDRange &range)
Definition: WriteCondHandle.h:275
Identifier
Definition: IdentifierFieldParser.cxx:14