]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - PMD/AliPMDDDLRawData.cxx
parity bit is introduced
[u/mrichter/AliRoot.git] / PMD / AliPMDDDLRawData.cxx
index 1f32a0d75f1996ade0cfae0d0fc449967880acce..83fe31458928e045b0b8c426db3cd304e6c65e63 100644 (file)
 #include <TTree.h>
 #include <TBranch.h>
 #include <TMath.h>
+#include <TString.h>
+#include <TSystem.h>
 
+#include "AliLog.h"
+#include "AliRawDataHeader.h"
+#include "AliBitPacking.h"
 #include "AliPMDdigit.h"
+#include "AliPMDBlockHeader.h"
+#include "AliPMDDspHeader.h"
+#include "AliPMDPatchBusHeader.h"
+#include "AliPMDRawStream.h"
 #include "AliPMDDDLRawData.h"
-
+#include "AliDAQ.h"
 
 ClassImp(AliPMDDDLRawData)
 
@@ -30,6 +39,24 @@ AliPMDDDLRawData::AliPMDDDLRawData():
 {
   // Default Constructor
   //
+
+}
+//____________________________________________________________________________
+AliPMDDDLRawData::AliPMDDDLRawData(const AliPMDDDLRawData& ddlraw):
+  TObject(ddlraw),
+  fDigits(ddlraw.fDigits)
+{
+  //Copy Constructor 
+}
+//____________________________________________________________________________
+AliPMDDDLRawData & AliPMDDDLRawData::operator=(const AliPMDDDLRawData& ddlraw)
+{
+  //Assignment operator 
+  if(this != &ddlraw)
+    {
+      fDigits = ddlraw.fDigits;
+    }
+  return *this;
 }
 //____________________________________________________________________________
 
@@ -41,42 +68,44 @@ AliPMDDDLRawData::~AliPMDDDLRawData()
 }
 
 //____________________________________________________________________________
-void AliPMDDDLRawData::WritePMDRawData(TTree *treeD, Int_t evtno)
+void AliPMDDDLRawData::WritePMDRawData(TTree *treeD)
 {
   // write digits into raw data format
 
   ofstream outfile;
 
   TBranch *branch = treeD->GetBranch("PMDDigit");
-  if (!branch) return;
+  if (!branch)
+    {
+      AliError("PMD Digit branch not found");
+      return;
+    }
   branch->SetAddress(&fDigits);  
   
-  //  Int_t   nmodules = (Int_t) treeD->GetEntries();
-  //  cout << " nmodules = " << nmodules << endl;
+  Int_t   nmodules = (Int_t) treeD->GetEntries();
+  AliDebug(1,Form("Number of modules inside treeD = %d",nmodules));
 
-  const Int_t kSize         = 4608;
-  const Int_t kDDL          = 6;
+  const Int_t kDDL          = AliDAQ::NumberOfDdls("PMD");
   Int_t modulePerDDL        = 0;
 
 
+  AliRawDataHeader header;
   UInt_t sizeRawData = 0;
-  UInt_t magicWord   = 0x123456;
-  UInt_t detectorID  = 0;
-  UInt_t ddlID       = 0;
-  Int_t  flag        = 0;
-  Int_t  version     = 1;
-  UInt_t mHSize      = 3*sizeof(UInt_t);
-  Int_t  totword     = 0;
-
-  UInt_t miniHeader[3];
+  
+  const Int_t kSize = 1536;
   UInt_t buffer[kSize];
 
+  UInt_t busPatch[50][1536];
+
+  Int_t contentsBus[50];
+
   Char_t filename[80];
 
+
   Int_t mmodule = 0;
   for(Int_t iddl = 0; iddl < kDDL; iddl++)
     {
-      sprintf(filename,"Ev%dPMDddl%d.dat",evtno,iddl);
+      strcpy(filename,AliDAQ::DdlFileName("PMD",iddl));
 #ifndef __DECCXX
       outfile.open(filename,ios::binary);
 #else
@@ -87,388 +116,581 @@ void AliPMDDDLRawData::WritePMDRawData(TTree *treeD, Int_t evtno)
        {
          modulePerDDL = 6;
          mmodule = 6*iddl;
-         detectorID = 0;
        }
       else if (iddl == 4)
        {
          modulePerDDL = 12;
          mmodule = 24;
-         detectorID = 1;
        }
       else if (iddl == 5)
        {
          modulePerDDL = 12;
-         mmodule = 36;
-         detectorID = 1;
+         mmodule = 30;
        }
 
-      miniHeader[0] = sizeRawData;
-      PackWord(0,23,magicWord,miniHeader[1]);
-      PackWord(24,31,detectorID,miniHeader[1]);
-      ddlID = iddl;
-      PackWord(0,15,ddlID,miniHeader[2]);
-      PackWord(16,23,flag,miniHeader[2]);
-      PackWord(24,31,version,miniHeader[2]);
 
 
-      // Write the Dummy Mini Header into the file
+      // Write the Dummy Data Header into the file
       Int_t bHPosition = outfile.tellp();
-      outfile.write((char*)(miniHeader),mHSize);
-
+      outfile.write((char*)(&header),sizeof(header));
 
-      for(Int_t ium = 0; ium < modulePerDDL; ium++)
+      for (Int_t ibus = 0; ibus < 50; ibus++)
        {
-         
-         for (Int_t i = 0; i < kSize; i++)
+         contentsBus[ibus] = 0;
+         for (Int_t ich = 0; ich < 1536; ich++)
            {
-             buffer[i] = 0;
+             busPatch[ibus][ich] = 0;
            }
+       }
+
+      for(Int_t ium = 0; ium < modulePerDDL; ium++)
+       {
+         if (iddl == 4 && ium == 6) mmodule = 42;
+
          // Extract energy deposition per cell and pack it
          // in a 32-bit word and returns all the total words
          // per one unit-module
          
-         GetUMDigitsData(treeD, mmodule, ium, iddl, totword, buffer);
-         
-         outfile.write((char*)buffer,totword*sizeof(UInt_t));
-
+         GetUMDigitsData(treeD, mmodule, iddl, contentsBus, busPatch);
          mmodule++;
-
        }
 
-      // Write real mini header
-      // take the pointer to the beginning of the mini header
-      // write the total number of words per ddl and bring the
-      // pointer to the current file position and close it
-      UInt_t cFPosition = outfile.tellp();
-      sizeRawData = cFPosition - bHPosition - mHSize;
-      outfile.seekp(bHPosition);
-      outfile.write((char*)(&sizeRawData),sizeof(UInt_t));
-      outfile.seekp(cFPosition);
+      
 
-      outfile.close();
-    } // DDL Loop over
+      Int_t ij = 0;
+      Int_t dsp[10];
+      Int_t dspBus[10];
+      for (Int_t i = 0; i < 10; i++)
+       {
+         dsp[i] = 0;
+         dspBus[i] = 0;
+         for (Int_t ibus=0; ibus < 5; ibus++)
+           {
+             if (contentsBus[ij] > 0)
+               {
+                 dsp[i] += contentsBus[ij];
+                 dspBus[i]++;
+               }
+             ij++;
+           }
+         // Add the patch Bus header to the DSP contents
+         dsp[i] += 4*dspBus[i];
+       }
 
+      Int_t dspBlockARDL    = 0;
+      Int_t dspBlockBRDL    = 0;
 
-}
-//____________________________________________________________________________
+      for (Int_t i = 0; i < 5; i++)
+       {
+         Int_t ieven = 2*i;
+         Int_t iodd  = 2*i + 1;
+         if (dsp[ieven] > 0)
+           {
+             dspBlockARDL += dsp[ieven];
+             Int_t remainder = dsp[ieven]%2;
+             if (remainder == 1)
+               {
+                 dspBlockARDL++;
+               }
+           }
+         if (dsp[iodd] > 0)
+           {
+             dspBlockBRDL += dsp[iodd];
+             Int_t remainder = dsp[ieven]%2;
+             if (remainder == 1)
+               {
+                 dspBlockARDL++;
+               }
+           }
+       }
+      
+      // Start writing the DDL file
 
-void AliPMDDDLRawData::ReadPMDRawData(Int_t evtno)
-{
-  // reads the raw data
-  ifstream infile;
+      AliPMDBlockHeader blHeader;
+      AliPMDDspHeader   dspHeader;
+      AliPMDPatchBusHeader pbusHeader;
 
-  Char_t filename[80];
+      const Int_t kblHLen   = blHeader.GetHeaderLength();
+      const Int_t kdspHLen  = dspHeader.GetHeaderLength();
+      const Int_t kpbusHLen = pbusHeader.GetHeaderLength();
 
-  const Int_t kDDL = 6;
-  Int_t imodule = 0;
+      UInt_t dspRDL = 0;
+      UInt_t dspBlockHeaderWord[8];
+      UInt_t dspHeaderWord[10];
+      UInt_t patchBusHeaderWord[4];
+      Int_t iskip[5];
 
-  for(Int_t iddl = 0; iddl < kDDL; iddl++)
-    {
-      sprintf(filename,"Ev%dPMDddl%d.dat",evtno,iddl);
-#ifndef __DECCXX
-      infile.open(filename,ios::binary);
-#else
-      infile.open(filename);
-#endif
-      
-      Int_t  ium;
-      Int_t  irow;
-      Int_t  icol;
-      UInt_t baseword;
-      UInt_t miniHeader[3];
-      UInt_t sizeRawData;
-      UInt_t mHSize = 3*sizeof(UInt_t);
-
-      infile.read((char*)(miniHeader), mHSize);
-      
-      sizeRawData = miniHeader[0];
-      Int_t totword = sizeRawData/4;
-      
-      UInt_t adc;
-      UInt_t chno;
-      UInt_t mcmno;
-      
-      for (Int_t iword = 0; iword < totword; iword++)
+
+      for (Int_t iblock = 0; iblock < 2; iblock++)
        {
-         infile.read((char*)(&baseword),sizeof(UInt_t));
-         
-         UnpackWord(0,11,adc,baseword);
-         UnpackWord(12,17,chno,baseword);
-         UnpackWord(18,28,mcmno,baseword);
+         // DSP Block Header
          
-         GetRowCol(iddl, mcmno, chno, ium, irow, icol);
-         if (iddl < 4)
+         for (Int_t i=0; i<kblHLen; i++)
            {
-             imodule = iddl*6 + ium;
+             dspBlockHeaderWord[i] = 0;
            }
-         else if (iddl == 4)
+         if (iblock == 0)
            {
-             imodule = 24 + ium;
+             dspBlockHeaderWord[1] = (UInt_t) (dspBlockARDL + kblHLen);
+             dspBlockHeaderWord[2] = (UInt_t) dspBlockARDL;
            }
-         else if (iddl == 5)
+         else if (iblock == 1)
            {
-             imodule = 36 + ium;
+             dspBlockHeaderWord[1] = (UInt_t) (dspBlockBRDL + kblHLen);
+             dspBlockHeaderWord[2] = (UInt_t) dspBlockBRDL;
            }
 
-         
+         outfile.write((char*)dspBlockHeaderWord,kblHLen*sizeof(UInt_t));
+
+         if (iblock == 0)
+           {
+             iskip[0] = 0;
+             iskip[1] = 10;
+             iskip[2] = 20;
+             iskip[3] = 30;
+             iskip[4] = 40;
+           }
+         else if (iblock == 1)
+           {
+             iskip[0] = 5;
+             iskip[1] = 15;
+             iskip[2] = 25;
+             iskip[3] = 35;
+             iskip[4] = 45;
+           }
+
+         for (Int_t idsp = 0; idsp < 5; idsp++)
+           {
+             // DSP Header
+             Int_t dspno = 0;
+             if (iblock == 0)
+               {
+                 dspno = 2*idsp;
+                 dspRDL = (UInt_t) dsp[dspno];
+               }
+             else if (iblock == 1)
+               {
+                 dspno = 2*idsp + 1;
+                 dspRDL = (UInt_t) dsp[dspno];
+               }
+
+             for (Int_t i=0; i<kdspHLen; i++)
+               {
+                 dspHeaderWord[i] = 0;
+               }
+             Int_t remainder = dspRDL%2;
+             if (remainder == 1) dspRDL++;
+
+             dspHeaderWord[1] = dspRDL + kdspHLen;
+             dspHeaderWord[2] = dspRDL;
+             dspHeaderWord[3] = dspno;
+             if (remainder == 1) dspHeaderWord[8] = 1; // setting the padding word
+             outfile.write((char*)dspHeaderWord,kdspHLen*sizeof(UInt_t));
+
+             for (Int_t ibus = 0; ibus < 5; ibus++)
+               {
+                 // Patch Bus Header
+                 Int_t busno = iskip[idsp] + ibus;
+                 Int_t patchbusRDL = contentsBus[busno];
+
+                 if (patchbusRDL > 0)
+                   {
+                     patchBusHeaderWord[0] = 0;
+                     patchBusHeaderWord[1] = (UInt_t) (patchbusRDL + kpbusHLen);
+                     patchBusHeaderWord[2] = (UInt_t) patchbusRDL;
+                     patchBusHeaderWord[3] = (UInt_t) busno;
+                   }
+                 else if (patchbusRDL == 0)
+                   {
+                     patchBusHeaderWord[0] = 0;
+                     patchBusHeaderWord[1] = (UInt_t) kpbusHLen;
+                     patchBusHeaderWord[2] = (UInt_t) 0;
+                     patchBusHeaderWord[3] = (UInt_t) busno;
+                   }
+
+
+                 outfile.write((char*)patchBusHeaderWord,4*sizeof(UInt_t));
+
+
+                 for (Int_t iword = 0; iword < patchbusRDL; iword++)
+                   {
+                     buffer[iword] = busPatch[busno][iword];
+                   }
+                 
+                 outfile.write((char*)buffer,patchbusRDL*sizeof(UInt_t));
+
+               } // End of patch bus loop
+
+
+             // Adding a padding word if the total words odd
+             if (remainder == 1)
+               {
+                 UInt_t paddingWord = dspHeader.GetDefaultPaddingWord();
+                 outfile.write((char*)(&paddingWord),sizeof(UInt_t));
+               }
+           }
        }
 
-    } // ddl loop
+      // Write real data header
+      // take the pointer to the beginning of the data header
+      // write the total number of words per ddl and bring the
+      // pointer to the current file position and close it
+      UInt_t cFPosition = outfile.tellp();
+      sizeRawData = cFPosition - bHPosition - sizeof(header);
+
+      header.fSize = cFPosition - bHPosition;
+      header.SetAttribute(0);  // valid data
+      outfile.seekp(bHPosition);
+      outfile.write((char*)(&header),sizeof(header));
+      outfile.seekp(cFPosition);
+
+      outfile.close();
+    } // DDL Loop over
+
+
 }
 //____________________________________________________________________________
-void AliPMDDDLRawData::GetUMDigitsData(TTree *treeD, Int_t imodule, Int_t ium,
-                                      Int_t ddlno, Int_t & totword,
-                                      UInt_t *buffer)
+void AliPMDDDLRawData::GetUMDigitsData(TTree *treeD, Int_t imodule,
+                                      Int_t ddlno, Int_t *contentsBus,
+                                      UInt_t busPatch[][1536])
 {
-
-  UInt_t dataword, baseword;
-
+  // Retrives digits data UnitModule by UnitModule
+  UInt_t baseword;
   UInt_t mcmno, chno;
   UInt_t adc;
-  Int_t  irownew = 0;
-  Int_t  icolnew = 0;
   Int_t  det, smn, irow, icol;
-
-
-  treeD->GetEntry(imodule); 
-  Int_t nentries = fDigits->GetLast();
-  totword = nentries+1;
-
-  for (Int_t ient = 0; ient < totword; ient++)
+  Int_t  parity;
+
+  const Int_t kMaxBus = 50;
+  Int_t totPatchBus, bPatchBus, ePatchBus;
+  Int_t ibus, totmcm, rows, cols, rowe, cole;
+  Int_t moduleno;
+  Int_t busno = 0;
+  Int_t patchBusNo[kMaxBus], mcmperBus[kMaxBus];
+  Int_t startRowBus[kMaxBus], startColBus[kMaxBus];
+  Int_t endRowBus[kMaxBus], endColBus[kMaxBus];
+
+  Int_t beginPatchBus = -1;
+  Int_t endPatchBus   = -1;
+  for(Int_t i = 0; i < kMaxBus; i++)
     {
-      fPMDdigit = (AliPMDdigit*)fDigits->UncheckedAt(ient);
-      
-      det    = fPMDdigit->GetDetector();
-      smn    = fPMDdigit->GetSMNumber();
-      irow   = fPMDdigit->GetRow();
-      icol   = fPMDdigit->GetColumn();
-      adc    = (UInt_t) fPMDdigit->GetADC();
-
-      if(smn < 12)
-       {
-         irownew = icol;
-         icolnew = irow;
-       }
-      else if( smn >= 12 && smn < 24)
-       {
-         irownew = irow;
-         icolnew = icol;
-       }
-
+      patchBusNo[i]  = -1;
+      mcmperBus[i]   = -1;
+      startRowBus[i] = -1;
+      startColBus[i] = -1;
+      endRowBus[i]   = -1;
+      endColBus[i]   = -1;
+    }
+  Int_t modulePerDDL = 0;
+  if (ddlno < 4)
+    {
+      modulePerDDL = 6;
+    }
+  else if (ddlno == 4 || ddlno == 5)
+    {
+      modulePerDDL = 12;
+    }
 
 
-      GetMCMCh(ddlno, ium, irownew, icolnew, mcmno, chno);
 
 
-      baseword = 0;
-      dataword = adc;
-      PackWord(0, 11,dataword,baseword);
-      dataword = chno;
-      PackWord(12,17,dataword,baseword);
-      dataword = mcmno;
-      PackWord(18,28,dataword,baseword);
-      dataword = 0;
-      PackWord(29,29,dataword,baseword);
-      dataword = 0;
-      PackWord(30,30,dataword,baseword);
-      dataword = 1;
-      PackWord(31,31,dataword,baseword);
-      
-      buffer[ient] = baseword;
-      
 
+  TString fileName(gSystem->Getenv("ALICE_ROOT"));
 
-      
+  if(ddlno == 0)
+    {
+      fileName += "/PMD/PMD_Mapping_ddl0.dat";
     }
-
-
-}
-//____________________________________________________________________________
-void AliPMDDDLRawData::GetMCMCh(Int_t ddlno, Int_t um,
-                               Int_t row, Int_t col,
-                               UInt_t &mcmno, UInt_t &chno)
-{
-  // This part will be modified once the final track layout on the PCB is
-  // designed. This will only change the coordinate of the individual cell
-  UInt_t ch[16][4] = { {3, 2, 29, 28},
-                      {5, 1, 31, 27},
-                      {6, 0, 30, 26},
-                      {7, 4, 25, 24},
-                      {8, 9, 20, 23},
-                      {10, 14, 16, 22},
-                      {11, 15, 17, 21},
-                      {12, 13, 18, 19},
-                      {35, 34, 61, 60},
-                      {37, 33, 63, 59},
-                      {38, 32, 62, 58},
-                      {39, 36, 57, 56},
-                      {40, 41, 52, 55},
-                      {42, 46, 48, 54},
-                      {43, 47, 49, 53},
-                      {44, 45, 50, 51} };
-  
-  if (ddlno == 0 || ddlno == 1)
+  else if(ddlno == 1)
     {
-      // PRE plane, SU Mod = 1, 2
-      Int_t irownew = row%16;
-      Int_t icolnew = col%4;
-      Int_t irowdiv = row/16;
-      Int_t icoldiv = col/4;
-
-      mcmno = 72*um + 6*icoldiv + irowdiv;
-      chno  = ch[irownew][icolnew];
+      fileName += "/PMD/PMD_Mapping_ddl1.dat";
     }
-  else if (ddlno == 2 || ddlno == 3)
+  else if(ddlno == 2)
     {
-      // PRE plane,  SU Mod = 3, 4
-      Int_t irownew = row%16;
-      Int_t icolnew = col%4;
-      Int_t irowdiv = row/16;
-      Int_t icoldiv = col/4;
-      
-      mcmno = 72*um + 3*icoldiv + irowdiv;
-      chno  = ch[irownew][icolnew];
+      fileName += "/PMD/PMD_Mapping_ddl2.dat";
     }
-  else if (ddlno == 4)
+  else if(ddlno == 3)
     {
-      // CPV plane,  SU Mod = 1, 2
-      Int_t irownew = row%16;
-      Int_t icolnew = col%4;
-      Int_t irowdiv = row/16;
-      Int_t icoldiv = col/4;
-
-      mcmno = 72*um + 6*icoldiv + irowdiv;
-      chno  = ch[irownew][icolnew];
+      fileName += "/PMD/PMD_Mapping_ddl3.dat";
     }
-  else if (ddlno == 5)
+  else if(ddlno == 4)
     {
-      // CPV plane,  SU Mod = 3, 4
-      Int_t irownew = row%16;
-      Int_t icolnew = col%4;
-      Int_t irowdiv = row/16;
-      Int_t icoldiv = col/4;
-      
-      mcmno = 72*um + 3*icoldiv + irowdiv;
-      chno  = ch[irownew][icolnew];
+      fileName += "/PMD/PMD_Mapping_ddl4.dat";
+    }
+  else if(ddlno == 5)
+    {
+      fileName += "/PMD/PMD_Mapping_ddl5.dat";
     }
 
-}
-//____________________________________________________________________________
+  ifstream infile;
+  infile.open(fileName.Data(), ios::in); // ascii file
+  if(!infile)
+    AliError(Form("Could not read the mapping file for DDL No = %d",ddlno));
 
-void AliPMDDDLRawData::GetRowCol(Int_t ddlno, UInt_t mcmno, UInt_t chno,
-                                Int_t &um, Int_t &row, Int_t &col)
-{
-  UInt_t ch[64] = { 9, 5, 1, 0, 13, 4, 8, 12,
-                   16, 17, 20, 24, 28, 29, 21, 25,
-                   22, 26, 30, 31, 18, 27, 23, 19,
-                   15, 14, 11, 7, 3, 2, 10, 6,
-                   41, 37, 33, 32, 45, 36, 40, 44,
-                   48, 49, 52, 56, 60, 61, 53, 57,
-                   54, 58, 62, 63, 50, 59, 55, 51,
-                   47, 46, 43, 39, 35, 34, 42, 38 };
-
-  if (ddlno == 1 || ddlno == 2)
+  for (Int_t im = 0; im < modulePerDDL; im++)
     {
-      um  = mcmno/72;
-      Int_t mcmnonew = mcmno - 72*um;
-      Int_t rowcol  = ch[chno];
-      Int_t irownew = rowcol/4;
-      Int_t icolnew = rowcol%4;
-      
-      Int_t remmcm  = mcmnonew%6;
-      Int_t divmcm  = mcmnonew/6;
+      infile >> moduleno;
+      infile >> totPatchBus >> bPatchBus >> ePatchBus;
+
+      if (moduleno == imodule)
+       {
+         beginPatchBus = bPatchBus;
+         endPatchBus   = ePatchBus;
+       }
       
-      row = 16*remmcm + irownew;
-      col =  4*divmcm + icolnew;
+      for(Int_t i=0; i<totPatchBus; i++)
+       {
+         infile >> ibus >> totmcm >> rows >> rowe >> cols >> cole;
+
+         if (moduleno == imodule)
+           {
+             patchBusNo[ibus]   = ibus;
+             mcmperBus[ibus]    = totmcm;
+             startRowBus[ibus]  = rows;
+             startColBus[ibus]  = cols;
+             endRowBus[ibus]    = rowe;
+             endColBus[ibus]    = cole;
+           }
+       }
+
     }
-  else   if (ddlno == 3 || ddlno == 4)
+
+  infile.close();
+
+  treeD->GetEntry(imodule); 
+  Int_t nentries = fDigits->GetLast();
+  Int_t totword = nentries+1;
+
+  AliPMDdigit *pmddigit;
+
+  for (Int_t ient = 0; ient < totword; ient++)
     {
-      um  = mcmno/72;
-      Int_t mcmnonew = mcmno - 72*um;
-      Int_t rowcol  = ch[chno];
-      Int_t irownew = rowcol/4;
-      Int_t icolnew = rowcol%4;
-      
-      Int_t remmcm  = mcmnonew%3;
-      Int_t divmcm  = mcmnonew/3;
+      pmddigit = (AliPMDdigit*)fDigits->UncheckedAt(ient);
       
-      row = 16*remmcm + irownew;
-      col =  4*divmcm + icolnew;
+      det    = pmddigit->GetDetector();
+      smn    = pmddigit->GetSMNumber();
+      irow   = pmddigit->GetRow();
+      icol   = pmddigit->GetColumn();
+      adc    = (UInt_t) pmddigit->GetADC();
+
+      TransformS2H(smn,irow,icol);
+
+      GetMCMCh(ddlno, irow, icol, beginPatchBus, endPatchBus,
+              mcmperBus, startRowBus, startColBus,
+              endRowBus, endColBus, busno, mcmno, chno);
+
+      baseword = 0;
+      AliBitPacking::PackWord(adc,baseword,0,11);
+      AliBitPacking::PackWord(chno,baseword,12,17);
+      AliBitPacking::PackWord(mcmno,baseword,18,28);
+      AliBitPacking::PackWord(0,baseword,29,30);
+      parity = ComputeParity(baseword);      // generate the parity bit
+      AliBitPacking::PackWord(parity,baseword,31,31);
+
+      Int_t jj = contentsBus[busno];
+      busPatch[busno][jj] = baseword;
+
+      contentsBus[busno]++;
     }
-  else if (ddlno == 4)
+
+}
+
+//____________________________________________________________________________
+void AliPMDDDLRawData::TransformS2H(Int_t smn, Int_t &irow, Int_t &icol)
+{
+  // Does the Software to Hardware coordinate transformation
+  //
+
+  Int_t  irownew = 0;
+  Int_t  icolnew = 0;
+
+  // First in digits we have all dimension 48x96
+  // Transform into the realistic one, i.e, For SM 0&1 96(row)x48(col)
+  // and for SM 2&3 48(row)x96(col)
+  // 
+  if(smn < 12)
     {
-      um  = mcmno/144;
-      Int_t mcmnonew = mcmno - 72*um;
-      Int_t rowcol  = ch[chno];
-      Int_t irownew = rowcol/4;
-      Int_t icolnew = rowcol%4;
-      
-      Int_t remmcm  = mcmnonew%6;
-      Int_t divmcm  = mcmnonew/6;
-      
-      row = 16*remmcm + irownew;
-      col =  4*divmcm + icolnew;
+      irownew = icol;
+      icolnew = irow;
     }
-  else if (ddlno == 5)
+  else if( smn >= 12 && smn < 24)
     {
-      um  = mcmno/144;
-      Int_t mcmnonew = mcmno - 72*um;
-      Int_t rowcol  = ch[chno];
-      Int_t irownew = rowcol/4;
-      Int_t icolnew = rowcol%4;
-      
-      Int_t remmcm  = mcmnonew%3;
-      Int_t divmcm  = mcmnonew/3;
-      
-      row = 16*remmcm + irownew;
-      col =  4*divmcm + icolnew;
+      irownew = irow;
+      icolnew = icol;
     }
 
+  // In the new geometry always Geant (0,0) and Hardware (0,0) start
+  // from the top left corner
+
+  irow = irownew;
+  icol = icolnew;
 
 }
+
+
 //____________________________________________________________________________
 
-void AliPMDDDLRawData::PackWord(UInt_t startbit, UInt_t stopbit, 
-                               UInt_t dataword, UInt_t &packedword)
+void AliPMDDDLRawData::GetMCMCh(Int_t ddlno, Int_t row, Int_t col,
+                               Int_t beginPatchBus, Int_t endPatchBus,
+                               Int_t *mcmperBus,
+                               Int_t *startRowBus, Int_t *startColBus,
+                               Int_t *endRowBus, Int_t *endColBus,
+                               Int_t & busno, UInt_t &mcmno, UInt_t &chno)
 {
-  UInt_t bitLength  = stopbit - startbit + 1;
-  UInt_t bitContent = (UInt_t) (TMath::Power(2,bitLength) - 1);
-  if(bitContent < dataword)
-    {
-      cout << " *** ERROR *** bitContent is less than the dataword" << endl;
-      return;
-    }
-  UInt_t packedBits = 0;
-  if (packedword != 0)
-    packedBits = (UInt_t) (TMath::Log(packedword)/TMath::Log(2));
+  // This converts row col to hardware channel number
+  // This is the final version of mapping supplied by Mriganka
+
+  static const UInt_t kCh[16][4] = { {56, 58, 59, 57},
+                                    {54, 62, 61, 53},
+                                    {52, 60, 63, 51},
+                                    {48, 50, 55, 49},
+                                    {46, 40, 45, 47},
+                                    {44, 32, 35, 43},
+                                    {42, 34, 33, 41},
+                                    {38, 36, 37, 39},
+                                    {24, 26, 27, 25},
+                                    {22, 30, 29, 21},
+                                    {20, 28, 31, 19},
+                                    {16, 18, 23, 17},
+                                    {14, 8, 13, 15},
+                                    {12, 0, 3, 11},
+                                    {10, 2, 1, 9},
+                                    {6, 4, 5, 7} };
+  
+  Int_t irownew = row%16;
+  Int_t icolnew = col%4;
+  
+  chno  = kCh[irownew][icolnew];
 
-  UInt_t counter;
-  if (packedBits <= stopbit)
-    {
-      counter   = 31 - stopbit;
-    }
-  else
+
+  for (Int_t ibus = beginPatchBus; ibus <= endPatchBus; ibus++)
     {
-      counter   = 31 - packedBits;
-    }
-  UInt_t dummyword = 0xFFFFFFFF;
-  dummyword >>= counter;
-  UInt_t lword = dataword << startbit;
-  UInt_t nword = lword | packedword;
-  packedword = dummyword & nword;
+      Int_t srow = startRowBus[ibus];
+      Int_t erow = endRowBus[ibus];
+      Int_t scol = startColBus[ibus];
+      Int_t ecol = endColBus[ibus];
+      Int_t tmcm = mcmperBus[ibus];
+      if ((row >= srow && row <= erow) && (col >= scol && col <= ecol))
+       {
+         busno = ibus;
 
+         // Find out the MCM Number
+         //
 
+         if (ddlno == 0 || ddlno == 1)
+           {
+             // PRE plane, SU Mod = 0, 1
+             Int_t rowdiff = endRowBus[ibus] - startRowBus[ibus];
+             if(rowdiff > 16)
+               {
+                 Int_t midrow = srow + 16;
+                 if(row >= srow && row < midrow)
+                   {
+                     mcmno = 12 + (col-scol)/4;
+                   }
+                 else if(row >= midrow && row < erow)
+                   {
+                     mcmno = (col-scol)/4;
+                   }
+               }
+             else
+               {
+                 mcmno = (col-scol)/4;
+               }
+           } // end of ddl 0 and 1
+         else if (ddlno == 2)
+           {
+             // PRE plane,  SU Mod = 2
+             
+             Int_t icolnew = (col - scol)/4;
+             mcmno = tmcm - 1 - icolnew;
+           }
+         else if (ddlno == 3)
+           {
+             // PRE plane,  SU Mod = 3
+             
+             Int_t icolnew = (col - scol)/4;
+             mcmno = tmcm - 1 - icolnew;
+           }
+         else if (ddlno == 4)
+           {
+             // CPV plane,  SU Mod = 0, 3 : ddl = 4
+             
+             if(ibus <= 20)
+               {
+                 Int_t rowdiff = endRowBus[ibus] - startRowBus[ibus];
+                 if(rowdiff > 16)
+                   {
+                     Int_t midrow = srow + 16;
+                     if(row >= srow && row < midrow)
+                       {
+                         mcmno = 12 + (col-scol)/4;
+                       }
+                     else if(row >= midrow && row < erow)
+                       {
+                         mcmno = (col-scol)/4;
+                       }
+                   }
+                 else
+                   {
+                     mcmno = (col-scol)/4;
+                   }
+               }
+             else if (ibus > 20)
+               {
+                 Int_t icolnew = (col - scol)/4;
+                 mcmno = tmcm - 1 - icolnew;
+               }
+           }
+         else if (ddlno == 5)
+           {
+             // CPV plane,  SU Mod = 2, 1 : ddl = 5
+             
+             if(ibus <= 20)
+               {
+                 Int_t rowdiff = endRowBus[ibus] - startRowBus[ibus];
+                 if(rowdiff > 16)
+                   {
+                     Int_t midrow = srow + 16;
+                     if(row >= srow && row < midrow)
+                       {
+                         mcmno = 12 + (col-scol)/4;
+                       }
+                     else if(row >= midrow && row < erow)
+                       {
+                         mcmno = (col-scol)/4;
+                       }
+                   }
+                 else
+                   {
+                     mcmno = (col-scol)/4;
+                   }
+               }
+             else if (ibus > 20)
+               {
+                 Int_t icolnew = (col - scol)/4;
+                 mcmno = tmcm - 1 - icolnew;
+               }
+           }
+       }  
+    }
+  
 }
 //____________________________________________________________________________
-void AliPMDDDLRawData::UnpackWord(UInt_t startbit, UInt_t stopbit, 
-                               UInt_t &dataword, UInt_t packedword)
+
+Int_t AliPMDDDLRawData::ComputeParity(UInt_t baseword)
 {
-  UInt_t bitLength  = stopbit - startbit + 1;
-  UInt_t bitContent = (UInt_t) (TMath::Power(2,bitLength) - 1);
-  bitContent <<= startbit;
-  dataword = packedword & bitContent;
-  dataword >>= startbit;
+  // Generate the parity bit
 
+  Int_t count = 0;
+  for(Int_t j=0; j<29; j++)
+    {
+      if (baseword & 0x01 ) count++;
+      baseword >>= 1;
+    }
+  Int_t parity = count%2;
+  return parity;
 }
+
 //____________________________________________________________________________