1 |
/***************************************************************** |
2 |
Function to read AC physics data v1.1 |
3 |
Author: Petter Hofverberg, petter@particle.kth.se |
4 |
|
5 |
0408 v1.0 alive |
6 |
0409 Added shifting operators to fix problem with odd data (BBAC|11DD) |
7 |
----- |
8 |
Errors: (returned to the main program as the variable "err") |
9 |
err= |
10 |
0 - Ok |
11 |
1 - Data found, but corrupt |
12 |
2 - Data dont found in buffer |
13 |
|
14 |
ToDo: i) do it endian-independent |
15 |
ii) check for more events in the same file? |
16 |
******************************************************************/ |
17 |
|
18 |
#include <stdio.h> |
19 |
#include "AC.h" |
20 |
|
21 |
int i,err,found,j; |
22 |
//unsigned short buffer[1000]; |
23 |
//unsigned short tmp1,tmp2; |
24 |
unsigned short buffer[1000]; |
25 |
unsigned short tmp1; |
26 |
|
27 |
struct physicsstruct physicsdata; |
28 |
struct physicsstruct *physicspointer; |
29 |
|
30 |
struct calibstruct calibdata; |
31 |
struct calibstruct *calibpointer; |
32 |
|
33 |
|
34 |
//int ACphysics(int length,unsigned short* datapointer,struct physicsstruct* physicspointer) |
35 |
int ACphysics(int length,unsigned char* datapointer,struct physicsstruct* physicspointer) |
36 |
{ |
37 |
|
38 |
int iter=2; |
39 |
|
40 |
*physicspointer=physicsdata; |
41 |
|
42 |
//look for header AC11, then cut out the event to a temp buffer |
43 |
while(found==0 && iter<length) |
44 |
{ |
45 |
tmp1 = ((*datapointer) << 8) | *(datapointer+1); |
46 |
|
47 |
if(tmp1==0xAC11) |
48 |
//if(tmp1==0xAC11) //flipped |
49 |
{ |
50 |
found=1; |
51 |
for(i=0;i<64;i++) |
52 |
{ |
53 |
buffer[i]=((*datapointer) << 8) | *(datapointer+1); |
54 |
datapointer=datapointer+2; |
55 |
} |
56 |
tmp1=0x0000; |
57 |
} |
58 |
else{ |
59 |
datapointer++; |
60 |
iter=iter+1;} |
61 |
} |
62 |
|
63 |
//check errors |
64 |
if(found==0) |
65 |
err=2; |
66 |
else if(buffer[1]==0x22AC || buffer[1]==0xAC22) |
67 |
err=0; |
68 |
else |
69 |
err=1; |
70 |
|
71 |
//Fill physicsdata from buffer |
72 |
for(i=0;i<2;i++) physicsdata.header[i]=buffer[i]; |
73 |
physicsdata.status=buffer[2]; |
74 |
physicsdata.hitmap=buffer[3]; |
75 |
for(i=0;i<6;i++) physicsdata.regist[i]=buffer[4+i]; |
76 |
for(i=0;i<16;i++) physicsdata.shift[i]=buffer[10+i]; |
77 |
for(i=0;i<16;i++) physicsdata.counters[i]=buffer[26+i]; |
78 |
for(i=0;i<8;i++) physicsdata.coinc[i]=buffer[42+i]; |
79 |
physicsdata.trigg=buffer[50]; |
80 |
for(i=0;i<2;i++) physicsdata.clock[i]=buffer[51+i]; |
81 |
for(i=0;i<2;i++) physicsdata.temp[i]=buffer[53+i]; |
82 |
for(i=0;i<8;i++) physicsdata.DAC[i]=buffer[55+i]; |
83 |
physicsdata.CRC=buffer[63]; |
84 |
|
85 |
return err; |
86 |
|
87 |
} |
88 |
|
89 |
|
90 |
int ACcalib(int length, unsigned char* datapointer, struct calibstruct* calibpointer) |
91 |
{ |
92 |
|
93 |
int iter=2; |
94 |
|
95 |
//point struct-pointer to calibdata |
96 |
*calibpointer=calibdata; |
97 |
|
98 |
//look for header 0xACCA |
99 |
while(found==0 && iter<length) |
100 |
{ |
101 |
tmp1 = ((*datapointer) << 8) | *(datapointer+1); |
102 |
|
103 |
if(tmp1==0xACCA) |
104 |
{ |
105 |
found=1; |
106 |
for(i=0;i<8256;i++) |
107 |
{ |
108 |
buffer[i]=((*datapointer) << 8) | *(datapointer+1); |
109 |
datapointer=datapointer+2; |
110 |
} |
111 |
tmp1=0x0000; |
112 |
} |
113 |
else{ |
114 |
datapointer++; |
115 |
iter=iter+1;} |
116 |
} |
117 |
|
118 |
//check errors |
119 |
if(found==0) |
120 |
err=2; |
121 |
else if(buffer[8254]==0xAC22) |
122 |
err=0; |
123 |
else |
124 |
err=1; |
125 |
|
126 |
printf("length: %d\n iter: %d\n",length,iter); |
127 |
|
128 |
//Fill calibdata from buffer |
129 |
calibdata.header=buffer[0]; |
130 |
for(i=0;i<5;i++) calibdata.status[i]=buffer[1+i]; |
131 |
for(i=0;i<8;i++) calibdata.temp[i]=buffer[6+i]; |
132 |
for(i=0;i<8;i++) calibdata.DAC1[i]=buffer[14+i]; |
133 |
for(i=0;i<8;i++) calibdata.DAC2[i]=buffer[22+i]; |
134 |
for(i=0;i<6;i++) calibdata.regist[i]=buffer[30+i]; |
135 |
for(i=0;i<8;i++) calibdata.time[i]=buffer[36+i]; |
136 |
calibdata.n_tr=buffer[44]; |
137 |
for(i=0;i<16;i++) calibdata.hitmap_tr[i]=buffer[45+i]; |
138 |
for(i=0;i<4096;i++) calibdata.curve1[i]=buffer[61+i]; |
139 |
for(i=0;i<4096;i++) calibdata.curve2[i]=buffer[4157+i]; |
140 |
calibdata.iCRC=buffer[8253]; |
141 |
calibdata.tail=buffer[8254]; |
142 |
calibdata.CRC=buffer[8255]; |
143 |
|
144 |
/* |
145 |
//debug |
146 |
if(err!=2) |
147 |
{ |
148 |
printf("header..... %hx\n",calibdata.header); |
149 |
printf("status..... %hx\n",calibdata.status[0]); |
150 |
printf("temp....... %hx\n",calibdata.temp[0]); |
151 |
printf("DAC1....... %hx\n",calibdata.DAC1[0]); |
152 |
printf("DAC2....... %hx\n",calibdata.DAC2[0]); |
153 |
printf("regist..... %hx\n",calibdata.regist[0]); |
154 |
printf("time....... %hx\n",calibdata.time[0]); |
155 |
printf("n_tr....... %hx\n",calibdata.n_tr); |
156 |
printf("hitmap_tr.. %hx\n",calibdata.hitmap_tr[0]); |
157 |
printf("curve1..... %hx\n",calibdata.curve1[0]); |
158 |
printf("curve2..... %hx\n",calibdata.curve2[0]); |
159 |
printf("iCRC....... %hx\n",calibdata.iCRC); |
160 |
printf("tail....... %hx\n",calibdata.tail); |
161 |
printf("CRC........ %hx\n",calibdata.CRC); |
162 |
} |
163 |
printf("err: %d\n",err); |
164 |
*/ |
165 |
|
166 |
return err; |
167 |
} |
168 |
|
169 |
|
170 |
|
171 |
//transposing function |
172 |
int flip(int i16) |
173 |
{ |
174 |
int temp; |
175 |
|
176 |
temp = 0; |
177 |
temp |= 0xFF00 & ((0x00FF & i16) << 8); |
178 |
temp |= 0x00FF & ((0xFF00 & i16) >> 8); |
179 |
return temp; |
180 |
} |