]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - MUON/AliMUONRawWriter.cxx
- Adding the array of slat segmentation and GetLayerSegmentation(..) method
[u/mrichter/AliRoot.git] / MUON / AliMUONRawWriter.cxx
index f9435e1900fa6fe65fa6b586544fc11dbee24ac4..89a68dd797eee0366ca6bb62d639f3e0115becf6 100644 (file)
  * provided "as is" without express or implied warranty.                  *
  **************************************************************************/
 
-////////////////////////////////////
-//
-// MUON Raw Data generaton in ALICE-MUON
-// This class version 3 (further details could be found in Alice-note)
-//
-// Implemented non-constant buspatch numbers for tracking
-// with correct DDL id (first guess)
-// (Ch. Finck, dec 2005)
-//
-// Digits2Raw:
-// Generates raw data for MUON tracker and finally for trigger
-// Using real mapping (inverse) for tracker
-// For trigger there is no mapping (mapping could be found in AliMUONTriggerCircuit)
-// Ch. Finck july 04
-// Use memcpy instead of assignment elt by elt
-// Introducing variable DSP numbers, real manu numbers per buspatch for st12
-// Implemented scaler event for Trigger
-// Ch. Finck , Jan. 06
-// 
-////////////////////////////////////
+/* $Id$ */
+
+/// \class AliMUONRawWriter
+/// MUON Raw Data generaton in ALICE-MUON
+/// This class version 3 (further details could be found in Alice-note)
+///
+/// Implemented non-constant buspatch numbers for tracking
+/// with correct DDL id (first guess)
+/// (Ch. Finck, dec 2005)
+///
+/// Digits2Raw:
+/// Generates raw data for MUON tracker and finally for trigger
+/// Using real mapping (inverse) for tracker
+/// For trigger there is no mapping (mapping could be found in AliMUONTriggerCircuit)
+/// Ch. Finck, July 04
+/// Use memcpy instead of assignment elt by elt
+/// Introducing variable DSP numbers, real manu numbers per buspatch for st12
+/// Implemented scaler event for Trigger
+/// Ch. Finck, Jan. 06
+/// Using bus itr in DDL instead of simple incrementation
+/// treat correctly the DDL & buspatch for station 3.
+/// Using informations from AliMUONTriggerCrateStore for 
+/// empty slots and non-notified cards in trigger crates.
+/// Ch. Finck, August 06.
 
 #include "AliMUONRawWriter.h"
 
-#include "AliBitPacking.h" 
-#include "AliLoader.h"
-#include "AliLog.h"
 #include "AliMUON.h"
+#include "AliMUONBlockHeader.h"
+#include "AliMUONBusStruct.h"
 #include "AliMUONConstants.h"
-#include "AliMUONDDLTracker.h"
-#include "AliMUONDDLTrigger.h"
+#include "AliMUONDarcHeader.h"
 #include "AliMUONData.h"
 #include "AliMUONDigit.h"
-#include "AliMUONGeometryModule.h"
-#include "AliMUONGeometrySegmentation.h"
-#include "AliMUONGeometryStore.h"
+#include "AliMUONDspHeader.h"
 #include "AliMUONGlobalTrigger.h"
+#include "AliMUONLocalStruct.h"
 #include "AliMUONLocalTrigger.h"
-#include "AliMUONScalerEventTrigger.h"
-#include "AliMUONSubEventTrigger.h"
+#include "AliMUONLocalTriggerBoard.h"
+#include "AliMUONRegHeader.h"
+#include "AliMUONTriggerCrate.h"
+#include "AliMUONTriggerCrateStore.h"
+
 #include "AliMpBusPatch.h"
 #include "AliMpDEManager.h"
+#include "AliMpExMap.h"
+#include "AliMpIntPair.h"
+#include "AliMpConstants.h"
 #include "AliMpPad.h"
 #include "AliMpPlaneType.h"
-#include "AliMpSegFactory.h"
+#include "AliMpSegmentation.h"
 #include "AliMpStationType.h"
 #include "AliMpVSegmentation.h"
-#include "AliRun.h"
-#include "TClonesArray.h"
+
+#include "AliRawReader.h"
+#include "AliBitPacking.h" 
+#include "AliDAQ.h"
+#include "AliLog.h"
+
+#include "TList.h"
+#include "TObjArray.h"
+#include "TStopwatch.h"
+
+/// \cond CLASSIMP
 ClassImp(AliMUONRawWriter) // Class implementation in ROOT context
+/// \endcond
 
 Int_t AliMUONRawWriter::fgManuPerBusSwp1B[12]  = {1, 27, 53, 79, 105, 131, 157, 183, 201, 214, 224, 232};
 Int_t AliMUONRawWriter::fgManuPerBusSwp1NB[12] = {1, 27, 53, 79, 105, 131, 157, 183, 201, 214, 225, 233};
@@ -69,35 +86,49 @@ Int_t AliMUONRawWriter::fgManuPerBusSwp1NB[12] = {1, 27, 53, 79, 105, 131, 157,
 Int_t AliMUONRawWriter::fgManuPerBusSwp2B[12]  = {1, 27, 53, 79, 105, 131, 157, 183, 201, 214, 226, 246};
 Int_t AliMUONRawWriter::fgManuPerBusSwp2NB[12] = {1, 27, 53, 79, 105, 131, 157, 183, 201, 214, 227, 245};
 
+namespace 
+{
+  enum ETimer { kWriteTracker, kWriteTrigger, kDigitLoop, kGetBusPatch, kTest, kLast };
+}
 
 //__________________________________________________________________________
 AliMUONRawWriter::AliMUONRawWriter(AliMUONData* data)
-: TObject(),
-  fScalerEvent(kFALSE)
+  : TObject(),
+    fMUONData(data),
+    fBlockHeader(new AliMUONBlockHeader()),
+    fDspHeader(new AliMUONDspHeader()),
+    fDarcHeader(new AliMUONDarcHeader()),
+    fRegHeader(new AliMUONRegHeader()),
+    fLocalStruct(new AliMUONLocalStruct()),
+    fBusPatchManager(new AliMpBusPatch()),
+    fCrateManager(new AliMUONTriggerCrateStore()),
+    fScalerEvent(kFALSE),
+    fHeader(),
+    fTimers(new TStopwatch[kLast])
+
 {
-  // Standard Constructor
-  AliDebug(1,"Standard ctor");
-      
-  // initialize container
-  fMUONData  = data;
+  /// Standard Constructor
 
-  // initialize array
-  fSubEventArray = new TClonesArray("AliMUONSubEventTracker",1000);
-  fSubEventArray->SetOwner(kTRUE);
+  AliDebug(1,"Standard ctor");
+  fFile[0] = fFile[1] = 0x0;  
+  fFile[2] = fFile[3] = 0x0;  
 
-  // ddl pointer
-  fDDLTracker = new AliMUONDDLTracker();
-  fDDLTrigger = new AliMUONDDLTrigger();
+  // setting data key to default value (only for writting)
+  fBlockHeader->SetDataKey(fBlockHeader->GetDefaultDataKey());
+  fDspHeader->SetDataKey(fDspHeader->GetDefaultDataKey());
 
-  fBusPatchManager = new AliMpBusPatch();
+  // bus patch managers
   fBusPatchManager->ReadBusPatchFile();
 
-  fSegFactory = new AliMpSegFactory();
+  // Crate manager
+  fCrateManager->ReadFromFile();
 
-  fTrackerTimer.Start(kTRUE); fTrackerTimer.Stop();
-  fTriggerTimer.Start(kTRUE); fTriggerTimer.Stop();
-  fMappingTimer.Start(kTRUE); fMappingTimer.Stop();
+  // timers
+  for ( Int_t i = 0; i < kLast; ++i )
+  {
+    fTimers[i].Start(kTRUE); 
+    fTimers[i].Stop();
+  }
   
 }
 
@@ -105,72 +136,94 @@ AliMUONRawWriter::AliMUONRawWriter(AliMUONData* data)
 AliMUONRawWriter::AliMUONRawWriter()
   : TObject(),
     fMUONData(0),
-    fDDLTracker(0),
-    fDDLTrigger(0),
+    fBlockHeader(0),
+    fDspHeader(0),
+    fDarcHeader(0),
+    fRegHeader(0),
+    fLocalStruct(0),
     fBusPatchManager(0),
+    fCrateManager(0x0),
     fScalerEvent(kFALSE),
-    fSegFactory(0x0)
+    fHeader(),
+    fTimers(0)
 {
-  // Default Constructor
+  /// Default Constructor
+
   AliDebug(1,"Default ctor");   
   fFile[0] = fFile[1] = 0x0;  
-  fTrackerTimer.Start(kTRUE); fTrackerTimer.Stop();
-  fTriggerTimer.Start(kTRUE); fTriggerTimer.Stop();
-  fMappingTimer.Start(kTRUE); fMappingTimer.Stop();
-}
-
-//_______________________________________________________________________
-AliMUONRawWriter::AliMUONRawWriter (const AliMUONRawWriter& rhs)
-  : TObject(rhs)
-{
-// Protected copy constructor
+  fFile[2] = fFile[3] = 0x0;  
 
-  AliFatal("Not implemented.");
-}
-
-//_______________________________________________________________________
-AliMUONRawWriter & 
-AliMUONRawWriter::operator=(const AliMUONRawWriter& rhs)
-{
-// Protected assignement operator
-
-  if (this == &rhs) return *this;
-
-  AliFatal("Not implemented.");
-    
-  return *this;  
 }
 
 //__________________________________________________________________________
 AliMUONRawWriter::~AliMUONRawWriter(void)
 {
+  /// Destructor
+
   AliDebug(1,"dtor");
   
-  delete fSubEventArray;
-  
-  delete fDDLTracker;
-  delete fDDLTrigger;
+  delete fBlockHeader;
+  delete fDspHeader;
+  delete fDarcHeader;
+  delete fRegHeader;
+  delete fLocalStruct;
 
   delete fBusPatchManager;
+  delete fCrateManager;
+
+  for ( Int_t i = 0; i < kLast; ++i )
+  {
+    AliDebug(1, Form("Execution time (timer %d) : R:%7.2fs C:%7.2fs",i,
+                 fTimers[i].RealTime(),fTimers[i].CpuTime()));
+  }
   
-  delete fSegFactory;
-  
-  AliInfo(Form("Execution time for MUON tracker : R:%.2fs C:%.2fs",
-               fTrackerTimer.RealTime(),fTrackerTimer.CpuTime()));
-  AliInfo(Form("   Execution time for MUON tracker (mapping calls part) "
-               ": R:%.2fs C:%.2fs",
-               fMappingTimer.RealTime(),fMappingTimer.CpuTime()));
-  AliInfo(Form("Execution time for MUON trigger : R:%.2fs C:%.2fs",
-               fTriggerTimer.RealTime(),fTriggerTimer.CpuTime()));
+  delete[] fTimers;
 }
 
+//______________________________________________________________________________
+//void
+//AliMUONRawWriter::CheckDigits()
+//{
+//  std::map<int,std::map<int,int> > m;
+//  
+//  for (Int_t iSt = 0; iSt < AliMUONConstants::NTrackingCh()/2; ++iSt) 
+//  {
+//    for (Int_t iCh = iSt*2; iCh <= iSt*2 + 1; ++iCh) 
+//    {      
+//      TClonesArray* muonDigits = fMUONData->Digits(iCh);
+//      for (Int_t idig = 0; idig < muonDigits->GetEntriesFast(); idig++) 
+//      {        
+//        AliMUONDigit* digit = (AliMUONDigit*) muonDigits->UncheckedAt(idig);
+//        Int_t busPatchId = GetBusPatch(*digit);
+//        m[busPatchId][digit->ManuId()]++;
+//      }
+//    } 
+//  }
+//  
+//  std::map<int,std::map<int,int> >::const_iterator it;
+//  
+//  Int_t nManuMax(0);
+//  
+//  for ( it = m.begin(); it != m.end(); ++it )
+//  {
+//    AliDebug(1,Form("BusPatch %3d has %3d manus",it->first,it->second.size()));
+//    nManuMax = std::max((Int_t)it->second.size(),nManuMax);
+//    std::map<int,int>::const_iterator it2;
+//    for ( it2 = it->second.begin(); it2 != it->second.end(); ++it2 )
+//    {
+//      AliDebug(1,Form("        BusPatch %3d Manu %4d Nch %3d",it->first,it2->first,it2->second));
+//    }
+//  }
+//  AliDebug(1,Form("Max manus per busPatch : %3d",nManuMax));
+//}
+
 //____________________________________________________________________
 Int_t AliMUONRawWriter::Digits2Raw()
 {
- // convert digits of the current event to raw data
 /// convert digits of the current event to raw data
 
   Int_t idDDL;
-  Char_t name[20];
+  Char_t name[255];
 
   fMUONData->GetLoader()->LoadDigits("READ");
 
@@ -182,35 +235,51 @@ Int_t AliMUONRawWriter::Digits2Raw()
   // This will get both tracker and trigger digits.
   fMUONData->GetDigits();
   
+//  CheckDigits();
+
   // tracking chambers
+  
+  for (Int_t iSt = 0; iSt < AliMUONConstants::NTrackingCh()/2; ++iSt) {
 
-  for (Int_t ich = 0; ich < AliMUONConstants::NTrackingCh(); ich++) 
-  {
-    // open files
-    idDDL = ich * 2  + 0x900; // official number for MUON
-    sprintf(name, "MUON_%d.ddl",idDDL);
+    // open files for one station
+    // cos station 3, 1/4 of DE's from 2 chambers has same DDL number 
+    idDDL = iSt * 4;
+    strcpy(name,AliDAQ::DdlFileName("MUONTRK",idDDL));
     fFile[0] = fopen(name,"w");
 
-    idDDL = (ich * 2) + 1 + 0x900;
-    sprintf(name, "MUON_%d.ddl",idDDL);
+    idDDL = (iSt * 4) + 1;
+    strcpy(name,AliDAQ::DdlFileName("MUONTRK",idDDL));
     fFile[1] = fopen(name,"w");
-    
-    WriteTrackerDDL(ich);
+
+    idDDL =  (iSt * 4) + 2;;
+    strcpy(name,AliDAQ::DdlFileName("MUONTRK",idDDL));
+    fFile[2] = fopen(name,"w");
+
+    idDDL =  (iSt * 4) + 3;
+    strcpy(name,AliDAQ::DdlFileName("MUONTRK",idDDL));
+    fFile[3] = fopen(name,"w");
+
+    WriteTrackerDDL(iSt);
   
-    // reset and close
+    // reset and close when station has been processed
     fclose(fFile[0]);
     fclose(fFile[1]);
+    fclose(fFile[2]);
+    fclose(fFile[3]);
+     
   }
  
+  AliDebug(1,"Tracker written");
+  
   // trigger chambers
  
   // open files
-  idDDL = 0xA00;// official number for MUTR
-  sprintf(name, "MUTR_%d.ddl",idDDL);
+  idDDL = 0;// MUTR
+  strcpy(name,AliDAQ::DdlFileName("MUONTRG",idDDL));
   fFile[0] = fopen(name,"w");
 
-  idDDL = 0xA00 + 1;
-  sprintf(name, "MUTR_%d.ddl",idDDL);
+  idDDL = 1;// MUTR
+  strcpy(name,AliDAQ::DdlFileName("MUONTRG",idDDL));
   fFile[1] = fopen(name,"w");
 
   WriteTriggerDDL();
@@ -219,291 +288,281 @@ Int_t AliMUONRawWriter::Digits2Raw()
   fclose(fFile[0]);
   fclose(fFile[1]);
 
+  AliDebug(1,"Trigger written");
+
   fMUONData->ResetDigits();
   fMUONData->ResetTrigger();  
   fMUONData->GetLoader()->UnloadDigits();
 
+  AliDebug(1,"muondata reset");
+  
   return kTRUE;
 }
 
 //____________________________________________________________________
-Int_t AliMUONRawWriter::WriteTrackerDDL(Int_t iCh)
+Int_t AliMUONRawWriter::WriteTrackerDDL(Int_t iSt)
 {
-  // writing DDL for tracker
-  // used inverse mapping
+  /// writing DDL for tracker
+  /// used inverse mapping
 
-  fTrackerTimer.Start(kFALSE);
+  fTimers[kWriteTracker].Start(kFALSE);
   
-  static const Int_t MAXADC = (1<<12)-1; // We code the charge on a 12 bits ADC.
+  static const Int_t kMAXADC = (1<<12)-1; // We code the charge on a 12 bits ADC.
+
   // resets
   TClonesArray* muonDigits = 0;
-  fSubEventArray->Clear();
-
-  //
-  TArrayI nbInBus(5000);
-  nbInBus.Reset();
 
   // DDL header
-  AliRawDataHeader header = fDDLTracker->GetHeader();
-  Int_t headerSize = fDDLTracker->GetHeaderSize();
+  Int_t headerSize = sizeof(fHeader)/4;
+
+  // DDL event one per half chamber
 
-  // data format
+  // raw data
   Char_t parity = 0x4;
   UShort_t manuId = 0;
   UChar_t channelId = 0;
   UShort_t charge = 0;
   Int_t busPatchId = 0;
-
   UInt_t word;
-  Int_t nEntries = 0;
-  Int_t* buffer = 0;
-  Int_t index;
-  Int_t indexDsp;
-  Int_t indexBlk;
-  Int_t padX;
-  Int_t padY;
-  Int_t cathode = 0;
-  Int_t detElemId;
-  Int_t nDigits;
 
-  const AliMUONDigit* digit;
 
-  AliDebug(3, Form("WriteDDL chamber %d\n", iCh+1));
+  // Dsp length
+  Int_t totalDspLength;
+  Int_t dspLength;
 
-  muonDigits = fMUONData->Digits(iCh);
-
-  nDigits = muonDigits->GetEntriesFast();
-  AliDebug(3,Form("ndigits = %d\n",nDigits));
-  // loop over digit
-  for (Int_t idig = 0; idig < nDigits; idig++) {
+  // block length
+  Int_t totalBlkLength;
+  Int_t blkLength; 
+  
+  // total DDL length
+  Int_t totalDDLLength;
 
-    digit = (AliMUONDigit*) muonDigits->UncheckedAt(idig);
+  // indexes
+  Int_t index;
+  Int_t indexDsp;
+  Int_t indexBlk;
 
-    padX = digit->PadX();
-    padY = digit->PadY();
-    charge = digit->Signal();
-    if ( charge > MAXADC )
-    {
-      // This is most probably an error in the digitizer (which should insure
-      // the charge is below MAXADC), so make it a (non-fatal) error indeed.
-      AliError(Form("adc value %d above %x. Setting to %x",
-                    charge,MAXADC,MAXADC));
-      charge = MAXADC;
-    }
-    cathode = digit->Cathode();
-    detElemId = digit->DetElemId();
+  // buffer size (max'ed out)
+  // (((43 manus max per bus patch *64 channels + 4 bus patch words) * 5 bus patch 
+  //   + 10 dsp words)*5 dsps + 8 block words)*2 blocks 
+  static const Int_t kBufferSize = (((43*64 + 4)*5 + 10)*5 + 8)*2;
+  
+  Int_t nDigits;
 
-    // inverse mapping
-    busPatchId = GetBusPatch(*digit);
-    if (busPatchId<0) continue;
+  AliMpExMap busPatchMap(kTRUE);
+  
+  fTimers[kDigitLoop].Start(kFALSE);
+  
+  for (Int_t iCh = iSt*2; iCh <= iSt*2 + 1; ++iCh) {
 
-    if ( digit->ManuId() > 0x7FF || digit->ManuId() < 0 ||
-         digit->ManuChannel() > 0x3F || digit->ManuChannel() < 0 )
-    {
-      StdoutToAliError(digit->Print(););
-      AliFatal("ManuId,ManuChannel are invalid for the digit above.");
-    }
-    
-    manuId = ( digit->ManuId() & 0x7FF ); // 11 bits
-    channelId = ( digit->ManuChannel() & 0x3F ); // 6 bits
+    muonDigits = fMUONData->Digits(iCh);
     
-    AliDebug(3,Form("input  detElemId %d busPatchId %d PadX %d PadY %d iCath %d \n", 
-                   detElemId, busPatchId, padX, padY, cathode));
+    nDigits = muonDigits->GetEntriesFast();
     
-    AliDebug(3,Form("busPatchId %d, manuId %d channelId %d\n", busPatchId, manuId, channelId ));
-          
-    //packing word
-    AliBitPacking::PackWord((UInt_t)parity,word,29,31);
-    AliBitPacking::PackWord((UInt_t)manuId,word,18,28);
-    AliBitPacking::PackWord((UInt_t)channelId,word,12,17);
-    AliBitPacking::PackWord((UInt_t)charge,word,0,11);
-
-    // DDL event one per half chamber
-    AliMUONSubEventTracker subEvent;
-    // set sub Event
-    subEvent.AddData(word);
-    subEvent.SetBusPatchId(busPatchId);
-       
-    // storing the number of identical buspatches
-    nbInBus[busPatchId]++;
-    AddData(subEvent);
-  }
-
-  // sorting by buspatch
-  fSubEventArray->Sort();
-
-  // gather datas from same bus patch
-  nEntries = fSubEventArray->GetEntriesFast();
-
-  for (Int_t i = 0; i < nEntries; i++) 
-  {
-    AliMUONSubEventTracker* temp = (AliMUONSubEventTracker*)fSubEventArray->At(i);
-    busPatchId = temp->GetBusPatchId();
-
-    // add bus patch header, length and total length managed by subevent class
-    temp->SetTriggerWord(0xdeadbeef);
-    for (Int_t j = 0; j < nbInBus[busPatchId]-1; j++) 
-    {
-      AliMUONSubEventTracker* temp1 =  (AliMUONSubEventTracker*)fSubEventArray->At(++i);
-      temp->AddData(temp1->GetData(0));
-      fSubEventArray->RemoveAt(i) ;
-    }
-  }
-  fSubEventArray->Compress();
-
-  if (AliLog::GetGlobalDebugLevel() == 3) 
-  {
-    nEntries = fSubEventArray->GetEntriesFast();
-    for (Int_t i = 0; i < nEntries; i++) 
-    {
-      AliMUONSubEventTracker* temp =  (AliMUONSubEventTracker*)fSubEventArray->At(i);
-      printf("busPatchid back %d\n",temp->GetBusPatchId());
-      for (Int_t j = 0; j < temp->GetLength(); j++) 
+    // loop over digit
+    for (Int_t idig = 0; idig < nDigits; ++idig) {
+      
+      AliMUONDigit* digit = static_cast<AliMUONDigit*>(muonDigits->UncheckedAt(idig));
+      
+      charge = digit->ADC();
+      if ( charge > kMAXADC )
       {
-        printf("manuId back %d, ",temp->GetManuId(j));
-        printf("channelId back %d, ",temp->GetChannelId(j));
-        printf("charge back %d\n",temp->GetCharge(j));
+        // This is most probably an error in the digitizer (which should insure
+        // the adc is below kMAXADC), so make it a (non-fatal) error indeed.
+        AliError(Form("adc value %d above %x for ch %d . Setting to %x. Digit is:",iCh,
+                      charge,kMAXADC,kMAXADC));
+        StdoutToAliError(digit->Print());
+        charge = kMAXADC;
       }
-    }
-    printf("\n");
-  }
+      
+      // inverse mapping
+      fTimers[kGetBusPatch].Start(kFALSE);
+      busPatchId = GetBusPatch(*digit);
+      fTimers[kGetBusPatch].Stop();
+      if (busPatchId<0) continue;
+      
+      fTimers[kTest].Start(kFALSE);
+      busPatchId = GetBusPatch(*digit);
+      fTimers[kTest].Stop();
+      
+      if ( digit->ManuId() > 0x7FF || digit->ManuId() < 0 ||
+           digit->ManuChannel() > 0x3F || digit->ManuChannel() < 0 )
+      {
+        StdoutToAliError(digit->Print(););
+        AliFatal("ManuId,ManuChannel are invalid for the digit above.");
+      }
+      
+      manuId = ( digit->ManuId() & 0x7FF ); // 11 bits
+      channelId = ( digit->ManuChannel() & 0x3F ); // 6 bits
+            
+      //packing word
+      word = 0;
+      AliBitPacking::PackWord((UInt_t)manuId,word,18,28);
+      AliBitPacking::PackWord((UInt_t)channelId,word,12,17);
+      AliBitPacking::PackWord((UInt_t)charge,word,0,11);
+      
+      // parity word
+      parity = word & 0x1;
+      for (Int_t i = 1; i <= 30; ++i) 
+        parity ^=  ((word >> i) & 0x1);
+      AliBitPacking::PackWord((UInt_t)parity,word,31,31);
+      
+      AliMUONBusStruct* busStruct = 
+        static_cast<AliMUONBusStruct*>(busPatchMap.GetValue(busPatchId));
+      
+      if (!busStruct)
+      {
+        busStruct = new AliMUONBusStruct;
+        busStruct->SetDataKey(busStruct->GetDefaultDataKey());
+        busStruct->SetBusPatchId(busPatchId);
+        busStruct->SetLength(0);
+        busPatchMap.Add(busPatchId,busStruct);
+      }
+
+      // set sub Event
+      busStruct->AddData(word);
+      
+    } // idig
+  } // loop over chamber in station
+    
+  fTimers[kDigitLoop].Stop();
   
   // getting info for the number of buspatches
   Int_t iBusPatch;
   Int_t length;
   Int_t iBusPerDSP[5];//number of bus patches per DSP
   Int_t iDspMax; //number max of DSP per block
   Int_t iFile = 0;
-  fBusPatchManager->GetDspInfo(iCh, iDspMax, iBusPerDSP);
 
-  TArrayI* vec = fBusPatchManager->GetBusfromDE((iCh+1)*100);
+  AliMUONBusStruct* busStructPtr(0x0);
 
-  Int_t iBus0AtCh = vec->At(0); //get first bus patch id for a given ich
-       
-  AliDebug(3,Form("iBus0AtCh %d", iBus0AtCh));
+  // open DDL files, 4 per station
+  for (Int_t iDDL = iSt*4; iDDL < 4 + iSt*4; ++iDDL) {
 
-  iBusPatch = iBus0AtCh - 1; // starting point for each chamber
+    fBusPatchManager->ResetBusItr(iDDL);
+    fBusPatchManager->GetDspInfo(iDDL, iDspMax, iBusPerDSP);
 
-  // open DDL file, on per 1/2 chamber
-  for (Int_t iDDL = 0; iDDL < 2; iDDL++) 
-  {
-    // filling buffer
-    buffer = new Int_t [(2048+24)*50]; // 24 words in average for one buspatch and 2048 manu info at most
+    Int_t buffer[kBufferSize];
     
+    totalDDLLength = 0;
+
     indexBlk = 0;
     indexDsp = 0;
     index = 0;
-    
+
     // two blocks A and B per DDL
-    for (Int_t iBlock = 0; iBlock < 2; iBlock++) 
-    {
+    for (Int_t iBlock = 0; iBlock < 2; ++iBlock) {
+      
       // block header
-      length = fDDLTracker->GetBlkHeaderLength();
-      memcpy(&buffer[index],fDDLTracker->GetBlkHeader(),length*4);
+      length = fBlockHeader->GetHeaderLength();
+      memcpy(&buffer[index],fBlockHeader->GetHeader(),length*4);
       indexBlk = index;
       index += length; 
       
       // 5 DSP's max per block
-      for (Int_t iDsp = 0; iDsp < iDspMax; iDsp++) 
-      {
+      for (Int_t iDsp = 0; iDsp < iDspMax; ++iDsp) {
         
         // DSP header
-        length = fDDLTracker->GetDspHeaderLength();
-        memcpy(&buffer[index],fDDLTracker->GetDspHeader(),length*4);
+        length = fDspHeader->GetHeaderLength();
+        memcpy(&buffer[index],fDspHeader->GetHeader(),length*4);
         indexDsp = index;
         index += length; 
         
         // 5 buspatches max per DSP
-        for (Int_t i = 0; i < iBusPerDSP[iDsp]; i++) 
-        {          
-          iBusPatch ++;
-          if ((fBusPatchManager->GetDDLfromBus(iBusPatch) % 2) == 1) // comparing to DDL file
-            iFile = 0;
-          else
-            iFile = 1;
+        for (Int_t i = 0; i < iBusPerDSP[iDsp]; i++) {
           
-          AliDebug(3,Form("iCh %d iDDL %d iBlock %d iDsp %d busPatchId %d", iCh, iDDL, iBlock, iDsp, iBusPatch));
+          iBusPatch = fBusPatchManager->NextBusInDDL(iDDL);
           
-          nEntries = fSubEventArray->GetEntriesFast();
-          AliMUONSubEventTracker* temp = 0x0;
-         busPatchId = -1;
-          for (Int_t iEntries = 0; iEntries < nEntries; iEntries++)
-          { // method "bourrique"...
-            temp = (AliMUONSubEventTracker*)fSubEventArray->At(iEntries);
-            busPatchId = temp->GetBusPatchId();
-            if (busPatchId == iBusPatch) break;
-            busPatchId = -1;
-            AliDebug(3,Form("busPatchId %d", temp->GetBusPatchId()));
-          } 
+          // iteration over bus patch in DDL
+          if (iBusPatch == -1) {
+            AliWarning(Form("Error in bus itr in DDL %d\n", iDDL));
+            continue;
+          }
+          
+          // 4 DDL's per station, condition needed for station 3
+          iFile = iDDL - iSt*4; // works only if DDL begins at zero (as it should be) !!!
+          
+          busStructPtr = static_cast<AliMUONBusStruct*>(busPatchMap.GetValue(iBusPatch));
           
           // check if buspatchid has digit
-          if (busPatchId != -1) 
-          {
-            // add bus patch structure
-            length = temp->GetHeaderLength();
-            memcpy(&buffer[index],temp->GetBusPatchHeader(),length*4);
+          if (busStructPtr) {
+            // add bus patch structure header
+            length = busStructPtr->GetHeaderLength();
+            memcpy(&buffer[index],busStructPtr->GetHeader(),length*4);
             index += length;
-            for (Int_t j = 0; j < temp->GetLength(); j++) 
-            {
-              buffer[index++] =  temp->GetData(j);
-              AliDebug(3,Form("busPatchId %d, manuId %d channelId %d\n", temp->GetBusPatchId(), 
-                              temp->GetManuId(j), temp->GetChannelId(j) ));
+            
+            // add bus patch data
+            length = busStructPtr->GetLength();
+            memcpy(&buffer[index],busStructPtr->GetData(),length*4);
+            index += length;
+            
+            if (AliLog::GetGlobalDebugLevel() == 3) {
+              for (Int_t j = 0; j < busStructPtr->GetLength(); j++) {
+                printf("busPatchId %d, manuId %d channelId %d\n", busStructPtr->GetBusPatchId(), 
+                       busStructPtr->GetManuId(j), busStructPtr->GetChannelId(j));
+              }
             }
-          }
-          else 
-          {
+          } else {
             // writting anyhow buspatch structure (empty ones)
-            buffer[index++] = 4; // total length
+            buffer[index++] = busStructPtr->GetDefaultDataKey(); // fill it also for empty data size
+            buffer[index++] = busStructPtr->GetHeaderLength(); // header length
             buffer[index++] = 0; // raw data length
             buffer[index++] = iBusPatch; // bus patch
-            buffer[index++] = 0xdeadbeef; // trigger word
           }
         } // bus patch
-        buffer[indexDsp] = index - indexDsp; // dsp length
-        buffer[indexDsp+1] = index - indexDsp - fDDLTracker->GetDspHeaderLength();
-        if ((index - indexDsp) % 2 == 0)
-          buffer[indexDsp+7] = 0;
-        else
-          buffer[indexDsp+7] = 1;
+        
+        // check if totalLength even
+        // set padding word in case
+        // Add one word 0xBEEFFACE at the end of DSP structure
+        totalDspLength  = index - indexDsp;
+        if ((totalDspLength % 2) == 1) { 
+          buffer[indexDsp + fDspHeader->GetHeaderLength() - 2] = 1;
+          buffer[index++] = fDspHeader->GetDefaultPaddingWord();
+          totalDspLength++;
+        }
+        
+        dspLength          = totalDspLength - fDspHeader->GetHeaderLength();
+        
+        buffer[indexDsp+1] = totalDspLength; // dsp total length
+        buffer[indexDsp+2] = dspLength; // data length  
+        
       } // dsp
-      buffer[indexBlk] = index - indexBlk; // block length
-      buffer[indexBlk+1] = index - indexBlk - fDDLTracker->GetBlkHeaderLength();
-    }
+      
+      totalBlkLength  = index - indexBlk;
+      blkLength       = totalBlkLength - fBlockHeader->GetHeaderLength();
+      totalDDLLength += totalBlkLength;
+      
+      buffer[indexBlk+1] = totalBlkLength; // total block length
+      buffer[indexBlk+2] = blkLength;
+      
+    } // block
     
     //writting onto disk
-    // write DDL 1 & 2
-    header.fSize = (index + headerSize) * 4;// total length in bytes
-    fwrite((char*)(&header),headerSize*4,1,fFile[iFile]);
-    fwrite(buffer,sizeof(int),index,fFile[iFile]);
+    // write DDL 1 - 4
+    // total length in bytes
+    fHeader.fSize = (totalDDLLength + headerSize) * 4;
       
-    delete[] buffer;
+    fwrite((char*)(&fHeader),headerSize*4,1,fFile[iFile]);
+    fwrite(buffer,sizeof(int),index,fFile[iFile]);
   }
-
-  fTrackerTimer.Stop();
+  
+  fTimers[kWriteTracker].Stop();
   return kTRUE;
 }
 
 //____________________________________________________________________
-Int_t AliMUONRawWriter::GetBusPatch(const AliMUONDigit& digit)
+Int_t AliMUONRawWriter::GetBusPatch(Int_t detElemId, Int_t manuId) const
 {
-  // Determine the BusPatch this digit belongs to.
-  
-  fMappingTimer.Start(kFALSE);
+  /// Determine the BusPatch this digit belongs to.
   
   Int_t* ptr = 0;
-
-  // information from digits
-  Int_t detElemId  = digit.DetElemId();
-
-  AliMpVSegmentation* seg = 
-    fSegFactory->CreateMpSegmentationByElectronics(detElemId, digit.ManuId());
+    
+  AliMpPlaneType plane = 
+    (manuId & AliMpConstants::ManuMask(kNonBendingPlane)) ? 
+    kNonBendingPlane : kBendingPlane; 
   
-  AliMpPlaneType plane = seg->PlaneType();
-
   AliMpStationType stationType = AliMpDEManager::GetStationType(detElemId);
-
+  
   if ( stationType == kStation1 || stationType == kStation2 )
   {
     if (plane == kBendingPlane) 
@@ -526,21 +585,21 @@ Int_t AliMUONRawWriter::GetBusPatch(const AliMUONDigit& digit)
       ptr = &fgManuPerBusSwp2NB[0];
     }
   }
-
+  
   // Getting buspatch id
   TArrayI* vec = fBusPatchManager->GetBusfromDE(detElemId);
   Int_t pos = 0;
-
-  Int_t m = ( digit.ManuId() & 0x3FF ); // remove bit 10
-                                //FIXME : how can we remove that condition
-  // on the 10-th bit ? All the rest need not any knowledge about it,
-  // can't we find a way to get manu<->buspatch transparent to this too ?
+  
+  Int_t m = ( manuId & 0x3FF ); // remove bit 10
+                                        //FIXME : how can we remove that condition
+                                        // on the 10-th bit ? All the rest need not any knowledge about it,
+                                        // can't we find a way to get manu<->buspatch transparent to this too ?
   
   if ( stationType == kStation1 || stationType == kStation2 )
   {
-    for (Int_t i = 0; i < 12; i++)
+    for (pos = 11; pos >=0 ; --pos)
     {
-      if (m >= *(ptr + pos++)) break;
+      if (m >= ptr[pos]) break;
     }
   }
   else 
@@ -548,44 +607,55 @@ Int_t AliMUONRawWriter::GetBusPatch(const AliMUONDigit& digit)
     // offset of 100 in manuId for following bus patch
     pos = m/100;
   }
-
+  
+  
   if (pos >(Int_t) vec->GetSize())
   {
     AliError(Form("pos greater %d than size %d manuId %d detElemId %d \n", 
-                   pos, (Int_t)vec->GetSize(), digit.ManuId(), detElemId));
+                  pos, (Int_t)vec->GetSize(), manuId, detElemId));
     AliError(Form("Chamber %s Plane %s manuId %d m %d",
-                    StationTypeName(stationType).Data(),
-                    PlaneTypeName(plane).Data(),
-                    digit.ManuId(),
-                    m));
+                  StationTypeName(stationType).Data(),
+                  PlaneTypeName(plane).Data(),
+                  manuId,
+                  m));
     return -1;
   }
   
   Int_t busPatchId = vec->At(pos);
-
-  fMappingTimer.Stop();
+  
+  if ( ( stationType == kStation1 || stationType == kStation2 ) &&
+       ( plane == kNonBendingPlane ) )
+  {
+    busPatchId += 12;
+  }
   
   return busPatchId;
 }
 
+//____________________________________________________________________
+Int_t AliMUONRawWriter::GetBusPatch(const AliMUONDigit& digit) const
+{
+  /// Determine the BusPatch this digit belongs to.
+
+  return GetBusPatch(digit.DetElemId(),digit.ManuId());
+}
+
 //____________________________________________________________________
 Int_t AliMUONRawWriter::WriteTriggerDDL()
 {
+  /// Write trigger DDL
 
-  fTriggerTimer.Start(kFALSE);
+  fTimers[kWriteTrigger].Start(kFALSE);
   
  // DDL event one per half chamber
-  AliMUONSubEventTrigger*    subEvent    = 0x0;
-  AliMUONScalerEventTrigger* scalerEvent = 0x0;
 
   // stored local id number 
   TArrayI isFired(256);
   isFired.Reset();
 
 
- // DDL header
-  AliRawDataHeader header = fDDLTrigger->GetHeader();
-  Int_t headerSize = fDDLTrigger->GetHeaderSize();
+ // DDL header size
+  Int_t headerSize = sizeof(AliRawDataHeader)/4;
 
   TClonesArray* localTrigger;
   TClonesArray* globalTrigger;
@@ -595,7 +665,13 @@ Int_t AliMUONRawWriter::WriteTriggerDDL()
   // global trigger for trigger pattern
   globalTrigger = fMUONData->GlobalTrigger(); 
   gloTrg = (AliMUONGlobalTrigger*)globalTrigger->UncheckedAt(0);
-  Int_t gloTrigPat = GetGlobalTriggerPattern(gloTrg);
+  if (!gloTrg) 
+  {
+    fTimers[kWriteTrigger].Stop();
+    return 0;
+  }
+  
+  Int_t gloTrigResp = gloTrg->GetGlobalResponse();
 
   // local trigger 
   localTrigger = fMUONData->LocalTrigger();    
@@ -605,15 +681,37 @@ Int_t AliMUONRawWriter::WriteTriggerDDL()
   Int_t index;
   Int_t iEntries = 0;
   Int_t iLocCard, locCard;
-  Char_t locDec, trigY, posY, posX,regOut;
+  Char_t locDec, trigY, posY, posX, regOut;
+  UInt_t regInpLpt;
+  UInt_t regInpHpt;
+
   Int_t devX;
   Int_t version = 1; // software version
-  Int_t eventType = 1; // trigger type: 1 for physics ?
+  Int_t eventPhys = 1; // trigger type: 1 for physics, 0 for software
   Int_t serialNb = 0xF; // serial nb of card: all bits on for the moment
-  Int_t globalFlag = 1; // set to 2 if global info present in DDL else set to 1
-
+  Int_t globalFlag = 0; // set to 1 if global info present in DDL else set to 0
+
+  // size of headers
+  static const Int_t kDarcHeaderLength   = fDarcHeader->GetDarcHeaderLength();
+  static const Int_t kGlobalHeaderLength = fDarcHeader->GetGlobalHeaderLength();
+  static const Int_t kDarcScalerLength   = fDarcHeader->GetDarcScalerLength();
+  static const Int_t kGlobalScalerLength = fDarcHeader->GetGlobalScalerLength();
+  static const Int_t kRegHeaderLength    = fRegHeader->GetHeaderLength();
+  static const Int_t kRegScalerLength    = fRegHeader->GetScalerLength();
+  static const Int_t kLocHeaderLength    = fLocalStruct->GetLength();
+  static const Int_t kLocScalerLength    = fLocalStruct->GetScalerLength();
+
+  // [16(local)*6 words + 6 words]*8(reg) + 8 words = 824 
+  static const Int_t kBufferSize = (16 * (kLocHeaderLength+1) +  (kRegHeaderLength+1))* 8 
+      +  kDarcHeaderLength + kGlobalHeaderLength + 2;
+
+  // [16(local)*51 words + 16 words]*8(reg) + 8 + 10 + 8 words scaler event 6682 words
+  static const Int_t kScalerBufferSize = (16 * (kLocHeaderLength +  kLocScalerLength +1) +  
+                                        (kRegHeaderLength + kRegScalerLength +1))* 8 +
+                                         (kDarcHeaderLength + kDarcScalerLength + 
+                                         kGlobalHeaderLength + kGlobalScalerLength + 2);
   if(fScalerEvent)
-    eventType = 2; //set to generate scaler events
+    eventPhys = 0; //set to generate scaler events
 
   Int_t nEntries = (Int_t) (localTrigger->GetEntries());// 234 local cards
   // stored the local card id that's fired
@@ -623,192 +721,227 @@ Int_t AliMUONRawWriter::WriteTriggerDDL()
   }
 
   if (!nEntries)
-    AliError("No Trigger information available");
+    AliDebug(1, "No Trigger information available");
 
   if(fScalerEvent)
-    // [16(local)*50 words + 14 words]*8(reg) + 6 + 10 + 6 words scaler event 6534 words
-    buffer = new Int_t [6534];
+    buffer = new Int_t [kScalerBufferSize];
   else
-    // [16(local)*5 words + 3 words]*8(reg) + 8 words = 672 
-    buffer = new Int_t [672];
+    buffer = new Int_t [kBufferSize];
 
-  if(fScalerEvent) {
-    scalerEvent = new  AliMUONScalerEventTrigger();
-    scalerEvent->SetNumbers(); // set some numbers for scalers
-  }
+  // reset crate
 
   // open DDL file, on per 1/2 chamber
   for (Int_t iDDL = 0; iDDL < 2; iDDL++) {
-    
-    index = 0; 
 
-    // DDL enhanced header
-    word = 0;
-    AliBitPacking::PackWord((UInt_t)iDDL+1,word,28,31); //see AliMUONDDLTrigger.h for details
-    AliBitPacking::PackWord((UInt_t)serialNb,word,24,27);
-    AliBitPacking::PackWord((UInt_t)version,word,16,23);
-    AliBitPacking::PackWord((UInt_t)eventType,word,12,15);
+    index = 0; 
 
     if (iDDL == 0) // suppose global info in DDL one
-      globalFlag = 2;
-    else 
       globalFlag = 1;
+    else 
+      globalFlag = 0;
 
-    AliBitPacking::PackWord((UInt_t)globalFlag,word,8,11);
-    fDDLTrigger->SetDDLWord(word);
-    buffer[index++]= word;
-
+    word = 0;
+    // set darc status word
+    // see AliMUONDarcHeader.h for details
+    AliBitPacking::PackWord((UInt_t)eventPhys,word,30,30);
+    AliBitPacking::PackWord((UInt_t)serialNb,word,20,23);
+    AliBitPacking::PackWord((UInt_t)globalFlag,word,10,10);
+    AliBitPacking::PackWord((UInt_t)version,word,12,19);
+    fDarcHeader->SetWord(word);
+
+    memcpy(&buffer[index], fDarcHeader->GetHeader(), (kDarcHeaderLength)*4); 
+    index += kDarcHeaderLength;
+
+    // no global input for the moment....
     if (iDDL == 0)
-      fDDLTrigger->SetGlobalOutput(gloTrigPat);// no global input for the moment....
+     fDarcHeader->SetGlobalOutput(gloTrigResp);
     else 
-      fDDLTrigger->SetGlobalOutput(0);
+     fDarcHeader->SetGlobalOutput(0);
 
     if (fScalerEvent) {
       // 6 DARC scaler words
-      memcpy(&buffer[index], scalerEvent->GetDarcScalers(),scalerEvent->GetDarcScalerLength()*4);
-      index += scalerEvent->GetDarcScalerLength();
+      memcpy(&buffer[index], fDarcHeader->GetDarcScalers(),kDarcScalerLength*4);
+      index += kDarcScalerLength;
     }
+    // end of darc word
+    buffer[index++] = fDarcHeader->GetEndOfDarc();
 
     // 4 words of global board input + Global board output
-    memcpy(&buffer[index], fDDLTrigger->GetGlobalInput(), (fDDLTrigger->GetHeaderLength()-1)*4); 
-    index += fDDLTrigger->GetHeaderLength() - 1; // kind tricky cos scaler info in-between Darc header
+    memcpy(&buffer[index], fDarcHeader->GetGlobalInput(), (kGlobalHeaderLength)*4); 
+    index += kGlobalHeaderLength; 
 
     if (fScalerEvent) {
       // 10 Global scaler words
-      memcpy(scalerEvent->GetGlobalScalers(), &buffer[index], scalerEvent->GetGlobalScalerLength()*4);
-      index += scalerEvent->GetGlobalScalerLength();
+      memcpy(fDarcHeader->GetGlobalScalers(), &buffer[index], kGlobalScalerLength*4);
+      index += kGlobalScalerLength;
     }
 
+    // end of global word
+    buffer[index++] = fDarcHeader->GetEndOfGlobal();
+
     // 8 regional cards per DDL
     for (Int_t iReg = 0; iReg < 8; iReg++) {
 
-      subEvent = new AliMUONSubEventTrigger();
+      // crate info
+      AliMUONTriggerCrate* crate = fCrateManager->Crate(iDDL, iReg);
+
+      if (!crate) 
+       AliWarning(Form("Missing crate number %d in DDL %d\n", iReg, iDDL));
 
       // Regional card header
       word = 0;
-      regOut  = 0;
-      AliBitPacking::PackWord((UInt_t)serialNb,word,24,28); //see  AliMUONSubEventTrigger.h for details
+
+      // set darc status word
+      fRegHeader->SetDarcWord(word);
+
+      regOut    = 0;
+      regInpHpt = regInpLpt = 0;
+      // fill darc word, not darc status for the moment (empty)
+      //see  AliMUONRegHeader.h for details
+      AliBitPacking::PackWord((UInt_t)eventPhys,word,31,31); 
+      AliBitPacking::PackWord((UInt_t)serialNb,word,19,24); 
       AliBitPacking::PackWord((UInt_t)version,word,16,23);
-      AliBitPacking::PackWord((UInt_t)iReg,word,12,15);
-      AliBitPacking::PackWord((UInt_t)regOut,word,0,7); // whenever regional output will be implemented
+      AliBitPacking::PackWord((UInt_t)iReg,word,15,18);
+      AliBitPacking::PackWord((UInt_t)regOut,word,0,7); // waiting realistic output of AliMUONGlobalTrigger (oct 06 ?)
+      fRegHeader->SetWord(word);
+
 
-      subEvent->SetRegWord(word);
-      memcpy(&buffer[index],subEvent->GetRegHeader(),subEvent->GetRegHeaderLength()*4);
-      index += subEvent->GetRegHeaderLength();
+      // fill header later, need local response
+      Int_t indexReg = index;
+      index += kRegHeaderLength;
 
       // 11 regional scaler word
       if (fScalerEvent) {
-       memcpy(&buffer[index], scalerEvent->GetRegScalers(), scalerEvent->GetRegScalerLength()*4);
-       index += scalerEvent->GetRegScalerLength();
+       memcpy(&buffer[index], fRegHeader->GetScalers(), kRegScalerLength*4);
+       index += kRegScalerLength;
       }
 
+      // end of regional word
+      buffer[index++] = fRegHeader->GetEndOfReg();
+      
+      TObjArray *boards = crate->Boards();
+
+
       // 16 local card per regional board
-      for (Int_t iLoc = 0; iLoc < 16; iLoc++) {
+      UShort_t localMask = 0x0;
 
-       iLocCard = iLoc + iReg*16 + iDDL*128;
-
-       if (isFired[iLocCard]) {
-         locTrg = (AliMUONLocalTrigger*)localTrigger->At(iEntries);
-         locCard = locTrg->LoCircuit();
-         locDec  = locTrg->GetLoDecision();
-         trigY = 0;
-         posY = locTrg->LoStripY();
-         posX = locTrg->LoStripX();
-         devX = locTrg->LoDev();
-         AliDebug(4,Form("loctrg %d, posX %d, posY %d, devX %d\n", 
-                         locTrg->LoCircuit(),locTrg->LoStripX(),locTrg->LoStripY(),locTrg->LoDev()));
-       } else { //no trigger (see PRR chpt 3.4)
-         locCard = -1;
-         locDec = 0;
-         trigY = 1;
-         posY = 15;
-         posX = 0;
-         devX = 0x8;
-       }
+      for (Int_t iLoc = 0; iLoc < 16; iLoc++) {
 
-       //packing word
-       word = 0;
-       AliBitPacking::PackWord((UInt_t)(iLocCard % 16),word,19,22); //card id number in crate
-       AliBitPacking::PackWord((UInt_t)locDec,word,15,18);
-       AliBitPacking::PackWord((UInt_t)trigY,word,14,14);
-       AliBitPacking::PackWord((UInt_t)posY,word,10,13);
-       AliBitPacking::PackWord((UInt_t)devX,word,5,9);
-       AliBitPacking::PackWord((UInt_t)posX,word,0,4);
-
-       if (locCard == iLocCard) {
-         // add local cards structure
-         buffer[index++] = (locTrg->GetX1Pattern() | (locTrg->GetX2Pattern() << 16));
-         buffer[index++] = (locTrg->GetX3Pattern() | (locTrg->GetX4Pattern() << 16));
-         buffer[index++] = (locTrg->GetY1Pattern() | (locTrg->GetY2Pattern() << 16));
-         buffer[index++] = (locTrg->GetY3Pattern() | (locTrg->GetY4Pattern() << 16));
-         buffer[index++] = (Int_t)word; // data word
-         if (iEntries < nEntries-1)
-           iEntries++;
-       } else {
-         buffer[index++] = 0; // 4 words for x1, x2, y1, y2
-         buffer[index++] = 0; 
-         buffer[index++] = 0; 
-         buffer[index++] = 0; 
-         buffer[index++] = (Int_t)word; // data word
+       // slot zero for Regional card
+       AliMUONLocalTriggerBoard* localBoard = (AliMUONLocalTriggerBoard*)boards->At(iLoc+1);
+
+       if (localBoard) { // if not empty slot
+
+         if ((iLocCard = localBoard->GetNumber()) != 0) {// if notified board
+
+           localMask |= (0x1 << iLoc); // local mask
+           if (isFired[iLocCard]) { // if card has triggered
+             locTrg  = (AliMUONLocalTrigger*)localTrigger->At(iEntries);
+             locCard = locTrg->LoCircuit();
+             locDec  = locTrg->GetLoDecision();
+             trigY = 0;
+             posY  = locTrg->LoStripY();
+             posX  = locTrg->LoStripX();
+             devX  = locTrg->LoDev();
+
+             AliDebug(4,Form("loctrg %d, posX %d, posY %d, devX %d\n", 
+                             locTrg->LoCircuit(),locTrg->LoStripX(),locTrg->LoStripY(),locTrg->LoDev()));
+           } else { //no trigger (see PRR chpt 3.4)
+             locDec = 0;
+             trigY = 1;
+             posY = 15;
+             posX = 0;
+             devX = 0x8;
+             // set local card id to -1
+             locCard = -1; 
+           }
+           // calculate regional input High and low Pt
+           UInt_t tmp1 = (locDec >> 2) & 0x3;
+           UInt_t tmp2 =  locDec & 0x3;
+           
+           regInpHpt |= tmp1 << (30 - iLoc*2);
+           regInpLpt |= tmp2 << (30 - iLoc*2);
+          
+           //packing word
+           word = 0;
+           AliBitPacking::PackWord((UInt_t)iLoc,word,19,22); //card id number in crate
+           AliBitPacking::PackWord((UInt_t)locDec,word,15,18);
+           AliBitPacking::PackWord((UInt_t)trigY,word,14,14);
+           AliBitPacking::PackWord((UInt_t)posY,word,10,13);
+           AliBitPacking::PackWord((UInt_t)devX,word,5,9);
+           AliBitPacking::PackWord((UInt_t)posX,word,0,4);
+
+           if (locCard == iLocCard) {
+             // add local cards structure
+             buffer[index++] = (locTrg->GetX1Pattern() | (locTrg->GetX2Pattern() << 16));
+             buffer[index++] = (locTrg->GetX3Pattern() | (locTrg->GetX4Pattern() << 16));
+             buffer[index++] = (locTrg->GetY1Pattern() | (locTrg->GetY2Pattern() << 16));
+             buffer[index++] = (locTrg->GetY3Pattern() | (locTrg->GetY4Pattern() << 16));
+             buffer[index++] = (Int_t)word; // data word
+             if (iEntries < nEntries-1)
+               iEntries++;
+           } else {
+             buffer[index++] = 0; // 4 words for x1, x2, y1, y2
+             buffer[index++] = 0; 
+             buffer[index++] = 0; 
+             buffer[index++] = 0; 
+             buffer[index++] = (Int_t)word; // data word
+
+           }
+         } else {// number!=0
+         // fill with 10CDEAD word for 'non-notified' slots
+         for (Int_t i = 0; i < fLocalStruct->GetLength(); i++)
+           buffer[index++] = fLocalStruct->GetDisableWord(); 
+         }
+       } else { 
+         // fill with 10CDEAD word for empty slots
+         for (Int_t i = 0; i < fLocalStruct->GetLength(); i++)
+           buffer[index++] = fLocalStruct->GetDisableWord(); 
+       }// condition localBoard
 
-       }
        // 45 regional scaler word
        if (fScalerEvent) {
-         memcpy(&buffer[index], scalerEvent->GetLocalScalers(), scalerEvent->GetLocalScalerLength()*4);
-         index += scalerEvent->GetLocalScalerLength();
+         memcpy(&buffer[index], fLocalStruct->GetScalers(), kLocScalerLength*4);
+         index += kLocScalerLength;
        }
 
-      } // local card 
+       // end of local structure words
+       buffer[index++] = fLocalStruct->GetEndOfLocal();
 
-      delete subEvent; 
+      } // local card 
+      // fill regional header with local output
+      fRegHeader->SetInput(regInpLpt, 0);
+      fRegHeader->SetInput(regInpHpt, 1);
+      fRegHeader->SetMask(localMask);
+      memcpy(&buffer[indexReg],fRegHeader->GetHeader(),kRegHeaderLength*4);
 
     } // Regional card
     
-    buffer[index++] = fDDLTrigger->GetEoD(); // End of DDL word
-    buffer[index++] = fDDLTrigger->GetEoD(); // End of DDL word for 64 bits transfer purpose
 
     // writting onto disk
-    // write DDL 1
-    header.fSize = (index + headerSize) * 4;// total length in bytes
-    fwrite((char*)(&header),headerSize*4,1,fFile[iDDL]);
+    // write DDL's
+    fHeader.fSize = (index + headerSize) * 4;// total length in bytes
+    fwrite((char*)(&fHeader),headerSize*4,1,fFile[iDDL]);
     fwrite(buffer,sizeof(int),index,fFile[iDDL]);
   
   }
   delete[] buffer;
 
-  delete scalerEvent;
-
-  fTriggerTimer.Stop();
+  fTimers[kWriteTrigger].Stop();
   
   return kTRUE;
 }
-
 //____________________________________________________________________
-Int_t AliMUONRawWriter::GetGlobalTriggerPattern(const AliMUONGlobalTrigger* gloTrg) const
+void AliMUONRawWriter::SetScalersNumbers()
 {
-  // global trigger pattern calculation
-
-  Int_t gloTrigPat = 0;
+  /// set numbers for scaler events for trigger headers
+  /// since this is provided by the experiment
+  /// put dummy numbers to check the monitoring
 
-  if (gloTrg->SinglePlusLpt())  gloTrigPat|= 0x1;
-  if (gloTrg->SinglePlusHpt())  gloTrigPat|= 0x2;
-  if (gloTrg->SinglePlusApt())  gloTrigPat|= 0x4;
+  fDarcHeader->SetScalersNumbers();
+  fRegHeader->SetScalersNumbers();
+  fLocalStruct->SetScalersNumbers();
  
-  if (gloTrg->SingleMinusLpt()) gloTrigPat|= 0x8;
-  if (gloTrg->SingleMinusHpt()) gloTrigPat|= 0x10;
-  if (gloTrg->SingleMinusApt()) gloTrigPat|= 0x20;
-  if (gloTrg->SingleUndefLpt()) gloTrigPat|= 0x40;
-  if (gloTrg->SingleUndefHpt()) gloTrigPat|= 0x80;
-  if (gloTrg->SingleUndefApt()) gloTrigPat|= 0x100;
-  if (gloTrg->PairUnlikeLpt())  gloTrigPat|= 0x200;
-  if (gloTrg->PairUnlikeHpt())  gloTrigPat|= 0x400;
-  if (gloTrg->PairUnlikeApt())  gloTrigPat|= 0x800;
-
-  if (gloTrg->PairLikeLpt())    gloTrigPat|= 0x1000;
-  if (gloTrg->PairLikeHpt())    gloTrigPat|= 0x2000;
-  if (gloTrg->PairLikeApt())    gloTrigPat|= 0x4000;
-
-  return gloTrigPat;
+  fScalerEvent = kTRUE;
 }
+