ATLAS Offline Software
LArReadParamsFromFile.icc
Go to the documentation of this file.
1 //Dear emacs, this is -*- c++ -*-
2 
3 /*
4  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
5 */
6 
7 
8 #include "LArRawConditions/LArConditionsContainerBase.h"
9 
10 #include <iostream>
11 #include <sstream>
12 #include <string>
13 
14 template<class DATA>
15 LArReadParamsFromFile<DATA>::LArReadParamsFromFile(const std::string& name, ISvcLocator* pSvcLocator):
16  AthAlgorithm(name, pSvcLocator),
17  m_onlineHelper(0),
18  m_emId(0),
19  m_hecId(0),
20  m_fcalId(0),
21  m_groupingType(LArConditionsContainerBase::SingleGroup),
22  m_useCalibLines(false),
23  m_dataclass(0)
24 
25 {
26  // file name to be read
27  declareProperty("File",m_file="") ;
28  // choose whether use offline ID (default is online)
29  declareProperty("UseOfflineIdentifier",m_useOfflineIdentifier=false) ;
30  // assign grouping type (only for LArConditionsContainer-based classes)
31  declareProperty("GroupingType",m_groupingName="Unknown") ;
32  declareProperty("ChannelIdType",m_chIdType="UNKNOWN");
33  declareProperty("CustomKey",m_customKey="");
34 }
35 
36 template <class DATA>
37 LArReadParamsFromFile<DATA>::~LArReadParamsFromFile()
38 {}
39 
40 template <class DATA>
41 StatusCode LArReadParamsFromFile<DATA>::initialize() {
42  ATH_MSG_DEBUG ( "in initialize" );
43 
44  if ( m_groupingName == "Unknown" ) {
45  m_groupingType = LArConditionsContainerBase::Unknown ;
46  } else if ( m_groupingName == "SingleGroup" ) {
47  m_groupingType = LArConditionsContainerBase::SingleGroup ;
48  } else if ( m_groupingName == "SubDetectorGrouping" ) {
49  m_groupingType = LArConditionsContainerBase::SubDetectorGrouping ;
50  } else if ( m_groupingName == "FeedThroughGrouping" ) {
51  m_groupingType = LArConditionsContainerBase::FeedThroughGrouping ;
52  } else if ( m_groupingName == "ExtendedFThGrouping" ) {
53  m_groupingType = LArConditionsContainerBase::ExtendedFTGrouping ;
54  } else if ( m_groupingName == "ExtendedSubDetGrouping" ) {
55  m_groupingType = LArConditionsContainerBase::ExtendedSubDetGrouping ;
56  } else {
57  ATH_MSG_ERROR ( "Grouping type " << m_groupingName << " is not foreseen!" );
58  ATH_MSG_ERROR ( "Only \"Unknown\", \"SingleGroup\", \"SubDetectorGrouping\", \"FeedThroughGrouping\" are allowed" );
59  return StatusCode::FAILURE ;
60  }
61 
62  const CaloCell_ID* idHelper = nullptr;
63  ATH_CHECK( detStore()->retrieve (idHelper, "CaloCell_ID") );
64  m_emId = idHelper->em_idHelper();
65  m_hecId = idHelper->hec_idHelper();
66  m_fcalId = idHelper->fcal_idHelper();
67  if (!m_emId) {
68  ATH_MSG_ERROR ( "Could not access lar EM ID helper" );
69  return StatusCode::FAILURE;
70  }
71  if (!m_hecId) {
72  ATH_MSG_ERROR ( "Could not access lar HEC ID helper" );
73  return StatusCode::FAILURE;
74  }
75  if (!m_fcalId) {
76  ATH_MSG_ERROR ( "Could not access lar FCAL ID helper" );
77  return StatusCode::FAILURE;
78  }
79 
80  ATH_CHECK( detStore()->retrieve(m_onlineHelper, "LArOnlineID") );
81 
82  ATH_CHECK( m_cablingKey.initialize() );
83 
84  if ( m_chIdType == std::string("UNKNOWN") ) {
85  // use jO specification (default=false) for m_useOfflineIdentifier
86  // and default=false for m_useCalibLines
87  m_useCalibLines = false ;
88  } else if ( m_chIdType == std::string("OfflineID") ) {
89  m_useOfflineIdentifier = true ;
90  m_useCalibLines = false ;
91  } else if ( m_chIdType == std::string("OnlineID") ) {
92  m_useOfflineIdentifier = false ;
93  m_useCalibLines = false ;
94  } else if ( m_chIdType == std::string("OnlineCalibID") ) {
95  m_useOfflineIdentifier = false ;
96  m_useCalibLines = true ;
97  } else {
98  ATH_MSG_ERROR ( "ChannelIdType=" << m_chIdType << " is not valid!" );
99  ATH_MSG_ERROR ( "Allowed options are: OfflineID, OnlineID, OnlineCalibID" );
100  return StatusCode::FAILURE ;
101  }
102 
103  if( m_useOfflineIdentifier ) {
104  ATH_MSG_INFO ( "Reading parameters using *** offline identifiers ***" );
105  } else if ( m_useCalibLines ) {
106  ATH_MSG_INFO ( "Reading parameters for *** calibration lines ***" );
107  } else {
108  ATH_MSG_INFO ( "Reading parameters using *** online identifiers ***" );
109  }
110 
111  return StatusCode::SUCCESS;
112 }
113 
114 
115 template <class DATA>
116 StatusCode LArReadParamsFromFile<DATA>::stop ATLAS_NOT_THREAD_SAFE ()
117 {
118  ATH_MSG_DEBUG ( "in stop" );
119 
120  if ( m_file == std::string("") ) {
121  ATH_MSG_ERROR ( "Input file name was not specified!" );
122  return StatusCode::FAILURE ;
123  }
124 
125  m_dataclass = new DATA ;
126  if ( m_groupingType != LArConditionsContainerBase::Unknown ) {
127  m_dataclass->setGroupingType( static_cast<LArConditionsContainerBase::GroupingType>(m_groupingType) ) ;
128  }
129  ATH_CHECK( m_dataclass->initialize() );
130 
131  std::string classname = LArParamsProperties::getClassName(m_dataclass) ;
132  std::string key = LArParamsProperties::keyword( classname ) ;
133  if ( m_customKey != "") key = m_customKey;
134 
135  ATH_CHECK( readFile() );
136  ATH_MSG_INFO ( "Object " << classname << " successfully uploaded from file " << m_file );
137 
138  ATH_CHECK( detStore()->record(m_dataclass,key) );
139  ATH_MSG_INFO ( "Object " << classname << " recorded into detStore with key " << key );
140 
141  ATH_CHECK ( do_symLink(m_dataclass) );
142  ATH_MSG_INFO ( "Object " << classname << " symLinked to its abstract interface" );
143 
144  ATH_MSG_INFO ( "finished!" );
145  return StatusCode::SUCCESS;
146 }// end stop-method.
147 
148 
149 template <class DATA>
150 StatusCode LArReadParamsFromFile<DATA>::readFile() {
151  // File format:
152  // for online ID: barrel_ec posneg FT slot channel parameters...
153  // for offline ID: det subdet barrel_ec layer region eta phi parameters...
154  int det, subdet, barrel_ec, posneg, ft, slot, channel, region, layer, eta, phi ;
155  std::vector<float> pRead ;
156  HWIdentifier chID ;
157 
158  SG::ReadCondHandle<LArOnOffIdMapping> cablingHdl{m_cablingKey};
159  const LArOnOffIdMapping* cabling{*cablingHdl};
160  if(!cabling) {
161  ATH_MSG_ERROR( "Do not have cabling from key " << m_cablingKey.key() );
162  return StatusCode::FAILURE;
163  }
164 
165  std::ifstream f(m_file.c_str()) ; // open input file
166  if ( ! f.good() ) {
167  ATH_MSG_ERROR ( "Could not open file " << m_file << ", are you sure it exists??" );
168  return StatusCode::FAILURE ;
169  }
170  ATH_MSG_DEBUG ( "File " << m_file << " opened" );
171 
172  unsigned nrec = 0 ;
173  unsigned npar = LArParamsProperties::numberOfParams(m_dataclass) ;
174  ATH_MSG_DEBUG ( "The file should contain " << npar << " parameters per record" );
175 
176  while (1) { // read through the file records
177 
178  if ( f.eof() ) break ;
179  std::string s ;
180  getline(f,s) ;
181  std::istringstream buffer(s) ;
182  ATH_MSG_VERBOSE ( "*** Record: '" << s << "'" );
183 
184  if ( s == std::string("") ) continue ; // skip empty records
185 
186  bool good_chID = true ;
187 
188  if ( m_useOfflineIdentifier ) { // get HWIdentifier from offline "coordinates"
189 
190  buffer >> det ;
191  if ( buffer.fail() ) {
192  ATH_MSG_WARNING ( "Could not read 'det' field for offline identifier" );
193  continue ; // skip and go to next record
194  }
195  buffer >> subdet ;
196  if ( buffer.fail() ) {
197  ATH_MSG_WARNING ( "Could not read 'subdet' field for offline identifier" );
198  continue ; // skip and go to next record
199  }
200  buffer >> barrel_ec ;
201  if ( buffer.fail() ) {
202  ATH_MSG_WARNING ( "Could not read 'barrel_ec' field for offline identifier" );
203  continue ; // skip and go to next record
204  }
205  buffer >> layer ;
206  if ( buffer.fail() ) {
207  ATH_MSG_WARNING ( "Could not read 'layer' field for offline identifier" );
208  continue ; // skip and go to next record
209  }
210  buffer >> region ;
211  if ( buffer.fail() ) {
212  ATH_MSG_WARNING ( "Could not read 'region' field for offline identifier" );
213  continue ; // skip and go to next record
214  }
215  buffer >> eta ;
216  if ( buffer.fail() ) {
217  ATH_MSG_WARNING ( "Could not read 'eta' field for offline identifier" );
218  continue ; // skip and go to next record
219  }
220  buffer >> phi ;
221  if ( buffer.fail() ) {
222  ATH_MSG_WARNING ( "Could not read 'phi' field for offline identifier" );
223  continue ; // skip and go to next record
224  }
225  ATH_MSG_VERBOSE ( "Det=" << det << " SubDet=" << subdet << " BarrelEC=" << barrel_ec
226  << " Sampling=" << layer << " Region=" << region << " Eta=" << eta << " Phi=" << phi );
227  good_chID = true ;
228  if ( det != 4 ) {
229  ATH_MSG_WARNING ( "Det=" << det << " is not LArCalorimeter -- cannot handle it!" );
230  good_chID = false ;
231  } else {
232  Identifier id ;
233  if ( subdet == 1 ) { // LArEM
234  id = m_emId->channel_id(barrel_ec,layer,region,eta,phi) ;
235  ATH_MSG_VERBOSE ( "LAr EM Identifier=" << id );
236  } else if ( subdet == 2 ) { // LArHEC
237  id = m_hecId->channel_id(barrel_ec,layer,region,eta,phi) ;
238  ATH_MSG_VERBOSE ( "LAr HEC Identifier=" << id );
239  } else if ( subdet == 3 ) { // LArFCAL
240  // id = m_fcalId->channel_id(det,layer,region,eta,phi) ;
241  // log << MSG::VERBOSE << "LAr FCAL Identifier=" << id << endmsg ;
242  ATH_MSG_WARNING ( "Subdet=" << subdet
243  << " is LArFCAL -- conversion to online channel not implemented yet!" );
244  good_chID = false ;
245  } else {
246  ATH_MSG_WARNING ( "Subdet=" << subdet << " is unknown -- cannot handle it!" );
247  good_chID = false ;
248  }
249  if ( good_chID ) {
250  try {
251  chID = cabling->createSignalChannelID(id) ;
252  } catch ( LArID_Exception & except ) {
253  ATH_MSG_WARNING ( "Could not get HWId for offline Id " << id );
254  good_chID = false ;
255  }
256  }
257  }
258  if ( ! good_chID ) continue ; // skip and go to next record
259 
260  } else { // get HWIdentifier from online "coordinates"
261 
262  buffer >> barrel_ec ;
263  if ( buffer.fail() ) {
264  ATH_MSG_WARNING ( "Could not read 'barrel_ec' field for online identifier" );
265  continue ; // skip and go to next record
266  }
267  buffer >> posneg ;
268  if ( buffer.fail() ) {
269  ATH_MSG_WARNING ( "Could not read 'posneg' field for online identifier" );
270  continue ; // skip and go to next record
271  }
272  buffer >> ft ;
273  if ( buffer.fail() ) {
274  ATH_MSG_WARNING ( "Could not read 'feedthrough' field for online identifier" );
275  continue ; // skip and go to next record
276  }
277  buffer >> slot ;
278  if ( buffer.fail() ) {
279  ATH_MSG_WARNING ( "Could not read 'slot' field for online identifier" );
280  continue ; // skip and go to next record
281  }
282  buffer >> channel ;
283  if ( buffer.fail() ) {
284  ATH_MSG_WARNING ( "Could not read 'channel' field for online identifier" );
285  continue ; // skip and go to next record
286  }
287  if ( m_useCalibLines ) {
288  chID = m_onlineHelper->calib_channel_Id(barrel_ec,posneg,ft,slot,channel) ;
289  } else {
290  chID = m_onlineHelper->channel_Id(barrel_ec,posneg,ft,slot,channel) ;
291  }
292 
293  } // here the HWIdentifier is obtained!
294 
295  if ( ! good_chID ) continue ; // skip and go to next record
296 
297  //
298  // get parameters
299  //
300  pRead.resize(npar) ;
301  const float defaultValue = -999 ;
302  for ( unsigned i=0 ; i<npar ; i++ ) pRead[i] = defaultValue ;
303  for ( unsigned i=0 ; i<npar ; i++ ) {
304  //if ( buffer.eof() ) {
305  //log << MSG::DEBUG << "end-of-record reached when reading parameter " << i << " of channel " << chID.get_compact() << endmsg ;
306  //break ;
307  //}
308  buffer >> pRead[i] ;
309  if ( buffer.fail() ) {
310  ATH_MSG_WARNING ( "failure when reading parameter " << i << " of channel " << chID.get_compact() );
311  break ;
312  }
313  }
314  // store into parameters vector
315  msg() << MSG::VERBOSE << "read ch=" << chID.get_compact() ;
316  for ( unsigned i=0 ; i<npar ; i++ ) msg() << " par[" << i << "]=" << pRead[i] ;
317  msg() << endmsg ;
318  for ( unsigned gain=0 ; gain<3 ; gain++ ) {
319  try {
320  StatusCode sc = set(m_dataclass,chID,gain,pRead) ;
321  if ( sc.isFailure() ) {
322  ATH_MSG_WARNING ( "Could not set parameters for ch=" << chID.get_compact() << " gain=" << gain );
323  }
324  } catch ( LArID_Exception & except ) {
325  ATH_MSG_WARNING ( "Bad HWId " << chID );
326  good_chID = false ;
327  }
328  }
329 
330  nrec ++ ;
331 
332  } // end of while(1) i.e. the loop through all file records
333 
334  ATH_MSG_DEBUG ( "end of file reached, read " << nrec << " records" );
335  return StatusCode::SUCCESS ;
336 }