ATLAS Offline Software
Loading...
Searching...
No Matches
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
16LArFlatFromFile::LArFlatFromFile( const std::string& name,
17 ISvcLocator* pSvcLocator ) :
18 ::AthAlgorithm( name, pSvcLocator ),
19 m_hashMax(0),
20 m_onlineID(nullptr), m_onlineSCID(nullptr)
21{
22
23}
24
26{
27 ATH_CHECK(m_cablingKey.initialize());
28 return StatusCode::SUCCESS;
29}
30
31
32
33void 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 int 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,hash,blobName);
62 value=1.0; //Default vaue is 1.0, since these are multiplicative constants
63 ++nDefault;
64 }
65 if (hash < 0) {
66 errIfConnected(chid,hash,blobName," Wrong hash !!!");
67 hash=0;
68 }
69 else if (hash >= static_cast<int>(nGain)) {
70 errIfConnected(chid,hash,blobName," Wrong Gain !!!");
71 hash=0;
72 }
73
74 if(m_isSC)
75 values[hash][m_onlineSCID->channel_Hash(chid)]=value;
76 else
77 values[hash][m_onlineID->channel_Hash(chid)]=value;
78 } // over the input file
79
80 for (unsigned gain=0;gain<nGain;++gain) {
81 auto attrList = std::make_unique<coral::AttributeList>(*spec);
82 (*attrList)["version"].setValue(0U);
83 coral::Blob& blob=(*attrList)[blobName].data<coral::Blob>();
84 blob.resize(m_hashMax*sizeof(float));
85 float* pblob=static_cast<float*>(blob.startingAddress());
86 for (unsigned hs=0;hs<m_hashMax;++hs) {
87 pblob[hs]=values[gain][hs];
88 ++nChannels;
89 }
90 unsigned coolChan=gain;
91
92 coll->add(coolChan,*attrList.release());
93 }
94
95 ATH_MSG_INFO( "Converted " << blobName << " to inline storage. Total number of channels=" << nChannels );
96 ATH_MSG_INFO( "Number of channels filled with default value (1.0) " << nDefault << " (including disconnected)" );
97 if (nCopiedEMPS)
98 ATH_MSG_INFO( "\t Number of low gain EMBPS channels copied from medium gain" << nCopiedEMPS );
99 StatusCode sc=detStore()->record(std::move(coll),outputName);
100 if (sc.isFailure()) {
101 ATH_MSG_ERROR( "Failed to record CondAttrListCollection with key" << outputName );
102 return;
103 }
104 return;
105}
106
107
108void LArFlatFromFile::ofcFlat(const std::string& input, const std::string& outputName) {
109
110 ATH_MSG_INFO("LArFlatFromFile::ofcFlat, starting");
111 // parsing the file, fill OFC arrays
112 std::vector<std::vector<std::vector<float> > > vofc_a; //[ngain][nchannel][nsamples]
113 std::vector<std::vector<std::vector<float> > > vofc_b;
114 vofc_a.resize(m_ngain);
115 vofc_b.resize(m_ngain);
116 for (unsigned i=0; i<m_ngain; ++i) {
117 vofc_a[i].resize(m_hashMax);
118 vofc_b[i].resize(m_hashMax);
119 }
120
121 std::ifstream myfile(input);
122 // we expect two lines per channel with Id, hash and values
123 // it contains particular pattern, change it if formatting has changed
124 std::string line, line1;
125 unsigned long id, id1;
126 unsigned hash, hash1;
127 char mychar, mychar1;
128 while (std::getline(myfile, line),std::getline(myfile, line1)) {
129 std::stringstream st(line);
130 st>>std::hex>>id>>std::dec>>hash;
131 std::stringstream st1(line1);
132 st>>std::hex>>id1>>std::dec>>hash1;
133 // check equality of Id and has in both lines
134 if(id != id1 || hash != hash1) {
135 ATH_MSG_ERROR("Different input id or hash: "<<line<<" "<<line1);
136 continue;
137 }
138 // Now it should contain a and b chars
139 st>>mychar;
140 st1>>mychar1;
141 if(mychar != 'a' || mychar1 != 'b') {
142 ATH_MSG_ERROR("Wrong OFC type: "<<line<<" "<<line1);
143
144 }
145
146 // check if nsamples corresponds to asked one, we expect the same number in each gain
147 }
148
149 unsigned nChannels=0;
150 unsigned nDefault=0;
151
152 coral::AttributeListSpecification* spec = new coral::AttributeListSpecification();
153 spec->extend("OFCa", "blob");
154 spec->extend("OFCb", "blob");
155 spec->extend("TimeOffset","blob");
156 spec->extend<unsigned>("nSamples");
157 spec->extend<unsigned>("version");
158 auto collOFC = std::make_unique<CondAttrListCollection>(true);
159
160 float timeOffset=0.;
161
162 for (unsigned gain=0;gain<m_ngain;++gain) {
163
164 auto attrList = std::make_unique<coral::AttributeList>(*spec);
165 (*attrList)["version"].setValue(0U);
166 coral::Blob& ofcaBlob=(*attrList)["OFCa"].data<coral::Blob>();
167 coral::Blob& ofcbBlob=(*attrList)["OFCb"].data<coral::Blob>();
168 coral::Blob& toBlob=(*attrList)["TimeOffset"].data<coral::Blob>();
169
170 (*attrList)["nSamples"].setValue(m_nsample);
171
172 ofcaBlob.resize(m_hashMax*sizeof(float)*m_nsample);
173 ofcbBlob.resize(m_hashMax*sizeof(float)*m_nsample);
174 toBlob.resize(m_hashMax*sizeof(float));
175 float* pOfca=static_cast<float*>(ofcaBlob.startingAddress());
176 float* pOfcb=static_cast<float*>(ofcbBlob.startingAddress());
177 float* pTimeOffset=static_cast<float*>(toBlob.startingAddress());
178 for (unsigned hs=0;hs<m_hashMax;++hs) {
179 const HWIdentifier chid=m_onlineID->channel_Id(hs);
180 LArOFCFlat::OFCRef_t ofca(vofc_a[gain][hs]);
181 LArOFCFlat::OFCRef_t ofcb(vofc_b[gain][hs]);
182
183 if (ofca.size()==m_nsample) {
184 for (unsigned i=0;i<m_nsample;++i) {
185 pOfca[hs*m_nsample+i]=ofca[i];
186 }
187 }
188 else {
189 std::stringstream message;
190 message <<"Number of samples don't match. Expect " << m_nsample << ", got " << ofca.size() << ".";
191 errIfConnected(chid,gain,"OFCa", message.str());
192 for (unsigned i=0;i<m_nsample;++i) {
193 pOfca[hs*m_nsample+i]=1.0;
194 }
195 ++nDefault;
196 }
197
198 if (ofcb.size()==m_nsample) {
199 for (unsigned i=0;i<m_nsample;++i) {
200 pOfcb[hs*m_nsample+i]=ofcb[i];
201 }
202 }
203 else {
204 std::stringstream message;
205 message <<"Number of samples don't match. Expect " << m_nsample << ", got " << ofcb.size() << ".";
206 errIfConnected(chid,gain,"OFCb", message.str());
207 for (unsigned i=0;i<m_nsample;++i) {
208 pOfcb[hs*m_nsample+i]=0.0;
209 }
210 }
211 pTimeOffset[hs]=timeOffset;
212 ++nChannels;
213
214 }//end loop over hash ids
215 collOFC->add(gain,*(attrList.release()));
216 }//end loop over gains
217
218 StatusCode sc=detStore()->record(std::move(collOFC),outputName);//"/LAR/ElecCalibFlat/OFC");
219 if (sc.isFailure()) {
220 ATH_MSG_ERROR( "Failed to record CondAttrListCollection OFC with key " << outputName );
221 return;
222 }
223
224 ATH_MSG_INFO( "Converted OFCs to inline storage. Total number of channels=" << nChannels );
225 ATH_MSG_INFO( "Number of channels filled with default OFCs {1,1,1,1} " << nDefault << " (including disconnected)" );
226 return;
227}
228
229
230
232
233
234 ATH_CHECK( detStore()->retrieve(m_onlineID));
235 ATH_CHECK( detStore()->retrieve(m_onlineSCID));
236
237 if(m_isSC)
238 m_hashMax=m_onlineSCID->channelHashMax();
239 else
240 m_hashMax=m_onlineID->channelHashMax();
241
242
243
244 //OFC:
245 if (m_OFCInput.size()) {
246 std::ifstream myfile(m_OFCInput);
247 // new lines will be skipped unless we stop it from happening:
248 myfile.unsetf(std::ios_base::skipws);
249
250 // count the newlines with an algorithm specialized for counting:
251 unsigned line_count = std::count( std::istream_iterator<char>(myfile),
252 std::istream_iterator<char>(), '\n');
253 myfile.close();
254 if (m_checkCompletness && 2*m_hashMax != line_count) {
255 ATH_MSG_ERROR( "Failed to check file with OFCs" );
256 return StatusCode::FAILURE;
257 } else {
258 if(m_isSC)
259 ofcFlat(m_OFCInput,"/LAR/ElecCalibSC/OFC");
260 else
261 ofcFlat(m_OFCInput,"/LAR/ElecCalibFlat/OFC");
262 }
263 }//end have m_OFCInput
264
265 //SIngle:
266 if (m_SingleInput.size()) {
267 std::ifstream myfile(m_SingleInput);
268 // new lines will be skipped unless we stop it from happening:
269 myfile.unsetf(std::ios_base::skipws);
270
271 // count the newlines with an algorithm specialized for counting:
272 unsigned line_count = std::count( std::istream_iterator<char>(myfile),
273 std::istream_iterator<char>(), '\n');
274 myfile.close();
275 if (m_checkCompletness && m_ngain*m_hashMax != line_count) {
276 ATH_MSG_ERROR( "Failed to check input file "<<m_SingleInput );
277 ATH_MSG_ERROR( "Line count: "<<line_count<<" expected: "<<m_ngain*m_hashMax<<" "<<m_isSC);
278 return StatusCode::FAILURE;
279 } else {
281 }
282 }//end have m_SingleInput
283
284 return StatusCode::SUCCESS;
285}
286
287
288
289
290
291void LArFlatFromFile::errIfConnected(const HWIdentifier chid, const int gain, const std::string& objName, const std::string& message) const{
292
294 const LArOnOffIdMapping* cabling{*cablingHdl};
295 if(!cabling) {
296 ATH_MSG_ERROR( "Do not have cabling mapping from key " << m_cablingKey.key() );
297 return;
298 }
299
300 if (cabling->isOnlineConnected(chid)) {
301 if (! (gain==2 && m_onlineID->isEMBPS(chid))) { //No LG Presampler calibration
302 ATH_MSG_ERROR( "No valid " << objName << " found for channel " << m_onlineID->channel_name(chid) << ", gain " << gain << ". ");
303 if (!message.empty()) ATH_MSG_ERROR( message );
304 ATH_MSG_ERROR( " Filling with default value." );
305 }
306 }
307 return;
308}
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_INFO(x)
This file defines the class for a collection of AttributeLists where each one is associated with a ch...
static Double_t sc
AthAlgorithm(const std::string &name, ISvcLocator *pSvcLocator)
Constructor with parameters:
const ServiceHandle< StoreGateSvc > & detStore() const
void ofcFlat(const std::string &input, const std::string &outputName)
Gaudi::Property< std::string > m_Folder
Gaudi::Property< bool > m_isSC
Gaudi::Property< std::string > m_SingleInput
virtual StatusCode initialize() override
Gaudi::Property< bool > m_checkCompletness
void errIfConnected(const HWIdentifier chid, const int gain, const std::string &objName, const std::string &message="") const
const LArOnline_SuperCellID * m_onlineSCID
Gaudi::Property< unsigned > m_ngain
const LArOnlineID * m_onlineID
virtual StatusCode stop() override
LArFlatFromFile()
Default constructor:
Gaudi::Property< unsigned > m_nsample
Gaudi::Property< std::string > m_BlobName
SG::ReadCondHandleKey< LArOnOffIdMapping > m_cablingKey
Gaudi::Property< std::string > m_OFCInput
Input files.
void singleFloatFlat(const char *blobName, const std::string &input, const std::string &outputName, const unsigned nGain)
ILArOFC::OFCRef_t OFCRef_t
Definition LArOFCFlat.h:24