ATLAS Offline Software
Loading...
Searching...
No Matches
LArRamps2Ntuple.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
4
8
9#include <cmath>
10
11LArRamps2Ntuple::LArRamps2Ntuple(const std::string& name, ISvcLocator* pSvcLocator):
12 LArCond2NtupleBase(name, pSvcLocator),
13 m_rampKey("LArRamp"){
14
15 declareProperty("ContainerKey", m_contKey,
16 "List of keys of RawRamp containers");
17 declareProperty("RampKey", m_rampKey,
18 "Key of LArRampComplete or LArRampMC objects");
19 declareProperty("NtupleName", m_ntName ="RAMPS");
20 declareProperty("RawRamp", m_rawRamp = false);
21 declareProperty("SaveAllSamples", m_saveAllSamples = false);
22 declareProperty("ApplyCorr", m_applyCorr=false);
23 declareProperty("AddCorrUndo", m_addCorrUndo=true);
24}
25
27 m_ntTitle="Ramps";
28 m_ntpath=std::string("/NTUPLES/FILE1/")+m_ntName;
29
30 ATH_CHECK(m_rampKey.initialize());
32}
33
34
36= default;
37
39 bool hasRawRampContainer=false;
40 StatusCode sc;
41 NTuple::Item<long> cellIndex;
42 NTuple::Item<long> gain;
43 NTuple::Item<long> corrUndo;
44 NTuple::Array<float> SampleMax;
45 NTuple::Array<float> TimeMax;
46 NTuple::Array<float> DAC;
47 NTuple::Array<float> ADC;
48 NTuple::Array<long> NTriggers;
49
50 // individual samples. Very dirty :-(
51 NTuple::Array<float> Sample0;
52 NTuple::Array<float> Sample1;
53 NTuple::Array<float> Sample2;
54 NTuple::Array<float> Sample3;
55 NTuple::Array<float> Sample4;
56 NTuple::Array<float> Sample5;
57 NTuple::Array<float> Sample6;
58
59 // idem for RMS
60 NTuple::Array<float> RMS0;
61 NTuple::Array<float> RMS1;
62 NTuple::Array<float> RMS2;
63 NTuple::Array<float> RMS3;
64 NTuple::Array<float> RMS4;
65 NTuple::Array<float> RMS5;
66 NTuple::Array<float> RMS6;
67
68 NTuple::Item<unsigned long> DACIndex;
69 NTuple::Array<float> coeffs;
70 NTuple::Item<unsigned long> coeffIndex;
71
72 NTuple::Item<float> RampRMS;
73
74 if (m_rawRamp && !m_contKey.empty()) {
75 //Retrieve Raw Ramp Container
76 for (const std::string& key : m_contKey) {
77 LArRawRampContainer* rawRampContainer=nullptr;
78 sc=m_detStore->retrieve(rawRampContainer,key);
79 if (sc!=StatusCode::SUCCESS || !rawRampContainer) {
80 ATH_MSG_WARNING( "Unable to retrieve LArRawRampContainer with key " << key );
81 }
82 else {
83 ATH_MSG_DEBUG( "Got LArRawRampContainer with key " << key );
84 hasRawRampContainer = true;
85 }
86 }
87 if (!hasRawRampContainer) ATH_MSG_WARNING( " No LArRawRampContainer found. Only fitted ramp in ntuple " );
88
89 }
90 //end-if m_rawRamp
91
92
93 // For compatibility with existing configurations, look in the detector
94 // store first, then in conditions.
95 const ILArRamp* ramp =
96 detStore()->tryConstRetrieve<ILArRamp> (m_rampKey.key());
97 if (!ramp) {
99 ramp = *readHandle;
100 }
101
102 LArRampComplete* rampComplete_nc=nullptr; //for (potential) applyCorr
103
104 if (ramp==nullptr) {
105 ATH_MSG_WARNING( "Unable to retrieve ILArRamp with key: "<<m_rampKey << " from DetectorStore" );
106 }
107
108 if (!ramp && !hasRawRampContainer) {
109 ATH_MSG_ERROR( "Have neither Raw Ramp nor Fitted Ramp. No Ntuple produced." );
110 return StatusCode::FAILURE;
111 }
112
113 sc=m_nt->addItem("cellIndex",cellIndex,0,2000);
114 if (sc!=StatusCode::SUCCESS) {
115 ATH_MSG_ERROR( "addItem 'Cell Index' failed" );
116 return StatusCode::FAILURE;
117 }
118
119 sc=m_nt->addItem("gain",gain,0,3);
120 if (sc!=StatusCode::SUCCESS) {
121 ATH_MSG_ERROR( "addItem 'gain' failed" );
122 return StatusCode::FAILURE;
123 }
124
125 if (m_addCorrUndo) {
126 sc=m_nt->addItem("corrUndo",corrUndo,0,1);
127 if (sc!=StatusCode::SUCCESS) {
128 ATH_MSG_ERROR( "addItem 'corrUndo' failed" );
129 return StatusCode::FAILURE;
130 }
131 }
132
133 if (hasRawRampContainer)
134 {
135 sc=m_nt->addItem("DACIndex",DACIndex,0,800);
136 if (sc!=StatusCode::SUCCESS) {
137 ATH_MSG_ERROR( "addItem 'DACIndex' failed" );
138 return StatusCode::FAILURE;
139 }
140
141 sc=m_nt->addItem("SampleMax",DACIndex,SampleMax);
142 if (sc!=StatusCode::SUCCESS)
143 {ATH_MSG_ERROR( "addItem 'SampleMax' failed" );
144 return StatusCode::FAILURE;
145 }
146
147 sc=m_nt->addItem("TimeMax",DACIndex,TimeMax);
148 if (sc!=StatusCode::SUCCESS)
149 {ATH_MSG_ERROR( "addItem 'TimeMax' failed" );
150 return StatusCode::FAILURE;
151 }
152 sc=m_nt->addItem("ADC",DACIndex,ADC);
153 if (sc!=StatusCode::SUCCESS)
154 {ATH_MSG_ERROR( "addItem 'ADC' failed" );
155 return StatusCode::FAILURE;
156 }
157
158 sc=m_nt->addItem("DAC",DACIndex,DAC);
159 if (sc!=StatusCode::SUCCESS)
160 {ATH_MSG_ERROR( "addItem 'DAC' failed" );
161 return StatusCode::FAILURE;
162 }
163
164 sc=m_nt->addItem("NTriggers",DACIndex,NTriggers);
165 if (sc!=StatusCode::SUCCESS) {
166 ATH_MSG_ERROR( "addItem 'NTriggers' failed" );
167 return StatusCode::FAILURE;
168 }
169
171 sc=m_nt->addItem("Sample0",DACIndex,Sample0);
172 if (sc!=StatusCode::SUCCESS)
173 {ATH_MSG_ERROR( "addItem 'Sample0' failed" );
174 return StatusCode::FAILURE;
175 }
176 sc=m_nt->addItem("Sample1",DACIndex,Sample1);
177 if (sc!=StatusCode::SUCCESS)
178 {ATH_MSG_ERROR( "addItem 'Sample1' failed" );
179 return StatusCode::FAILURE;
180 }
181 sc=m_nt->addItem("Sample2",DACIndex,Sample2);
182 if (sc!=StatusCode::SUCCESS)
183 {ATH_MSG_ERROR( "addItem 'Sample2' failed" );
184 return StatusCode::FAILURE;
185 }
186 sc=m_nt->addItem("Sample3",DACIndex,Sample3);
187 if (sc!=StatusCode::SUCCESS)
188 {ATH_MSG_ERROR( "addItem 'Sample3' failed" );
189 return StatusCode::FAILURE;
190 }
191 sc=m_nt->addItem("Sample4",DACIndex,Sample4);
192 if (sc!=StatusCode::SUCCESS)
193 {ATH_MSG_ERROR( "addItem 'Sample4' failed" );
194 return StatusCode::FAILURE;
195 }
196 sc=m_nt->addItem("Sample5",DACIndex,Sample5);
197 if (sc!=StatusCode::SUCCESS)
198 {ATH_MSG_ERROR( "addItem 'Sample5' failed" );
199 return StatusCode::FAILURE;
200 }
201 sc=m_nt->addItem("Sample6",DACIndex,Sample6);
202 if (sc!=StatusCode::SUCCESS)
203 {ATH_MSG_ERROR( "addItem 'Sample6' failed" );
204 return StatusCode::FAILURE;
205 }
206
207 sc=m_nt->addItem("RMS0",DACIndex,RMS0);
208 if (sc!=StatusCode::SUCCESS)
209 {ATH_MSG_ERROR( "addItem 'RMS0' failed" );
210 return StatusCode::FAILURE;
211 }
212 sc=m_nt->addItem("RMS1",DACIndex,RMS1);
213 if (sc!=StatusCode::SUCCESS)
214 {ATH_MSG_ERROR( "addItem 'RMS1' failed" );
215 return StatusCode::FAILURE;
216 }
217 sc=m_nt->addItem("RMS2",DACIndex,RMS2);
218 if (sc!=StatusCode::SUCCESS)
219 {ATH_MSG_ERROR( "addItem 'RMS2' failed" );
220 return StatusCode::FAILURE;
221 }
222 sc=m_nt->addItem("RMS3",DACIndex,RMS3);
223 if (sc!=StatusCode::SUCCESS)
224 {ATH_MSG_ERROR( "addItem 'RMS3' failed" );
225 return StatusCode::FAILURE;
226 }
227 sc=m_nt->addItem("RMS4",DACIndex,RMS4);
228 if (sc!=StatusCode::SUCCESS)
229 {ATH_MSG_ERROR( "addItem 'RMS4' failed" );
230 return StatusCode::FAILURE;
231 }
232 sc=m_nt->addItem("RMS5",DACIndex,RMS5);
233 if (sc!=StatusCode::SUCCESS)
234 {ATH_MSG_ERROR( "addItem 'RMS5' failed" );
235 return StatusCode::FAILURE;
236 }
237 sc=m_nt->addItem("RMS6",DACIndex,RMS6);
238 if (sc!=StatusCode::SUCCESS)
239 {ATH_MSG_ERROR( "addItem 'RMS6' failed" );
240 return StatusCode::FAILURE;
241 }
242 }// end-if Save all samples
243 }//end if rawRampContainer
244
245 if (ramp) {
246 sc=m_nt->addItem("Xi",coeffIndex,0,7);
247 if (sc!=StatusCode::SUCCESS)
248 {ATH_MSG_ERROR( "addItem 'coeffIndex' failed" );
249 return StatusCode::FAILURE;
250 }
251
252 sc=m_nt->addItem("X",coeffIndex,coeffs);
253 if (sc!=StatusCode::SUCCESS)
254 {ATH_MSG_ERROR( "addItem 'coeff' failed" );
255 return StatusCode::FAILURE;
256 }
257
258 if (hasRawRampContainer) { //== RampComplete && RawRamp
259 sc=m_nt->addItem("RampRMS",RampRMS,-1000,1000);
260 if (sc!=StatusCode::SUCCESS)
261 {ATH_MSG_ERROR( "addItem 'RampRMS' failed" );
262 return StatusCode::FAILURE;
263 }
264 }
265
266 if (m_applyCorr) {
267 const LArRampComplete* rampComplete=dynamic_cast<const LArRampComplete*>(ramp);
268 if (!rampComplete) {
269 ATH_MSG_WARNING("Failed to dyn-cast to ILArRamp to LArRampComplete. Cannot apply corrections");
270 m_applyCorr=false;
271 }
272 if (rampComplete and !rampComplete->correctionsApplied()) {
273 rampComplete_nc=const_cast<LArRampComplete*>(rampComplete);
274 sc=rampComplete_nc->applyCorrections();
275 if (sc.isFailure()) {
276 ATH_MSG_ERROR( "Failed to apply corrections to LArRampComplete!" );
277 }
278 else
279 ATH_MSG_INFO( "Applied corrections to LArRampComplete" );
280 }
281 else {
282 ATH_MSG_WARNING( "Corrections already applied. Can't apply twice!" );
283 }
284 }// end if applyCorr
285 }//end-if ramp
286
287 const LArOnOffIdMapping *cabling=nullptr;
288 if(m_isSC) {
289 ATH_MSG_DEBUG( "LArRamps2Ntuple: using SC cabling" );
291 cabling=*cablingHdl;
292 }else{
294 cabling=*cablingHdl;
295 }
296
297
298 if(!cabling) {
299 ATH_MSG_WARNING( "Do not have cabling object LArOnOffIdMapping" );
300 return StatusCode::FAILURE;
301 }
302
303 unsigned cellCounter=0;
304 std::set<std::pair<HWIdentifier,unsigned> > cellDone;
305 if (hasRawRampContainer) { //Loop over raw ramp container and fill ntuple
306
307 //Retrieve Raw Ramp Container
308 for (const std::string& key : m_contKey) {
309 LArRawRampContainer* rawRampContainer=nullptr;
310 sc=m_detStore->retrieve(rawRampContainer,key);
311 if (sc!=StatusCode::SUCCESS || !rawRampContainer) {
312 ATH_MSG_WARNING( "Unable to retrieve LArRawRampContainer with key " << key );
313 continue;
314 }
315 for (const LArRawRamp* rawramp : *rawRampContainer) {
316 const std::vector<LArRawRamp::RAMPPOINT_t>& singleRamp=rawramp->theRamp();
317
318 for (DACIndex=0;DACIndex<singleRamp.size();DACIndex++) {
319 SampleMax[DACIndex] = singleRamp[DACIndex].iMaxSample;
320 TimeMax[DACIndex] = singleRamp[DACIndex].TimeMax;
321 ADC[DACIndex] = singleRamp[DACIndex].ADC;
322 NTriggers[DACIndex] = singleRamp[DACIndex].NTriggers;
323 DAC[DACIndex] = singleRamp[DACIndex].DAC;
324
326
327 if ( singleRamp[DACIndex].Samples.empty() || singleRamp[DACIndex].RMS.empty() ) {
328 ATH_MSG_WARNING( "Cannot save all samples, vector empty" );
329 } else {
330
331 Sample0[DACIndex]=singleRamp[DACIndex].Samples[0];
332 Sample1[DACIndex]=singleRamp[DACIndex].Samples[1];
333 Sample2[DACIndex]=singleRamp[DACIndex].Samples[2];
334 Sample3[DACIndex]=singleRamp[DACIndex].Samples[3];
335 Sample4[DACIndex]=singleRamp[DACIndex].Samples[4];
336 if(singleRamp[DACIndex].Samples.size()>5) Sample5[DACIndex]=singleRamp[DACIndex].Samples[5];
337 if(singleRamp[DACIndex].Samples.size()>6) Sample6[DACIndex]=singleRamp[DACIndex].Samples[6];
338
339 RMS0[DACIndex]=singleRamp[DACIndex].RMS[0];
340 RMS1[DACIndex]=singleRamp[DACIndex].RMS[1];
341 RMS2[DACIndex]=singleRamp[DACIndex].RMS[2];
342 RMS3[DACIndex]=singleRamp[DACIndex].RMS[3];
343 RMS4[DACIndex]=singleRamp[DACIndex].RMS[4];
344 if(singleRamp[DACIndex].RMS.size()>5) RMS5[DACIndex]=singleRamp[DACIndex].RMS[5];
345 if(singleRamp[DACIndex].RMS.size()>6) RMS6[DACIndex]=singleRamp[DACIndex].RMS[6];
346 }
347 }
348
349 }
350
351 HWIdentifier chid=rawramp->channelID();
352 unsigned igain = (unsigned)rawramp->gain();
353 gain = igain;
354 if (m_addCorrUndo) corrUndo=0;
355 if (ramp && cabling->isOnlineConnected(chid)) {
356
357 //FT move to here
358 fillFromIdentifier(chid);
359 cellDone.insert(std::make_pair(chid, igain));
360
361 unsigned nDAC=0;
362 unsigned nCoeff=0;
363 const ILArRamp::RampRef_t rampcoeff = ramp->ADC2DAC(chid,igain);
364 if (rampcoeff.size()==0) {
365 ATH_MSG_WARNING( "Can't get fitted Ramp slot=" << static_cast<long>(m_slot) <<
366 " channel=" << static_cast<long>(m_channel) << " gain=" << igain );
367 }
368 for (coeffIndex=0;coeffIndex<rampcoeff.size();coeffIndex++) coeffs[coeffIndex]=rampcoeff[coeffIndex];
369 nDAC = singleRamp.size();
370 nCoeff = rampcoeff.size();
371
372 if (nDAC>1 && nCoeff>0) {
373
374 double rampDev=0;
375 for (unsigned iDAC=1;iDAC<nDAC;iDAC++) {
376 double fittedResult=0;
377 for (unsigned icoeff=0;icoeff<nCoeff;icoeff++)
378 fittedResult+=coeffs[icoeff]*pow(ADC[iDAC],icoeff);
379 rampDev+=(fittedResult-DAC[iDAC])*(fittedResult-DAC[iDAC]);
380 }//end loop over DAC values
381 RampRMS=1./(nDAC-1)*sqrt(rampDev);
382 }//end if nDAC>0 && nCoeff>0
383 else
384 RampRMS=-999;
385
386 sc=ntupleSvc()->writeRecord(m_nt);
387 if (sc!=StatusCode::SUCCESS) {
388 ATH_MSG_ERROR( "writeRecord failed" );
389 return StatusCode::FAILURE;
390 }
391
392 }//end if rampComplete or rampMC
393
394 cellCounter++;
395
396 }//end loop over container
397 } // loop over gains
398 }
399
400 //Iterate over gains and cells
401 // check if cell was already filled in rawRamp loop
402 unsigned nGain = m_isSC ? 1 : CaloGain::LARNGAIN;
403 for ( unsigned igain=CaloGain::LARHIGHGAIN; igain<nGain ; ++igain )
404 {
405 for (HWIdentifier chid : m_onlineId->channel_range()) {
406 if (cabling->isOnlineConnected(chid)) {
407 gain = (long)igain;
408 if ( !cellDone.empty() && cellDone.contains(std::make_pair(chid,gain)) ) continue;
409 if (m_addCorrUndo) corrUndo = 0;
410 if (not ramp) continue; // No ramp for this cell
411 const ILArRamp::RampRef_t rampcoeff=ramp->ADC2DAC(chid, gain);
412 if (rampcoeff.size()==0) continue; // No ramp for this cell
413 cellIndex = cellCounter;
414 fillFromIdentifier(chid);
415 for (coeffIndex=0;coeffIndex<rampcoeff.size();coeffIndex++) coeffs[coeffIndex]=rampcoeff[coeffIndex];
416
417 sc=ntupleSvc()->writeRecord(m_nt);
418
419 if (sc!=StatusCode::SUCCESS) {
420 ATH_MSG_ERROR( "writeRecord failed" );
421 return StatusCode::FAILURE;
422 }
423 }// end if isConnected
424 cellCounter++;
425 }//end loop over cells
426 }//end loop over gains
427
428
429 if (ramp && m_addCorrUndo) {
430 //Now loop over undoCorrections:
431 for ( unsigned igain=CaloGain::LARHIGHGAIN;
432 igain<CaloGain::LARNGAIN ; ++igain ) {
434 itUndo_e = itUndo;
435 const LArRampComplete *rampComplete=dynamic_cast<const LArRampComplete *>(ramp);
436 if(rampComplete) {
437 itUndo = rampComplete->undoCorrBegin(igain);
438 itUndo_e=rampComplete->undoCorrEnd(igain);
439
440 for(;itUndo!=itUndo_e;itUndo++) {
441 const HWIdentifier chid(itUndo->first);
442 gain = (long)igain;
443 corrUndo = 1;
444 const std::vector<float>& rampcoeff=itUndo->second.m_vRamp;
445 if (rampcoeff.empty())
446 continue; // No ramp for this cell
447
448 cellIndex = cellCounter;
449 fillFromIdentifier(chid);
450
451 for (coeffIndex=0;coeffIndex<rampcoeff.size();coeffIndex++) coeffs[coeffIndex]=rampcoeff[coeffIndex];
452
453 sc=ntupleSvc()->writeRecord(m_nt);
454
455 if (sc!=StatusCode::SUCCESS) {
456 ATH_MSG_ERROR( "writeRecord failed" );
457 return StatusCode::FAILURE;
458 }
459 }//end loop over cells
460 }
461 }//end loop over gains
462 }//end if add corrections
463
464 if (rampComplete_nc) {
465 ATH_CHECK(rampComplete_nc->undoCorrections());
466 ATH_MSG_INFO("Reverted corrections of LArRampComplete container");
467 }
468
469 ATH_MSG_INFO( "LArRamps2Ntuple has finished." );
470 return StatusCode::SUCCESS;
471
472} // end finalize-method.
#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)
static Double_t sc
INTupleSvc * ntupleSvc()
constexpr int pow(int base, int exp) noexcept
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T, V, H > &t)
const ServiceHandle< StoreGateSvc > & detStore() const
virtual RampRef_t ADC2DAC(const HWIdentifier &id, int gain) const =0
LArVectorProxy RampRef_t
This class defines the interface for accessing Ramp @stereotype Interface.
Definition ILArRamp.h:31
Gaudi::Property< bool > m_isSC
StoreGateSvc * m_detStore
SG::ReadCondHandleKey< LArOnOffIdMapping > m_cablingSCKey
NTuple::Item< long > m_slot
SG::ReadCondHandleKey< LArOnOffIdMapping > m_cablingKey
const LArOnlineID_Base * m_onlineId
NTuple::Item< long > m_channel
bool fillFromIdentifier(const HWIdentifier &id)
LArCond2NtupleBase(const std::string &name, ISvcLocator *pSvcLocator)
bool correctionsApplied() const
Have corrections been applied?
StatusCode applyCorrections()
apply correction set
StatusCode undoCorrections()
undo corrections that have been already applied
ConstCorrectionIt undoCorrBegin(unsigned int gain) const
get iterator over the Undo-Vector for a certain gain
ConstCorrectionIt undoCorrEnd(unsigned int gain) const
Subset::ConstCorrectionVecIt ConstCorrectionIt
SG::ReadCondHandleKey< ILArRamp > m_rampKey
std::vector< std::string > m_contKey
StatusCode initialize()
std::string m_ntName
LArRamps2Ntuple(const std::string &name, ISvcLocator *pSvcLocator)
virtual StatusCode stop()
This class stores a unfittet ramp (=a vector a ADC and DAC values)
Definition LArRawRamp.h:25
@ LARNGAIN
Definition CaloGain.h:19
@ LARHIGHGAIN
Definition CaloGain.h:18