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