]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HMPID/AliHMPIDRawStream.cxx
Now segment is correct
[u/mrichter/AliRoot.git] / HMPID / AliHMPIDRawStream.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 ///////////////////////////////////////////////////////////////////////////////
17 ///
18 /// This is a class for reading the HMPID raw data
19 /// The format of the raw data corresponds to the one
20 /// which was documented by Paolo Martinengo.
21 ///
22 ///////////////////////////////////////////////////////////////////////////////
23
24 #include "AliHMPIDRawStream.h"
25 #include "AliRawReader.h"
26 #include "AliLog.h"
27
28 Int_t fPos[170000];
29 Int_t iPos = 0;
30 //static Bool_t stDeb = kTRUE;
31
32 ClassImp(AliHMPIDRawStream)
33
34 //_____________________________________________________________________________
35 AliHMPIDRawStream::AliHMPIDRawStream(AliRawReader* rawReader) :
36   fDDLNumber(-1),
37   fRawReader(rawReader),
38   fData(NULL),
39   fPosition(-1)
40 {
41   // Constructor
42   Init();
43
44   fRawReader->Reset();
45   fRawReader->Select("HMPID");
46 }
47 //-----------------------------------------------------------------------------
48 AliHMPIDRawStream::AliHMPIDRawStream() :
49   fDDLNumber(-1),
50   fRawReader(0x0),
51   fData(NULL),
52   fPosition(-1)
53 {
54   // Constructor
55   Init();
56 }
57 //_____________________________________________________________________________
58 AliHMPIDRawStream::~AliHMPIDRawStream()
59 {
60   // destructor
61 }
62
63 //_____________________________________________________________________________
64 void AliHMPIDRawStream::Init()
65 {
66   // Initalize the container
67   // with the pad charges
68   Int_t n=0;
69   iPos = 0;
70   for(Int_t h = 0; h < kNDDL; h++) {  
71     for(Int_t i = 0; i < kNRows; i++){
72       for(Int_t j = 0; j < kNDILOGICAdd; j++){
73         for(Int_t k = 0; k < kNPadAdd; k++){
74           fCharge[h][i][j][k] = -1;
75           fPad[h][i][j][k]=-1;
76           fPos[++n] = 0;
77         }
78       }
79     }
80   }
81   fZeroSup=kTRUE;
82   
83   for(Int_t l=1; l < kSumErr; l++) fNumOfErr[l]=0;               //reset errors
84 }//Init()
85 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
86 void AliHMPIDRawStream::Reset()
87 {
88   // reset raw stream params
89   // Reinitalize the containers
90   Init();
91   fDDLNumber = -1;
92   fPosition = -1;
93   fData = NULL;
94   if (fRawReader) fRawReader->Reset();
95 }
96 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
97 Bool_t AliHMPIDRawStream::Next()
98 {
99   // read next DDL raw data from the HMPID raw data stream
100   // return kFALSE in case of error or no data left
101   AliDebug(1,"Start.");
102   do {
103     if (!fRawReader->ReadNextData(fData)) return kFALSE;
104   } while (fRawReader->GetDataSize() == 0);
105   
106      
107   /*
108   Event type is selected as in $ALICE_ROOT/RAW/event.h  
109   #define START_OF_RUN                    ((eventTypeType) 1)
110   #define END_OF_RUN                      ((eventTypeType) 2)
111   #define START_OF_RUN_FILES              ((eventTypeType) 3)
112   #define END_OF_RUN_FILES                ((eventTypeType) 4)
113   #define START_OF_BURST                  ((eventTypeType) 5)
114   #define END_OF_BURST                    ((eventTypeType) 6)
115   #define PHYSICS_EVENT                   ((eventTypeType) 7) <<---------------  
116   #define CALIBRATION_EVENT               ((eventTypeType) 8)
117   #define EVENT_FORMAT_ERROR              ((eventTypeType) 9)
118   #define START_OF_DATA                   ((eventTypeType)10)
119   #define END_OF_DATA                     ((eventTypeType)11)
120   #define SYSTEM_SOFTWARE_TRIGGER_EVENT   ((eventTypeType)12)
121   #define DETECTOR_SOFTWARE_TRIGGER_EVENT ((eventTypeType)13)
122   #define EVENT_TYPE_MIN                  1
123   #define EVENT_TYPE_MAX                  13 
124   */
125
126   fPosition = 0;
127   Bool_t status;
128   
129   if(fRawReader->GetType() == 7)  {                             //New: Select Physics events, Old: Raw data size is not 0 and not 47148 (pedestal)
130   
131     fDDLNumber = fRawReader->GetDDLID();
132     
133     Init();
134     
135     status = ReadHMPIDRawData();
136 //    if(status) Printf("Event DDL %i successfully decoded!.",fDDLNumber);
137 // Just for test...
138 //    for(Int_t i=0;i<fRawReader->GetDataSize()/4;i++) {
139 //      GetWord();
140 //    }
141 //...
142     
143 //    if(stDeb) DumpData(fRawReader->GetDataSize());
144 //    stDeb=kFALSE;
145   }
146   return status;
147 }
148     
149 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
150 Bool_t AliHMPIDRawStream::ReadHMPIDRawData()
151 {
152   Int_t cntGlob = fRawReader->GetDataSize()/4;
153   Int_t cnt = cntGlob;
154   UInt_t word32;
155   Int_t nwSeg;
156   Int_t cntSegment;
157
158   word32 = GetWord(cnt);cnt--;
159
160   
161   while (cnt>0) {
162     
163     nwSeg = (word32 >> kbit8) & 0xfff;
164     if(!CheckSegment(word32)) return kFALSE;
165     if(!ReadSegment(word32,cntSegment)) return kFALSE;
166
167     if(nwSeg != cntSegment) {Printf("Error in Segment counters: %i different wrt %i",nwSeg,cntSegment);return kFALSE;}
168     Printf(" cnt %i cntSegment %i",cnt,cntSegment);
169     word32 = GetWord(cntSegment+1,kBwd);
170     cnt-=cntSegment+1;
171   }
172   
173   return kTRUE;
174   
175 }
176 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
177 Bool_t AliHMPIDRawStream::ReadSegment(UInt_t word32,Int_t &cntSegment)
178 {
179   cntSegment = (word32 >> kbit8) & 0xfff;
180   Int_t cnt = cntSegment;
181   Int_t cntRow;
182   Int_t nwRow;
183
184   word32 = GetWord(cnt,kBwd);
185   
186   while (cnt>0) {
187
188     cntRow  = (word32 >> kbit16) & 0xfff;
189     if(!CheckRowMarker(word32)) return kFALSE;
190     if(!ReadRow(word32,nwRow)) return kFALSE;
191
192     if(nwRow != cntRow) {Printf("Error in Row counters: %i different wrt %i",nwRow,cntRow);return kFALSE;}
193     word32 = GetWord(cntRow+1);
194     cnt -= cntRow+1;
195     
196   }
197
198   cntSegment -= cnt;
199   
200   return kTRUE;
201     
202 }    
203 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
204 Bool_t AliHMPIDRawStream::ReadRow(UInt_t word32,Int_t &cntRow)
205 {
206   Int_t cnt;
207   Int_t cntDilogic;
208   Int_t nwDil;
209   
210   cntRow  = (word32 >> kbit16) & 0xfff;
211   cnt = cntRow;  
212   
213   word32 = GetWord(cntRow);
214   
215   while (cnt>0) {
216     
217     if(!CheckEoE(word32,nwDil)) return kFALSE;
218     if(!ReadDilogic(word32,cntDilogic)) return kFALSE;
219     
220     if(nwDil != cntDilogic) {Printf("Error in Dilogic counters: %i different wrt %i",nwDil,cntDilogic);return kFALSE;}
221     cnt -= cntDilogic;
222     word32 = GetWord(1,kBwd); // go to next Dilogic bank...
223     cnt--;
224 //    Printf(" cnt %i cntDilogic %i ",cnt,cntDilogic);
225   }
226   
227   cntRow -= cnt;  
228   
229   return kTRUE;
230   
231 }
232 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
233 Bool_t AliHMPIDRawStream::ReadDilogic(UInt_t word32,Int_t &cntDilogic)
234 {
235   cntDilogic = word32 & 0x7f;
236   
237   Int_t cnt = cntDilogic;
238
239   for(Int_t iDil=0;iDil<cntDilogic;iDil++) {
240     UInt_t dilogic = 0, row = 0;
241     word32 = GetWord(1,kBwd);
242 //check on row number      
243     cnt--;
244     row = (word32 >> kbit22) & 0xf;
245     if(!CheckRow(row)) continue;
246 //check dilogic number     
247     dilogic = (word32 >> kbit18) & 0xf;                                              //dilogic info in raw word is between bits: 18...21
248     if(!CheckDilogic(dilogic)) continue;
249 //check pad number
250     UInt_t pad = (word32 >> kbit12) & 0x3f;                                          //pad info in raw word is between bits: 12...17
251     if(!CheckPad(pad)) continue;
252     fCharge[fDDLNumber][row][dilogic][pad] = word32 & 0xfff;
253 //            Printf(" (word %08X) DDL %i row %i dil %i pad %i ",word32,fDDLNumber,row,dilogic,pad);
254   }
255
256   cntDilogic -= cnt;  
257   return kTRUE;  
258 }
259 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
260 Bool_t AliHMPIDRawStream::CheckSegment(UInt_t word)
261 {
262   UInt_t markSegment = 0xAB0;
263   /*
264   if (iRow%8 == 0) {
265     UInt_t segWord = GetWord();          
266     if ((segWord >> 20) != statusSegWord) {
267       fRawReader->AddMajorErrorLog(kBadSegWordErr);
268       AliWarning(Form("Wrong segment word signature: %x, expected 0xab0!",(segWord >> 20)));
269       fNumOfErr[kBadSegWordErr]++;
270       return kTRUE;
271     }
272 */
273   UInt_t segMarker = (word >> kbit20) & 0xfff;
274   if (segMarker != markSegment ) {
275     fRawReader->AddMajorErrorLog(kWrongSegErr,Form("Segment marker %0X wrong (expected %0X) at address %i in word %0X!",segMarker,markSegment,fPosition/4,word));
276     AliWarning(Form("Segment marker %X wrong (expected %0X)! at address %i in word %0X!",segMarker,markSegment,fPosition/4,word));
277     fNumOfErr[kWrongSegErr]++;
278     return kFALSE;
279   }
280   
281   UInt_t segAddress = word & 0xff;
282   if (segAddress<1 ||segAddress>3) {
283     fRawReader->AddMajorErrorLog(kWrongSegErr,Form("Segment address %d not in the valid range [1-3] at address %i in word %0X",segAddress,fPosition/4,word));
284     AliWarning(Form("Segment address %d not in the valid range [1-3]",segAddress));
285     fNumOfErr[kWrongSegErr]++;
286     return kFALSE;
287   }
288   Printf("Segment Marker found! Number of segment is %i",segAddress);
289   return kTRUE;
290 }
291 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
292 Bool_t AliHMPIDRawStream::CheckRow(UInt_t row)
293 {
294 //check on row number      
295   if(row>=1 && row <=kNRows) return kTRUE;
296   
297   fRawReader->AddMajorErrorLog(kWrongRowErr,Form("row %d",row));
298   AliWarning(Form("Wrong row index: %d, expected (1 -> %d)!",row,kNRows));
299   fNumOfErr[kWrongRowErr]++;
300   return kFALSE;
301 }
302 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
303 Bool_t AliHMPIDRawStream::CheckDilogic(UInt_t dilogic)
304 {
305 //check dilogic number     
306   if (dilogic>= 1 && dilogic <=kNDILOGICAdd) return kTRUE;
307
308   fRawReader->AddMajorErrorLog(kWrongDilogicErr,Form("dil %d",dilogic));
309   AliWarning(Form("Wrong DILOGIC index: %d, expected (1 -> %d)!",dilogic,kNDILOGICAdd));
310   fNumOfErr[kWrongDilogicErr]++;
311   //dilogic = iDILOGIC;
312   return kFALSE;
313 }      
314 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
315 Bool_t AliHMPIDRawStream::CheckPad(UInt_t pad)
316 {
317 //check pad number     
318   if (pad < kNPadAdd) return kTRUE;
319   
320   fRawReader->AddMajorErrorLog(kWrongPadErr,Form("pad %d",pad));
321   AliWarning(Form("Wrong pad index: %d, expected (0 -> %d)!",pad,kNPadAdd));
322   fNumOfErr[kWrongPadErr]++;
323   return kFALSE;
324 }    
325 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
326 Bool_t AliHMPIDRawStream::CheckEoE(UInt_t word,Int_t &nDil)
327 {
328   if (!((word >> kbit27) & 0x1)) {                                                //check 27th bit in EoE. It must be 1!
329     fRawReader->AddMajorErrorLog(kEoEFlagErr);
330     AliWarning(Form("Missing end-of-event flag! (%08X)",word));
331     fNumOfErr[kEoEFlagErr]++;
332     return kFALSE;
333   }
334   nDil = word & 0x7f;
335   if(nDil < 1 || nDil > 48 ) {
336     fRawReader->AddMajorErrorLog(kEoESizeErr,Form("EoE size=%d",nDil));
337     AliWarning(Form("Wrong end-of-event word-count: %08X",word));
338     fNumOfErr[kEoESizeErr]++;
339     return kFALSE;
340   }
341 //  UInt_t da = (eOfEvent >> 18) & 0xf;
342 //  if (cntData!=0 && da != dilogic) {
343 //    fRawReader->AddMajorErrorLog(kEoEDILOGICErr,Form("eoe dil %d != %d",da,dilogic));
344 //    AliWarning(Form("Wrong DILOGIC address found in end-of-event: %d, expected %d!",da,dilogic));
345 //    fNumOfErr[kEoEDILOGICErr]++;
346 //    return kFALSE;  AliQAChecker::Instance()->Run(AliQA::kHMPID, task, obj) ;  
347
348 //  }
349 //  UInt_t ca = (eOfEvent >> 22) & 0x1f;
350 //  if (cntData!=0 &&  ca != row) {
351 //    fRawReader->AddMajorErrorLog(kEoERowErr,Form("eoe row %d != %d",ca,row));
352 //    AliWarning(Form("Wrong row index found in end-of-event: %d, expected %d!",ca,row));
353 //    fNumOfErr[kEoERowErr]++;
354 //    return kFALSE;
355 //  }
356   return kTRUE;
357 }
358 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
359 Bool_t AliHMPIDRawStream::CheckRowMarker(UInt_t word)
360 {
361   UInt_t nMAXwordsInRow = 0x1EA;
362   UInt_t statusControlRow = 0x32a8; // 0x36a8 for zero suppression
363 //First check on row marker    
364   UInt_t rowControlWord = word >> kbit0 & 0xfbff;
365   
366   if(rowControlWord != statusControlRow) {
367     fRawReader->AddMajorErrorLog(kRowMarkerErr);
368     AliWarning(Form("Wrong row marker %x expected 0x32a8!",rowControlWord));
369     fNumOfErr[kRowMarkerErr]++;
370     return kFALSE;
371   }
372 //Second check on row marker    
373    UInt_t wordsInRow = word >> kbit16 & 0x0fff;                // Number of words after the row marker
374   
375   if (wordsInRow > nMAXwordsInRow) {
376     fRawReader->AddMajorErrorLog(kRowMarkerSizeErr);
377     AliWarning(Form(" FATAL: Number of words %x in a row exceeds the expected value: 0x1EA !",wordsInRow));
378     fNumOfErr[kRowMarkerSizeErr]++;
379     return kFALSE;
380   }
381   
382   return kTRUE;
383 }
384 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
385 UInt_t AliHMPIDRawStream::GetWord(Int_t n,EDirection dir)
386 {
387   // This method returns the n-th 32 bit word
388   // inside the raw data payload.
389   // The method is supposed to be endian (platform)
390   // independent.
391   if (!fData || fPosition < 0) AliFatal("Raw data payload buffer is not yet initialized !");
392   
393   UInt_t word = 0;
394   
395   if(dir==kBwd) n = -n; 
396   fPosition+=4*n-4;
397   
398   StorePosition();
399   
400   word |= fData[fPosition++];
401   word |= fData[fPosition++] << 8;
402   word |= fData[fPosition++] << 16;
403   word |= fData[fPosition++] << 24;
404
405   return word;
406 }
407 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
408 void AliHMPIDRawStream::DumpData(Int_t nw)
409 {
410    for(Int_t i=0;i<nw;i+=4) {
411      if(!(i%16)) printf(" \n %8i) ",i);
412      printf("%02X%02X%02X%02X [ %06i ] ",fData[i+3],fData[i+2],fData[i+1],fData[i+0],fPos[i]);
413    }
414    Printf(" \n -----end of dump ----------- ");
415 }
416 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
417 void AliHMPIDRawStream::StorePosition()
418 {
419 //  if(fPos[fPosition]!=0) {Printf("Position already stored!!! Value %i at address %i",fPos[fPosition],fPosition); return;}
420   iPos++;
421   fPos[fPosition] = iPos;
422 //  if(stDeb)Printf("%i - Actual position %i",iPos,fPosition); 
423 }