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