]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/misc/AliL3AltroMemHandler.cxx
Added read support for Altro like data.
[u/mrichter/AliRoot.git] / HLT / misc / AliL3AltroMemHandler.cxx
1 /* $Id$
2 Author: Constantin Loizides <mailto: loizides@ikf.physik.uni-frankfurt.de>
3 */
4
5 #include <iostream.h>
6 #include <stdio.h>
7 #include <string.h>
8 #include "AliL3AltroMemHandler.h"
9
10 /** \class AliL3AltroMemHandler
11 //<pre>
12 //--------------------------------------------------------------------
13 // AliL3AltroMemHandler
14 //
15 // Converts digits in memory into a backlinked ALTRO like data format.
16 // Its output file is used as input to the various VHDL testbenches.
17 // The file misc/read.cxx shows how to use this class.
18 */
19
20 ClassImp(AliL3AltroMemHandler)
21
22 AliL3AltroMemHandler::AliL3AltroMemHandler(){
23   Clear();
24   ClearRead();
25 };
26
27 void AliL3AltroMemHandler::Clear(){
28   memset(altromem,0,ALTRO_SIZE);
29   memset(times_per_pad,0,1024);
30   memset(charges_per_pad,0,1024);
31   counter=ALTRO_SIZE;
32   tcounter=0;
33   lpad=0;
34   lrow=0;
35   flag=kFALSE;
36 };
37
38 void AliL3AltroMemHandler::ClearRead(){
39   rcounter=0;
40   scounter=0;
41   rpad=0;
42   rrow=0;
43   rtime=0;
44 }
45
46 void AliL3AltroMemHandler::Write(UShort_t row, UChar_t pad, UShort_t time, UShort_t charge)
47 {
48   if(tcounter==0){
49     lrow=row;
50     lpad=pad;
51   } else if((lrow!=row) || (lpad!=pad)){
52     MakeAltroPackets(); //make packets
53     Write();            //write packets
54     Clear();            //clear up for next pad
55
56     lrow=row;
57     lpad=pad;
58   }
59
60   Add(charge,time);
61 }
62
63 void AliL3AltroMemHandler::Add(UShort_t charge, UShort_t time)
64 {
65   times_per_pad[tcounter]=time;
66   charges_per_pad[tcounter]=charge;
67   tcounter++;
68 }
69
70 void AliL3AltroMemHandler::MakeAltroPackets()
71 {
72   UShort_t i=0,j=0;
73   UShort_t t=0,seqlength;
74   UShort_t htime,ltime;
75
76   while(t<tcounter){
77     pcounter=0;
78     altromem[--counter]=0xFFFF; //mark return
79
80     //make packets
81     while((pcounter<ALTRO_SIZE) && (t<tcounter)){
82
83       //find sequence
84       i=t;
85       ltime=times_per_pad[t];
86       j=0;
87       while((i+1<tcounter)&&(times_per_pad[i+1]==ltime+j+1)){
88         i++;
89         j++;
90       }
91       seqlength=j+1; //number of consecutive times   
92       htime=times_per_pad[i]; //abs. time for sequence
93
94 /*
95   cout << " counter " << counter << endl;
96   if(htime!=ltime+j){
97   cerr << "Error: " << ltime << " - " << htime << endl;
98   exit(1);
99   }
100 */
101       //don't store sequence if it doesn't fit into packet
102       if(pcounter+seqlength>=ALTRO_SIZE) break;
103
104       //store charges of sequence
105       for(UShort_t k=0;k<seqlength;k++){
106         altromem[--counter]=charges_per_pad[t];
107         pcounter++;
108         t++;
109       }
110
111       altromem[--counter]=htime;       //store abs. time of sequence
112       pcounter++;
113       altromem[--counter]=seqlength+2; //store length of sequence
114       pcounter++;
115     }
116
117     AddTrailer();
118   }
119 }
120
121 void AliL3AltroMemHandler::AddTrailer()
122 {
123   UShort_t savepcounter=pcounter;
124
125   while(pcounter%4!=0){
126     altromem[--counter]=0x2AA;
127     pcounter++;
128   } 
129   altromem[--counter]=0x2AA;
130   altromem[--counter]=savepcounter;
131   altromem[--counter]=lpad;
132   altromem[--counter]=lrow;
133 }
134
135 void AliL3AltroMemHandler::Write()
136 {
137   if(counter==ALTRO_SIZE) return;
138
139   //save packets (reversed) to file
140   UShort_t *ptr=altromem+counter;
141   for (int i=counter;i<ALTRO_SIZE;i++) {
142
143     if(flag==kTRUE){
144       if (*ptr==0xFFFF) continue; //no return for end of packet
145       fwrite(ptr,sizeof(UShort_t),1,fOutBinary);
146     } else {
147       if (*ptr==0xFFFF) fprintf(fOutBinary,"\n");
148       else fprintf(fOutBinary,"%X ",*ptr);
149     }
150
151     ptr++;
152   }
153 }
154
155 void AliL3AltroMemHandler::WriteFinal()
156 {
157   if(tcounter>0){
158     MakeAltroPackets();
159     Write();
160   }
161 }
162
163 Bool_t AliL3AltroMemHandler::Read(UShort_t &row, UChar_t &pad, UShort_t &time, UShort_t &charge)
164 {
165   if(flag==kTRUE) {
166     LOG(AliL3Log::kWarning,"AliL3AltroMemHandler::Read","File Open")<<"Binary not supported!"<<ENDLOG;
167     return kFALSE;
168   }
169   
170   if(feof(fInBinary)){
171     LOG(AliL3Log::kDebug,"AliL3AltroMemHandler::Read","File Open")<<"End of File!"<<ENDLOG;
172     return kFALSE;
173   }
174
175   unsigned int dummy,dummy1;
176
177   if(rcounter==0){
178     fscanf(fInBinary,"%x",&dummy);
179     rrow=(UShort_t)dummy;
180     fscanf(fInBinary,"%x",&dummy);
181     rpad=(UShort_t)dummy;
182     fscanf(fInBinary,"%x",&dummy);
183     rcounter=(UShort_t)dummy;
184     fscanf(fInBinary,"%x",&dummy);
185     if(dummy!=682){
186       if(feof(fInBinary)){
187         ClearRead();
188         return kFALSE;
189       } else {
190         LOG(AliL3Log::kError,"AliL3AltroMemHandler::Read","Trailer not found!")<<ENDLOG;
191         return kFALSE;
192       }
193     }
194     dummy1 = rcounter;
195     while(dummy1 % 4 != 0) {
196       fscanf(fInBinary,"%x",&dummy);
197       dummy1++;
198     } 
199   } 
200   if(scounter==0){
201     fscanf(fInBinary,"%x",&dummy);
202     scounter=(UShort_t)dummy-2;
203     fscanf(fInBinary,"%x",&dummy);
204     rtime=(UShort_t)dummy;
205     rcounter-=2;
206   }
207   fscanf(fInBinary,"%x",&dummy);
208
209   row=rrow;
210   pad=rpad;
211   time=rtime;
212   charge=(UShort_t)dummy;
213
214   scounter--;
215   rcounter--;
216   rtime--;
217   return kTRUE;
218 }
219
220 Bool_t AliL3AltroMemHandler::ReadSequence(UShort_t &row, UChar_t &pad, UShort_t &time, UChar_t &n, UShort_t **charges)
221 {
222   if(flag==kTRUE) {
223     LOG(AliL3Log::kWarning,"AliL3AltroMemHandler::ReadSequence","File Open")<<"Binary not supported!"<<ENDLOG;
224     return kFALSE;
225   }
226   
227   if(feof(fInBinary)){
228     LOG(AliL3Log::kDebug,"AliL3AltroMemHandler::ReadSequence","File Open")<<"End of File!"<<ENDLOG;
229     return kFALSE;
230   }
231
232   unsigned int dummy,dummy1,dummy2;
233
234   if(rcounter==0){
235     fscanf(fInBinary,"%x",&dummy);
236     rrow=(UShort_t)dummy;
237     fscanf(fInBinary,"%x",&dummy);
238     rpad=(UShort_t)dummy;
239     fscanf(fInBinary,"%x",&dummy);
240     rcounter=(UShort_t)dummy;
241     fscanf(fInBinary,"%x",&dummy);
242     if(dummy!=682){
243       if(feof(fInBinary)){
244         ClearRead();
245         return kFALSE;
246       } else {
247         LOG(AliL3Log::kError,"AliL3AltroMemHandler::ReadSequence","Format") <<"Trailer not found!"<<ENDLOG;
248         return kFALSE;
249       }
250     }
251     dummy1 = rcounter;
252     while(dummy1 % 4 != 0) {
253       fscanf(fInBinary,"%x",&dummy);
254       dummy1++;
255     } 
256   } 
257
258   fscanf(fInBinary,"%x",&dummy);
259   scounter=(UShort_t)dummy-2;
260   fscanf(fInBinary,"%x",&dummy);
261   rtime=(UShort_t)dummy;
262   rcounter-=2;
263
264   if(n<scounter){
265     if(*charges) delete[] *charges;
266     *charges=new UShort_t[scounter];
267   }
268
269   dummy2=0;
270   while(dummy2<scounter){
271     fscanf(fInBinary,"%x",&dummy);
272     (*charges)[dummy2]=(UShort_t)dummy;
273     dummy2++;
274     rcounter--;
275   }
276
277   row=rrow;
278   pad=rpad;
279   time=rtime;
280   n=scounter;
281   return kTRUE;
282 }
283
284 Bool_t AliL3AltroMemHandler::SetBinaryInput(FILE *file)
285 {
286   fInBinary = file;
287   if(!fInBinary){
288     LOG(AliL3Log::kWarning,"AliL3AltroMemHandler::SetBinaryInput","File Open")<<"Pointer to File = 0x0 "<<ENDLOG;
289     return kFALSE;
290   }
291   ClearRead();
292   flag=kTRUE;
293   return kTRUE;
294 }
295
296 Bool_t AliL3AltroMemHandler::SetASCIIInput(FILE *file)
297 {
298   fInBinary = file;
299   if(!fInBinary){
300     LOG(AliL3Log::kWarning,"AliL3AltroMemHandler::SetASCIIInput","File Open")<<"Pointer to File = 0x0 "<<ENDLOG;
301     return kFALSE;
302   }
303   ClearRead();
304   flag=kFALSE;
305   return kTRUE;
306 }
307
308 Bool_t AliL3AltroMemHandler::SetBinaryOutput(FILE *file){
309   fOutBinary = file;
310   if(!fOutBinary){
311     LOG(AliL3Log::kWarning,"AliL3AltroMemHandler::SetBinaryOutput","File Open") <<"Pointer to File = 0x0 "<<ENDLOG;
312     return kFALSE;
313   }
314   Clear();
315   flag=kTRUE;
316   return kTRUE;
317 }
318
319 Bool_t AliL3AltroMemHandler::SetASCIIOutput(FILE *file){
320   fOutBinary = file;
321   if(!fOutBinary){
322     LOG(AliL3Log::kWarning,"AliL3AltroMemHandler::SetASCIIOutput","File Open") <<"Pointer to File = 0x0 "<<ENDLOG;
323     return kFALSE;
324   }
325   Clear();
326   flag=kFALSE;
327   return kTRUE;
328 }
329