Rev 234 |
Go to most recent revision |
Blame |
Compare with Previous |
Last modification |
View Log
| RSS feed
#ifdef _CVI_
# include <ansi_c.h>
# else /* _CVI_ */
# include <stdlib.h>
# include <stdio.h>
# include <string.h>
#endif /* _CVI_ */
#include "H2D.h"
H2D
*h2
[H2DMAX
];
//int Printf(char *format, ...);
int _VI_FUNC H2D_Clear
(int h2d
) {
if (!h2
[h2d
]) return -1;
memset(h2
[h2d
]->data
, 0,h2
[h2d
]->size
);
h2
[h2d
]->min
=0;
h2
[h2d
]->max
=0;
h2
[h2d
]->nentries
=0;
return 0;
}
int _VI_FUNC H2D_Print
(int h2d
) {
if (!h2
[h2d
]) return -1;
//Printf("PrintH2D nx=%d minx=%f stepx=%f ny=%d miny=%f stepy=%f size=%d\n", h2[h2d]->nx, h2[h2d]->minx, h2[h2d]->stepx, h2[h2d]->ny, h2[h2d]->miny, h2[h2d]->stepy, h2[h2d]->size ) ;
return 0;
}
int _VI_FUNC H2D_Exist
(int h
) {
if (h2
[h
]) return 1;
else return 0;
}
int _VI_FUNC H2D_GetBin
(int h2d
,int x
, int y
) {
return x
+h2
[h2d
]->nx
* y
;
}
int _VI_FUNC H2D_CalculateBin
(int h
, int axis
, double value
) {
int nx
=0;
double xmin
=0,dx
=0;
int bin
;
switch (axis
) {
case 0:
nx
= H2D_GetNbinsX
(h
);
xmin
= H2D_GetMinX
(h
);
dx
= H2D_GetStepX
(h
);
break;
case 1:
nx
= H2D_GetNbinsY
(h
);
xmin
= H2D_GetMinY
(h
);
dx
= H2D_GetStepY
(h
);
break;
default:
return -1;
}
if (dx
<1e-10) return -1;
if (value
<xmin
) return -1;
bin
= (int)((value
-xmin
)/dx
);
if (bin
>=nx
) return -1;
else return bin
;
}
int _VI_FUNC H2D_Fill
(int h2d
,double x
, double y
, double val
) {
int ix
,iy
;
if (!h2
[h2d
]) return -1;
ix
= H2D_CalculateBin
(h2d
,0,x
);
if (ix
<0) return ix
;
iy
= H2D_CalculateBin
(h2d
,1,y
);
if (iy
<0) return iy
;
double val0
= H2D_GetBinContent
(h2d
,ix
, iy
);
return H2D_SetBinContent
(h2d
,ix
, iy
, val
+val0
);
}
int _VI_FUNC H2D_SetBinContent
(int h2d
,int x
, int y
, double val
) {
int idx
;
if (!h2
[h2d
]) {
//Printf("FillH2D_ error h2d is not initialized\n");
return -1;
}
idx
= x
+y
*h2
[h2d
]->nx
;
h2
[h2d
]->data
[idx
]+=val
;
//Printf("%d %d data %f %f\n",x,y,val, h2[h2d]->data[idx]);
if (h2
[h2d
]->data
[idx
]>h2
[h2d
]->max
) h2
[h2d
]->max
= h2
[h2d
]->data
[idx
];
if (h2
[h2d
]->data
[idx
]<h2
[h2d
]->min
) h2
[h2d
]->min
= h2
[h2d
]->data
[idx
];
h2
[h2d
]->nentries
++;
return 0;
}
double _VI_FUNC H2D_GetBinContent
(int h2d
,int atx
,int aty
) {
int idx
;
if (!h2
[h2d
]) return 0;
if (h2
[h2d
]->nx
<= (unsigned int)atx
) return 0;
if (h2
[h2d
]->ny
<= (unsigned int)aty
) return 0;
idx
= atx
+aty
*h2
[h2d
]->nx
;
if (idx
*sizeof(double) < h2
[h2d
]->size
) return h2
[h2d
]->data
[idx
];
return 0;
}
int _VI_FUNC H2D_Init
(int h2d
,const char *name
,const char *title
,int nx
, double minx
, double maxx
, int ny
, double miny
, double maxy
) {
if (h2
[h2d
]) {
free(h2
[h2d
]->data
);
free(h2
[h2d
]);
h2
[h2d
] = NULL
;
}
//printf("InitH2D_ hID=%d\n",h2d);
h2
[h2d
] = (H2D
*) malloc(sizeof(H2D
));
//h2 =h2d;
H2D_SetTitle
(h2d
,title
);
H2D_SetName
(h2d
,name
);
h2
[h2d
]->id
=H2D_ID
;
h2
[h2d
]->nx
= nx
;
h2
[h2d
]->ny
= ny
;
h2
[h2d
]->minx
= minx
;
h2
[h2d
]->miny
= miny
;
h2
[h2d
]->stepx
= (nx
>0)?(maxx
-minx
)/(nx
):0;
h2
[h2d
]->stepy
= (nx
>0)?(maxy
-miny
)/(ny
):0;
h2
[h2d
]->size
= h2
[h2d
]->nx
*h2
[h2d
]->ny
*sizeof(double);
h2
[h2d
]->data
= (double *) malloc(h2
[h2d
]->size
);
h2
[h2d
]->len
=sizeof(H2D
)-sizeof(double *)+h2
[h2d
]->size
;
H2D_Clear
(h2d
);
H2D_Print
(h2d
);
//Printf("InitH2D 0x%x\n", h2d );
return h2d
;
}
double _VI_FUNC H2D_GetXBinCenter
(int h2d
,int xbin
) {
return h2
[h2d
]->minx
+(xbin
+0.5)*h2
[h2d
]->stepx
;
}
double _VI_FUNC H2D_GetYBinCenter
(int h2d
,int ybin
) {
return h2
[h2d
]->miny
+(ybin
+0.5)*h2
[h2d
]->stepy
;
}
int _VI_FUNC H2D_Write2File
(int h2d
,FILE
*fp
) {
if (!fp
) return -1;
//printf("H2D sizeof(H2D)=%lu len-datasize=%d len=%lu datasize=%d\t",sizeof(H2D)-sizeof(double *),h2[h2d]->len-h2[h2d]->size,h2[h2d]->len,h2[h2d]->size);
//printf("H2D sz=%d %d\n",sizeof(double),sizeof(double *));
if (!H2D_Exist
(h2d
)){
printf("Histogram H2D=%d is not initialized\n",h2d
);
return -1;
}
fwrite (h2
[h2d
], 1, sizeof(H2D
)-sizeof(double *), fp
);
fwrite (h2
[h2d
]->data
, 1, h2
[h2d
]->size
, fp
);
return 0;
}
int _VI_FUNC H2D_Write
(int h2d
,const char *fname
,const char *opt
) {
FILE
*fp
=fopen(fname
,opt
);
H2D_Write2File
(h2d
,fp
);
fclose(fp
);
return 0;
}
int _VI_FUNC H2D_SetTitle
(int h2d
,const char *title
) {
sprintf(h2
[h2d
]->title
,"%s",title
);
return 0;
}
int _VI_FUNC H2D_SetTitleX
(int h2d
,const char *title
) {
sprintf(h2
[h2d
]->titlex
,"%s",title
);
return 0;
}
int _VI_FUNC H2D_SetTitleY
(int h2d
,const char *title
) {
sprintf(h2
[h2d
]->titley
,"%s",title
);
return 0;
}
int _VI_FUNC H2D_SetName
(int h2d
,const char *title
) {
sprintf(h2
[h2d
]->name
,"%s",title
);
return 0;
}
int _VI_FUNC H2D_GetNbinsY
(int h
) {
if (h2
[h
]) return h2
[h
]->ny
;
else return 0;
}
int _VI_FUNC H2D_GetNbinsX
(int h
) {
if (h2
[h
]) return h2
[h
]->nx
;
else return 0;
}
double _VI_FUNC H2D_GetMinY
(int h
) {
if (h2
[h
]) return h2
[h
]->miny
;
else return 0;
}
double _VI_FUNC H2D_GetMinX
(int h
) {
if (h2
[h
]) return h2
[h
]->minx
;
else return 0;
}
double _VI_FUNC H2D_GetMaxX
(int h
){
return h2
[h
]->minx
+(h2
[h
]->nx
-1)*h2
[h
]->stepx
;
}
double _VI_FUNC H2D_GetMaxY
(int h
){
return h2
[h
]->miny
+(h2
[h
]->ny
-1)*h2
[h
]->stepy
;
}
double _VI_FUNC H2D_GetStepY
(int h
) {
if (h2
[h
]) return h2
[h
]->stepy
;
else return 0;
}
double _VI_FUNC H2D_GetStepX
(int h
) {
if (h2
[h
]) return h2
[h
]->stepx
;
else return 0;
}
double _VI_FUNC H2D_GetMin
(int h
) {
if (h2
[h
]) return h2
[h
]->min
;
else return 0;
}
double _VI_FUNC H2D_GetMax
(int h
) {
if (h2
[h
]) return h2
[h
]->max
;
else return 0;
}
double * _VI_FUNC H2D_GetData
(int h
) {
if (h2
[h
]) return h2
[h
]->data
;
else return NULL
;
}
#ifdef _CVI_
// defined only in CVI
static HColorMap
*colormap
= NULL
;
HColorMap
* _VI_FUNC H2D_GetColorMap
(void) {
return colormap
;
}
int _VI_FUNC H2D_SetRangeColors
( double min
, double max
) {
int i
;
if (colormap
== NULL
) {
colormap
= malloc(sizeof(HColorMap
));
colormap
->numberofColors
= 5;
colormap
->array
= malloc(colormap
->numberofColors
*sizeof(ColorMapEntry
));
colormap
->array
[0].
color = 0x0000ff; //BLUE
colormap
->array
[1].
color = 0x00ff00; //GREEN
colormap
->array
[2].
color = 0xffff00; //YELLOW
colormap
->array
[3].
color = 0xff8000; //ORANGE
colormap
->array
[4].
color = 0xff0000; //RED
colormap
->HiColor
=colormap
->array
[colormap
->numberofColors
-1].
color ;
}
if (colormap
->numberofColors
<2) return -1;
double fx
= (max
-min
)/(colormap
->numberofColors
-1);
for (i
=0; i
<colormap
->numberofColors
; i
++) {
colormap
->array
[i
].
dataValue.
valDouble=i
*fx
+min
;
}
return 0;
}
int _VI_FUNC H2D_Draw
(int histogram
,int panel
, int control
, int *plothandle
) {
if (!H2D_Exist
(histogram
)) {
printf("2D Histogram %d does not exist!\n",histogram
);
return 0;
}
if (*plothandle
> 0 ) DeleteGraphPlot
(panel
, control
, *plothandle
, VAL_IMMEDIATE_DRAW
);
H2D_SetRangeColors
(H2D_GetMin
(histogram
),H2D_GetMax
(histogram
));
*plothandle
= PlotScaledIntensity
(panel
, control
,
H2D_GetData
(histogram
),
H2D_GetNbinsX
(histogram
),
H2D_GetNbinsY
(histogram
),
VAL_DOUBLE
,
H2D_GetStepY
(histogram
),
H2D_GetYBinCenter
(histogram
,0),
H2D_GetStepX
(histogram
),
H2D_GetXBinCenter
(histogram
,0),
colormap
->array
,
colormap
->HiColor
,
colormap
->numberofColors
, 1, 0);
RefreshGraph
(panel
, control
);
ProcessSystemEvents
();
return *plothandle
;
}
#endif