#ifndef MARS_MPedCalcFromLoGain
#define MARS_MPedCalcFromLoGain

#ifndef MARS_MExtractor
#include "MExtractor.h"
#endif

#ifndef ROOT_TArrayD
#include <TArrayD.h>
#endif

#ifndef ROOT_TArrayI
#include <TArrayI.h>
#endif

class MGeomCam;
class MBadPixelsCam;

class MPedCalcFromLoGain : public MExtractor
{
    static const Byte_t fgHiGainFirst;      // First FADC slice Hi-Gain (currently set to: 3)
    static const Byte_t fgHiGainLast;       // Last FADC slice Hi-Gain (currently set to: 14)
    static const Byte_t fgLoGainFirst;      // First FADC slice Lo-Gain (currently set to: 3)
    static const Byte_t fgLoGainLast;       // Last FADC slice Lo-Gain (currently set to: 14)
    static const Byte_t fgHiGainWindowSize; // The extraction window Hi-Gain
    static const Byte_t fgLoGainWindowSize; // The extraction window Lo-Gain
    static const Byte_t fgMaxHiGainVar;     // The maximum difference between the highest and lowest slice

    Int_t   fNumEventsDump;      // Number of event after which MPedestalCam gets updated

    Byte_t  fMaxHiGainVar;
    Byte_t  fWindowSizeHiGain;   // Number of Hi Gain slices in window
    Byte_t  fWindowSizeLoGain;   // Number of Lo Gain slices in window

    Bool_t  fPedestalUpdate;

    MGeomCam *fGeom;             // Camera geometry
    TString   fPedContainerName; // name of the 'MPedestalCam' container

    TArrayI fNumEventsUsed;      // Number of events used for pedestal calc for each pixel
    TArrayI fTotalCounter;       // Counter for dumping values to Pedestal Container
    TArrayD fSumx;               // sum of values
    TArrayD fSumx2;              // sum of squared values
    TArrayD fSumAB0;             // sum of ABFlag=0 slices
    TArrayD fSumAB1;             // sum of ABFlag=1 slices

    // MParContainer
    Int_t  PreProcess (MParList *pList);
    Bool_t ReInit     (MParList *pList);
    Int_t  Process    ();
    Int_t  PostProcess();
    Int_t  ReadEnv(const TEnv &env, TString prefix, Bool_t print);

    // Calculation
    void Calc(ULong_t n, UInt_t idx);

public:
    MPedCalcFromLoGain(const char *name=NULL, const char *title=NULL);

    // TObject
    void Clear(const Option_t *o="");

    // Setter
    void SetRange(Byte_t hifirst=fgHiGainFirst, Byte_t hilast=fgHiGainLast, Byte_t lofirst=fgLoGainFirst, Byte_t lolast=fgLoGainLast);
    void SetWindowSize(Byte_t windowh=fgHiGainWindowSize, Byte_t windowl=fgLoGainWindowSize);
    void SetMaxHiGainVar(Byte_t maxvar=fgMaxHiGainVar);
    void SetDumpEvents(UInt_t dumpevents = 0) {fNumEventsDump = dumpevents;}
    void SetPedestalUpdate(Bool_t pedupdate)  {fPedestalUpdate = pedupdate;}

    void SetPedContainerName(const char *name) { fPedContainerName = name; }

    // Getter
    TArrayI *GetNumEventsUsed() { return &fNumEventsUsed; }

    ClassDef(MPedCalcFromLoGain, 0)   // Task to calculate pedestals from pedestal runs raw data
};

#endif
