Fixes in the memory managment, additional protections
[u/mrichter/AliRoot.git] / HMPID / AliHMPIDRawStream.h
index 6f1b4b8728267f22e6a15e4d3ca33caddcf1405f..d95a3fae05b78a78e3c6bfa80c518bc494fc0fb0 100644 (file)
@@ -19,6 +19,7 @@
 #include <AliFstream.h>
 #include "AliHMPIDDigit.h"
 #include "AliDAQ.h"
 #include <AliFstream.h>
 #include "AliHMPIDDigit.h"
 #include "AliDAQ.h"
+#include "AliRawDataHeaderSim.h"
 class AliRawReader;
 
 class AliHMPIDRawStream: public TObject {
 class AliRawReader;
 
 class AliHMPIDRawStream: public TObject {
@@ -30,44 +31,66 @@ class AliHMPIDRawStream: public TObject {
 
     virtual void     Reset();
     virtual Bool_t   Next();
 
     virtual void     Reset();
     virtual Bool_t   Next();
-            void     Init();
+            void     InitVars(Int_t n);
+            void     DelVars();
     
     
-           Int_t    Ch( Int_t ddl,Int_t row,Int_t dil,Int_t pad    ) {return AliHMPIDParam::A2C(fPad[ddl][row][dil][pad]); }            //chamber number
-           
-           Int_t GetDDLNumber()  const { return fDDLNumber; }                                                                            // Provide current DDL number
-    inline Int_t GetCharge(Int_t ddl,Int_t row, Int_t dilogic, Int_t pad);                                                                         // Provide the charge observed in certain row,dilogic,pad channel
-           inline Int_t GetPad(Int_t ddl,Int_t row,Int_t dil,Int_t pad);                                                                        //
-           
-           Int_t   Pc          ( Int_t ddl,Int_t row,Int_t dil,Int_t pad                            ) {return AliHMPIDParam::A2P(fPad[ddl][row][dil][pad]);}                                                 //PC position number
-           Int_t   PadPcX      ( Int_t ddl,Int_t row,Int_t dil,Int_t pad                            ) {return AliHMPIDParam::A2X(fPad[ddl][row][dil][pad]);}                                                 //pad pc x # 0..79
-           Int_t   PadPcY      ( Int_t ddl,Int_t row,Int_t dil,Int_t pad                            ) {return AliHMPIDParam::A2Y(fPad[ddl][row][dil][pad]);}                                                 //pad pc y # 0..47
+           Int_t GetDDLNumber()  const { return fDDLNumber; }                                                                          // Provide current DDL number
+   static inline Int_t GetPad(Int_t ddl,Int_t row,Int_t dil,Int_t pad);                                                                 //
+           Int_t   GetNPads()       const { return fNPads;}
+            Int_t*  GetPadArray()    const { return fPad;}
+            Int_t*  GetChargeArray() const { return fCharge;}
+           Int_t   Pc          ( Int_t ddl,Int_t row,Int_t dil,Int_t pad                            ) {return AliHMPIDParam::A2P(GetPad(ddl,row,dil,pad));}                                                 //PC position number
+           Int_t   PadPcX      ( Int_t ddl,Int_t row,Int_t dil,Int_t pad                            ) {return AliHMPIDParam::A2X(GetPad(ddl,row,dil,pad));}                                                 //pad pc x # 0..79
+           Int_t   PadPcY      ( Int_t ddl,Int_t row,Int_t dil,Int_t pad                            ) {return AliHMPIDParam::A2Y(GetPad(ddl,row,dil,pad));}                                                 //pad pc y # 0..47
    
            inline  Bool_t SetZeroSup (Bool_t isSup);
     inline  Bool_t GetZeroSup(); 
    
            inline  Bool_t SetZeroSup (Bool_t isSup);
     inline  Bool_t GetZeroSup(); 
-                 
-    inline void    Raw            (UInt_t &w32,Int_t &ddl,Int_t &r,Int_t &d,Int_t &a);                                               //digit->(w32,ddl,r,d,a)
-    inline void    Raw            (Int_t ddl,Int_t r,Int_t d,Int_t a);                                                               //raw->abs pad number
-    inline Bool_t  Raw            (UInt_t  w32,Int_t  ddl,AliRawReader *pRR);                                                      //(w32,ddl)->digit
-    inline void   SetCharge      (Int_t ddl,Int_t row,Int_t dil,Int_t pad,Int_t q);
+    inline  Int_t  GetErrors(Int_t ddl,Int_t eType);                                                                                   //Get errors and occurance
+    
+    Bool_t ReadHMPIDRawData();                           // Read HMPID Raw data
+    Bool_t ReadSegment(Int_t &cntSegment);               // Read Segment
+    Bool_t ReadRow(Int_t &cntRow);                       // Read Row
+    Bool_t ReadDilogic(Int_t &cntDilogic);               // Read Dilogic
+
+    Bool_t CheckRow(UInt_t row);                         // Check Row
+    Bool_t CheckDilogic(UInt_t dilogic);                 // Check Dilogic
+    Bool_t CheckPad(UInt_t pad);                         // Check pad
+    Bool_t CheckEoE(Int_t &nDil);                        // Check EoE
+    Bool_t CheckRowMarker();                             // Check RowMarker
+    Bool_t CheckSegment();                               // Check Segment
+    void   DumpData(Int_t nw);                           // Dump Data
+    void   StorePosition();                              //Debug purpose
+    
+//    inline void    Raw            (UInt_t &w32,Int_t &ddl,Int_t &r,Int_t &d,Int_t &a);                                              //digit->(w32,ddl,r,d,a)
+//    inline void    Raw            (Int_t ddl,Int_t r,Int_t d,Int_t a);                                                              //raw->abs pad number
+//    inline Bool_t  Raw            (UInt_t  w32,Int_t  ddl,AliRawReader *pRR);                                                       //(w32,ddl)->digit
     inline void    WriteRaw       (TObjArray *pDigLst                             );                                                      //write as raw stream     
     inline void   WriteRowMarker  (AliFstream *ddl,UInt_t size);
     inline void   WriteEoE        (AliFstream *ddl,UInt_t row,UInt_t dil,UInt_t wordCnt);  
     inline void    WriteRaw       (TObjArray *pDigLst                             );                                                      //write as raw stream     
     inline void   WriteRowMarker  (AliFstream *ddl,UInt_t size);
     inline void   WriteEoE        (AliFstream *ddl,UInt_t row,UInt_t dil,UInt_t wordCnt);  
-    inline void   WriteSegMarker  (AliFstream *ddl,UInt_t row);   
+    inline void   WriteSegMarker  (AliFstream *ddl,UInt_t row, Int_t nwInSeg);   
     
 //    inline TClonesArray  ReMap(TClonesArray *pDigIn);
     
 //    inline TClonesArray  ReMap(TClonesArray *pDigIn);
+enum EDirection {kFwd,kBwd};
+
+enum Ebits {kbit0,kbit1 , kbit2, kbit3, kbit4, kbit5, kbit6, kbit7, kbit8,
+                  kbit9 ,kbit10,kbit11,kbit12,kbit13,kbit14,kbit15,kbit16,
+                  kbit17,kbit18,kbit19,kbit20,kbit21,kbit22,kbit23,kbit24,
+                  kbit25,kbit26,kbit27,kbit28,kbit29,kbit30,kbit31,kbit32};
     
     enum EHMPIDRawStreamError {
     
     enum EHMPIDRawStreamError {
-      kRawDataSizeErr = 1,
-      kRowMarkerErr = 2,
-      kWrongRowErr = 3,
-      kWrongDilogicErr = 4,
-      kWrongPadErr = 5,
-      kEoEFlagErr = 6,
-      kEoESizeErr = 7,
-      kEoEDILOGICErr = 8,
-      kEoERowErr = 9,
-      kBadSegWordErr = 10,
-      kWrongSegErr = 11
+      kRawDataSizeErr   = 1,
+      kRowMarkerErr     = 2,
+      kWrongRowErr      = 3,
+      kWrongDilogicErr  = 4,
+      kWrongPadErr      = 5,
+      kEoEFlagErr       = 6,
+      kEoESizeErr       = 7,
+      kEoEDILOGICErr    = 8,
+      kEoERowErr        = 9,
+      kBadSegWordErr    = 10,
+      kWrongSegErr      = 11,
+      kRowMarkerSizeErr = 12,
+      kSumErr           = 13                                            //This is always the last one, to retreive the number of errors
     };
     
     enum {
     };
     
     enum {
@@ -87,27 +110,25 @@ class AliHMPIDRawStream: public TObject {
     AliHMPIDRawStream& operator = (const AliHMPIDRawStream& stream);
     AliHMPIDRawStream(const AliHMPIDRawStream& stream);
 
     AliHMPIDRawStream& operator = (const AliHMPIDRawStream& stream);
     AliHMPIDRawStream(const AliHMPIDRawStream& stream);
 
-    UInt_t           GetNextWord();
-
-    Int_t            fCharge[kNDDL][kNRows+1][kNDILOGICAdd+1][kNPadAdd]; // Array for charge values for all channels in one DDL
-    
-    Int_t            fPad[kNDDL][kNRows+1][kNDILOGICAdd+1][kNPadAdd]; // Array for abs pad values for all channels in one DDL
-
-    UInt_t           fRawWord[kNDDL][kNRows+1][kNDILOGICAdd+1][kNPadAdd];
-        
-    Int_t            fDDLNumber;    // index of current DDL number
-
-    AliRawReader*    fRawReader;    // object for reading the raw data
-
-    UChar_t*         fData;         // raw data
-
-    Int_t            fPosition;     // current position in fData
-    
-    Bool_t           fZeroSup;
-
-    ClassDef(AliHMPIDRawStream, 0)  // base class for reading HMPID raw digits
+    UInt_t           GetWord(Int_t n=1,EDirection dir=kFwd);             // Get n-th word
+    UInt_t           GetNextWord();                                      // Get next word
+    Int_t            fNPads;                                             // counter of pads in one DDL
+    Int_t           *fCharge;                                            // Array for charge values for all channels in one DDL
+    Int_t           *fPad;                                               // Array for abs pad values for all channels in one DDL
+    Int_t            fDDLNumber;                                         // index of current DDL number
+    AliRawReader    *fRawReader;                                         // object for reading the raw data
+    UChar_t         *fData;                                              // raw data
+    Int_t          **fNumOfErr;                                          // Store the numner of errors for a given error type and a given DDL
+    Int_t            fPosition;                                          // current word
+    UInt_t           fWord;                                              // current position in fData
+    Bool_t           fZeroSup;                                           // set if zero suppression is applied
+    Int_t           *fPos;                                               // for debug purposes
+    Int_t            iPos;                                               // counter for debug
+  
+    ClassDef(AliHMPIDRawStream, 2)                                       // base class for reading HMPID raw digits
 };
 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 };
 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+    /*
 void AliHMPIDRawStream::Raw(UInt_t &w32,Int_t &ddl,Int_t &r,Int_t &d,Int_t &a)
 {
 // Convert raw stream word to raw word format
 void AliHMPIDRawStream::Raw(UInt_t &w32,Int_t &ddl,Int_t &r,Int_t &d,Int_t &a)
 {
 // Convert raw stream word to raw word format
@@ -121,14 +142,16 @@ void AliHMPIDRawStream::Raw(UInt_t &w32,Int_t &ddl,Int_t &r,Int_t &d,Int_t &a)
   a=y2a[PadPcY(ddl,r,d,a)%6]+6*(PadPcX(ddl,r,d,a)%8);                                           //ADDRESS 0..47        
   
   w32=0;    
   a=y2a[PadPcY(ddl,r,d,a)%6]+6*(PadPcX(ddl,r,d,a)%8);                                           //ADDRESS 0..47        
   
   w32=0;    
-  AliBitPacking::PackWord((fCharge[ddl][r][d][a]>4095)?4095:(UInt_t)fCharge[ddl][r][d][a],w32, 0,11);       // 0000 0rrr rrdd ddaa aaaa qqqq qqqq qqqq        Qdc               bits (00..11) counts (0..4095)
+  AliBitPacking::PackWord((fCharge[fNPads]>4095)?4095:(UInt_t)fCharge[fNPads],w32, 0,11);       // 0000 0rrr rrdd ddaa aaaa qqqq qqqq qqqq        Qdc               bits (00..11) counts (0..4095)
   //molnarl: Since in simulation the the charge can be > than 4095 but not in real life we need to protect. If fQ>4095 after packing we will get 0 for the charge! 
   assert(0<=a&&a<=47);AliBitPacking::PackWord(        a ,w32,12,17);  // 3322 2222 2222 1111 1111 1000 0000 0000        DILOGIC address   bits (12..17) counts (0..47)
   assert(1<=d&&d<=10);AliBitPacking::PackWord(        d ,w32,18,21);  // 1098 7654 3210 9876 5432 1098 7654 3210        DILOGIC number    bits (18..21) counts (1..10)
   assert(1<=r&&r<=24);AliBitPacking::PackWord(        r ,w32,22,26);  //                                                Row number        bits (22..26) counts (1..24)  
 }
   //molnarl: Since in simulation the the charge can be > than 4095 but not in real life we need to protect. If fQ>4095 after packing we will get 0 for the charge! 
   assert(0<=a&&a<=47);AliBitPacking::PackWord(        a ,w32,12,17);  // 3322 2222 2222 1111 1111 1000 0000 0000        DILOGIC address   bits (12..17) counts (0..47)
   assert(1<=d&&d<=10);AliBitPacking::PackWord(        d ,w32,18,21);  // 1098 7654 3210 9876 5432 1098 7654 3210        DILOGIC number    bits (18..21) counts (1..10)
   assert(1<=r&&r<=24);AliBitPacking::PackWord(        r ,w32,22,26);  //                                                Row number        bits (22..26) counts (1..24)  
 }
+*/
 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-void AliHMPIDRawStream::Raw(Int_t ddl,Int_t r,Int_t d,Int_t a)
+    /*
+Int_t AliHMPIDRawStream::Raw(Int_t ddl,Int_t r,Int_t d,Int_t a)
 {
   //Assign absolute pad ID based on ddl,row,dil,pad
   //Arguments: DDL, row number, dilogic number, dilogic address(pad)
 {
   //Assign absolute pad ID based on ddl,row,dil,pad
   //Arguments: DDL, row number, dilogic number, dilogic address(pad)
@@ -140,7 +163,7 @@ void AliHMPIDRawStream::Raw(Int_t ddl,Int_t r,Int_t d,Int_t a)
   Int_t tmp=(r-1)/8;              Int_t pc=(ddl%2)? 5-2*tmp:2*tmp; 
                                   Int_t px=(d-1)*8+a/6;
         tmp=(ddl%2)?(24-r):r-1;   Int_t py=6*(tmp%8)+a2y[a%6];
   Int_t tmp=(r-1)/8;              Int_t pc=(ddl%2)? 5-2*tmp:2*tmp; 
                                   Int_t px=(d-1)*8+a/6;
         tmp=(ddl%2)?(24-r):r-1;   Int_t py=6*(tmp%8)+a2y[a%6];
-  fPad[ddl][r][d][a]=AliHMPIDParam::Abs(ch,pc,px,py);
+  return AliHMPIDParam::Abs(ch,pc,px,py);
 }
 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 Bool_t AliHMPIDRawStream::Raw(UInt_t w32,Int_t ddl, AliRawReader *pRR)
 }
 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 Bool_t AliHMPIDRawStream::Raw(UInt_t w32,Int_t ddl, AliRawReader *pRR)
@@ -162,16 +185,7 @@ Bool_t AliHMPIDRawStream::Raw(UInt_t w32,Int_t ddl, AliRawReader *pRR)
   fCharge[ddl][r][d][a]=q;
   return kTRUE;
 }
   fCharge[ddl][r][d][a]=q;
   return kTRUE;
 }
-//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-void AliHMPIDRawStream::SetCharge(Int_t ddl,Int_t row,Int_t dil,Int_t pad,Int_t q)
-{ 
-  //Setter for the charger in the raw stream
-  //Arguments: DDL, row number, dilogic number, dilogic address(pad), charge
-  //Returns:   Charge from the raw stream
-     fCharge[ddl][row][dil][pad]=q;
-     //  return fCharge[ddl][row][dil][pad];
-      
-}
+*/
 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 Int_t AliHMPIDRawStream::GetPad(Int_t ddl,Int_t row,Int_t dil,Int_t pad)
 {
 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 Int_t AliHMPIDRawStream::GetPad(Int_t ddl,Int_t row,Int_t dil,Int_t pad)
 {
@@ -191,47 +205,18 @@ Int_t AliHMPIDRawStream::GetPad(Int_t ddl,Int_t row,Int_t dil,Int_t pad)
         tmp=(ddl%2)?(24-row):row-1;   
                                   Int_t py=6*(tmp%8)+a2y[pad%6];
                                   
         tmp=(ddl%2)?(24-row):row-1;   
                                   Int_t py=6*(tmp%8)+a2y[pad%6];
                                   
-  return fPad[ddl][row][dil][pad]=AliHMPIDParam::Abs(ch,pc,px,py);
+  return AliHMPIDParam::Abs(ch,pc,px,py);
 }//GetPad()
 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 }//GetPad()
 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-Int_t AliHMPIDRawStream::GetCharge(Int_t ddl,Int_t row, Int_t dilogic, Int_t pad)
-{
-  // The method returns the charge collected
-  // in a particular channel
-  // Return -1 in case the charge from the channels
-  // has not been read or invalid arguments
-  if (ddl < 0 || ddl > kNDDL) {
-    AliError(Form("Wrong DDL index %d!",ddl));
-    return 0;
-  }  
-  if (row < 1 || row > kNRows) {
-    AliError(Form("Wrong row index %d!",row));
-    return 0;
-  }
-
-  if (dilogic < 1 || dilogic > kNDILOGICAdd) {
-    AliError(Form("Wrong DILOGIC address %d!",dilogic));
-    return 0;
-  }
-  
-  if (pad >= kNPadAdd) {
-    AliError(Form("Wrong pad index %d!",pad));
-    return 0;
-  }
-
-  return fCharge[ddl][row][dilogic][pad];
-  
-}
-//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 void AliHMPIDRawStream::WriteRowMarker(AliFstream *ddl,UInt_t size)
 {
   //Writes the row marker for real data and pedestal into the ddl stream
   //Arguments: ddl stream and the size of the block of the given row, the siye is at least the 10 EoE words!
   //Returns:   nothing
   UInt_t w32=0;
 void AliHMPIDRawStream::WriteRowMarker(AliFstream *ddl,UInt_t size)
 {
   //Writes the row marker for real data and pedestal into the ddl stream
   //Arguments: ddl stream and the size of the block of the given row, the siye is at least the 10 EoE words!
   //Returns:   nothing
   UInt_t w32=0;
-  UInt_t marker=12968;                                    //ror marker: 32a8 in hexa; 12968 in decimal
+  UInt_t marker=13992;                                   //for pedestal=12968  ==  32a8 for zero suppressed 36a8
   AliBitPacking::PackWord(size,  w32, 16,31);            //number of roaw written after row marker (digits and EoE)
   AliBitPacking::PackWord(size,  w32, 16,31);            //number of roaw written after row marker (digits and EoE)
-  AliBitPacking::PackWord(marker,w32,0,15);              //32a8=12968
+  AliBitPacking::PackWord(marker,w32,0,15);              //the marker word
   ddl->WriteBuffer((char*)&w32,sizeof(w32));              
 }
 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   ddl->WriteBuffer((char*)&w32,sizeof(w32));              
 }
 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
@@ -250,15 +235,20 @@ void AliHMPIDRawStream::WriteEoE(AliFstream *ddl,UInt_t row,UInt_t dil,UInt_t wo
   ddl->WriteBuffer((char*)&w32,sizeof(w32));      
 } 
 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++     
   ddl->WriteBuffer((char*)&w32,sizeof(w32));      
 } 
 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++     
-void AliHMPIDRawStream::WriteSegMarker(AliFstream *ddl,UInt_t row)
+void AliHMPIDRawStream::WriteSegMarker(AliFstream *ddl,UInt_t row, Int_t nwInSeg)
 {
   //Writes the segment marker (after 8 rows) into the ddl stream
 {
   //Writes the segment marker (after 8 rows) into the ddl stream
-  //Arguments: ddl stream and the segment: row 8 -> 0x5900, row 16 -> 5901, row 24 -> 5902
+  //Arguments: ddl stream and the segment: row 8 -> 0x5800, row 16 -> 5801, row 24 -> 5802 for pedestal
   //Retruns:   nothing
     UInt_t w32=0;
   //Retruns:   nothing
     UInt_t w32=0;
-                                AliBitPacking::PackWord((UInt_t)43791,        w32,16,31);       //43791==AB0F
-                                AliBitPacking::PackWord((UInt_t)(22784+row/8),w32, 0,15);       //22784==5900    
-    ddl->WriteBuffer((char*)&w32,sizeof(w32)); 
+
+      //Segment marker: 2736 == ab0
+      //AliBitPacking::PackWord((UInt_t)0   ,w32,27,31);          //zero out the rest of the bits, since they are not needed
+      AliBitPacking::PackWord((UInt_t)2736   ,w32,20,31);       //ab0 the segment marker word
+      AliBitPacking::PackWord((UInt_t)nwInSeg,w32, 8,19);       //number of words in the segment
+      AliBitPacking::PackWord((UInt_t)(row/8),w32, 0, 7);       //segment 0,1,2    
+      ddl->WriteBuffer((char*)&w32,sizeof(w32)); 
+      //Printf("Segment word created is: %x",w32);
 }      
 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++     
 Bool_t AliHMPIDRawStream::SetZeroSup (Bool_t isSup)
 }      
 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++     
 Bool_t AliHMPIDRawStream::SetZeroSup (Bool_t isSup)
@@ -288,6 +278,7 @@ void AliHMPIDRawStream::WriteRaw(TObjArray *pDigAll)
   Int_t  cntLeoe,cntReoe;
   UInt_t posL,posR;
   UInt_t cntLseg,cntRseg;
   Int_t  cntLeoe,cntReoe;
   UInt_t posL,posR;
   UInt_t cntLseg,cntRseg;
+  UInt_t cntwInLseg=0,cntwInRseg=0;
   Int_t  cntRdig=0,cntLdig=0;
   
   UInt_t posLmarker,posRmarker;
   Int_t  cntRdig=0,cntLdig=0;
   
   UInt_t posLmarker,posRmarker;
@@ -310,7 +301,7 @@ void AliHMPIDRawStream::WriteRaw(TObjArray *pDigAll)
     AliFstream* ddlL;                                 //output streams, 2 per chamber
     AliFstream* ddlR;                          
     
     AliFstream* ddlL;                                 //output streams, 2 per chamber
     AliFstream* ddlR;                          
     
-    AliRawDataHeader header; header.SetAttribute(0);  //empty DDL header
+    AliRawDataHeaderSim header; header.SetAttribute(0);  //empty DDL header
     
     ddlL = new AliFstream(AliDAQ::DdlFileName("HMPID",2*iCh+1)); //left and right looking at the IP
     ddlR = new AliFstream(AliDAQ::DdlFileName("HMPID",2*iCh));   //open both DDL of this chamber in parallel
     
     ddlL = new AliFstream(AliDAQ::DdlFileName("HMPID",2*iCh+1)); //left and right looking at the IP
     ddlR = new AliFstream(AliDAQ::DdlFileName("HMPID",2*iCh));   //open both DDL of this chamber in parallel
@@ -322,7 +313,7 @@ void AliHMPIDRawStream::WriteRaw(TObjArray *pDigAll)
     digcnt=0;
     
     TClonesArray *pDigCh=(TClonesArray *)pDigAll->At(iCh); //list of digits for current chamber 
     digcnt=0;
     
     TClonesArray *pDigCh=(TClonesArray *)pDigAll->At(iCh); //list of digits for current chamber 
-    //Printf("::::::::::::::::::; Number of Digits to write: %d == %d",pDigCh->GetEntriesFast(),pDigCh->GetEntries());
+   
     for(Int_t iDig=0;iDig<pDigCh->GetEntriesFast();iDig++){//digits loop
       AliHMPIDDigit *pDig1=(AliHMPIDDigit*)pDigCh->At(iDig);
       pDig1->Raw(w32,ddl,r,d,a);
     for(Int_t iDig=0;iDig<pDigCh->GetEntriesFast();iDig++){//digits loop
       AliHMPIDDigit *pDig1=(AliHMPIDDigit*)pDigCh->At(iDig);
       pDig1->Raw(w32,ddl,r,d,a);
@@ -332,8 +323,8 @@ void AliHMPIDRawStream::WriteRaw(TObjArray *pDigAll)
     for(Int_t row = 1; row <= AliHMPIDRawStream::kNRows; row++){ //AliHMPIDRawStream::kNRows=25!
       cntRrow=0;cntLrow=0;cntLseg=0;cntRseg=0;// 
       cntLeoe=0;cntReoe=0;
     for(Int_t row = 1; row <= AliHMPIDRawStream::kNRows; row++){ //AliHMPIDRawStream::kNRows=25!
       cntRrow=0;cntLrow=0;cntLseg=0;cntRseg=0;// 
       cntLeoe=0;cntReoe=0;
-      posLmarker=ddlL->Tellp(); WriteRowMarker(ddlL,(UInt_t)1);   cntL++; cntRrow++; 
-      posRmarker=ddlR->Tellp(); WriteRowMarker(ddlR,(UInt_t)1);   cntR++; cntLrow++; 
+      posLmarker=ddlL->Tellp(); WriteRowMarker(ddlL,(UInt_t)1);   cntL++; cntRrow++; cntwInRseg++;
+      posRmarker=ddlR->Tellp(); WriteRowMarker(ddlR,(UInt_t)1);   cntR++; cntLrow++; cntwInLseg++;
       for(Int_t dil = 1; dil <= AliHMPIDRawStream::kNDILOGICAdd; dil++){ //AliHMPIDRawStream::kNDILOGICAdd = 11!
        cntLpad=0;cntRpad=0;
         for(Int_t pad = 0; pad < AliHMPIDRawStream::kNPadAdd; pad++){   //AliHMPIDRawStream::kNPadAdd     = 48
       for(Int_t dil = 1; dil <= AliHMPIDRawStream::kNDILOGICAdd; dil++){ //AliHMPIDRawStream::kNDILOGICAdd = 11!
        cntLpad=0;cntRpad=0;
         for(Int_t pad = 0; pad < AliHMPIDRawStream::kNPadAdd; pad++){   //AliHMPIDRawStream::kNPadAdd     = 48
@@ -344,19 +335,19 @@ void AliHMPIDRawStream::WriteRaw(TObjArray *pDigAll)
              if(pDig->Q() < 0 ) continue;                                                 //We can turn of the zero sup for pedestal simulation
               //Printf("::::::::::::::: ddl from Digit : %d",ddl);
              if(ddl%2){                                                                               //write raw digit selecting on DDL
              if(pDig->Q() < 0 ) continue;                                                 //We can turn of the zero sup for pedestal simulation
               //Printf("::::::::::::::: ddl from Digit : %d",ddl);
              if(ddl%2){                                                                               //write raw digit selecting on DDL
-               ddlL->WriteBuffer((char*)&w32,sizeof(w32));   cntL++; cntLpad++; cntLrow++;  cntLdig++;//Printf(" WL: %x isDig: %d",w32,isDigThere[iddl][row][dil][pad]);
+               ddlL->WriteBuffer((char*)&w32,sizeof(w32));   cntL++; cntLpad++; cntLrow++;  cntLdig++; cntwInLseg++;//Printf(" WL: %x isDig: %d",w32,isDigThere[iddl][row][dil][pad]);
               }else{
               }else{
-               ddlR->WriteBuffer((char*)&w32,sizeof(w32));   cntR++; cntRpad++; cntRrow++;   cntRdig++;//Printf(" WR: %x isDig: %d",w32,isDigThere[iddl][row][dil][pad]);
+               ddlR->WriteBuffer((char*)&w32,sizeof(w32));   cntR++; cntRpad++; cntRrow++;   cntRdig++;cntwInRseg++;//Printf(" WR: %x isDig: %d",w32,isDigThere[iddl][row][dil][pad]);
              }
             }//ddl 
           }//isDig
        }//pad
              }
             }//ddl 
           }//isDig
        }//pad
-        WriteEoE(ddlL,row,dil,cntLpad); cntL++;  cntLrow++;    cntLeoe++;                                 //molnarl: write EoE markers
-        WriteEoE(ddlR,row,dil,cntRpad); cntR++;  cntRrow++;    cntReoe++;
+        WriteEoE(ddlL,row,dil,cntLpad); cntL++;  cntLrow++;    cntLeoe++;   cntwInLseg++;                              //molnarl: write EoE markers
+        WriteEoE(ddlR,row,dil,cntRpad); cntR++;  cntRrow++;    cntReoe++;   cntwInRseg++;
       }//dil
       if(row%8==0){                                               
       }//dil
       if(row%8==0){                                               
-        WriteSegMarker(ddlL,row); cntL++;  cntLseg++;
-        WriteSegMarker(ddlR,row); cntR++;  cntRseg++;
+        WriteSegMarker(ddlL,row,cntwInLseg); cntL++;  cntLseg++; cntwInLseg=0;
+        WriteSegMarker(ddlR,row,cntwInRseg); cntR++;  cntRseg++;  cntwInRseg=0; 
       }
       posL=ddlL->Tellp();   ddlL->Seekp(posLmarker);    WriteRowMarker(ddlL,(UInt_t)(cntLrow-1)); ddlL->Seekp(posL);      //find the marker position write and  go back to the actual position to continue writing                    
       posR=ddlR->Tellp();   ddlR->Seekp(posRmarker);    WriteRowMarker(ddlR,(UInt_t)(cntRrow-1)); ddlR->Seekp(posR);                           
       }
       posL=ddlL->Tellp();   ddlL->Seekp(posLmarker);    WriteRowMarker(ddlL,(UInt_t)(cntLrow-1)); ddlL->Seekp(posL);      //find the marker position write and  go back to the actual position to continue writing                    
       posR=ddlR->Tellp();   ddlR->Seekp(posRmarker);    WriteRowMarker(ddlR,(UInt_t)(cntRrow-1)); ddlR->Seekp(posR);                           
@@ -369,5 +360,17 @@ void AliHMPIDRawStream::WriteRaw(TObjArray *pDigAll)
   }//chambers loop
 }//WriteRaw()
 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   }//chambers loop
 }//WriteRaw()
 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+Int_t AliHMPIDRawStream::GetErrors(Int_t ddl,Int_t eType)
+{
+// Return the number of errors for a given error tye during raw data reading
+// Arguments: errorType
+//   Returns: error or -999 if error Type does not exist
+  
+  if(eType < 1 || eType> kSumErr-1 ) return -999;
+  else if( ddl < 0 || ddl > kNDDL-1 )  return -999;
+  else if (!fNumOfErr) return -999;
+  else return fNumOfErr[ddl][eType];
+} //GetErrors()     
+//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
 #endif
 
 #endif