]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - MUON/AliMUONRawStreamTrackerHP.cxx
Renamed output file to Vertex.Performance.root
[u/mrichter/AliRoot.git] / MUON / AliMUONRawStreamTrackerHP.cxx
index 783a8293286b025991a69266de39b7be3eb40763..aa82a6ee5768d75ed94eb3fae0a5134dba4d1f92 100644 (file)
 //-----------------------------------------------------------------------------
 
 #include "AliMUONRawStreamTrackerHP.h"
+#include "AliMUONTrackerDDLDecoder.h"
+#include "AliMUONDspHeader.h"
+#include "AliMUONBlockHeader.h"
+#include "AliMUONBusStruct.h"
+#include "AliMUONDDLTracker.h"
 #include "AliRawReader.h"
 #include "AliLog.h"
 #include <cassert>
 #include <iostream>
-#include <iomanip>
 using std::cout;
 using std::endl;
 using std::hex;
@@ -58,18 +62,25 @@ using std::dec;
 ClassImp(AliMUONRawStreamTrackerHP)
 /// \endcond
 
+const Int_t AliMUONRawStreamTrackerHP::fgkMaxDDL = 20;
 
 AliMUONRawStreamTrackerHP::AliMUONRawStreamTrackerHP() :
-       AliMUONVRawStreamTracker(),
+       TObject(),
+       fEnableErrorLogger(kFALSE),
        fDecoder(),
        fDDL(0),
        fBufferSize(8192),
        fBuffer(new UChar_t[8192]),
-       fCurrentBusPatch(NULL),
-       fCurrentData(NULL),
-       fEndOfData(NULL),
+       fkCurrentBusPatch(NULL),
+       fkCurrentData(NULL),
+       fkEndOfData(NULL),
        fHadError(kFALSE),
-       fDone(kFALSE)
+       fDone(kFALSE),
+       fDDLObject(NULL),
+       fTotalNumberOfGlitchErrors(0),
+       fTotalNumberOfParityErrors(0),
+       fTotalNumberOfPaddingErrors(0),
+       fTotalNumberOfTokenLostErrors(0)
 {
        ///
        /// Default constructor.
@@ -85,22 +96,26 @@ AliMUONRawStreamTrackerHP::AliMUONRawStreamTrackerHP() :
                        fDecoder.MaxDSPs(),
                        fDecoder.MaxBusPatches()
                );
-
-       fDecoder.GetHandler().SetRawStream(this);
 }
 
 
 AliMUONRawStreamTrackerHP::AliMUONRawStreamTrackerHP(AliRawReader* rawReader) :
-       AliMUONVRawStreamTracker(rawReader),
+       TObject(),
+       fEnableErrorLogger(kFALSE),
        fDecoder(),
        fDDL(0),
        fBufferSize(8192),
        fBuffer(new UChar_t[8192]),
-       fCurrentBusPatch(NULL),
-       fCurrentData(NULL),
-       fEndOfData(NULL),
+       fkCurrentBusPatch(NULL),
+       fkCurrentData(NULL),
+       fkEndOfData(NULL),
        fHadError(kFALSE),
-       fDone(kFALSE)
+       fDone(kFALSE),
+       fDDLObject(NULL),
+       fTotalNumberOfGlitchErrors(0),
+       fTotalNumberOfParityErrors(0),
+       fTotalNumberOfPaddingErrors(0),
+       fTotalNumberOfTokenLostErrors(0)
 {
        ///
        /// Constructor with AliRawReader as argument.
@@ -117,7 +132,7 @@ AliMUONRawStreamTrackerHP::AliMUONRawStreamTrackerHP(AliRawReader* rawReader) :
                        fDecoder.MaxBusPatches()
                );
        
-       fDecoder.GetHandler().SetRawStream(this);
+       fDecoder.GetHandler().SetReader(rawReader);
 }
 
 
@@ -131,6 +146,10 @@ AliMUONRawStreamTrackerHP::~AliMUONRawStreamTrackerHP()
        {
                delete [] fBuffer;
        }
+       if (fDDLObject != NULL)
+       {
+               delete fDDLObject;
+       }
 }
 
 
@@ -144,6 +163,10 @@ void AliMUONRawStreamTrackerHP::First()
        fDDL = 0;
        fDone = kFALSE;
        NextDDL();
+       fTotalNumberOfGlitchErrors = 0;
+       fTotalNumberOfPaddingErrors = 0;
+       fTotalNumberOfParityErrors = 0;
+       fTotalNumberOfTokenLostErrors = 0;
 }
 
 
@@ -155,6 +178,19 @@ Bool_t AliMUONRawStreamTrackerHP::NextDDL()
 
        assert( GetReader() != NULL );
        
+       // The temporary object if generated in GetDDLTracker, is now stale,
+       // so delete it.
+       if (fDDLObject != NULL)
+       {
+               delete fDDLObject;
+               fDDLObject = NULL;
+       }
+       
+       // Better to reset these pointers.
+       fkCurrentBusPatch = NULL;
+       fkCurrentData = NULL;
+       fkEndOfData = NULL;
+       
        while (fDDL < GetMaxDDL())
        {
                GetReader()->Reset();
@@ -165,7 +201,7 @@ Bool_t AliMUONRawStreamTrackerHP::NextDDL()
        }
 
        // If we reach the end of the DDL list for this event then reset the
-       // DDL counter, mark the iteration as done and 
+       // DDL counter, mark the iteration as done and exit.
        if (fDDL >= GetMaxDDL())
        {
                fDDL = 0;
@@ -208,7 +244,7 @@ Bool_t AliMUONRawStreamTrackerHP::NextDDL()
        }
        
 #ifndef R__BYTESWAP
-       Swap(reinterpret_cast<UInt_t*>fBuffer, dataSize / sizeof(UInt_t)); // Swap needed for mac power pc.
+       Swap(reinterpret_cast<UInt_t*>(fBuffer), dataSize / sizeof(UInt_t)); // Swap needed for mac power pc.
 #endif
        
        bool result = false;
@@ -218,6 +254,10 @@ Bool_t AliMUONRawStreamTrackerHP::NextDDL()
                // handler we need to trap any memory allocation exception to be robust.
                result = fDecoder.Decode(fBuffer, dataSize);
                fHadError = (result == true ? kFALSE : kTRUE);
+               fTotalNumberOfGlitchErrors += fDecoder.GetHandler().GlitchErrorCount();
+               fTotalNumberOfParityErrors += fDecoder.GetHandler().ParityErrorCount();
+               fTotalNumberOfPaddingErrors += fDecoder.GetHandler().PaddingErrorCount();
+               fTotalNumberOfTokenLostErrors += fDecoder.GetHandler().TokenLostCount();
        }
        catch (const std::bad_alloc&)
        {
@@ -226,22 +266,22 @@ Bool_t AliMUONRawStreamTrackerHP::NextDDL()
        }
 
        // Update the current bus patch pointers.
-       fCurrentBusPatch = fDecoder.GetHandler().FirstBusPatch();
-       if (fCurrentBusPatch != fDecoder.GetHandler().EndOfBusPatch())
+       fkCurrentBusPatch = fDecoder.GetHandler().FirstBusPatch();
+       if (fkCurrentBusPatch != fDecoder.GetHandler().EndOfBusPatch())
        {
-               fCurrentData = fCurrentBusPatch->GetData();
-               fEndOfData = fCurrentData + fCurrentBusPatch->GetDataCount();
+               fkCurrentData = fkCurrentBusPatch->GetData();
+               fkEndOfData = fkCurrentData + fkCurrentBusPatch->GetDataCount();
        }
        else
        {
                // If the DDL did not have any bus patches then mark both fCurrentData
                // and fEndOfData as NULL so that in Next() we are forced to find the
                // first non empty DDL.
-               fCurrentData = fEndOfData = NULL;
+               fkCurrentData = fkEndOfData = NULL;
        }
 
        fDDL++; // Remember to increment index to next DDL.
-       return result;
+       return kTRUE;
 }
 
 
@@ -254,26 +294,31 @@ Bool_t AliMUONRawStreamTrackerHP::IsDone() const
 }
 
 
-Bool_t AliMUONRawStreamTrackerHP::Next(
-               Int_t& busPatchId, UShort_t& manuId, UChar_t& manuChannel,
-               UShort_t& adc
-       )
+Bool_t AliMUONRawStreamTrackerHP::Next(Int_t& busPatchId, 
+                                       UShort_t& manuId, 
+                                       UChar_t& manuChannel,
+                                       UShort_t& adc, 
+                                       Bool_t skipParityErrors)
 {
        /// Advance one step in the iteration. Returns false if finished.
        /// [out] \param busPatchId  This is filled with the bus patch ID of the digit.
        /// [out] \param manuId      This is filled with the MANU ID of the digit.
        /// [out] \param manuChannel This is filled with the MANU channel ID of the digit.
        /// [out] \param adc         This is filled with the ADC signal value of the digit.
+       /// [in] \param skipParityErrors If this is kTRUE, we'll skip the buspatches that
+       ///                              have some parity errors
        /// \return kTRUE if we read another digit and kFALSE if we have read all the
        ///    digits already, i.e. at the end of the iteration.
        
+       if (fkCurrentData == NULL) return kFALSE;
+       
 retry:
        // Check if we still have data to be returned for the current bus patch.
-       if (fCurrentData != fEndOfData)
+       if (fkCurrentData != fkEndOfData)
        {
-               busPatchId = fCurrentBusPatch->GetBusPatchId();
-               AliMUONTrackerDDLDecoderEventHandler::UnpackADC(*fCurrentData, manuId, manuChannel, adc);
-               fCurrentData++;
+               busPatchId = fkCurrentBusPatch->GetBusPatchId();
+               AliMUONTrackerDDLDecoderEventHandler::UnpackADC(*fkCurrentData, manuId, manuChannel, adc);
+               fkCurrentData++;
                return kTRUE;
        }
        else
@@ -282,13 +327,22 @@ retry:
                // bus patches to process for the current DDL. If we do, then increment
                // the current bus patch, make sure it is not the last one and then try
                // reading the first element again.
-               if (fCurrentBusPatch != fDecoder.GetHandler().EndOfBusPatch())
+               if (fkCurrentBusPatch != fDecoder.GetHandler().EndOfBusPatch())
                {
-                       fCurrentBusPatch++;
-                       if (fCurrentBusPatch != fDecoder.GetHandler().EndOfBusPatch())
+                       fkCurrentBusPatch++;
+                       if (fkCurrentBusPatch != fDecoder.GetHandler().EndOfBusPatch())
                        {
-                               fCurrentData = fCurrentBusPatch->GetData();
-                               fEndOfData = fCurrentData + fCurrentBusPatch->GetDataCount();
+                               fkCurrentData = fkCurrentBusPatch->GetData();
+                               fkEndOfData = fkCurrentData + fkCurrentBusPatch->GetDataCount();
+                               if ( skipParityErrors )
+                               {
+                                       Bool_t ok(kTRUE);
+                                       for ( Int_t i = 0; i < fkCurrentBusPatch->GetLength() && ok; ++ i )
+                                       {
+                                               ok = fkCurrentBusPatch->IsParityOk(i);
+                                       }
+                                       if (!ok) fkCurrentData = fkEndOfData;
+                               }
                                goto retry;
                        }
                }
@@ -302,6 +356,62 @@ retry:
 }
 
 
+AliMUONDDLTracker* AliMUONRawStreamTrackerHP::GetDDLTracker() const
+{
+       /// Construct and return a pointer to the DDL payload object.
+       /// \return Pointer to internally constructed AliMUONDDLTracker object.
+       ///         The object is owned by this class and should not be deleted
+       ///         by the caller.
+       ///
+       /// \note This method should not be used just to gain access to the DDL
+       /// payload, unless there is a good reason to have the AliMUONDDLTracker
+       /// object. For example, if you want to modify the data and then save it
+       /// to another DDL stream. Otherwise it can be an order of magnitude
+       /// faster to access the DDL headers and data with the GetBlockHeader,
+       /// GetDspHeader and GetBusPatch methods for example.
+       /// Refer to the MUONRawStreamTracker.C macro to see how to use the fast
+       /// decoder interface optimally.
+       
+       if (fDDLObject != NULL) return fDDLObject;
+       
+       fDDLObject = new AliMUONDDLTracker;
+       for (Int_t iBlock = 0; iBlock < (Int_t)GetBlockCount(); iBlock++)
+       {
+               AliMUONBlockHeader blockHeader;
+               AliMUONDspHeader dspHeader;
+               AliMUONBusStruct busPatch;
+               
+               const AliBlockHeader* bh = GetBlockHeader(iBlock);
+               // Copy block header and add it to the DDL object.
+               memcpy(blockHeader.GetHeader(), bh->GetHeader(), sizeof(AliMUONBlockHeaderStruct));
+               fDDLObject->AddBlkHeader(blockHeader);
+               
+               for (Int_t iDsp = 0; iDsp < (Int_t)bh->GetDspCount(); iDsp++)
+               {
+                       const AliDspHeader* dh = bh->GetDspHeader(iDsp);
+                       // Copy DSP header and add it to the DDL object.
+                       memcpy(dspHeader.GetHeader(), dh->GetHeader(), sizeof(AliMUONDSPHeaderStruct));
+                       fDDLObject->AddDspHeader(dspHeader, iBlock);
+                       
+                       const AliBusPatch* bp = dh->GetFirstBusPatch();
+                       while (bp != NULL)
+                       {
+                               // Copy bus patch header, data and add everything into DDL object.
+                               memcpy(busPatch.GetHeader(), bp->GetHeader(), sizeof(AliMUONBusPatchHeaderStruct));
+                               busPatch.SetAlloc(bp->GetLength());
+                               memcpy(busPatch.GetData(), bp->GetData(), bp->GetDataCount()*sizeof(UInt_t));
+                               busPatch.SetBlockId(iBlock);
+                               busPatch.SetDspId(iDsp);
+                               fDDLObject->AddBusPatch(busPatch, iBlock, iDsp);
+                               bp = bp->Next();
+                       }
+               }
+       }
+       
+       return fDDLObject;
+}
+
+
 void AliMUONRawStreamTrackerHP::SetMaxBlock(Int_t blk)
 {
        /// Set maximum number of blocks per DDL allowed.
@@ -340,6 +450,16 @@ void AliMUONRawStreamTrackerHP::SetMaxBus(Int_t bus)
                );
 }
 
+AliRawReader* AliMUONRawStreamTrackerHP::GetReader()
+{
+  return fDecoder.GetHandler().GetReader();
+}
+
+void AliMUONRawStreamTrackerHP::SetReader(AliRawReader* reader)
+{
+  fDecoder.GetHandler().SetReader(reader);
+}
+
 ///////////////////////////////////////////////////////////////////////////////
 
 void AliMUONRawStreamTrackerHP::AliBlockHeader::Print() const
@@ -410,7 +530,7 @@ void AliMUONRawStreamTrackerHP::AliBusPatch::Print(const Option_t* opt) const
 ///////////////////////////////////////////////////////////////////////////////
 
 AliMUONRawStreamTrackerHP::AliDecoderEventHandler::AliDecoderEventHandler() :
-       fRawStream(NULL),
+       fRawReader(0x0),
        fBufferStart(NULL),
        fBlockCount(0),
        fBlocks(NULL),
@@ -426,7 +546,11 @@ AliMUONRawStreamTrackerHP::AliDecoderEventHandler::AliDecoderEventHandler() :
        fParityErrors(0),
        fGlitchErrors(0),
        fPaddingErrors(0),
-       fWarnings(kTRUE)
+       fTokenLostErrors(0),
+       fWarnings(kTRUE),
+       fMaxBlocks(),
+       fMaxDsps(),
+       fMaxBusPatches()
 {
        /// Default constructor initialises the internal parity flags buffer to
        /// store 8192 elements. This array will grow dynamically if needed.
@@ -474,7 +598,11 @@ void AliMUONRawStreamTrackerHP::AliDecoderEventHandler::SetMaxStructs(
        fBlocks = new AliBlockHeader[maxBlocks];
        fDSPs = new AliDspHeader[maxBlocks*maxDsps];
        fBusPatches = new AliBusPatch[maxBlocks*maxDsps*maxBusPatches];
-       fEndOfBusPatches = fEndOfBusPatches;
+       fEndOfBusPatches = fBusPatches;
+
+       fMaxBlocks = maxBlocks;
+       fMaxDsps = maxDsps;
+       fMaxBusPatches = maxBusPatches;
 }
 
 
@@ -487,8 +615,6 @@ void AliMUONRawStreamTrackerHP::AliDecoderEventHandler::OnNewBuffer(
        /// \param buffer  The pointer to the buffer storing the DDL payload.
        /// \param bufferSize  The size of the buffer in bytes.
 
-       assert( fRawStream != NULL );
-       
        // remember the start of the buffer to be used in OnError.
        fBufferStart = buffer;
 
@@ -496,6 +622,7 @@ void AliMUONRawStreamTrackerHP::AliDecoderEventHandler::OnNewBuffer(
        fParityErrors = 0;
        fGlitchErrors = 0;
        fPaddingErrors = 0;
+       fTokenLostErrors = 0;
 
        // Check if we will have enough space in the fParityOk array.
        // If we do not then we need to resize the array.
@@ -533,13 +660,12 @@ void AliMUONRawStreamTrackerHP::AliDecoderEventHandler::OnError(
        /// This is called by the high performance decoder when a error occurs
        /// when trying to decode the DDL payload. This indicates corruption in
        /// the data. This method converts the error code to a descriptive message
-       /// and log this with the raw reader.
+       /// and logs this with the raw reader.
        /// \param error  The error code indicating the problem.
        /// \param location  A pointer to the location within the DDL payload buffer
        ///              being decoded where the problem with the data was found.
 
-       assert( fRawStream != NULL );
-       assert( fRawStream->GetReader() != NULL );
+       assert( fRawReader != NULL );
        
        Char_t* message = NULL;
        UInt_t word = 0;
@@ -547,14 +673,16 @@ void AliMUONRawStreamTrackerHP::AliDecoderEventHandler::OnError(
        switch (error)
        {
        case kGlitchFound:
+               fGlitchErrors++;
                message = Form(
-                       "Glitch error detected in DSP %d, skipping event ",
-                       fCurrentBlock->GetDspId()
+                       "Glitch error detected in DSP %d (0x%X), skipping event ",
+                       fCurrentBlock->GetDspId(), fCurrentBlock->GetDspId()
                );
-               fRawStream->GetReader()->AddMajorErrorLog(error, message);
+               fRawReader->AddMajorErrorLog(error, message);
                break;
 
        case kBadPaddingWord:
+               fPaddingErrors++;
                // We subtract 1 from the current numbers of blocks, DSPs
                // and bus patches to get the indices.
                message = Form(
@@ -563,10 +691,11 @@ void AliMUONRawStreamTrackerHP::AliDecoderEventHandler::OnError(
                        fCurrentBlock->GetDspCount()-1,
                        fCurrentDSP->GetBusPatchCount()-1
                );
-               fRawStream->GetReader()->AddMinorErrorLog(error, message);
+               fRawReader->AddMinorErrorLog(error, message);
                break;
 
        case kParityError:
+               fParityErrors++;
                // location points to the incorrect data word and
                // fCurrentBusPatch->GetData() returns a pointer to the start of
                // bus patches data, so the difference divided by 4 gives the 32
@@ -580,16 +709,28 @@ void AliMUONRawStreamTrackerHP::AliDecoderEventHandler::OnError(
                        fCurrentBusPatch->GetChannelId(word),
                        fCurrentBusPatch->GetBusPatchId()
                );
-               fRawStream->GetReader()->AddMinorErrorLog(error, message);
+               fRawReader->AddMinorErrorLog(error, message);
+               break;
+               
+       case kTokenLost:
+               fTokenLostErrors++;
+               word = *reinterpret_cast<const UInt_t*>(location);
+               message = Form(
+                       "Lost token error detected in DSP %d (0x%X) with address 0x%X and code %d.",
+                       fCurrentBlock->GetDspId(), fCurrentBlock->GetDspId(),
+                       ((word & 0xFFFF0000) >> 16),
+                       (word & 0xF)
+               );
+               fRawReader->AddMinorErrorLog(error, message);
                break;
 
        default:
                message = Form(
                        "%s (At byte %d in DDL.)",
                        ErrorCodeToMessage(error),
-                       (unsigned long)location - (unsigned long)fBufferStart
+                       (unsigned long)location - (unsigned long)fBufferStart + sizeof(AliRawDataHeader)
                );
-               fRawStream->GetReader()->AddMajorErrorLog(error, message);
+               fRawReader->AddMajorErrorLog(error, message);
                break;
        }