1 |
//If big-little endian problem: switch tmp1&0xFF00 + tmp2&0x00FF -> |
/***************************************************************** |
2 |
// tmp1&0x00FF + tmp2&0xFF00 |
Function to read AC physics data v1.1 |
3 |
#include <stdio.h> |
Author: Petter Hofverberg, petter@particle.kth.se |
4 |
#include "ACphysics.h" |
|
5 |
|
0408 v1.0 alive |
6 |
int ACphysics(int length,unsigned short* datapointer,struct datastruct* physicspointer){ |
0409 Added shifting operators to fix problem with odd data (BBAC|11DD) |
7 |
|
----- |
8 |
//physics structure |
Errors: (returned to the main program as the variable "err") |
9 |
struct datastruct physicsdata; |
err= |
10 |
|
0 - Ok |
11 |
//pointer to struct |
1 - Data found, but corrupt |
12 |
struct datastruct *physicspointer; |
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 |
//read variables |
#include <stdio.h> |
19 |
int i,err,found; |
#include "AC.h" |
20 |
unsigned short buffer[100]; |
int i,err,found,j; |
|
unsigned short tmp1,tmp2; |
|
21 |
|
|
22 |
int iter=2; |
int ACphysics(int length,unsigned short* datapointer,struct physicsstruct* physicspointer) |
23 |
|
{ |
24 |
|
int iter=2; |
25 |
|
|
26 |
*physicspointer=physicsdata; |
*physicspointer=physicsdata; |
27 |
|
|
28 |
//look for header AC11, then cut out the event to a temp buffer |
//look for header AC11, then cut out the event to a temp buffer |
29 |
while(found==0 && iter<length) |
while(found==0 && iter<length) |
30 |
{ |
{ |
31 |
tmp1=*datapointer; |
tmp1=*datapointer; |
32 |
tmp2=*(datapointer+1); |
tmp2=*(datapointer+1); |
33 |
//printf("check: %hx\n",tmp1); |
|
34 |
if(tmp1==0x11AC) |
if(tmp1==0x11AC) |
35 |
|
//if(tmp1==0xAC11) //flipped |
36 |
{ |
{ |
37 |
found=1; |
found=1; |
38 |
for(i=0;i<64;i++) |
for(i=0;i<64;i++) |
39 |
{ |
{ |
40 |
buffer[i]=*datapointer; |
buffer[i]=flip(*datapointer); |
41 |
datapointer++; |
datapointer++; |
42 |
} |
} |
43 |
tmp1=0x0000; |
tmp1=0x0000; |
44 |
} |
} |
45 |
else if((tmp1&0xFF00) + (tmp2&0x00FF)==0xAC11) |
else if(((tmp1&0xFF00) >> 8 ) + ((tmp2&0x00FF) << 8 ) == 0x11AC) |
46 |
|
//else if((tmp1&0x00FF) + (tmp2&0xFF00)==0xAC11) //flipped |
47 |
{ |
{ |
48 |
found=1; |
found=1; |
49 |
for(i=0;i<64;i++) |
for(i=0;i<64;i++) |
50 |
{ |
{ |
51 |
buffer[i]=(tmp1&0xFF00) + (tmp2&0x00FF); |
buffer[i]=flip(((tmp1&0xFF00) >> 8 ) + ((tmp2&0x00FF) << 8 )); |
52 |
|
//buffer[i]=(tmp1&0x00FF) + (tmp2&0xFF00); //flipped |
53 |
datapointer++; |
datapointer++; |
54 |
tmp1=tmp2; |
tmp1=tmp2; |
55 |
tmp2=*(datapointer+1); |
tmp2=*(datapointer+1); |
61 |
} |
} |
62 |
|
|
63 |
//check errors |
//check errors |
64 |
if(iter>=length) |
if(found==0) |
65 |
err=2; |
err=2; |
66 |
else if(buffer[1]==0xAC22) |
else if(buffer[1]==0x22AC || buffer[1]==0xAC22) |
67 |
err=0; |
err=0; |
68 |
else |
else |
69 |
err=1; |
err=1; |