Subversion Repositories f9daq

Rev

Rev 221 | Rev 264 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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