// @(#)root/hist:$Id$
// Author: Olivier Couet, Deniz Gunceler

/*************************************************************************
 * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/

#ifndef ROOT_TH2Poly
#define ROOT_TH2Poly

//////////////////////////////////////////////////////////////////////////
//                                                                      //
// TH2Poly                                                              //
//                                                                      //
// 2-Dim histogram with polygon bins                                    //
//                                                                      //
//////////////////////////////////////////////////////////////////////////

#ifndef ROOT_TH2
#include "TH2.h"
#endif

#include "TList.h"

class TH2PolyBin: public TObject{

public:
   TH2PolyBin();
   TH2PolyBin(TObject *poly, Int_t bin_number);
   virtual ~TH2PolyBin();

   void      ClearContent(){fContent = 0;}
   void      Fill(Double_t w) {fContent = fContent+w; SetChanged(true);}
   Double_t  GetArea();
   Double_t  GetContent() const{return fContent;}
   Bool_t    GetChanged() const{return fChanged;}
   Int_t     GetBinNumber() const {return fNumber;}
   TObject  *GetPolygon() const {return fPoly;}
   Double_t  GetXMax();
   Double_t  GetXMin();
   Double_t  GetYMax();
   Double_t  GetYMin();
   Bool_t    IsInside(Double_t x, Double_t y) const;
   void      SetChanged(Bool_t flag){fChanged = flag;}
   void      SetContent(Double_t content){fContent = content; SetChanged(true);}

protected:
   Bool_t    fChanged;   //For the 3D Painter
   Int_t     fNumber;    //Bin number of the bin in TH2Poly
   TObject  *fPoly;      //Object holding the polygon definition
   Double_t  fArea;      //Bin area
   Double_t  fContent;   //Bin content
   Double_t  fXmin;      //X minimum value
   Double_t  fYmin;      //Y minimum value
   Double_t  fXmax;      //X maximum value
   Double_t  fYmax;      //Y maximum value

   ClassDef(TH2PolyBin,1)  //2-Dim polygon bins
};

class TList;
class TGraph;
class TMultiGraph;
class TPad;

class TH2Poly : public TH2 {

public:
   TH2Poly();
   TH2Poly(const char *name,const char *title, Double_t xlow, Double_t xup, Double_t ylow, Double_t yup);
   TH2Poly(const char *name,const char *title, Int_t nX, Double_t xlow, Double_t xup,  Int_t nY, Double_t ylow, Double_t yup);
   virtual ~TH2Poly();

   Int_t        AddBin(TObject *poly);
   Int_t        AddBin(Int_t n, const Double_t *x, const Double_t *y);
   Int_t        AddBin(Double_t x1, Double_t y1, Double_t x2, Double_t  y2);
   virtual Bool_t Add(const TH1 *h1, Double_t c1);
   virtual Bool_t Add(const TH1 *h1, const TH1 *h2, Double_t c1=1, Double_t c2=1);
   virtual Bool_t Add(TF1 *h1, Double_t c1=1, Option_t *option="");
   void         ClearBinContents();                 // Clears the content of all bins
   TObject     *Clone(const char* newname = "") const;
   void         ChangePartition(Int_t n, Int_t m);  // Sets the number of partition cells to another value
   Int_t        Fill(Double_t x,Double_t y);
   Int_t        Fill(Double_t x,Double_t y, Double_t w);
   Int_t        Fill(const char* name, Double_t w);
   void         FillN(Int_t ntimes, const Double_t* x, const Double_t* y, const Double_t* w, Int_t stride = 1);
   Int_t        Fill(Double_t){return -1;}                              //MayNotUse
   Int_t        Fill(Double_t , const char *, Double_t){return -1;}     //MayNotUse
   Int_t        Fill(const char *, Double_t , Double_t ){return -1;}    //MayNotUse
   Int_t        Fill(const char *, const char *, Double_t ){return -1;} //MayNotUse
   void         FillN(Int_t, const Double_t*, const Double_t*, Int_t){return;}  //MayNotUse
   Int_t        FindBin(Double_t x, Double_t y, Double_t z = 0);
   TList       *GetBins(){return fBins;}                                // Returns the TList of all bins in the histogram
   Double_t     GetBinContent(Int_t bin) const;
   Double_t     GetBinContent(Int_t, Int_t) const {return 0;}           //MayNotUse
   Double_t     GetBinContent(Int_t, Int_t, Int_t) const {return 0;}    //MayNotUse
   Bool_t       GetBinContentChanged() const{return fBinContentChanged;}
   Double_t     GetBinError(Int_t bin) const;
   Double_t     GetBinError(Int_t , Int_t) const {return 0;}            //MayNotUse
   Double_t     GetBinError(Int_t , Int_t , Int_t) const {return 0;}    //MayNotUse
   const char  *GetBinName(Int_t bin) const;
   const char  *GetBinTitle(Int_t bin) const;
   Bool_t       GetFloat(){return fFloat;}
   Double_t     GetMaximum() const;
   Double_t     GetMaximum(Double_t maxval) const;
   Double_t     GetMinimum() const;
   Double_t     GetMinimum(Double_t minval) const;
   Bool_t       GetNewBinAdded() const{return fNewBinAdded;}
   Int_t        GetNumberOfBins() const{return fNcells;}
   void         Honeycomb(Double_t xstart, Double_t ystart, Double_t a, Int_t k, Int_t s);   // Bins the histogram using a honeycomb structure
   Double_t     Integral(Option_t* option = "") const;
   Double_t     Integral(Int_t, Int_t, const Option_t*) const{return 0;}                             //MayNotUse
   Double_t     Integral(Int_t, Int_t, Int_t, Int_t, const Option_t*) const{return 0;}               //MayNotUse
   Double_t     Integral(Int_t, Int_t, Int_t, Int_t, Int_t, Int_t, const Option_t*) const{return 0;} //MayNotUse
   Long64_t     Merge(TCollection *);
   void         Reset(Option_t *option);
   void         SavePrimitive(std::ostream& out, Option_t* option = "");
   virtual void Scale(Double_t c1 = 1, Option_t* option = "");
   void         SetBinContent(Int_t bin, Double_t content);
   void         SetBinContent(Int_t, Int_t, Double_t){return;}           //MayNotUse
   void         SetBinContent(Int_t, Int_t, Int_t, Double_t){return;}    //MayNotUse
   void         SetBinContentChanged(Bool_t flag){fBinContentChanged = flag;}
   void         SetFloat(Bool_t flag = true);
   void         SetNewBinAdded(Bool_t flag){fNewBinAdded = flag;}

protected:
   TList   *fBins;              //List of bins. The list owns the contained objects
   Double_t fOverflow[9];       //Overflow bins
   Int_t    fCellX;             //Number of partition cells in the x-direction of the histogram
   Int_t    fCellY;             //Number of partition cells in the y-direction of the histogram
   Int_t    fNCells;            //Number of partition cells: fCellX*fCellY
   TList   *fCells;             //[fNCells] The array of TLists that store the bins that intersect with each cell. List do not own the contained objects
   Double_t fStepX, fStepY;     //Dimensions of a partition cell
   Bool_t  *fIsEmpty;           //[fNCells] The array that returns true if the cell at the given coordinate is empty
   Bool_t  *fCompletelyInside;  //[fNCells] The array that returns true if the cell at the given coordinate is completely inside a bin
   Bool_t   fFloat;             //When set to kTRUE, allows the histogram to expand if a bin outside the limits is added.
   Bool_t   fNewBinAdded;       //!For the 3D Painter
   Bool_t   fBinContentChanged; //!For the 3D Painter

   void   AddBinToPartition(TH2PolyBin *bin);  // Adds the input bin into the partition matrix
   void   Initialize(Double_t xlow, Double_t xup, Double_t ylow, Double_t yup, Int_t n, Int_t m);
   Bool_t IsIntersecting(TH2PolyBin *bin, Double_t xclipl, Double_t xclipr, Double_t yclipb, Double_t yclipt);
   Bool_t IsIntersectingPolygon(Int_t bn, Double_t *x, Double_t *y, Double_t xclipl, Double_t xclipr, Double_t yclipb, Double_t yclipt);  
   // needed by TH1 - no need to have a separate implementation
   virtual Double_t RetrieveBinContent(Int_t bin) const { return GetBinContent(bin); }
   virtual void     UpdateBinContent(Int_t bin, Double_t content) { return SetBinContent(bin,content); }

   ClassDef(TH2Poly,1)  //2-Dim histogram with polygon bins
};

#endif
 TH2Poly.h:1
 TH2Poly.h:2
 TH2Poly.h:3
 TH2Poly.h:4
 TH2Poly.h:5
 TH2Poly.h:6
 TH2Poly.h:7
 TH2Poly.h:8
 TH2Poly.h:9
 TH2Poly.h:10
 TH2Poly.h:11
 TH2Poly.h:12
 TH2Poly.h:13
 TH2Poly.h:14
 TH2Poly.h:15
 TH2Poly.h:16
 TH2Poly.h:17
 TH2Poly.h:18
 TH2Poly.h:19
 TH2Poly.h:20
 TH2Poly.h:21
 TH2Poly.h:22
 TH2Poly.h:23
 TH2Poly.h:24
 TH2Poly.h:25
 TH2Poly.h:26
 TH2Poly.h:27
 TH2Poly.h:28
 TH2Poly.h:29
 TH2Poly.h:30
 TH2Poly.h:31
 TH2Poly.h:32
 TH2Poly.h:33
 TH2Poly.h:34
 TH2Poly.h:35
 TH2Poly.h:36
 TH2Poly.h:37
 TH2Poly.h:38
 TH2Poly.h:39
 TH2Poly.h:40
 TH2Poly.h:41
 TH2Poly.h:42
 TH2Poly.h:43
 TH2Poly.h:44
 TH2Poly.h:45
 TH2Poly.h:46
 TH2Poly.h:47
 TH2Poly.h:48
 TH2Poly.h:49
 TH2Poly.h:50
 TH2Poly.h:51
 TH2Poly.h:52
 TH2Poly.h:53
 TH2Poly.h:54
 TH2Poly.h:55
 TH2Poly.h:56
 TH2Poly.h:57
 TH2Poly.h:58
 TH2Poly.h:59
 TH2Poly.h:60
 TH2Poly.h:61
 TH2Poly.h:62
 TH2Poly.h:63
 TH2Poly.h:64
 TH2Poly.h:65
 TH2Poly.h:66
 TH2Poly.h:67
 TH2Poly.h:68
 TH2Poly.h:69
 TH2Poly.h:70
 TH2Poly.h:71
 TH2Poly.h:72
 TH2Poly.h:73
 TH2Poly.h:74
 TH2Poly.h:75
 TH2Poly.h:76
 TH2Poly.h:77
 TH2Poly.h:78
 TH2Poly.h:79
 TH2Poly.h:80
 TH2Poly.h:81
 TH2Poly.h:82
 TH2Poly.h:83
 TH2Poly.h:84
 TH2Poly.h:85
 TH2Poly.h:86
 TH2Poly.h:87
 TH2Poly.h:88
 TH2Poly.h:89
 TH2Poly.h:90
 TH2Poly.h:91
 TH2Poly.h:92
 TH2Poly.h:93
 TH2Poly.h:94
 TH2Poly.h:95
 TH2Poly.h:96
 TH2Poly.h:97
 TH2Poly.h:98
 TH2Poly.h:99
 TH2Poly.h:100
 TH2Poly.h:101
 TH2Poly.h:102
 TH2Poly.h:103
 TH2Poly.h:104
 TH2Poly.h:105
 TH2Poly.h:106
 TH2Poly.h:107
 TH2Poly.h:108
 TH2Poly.h:109
 TH2Poly.h:110
 TH2Poly.h:111
 TH2Poly.h:112
 TH2Poly.h:113
 TH2Poly.h:114
 TH2Poly.h:115
 TH2Poly.h:116
 TH2Poly.h:117
 TH2Poly.h:118
 TH2Poly.h:119
 TH2Poly.h:120
 TH2Poly.h:121
 TH2Poly.h:122
 TH2Poly.h:123
 TH2Poly.h:124
 TH2Poly.h:125
 TH2Poly.h:126
 TH2Poly.h:127
 TH2Poly.h:128
 TH2Poly.h:129
 TH2Poly.h:130
 TH2Poly.h:131
 TH2Poly.h:132
 TH2Poly.h:133
 TH2Poly.h:134
 TH2Poly.h:135
 TH2Poly.h:136
 TH2Poly.h:137
 TH2Poly.h:138
 TH2Poly.h:139
 TH2Poly.h:140
 TH2Poly.h:141
 TH2Poly.h:142
 TH2Poly.h:143
 TH2Poly.h:144
 TH2Poly.h:145
 TH2Poly.h:146
 TH2Poly.h:147
 TH2Poly.h:148
 TH2Poly.h:149
 TH2Poly.h:150
 TH2Poly.h:151
 TH2Poly.h:152
 TH2Poly.h:153
 TH2Poly.h:154
 TH2Poly.h:155