ATLAS Offline Software
LArRawChannelBuilderToolOFCIter.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 
9 #include "GaudiKernel/MsgStream.h"
10 
11 #include "LArRawEvent/LArDigit.h"
12 
13 #include "CLHEP/Units/SystemOfUnits.h"
14 
15 #include <cmath>
16 
17 using CLHEP::ns;
18 using CLHEP::nanosecond;
19 using CLHEP::picosecond;
20 
21 #define MAXINT 2147483000
22 #define MAXINT2 -2147483000
23 
25  const std::string& name,
26  const IInterface* parent):
28  m_larOFIterCont(nullptr),
29  m_storeGate("StoreGateSvc", name),
30  m_peakReco("LArOFPeakRecoTool") {
31  m_helper = new LArRawChannelBuilderStatistics( 3, // number of possible errors
32  0x05); // bit pattern special for this tool,
33  // to be stored in "uint16_t provenance"
34  m_helper->setErrorString(0, "no errors");
35  m_helper->setErrorString(1, "channel saturated");
36  m_helper->setErrorString(2, "OFC not valid");
37 
38  declareProperty("ADCMax", m_AdcMax=4095);
40  declareProperty("ECut", m_Ecut=50);
41  declareProperty("doQual", m_doQual=true);
42  declareProperty("doIter", m_doIter=true);
43  declareProperty("defaultPhase", m_defaultPhase=0);
44  declareProperty("minADCforIter", m_minADCforIter=30);
45  declareProperty("minADCforIterInSigma", m_minADCforIterInSigma=-1);
46  declareProperty("minSample", m_minSample=0);
47  declareProperty("maxSample", m_maxSample=31);
48  declareProperty("nIterations", m_nIterProp=10);
49  declareProperty("StoreTiming", m_storeTiming=false,
50  "Create container of LArOFIterResults in StoreGate");
51  declareProperty("TimingContainerKey", m_timingContKey="LArOFIterResult",
52  "Key of the LArOFIterResultsContainer in StoreGate");
53  declareProperty("doMC", m_doMC=false,
54  " take noise from LArNoise instead of LArPedestal");
55 
56 }
57 
59 {
60  MsgStream log(msgSvc(), name());
61 
62  StatusCode sc=m_peakReco.retrieve();
63 
64  if( sc.isFailure() ) {
65  log << MSG::ERROR << "Unable to retrieve LArOFPeakRecoTool" <<endmsg;
66  return StatusCode::FAILURE;
67  }
68 
69  sc = m_storeGate.retrieve();
70  if (sc.isFailure()) {
71  log << MSG::ERROR << "StoreGate service not found" << endmsg;
72  return sc;
73  }
74 
75  if (m_minADCforIterInSigma>0) {//threshold given in terms of pedestal-rms, get pedestal
76  if (m_doMC) {
77  log << MSG::INFO << " Min ADC for iteration " << m_minADCforIterInSigma << "* LArNoise " <<endmsg;
78  }
79  else {
80  log << MSG::INFO << " Min ADC for iteration " << m_minADCforIterInSigma << "* pedestalRMS " <<endmsg;
81  }
82  }
83  else
84  log << MSG::INFO << " Min ADC for iteration "<<m_minADCforIter <<endmsg;
85 
86  log << MSG::INFO << " DefaultPhase "<<m_defaultPhase <<endmsg;
87  log << MSG::INFO << " Min and Max Sample "<<m_minSample<< " "<<m_maxSample<<endmsg;
88 
91 
92  return StatusCode::SUCCESS;
93 }
94 
96  //std::cout << "in LArRawChannelBuilderToolOFCIter::initEvent()" << std::endl;
97  if (m_storeTiming) {
100  if (sc.isFailure()) {
101  MsgStream log(msgSvc(), name());
102  log << MSG::ERROR << "Failed to record a LArOFIterResultsContainer with key " << m_timingContKey << " to StoreGate." << endmsg;
103  delete m_larOFIterCont;
104  m_larOFIterCont=nullptr;
105  }
106  }
107 }
108 
109 
111  float pedestal,
112  const std::vector<float>& ramps,
113  MsgStream* pLog )
114 {
115  const HWIdentifier chid=m_parent->curr_chid;
117  const EventContext& ctx = Gaudi::Hive::currentContext();
118 
119  uint16_t iprovenance=0;
120 
121  bool debugPrint=false;
122  if (bool(pLog)) {
123  if (pLog->level() <= MSG::DEBUG) debugPrint=true;
124  }
125 
126  if(debugPrint)
127  (*pLog) << MSG::DEBUG << "Start " <<MSG::hex<< chid.get_compact() <<MSG::dec<< endmsg;
128 
129 
130  // Loop over samples, to find maximum, check for saturation and subtract pedestal
131  const std::vector<short>& samples = digit->samples();
132  unsigned int sampsize = (unsigned int) samples.size();
133  float peakval = -999.;
134  unsigned int ipeak = 0;
135  //static std::vector<float> signal(sampsize); //Pedestal-subtracted
136  m_signal.resize(sampsize);
137  float currval = 0.;
138  for (unsigned int ii = 0; ii < sampsize; ii++) {
139  if (samples[ii]==0 || samples[ii]>=m_AdcMax) { //Check for saturation
140  if(debugPrint) (*pLog) << MSG::DEBUG
141  << "Saturation on channel 0x" << MSG::hex << chid.get_compact() << MSG::dec
142  << " ADC=" << samples[ii];
143  if ( m_skipSaturatedCells ) {
144  if(debugPrint) (*pLog) << " Skipping channel." << endmsg;
146  return false;
147  }
148  else
149  if(debugPrint) (*pLog) << endmsg;
150 
151  iprovenance = iprovenance | 0x0400;
152  } //end if saturated
153  currval = (float)(samples[ii] - pedestal);
154  m_signal[ii]=currval;
155  if ((ii >= m_minSample)&&(ii <= m_maxSample)&&(currval > peakval)) { ipeak = ii; peakval = currval; }
156  }
157 
158  if(debugPrint) (*pLog) << MSG::DEBUG << "Peak value: " << peakval << ", peak sample:" << ipeak << endmsg;
159 
160  int nIteration = m_nIterProp;
161  bool doIter=false;
162  if (m_doIter) {
163  if (m_minADCforIterInSigma>0) {//threshold given in terms of pedestal-rms, get pedestal
164  if (m_doMC) {
166  float sigma = larNoise->noise(chid,gain);
167  if (peakval > (sigma*m_minADCforIterInSigma)) doIter=true;
168  }
169  else {
171  float vRMS=larPedestal->pedestalRMS(chid,gain);
172  if (vRMS >= (1.0+LArElecCalib::ERRORCODE)) {
173  if (peakval > (vRMS*m_minADCforIterInSigma)) doIter=true;//enough signal...
174  }
175  else { //no pedestal found, use adc threshold
176  if (peakval > m_minADCforIter) doIter=true;//enough signal...
177  }
178  }
179  }//end if threshold given in terms of pedestal rms
180  else
181  if (peakval >= m_minADCforIter) doIter=true;//enough signal...
182  }//end if doIter
183 
184 
185  if (!doIter) {//No iteration, insufficient signal
186  nIteration=1;
187  ipeak = m_parent->curr_shiftTimeSamples + 2 ;
188  }
189 
190 
191  if (ipeak > sampsize - 3) ipeak = sampsize - 3 ;
192  if (ipeak < 2) ipeak = 2;
193 
194 
195  unsigned int peak_min = ipeak - 1 ;
196  unsigned int peak_max = ipeak + 1 ;
197 
198  float ADCPeak=0;
199  float time=0.;
202  nIteration,
203  ipeak,peak_min, peak_max
204  );
205 
206  if (m_larOFIterCont) {
207  //the iteration works always on teh same copy of the results object. Need to do a deep-copy in order to publish...
208  //LArOFIterResults* res=new LArOFIterResults(results);
209  m_larOFIterCont->push_back(results);
210  }
211  if (results.getValid()) {
212  ADCPeak = results.getAmplitude();
213  // this should be ~0 if the peak is at curr_shiftTimeSamples
214 
215  // FIXME: this time definition still misses the tstart from the OFC to be absolutely computed
216  time = (25.*((int)(results.getPeakSample_final())-2-m_parent->curr_shiftTimeSamples)-(results.getDelay_final()-results.getTau()));
217  //log << MSG::DEBUG << "Peak and time properly retrieved with OFPeakRecoTool:";
218  //log << MSG::DEBUG << "ADCPeak = " << ADCPeak <<", time = "<< time << endmsg;
219  }
220  else {
221  // log << MSG::DEBUG << ". OFC iteration not valid for channel 0x"
222  // << MSG::hex << chid.get_compact() << MSG::dec
223  // << " Gain = " << gain << ". Skipping channel." << endmsg;
225  return false;
226  }
227 
228  float power=1;
229  float energy=0;
230  for( unsigned int i=0; i<ramps.size(); i++)
231  {
232  energy += ramps[i] * power;
233  power *= ADCPeak;
234  }
235 
236 
237 
238  //log << MSG::DEBUG << "ADCPeak = " << ADCPeak <<", time = "<< time<<
239  // " energy "<<energy <<endmsg;
240 
241  uint16_t iquality=0;
242 
243  if ((m_doQual) && (std::fabs(energy)>m_Ecut) && (results.getValid())) {
244  int iqua = (int)(results.getQuality());
245  if (iqua > 0xFFFF) iqua=0xFFFF;
246  iquality = (uint16_t)(iqua & 0xFFFF);
247  iprovenance = iprovenance | 0x2000;
248  }
249 
250 
251  //Reminder: Bit-pattern
252  //ppcc bbbb sqqq qqqq qqqq
253 
254  iprovenance |= (m_parent->qualityBitPattern & 0x00FF);
255  iprovenance |= (m_helper->returnBitPattern() & 0x00FF);
256  if (results.getConverged())
257  iprovenance |= 0x0100;
258  iprovenance = iprovenance & 0x3FFF;
259 
260  time=time*(nanosecond/picosecond); //Convert time to ps
261 
262  const float fMAXINT = static_cast<float>(MAXINT);
263  const float fMAXINT2 = static_cast<float>(MAXINT2);
264 
265  if (time>fMAXINT) time=fMAXINT;
266  if (time<fMAXINT2) time=fMAXINT2;
267 
268  if (energy>fMAXINT) energy=fMAXINT;
269  if (energy<fMAXINT2) energy=fMAXINT2;
270 
271 
272  // Suppress false positive seen wth gcc.
273 #if __GNUC__ >= 11 && __GNUC__ <= 13
274 # pragma GCC diagnostic push
275 # pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
276 #endif
277  (this->*m_buildIt)((int)(floor(energy+0.5)),(int)floor(time+0.5),iquality,iprovenance,digit);
278 #if __GNUC__ >= 11 && __GNUC__ <= 13
279 # pragma GCC diagnostic pop
280 #endif
281 
282 
284 
285 
286  return true;
287 }
288 
LArRawChannelBuilderParams::curr_gain
CaloGain::CaloGain curr_gain
Definition: LArRawChannelBuilderParams.h:25
LArRawChannelBuilderToolOFCIter::m_maxSample
unsigned int m_maxSample
Definition: LArRawChannelBuilderToolOFCIter.h:60
pdg_comparison.sigma
sigma
Definition: pdg_comparison.py:324
SG::ReadCondHandle
Definition: ReadCondHandle.h:44
python.SystemOfUnits.nanosecond
int nanosecond
Definition: SystemOfUnits.py:119
LArRawChannelBuilderToolOFCIter::m_timingContKey
std::string m_timingContKey
Definition: LArRawChannelBuilderToolOFCIter.h:68
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
AthCommonDataStore< AthCommonMsg< AlgTool > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
MAXINT2
#define MAXINT2
Definition: LArRawChannelBuilderToolOFCIter.cxx:22
LArRawChannelBuilderToolOFCIter::m_pedestalRMSKey
SG::ReadCondHandleKey< ILArPedestal > m_pedestalRMSKey
Definition: LArRawChannelBuilderToolOFCIter.h:75
MAXINT
#define MAXINT
Definition: LArRawChannelBuilderToolOFCIter.cxx:21
LArRawChannelBuilderToolOFCIter::m_minADCforIterInSigma
float m_minADCforIterInSigma
Definition: LArRawChannelBuilderToolOFCIter.h:58
LArRawChannelBuilderToolOFCIter::LArRawChannelBuilderToolOFCIter
LArRawChannelBuilderToolOFCIter(const std::string &type, const std::string &name, const IInterface *parent)
Definition: LArRawChannelBuilderToolOFCIter.cxx:24
LArRawChannelBuilderToolOFCIter::m_minSample
unsigned int m_minSample
Definition: LArRawChannelBuilderToolOFCIter.h:59
LArRawChannelBuilderToolOFCIter::initTool
StatusCode initTool()
Definition: LArRawChannelBuilderToolOFCIter.cxx:58
LArRawChannelBuilderStatistics::setErrorString
void setErrorString(unsigned int nerr, const std::string &s)
Definition: LArRawChannelBuilderStatistics.cxx:69
LArRawChannelBuilderToolOFCIter::m_storeTiming
bool m_storeTiming
Definition: LArRawChannelBuilderToolOFCIter.h:66
LArRawChannelBuilderToolOFCIter::m_peakReco
ToolHandle< LArOFPeakRecoTool > m_peakReco
Definition: LArRawChannelBuilderToolOFCIter.h:71
LArRawChannelBuilderToolOFCIter::m_doIter
bool m_doIter
Definition: LArRawChannelBuilderToolOFCIter.h:55
checkRpcDigits.digit
digit
Definition: checkRpcDigits.py:186
LArRawChannelBuilderStatistics
Returns various counters from the LArRawChannel building.
Definition: LArRawChannelBuilderStatistics.h:21
HWIdentifier
Definition: HWIdentifier.h:13
LArRawChannelBuilderToolOFCIter::m_minADCforIter
unsigned int m_minADCforIter
Definition: LArRawChannelBuilderToolOFCIter.h:57
LArRawChannelBuilderStatistics::incrementErrorCount
void incrementErrorCount(unsigned int nerr)
Definition: LArRawChannelBuilderStatistics.cxx:34
ReadCondHandle.h
AthenaPoolTestRead.sc
sc
Definition: AthenaPoolTestRead.py:27
LArRawChannelBuilderParams::curr_shiftTimeSamples
int curr_shiftTimeSamples
Definition: LArRawChannelBuilderParams.h:31
LArRawChannelBuilderToolOFCIter::m_storeGate
ServiceHandle< StoreGateSvc > m_storeGate
Definition: LArRawChannelBuilderToolOFCIter.h:69
LArRawChannelBuilderToolOFCIter::m_signal
std::vector< float > m_signal
Definition: LArRawChannelBuilderToolOFCIter.h:63
LArDigit.h
xAOD::uint16_t
setWord1 uint16_t
Definition: eFexEMRoI_v1.cxx:88
StdJOSetup.msgSvc
msgSvc
Provide convenience handles for various services.
Definition: StdJOSetup.py:36
LArDigit
Liquid Argon digit base class.
Definition: LArDigit.h:25
lumiFormat.i
int i
Definition: lumiFormat.py:92
LArRawChannelBuilderToolBase::energy
int energy()
Definition: LArRawChannelBuilderToolBase.h:46
endmsg
#define endmsg
Definition: AnalysisConfig_Ntuple.cxx:63
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
01SubmitToGrid.samples
samples
Definition: 01SubmitToGrid.py:58
LArOFIterResultsContainer
Definition: LArOFIterResultsContainer.h:14
ILArNoise::noise
virtual const float & noise(const HWIdentifier &id, int gain) const =0
test_pyathena.parent
parent
Definition: test_pyathena.py:15
LArRawChannelBuilderToolBase
Base tool to make the interface with the driver.
Definition: LArRawChannelBuilderToolBase.h:33
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
LArRawChannelBuilderToolOFCIter::m_defaultPhase
float m_defaultPhase
Definition: LArRawChannelBuilderToolOFCIter.h:56
LArRawChannelBuilderToolBase::gain
CaloGain::CaloGain gain()
Definition: LArRawChannelBuilderToolBase.h:48
LArRawChannelBuilderToolBaseClass::m_helper
LArRawChannelBuilderStatistics * m_helper
Definition: LArRawChannelBuilderToolBaseClass.h:82
LArRawChannelBuilderDriver.h
LArRawChannelBuilderToolOFCIter::m_larOFIterCont
LArOFIterResultsContainer * m_larOFIterCont
Definition: LArRawChannelBuilderToolOFCIter.h:67
LArRawChannelBuilderToolBase::m_buildIt
void(LArRawChannelBuilderToolBase::* m_buildIt)(int energy, int time, uint16_t quality, uint16_t provenance, const LArDigit *digit)
Definition: LArRawChannelBuilderToolBase.h:57
LArRawChannelBuilderToolOFCIter::m_larNoiseKey
SG::ReadCondHandleKey< ILArNoise > m_larNoiseKey
Definition: LArRawChannelBuilderToolOFCIter.h:77
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
LArRawChannelBuilderToolOFCIter.h
LArRawChannelBuilderToolOFCIter::m_Ecut
float m_Ecut
Definition: LArRawChannelBuilderToolOFCIter.h:51
LArRawChannelBuilderToolOFCIter::m_doMC
bool m_doMC
Definition: LArRawChannelBuilderToolOFCIter.h:64
SG::CondHandleKey::initialize
StatusCode initialize(bool used=true)
python.ami.results
def results
Definition: ami.py:386
LArRawChannelBuilderToolOFCIter::m_doQual
bool m_doQual
Definition: LArRawChannelBuilderToolOFCIter.h:54
CaloGain::CaloGain
CaloGain
Definition: CaloGain.h:11
LArRawChannelBuilderToolOFCIter::m_skipSaturatedCells
bool m_skipSaturatedCells
Definition: LArRawChannelBuilderToolOFCIter.h:52
LArRawChannelBuilderToolOFCIter::initEvent
void initEvent()
Definition: LArRawChannelBuilderToolOFCIter.cxx:95
Identifier::get_compact
value_type get_compact(void) const
Get the compact id.
LArRawChannelBuilderToolOFCIter::buildRawChannel
bool buildRawChannel(const LArDigit *digit, float pedestal, const std::vector< float > &ramps, MsgStream *pLog)
Definition: LArRawChannelBuilderToolOFCIter.cxx:110
python.CaloScaleNoiseConfig.type
type
Definition: CaloScaleNoiseConfig.py:78
DEBUG
#define DEBUG
Definition: page_access.h:11
LArRawChannelBuilderToolOFCIter::m_AdcMax
unsigned short m_AdcMax
Definition: LArRawChannelBuilderToolOFCIter.h:53
python.CaloCondTools.log
log
Definition: CaloCondTools.py:20
LArRawChannelBuilderToolOFCIter::m_nIterProp
unsigned int m_nIterProp
Definition: LArRawChannelBuilderToolOFCIter.h:61
python.SystemOfUnits.ns
int ns
Definition: SystemOfUnits.py:130
LArElecCalib::ERRORCODE
@ ERRORCODE
Definition: LArCalibErrorCode.h:17
LArRawChannelBuilderToolBase::time
int time()
Definition: LArRawChannelBuilderToolBase.h:47
LArRawChannelBuilderStatistics::returnBitPattern
unsigned int returnBitPattern() const
Definition: LArRawChannelBuilderStatistics.cxx:85
Overlay::debugPrint
std::string debugPrint(const IDC_Container *container, unsigned numprint=25)
Diagnostic output of Identifiable Containers.
ILArPedestal::pedestalRMS
virtual float pedestalRMS(const HWIdentifier &id, int gain) const =0
access to RMS of Pedestal index by Identifier, and gain setting
LArRawChannelBuilderParams::curr_chid
HWIdentifier curr_chid
Definition: LArRawChannelBuilderParams.h:24
LArRawChannelBuilderParams::qualityBitPattern
unsigned int qualityBitPattern
Definition: LArRawChannelBuilderParams.h:34
LArRawChannelBuilderToolBaseClass::m_parent
LArRawChannelBuilderParams * m_parent
Definition: LArRawChannelBuilderToolBaseClass.h:80
readCCLHist.float
float
Definition: readCCLHist.py:83
python.SystemOfUnits.picosecond
int picosecond
Definition: SystemOfUnits.py:123
LArOFIterResults
Definition: LArOFIterResults.h:15