Record changes.
[u/mrichter/AliRoot.git] / T0 / AliT0RawReader.cxx
1 #include "AliT0RawReader.h"
2 #include "AliT0Parameters.h"
3 #include "AliBitPacking.h"
4 #include "TBits.h"
5
6 #include <Riostream.h>
7 #include "TMath.h"
8 #include "TH1F.h"
9 #include "TArrayI.h"
10 #include "AliLog.h"
11  
12 ClassImp(AliT0RawReader)
13   
14   AliT0RawReader::AliT0RawReader (AliRawReader *rawReader, Bool_t isOnline)
15     :  TTask("T0RawReader","read raw T0 data"),
16        fRawReader(rawReader),
17        fData(NULL),
18        fPosition(0),
19        fParam(NULL),
20        fIsOnline(isOnline)
21 {
22   //
23 // create an object to read T0raw digits
24   AliDebug(1,"Start ");
25  
26   fRawReader->Reset();
27   fRawReader->Select("T0");
28   fParam = AliT0Parameters::Instance();  
29   if (fIsOnline)
30     fParam->InitIfOnline();
31   else 
32     fParam->Init();
33  
34 }
35  AliT0RawReader::~AliT0RawReader ()
36 {
37   // 
38 }
39 /*
40 AliT0RawReader::AliT0RawReader(const AliT0RawReader& o): TTask(o),
41      fRawReader(rawReader),
42        fData(NULL),
43        fPosition(0)
44 {
45   //
46 }
47 */
48
49
50 Bool_t  AliT0RawReader::Next()
51 {
52 // read the next raw digit
53 // returns kFALSE if there is no digit left
54 //"LookUpTable":
55 // Amplitude LED TRM=0; chain=0; TDC 0 -5        channel 0,2,4,6
56 // Time CFD      TRM=0; chain=0; TDC 6 - 11      channel 0,2,4,6
57 // mean time     TRM=0; chain=0; TDC 12          channel 0
58 // T0A           TRM=0; chain=0; TDC 12          channel 2
59 // T0C           TRM=0; chain=0; TDC 12          channel 4
60 // vertex        TRM=0; chain=0; TDC 12          channel 6
61 // mult QTC0        TRM=0; chain=0; TDC 13          channel 0
62 // mult QTC1        TRM=0; chain=0; TDC 13          channel 2
63
64 // Charge QTC0   TRM=1; chain=0; TDC 0 -5        channel 0,2,4,6
65 // Charge QTC1   TRM=1; chain=0; TDC 6 - 11      channel 0,2,4,6
66 // T0A trigger          TRM=1; chain=0; TDC 12          channel 0
67 // T0C trigger          TRM=1; chain=0; TDC 12          channel 2
68 // vertex trigger       TRM=1; chain=0; TDC 12          channel 4
69 // trigger central      TRM=1; chain=0; TDC 13          channel 0
70 // tigger semicenral    TRM=1; chain=0; TDC 13          channel 2
71 //
72 // allData array collect data from all channels in one :
73 // allData[0] - allData[23] 24 CFD channels
74 // allData[24] -   allData[47] 24 LED channels
75 //  allData[48]  mean (T0) signal  
76 // allData[49]   time difference (vertex)
77
78  
79   UInt_t word;
80   Int_t time=0,  itdc=0, ichannel=0, uu; 
81   Int_t numberOfWordsInTRM=0, iTRM=0;
82   Int_t tdcTime, koef,hit=0;
83   Int_t koefhits[110];
84     Int_t  TRM_CHAIN_0_HEADER =  0x00000000;
85   Int_t  TRM_CHAIN_0_TRAILER =  0x10000000;
86   
87   Int_t  FILLER =  0x70000000;
88   Bool_t correct=kTRUE;
89   Int_t header;
90
91    Int_t fNTRM = fParam->GetNumberOfTRMs();
92    for ( Int_t k=0; k<110; k++) {
93     koefhits[k]=0;
94     for ( Int_t jj=0; jj<5; jj++) {
95       fAllData[k][jj]=0;
96      }
97    }
98     do {
99       if (!fRawReader->ReadNextData(fData)) return kFALSE;
100     } while (fRawReader->GetDataSize() == 0);
101     
102     fPosition = 0;
103      cout.setf( ios_base::hex, ios_base::basefield );
104     
105     //DRM header
106     for (Int_t i=0; i<6; i++) {
107       word = GetNextWord();
108       header = AliBitPacking::UnpackWord(word,28,31);
109       if( header !=4 )
110         {
111           AliWarning(Form(" !!!! wrong  DRM header  %x!!!!", word));
112           fRawReader->AddFatalErrorLog(kWrongDRMHeader,Form("w=%x",word));
113           break;
114         }
115     }
116     
117     for (Int_t ntrm=0; ntrm< fNTRM; ntrm++)
118       {
119         //TRMheader  
120         word = GetNextWord();
121         header = AliBitPacking::UnpackWord(word,28,31);
122 //      cout<<" TRM header "<<word<<endl;
123         if ( header != 4 )
124           {
125             AliWarning(Form(" !!!! wrong TRM header  %x!!!!", word));
126             fRawReader->AddMajorErrorLog(kWrongTRMHeader,Form("w=%x",word));
127             break;
128           }
129         numberOfWordsInTRM=AliBitPacking::UnpackWord(word,4,16);
130 //      cout<<" numberOfWordsInTRM "<<numberOfWordsInTRM<<endl;
131         iTRM=AliBitPacking::UnpackWord(word,0,3);
132         for( Int_t ichain=0; ichain<2; ichain++)
133           {
134             //chain header
135             word = GetNextWord();
136 //          cout<<" chain header "<<word<<endl;
137             uu = word & TRM_CHAIN_0_HEADER;
138             if(uu != TRM_CHAIN_0_HEADER) 
139               {
140                 AliWarning(Form(" !!!! wrong CHAIN  0  header %x!!!!", word));
141                 fRawReader->AddMajorErrorLog(kWrongChain0Header,Form("w=%x",word));
142                 break;
143               }
144             word = GetNextWord();
145 //          cout<<" next "<<word<<endl;
146             tdcTime =  AliBitPacking::UnpackWord(word,31,31);   
147 //          for (; tdcTime==1; tdcTime) 
148         while(tdcTime==1)
149               {
150 //                      cout<<" packed "<<word<<endl;
151                 itdc=AliBitPacking::UnpackWord(word,24,27);
152                 ichannel=AliBitPacking::UnpackWord(word,21,23);
153                 time=AliBitPacking::UnpackWord(word,0,20);
154                 
155                 koef = fParam->GetChannel(iTRM,itdc,ichain,ichannel);
156                         if (koef != 0 ) 
157 //                cout<<"RawReader>> "<<"koef "<<koef<<" trm "<<iTRM<<
158 //                  " tdc "<<itdc<<" chain "<<ichain<<
159 //                  " channel "<<ichannel<<" time "<<time<<endl;
160                 if (koef ==-1 ){
161                   AliWarning(Form("Incorrect lookup table ! "));
162                   fRawReader->AddMajorErrorLog(kIncorrectLUT);
163                   correct=kFALSE;
164                 }
165                 if(correct){
166                   hit=koefhits[koef];
167                   if(hit>5) {  
168                     AliWarning(Form("Too many hits for %i channel  ! ",koef)); 
169                     break; 
170                   }
171                   fAllData[koef][hit]=time; 
172                   koefhits[koef]++;
173                 }
174                   word = GetNextWord();
175 //                cout<<" next word in cycle "<<word<<endl;
176                   tdcTime =  AliBitPacking::UnpackWord(word,31,31);   
177                 
178             }
179             
180 //          cout<<" chain trailer "<<word<<endl;
181             uu = word&TRM_CHAIN_0_TRAILER;
182             if(uu != TRM_CHAIN_0_TRAILER )
183               {
184                 AliWarning(Form(" !!!! wrong CHAIN 0 trailer %x !!!!", word));
185                   fRawReader->AddMajorErrorLog(kWrongChain0Trailer,Form("w=%x",word));
186                 break;
187               }
188           }
189             
190         word = GetNextWord(); //TRM trailer
191 //      cout<<" TRM trailer "<<word<<endl;
192         header = AliBitPacking::UnpackWord(word,28,31);
193         if( header !=5 )
194           {
195             AliWarning(Form(" !!!! wrong TRM GLOBAL trailer  %x!!!!", word));
196             fRawReader->AddMajorErrorLog(kWrongTRMTrailer,Form("w=%x",word));
197             break;
198           }
199       } //TRM loop
200     word = GetNextWord(); //
201     if (word == FILLER )  word = GetNextWord(); 
202      header = AliBitPacking::UnpackWord(word,28,31);
203      if( header !=5 )
204        {
205          AliWarning(Form(" !!!! wrong DRM GLOBAL trailer  %x!!!!", word));
206          fRawReader->AddFatalErrorLog(kWrongDRMTrailer,Form("w=%x",word));
207       }
208      cout.setf( ios_base::dec, ios_base::basefield );
209     
210      return kTRUE;
211 }
212 //_____________________________________________________________________________
213 Int_t AliT0RawReader::GetPosition()
214 {
215   // Sets the position in the
216   // input stream
217   if (((fRawReader->GetDataSize() * 8) % 32) != 0)
218     AliFatal(Form("Incorrect raw data size ! %d words are found !",fRawReader->GetDataSize()));
219   return (fRawReader->GetDataSize() * 8) / 32;
220 }
221 //_____________________________________________________________________________
222 UInt_t AliT0RawReader::GetNextWord()
223 {
224   // Read the next 32 bit word in backward direction
225   // The input stream access is given by fData and fPosition
226
227
228   //   fPosition--;
229   Int_t iBit = fPosition * 32;
230   Int_t iByte = iBit / 8;
231
232   UInt_t word = 0;
233   word  = fData[iByte+3]<<24;
234   word |= fData[iByte+2]<<16;
235   word |= fData[iByte+1]<<8;
236   word |= fData[iByte];
237    fPosition++;
238
239   return word;
240
241 }
242