#include <TH1D.h>
#include <TStyle.h>
#include <TPad.h>
#include "mH1D.h"
#include <TSystem.h>
double H1DGetBinContent(H1D *h1d,int atx){
if (!h1d) return 0;
if (h1d->nx <= atx) return 0;
if (atx < h1d->size ) return h1d->data[atx];
return 0;
}
int H1Dload(char *fname){
TString dfile( gSystem->Getenv("USERPROFILE") );
dfile.ReplaceAll("\\","/");
dfile += TString("/Desktop/data/") + fname;
fprintf(stderr, "Filename %s\n",dfile.Data());
FILE *fp= fopen(dfile.Data() ,"rb");
if (!fp) return -1;
H1D *h = new H1D;
int size=sizeof(H1D);
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 +datasize=%d == len=%d\t",nb1,sizeof(H1D), h->size, h->len);
printf("data nb=%d size=%d\n",nb,size);
fclose(fp);
TH1D *h1d = new TH1D(h->name, h->title,
h->nx, h->minx-0.5*h->stepx,h->minx+(h->nx-0.5)*h->stepx );
h1d->SetTitle(h->title);
h1d->SetTitle("");
h1d->GetXaxis()->SetTitle(h->titlex);
h1d->GetYaxis()->SetTitle(h->titley);
printf("TH1D name='%s' title='%s' x='%s' y='%s'\nnx=%d min=%f max=%f \n",h->name, h->title,h->titlex, h->titley,
h->nx, h->minx,h->minx+h->nx*h->stepx);
//h2d->ls();
//h2d->Print();
for (int ix=0;ix<h->nx;ix++){
double g=H1DGetBinContent(h,ix);
h1d->SetBinContent(ix+1, g);
// if (g>0) printf("i %d j %d v %g\n",ix+1,iy+1,g);
}
h1d->SetEntries(h->nentries);
gStyle->SetOptStat(1);
gStyle->SetOptFit(1);
h1d->Draw("colz");
TString hname;
//sprintf(hname,"%s.pdf",dfile.Data());
hname = dfile + ".pdf";
gPad->SaveAs(hname);
//sprintf(hname,"%s.root",dfile.Data());
hname = dfile + ".root";
//printf("%s\n",hname);
printf("%s\n",hname.Data());
gPad->SaveAs(hname);
h1d->FitPanel();
return 0;
}