ATLAS Offline Software
Loading...
Searching...
No Matches
TileHitToRawChannel.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
5//*****************************************************************************
6// Filename : TileHitToRawChannel.cxx
7// Author : Zhifang
8// Created : April, 2002
9//
10// DESCRIPTION:
11// Implement the TileHitToRawChannel class
12//
13// HISTORY:
14//
15// BUGS:
16//
17//*****************************************************************************
18
19// Tile includes
27
28// Calo includes
31
32// Atlas includes
37// For the Athena-based random numbers.
40
41//CLHEP includes
42#include <CLHEP/Random/Randomize.h>
43
44
45using CLHEP::RandGaussQ;
46
47
48//
49// Constructor
50//
51TileHitToRawChannel::TileHitToRawChannel(const std::string& name, ISvcLocator* pSvcLocator)
52 : AthAlgorithm(name, pSvcLocator)
53{
54}
55
57 std::vector<HWIdentifier *>::iterator itr = m_all_ids.begin();
58 std::vector<HWIdentifier *>::iterator last = m_all_ids.end();
59
60 for (; itr != last; ++itr) {
61 delete[] (*itr);
62 }
63}
64
65//
66// Alg standard initialize function
67//
72
73 // retrieve TileID helper and TileInfo from det store
74
75 ATH_CHECK( detStore()->retrieve(m_tileID) );
76
77 ATH_CHECK( detStore()->retrieve(m_tileTBID) );
78
79 ATH_CHECK( detStore()->retrieve(m_tileHWID) );
80
81 ATH_CHECK( detStore()->retrieve(m_tileInfo, m_infoName.value()) );
82
83 ATH_CHECK( m_samplingFractionKey.initialize() );
84 ATH_CHECK( m_emScaleKey.initialize() );
85
86 ATH_CHECK( m_tileToolNoiseSample.retrieve() );
87
88 if (m_tileNoise) {
89 ATH_CHECK(m_atRndmGenSvc.retrieve());
90 }
91
92 ATH_CHECK( m_cablingSvc.retrieve() );
93 m_cabling = m_cablingSvc->cablingService();
94
95 // Get needed parameters from TileInfo
96 m_ampMaxHi = m_tileInfo->ADCmax();
97
98 /* Get TileNoise flag from TileInfo
99 (true => generate noise in RC's). */
100 m_tileNoise = m_tileInfo->TileNoise();
101
102 /* Get TileZeroSuppress flag from TileInfo
103 (true => apply threshold cut for RC's). */
104 m_tileThresh = m_tileInfo->TileZeroSuppress();
105
106 m_threshHi = m_tileInfo->ThresholdRawChannel(TileID::HIGHGAIN);
107
108 if (m_tileThresh) {
109 ATH_MSG_INFO( "ampMaxHi=" << m_ampMaxHi
110 << ", tileNoise=" << ((m_tileNoise) ? "true" : "false")
111 << ", tileThresh=" << "true, thresh_hi=" << m_threshHi );
112 } else {
113 ATH_MSG_INFO( "ampMaxHi=" << m_ampMaxHi
114 << ", tileNoise=" << ((m_tileNoise) ? "true" : "false")
115 << ", tileThresh=" << "false" );
116 }
117
118
119 IdContext drawer_context = m_tileHWID->drawer_context();
120 int ndrawers = m_tileHWID->drawer_hash_max();
121 int nchannels = 48;
122
123 m_all_ids.reserve(ndrawers);
124
125 for (int dr = 0; dr < ndrawers; ++dr) {
126
127 HWIdentifier drawer_id;
128 /* int result = */m_tileHWID->get_id(dr, drawer_id, &drawer_context);
129
130 HWIdentifier * adc_ids = new HWIdentifier[nchannels];
131 if (m_tileHWID->ros(drawer_id) > 0) { // protection against BEAM ROD
132 for (int ch = 0; ch < nchannels; ++ch) {
133 adc_ids[ch] = m_tileHWID->adc_id(drawer_id, ch, TileID::HIGHGAIN);
134 }
135 }
136 m_all_ids.push_back(adc_ids);
137 }
138
139 ATH_CHECK( m_hitContainerKey.initialize() );
140 ATH_CHECK( m_rawChannelContainerKey.initialize() );
141
142 ATH_MSG_INFO( "TileHitToRawChannel initialization completed" );
143
144 return StatusCode::SUCCESS;
145}
146
147//
148// Alg standard execute function
149//
150StatusCode TileHitToRawChannel::execute(const EventContext& ctx) {
151
152 ATH_MSG_DEBUG( "Executing TileHitToRawChannel" );
153
154
155 ATHRNG::RNGWrapper* rngWrapper = m_atRndmGenSvc->getEngine(this, m_randomStreamName);
156 rngWrapper->setSeed( m_randomStreamName, ctx );
157 CLHEP::HepRandomEngine* rndmEngine = rngWrapper->getEngine(ctx);
158
160 ATH_CHECK( samplingFraction.isValid() );
161
163 ATH_CHECK( emScale.isValid() );
164
165 // step1: read hits from TES
167 ATH_CHECK( hitContainer.isValid() );
168
169 //Zero sums for monitoring.
170 int nChan = 0;
171 int nChLG = 0;
172 int nChHG = 0;
173 double eHitTot = 0.;
174 double eCh = 0.;
175 double eChLG = 0.;
176 double eChHG = 0.;
177
178 // prepare arrays for all channels in one drawer (collection)
179 const int nChMax = 48;
180 double random[nChMax];
181 memset(random, 0, sizeof(random));
182 HWIdentifier * adc_ids;
183 int adc_gain[nChMax];
184 double adc_ampl[nChMax];
185 double adc_time[nChMax];
186 double adc_qual[nChMax];
187
188 IdContext drawer_context = m_tileHWID->drawer_context();
189
190 // step2: form raw channels, and put them in container
191 auto rawChannelContainer = std::make_unique<TileMutableRawChannelContainer>(true, m_rChType, m_rChUnit);
192 ATH_CHECK( rawChannelContainer->status() );
193
194 // iterate over all collections in a container
195 for (const TileHitCollection* hitCollection : *hitContainer) {
196
197 HWIdentifier drawer_id = m_tileHWID->drawer_id(hitCollection->identify());
198 int ros = m_tileHWID->ros(drawer_id);
199 int drawer = m_tileHWID->drawer(drawer_id);
200 int drawerIdx = TileCalibUtils::getDrawerIdx(ros, drawer);
201 if (m_cabling->connected(ros, drawer)) {
202 ATH_MSG_VERBOSE( "ROS " << ros
203 << " drawer " << drawer
204 << " is connected" );
205 } else {
206 continue;
207 }
208 int ch;
209
210 ATH_MSG_VERBOSE( MSG::hex
211 << "Collection = 0x" << hitCollection->identify() << MSG::dec
212 << " : ROS=" << ros
213 << ", drawer=" << drawer );
214
215 IdentifierHash idhash;
216 /* int result = */m_tileHWID->get_hash(drawer_id, idhash, &drawer_context);
217 adc_ids = m_all_ids[idhash];
218
219 // If tileNoise is requested, generate random numbers to give noise
220 if (m_tileNoise) {
221
222 RandGaussQ::shootArray(rndmEngine, nChMax, random, 0.0, 1.0);
223
224 for (ch = 0; ch < nChMax; ++ch) {
225 adc_gain[ch] = TileID::HIGHGAIN;
226 adc_ampl[ch] = random[ch] * m_tileToolNoiseSample->getHfn(drawerIdx, ch, TileID::HIGHGAIN, TileRawChannelUnit::ADCcounts, ctx)
227 * m_tileInfo->getNoiseScaleFactor();
228
229 }
230 } else {
231 memset(adc_gain, -1, sizeof(adc_gain)); /* TileID::INVALID */
232 memset(adc_ampl, 0, sizeof(adc_ampl));
233 }
234
235 memset(adc_time, 0, sizeof(adc_time));
236 memset(adc_qual, 0, sizeof(adc_qual));
237
238 // iterate over all hits in a collection
239 for (const TileHit* tile_hit : *hitCollection) {
240
241 // Get hit Identifier (= identifier of pmt)
242 Identifier pmt_id = tile_hit->pmt_ID();
243 HWIdentifier channel_id = tile_hit->pmt_HWID();
244
245 // index for array is the channel number
246 ch = m_tileHWID->channel(channel_id);
247
248 /* Get hit amplitude and convert to energy (cell-dependent) */
249 double e_hit = tile_hit->energy();
250 double time = tile_hit->time();
251 if (m_deltaT > 0.0) {
252 if (fabs(time) >= m_deltaT) { // reset first hit if it's outside deltaT window
253 e_hit = 0.0;
254 time = 0.0;
255 }
256 double etime = e_hit * time;
257 for (int i = tile_hit->size() - 1; i > 0; --i) { // don't use first hit (i=0)
258 double en = tile_hit->energy(i);
259 double ti = tile_hit->time(i);
260 if (fabs(ti) < m_deltaT && en > 0.0) {
261 e_hit += en;
262 etime += en * ti;
263 }
264 }
265 if (e_hit > 0.0)
266 time = etime / e_hit;
267 else
268 continue; // go to next hit, do not create channels with zero energy
269 }
270 adc_time[ch] = time;
271 double hit_calib = samplingFraction->getSamplingFraction(drawerIdx, ch);
272 double e_ch = e_hit * hit_calib;
273
274 /* Convert to amplitude of channel (assuming high gain) */
275 /* need to divide here, because "calib" converts amp to energy */
276 int gain = TileID::HIGHGAIN;
277 double amp_ch = e_ch / emScale->calibrateChannel(drawerIdx, ch, gain, 1., m_rChUnit
279 double noise;
280 // If high saturates, convert adc_id to low-gain value and recalculate.
281 if (adc_ampl[ch] + amp_ch + m_tileToolNoiseSample->getPed(drawerIdx, ch, gain, TileRawChannelUnit::ADCcounts, ctx) > m_ampMaxHi) {
282
283 gain = TileID::LOWGAIN;
284 amp_ch = e_ch / emScale->calibrateChannel(drawerIdx, ch, gain, 1., m_rChUnit
286
287 // If Noise is requested,
288 // recalculate noise using the SAME random number as for high.
289 if (m_tileNoise) {
290 adc_ampl[ch] = random[ch] * m_tileToolNoiseSample->getHfn(drawerIdx, ch, TileID::LOWGAIN, TileRawChannelUnit::ADCcounts, ctx)
291 * m_tileInfo->getNoiseScaleFactor();
292 }
293 }
294
295 adc_gain[ch] = gain;
296 noise = adc_ampl[ch];
297 adc_ampl[ch] += amp_ch; // add real amplitude to the noise
298
299 ATH_MSG_VERBOSE( "ch=" << ch << ((gain == TileID::HIGHGAIN) ? " Hi" :" Lo")
300 << " pmt=" << m_tileID->to_string(pmt_id, -1)
301 << " adc=" << m_tileHWID->to_string(channel_id, -1) << "/" << gain
302 << " noise=" << noise
303 << " amp=" << amp_ch
304 << " tot=" << adc_ampl[ch]);
305
306 ++nChan;
307 eHitTot += e_hit;
308 eCh += e_ch;
309 }
310
311 // store raw channels for one drawer
312
313 for (int ch = 0; ch < nChMax; ++ch) {
314
315 int gain = adc_gain[ch];
316
317 // without noise most of channels do not exist
318 // select only valid channels
319 if (gain != -1) {
320
321 HWIdentifier adc_id = adc_ids[ch];
322 double amp = adc_ampl[ch];
323
324 bool lrcGood = true;
325 double thresh = -99999;
326
327 if (TileID::HIGHGAIN != gain) {
328 // change ADC ID (channel switched to low gain)
329 adc_id = m_tileHWID->adc_id(m_tileHWID->channel_id(adc_id), gain);
330
331 } else {
332 // Apply threshold cut to small signals (always high gain)
333 if (m_tileThresh) {
334 thresh = m_threshHi;
335 if (thresh < 0) {
336 if (fabs(amp) < fabs(thresh))
337 lrcGood = false;
338 } else {
339 if (amp < thresh)
340 lrcGood = false;
341 }
342 }
343 }
344
345 if (lrcGood) {
346
347 auto rawChannel = std::make_unique<TileRawChannel>(adc_id, amp, adc_time[ch], adc_qual[ch]);
348 ATH_CHECK( rawChannelContainer->push_back(std::move(rawChannel)) );
349
350 if (TileID::HIGHGAIN == gain) {
351 ++nChHG;
352 eChHG += amp;
353 } else {
354 ++nChLG;
355 eChLG += amp;
356 }
357
358 ATH_MSG_VERBOSE( "Accept RC thr=" << ((thresh < 0) ? "+" : "" ) << thresh
359 << ", id=" << m_tileHWID->to_string(adc_id)
360 << ", amp=" << amp );
361
362 } else {
363
364 ATH_MSG_VERBOSE( "Reject RC thr=" << ((thresh < 0) ? "+" : "") << thresh
365 << ", id=" << m_tileHWID->to_string(adc_id)
366 << ", amp=" << amp );
367 }
368 }
369 }
370 }
371
372
373 // Execution completed.
374 if (msgLvl(MSG::DEBUG)) {
375 msg(MSG::DEBUG) << "TileHitToRawChannel execution completed." << endmsg;
376 msg(MSG::DEBUG) << " nChan=" << nChan
377 << " eHitTot=" << eHitTot
378 << " eneTot=" << eCh
379 << " nchLG=" << nChLG
380 << " eChLG=" << eChLG
381 << " nchHG=" << nChHG
382 << " eChHG=" << eChHG << endmsg;
383 }
384
385
387 ATH_CHECK( rawChannelCnt.record(std::move(rawChannelContainer)) );
388
389 return StatusCode::SUCCESS;
390}
391
393
394 ATH_MSG_INFO( "TileHitToRawChannel::finalize() end." );
395
396 return StatusCode::SUCCESS;
397}
398
#define endmsg
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_DEBUG(x,...)
#define ATH_MSG_VERBOSE(x,...)
#define ATH_MSG_INFO(x,...)
Helpers for checking error return status codes and reporting errors.
Handle class for reading from StoreGate.
Handle class for recording to StoreGate.
Helper for holding non-const raw data prior to recording in SG.
A wrapper class for event-slot-local random engines.
Definition RNGWrapper.h:56
void setSeed(const std::string &algName, const EventContext &ctx)
Set the random seed using a string (e.g.
Definition RNGWrapper.h:154
CLHEP::HepRandomEngine * getEngine(const EventContext &ctx) const
Retrieve the random engine corresponding to the provided EventContext.
Definition RNGWrapper.h:108
AthAlgorithm(const std::string &name, ISvcLocator *pSvcLocator)
Constructor.
const ServiceHandle< StoreGateSvc > & detStore() const
bool msgLvl(const MSG::Level lvl) const
This class saves the "context" of an expanded identifier (ExpandedIdentifier) for compact or hash ver...
Definition IdContext.h:26
This is a "hash" representation of an Identifier.
virtual bool isValid() override final
Can the handle be successfully dereferenced?
StatusCode record(std::unique_ptr< T > data)
Record a const object to the store.
static unsigned int getDrawerIdx(unsigned int ros, unsigned int drawer)
Returns a drawer hash.
bool m_tileThresh
If true => apply threshold on the conversion to raw channels.
ServiceHandle< IAthRNGSvc > m_atRndmGenSvc
Random number generator engine to use.
double m_ampMaxHi
Value of the maximum amplitude to be stored as a high gain channel.
double m_threshHi
Value of the mimimal amplitude required to do the conversion to raw channel in high gain (not used fo...
SG::ReadCondHandleKey< TileSamplingFraction > m_samplingFractionKey
Name of TileSamplingFraction in condition store.
SG::WriteHandleKey< TileRawChannelContainer > m_rawChannelContainerKey
TileFragHash::TYPE m_rChType
Type of TileRawChannels (Digitizar, OF1, OF2, Fit, etc.)(see TileFragHash.h).
std::vector< HWIdentifier * > m_all_ids
Vector to store all the drawer ids.
virtual StatusCode execute(const EventContext &ctx) override
execute method
SG::ReadHandleKey< TileHitContainer > m_hitContainerKey
Gaudi::Property< std::string > m_infoName
name of the TileInfo object
SG::ReadCondHandleKey< TileEMScale > m_emScaleKey
Name of TileEMScale in condition store.
const TileCablingService * m_cabling
Pointer to the TileCablingService instance.
virtual ~TileHitToRawChannel()
Destructor.
ServiceHandle< TileCablingSvc > m_cablingSvc
Name of Tile cabling service.
virtual StatusCode finalize() override
finalize method
TileHitToRawChannel(const std::string &name, ISvcLocator *pSvcLocator)
Constructor.
const TileInfo * m_tileInfo
Pointer to TileInfo.
Gaudi::Property< std::string > m_randomStreamName
Random Stream Name.
Gaudi::Property< bool > m_calibrateEnergy
if true, amplitude is converted to pCb
const TileID * m_tileID
Pointer to TileID helper.
virtual StatusCode initialize() override
initialize method
const TileTBID * m_tileTBID
Pointer to TileID helper.
ToolHandle< TileCondToolNoiseSample > m_tileToolNoiseSample
bool m_tileNoise
If true => generate noise for the TileRawChannel creation.
const TileHWID * m_tileHWID
Pointer to TileHWID helper.
TileRawChannelUnit::UNIT m_rChUnit
Units used for the TileRawChannels (ADC, pCb, etc.)(see TileInfo.h).
Gaudi::Property< double > m_deltaT
if true, keep only hits in deltaT range