#include "TH2D.h"
#include "..\cvi\H2D.h"
double H2DGetBinContent(H2D *h2d,int atx, int aty){
int idx;
if (!h2d) return 0;
if (h2d->nx <= atx) return 0;
if (h2d->ny <= aty) return 0;
idx = atx+aty*h2d->nx;
if (idx < h2d->size ) return h2d->data[idx];
return 0;
}
int H2Dload(char *fname){
FILE *fp= fopen(fname ,"rb");
if (!fp) return -1;
H2D *h = new H2D;
int size=sizeof(H2D);
int nb1 = fread(h,1, size,fp);
if (size + h->size !=h->len ) printf("WRONG HEADER ! Check sizeof H2D on CVI\n");
size=h->size;
h->data = new double[size];
int nb = fread(h->data,1, size,fp);
printf("hdr nb=%d sizeof(HDR)=%d len=%d datasize=%d\t",nb1,sizeof(H2D),h->len, h->size);
printf("data nb=%d size=%d\n",nb,size);
TH2D *h2d = new TH2D(h->name, h->title,
h->nx, h->minx-0.5*h->stepx,h->minx+(h->nx-0.5)*h->stepx,
h->ny, h->miny-0.5*h->stepy,h->miny+(h->ny-0.5)*h->stepy);
h2d->SetTitle(h->title);
h2d->SetTitle("");
h2d->GetXaxis()->SetTitle(h->titlex);
h2d->GetYaxis()->SetTitle(h->titley);
printf("TH2D name=%s %s %s %s\nnx=%d %f %f \nny=%d %f %f\n",h->name, h->title,h->titlex, h->titley,
h->nx, h->minx-0.5*h->stepx,h->minx+(h->nx-0.5)*h->stepx,
h->ny, h->miny-0.5*h->stepy,h->miny+(h->ny-0.5)*h->stepy);
//h2d->ls();
//h2d->Print();
for (int ix=0;ix<h->nx;ix++){
for (int iy=0;iy<h->ny;iy++){
double g=H2DGetBinContent(h,ix,iy);
h2d->SetBinContent(ix+1,iy+1, g);
// if (g>0) printf("i %d j %d v %g\n",ix+1,iy+1,g);
}
}
gStyle->SetOptStat(0);
h2d->Draw("colz");
char hname[100];
sprintf(hname,"%s.pdf",fname);
gPad->SaveAs(hname);
}