ATLAS Offline Software
Loading...
Searching...
No Matches
TileRawCorrelatedNoise.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// Tile includes
10
11// Atlas includes
12// access all RawChannels inside container
18
19#include <cstdio> //sprintf
20
21
22// #############################################################################
23TileRawCorrelatedNoise::TileRawCorrelatedNoise(const std::string& name, ISvcLocator* pSvcLocator)
24 : AthAlgorithm(name, pSvcLocator)
25{
26// #############################################################################
27
28 declareProperty("nRMSThreshold", m_nRMS_threshold = 2.);
29 declareProperty("AlphaMatrixFilePrefix", m_alphaMatrixFilePrefix = "AlphaMatrix");
30 declareProperty("MeanFilePrefix", m_meanFilePrefix = "Mean");
31 declareProperty("Sample3RMSFilePrefix", m_sample3RMSFilePrefix = "RMS");
32 declareProperty("UseMeanFiles", m_useMeanFiles = true);
33 declareProperty("PMTOrder", m_pmtOrder = false);
34}
35
36// #############################################################################
38// #############################################################################
39
40}
41
42// #############################################################################
44// #############################################################################
45
46 static constexpr int PmtToChannelBarrel[48] = {
47 1, 2, 3, 4, 5, 6, 7, 8,
48 9, 10, 11, 12, 13, 14, 15, 16,
49 17, 18, 19, 20, 21, 22, 23, 24,
50 27, 26, 25, 30, 29, 28, 33, 32,
51 31, 36, 35, 34, 39, 38, 37, 42,
52 41, 40, 45, 44, 43, 48, 47, 46 };
53
54 static constexpr int PmtToChannelExtendedBarrel[48] = {
55 1, 2, 3, 4, 5, 6, 7, 8,
56 9, 10, 11, 12, 13, 14, 15, 16,
57 17, 18, 19, 20, 21, 22, 23, 24,
58 27, 26, 25, 31, 32, 28, 33, 29,
59 30, 36, 35, 34, 44, 38, 37, 43,
60 42, 41, 45, 39, 40, 48, 47, 46 };
61
62 m_alphaMatrix = std::make_unique<AlphaMatrix>();
63
64 // read alpha matrix
65 FILE* AlphaMatrixFile[4][64]{};
66 char Rosstr[10];
67 char buff[1000];
68 // Cycle over 4 partitions and 64 modules
69 for (int Ros = 1; Ros < 5; ++Ros) {
70 for (int Drawer = 0; Drawer < 64; ++Drawer) {
71 // open file which corresponds to AlphaMatrix[Ros][Drawer]
72 if (Ros == 1) sprintf(Rosstr, "LBA");
73 else if (Ros == 2) sprintf(Rosstr, "LBC");
74 else if (Ros == 3) sprintf(Rosstr, "EBA");
75 else sprintf(Rosstr, "EBC");
76
77 sprintf(buff, "%s%s%02d.txt", m_alphaMatrixFilePrefix.c_str(), Rosstr, Drawer + 1);
78 std::string filestr(buff);
79 std::string file = PathResolver::find_file(buff, "DATAPATH");
80// ATH_MSG_INFO( " buff=" << buff
81// << " filestr=" << filestr.c_str()
82// << " file=" << file.c_str() );
83 if (file.size() == 0) {
84 ATH_MSG_WARNING( "Could not find input file " << buff );
85 } else {
86 ATH_MSG_INFO( "Reading file " << file );
87 AlphaMatrixFile[Ros - 1][Drawer] = fopen(file.c_str(), "r");
88 }
89 if (AlphaMatrixFile[Ros - 1][Drawer] == NULL) {
90 ATH_MSG_ERROR( "Can't read input Alpha Matrix files." );
91 return StatusCode::FAILURE;
92 }
93 ATH_MSG_DEBUG( " **** Start of Alpha Matrix Read Out" );
94 FILE* alpha_file = AlphaMatrixFile[Ros - 1][Drawer];
95// if(fgets(buff, sizeof(buff), alpha_file) != NULL) {
96// ATH_MSG_DEBUG( "Matrix is being loaded: " << buff );
97// }
98 // load tokens to be searched for in a string
99 const char* TOKENS = { " \t\n" };
100 // read Matrix
101 int dima = 48;
102 char* saveptr = nullptr;
103 for (int line = 0; line < dima; line++) {
104 if (fgets(buff, sizeof(buff), alpha_file) != NULL) {
105 ATH_MSG_DEBUG( "line " << line << " is " << buff );
106 for (int column = 0; column < dima; column++) {
107 // Check for comment lines
108 if (*buff == '!' || *buff == '*') continue;
109 // read value
110 double pippo = 0;
111 if (const char* word = strtok_r(column==0 ? buff : nullptr, TOKENS, &saveptr))
112 {
113 pippo = atof(word);
114 }
115
116 ATH_MSG_VERBOSE ( "elem " << column << " is " << pippo );
117 int chline = line;
118 int chcolumn = column;
119 // read alpha matrix in pmt order but save it in channel order if m_pmtOrder is true
120 if (m_pmtOrder) {
121 if (Ros < 3) {
122 chline = PmtToChannelBarrel[line] - 1;
123 chcolumn = PmtToChannelBarrel[column] - 1;
124 } else {
125 chline = PmtToChannelExtendedBarrel[line] - 1;
126 chcolumn = PmtToChannelExtendedBarrel[column] - 1;
127 }
128 }
129 m_alphaMatrix->m[Ros - 1][Drawer][chline][chcolumn] = pippo;
130 }
131 }
132 }
133 fclose(alpha_file);
134 }
135 }
136
137 if (m_useMeanFiles) {
138 // read mean
139 int nSamples = 7;
140 FILE* MeanFile[4][64];
141 // cicle over 4 partitions and 64 modules
142 for (int Ros = 1; Ros < 5; ++Ros) {
143 for (int Drawer = 0; Drawer < 64; ++Drawer) {
144 // open file which corresponds to Mean[Ros][Drawer]
145 if (Ros == 1)
146 sprintf(Rosstr, "LBA");
147 else if (Ros == 2)
148 sprintf(Rosstr, "LBC");
149 else if (Ros == 3)
150 sprintf(Rosstr, "EBA");
151 else
152 sprintf(Rosstr, "EBC");
153 sprintf(buff, "%s%s%02d.txt", m_meanFilePrefix.c_str(), Rosstr, Drawer + 1);
154 std::string filestr(buff);
155 std::string file = PathResolver::find_file(buff, "DATAPATH");
156 if (file.size() == 0) {
157 ATH_MSG_VERBOSE ( "Could not find input file " << buff );
158 } else {
159 ATH_MSG_INFO( "Reading file " << file );
160 MeanFile[Ros - 1][Drawer] = fopen(file.c_str(), "r");
161 }
162 if (MeanFile[Ros - 1][Drawer] == NULL) {
163 ATH_MSG_ERROR( "Can't read input Mean files." );
164 return StatusCode::FAILURE;
165 }
166
167 ATH_MSG_DEBUG( " **** Start of Means Read Out" );
168 FILE* mean_file = MeanFile[Ros - 1][Drawer];
169 //if(fgets(buff, sizeof(buff), mean_file) != NULL) {
170 // if (lDebug)
171 // log << MSG::DEBUG << "Vector is being loaded: "<< buff << endmsg;
172 //}
173 // load tokens to be searched for in a string
174 const char* TOKENS = { " \t\n" };
175 // read Vector
176 int dima = 48;
177 char* saveptr = nullptr;
178 for (int line = 0; line < dima; line++) {
179 if (fgets(buff, sizeof(buff), mean_file) != NULL) {
180 ATH_MSG_DEBUG( "line " << line << " is " << buff );
181 for (int Sample = 0; Sample < nSamples; Sample++) {
182 // Check for comment lines
183 if (*buff == '!' || *buff == '*') continue;
184 // read value
185 double pippo = 0;
186 if (const char* word = strtok_r(Sample==0 ? buff : nullptr, TOKENS, &saveptr))
187 {
188 pippo = atof(word);
189 }
190
191 ATH_MSG_VERBOSE ( "elem " << Sample << " is " << pippo );
192 int chline = line;
193 // read lines of mean matrix in pmt order but save it in channel order if m_pmtOrder is true
194 if (m_pmtOrder) {
195 if (Ros < 3) chline = PmtToChannelBarrel[line] - 1;
196 else chline = PmtToChannelExtendedBarrel[line] - 1;
197 }
198 m_meanSamples[Ros - 1][Drawer][chline][Sample] = pippo;
199 }
200 }
201 }
202 fclose(mean_file);
203 }
204 }
205 } else {
206 // Initialize mean
207 int nSamples = 7;
208 for (int Ros = 1; Ros < 5; ++Ros) {
209 for (int Drawer = 0; Drawer < 64; ++Drawer) {
210 for (int Channel = 0; Channel < 48; ++Channel) {
211 for (int Sample = 0; Sample < nSamples; ++Sample) {
212 //MeanSamples[Ros-1][Drawer][Channel][Sample]=50.;
213 m_meanSamples[Ros - 1][Drawer][Channel][Sample] = -1.;
214 }
215 }
216 }
217 }
218 }
219
220 // read sample 3 RMS
221 FILE* Sample3RMSFile[4][64];
222 // cicle over 4 partitions and 64 modules
223 for (int Ros = 1; Ros < 5; ++Ros) {
224 for (int Drawer = 0; Drawer < 64; ++Drawer) {
225 // open file which corresponds to Sample3RMS[Ros][Drawer]
226 if (Ros == 1) sprintf(Rosstr, "LBA");
227 else if (Ros == 2) sprintf(Rosstr, "LBC");
228 else if (Ros == 3) sprintf(Rosstr, "EBA");
229 else sprintf(Rosstr, "EBC");
230
231 sprintf(buff, "%s%s%02d.txt", m_sample3RMSFilePrefix.c_str(), Rosstr, Drawer + 1);
232 std::string filestr(buff);
233 std::string file = PathResolver::find_file(buff, "DATAPATH");
234 if (file.size() == 0) {
235 ATH_MSG_VERBOSE ( "Could not find input file " << buff );
236 } else {
237 ATH_MSG_INFO( "Reading file " << file );
238 Sample3RMSFile[Ros - 1][Drawer] = fopen(file.c_str(), "r");
239 }
240 if (Sample3RMSFile[Ros - 1][Drawer] == NULL) {
241 ATH_MSG_ERROR( "Can't read input sample 3 RMS files." );
242 return StatusCode::FAILURE;
243 }
244
245 ATH_MSG_DEBUG( " **** Start of sample 3 RMS Read Out" );
246 FILE* rms_file = Sample3RMSFile[Ros - 1][Drawer];
247 //if(fgets(buff, sizeof(buff), rms_file) != NULL) {
248 // if (lDebug)
249 // log << MSG::DEBUG << "Vector is being loaded: "<< buff << endmsg;
250 //}
251 // load tokens to be searched for in a string
252 char* word;
253 const char* TOKENS = { " \t\n" };
254 // read Vector
255 int dima = 48;
256 for (int line = 0; line < dima; line++) {
257 if (fgets(buff, sizeof(buff), rms_file) != NULL) {
258 ATH_MSG_DEBUG( "line " << line << " is " << buff );
259 // Check for comment lines
260 if (*buff == '!' || *buff == '*') continue;
261 // read value
262 double pippo;
263 char* saveptr = nullptr;
264 if ((word = strtok_r(buff, TOKENS, &saveptr)) == NULL) pippo = 0;
265 else pippo = atof(word);
266 // read value
267 ATH_MSG_VERBOSE ( "elem is " << pippo );
268 int chline = line;
269 // read rms vector in pmt order but save it in channel order if m_pmtOrder is true
270 if (m_pmtOrder) {
271 if (Ros < 3) chline = PmtToChannelBarrel[line] - 1;
272 else chline = PmtToChannelExtendedBarrel[line] - 1;
273 }
274 m_sample3RMS[Ros - 1][Drawer][chline] = pippo;
275 }
276 }
277 fclose(rms_file);
278 }
279 }
280
281 ATH_CHECK( m_inputDigitsContainerKey.initialize() );
283
284 if (!m_useMeanFiles) {
285 ATH_CHECK( m_tileToolNoiseSample.retrieve() );
286 }
287
288 ATH_MSG_INFO( "Initialization completed successfully" );
289
290 return StatusCode::SUCCESS;
291}
292
293// #############################################################################
295// #############################################################################
296
297 const EventContext &ctx = Gaudi::Hive::currentContext();
298
299 // get named TileDigitsContaner from TES
301
302 ATH_MSG_DEBUG( "Got TileDigitsContainer '" << m_inputDigitsContainerKey.key() << "'" );
303
304 const TileHWID* tileHWID;
305 CHECK( detStore()->retrieve(tileHWID, "TileHWID") );
306 //coverity[STACK_USE]
307 const TileDigits* OriginalDigits[4][64][48];
308
309 // go through ALL TileDigits in container
310 SelectAllObject<TileDigitsContainer> selAll(inputDigitsContainer.cptr());
311 SelectAllObject<TileDigitsContainer>::const_iterator digItr = selAll.begin();
312 SelectAllObject<TileDigitsContainer>::const_iterator lastDig = selAll.end();
313
314 // read digits
315 for (; digItr != lastDig; ++digItr) {
316 const HWIdentifier adc_HWID = (*digItr)->adc_HWID();
317 int Ros = tileHWID->ros(adc_HWID);
318 int Drawer = tileHWID->drawer(adc_HWID);
319 int Channel = tileHWID->channel(adc_HWID);
320 OriginalDigits[Ros - 1][Drawer][Channel] = (*digItr);
321
322 if (!m_useMeanFiles) {
323 // read pedestal value and use it as mean
324 unsigned int drawerIdx = TileCalibUtils::getDrawerIdx(Ros, Drawer);
325 int adc = tileHWID->adc(adc_HWID);
326 double ped = m_tileToolNoiseSample->getPed(drawerIdx, Channel, adc, TileRawChannelUnit::ADCcounts, ctx);
327 int nSamples = 7;
328 for (int Sample = 0; Sample < nSamples; ++Sample) {
329 m_meanSamples[Ros - 1][Drawer][Channel][Sample] = ped;
330 }
331 }
332 }
333
334 // prepare new samples
335 const int nSamples = 7;
336 struct Arrays {
337 float NewSamples[4][64][48][nSamples];
338 };
339 auto a = std::make_unique<Arrays>();
340 for (int Ros = 1; Ros < 5; ++Ros) {
341 for (int Drawer = 0; Drawer < 64; ++Drawer) {
342 for (int Channel = 0; Channel < 48; ++Channel) {
343 for (int Sample = 0; Sample < nSamples; ++Sample) {
344 a->NewSamples[Ros - 1][Drawer][Channel][Sample] =
345 ((OriginalDigits[Ros - 1][Drawer][Channel])->samples())[Sample];
346 }
347 }
348 }
349 }
350
351 // apply method
352 for (int Ros = 1; Ros < 5; ++Ros) {
353 for (int Drawer = 0; Drawer < 64; ++Drawer) {
354 for (int Channel = 0; Channel < 48; ++Channel) {
355 if (OriginalDigits[Ros - 1][Drawer][Channel]) {
356 int nSamples = (OriginalDigits[Ros - 1][Drawer][Channel])->nsamples();
357 std::vector<float> digits(nSamples);
358 for (int jCh = 0; jCh < 48; ++jCh) {
359 if (OriginalDigits[Ros - 1][Drawer][jCh]) {
360 if (Channel != jCh
361 && fabs(((OriginalDigits[Ros - 1][Drawer][jCh])->samples())[3]
362 - m_meanSamples[Ros - 1][Drawer][jCh][3])
363 < m_nRMS_threshold * m_sample3RMS[Ros - 1][Drawer][jCh]) {
364
365 for (int Sample = 0; Sample < nSamples; ++Sample)
366 a->NewSamples[Ros - 1][Drawer][Channel][Sample] =
367 a->NewSamples[Ros - 1][Drawer][Channel][Sample]
368 - m_alphaMatrix->m[Ros - 1][Drawer][Channel][jCh]
369 * (((OriginalDigits[Ros - 1][Drawer][jCh])->samples())[Sample]
370 - m_meanSamples[Ros - 1][Drawer][jCh][Sample]);
371 }
372 }
373 }
374 }
375 }
376 }
377 }
378
379 // create new container
380
381 auto outputDigitsContainer = std::make_unique<TileMutableDigitsContainer>();
382 ATH_CHECK( outputDigitsContainer->status() );
383
384 // fill new container
385 for (int Ros = 1; Ros < 5; ++Ros) {
386 for (int Drawer = 0; Drawer < 64; ++Drawer) {
387 for (int Channel = 0; Channel < 48; ++Channel) {
388 if (OriginalDigits[Ros - 1][Drawer][Channel]) {
389 int nSamples = (OriginalDigits[Ros - 1][Drawer][Channel])->nsamples();
390 std::vector<float> digits(nSamples);
391 for (int Sample = 0; Sample < nSamples; ++Sample) {
392 digits[Sample] = a->NewSamples[Ros - 1][Drawer][Channel][Sample];
393 }
394 TileDigits* NewDigits = new TileDigits(
395 (OriginalDigits[Ros - 1][Drawer][Channel])->adc_HWID(), digits);
396 ATH_CHECK( outputDigitsContainer->push_back(NewDigits) );
397 }
398 }
399 }
400 }
401
403 ATH_CHECK( outputDigitsCnt.record(std::move(outputDigitsContainer)) );
404
405
406 ATH_MSG_DEBUG( "execute completed successfully" );
407
408 return StatusCode::SUCCESS;
409}
410
411// #############################################################################
413// #############################################################################
414
415 ATH_MSG_INFO( " finalize completed successfully" );
416
417 return StatusCode::SUCCESS;
418}
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_VERBOSE(x)
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
Helpers for checking error return status codes and reporting errors.
#define CHECK(...)
Evaluate an expression and check for errors.
static Double_t a
SelectAllObjectMT< DCC, OBJECT > SelectAllObject
Handle class for reading from StoreGate.
Handle class for recording to StoreGate.
Helper for holding non-const raw data prior to recording in SG.
AthAlgorithm(const std::string &name, ISvcLocator *pSvcLocator)
Constructor with parameters:
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T, V, H > &t)
const ServiceHandle< StoreGateSvc > & detStore() const
static std::string find_file(const std::string &logical_file_name, const std::string &search_path)
const_pointer_type cptr()
Dereference the pointer.
StatusCode record(std::unique_ptr< T > data)
Record a const object to the store.
const_iterator end()
const_iterator begin()
static unsigned int getDrawerIdx(unsigned int ros, unsigned int drawer)
Returns a drawer hash.
Helper class for TileCal online (hardware) identifiers.
Definition TileHWID.h:49
int channel(const HWIdentifier &id) const
extract channel field from HW identifier
Definition TileHWID.h:189
int adc(const HWIdentifier &id) const
extract adc field from HW identifier
Definition TileHWID.h:193
int drawer(const HWIdentifier &id) const
extract drawer field from HW identifier
Definition TileHWID.h:171
int ros(const HWIdentifier &id) const
extract ros field from HW identifier
Definition TileHWID.h:167
SG::WriteHandleKey< TileDigitsContainer > m_outputDigitsContainerKey
float m_meanSamples[4][64][48][7]
virtual StatusCode initialize()
TileRawCorrelatedNoise(const std::string &name, ISvcLocator *pSvcLocator)
ToolHandle< TileCondToolNoiseSample > m_tileToolNoiseSample
SG::ReadHandleKey< TileDigitsContainer > m_inputDigitsContainerKey
std::unique_ptr< AlphaMatrix > m_alphaMatrix
TFile * file