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 "AliMUONDspHeader.h"
48 #include "AliMUONBlockHeader.h"
49 #include "AliMUONBusStruct.h"
50 #include "AliMUONDDLTracker.h"
51 #include "AliRawReader.h"
62 ClassImp(AliMUONRawStreamTrackerHP)
66 AliMUONRawStreamTrackerHP::AliMUONRawStreamTrackerHP() :
67 AliMUONVRawStreamTracker(),
71 fBuffer(new UChar_t[8192]),
72 fCurrentBusPatch(NULL),
80 /// Default constructor.
83 // Must set this flag to get all information about parity errors though
84 // the OnData method. OnError gets them either way.
85 fDecoder.ExitOnError(false);
86 fDecoder.SendDataOnParityError(true);
88 fDecoder.GetHandler().SetMaxStructs(
91 fDecoder.MaxBusPatches()
94 fDecoder.GetHandler().SetRawStream(this);
98 AliMUONRawStreamTrackerHP::AliMUONRawStreamTrackerHP(AliRawReader* rawReader) :
99 AliMUONVRawStreamTracker(rawReader),
103 fBuffer(new UChar_t[8192]),
104 fCurrentBusPatch(NULL),
112 /// Constructor with AliRawReader as argument.
115 // Must set this flag to get all information about parity errors though
116 // the OnData method. OnError gets them either way.
117 fDecoder.ExitOnError(false);
118 fDecoder.SendDataOnParityError(true);
120 fDecoder.GetHandler().SetMaxStructs(
121 fDecoder.MaxBlocks(),
123 fDecoder.MaxBusPatches()
126 fDecoder.GetHandler().SetRawStream(this);
130 AliMUONRawStreamTrackerHP::~AliMUONRawStreamTrackerHP()
133 /// Default destructor.
140 if (fDDLObject != NULL)
147 void AliMUONRawStreamTrackerHP::First()
149 /// Initialise or reset the iterator.
150 /// The first DDL will be found and decoded.
152 assert( GetReader() != NULL );
160 Bool_t AliMUONRawStreamTrackerHP::NextDDL()
162 /// Reading the next tracker DDL and decode the payload with the
163 /// high performance decoder.
164 /// \return kTRUE if the next DDL was successfully read and kFALSE otherwise.
166 assert( GetReader() != NULL );
168 // The temporary object if generated in GetDDLTracker, is now stale,
170 if (fDDLObject != NULL)
176 // Better to reset these pointers.
177 fCurrentBusPatch = NULL;
181 while (fDDL < GetMaxDDL())
183 GetReader()->Reset();
184 GetReader()->Select("MUONTRK", fDDL, fDDL); // Select the DDL file to be read.
185 if (GetReader()->ReadHeader()) break;
186 AliDebug(3, Form("Skipping DDL %d which does not seem to be there", fDDL+1));
190 // If we reach the end of the DDL list for this event then reset the
191 // DDL counter, mark the iteration as done and exit.
192 if (fDDL >= GetMaxDDL())
203 AliDebug(3, Form("DDL Number %d\n", fDDL));
205 Int_t dataSize = GetReader()->GetDataSize(); // in bytes
206 // Check if we have enough buffer space already in fBuffer. If we do then
207 // just continue reading otherwise we need to resize the buffer.
208 if (fBufferSize < dataSize)
218 fBuffer = new UChar_t[dataSize];
219 fBufferSize = dataSize;
221 catch (const std::bad_alloc&)
223 AliError("Could not allocate more buffer space. Cannot decode DDL.");
228 if (not GetReader()->ReadNext(fBuffer, dataSize))
234 Swap(reinterpret_cast<UInt_t*>(fBuffer), dataSize / sizeof(UInt_t)); // Swap needed for mac power pc.
240 // Since we might allocate memory inside OnNewBuffer in the event
241 // handler we need to trap any memory allocation exception to be robust.
242 result = fDecoder.Decode(fBuffer, dataSize);
243 fHadError = (result == true ? kFALSE : kTRUE);
245 catch (const std::bad_alloc&)
247 AliError("Could not allocate more buffer space. Cannot decode DDL.");
251 // Update the current bus patch pointers.
252 fCurrentBusPatch = fDecoder.GetHandler().FirstBusPatch();
253 if (fCurrentBusPatch != fDecoder.GetHandler().EndOfBusPatch())
255 fCurrentData = fCurrentBusPatch->GetData();
256 fEndOfData = fCurrentData + fCurrentBusPatch->GetDataCount();
260 // If the DDL did not have any bus patches then mark both fCurrentData
261 // and fEndOfData as NULL so that in Next() we are forced to find the
262 // first non empty DDL.
263 fCurrentData = fEndOfData = NULL;
266 fDDL++; // Remember to increment index to next DDL.
271 Bool_t AliMUONRawStreamTrackerHP::IsDone() const
273 /// Indicates whether the iteration is finished or not.
274 /// \return kTRUE if we already read all the digits and kFALSE if not.
280 Bool_t AliMUONRawStreamTrackerHP::Next(
281 Int_t& busPatchId, UShort_t& manuId, UChar_t& manuChannel,
285 /// Advance one step in the iteration. Returns false if finished.
286 /// [out] \param busPatchId This is filled with the bus patch ID of the digit.
287 /// [out] \param manuId This is filled with the MANU ID of the digit.
288 /// [out] \param manuChannel This is filled with the MANU channel ID of the digit.
289 /// [out] \param adc This is filled with the ADC signal value of the digit.
290 /// \return kTRUE if we read another digit and kFALSE if we have read all the
291 /// digits already, i.e. at the end of the iteration.
293 if (fCurrentData == NULL) return kFALSE;
296 // Check if we still have data to be returned for the current bus patch.
297 if (fCurrentData != fEndOfData)
299 busPatchId = fCurrentBusPatch->GetBusPatchId();
300 AliMUONTrackerDDLDecoderEventHandler::UnpackADC(*fCurrentData, manuId, manuChannel, adc);
306 // We hit the end of the current bus patch so check if we have any more
307 // bus patches to process for the current DDL. If we do, then increment
308 // the current bus patch, make sure it is not the last one and then try
309 // reading the first element again.
310 if (fCurrentBusPatch != fDecoder.GetHandler().EndOfBusPatch())
313 if (fCurrentBusPatch != fDecoder.GetHandler().EndOfBusPatch())
315 fCurrentData = fCurrentBusPatch->GetData();
316 fEndOfData = fCurrentData + fCurrentBusPatch->GetDataCount();
321 // This was the last bus patch in the DDL so read in the next one and
322 // try reading the first data element again.
323 // Note: fCurrentBusPatch is set inside NextDDL().
324 if (NextDDL()) goto retry;
330 AliMUONDDLTracker* AliMUONRawStreamTrackerHP::GetDDLTracker() const
332 /// Construct and return a pointer to the DDL payload object.
333 /// \return Pointer to internally constructed AliMUONDDLTracker object.
334 /// The object is owned by this class and should not be deleted
337 /// \note This method should not be used just to gain access to the DDL
338 /// payload, unless there is a good reason to have the AliMUONDDLTracker
339 /// object. For example, if you want to modify the data and then save it
340 /// to another DDL stream. Otherwise it can be an order of magnitude
341 /// faster to access the DDL headers and data with the GetBlockHeader,
342 /// GetDspHeader and GetBusPatch methods for example.
343 /// Refer to the MUONRawStreamTracker.C macro to see how to use the fast
344 /// decoder interface optimally.
346 if (fDDLObject != NULL) return fDDLObject;
348 fDDLObject = new AliMUONDDLTracker;
349 for (Int_t iBlock = 0; iBlock < (Int_t)GetBlockCount(); iBlock++)
351 AliMUONBlockHeader blockHeader;
352 AliMUONDspHeader dspHeader;
353 AliMUONBusStruct busPatch;
355 const AliBlockHeader* bh = GetBlockHeader(iBlock);
356 // Copy block header and add it to the DDL object.
357 memcpy(blockHeader.GetHeader(), bh->GetHeader(), sizeof(AliMUONBlockHeaderStruct));
358 fDDLObject->AddBlkHeader(blockHeader);
360 for (Int_t iDsp = 0; iDsp < (Int_t)bh->GetDspCount(); iDsp++)
362 const AliDspHeader* dh = bh->GetDspHeader(iDsp);
363 // Copy DSP header and add it to the DDL object.
364 memcpy(dspHeader.GetHeader(), dh->GetHeader(), sizeof(AliMUONDSPHeaderStruct));
365 fDDLObject->AddDspHeader(dspHeader, iBlock);
367 const AliBusPatch* bp = dh->GetFirstBusPatch();
370 // Copy bus patch header, data and add everything into DDL object.
371 memcpy(busPatch.GetHeader(), bp->GetHeader(), sizeof(AliMUONBusPatchHeaderStruct));
372 busPatch.SetAlloc(bp->GetLength());
373 memcpy(busPatch.GetData(), bp->GetData(), bp->GetDataCount()*sizeof(UInt_t));
374 busPatch.SetBlockId(iBlock);
375 busPatch.SetDspId(iDsp);
376 fDDLObject->AddBusPatch(busPatch, iBlock, iDsp);
386 void AliMUONRawStreamTrackerHP::SetMaxBlock(Int_t blk)
388 /// Set maximum number of blocks per DDL allowed.
389 fDecoder.MaxBlocks( (UInt_t) blk );
391 fDecoder.GetHandler().SetMaxStructs(
392 fDecoder.MaxBlocks(),
394 fDecoder.MaxBusPatches()
399 void AliMUONRawStreamTrackerHP::SetMaxDsp(Int_t dsp)
401 /// Set maximum number of Dsp per block allowed.
402 fDecoder.MaxDSPs( (UInt_t) dsp );
404 fDecoder.GetHandler().SetMaxStructs(
405 fDecoder.MaxBlocks(),
407 fDecoder.MaxBusPatches()
412 void AliMUONRawStreamTrackerHP::SetMaxBus(Int_t bus)
414 /// Set maximum number of Buspatch per Dsp allowed.
415 fDecoder.MaxBusPatches( (UInt_t) bus );
417 fDecoder.GetHandler().SetMaxStructs(
418 fDecoder.MaxBlocks(),
420 fDecoder.MaxBusPatches()
424 ///////////////////////////////////////////////////////////////////////////////
426 void AliMUONRawStreamTrackerHP::AliBlockHeader::Print() const
428 /// Print header to screen.
430 cout << "CRT info" << endl;
433 cout << "Header is NULL" << endl;
436 cout << "DataKey: 0x" << hex << fHeader->fDataKey << dec << endl;
437 cout << "TotalLength: " << fHeader->fTotalLength << endl;
438 cout << "Length: " << fHeader->fLength << endl;
439 cout << "DspId: " << fHeader->fDSPId << endl;
440 cout << "L0Trigger: " << fHeader->fL0Trigger << endl;
441 cout << "MiniEventId: " << fHeader->fMiniEventId<< endl;
442 cout << "EventId1: " << fHeader->fEventId1 << endl;
443 cout << "EventId2: " << fHeader->fEventId2 << endl;
447 void AliMUONRawStreamTrackerHP::AliDspHeader::Print() const
449 /// Print header to screen.
451 cout << "FRT info" << endl;
454 cout << "Header is NULL" << endl;
457 cout << "DataKey: 0x" << hex << fHeader->fDataKey << dec << endl;
458 cout << "TotalLength: " << fHeader->fTotalLength << endl;
459 cout << "Length : " << fHeader->fLength << endl;
460 cout << "DspId: " << fHeader->fDSPId << endl;
461 cout << "BlkL1ATrigger: " << fHeader->fBlkL1ATrigger << endl;
462 cout << "MiniEventId: " << fHeader->fMiniEventId << endl;
463 cout << "L1ATrigger: " << fHeader->fL1ATrigger << endl;
464 cout << "L1RTrigger: " << fHeader->fL1RTrigger << endl;
465 cout << "PaddingWord: " << fHeader->fPaddingWord << endl;
466 cout << "ErrorWord: " << fHeader->fErrorWord << endl;
470 void AliMUONRawStreamTrackerHP::AliBusPatch::Print(const Option_t* opt) const
472 /// Print header to screen.
473 cout << "Bus patch info" << endl;
476 cout << "Header is NULL" << endl;
479 cout << "DataKey: 0x" << hex << fHeader->fDataKey << dec << endl;
480 cout << "fTotalLength: " << fHeader->fTotalLength << endl;
481 cout << "fLength: " << fHeader->fLength << endl;
482 cout << "fBusPatchId: " << fHeader->fBusPatchId << endl;
484 if (TString(opt).Contains("all"))
486 for (UInt_t i = 0; i < fHeader->fLength; ++i)
487 cout << "Data["<< i << "] = " << fData[i] << endl;
491 ///////////////////////////////////////////////////////////////////////////////
493 AliMUONRawStreamTrackerHP::AliDecoderEventHandler::AliDecoderEventHandler() :
500 fEndOfBusPatches(NULL),
502 fParityOk(new Bool_t[8192]),
505 fCurrentBusPatch(NULL),
506 fCurrentParityOkFlag(NULL),
512 /// Default constructor initialises the internal parity flags buffer to
513 /// store 8192 elements. This array will grow dynamically if needed.
517 AliMUONRawStreamTrackerHP::AliDecoderEventHandler::~AliDecoderEventHandler()
519 /// Default destructor cleans up the allocated memory.
521 if (fParityOk != NULL) delete [] fParityOk;
522 if (fBlocks != NULL) delete [] fBlocks;
523 if (fDSPs != NULL) delete [] fDSPs;
524 if (fBusPatches != NULL) delete [] fBusPatches;
528 void AliMUONRawStreamTrackerHP::AliDecoderEventHandler::SetMaxStructs(
529 UInt_t maxBlocks, UInt_t maxDsps, UInt_t maxBusPatches
532 /// Sets the maximum number of structures allowed.
534 // Start by clearing the current arrays.
545 if (fBusPatches != NULL)
547 delete [] fBusPatches;
550 fCurrentBlock = NULL;
552 fCurrentBusPatch = NULL;
554 // Allocate new memory.
555 fBlocks = new AliBlockHeader[maxBlocks];
556 fDSPs = new AliDspHeader[maxBlocks*maxDsps];
557 fBusPatches = new AliBusPatch[maxBlocks*maxDsps*maxBusPatches];
558 fEndOfBusPatches = fBusPatches;
562 void AliMUONRawStreamTrackerHP::AliDecoderEventHandler::OnNewBuffer(
563 const void* buffer, UInt_t bufferSize
566 /// This is called by the high performance decoder when a new DDL payload
567 /// is about to be decoded.
568 /// \param buffer The pointer to the buffer storing the DDL payload.
569 /// \param bufferSize The size of the buffer in bytes.
571 assert( fRawStream != NULL );
573 // remember the start of the buffer to be used in OnError.
574 fBufferStart = buffer;
576 // Reset error counters.
581 // Check if we will have enough space in the fParityOk array.
582 // If we do not then we need to resize the array.
583 // bufferSize / sizeof(UInt_t) will be a safe over estimate of the
584 // number of channels that we will find.
585 UInt_t maxChannelsPossible = bufferSize / sizeof(UInt_t);
586 if (maxChannelsPossible > fMaxChannels)
588 if (fParityOk != NULL)
594 fParityOk = new Bool_t[maxChannelsPossible];
595 fMaxChannels = maxChannelsPossible;
598 // Reset the current pointers which will be used to track where we need to
599 // fill fBlocks, fDSPs, fBusPatches and the parity flag. We have to subtract
600 // one space because we will increment the pointer the first time in the
602 fCurrentBlock = fBlocks-1;
603 fCurrentDSP = fDSPs-1;
604 fCurrentBusPatch = fBusPatches-1;
605 fCurrentParityOkFlag = fParityOk-1;
610 void AliMUONRawStreamTrackerHP::AliDecoderEventHandler::OnError(
611 ErrorCode error, const void* location
614 /// This is called by the high performance decoder when a error occurs
615 /// when trying to decode the DDL payload. This indicates corruption in
616 /// the data. This method converts the error code to a descriptive message
617 /// and logs this with the raw reader.
618 /// \param error The error code indicating the problem.
619 /// \param location A pointer to the location within the DDL payload buffer
620 /// being decoded where the problem with the data was found.
622 assert( fRawStream != NULL );
623 assert( fRawStream->GetReader() != NULL );
625 Char_t* message = NULL;
633 "Glitch error detected in DSP %d, skipping event ",
634 fCurrentBlock->GetDspId()
636 fRawStream->GetReader()->AddMajorErrorLog(error, message);
639 case kBadPaddingWord:
641 // We subtract 1 from the current numbers of blocks, DSPs
642 // and bus patches to get the indices.
644 "Padding word error for iBlock %d, iDsp %d, iBus %d\n",
646 fCurrentBlock->GetDspCount()-1,
647 fCurrentDSP->GetBusPatchCount()-1
649 fRawStream->GetReader()->AddMinorErrorLog(error, message);
654 // location points to the incorrect data word and
655 // fCurrentBusPatch->GetData() returns a pointer to the start of
656 // bus patches data, so the difference divided by 4 gives the 32
658 word = ((unsigned long)location - (unsigned long)fCurrentBusPatch->GetData())
661 "Parity error in word %d for manuId %d and channel %d in buspatch %d\n",
663 fCurrentBusPatch->GetManuId(word),
664 fCurrentBusPatch->GetChannelId(word),
665 fCurrentBusPatch->GetBusPatchId()
667 fRawStream->GetReader()->AddMinorErrorLog(error, message);
672 "%s (At byte %d in DDL.)",
673 ErrorCodeToMessage(error),
674 (unsigned long)location - (unsigned long)fBufferStart + sizeof(AliRawDataHeader)
676 fRawStream->GetReader()->AddMajorErrorLog(error, message);
683 "AliMUONRawStreamTrackerHP::AliDecoderEventHandler",