Subversion Repositories f9daq

Rev

Rev 221 | 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 "H3D.h"
  10. #include "H2D.h"
  11. #include "H1D.h"
  12.  
  13. #define H3DMAX 500
  14. H3D *h3[H3DMAX];
  15. //int Printf(char *format, ...);
  16.  
  17. int _VI_FUNC  H3D_Clear(int h3d) {
  18.   if (!h3[h3d]) return -1;
  19.   memset(h3[h3d]->data, 0,h3[h3d]->size );
  20.   h3[h3d]->min=0;
  21.   h3[h3d]->max=0;
  22.   h3[h3d]->nentries=0;
  23.   return  0;
  24.  
  25. }
  26.  
  27. int _VI_FUNC  H3D_Print(int h3d) {
  28.   if (!h3[h3d]) return -1;
  29. //Printf("PrintH3D nx=%d minx=%f stepx=%f ny=%d miny=%f stepy=%f size=%d\n", h3[h3d]->nx, h3[h3d]->minx, h3[h3d]->stepx, h3[h3d]->ny, h3[h3d]->miny, h3[h3d]->stepy, h3[h3d]->size ) ;
  30.   return  0;
  31.  
  32. }
  33.  
  34. int _VI_FUNC  H3D_Exist(int h) {
  35.   if (h3[h]) return 1;
  36.   else return 0;
  37. }
  38.  
  39.  
  40. int _VI_FUNC  H3D_CalculateBin(int h, int axis, double value) {
  41.   int nx=1,xmin=0,dx=0;
  42.   int bin;
  43.   switch (axis) {
  44.     case 0:
  45.       nx = H3D_GetNbinsX(h);
  46.       xmin= H3D_GetMinX(h);
  47.       dx = H3D_GetStepX(h);
  48.       break;
  49.     case 1:
  50.       nx = H3D_GetNbinsY(h);
  51.       xmin= H3D_GetMinY(h);
  52.       dx = H3D_GetStepY(h);
  53.       break;
  54.     case 2:
  55.       nx = H3D_GetNbinsZ(h);
  56.       xmin= H3D_GetMinZ(h);
  57.       dx = H3D_GetStepZ(h);
  58.       break;
  59.  
  60.   }
  61.   if (dx==0) return -1;
  62.   if (value<xmin) return -1;
  63.   bin = (int)((value-xmin)/dx);
  64.   if (bin>=nx) return -1;
  65.   else return bin;
  66. }
  67.  
  68. int _VI_FUNC  H3D_Fill(int h3d,double x, double y, double z, double val) {
  69.  
  70.   int ix,iy, iz;
  71.   if (!h3[h3d]) return -1;
  72.  
  73.   ix = H3D_CalculateBin(h3d,0,x);
  74.   if (ix<0) return ix;
  75.  
  76.   iy = H3D_CalculateBin(h3d,1,y);
  77.   if (iy<0) return iy;
  78.  
  79.   iz = H3D_CalculateBin(h3d,2,z);
  80.   if (iz<0) return iz;
  81.  
  82.  
  83.   H3D_FillBin(h3d, ix, iy, iz, val);
  84.   return 0;
  85. }
  86.  
  87. int _VI_FUNC  H3D_GetBin(int h3d,int x, int y, int z) {
  88.   return x+h3[h3d]->nx *(y+z*h3[h3d]->ny);
  89. }
  90.  
  91. int _VI_FUNC  H3D_FillBin(int h3d,int x, int y, int z, double val) {
  92.  
  93.   int idx;
  94.   if (!h3[h3d]) {
  95. //Printf("FillH3D error h3d is not initialized\n");
  96.     return -1;
  97.   }
  98.  
  99.   idx = H3D_GetBin(h3d,x,y,z);
  100.   h3[h3d]->data[idx]+=val;
  101. //Printf("%d %d data %f %f\n",x,y,val, h3[h3d]->data[idx]);
  102.   if (h3[h3d]->data[idx]>h3[h3d]->max) h3[h3d]->max= h3[h3d]->data[idx];
  103.   if (h3[h3d]->data[idx]<h3[h3d]->min) h3[h3d]->min= h3[h3d]->data[idx];
  104.   h3[h3d]->nentries++;
  105.   return 0;
  106. }
  107.  
  108. double _VI_FUNC  H3D_GetBinContent(int h3d,int atx, int aty, int atz) {
  109.  
  110.   int idx;
  111.   if (!h3[h3d]) return 0;
  112.   if (h3[h3d]->nx <= atx)  return 0;
  113.   if (h3[h3d]->ny <= aty)  return 0;
  114.   if (h3[h3d]->nz <= atz)  return 0;
  115.   idx = H3D_GetBin(h3d,atx,aty,atz);
  116.   if (idx*sizeof(double) < h3[h3d]->size ) return h3[h3d]->data[idx];
  117.  
  118.  
  119.   return 0;
  120. }
  121.  
  122.  
  123. int _VI_FUNC  H3D_Init(int h3d,char *name, char *title,
  124.                        int nx, double minx, double maxx,
  125.                        int ny, double miny, double maxy,
  126.                        int nz, double minz, double maxz) {
  127.  
  128.   if (h3[h3d]) {
  129.  
  130.     free(h3[h3d]->data);
  131.     free(h3[h3d]);
  132.     h3[h3d] = NULL;
  133.   }
  134.   //printf("InitH3D hID=%d\n",h3d);
  135.   h3[h3d] = (H3D *) malloc(sizeof(H3D));
  136. //h2  =h3d;
  137.  
  138.   H3D_SetTitle(h3d,title);
  139.   H3D_SetName(h3d,name);
  140.   h3[h3d]->id=H3D_ID;
  141.   h3[h3d]->nx = nx;
  142.   h3[h3d]->ny = ny;
  143.   h3[h3d]->nz = nz;
  144.  
  145.   h3[h3d]->minx = minx;
  146.   h3[h3d]->miny = miny;
  147.   h3[h3d]->minz = minz;
  148.  
  149.   h3[h3d]->stepx = (nx>1)?(maxx-minx)/(nx):0;
  150.   h3[h3d]->stepy = (nx>1)?(maxy-miny)/(ny):0;
  151.   h3[h3d]->stepz = (nx>1)?(maxz-minz)/(nz):0;
  152.  
  153.   h3[h3d]->size = h3[h3d]->nx*h3[h3d]->ny*h3[h3d]->nz*sizeof(double);
  154.   h3[h3d]->data = (double *) malloc(h3[h3d]->size);
  155.   h3[h3d]->len=sizeof(H3D)-sizeof(double *)+h3[h3d]->size;
  156.   H3D_Clear(h3d);
  157.   H3D_Print(h3d);
  158. //Printf("InitH3D 0x%x\n", h3d );
  159.   return  0;
  160.  
  161. }
  162.  
  163.  
  164. int _VI_FUNC  H3D_SliXY(int histogram,int slice, int direction) {      
  165.  
  166. if (!H3D_Exist(histogram)) {
  167.     printf("3D Histogram %d does not exist!\n",histogram);
  168.     return -1;
  169.   }
  170.  
  171.   int hid=499;
  172.   switch (direction){
  173.     case 0:// xy
  174.       H2D_Init(hid,"projectionXY","projectionXY",
  175.                H3D_GetNbinsX(histogram), H3D_GetMinX(histogram),H3D_GetMaxX(histogram),
  176.                H3D_GetNbinsY(histogram), H3D_GetMinY(histogram),H3D_GetMaxY(histogram)
  177.               );
  178.       for (int i=0; i < H3D_GetNbinsX(histogram); i++ )
  179.         for (int j=0; j < H3D_GetNbinsY(histogram); j++ )
  180.           H2D_SetBinContent(hid,i,j,H3D_GetBinContent(histogram,i,j,slice));
  181.       break;
  182.     case 1:// xz
  183.       H2D_Init(hid,"projectionXZ","projectionXZ",
  184.                H3D_GetNbinsX(histogram), H3D_GetMinX(histogram),H3D_GetMaxX(histogram),
  185.                H3D_GetNbinsZ(histogram), H3D_GetMinZ(histogram),H3D_GetMaxZ(histogram)
  186.               );
  187.       for (int i=0; i < H3D_GetNbinsX(histogram); i++ )
  188.         for (int j=0; j < H3D_GetNbinsZ(histogram); j++ )
  189.           H2D_SetBinContent(hid,i,j,H3D_GetBinContent(histogram,i,slice,j));
  190.       break;
  191.     case 2:// yz
  192.     default:
  193.       H2D_Init(hid,"projectionYZ","projectionYZ",
  194.                H3D_GetNbinsY(histogram), H3D_GetMinY(histogram),H3D_GetMaxY(histogram),
  195.                H3D_GetNbinsZ(histogram), H3D_GetMinZ(histogram),H3D_GetMaxZ(histogram)
  196.               );
  197.       for (int i=0; i < H3D_GetNbinsY(histogram); i++ )
  198.         for (int j=0; j < H3D_GetNbinsZ(histogram); j++ )
  199.           H2D_SetBinContent(hid,i,j,H3D_GetBinContent(histogram,slice,i,j));
  200.      
  201.       break;
  202.   }
  203.   return hid;
  204. }
  205.  
  206. double  _VI_FUNC H3D_GetMaxX(int h){
  207.   return h3[h]->minx+(h3[h]->nx)*h3[h]->stepx;
  208. }
  209.  
  210. double  _VI_FUNC H3D_GetMaxY(int h){
  211.   return h3[h]->miny+(h3[h]->ny)*h3[h]->stepy;
  212. }
  213.  
  214. double  _VI_FUNC H3D_GetMaxZ(int h){
  215.   return h3[h]->minz+(h3[h]->nz)*h3[h]->stepz;
  216. }
  217.  
  218. double _VI_FUNC  H3D_GetXBinCenter(int h3d,int xbin) {
  219.   return h3[h3d]->minx+(xbin+0.5)*h3[h3d]->stepx;
  220. }
  221.  
  222. double _VI_FUNC  H3D_GetYBinCenter(int h3d,int ybin) {
  223.   return h3[h3d]->miny+(ybin+0.5)*h3[h3d]->stepy;
  224. }
  225.  
  226. double _VI_FUNC  H3D_GetZBinCenter(int h3d,int zbin) {
  227.   return h3[h3d]->minz+(zbin+0.5)*h3[h3d]->stepz;
  228. }
  229.  
  230.  
  231. int _VI_FUNC  H3D_Write2File(int h3d,FILE *fp) {
  232.  
  233.   if (!fp) return -1;
  234. //printf("H3D sizeof(H3D)=%lu len-datasize=%d len=%lu datasize=%d\t",sizeof(H3D)-sizeof(double *),h3[h3d]->len-h3[h3d]->size,h3[h3d]->len,h3[h3d]->size);
  235. //printf("H3D sz=%d %d\n",sizeof(double),sizeof(double *));
  236.   fwrite (h3[h3d], 1, sizeof(H3D)-sizeof(double *), fp);
  237.   fwrite (h3[h3d]->data, 1, h3[h3d]->size, fp);
  238.   return  0;
  239. }
  240.  
  241. int _VI_FUNC  H3D_Write(int h3d,const char *fname,const char *opt) {
  242.   FILE *fp=fopen(fname,opt);
  243.   H3D_Write2File(h3d,fp);
  244.   fclose(fp);
  245.   return  0;
  246. }
  247.  
  248.  
  249.  
  250.  
  251. int _VI_FUNC  H3D_SetTitle(int h3d,char *title) {
  252.   sprintf(h3[h3d]->title,"%s",title);
  253.   return 0;
  254. }
  255.  
  256.  
  257. int _VI_FUNC  H3D_SetTitleX(int h3d,char *title) {
  258.   sprintf(h3[h3d]->titlex,"%s",title);
  259.   return 0;
  260. }
  261.  
  262.  
  263. int _VI_FUNC  H3D_SetTitleY(int h3d,char *title) {
  264.   sprintf(h3[h3d]->titley,"%s",title);
  265.   return 0;
  266. }
  267.  
  268. int _VI_FUNC  H3D_SetTitleZ(int h3d,char *title) {
  269.   sprintf(h3[h3d]->titlez,"%s",title);
  270.   return 0;
  271. }
  272.  
  273.  
  274. int _VI_FUNC  H3D_SetName(int h3d,char *title) {
  275.   sprintf(h3[h3d]->name,"%s",title);
  276.   return 0;
  277. }
  278.  
  279. int _VI_FUNC  H3D_GetNbinsY(int h) {
  280.   if (h3[h]) return h3[h]->ny;
  281.   else return 0;
  282. }
  283.  
  284. int _VI_FUNC  H3D_GetNbinsX(int h) {
  285.   if (h3[h]) return h3[h]->nx;
  286.   else return 0;
  287. }
  288.  
  289. int _VI_FUNC  H3D_GetNbinsZ(int h) {
  290.   if (h3[h]) return h3[h]->nz;
  291.   else return 0;
  292. }
  293.  
  294.  
  295.  
  296. double _VI_FUNC  H3D_GetMinX(int h) {
  297.   if (h3[h]) return h3[h]->minx;
  298.   else return 0;
  299. }
  300.  
  301. double _VI_FUNC  H3D_GetMinY(int h) {
  302.   if (h3[h]) return h3[h]->miny;
  303.   else return 0;
  304. }
  305.  
  306. double _VI_FUNC  H3D_GetMinZ(int h) {
  307.   if (h3[h]) return h3[h]->minz;
  308.   else return 0;
  309. }
  310.  
  311.  
  312.  
  313.  
  314. double _VI_FUNC  H3D_GetStepX(int h) {
  315.   if (h3[h]) return h3[h]->stepx;
  316.   else return 0;
  317. }
  318.  
  319. double _VI_FUNC  H3D_GetStepY(int h) {
  320.   if (h3[h]) return h3[h]->stepy;
  321.   else return 0;
  322. }
  323.  
  324. double _VI_FUNC  H3D_GetStepZ(int h) {
  325.   if (h3[h]) return h3[h]->stepz;
  326.   else return 0;
  327. }
  328.  
  329.  
  330.  
  331.  
  332. double _VI_FUNC  H3D_GetMin(int h) {
  333.   if (h3[h]) return h3[h]->min;
  334.   else return 0;
  335. }
  336.  
  337. double _VI_FUNC  H3D_GetMax(int h) {
  338.   if (h3[h]) return h3[h]->max;
  339.   else return 0;
  340. }
  341.  
  342. double *  _VI_FUNC  H3D_GetSliceXYData(int h, int slice) {
  343.   if (h3[h]) return (h3[h]->data+slice*h3[h]->nx*h3[h]->ny);
  344.   else return NULL;
  345. }
  346.  
  347. double *  _VI_FUNC  H3D_GetData(int h) {
  348.   if (h3[h]) return h3[h]->data;
  349.   else return NULL;
  350. }
  351.  
  352.  
  353. #ifdef _CVI_
  354. // defined only in CVI
  355. static HColorMap *colormap = NULL;
  356.  
  357.  
  358. HColorMap * _VI_FUNC  H3D_GetColorMap(void) {
  359.   return colormap;
  360. }
  361.  
  362.  
  363. int _VI_FUNC  H3D_SetRangeColors( double min, double max) {
  364.   int i;
  365.  
  366.  
  367.   if (colormap == NULL) {
  368.     colormap = malloc(sizeof(HColorMap));
  369.  
  370.     colormap->numberofColors = 5;
  371.     colormap->array = malloc(colormap->numberofColors*sizeof(ColorMapEntry));
  372.  
  373.     colormap->array[0].color = 0x0000ff; //BLUE
  374.     colormap->array[1].color = 0x00ff00; //GREEN
  375.     colormap->array[2].color = 0xffff00; //YELLOW
  376.     colormap->array[3].color = 0xff8000; //ORANGE
  377.     colormap->array[4].color = 0xff0000; //RED
  378.  
  379.     colormap->HiColor =colormap->array[colormap->numberofColors-1].color ;
  380.   }
  381.   if (colormap->numberofColors<2) return -1;
  382.   double fx = (max-min)/(colormap->numberofColors-1);
  383.   for  (i=0; i<colormap->numberofColors; i++) {
  384.     colormap->array[i].dataValue.valDouble=i*fx+min;
  385.   }
  386.   return 0;
  387. }
  388.  
  389. int  _VI_FUNC  H3D_DrawSliceXY(int histogram,int slice,int direction, int panel, int control, int *plothandle) {
  390.  
  391.  
  392.  
  393.   if (H3D_GetNbinsY(histogram)==1|| H3D_GetNbinsX(histogram)==1) {
  394.     if (H3D_GetNbinsY(histogram)==1) {
  395.       H1D_Init(499,"projection","projection", H3D_GetNbinsX(histogram), H3D_GetMinX(histogram),H3D_GetMaxX(histogram));
  396.       for (int i=0; i < H3D_GetNbinsX(histogram); i++ ) H1D_SetBinContent(499,i,H3D_GetBinContent(histogram,i,0,slice));
  397.     } else {
  398.       H1D_Init(499,"projection","projection", H3D_GetNbinsY(histogram), H3D_GetMinY(histogram),H3D_GetMaxY(histogram));
  399.       for (int i=0; i < H3D_GetNbinsY(histogram); i++ ) H1D_SetBinContent(499,i,H3D_GetBinContent(histogram,0,i,slice));
  400.     }
  401.     H1D_Draw(499, panel, control, plothandle);
  402.   } else {
  403.  
  404.     if (*plothandle> 0 ) DeleteGraphPlot (panel, control, *plothandle, VAL_IMMEDIATE_DRAW);
  405.     H3D_SetRangeColors(H3D_GetMin(histogram),H3D_GetMax(histogram));
  406.     *plothandle = PlotScaledIntensity (panel, control,
  407.                                        H3D_GetSliceXYData(histogram, slice),
  408.                                        H3D_GetNbinsX(histogram),
  409.                                        H3D_GetNbinsY(histogram),
  410.                                        VAL_DOUBLE,
  411.                                        H3D_GetStepY(histogram),
  412.                                        H3D_GetYBinCenter(histogram,0),
  413.                                        H3D_GetStepX(histogram),
  414.                                        H3D_GetXBinCenter(histogram,0),
  415.                                        colormap->array,
  416.                                        colormap->HiColor,
  417.                                        colormap->numberofColors, 1, 0);
  418.   }
  419.   ProcessSystemEvents ();
  420.   return *plothandle;
  421.  
  422. }
  423. #endif
  424.