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