]> git.uio.no Git - u/mrichter/AliRoot.git/blame_incremental - FMD/AliFMDRawReader.cxx
Fixed Coverity issues
[u/mrichter/AliRoot.git] / FMD / AliFMDRawReader.cxx
... / ...
CommitLineData
1/**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3 * *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
6 * *
7 * Permission to use, copy, modify and distribute this software and its *
8 * documentation strictly for non-commercial purposes is hereby granted *
9 * without fee, provided that the above copyright notice appears in all *
10 * copies and that both the copyright notice and this permission notice *
11 * appear in the supporting documentation. The authors make no claims *
12 * about the suitability of this software for any purpose. It is *
13 * provided "as is" without express or implied warranty. *
14 **************************************************************************/
15/* $Id$ */
16/** @file AliFMDRawReader.cxx
17 @author Christian Holm Christensen <cholm@nbi.dk>
18 @date Mon Mar 27 12:45:23 2006
19 @brief Class to read raw data
20 @ingroup FMD_rec
21*/
22//____________________________________________________________________
23//
24// Class to read ADC values from a AliRawReader object.
25//
26// This class uses the AliFMDRawStreamer class to read the ALTRO
27// formatted data.
28//
29// +-------+
30// | TTask |
31// +-------+
32// ^
33// |
34// +-----------------+ <<references>> +--------------+
35// | AliFMDRawReader |<>----------------| AliRawReader |
36// +-----------------+ +--------------+
37// | ^
38// | <<uses>> |
39// V |
40// +-----------------+ <<uses>> |
41// | AliFMDRawStream |------------------------+
42// +-----------------+
43// |
44// V
45// +----------------+
46// | AliAltroStream |
47// +----------------+
48//
49// #include <AliLog.h> // ALILOG_H
50#include "AliFMDDebug.h" // Better debug macros
51#include "AliFMDParameters.h" // ALIFMDPARAMETERS_H
52#include "AliFMDDigit.h" // ALIFMDDIGIT_H
53#include "AliFMDSDigit.h" // ALIFMDSDIGIT_H
54// #include "AliFMDRawStream.h" // ALIFMDRAWSTREAM_H
55#include "AliRawReader.h" // ALIRAWREADER_H
56#include "AliFMDRawReader.h" // ALIFMDRAWREADER_H
57#include "AliFMDDebug.h"
58#include "AliFMDCalibSampleRate.h"
59#include "AliFMDCalibStripRange.h"
60#include "AliFMDAltroMapping.h"
61#include "AliFMDUShortMap.h"
62// #include "AliFMDAltroIO.h" // ALIFMDALTROIO_H
63#include "AliAltroRawStreamV3.h"
64#include <TArrayS.h> // ROOT_TArrayS
65#include <TTree.h> // ROOT_TTree
66#include <TClonesArray.h> // ROOT_TClonesArray
67#include <TString.h>
68#include <iostream>
69#include <climits>
70// #include <iomanip>
71
72//____________________________________________________________________
73ClassImp(AliFMDRawReader)
74#if 0
75 ; // This is here to keep Emacs for indenting the next line
76#endif
77
78//____________________________________________________________________
79AliFMDRawReader::AliFMDRawReader(AliRawReader* reader, TTree* tree)
80 : TTask("FMDRawReader", "Reader of Raw ADC values from the FMD"),
81 fTree(tree),
82 fReader(reader),
83 // fSampleRate(1),
84 fData(0),
85 fNbytes(0),
86 fMinStrip(0),
87 fMaxStrip(127),
88 fPreSamp(14+5),
89 fSeen(0)
90{
91 // Default CTOR
92 for (Int_t i = 0; i < 3; i++) {
93 fSampleRate[i] = 0;
94 fZeroSuppress[i] = kFALSE;
95 fNoiseFactor[i] = 1;
96 fL1Phase[i] = 0;
97 }
98}
99
100//____________________________________________________________________
101void
102AliFMDRawReader::Exec(Option_t*)
103{
104 // Read the data
105 TClonesArray* array = new TClonesArray("AliFMDDigit");
106 if (!fTree) {
107 AliError("No tree");
108 return;
109 }
110 fTree->Branch("FMD", &array);
111
112
113 ReadAdcs(array);
114 Int_t nWrite = fTree->Fill();
115 AliDebug(1,Form("Got a grand total of %d digits, wrote %d bytes to tree",
116 array->GetEntriesFast(), nWrite));
117 delete array;
118}
119
120//____________________________________________________________________
121Int_t
122AliFMDRawReader::NewDDL(AliAltroRawStreamV3& input, UShort_t& det)
123{
124 // Process a new DDL. Sets the internal data members fZeroSuppress,
125 // fSampleRate, and fNoiseFactor based on information in the RCU trailer.
126 //
127 // Parameters:
128 // input Input stream
129 // det On return, the detector number
130 //
131 // Return value:
132 // negative value in case of problems, the DDL number otherwise
133
134 // Get the DDL number
135 UInt_t ddl = input.GetDDLNumber();
136 AliDebug(2,Form("DDL number %d", ddl));
137
138 // Note, previously, the ALTROCFG1 register was interpreted as
139 //
140 // Bits Value Description
141 // 0- 3 0/1 1st Baseline filter, mode
142 // 4- 5 Over-1 2nd baseline filter, # of pre-samples
143 // 6- 9 factor 2nd baseline filter, # of post-samples
144 // 10- 0 2nd baseline filter, enable
145 // 11-12 00 Zero suppression, glitch filter mode
146 // 13-15 001 Zero suppression, # of post samples
147 // 16-17 01 Zero suppression, # of pre samples
148 // 18 0/1 Zero suppression, enable
149 //
150 // The interpretation used in AliAltroRawStreamerV3 - which
151 // corresponds directly to ALTRO DPCFG register - is
152 //
153 // Bits Value Description
154 // 0- 3 0/1 1st Baseline filter, mode
155 // 4 0 Polarity (if '1', then "1's inverse")
156 // 5- 6 01 Zero suppression, # of pre samples
157 // 7-10 0001 Zero suppression, # of post samples
158 // 11 0 2nd baseline filter, enable
159 // 12-13 00 Zero suppression, glitch filter mode
160 // 14-16 factor 2nd baseline filter, # of post-samples
161 // 17-18 01 2nd baseline filter, # of pre-samples
162 // 19 0/1 Zero suppression, enable
163 //
164 // Writing 'x' for variable values, that means we have the
165 // following patterns for the 2 cases
166 //
167 // bit # 20 16 12 8 4 0
168 // old |0x01|0010|00xx|xxxx|xxxx|
169 // new |x01x|xx00|0000|1010|xxxx|
170 //
171 // That means that we can check if bits 10-13 are '1000' or
172 // '0000', which will tell us if the value was written with the
173 // new or the old interpretation. That is, we can check that
174 //
175 // if (((altrocfg1 >> 10) & 0x8) == 0x8) {
176 // // old interpretation
177 // }
178 // else {
179 // // New interpretation
180 // }
181 //
182 // That means, that we should never
183 //
184 // - change the # of zero suppression post samples
185 // - Turn on 2nd baseline correction
186 // - Change the zero-suppression glitch filter mode
187 //
188 // This change as introduced in version 1.2 of Rcu++
189 //
190 UInt_t cfg1 = input.GetAltroCFG1();
191 if (((cfg1 >> 10) & 0x8) == 0x8) {
192 UInt_t cfg2 = input.GetAltroCFG2();
193 AliDebug(3,Form("We have data from older MiniConf 0x%x cfg2=0x%08x",
194 ((cfg1 >> 10) & 0x8), cfg2));
195 fZeroSuppress[ddl] = (cfg1 >> 0) & 0x1;
196 fNoiseFactor[ddl] = (cfg1 >> 6) & 0xF;
197 fSampleRate[ddl] = (cfg2 >> 20) & 0xF;
198 }
199 else {
200 AliDebug(3,Form("We have data from newer MiniConf 0x%x",
201 ((cfg1 >> 10) & 0x8)));
202 fZeroSuppress[ddl] = input.GetZeroSupp();
203 // WARNING: We store the noise factor in the 2nd baseline
204 // filters excluded post samples, since we'll never use that
205 // mode.
206 fNoiseFactor[ddl] = input.GetNPostsamples();
207 // WARNING: We store the sample rate in the number of pre-trigger
208 // samples, since we'll never use that mode.
209 fSampleRate[ddl] = input.GetNPretriggerSamples();
210 //
211 }
212 AliDebug(10,Form("Phase of DDL=%d is %g (%d)", ddl, input.GetL1Phase(),
213 input.GetAltroCFG2() & 0x1F));
214 fL1Phase[ddl] = input.GetAltroCFG2() & 0x1F; // input.GetL1Phase();
215 AliDebug(3,Form("RCU @ DDL %d zero suppression: %s",
216 ddl, (fZeroSuppress[ddl] ? "yes" : "no")));
217 AliDebug(3,Form("RCU @ DDL %d noise factor: %d", ddl,fNoiseFactor[ddl]));
218 AliDebug(3,Form("RCU @ DDL %d sample rate: %d", ddl,fSampleRate[ddl]));
219
220
221 // Get Errors seen
222 Int_t nChAddrMismatch = input.GetNChAddrMismatch();
223 Int_t nChLenMismatch = input.GetNChLengthMismatch();
224 if (nChAddrMismatch != 0)
225 AliWarning(Form("Got %d channels with address mis-matches for 0x%03x",
226 nChAddrMismatch, ddl));
227 if (nChLenMismatch != 0)
228 AliWarning(Form("Got %d channels with length mis-matches for 0x%03x",
229 nChLenMismatch, ddl));
230
231 // Map DDL number to the detector number
232 AliFMDParameters* pars = AliFMDParameters::Instance();
233 AliFMDAltroMapping* map = pars->GetAltroMap();
234 if (map->DDL2Detector(ddl) < 0) return -1;
235 det = map->DDL2Detector(ddl);
236
237 if (AliLog::GetDebugLevel("FMD", 0) > 5)
238 input.PrintRCUTrailer();
239 return ddl;
240}
241
242//____________________________________________________________________
243Int_t
244AliFMDRawReader::NewChannel(const AliAltroRawStreamV3& input, UShort_t det,
245 Char_t& ring, UShort_t& sec, Short_t& strbase)
246{
247 // Processs a new channel. Sets the internal data members
248 // fMinStrip, fMaxStrip, and fPreSamp.
249 //
250 // Parameter:
251 // input Input stream
252 // ring On return, the ring identifier
253 // sec On return, the sector number
254 // strbase On return, the strip base
255 //
256 // Return value
257 // negative value in case of problems, hardware address otherwise
258
259 // Get the hardware address, and map that to detector coordinates
260 UShort_t board, chip, channel;
261 Int_t ddl = input.GetDDLNumber();
262 Int_t hwaddr = input.GetHWAddress();
263 if (input.IsChannelBad()) {
264 const char* msg = Form("Ignoring channel %03d/0x%03x with errors",
265 ddl, hwaddr);
266 AliWarning(msg);
267 if (AliDebugLevel() > 10) input.HexDumpChannel();
268 fReader->AddMinorErrorLog(AliAltroRawStreamV3::kAltroPayloadErr,msg);
269 fNErrors[ddl] += 1;
270 return 0xFFFF;
271 }
272
273 AliFMDParameters* pars = AliFMDParameters::Instance();
274 AliFMDAltroMapping* map = pars->GetAltroMap();
275 // Map to hardware stuff
276 map->ChannelAddress(hwaddr, board, chip, channel);
277 // Then try to map to detector address
278 if (!map->Channel2StripBase(board, chip, channel, ring, sec, strbase)) {
279 AliError(Form("Failed to get detector id from DDL %d, "
280 "hardware address 0x%03x", ddl, hwaddr));
281 return -1;
282 }
283 AliDebug(4,Form("Board: 0x%02x, Altro: 0x%x, Channel: 0x%x",
284 board, chip, channel));
285
286 // Get the 'conditions'
287 fMinStrip = pars->GetMinStrip(det, ring, sec, strbase);
288 fMaxStrip = pars->GetMaxStrip(det, ring, sec, strbase);
289 fPreSamp = pars->GetPreSamples(det, ring, sec, strbase);
290 if (fSampleRate[ddl] == 0)
291 fSampleRate[ddl] = pars->GetSampleRate(det, ring, sec, strbase);
292
293 return hwaddr;
294}
295
296//____________________________________________________________________
297Bool_t
298AliFMDRawReader::NewBunch(const AliAltroRawStreamV3& input,
299 UShort_t& start, UShort_t& length)
300{
301 //
302 // Do some checks on the bunch data
303 //
304 Int_t ddl = input.GetDDLNumber();
305 Int_t hwaddr = input.GetHWAddress();
306 UShort_t nSamples = input.GetNSamplesPerCh() + fPreSamp;
307 UShort_t tstart = input.GetStartTimeBin();
308 length = input.GetBunchLength();
309
310 if (tstart >= nSamples) {
311 const char* msg = Form("Bunch in %03d/0x%03x has an start time greater "
312 "than number of samples: 0x%x >= 0x%x",
313 ddl, hwaddr, tstart, nSamples);
314 AliWarning(msg);
315 if (AliDebugLevel() > 10) input.HexDumpChannel();
316 fReader->AddMinorErrorLog(AliAltroRawStreamV3::kAltroPayloadErr,msg);
317 fNErrors[ddl]++;
318 return false;
319 }
320 if ((int(tstart) - length + 1) < 0) {
321 const char* msg = Form("Bunch in %03d/0x%03x has an invalid length and "
322 "start time: 0x%x,0x%x (%d-%d+1=%d<0)",
323 ddl, hwaddr, length, tstart, tstart, length,
324 int(tstart)-length+1);
325 AliWarning(msg);
326 if (AliDebugLevel() > 10) input.HexDumpChannel();
327 fReader->AddMinorErrorLog(AliAltroRawStreamV3::kAltroPayloadErr,msg);
328 fNErrors[ddl]++;
329 return false;
330 }
331 if (tstart >= start) {
332 const char* msg = Form("Bunch in %03d/0x%03x has early start time: "
333 "0x%x >= 0x%x", ddl, hwaddr, tstart, start);
334 AliWarning(msg);
335 if (AliDebugLevel() > 10) input.HexDumpChannel();
336 fReader->AddMinorErrorLog(AliAltroRawStreamV3::kAltroPayloadErr,msg);
337 fNErrors[ddl]++;
338 return false;
339 }
340 start = tstart;
341 return true;
342}
343
344//____________________________________________________________________
345Int_t
346AliFMDRawReader::NewSample(const AliAltroRawStreamV3& input,
347 Int_t i, UShort_t t, UShort_t sec,
348 UShort_t strbase, Short_t& str, UShort_t& samp)
349{
350 // Process a new timebin
351 //
352 // Parameters:
353 // input Input stream
354 // i Index into bunch data
355 // t Time
356 // strbase Base of strip numbers for this channel
357 // str On return, the strip number
358 // samp On return, the sample number
359 //
360 // Return value
361 // negative value in case of problems, ADC value otherwise
362 if (t < fPreSamp) return -1;
363
364 Int_t ddl = input.GetDDLNumber();
365 Int_t hwa = input.GetHWAddress();
366 const UShort_t* data = input.GetSignals();
367 Short_t adc = data[i];
368 AliDebug(10,Form("0x%04x/0x%03x/%04d %4d", ddl, hwa, t, adc));
369
370 AliFMDParameters* pars = AliFMDParameters::Instance();
371 AliFMDAltroMapping* map = pars->GetAltroMap();
372
373 samp = 0;
374 Short_t stroff = 0;
375 map->Timebin2Strip(sec, t, fPreSamp, fSampleRate[ddl], stroff, samp);
376 str = strbase + stroff;
377
378 AliDebug(20,Form("0x%04x/0x%03x/%04d=%4d maps to strip %3d sample %d "
379 "(pre: %d, min: %d, max: %d, rate: %d)",
380 ddl, hwa, t, adc, str, samp, fPreSamp,
381 fMinStrip, fMaxStrip, fSampleRate[ddl]));
382 if (str < 0) {
383 AliDebug(10,Form("Got presamples at timebin %d", i));
384 return -1;
385 }
386
387 // VA1 Local strip number
388 Short_t lstrip = (t - fPreSamp) / fSampleRate[ddl] + fMinStrip;
389
390 AliDebug(15,Form("Checking if strip %d (%d) in range [%d,%d]",
391 lstrip, str, fMinStrip, fMaxStrip));
392 if (lstrip < fMinStrip || lstrip > fMaxStrip) {
393 AliDebug(10,Form("Strip %03d-%d (%d,%d) from t=%d out of range (%3d->%3d)",
394 str, samp, lstrip, stroff, t, fMinStrip, fMaxStrip));
395 adc = -1;
396 }
397 // Possibly do pedestal subtraction of signal
398 if (adc > 1023)
399 AliWarning(Form("ADC value out of range: %4d", adc));
400 return adc;
401}
402
403//____________________________________________________________________
404Int_t
405AliFMDRawReader::NextSample(UShort_t& det, Char_t& rng, UShort_t& sec,
406 UShort_t& str, UShort_t& sam, UShort_t& rat,
407 Short_t& adc, Bool_t& zs, UShort_t& fac)
408{
409 // Scan current event for next signal. It returns kFALSE when
410 // there's no more data in the event.
411 //
412 // Note, that this member function is in principle very fast, but
413 // contains less error checking. In particular, channels that have
414 // bad bunches cannot be checked here. Seeing a bad bunch will only
415 // skip the remainder of the channel and not reset the already read
416 // digits. This is potentially dangerous.
417 //
418 // Parameters:
419 // det On return, contain the detector number
420 // rng On return, contain the ring identifier
421 // sec On return, contain the sector number
422 // str On return, contain the strip number
423 // sam On return, contain the sample number
424 // rat On return, contain the sample rate
425 // adc On return, contain the ADC counts
426 // zs On return, contain the zero-supp. flag
427 // fac On return, contain the zero-supp. noise factor
428 //
429 // Return values:
430 // 0 No more data
431 // -1 Read sample belongs to a bad bunch
432 // >0 Good status - contains bit mask of values
433 // Bit 1 New DDL
434 // Bit 2 New Channel
435 // Bit 3 New Bunch
436 // Bit 4 New Sample
437 static AliAltroRawStreamV3 stream(fReader); // = 0;
438 static Int_t ddl = -1;
439 static UShort_t tdet = 0;
440 static Char_t trng = '\0';
441 static UShort_t tsec = 0;
442 static Short_t tstr = 0;
443 static Short_t bstr = -1;
444 static UShort_t tsam = 0;
445 static UInt_t trate = 0;
446 static Int_t hwaddr = -1;
447 static UShort_t start = 0;
448 static UShort_t length = 0;
449 static Short_t t = -1;
450 static Int_t i = 0;
451 // First entry!
452 if (stream.GetDDLNumber() < 0) {
453 fReader->Reset();
454 fReader->Select("FMD");
455 stream.Reset();
456 stream.SelectRawData("FMD");
457 stream.SetCheckAltroPayload(false);
458 for (Int_t j = 0; j < kNDDL; j++) fNErrors[j] = 0;
459
460 // Reset variables
461 ddl = -1;
462 trate = 0;
463 tdet = 0;
464 trng = '\0';
465 tsec = 0;
466 tstr = 0;
467 tsam = -1;
468 hwaddr = -1;
469 }
470
471 UShort_t ret = 0;
472 do {
473 AliDebug(15,Form("t=%4d, start=%4d, length=%4d", t, start, length));
474 if (t < start - length + 1) {
475 AliDebug(10,Form("Time t=%d < start-length+1=%d-%d+1 (%3d/0x%03x)",
476 t, start, length, ddl, hwaddr));
477 if (hwaddr > 0xFFF ||
478 hwaddr < 0 ||
479 !stream.NextBunch()) {
480 if (AliDebugLevel() >= 10 && hwaddr > 0xFFF) {
481 AliDebug(10,"Last channel read was marked bad");
482 }
483 if (AliDebugLevel() >= 10 && hwaddr < 0) {
484 AliDebug(10,"No more channels");
485 }
486 AliDebug(10,"No next bunch, or first entry");
487 if (ddl < 0 || !stream.NextChannel()) {
488 if (AliDebugLevel() >= 10 && ddl < 0) {
489 AliDebug(10,"No DDL");
490 }
491 AliDebug(10,"No next channel, or first entry");
492 if (!stream.NextDDL()) {
493 AliDebug(10,"No more DDLs");
494 stream.Reset();
495 return 0;
496 }
497 ddl = NewDDL(stream, tdet);
498 AliDebug(5,Form("New DDL: %d (%d)", ddl, tdet));
499 ret |= 0x1;
500 continue;
501 }
502 hwaddr = NewChannel(stream, tdet, trng, tsec, bstr);
503 if (hwaddr > 0xFFF) fNErrors[ddl] += 1;
504 AliDebug(5,Form("New Channel: %3d/0x%03x", ddl, hwaddr));
505 start = 1024;
506 ret |= 0x2;
507 continue;
508 }
509 if (!NewBunch(stream, start, length)) {
510 // AliWarning(Form("Bad bunch in %3d/0x%03x read - "
511 // "should progress to next channel "
512 // "(t=%4d,start=%4d,length=%4d)",
513 // ddl, hwaddr, t,start, length));
514 hwaddr = 0xFFFF; // Bad channel
515 return -1;
516 }
517 AliDebug(5, Form("New bunch in %3d/0x%03x: start=0x%03x, length=%4d",
518 ddl, hwaddr, start, length));
519 ret |= 0x4;
520 t = start;
521 i = 0;
522 AliDebug(10,Form("Got new bunch FMD%d%c[%2d], bunch @ %d, length=%d",
523 tdet, trng, tsec, start, length));
524 }
525 Int_t tadc = NewSample(stream, i, t, tsec, bstr, tstr, tsam);
526 AliDebug(10,Form("New sample FMD%d%c[%2d,%3d]-%d = 0x%03x",
527 tdet, trng, tsec, tstr, tsam, tadc));
528 ret |= 0x8;
529 if (tadc >= 0) {
530 det = tdet;
531 rng = trng;
532 sec = tsec;
533 str = tstr;
534 sam = tsam;
535 adc = tadc;
536 rat = fSampleRate[ddl];
537 zs = fZeroSuppress[ddl];
538 fac = fNoiseFactor[ddl];
539 t--;
540 i++;
541 AliDebug(10,Form("Returning FMD%d%c[%2d,%3d]-%d = 0x%03x (%d,%d,%d)",
542 det, rng, sec, str, sam, adc, rat, zs, fac));
543 break;
544 }
545 t--;
546 i++;
547 } while (true);
548 AliDebug(5,Form("Returning 0x%02x", ret));
549 return ret;
550}
551
552
553//____________________________________________________________________
554Int_t
555AliFMDRawReader::NextSignal(UShort_t& det, Char_t& rng,
556 UShort_t& sec, UShort_t& str,
557 Short_t& adc, Bool_t& zs,
558 UShort_t& fac)
559{
560 //
561 // Get the next signal
562 //
563 // Parameters:
564 // det On return, the detector
565 // rng On return, the ring
566 // sec On return, the sector
567 // str On return, the strip
568 // adc On return, the ADC value
569 // zs On return, whether zero-supp. is enabled
570 // fac On return, the usd noise factor
571 //
572 // Return:
573 // true if valid data is returned
574 //
575 Int_t ret = 0;
576 do {
577 UShort_t samp, rate;
578 if ((ret = NextSample(det, rng, sec, str, samp, rate, adc, zs, fac)) <= 0)
579 return ret;
580
581 Bool_t take = SelectSample(samp, rate);
582 if (!take) continue;
583 break;
584 } while (true);
585 return ret;
586}
587
588//____________________________________________________________________
589Bool_t
590AliFMDRawReader::SelectSample(UShort_t samp, UShort_t rate)
591{
592 // Check if the passed sample is the one we need
593 Bool_t take = kFALSE;
594 switch (rate) {
595 case 1: take = kTRUE; break;
596 case 2: if (samp == 1) take = kTRUE; break;
597 case 3: if (samp == 1) take = kTRUE; break;
598 case 4: if (samp == 2) take = kTRUE; break;
599 default: if (samp == rate-2) take = kTRUE; break;
600 }
601
602 return take;
603}
604
605//____________________________________________________________________
606Bool_t
607AliFMDRawReader::ReadAdcs(TClonesArray* array)
608{
609 // Read ADC values from raw input into passed TClonesArray of AliFMDDigit
610 // objects.
611 AliDebug(3,Form("Reading ADC values into a TClonesArray"));
612
613 // Read raw data into the digits array, using AliFMDAltroReader.
614 if (!array) {
615 AliError("No TClonesArray passed");
616 return kFALSE;
617 }
618 const UShort_t kUShortMax = (1 << 16) - 1;
619 fSeen.Reset(kUShortMax);
620 for (Int_t ddl = 0; ddl < kNDDL; ddl++) fNErrors[ddl] = 0;
621
622 AliAltroRawStreamV3 input(fReader);
623 input.Reset();
624 input.SetCheckAltroPayload(false);
625 input.SelectRawData("FMD");
626
627 // Loop over input RORCs
628 while (input.NextDDL()) {
629 UShort_t det = 0;
630 Int_t ddl = NewDDL(input, det);
631 if (ddl < 0) break;
632 fNErrors[ddl] = 0;
633
634 while (input.NextChannel()) {
635 // Get the hardware address, and map that to detector coordinates
636 Char_t ring;
637 UShort_t sec;
638 Short_t strbase;
639 Int_t hwaddr = NewChannel(input, det, ring, sec, strbase);
640 if (hwaddr < 0) break;
641 if (hwaddr > 0xFFF) continue;
642
643 UShort_t start = 0x3FF;
644 Bool_t errors = false;
645 Int_t first = -1;
646 Int_t last = -1;
647 // Loop over bunches
648 while (input.NextBunch()) {
649 // Get Lenght of bunch, and pointer to the data
650 const UShort_t* data = input.GetSignals();
651 UShort_t length;
652 if (!NewBunch(input, start, length)) {
653 errors = true;
654 break;
655 }
656
657
658 // Loop over the data and store it.
659 for (Int_t i = 0; i < length; i++) {
660 // Time
661 Short_t str;
662 UShort_t samp;
663 Int_t t = start - i;
664 Int_t adc = NewSample(input, i, t, sec, strbase, str, samp);
665 if (adc < 0) continue;
666 UShort_t counts = adc;
667
668 AliDebug(10, Form("FMD%d%c[%02d,%03d]-%d: %4d",
669 det, ring, sec, str, samp, counts));
670 // Check the cache of indicies
671 Int_t idx = fSeen(det, ring, sec, str);
672 AliFMDDigit* digit = 0;
673 if (idx == kUShortMax) {
674 // We haven't seen this strip yet.
675 fSeen(det, ring, sec, str) = idx = array->GetEntriesFast();
676 AliDebug(7,Form("making digit @ %5d for FMD%d%c[%2d,%3d]-%d "
677 "from %3d/0x%03x/%4d",
678 idx, det, ring, sec, str, samp, ddl, hwaddr, t));
679 digit = new ((*array)[idx]) AliFMDDigit(det, ring, sec, str);
680 digit->SetDefaultCounts(fSampleRate[ddl]);
681 }
682 else {
683 digit = static_cast<AliFMDDigit*>(array->At(idx));
684 }
685 if (first < 0) first = idx;
686 last = idx;
687 AliDebug(10, Form("Setting FMD%d%c[%2d,%3d]-%d from timebin "
688 "%4d=%4d (%4d)", det, ring, sec, str, samp, t,
689 counts, data[i]));
690 digit->SetCount(samp, counts);
691 } // for (i)
692 } // while (bunch)
693 if (errors) {
694 AliWarning(Form("Channel %3d/0x%03x contain errors, "
695 "resetting index %d to %d", ddl, hwaddr, first, last));
696 if (first >= 0) {
697 for (Int_t i = first; i <= last; i++) {
698 AliFMDDigit* digit = static_cast<AliFMDDigit*>(array->At(i));
699 for (Int_t j = 0; j < fSampleRate[ddl]; j++) {
700 AliDebug(10,Form("Resetting strip %s=%d",
701 digit->GetName(),digit->Counts()));
702 digit->SetCount(j, kBadSignal);
703 }
704 }
705 }
706 }
707 // if (errors && (AliDebugLevel() > 0)) input.HexDumpChannel();
708 } // while (channel)
709 } // while (ddl)
710 return kTRUE;
711}
712//____________________________________________________________________
713Bool_t
714AliFMDRawReader::ReadAdcs(AliFMDUShortMap& map)
715{
716 // Read ADC values from raw input into passed TClonesArray of AliFMDDigit
717 // objects.
718 AliDebug(3,Form("Reading ADC values into a map"));
719
720 // const UShort_t kUShortMax = (1 << 16) - 1;
721 for (Int_t ddl = 0; ddl < kNDDL; ddl++) fNErrors[ddl] = 0;
722
723 AliAltroRawStreamV3 input(fReader);
724 input.Reset();
725 input.SetCheckAltroPayload(false);
726 input.SelectRawData("FMD");
727
728 // Loop over input RORCs
729 while (input.NextDDL()) {
730 UShort_t det = 0;
731 Int_t ddl = NewDDL(input, det);
732 if (ddl < 0) break;
733 fNErrors[ddl] = 0;
734
735 while (input.NextChannel()) {
736 // Get the hardware address, and map that to detector coordinates
737 Char_t ring;
738 UShort_t sec;
739 Short_t strbase;
740 Int_t hwaddr = NewChannel(input, det, ring, sec, strbase);
741 if (hwaddr < 0) break;
742 if (hwaddr > 0xFFF) continue;
743
744 UShort_t start = 0x3FF;
745 Bool_t errors = false;
746 Int_t first = -1;
747 Int_t last = -1;
748 // Loop over bunches
749 while (input.NextBunch()) {
750 // Get Lenght of bunch, and pointer to the data
751 // const UShort_t* data = input.GetSignals();
752 UShort_t length;
753 if (!NewBunch(input, start, length)) {
754 errors = true;
755 break;
756 }
757
758
759 // Loop over the data and store it.
760 for (Int_t i = 0; i < length; i++) {
761 // Time
762 Short_t str;
763 UShort_t samp;
764 Int_t t = start - i;
765 Int_t adc = NewSample(input, i, t, sec, strbase, str, samp);
766 if (adc < 0) continue;
767 UShort_t counts = adc;
768
769 AliDebug(10, Form("FMD%d%c[%02d,%03d]-%d: %4d",
770 det, ring, sec, str, samp, counts));
771 if (SelectSample(samp, fSampleRate[ddl]))
772 map(det,ring,sec,str) = counts;
773 if (first < 0) first = str;
774 last = str;
775 } // for (i)
776 } // while (bunch)
777 if (errors) {
778 AliWarning(Form("Channel %3d/0x%03x contain errors, "
779 "resetting strips %d to %d", ddl, hwaddr, first, last));
780 if (first >= 0) {
781 Int_t ds = first <= last ? 1 : -1;
782 for (Int_t i = first; i != last+ds; i += ds) {
783 AliDebug(10, Form("Resetting strip FMD%d%c[%02d,%03d]=%d",
784 det,ring,sec,i,map(det,ring,sec,i)));
785 map(det,ring,sec,i) = kBadSignal;
786 }
787 }
788 }
789 } // while (channel)
790 } // while (ddl)
791 return kTRUE;
792}
793
794//____________________________________________________________________
795Bool_t AliFMDRawReader::ReadSODevent(AliFMDCalibSampleRate* sampleRate,
796 AliFMDCalibStripRange* stripRange,
797 TArrayS &pulseSize,
798 TArrayS &pulseLength,
799 Bool_t* detectors)
800{
801 //
802 // Read SOD event into passed objects.
803 //
804 // Parameters:
805 // samplerate The sample rate object to fill
806 // striprange The strip range object to fill
807 // pulseSize The pulse size object to fill
808 // pulseLength The pulse length (in events) object to fill
809 //
810 // Return:
811 // @c true on success
812 //
813 AliDebug(0,Form("Start of SOD/EOD"));
814
815 UInt_t shift_clk[18];
816 UInt_t sample_clk[18];
817 UInt_t strip_low[18];
818 UInt_t strip_high[18];
819 UInt_t pulse_size[18];
820 UInt_t pulse_length[18];
821 for (size_t i = 0; i < 18; i++) {
822 shift_clk[i] = 0;
823 sample_clk[i] = 0;
824 strip_low[i] = 0;
825 strip_high[i] = 0;
826 pulse_size[i] = 0;
827 pulse_length[i] = 0;
828 }
829 AliFMDParameters* param = AliFMDParameters::Instance();
830 AliFMDAltroMapping* map = param->GetAltroMap();
831
832 AliAltroRawStreamV3 streamer(fReader);
833 streamer.Reset();
834 streamer.SelectRawData("FMD");
835 //fReader->GetDDLID();
836 while (streamer.NextDDL()) {
837 Int_t ddl = streamer.GetDDLNumber();
838 Int_t detID = fReader->GetDetectorID();
839 if (detectors) detectors[map->DDL2Detector(ddl)-1] = kTRUE;
840 AliDebug(0,Form(" From reader: DDL number is %d , det ID is %d",ddl,detID));
841
842 ULong_t nPayloadWords = streamer.GetRCUPayloadSizeInSOD();
843 UChar_t* payloadData = streamer.GetRCUPayloadInSOD();
844 UInt_t* payloadWords = reinterpret_cast<UInt_t*>(payloadData);
845 //UInt_t* payloadWords = streamer.GetRCUPayloadInSOD();
846
847 //std::cout<<nPayloadWords<<" "<<ddl<<std::endl;
848 for (ULong_t i = 1; i <= nPayloadWords ; i++, payloadWords++) {
849 UInt_t payloadWord = *payloadWords; // Get32bitWord(i);
850
851 //std::cout<<i<<Form(" word: 0x%x",payloadWord)<<std::endl;
852 // address is only 24 bit
853 UInt_t address = (0xffffff & payloadWord);
854 UInt_t type = ((address >> 21) & 0xf);
855 UInt_t error = ((address >> 20) & 0x1);
856 UInt_t bcast = ((address >> 18) & 0x1);
857 UInt_t bc_not_altro = ((address >> 17) & 0x1);
858 UInt_t board = ((address >> 12) & 0x1f);
859 UInt_t instruction = 0;
860 UInt_t chip = 0;
861 UInt_t channel = 0;
862 if(bc_not_altro)
863 instruction = address & 0xfff;
864 else {
865 chip = ((address >> 9) & 0x7);
866 channel = ((address >> 5) & 0x5);
867 instruction = (address & 0x1f);
868 }
869
870 Bool_t readDataWord = kFALSE;
871 switch(type) {
872 case 0x0: // Fec read
873 readDataWord = kTRUE;
874 case 0x1: // Fec cmd
875 case 0x2: // Fec write
876 i++;
877 payloadWords++;
878 break;
879 case 0x4: // Loop
880 case 0x5: // Wait
881 break;
882 case 0x6: // End sequence
883 case 0x7: // End Mem
884 i = nPayloadWords + 1;
885 break;
886 default:
887 break;
888 }
889
890 //Don't read unless we have a FEC_RD
891 if(!readDataWord) continue;
892
893 UInt_t dataWord = *payloadWords;//Get32bitWord(i);
894 UInt_t data = (0xFFFFF & dataWord) ;
895 //UInt_t data = (0xFFFF & dataWord) ;
896
897 if(error) {
898 AliWarning(Form("error bit detected at Word 0x%06x; "
899 "error % d, type %d, bc_not_altro %d, "
900 "bcast %d, board 0x%02x, chip 0x%x, "
901 "channel 0x%02x, instruction 0x%03x",
902 address, error, type, bc_not_altro,
903 bcast,board,chip,channel,instruction));
904 //process error
905 continue;
906 }
907
908
909 switch(instruction) {
910
911 case 0x01: break; // First ADC T
912 case 0x02: break; // I 3.3 V
913 case 0x03: break; // I 2.5 V altro digital
914 case 0x04: break; // I 2.5 V altro analog
915 case 0x05: break; // I 2.5 V VA
916 case 0x06: break; // First ADC T
917 case 0x07: break; // I 3.3 V
918 case 0x08: break; // I 2.5 V altro digital
919 case 0x09: break; // I 2.5 V altro analog
920 case 0x0A: break; // I 2.5 V VA
921 case 0x2D: break; // Second ADC T
922 case 0x2E: break; // I 1.5 V VA
923 case 0x2F: break; // I -2.0 V
924 case 0x30: break; // I -2.0 V VA
925 case 0x31: break; // 2.5 V Digital driver
926 case 0x32: break; // Second ADC T
927 case 0x33: break; // I 1.5 V VA
928 case 0x34: break; // I -2.0 V
929 case 0x35: break; // I -2.0 V VA
930 case 0x36: break; // 2.5 V Digital driver
931 case 0x37: break; // Third ADC T
932 case 0x38: break; // Temperature sens. 1
933 case 0x39: break; // Temperature sens. 2
934 case 0x3A: break; // U 2.5 altro digital (m)
935 case 0x3B: break; // U 2.5 altro analog (m)
936 case 0x3C: break; // Third ADC T
937 case 0x3D: break; // Temperature sens. 1
938 case 0x3E: break; // Temperature sens. 2
939 case 0x3F: break; // U 2.5 altro digital (m)
940 case 0x40: break; // U 2.5 altro analog (m)
941 case 0x41: break; // Forth ADC T
942 case 0x42: break; // U 2.5 VA (m)
943 case 0x43: break; // U 1.5 VA (m)
944 case 0x44: break; // U -2.0 VA (m)
945 case 0x45: break; // U -2.0 (m)
946 case 0x46: break; // Forth ADC T
947 case 0x47: break; // U 2.5 VA (m)
948 case 0x48: break; // U 1.5 VA (m)
949 case 0x49: break; // U -2.0 VA (m)
950 case 0x4A: break; // U -2.0 (m)
951 // Counters
952 case 0x0B: break; // L1 trigger CouNTer
953 case 0x0C: break; // L2 trigger CouNTer
954 case 0x0D: break; // Sampling CLK CouNTer
955 case 0x0E: break; // DSTB CouNTer
956 // Test mode
957 case 0x0F: break; // Test mode word
958 case 0x10: break; // Undersampling ratio.
959 // Configuration and status
960 case 0x11: break; // Config/Status Register 0
961 case 0x12: break; // Config/Status Register 1
962 case 0x13: break; // Config/Status Register 2
963 case 0x14: break; // Config/Status Register 3
964 case 0x15: break; // Free
965 // Comands:
966 case 0x16: break; // Latch L1, L2, SCLK Counters
967 case 0x17: break; // Clear counters
968 case 0x18: break; // Clear CSR1
969 case 0x19: break; // rstb ALTROs
970 case 0x1A: break; // rstb BC
971 case 0x1B: break; // Start conversion
972 case 0x1C: break; // Scan event length
973 case 0x1D: break; // Read event length
974 case 0x1E: break; // Start test mode
975 case 0x1F: break; // Read acquisition memory
976 // FMD
977 case 0x20: break; // FMDD status
978 case 0x21: break; // L0 counters
979 case 0x22: break; // FMD: Wait to hold
980 case 0x23: break; // FMD: L1 timeout
981 case 0x24: break; // FMD: L2 timeout
982 case 0x25: // FMD: Shift clk
983 shift_clk[board] = ((data >> 8 ) & 0xFF);
984 AliDebug(30,Form("Read shift_clk=%d for board 0x%02x",
985 shift_clk[board], board));
986 break;
987 case 0x26: // FMD: Strips
988 strip_low[board] = ((data >> 0 ) & 0xFF);
989 strip_high[board] = ((data >> 8 ) & 0xFF);
990 break;
991 case 0x27: // FMD: Cal pulse
992 pulse_size[board] = ((data >> 8 ) & 0xFF);
993 break;
994 case 0x28: break; // FMD: Shape bias
995 case 0x29: break; // FMD: Shape ref
996 case 0x2A: break; // FMD: Preamp ref
997 case 0x2B: // FMD: Sample clk
998 sample_clk[board] = ((data >> 8 ) & 0xFF);
999 AliDebug(30,Form("Read sample_clk=%d for board 0x%02x",
1000 sample_clk[board], board));
1001 break;
1002 case 0x2C: break; // FMD: Commands
1003 case 0x4B: // FMD: Cal events
1004 pulse_length[board] = ((data >> 0 ) & 0xFF);
1005 break;
1006 default: break;
1007
1008 }
1009 AliDebug(50,Form("instruction 0x%x, dataword 0x%x",
1010 instruction,dataWord));
1011 } // End of loop over Result memory event
1012
1013 UShort_t det = 0;
1014 UShort_t sector = 0;
1015 Short_t strip = -1;
1016 Char_t ring = '\0';
1017
1018 const UInt_t boards[4] = {0,1,16,17};
1019 for(Int_t i=0;i<4;i++) {
1020 if(ddl==0 && (i==1 || i==3)) continue;
1021
1022 UInt_t chip =0, channel=0;
1023 det = map->DDL2Detector(ddl);
1024 map->Channel2StripBase(boards[i], chip, channel, ring, sector, strip);
1025
1026 UInt_t samplerate = 0;
1027#if USE_VOTE
1028 if(sample_clk[boards[i]] == 0) {
1029 if(ddl == 0) {
1030 Int_t sample1 = sample_clk[boards[0]];
1031 Int_t sample2 = sample_clk[boards[2]];
1032 if(sample1) sample_clk[boards[i]] = sample1;
1033 else sample_clk[boards[i]] = sample2;
1034 }
1035 else {
1036 Int_t sample1 = sample_clk[boards[0]];
1037 Int_t sample2 = sample_clk[boards[1]];
1038 Int_t sample3 = sample_clk[boards[2]];
1039 Int_t sample4 = sample_clk[boards[3]];
1040 Int_t agreement = 0;
1041 if(sample1 == sample2) agreement++;
1042 if(sample1 == sample3) agreement++;
1043 if(sample1 == sample4) agreement++;
1044 if(sample2 == sample3) agreement++;
1045 if(sample2 == sample4) agreement++;
1046 if(sample3 == sample4) agreement++;
1047
1048 Int_t idx = 0;
1049 if(i<3) idx = i+1;
1050 else idx = i-1;
1051 if(agreement == 3) {
1052 sample_clk[boards[i]] = sample_clk[boards[idx]];
1053 shift_clk[boards[i]] = shift_clk[boards[idx]];
1054 strip_low[boards[i]] = strip_low[boards[idx]];
1055 strip_high[boards[i]] = strip_high[boards[idx]];
1056 pulse_length[boards[i]] = pulse_length[boards[idx]];
1057 pulse_size[boards[i]] = pulse_size[boards[idx]];
1058 AliDebug(3,Form("Vote taken for ddl %d, board 0x%x",
1059 ddl,boards[i]));
1060 }
1061 }
1062 }
1063#endif
1064
1065 if(sample_clk[boards[i]])
1066 samplerate = shift_clk[boards[i]]/sample_clk[boards[i]];
1067 AliDebug(10,Form("Sample rate for board 0x%02x is %d",
1068 boards[i], samplerate));
1069 sampleRate->Set(det,ring,sector,0,samplerate);
1070 stripRange->Set(det,ring,sector,0,
1071 strip_low[boards[i]],strip_high[boards[i]]);
1072
1073 AliDebug(20,Form("det %d, ring %c, ",det,ring));
1074 pulseLength.AddAt(pulse_length[boards[i]],
1075 GetHalfringIndex(det,ring,boards[i]/16));
1076 pulseSize.AddAt(pulse_size[boards[i]],
1077 GetHalfringIndex(det,ring,boards[i]/16));
1078
1079
1080
1081 AliDebug(20,Form(": Board: 0x%02x\n"
1082 "\tstrip_low %3d, strip_high %3d\n"
1083 "\tshift_clk %3d, sample_clk %3d\n"
1084 "\tpulse_size %3d, pulse_length %3d",
1085 boards[i],
1086 strip_low[boards[i]], strip_high[boards[i]],
1087 shift_clk[boards[i]], sample_clk[boards[i]],
1088 pulse_size[boards[i]],pulse_length[boards[i]]));
1089 }
1090
1091 }
1092
1093 AliFMDParameters::Instance()->SetSampleRate(sampleRate);
1094 AliFMDParameters::Instance()->SetStripRange(stripRange);
1095
1096 AliDebug(0,Form("End of SOD/EOD"));
1097
1098 return kTRUE;
1099}
1100//____________________________________________________________________
1101
1102UInt_t AliFMDRawReader::Get32bitWord(Int_t idx)
1103{
1104 // This method returns the 32 bit word at a given
1105 // position inside the raw data payload.
1106 // The 'index' points to the beginning of the next word.
1107 // The method is supposed to be endian (platform)
1108 // independent.
1109 if (!fData) {
1110 AliFatal("Raw data paylod buffer is not yet initialized !");
1111 }
1112
1113 Int_t index = 4*idx;
1114
1115 if (index < 4) {
1116 // fRawReader->AddFatalErrorLog(k32bitWordReadErr,Form("pos = %d",index));
1117 // PrintDebug();
1118 AliWarning(Form("Invalid raw data payload position (%d) !",index));
1119 }
1120
1121 UInt_t word = 0;
1122
1123 word = fData[--index] << 24;
1124 word |= fData[--index] << 16;
1125 word |= fData[--index] << 8;
1126 word |= fData[--index] << 0 ;
1127
1128 return word;
1129}
1130//_____________________________________________________________________
1131Int_t AliFMDRawReader::GetHalfringIndex(UShort_t det, Char_t ring,
1132 UShort_t board) const
1133{
1134 //
1135 // Get short index for a given half-ring
1136 //
1137 // Parameters:
1138 // det Detector number
1139 // ring Ring identifer
1140 // board Board number
1141 //
1142 // Return:
1143 //
1144 //
1145 UShort_t iring = (ring == 'I' ? 1 : 0);
1146
1147 Int_t index = (((det-1) << 2) | (iring << 1) | (board << 0));
1148
1149 return index-2;
1150
1151}
1152
1153//____________________________________________________________________
1154//
1155// EOF
1156//
1157