// ESAF : Euso Simulation and Analysis Framework
// $Id: GTUClusteringModule.hh,v 1.6 2004/09/28 12:10:11 thea Exp $
// R. Pesce created Mar, 11 2004

#ifndef __GTUCLUSTERINGMODULE_HH_
#define __GTUCLUSTERINGMODULE_HH_

#include "euso.hh"
#include "RecoModule.hh"

class RecoEvent;
class EusoCluster;
class RecoRootEvent;

class GTUClusteringModule : public RecoModule {
public:
    // ctor
    GTUClusteringModule();

    // dtor
    virtual ~GTUClusteringModule();

    // Init method: called at the beginning of a run
    virtual Bool_t Init();

    // called before each event process
    virtual Bool_t PreProcess();

    // event processing
    virtual Bool_t Process( RecoEvent* );

    // called after processing an event
    virtual Bool_t PostProcess();

    // save root output
    virtual Bool_t SaveRootData( RecoRootEvent* );
 
    // called at the end of each run
    virtual Bool_t Done();

    // memory clean
    virtual void UserMemoryClean();

    EsafConfigClass(Reco, GTUClusteringModule)

private:
    RecoEvent *fEv;
    RecoRootEvent *fRecoRootEvent;

    vector<EusoCluster*> fSpClusters;
    Double_t fThetaMin;
    Double_t fThetaMax;
    Double_t fPhiMin;
    Double_t fPhiMax;
    Int_t fGtuMin;
    Int_t fGtuMax;

    Int_t fNumPoints;
    vector<Int_t> fPixels;
    Int_t fNumHitsMinimum;

    vector< vector<Int_t> > fTmClusters;
    vector<Int_t> fCluPixels;

    // variables for clustering
    Bool_t fUseNatural;         // use natural values
    Int_t fNumClusters;         // total number of clusters
    Int_t fNumSigClusters;      // number of significant clusters
    Int_t fNumClustersUniform;  // number of clusters expected in an uniform distribution
    Int_t fNumPointsMinimum;    // minimum number of points in a significant cluster
    Int_t fSignificanceLevel;   // level of significance (from config file)
    Double_t fDensity;          // density of points
    Double_t fThreshold;        // threshold distance
    Int_t fNumNodes;            // number of nodes
    Int_t fNumPointsCluster;    // number of points in cluster
    Int_t fCounter;             // point to be processed
    Int_t fBookmark;            // first point of a new cluster
    vector<Int_t> fId;          // identifier of point
    vector<Bool_t> fFlag1;      // flag that indicates if a point is already in a cluster
    vector<Bool_t> fFlag2;      // flag that indicates if a point is already considered in the
                                // current search
    
    // methods for clustering
    void Prepare();
    void CalculateDensity();
    Int_t GTUDistance(Int_t n1, Int_t n2);
    void CalculateThreshold();
    void CalculateNumClustersUniform();
    void CalculateNumPointsMinimum();

    void NewCluster();
    void SearchClusters();
    void WriteCluster();
    void FillCluster();

    void Histo();
    ClassDef(GTUClusteringModule,0)
};

#endif  /* __GTUCLUSTERINGMODULE_HH_ */