Subversion Repositories f9daq

Rev

Rev 248 | 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 "H3D.h"
10
#include "H2D.h"
11
#include "H1D.h" 
12
 
264 f9daq 13
 
209 f9daq 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
 
248 f9daq 82
 
209 f9daq 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
 
221 f9daq 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;
209 f9daq 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;
264 f9daq 172
        int i=0;
173
        int j=0;
209 f9daq 174
  switch (direction){
175
    case 0:// xy
176
      H2D_Init(hid,"projectionXY","projectionXY",
177
               H3D_GetNbinsX(histogram), H3D_GetMinX(histogram),H3D_GetMaxX(histogram),
178
               H3D_GetNbinsY(histogram), H3D_GetMinY(histogram),H3D_GetMaxY(histogram)
179
              );
264 f9daq 180
      for (i=0; i < H3D_GetNbinsX(histogram); i++ )
181
        for (j=0; j < H3D_GetNbinsY(histogram); j++ )
209 f9daq 182
          H2D_SetBinContent(hid,i,j,H3D_GetBinContent(histogram,i,j,slice));
183
      break;
184
    case 1:// xz
185
      H2D_Init(hid,"projectionXZ","projectionXZ",
186
               H3D_GetNbinsX(histogram), H3D_GetMinX(histogram),H3D_GetMaxX(histogram),
187
               H3D_GetNbinsZ(histogram), H3D_GetMinZ(histogram),H3D_GetMaxZ(histogram)
188
              );
264 f9daq 189
      for (i=0; i < H3D_GetNbinsX(histogram); i++ )
190
        for (j=0; j < H3D_GetNbinsZ(histogram); j++ )
209 f9daq 191
          H2D_SetBinContent(hid,i,j,H3D_GetBinContent(histogram,i,slice,j));
192
      break;
193
    case 2:// yz
194
    default:
195
      H2D_Init(hid,"projectionYZ","projectionYZ",
196
               H3D_GetNbinsY(histogram), H3D_GetMinY(histogram),H3D_GetMaxY(histogram),
197
               H3D_GetNbinsZ(histogram), H3D_GetMinZ(histogram),H3D_GetMaxZ(histogram)
198
              );
264 f9daq 199
      for (i=0; i < H3D_GetNbinsY(histogram); i++ )
200
        for (j=0; j < H3D_GetNbinsZ(histogram); j++ )
209 f9daq 201
          H2D_SetBinContent(hid,i,j,H3D_GetBinContent(histogram,slice,i,j));
202
 
203
      break;
204
  }
205
  return hid;
206
}
207
 
208
double  _VI_FUNC H3D_GetMaxX(int h){
221 f9daq 209
  return h3[h]->minx+(h3[h]->nx)*h3[h]->stepx;
209 f9daq 210
}
211
 
212
double  _VI_FUNC H3D_GetMaxY(int h){
221 f9daq 213
  return h3[h]->miny+(h3[h]->ny)*h3[h]->stepy;
209 f9daq 214
}
215
 
216
double  _VI_FUNC H3D_GetMaxZ(int h){
221 f9daq 217
  return h3[h]->minz+(h3[h]->nz)*h3[h]->stepz;
209 f9daq 218
}
219
 
220
double _VI_FUNC  H3D_GetXBinCenter(int h3d,int xbin) {
221 f9daq 221
  return h3[h3d]->minx+(xbin+0.5)*h3[h3d]->stepx;
209 f9daq 222
}
223
 
224
double _VI_FUNC  H3D_GetYBinCenter(int h3d,int ybin) {
221 f9daq 225
  return h3[h3d]->miny+(ybin+0.5)*h3[h3d]->stepy;
209 f9daq 226
}
227
 
228
double _VI_FUNC  H3D_GetZBinCenter(int h3d,int zbin) {
221 f9daq 229
  return h3[h3d]->minz+(zbin+0.5)*h3[h3d]->stepz;
209 f9daq 230
}
231
 
232
 
233
int _VI_FUNC  H3D_Write2File(int h3d,FILE *fp) {
234
 
235
  if (!fp) return -1;
236
//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);
237
//printf("H3D sz=%d %d\n",sizeof(double),sizeof(double *));
238
  fwrite (h3[h3d], 1, sizeof(H3D)-sizeof(double *), fp);
239
  fwrite (h3[h3d]->data, 1, h3[h3d]->size, fp);
240
  return  0;
241
}
242
 
243
int _VI_FUNC  H3D_Write(int h3d,const char *fname,const char *opt) {
244
  FILE *fp=fopen(fname,opt);
245
  H3D_Write2File(h3d,fp);
246
  fclose(fp);
247
  return  0;
248
}
249
 
250
 
251
 
252
 
253
int _VI_FUNC  H3D_SetTitle(int h3d,char *title) {
254
  sprintf(h3[h3d]->title,"%s",title);
255
  return 0;
256
}
257
 
258
 
259
int _VI_FUNC  H3D_SetTitleX(int h3d,char *title) {
260
  sprintf(h3[h3d]->titlex,"%s",title);
261
  return 0;
262
}
263
 
264
 
265
int _VI_FUNC  H3D_SetTitleY(int h3d,char *title) {
266
  sprintf(h3[h3d]->titley,"%s",title);
267
  return 0;
268
}
269
 
270
int _VI_FUNC  H3D_SetTitleZ(int h3d,char *title) {
271
  sprintf(h3[h3d]->titlez,"%s",title);
272
  return 0;
273
}
274
 
275
 
276
int _VI_FUNC  H3D_SetName(int h3d,char *title) {
277
  sprintf(h3[h3d]->name,"%s",title);
278
  return 0;
279
}
280
 
281
int _VI_FUNC  H3D_GetNbinsY(int h) {
282
  if (h3[h]) return h3[h]->ny;
283
  else return 0;
284
}
285
 
286
int _VI_FUNC  H3D_GetNbinsX(int h) {
287
  if (h3[h]) return h3[h]->nx;
288
  else return 0;
289
}
290
 
291
int _VI_FUNC  H3D_GetNbinsZ(int h) {
292
  if (h3[h]) return h3[h]->nz;
293
  else return 0;
294
}
295
 
296
 
297
 
298
double _VI_FUNC  H3D_GetMinX(int h) {
299
  if (h3[h]) return h3[h]->minx;
300
  else return 0;
301
}
302
 
303
double _VI_FUNC  H3D_GetMinY(int h) {
304
  if (h3[h]) return h3[h]->miny;
305
  else return 0;
306
}
307
 
308
double _VI_FUNC  H3D_GetMinZ(int h) {
309
  if (h3[h]) return h3[h]->minz;
310
  else return 0;
311
}
312
 
313
 
314
 
315
 
316
double _VI_FUNC  H3D_GetStepX(int h) {
317
  if (h3[h]) return h3[h]->stepx;
318
  else return 0;
319
}
320
 
321
double _VI_FUNC  H3D_GetStepY(int h) {
322
  if (h3[h]) return h3[h]->stepy;
323
  else return 0;
324
}
325
 
326
double _VI_FUNC  H3D_GetStepZ(int h) {
327
  if (h3[h]) return h3[h]->stepz;
328
  else return 0;
329
}
330
 
331
 
332
 
333
 
334
double _VI_FUNC  H3D_GetMin(int h) {
335
  if (h3[h]) return h3[h]->min;
336
  else return 0;
337
}
338
 
339
double _VI_FUNC  H3D_GetMax(int h) {
340
  if (h3[h]) return h3[h]->max;
341
  else return 0;
342
}
343
 
344
double *  _VI_FUNC  H3D_GetSliceXYData(int h, int slice) {
345
  if (h3[h]) return (h3[h]->data+slice*h3[h]->nx*h3[h]->ny);
346
  else return NULL;
347
}
348
 
349
double *  _VI_FUNC  H3D_GetData(int h) {
350
  if (h3[h]) return h3[h]->data;
351
  else return NULL;
352
}
353
 
354
 
355
#ifdef _CVI_
356
// defined only in CVI
357
static HColorMap *colormap = NULL;
358
 
359
 
360
HColorMap * _VI_FUNC  H3D_GetColorMap(void) {
361
  return colormap;
362
}
363
 
364
 
365
int _VI_FUNC  H3D_SetRangeColors( double min, double max) {
366
  int i;
367
 
368
 
369
  if (colormap == NULL) {
370
    colormap = malloc(sizeof(HColorMap));
371
 
372
    colormap->numberofColors = 5;
373
    colormap->array = malloc(colormap->numberofColors*sizeof(ColorMapEntry));
374
 
375
    colormap->array[0].color = 0x0000ff; //BLUE
376
    colormap->array[1].color = 0x00ff00; //GREEN
377
    colormap->array[2].color = 0xffff00; //YELLOW
378
    colormap->array[3].color = 0xff8000; //ORANGE
379
    colormap->array[4].color = 0xff0000; //RED
380
 
381
    colormap->HiColor =colormap->array[colormap->numberofColors-1].color ;
382
  }
383
  if (colormap->numberofColors<2) return -1;
384
  double fx = (max-min)/(colormap->numberofColors-1);
385
  for  (i=0; i<colormap->numberofColors; i++) {
386
    colormap->array[i].dataValue.valDouble=i*fx+min;
387
  }
388
  return 0;
389
}
390
 
391
int  _VI_FUNC  H3D_DrawSliceXY(int histogram,int slice,int direction, int panel, int control, int *plothandle) {
392
 
393
 
394
 
395
  if (H3D_GetNbinsY(histogram)==1|| H3D_GetNbinsX(histogram)==1) {
396
    if (H3D_GetNbinsY(histogram)==1) {
221 f9daq 397
      H1D_Init(499,"projection","projection", H3D_GetNbinsX(histogram), H3D_GetMinX(histogram),H3D_GetMaxX(histogram));
209 f9daq 398
      for (int i=0; i < H3D_GetNbinsX(histogram); i++ ) H1D_SetBinContent(499,i,H3D_GetBinContent(histogram,i,0,slice));
399
    } else {
221 f9daq 400
      H1D_Init(499,"projection","projection", H3D_GetNbinsY(histogram), H3D_GetMinY(histogram),H3D_GetMaxY(histogram));
209 f9daq 401
      for (int i=0; i < H3D_GetNbinsY(histogram); i++ ) H1D_SetBinContent(499,i,H3D_GetBinContent(histogram,0,i,slice));
402
    }
403
    H1D_Draw(499, panel, control, plothandle);
404
  } else {
405
 
406
    if (*plothandle> 0 ) DeleteGraphPlot (panel, control, *plothandle, VAL_IMMEDIATE_DRAW);
407
    H3D_SetRangeColors(H3D_GetMin(histogram),H3D_GetMax(histogram));
408
    *plothandle = PlotScaledIntensity (panel, control,
409
                                       H3D_GetSliceXYData(histogram, slice),
410
                                       H3D_GetNbinsX(histogram),
411
                                       H3D_GetNbinsY(histogram),
412
                                       VAL_DOUBLE,
413
                                       H3D_GetStepY(histogram),
221 f9daq 414
                                       H3D_GetYBinCenter(histogram,0),
209 f9daq 415
                                       H3D_GetStepX(histogram),
221 f9daq 416
                                       H3D_GetXBinCenter(histogram,0),
209 f9daq 417
                                       colormap->array,
418
                                       colormap->HiColor,
419
                                       colormap->numberofColors, 1, 0);
420
  }
421
  ProcessSystemEvents ();
422
  return *plothandle;
423
 
424
}
425
#endif