Subversion Repositories f9daq

Rev

Blame | 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 H2DClear(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 H2DPrint(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 H2DExist(int h) {
  31.   if (h2[h]) return 1;
  32.   else return 0;
  33. }
  34.  
  35. int H2DGetBin(int h2d,int x, int y) {
  36.   return x+h2[h2d]->nx * y;
  37. }
  38.  
  39. int H2DCalculateBin(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 = H2DGetNbinsX(h);
  46.       xmin= H2DGetMinX(h);
  47.       dx = H2DGetStepX(h);
  48.       break;
  49.     case 1:
  50.       nx = H2DGetNbinsY(h);
  51.       xmin= H2DGetMinY(h);
  52.       dx = H2DGetStepY(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 H2DFill(int h2d,double x, double y, double val) {
  65.  
  66.   int ix,iy;
  67.   if (!h2[h2d]) return -1;
  68.   ix = H2DCalculateBin(h2d,0,x);
  69.   if (ix<0) return ix;
  70.   iy = H2DCalculateBin(h2d,1,y);
  71.   if (iy<0) return iy;
  72.   return H2DFillBin(h2d,ix, iy, val);
  73. }
  74.  
  75. int H2DFillBin(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 H2DGetBinContent(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 H2DInit(int h2d,const char *name,const char *title,int nx, double minx, double stepx, int ny, double miny, double stepy) {
  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.   H2DSetTitle(h2d,title);
  114.   H2DSetName(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.   h2[h2d]->stepx = stepx;
  121.   h2[h2d]->stepy = stepy;
  122.   h2[h2d]->size = h2[h2d]->nx*h2[h2d]->ny*sizeof(double);
  123.   h2[h2d]->data = (double *) malloc(h2[h2d]->size);
  124.   h2[h2d]->len=sizeof(H2D)-sizeof(double *)+h2[h2d]->size;
  125.   H2DClear(h2d);
  126.   H2DPrint(h2d);
  127. //Printf("InitH2D 0x%x\n", h2d );
  128.   return  0;
  129. }
  130.  
  131. double H2DGetXBinCenter(int h2d,int xbin) {
  132.   return h2[h2d]->minx+xbin*h2[h2d]->stepx;
  133. }
  134.  
  135. double H2DGetYBinCenter(int h2d,int ybin) {
  136.   return h2[h2d]->miny+ybin*h2[h2d]->stepy;
  137. }
  138.  
  139. int H2DWrite2File(int h2d,FILE *fp) {
  140.  
  141.   if (!fp) return -1;
  142. //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);
  143. //printf("H2D sz=%d %d\n",sizeof(double),sizeof(double *));
  144.   if (!H2DExist(h2d)){
  145.     printf("Histogram H2D=%d is not initialized\n",h2d);
  146.     return -1;
  147.   }
  148.   fwrite (h2[h2d], 1, sizeof(H2D)-sizeof(double *), fp);
  149.   fwrite (h2[h2d]->data, 1, h2[h2d]->size, fp);
  150.   return  0;
  151. }
  152.  
  153. int H2DWrite(int h2d,const char *fname,const char *opt) {
  154.   FILE *fp=fopen(fname,opt);
  155.   H2DWrite2File(h2d,fp);
  156.   fclose(fp);
  157.   return  0;
  158. }
  159.  
  160. int H2DSetTitle(int h2d,const char *title) {
  161.   sprintf(h2[h2d]->title,"%s",title);
  162.   return 0;
  163. }
  164.  
  165. int H2DSetTitleX(int h2d,const char *title) {
  166.   sprintf(h2[h2d]->titlex,"%s",title);
  167.   return 0;
  168. }
  169.  
  170. int H2DSetTitleY(int h2d,const char *title) {
  171.   sprintf(h2[h2d]->titley,"%s",title);
  172.   return 0;
  173. }
  174.  
  175. int H2DSetName(int h2d,const char *title) {
  176.   sprintf(h2[h2d]->name,"%s",title);
  177.   return 0;
  178. }
  179.  
  180. int H2DGetNbinsY(int h) {
  181.   if (h2[h]) return h2[h]->ny;
  182.   else return 0;
  183. }
  184.  
  185. int H2DGetNbinsX(int h) {
  186.   if (h2[h]) return h2[h]->nx;
  187.   else return 0;
  188. }
  189.  
  190. double H2DGetMinY(int h) {
  191.   if (h2[h]) return h2[h]->miny;
  192.   else return 0;
  193. }
  194.  
  195. double H2DGetMinX(int h) {
  196.   if (h2[h]) return h2[h]->minx;
  197.   else return 0;
  198. }
  199.  
  200. double H2DGetStepY(int h) {
  201.   if (h2[h]) return h2[h]->stepy;
  202.   else return 0;
  203. }
  204.  
  205. double H2DGetStepX(int h) {
  206.   if (h2[h]) return h2[h]->stepx;
  207.   else return 0;
  208. }
  209.  
  210. double H2DGetMin(int h) {
  211.   if (h2[h]) return h2[h]->min;
  212.   else return 0;
  213. }
  214.  
  215. double H2DGetMax(int h) {
  216.   if (h2[h]) return h2[h]->max;
  217.   else return 0;
  218. }
  219.  
  220. double * H2DGetData(int h) {
  221.   if (h2[h]) return h2[h]->data;
  222.   else return NULL;
  223. }
  224.  
  225.