ATLAS Offline Software
Loading...
Searching...
No Matches
TRTCondWrite.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
5#include <fstream>
6#include <iostream>
7#include <string>
8#include "TRTCondWrite.h"
14
22TRTCondWrite::TRTCondWrite(const std::string &name, ISvcLocator *pSvcLocator)
23 : AthCondAlgorithm(name, pSvcLocator) {}
24
26{
27
28 ATH_MSG_DEBUG("TRTCondWrite::initialize() called");
29
30 // Get ID helper
31 ATH_CHECK(detStore()->retrieve(m_trtid, "TRT_ID"));
32
33 // Get condSvc
34 ATH_CHECK(m_condSvc.retrieve());
35
36 // Format of input text file
37 int format = 0;
38 if (m_par_caltextfile != "")
39 {
40 ATH_MSG_INFO(" input text file supplied " << m_par_caltextfile);
41 if (StatusCode::SUCCESS != checkTextFile(m_par_caltextfile, format))
42 {
43 ATH_MSG_INFO("Could not find or could not read text file" << m_par_caltextfile);
44 return StatusCode::SUCCESS;
45 }
46 ATH_MSG_INFO(" Found format " << format << " in text file " << m_par_caltextfile);
47 }
48 else
49 {
50 ATH_MSG_ERROR(" No input text file supplied. Initialization done. ");
51 return StatusCode::FAILURE;
52 }
53
54 // Write keys
55 // If an input text file is specified, initialize write keys. The corresponding folders must be blocked.
56
57 if (m_par_caltextfile != "" && format > 0)
58 {
59 ATH_CHECK(m_rtWriteKey.initialize());
60 ATH_CHECK(m_t0WriteKey.initialize());
61 }
62 ATH_MSG_INFO(" Initilization done with WriteKey Registraton ");
63
64 return StatusCode::SUCCESS;
65}
66
67StatusCode TRTCondWrite::execute(const EventContext& ctx) const
68{
69
70 // Read from text file?
71 if (!m_par_caltextfile.empty())
72 {
73 std::ifstream infile(m_par_caltextfile.value().c_str());
74 if (infile)
75 {
76 ATH_MSG_INFO(" Read calibration constants from text file " << m_par_caltextfile);
77 int format = 0;
78 if (StatusCode::SUCCESS != readTextFile(ctx, m_par_caltextfile, format))
79 {
80 ATH_MSG_FATAL("Could not read calibration objects from text file " << m_par_caltextfile);
81 return StatusCode::FAILURE;
82 }
83 }
84 else
85 {
86 ATH_MSG_ERROR("Input file does not exist " << m_par_caltextfile.value().c_str());
87 return StatusCode::FAILURE;
88 }
89 infile.close();
90 }
91 else
92 {
93 ATH_MSG_ERROR("No input filename supplied ");
94 return StatusCode::FAILURE;
95 }
96
97 return StatusCode::SUCCESS;
98 ;
99}
100
102{
103 return StatusCode::SUCCESS;
104}
105
106StatusCode TRTCondWrite::checkTextFile(const std::string &filename, int &format)
107{
108
109 StatusCode sc = StatusCode::SUCCESS;
110 std::ifstream infile(filename.c_str());
111 if (!infile)
112 {
113 sc = StatusCode::FAILURE;
114 }
115 else
116 {
117 // read the format tag. if none, default to 0
118 format = 0;
119 char line[512];
120 infile.getline(line, 512);
121 std::string linestring(line);
122 size_t pos = linestring.find("Fileformat");
123 if (pos != std::string::npos)
124 {
125 sscanf(line, "# Fileformat=%d", &format);
126 }
127 else
128 {
129 ATH_MSG_WARNING("Input file has no Fileformat identifier. Assuming format=0.");
130 // 'rewind' the file
131
132 infile.close();
133 infile.open(filename.c_str());
134 }
135 }
136 infile.close();
137 return sc;
138}
139
140StatusCode TRTCondWrite::readTextFile(const EventContext& ctx, const std::string &filename, int &format) const
141{
142
143 StatusCode sc = StatusCode::SUCCESS;
144 std::ifstream infile(filename.c_str());
145 if (!infile)
146 {
147 ATH_MSG_ERROR("Cannot find input file " << filename);
148 sc = StatusCode::FAILURE;
149 }
150 else
151 {
152 // read the format tag. if none, default to 0
153 format = 0;
154 char line[512];
155 infile.getline(line, 512);
156 std::string linestring(line);
157 size_t pos = linestring.find("Fileformat");
158 if (pos != std::string::npos)
159 {
160 sscanf(line, "# Fileformat=%d", &format);
161 }
162 else
163 {
164 ATH_MSG_WARNING("Input file has no Fileformat identifier. Assuming format=1");
165 // 'rewind' the file
166
167 infile.close();
168 infile.open(filename.c_str());
169 }
170 ATH_MSG_INFO("Reading calibration data from text file " << filename << " format " << format);
171 // force format 1 here
172 sc = readTextFile_Format1(ctx, infile);
173 }
174 infile.close();
175 return sc;
176}
177
178StatusCode TRTCondWrite::readTextFile_Format1(const EventContext& ctx, std::istream &infile) const
179{
180
181 enum ReadMode
182 {
183 ReadingRtRelation,
184 ReadingStrawT0,
185 ReadingGarbage
186 };
187 ReadMode readmode = ReadingGarbage;
188
189 // Make containers for access via ReadCondHandle for the reconstruction.
190 std::unique_ptr<RtRelationContainer> rtCdo{std::make_unique<RtRelationContainer>()};
191 std::unique_ptr<StrawT0Container> t0Cdo{std::make_unique<StrawT0Container>()};
192
193 char line[512];
194 int nrtrelations(0), nstrawt0(0);
195 while (infile.getline(line, 512))
196 {
197 if (line[0] == '#')
198 {
199 // line with tag
200 std::string linestring(line);
201 if (linestring.find("RtRelation") != std::string::npos)
202 {
203 readmode = ReadingRtRelation;
204 ATH_MSG_INFO(" Found line with Rt tag ");
205 }
206 else if (linestring.find("StrawT0") != std::string::npos)
207 {
208 readmode = ReadingStrawT0;
209 ATH_MSG_INFO(" Found line with T0 tag ");
210 }
211 else
212 readmode = ReadingGarbage;
213 }
214 else if (readmode != ReadingGarbage)
215 {
216 std::istringstream is(line);
217 // read the id
219 is >> id;
220 // read the semicolon that end the id
221 char dummy;
222 is >> dummy;
223
224 // read the object
225 if (readmode == ReadingRtRelation)
226 {
227
229 //Values read in should have their values checked against sensible limits.
230 //We assume the input is trusted however.
231 //coverity[TAINTED_SCALAR]
232 rtCdo->set(id, rt);
233 delete rt;
234 ++nrtrelations;
235 }
236 else if (readmode == ReadingStrawT0)
237 {
238
239 float t0(0), t0err(0);
240 is >> t0 >> t0err;
241 if (t0 > 0)
242 {
243 //Values read in should have their values checked against sensible limits.
244 //We assume the input is trusted however.
245 //coverity[TAINTED_SCALAR]
246 t0Cdo->setT0(id, t0, t0err);
247 ++nstrawt0;
248 }
249 }
250 }
251 }
252
253 // Check that containers were filled
254
255 size_t t0footprint = t0Cdo->footprint();
256 size_t rtfootprint = rtCdo->footprint();
257
258 ATH_MSG_INFO("read " << nstrawt0 << " t0 and " << nrtrelations << " rt from file. "
259 << " t0/rt footprints " << t0footprint << " / " << rtfootprint << " t0 footprint after crunch " << t0Cdo->footprint());
260
261 // Record the containers for access via ReadCondHandle during reconstruction
262 const EventIDRange rangeW = IOVInfRange();
264 if (rtWriteHandle.isValid())
265 {
266 ATH_MSG_DEBUG(" RtRelationContainer already available ");
267 return StatusCode::SUCCESS;
268 }
269
270 if (rtWriteHandle.record(rangeW, std::move(rtCdo)).isFailure())
271 {
272 ATH_MSG_ERROR("Could not record RT Container for key " << m_rtWriteKey.fullKey() << " with WriteHandle ");
273 return StatusCode::FAILURE;
274 }
275 else
276 {
277 ATH_MSG_INFO("Recorded RT Container for key " << m_rtWriteKey.fullKey() << " with range " << rtWriteHandle.getRange());
278 }
279
281 if (t0WriteHandle.isValid())
282 {
283 ATH_MSG_DEBUG(" StrawT0Container already available ");
284 return StatusCode::SUCCESS;
285 }
286
287 if (t0Cdo->initialize().isFailure())
288 ATH_MSG_WARNING("Could not initialize T0 Container for key " << m_t0WriteKey.fullKey());
289
290 if (t0WriteHandle.record(rangeW, std::move(t0Cdo)).isFailure())
291 {
292 ATH_MSG_ERROR("Could not record T0 Container for key " << m_t0WriteKey.fullKey() << " with WriteHandle ");
293 return StatusCode::FAILURE;
294 }
295 else
296 {
297 ATH_MSG_INFO("Recorded T0 Container for key " << m_t0WriteKey.fullKey() << " with range " << t0WriteHandle.getRange());
298 }
299
300 return StatusCode::SUCCESS;
301}
302
304{
305 return TRTCond::ExpandedIdentifier(m_trtid->barrel_ec(id), m_trtid->layer_or_wheel(id),
306 m_trtid->phi_module(id), m_trtid->straw_layer(id),
307 m_trtid->straw(id), level);
308}
309EventIDRange TRTCondWrite::IOVInfRange() const
310{
311 const EventIDBase start{1, EventIDBase::UNDEFEVT, EventIDBase::UNDEFNUM, EventIDBase::UNDEFNUM, 0};
312 const EventIDBase stop{EventIDBase::UNDEFNUM - 1, EventIDBase::UNDEFEVT, EventIDBase::UNDEFNUM, EventIDBase::UNDEFNUM, EventIDBase::UNDEFNUM - 1};
313 return EventIDRange{start, stop};
314}
#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)
static Double_t sc
static Double_t t0
CondAlg to read TRT calibration constants in from text file and load them in ConditionsStore.
const ServiceHandle< StoreGateSvc > & detStore() const
Base class for conditions algorithms.
const EventIDRange & getRange() const
StatusCode record(const EventIDRange &range, T *t)
record handle, with explicit range DEPRECATED
TRTCondWrite(const std::string &name, ISvcLocator *pSvcLocator)
constructor
virtual TRTCond::ExpandedIdentifier trtcondid(const Identifier &id, int level=TRTCond::ExpandedIdentifier::STRAW) const
create an TRTCond::ExpandedIdentifier from a TRTID identifier
virtual StatusCode execute(const EventContext &ctx) const override
virtual StatusCode checkTextFile(const std::string &file, int &format)
read calibration from text file into TDS
Gaudi::Property< std::string > m_par_caltextfile
SG::WriteCondHandleKey< StrawT0Container > m_t0WriteKey
virtual StatusCode initialize(void) override
const TRT_ID * m_trtid
trt id helper
ServiceHandle< ICondSvc > m_condSvc
virtual EventIDRange IOVInfRange() const
virtual StatusCode readTextFile_Format1(const EventContext &, std::istream &) const
SG::WriteCondHandleKey< RtRelationContainer > m_rtWriteKey
virtual StatusCode finalize(void) override
virtual StatusCode readTextFile(const EventContext &ctx, const std::string &file, int &format) const
static RtRelation * readFromFile(std::istream &is)
read method
Base class for rt-relations in the TRT.
Definition RtRelation.h:27