source: trunk/MagicSoft/Simulation/Detector/ReflectorII/geometry.c@ 1602

Last change on this file since 1602 was 1535, checked in by bigongia, 22 years ago
Version 0.6. Changed output format: added run header, changed event header, added ascii parameter files attached at the end of every output file to keep all info.
File size: 6.5 KB
Line 
1#include <stdio.h>
2#include <string.h>
3#include <stdlib.h>
4#include <math.h>
5#include "diag.h"
6#include "geometry.h"
7#include "init.h"
8
9extern char line[]; /* parsing buf. (init) */
10extern char axisdev_filename[256], reflectivity_filename[256];
11
12float ct_Focal_mean; /* focal dist. (mean) (cm) */
13float ct_PSpread_mean; /* pt. spread fn. (mean) (cm) */
14float ct_BlackSpot_rad; /* black spot radius (cm) */
15float ct_RMirror; /* rad. of single mirror (cm) */
16int ct_NMirrors=0; /* number of mirrors */
17float ct_CameraWidth; /* camera width (cm) */
18int ct_NPixels; /* number of pixels */
19float ct_PixelWidth; /* pixel width (cm) */
20
21mirror *ct_data=NULL; /* ptr to mirror data */
22
23int nReflectivity=0; /* elements in refl. table */
24float *Reflectivity[2]; /* reflectivity table */
25float *AxisDeviation[2]; /* axis deviation table */
26float *ct_Focal; /* focal length table */
27
28/* Prototypes */
29static void ReadMirrorTable(FILE *geofile);
30static void ReadReflectivity(char *datname);
31static void ReadAxisDev(char *datname);
32static void ReadFocals(void);
33
34static void ReadMirrorTable(FILE *geofile)
35{ int i; /* Mirror index */
36
37 if ((ct_data=(mirror *)malloc(sizeof(mirror)*ct_NMirrors)) == NULL)
38 FatalError(MIRR_ALLOC_FTL, ct_NMirrors);
39 Log(MIRR_ALLOC_LOG, ct_NMirrors);
40 Log(MIRR_TABLE_LOG);
41
42 Log(READ_ASCII_LOG);
43 for (i=0; i<ct_NMirrors; i++)
44 {
45 if (12 != fscanf(geofile, "%d %f %f %f %f %f %f %f %f %f %f %f",
46 &ct_data[i].i, &ct_data[i].f,
47 &ct_data[i].sx, &ct_data[i].sy,
48 &ct_data[i].x, &ct_data[i].y, &ct_data[i].z,
49 &ct_data[i].theta, &ct_data[i].phi,
50 &ct_data[i].xn, &ct_data[i].yn, &ct_data[i].zn))
51 break;
52 }
53 if (i < ct_NMirrors)
54 FatalError(MIRR_FEW___FTL, i);
55
56} /* end of ReadMirrorTable */
57
58static void ReadReflectivity(char *datname)
59{
60 FILE *datfile = fopen(datname, "r");
61 int current = 0;
62
63 if (datfile == NULL)
64 FatalError(RFLF_ERROR_FTL, datname);
65 else
66 printf("Reading file %s\n", datname);
67
68 while (fgets(line, LINE_MAX_LENGTH, datfile))
69 {
70 if (line[0] == '#') continue;
71
72 if (nReflectivity == 0)
73 {
74 nReflectivity = atoi(line);
75 if (nReflectivity)
76 {
77 if ((Reflectivity[0] =
78 (float *) malloc(sizeof(float) * nReflectivity)) == NULL ||
79 (Reflectivity[1] =
80 (float *) malloc(sizeof(float) * nReflectivity)) == NULL)
81 FatalError(REFL_ALLOC_FTL, nReflectivity);
82 }
83 }
84 else if (2 == sscanf(line, "%f %f", &Reflectivity[0][current],
85 &Reflectivity[1][current]))
86 {
87 current++;
88 if (current >= nReflectivity) break;
89 }
90 }
91 fclose(datfile);
92
93 nReflectivity = current;
94
95} /* end of ReadReflectivity */
96
97static void ReadAxisDev(char *datname)
98{ FILE *datfile = fopen(datname, "r");
99 int current = 0;
100
101 if (datfile == NULL)
102 FatalError(AXIS_ERROR_FTL, datname);
103 else
104 printf("Reading file %s\n", axisdev_filename);
105
106 if ((AxisDeviation[0]=
107 (float *) malloc(sizeof(float) * ct_NMirrors)) == NULL
108 || (AxisDeviation[1]=
109 (float *) malloc(sizeof(float) * ct_NMirrors)) == NULL)
110 FatalError(AXIS_ALLOC_FTL, ct_NMirrors);
111 while (fgets(line, LINE_MAX_LENGTH, datfile))
112 { if (line[0] == '#') continue;
113 if (2==sscanf(line, "%f %f",
114 &AxisDeviation[0][current], &AxisDeviation[1][current]));
115 { current++;
116 if (current >= ct_NMirrors) break; }}
117 fclose(datfile);
118
119 if (current != ct_NMirrors)
120 FatalError(AXIS_FEW___FTL, current, ct_NMirrors);
121} /* end of ReadAxisDev */
122
123static void ReadFocals(void)
124{ int loop;
125
126 if ((ct_Focal = (float *) malloc(sizeof(float) * ct_NMirrors)) == NULL)
127 FatalError(FOCL_FEW___FTL, ct_NMirrors);
128
129 for (loop=0; loop<ct_NMirrors; loop++)
130 ct_Focal[loop] = ct_data[loop].f;
131} /* end of ReadFocals */
132
133void GeometrySwitch(FILE *geofile)
134{ char *value_ptr = NULL; /* ptr at parm value */
135 int switch_end = FALSE; /* bool to exit loop */
136 extern char whites[]; /* white chars (init) */
137 extern int ParseLine(FILE *parfile, /* FILE with parms */
138 const char *token_list[], /* array w/tokens */
139 int tokens, /* nr of tokens */
140 char **value_ptr); /* ptr->parm val. */
141
142 /* Initialise arrays */
143 Reflectivity[0] = AxisDeviation[0] = NULL;
144
145 do
146 { switch(ParseLine(geofile, ctparms, ARRAY_SZ(ctparms), &value_ptr))
147 { case type:
148 if (1 != atoi(value_ptr))
149 FatalError(TYPE_ERROR_FTL);
150 break;
151 case focal_distance:
152 Log(LOG__FLOAT_LOG, "focal distance (average, cm)",
153 ct_Focal_mean = (float) atof(value_ptr));
154 break;
155 case focal_std: /* not implemented. */
156 break;
157 case point_spread:
158 Log(LOG__FLOAT_LOG, "point spread fn. sigma (average, cm)",
159 ct_PSpread_mean = (float) atof(value_ptr));
160 break;
161 case point_std: /* not implemented */
162 break;
163 case adjustment_dev: /* not implemented */
164 break;
165 case black_spot:
166 Log(LOG__FLOAT_LOG, "radius of black spot (cm)",
167 ct_BlackSpot_rad = (float) atof(value_ptr));
168 break;
169 case n_mirrors:
170 Log(LOG__INT___LOG, "number of mirrors",
171 ct_NMirrors = atoi(value_ptr));
172 break;
173 case r_mirror:
174 Log(LOG__FLOAT_LOG, "single mirror radius (cm)",
175 ct_RMirror = (float) atof(value_ptr));
176 break;
177 case camera_width:
178 Log(LOG__FLOAT_LOG, "camera radius (cm)",
179 ct_CameraWidth = (float) atof(value_ptr));
180 break;
181 case n_pixels:
182 Log(LOG__INT___LOG, "number of pixels",
183 ct_NPixels = atoi(value_ptr));
184 break;
185 case pixel_width:
186 Log(LOG__FLOAT_LOG, "pixel width (cm)",
187 ct_PixelWidth = (float) atof(value_ptr));
188 break;
189 case n_centralpixels:
190 /* this parameter is for camera, not for reflector */
191 break;
192 case n_gappixels:
193 /* this parameter is for camera, not for reflector */
194 break;
195 case refl_file:
196 ReadReflectivity(value_ptr);
197 break;
198 case axisdev_file:
199 ReadAxisDev(value_ptr);
200 break;
201 case define_mirrors:
202 if (ct_NMirrors) ReadMirrorTable(geofile);
203 else FatalError(MIRR_NSPEC_FTL);
204 switch_end = TRUE;
205 break;
206 default: switch_end = TRUE;
207 break; }}
208 while (!switch_end);
209 fclose(geofile);
210
211 if (strlen(reflectivity_filename) == 0)
212 strcpy(reflectivity_filename, REFLECTIVITY_FILE);
213 if (Reflectivity[0] == NULL) ReadReflectivity(reflectivity_filename);
214
215 if (strlen(axisdev_filename) == 0)
216 strcpy(axisdev_filename, AXISDEVIATION_FILE);
217 if (AxisDeviation[0]== NULL) ReadAxisDev(axisdev_filename);
218
219
220 ReadFocals();
221} /* end of GeometrySwitch */
222
Note: See TracBrowser for help on using the repository browser.