]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - TOF/AliTOFDDLRawData.cxx
New Run Range
[u/mrichter/AliRoot.git] / TOF / AliTOFDDLRawData.cxx
index 9883c972e5f159498573f84dc80a2d8ff6d50a33..40fc4583a164be1c58f4911eb72d5e427d8ce14c 100644 (file)
 
 /*
 $Log$
+Revision 1.19  2007/06/22 11:37:47  cvetan
+Fixes in order to write correct raw-data on big-endian platforms (Marco)
+
+Revision 1.18  2007/05/21 13:26:19  decaro
+Correction on matching_window control and bug fixing (R.Preghenella)
+
+Revision 1.17  2007/05/10 09:29:34  hristov
+Last moment fixes and changes from v4-05-Release (Silvia)
+
+Revision 1.16  2007/05/03 09:07:22  decaro
+Double digit in the same TDC channel. Wrong sequence during the raw data writing in unpacked mode: solved
+
+Revision 1.15  2007/04/23 16:51:39  decaro
+Digits-to-raw_data conversion: correction for a more real description (A.De Caro, R.Preghenella)
+
+Revision 1.14  2007/03/28 10:50:33  decaro
+Rounding off problem in rawData coding/decoding: solved
+
+Revision 1.13  2007/02/20 15:57:00  decaro
+Raw data update: to read the TOF raw data defined in UNPACKED mode
+
+Revision 1.12  2006/08/22 13:29:42  arcelli
+removal of effective c++ warnings (C.Zampolli)
+
+Revision 1.11  2006/08/10 14:46:54  decaro
+TOF raw data format: updated version
+
 Revision 1.10.1  2006/06/28 A.De Caro
         Update TOF raw data format
                according to the final version
@@ -59,41 +86,43 @@ Revision 0.01  2004/6/11 A.De Caro, S.B.Sellitto, R.Silvestri
 #include "AliTOFdigit.h"
 #include "AliTOFGeometry.h"
 #include "AliTOFRawStream.h"
+#include "AliFstream.h"
 
 extern TRandom *gRandom;
 
 ClassImp(AliTOFDDLRawData)
 
 //---------------------------------------------------------------------------
-AliTOFDDLRawData::AliTOFDDLRawData()
+AliTOFDDLRawData::AliTOFDDLRawData():
+  fVerbose(0),
+  fIndex(-1),
+  fPackedAcquisition(kTRUE),
+  fFakeOrphaneProduction(kFALSE),
+  fMatchingWindow(8192),
+  fTOFdigitMap(new AliTOFDigitMap()),
+  fTOFdigitArray(0x0),
+  fTOFrawStream(new AliTOFRawStream())
 {
   //Default constructor
-  fIndex=-1;
-  fVerbose=0;
-  fTOFgeometry = 0;
-  fTOFdigitMap = new AliTOFDigitMap();
 }
-
-//----------------------------------------------------------------------------
-AliTOFDDLRawData::AliTOFDDLRawData(AliTOFGeometry *tofGeom)
-{
-  //Constructor
-  fIndex=-1;
-  fVerbose=0;
-  fTOFgeometry = tofGeom;
-  fTOFdigitMap = new AliTOFDigitMap();
-  fTOFdigitArray = 0x0;
-  fTOFrawStream = new AliTOFRawStream();
-
-}
-
 //----------------------------------------------------------------------------
 AliTOFDDLRawData::AliTOFDDLRawData(const AliTOFDDLRawData &source) :
-    TObject(source) {
+  TObject(source),
+  fVerbose(0),
+  fIndex(-1),
+  fPackedAcquisition(kTRUE),
+  fFakeOrphaneProduction(kFALSE),
+  fMatchingWindow(8192),
+  fTOFdigitMap(new AliTOFDigitMap()),
+  fTOFdigitArray(0x0),
+  fTOFrawStream(new AliTOFRawStream())
+ {
   //Copy Constructor
   this->fIndex=source.fIndex;
   this->fVerbose=source.fVerbose;
-  this->fTOFgeometry=source.fTOFgeometry;
+  this->fPackedAcquisition=source.fPackedAcquisition;
+  this->fFakeOrphaneProduction=source.fFakeOrphaneProduction;
+  this->fMatchingWindow=source.fMatchingWindow;
   this->fTOFdigitMap=source.fTOFdigitMap;
   this->fTOFdigitArray=source.fTOFdigitArray;
   this->fTOFrawStream=source.fTOFrawStream;
@@ -105,13 +134,21 @@ AliTOFDDLRawData& AliTOFDDLRawData::operator=(const AliTOFDDLRawData &source) {
   //Assigment operator
   this->fIndex=source.fIndex;
   this->fVerbose=source.fVerbose;
-  this->fTOFgeometry=source.fTOFgeometry;
+  this->fPackedAcquisition=source.fPackedAcquisition;
+  this->fFakeOrphaneProduction=source.fFakeOrphaneProduction;
+  this->fMatchingWindow=source.fMatchingWindow;
   this->fTOFdigitMap=source.fTOFdigitMap;
   this->fTOFdigitArray=source.fTOFdigitArray;
   this->fTOFrawStream=source.fTOFrawStream;
   return *this;
 }
 
+//----------------------------------------------------------------------------
+AliTOFDDLRawData::~AliTOFDDLRawData()
+{
+  delete fTOFdigitMap;
+  delete fTOFrawStream;
+}
 //----------------------------------------------------------------------------
 Int_t AliTOFDDLRawData::RawDataTOF(TBranch* branch)
 {
@@ -128,7 +165,7 @@ Int_t AliTOFDDLRawData::RawDataTOF(TBranch* branch)
   fTOFdigitArray = * (TClonesArray**) branch->GetAddress();
 
   char fileName[15];
-  ofstream outfile;         // logical name of the output file 
+  AliFstream* outfile;         // logical name of the output file 
 
   //AliRawDataHeader header;
   AliRawDataHeaderSim header;
@@ -151,17 +188,13 @@ Int_t AliTOFDDLRawData::RawDataTOF(TBranch* branch)
   for (nDDL=0; nDDL<AliDAQ::NumberOfDdls("TOF"); nDDL++) {
 
     strcpy(fileName,AliDAQ::DdlFileName("TOF",nDDL)); //The name of the output file
-#ifndef __DECCXX
-    outfile.open(fileName,ios::binary);
-#else
-    outfile.open(fileName);
-#endif
 
+    outfile = new AliFstream(fileName);
     iDDL = fTOFrawStream->GetDDLnumberPerSector(nDDL);
 
     // write Dummy DATA HEADER
-    UInt_t dataHeaderPosition = outfile.tellp();
-    outfile.write((char*)(&header),sizeof(header));
+    UInt_t dataHeaderPosition = outfile->Tellp();
+    outfile->WriteBuffer((char*)(&header),sizeof(header));
 
     // DRM section: trailer
     MakeDRMtrailer(buf);
@@ -213,21 +246,21 @@ Int_t AliTOFDDLRawData::RawDataTOF(TBranch* branch)
 
     ReverseArray(buf, fIndex+1);
 
-    outfile.write((char *)buf,((fIndex+1)*sizeof(UInt_t)));
+    outfile->WriteBuffer((char *)buf,((fIndex+1)*sizeof(UInt_t)));
 
     for (jj=0; jj<(fIndex+1); jj++) buf[jj]=0;
     fIndex = -1;
     
     //Write REAL DATA HEADER
-    UInt_t currentFilePosition = outfile.tellp();
+    UInt_t currentFilePosition = outfile->Tellp();
     sizeRawData = currentFilePosition - dataHeaderPosition - sizeof(header);
     header.fSize = currentFilePosition - dataHeaderPosition;
     header.SetAttribute(0);  // valid data
-    outfile.seekp(dataHeaderPosition);
-    outfile.write((char*)(&header),sizeof(header));
-    outfile.seekp(currentFilePosition);
+    outfile->Seekp(dataHeaderPosition);
+    outfile->WriteBuffer((char*)(&header),sizeof(header));
+    outfile->Seekp(currentFilePosition);
 
-    outfile.close();
+    delete outfile;
 
   } //end loop on DDL file number
 
@@ -292,12 +325,35 @@ void AliTOFDDLRawData::MakeDRMheader(Int_t nDDL, UInt_t *buf)
   fIndex++;
   buf[fIndex]=baseWord;
 
+  // DRM status header 4
+  baseWord=0;
+  word = 1; // 0001 -> DRM data are coming from the VME slot number 1
+  AliBitPacking::PackWord(word,baseWord, 0, 3);
+  word = 0; // temperature
+  AliBitPacking::PackWord(word,baseWord, 4,13);
+  word = 0; // zero
+  AliBitPacking::PackWord(word,baseWord, 14,14);
+  word = 0; // ACK
+  AliBitPacking::PackWord(word,baseWord, 15,15);
+  word = 0; // Sens AD
+  AliBitPacking::PackWord(word,baseWord, 16,18);
+  word = 0; // zero
+  AliBitPacking::PackWord(word,baseWord, 19,19);
+  word = 0; // reserved for future use
+  AliBitPacking::PackWord(word,baseWord, 20,27);
+  word = 4; // 0100 -> DRM header ID
+  AliBitPacking::PackWord(word,baseWord,28,31);
+  fIndex++;
+  buf[fIndex]=baseWord;
+
   // DRM status header 3
   baseWord=0;
   word = 1; // 0001 -> DRM data are coming from the VME slot number 1
   AliBitPacking::PackWord(word,baseWord, 0, 3);
-  word = 0; // TTC event counter
-  AliBitPacking::PackWord(word,baseWord, 4,27);
+  word = 0; // L0 BCID
+  AliBitPacking::PackWord(word,baseWord, 4,15);
+  word = 0; // Run Time info
+  AliBitPacking::PackWord(word,baseWord, 16,27);
   word = 4; // 0100 -> DRM header ID
   AliBitPacking::PackWord(word,baseWord,28,31);
   fIndex++;
@@ -321,7 +377,9 @@ void AliTOFDDLRawData::MakeDRMheader(Int_t nDDL, UInt_t *buf)
   word = 0; //
   AliBitPacking::PackWord(word,baseWord,15,15);
   word = 0; // fault ID
-  AliBitPacking::PackWord(word,baseWord,16,27);
+  AliBitPacking::PackWord(word,baseWord,16,26);
+  word = 0; // RTO
+  AliBitPacking::PackWord(word,baseWord,27,27);
   word = 4; // 0100 -> DRM header ID
   AliBitPacking::PackWord(word,baseWord,28,31);
   fIndex++;
@@ -344,8 +402,12 @@ void AliTOFDDLRawData::MakeDRMheader(Int_t nDDL, UInt_t *buf)
       
   word = 1; // LHC clock status: 1/0
   AliBitPacking::PackWord(word,baseWord,15,15);
+  word = 0; // Vers ID
+  AliBitPacking::PackWord(word,baseWord,16,20);
+  word = 0; // DRMH size
+  AliBitPacking::PackWord(word,baseWord,21,24);
   word = 0; // reserved for future use
-  AliBitPacking::PackWord(word,baseWord,16,27);
+  AliBitPacking::PackWord(word,baseWord,25,27);
   word = 4; // 0100 -> DRM header ID
   AliBitPacking::PackWord(word,baseWord,28,31);
   fIndex++;
@@ -655,7 +717,11 @@ void AliTOFDDLRawData::MakeTRMheader(Int_t nTRM, UInt_t *buf)
   AliBitPacking::PackWord(word,baseWord, 0, 3);
   word = 0; // event words
   AliBitPacking::PackWord(word,baseWord, 4,16);
-  word = 0; // ACQuisition mode: [0;3] see document
+
+  if (fPackedAcquisition)
+    word = 0; // ACQuisition mode: [0;3] see document
+  else
+    word = 3; // ACQuisition mode: [0;3] see document
   AliBitPacking::PackWord(word,baseWord,17,18);
   word = 0; // description of a SEU inside LUT tables for INL compensation;
             // the data are unaffected
@@ -823,6 +889,9 @@ void AliTOFDDLRawData::MakeTDCdigits(Int_t nDDL, Int_t nTRM, Int_t iChain,
   // TRM TDC digit
   //
 
+  const Double_t kOneMoreFilledCell = 1./(AliTOFGeometry::NPadXSector()*AliTOFGeometry::NSectors());
+  Double_t percentFilledCells = Double_t(fTOFdigitMap->GetFilledCellNumber())/(AliTOFGeometry::NPadXSector()*AliTOFGeometry::NSectors());
+
   if (nDDL<0 || nDDL>71) {
     AliWarning(Form(" DDL number is out of the right range [0;71] (nDDL = %3i", nDDL));
     return;
@@ -838,6 +907,10 @@ void AliTOFDDLRawData::MakeTDCdigits(Int_t nDDL, Int_t nTRM, Int_t iChain,
     return;
   }
   
+  Int_t psArray[1000];
+  UInt_t localBuffer[1000];
+  Int_t localIndex = -1;
+
   Int_t iDDL = nDDL%AliTOFGeometry::NDDL();
 
   Int_t volume[5] = {-1, -1, -1, -1, -1};
@@ -846,6 +919,9 @@ void AliTOFDDLRawData::MakeTDCdigits(Int_t nDDL, Int_t nTRM, Int_t iChain,
   Int_t totCharge = -1;
   Int_t timeOfFlight = -1;
 
+  Int_t trailingSpurious = -1;
+  Int_t leadingSpurious = -1;
+
   AliTOFdigit *digs;
 
   UInt_t baseWord=0;
@@ -855,6 +931,11 @@ void AliTOFDDLRawData::MakeTDCdigits(Int_t nDDL, Int_t nTRM, Int_t iChain,
   Int_t nTDC = -1;
   Int_t iCH = -1;
 
+  //Int_t numberOfMeasuresPerChannel = 0;
+  //Int_t maxMeasuresPerChannelInTDC = 0;
+
+  Bool_t outOut = HeadOrTail();
+
   ofstream ftxt;
 
   if (fVerbose==2) ftxt.open("TOFdigits.txt",ios::app);
@@ -871,21 +952,93 @@ void AliTOFDDLRawData::MakeTDCdigits(Int_t nDDL, Int_t nTRM, Int_t iChain,
     // loop on TDC channel number
     for (iCH=AliTOFGeometry::NCh()-1; iCH>=0; iCH--) {
 
+      //numberOfMeasuresPerChannel = 0;
+
       fTOFrawStream->EquipmentId2VolumeId(nDDL, nTRM, iChain, nTDC, iCH, volume);
        
       if (volume[0]==-1 || volume[1]==-1 || volume[2]==-1 ||
          volume[3]==-1 || volume[4]==-1) continue;
-      //AliInfo(Form(" sector = %2i plate = %1i strip = %2i
-      //padX = %2i padZ = %1i", volume[0], volume[1], volume[2],
-      //volume[3], volume[4]));
 
       for (jj=0; jj<3; jj++) indexDigit[jj] = -1;
 
       fTOFdigitMap->GetDigitIndex(volume, indexDigit);
 
+      if (indexDigit[0]<0) {
+
+       trailingSpurious = Int_t(2097152*gRandom->Rndm());
+       leadingSpurious = Int_t(2097152*gRandom->Rndm());
+
+       if ( fFakeOrphaneProduction &&
+            ( ( fPackedAcquisition && percentFilledCells<0.12 && gRandom->Rndm()<(0.12-percentFilledCells) ) ||
+              (!fPackedAcquisition && percentFilledCells<0.24 && gRandom->Rndm()<(0.24-percentFilledCells) )  )  ) {
+
+         percentFilledCells+=kOneMoreFilledCell;
+
+         Int_t dummyPS = 0;
+
+         if (outOut) {
+           word = trailingSpurious; // trailing edge measurement
+           dummyPS = 2;
+         }
+         else {
+           word = leadingSpurious; // leading edge measurement
+           dummyPS = 1;
+         }
+
+         if (fVerbose==2) {
+           if (nDDL<10) ftxt << "  " << nDDL;
+           else         ftxt << " " << nDDL;
+           if (nTRM<10) ftxt << "  " << nTRM;
+           else         ftxt << " " << nTRM;
+           ftxt << "  " << iChain;
+           if (nTDC<10) ftxt << "  " << nTDC;
+           else         ftxt << " " << nTDC;
+           ftxt << "  " << iCH;
+           if (volume[0]<10) ftxt  << "  ->  " << volume[0];
+           else              ftxt  << "  -> " << volume[0];
+           ftxt << "  " << volume[1];
+           if (volume[2]<10) ftxt << "  " << volume[2];
+           else              ftxt << " " << volume[2];
+           ftxt << "  " << volume[4];
+           if (volume[3]<10) ftxt << "  " << volume[3];
+           else              ftxt << " " << volume[3];
+           ftxt << "   " << -1;
+           if (word<10)                           ftxt << "        " << word;
+           else if (word>=10     && word<100)     ftxt << "       " << word;
+           else if (word>=100    && word<1000)    ftxt << "      " << word;
+           else if (word>=1000   && word<10000)   ftxt << "     " << word;
+           else if (word>=10000  && word<100000)  ftxt << "    " << word;
+           else if (word>=100000 && word<1000000) ftxt << "   " << word;
+           else                                   ftxt << "  " << word;
+           ftxt << "   " << dummyPS << endl;
+         }
+
+         AliBitPacking::PackWord(word,baseWord, 0,20);
+         word = iCH; // TDC channel ID [0;7]
+         AliBitPacking::PackWord(word,baseWord,21,23);
+         word = nTDC; // TDC ID [0;14]
+         AliBitPacking::PackWord(word,baseWord,24,27);
+         word = 0; // error flag
+         AliBitPacking::PackWord(word,baseWord,28,28);
+         word = dummyPS; // Packing Status [0;3]
+         AliBitPacking::PackWord(word,baseWord,29,30);
+         word = 1; // TRM TDC digit ID
+         AliBitPacking::PackWord(word,baseWord,31,31);
+
+         localIndex++;
+         localBuffer[localIndex]=baseWord;
+         psArray[localIndex]=dummyPS;
+
+         nWordsPerTRM++;
+         baseWord=0;
+
+       } // if ( fFakeOrphaneProduction && ( ( fPackedAcquisition && percentFilledCells<0.12 && gRandom->Rndm()<(0.12-percentFilledCells) ) or ... ) )
+      } // if (indexDigit[0]<0)
+
       for (jj=0; jj<3;jj++) {
 
        if (indexDigit[jj]<0) continue;
+
        digs = (AliTOFdigit*)fTOFdigitArray->UncheckedAt(indexDigit[jj]);
          
        if (digs->GetSector()!=volume[0] ||
@@ -894,11 +1047,18 @@ void AliTOFDDLRawData::MakeTDCdigits(Int_t nDDL, Int_t nTRM, Int_t iChain,
            digs->GetPadx()  !=volume[3] ||
            digs->GetPadz()  !=volume[4]) AliWarning(" --- ERROR --- ");
 
-       timeOfFlight = (Int_t)(digs->GetTdc())%8192;
-       totCharge = (Int_t)(digs->GetToT());//digs->GetAdc();
+       timeOfFlight = (Int_t)(digs->GetTdc());
+
+       if (timeOfFlight>=fMatchingWindow) continue;
+
+       //numberOfMeasuresPerChannel++;
+
+       // totCharge = (Int_t)digs->GetAdc(); //Use realistic ToT, for Standard production with no miscalibration/Slewing it == fAdC in digit (see AliTOFDigitizer)
+       totCharge = (Int_t)(digs->GetToT());
        // temporary control
-       if (totCharge<0) totCharge = TMath::Abs(totCharge);
-       if (totCharge>=256) totCharge = 255;
+       if (totCharge<0) totCharge = 0;//TMath::Abs(totCharge);
+
+       if (fPackedAcquisition) {
 
        if (fVerbose==2) {
          if (nDDL<10) ftxt << "  " << nDDL;
@@ -917,18 +1077,19 @@ void AliTOFDDLRawData::MakeTDCdigits(Int_t nDDL, Int_t nTRM, Int_t iChain,
          ftxt << "  " << volume[4];
          if (volume[3]<10) ftxt << "  " << volume[3];
          else              ftxt << " " << volume[3];
-         if (totCharge<10)                        ftxt << "        " << totCharge;
-         else if (totCharge>=10 && totCharge<100) ftxt << "       " << totCharge;
-         else                                     ftxt << "      " << totCharge;
-         if (timeOfFlight<10)                             ftxt << "      " << timeOfFlight << endl;
-         else if (timeOfFlight>=10 && timeOfFlight<100)   ftxt << "     " << timeOfFlight << endl;
-         else if (timeOfFlight>=100 && timeOfFlight<1000) ftxt << "    " << timeOfFlight << endl;
-         else                                             ftxt << "   " << timeOfFlight << endl;
+         if (totCharge<10)                        ftxt << "    " << totCharge;
+         else if (totCharge>=10 && totCharge<100) ftxt << "   " << totCharge;
+         else                                     ftxt << "  " << totCharge;
+         if (timeOfFlight<10)                             ftxt << "     " << timeOfFlight << endl;
+         else if (timeOfFlight>=10  && timeOfFlight<100)  ftxt << "    " << timeOfFlight << endl;
+         else if (timeOfFlight>=100 && timeOfFlight<1000) ftxt << "   " << timeOfFlight << endl;
+         else                                             ftxt << "  " << timeOfFlight << endl;
        }
 
-       word = timeOfFlight; // time-of-fligth measurement
+       word = timeOfFlight%8192; // time-of-fligth measurement
        AliBitPacking::PackWord(word,baseWord, 0,12);
 
+       if (totCharge>=256) totCharge = 255;
        word = totCharge; // time-over-threshould measurement
        AliBitPacking::PackWord(word,baseWord,13,20);
 
@@ -938,20 +1099,373 @@ void AliTOFDDLRawData::MakeTDCdigits(Int_t nDDL, Int_t nTRM, Int_t iChain,
        AliBitPacking::PackWord(word,baseWord,24,27);
        word = 0; // error flag
        AliBitPacking::PackWord(word,baseWord,28,28);
-       word = 0; // Packing Status [0;5]
+       word = 0; // Packing Status [0;3]
        AliBitPacking::PackWord(word,baseWord,29,30);
        word = 1; // TRM TDC digit ID
        AliBitPacking::PackWord(word,baseWord,31,31);
-       fIndex++;
-       buf[fIndex]=baseWord;
+
+       localIndex++;
+       localBuffer[localIndex]=baseWord;
+
+       nWordsPerTRM++;
+       baseWord=0;
+
+       if ( fFakeOrphaneProduction &&
+            percentFilledCells<0.12 && gRandom->Rndm()<(0.12-percentFilledCells) ) {
+
+         percentFilledCells+=kOneMoreFilledCell;
+
+         trailingSpurious = Int_t(2097152*gRandom->Rndm());
+         leadingSpurious = Int_t(2097152*gRandom->Rndm());
+
+         Int_t dummyPS = 0;
+
+         if (outOut) {
+           word = trailingSpurious; // trailing edge measurement
+           dummyPS = 2;
+         }
+         else {
+           word = leadingSpurious; // leading edge measurement
+           dummyPS = 1;
+         }
+
+         if (fVerbose==2) {
+           if (nDDL<10) ftxt << "  " << nDDL;
+           else         ftxt << " " << nDDL;
+           if (nTRM<10) ftxt << "  " << nTRM;
+           else         ftxt << " " << nTRM;
+           ftxt << "  " << iChain;
+           if (nTDC<10) ftxt << "  " << nTDC;
+           else         ftxt << " " << nTDC;
+           ftxt << "  " << iCH;
+           if (volume[0]<10) ftxt  << "  ->  " << volume[0];
+           else              ftxt  << "  -> " << volume[0];
+           ftxt << "  " << volume[1];
+           if (volume[2]<10) ftxt << "  " << volume[2];
+           else              ftxt << " " << volume[2];
+           ftxt << "  " << volume[4];
+           if (volume[3]<10) ftxt << "  " << volume[3];
+           else              ftxt << " " << volume[3];
+           ftxt << "   " << -1;
+           if (word<10)                           ftxt << "        " << word;
+           else if (word>=10     && word<100)     ftxt << "       " << word;
+           else if (word>=100    && word<1000)    ftxt << "      " << word;
+           else if (word>=1000   && word<10000)   ftxt << "     " << word;
+           else if (word>=10000  && word<100000)  ftxt << "    " << word;
+           else if (word>=100000 && word<1000000) ftxt << "   " << word;
+           else                                   ftxt << "  " << word;
+           ftxt << "   " << dummyPS << endl;
+         }
+
+         AliBitPacking::PackWord(word,baseWord, 0,20);
+         word = iCH; // TDC channel ID [0;7]
+         AliBitPacking::PackWord(word,baseWord,21,23);
+         word = nTDC; // TDC ID [0;14]
+         AliBitPacking::PackWord(word,baseWord,24,27);
+         word = 0; // error flag
+         AliBitPacking::PackWord(word,baseWord,28,28);
+         word = dummyPS; // Packing Status [0;3]
+         AliBitPacking::PackWord(word,baseWord,29,30);
+         word = 1; // TRM TDC digit ID
+         AliBitPacking::PackWord(word,baseWord,31,31);
+
+         localIndex++;
+         localBuffer[localIndex]=baseWord;
+         psArray[localIndex]=dummyPS;
+
+         nWordsPerTRM++;
+         baseWord=0;
+
+       } // if ( fFakeOrphaneProduction && percentFilledCells<0.12 && gRandom->Rndm()<(0.12-percentFilledCells) )
+
+
+       } // if (fPackedAcquisition)
+       else { // if (!fPackedAcquisition)
+
+       if ( fFakeOrphaneProduction &&
+            percentFilledCells<0.24 && gRandom->Rndm()<(0.24-percentFilledCells) && outOut ) {
+
+         percentFilledCells+=kOneMoreFilledCell;
+
+         trailingSpurious = Int_t(2097152*gRandom->Rndm());
+         word = trailingSpurious;
+         Int_t dummyPS = 2;
+
+         if (fVerbose==2) {
+           if (nDDL<10) ftxt << "  " << nDDL;
+           else         ftxt << " " << nDDL;
+           if (nTRM<10) ftxt << "  " << nTRM;
+           else         ftxt << " " << nTRM;
+           ftxt << "  " << iChain;
+           if (nTDC<10) ftxt << "  " << nTDC;
+           else         ftxt << " " << nTDC;
+           ftxt << "  " << iCH;
+           if (volume[0]<10) ftxt  << "  ->  " << volume[0];
+           else              ftxt  << "  -> " << volume[0];
+           ftxt << "  " << volume[1];
+           if (volume[2]<10) ftxt << "  " << volume[2];
+           else              ftxt << " " << volume[2];
+           ftxt << "  " << volume[4];
+           if (volume[3]<10) ftxt << "  " << volume[3];
+           else              ftxt << " " << volume[3];
+           ftxt << "   " << -1;
+           if (word<10)                           ftxt << "        " << word;
+           else if (word>=10     && word<100)     ftxt << "       " << word;
+           else if (word>=100    && word<1000)    ftxt << "      " << word;
+           else if (word>=1000   && word<10000)   ftxt << "     " << word;
+           else if (word>=10000  && word<100000)  ftxt << "    " << word;
+           else if (word>=100000 && word<1000000) ftxt << "   " << word;
+           else                                   ftxt << "  " << word;
+           ftxt << "   " << dummyPS << endl;
+         }
+
+         AliBitPacking::PackWord(word,baseWord, 0,20);
+         word = iCH; // TDC channel ID [0;7]
+         AliBitPacking::PackWord(word,baseWord,21,23);
+         word = nTDC; // TDC ID [0;14]
+         AliBitPacking::PackWord(word,baseWord,24,27);
+         word = 0; // error flag
+         AliBitPacking::PackWord(word,baseWord,28,28);
+         word = dummyPS; // Packing Status [0;3]
+         AliBitPacking::PackWord(word,baseWord,29,30);
+         word = 1; // TRM TDC digit ID
+         AliBitPacking::PackWord(word,baseWord,31,31);
+
+         localIndex++;
+         localBuffer[localIndex]=baseWord;
+         psArray[localIndex]=dummyPS;
+
+         nWordsPerTRM++;
+         baseWord=0;
+
+       } // if ( fFakeOrphaneProduction && percentFilledCells<0.24 && gRandom->Rndm()<(0.24-percentFilledCells)  && outOut )
+
+
+       word = (timeOfFlight + Int_t(totCharge*AliTOFGeometry::ToTBinWidth()/AliTOFGeometry::TdcBinWidth()))%2097152; // trailing edge measurement
+
+       if (fVerbose==2) {
+         if (nDDL<10) ftxt << "  " << nDDL;
+         else         ftxt << " " << nDDL;
+         if (nTRM<10) ftxt << "  " << nTRM;
+         else         ftxt << " " << nTRM;
+         ftxt << "  " << iChain;
+         if (nTDC<10) ftxt << "  " << nTDC;
+         else         ftxt << " " << nTDC;
+         ftxt << "  " << iCH;
+         if (volume[0]<10) ftxt  << "  ->  " << volume[0];
+         else              ftxt  << "  -> " << volume[0];
+         ftxt << "  " << volume[1];
+         if (volume[2]<10) ftxt << "  " << volume[2];
+         else              ftxt << " " << volume[2];
+         ftxt << "  " << volume[4];
+         if (volume[3]<10) ftxt << "  " << volume[3];
+         else              ftxt << " " << volume[3];
+         ftxt << "   " << -1;
+         if (word<10)                           ftxt << "        " << word;
+         else if (word>=10     && word<100)     ftxt << "       " << word;
+         else if (word>=100    && word<1000)    ftxt << "      " << word;
+         else if (word>=1000   && word<10000)   ftxt << "     " << word;
+         else if (word>=10000  && word<100000)  ftxt << "    " << word;
+         else if (word>=100000 && word<1000000) ftxt << "   " << word;
+         else                                   ftxt << "  " << word;
+         ftxt << "   " << 2 << endl;
+       }
+
+       AliBitPacking::PackWord(word,baseWord, 0,20);
+
+       word = iCH; // TDC channel ID [0;7]
+       AliBitPacking::PackWord(word,baseWord,21,23);
+       word = nTDC; // TDC ID [0;14]
+       AliBitPacking::PackWord(word,baseWord,24,27);
+       word = 0; // error flag
+       AliBitPacking::PackWord(word,baseWord,28,28);
+       word = 2; // Packing Status [0;3]
+       AliBitPacking::PackWord(word,baseWord,29,30);
+       word = 1; // TRM TDC digit ID
+       AliBitPacking::PackWord(word,baseWord,31,31);
+
+       localIndex++;
+       localBuffer[localIndex]=baseWord;
+       psArray[localIndex]=2;
 
        nWordsPerTRM++;
        baseWord=0;
 
+       word = timeOfFlight%2097152; // leading edge measurement
+
+       if (fVerbose==2) {
+         if (nDDL<10) ftxt << "  " << nDDL;
+         else         ftxt << " " << nDDL;
+         if (nTRM<10) ftxt << "  " << nTRM;
+         else         ftxt << " " << nTRM;
+         ftxt << "  " << iChain;
+         if (nTDC<10) ftxt << "  " << nTDC;
+         else         ftxt << " " << nTDC;
+         ftxt << "  " << iCH;
+         if (volume[0]<10) ftxt  << "  ->  " << volume[0];
+         else              ftxt  << "  -> " << volume[0];
+         ftxt << "  " << volume[1];
+         if (volume[2]<10) ftxt << "  " << volume[2];
+         else              ftxt << " " << volume[2];
+         ftxt << "  " << volume[4];
+         if (volume[3]<10) ftxt << "  " << volume[3];
+         else              ftxt << " " << volume[3];
+         ftxt << "   " << -1;
+         if (word<10)                           ftxt << "        " << word;
+         else if (word>=10     && word<100)     ftxt << "       " << word;
+         else if (word>=100    && word<1000)    ftxt << "      " << word;
+         else if (word>=1000   && word<10000)   ftxt << "     " << word;
+         else if (word>=10000  && word<100000)  ftxt << "    " << word;
+         else if (word>=100000 && word<1000000) ftxt << "   " << word;
+         else                                   ftxt << "  " << word;
+         ftxt << "   " << 1 << endl;
+       }
+
+       AliBitPacking::PackWord(word,baseWord, 0,20);
+
+       word = iCH; // TDC channel ID [0;7]
+       AliBitPacking::PackWord(word,baseWord,21,23);
+       word = nTDC; // TDC ID [0;14]
+       AliBitPacking::PackWord(word,baseWord,24,27);
+       word = 0; // error flag
+       AliBitPacking::PackWord(word,baseWord,28,28);
+       word = 1; // Packing Status [0;3]
+       AliBitPacking::PackWord(word,baseWord,29,30);
+       word = 1; // TRM TDC digit ID
+       AliBitPacking::PackWord(word,baseWord,31,31);
+
+       localIndex++;
+       localBuffer[localIndex]=baseWord;
+       psArray[localIndex]=1;
+
+       nWordsPerTRM++;
+       baseWord=0;
+
+
+       if ( fFakeOrphaneProduction &&
+            percentFilledCells<0.24 && gRandom->Rndm()<(0.24-percentFilledCells) && !outOut ) {
+
+         percentFilledCells+=kOneMoreFilledCell;
+
+         leadingSpurious = Int_t(2097152*gRandom->Rndm());
+         word = leadingSpurious;
+         Int_t dummyPS = 1;
+
+         if (fVerbose==2) {
+           if (nDDL<10) ftxt << "  " << nDDL;
+           else         ftxt << " " << nDDL;
+           if (nTRM<10) ftxt << "  " << nTRM;
+           else         ftxt << " " << nTRM;
+           ftxt << "  " << iChain;
+           if (nTDC<10) ftxt << "  " << nTDC;
+           else         ftxt << " " << nTDC;
+           ftxt << "  " << iCH;
+           if (volume[0]<10) ftxt  << "  ->  " << volume[0];
+           else              ftxt  << "  -> " << volume[0];
+           ftxt << "  " << volume[1];
+           if (volume[2]<10) ftxt << "  " << volume[2];
+           else              ftxt << " " << volume[2];
+           ftxt << "  " << volume[4];
+           if (volume[3]<10) ftxt << "  " << volume[3];
+           else              ftxt << " " << volume[3];
+           ftxt << "   " << -1;
+           if (word<10)                           ftxt << "        " << word;
+           else if (word>=10     && word<100)     ftxt << "       " << word;
+           else if (word>=100    && word<1000)    ftxt << "      " << word;
+           else if (word>=1000   && word<10000)   ftxt << "     " << word;
+           else if (word>=10000  && word<100000)  ftxt << "    " << word;
+           else if (word>=100000 && word<1000000) ftxt << "   " << word;
+           else                                   ftxt << "  " << word;
+           ftxt << "   " << dummyPS << endl;
+         }
+
+         AliBitPacking::PackWord(word,baseWord, 0,20);
+         word = iCH; // TDC channel ID [0;7]
+         AliBitPacking::PackWord(word,baseWord,21,23);
+         word = nTDC; // TDC ID [0;14]
+         AliBitPacking::PackWord(word,baseWord,24,27);
+         word = 0; // error flag
+         AliBitPacking::PackWord(word,baseWord,28,28);
+         word = dummyPS; // Packing Status [0;3]
+         AliBitPacking::PackWord(word,baseWord,29,30);
+         word = 1; // TRM TDC digit ID
+         AliBitPacking::PackWord(word,baseWord,31,31);
+
+         localIndex++;
+         localBuffer[localIndex]=baseWord;
+         psArray[localIndex]=dummyPS;
+
+         nWordsPerTRM++;
+         baseWord=0;
+
+       } // if ( fFakeOrphaneProduction && percentFilledCells<0.24 && gRandom->Rndm()<(0.24-percentFilledCells) && !outOut )
+
+
+       } // if (!fPackedAcquisition)
+
       } //end loop on digits in the same volume
 
+      //if (numberOfMeasuresPerChannel>maxMeasuresPerChannelInTDC)
+      //maxMeasuresPerChannelInTDC = numberOfMeasuresPerChannel;
+
     } // end loop on TDC channel number
 
+    //AliInfo(Form(" TDC number %2i:  numberOfMeasuresPerChannel = %2i  ---  maxMeasuresPerChannelInTDC = %2i ", nTDC, numberOfMeasuresPerChannel, maxMeasuresPerChannelInTDC));
+
+    if (localIndex==-1) continue;
+
+    if (fPackedAcquisition) {
+
+      for (Int_t jj=0; jj<=localIndex; jj++) {
+       fIndex++;
+       buf[fIndex] = localBuffer[jj];
+       localBuffer[jj] = 0;
+       psArray[jj] = -1;
+      }
+
+    }
+    else {
+      /*
+      if (maxMeasuresPerChannelInTDC = 1) {
+
+       for (Int_t jj=0; jj<=localIndex; jj++) {
+         if (psArray[jj]==2) {
+           fIndex++;
+           buf[fIndex] = localBuffer[jj];
+           localBuffer[jj] = 0;
+           psArray[jj] = -1;
+         }
+       }
+       for (Int_t jj=0; jj<=localIndex; jj++) {
+         if (psArray[jj]==1) {
+           fIndex++;
+           buf[fIndex] = localBuffer[jj];
+           localBuffer[jj] = 0;
+           psArray[jj] = -1;
+         }
+       }
+
+      } // if (maxMeasuresPerChannelInTDC = 1)
+      else if (maxMeasuresPerChannelInTDC>1) {
+
+       AliInfo(Form(" In the TOF DDL %2i, TRM %2i, TDC %2i, chain %1i, the maximum number of t.o.f. good measurements per channel is %2i",
+                    nDDL, nTRM, iChain, nTDC, iCH, maxMeasuresPerChannelInTDC));
+      */
+       for (Int_t jj=0; jj<=localIndex; jj++) {
+           fIndex++;
+           buf[fIndex] = localBuffer[jj];
+           localBuffer[jj] = 0;
+           psArray[jj] = -1;
+       }
+
+       //} // else if (maxMeasuresPerChannelInTDC>1)
+
+    } // else (!fPackedAcquisition)
+
+    localIndex = -1;
+
+    //maxMeasuresPerChannelInTDC = 0;
+
   } // end loop on TDC number
 
 
@@ -977,3 +1491,17 @@ void AliTOFDDLRawData::ReverseArray(UInt_t a[], Int_t n) const
   return;
 
 }
+
+//----------------------------------------------------------------------------
+Bool_t AliTOFDDLRawData::HeadOrTail() const
+{
+  //
+  // Returns the result of a 'pitch and toss'
+  //
+
+  Double_t dummy = gRandom->Rndm();
+
+  if (dummy<0.5) return kFALSE;
+  else return kTRUE;
+
+}