Added capability to handle backlinked list properly.
[u/mrichter/AliRoot.git] / HLT / misc / AliL3AltroMemHandler.cxx
CommitLineData
acf814d1 1/* $Id$
2Author: 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
9c50e540 10/** \class AliL3AltroMemHandler
11// Converts digits in memory into a backlinked ALTRO like data format.
faebf2f5 12// Its output file is used as input to the various VHDL testbenches.
9c50e540 13// The file misc/read.cxx shows how to use this class.
9c50e540 14*/
15
16ClassImp(AliL3AltroMemHandler)
17
acf814d1 18AliL3AltroMemHandler::AliL3AltroMemHandler(){
19 Clear();
20};
21
acf814d1 22void AliL3AltroMemHandler::Clear(){
23 memset(altromem,0,ALTRO_SIZE);
faebf2f5 24 memset(times_per_pad,0,1024);
25 memset(charges_per_pad,0,1024);
26 counter=ALTRO_SIZE;
acf814d1 27 tcounter=0;
28 lpad=0;
acf814d1 29 lrow=0;
30 flag=kFALSE;
31};
32
faebf2f5 33void AliL3AltroMemHandler::Write(UShort_t row, UChar_t pad, UShort_t charge, UShort_t time)
34{
35 if(tcounter==0){
36 lrow=row;
37 lpad=pad;
38 } else if((lrow!=row) || (lpad!=pad)){
39 MakeAltroPackets(); //make packets
40 Write(); //write packets
41 Clear(); //clear up for next pad
42
43 lrow=row;
44 lpad=pad;
45 }
46
47 Add(charge,time);
48}
49
50void AliL3AltroMemHandler::Add(UShort_t charge, UShort_t time)
51{
52 times_per_pad[tcounter]=time;
53 charges_per_pad[tcounter]=charge;
54 tcounter++;
55}
56
57void AliL3AltroMemHandler::MakeAltroPackets()
58{
59 UShort_t i=0,j=0;
60 UShort_t t=0,seqlength;
61 UShort_t htime,ltime;
62 int ddd=0;
63 while(t<tcounter){
64 pcounter=0;
65 altromem[--counter]=0xFFFF; //mark return
66
67 //make packets
68 while((pcounter<ALTRO_SIZE) && (t<tcounter)){
69
70 //find sequence
71 i=t;
72 ltime=times_per_pad[t];
73 j=0;
74 while((i+1<tcounter)&&(times_per_pad[i+1]==ltime+j+1)){
75 i++;
76 j++;
77 }
78 seqlength=j+1; //number of consecutive times
79 htime=times_per_pad[i]; //abs. time for sequence
80/*
81 cout << " counter " << counter << endl;
82 if(htime!=ltime+j){
83 cerr << "Error: " << ltime << " - " << htime << endl;
84 exit(1);
85 }
86*/
87 //store charges of sequence
88 for(UShort_t k=0;k<seqlength;k++){
89 altromem[--counter]=charges_per_pad[t];
90 pcounter++;
91 t++;
92 }
93
94 altromem[--counter]=htime; //store abs. time of sequence
95 pcounter++;
96 altromem[--counter]=seqlength+2; //store length of sequence
97 pcounter++;
98 }
99
100 AddTrailer();
101 }
acf814d1 102}
103
faebf2f5 104void AliL3AltroMemHandler::AddTrailer()
105{
106 UShort_t savepcounter=pcounter;
acf814d1 107
faebf2f5 108 while(pcounter%4!=0){
109 altromem[--counter]=0x2AA;
110 pcounter++;
acf814d1 111 }
faebf2f5 112 altromem[--counter]=0x2AA;
113 altromem[--counter]=savepcounter;
114 altromem[--counter]=lpad;
115 altromem[--counter]=lrow;
116}
acf814d1 117
faebf2f5 118void AliL3AltroMemHandler::Write()
119{
120 if(counter==ALTRO_SIZE) return;
121
122 //save packets (reversed) to file
acf814d1 123 UShort_t *ptr=altromem+counter;
faebf2f5 124 for (int i=counter;i<ALTRO_SIZE;i++) {
125
126 if(flag==kTRUE){
127 if (*ptr==0xFFFF) continue; //no return for end of packet
128 fwrite(ptr,sizeof(UShort_t),1,fOutBinary);
129 } else {
130 if (*ptr==0xFFFF) fprintf(fOutBinary,"\n");
131 else fprintf(fOutBinary,"%X ",*ptr);
132 }
133
134 ptr++;
acf814d1 135 }
faebf2f5 136}
acf814d1 137
faebf2f5 138void AliL3AltroMemHandler::WriteFinal()
139{
140 if(tcounter>0){
141 MakeAltroPackets();
142 Write();
143 }
acf814d1 144}
145
146/*
147Bool_t AliL3AltroMemHandler::SetBinaryInput(FILE *file){
148 fInBinary = file;
149 if(!fInBinary){
150 //LOG(AliL3Log::kWarning,"AliL3AltroMem::SetBinaryInput","File Open")<<"Pointer to File = 0x0 "<<ENDLOG;
151 return kFALSE;
152 }
153 return kTRUE;
154}
155*/
156
157Bool_t AliL3AltroMemHandler::SetBinaryOutput(FILE *file){
158 fOutBinary = file;
159 if(!fOutBinary){
160 LOG(AliL3Log::kWarning,"AliL3AltroMemHandler::SetBinaryOutput","File Open") <<"Pointer to File = 0x0 "<<ENDLOG;
161 return kFALSE;
162 }
faebf2f5 163 flag=kTRUE;
164 return kTRUE;
165}
166
167Bool_t AliL3AltroMemHandler::SetASCIIOutput(FILE *file){
168 fOutBinary = file;
169 if(!fOutBinary){
170 LOG(AliL3Log::kWarning,"AliL3AltroMemHandler::SetASCIIOutput","File Open") <<"Pointer to File = 0x0 "<<ENDLOG;
171 return kFALSE;
172 }
173 flag=kFALSE;
acf814d1 174 return kTRUE;
175}