#include <userint.h>
#ifdef _CVI_
# include <ansi_c.h>
# else _CVI_
# include <stdlib.h>
# include <stdio.h>
# include <string.h>
#endif _CVI_
#include "H1D.h"
#define H1DMAX 500
H1D *h1[H1DMAX];
//int Printf(char *format, ...);
int _VI_FUNC H1D_Clear(int h1d) {
if (!h1[h1d]) return -1;
memset(h1
[h1d
]->data
, 0,h1
[h1d
]->size
);
h1[h1d]->min=0;
h1[h1d]->max=0;
h1[h1d]->nentries=0;
return 0;
}
int _VI_FUNC H1D_Print(int h1d) {
if (!h1[h1d]) return -1;
//Printf("PrintH1D_ nx=%d minx=%f stepx=%f ny=%d miny=%f stepy=%f size=%d\n", h1[h1d]->nx, h1[h1d]->minx, h1[h1d]->stepx, h1[h1d]->ny, h1[h1d]->miny, h1[h1d]->stepy, h1[h1d]->size ) ;
return 0;
}
int _VI_FUNC H1D_Exist(int h) {
if (h1[h]) return 1;
else return 0;
}
int _VI_FUNC H1D_GetBin(int h, double value) {
int nx,xmin,dx;
int bin;
nx = H1D_GetNbinsX(h);
xmin= H1D_GetMinX(h);
dx = H1D_GetStepX(h);
if (dx==0) return -1;
if (value<xmin) return -1;
bin = (int)((value-xmin)/dx);
if (bin>=nx) return -1;
else return bin;
}
int _VI_FUNC H1D_Fill(int h1d,double x, double val) {
int ix;
if (!h1[h1d]) return -1;
ix = H1D_GetBin(h1d,x);
if (ix<0) return ix;
h1[h1d]->data[ix]+=val;
//Printf("%d %d data %f %f\n",x,y,val, h1[h1d]->data[idx]);
if (h1[h1d]->data[ix]>h1[h1d]->max) h1[h1d]->max= h1[h1d]->data[ix];
if (h1[h1d]->data[ix]<h1[h1d]->min) h1[h1d]->min= h1[h1d]->data[ix];
h1[h1d]->nentries++;
return 0;
}
int _VI_FUNC H1D_FillBin(int h1d,int x, double val) {
if (!h1[h1d]) {
//Printf("FillH1D_ error h1d is not initialized\n");
return -1;
}
h1[h1d]->data[x]+=val;
//Printf("%d %d data %f %f\n",x,y,val, h1[h1d]->data[idx]);
if (h1[h1d]->data[x]>h1[h1d]->max) h1[h1d]->max= h1[h1d]->data[x];
if (h1[h1d]->data[x]<h1[h1d]->min) h1[h1d]->min= h1[h1d]->data[x];
h1[h1d]->nentries++;
return 0;
}
int _VI_FUNC H1D_SetBinContent(int h1d,int x, double val) {
if (!h1[h1d]) {
//Printf("FillH1D_ error h1d is not initialized\n");
return -1;
}
h1[h1d]->data[x]=val;
//Printf("%d %d data %f %f\n",x,y,val, h1[h1d]->data[idx]);
if (h1[h1d]->data[x]>h1[h1d]->max) h1[h1d]->max= h1[h1d]->data[x];
if (h1[h1d]->data[x]<h1[h1d]->min) h1[h1d]->min= h1[h1d]->data[x];
h1[h1d]->nentries++;
return 0;
}
double _VI_FUNC H1D_GetBinContent(int h1d,int atx) {
int idx;
if (!h1[h1d]) return 0;
if (h1[h1d]->nx <= atx) return 0;
if (atx<0) return 0;
if (atx*sizeof(double) < h1[h1d]->size ) return h1[h1d]->data[atx];
return 0;
}
int _VI_FUNC H1D_Init(int h1d,char *name, char *title,int nx, double minx, double maxx) {
if (h1[h1d]) {
h1[h1d] = NULL;
}
// if (h1d!=H1D_MAX-1) printf("InitH1D_ hID=%d\n",h1d);
h1
[h1d
] = (H1D
*) malloc(sizeof(H1D
));
//h2 =h1d;
H1D_SetTitle(h1d,title);
H1D_SetName(h1d,name);
H1D_SetTitleX(h1d,"x");
;
h1[h1d]->id=H1D_ID;
h1[h1d]->nx = nx;
h1[h1d]->minx = minx;
h1[h1d]->stepx = (nx>0)?(maxx-minx)/nx:0;
h1[h1d]->size = h1[h1d]->nx*sizeof(double);
h1
[h1d
]->data
= (double *) malloc(h1
[h1d
]->size
);
h1[h1d]->len=sizeof(H1D)-sizeof(double *)+h1[h1d]->size;
H1D_Clear(h1d);
H1D_Print(h1d);
//Printf("InitH1D 0x%x\n", h1d );
return 0;
}
double _VI_FUNC H1D_GetXBinCenter(int h1d,int xbin) {
return h1[h1d]->minx+(xbin+0.5)*h1[h1d]->stepx;
}
int _VI_FUNC H1D_Write2File(int h1d,FILE *fp) {
if (!fp) return -1;
//printf("H1D sizeof(H1D)=%lu len-datasize=%d len=%lu datasize=%d\t",sizeof(H1D)-sizeof(double *),h1[h1d]->len-h1[h1d]->size,h1[h1d]->len,h1[h1d]->size);
//printf("H1D sz=%d %d\n",sizeof(double),sizeof(double *));
if (!H1D_Exist(h1d)){
printf("Histogram H1D=%d is not initialized\n",h1d
);
return -1;
}
fwrite (h1
[h1d
], 1, sizeof(H1D
)-sizeof(double *), fp
);
fwrite (h1
[h1d
]->data
, 1, h1
[h1d
]->size
, fp
);
return 0;
}
int _VI_FUNC H1D_Write(int h1d,const char *fname,const char *opt) {
FILE
*fp
=fopen(fname
,opt
);
H1D_Write2File(h1d,fp);
return 0;
}
int _VI_FUNC H1D_SetTitle(int h1d,char *title) {
if (!h1[h1d]) {
printf("h1d %d does not exist %s\n",h1d
, title
);
return 0;
}
sprintf(h1
[h1d
]->title
,"%s",title
);
return 0;
}
int _VI_FUNC H1D_SetTitleX(int h1d,char *title) {
sprintf(h1
[h1d
]->titlex
,"%s",title
);
return 0;
}
int _VI_FUNC H1D_SetTitleY(int h1d,char *title) {
sprintf(h1
[h1d
]->titley
,"%s",title
);
return 0;
}
char * _VI_FUNC H1D_GetTitleX(int h1d) {
return h1[h1d]->titlex;
}
char * _VI_FUNC H1D_GetTitleY(int h1d) {
return h1[h1d]->titley;
}
char * _VI_FUNC H1D_GetTitle(int h1d) {
return h1[h1d]->title;
}
int _VI_FUNC H1D_SetName(int h1d,char *title) {
return 0;
}
int _VI_FUNC H1D_GetNbinsX(int h) {
if (h1[h]) return h1[h]->nx;
else return 0;
}
double _VI_FUNC H1D_GetMinX(int h) {
if (h1[h]) return h1[h]->minx;
else return 0;
}
double _VI_FUNC H1D_GetMaxX(int h){
return h1[h]->minx+ h1[h]->nx*h1[h]->stepx;
}
double _VI_FUNC H1D_GetStepX(int h) {
if (h1[h]) return h1[h]->stepx;
else return 0;
}
double _VI_FUNC H1D_GetMin(int h) {
if (h1[h]) return h1[h]->min;
else return 0;
}
double _VI_FUNC H1D_GetMax(int h) {
if (h1[h]) return h1[h]->max;
else return 0;
}
double * _VI_FUNC H1D_GetData(int h) {
if (h1[h]) return h1[h]->data;
else return NULL;
}
int _VI_FUNC H1D_Draw(int histogram,int panel, int control, int *plothandle) {
const unsigned int hcolors[8]= {VAL_RED, VAL_GREEN, VAL_BLUE, VAL_CYAN, VAL_MAGENTA, VAL_YELLOW, VAL_GRAY, VAL_BLACK};
if (!H1D_Exist(histogram)) {
printf("1D Histogram %d does not exist!\n",histogram
);
return 0;
}
#ifdef _CVI_
if (*plothandle> 0 ) DeleteGraphPlot (panel, control, *plothandle, VAL_IMMEDIATE_DRAW);
*plothandle = PlotWaveform (panel, control, H1D_GetData(histogram), H1D_GetNbinsX(histogram), VAL_DOUBLE, 1, 0,
H1D_GetXBinCenter(histogram,0), H1D_GetStepX(histogram), VAL_FAT_LINE, VAL_SMALL_SOLID_SQUARE, VAL_SOLID, 1,
hcolors[histogram%8]);
RefreshGraph (panel, control);
ProcessSystemEvents ();
#endif
return *plothandle;
}