Subversion Repositories f9daq

Rev

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

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