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