Subversion Repositories f9daq

Rev

Rev 234 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. #ifdef _CVI_
  2. #  include <ansi_c.h>
  3. # else /* _CVI_ */
  4. #  include <stdlib.h>
  5. #  include <stdio.h>
  6. #  include <string.h>
  7. #endif /* _CVI_ */
  8.  
  9. #include "H2D.h"
  10.  
  11. #define H2DMAX 500
  12. H2D *h2[H2DMAX];
  13. //int Printf(char *format, ...);
  14.  
  15. int _VI_FUNC  H2D_Clear(int h2d) {
  16.   if (!h2[h2d]) return -1;
  17.   memset(h2[h2d]->data, 0,h2[h2d]->size );
  18.   h2[h2d]->min=0;
  19.   h2[h2d]->max=0;
  20.   h2[h2d]->nentries=0;
  21.   return  0;
  22. }
  23.  
  24. int _VI_FUNC  H2D_Print(int h2d) {
  25.   if (!h2[h2d]) return -1;
  26. //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 ) ;
  27.   return  0;
  28. }
  29.  
  30. int _VI_FUNC  H2D_Exist(int h) {
  31.   if (h2[h]) return 1;
  32.   else return 0;
  33. }
  34.  
  35. int _VI_FUNC  H2D_GetBin(int h2d,int x, int y) {
  36.   return x+h2[h2d]->nx * y;
  37. }
  38.  
  39. int _VI_FUNC  H2D_CalculateBin(int h, int axis, double value) {
  40.   int nx=0;
  41.   double xmin=0,dx=0;
  42.   int bin;
  43.   switch (axis) {
  44.     case 0:
  45.       nx = H2D_GetNbinsX(h);
  46.       xmin= H2D_GetMinX(h);
  47.       dx = H2D_GetStepX(h);
  48.       break;
  49.     case 1:
  50.       nx = H2D_GetNbinsY(h);
  51.       xmin= H2D_GetMinY(h);
  52.       dx = H2D_GetStepY(h);
  53.       break;
  54.     default:
  55.       return -1;
  56.   }
  57.   if (dx<1e-10) return -1;
  58.   if (value<xmin) return -1;
  59.   bin = (int)((value-xmin)/dx);
  60.   if (bin>=nx) return -1;                                                  
  61.   else return bin;
  62. }
  63.  
  64. int _VI_FUNC  H2D_Fill(int h2d,double x, double y, double val) {
  65.  
  66.   int ix,iy;
  67.   if (!h2[h2d]) return -1;
  68.   ix = H2D_CalculateBin(h2d,0,x);
  69.   if (ix<0) return ix;
  70.   iy = H2D_CalculateBin(h2d,1,y);
  71.   if (iy<0) return iy;
  72.   return H2D_SetBinContent(h2d,ix, iy, val);
  73. }
  74.  
  75. int _VI_FUNC  H2D_SetBinContent(int h2d,int x, int y, double val) {
  76.  
  77.   int idx;
  78.   if (!h2[h2d]) {
  79. //Printf("FillH2D_ error h2d is not initialized\n");
  80.     return -1;
  81.   }
  82.  
  83.   idx = x+y*h2[h2d]->nx;
  84.   h2[h2d]->data[idx]+=val;
  85. //Printf("%d %d data %f %f\n",x,y,val, h2[h2d]->data[idx]);
  86.   if (h2[h2d]->data[idx]>h2[h2d]->max) h2[h2d]->max= h2[h2d]->data[idx];
  87.   if (h2[h2d]->data[idx]<h2[h2d]->min) h2[h2d]->min= h2[h2d]->data[idx];
  88.   h2[h2d]->nentries++;
  89.   return 0;
  90. }
  91.  
  92. double _VI_FUNC  H2D_GetBinContent(int h2d,int atx,int aty) {
  93.  
  94.   int idx;
  95.   if (!h2[h2d]) return 0;
  96.   if (h2[h2d]->nx <= (unsigned int)atx)  return 0;
  97.   if (h2[h2d]->ny <= (unsigned int)aty)  return 0;
  98.   idx = atx+aty*h2[h2d]->nx;
  99.   if (idx*sizeof(double) < h2[h2d]->size ) return h2[h2d]->data[idx];
  100.   return 0;
  101. }
  102.  
  103. 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) {
  104.  
  105.   if (h2[h2d]) {
  106.     free(h2[h2d]->data);
  107.     free(h2[h2d]);
  108.     h2[h2d] = NULL;
  109.   }
  110.   //printf("InitH2D_ hID=%d\n",h2d);
  111.   h2[h2d] = (H2D *) malloc(sizeof(H2D));
  112. //h2  =h2d;
  113.   H2D_SetTitle(h2d,title);
  114.   H2D_SetName(h2d,name);
  115.   h2[h2d]->id=H2D_ID;
  116.   h2[h2d]->nx = nx;
  117.   h2[h2d]->ny = ny;
  118.   h2[h2d]->minx = minx;
  119.   h2[h2d]->miny = miny;
  120.  
  121.   h2[h2d]->stepx = (nx>1)?(maxx-minx)/(nx-1):0;
  122.   h2[h2d]->stepy = (nx>1)?(maxy-miny)/(ny-1):0;
  123.  
  124.   h2[h2d]->size = h2[h2d]->nx*h2[h2d]->ny*sizeof(double);
  125.   h2[h2d]->data = (double *) malloc(h2[h2d]->size);
  126.   h2[h2d]->len=sizeof(H2D)-sizeof(double *)+h2[h2d]->size;
  127.   H2D_Clear(h2d);
  128.   H2D_Print(h2d);
  129. //Printf("InitH2D 0x%x\n", h2d );
  130.   return  0;
  131. }
  132.  
  133. double _VI_FUNC  H2D_GetXBinCenter(int h2d,int xbin) {
  134.   return h2[h2d]->minx+xbin*h2[h2d]->stepx;
  135. }
  136.  
  137. double _VI_FUNC  H2D_GetYBinCenter(int h2d,int ybin) {
  138.   return h2[h2d]->miny+ybin*h2[h2d]->stepy;
  139. }
  140.  
  141. int _VI_FUNC  H2D_Write2File(int h2d,FILE *fp) {
  142.  
  143.   if (!fp) return -1;
  144. //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);
  145. //printf("H2D sz=%d %d\n",sizeof(double),sizeof(double *));
  146.   if (!H2D_Exist(h2d)){
  147.     printf("Histogram H2D=%d is not initialized\n",h2d);
  148.     return -1;
  149.   }
  150.   fwrite (h2[h2d], 1, sizeof(H2D)-sizeof(double *), fp);
  151.   fwrite (h2[h2d]->data, 1, h2[h2d]->size, fp);
  152.   return  0;
  153. }
  154.  
  155. int _VI_FUNC  H2D_Write(int h2d,const char *fname,const char *opt) {
  156.   FILE *fp=fopen(fname,opt);
  157.   H2D_Write2File(h2d,fp);
  158.   fclose(fp);
  159.   return  0;
  160. }
  161.  
  162. int _VI_FUNC  H2D_SetTitle(int h2d,const char *title) {
  163.   sprintf(h2[h2d]->title,"%s",title);
  164.   return 0;
  165. }
  166.  
  167. int _VI_FUNC  H2D_SetTitleX(int h2d,const char *title) {
  168.   sprintf(h2[h2d]->titlex,"%s",title);
  169.   return 0;
  170. }
  171.  
  172. int _VI_FUNC  H2D_SetTitleY(int h2d,const char *title) {
  173.   sprintf(h2[h2d]->titley,"%s",title);
  174.   return 0;
  175. }
  176.  
  177. int _VI_FUNC  H2D_SetName(int h2d,const char *title) {
  178.   sprintf(h2[h2d]->name,"%s",title);
  179.   return 0;
  180. }
  181.  
  182. int _VI_FUNC  H2D_GetNbinsY(int h) {
  183.   if (h2[h]) return h2[h]->ny;
  184.   else return 0;
  185. }
  186.  
  187. int _VI_FUNC  H2D_GetNbinsX(int h) {
  188.   if (h2[h]) return h2[h]->nx;
  189.   else return 0;
  190. }
  191.  
  192. double _VI_FUNC  H2D_GetMinY(int h) {
  193.   if (h2[h]) return h2[h]->miny;
  194.   else return 0;
  195. }
  196.  
  197. double _VI_FUNC  H2D_GetMinX(int h) {
  198.   if (h2[h]) return h2[h]->minx;
  199.   else return 0;
  200. }
  201.  
  202.  
  203. double  _VI_FUNC H2D_GetMaxX(int h){
  204.   return h2[h]->minx+(h2[h]->nx-1)*h2[h]->stepx;
  205. }
  206.  
  207. double  _VI_FUNC H2D_GetMaxY(int h){
  208.   return h2[h]->miny+(h2[h]->ny-1)*h2[h]->stepy;
  209. }
  210.  
  211.  
  212. double _VI_FUNC  H2D_GetStepY(int h) {
  213.   if (h2[h]) return h2[h]->stepy;
  214.   else return 0;
  215. }
  216.  
  217. double _VI_FUNC  H2D_GetStepX(int h) {
  218.   if (h2[h]) return h2[h]->stepx;
  219.   else return 0;
  220. }
  221.  
  222. double _VI_FUNC  H2D_GetMin(int h) {
  223.   if (h2[h]) return h2[h]->min;
  224.   else return 0;
  225. }
  226.  
  227. double _VI_FUNC  H2D_GetMax(int h) {
  228.   if (h2[h]) return h2[h]->max;
  229.   else return 0;
  230. }
  231.  
  232. double * _VI_FUNC  H2D_GetData(int h) {
  233.   if (h2[h]) return h2[h]->data;
  234.   else return NULL;
  235. }
  236.  
  237.  
  238.  
  239. #ifdef _CVI_
  240. // defined only in CVI
  241. static HColorMap *colormap = NULL;
  242.  
  243.  
  244. HColorMap * _VI_FUNC  H2D_GetColorMap(void) {
  245.   return colormap;
  246. }
  247.  
  248.  
  249. int _VI_FUNC  H2D_SetRangeColors( double min, double max) {
  250.   int i;
  251.  
  252.  
  253.   if (colormap == NULL) {
  254.     colormap = malloc(sizeof(HColorMap));
  255.  
  256.     colormap->numberofColors = 5;
  257.     colormap->array = malloc(colormap->numberofColors*sizeof(ColorMapEntry));
  258.  
  259.     colormap->array[0].color = 0x0000ff; //BLUE
  260.     colormap->array[1].color = 0x00ff00; //GREEN
  261.     colormap->array[2].color = 0xffff00; //YELLOW
  262.     colormap->array[3].color = 0xff8000; //ORANGE
  263.     colormap->array[4].color = 0xff0000; //RED
  264.  
  265.     colormap->HiColor =colormap->array[colormap->numberofColors-1].color ;
  266.   }
  267.   if (colormap->numberofColors<2) return -1;
  268.   double fx = (max-min)/(colormap->numberofColors-1);
  269.   for  (i=0; i<colormap->numberofColors; i++) {
  270.     colormap->array[i].dataValue.valDouble=i*fx+min;
  271.   }
  272.   return 0;
  273. }
  274.  
  275.  
  276.  
  277.  
  278.  
  279. int _VI_FUNC  H2D_Draw(int histogram,int panel, int control, int *plothandle) {
  280.  
  281.   if (!H2D_Exist(histogram)) {
  282.     printf("2D Histogram %d does not exist!\n",histogram);
  283.     return 0;
  284.   }
  285.  
  286.  
  287.     if (*plothandle> 0 ) DeleteGraphPlot (panel, control, *plothandle, VAL_IMMEDIATE_DRAW);
  288.     H2D_SetRangeColors(H2D_GetMin(histogram),H2D_GetMax(histogram));
  289.     *plothandle = PlotScaledIntensity (panel, control,
  290.                                        H2D_GetData(histogram),
  291.                                        H2D_GetNbinsX(histogram),
  292.                                        H2D_GetNbinsY(histogram),
  293.                                        VAL_DOUBLE,
  294.                                        H2D_GetStepY(histogram),
  295.                                        H2D_GetMinY(histogram),
  296.                                        H2D_GetStepX(histogram),
  297.                                        H2D_GetMinX(histogram),
  298.                                        colormap->array,
  299.                                        colormap->HiColor,
  300.                                        colormap->numberofColors, 1, 0);
  301.     RefreshGraph (panel, control);
  302.  
  303.   ProcessSystemEvents ();
  304.   return *plothandle;
  305.  
  306. }
  307.  
  308. #endif
  309.