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