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