ATLAS Offline Software
Loading...
Searching...
No Matches
FixLArElecSCCalib.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
4
22
23
24#include "FixLArElecSCCalib.h"
25#include "Gaudi/Property.h"
26#include "GaudiKernel/IToolSvc.h"
27
34
37
40#include "CoralBase/Blob.h"
41
42#include "TTree.h"
43#include "TFile.h"
44
45#include <fstream>
46
47FixLArElecSCCalib::FixLArElecSCCalib(const std::string& name, ISvcLocator* pSvcLocator) :
48 AthAlgorithm(name,pSvcLocator){
49
50}
51
54
56 ATH_MSG_INFO ( " in initialize " );
57
58 ATH_CHECK( detStore()->retrieve(m_sem_idhelper) );
59 ATH_CHECK( detStore()->retrieve(m_shec_idhelper) );
60 ATH_CHECK( detStore()->retrieve(m_sfcal_idhelper) );
62 ATH_CHECK( detStore()->retrieve(m_scell_idhelper) );
63
64 ATH_CHECK( m_cablingKeySC.initialize() );
65 ATH_CHECK( m_CLKeySC.initialize() );
66 ATH_CHECK( m_mcSymKey.initialize() );
67
68 return StatusCode::SUCCESS;
69}
70
72
73 ATH_MSG_INFO ( " in execute " );
74 return StatusCode::SUCCESS;
75}
76
78
79 ATH_MSG_INFO ( " in stop " );
80
81 switch(m_fixFlag) {
82 case 1: return fix1();
83 case 2: {
85 const LArOnOffIdMapping* cabling{*cablingHdl};
86 if(!cabling) {
87 ATH_MSG_ERROR( "Do not have cabling mapping from key " << m_cablingKeySC.key() );
88 return StatusCode::FAILURE;
89 }
91 const LArCalibLineMapping *clCont {*clHdl};
92 if(!clCont) {
93 ATH_MSG_ERROR( "Do not have calib line mapping !!!" );
94 return StatusCode::FAILURE;
95 }
96 return fix2(cabling, clCont);
97 }
98 case 3: {
100 const LArOnOffIdMapping* cabling{*cablingHdl};
101 if(!cabling) {
102 ATH_MSG_ERROR( "Do not have cabling mapping from key " << m_cablingKeySC.key() );
103 return StatusCode::FAILURE;
104 }
106 const LArMCSym* sym{*symHdl};
107 if(!sym) {
108 ATH_MSG_ERROR( "Do not have MCSym from key " << m_mcSymKey.key() );
109 return StatusCode::FAILURE;
110 }
111 return fix3(cabling,sym);
112 }
113 default: return StatusCode::SUCCESS;
114 }
115}
116
118
119 ATH_MSG_INFO ( " in fix1() " );
120
121 // Fix1 is for scaling the Pedestals and Rams
122 int n=0;
123
124 const LArPedestalSC *ped = nullptr;
125 CHECK(detStore()->retrieve(ped, "Pedestal_orig"));
126
127 coral::AttributeListSpecification* spec = new coral::AttributeListSpecification();
128 spec->extend("Pedestal", "blob");
129 spec->extend("PedestalRMS", "blob");
130 spec->extend<unsigned>("version");
131
133
134 unsigned gain=0;
135 unsigned hashMax=m_sonline_idhelper->channelHashMax();
136 coral::AttributeList* attrList = new coral::AttributeList(*spec);
137 (*attrList)["version"].setValue(0U);
138 coral::Blob& blobPed=(*attrList)["Pedestal"].data<coral::Blob>();
139 coral::Blob& blobRMS=(*attrList)["PedestalRMS"].data<coral::Blob>();
140 blobPed.resize(hashMax*sizeof(float));
141 blobRMS.resize(hashMax*sizeof(float));
142 float* pblobPed=static_cast<float*>(blobPed.startingAddress());
143 float* pblobRMS=static_cast<float*>(blobRMS.startingAddress());
144
145 for (unsigned hs=0;hs<hashMax;++hs) {
146 const HWIdentifier chid=m_sonline_idhelper->channel_Id(hs);
147 float p=ped->pedestal(chid,gain);
148 float rms=ped->pedestalRMS(chid,gain);
149
150 pblobPed[hs]=p*m_fixFactor;
151 pblobRMS[hs]=rms;
152 ++n;
153 }//end loop over hash ids
154 collPed->add(gain,*attrList);
155
156 StatusCode sc=detStore()->record(collPed,"/LAR/ElecCalibMCSC/Pedestal");
157 if (sc.isFailure()) {
158 ATH_MSG_ERROR( "Failed to record CondAttrListCollection for pedestal " );
159 return sc;
160 }
161
162 ATH_MSG_DEBUG(" Number of entries changes in pedestals = " <<n );
163
164 // Now ramp
165 n=0;
166 const LArRampSC *ramp = nullptr;
167 CHECK(detStore()->retrieve(ramp, "Ramp_orig"));
168
169 spec = new coral::AttributeListSpecification();
170 spec->extend("RampVec", "blob");
171 spec->extend<unsigned>("nPoints");
172 spec->extend<unsigned>("version");
174
175
176 std::vector<float> defaultRamp={0.0,1.0};
177
178 gain=0;
179
180 unsigned nPoints=0;
181 unsigned nDefault=0;
182 for (unsigned hs=0;hs<hashMax && nPoints==0;++hs) {
183 const HWIdentifier chid=m_sonline_idhelper->channel_Id(hs);
184 const ILArRamp::RampRef_t rampref= ramp->ADC2DAC(chid,gain);
185 nPoints=rampref.size();
186 }
187 if (nPoints==0) {
188 ATH_MSG_ERROR( "All input Ramps for gain " << gain << " have 0 points!" );
189 return StatusCode::FAILURE;
190 }
191
192 defaultRamp.resize(nPoints,0.0); //fill remaining points if needed
193 ATH_MSG_DEBUG( "Gain " << gain << ": Found a ramp polynom of degree " << nPoints << " in input data" );
194 attrList = new coral::AttributeList(*spec);
195 (*attrList)["version"].setValue(0U);
196 coral::Blob& blobRamp=(*attrList)["RampVec"].data<coral::Blob>();
197 (*attrList)["nPoints"].setValue(nPoints);
198 blobRamp.resize(hashMax*sizeof(float)*nPoints);
199 float* pblobRamp=static_cast<float*>(blobRamp.startingAddress());
200
201 for (unsigned hs=0;hs<hashMax;++hs) {
202 const HWIdentifier chid=m_sonline_idhelper->channel_Id(hs);
203 std::vector<float> rampVec(ramp->ADC2DAC(chid,gain).asVector());
204
205 if (rampVec.size()>=nPoints) {
206 for (size_t i=0;i<nPoints;++i) {
207 pblobRamp[nPoints*hs+i]=rampVec[i]*m_fixFactor;
208 }
209 }
210 else {
211 std::stringstream message;
212 message <<"Polynom degree doesn't match. Expect " << nPoints << ", got " << rampVec.size() << ".";
213 for (size_t i=0;i<nPoints;++i) {
214 pblobRamp[nPoints*hs+i]=defaultRamp[i];
215 }
216 ++nDefault;
217 }
218 ++n;
219 }//end loop over hash ids
220 coll->add(gain,*attrList);
221
222 sc=detStore()->record(coll,"/LAR/ElecCalibMCSC/Ramp");
223 if (sc.isFailure()) {
224 ATH_MSG_ERROR( "Failed to record CondAttrListCollection for ramp " );
225 return StatusCode::FAILURE;
226 }
227
228 ATH_MSG_INFO( "Converted Ramps to inline storage. Total number of channels " << n );
229 ATH_MSG_INFO( "Number of channels filled with default ramp {0,1} " << nDefault << " (including disconnected)" );
230
231 return StatusCode::SUCCESS;
232}
233
234StatusCode FixLArElecSCCalib::fix2(const LArOnOffIdMapping *cabling, const LArCalibLineMapping *cl) {
235
236 ATH_MSG_INFO ( " in fix2() " );
237
238 const uint32_t onlHashMax=m_sonline_idhelper->channelHashMax(); // all SC cells
239
240 coral::AttributeListSpecification* spec_calib = new coral::AttributeListSpecification();
241 spec_calib->extend("OnlineHashToCalibIds", "blob");
242 spec_calib->extend<unsigned>("version");
243 auto al_calib = std::make_unique<AthenaAttributeList>(*spec_calib);
244 coral::Blob& blobCalib=(*al_calib)["OnlineHashToCalibIds"].data<coral::Blob>();
245 (*al_calib)["version"].setValue(0U);
246 blobCalib.resize(onlHashMax*sizeof(uint32_t)*5); //Bigger than necessary
247 ATH_MSG_DEBUG("blobCalib size: "<<onlHashMax<<"*5");
248
249 spec_calib->release();
250 spec_calib = nullptr;
251
252 uint32_t* pBlobCalib=static_cast<uint32_t*>(blobCalib.startingAddress());
253
254 size_t calibIndex=0;
255
256 std::vector<unsigned> calibHist(17,0);
257 unsigned calibHistMax=0;
258
259 std::ofstream outfile("SCIdentifiers.txt");
260
261 outfile << "hash id bec pn FT SL chan id calo pn sampl reg eta phi calib" << std::endl;
262
263 for (uint32_t onlHash=0;onlHash<onlHashMax;++onlHash) {
264 const HWIdentifier hwid=m_sonline_idhelper->channel_Id(onlHash);
265 const std::vector<HWIdentifier>& calibIDs_tmp=cl->calibSlotLine(hwid);
266 std::vector<HWIdentifier> calibIDs;
267 for (unsigned i=0; i<calibIDs_tmp.size(); ++i) {
268 if(std::find(calibIDs.begin(), calibIDs.end(), calibIDs_tmp[i]) == calibIDs.end()) calibIDs.push_back(calibIDs_tmp[i]);
269 }
270 // deduplicate
271 Identifier id{};
272 auto pLArIdBase = dynamic_cast<const LArOnlineID_Base*>(m_sonline_idhelper);
273 auto pCaloCellBase = dynamic_cast<const CaloCell_Base_ID*>(m_scell_idhelper);
274 if ((pLArIdBase == nullptr) or (pCaloCellBase==nullptr)){
275 ATH_MSG_ERROR("dynamic_cast failed in FixLArElecSCCalib::fix2");
276 return StatusCode::FAILURE;
277 }
278 if (cabling->isOnlineConnected(hwid)) {
279 id=cabling->cnvToIdentifier(hwid);
280 print(hwid,pLArIdBase, pCaloCellBase,&id,&calibIDs,outfile);
281 } else {
282 print(hwid,pLArIdBase, pCaloCellBase,nullptr,&calibIDs,outfile);
283 }
284 const size_t nCalibLines=calibIDs.size();
285 if (nCalibLines > calibHistMax ) calibHistMax=nCalibLines;
286 if(calibHistMax > 17) {
287 ATH_MSG_ERROR( "Too much calib lines, adjust please !!!" << calibHistMax);
288 return StatusCode::FAILURE;
289 }
290 (calibHist[nCalibLines])++;
291 pBlobCalib[calibIndex++]=nCalibLines;
292 for(uint32_t iCalib=0;iCalib<nCalibLines;++iCalib)
293 pBlobCalib[calibIndex++]=calibIDs[iCalib].get_identifier32().get_compact();
294 }
295 blobCalib.resize(calibIndex*sizeof(uint32_t)); //Size down to actual size
296 outfile.close();
297 ATH_MSG_INFO( "calibHistMax: " << calibHistMax);
298 ATH_MSG_INFO( "BlobSize CalibId:" << calibIndex);
299 msg(MSG::INFO) << "nCalib[i] ";
300 for (unsigned j=0;j<17;++j)
301 msg() << calibHist[j] << "/";
302 msg() << endmsg;
303 ATH_CHECK(detStore()->record(std::move(al_calib),"/LAR/IdentifierOfl/CalibIdMap_SC"));
304 return StatusCode::SUCCESS;
305}
306
307void FixLArElecSCCalib::print (const HWIdentifier& hwid, const LArOnlineID_Base* onlineID, const CaloCell_Base_ID* caloCellID,
308 const Identifier *id, std::vector<HWIdentifier>* calibIDs, std::ostream& out) {
309 const IdentifierHash hwid_hash=onlineID->channel_Hash(hwid);
310 out << hwid_hash << " " << std::hex << "0x" << hwid.get_identifier32().get_compact() << std::dec << " "
311 << onlineID->barrel_ec(hwid) << " "
312 << onlineID->pos_neg(hwid) << " "
313 << onlineID->feedthrough(hwid) << " "
314 << onlineID->slot(hwid) << " "
315 << onlineID->channel(hwid) << " : ";
316 if (id) {
317 out << std::hex << "0x" << id->get_identifier32().get_compact() << std::dec << " "
318 << caloCellID->sub_calo(*id) << " "
319 << caloCellID->pos_neg(*id) << " "
320 << caloCellID->sampling(*id) << " "
321 << caloCellID->region(*id) << " "
322 << caloCellID->eta(*id) << " "
323 << caloCellID->phi(*id) << " ";
324 }
325 else
326 out << " disconnected ";
327
328 if(calibIDs){
329 for (size_t i=0;i<calibIDs->size();++i) {
330 out << std::hex << "0x" << calibIDs->at(i).get_identifier32().get_compact() << " ";
331 }
332 } else
333 out << " no calib ";
334
335 out << std::dec << std::endl;
336}
337
338StatusCode FixLArElecSCCalib::fix3(const LArOnOffIdMapping *cabling, const LArMCSym *sym) {
339
340 ATH_MSG_INFO ( " in fix3() " );
341
342 // Fix3 is for filling the MinBias blob from ntuple
343
344 auto spec = new coral::AttributeListSpecification();
345 spec->extend("MinBias", "blob");
346 spec->extend<unsigned>("version");
347 auto coll=std::make_unique<CondAttrListCollection>(true);
348
349 auto specav = new coral::AttributeListSpecification();
350 specav->extend("MinBiasAverage", "blob");
351 specav->extend<unsigned>("version");
353
354 unsigned hashMax=m_sonline_idhelper->channelHashMax();
355 coral::AttributeList attrList = coral::AttributeList(*spec);
356 attrList["version"].setValue(0U);
357 coral::Blob& blob = attrList["MinBias"].data<coral::Blob>();
358 blob.resize(hashMax*sizeof(float));
359 float* pblob=static_cast<float*>(blob.startingAddress());
360
361 coral::AttributeList attrListav = coral::AttributeList(*specav);
362 attrListav["version"].setValue(0U);
363 coral::Blob& blobav= attrListav["MinBiasAverage"].data<coral::Blob>();
364 blobav.resize(hashMax*sizeof(float));
365 float* pblobav=static_cast<float*>(blobav.startingAddress());
366
367 std::unique_ptr<TFile> fin= std::make_unique<TFile>(m_infile.value().c_str());
368 TTree *tin=dynamic_cast<TTree*>(fin->Get("m_tree"));
369 if (not tin) return StatusCode::FAILURE;
370 int ncell{};
371 int * identifier = new int[2862];
372 int * layer = new int[2862];
373 int * region = new int[2862];
374 int * ieta = new int[2862];
375 float * eta = new float[2862];
376 double * average = new double[2862];
377 double * rms = new double[2862];
378 TBranch *b_ncell{};
379 TBranch *b_identifier{};
380 TBranch *b_layer{};
381 TBranch *b_region{};
382 TBranch *b_ieta{};
383 TBranch *b_eta{};
384 TBranch *b_average{};
385 TBranch *b_rms{};
386 tin->SetMakeClass(1);
387 tin->SetBranchAddress("ncell", &ncell, &b_ncell);
388 tin->SetBranchAddress("identifier", identifier, &b_identifier);
389 tin->SetBranchAddress("layer", layer, &b_layer);
390 tin->SetBranchAddress("region", region, &b_region);
391 tin->SetBranchAddress("ieta", ieta, &b_ieta);
392 tin->SetBranchAddress("eta", eta, &b_eta);
393 tin->SetBranchAddress("average", average, &b_average);
394 tin->SetBranchAddress("rms", rms, &b_rms);
395 tin->GetEntry(0);
396
397
398 // read the ntuple (symmetrized)
399 std::map<Identifier, float> vmap, vmapav;
400 for(int icell=0; icell<ncell; ++icell) {
401
402 Identifier32 id32(identifier[icell]);
403 Identifier id(id32);
404 vmapav[id] = average[icell];
405 vmap[id] = rms[icell];
406
407 }
408 // now fill all SC
409 for (unsigned onlHash=0;onlHash<hashMax;++onlHash) {
410 const HWIdentifier hwid=m_sonline_idhelper->channel_Id(onlHash);
411 const Identifier id = cabling->cnvToIdentifier(hwid);
412 const HWIdentifier hwid2 = sym->ZPhiSymOfl(id);
413 const Identifier idsym = cabling->cnvToIdentifier(hwid2);
414 if (vmap.count(idsym)==0 || vmapav.count(idsym)==0) {
415 ATH_MSG_WARNING("Do not have sym value: "<<idsym.get_identifier32().get_compact()<<" "<<vmap[idsym]<<" "<<vmapav[idsym]);
416 } else {
417 pblob[onlHash] = vmap[idsym];
418 pblobav[onlHash] = vmapav[idsym];
419 }
420 }
421
422 coll->add(0,attrList);
423 collav->add(0,attrListav);
424
425 auto sz = coll->size();
426 ATH_CHECK(detStore()->record(std::move(coll),"/LAR/ElecCalibMCSC/MinBias"));
427 ATH_MSG_DEBUG("Stored coll with size "<<sz<<" into /LAR/ElecCalibMCSC/MinBias");
428
429 auto szav = collav->size();
430 ATH_CHECK(detStore()->record(collav,"/LAR/ElecCalibMCSC/MinBiasAverage"));
431 ATH_MSG_DEBUG("Stored coll with size "<<szav<<" into /LAR/ElecCalibMCSC/MinBiasAverage");
432
433 return StatusCode::SUCCESS;
434}
Scalar eta() const
pseudorapidity method
#define endmsg
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
Helper base class for offline cell identifiers.
Helper class for offline supercell identifiers.
This file defines the class for a collection of AttributeLists where each one is associated with a ch...
#define CHECK(...)
Evaluate an expression and check for errors.
static Double_t sz
static Double_t sc
AthAlgorithm(const std::string &name, ISvcLocator *pSvcLocator)
Constructor with parameters:
const ServiceHandle< StoreGateSvc > & detStore() const
MsgStream & msg() const
Helper base class for offline cell identifiers.
int phi(const Identifier id) const
LAr field values (NOT_VALID == invalid request)
int sampling(const Identifier id) const
LAr field values (NOT_VALID == invalid request)
int sub_calo(const Identifier id) const
returns an int taken from SUBCALO enum and describing the subCalo to which the Id belongs.
int region(const Identifier id) const
LAr field values (NOT_VALID == invalid request)
int pos_neg(const Identifier id) const
LAr field values (NOT_VALID == invalid request)
int eta(const Identifier id) const
LAr field values (NOT_VALID == invalid request)
This class is a collection of AttributeLists where each one is associated with a channel number.
SG::ReadCondHandleKey< LArCalibLineMapping > m_CLKeySC
StatusCode fix2(const LArOnOffIdMapping *cabling, const LArCalibLineMapping *cl)
const LArHEC_SuperCell_ID * m_shec_idhelper
const LArEM_SuperCell_ID * m_sem_idhelper
const LArFCAL_SuperCell_ID * m_sfcal_idhelper
virtual StatusCode initialize() override
DoubleProperty m_fixFactor
virtual StatusCode execute() override
const CaloCell_SuperCell_ID * m_scell_idhelper
IntegerProperty m_fixFlag
SG::ReadCondHandleKey< LArOnOffIdMapping > m_cablingKeySC
FixLArElecSCCalib(const std::string &name, ISvcLocator *pSvcLocator)
SG::ReadCondHandleKey< LArMCSym > m_mcSymKey
virtual StatusCode stop() override
void print(const HWIdentifier &hwid, const LArOnlineID_Base *onlineID, const CaloCell_Base_ID *caloCellID, const Identifier *id=nullptr, std::vector< HWIdentifier > *calibIDs=nullptr, std::ostream &out=std::cout)
StatusCode fix3(const LArOnOffIdMapping *cabling, const LArMCSym *sym)
const LArOnline_SuperCellID * m_sonline_idhelper
StringProperty m_infile
LArVectorProxy RampRef_t
This class defines the interface for accessing Ramp @stereotype Interface.
Definition ILArRamp.h:31
value_type get_compact() const
Get the compact id.
This is a "hash" representation of an Identifier.
Identifier32 get_identifier32() const
Get the 32-bit version Identifier, will be invalid if >32 bits needed.
Helper class to handle z-phi symmetry of calibration constants in MC.
Definition LArMCSym.h:19
HWIdentifier ZPhiSymOfl(const Identifier notSymOffId) const
Find the symmetric HWID for an offline cell identifier.
Definition LArMCSym.h:48
Helper for the Liquid Argon Calorimeter cell identifiers.
int feedthrough(const HWIdentifier id) const
Return the feedthrough of a hardware cell identifier : feedthrough = [0,31] Barrel - A/C side or H/...
int slot(const HWIdentifier id) const
Return the slot number of a hardware cell identifier: slot = [1,15] Slot-ID in top part of the crat...
int barrel_ec(const HWIdentifier id) const
Return the position barrel or endcap of a hardware cell identifier: barrel_ec = [0,...
IdentifierHash channel_Hash(HWIdentifier channelId) const
Create channel_hash from channel_Id.
int channel(const HWIdentifier id) const
Return the channel number of a hardware cell identifier channel = [0,127] in all FEB.
int pos_neg(const HWIdentifier id) const
Return the side of a hardware cell identifier pos_neg = [0,1] positive-side or negative-side Barrel...
virtual RampRef_t ADC2DAC(const HWIdentifier &CellID, int gain) const
Definition LArRampSC.cxx:26
std::vector< value_type > asVector() const
Convert back to a vector.