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