ATLAS Offline Software
LArFlatFromFile.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 "LArFlatFromFile.h"
8 
9 
12 #include "CoralBase/Blob.h"
13 
14 #include <fstream>
15 
17  ISvcLocator* pSvcLocator ) :
18  ::AthAlgorithm( name, pSvcLocator ),
19  m_hashMax(0),
20  m_onlineID(nullptr), m_onlineSCID(nullptr)
21 {
22 
23 }
24 
26 {
28  return StatusCode::SUCCESS;
29 }
30 
31 
32 
33 void LArFlatFromFile::singleFloatFlat(const char* blobName, const std::string& input,
34  const std::string& outputName, const unsigned nGain) {
35 
36  unsigned nChannels=0;
37  unsigned nCopiedEMPS=0;
38  unsigned nDefault=0;
39 
40  coral::AttributeListSpecification* spec = new coral::AttributeListSpecification();
41  spec->extend(blobName, "blob");
42  spec->extend<unsigned>("version");
43  std::unique_ptr<CondAttrListCollection> coll= std::make_unique<CondAttrListCollection>(true);
44  // we expect line per channel with Id, hash and value
45  std::ifstream myfile(input);
46  std::string line;
47  std::vector< std::vector<float> > values(nGain, std::vector<float>(m_hashMax,1.0f));
48  unsigned id;
49  unsigned hash;
50  float value;
51  while (std::getline(myfile, line)) {
52  std::stringstream st(line);
53  if(m_isSC) {
54  st>>std::dec>>id>>std::dec>>value;
55  hash=0;
56  } else {
57  st>>std::hex>>id>>std::dec>>hash>>value;
58  }
59  const HWIdentifier chid(id);
60  if (value < 0) {
61  errIfConnected(chid,static_cast<int>(hash),blobName);
62  value=1.0; //Default vaue is 1.0, since these are multiplicative constants
63  ++nDefault;
64  }
65  if (hash >= nGain) {
66  errIfConnected(chid,static_cast<int>(hash),blobName," Wrong Gain !!!");
67  hash=0;
68  }
69 
70  if(m_isSC)
72  else
74  } // over the input file
75 
76  for (unsigned gain=0;gain<nGain;++gain) {
77  auto attrList = std::make_unique<coral::AttributeList>(*spec);
78  (*attrList)["version"].setValue(0U);
79  coral::Blob& blob=(*attrList)[blobName].data<coral::Blob>();
80  blob.resize(m_hashMax*sizeof(float));
81  float* pblob=static_cast<float*>(blob.startingAddress());
82  for (unsigned hs=0;hs<m_hashMax;++hs) {
83  pblob[hs]=values[gain][hs];
84  ++nChannels;
85  }
86  unsigned coolChan=gain;
87 
88  coll->add(coolChan,*attrList.release());
89  }
90 
91  ATH_MSG_INFO( "Converted " << blobName << " to inline storage. Total number of channels=" << nChannels );
92  ATH_MSG_INFO( "Number of channels filled with default value (1.0) " << nDefault << " (including disconnected)" );
93  if (nCopiedEMPS)
94  ATH_MSG_INFO( "\t Number of low gain EMBPS channels copied from medium gain" << nCopiedEMPS );
95  StatusCode sc=detStore()->record(std::move(coll),outputName);
96  if (sc.isFailure()) {
97  ATH_MSG_ERROR( "Failed to record CondAttrListCollection with key" << outputName );
98  return;
99  }
100  return;
101 }
102 
103 
104 void LArFlatFromFile::ofcFlat(const std::string& input, const std::string& outputName) {
105 
106  ATH_MSG_INFO("LArFlatFromFile::ofcFlat, starting");
107  // parsing the file, fill OFC arrays
108  std::vector<std::vector<std::vector<float> > > vofc_a; //[ngain][nchannel][nsamples]
109  std::vector<std::vector<std::vector<float> > > vofc_b;
110  vofc_a.resize(m_ngain);
111  vofc_b.resize(m_ngain);
112  for (unsigned i=0; i<m_ngain; ++i) {
113  vofc_a[i].resize(m_hashMax);
114  vofc_b[i].resize(m_hashMax);
115  }
116 
117  std::ifstream myfile(input);
118  // we expect two lines per channel with Id, hash and values
119  // it contains particular pattern, change it if formatting has changed
120  std::string line, line1;
121  unsigned long id, id1;
122  unsigned hash, hash1;
123  char mychar, mychar1;
124  while (std::getline(myfile, line),std::getline(myfile, line1)) {
125  std::stringstream st(line);
126  st>>std::hex>>id>>std::dec>>hash;
127  std::stringstream st1(line1);
128  st>>std::hex>>id1>>std::dec>>hash1;
129  // check equality of Id and has in both lines
130  if(id != id1 || hash != hash1) {
131  ATH_MSG_ERROR("Different input id or hash: "<<line<<" "<<line1);
132  continue;
133  }
134  // Now it should contain a and b chars
135  st>>mychar;
136  st1>>mychar1;
137  if(mychar != 'a' || mychar1 != 'b') {
138  ATH_MSG_ERROR("Wrong OFC type: "<<line<<" "<<line1);
139 
140  }
141 
142  // check if nsamples corresponds to asked one, we expect the same number in each gain
143  }
144 
145  unsigned nChannels=0;
146  unsigned nDefault=0;
147 
148  coral::AttributeListSpecification* spec = new coral::AttributeListSpecification();
149  spec->extend("OFCa", "blob");
150  spec->extend("OFCb", "blob");
151  spec->extend("TimeOffset","blob");
152  spec->extend<unsigned>("nSamples");
153  spec->extend<unsigned>("version");
154  auto collOFC = std::make_unique<CondAttrListCollection>(true);
155 
156  float timeOffset=0.;
157 
158  for (unsigned gain=0;gain<m_ngain;++gain) {
159 
160  auto attrList = std::make_unique<coral::AttributeList>(*spec);
161  (*attrList)["version"].setValue(0U);
162  coral::Blob& ofcaBlob=(*attrList)["OFCa"].data<coral::Blob>();
163  coral::Blob& ofcbBlob=(*attrList)["OFCb"].data<coral::Blob>();
164  coral::Blob& toBlob=(*attrList)["TimeOffset"].data<coral::Blob>();
165 
166  (*attrList)["nSamples"].setValue(m_nsample);
167 
168  ofcaBlob.resize(m_hashMax*sizeof(float)*m_nsample);
169  ofcbBlob.resize(m_hashMax*sizeof(float)*m_nsample);
170  toBlob.resize(m_hashMax*sizeof(float));
171  float* pOfca=static_cast<float*>(ofcaBlob.startingAddress());
172  float* pOfcb=static_cast<float*>(ofcbBlob.startingAddress());
173  float* pTimeOffset=static_cast<float*>(toBlob.startingAddress());
174  for (unsigned hs=0;hs<m_hashMax;++hs) {
175  const HWIdentifier chid=m_onlineID->channel_Id(hs);
176  LArOFCFlat::OFCRef_t ofca(vofc_a[gain][hs]);
177  LArOFCFlat::OFCRef_t ofcb(vofc_b[gain][hs]);
178 
179  if (ofca.size()==m_nsample) {
180  for (unsigned i=0;i<m_nsample;++i) {
181  pOfca[hs*m_nsample+i]=ofca[i];
182  }
183  }
184  else {
185  std::stringstream message;
186  message <<"Number of samples don't match. Expect " << m_nsample << ", got " << ofca.size() << ".";
187  errIfConnected(chid,gain,"OFCa", message.str());
188  for (unsigned i=0;i<m_nsample;++i) {
189  pOfca[hs*m_nsample+i]=1.0;
190  }
191  ++nDefault;
192  }
193 
194  if (ofcb.size()==m_nsample) {
195  for (unsigned i=0;i<m_nsample;++i) {
196  pOfcb[hs*m_nsample+i]=ofcb[i];
197  }
198  }
199  else {
200  std::stringstream message;
201  message <<"Number of samples don't match. Expect " << m_nsample << ", got " << ofcb.size() << ".";
202  errIfConnected(chid,gain,"OFCb", message.str());
203  for (unsigned i=0;i<m_nsample;++i) {
204  pOfcb[hs*m_nsample+i]=0.0;
205  }
206  }
207  pTimeOffset[hs]=timeOffset;
208  ++nChannels;
209 
210  }//end loop over hash ids
211  collOFC->add(gain,*(attrList.release()));
212  }//end loop over gains
213 
214  StatusCode sc=detStore()->record(std::move(collOFC),outputName);//"/LAR/ElecCalibFlat/OFC");
215  if (sc.isFailure()) {
216  ATH_MSG_ERROR( "Failed to record CondAttrListCollection OFC with key " << outputName );
217  return;
218  }
219 
220  ATH_MSG_INFO( "Converted OFCs to inline storage. Total number of channels=" << nChannels );
221  ATH_MSG_INFO( "Number of channels filled with default OFCs {1,1,1,1} " << nDefault << " (including disconnected)" );
222  return;
223 }
224 
225 
226 
228 
229 
232 
233  if(m_isSC)
235  else
237 
238 
239 
240  //OFC:
241  if (m_OFCInput.size()) {
242  std::ifstream myfile(m_OFCInput);
243  // new lines will be skipped unless we stop it from happening:
244  myfile.unsetf(std::ios_base::skipws);
245 
246  // count the newlines with an algorithm specialized for counting:
247  unsigned line_count = std::count( std::istream_iterator<char>(myfile),
248  std::istream_iterator<char>(), '\n');
249  myfile.close();
250  if (m_checkCompletness && 2*m_hashMax != line_count) {
251  ATH_MSG_ERROR( "Failed to check file with OFCs" );
252  return StatusCode::FAILURE;
253  } else {
254  if(m_isSC)
255  ofcFlat(m_OFCInput,"/LAR/ElecCalibSC/OFC");
256  else
257  ofcFlat(m_OFCInput,"/LAR/ElecCalibFlat/OFC");
258  }
259  }//end have m_OFCInput
260 
261  //SIngle:
262  if (m_SingleInput.size()) {
263  std::ifstream myfile(m_SingleInput);
264  // new lines will be skipped unless we stop it from happening:
265  myfile.unsetf(std::ios_base::skipws);
266 
267  // count the newlines with an algorithm specialized for counting:
268  unsigned line_count = std::count( std::istream_iterator<char>(myfile),
269  std::istream_iterator<char>(), '\n');
270  myfile.close();
271  if (m_checkCompletness && m_ngain*m_hashMax != line_count) {
272  ATH_MSG_ERROR( "Failed to check input file "<<m_SingleInput );
273  ATH_MSG_ERROR( "Line count: "<<line_count<<" expected: "<<m_ngain*m_hashMax<<" "<<m_isSC);
274  return StatusCode::FAILURE;
275  } else {
277  }
278  }//end have m_SingleInput
279 
280  return StatusCode::SUCCESS;
281 }
282 
283 
284 
285 
286 
287 void LArFlatFromFile::errIfConnected(const HWIdentifier chid, const int gain, const std::string& objName, const std::string& message) const{
288 
290  const LArOnOffIdMapping* cabling{*cablingHdl};
291  if(!cabling) {
292  ATH_MSG_ERROR( "Do not have cabling mapping from key " << m_cablingKey.key() );
293  return;
294  }
295 
296  if (cabling->isOnlineConnected(chid)) {
297  if (! (gain==2 && m_onlineID->isEMBPS(chid))) { //No LG Presampler calibration
298  ATH_MSG_ERROR( "No valid " << objName << " found for channel " << m_onlineID->channel_name(chid) << ", gain " << gain << ". ");
299  if (!message.empty()) ATH_MSG_ERROR( message );
300  ATH_MSG_ERROR( " Filling with default value." );
301  }
302  }
303  return;
304 }
python.PyKernel.retrieve
def retrieve(aClass, aKey=None)
Definition: PyKernel.py:110
RPDUtils::nChannels
unsigned constexpr int nChannels
Definition: RPDUtils.h:23
LArOnlineID_Base::channel_Hash
IdentifierHash channel_Hash(HWIdentifier channelId) const
Create channel_hash from channel_Id.
Definition: LArOnlineID_Base.cxx:1636
CondAttrListCollection.h
This file defines the class for a collection of AttributeLists where each one is associated with a ch...
SG::ReadCondHandle
Definition: ReadCondHandle.h:44
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
LArFlatFromFile::m_OFCInput
Gaudi::Property< std::string > m_OFCInput
Input files.
Definition: LArFlatFromFile.h:56
CaloCondBlobAlgs_fillNoiseFromASCII.spec
spec
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:46
LArG4FSStartPointFilterLegacy.line1
line1
Definition: LArG4FSStartPointFilterLegacy.py:58
LArFlatFromFile::ofcFlat
void ofcFlat(const std::string &input, const std::string &outputName)
Definition: LArFlatFromFile.cxx:104
CaloCondBlobAlgs_fillNoiseFromASCII.gain
gain
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:109
ReadCellNoiseFromCool.cabling
cabling
Definition: ReadCellNoiseFromCool.py:154
LArFlatFromFile::m_checkCompletness
Gaudi::Property< bool > m_checkCompletness
Definition: LArFlatFromFile.h:71
python.subdetectors.tile.Blob
Blob
Definition: tile.py:17
athena.value
value
Definition: athena.py:124
LArFlatFromFile::m_BlobName
Gaudi::Property< std::string > m_BlobName
Definition: LArFlatFromFile.h:68
SG::VarHandleKey::key
const std::string & key() const
Return the StoreGate ID for the referenced object.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:141
HWIdentifier
Definition: HWIdentifier.h:13
AthenaAttributeList.h
ReweightUtils.message
message
Definition: ReweightUtils.py:15
LArFlatFromFile::stop
virtual StatusCode stop() override
Definition: LArFlatFromFile.cxx:227
LArOFCFlat.h
LArFlatFromFile::LArFlatFromFile
LArFlatFromFile()
Default constructor:
dq_defect_bulk_create_defects.line
line
Definition: dq_defect_bulk_create_defects.py:27
XMLtoHeader.count
count
Definition: XMLtoHeader.py:84
LArFlatFromFile.h
AthenaPoolTestRead.sc
sc
Definition: AthenaPoolTestRead.py:27
python.Bindings.values
values
Definition: Control/AthenaPython/python/Bindings.py:808
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
LArOnlineID_Base::channelHashMax
size_type channelHashMax() const
Define channel hash tables max size.
Definition: LArOnlineID_Base.cxx:1901
LArFlatFromFile::initialize
virtual StatusCode initialize() override
Definition: LArFlatFromFile.cxx:25
LArFlatFromFile::m_onlineSCID
const LArOnline_SuperCellID * m_onlineSCID
Definition: LArFlatFromFile.h:50
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
lumiFormat.i
int i
Definition: lumiFormat.py:85
CreatePhysValWebPage.hs
hs
Definition: CreatePhysValWebPage.py:107
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
python.getProblemFolderFromLogs.st
st
Definition: getProblemFolderFromLogs.py:68
PlotPulseshapeFromCool.input
input
Definition: PlotPulseshapeFromCool.py:106
LArOnlineID_Base::channel_Id
HWIdentifier channel_Id(int barrel_ec, int pos_neg, int feedthrough, int slot, int channel) const
create channel identifier from fields
Definition: LArOnlineID_Base.cxx:1569
LArFlatFromFile::m_onlineID
const LArOnlineID * m_onlineID
Definition: LArFlatFromFile.h:49
LArFlatFromFile::singleFloatFlat
void singleFloatFlat(const char *blobName, const std::string &input, const std::string &outputName, const unsigned nGain)
Definition: LArFlatFromFile.cxx:33
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
hist_file_dump.f
f
Definition: hist_file_dump.py:140
LArFlatFromFile::m_ngain
Gaudi::Property< unsigned > m_ngain
Definition: LArFlatFromFile.h:63
LArFlatFromFile::m_SingleInput
Gaudi::Property< std::string > m_SingleInput
Definition: LArFlatFromFile.h:57
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
SG::CondHandleKey::initialize
StatusCode initialize(bool used=true)
LArFlatFromFile::m_isSC
Gaudi::Property< bool > m_isSC
Definition: LArFlatFromFile.h:60
lumiFormat.outputName
string outputName
Definition: lumiFormat.py:65
LArFlatFromFile::errIfConnected
void errIfConnected(const HWIdentifier chid, const int gain, const std::string &objName, const std::string &message="") const
Definition: LArFlatFromFile.cxx:287
CaloCondBlobAlgs_fillNoiseFromASCII.hash
dictionary hash
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:108
LArOnlineID_Base::isEMBPS
bool isEMBPS(const HWIdentifier id) const
Definition: LArOnlineID_Base.cxx:1665
LArOnline_SuperCellID.h
LArFlatFromFile::m_cablingKey
SG::ReadCondHandleKey< LArOnOffIdMapping > m_cablingKey
Definition: LArFlatFromFile.h:53
LArFlatFromFile::m_nsample
Gaudi::Property< unsigned > m_nsample
Definition: LArFlatFromFile.h:64
LArOnlineID_Base::channel_name
std::string channel_name(const HWIdentifier id) const
Return a string corresponding to a feedthrough name given an identifier.
Definition: LArOnlineID_Base.cxx:225
LArFlatFromFile::m_hashMax
unsigned m_hashMax
Definition: LArFlatFromFile.h:48
CaloCondBlobAlgs_fillNoiseFromASCII.blob
blob
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:95
LArVectorProxy
Proxy for accessing a range of float values like a vector.
Definition: LArVectorProxy.h:38
LArFlatFromFile::m_Folder
Gaudi::Property< std::string > m_Folder
Definition: LArFlatFromFile.h:67
LArOnOffIdMapping
Definition: LArOnOffIdMapping.h:20