#include "TROOT.h"
#include "TClass.h"
#include "TH2Poly.h"
#include "TCutG.h"
#include "TList.h"
#include "TMath.h"
#include "TMultiGraph.h"
#include "TGraph.h"
#include "TStyle.h"
#include "TCanvas.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include "Riostream.h"
ClassImp(TH2Poly)
TH2Poly::TH2Poly()
{
Initialize(0., 0., 0., 0., 25, 25);
SetName("NoName");
SetTitle("NoTitle");
SetFloat();
}
TH2Poly::TH2Poly(const char *name,const char *title, Double_t xlow,Double_t xup
, Double_t ylow,Double_t yup)
{
Initialize(xlow, xup, ylow, yup, 25, 25);
SetName(name);
SetTitle(title);
SetFloat(kFALSE);
}
TH2Poly::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)
{
Initialize(xlow, xup, ylow, yup, nX, nY);
SetName(name);
SetTitle(title);
SetFloat(kFALSE);
}
TH2Poly::~TH2Poly()
{
delete[] fCells;
delete[] fIsEmpty;
delete[] fCompletelyInside;
delete fBins;
}
Int_t TH2Poly::AddBin(TObject *poly)
{
if (!poly) return 0;
if (fBins == 0) {
fBins = new TList();
fBins->SetOwner();
}
fNcells++;
TH2PolyBin *bin = new TH2PolyBin(poly, fNcells);
Bool_t flag = kFALSE;
if (fFloat) {
if (fXaxis.GetXmin() > bin->GetXMin()) {
fXaxis.Set(100, bin->GetXMin(), fXaxis.GetXmax());
flag = kTRUE;
}
if (fXaxis.GetXmax() < bin->GetXMax()) {
fXaxis.Set(100, fXaxis.GetXmin(), bin->GetXMax());
flag = kTRUE;
}
if (fYaxis.GetXmin() > bin->GetYMin()) {
fYaxis.Set(100, bin->GetYMin(), fYaxis.GetXmax());
flag = kTRUE;
}
if (fYaxis.GetXmax() < bin->GetYMax()) {
fYaxis.Set(100, fYaxis.GetXmin(), bin->GetYMax());
flag = kTRUE;
}
if (flag) ChangePartition(fCellX, fCellY);
} else {
}
fBins->Add((TObject*) bin);
SetNewBinAdded(kTRUE);
AddBinToPartition(bin);
return fNcells;
}
Int_t TH2Poly::AddBin(Int_t n, const Double_t *x, const Double_t *y)
{
TGraph *g = new TGraph(n, x, y);
Int_t bin = AddBin(g);
return bin;
}
Int_t TH2Poly::AddBin(Double_t x1, Double_t y1, Double_t x2, Double_t y2)
{
Double_t x[] = {x1, x1, x2, x2, x1};
Double_t y[] = {y1, y2, y2, y1, y1};
TGraph *g = new TGraph(5, x, y);
Int_t bin = AddBin(g);
return bin;
}
Bool_t TH2Poly::Add(const TH1 *h1, Double_t c1)
{
Int_t bin;
TH2Poly *h1p = (TH2Poly *)h1;
if (h1p->GetNumberOfBins() != fNcells) {
Error("Add","Attempt to add histograms with different number of bins");
return kFALSE;
}
TList *h1pBins = h1p->GetBins();
TH2PolyBin *thisBin, *h1pBin;
for (bin=1;bin<=fNcells;bin++) {
thisBin = (TH2PolyBin*)fBins->At(bin-1);
h1pBin = (TH2PolyBin*)h1pBins->At(bin-1);
if(thisBin->GetXMin() != h1pBin->GetXMin() ||
thisBin->GetXMax() != h1pBin->GetXMax() ||
thisBin->GetYMin() != h1pBin->GetYMin() ||
thisBin->GetYMax() != h1pBin->GetYMax()) {
Error("Add","Attempt to add histograms with different bin limits");
return kFALSE;
}
}
if (fSumw2.fN == 0 && h1p->GetSumw2N() != 0) Sumw2();
Double_t factor =1;
if (h1p->GetNormFactor() != 0)
factor = h1p->GetNormFactor()/h1p->GetSumOfWeights();
for (bin=1;bin<=fNcells;bin++) {
thisBin = (TH2PolyBin*)fBins->At(bin-1);
h1pBin = (TH2PolyBin*)h1pBins->At(bin-1);
thisBin->SetContent(thisBin->GetContent()+c1*h1pBin->GetContent());
if (fSumw2.fN) {
Double_t e1 = factor*h1p->GetBinError(bin);
fSumw2.fArray[bin] += c1*c1*e1*e1;
}
}
return kTRUE;
}
Bool_t TH2Poly::Add(TF1 *, Double_t, Option_t *)
{
Warning("Add","Not implement for TH2Poly");
return kFALSE;
}
Bool_t TH2Poly::Add(const TH1 *, const TH1 *, Double_t, Double_t)
{
Warning("Add","Not implement for TH2Poly");
return kFALSE;
}
void TH2Poly::AddBinToPartition(TH2PolyBin *bin)
{
Int_t nl, nr, mb, mt;
Double_t xclipl, xclipr, yclipb, yclipt;
Double_t binXmax, binXmin, binYmax, binYmin;
binXmax = bin->GetXMax();
binXmin = bin->GetXMin();
binYmax = bin->GetYMax();
binYmin = bin->GetYMin();
nl = (Int_t)(floor((binXmin - fXaxis.GetXmin())/fStepX));
nr = (Int_t)(floor((binXmax - fXaxis.GetXmin())/fStepX));
mb = (Int_t)(floor((binYmin - fYaxis.GetXmin())/fStepY));
mt = (Int_t)(floor((binYmax - fYaxis.GetXmin())/fStepY));
if (nr>=fCellX) nr = fCellX-1;
if (mt>=fCellY) mt = fCellY-1;
if (nl<0) nl = 0;
if (mb<0) mb = 0;
fNCells = fCellX*fCellY;
for (int i = nl; i <= nr; i++) {
xclipl = fXaxis.GetXmin() + i*fStepX;
xclipr = xclipl + fStepX;
for (int j = mb; j <= mt; j++) {
yclipb = fYaxis.GetXmin() + j*fStepY;
yclipt = yclipb + fStepY;
if ((binXmin >= xclipl) && (binXmax <= xclipr) &&
(binYmax <= yclipt) && (binYmin >= yclipb)){
fCells[i + j*fCellX].Add((TObject*) bin);
fIsEmpty[i + j*fCellX] = kFALSE;
return;
}
if (IsIntersecting(bin, xclipl, xclipr, yclipb, yclipt)) {
fCells[i + j*fCellX].Add((TObject*) bin);
fIsEmpty[i + j*fCellX] = kFALSE;
continue;
}
if((bin->IsInside(xclipl,yclipb)) || (bin->IsInside(xclipl,yclipt))){
fCells[i + j*fCellX].Add((TObject*) bin);
fIsEmpty[i + j*fCellX] = kFALSE;
fCompletelyInside[i + fCellX*j] = kTRUE;
continue;
}
if((bin->IsInside(xclipr,yclipb)) || (bin->IsInside(xclipr,yclipt))){
fCells[i + j*fCellX].Add((TObject*) bin);
fIsEmpty[i + j*fCellX] = kFALSE;
fCompletelyInside[i + fCellX*j] = kTRUE;
continue;
}
}
}
}
void TH2Poly::ChangePartition(Int_t n, Int_t m)
{
fCellX = n;
fCellY = m;
delete [] fCells;
fNCells = fCellX*fCellY;
fCells = new TList [fNCells];
fStepX = (fXaxis.GetXmax() - fXaxis.GetXmin())/fCellX;
fStepY = (fYaxis.GetXmax() - fYaxis.GetXmin())/fCellY;
delete [] fIsEmpty;
delete [] fCompletelyInside;
fIsEmpty = new Bool_t [fNCells];
fCompletelyInside = new Bool_t [fNCells];
for (int i = 0; i<fNCells; i++) {
fIsEmpty[i] = kTRUE;
fCompletelyInside[i] = kFALSE;
}
TIter next(fBins);
TObject *obj;
while((obj = next())){
AddBinToPartition((TH2PolyBin*) obj);
}
}
void TH2Poly::ClearBinContents()
{
TIter next(fBins);
TObject *obj;
TH2PolyBin *bin;
while ((obj = next())) {
bin = (TH2PolyBin*) obj;
bin->ClearContent();
}
fTsumw = 0;
fTsumwx = 0;
fTsumwx2 = 0;
fTsumwy = 0;
fTsumwy2 = 0;
fEntries = 0;
}
void TH2Poly::Reset(Option_t *opt)
{
TIter next(fBins);
TObject *obj;
TH2PolyBin *bin;
while ((obj = next())) {
bin = (TH2PolyBin*) obj;
bin->ClearContent();
}
TH2::Reset(opt);
}
TH1 *TH2Poly::DrawCopy(Option_t *option) const
{
TString opt = option;
opt.ToLower();
if (gPad && !opt.Contains("same")) gPad->Clear();
TH2Poly *newth2 = (TH2Poly*)Clone();
newth2->SetDirectory(0);
newth2->SetBit(kCanDelete);
newth2->AppendPad(option);
return newth2;
}
Int_t TH2Poly::FindBin(Double_t x, Double_t y, Double_t)
{
Int_t overflow = 0;
if (y > fYaxis.GetXmax()) overflow += -1;
else if (y > fYaxis.GetXmin()) overflow += -4;
else overflow += -7;
if (x > fXaxis.GetXmax()) overflow += -2;
else if (x > fXaxis.GetXmin()) overflow += -1;
if (overflow != -5) return overflow;
Int_t n = (Int_t)(floor((x-fXaxis.GetXmin())/fStepX));
Int_t m = (Int_t)(floor((y-fYaxis.GetXmin())/fStepY));
if (n>=fCellX) n = fCellX-1;
if (m>=fCellY) m = fCellY-1;
if (n<0) n = 0;
if (m<0) m = 0;
if (fIsEmpty[n+fCellX*m]) return -5;
TH2PolyBin *bin;
TIter next(&fCells[n+fCellX*m]);
TObject *obj;
while ((obj=next())) {
bin = (TH2PolyBin*)obj;
if (bin->IsInside(x,y)) return bin->GetBinNumber();
}
return -5;
}
Int_t TH2Poly::Fill(Double_t x, Double_t y)
{
return Fill(x, y, 1.0);
}
Int_t TH2Poly::Fill(Double_t x, Double_t y, Double_t w)
{
if (fNcells==0) return 0;
Int_t overflow = 0;
if (y > fYaxis.GetXmax()) overflow += -1;
else if (y > fYaxis.GetXmin()) overflow += -4;
else overflow += -7;
if (x > fXaxis.GetXmax()) overflow += -2;
else if(x > fXaxis.GetXmin()) overflow += -1;
if (overflow != -5) {
fOverflow[-overflow - 1]++;
return overflow;
}
Int_t n = (Int_t)(floor((x-fXaxis.GetXmin())/fStepX));
Int_t m = (Int_t)(floor((y-fYaxis.GetXmin())/fStepY));
if (n>=fCellX) n = fCellX-1;
if (m>=fCellY) m = fCellY-1;
if (n<0) n = 0;
if (m<0) m = 0;
if (fIsEmpty[n+fCellX*m]) {
fOverflow[4]++;
return -5;
}
TH2PolyBin *bin;
Int_t bi;
TIter next(&fCells[n+fCellX*m]);
TObject *obj;
while ((obj=next())) {
bin = (TH2PolyBin*)obj;
bi = bin->GetBinNumber()-1;
if (bin->IsInside(x,y)) {
bin->Fill(w);
fTsumw = fTsumw + w;
fTsumwx = fTsumwx + w*x;
fTsumwx2 = fTsumwx2 + w*x*x;
fTsumwy = fTsumwy + w*y;
fTsumwy2 = fTsumwy2 + w*y*y;
if (fSumw2.fN) fSumw2.fArray[bi] += w*w;
fEntries++;
SetBinContentChanged(kTRUE);
return bin->GetBinNumber();
}
}
fOverflow[4]++;
return -5;
}
Int_t TH2Poly::Fill(const char* name, Double_t w)
{
TString sname(name);
TIter next(fBins);
TObject *obj;
TH2PolyBin *bin;
while ((obj = next())) {
bin = (TH2PolyBin*) obj;
if (!sname.CompareTo(bin->GetPolygon()->GetName())) {
bin->Fill(w);
fEntries++;
SetBinContentChanged(kTRUE);
return bin->GetBinNumber();
}
}
return 0;
}
void TH2Poly::FillN(Int_t ntimes, const Double_t* x, const Double_t* y,
const Double_t* w, Int_t stride)
{
for (int i = 0; i < ntimes; i += stride) {
Fill(x[i], y[i], w[i]);
}
}
Double_t TH2Poly::Integral(Option_t* option) const
{
TString opt = option;
opt.ToLower();
if ((opt.Contains("width")) || (opt.Contains("area"))) {
Double_t w;
Double_t integral = 0.;
TIter next(fBins);
TObject *obj;
TH2PolyBin *bin;
while ((obj=next())) {
bin = (TH2PolyBin*) obj;
w = bin->GetArea();
integral += w*(bin->GetContent());
}
return integral;
} else {
return fTsumw;
}
}
Double_t TH2Poly::GetBinContent(Int_t bin) const
{
if (bin > fNcells || bin == 0 || bin < -9) return 0;
if (bin<0) return fOverflow[-bin - 1];
return ((TH2PolyBin*) fBins->At(bin-1))->GetContent();
}
Double_t TH2Poly::GetBinError(Int_t bin) const
{
if (bin < 0) bin = 0;
if (bin > (fNcells)) return 0;
if (fBuffer) ((TH1*)this)->BufferEmpty();
if (fSumw2.fN) {
Double_t err2 = fSumw2.fArray[bin-1];
return TMath::Sqrt(err2);
}
Double_t error2 = TMath::Abs(GetBinContent(bin));
return TMath::Sqrt(error2);
}
const char *TH2Poly::GetBinName(Int_t bin) const
{
if (bin > (fNcells)) return "";
if (bin < 0) return "";
return ((TH2PolyBin*) fBins->At(bin-1))->GetPolygon()->GetName();
}
const char *TH2Poly::GetBinTitle(Int_t bin) const
{
if (bin > (fNcells)) return "";
if (bin < 0) return "";
return ((TH2PolyBin*) fBins->At(bin-1))->GetPolygon()->GetTitle();
}
Double_t TH2Poly::GetMaximum() const
{
if (fNcells==0) return 0;
if (fMaximum != -1111) return fMaximum;
TH2PolyBin *b;
TIter next(fBins);
TObject *obj;
Double_t max,c;
max = ((TH2PolyBin*) next())->GetContent();
while ((obj=next())) {
b = (TH2PolyBin*)obj;
c = b->GetContent();
if (c>max) max = c;
}
return max;
}
Double_t TH2Poly::GetMaximum(Double_t maxval) const
{
if (fNcells==0) return 0;
if (fMaximum != -1111) return fMaximum;
TH2PolyBin *b;
TIter next(fBins);
TObject *obj;
Double_t max,c;
max = ((TH2PolyBin*) next())->GetContent();
while ((obj=next())) {
b = (TH2PolyBin*)obj;
c = b->GetContent();
if (c>max && c<maxval) max=c;
}
return max;
}
Double_t TH2Poly::GetMinimum() const
{
if (fNcells==0) return 0;
if (fMinimum != -1111) return fMinimum;
TH2PolyBin *b;
TIter next(fBins);
TObject *obj;
Double_t min,c;
min = ((TH2PolyBin*) next())->GetContent();
while ((obj=next())) {
b = (TH2PolyBin*)obj;
c = b->GetContent();
if (c<min) min=c;
}
return min;
}
Double_t TH2Poly::GetMinimum(Double_t minval) const
{
if (fNcells==0) return 0;
if (fMinimum != -1111) return fMinimum;
TH2PolyBin *b;
TIter next(fBins);
TObject *obj;
Double_t min,c;
min = ((TH2PolyBin*) next())->GetContent();
while ((obj=next())) {
b = (TH2PolyBin*)obj;
c = b->GetContent();
if (c<min && c>minval) min=c;
}
return min;
}
void TH2Poly::Honeycomb(Double_t xstart, Double_t ystart, Double_t a,
Int_t k, Int_t s)
{
Double_t numberOfHexagonsInTheRow;
Double_t x[6], y[6];
Double_t xloop, yloop, xtemp;
xloop = xstart; yloop = ystart + a/2.0;
for (int sCounter = 0; sCounter < s; sCounter++) {
xtemp = xloop;
if(sCounter%2 == 0){numberOfHexagonsInTheRow = k;}
else{numberOfHexagonsInTheRow = k - 1;}
for (int kCounter = 0; kCounter < numberOfHexagonsInTheRow; kCounter++) {
x[0] = xtemp;
y[0] = yloop;
x[1] = x[0];
y[1] = y[0] + a;
x[2] = x[1] + a*TMath::Sqrt(3)/2.0;
y[2] = y[1] + a/2.0;
x[3] = x[2] + a*TMath::Sqrt(3)/2.0;
y[3] = y[1];
x[4] = x[3];
y[4] = y[0];
x[5] = x[2];
y[5] = y[4] - a/2.0;
this->AddBin(6, x, y);
xtemp += a*TMath::Sqrt(3);
}
if (sCounter%2 == 0) xloop += a*TMath::Sqrt(3)/2.0;
else xloop -= a*TMath::Sqrt(3)/2.0;
yloop += 1.5*a;
}
}
void TH2Poly::Initialize(Double_t xlow, Double_t xup,
Double_t ylow, Double_t yup, Int_t n, Int_t m)
{
Int_t i;
fDimension = 2;
fBins = 0;
fNcells = 0;
fXaxis.Set(100, xlow, xup);
fYaxis.Set(100, ylow, yup);
for (i=0; i<9; i++) fOverflow[i] = 0.;
fEntries = 0;
fTsumw = 0.;
fTsumwx = 0.;
fTsumwx2 = 0.;
fTsumwy2 = 0.;
fTsumwy = 0.;
fCellX = n;
fCellY = m;
fNCells = fCellX*fCellY;
fCells = new TList [fNCells];
fStepX = (fXaxis.GetXmax() - fXaxis.GetXmin())/fCellX;
fStepY = (fYaxis.GetXmax() - fYaxis.GetXmin())/fCellY;
fIsEmpty = new Bool_t [fNCells];
fCompletelyInside = new Bool_t [fNCells];
for (i = 0; i<fNCells; i++) {
fIsEmpty[i] = kTRUE;
fCompletelyInside[i] = kFALSE;
}
SetNewBinAdded(kFALSE);
SetBinContentChanged(kFALSE);
}
Bool_t TH2Poly::IsIntersecting(TH2PolyBin *bin,
Double_t xclipl, Double_t xclipr,
Double_t yclipb, Double_t yclipt)
{
Int_t gn;
Double_t *gx;
Double_t *gy;
Bool_t inter = kFALSE;
TObject *poly = bin->GetPolygon();
if (poly->IsA() == TGraph::Class()) {
TGraph *g = (TGraph*)poly;
gx = g->GetX();
gy = g->GetY();
gn = g->GetN();
inter = IsIntersectingPolygon(gn, gx, gy, xclipl, xclipr, yclipb, yclipt);
}
if (poly->IsA() == TMultiGraph::Class()) {
TMultiGraph *mg = (TMultiGraph*)poly;
TList *gl = mg->GetListOfGraphs();
if (!gl) return inter;
TGraph *g;
TIter next(gl);
while ((g = (TGraph*) next())) {
gx = g->GetX();
gy = g->GetY();
gn = g->GetN();
inter = IsIntersectingPolygon(gn, gx, gy, xclipl, xclipr,
yclipb, yclipt);
if (inter) return inter;
}
}
return inter;
}
Bool_t TH2Poly::IsIntersectingPolygon(Int_t bn, Double_t *x, Double_t *y,
Double_t xclipl, Double_t xclipr,
Double_t yclipb, Double_t yclipt)
{
Bool_t p0R, p0L, p0T, p0B, p0xM, p0yM, p1R, p1L, p1T;
Bool_t p1B, p1xM, p1yM, p0In, p1In;
for (int counter = 0; counter < (bn-1); counter++) {
p0L = x[counter] <= xclipl;
p1L = x[counter + 1] <= xclipl;
if (p0L && p1L) continue;
p0R = x[counter] >= xclipr;
p1R = x[counter + 1] >= xclipr;
if (p0R && p1R) continue;
p0T = y[counter] >= yclipt;
p1T = y[counter + 1] >= yclipt;
if (p0T && p1T) continue;
p0B = y[counter] <= yclipb;
p1B = y[counter + 1] <= yclipb;
if (p0B && p1B) continue;
p0xM = !p0R && !p0L;
p0yM = !p0T && !p0B;
p1xM = !p1R && !p1L;
p1yM = !p1T && !p1B;
p0In = p0xM && p0yM;
p1In = p1xM && p1yM;
if (p0In) {
if (p1In) continue;
return kTRUE;
} else {
if (p1In) return kTRUE;
}
if (p0xM && p1xM) return kTRUE;
if (p0yM && p1yM) return kTRUE;
Double_t xcoord[3], ycoord[3];
xcoord[0] = x[counter];
xcoord[1] = x[counter + 1];
ycoord[0] = y[counter];
ycoord[1] = y[counter + 1];
if (p0L) {
if(p1T){
xcoord[2] = xclipl;
ycoord[2] = yclipb;
if((TMath::IsInside(xclipl, yclipt, 3, xcoord, ycoord)) ||
(TMath::IsInside(xclipr, yclipb, 3, xcoord, ycoord))) continue;
else return kTRUE;
} else if (p1B) {
xcoord[2] = xclipl;
ycoord[2] = yclipt;
if((TMath::IsInside(xclipl, yclipb, 3, xcoord, ycoord)) ||
(TMath::IsInside(xclipr, yclipt, 3, xcoord, ycoord))) continue;
else return kTRUE;
} else {
if (p0T) {
xcoord[2] = xclipl;
ycoord[2] = yclipb;
if (TMath::IsInside(xclipr, yclipt, 3, xcoord, ycoord)) continue;
else return kTRUE;
}
if (p0B) {
xcoord[2] = xclipl;
ycoord[2] = yclipt;
if (TMath::IsInside(xclipr, yclipb, 3, xcoord, ycoord)) continue;
else return kTRUE;
}
}
} else if (p0R) {
if (p1T) {
xcoord[2] = xclipl;
ycoord[2] = yclipb;
if ((TMath::IsInside(xclipr, yclipb, 3, xcoord, ycoord)) ||
(TMath::IsInside(xclipl, yclipt, 3, xcoord, ycoord))) continue;
else return kTRUE;
} else if (p1B) {
xcoord[2] = xclipl;
ycoord[2] = yclipt;
if ((TMath::IsInside(xclipl, yclipb, 3, xcoord, ycoord)) ||
(TMath::IsInside(xclipr, yclipt, 3, xcoord, ycoord))) continue;
else return kTRUE;
} else{
if (p0T) {
xcoord[2] = xclipr;
ycoord[2] = yclipb;
if (TMath::IsInside(xclipl, yclipt, 3, xcoord, ycoord)) continue;
else return kTRUE;
}
if (p0B) {
xcoord[2] = xclipr;
ycoord[2] = yclipt;
if (TMath::IsInside(xclipl, yclipb, 3, xcoord, ycoord)) continue;
else return kTRUE;
}
}
}
}
return kFALSE;
}
Long64_t TH2Poly::Merge(TCollection *)
{
Error("Merge","Cannot merge TH2Poly");
return 0;
}
void TH2Poly::SavePrimitive(ostream &out, Option_t *option)
{
out <<" "<<endl;
out <<" "<< ClassName() <<" *";
static Int_t hcounter = 0;
TString histName = GetName();
if (!fDirectory && !histName.Contains("Graph")) {
hcounter++;
histName += "__";
histName += hcounter;
}
const char *hname = histName.Data();
out << hname << " = new " << ClassName() << "(\"" << hname << "\", \""
<< GetTitle() << "\", " << fCellX << ", " << fXaxis.GetXmin()
<< ", " << fXaxis.GetXmax()
<< ", " << fCellY << ", " << fYaxis.GetXmin() << ", "
<< fYaxis.GetXmax() << ");" << endl;
TIter next(fBins);
TObject *obj;
TH2PolyBin *th2pBin;
while((obj = next())){
th2pBin = (TH2PolyBin*) obj;
th2pBin->GetPolygon()->SavePrimitive(out,
Form("th2poly%s",histName.Data()));
}
out<<" "<<endl;
Int_t bin;
for (bin=1;bin<=fNcells;bin++) {
Double_t bc = GetBinContent(bin);
if (bc) {
out<<" "<<hname<<"->SetBinContent("<<bin<<","<<bc<<");"<<endl;
}
}
if (fSumw2.fN) {
for (bin=1;bin<=fNcells;bin++) {
Double_t be = GetBinError(bin);
if (be) {
out<<" "<<hname<<"->SetBinError("<<bin<<","<<be<<");"<<endl;
}
}
}
TH1::SavePrimitiveHelp(out, hname, option);
}
void TH2Poly::Scale(Double_t c1, Option_t*)
{
for( int i = 0; i < this->GetNumberOfBins(); i++ ) {
this->SetBinContent(i+1, c1*this->GetBinContent(i+1));
}
}
void TH2Poly::SetBinContent(Int_t bin, Double_t content)
{
if (bin > (fNcells) || bin == 0 || bin < -9 ) return;
if (bin > 0)
((TH2PolyBin*) fBins->At(bin-1))->SetContent(content);
else
fOverflow[-bin - 1] += content;
SetBinContentChanged(kTRUE);
}
void TH2Poly::SetFloat(Bool_t flag)
{
fFloat = flag;
}
TH2PolyBin::TH2PolyBin()
{
fPoly = 0;
fContent = 0.;
fNumber = 0;
fXmax = -1111;
fXmin = -1111;
fYmax = -1111;
fYmin = -1111;
fArea = 0;
SetChanged(kTRUE);
}
TH2PolyBin::TH2PolyBin(TObject *poly, Int_t bin_number)
{
fContent = 0.;
fNumber = bin_number;
fArea = 0.;
fPoly = poly;
fXmax = -1111;
fXmin = -1111;
fYmax = -1111;
fYmin = -1111;
SetChanged(kTRUE);
}
TH2PolyBin::~TH2PolyBin()
{
if (fPoly) delete fPoly;
}
Double_t TH2PolyBin::GetArea()
{
Int_t bn;
if (fArea == 0) {
if (fPoly->IsA() == TGraph::Class()) {
TGraph *g = (TGraph*)fPoly;
bn = g->GetN();
fArea = g->Integral(0,bn-1);
}
if (fPoly->IsA() == TMultiGraph::Class()) {
TMultiGraph *mg = (TMultiGraph*)fPoly;
TList *gl = mg->GetListOfGraphs();
if (!gl) return fArea;
TGraph *g;
TIter next(gl);
while ((g = (TGraph*) next())) {
bn = g->GetN();
fArea = fArea + g->Integral(0,bn-1);
}
}
}
return fArea;
}
Double_t TH2PolyBin::GetXMax()
{
if (fXmax != -1111) return fXmax;
Int_t bn,i;
Double_t *bx;
if (fPoly->IsA() == TGraph::Class()) {
TGraph *g = (TGraph*)fPoly;
bx = g->GetX();
bn = g->GetN();
fXmax = bx[0];
for (i=1; i<bn; i++) {if (fXmax < bx[i]) fXmax = bx[i];}
}
if (fPoly->IsA() == TMultiGraph::Class()) {
TMultiGraph *mg = (TMultiGraph*)fPoly;
TList *gl = mg->GetListOfGraphs();
if (!gl) return fXmax;
TGraph *g;
TIter next(gl);
Bool_t first = kTRUE;
while ((g = (TGraph*) next())) {
bx = g->GetX();
bn = g->GetN();
if (first) {fXmax = bx[0]; first = kFALSE;}
for (i=0; i<bn; i++) {if (fXmax < bx[i]) fXmax = bx[i];}
}
}
return fXmax;
}
Double_t TH2PolyBin::GetXMin()
{
if (fXmin != -1111) return fXmin;
Int_t bn,i;
Double_t *bx;
if (fPoly->IsA() == TGraph::Class()) {
TGraph *g = (TGraph*)fPoly;
bx = g->GetX();
bn = g->GetN();
fXmin = bx[0];
for (i=1; i<bn; i++) {if (fXmin > bx[i]) fXmin = bx[i];}
}
if (fPoly->IsA() == TMultiGraph::Class()) {
TMultiGraph *mg = (TMultiGraph*)fPoly;
TList *gl = mg->GetListOfGraphs();
if (!gl) return fXmin;
TGraph *g;
TIter next(gl);
Bool_t first = kTRUE;
while ((g = (TGraph*) next())) {
bx = g->GetX();
bn = g->GetN();
if (first) {fXmin = bx[0]; first = kFALSE;}
for (i=0; i<bn; i++) {if (fXmin > bx[i]) fXmin = bx[i];}
}
}
return fXmin;
}
Double_t TH2PolyBin::GetYMax()
{
if (fYmax != -1111) return fYmax;
Int_t bn,i;
Double_t *by;
if (fPoly->IsA() == TGraph::Class()) {
TGraph *g = (TGraph*)fPoly;
by = g->GetY();
bn = g->GetN();
fYmax = by[0];
for (i=1; i<bn; i++) {if (fYmax < by[i]) fYmax = by[i];}
}
if (fPoly->IsA() == TMultiGraph::Class()) {
TMultiGraph *mg = (TMultiGraph*)fPoly;
TList *gl = mg->GetListOfGraphs();
if (!gl) return fYmax;
TGraph *g;
TIter next(gl);
Bool_t first = kTRUE;
while ((g = (TGraph*) next())) {
by = g->GetY();
bn = g->GetN();
if (first) {fYmax = by[0]; first = kFALSE;}
for (i=0; i<bn; i++) {if (fYmax < by[i]) fYmax = by[i];}
}
}
return fYmax;
}
Double_t TH2PolyBin::GetYMin()
{
if (fYmin != -1111) return fYmin;
Int_t bn,i;
Double_t *by;
if (fPoly->IsA() == TGraph::Class()) {
TGraph *g = (TGraph*)fPoly;
by = g->GetY();
bn = g->GetN();
fYmin = by[0];
for (i=1; i<bn; i++) {if (fYmin > by[i]) fYmin = by[i];}
}
if (fPoly->IsA() == TMultiGraph::Class()) {
TMultiGraph *mg = (TMultiGraph*)fPoly;
TList *gl = mg->GetListOfGraphs();
if (!gl) return fYmin;
TGraph *g;
TIter next(gl);
Bool_t first = kTRUE;
while ((g = (TGraph*) next())) {
by = g->GetY();
bn = g->GetN();
if (first) {fYmin = by[0]; first = kFALSE;}
for (i=0; i<bn; i++) {if (fYmin > by[i]) fYmin = by[i];}
}
}
return fYmin;
}
Bool_t TH2PolyBin::IsInside(Double_t x, Double_t y) const
{
Int_t in=0;
if (fPoly->IsA() == TGraph::Class()) {
TGraph *g = (TGraph*)fPoly;
in = g->IsInside(x, y);
}
if (fPoly->IsA() == TMultiGraph::Class()) {
TMultiGraph *mg = (TMultiGraph*)fPoly;
in = mg->IsInside(x, y);
}
return in;
}