#ifndef HMDCRICHMATCH_H
#define HMDCRICHMATCH_H
#include "hreconstructor.h"
#include "TString.h"
#include "TF1.h"
class HCategory;
class HIterator;
class HMdcLookUpTb;
class HMdcCalibrater1;
class HMdcEvntListCells;
class TH1F;
class TFile;
class TLine;
class HMdcDGaus : public TF1 {
  private:
    Float_t condMean;   
    Float_t condSig1;   
    Float_t condSig2;   
  public:
    HMdcDGaus(void);
    HMdcDGaus(const Char_t* name, Double_t xmin, Double_t xmax);
    void setTestCond(Float_t cM,Float_t cS1,Float_t cS2) {
      condMean=cM;
      condSig1=cS1;
      condSig2=cS2;
    }
    Int_t histFit(TH1F* hst, Float_t  oMean, Float_t  oSig,
        Float_t& nMean, Float_t& nSig, Bool_t doTest=kTRUE);
    static Double_t dgaus(Double_t *x, Double_t *par);
    
  ClassDef(HMdcDGaus,0)
};
class HMdcRichCorr : public HReconstructor  {
  private:
    TString fileName;              
    TString flNmWoExt;             
    TString runId;
    TString eventDate;
    TString eventTime;
    HMdcCalibrater1* fCalib1;      
    
    HMdcEvntListCells* pListCells; 
    Bool_t isMdcLCellsOwn;         
    HCategory* fClusCat;           
    HIterator* iterMdcClus;        
    Int_t nMdcHits[6];             
    HCategory* fRichHitCat;        
    HIterator* iterRichHit;
    Int_t nRichHits[6];            
    Float_t richTheta[6][400];     
    Float_t richPhi[6][400];       
    Int_t nMdcHitRing[6][400];     
    Float_t radToDeg;              
    HMdcLookUpTb* fLookUpTb;       
    Int_t level4;                  
    Int_t level5;                  
    Int_t nLayers[6][4];           
    Int_t clFinderType;            
    
    Int_t nRingsCut;               
    Float_t dThCuts[6][2][2];      
    Float_t dPhCuts[6][2][2];      
    Float_t numSigTh;              
    Float_t numSigPh;              
    
    Float_t planeMeanDTh[2];
    Float_t planeSigmaDTh[2];
    Float_t planeMeanDPh[2];
    Float_t planeSigmaDPh[2];
    
    Int_t nEvents;                 
    Int_t nRingEvents;             
    Int_t nMatchedEvents;          
    Int_t nMatchedEvents1R;        
    
    
    TString histFileDir;
    TString histFileSuffix;
    TString histFileOption;
    TString histFile;
    TString psFile;
    TH1F* hDThAll[2];
    TH1F* hDPhAll[2];
    TH1F* hDTh[6][2];
    TH1F* hDPh[6][2];
    TH1F* hNRings;
    TH1F* hNMRings;
    TH1F* hNEvents;
    
    Bool_t saveHist;
    Bool_t saveCanv;
    Bool_t savePSFile;
    
    
    HMdcDGaus funDGaus;        
    Int_t fitStatDTh[6][2];    
    Int_t fitStatDPh[6][2];    
  public:
    HMdcRichCorr(void);
    HMdcRichCorr(const Text_t *name,const Text_t *title, Int_t nRCut=1);
    ~HMdcRichCorr(void);
    Bool_t init(void);
    Bool_t reinit(void);
    Bool_t finalize(void);
    Int_t  execute(void);
    void printStatus(void) const;
    void makeCalibrater1(Int_t vers=1, Int_t cut=1, Int_t domerge=0);
    void setNLayers(const Int_t *lst);
    void setTypeClFinder(Int_t type) {clFinderType=type;}
    void setLevel(Int_t l4s1, Int_t l5s1) {
      level4=l4s1;
      level5=l5s1;
    }
    void setDThetaCuts(Float_t dTh1, Float_t dTh2, Float_t nSig=3.);
    void setDPhiCuts(Float_t dPh1, Float_t dPh2, Float_t nSig=3.);
    void setDThetaCuts(const Float_t* dTh, Float_t nSig=3.);
    void setDPhiCuts(const Float_t* dPh, Float_t nSig=3.);
    
    void setDThetaCutsSig(Float_t mDTh,Float_t sDTh,Float_t nSig=3.);
    void setDPhiCutsSig(Float_t mDPh,Float_t sDPh,Float_t nSig=3.);
    void setDThetaCuts(const Float_t* mDTh,const Float_t* sDTh,Float_t nSig=3.);
    void setDPhiCuts(const Float_t* mDPh,const Float_t* sDPh,Float_t nSig=3.);
    
    void setHistFile(const Char_t* dir,const Char_t* suf,
                     const Char_t* option="NEW");
    
    void setTestCond(Float_t cM,Float_t cS1,Float_t cS2) {
      funDGaus.setTestCond(cM,cS1,cS2);
    }
    void setSaveFlags(Bool_t hist, Bool_t canv, Bool_t psfile) {
      saveHist=hist;
      saveCanv=canv;
      savePSFile=psfile;
    }
  private:
    void setup(void);
    void fillListByRichHit(void);
    Bool_t reinitMdcFinder(void);
    void findMdcClusters(void);
    void makeHist(void);
    TFile* openHistFile(const Char_t* flag);
    Float_t meanDTh(Int_t s, Int_t m) {
      return (dThCuts[s][m][0]+dThCuts[s][m][1])*0.5;
    }
    Float_t meanDPh(Int_t s, Int_t m) {
      return (dPhCuts[s][m][0]+dPhCuts[s][m][1])*0.5;
    }
    Float_t sigmaDTh(Int_t s, Int_t m) {
      return (numSigTh>0) ? (dThCuts[s][m][1]-dThCuts[s][m][0])/(2*numSigTh):0.;
    }
    Float_t sigmaDPh(Int_t s, Int_t m) {
      return (numSigPh>0) ? (dPhCuts[s][m][1]-dPhCuts[s][m][0])/(2*numSigPh):0.;
    }
    void calcAvMnSg(void);
    void drawCut(TLine* ln,Float_t mean,Float_t sigma,Float_t nSig,
        Int_t color, Double_t y);
  ClassDef(HMdcRichCorr,0)
};
#endif