ATLAS Offline Software
TRTCondWrite.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #include <fstream>
6 #include <iostream>
7 #include <string>
14 
15 
16 
24 TRTCondWrite::TRTCondWrite(const std::string& name, ISvcLocator* pSvcLocator)
25  :AthAlgorithm (name, pSvcLocator),
26  m_setup(false),
27  m_par_caltextfile(""), // input text file
28  m_trtid(0),
29  m_detstore("DetectorStore",name),
30  m_condSvc("CondSvc",name)
31 
32 {
33  // declare algorithm parameters
34  declareProperty("DetectorStore",m_detstore);
35  declareProperty("CalibInputFile",m_par_caltextfile);
36 }
37 
39 {}
40 
42 
43  ATH_MSG_DEBUG("TRTCondWrite::initialize() called");
44 
45  //
46  // Get ID helper
47  ATH_CHECK( detStore()->retrieve(m_trtid,"TRT_ID") );
48 
49  //
50  // Get condSvc
51  ATH_CHECK( m_condSvc.retrieve() );
52 
53 
54  // Format of input text file
55 
56  int format =0;
57  if( m_par_caltextfile != "" ) {
58  ATH_MSG_INFO( " input text file supplied " << m_par_caltextfile);
59  if(StatusCode::SUCCESS!=checkTextFile(m_par_caltextfile, format)) {
60  ATH_MSG_INFO( "Could not find or could not read text file" << m_par_caltextfile);
61  return StatusCode::SUCCESS;
62  }
63  ATH_MSG_INFO( " Found format " << format << " in text file " << m_par_caltextfile);
64  } else {
65  ATH_MSG_INFO( " No input text file supplied. Initialization done. ");
66  return StatusCode::SUCCESS;
67  }
68 
69 
70  // Write keys
71 
72 
73  // If an input text file is specified, initialize write keys. The corresponding folders must be blocked.
74 
75  if(m_par_caltextfile != "" && format>0) {
76 
77  ATH_CHECK( m_rtWriteKey.initialize() );
78  if(m_condSvc->regHandle(this,m_rtWriteKey).isFailure()) ATH_MSG_ERROR("unable to register WriteCondHandle " << m_rtWriteKey.fullKey());
79  ATH_MSG_INFO("Registered WriteCondHandle " << m_rtWriteKey.fullKey());
80  ATH_CHECK( m_t0WriteKey.initialize() );
81  if(m_condSvc->regHandle(this,m_t0WriteKey).isFailure()) ATH_MSG_ERROR("unable to register WriteCondHandle " << m_t0WriteKey.fullKey());
82  ATH_MSG_INFO("Registered WriteCondHandle " << m_t0WriteKey.fullKey());
83  }
84  ATH_MSG_INFO( " Initilization done with WriteKey Registraton ");
85 
86  return StatusCode::SUCCESS;
87 }
88 
90 
91  StatusCode sc = StatusCode::SUCCESS;
92  //
93  // at first event:
94  if (!m_setup) {
95 
96 
97  // Read from text file?
98 
99  if( !m_par_caltextfile.empty()) {
100  std::ifstream infile(m_par_caltextfile.c_str()) ;
101  if(infile) {
102  ATH_MSG_INFO( " Read calibration constants from text file " << m_par_caltextfile);
103  int format =0;
104  if(StatusCode::SUCCESS!=readTextFile(m_par_caltextfile, format)) {
105  ATH_MSG_FATAL( "Could not read calibration objects from text file " << m_par_caltextfile);
106  return StatusCode::FAILURE ;
107  }
108  } else{
109  ATH_MSG_INFO( "Input file does not exist " );
110  }
111  infile.close();
112  } else {
113  ATH_MSG_INFO( "No input filename supplied " );
114  }
115 
116 
117 
118 
119  m_setup=true;
120 
121  }
122 
123  return sc;
124 }
125 
127 
128  StatusCode sc = StatusCode::SUCCESS;
129 
130  return sc;
131 }
133 {
134 
135  StatusCode sc=StatusCode::SUCCESS ;
136  std::ifstream infile(filename.c_str()) ;
137  if(!infile) {
138  sc=StatusCode::FAILURE;
139  } else {
140  // read the format tag. if none, default to 0
141  format = 0 ;
142  char line[512] ;
143  infile.getline(line,512) ;
144  std::string linestring(line) ;
145  size_t pos = linestring.find("Fileformat") ;
146  if(pos != std::string::npos) {
147  sscanf(line,"# Fileformat=%d",&format) ;
148  } else {
149  ATH_MSG_WARNING( "Input file has no Fileformat identifier. Assuming format=0.");
150  // 'rewind' the file
151 
152  infile.close() ;
153  infile.open(filename.c_str()) ;
154  }
155  }
156  infile.close() ;
157  return sc ;
158 }
159 
161 {
162 
163  StatusCode sc=StatusCode::SUCCESS ;
164  std::ifstream infile(filename.c_str()) ;
165  if(!infile) {
166  ATH_MSG_ERROR( "Cannot find input file " << filename ) ;
167  } else {
168  // read the format tag. if none, default to 0
169  format = 0 ;
170  char line[512] ;
171  infile.getline(line,512) ;
172  std::string linestring(line) ;
173  size_t pos = linestring.find("Fileformat") ;
174  if(pos != std::string::npos) {
175  sscanf(line,"# Fileformat=%d",&format) ;
176  } else {
177  ATH_MSG_WARNING( "Input file has no Fileformat identifier. Assuming format=1");
178  // 'rewind' the file
179 
180  infile.close() ;
181  infile.open(filename.c_str()) ;
182  }
183  ATH_MSG_INFO( "Reading calibration data from text file " << filename << " format " << format ) ;
184  // force format 1 here
186  }
187  infile.close() ;
188  return sc ;
189 }
190 
191 
193 {
194 
195 
196  enum ReadMode { ReadingRtRelation, ReadingStrawT0, ReadingGarbage } ;
197  ReadMode readmode =ReadingGarbage ;
198 
199  // Make containers for access via ReadCondHandle for the reconstruction.
200  std::unique_ptr<RtRelationContainer> rtCdo{std::make_unique<RtRelationContainer>()};
201  std::unique_ptr<StrawT0Container> t0Cdo{std::make_unique<StrawT0Container>()};
202 
203  char line[512] ;
204  int nrtrelations(0), nstrawt0(0) ;
205  while( infile.getline(line,512) ) {
206  if(line[0] == '#') {
207  // line with tag
208  std::string linestring(line) ;
209  if(linestring.find("RtRelation") != std::string::npos) {
210  readmode = ReadingRtRelation ;
211  ATH_MSG_INFO(" Found line with Rt tag ");
212 
213  } else if (linestring.find("StrawT0") != std::string::npos) {
214  readmode = ReadingStrawT0 ;
215  ATH_MSG_INFO(" Found line with T0 tag ");
216 
217  }else readmode = ReadingGarbage ;
218  } else if( readmode != ReadingGarbage) {
219  std::istringstream is(line) ;
220  // read the id
222  is >> id ;
223  // read the semicolon that end the id
224  char dummy ;
225  is >> dummy ;
226 
227  // read the object
228  if( readmode == ReadingRtRelation ) {
229 
230 
232  rtCdo->set( id,rt);
233  delete rt ;
234  ++nrtrelations ;
235  } else if( readmode == ReadingStrawT0 ) {
236 
237  float t0(0), t0err(0) ;
238  is >> t0 >> t0err;
239  t0Cdo->setT0( id, t0, t0err);
240  ++nstrawt0 ;
241  }
242  }
243  }
244 
245  // Check that containers were filled
246 
247  size_t t0footprint = t0Cdo->footprint() ;
248  size_t rtfootprint = rtCdo->footprint() ;
249  //t0Cdo->crunch();
250 
251 
252  ATH_MSG_INFO( "read " << nstrawt0 << " t0 and " << nrtrelations << " rt from file. "
253  << " t0/rt footprints " << t0footprint << " / " << rtfootprint << " t0 footprint after crunch " << t0Cdo->footprint());
254 
255 
256 
257  //Record the containers for access via ReadCondHandle during reconstruction
258 
259  const EventIDRange rangeW= IOVInfRange();
261  if(rtWriteHandle.isValid()) {
262  ATH_MSG_DEBUG(" RtRelationContainer already available ");
263  return StatusCode::SUCCESS ;
264  }
265 
266  if(rtWriteHandle.record(rangeW,std::move(rtCdo)).isFailure()) {
267  ATH_MSG_ERROR("Could not record RT Container for key " << m_rtWriteKey.fullKey() << " with WriteHandle ");
268  return StatusCode::FAILURE;
269  } else {
270  ATH_MSG_INFO("Recorded RT Container for key " << m_rtWriteKey.fullKey() << " with range " << rtWriteHandle.getRange());
271  }
272 
273 
275  if(t0WriteHandle.isValid()) {
276  ATH_MSG_DEBUG(" StrawT0Container already available ");
277  return StatusCode::SUCCESS ;
278  }
279 
280  if(t0Cdo->initialize().isFailure()) ATH_MSG_WARNING("Could not initialize T0 Container for key " << m_t0WriteKey.fullKey());
281 
282  if(t0WriteHandle.record(rangeW,std::move(t0Cdo)).isFailure()) {
283  ATH_MSG_ERROR("Could not record T0 Container for key " << m_t0WriteKey.fullKey() << " with WriteHandle ");
284  return StatusCode::FAILURE;
285  } else {
286  ATH_MSG_INFO("Recorded T0 Container for key " << m_t0WriteKey.fullKey() << " with range " << t0WriteHandle.getRange());
287  }
288 
289 
290  return StatusCode::SUCCESS ;
291 }
292 
294 {
297  m_trtid->straw(id),level ) ;
298 }
299 EventIDRange TRTCondWrite::IOVInfRange() const
300 {
301  const EventIDBase start {1, EventIDBase::UNDEFEVT, EventIDBase::UNDEFNUM, EventIDBase::UNDEFNUM, 0};
302  const EventIDBase stop {EventIDBase::UNDEFNUM-1, EventIDBase::UNDEFEVT, EventIDBase::UNDEFNUM, EventIDBase::UNDEFNUM, EventIDBase::UNDEFNUM-1};
303  return EventIDRange{start,stop};
304 }
305 
306 
307 
308 
python.PyKernel.retrieve
def retrieve(aClass, aKey=None)
Definition: PyKernel.py:110
TRTCondWrite::finalize
virtual StatusCode finalize(void) override
Definition: TRTCondWrite.cxx:126
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
checkFileSG.line
line
Definition: checkFileSG.py:75
DinesRtRelation.h
TRTCondWrite::m_t0WriteKey
SG::WriteCondHandleKey< StrawT0Container > m_t0WriteKey
Definition: TRTCondWrite.h:69
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
AthCommonDataStore< AthCommonMsg< Algorithm > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
TRTCondWrite::m_condSvc
ServiceHandle< ICondSvc > m_condSvc
Definition: TRTCondWrite.h:65
mergePhysValFiles.start
start
Definition: DataQuality/DataQualityUtils/scripts/mergePhysValFiles.py:14
TRTCondWrite::m_rtWriteKey
SG::WriteCondHandleKey< RtRelationContainer > m_rtWriteKey
Definition: TRTCondWrite.h:68
TRTCondWrite::m_par_caltextfile
std::string m_par_caltextfile
input text file
Definition: TRTCondWrite.h:62
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:293
BasicRtRelation.h
TRTCondWrite::~TRTCondWrite
~TRTCondWrite(void)
destructor
Definition: TRTCondWrite.cxx:38
TRTCondWrite::m_setup
bool m_setup
true at first event
Definition: TRTCondWrite.h:61
TRTCondWrite::readTextFile
virtual StatusCode readTextFile(const std::string &file, int &format)
Definition: TRTCondWrite.cxx:160
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:89
TRTCond::RtRelation
Definition: RtRelation.h:27
TRT_ID::straw
int straw(const Identifier &id) const
Definition: TRT_ID.h:902
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
Identifier
Definition: DetectorDescription/Identifier/Identifier/Identifier.h:32
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
TRTCondWrite::IOVInfRange
virtual EventIDRange IOVInfRange() const
Definition: TRTCondWrite.cxx:299
python.xAODType.dummy
dummy
Definition: xAODType.py:4
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:866
TRT_ID::straw_layer
int straw_layer(const Identifier &id) const
Definition: TRT_ID.h:893
TRT_ID::layer_or_wheel
int layer_or_wheel(const Identifier &id) const
Definition: TRT_ID.h:884
TRTCondWrite::TRTCondWrite
TRTCondWrite(const std::string &name, ISvcLocator *pSvcLocator)
constructor
Definition: TRTCondWrite.cxx:24
AthAlgorithm
Definition: AthAlgorithm.h:47
id
SG::auxid_t id
Definition: Control/AthContainers/Root/debug.cxx:194
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
TRT_ID::phi_module
int phi_module(const Identifier &id) const
Definition: TRT_ID.h:875
python.LumiBlobConversion.pos
pos
Definition: LumiBlobConversion.py:18
RtRelationFactory.h
TRTCondWrite::initialize
virtual StatusCode initialize(void) override
Definition: TRTCondWrite.cxx:41
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:24
TRTCondWrite::m_trtid
const TRT_ID * m_trtid
trt id helper
Definition: TRTCondWrite.h:63
TRTCondWrite::readTextFile_Format1
virtual StatusCode readTextFile_Format1(std::istream &)
Definition: TRTCondWrite.cxx:192
TRTCond::ExpandedIdentifier
Identifier for TRT detector elements in the conditions code.
Definition: InnerDetector/InDetConditions/TRT_ConditionsData/TRT_ConditionsData/ExpandedIdentifier.h:30
TRTCondWrite::m_detstore
ServiceHandle< StoreGateSvc > m_detstore
Definition: TRTCondWrite.h:64
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:132