]> git.uio.no Git - u/mrichter/AliRoot.git/blame - HLT/misc/AliHLTAltroMemHandler.cxx
Changed the data format to pass to the Make methods
[u/mrichter/AliRoot.git] / HLT / misc / AliHLTAltroMemHandler.cxx
CommitLineData
3e87ef69 1// @(#) $Id$
1e843b89 2
3e87ef69 3// Author: Constantin Loizides <mailto: loizides@ikf.uni-frankfurt.de>
4// *-- Copyright & copy ALICE HLT Group
4aa41877 5/** \class AliHLTAltroMemHandler
3e87ef69 6<pre>
a36f659c 7//--------------------------------------------------------------------
4aa41877 8// AliHLTAltroMemHandler
a36f659c 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
4aa41877 16#include "AliHLTStandardIncludes.h"
54b54089 17
4aa41877 18#include "AliHLTAltroMemHandler.h"
19#include "AliHLTLogging.h"
54b54089 20
5929c18d 21#if __GNUC__ >= 3
22using namespace std;
23#endif
24
4aa41877 25ClassImp(AliHLTAltroMemHandler)
9c50e540 26
4aa41877 27AliHLTAltroMemHandler::AliHLTAltroMemHandler(){
54b54089 28 // default constructor
acf814d1 29 Clear();
a36f659c 30 ClearRead();
acf814d1 31};
32
4aa41877 33void AliHLTAltroMemHandler::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
4aa41877 45void AliHLTAltroMemHandler::ClearRead(){
54b54089 46 // clears the reading
47 fRCounter=0;
48 fSCounter=0;
49 fRPad=0;
50 fRRow=0;
51 fRTime=0;
a36f659c 52}
53
4aa41877 54void AliHLTAltroMemHandler::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
4aa41877 72void AliHLTAltroMemHandler::Add(UShort_t charge, UShort_t time)
faebf2f5 73{
54b54089 74 // adds new time and charge
75 fTimesPerPad[fTCounter]=time;
76 fChargesPerPad[fTCounter]=charge;
77 fTCounter++;
faebf2f5 78}
79
4aa41877 80void AliHLTAltroMemHandler::MakeAltroPackets()
faebf2f5 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
4aa41877 125void AliHLTAltroMemHandler::AddTrailer()
faebf2f5 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
4aa41877 140void AliHLTAltroMemHandler::Write()
faebf2f5 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
4aa41877 161void AliHLTAltroMemHandler::WriteFinal()
faebf2f5 162{
54b54089 163 // makes Altro packets and writes them
164 if(fTCounter>0){
faebf2f5 165 MakeAltroPackets();
166 Write();
167 }
acf814d1 168}
169
4aa41877 170Bool_t AliHLTAltroMemHandler::Read(UShort_t &row, UChar_t &pad, UShort_t &time, UShort_t &charge)
a36f659c 171{
54b54089 172 // reads the packets
173 if(fFlag==kTRUE) {
4aa41877 174 LOG(AliHLTLog::kWarning,"AliHLTAltroMemHandler::Read","File Open")<<"Binary not supported!"<<ENDLOG;
a36f659c 175 return kFALSE;
176 }
177
178 if(feof(fInBinary)){
4aa41877 179 LOG(AliHLTLog::kDebug,"AliHLTAltroMemHandler::Read","File Open")<<"End of File!"<<ENDLOG;
a36f659c 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 {
4aa41877 198 LOG(AliHLTLog::kError,"AliHLTAltroMemHandler::Read","Trailer not found!")<<ENDLOG;
a36f659c 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
4aa41877 228Bool_t AliHLTAltroMemHandler::ReadSequence(UShort_t &row, UChar_t &pad, UShort_t &time, UChar_t &n, UShort_t **charges)
a36f659c 229{
54b54089 230 // reads sequence
231 if(fFlag==kTRUE) {
4aa41877 232 LOG(AliHLTLog::kWarning,"AliHLTAltroMemHandler::ReadSequence","File Open")<<"Binary not supported!"<<ENDLOG;
a36f659c 233 return kFALSE;
234 }
235
236 if(feof(fInBinary)){
4aa41877 237 LOG(AliHLTLog::kDebug,"AliHLTAltroMemHandler::ReadSequence","File Open")<<"End of File!"<<ENDLOG;
a36f659c 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 {
4aa41877 256 LOG(AliHLTLog::kError,"AliHLTAltroMemHandler::ReadSequence","Format") <<"Trailer not found!"<<ENDLOG;
a36f659c 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
4aa41877 293Bool_t AliHLTAltroMemHandler::SetBinaryInput(FILE *file)
a36f659c 294{
54b54089 295 // sets binary input
acf814d1 296 fInBinary = file;
297 if(!fInBinary){
4aa41877 298 LOG(AliHLTLog::kWarning,"AliHLTAltroMemHandler::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
4aa41877 306Bool_t AliHLTAltroMemHandler::SetASCIIInput(FILE *file)
a36f659c 307{
54b54089 308 // sets ASCII input
a36f659c 309 fInBinary = file;
310 if(!fInBinary){
4aa41877 311 LOG(AliHLTLog::kWarning,"AliHLTAltroMemHandler::SetASCIIInput","File Open")<<"Pointer to File = 0x0 "<<ENDLOG;
a36f659c 312 return kFALSE;
313 }
314 ClearRead();
54b54089 315 fFlag=kFALSE;
acf814d1 316 return kTRUE;
317}
acf814d1 318
4aa41877 319Bool_t AliHLTAltroMemHandler::SetBinaryOutput(FILE *file){
54b54089 320 // sets binary output
acf814d1 321 fOutBinary = file;
322 if(!fOutBinary){
4aa41877 323 LOG(AliHLTLog::kWarning,"AliHLTAltroMemHandler::SetBinaryOutput","File Open") <<"Pointer to File = 0x0 "<<ENDLOG;
acf814d1 324 return kFALSE;
325 }
a36f659c 326 Clear();
54b54089 327 fFlag=kTRUE;
faebf2f5 328 return kTRUE;
329}
330
4aa41877 331Bool_t AliHLTAltroMemHandler::SetASCIIOutput(FILE *file){
54b54089 332 // sets ASCII output
faebf2f5 333 fOutBinary = file;
334 if(!fOutBinary){
4aa41877 335 LOG(AliHLTLog::kWarning,"AliHLTAltroMemHandler::SetASCIIOutput","File Open") <<"Pointer to File = 0x0 "<<ENDLOG;
faebf2f5 336 return kFALSE;
337 }
a36f659c 338 Clear();
54b54089 339 fFlag=kFALSE;
acf814d1 340 return kTRUE;
341}
a36f659c 342