#ifndef MARS_MRawEvtPixelIter
#define MARS_MRawEvtPixelIter
///////////////////////////////////////////////////////////////////////////////
//
//  MRawEvtPixelIter
//
//  class to iterate over all pixels of one event.
//
///////////////////////////////////////////////////////////////////////////////
#ifndef MARS_MAGIC
#include "MAGIC.h"
#endif

class MRawEvtData;

class MRawEvtPixelIter : public TObject
{
private:
    UShort_t fNumHiGainEntry;   //! actual number of entry in fHiGainPixId
    UShort_t fNumLoGainEntry;   //! actual number of entry in fLoGainPixId

    UShort_t *fHiGainId;        //! actual entry of fHiGainPixId
    UShort_t *fLoGainId;        //! actual entry of fLoGainPixId

    Byte_t   *fHiGainPos;       //! pointer to hi-gain samples of actual pixel
    Byte_t   *fLoGainPos;       //! pointer to lo-gain samples of actual pixel

    Char_t   *fABFlags;         //! pointer to AB flags

    UShort_t fNumEntry;

    Byte_t fNumHiGainSamples;   //!
    Byte_t fNumLoGainSamples;   //!

    MRawEvtData *fData;         //! pointer to object which we are iterating

public:
    MRawEvtPixelIter(MRawEvtData *dat);

    MRawEvtData *Next();

    Bool_t Jump(UShort_t id)
    {
        //
        // Jump to the pixel with the pixel-id ID
        // If it doesn't exist return FALSE
        //
        Reset();
        while (Next())
            if (GetPixelId() == id)
                return kTRUE;
        return kFALSE;
    }

    UShort_t GetPixelId() const
    {
        //
        // return Id of actual pixel
        //
        return *fHiGainId;
    }

    UShort_t GetNumEntry() const
    {
        return fNumHiGainEntry;
    }

    Byte_t *GetHiGainSamples() const
    {
        //
        // return a pointer to the fadc samples of the hi gains
        // WARNING: Don't forget to get the number of valid entries
        //          (GetNumSamples) to know how many entries of the array
        //          belong to the actual pixel
        //
        return fHiGainPos;
    }

    ULong_t GetSumHiGainSamples() const;
    ULong_t GetSumSqrHiGainSamples() const;
    Float_t GetVarHiGainSamples() const;

    Byte_t  GetMaxHiGainSample() const;
    Byte_t  GetMaxLoGainSample() const;
    Byte_t  GetIdxMaxHiGainSample() const;
    Short_t GetIdxMaxLoGainSample() const;
    Short_t GetIdxMaxHiLoGainSample() const;

    Bool_t HasLoGain() const
    {
        //
        // return kTRUE  the lo gains exist for the actual pixel, else return kFALSE
        //
        return fLoGainId && *fHiGainId==*fLoGainId;
    }
    Bool_t HasABFlag() const
    {
        //
        // return kTRUE  the lo gains exist for the actual pixel, else return kFALSE
        //
        return TESTBIT(fABFlags[GetPixelId()/8], GetPixelId()%8);
    }
    Byte_t GetABFlag() const
    {
        //
        // return kTRUE  the lo gains exist for the actual pixel, else return kFALSE
        //
        return (Byte_t)fABFlags[GetPixelId()/8];
    }

    Byte_t *GetLoGainSamples() const
    {
        //
        // return a pointer to the fadc samples of the lo gains if they exist
        // for the actual pixel, else return zero
        //
        return HasLoGain() ? fLoGainPos : NULL;
    }

    ULong_t GetSumLoGainSamples() const;
    ULong_t GetSumSqrLoGainSamples() const;

    void Reset();

    void Draw(Option_t *t="GRAPH");

    ClassDef(MRawEvtPixelIter, 0) // iterates over all pixels of one MRawEvtData object
};

#endif
