/* $Id$ */
-//////////////////////////////////////////////////////////////////////
+//____________________________________________________________________
//
// Buffer to read RAW ALTRO FMD format from a AliRawReader
//
-// Currently, I had to overload the Next member function and introduce
-// my own members fMyData, fMyPosition, fMyCount, and fMyBunchLength.
-// The reason is, that we can use the fMyCount to determine the
-// sampling rate used in the ALTRO. However,
-// AliAltroBuffer::fCount is private, so we can not access it.
-//
-// If it wasn't I'd implement Next as
-//
-// Bool_t
-// AliFMDRawStreamer::Next()
-// {
-// if (AliAltroRawStreamer::Next()) {
-// if (fPrevPad != fPad)
-// fSampleRate = (fCount - 2) / 128;
-// return kTRUE;
-// }
-// return kFALSE;
-// }
+// This class derives from AliAltroBuffer, but overloads the memer
+// function Next to do some extra processing. In particular, it tries
+// to autodetect the sample rate. If zero-suppression was used when
+// writing the raw data, then the automatic discovery will not work,
+// and the sample rate should be set explicitly.
//
-//////////////////////////////////////////////////////////////////////
-#ifndef ALIFMDRAWSTREAM_H
-# include "AliFMDRawStream.h"
-#endif
-#ifndef ALIRAWREADER_H
-# include "AliRawReader.h"
-#endif
-#ifndef __IOSTREAM__
-# include <iostream>
-#endif
+#include "AliFMDRawStream.h" // ALIFMDRAWSTREAM_H
+// #include <AliRawReader.h> // ALIRAWREADER_H
+#include "AliFMDParameters.h"
+// #include <AliLog.h>
+#include "AliFMDDebug.h" // Better debug macros
+// #include <iomanip>
+// #include <iostream>
+#include "AliRawReader.h"
+#include <climits>
//____________________________________________________________________
-ClassImp(AliFMDRawStream);
+ClassImp(AliFMDRawStream)
+#if 0
+ ; // This is here to keep Emacs for indenting the next line
+#endif
//____________________________________________________________________
AliFMDRawStream::AliFMDRawStream(AliRawReader* reader)
- : AliAltroRawStream(reader),
- // fMyData(0),
- // fMyPosition(0),
- // fMyCount(0),
- // fMyBunchLength(0),
- fPrevTime(-1)
-{}
-
-
-
-//_____________________________________________________________________________
-Bool_t
-AliFMDRawStream::Next()
+ : AliAltroRawStream(reader)
{
- // read the next raw digit
- // returns kFALSE if there is no digit left
- fPrevTime = fTime;
- if (AliAltroRawStream::Next()) {
- if (fPrevPad != fPad) {
- fSampleRate = fTimeBunch / 128;
-#if 1
- std::cout << "Setting Sample rate to (" << fTimeBunch << "/"
- << 128 << "=" << fSampleRate << std::endl;
-#endif
- }
- return kTRUE;
- }
- return kFALSE;
+ // CTOR
+ reader->Reset();
+ fNoAltroMapping = kFALSE;
+ // Select FMD DDL's
+ SelectRawData("FMD");
}
-#if 0
//_____________________________________________________________________________
Bool_t
-AliFMDRawStream::Next()
+AliFMDRawStream::ReadChannel(UInt_t& ddl, UInt_t& addr,
+ UInt_t& len, volatile UShort_t* data)
{
- // read the next raw digit
- // returns kFALSE if there is no digit left
- fPrevSector = fSector;
- fPrevRow = fRow;
- fPrevPad = fPad;
- fPrevTime = fTime;
-
- while (fMyCount == 0) { // next trailer
- if (fMyPosition <= 0) { // next payload
- do {
- if (!fRawReader->ReadNextData(fMyData)) return kFALSE;
- } while (fRawReader->GetDataSize() == 0);
-
- fMyPosition = (fRawReader->GetDataSize() * 8) / 10;
- while (Get10BitWord(fMyData, fMyPosition-1) == 0x2AA) fMyPosition--;
- }
-
- if (fMyPosition > 0) {
- // read the trailer
- if (fMyPosition <= 4) {
- Error("Next", "could not read trailer");
- return kFALSE;
+ // Read one channel and return. Returns 0 when there's no more
+ // data.
+ Int_t l = 0;
+ static Int_t last = 0xFFFF; // 0xFFFF means signal is used
+ Bool_t next = kTRUE;
+ do {
+ Int_t signal = last;
+ if (last > 0x3FF) {
+ AliFMDDebug(30, ("Last is 0x%x, so reading a new word", last));
+ next = Next();
+ if(!next){
+ AliFMDDebug(15, ("Read word # %d (!next)", l));
+ addr = GetPrevHWAddress();
+ ddl = (GetPrevDDLNumber() < 0 ? UINT_MAX: GetPrevDDLNumber());
+ len = l+1; // Need to add one - l points to last valid index
+ last = signal;
+ break;
+ }
+ signal = GetSignal();
+ if (GetHWAddress() != GetPrevHWAddress() && GetPrevHWAddress() >= 0) {
+ AliFMDDebug(15, ("New hardware address, was 0x%x, now 0x%x",
+ GetPrevHWAddress(), GetHWAddress()));
+ addr = GetPrevHWAddress();
+ ddl = (GetPrevDDLNumber() < 0 ? UINT_MAX : GetPrevDDLNumber());
+ len = l+1; // Need to add one - l points to last valid index
+ last = signal;
+ break;
}
- fSector = Get10BitWord(fMyData, --fMyPosition);
- fRow = Get10BitWord(fMyData, --fMyPosition);
- fPad = Get10BitWord(fMyData, --fMyPosition);
- fMyCount = Get10BitWord(fMyData, --fMyPosition);
-
- fMyPosition -= (4 - (fMyCount % 4)) % 4; // skip fill words
- fMyBunchLength = 0;
-
- // Set the sample rate, based on the number of samples in the
- // channel.
- fSampleRate = (fMyCount - 2) / 128;
-#if 0
- std::cout << "Setting Sample rate to (" << fMyCount << " - 2)/"
- << 128 << "=" << fSampleRate << std::endl;
-#endif
}
- }
-
- if (fMyBunchLength == 0) {
- if (fMyPosition <= 0) {
- Error("Next", "could not read bunch length");
- return kFALSE;
+ // Sanity check - if the total bunch length is less than 1, then
+ // read until we get the next bunch.
+ Int_t b = GetTimeLength();
+ if (b < 1) {
+ AliWarning(Form("Bunch length %0d is less than 0 for "
+ "DDL %4d address 0x%03x",
+ b, ddl, addr));
+ last = 0xFFFF;
+ continue;
}
- fMyBunchLength = Get10BitWord(fMyData, --fMyPosition) - 2;
- fMyCount--;
-
- if (fMyPosition <= 0) {
- Error("Next", "could not read time bin");
- return kFALSE;
+ // Sanity check - if the current time is less than 0, then read
+ // until we get a new bunch.
+ Int_t t = GetTime();
+ if (t < 0) {
+ AliWarning(Form("Time %0d is less than 0 for DDL %4d address 0x%03x",
+ t, ddl, addr));
+ last = 0xFFFF;
+ continue;
}
- fTime = Get10BitWord(fMyData, --fMyPosition);
- fMyCount--;
- } else {
- fTime--;
- }
-
- if (fMyPosition <= 0) {
- Error("Next", "could not read sample amplitude");
- return kFALSE;
- }
- fSignal = Get10BitWord(fMyData, --fMyPosition);
- fMyCount--;
- fMyBunchLength--;
-
- return kTRUE;
+ l = TMath::Max(l, t);
+ data[t] = signal;
+ last = 0xFFFF;
+#if 0
+ AliFMDDebug(signal > 512 ? 1 : 0, ("Signal @ %d (%d) is %d",
+ time, t, data[t]));
+#endif
+ } while (next);
+ return next;
}
-//_____________________________________________________________________________
-UShort_t
-AliFMDRawStream::Get10BitWord(UChar_t* buffer, Int_t position) const
-{
-// return a word in a 10 bit array as an UShort_t
- Int_t iBit = position * 10;
- Int_t iByte = iBit / 8;
- Int_t shift = iBit % 8;
-// return ((buffer[iByte+1] * 256 + buffer[iByte]) >> shift) & 0x03FF;
-
- // recalculate the byte numbers and the shift because
- // the raw data is written as integers where the high bits are filled first
- // -> little endian is assumed here !
- Int_t iByteHigh = 4 * (iByte / 4) + 3 - (iByte % 4);
- iByte++;
- Int_t iByteLow = 4 * (iByte / 4) + 3 - (iByte % 4);
- shift = 6 - shift;
- return ((buffer[iByteHigh] * 256 + buffer[iByteLow]) >> shift) & 0x03FF;
-}
-#endif
//_____________________________________________________________________________
//
// EOF