Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
326 | 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 "H1D.h" |
||
10 | |||
11 | #define H1DMAX 500 |
||
12 | H1D *h1[H1DMAX]; |
||
13 | //int Printf(char *format, ...); |
||
14 | |||
15 | int H1DClear(int h1d) { |
||
16 | if (!h1[h1d]) return -1; |
||
17 | memset(h1[h1d]->data, 0,h1[h1d]->size ); |
||
18 | h1[h1d]->min=0; |
||
19 | h1[h1d]->max=0; |
||
20 | h1[h1d]->nentries=0; |
||
21 | return 0; |
||
22 | |||
23 | } |
||
24 | |||
25 | int H1DPrint(int h1d) { |
||
26 | if (!h1[h1d]) return -1; |
||
27 | //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 ) ; |
||
28 | return 0; |
||
29 | |||
30 | } |
||
31 | |||
32 | int H1DExist(int h) { |
||
33 | if (h1[h]) return 1; |
||
34 | else return 0; |
||
35 | } |
||
36 | |||
37 | |||
38 | int H1DGetBin(int h, double value) { |
||
39 | int nx,xmin,dx; |
||
40 | int bin; |
||
41 | |||
42 | nx = H1DGetNbinsX(h); |
||
43 | xmin= H1DGetMinX(h); |
||
44 | dx = H1DGetStepX(h); |
||
45 | |||
46 | if (dx==0) return -1; |
||
47 | if (value<xmin) return -1; |
||
48 | bin = (int)((value-xmin)/dx); |
||
49 | if (bin>=nx) return -1; |
||
50 | else return bin; |
||
51 | } |
||
52 | |||
53 | int H1DFill(int h1d,double x, double val) { |
||
54 | |||
55 | int ix; |
||
56 | if (!h1[h1d]) return -1; |
||
57 | |||
58 | ix = H1DGetBin(h1d,x); |
||
59 | if (ix<0) return ix; |
||
60 | |||
61 | h1[h1d]->data[ix]+=val; |
||
62 | //Printf("%d %d data %f %f\n",x,y,val, h1[h1d]->data[idx]); |
||
63 | if (h1[h1d]->data[ix]>h1[h1d]->max) h1[h1d]->max= h1[h1d]->data[ix]; |
||
64 | if (h1[h1d]->data[ix]<h1[h1d]->min) h1[h1d]->min= h1[h1d]->data[ix]; |
||
65 | h1[h1d]->nentries++; |
||
66 | return 0; |
||
67 | } |
||
68 | |||
69 | |||
70 | int H1DFillBin(int h1d,int x, double val) { |
||
71 | |||
72 | if (!h1[h1d]) { |
||
73 | //Printf("FillH1D error h1d is not initialized\n"); |
||
74 | return -1; |
||
75 | } |
||
76 | |||
77 | h1[h1d]->data[x]+=val; |
||
78 | //Printf("%d %d data %f %f\n",x,y,val, h1[h1d]->data[idx]); |
||
79 | if (h1[h1d]->data[x]>h1[h1d]->max) h1[h1d]->max= h1[h1d]->data[x]; |
||
80 | if (h1[h1d]->data[x]<h1[h1d]->min) h1[h1d]->min= h1[h1d]->data[x]; |
||
81 | h1[h1d]->nentries++; |
||
82 | return 0; |
||
83 | } |
||
84 | |||
85 | int H1DSetBinContent(int h1d,int x, double val) { |
||
86 | |||
87 | if (!h1[h1d]) { |
||
88 | //Printf("FillH1D error h1d is not initialized\n"); |
||
89 | return -1; |
||
90 | } |
||
91 | |||
92 | h1[h1d]->data[x]=val; |
||
93 | //Printf("%d %d data %f %f\n",x,y,val, h1[h1d]->data[idx]); |
||
94 | if (h1[h1d]->data[x]>h1[h1d]->max) h1[h1d]->max= h1[h1d]->data[x]; |
||
95 | if (h1[h1d]->data[x]<h1[h1d]->min) h1[h1d]->min= h1[h1d]->data[x]; |
||
96 | h1[h1d]->nentries++; |
||
97 | return 0; |
||
98 | } |
||
99 | |||
100 | |||
101 | |||
102 | double H1DGetBinContent(int h1d,int atx) { |
||
103 | |||
104 | int idx; |
||
105 | if (!h1[h1d]) return 0; |
||
106 | if (h1[h1d]->nx <= atx) return 0; |
||
107 | if (atx<0) return 0; |
||
108 | if (atx*sizeof(double) < h1[h1d]->size ) return h1[h1d]->data[atx]; |
||
109 | |||
110 | |||
111 | return 0; |
||
112 | } |
||
113 | |||
114 | |||
115 | int H1DInit(int h1d,char *name, char *title,int nx, double minx, double stepx) { |
||
116 | |||
117 | if (h1[h1d]) { |
||
118 | |||
119 | free(h1[h1d]->data); |
||
120 | free(h1[h1d]); |
||
121 | h1[h1d] = NULL; |
||
122 | } |
||
123 | // if (h1d!=H1DMAX-1) printf("InitH1D hID=%d\n",h1d); |
||
124 | h1[h1d] = (H1D *) malloc(sizeof(H1D)); |
||
125 | //h2 =h1d; |
||
126 | |||
127 | H1DSetTitle(h1d,title); |
||
128 | H1DSetName(h1d,name); |
||
129 | H1DSetTitleX(h1d,"x"); |
||
130 | ; |
||
131 | h1[h1d]->id=H1D_ID; |
||
132 | h1[h1d]->nx = nx; |
||
133 | |||
134 | |||
135 | h1[h1d]->minx = minx; |
||
136 | |||
137 | h1[h1d]->stepx = stepx; |
||
138 | |||
139 | h1[h1d]->size = h1[h1d]->nx*sizeof(double); |
||
140 | h1[h1d]->data = (double *) malloc(h1[h1d]->size); |
||
141 | h1[h1d]->len=sizeof(H1D)-sizeof(double *)+h1[h1d]->size; |
||
142 | H1DClear(h1d); |
||
143 | H1DPrint(h1d); |
||
144 | //Printf("InitH1D 0x%x\n", h1d ); |
||
145 | return 0; |
||
146 | |||
147 | } |
||
148 | |||
149 | |||
150 | double H1DGetXBinCenter(int h1d,int xbin) { |
||
151 | return h1[h1d]->minx+xbin*h1[h1d]->stepx; |
||
152 | } |
||
153 | |||
154 | |||
155 | int H1DWrite2File(int h1d,FILE *fp) { |
||
156 | |||
157 | if (!fp) return -1; |
||
158 | //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); |
||
159 | //printf("H1D sz=%d %d\n",sizeof(double),sizeof(double *)); |
||
160 | if (!H1DExist(h1d)){ |
||
161 | printf("Histogram H1D=%d is not initialized\n",h1d); |
||
162 | return -1; |
||
163 | } |
||
164 | fwrite (h1[h1d], 1, sizeof(H1D)-sizeof(double *), fp); |
||
165 | fwrite (h1[h1d]->data, 1, h1[h1d]->size, fp); |
||
166 | return 0; |
||
167 | } |
||
168 | |||
169 | int H1DWrite(int h1d,const char *fname,const char *opt) { |
||
170 | FILE *fp=fopen(fname,opt); |
||
171 | H1DWrite2File(h1d,fp); |
||
172 | fclose(fp); |
||
173 | return 0; |
||
174 | } |
||
175 | |||
176 | |||
177 | |||
178 | |||
179 | int H1DSetTitle(int h1d,char *title) { |
||
180 | if (!h1[h1d]) { |
||
181 | printf("h1d %d does not exist %s\n",h1d, title); |
||
182 | return 0; |
||
183 | } |
||
184 | sprintf(h1[h1d]->title,"%s",title); |
||
185 | return 0; |
||
186 | } |
||
187 | |||
188 | |||
189 | int H1DSetTitleX(int h1d,char *title) { |
||
190 | sprintf(h1[h1d]->titlex,"%s",title); |
||
191 | return 0; |
||
192 | } |
||
193 | |||
194 | |||
195 | int H1DSetTitleY(int h1d,char *title) { |
||
196 | sprintf(h1[h1d]->titley,"%s",title); |
||
197 | return 0; |
||
198 | } |
||
199 | |||
200 | |||
201 | char * H1DGetTitleX(int h1d) { |
||
202 | return h1[h1d]->titlex; |
||
203 | } |
||
204 | |||
205 | char * H1DGetTitleY(int h1d) { |
||
206 | return h1[h1d]->titley; |
||
207 | } |
||
208 | |||
209 | char * H1DGetTitle(int h1d) { |
||
210 | return h1[h1d]->title; |
||
211 | } |
||
212 | |||
213 | |||
214 | |||
215 | int H1DSetName(int h1d,char *title) { |
||
216 | sprintf(h1[h1d]->name,"%s",title); |
||
217 | return 0; |
||
218 | } |
||
219 | |||
220 | int H1DGetNbinsX(int h) { |
||
221 | if (h1[h]) return h1[h]->nx; |
||
222 | else return 0; |
||
223 | } |
||
224 | |||
225 | |||
226 | |||
227 | double H1DGetMinX(int h) { |
||
228 | if (h1[h]) return h1[h]->minx; |
||
229 | else return 0; |
||
230 | } |
||
231 | |||
232 | |||
233 | |||
234 | double H1DGetStepX(int h) { |
||
235 | if (h1[h]) return h1[h]->stepx; |
||
236 | else return 0; |
||
237 | } |
||
238 | |||
239 | |||
240 | double H1DGetMin(int h) { |
||
241 | if (h1[h]) return h1[h]->min; |
||
242 | else return 0; |
||
243 | } |
||
244 | |||
245 | double H1DGetMax(int h) { |
||
246 | if (h1[h]) return h1[h]->max; |
||
247 | else return 0; |
||
248 | } |
||
249 | |||
250 | double * H1DGetData(int h) { |
||
251 | if (h1[h]) return h1[h]->data; |
||
252 | else return NULL; |
||
253 | } |
||
254 |