/[PAMELA software]/yoda/techmodel/forroutines/anticounter/AC.c
ViewVC logotype

Diff of /yoda/techmodel/forroutines/anticounter/AC.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

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

Legend:
Removed from v.2.1  
changed lines
  Added in v.2.2

  ViewVC Help
Powered by ViewVC 1.1.23