1 /**************************************************************************
2 * This file is property of and copyright by the ALICE HLT Project *
3 * All rights reserved. *
6 * Artur Szostak <artursz@iafrica.com> *
8 * Permission to use, copy, modify and distribute this software and its *
9 * documentation strictly for non-commercial purposes is hereby granted *
10 * without fee, provided that the above copyright notice appears in all *
11 * copies and that both the copyright notice and this permission notice *
12 * appear in the supporting documentation. The authors make no claims *
13 * about the suitability of this software for any purpose. It is *
14 * provided "as is" without express or implied warranty. *
15 **************************************************************************/
20 /// \file AliMUONRawStreamTrackerHP.cxx
21 /// \author Artur Szostak <artursz@iafrica.com>
23 /// \brief Implementation of the the high performance decoder AliMUONRawStreamTrackerHP.
26 //-----------------------------------------------------------------------------
28 /// \class AliMUONRawStreamTrackerHP
29 /// \brief A high performance stream decoder for muon tracking DDL streams.
31 /// This is the raw stream class which interfaces between the high performance
32 /// core decoder and the AliRawReader class.
33 /// To gain the most out of the decoder, the Next() method which returns batches
34 /// of decoded digit / channel information should be used. That is:
36 /// const AliBusPatch* Next();
39 /// This decoder tries to implement as similar an interface as possible to
40 /// AliMUONRawStreamTracker where possible. However certain constructs which
41 /// would slow us down too much are avoided.
43 /// \author Artur Szostak <artursz@iafrica.com>
44 //-----------------------------------------------------------------------------
46 #include "AliMUONRawStreamTrackerHP.h"
47 #include "AliRawReader.h"
58 ClassImp(AliMUONRawStreamTrackerHP)
62 AliMUONRawStreamTrackerHP::AliMUONRawStreamTrackerHP() :
63 AliMUONVRawStreamTracker(),
67 fBuffer(new UChar_t[8192]),
68 fCurrentBusPatch(NULL),
75 /// Default constructor.
78 // Must set this flag to get all information about parity errors though
79 // the OnData method. OnError gets them either way.
80 fDecoder.ExitOnError(false);
81 fDecoder.SendDataOnParityError(true);
83 fDecoder.GetHandler().SetMaxStructs(
86 fDecoder.MaxBusPatches()
89 fDecoder.GetHandler().SetRawStream(this);
93 AliMUONRawStreamTrackerHP::AliMUONRawStreamTrackerHP(AliRawReader* rawReader) :
94 AliMUONVRawStreamTracker(rawReader),
98 fBuffer(new UChar_t[8192]),
99 fCurrentBusPatch(NULL),
106 /// Constructor with AliRawReader as argument.
109 // Must set this flag to get all information about parity errors though
110 // the OnData method. OnError gets them either way.
111 fDecoder.ExitOnError(false);
112 fDecoder.SendDataOnParityError(true);
114 fDecoder.GetHandler().SetMaxStructs(
115 fDecoder.MaxBlocks(),
117 fDecoder.MaxBusPatches()
120 fDecoder.GetHandler().SetRawStream(this);
124 AliMUONRawStreamTrackerHP::~AliMUONRawStreamTrackerHP()
127 /// Default destructor.
137 void AliMUONRawStreamTrackerHP::First()
139 /// Initialise or reset the iterator.
140 /// The first DDL will be found and decoded.
142 assert( GetReader() != NULL );
150 Bool_t AliMUONRawStreamTrackerHP::NextDDL()
152 /// Reading the next tracker DDL and decode the payload with the
153 /// high performance decoder.
154 /// \return kTRUE if the next DDL was successfully read and kFALSE otherwise.
156 assert( GetReader() != NULL );
158 // Better to reset these pointers.
159 fCurrentBusPatch = NULL;
163 while (fDDL < GetMaxDDL())
165 GetReader()->Reset();
166 GetReader()->Select("MUONTRK", fDDL, fDDL); // Select the DDL file to be read.
167 if (GetReader()->ReadHeader()) break;
168 AliDebug(3, Form("Skipping DDL %d which does not seem to be there", fDDL+1));
172 // If we reach the end of the DDL list for this event then reset the
173 // DDL counter, mark the iteration as done and exit.
174 if (fDDL >= GetMaxDDL())
185 AliDebug(3, Form("DDL Number %d\n", fDDL));
187 Int_t dataSize = GetReader()->GetDataSize(); // in bytes
188 // Check if we have enough buffer space already in fBuffer. If we do then
189 // just continue reading otherwise we need to resize the buffer.
190 if (fBufferSize < dataSize)
200 fBuffer = new UChar_t[dataSize];
201 fBufferSize = dataSize;
203 catch (const std::bad_alloc&)
205 AliError("Could not allocate more buffer space. Cannot decode DDL.");
210 if (not GetReader()->ReadNext(fBuffer, dataSize))
216 Swap(reinterpret_cast<UInt_t*>(fBuffer), dataSize / sizeof(UInt_t)); // Swap needed for mac power pc.
222 // Since we might allocate memory inside OnNewBuffer in the event
223 // handler we need to trap any memory allocation exception to be robust.
224 result = fDecoder.Decode(fBuffer, dataSize);
225 fHadError = (result == true ? kFALSE : kTRUE);
227 catch (const std::bad_alloc&)
229 AliError("Could not allocate more buffer space. Cannot decode DDL.");
233 // Update the current bus patch pointers.
234 fCurrentBusPatch = fDecoder.GetHandler().FirstBusPatch();
235 if (fCurrentBusPatch != fDecoder.GetHandler().EndOfBusPatch())
237 fCurrentData = fCurrentBusPatch->GetData();
238 fEndOfData = fCurrentData + fCurrentBusPatch->GetDataCount();
242 // If the DDL did not have any bus patches then mark both fCurrentData
243 // and fEndOfData as NULL so that in Next() we are forced to find the
244 // first non empty DDL.
245 fCurrentData = fEndOfData = NULL;
248 fDDL++; // Remember to increment index to next DDL.
253 Bool_t AliMUONRawStreamTrackerHP::IsDone() const
255 /// Indicates whether the iteration is finished or not.
256 /// \return kTRUE if we already read all the digits and kFALSE if not.
262 Bool_t AliMUONRawStreamTrackerHP::Next(
263 Int_t& busPatchId, UShort_t& manuId, UChar_t& manuChannel,
267 /// Advance one step in the iteration. Returns false if finished.
268 /// [out] \param busPatchId This is filled with the bus patch ID of the digit.
269 /// [out] \param manuId This is filled with the MANU ID of the digit.
270 /// [out] \param manuChannel This is filled with the MANU channel ID of the digit.
271 /// [out] \param adc This is filled with the ADC signal value of the digit.
272 /// \return kTRUE if we read another digit and kFALSE if we have read all the
273 /// digits already, i.e. at the end of the iteration.
275 if (fCurrentData == NULL) return kFALSE;
278 // Check if we still have data to be returned for the current bus patch.
279 if (fCurrentData != fEndOfData)
281 busPatchId = fCurrentBusPatch->GetBusPatchId();
282 AliMUONTrackerDDLDecoderEventHandler::UnpackADC(*fCurrentData, manuId, manuChannel, adc);
288 // We hit the end of the current bus patch so check if we have any more
289 // bus patches to process for the current DDL. If we do, then increment
290 // the current bus patch, make sure it is not the last one and then try
291 // reading the first element again.
292 if (fCurrentBusPatch != fDecoder.GetHandler().EndOfBusPatch())
295 if (fCurrentBusPatch != fDecoder.GetHandler().EndOfBusPatch())
297 fCurrentData = fCurrentBusPatch->GetData();
298 fEndOfData = fCurrentData + fCurrentBusPatch->GetDataCount();
303 // This was the last bus patch in the DDL so read in the next one and
304 // try reading the first data element again.
305 // Note: fCurrentBusPatch is set inside NextDDL().
306 if (NextDDL()) goto retry;
312 void AliMUONRawStreamTrackerHP::SetMaxBlock(Int_t blk)
314 /// Set maximum number of blocks per DDL allowed.
315 fDecoder.MaxBlocks( (UInt_t) blk );
317 fDecoder.GetHandler().SetMaxStructs(
318 fDecoder.MaxBlocks(),
320 fDecoder.MaxBusPatches()
325 void AliMUONRawStreamTrackerHP::SetMaxDsp(Int_t dsp)
327 /// Set maximum number of Dsp per block allowed.
328 fDecoder.MaxDSPs( (UInt_t) dsp );
330 fDecoder.GetHandler().SetMaxStructs(
331 fDecoder.MaxBlocks(),
333 fDecoder.MaxBusPatches()
338 void AliMUONRawStreamTrackerHP::SetMaxBus(Int_t bus)
340 /// Set maximum number of Buspatch per Dsp allowed.
341 fDecoder.MaxBusPatches( (UInt_t) bus );
343 fDecoder.GetHandler().SetMaxStructs(
344 fDecoder.MaxBlocks(),
346 fDecoder.MaxBusPatches()
350 ///////////////////////////////////////////////////////////////////////////////
352 void AliMUONRawStreamTrackerHP::AliBlockHeader::Print() const
354 /// Print header to screen.
356 cout << "CRT info" << endl;
359 cout << "Header is NULL" << endl;
362 cout << "DataKey: 0x" << hex << fHeader->fDataKey << dec << endl;
363 cout << "TotalLength: " << fHeader->fTotalLength << endl;
364 cout << "Length: " << fHeader->fLength << endl;
365 cout << "DspId: " << fHeader->fDSPId << endl;
366 cout << "L0Trigger: " << fHeader->fL0Trigger << endl;
367 cout << "MiniEventId: " << fHeader->fMiniEventId<< endl;
368 cout << "EventId1: " << fHeader->fEventId1 << endl;
369 cout << "EventId2: " << fHeader->fEventId2 << endl;
373 void AliMUONRawStreamTrackerHP::AliDspHeader::Print() const
375 /// Print header to screen.
377 cout << "FRT info" << endl;
380 cout << "Header is NULL" << endl;
383 cout << "DataKey: 0x" << hex << fHeader->fDataKey << dec << endl;
384 cout << "TotalLength: " << fHeader->fTotalLength << endl;
385 cout << "Length : " << fHeader->fLength << endl;
386 cout << "DspId: " << fHeader->fDSPId << endl;
387 cout << "BlkL1ATrigger: " << fHeader->fBlkL1ATrigger << endl;
388 cout << "MiniEventId: " << fHeader->fMiniEventId << endl;
389 cout << "L1ATrigger: " << fHeader->fL1ATrigger << endl;
390 cout << "L1RTrigger: " << fHeader->fL1RTrigger << endl;
391 cout << "PaddingWord: " << fHeader->fPaddingWord << endl;
392 cout << "ErrorWord: " << fHeader->fErrorWord << endl;
396 void AliMUONRawStreamTrackerHP::AliBusPatch::Print(const Option_t* opt) const
398 /// Print header to screen.
399 cout << "Bus patch info" << endl;
402 cout << "Header is NULL" << endl;
405 cout << "DataKey: 0x" << hex << fHeader->fDataKey << dec << endl;
406 cout << "fTotalLength: " << fHeader->fTotalLength << endl;
407 cout << "fLength: " << fHeader->fLength << endl;
408 cout << "fBusPatchId: " << fHeader->fBusPatchId << endl;
410 if (TString(opt).Contains("all"))
412 for (UInt_t i = 0; i < fHeader->fLength; ++i)
413 cout << "Data["<< i << "] = " << fData[i] << endl;
417 ///////////////////////////////////////////////////////////////////////////////
419 AliMUONRawStreamTrackerHP::AliDecoderEventHandler::AliDecoderEventHandler() :
426 fEndOfBusPatches(NULL),
428 fParityOk(new Bool_t[8192]),
431 fCurrentBusPatch(NULL),
432 fCurrentParityOkFlag(NULL),
438 /// Default constructor initialises the internal parity flags buffer to
439 /// store 8192 elements. This array will grow dynamically if needed.
443 AliMUONRawStreamTrackerHP::AliDecoderEventHandler::~AliDecoderEventHandler()
445 /// Default destructor cleans up the allocated memory.
447 if (fParityOk != NULL) delete [] fParityOk;
448 if (fBlocks != NULL) delete [] fBlocks;
449 if (fDSPs != NULL) delete [] fDSPs;
450 if (fBusPatches != NULL) delete [] fBusPatches;
454 void AliMUONRawStreamTrackerHP::AliDecoderEventHandler::SetMaxStructs(
455 UInt_t maxBlocks, UInt_t maxDsps, UInt_t maxBusPatches
458 /// Sets the maximum number of structures allowed.
460 // Start by clearing the current arrays.
471 if (fBusPatches != NULL)
473 delete [] fBusPatches;
476 fCurrentBlock = NULL;
478 fCurrentBusPatch = NULL;
480 // Allocate new memory.
481 fBlocks = new AliBlockHeader[maxBlocks];
482 fDSPs = new AliDspHeader[maxBlocks*maxDsps];
483 fBusPatches = new AliBusPatch[maxBlocks*maxDsps*maxBusPatches];
484 fEndOfBusPatches = fBusPatches;
488 void AliMUONRawStreamTrackerHP::AliDecoderEventHandler::OnNewBuffer(
489 const void* buffer, UInt_t bufferSize
492 /// This is called by the high performance decoder when a new DDL payload
493 /// is about to be decoded.
494 /// \param buffer The pointer to the buffer storing the DDL payload.
495 /// \param bufferSize The size of the buffer in bytes.
497 assert( fRawStream != NULL );
499 // remember the start of the buffer to be used in OnError.
500 fBufferStart = buffer;
502 // Reset error counters.
507 // Check if we will have enough space in the fParityOk array.
508 // If we do not then we need to resize the array.
509 // bufferSize / sizeof(UInt_t) will be a safe over estimate of the
510 // number of channels that we will find.
511 UInt_t maxChannelsPossible = bufferSize / sizeof(UInt_t);
512 if (maxChannelsPossible > fMaxChannels)
514 if (fParityOk != NULL)
520 fParityOk = new Bool_t[maxChannelsPossible];
521 fMaxChannels = maxChannelsPossible;
524 // Reset the current pointers which will be used to track where we need to
525 // fill fBlocks, fDSPs, fBusPatches and the parity flag. We have to subtract
526 // one space because we will increment the pointer the first time in the
528 fCurrentBlock = fBlocks-1;
529 fCurrentDSP = fDSPs-1;
530 fCurrentBusPatch = fBusPatches-1;
531 fCurrentParityOkFlag = fParityOk-1;
536 void AliMUONRawStreamTrackerHP::AliDecoderEventHandler::OnError(
537 ErrorCode error, const void* location
540 /// This is called by the high performance decoder when a error occurs
541 /// when trying to decode the DDL payload. This indicates corruption in
542 /// the data. This method converts the error code to a descriptive message
543 /// and logs this with the raw reader.
544 /// \param error The error code indicating the problem.
545 /// \param location A pointer to the location within the DDL payload buffer
546 /// being decoded where the problem with the data was found.
548 assert( fRawStream != NULL );
549 assert( fRawStream->GetReader() != NULL );
551 Char_t* message = NULL;
559 "Glitch error detected in DSP %d, skipping event ",
560 fCurrentBlock->GetDspId()
562 fRawStream->GetReader()->AddMajorErrorLog(error, message);
565 case kBadPaddingWord:
567 // We subtract 1 from the current numbers of blocks, DSPs
568 // and bus patches to get the indices.
570 "Padding word error for iBlock %d, iDsp %d, iBus %d\n",
572 fCurrentBlock->GetDspCount()-1,
573 fCurrentDSP->GetBusPatchCount()-1
575 fRawStream->GetReader()->AddMinorErrorLog(error, message);
580 // location points to the incorrect data word and
581 // fCurrentBusPatch->GetData() returns a pointer to the start of
582 // bus patches data, so the difference divided by 4 gives the 32
584 word = ((unsigned long)location - (unsigned long)fCurrentBusPatch->GetData())
587 "Parity error in word %d for manuId %d and channel %d in buspatch %d\n",
589 fCurrentBusPatch->GetManuId(word),
590 fCurrentBusPatch->GetChannelId(word),
591 fCurrentBusPatch->GetBusPatchId()
593 fRawStream->GetReader()->AddMinorErrorLog(error, message);
598 "%s (At byte %d in DDL.)",
599 ErrorCodeToMessage(error),
600 (unsigned long)location - (unsigned long)fBufferStart + sizeof(AliRawDataHeader)
602 fRawStream->GetReader()->AddMajorErrorLog(error, message);
609 "AliMUONRawStreamTrackerHP::AliDecoderEventHandler",