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