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