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