1 |
/***************************************************************** |
/*******************************v1.2******************************** |
2 |
Function to read AC physics data v1.1 |
|
3 |
|
Functions to read AC physics data (ACphysics) and AC calibration |
4 |
|
data (ACcalib). |
5 |
Author: Petter Hofverberg, petter@particle.kth.se |
Author: Petter Hofverberg, petter@particle.kth.se |
6 |
|
|
7 |
0408 v1.0 alive |
0410 v1.0 alive |
8 |
0409 Added shifting operators to fix problem with odd data (BBAC|11DD) |
0412 v1.1 fixed problem with found, and changed headers |
9 |
----- |
0502 v1.2 added a crc check |
10 |
|
|
11 |
Errors: (returned to the main program as the variable "err") |
Errors: (returned to the main program as the variable "err") |
12 |
err= |
err= |
13 |
0 - Ok |
0xFF - data (physics or calibration) from both cards found |
14 |
1 - Data found, but corrupt |
0xF0 - only data from main card found |
15 |
2 - Data dont found in buffer |
0x0F - only data from extra card found |
16 |
|
0x00 - no data found |
17 |
|
|
18 |
ToDo: i) do it endian-independent |
CRCcheck: 1 - OK, 0 - error |
|
ii) check for more events in the same file? |
|
19 |
******************************************************************/ |
******************************************************************/ |
20 |
|
|
21 |
#include <stdio.h> |
#include <stdio.h> |
22 |
#include "AC.h" |
#include "AC.h" |
23 |
|
#define TRUE 1 |
24 |
|
#define FALSE 0 |
25 |
|
|
26 |
int i,err,found,j; |
int i,j,found; |
|
//unsigned short buffer[1000]; |
|
|
//unsigned short tmp1,tmp2; |
|
27 |
unsigned short buffer[1000]; |
unsigned short buffer[1000]; |
28 |
unsigned short tmp1; |
unsigned short tmp1,tmp2,check; |
29 |
|
unsigned char err; |
30 |
|
|
31 |
struct physicsstruct physicsdata; |
struct physicsstruct physicsdata[2]; |
32 |
struct physicsstruct *physicspointer; |
struct physicsstruct *physicspointer; |
33 |
|
|
34 |
struct calibstruct calibdata; |
struct calibstruct calibdata; |
35 |
struct calibstruct *calibpointer; |
struct calibstruct *calibpointer; |
36 |
|
|
37 |
|
|
38 |
//int ACphysics(int length,unsigned short* datapointer,struct physicsstruct* physicspointer) |
//CRC check function |
39 |
int ACphysics(int length,unsigned char* datapointer,struct physicsstruct* physicspointer) |
short crc(short old,short new) |
40 |
|
{ |
41 |
|
union crc_data { |
42 |
|
short word; |
43 |
|
struct bit_field { |
44 |
|
unsigned b0:1; |
45 |
|
unsigned b1:1; |
46 |
|
unsigned b2:1; |
47 |
|
unsigned b3:1; |
48 |
|
unsigned b4:1; |
49 |
|
unsigned b5:1; |
50 |
|
unsigned b6:1; |
51 |
|
unsigned b7:1; |
52 |
|
unsigned b8:1; |
53 |
|
unsigned b9:1; |
54 |
|
unsigned b10:1; |
55 |
|
unsigned b11:1; |
56 |
|
unsigned b12:1; |
57 |
|
unsigned b13:1; |
58 |
|
unsigned b14:1; |
59 |
|
unsigned b15:1; |
60 |
|
} bit; |
61 |
|
} c,d,r; |
62 |
|
|
63 |
|
c.word = old; |
64 |
|
d.word = new; |
65 |
|
r.word = 0; |
66 |
|
|
67 |
|
r.bit.b0 = c.bit.b0 ^ c.bit.b4 ^ c.bit.b8 ^ c.bit.b11 ^ c.bit.b12 ^ |
68 |
|
d.bit.b0 ^ d.bit.b4 ^ d.bit.b8 ^ d.bit.b11 ^ d.bit.b12; |
69 |
|
|
70 |
|
r.bit.b1 = c.bit.b1 ^ c.bit.b5 ^ c.bit.b9 ^ c.bit.b12 ^ c.bit.b13 ^ |
71 |
|
d.bit.b1 ^ d.bit.b5 ^ d.bit.b9 ^ d.bit.b12 ^ d.bit.b13; |
72 |
|
|
73 |
|
r.bit.b2 = c.bit.b2 ^ c.bit.b6 ^ c.bit.b10 ^ c.bit.b13 ^ c.bit.b14 ^ |
74 |
|
d.bit.b2 ^ d.bit.b6 ^ d.bit.b10 ^ d.bit.b13 ^ d.bit.b14; |
75 |
|
|
76 |
|
r.bit.b3 = c.bit.b3 ^ c.bit.b7 ^ c.bit.b11 ^ c.bit.b14 ^ c.bit.b15 ^ |
77 |
|
d.bit.b3 ^ d.bit.b7 ^ d.bit.b11 ^ d.bit.b14 ^ d.bit.b15; |
78 |
|
|
79 |
|
r.bit.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it.b12 ^ c.bit.b13 ^ |
103 |
|
c.bit.b15 ^ |
104 |
|
d.bit.b4 ^ d.bit.b8 ^ d.bit.b9 ^ d.bit.b12 ^ d.bit.b13 ^ |
105 |
|
d.bit.b15; |
106 |
|
|
107 |
|
r.bit.b10 = c.bit.b5 ^ c.bit.b9 ^ c.bit.b10 ^ c.bit.b13 ^ c.bit.b14 ^ |
108 |
|
d.bit.b5 ^ d.bit.b9 ^ d.bit.b10 ^ d.bit.b13 ^ d.bit.b14; |
109 |
|
|
110 |
|
r.bit.b11 = c.bit.b6 ^ c.bit.b10 ^ c.bit.b11 ^ c.bit.b14 ^ c.bit.b15 ^ |
111 |
|
d.bit.b6 ^ d.bit.b10 ^ d.bit.b11 ^ d.bit.b14 ^ d.bit.b15; |
112 |
|
|
113 |
|
r.bit.b12 = c.bit.b0 ^ c.bit.b4 ^ c.bit.b7 ^ c.bit.b8 ^ c.bit.b15 ^ |
114 |
|
d.bit.b0 ^ d.bit.b4 ^ d.bit.b7 ^ d.bit.b8 ^ d.bit.b15; |
115 |
|
|
116 |
|
r.bit.b13 = c.bit.b1 ^ c.bit.b5 ^ c.bit.b8 ^ c.bit.b9 ^ |
117 |
|
d.bit.b1 ^ d.bit.b5 ^ d.bit.b8 ^ d.bit.b9; |
118 |
|
|
119 |
|
r.bit.b14 = c.bit.b2 ^ c.bit.b6 ^ c.bit.b9 ^ c.bit.b10 ^ |
120 |
|
d.bit.b2 ^ d.bit.b6 ^ d.bit.b9 ^ d.bit.b10; |
121 |
|
|
122 |
|
r.bit.b15 = c.bit.b3 ^ c.bit.b7 ^ c.bit.b10 ^ c.bit.b11 ^ |
123 |
|
d.bit.b3 ^ d.bit.b7 ^ d.bit.b10 ^ d.bit.b11; |
124 |
|
|
125 |
|
return r.word; |
126 |
|
} |
127 |
|
|
128 |
|
|
129 |
|
//Fill physicsdata from buffer |
130 |
|
void fillphys(int k,unsigned short *buff) |
131 |
{ |
{ |
132 |
|
for(i=0;i<2;i++) physicsdata[k].header[i]=buff[i]; |
133 |
|
physicsdata[k].status=buff[2]; |
134 |
|
physicsdata[k].hitmap=buff[3]; |
135 |
|
for(i=0;i<6;i++) physicsdata[k].regist[i]=buff[4+i]; |
136 |
|
for(i=0;i<16;i++) physicsdata[k].shift[i]=buff[10+i]; |
137 |
|
for(i=0;i<16;i++) physicsdata[k].counters[i]=buff[26+i]; |
138 |
|
for(i=0;i<8;i++) physicsdata[k].coinc[i]=buff[42+i]; |
139 |
|
physicsdata[k].trigg=buff[50]; |
140 |
|
for(i=0;i<2;i++) physicsdata[k].clock[i]=buff[51+i]; |
141 |
|
for(i=0;i<2;i++) physicsdata[k].temp[i]=buff[53+i]; |
142 |
|
for(i=0;i<8;i++) physicsdata[k].DAC[i]=buff[55+i]; |
143 |
|
physicsdata[k].CRC=buff[63]; |
144 |
|
if(check==physicsdata[k].CRC) |
145 |
|
physicsdata[k].CRCcheck=TRUE; |
146 |
|
else |
147 |
|
physicsdata[k].CRCcheck=FALSE; |
148 |
|
} |
149 |
|
|
150 |
int iter=2; |
//Fill calibdata from buffer |
151 |
|
void fillcalib(unsigned short *buff) |
152 |
|
{ |
153 |
|
for(i=0;i<2;i++) calibdata.header[i]=buff[i]; |
154 |
|
for(i=0;i<5;i++) calibdata.status[i]=buff[2+i]; |
155 |
|
for(i=0;i<8;i++) calibdata.temp[i]=buff[7+i]; |
156 |
|
for(i=0;i<8;i++) calibdata.DAC1[i]=buff[15+i]; |
157 |
|
for(i=0;i<8;i++) calibdata.DAC2[i]=buff[23+i]; |
158 |
|
for(i=0;i<6;i++) calibdata.regist[i]=buff[31+i]; |
159 |
|
for(i=0;i<8;i++) calibdata.time[i]=buff[37+i]; |
160 |
|
calibdata.n_tr=buff[45]; |
161 |
|
for(i=0;i<16;i++) calibdata.hitmap_tr[i]=buff[46+i]; |
162 |
|
for(i=0;i<4096;i++) calibdata.curve1[i]=buff[62+i]; |
163 |
|
for(i=0;i<4096;i++) calibdata.curve2[i]=buff[4158+i]; |
164 |
|
calibdata.iCRC=buff[8254]; |
165 |
|
calibdata.tail=buff[8255]; |
166 |
|
calibdata.CRC=buff[8256]; |
167 |
|
if(check==calibdata.CRC) |
168 |
|
calibdata.CRCcheck=TRUE; |
169 |
|
else |
170 |
|
calibdata.CRCcheck=FALSE; |
171 |
|
} |
172 |
|
|
173 |
*physicspointer=physicsdata; |
unsigned char ACphysics(int length,unsigned char* datapointer,struct physicsstruct* physicspointer) |
174 |
|
{ |
175 |
|
|
176 |
//look for header AC11, then cut out the event to a temp buffer |
err=0; |
177 |
while(found==0 && iter<length) |
found=0; |
178 |
|
check=0; |
179 |
|
int iter=2; |
180 |
|
for(i=0;i<1000;i++) |
181 |
|
buffer[i]=0; |
182 |
|
fillphys(0,&buffer[0]); |
183 |
|
fillphys(1,&buffer[0]); |
184 |
|
|
185 |
|
//point struct-pointer to physicsdata |
186 |
|
//physicspointer[0]=physicsdata[0]; //card1 |
187 |
|
//physicspointer[1]=physicsdata[1]; //card2 |
188 |
|
//physicspointer=&physicsdata[0]; |
189 |
|
|
190 |
|
//look for top header ACAC, then unpack data to main card buffer or extra card buffer depending on the subheader |
191 |
|
//AC11 for main, AC22 for extra |
192 |
|
while(found<2 && iter<length) |
193 |
{ |
{ |
194 |
tmp1 = ((*datapointer) << 8) | *(datapointer+1); |
tmp1 = ((*datapointer) << 8) | *(datapointer+1); |
195 |
|
tmp2 = ((*(datapointer+2)) << 8) | *(datapointer+3); |
196 |
if(tmp1==0xAC11) |
if(tmp1 == 0xACAC && ((tmp2 == 0xAC11 && (err&0xF0)==0x0) || (tmp2 == 0xAC22 && (err&0x0F)==0x0))) |
|
//if(tmp1==0xAC11) //flipped |
|
197 |
{ |
{ |
|
found=1; |
|
198 |
for(i=0;i<64;i++) |
for(i=0;i<64;i++) |
199 |
{ |
{ |
200 |
buffer[i]=((*datapointer) << 8) | *(datapointer+1); |
buffer[i]=((*datapointer) << 8) | *(datapointer+1); |
201 |
|
if(i<63) |
202 |
|
check=crc(check,buffer[i]); |
203 |
datapointer=datapointer+2; |
datapointer=datapointer+2; |
204 |
} |
} |
205 |
tmp1=0x0000; |
if(tmp2==0xAC11){ //main card |
206 |
|
fillphys(0,&buffer[0]); |
207 |
|
err |= 0xF0; |
208 |
|
} |
209 |
|
else{ //extra card |
210 |
|
fillphys(1,&buffer[0]); |
211 |
|
err |= 0x0F; |
212 |
|
} |
213 |
|
tmp1=tmp2=0x0000; |
214 |
|
check=0x0; |
215 |
|
found++; |
216 |
} |
} |
217 |
else{ |
else{ |
218 |
datapointer++; |
datapointer++; |
219 |
iter=iter+1;} |
iter=iter+1;} |
220 |
} |
} |
221 |
|
//point struct-pointer to physicsdata |
222 |
//check errors |
physicspointer[0]=physicsdata[0]; //card1 |
223 |
if(found==0) |
physicspointer[1]=physicsdata[1]; //card2 |
|
err=2; |
|
|
else if(buffer[1]==0x22AC || buffer[1]==0xAC22) |
|
|
err=0; |
|
|
else |
|
|
err=1; |
|
|
|
|
|
//Fill physicsdata from buffer |
|
|
for(i=0;i<2;i++) physicsdata.header[i]=buffer[i]; |
|
|
physicsdata.status=buffer[2]; |
|
|
physicsdata.hitmap=buffer[3]; |
|
|
for(i=0;i<6;i++) physicsdata.regist[i]=buffer[4+i]; |
|
|
for(i=0;i<16;i++) physicsdata.shift[i]=buffer[10+i]; |
|
|
for(i=0;i<16;i++) physicsdata.counters[i]=buffer[26+i]; |
|
|
for(i=0;i<8;i++) physicsdata.coinc[i]=buffer[42+i]; |
|
|
physicsdata.trigg=buffer[50]; |
|
|
for(i=0;i<2;i++) physicsdata.clock[i]=buffer[51+i]; |
|
|
for(i=0;i<2;i++) physicsdata.temp[i]=buffer[53+i]; |
|
|
for(i=0;i<8;i++) physicsdata.DAC[i]=buffer[55+i]; |
|
|
physicsdata.CRC=buffer[63]; |
|
224 |
|
|
225 |
return err; |
return err; |
226 |
|
|
227 |
} |
} |
228 |
|
|
229 |
|
|
230 |
int ACcalib(int length, unsigned char* datapointer, struct calibstruct* calibpointer) |
unsigned char ACcalib(int length, unsigned char* datapointer, struct calibstruct* calibpointer) |
231 |
{ |
{ |
232 |
|
|
233 |
|
err=0; |
234 |
|
found=0; |
235 |
|
check=0; |
236 |
int iter=2; |
int iter=2; |
237 |
|
for(i=0;i<1000;i++) |
238 |
|
buffer[i]=0; |
239 |
|
fillcalib(&buffer[0]); |
240 |
|
|
241 |
//point struct-pointer to calibdata |
//point struct-pointer to calibdata |
242 |
*calibpointer=calibdata; |
//calibpointer[0]=calibdata[0]; //card1 |
243 |
|
//calibpointer[1]=calibdata[1]; //card2 |
244 |
|
|
245 |
//look for header 0xACCA |
//look for the top header 0xACCA, then the subheaders 0xAC11 and 0xAC22, and unpack the data |
246 |
while(found==0 && iter<length) |
//to each card depending on these. |
247 |
|
while(found<1 && iter<length) |
248 |
{ |
{ |
249 |
tmp1 = ((*datapointer) << 8) | *(datapointer+1); |
tmp1 = ((*datapointer) << 8) | *(datapointer+1); |
250 |
|
tmp2 = ((*(datapointer+2)) << 8) | *(datapointer+3); |
251 |
|
|
252 |
if(tmp1==0xACCA) |
if(tmp1 == 0xACCA && ((tmp2 == 0xAC11 && (err&0xF0)==0x0) || (tmp2 == 0xACA2 && (err&0x0F)==0x0))) |
253 |
{ |
{ |
254 |
found=1; |
for(i=0;i<8258;i++) |
|
for(i=0;i<8256;i++) |
|
255 |
{ |
{ |
256 |
buffer[i]=((*datapointer) << 8) | *(datapointer+1); |
buffer[i]=((*datapointer) << 8) | *(datapointer+1); |
257 |
|
if(i<8257) |
258 |
|
check=crc(check,buffer[i]); |
259 |
datapointer=datapointer+2; |
datapointer=datapointer+2; |
260 |
|
} |
261 |
|
|
262 |
|
if(tmp2==0xAC11){ //main |
263 |
|
fillcalib(&buffer[0]); |
264 |
|
err |= 0xF0; |
265 |
|
} |
266 |
|
else{ //extra |
267 |
|
fillcalib(&buffer[0]); |
268 |
|
err |= 0x0F; |
269 |
} |
} |
270 |
tmp1=0x0000; |
tmp1=tmp2=0x0000; |
271 |
|
found++; |
272 |
} |
} |
273 |
else{ |
else{ |
274 |
datapointer++; |
datapointer++; |
275 |
iter=iter+1;} |
iter=iter+1;} |
|
} |
|
|
|
|
|
//check errors |
|
|
if(found==0) |
|
|
err=2; |
|
|
else if(buffer[8254]==0xAC22) |
|
|
err=0; |
|
|
else |
|
|
err=1; |
|
|
|
|
|
printf("length: %d\n iter: %d\n",length,iter); |
|
|
|
|
|
//Fill calibdata from buffer |
|
|
calibdata.header=buffer[0]; |
|
|
for(i=0;i<5;i++) calibdata.status[i]=buffer[1+i]; |
|
|
for(i=0;i<8;i++) calibdata.temp[i]=buffer[6+i]; |
|
|
for(i=0;i<8;i++) calibdata.DAC1[i]=buffer[14+i]; |
|
|
for(i=0;i<8;i++) calibdata.DAC2[i]=buffer[22+i]; |
|
|
for(i=0;i<6;i++) calibdata.regist[i]=buffer[30+i]; |
|
|
for(i=0;i<8;i++) calibdata.time[i]=buffer[36+i]; |
|
|
calibdata.n_tr=buffer[44]; |
|
|
for(i=0;i<16;i++) calibdata.hitmap_tr[i]=buffer[45+i]; |
|
|
for(i=0;i<4096;i++) calibdata.curve1[i]=buffer[61+i]; |
|
|
for(i=0;i<4096;i++) calibdata.curve2[i]=buffer[4157+i]; |
|
|
calibdata.iCRC=buffer[8253]; |
|
|
calibdata.tail=buffer[8254]; |
|
|
calibdata.CRC=buffer[8255]; |
|
|
|
|
|
/* |
|
|
//debug |
|
|
if(err!=2) |
|
|
{ |
|
|
printf("header..... %hx\n",calibdata.header); |
|
|
printf("status..... %hx\n",calibdata.status[0]); |
|
|
printf("temp....... %hx\n",calibdata.temp[0]); |
|
|
printf("DAC1....... %hx\n",calibdata.DAC1[0]); |
|
|
printf("DAC2....... %hx\n",calibdata.DAC2[0]); |
|
|
printf("regist..... %hx\n",calibdata.regist[0]); |
|
|
printf("time....... %hx\n",calibdata.time[0]); |
|
|
printf("n_tr....... %hx\n",calibdata.n_tr); |
|
|
printf("hitmap_tr.. %hx\n",calibdata.hitmap_tr[0]); |
|
|
printf("curve1..... %hx\n",calibdata.curve1[0]); |
|
|
printf("curve2..... %hx\n",calibdata.curve2[0]); |
|
|
printf("iCRC....... %hx\n",calibdata.iCRC); |
|
|
printf("tail....... %hx\n",calibdata.tail); |
|
|
printf("CRC........ %hx\n",calibdata.CRC); |
|
276 |
} |
} |
277 |
printf("err: %d\n",err); |
*calibpointer=calibdata; //card1 |
278 |
*/ |
|
|
|
|
279 |
return err; |
return err; |
280 |
} |
} |
281 |
|
|
|
|
|
|
|
|
|
//transposing function |
|
|
int flip(int i16) |
|
|
{ |
|
|
int temp; |
|
|
|
|
|
temp = 0; |
|
|
temp |= 0xFF00 & ((0x00FF & i16) << 8); |
|
|
temp |= 0x00FF & ((0xFF00 & i16) >> 8); |
|
|
return temp; |
|
|
} |
|