ATLAS Offline Software
MuonAlignmentErrorDbAlg.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3 */
4 
7 #include <GaudiKernel/EventIDRange.h>
8 #include <fstream>
9 #include <iterator>
10 
11 MuonAlignmentErrorDbAlg::MuonAlignmentErrorDbAlg(const std::string& name, ISvcLocator* pSvcLocator) :
12  AthReentrantAlgorithm(name, pSvcLocator) {}
13 
15  ATH_MSG_DEBUG("initialize " << name());
18 
19  return StatusCode::SUCCESS;
20 }
21 
22 StatusCode MuonAlignmentErrorDbAlg::execute(const EventContext& ctx) const {
23  ATH_MSG_DEBUG("execute " << name());
24 
25  // Write Cond Handle
26 
28  if (writeHandle.isValid()) {
29  ATH_MSG_DEBUG("CondHandle " << writeHandle.fullKey() << " is already valid."
30  << ". In theory this should not be called, but may happen"
31  << " if multiple concurrent events are being processed out of order.");
32  return StatusCode::SUCCESS;
33  }
34  std::unique_ptr<MuonAlignmentErrorData> writeCdo{std::make_unique<MuonAlignmentErrorData>()};
35 
36  std::string clobContent;
37  EventIDRange rangeW;
38  std::tie(clobContent, rangeW) = m_clobFileOverride.value().empty() ? getDbClobContent(ctx) : getFileClobContent();
39 
40  if (clobContent.empty()) {
41  ATH_MSG_ERROR("Cannot retrieve alignment error CLOB");
42  return StatusCode::FAILURE;
43  }
44 
45  std::istringstream indata(clobContent);
46  if (!indata) {
47  ATH_MSG_ERROR("Alignment error configuration invalid");
48  return StatusCode::FAILURE;
49  }
50 
51  ATH_MSG_DEBUG("***********************************");
52  ATH_MSG_DEBUG("PARSING LIST OF DEVIATIONS...");
53 
54  std::string line;
55  std::vector<MuonAlignmentErrorData::Deviation> deviationVec;
57  while (getline(indata, line)) {
58  // READING COMMENTS
59  if (line.compare(0, 1,"#") == 0) {
60  // ATH_MSG_DEBUG("Reading a commented line saying " << line);
61  continue;
62  }
63 
64  // READING FROM INPUT FILE: //
65  std::string flag("");
66  std::string name_sstring("");
67  std::string multilayer_sstring("");
68  double translation(0.);
69  double rotation(0.);
70 
71  // GET INPUT FILE VERSION
72  if (line.compare(0, 7, "version") == 0) {
73  std::string clobVersion;
74  std::istringstream(line) >> flag >> clobVersion;
75  ATH_MSG_INFO("*****************************************");
76  ATH_MSG_INFO("Input file version " << clobVersion);
77  ATH_MSG_INFO("*****************************************");
78  writeCdo->setClobVersion(std::move(clobVersion));
79  continue;
80  }
81 
82  // get the flag has_nsw_hits
83  if (line.compare(0, 12, "has_nsw_hits") == 0) {
84  bool hasNswHits{false};
85  std::istringstream(line) >> flag >> hasNswHits;
86  writeCdo->setHasNswHits(hasNswHits);
87  continue;
88  }
89 
90  // A FLAG CHARACTERIZING THE TYPE OF ERROR
91  // A REGULAR EXPRESSION FOR THE STATION NAME (EX: BIL.*) //
92  // TWO DOUBLES FOR THE TRANSLATION AND ROTATION DEVIATIONS //
93  if (std::istringstream(line) >> flag >> name_sstring >> multilayer_sstring >> translation >> rotation) {
94  ATH_MSG_DEBUG(flag << " " << name_sstring << " " << multilayer_sstring << " " << translation << " " << rotation);
95 
96  // SAVING THE STATION DEVIATIONS IN THE STRUCT //
97  aDev.stationName = name_sstring;
98  aDev.multilayer = multilayer_sstring;
99  aDev.translation = translation;
100  aDev.rotation = rotation;
101 
102  // FILLING THE VECTOR OF STRUCT CONTAINING THE STATION DEVIATIONS //
103  deviationVec.emplace_back(std::move(aDev));
104 
105  } // check stream is not at the end
106 
107  } // end of loop on input file lines
108 
109  if (deviationVec.empty()) {
110  ATH_MSG_WARNING("Could not read any alignment error configuration");
111  return StatusCode::FAILURE;
112  }
113  writeCdo->setDeviations(std::move(deviationVec));
114 
115  if (writeHandle.record(rangeW, std::move(writeCdo)).isFailure()) {
116  ATH_MSG_FATAL("Could not record MuonAlignmentErrorData " << writeHandle.key() << " with EventRange " << rangeW
117  << " into Conditions Store");
118  return StatusCode::FAILURE;
119  }
120  ATH_MSG_INFO("recorded new " << writeHandle.key() << " with range " << rangeW << " into Conditions Store");
121 
122  return StatusCode::SUCCESS;
123 }
124 
125 std::tuple<std::string, EventIDRange> MuonAlignmentErrorDbAlg::getDbClobContent(const EventContext& ctx) const {
126  // Read Cond Handle
128  const CondAttrListCollection* readCdo{*readHandle};
129  // const CondAttrListCollection* atrc(0);
130  // readCdo = *readHandle;
131  if (readCdo == nullptr) {
132  ATH_MSG_ERROR("Null pointer to the read conditions object");
133  return std::make_tuple(std::string(), EventIDRange());
134  }
135 
136  EventIDRange rangeW;
137  if (!readHandle.range(rangeW)) {
138  ATH_MSG_ERROR("Failed to retrieve validity range for " << readHandle.key());
139  return std::make_tuple(std::string(), EventIDRange());
140  }
141 
142  ATH_MSG_INFO("Size of CondAttrListCollection " << readHandle.fullKey() << " readCdo->size()= " << readCdo->size());
143  ATH_MSG_INFO("Range of input is " << rangeW);
144 
145  // like MuonAlignmentErrorDbTool::loadAlignmentError() after retrieving atrc (readCdo)
146 
147  std::string clobContent, dbClobVersion;
149  for (itr = readCdo->begin(); itr != readCdo->end(); ++itr) {
150  const coral::AttributeList& atr = itr->second;
151  clobContent = *(static_cast<const std::string*>((atr["syserrors"]).addressOfData()));
152  dbClobVersion = *(static_cast<const std::string*>((atr["version"]).addressOfData()));
153  }
154  return std::make_tuple(std::move(clobContent), rangeW);
155 }
156 
157 std::tuple<std::string, EventIDRange> MuonAlignmentErrorDbAlg::getFileClobContent() const {
158  ATH_MSG_INFO("Retrieving alignment error CLOB from file override " << m_clobFileOverride.value());
159 
160  std::ifstream in(m_clobFileOverride.value());
161  if (!in) {
162  ATH_MSG_ERROR("Failed to read CLOB file " << m_clobFileOverride.value());
163  return std::make_tuple(std::string(), EventIDRange());
164  }
165  return std::make_tuple(std::string(std::istreambuf_iterator<char>(in), {}), IOVInfiniteRange::infiniteTime());
166 }
167 
MuonAlignmentErrorDbAlg::m_readKey
SG::ReadCondHandleKey< CondAttrListCollection > m_readKey
Definition: MuonAlignmentErrorDbAlg.h:34
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
checkFileSG.line
line
Definition: checkFileSG.py:75
MuonAlignmentErrorData::Deviation
Definition: MuonAlignmentErrorData.h:24
SG::ReadCondHandle
Definition: ReadCondHandle.h:44
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
MuonAlignmentErrorDbAlg::initialize
StatusCode initialize() override
Definition: MuonAlignmentErrorDbAlg.cxx:14
MuonAlignmentErrorDbAlg::getDbClobContent
std::tuple< std::string, EventIDRange > getDbClobContent(const EventContext &ctx) const
Definition: MuonAlignmentErrorDbAlg.cxx:125
python.PyKernel.AttributeList
AttributeList
Definition: PyKernel.py:36
CondAttrListCollection
This class is a collection of AttributeLists where each one is associated with a channel number....
Definition: CondAttrListCollection.h:52
AthReentrantAlgorithm
An algorithm that can be simultaneously executed in multiple threads.
Definition: AthReentrantAlgorithm.h:83
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
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
xAOD::rotation
rotation
Definition: TrackSurface_v1.cxx:15
master.flag
bool flag
Definition: master.py:29
MuonAlignmentErrorDbAlg::m_writeKey
SG::WriteCondHandleKey< MuonAlignmentErrorData > m_writeKey
Definition: MuonAlignmentErrorDbAlg.h:36
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
MuonAlignmentErrorDbAlg::MuonAlignmentErrorDbAlg
MuonAlignmentErrorDbAlg(const std::string &name, ISvcLocator *pSvcLocator)
Definition: MuonAlignmentErrorDbAlg.cxx:11
IOVInfiniteRange.h
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
SG::CondHandleKey::initialize
StatusCode initialize(bool used=true)
MuonAlignmentErrorDbAlg::getFileClobContent
std::tuple< std::string, EventIDRange > getFileClobContent() const
Definition: MuonAlignmentErrorDbAlg.cxx:157
MuonAlignmentErrorData::Deviation::rotation
double rotation
Definition: MuonAlignmentErrorData.h:28
MuonAlignmentErrorDbAlg.h
MuonAlignmentErrorData::Deviation::stationName
boost::regex stationName
Definition: MuonAlignmentErrorData.h:25
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
CondAttrListCollection::const_iterator
ChanAttrListMap::const_iterator const_iterator
Definition: CondAttrListCollection.h:63
MuonAlignmentErrorData::Deviation::multilayer
boost::regex multilayer
Definition: MuonAlignmentErrorData.h:26
MuonAlignmentErrorData::Deviation::translation
double translation
Definition: MuonAlignmentErrorData.h:27
IOVInfiniteRange::infiniteTime
static EventIDRange infiniteTime()
Produces an EventIDRange that is inifinite in Time and invalid in RunLumi.
Definition: IOVInfiniteRange.h:47
MuonAlignmentErrorDbAlg::m_clobFileOverride
Gaudi::Property< std::string > m_clobFileOverride
Definition: MuonAlignmentErrorDbAlg.h:38
MuonAlignmentErrorDbAlg::execute
StatusCode execute(const EventContext &ctx) const override
Definition: MuonAlignmentErrorDbAlg.cxx:22
SG::WriteCondHandle
Definition: WriteCondHandle.h:26