ATLAS Offline Software
TRTCondWrite.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #include <fstream>
6 #include <iostream>
7 #include <string>
8 #include "TRTCondWrite.h"
14 
22 TRTCondWrite::TRTCondWrite(const std::string &name, ISvcLocator *pSvcLocator)
23  : AthAlgorithm(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 
60  ATH_CHECK(m_rtWriteKey.initialize());
61  if (m_condSvc->regHandle(this, m_rtWriteKey).isFailure())
62  ATH_MSG_ERROR("unable to register WriteCondHandle " << m_rtWriteKey.fullKey());
63  ATH_MSG_INFO("Registered WriteCondHandle " << m_rtWriteKey.fullKey());
64  ATH_CHECK(m_t0WriteKey.initialize());
65  if (m_condSvc->regHandle(this, m_t0WriteKey).isFailure())
66  ATH_MSG_ERROR("unable to register WriteCondHandle " << m_t0WriteKey.fullKey());
67  ATH_MSG_INFO("Registered WriteCondHandle " << m_t0WriteKey.fullKey());
68  }
69  ATH_MSG_INFO(" Initilization done with WriteKey Registraton ");
70 
71  return StatusCode::SUCCESS;
72 }
73 
75 {
76 
77  // Read from text file?
78  if (!m_par_caltextfile.empty())
79  {
80  std::ifstream infile(m_par_caltextfile.value().c_str());
81  if (infile)
82  {
83  ATH_MSG_INFO(" Read calibration constants from text file " << m_par_caltextfile);
84  int format = 0;
85  if (StatusCode::SUCCESS != readTextFile(m_par_caltextfile, format))
86  {
87  ATH_MSG_FATAL("Could not read calibration objects from text file " << m_par_caltextfile);
88  return StatusCode::FAILURE;
89  }
90  }
91  else
92  {
93  ATH_MSG_ERROR("Input file does not exist " << m_par_caltextfile.value().c_str());
94  return StatusCode::FAILURE;
95  }
96  infile.close();
97  }
98  else
99  {
100  ATH_MSG_ERROR("No input filename supplied ");
101  return StatusCode::FAILURE;
102  }
103 
104  return StatusCode::SUCCESS;
105  ;
106 }
107 
109 {
110  return StatusCode::SUCCESS;
111 }
112 
114 {
115 
116  StatusCode sc = StatusCode::SUCCESS;
117  std::ifstream infile(filename.c_str());
118  if (!infile)
119  {
120  sc = StatusCode::FAILURE;
121  }
122  else
123  {
124  // read the format tag. if none, default to 0
125  format = 0;
126  char line[512];
127  infile.getline(line, 512);
128  std::string linestring(line);
129  size_t pos = linestring.find("Fileformat");
130  if (pos != std::string::npos)
131  {
132  sscanf(line, "# Fileformat=%d", &format);
133  }
134  else
135  {
136  ATH_MSG_WARNING("Input file has no Fileformat identifier. Assuming format=0.");
137  // 'rewind' the file
138 
139  infile.close();
140  infile.open(filename.c_str());
141  }
142  }
143  infile.close();
144  return sc;
145 }
146 
148 {
149 
150  StatusCode sc = StatusCode::SUCCESS;
151  std::ifstream infile(filename.c_str());
152  if (!infile)
153  {
154  ATH_MSG_ERROR("Cannot find input file " << filename);
155  sc = StatusCode::FAILURE;
156  }
157  else
158  {
159  // read the format tag. if none, default to 0
160  format = 0;
161  char line[512];
162  infile.getline(line, 512);
163  std::string linestring(line);
164  size_t pos = linestring.find("Fileformat");
165  if (pos != std::string::npos)
166  {
167  sscanf(line, "# Fileformat=%d", &format);
168  }
169  else
170  {
171  ATH_MSG_WARNING("Input file has no Fileformat identifier. Assuming format=1");
172  // 'rewind' the file
173 
174  infile.close();
175  infile.open(filename.c_str());
176  }
177  ATH_MSG_INFO("Reading calibration data from text file " << filename << " format " << format);
178  // force format 1 here
180  }
181  infile.close();
182  return sc;
183 }
184 
186 {
187 
188  enum ReadMode
189  {
190  ReadingRtRelation,
191  ReadingStrawT0,
192  ReadingGarbage
193  };
194  ReadMode readmode = ReadingGarbage;
195 
196  // Make containers for access via ReadCondHandle for the reconstruction.
197  std::unique_ptr<RtRelationContainer> rtCdo{std::make_unique<RtRelationContainer>()};
198  std::unique_ptr<StrawT0Container> t0Cdo{std::make_unique<StrawT0Container>()};
199 
200  char line[512];
201  int nrtrelations(0), nstrawt0(0);
202  while (infile.getline(line, 512))
203  {
204  if (line[0] == '#')
205  {
206  // line with tag
207  std::string linestring(line);
208  if (linestring.find("RtRelation") != std::string::npos)
209  {
210  readmode = ReadingRtRelation;
211  ATH_MSG_INFO(" Found line with Rt tag ");
212  }
213  else if (linestring.find("StrawT0") != std::string::npos)
214  {
215  readmode = ReadingStrawT0;
216  ATH_MSG_INFO(" Found line with T0 tag ");
217  }
218  else
219  readmode = ReadingGarbage;
220  }
221  else if (readmode != ReadingGarbage)
222  {
223  std::istringstream is(line);
224  // read the id
226  is >> id;
227  // read the semicolon that end the id
228  char dummy;
229  is >> dummy;
230 
231  // read the object
232  if (readmode == ReadingRtRelation)
233  {
234 
236  rtCdo->set(id, rt);
237  delete rt;
238  ++nrtrelations;
239  }
240  else if (readmode == ReadingStrawT0)
241  {
242 
243  float t0(0), t0err(0);
244  is >> t0 >> t0err;
245  if (t0 > 0)
246  {
247  t0Cdo->setT0(id, t0, t0err);
248  ++nstrawt0;
249  }
250  }
251  }
252  }
253 
254  // Check that containers were filled
255 
256  size_t t0footprint = t0Cdo->footprint();
257  size_t rtfootprint = rtCdo->footprint();
258 
259  ATH_MSG_INFO("read " << nstrawt0 << " t0 and " << nrtrelations << " rt from file. "
260  << " t0/rt footprints " << t0footprint << " / " << rtfootprint << " t0 footprint after crunch " << t0Cdo->footprint());
261 
262  // Record the containers for access via ReadCondHandle during reconstruction
263  const EventIDRange rangeW = IOVInfRange();
265  if (rtWriteHandle.isValid())
266  {
267  ATH_MSG_DEBUG(" RtRelationContainer already available ");
268  return StatusCode::SUCCESS;
269  }
270 
271  if (rtWriteHandle.record(rangeW, std::move(rtCdo)).isFailure())
272  {
273  ATH_MSG_ERROR("Could not record RT Container for key " << m_rtWriteKey.fullKey() << " with WriteHandle ");
274  return StatusCode::FAILURE;
275  }
276  else
277  {
278  ATH_MSG_INFO("Recorded RT Container for key " << m_rtWriteKey.fullKey() << " with range " << rtWriteHandle.getRange());
279  }
280 
282  if (t0WriteHandle.isValid())
283  {
284  ATH_MSG_DEBUG(" StrawT0Container already available ");
285  return StatusCode::SUCCESS;
286  }
287 
288  if (t0Cdo->initialize().isFailure())
289  ATH_MSG_WARNING("Could not initialize T0 Container for key " << m_t0WriteKey.fullKey());
290 
291  if (t0WriteHandle.record(rangeW, std::move(t0Cdo)).isFailure())
292  {
293  ATH_MSG_ERROR("Could not record T0 Container for key " << m_t0WriteKey.fullKey() << " with WriteHandle ");
294  return StatusCode::FAILURE;
295  }
296  else
297  {
298  ATH_MSG_INFO("Recorded T0 Container for key " << m_t0WriteKey.fullKey() << " with range " << t0WriteHandle.getRange());
299  }
300 
301  return StatusCode::SUCCESS;
302 }
303 
305 {
308  m_trtid->straw(id), level);
309 }
310 EventIDRange TRTCondWrite::IOVInfRange() const
311 {
312  const EventIDBase start{1, EventIDBase::UNDEFEVT, EventIDBase::UNDEFNUM, EventIDBase::UNDEFNUM, 0};
313  const EventIDBase stop{EventIDBase::UNDEFNUM - 1, EventIDBase::UNDEFEVT, EventIDBase::UNDEFNUM, EventIDBase::UNDEFNUM, EventIDBase::UNDEFNUM - 1};
314  return EventIDRange{start, stop};
315 }
python.PyKernel.retrieve
def retrieve(aClass, aKey=None)
Definition: PyKernel.py:110
TRTCondWrite::finalize
virtual StatusCode finalize(void) override
Definition: TRTCondWrite.cxx:108
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
DinesRtRelation.h
TRTCondWrite::m_t0WriteKey
SG::WriteCondHandleKey< StrawT0Container > m_t0WriteKey
Definition: TRTCondWrite.h:65
run.infile
string infile
Definition: run.py:13
vtune_athena.format
format
Definition: vtune_athena.py:14
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
TRTCondWrite::m_condSvc
ServiceHandle< ICondSvc > m_condSvc
Definition: TRTCondWrite.h:60
mergePhysValFiles.start
start
Definition: DataQuality/DataQualityUtils/scripts/mergePhysValFiles.py:13
TRTCondWrite::m_rtWriteKey
SG::WriteCondHandleKey< RtRelationContainer > m_rtWriteKey
Definition: TRTCondWrite.h:64
ALFA_EventTPCnv_Dict::t0
std::vector< ALFA_RawData_p1 > t0
Definition: ALFA_EventTPCnvDict.h:42
PixelModuleFeMask_create_db.stop
int stop
Definition: PixelModuleFeMask_create_db.py:76
TRTCondWrite::trtcondid
virtual TRTCond::ExpandedIdentifier trtcondid(const Identifier &id, int level=TRTCond::ExpandedIdentifier::STRAW) const
create an TRTCond::ExpandedIdentifier from a TRTID identifier
Definition: TRTCondWrite.cxx:304
BasicRtRelation.h
dq_defect_bulk_create_defects.line
line
Definition: dq_defect_bulk_create_defects.py:27
TRTCondWrite::readTextFile
virtual StatusCode readTextFile(const std::string &file, int &format)
Definition: TRTCondWrite.cxx:147
AthenaPoolTestRead.sc
sc
Definition: AthenaPoolTestRead.py:27
python.iconfTool.models.loaders.level
level
Definition: loaders.py:20
AthCommonDataStore< AthCommonMsg< Algorithm > >::detStore
const ServiceHandle< StoreGateSvc > & detStore() const
The standard StoreGateSvc/DetectorStore Returns (kind of) a pointer to the StoreGateSvc.
Definition: AthCommonDataStore.h:95
TRTCondWrite::execute
virtual StatusCode execute(void) override
Definition: TRTCondWrite.cxx:74
TRTCond::RtRelation
Definition: RtRelation.h:27
TRT_ID::straw
int straw(const Identifier &id) const
Definition: TRT_ID.h:896
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
TRTCond::RtRelationFactory::readFromFile
static RtRelation * readFromFile(std::istream &is)
read method
Definition: RtRelationFactory.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
BinnedRtRelation.h
WriteCondHandle.h
CalibDbCompareRT.dummy
dummy
Definition: CalibDbCompareRT.py:59
TRTCondWrite::IOVInfRange
virtual EventIDRange IOVInfRange() const
Definition: TRTCondWrite.cxx:310
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
TRT_ID::barrel_ec
int barrel_ec(const Identifier &id) const
Values of different levels (failure returns 0)
Definition: TRT_ID.h:860
TRT_ID::straw_layer
int straw_layer(const Identifier &id) const
Definition: TRT_ID.h:887
TRT_ID::layer_or_wheel
int layer_or_wheel(const Identifier &id) const
Definition: TRT_ID.h:878
TRTCondWrite::TRTCondWrite
TRTCondWrite(const std::string &name, ISvcLocator *pSvcLocator)
constructor
Definition: TRTCondWrite.cxx:22
AthAlgorithm
Definition: AthAlgorithm.h:47
id
SG::auxid_t id
Definition: Control/AthContainers/Root/debug.cxx:239
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:240
TRT_ID::phi_module
int phi_module(const Identifier &id) const
Definition: TRT_ID.h:869
TRTCondWrite::m_par_caltextfile
Gaudi::Property< std::string > m_par_caltextfile
Definition: TRTCondWrite.h:61
python.LumiBlobConversion.pos
pos
Definition: LumiBlobConversion.py:16
RtRelationFactory.h
TRTCondWrite::initialize
virtual StatusCode initialize(void) override
Definition: TRTCondWrite.cxx:25
TRTCondWrite.h
CondAlg to read TRT calibration constants in from text file and load them in ConditionsStore.
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
CaloCellTimeCorrFiller.filename
filename
Definition: CaloCellTimeCorrFiller.py:23
TRTCondWrite::m_trtid
const TRT_ID * m_trtid
trt id helper
Definition: TRTCondWrite.h:59
TRTCondWrite::readTextFile_Format1
virtual StatusCode readTextFile_Format1(std::istream &)
Definition: TRTCondWrite.cxx:185
TRTCond::ExpandedIdentifier
Identifier for TRT detector elements in the conditions code.
Definition: InnerDetector/InDetConditions/TRT_ConditionsData/TRT_ConditionsData/ExpandedIdentifier.h:30
SG::WriteCondHandle
Definition: WriteCondHandle.h:26
TRTCondWrite::checkTextFile
virtual StatusCode checkTextFile(const std::string &file, int &format)
read calibration from text file into TDS
Definition: TRTCondWrite.cxx:113
Identifier
Definition: IdentifierFieldParser.cxx:14