#ifndef MARS_MCerPhotEvt
#define MARS_MCerPhotEvt

#ifndef ROOT_TClonesArray
#include <TClonesArray.h>
#endif
#ifndef ROOT_TArrayI
#include <TArrayI.h>
#endif
#ifndef MARS_MCamEvent
#include "MCamEvent.h"
#endif
#ifndef MARS_MCerPhotPix
#include "MCerPhotPix.h"
#endif

class MGeomCam;
class MCerPhotPix;
class MCerPhotEvtIter;

class MCerPhotEvt : public MParContainer, public MCamEvent
{
    friend class MCerPhotEvtIter;
private:
    UInt_t        fNumPixels;
    Short_t       fNumIslands;
     Int_t        fMaxIndex;
    TArrayI       fLut;        // Lookup tabel to lookup pixel by index
    TClonesArray *fPixels;     //-> FIXME: Change TClonesArray away from a pointer?

    void RebuildLut()
    {
        // Resize Lut
        fLut.Set(fMaxIndex+1);

        // Reset Lut
        fLut.Reset(-1);

        // Rebuild Lut
        for (UInt_t i=0; i<GetNumPixels(); i++)
        {
            const MCerPhotPix &pix = (*this)[i];
            fLut[pix.GetPixId()] = i;
        }
    }

    Double_t CalcIsland(const MGeomCam &geom, Int_t idx, Int_t num);

public:
    MCerPhotEvt(const char *name=NULL, const char *title=NULL);
    ~MCerPhotEvt() { delete fPixels; }

    // Setter function to fill pixels
    MCerPhotPix *AddPixel(Int_t idx, Float_t nph=0, Float_t er=0);
    void FixSize();

    // Getter functions
    UInt_t  GetNumPixels() const { return fNumPixels; }
    Short_t GetNumIslands() const { return fNumIslands; };

    Bool_t  IsPixelExisting(Int_t id) const;
    Bool_t  IsPixelUsed    (Int_t id) const;
    Bool_t  IsPixelCore    (Int_t id) const;

    Float_t GetNumPhotonsMin(const MGeomCam *geom=NULL) const;
    Float_t GetNumPhotonsMax(const MGeomCam *geom=NULL) const;

    Float_t GetRatioMin(const MGeomCam *geom=NULL) const;
    Float_t GetRatioMax(const MGeomCam *geom=NULL) const;

    Float_t GetErrorPhotMin(const MGeomCam *geom=NULL) const;
    Float_t GetErrorPhotMax(const MGeomCam *geom=NULL) const;

    // Getter functions to access single pixels
    MCerPhotPix &operator[](int i)       { return *(MCerPhotPix*)(fPixels->UncheckedAt(i)); }
    MCerPhotPix &operator[](int i) const { return *(MCerPhotPix*)(fPixels->UncheckedAt(i)); }

    MCerPhotPix *GetPixById(Int_t idx) const;

    // Functions to change the contained data
    void Scale(Double_t f) { fPixels->ForEach(MCerPhotPix, Scale)(f); }
    void RemoveUnusedPixels();
    Int_t CalcIslands(const MGeomCam &geom);
    void Sort(Int_t upto = kMaxInt)
    {
        // Sort pixels by index
        fPixels->Sort(upto);
        RebuildLut();
    } // For convinience: Sort pixels by index

    // class MParContainer
    void Reset();

    // class TObject
    void Print(Option_t *opt=NULL) const;
    void Clear(Option_t *opt=NULL) { Reset(); }

    // class MCamEvent
    Bool_t GetPixelContent(Double_t &val, Int_t idx, const MGeomCam &cam, Int_t type=0) const;
    void DrawPixelContent(Int_t num) const;

    // To build an iterator for this class
    operator TIterator*() const;

    ClassDef(MCerPhotEvt, 3)    // class for an event containing cerenkov photons
};

class MCerPhotEvtIter : public TObjArrayIter
{
private:
    Bool_t fUsedOnly;
public:
    MCerPhotEvtIter(const MCerPhotEvt *evt, Bool_t usedonly=kTRUE, Bool_t dir=kIterForward) : TObjArrayIter(evt->fPixels, dir), fUsedOnly(usedonly) { }
    TObject *Next();
    TIterator &operator=(const TIterator &) { return *this; }
    ClassDef(MCerPhotEvtIter, 0)
};

inline MCerPhotEvt::operator TIterator*() const { return new MCerPhotEvtIter(this); }

#endif
