Subversion Repositories f9daq

Rev

Rev 261 | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. #include <userint.h>
  2.  
  3.  
  4. #ifdef _CVI_
  5. #  include <ansi_c.h>
  6. # else _CVI_
  7. #  include <stdlib.h>
  8. #  include <stdio.h>
  9. #  include <string.h>
  10. #endif _CVI_
  11.  
  12. #include "H1D.h"
  13.  
  14.  
  15. H1D *h1[H1DMAX];
  16. //int Printf(char *format, ...);
  17.  
  18. int  _VI_FUNC  H1D_Clear(int h1d) {
  19.   if (!h1[h1d]) return -1;
  20.   memset(h1[h1d]->data, 0,h1[h1d]->size );
  21.   h1[h1d]->min=0;
  22.   h1[h1d]->max=0;
  23.   h1[h1d]->nentries=0;
  24.   return  0;
  25.  
  26. }
  27.  
  28. int  _VI_FUNC  H1D_Print(int h1d) {
  29.   if (!h1[h1d]) return -1;
  30. //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 ) ;
  31.   return  0;
  32.  
  33. }
  34.  
  35. int  _VI_FUNC H1D_Exist(int h) {
  36.   if (h1[h]) return 1;
  37.   else return 0;
  38. }
  39.  
  40.  
  41. int  _VI_FUNC  H1D_GetBin(int h, double value) {
  42.   int nx;
  43.   double xmin,dx;
  44.   int bin;
  45.  
  46.   nx = H1D_GetNbinsX(h);
  47.   xmin= H1D_GetMinX(h);
  48.   dx = H1D_GetStepX(h);
  49.  
  50.   if (dx==0) return -1;
  51.   if (value<xmin) return -1;
  52.   bin = (int)((value-xmin)/dx);
  53.   if (bin>=nx) return -1;
  54.   else return bin;
  55. }
  56.  
  57. int  _VI_FUNC  H1D_Fill(int h1d,double x, double val) {
  58.  
  59.   int ix;
  60.   if (!h1[h1d]) return -1;
  61.  
  62.   ix = H1D_GetBin(h1d,x);
  63.   if (ix<0) return ix;
  64.  
  65.   h1[h1d]->data[ix]+=val;
  66. //Printf("%d %d data %f %f\n",x,y,val, h1[h1d]->data[idx]);
  67.   if (h1[h1d]->data[ix]>h1[h1d]->max) h1[h1d]->max= h1[h1d]->data[ix];
  68.   if (h1[h1d]->data[ix]<h1[h1d]->min) h1[h1d]->min= h1[h1d]->data[ix];
  69.   h1[h1d]->nentries++;
  70.   return 0;
  71. }
  72.  
  73.  
  74. int  _VI_FUNC  H1D_FillBin(int h1d,int x, double val) {
  75.  
  76.   if (!h1[h1d]) {
  77. //Printf("FillH1D_ error h1d is not initialized\n");
  78.     return -1;
  79.   }
  80.  
  81.   h1[h1d]->data[x]+=val;
  82. //Printf("%d %d data %f %f\n",x,y,val, h1[h1d]->data[idx]);
  83.   if (h1[h1d]->data[x]>h1[h1d]->max) h1[h1d]->max= h1[h1d]->data[x];
  84.   if (h1[h1d]->data[x]<h1[h1d]->min) h1[h1d]->min= h1[h1d]->data[x];
  85.   h1[h1d]->nentries++;
  86.   return 0;
  87. }
  88.  
  89. int  _VI_FUNC  H1D_SetBinContent(int h1d,int x, double val) {
  90.  
  91.   if (!h1[h1d]) {
  92. //Printf("FillH1D_ error h1d is not initialized\n");
  93.     return -1;
  94.   }
  95.  
  96.   h1[h1d]->data[x]=val;
  97. //Printf("%d %d data %f %f\n",x,y,val, h1[h1d]->data[idx]);
  98.   if (h1[h1d]->data[x]>h1[h1d]->max) h1[h1d]->max= h1[h1d]->data[x];
  99.   if (h1[h1d]->data[x]<h1[h1d]->min) h1[h1d]->min= h1[h1d]->data[x];
  100.   h1[h1d]->nentries++;
  101.   return 0;
  102. }
  103.  
  104.  
  105.  
  106. double  _VI_FUNC  H1D_GetBinContent(int h1d,int atx) {
  107.  
  108.  
  109.   if (!h1[h1d]) return 0;
  110.   if (h1[h1d]->nx <= atx)  return 0;
  111.   if (atx<0)  return 0;
  112.   if (atx*sizeof(double) < h1[h1d]->size ) return h1[h1d]->data[atx];
  113.  
  114.  
  115.   return 0;
  116. }
  117.  
  118.  
  119. int  _VI_FUNC  H1D_Init(int h1d,char *name, char *title,int nx, double minx, double maxx) {
  120.  
  121.   if (h1[h1d]) {
  122.  
  123.     free(h1[h1d]->data);
  124.     free(h1[h1d]);
  125.     h1[h1d] = NULL;
  126.   }
  127.   // if (h1d!=H1D_MAX-1) printf("InitH1D_ hID=%d\n",h1d);
  128.   h1[h1d] = (H1D *) malloc(sizeof(H1D));
  129. //h2  =h1d;
  130.  
  131.   H1D_SetTitle(h1d,title);
  132.   H1D_SetName(h1d,name);
  133.   H1D_SetTitleX(h1d,"x");
  134.   ;
  135.   h1[h1d]->id=H1D_ID;
  136.   h1[h1d]->nx = nx;
  137.  
  138.  
  139.   h1[h1d]->minx = minx;
  140.   h1[h1d]->stepx = (nx>0)?(maxx-minx)/nx:0;
  141.  
  142.  
  143.   h1[h1d]->size = h1[h1d]->nx*sizeof(double);
  144.   h1[h1d]->data = (double *) malloc(h1[h1d]->size);
  145.   h1[h1d]->len=sizeof(H1D)-sizeof(double *)+h1[h1d]->size;
  146.   H1D_Clear(h1d);
  147.   H1D_Print(h1d);
  148. //Printf("InitH1D 0x%x\n", h1d );
  149.   return  h1d;
  150.  
  151. }
  152.  
  153.  
  154. double  _VI_FUNC  H1D_GetXBinCenter(int h1d,int xbin) {
  155.   return h1[h1d]->minx+(xbin+0.5)*h1[h1d]->stepx;
  156. }
  157.  
  158.  
  159. int  _VI_FUNC  H1D_Write2File(int h1d,FILE *fp) {
  160.  
  161.   if (!fp) return -1;
  162. //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);
  163. //printf("H1D sz=%d %d\n",sizeof(double),sizeof(double *));
  164.   if (!H1D_Exist(h1d)){
  165.     printf("Histogram H1D=%d is not initialized\n",h1d);
  166.     return -1;
  167.   }
  168.   fwrite (h1[h1d], 1, sizeof(H1D)-sizeof(double *), fp);
  169.   fwrite (h1[h1d]->data, 1, h1[h1d]->size, fp);
  170.   return  0;
  171. }
  172.  
  173. int  _VI_FUNC  H1D_Write(int h1d,const char *fname,const char *opt) {
  174.   FILE *fp=fopen(fname,opt);
  175.   H1D_Write2File(h1d,fp);
  176.   fclose(fp);
  177.   return  0;
  178. }
  179.  
  180.  
  181.  
  182.  
  183. int  _VI_FUNC  H1D_SetTitle(int h1d,char *title) {
  184.   if (!h1[h1d]) {
  185.     printf("h1d %d does not exist %s\n",h1d, title);
  186.     return 0;
  187.   }
  188.   sprintf(h1[h1d]->title,"%s",title);
  189.   return 0;
  190. }
  191.  
  192.  
  193. int  _VI_FUNC  H1D_SetTitleX(int h1d,char *title) {
  194.   sprintf(h1[h1d]->titlex,"%s",title);
  195.   return 0;
  196. }
  197.  
  198.  
  199. int  _VI_FUNC H1D_SetTitleY(int h1d,char *title) {
  200.   sprintf(h1[h1d]->titley,"%s",title);
  201.   return 0;
  202. }
  203.  
  204.  
  205. char *  _VI_FUNC  H1D_GetTitleX(int h1d) {
  206.   return h1[h1d]->titlex;
  207. }
  208.  
  209. char *  _VI_FUNC H1D_GetTitleY(int h1d) {
  210.   return h1[h1d]->titley;
  211. }
  212.  
  213. char *  _VI_FUNC H1D_GetTitle(int h1d) {
  214.   return h1[h1d]->title;
  215. }
  216.  
  217.  
  218.  
  219. int  _VI_FUNC  H1D_SetName(int h1d,char *title) {
  220.   sprintf(h1[h1d]->name,"%s",title);
  221.   return 0;
  222. }
  223.  
  224. int  _VI_FUNC H1D_GetNbinsX(int h) {
  225.   if (h1[h]) return h1[h]->nx;
  226.   else return 0;
  227. }
  228.  
  229.  
  230.  
  231. double  _VI_FUNC H1D_GetMinX(int h) {
  232.   if (h1[h]) return h1[h]->minx;
  233.   else return 0;
  234. }
  235.  
  236. double  _VI_FUNC H1D_GetMaxX(int h){
  237.   return h1[h]->minx+ h1[h]->nx*h1[h]->stepx;
  238. }
  239.  
  240.  
  241. double  _VI_FUNC H1D_GetStepX(int h) {
  242.   if (h1[h]) return h1[h]->stepx;
  243.   else return 0;
  244. }
  245.  
  246.  
  247. double  _VI_FUNC H1D_GetMin(int h) {
  248.   if (h1[h]) return h1[h]->min;
  249.   else return 0;
  250. }
  251.  
  252. double  _VI_FUNC H1D_GetMax(int h) {
  253.   if (h1[h]) return h1[h]->max;
  254.   else return 0;
  255. }
  256.  
  257. double *  _VI_FUNC H1D_GetData(int h) {
  258.   if (h1[h]) return h1[h]->data;
  259.   else return NULL;
  260. }
  261.  
  262.  
  263. int  _VI_FUNC  H1D_Draw(int histogram,int panel, int control, int *plothandle) {
  264.   const unsigned int hcolors[8]= {VAL_RED, VAL_GREEN, VAL_BLUE, VAL_CYAN, VAL_MAGENTA, VAL_YELLOW, VAL_GRAY, VAL_BLACK};
  265.   if (!H1D_Exist(histogram)) {
  266.     printf("1D Histogram %d does not exist!\n",histogram);
  267.     return 0;
  268.   }
  269.  
  270. #ifdef _CVI_  
  271.   if (*plothandle> 0 ) DeleteGraphPlot (panel, control, *plothandle, VAL_IMMEDIATE_DRAW);
  272.  
  273.   *plothandle = PlotWaveform (panel, control, H1D_GetData(histogram), H1D_GetNbinsX(histogram), VAL_DOUBLE, 1, 0,
  274.                               H1D_GetXBinCenter(histogram,0), H1D_GetStepX(histogram), VAL_FAT_LINE, VAL_SMALL_SOLID_SQUARE, VAL_SOLID, 1,
  275.                               hcolors[histogram%8]);
  276.   RefreshGraph (panel, control);
  277.   ProcessSystemEvents ();
  278. #endif
  279.  
  280.   return *plothandle;
  281.  
  282. }
  283.  
  284.