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