]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - TRD/AliTRDrawData.cxx
Protections added in GetEvent method.
[u/mrichter/AliRoot.git] / TRD / AliTRDrawData.cxx
index 5eba5586aac6829f2d44a86581f68c648d180dfd..148c618fad9713fde94b7854172e8197e8aedff3 100644 (file)
 #include "AliTRDarrayDictionary.h"
 #include "AliTRDarrayADC.h"
 #include "AliTRDrawStreamBase.h"
-#include "AliTRDrawOldStream.h"
 #include "AliTRDcalibDB.h"
 #include "AliTRDSignalIndex.h"
 #include "AliTRDfeeParam.h"
 #include "AliTRDmcmSim.h"
+#include "AliTRDdigitsParam.h"
 
 ClassImp(AliTRDrawData)
 
 Int_t AliTRDrawData::fgRawFormatVersion = AliTRDrawData::kRawNewFormat;
-Int_t AliTRDrawData::fgDataSuppressionLevel = 1;
+Int_t AliTRDrawData::fgDataSuppressionLevel = 0;
 
 //_____________________________________________________________________________
 AliTRDrawData::AliTRDrawData()
   :TObject()
+  ,fRunLoader(NULL)
   ,fGeo(NULL)
   ,fFee(NULL)
   ,fNumberOfDDLs(0)
+  ,fTrackletTree(NULL)
+  ,fTrackletContainer(NULL)
   ,fSMindexPos(0)
   ,fStackindexPos(0)
   ,fEventCounter(0)
+  ,fDigitsParam(NULL)
 {
   //
   // Default constructor
@@ -70,12 +74,16 @@ AliTRDrawData::AliTRDrawData()
 //_____________________________________________________________________________
 AliTRDrawData::AliTRDrawData(const AliTRDrawData &r)
   :TObject(r)
+  ,fRunLoader(NULL)
   ,fGeo(NULL)
   ,fFee(NULL)
   ,fNumberOfDDLs(0)
+  ,fTrackletTree(NULL)
+  ,fTrackletContainer(NULL)
   ,fSMindexPos(0)
   ,fStackindexPos(0)
   ,fEventCounter(0)
+  ,fDigitsParam(NULL)
 {
   //
   // Copy constructor
@@ -93,10 +101,15 @@ AliTRDrawData::~AliTRDrawData()
   // Destructor
   //
 
+  if (fTrackletContainer){
+    delete fTrackletContainer;
+    fTrackletContainer = NULL;
+  }
+
 }
 
 //_____________________________________________________________________________
-Bool_t AliTRDrawData::Digits2Raw(TTree *digitsTree, TTree *tracks )
+Bool_t AliTRDrawData::Digits2Raw(TTree *digitsTree, const TTree *tracks )
 {
   //
   // Initialize necessary parameters and call one
@@ -106,7 +119,7 @@ Bool_t AliTRDrawData::Digits2Raw(TTree *digitsTree, TTree *tracks )
   // will be supported in higher version simulator.
   //
 
-  AliTRDdigitsManager* digitsManager = new AliTRDdigitsManager();
+  AliTRDdigitsManager* const digitsManager = new AliTRDdigitsManager();
 
   if (!digitsManager->ReadDigits(digitsTree)) {
     delete digitsManager;
@@ -162,16 +175,19 @@ Bool_t AliTRDrawData::Digits2Raw(AliTRDdigitsManager *digitsManager)
 
   // Buffer to temporary store half chamber data
   UInt_t     *hcBuffer    = new UInt_t[kMaxHcWords];
-  
+
   Bool_t newEvent = kFALSE;  // only for correct readout tree
   Bool_t newSM    = kFALSE;  // new SM flag, for writing SM index words
   Bool_t newStack = kFALSE;  // new stack flag, for writing stack index words
 
+  // Get digits parameter
+  fDigitsParam = digitsManager->GetDigitsParam();
+
   // sect is same as iDDL, so I use only sect here.
   for (Int_t sect = 0; sect < fGeo->Nsector(); sect++) { 
 
     char name[1024];
-    sprintf(name,"TRD_%d.ddl",sect + AliTRDrawOldStream::kDDLOffset);
+    sprintf(name,"TRD_%d.ddl",sect + AliTRDrawStreamBase::kDDLOffset);
 
     AliFstream* of = new AliFstream(name);
 
@@ -184,61 +200,64 @@ Bool_t AliTRDrawData::Digits2Raw(AliTRDdigitsManager *digitsManager)
     Int_t npayloadbyte = 0;
 
 
-    if ( fgRawFormatVersion == 0 ){
+   if ( fgRawFormatVersion == 0 ){
     // GTU common data header (5x4 bytes per super module, shows link mask)
     for( Int_t stack = 0; stack < fGeo->Nstack(); stack++ ) {
-      UInt_t gtuCdh = (UInt_t)(0xe << 28);
-      for( Int_t layer = 0; layer < fGeo->Nlayer(); layer++) {
-    Int_t iDet = fGeo->GetDetector(layer, stack, sect);
+        UInt_t gtuCdh = (UInt_t)(0xe << 28);
+        for( Int_t layer = 0; layer < fGeo->Nlayer(); layer++) {
+            Int_t iDet = fGeo->GetDetector(layer, stack, sect);
 
-    // If chamber status is ok, we assume that the optical link is also OK.
-        // This is shown in the GTU link mask.
-    if ( AliTRDcalibDB::Instance()->GetChamberStatus(iDet) )
-      gtuCdh = gtuCdh | (3 << (2*layer));
-      }
-      of->WriteBuffer((char *) (& gtuCdh), sizeof(gtuCdh));
-      npayloadbyte += 4;
-    }
+            // If chamber status is ok, we assume that the optical link is also OK.
+            // This is shown in the GTU link mask.
+            if ( AliTRDcalibDB::Instance()->GetChamberStatus(iDet) )
+                gtuCdh = gtuCdh | (3 << (2*layer));
+        }
+        of->WriteBuffer((char *) (& gtuCdh), sizeof(gtuCdh));
+        npayloadbyte += 4;
     }
+   }
 
 
     // check the existance of the data
     // SM index word and Stack index word
    if ( fgRawFormatVersion == 1 ){
-    UInt_t *iwbuffer = new UInt_t[42]; // index word buffer; max 42 = 2 SM headers + 5*8 stack headers
+    UInt_t *iwbuffer = new UInt_t[109]; // index word buffer; max 109 = 2 SM headers + 67 dummy headers + 5*8 stack headers
     Int_t nheader = 0;
-    UInt_t StackMask = 0x0;
-    Bool_t StackHasData = kFALSE;
-    Bool_t SMHasData = kFALSE;
-    iwbuffer[nheader++] = 0x0001a020;   // SM index words 
+    UInt_t bStackMask = 0x0;
+    Bool_t bStackHasData = kFALSE;
+    Bool_t bSMHasData = kFALSE;
+
+    //iwbuffer[nheader++] = 0x0001a020;   // SM index words 
+    iwbuffer[nheader++] = 0x0044a020;   // SM index words | additional SM header:48 = 1 SM header + 47 dummy words(for future use)
     iwbuffer[nheader++] = 0x10404071;   // SM header
+    for ( Int_t i=0; i<66; i++ ) iwbuffer[nheader++] = 0x00000000;  // dummy words 
+    iwbuffer[nheader++] = 0x10000000;   // end of dummy words
 
     for ( Int_t stack= 0; stack < fGeo->Nstack(); stack++) {
-        UInt_t LinkMask = 0x0;
+        UInt_t linkMask = 0x0;
         for( Int_t layer = 0; layer < fGeo->Nlayer(); layer++) {
             Int_t iDet = fGeo->GetDetector(layer,stack,sect);
                        AliTRDarrayADC *digits = (AliTRDarrayADC *) digitsManager->GetDigits(iDet);
             if ( digits->HasData() ) {
-                StackMask = StackMask | ( 1 << stack ); // active stack mask for new stack
-                LinkMask = LinkMask | ( 3 << (2*layer) );    // 3 = 0011
-                StackHasData = kTRUE;
-                SMHasData = kTRUE;
+                bStackMask = bStackMask | ( 1 << stack ); // active stack mask for new stack
+                linkMask = linkMask | ( 3 << (2*layer) );    // 3 = 0011
+                bStackHasData = kTRUE;
+                bSMHasData = kTRUE;
             } // has data
         } // loop over layer
 
-        if ( fgDataSuppressionLevel==0 || StackHasData ){
-        //if ( StackHasData ){
-            iwbuffer[nheader++] = 0x0007a000 | LinkMask;    // stack index word + link masks
-            //if (fgDataSuppressionLevel==0) iwbuffer[nheader-1] = 0x0007afff;  // no suppression
+        if ( fgDataSuppressionLevel==0 || bStackHasData ){
+            iwbuffer[nheader++] = 0x0007a000 | linkMask;    // stack index word + link masks
+            if (fgDataSuppressionLevel==0) iwbuffer[nheader-1] = 0x0007afff;  // no suppression
             iwbuffer[nheader++] = 0x04045b01;               // stack header
             for (Int_t i=0;i<6;i++) iwbuffer[nheader++] = 0x00000000; // 6 dummy words
-            StackHasData = kFALSE;
+            bStackHasData = kFALSE;
         }
     } // loop over stack
 
-    if ( fgDataSuppressionLevel==0 || SMHasData ){
-        iwbuffer[0] = iwbuffer[0] | StackMask;  // add stack masks to SM index word
-        if (fgDataSuppressionLevel==0) iwbuffer[0] = 0x0001a03f;    // no suppression
+    if ( fgDataSuppressionLevel==0 || bSMHasData ){
+        iwbuffer[0] = iwbuffer[0] | bStackMask;  // add stack masks to SM index word
+        if (fgDataSuppressionLevel==0) iwbuffer[0] = 0x0044a03f;    // no suppression : all stacks are active
         of->WriteBuffer((char *) iwbuffer, nheader*4);
         AliDebug(11, Form("SM %d index word: %08x", iwbuffer[0]));
         AliDebug(11, Form("SM %d header: %08x", iwbuffer[1]));
@@ -321,7 +340,8 @@ Bool_t AliTRDrawData::Digits2Raw(AliTRDdigitsManager *digitsManager)
         hcwords = ProduceHcData(digits,1,iDet,hcBuffer,kMaxHcWords,newEvent,newSM);
         of->WriteBuffer((char *) hcBuffer, hcwords*4);
         npayloadbyte += hcwords*4;
-       } else {
+       } else {   // in case of no-suppression or NZS
+/*        
         hcBuffer[hcwords++] = fgkEndOfTrackletMarker;
         hcBuffer[hcwords++] = fgkEndOfTrackletMarker;
         hcBuffer[hcwords++] = (1<<31) | (0<<24) | (0<<17) | (1<<14) | (sect<<9) | (layer<<6) | (stack<<3) | (0<<2) | 1;
@@ -340,9 +360,14 @@ Bool_t AliTRDrawData::Digits2Raw(AliTRDdigitsManager *digitsManager)
         hcBuffer[hcwords++] = kEndofrawdatamarker;
         hcBuffer[hcwords++] = kEndofrawdatamarker;
         hcBuffer[hcwords++] = kEndofrawdatamarker;
+*/        
+        hcwords = ProduceHcDataNoSuppression(0,iDet,hcBuffer,kMaxHcWords); // side 0
+        of->WriteBuffer((char *) hcBuffer, hcwords*4);
         npayloadbyte += hcwords*4;
 
+        hcwords = ProduceHcDataNoSuppression(1,iDet,hcBuffer,kMaxHcWords); // side 1
         of->WriteBuffer((char *) hcBuffer, hcwords*4);
+        npayloadbyte += hcwords*4;
        }
     }
 
@@ -357,7 +382,7 @@ Bool_t AliTRDrawData::Digits2Raw(AliTRDdigitsManager *digitsManager)
     of->Seekp(hpos);         // Rewind to header position
     of->WriteBuffer((char *) (& header), sizeof(header));
     delete of;
-  }
+  } // loop over sector(SM)
 
   delete [] hcBuffer;
 
@@ -494,7 +519,7 @@ Int_t AliTRDrawData::ProduceHcData(AliTRDarrayADC *digits, Int_t side, Int_t det
        Int_t         sect = fGeo->GetSector( det );  // Sector (=iDDL)
        const Int_t kCtype = fGeo->GetStack(det) == 2 ? 0 : 1;                       // Chamber type (0:C0, 1:C1)
 
-       Bool_t tracklet_on = fFee->GetTracklet();     // tracklet simulation active?
+       Bool_t trackletOn = fFee->GetTracklet();     // tracklet simulation active?
 
        AliDebug(1,Form("Producing raw data for sect=%d layer=%d stack=%d side=%d",sect,layer,stack,side));
         
@@ -503,7 +528,7 @@ Int_t AliTRDrawData::ProduceHcData(AliTRDarrayADC *digits, Int_t side, Int_t det
        UInt_t *tempBuffer = buf; // tempBuffer used to write ADC data
                                  // different in case of tracklet writing
        
-       if (tracklet_on) {
+       if (trackletOn) {
          tempBuffer = new UInt_t[maxSize];
           tempnw = new Int_t(0);
           tempof = new Int_t(0);
@@ -525,7 +550,7 @@ Int_t AliTRDrawData::ProduceHcData(AliTRDarrayADC *digits, Int_t side, Int_t det
             
             mcm->Init(det, iRob, iMcm);
             mcm->SetData(digits);     // no filtering done here (already done in digitizer)
-           if (tracklet_on) {
+           if (trackletOn) {
              mcm->Tracklet();
               Int_t tempNw = mcm->ProduceTrackletStream(&buf[nw], maxSize - nw);
              if(  tempNw < 0 ) {
@@ -554,7 +579,7 @@ Int_t AliTRDrawData::ProduceHcData(AliTRDarrayADC *digits, Int_t side, Int_t det
 
 
        // in case of tracklet writing copy temp data to final buffer
-       if (tracklet_on) {
+       if (trackletOn) {
          if (nw + *tempnw < maxSize) {
            memcpy(&buf[nw], tempBuffer, *tempnw * sizeof(UInt_t));
            nw += *tempnw;
@@ -566,15 +591,15 @@ Int_t AliTRDrawData::ProduceHcData(AliTRDarrayADC *digits, Int_t side, Int_t det
 
        // Write end of raw data marker
        if (nw+3 < maxSize) {
-          buf[nw++] = 0x00000000; // fFee->GetRawDataEndmarker(); 
-          buf[nw++] = 0x00000000; // fFee->GetRawDataEndmarker(); 
-          buf[nw++] = 0x00000000; // fFee->GetRawDataEndmarker(); 
-          buf[nw++] = 0x00000000; // fFee->GetRawDataEndmarker(); 
+          buf[nw++] = fgkEndOfDataMarker; // fFee->GetRawDataEndmarker(); 
+          buf[nw++] = fgkEndOfDataMarker; // fFee->GetRawDataEndmarker(); 
+          //buf[nw++] = fgkEndOfDataMarker; // fFee->GetRawDataEndmarker(); 
+          //buf[nw++] = fgkEndOfDataMarker; // fFee->GetRawDataEndmarker(); 
        } else {
           of++;
        }
        
-        if (tracklet_on) {
+        if (trackletOn) {
           delete [] tempBuffer;
           delete tempof;
           delete tempnw;
@@ -587,6 +612,69 @@ Int_t AliTRDrawData::ProduceHcData(AliTRDarrayADC *digits, Int_t side, Int_t det
        return nw;
 }
 
+//_____________________________________________________________________________
+Int_t AliTRDrawData::ProduceHcDataNoSuppression(Int_t side, Int_t det, UInt_t *buf, Int_t maxSize){
+
+    // This function generates below words for no-suppression option(same as real data format) 
+    //   1. end of tracklet marker
+    //   2. HC index word, HC header
+    //   3. MCM header, ADC mask
+    //   4. end of data marker
+    //
+
+    Int_t   nw = 0;         // number of written words
+    Int_t   of = 0;         // number of overflowed words
+    UInt_t  x  = 0;         // word buffer
+    //UInt_t *tempBuffer  = buf;   // temp buffer
+    Int_t  *tempnw      = &nw;   // temp number of written words
+    Int_t  *tempof      = &of;   // temp number of overflowed words
+
+    const Int_t kCtype = fGeo->GetStack(det) == 2 ? 0 : 1;   // Chamber type (0:C0, 1:C1)
+
+    WriteIntermediateWordsV2(buf, *tempnw, *tempof, maxSize, det, side);  // end of tracklet marker and HC headers
+
+    for (Int_t iRobRow = 0; iRobRow <= (kCtype + 3)-1; iRobRow++ ) {    // ROB number should be increasing
+        Int_t iRob = iRobRow * 2 + side;  // ROB position
+
+        for (Int_t iMcmRB = 0; iMcmRB < fGeo->MCMmax(); iMcmRB++ ) {    // MCM on ROB
+            Int_t iMcm = 16 - 4*(iMcmRB/4 + 1) + (iMcmRB%4);    // MCM position
+
+            if ( nw+2 < maxSize ){
+                x = 0;
+                x = (1<<31) | (iRob << 28) | (iMcm << 24) | ((fEventCounter % 0x100000) << 4) | 0xC;    // MCM header
+                buf[nw++] = x;
+
+                x = 0;
+                // Produce ADC mask : nncc cccm mmmm mmmm mmmm mmmm mmmm 1100
+                //                n : unused , c : ADC count, m : selected ADCs , where ccccc are inverted
+                x = x | (1 << 30) | (31 << 25) | 0xC;   // 11111 = 31
+                buf[nw++] = x;
+            } else {
+                of++;
+            }
+
+        }   // loop over MCM
+    }   // loop over ROB
+
+       // Write end of raw data marker
+       if (nw+1 < maxSize) {
+          buf[nw++] = fgkEndOfDataMarker; // fFee->GetRawDataEndmarker(); 
+          buf[nw++] = fgkEndOfDataMarker; // fFee->GetRawDataEndmarker(); 
+          //buf[nw++] = fgkEndOfDataMarker; // fFee->GetRawDataEndmarker(); 
+          //buf[nw++] = fgkEndOfDataMarker; // fFee->GetRawDataEndmarker(); 
+       } else {
+          of++;
+       }
+
+    if ( of != 0 ){     // if there is overflow
+        AliError("Buffer overflow. Data is truncated. Please increase buffer size and recompile.");
+    }
+
+    AliDebug(1, Form("Number of written words in this HC is %d",nw));
+
+    return nw;
+}
+
 //_____________________________________________________________________________
 Int_t AliTRDrawData::ProduceHcDataV1andV2(AliTRDarrayADC *digits, Int_t side
                                         , Int_t det, UInt_t *buf, Int_t maxSize)
@@ -617,7 +705,8 @@ Int_t AliTRDrawData::ProduceHcDataV1andV2(AliTRDarrayADC *digits, Int_t side
   Int_t         sect = fGeo->GetSector( det );  // Sector (=iDDL)
   Int_t         nRow = fGeo->GetRowMax( layer, stack, sect );
   Int_t         nCol = fGeo->GetColMax( layer );
-  const Int_t kNTBin = AliTRDcalibDB::Instance()->GetNumberOfTimeBins();
+  const Int_t kNTBin = fDigitsParam->GetNTimeBins(det); 
+
   Int_t       kCtype = 0;                       // Chamber type (0:C0, 1:C1)
   Int_t          iEv = 0xA;                     // Event ID. Now fixed to 10, how do I get event id?
   UInt_t           x = 0;                       // General used number
@@ -788,13 +877,13 @@ Int_t AliTRDrawData::ProduceHcDataV3(AliTRDarrayADC *digits, Int_t side , Int_t
   Int_t         sect = fGeo->GetSector( det );  // Sector (=iDDL)
   Int_t         nRow = fGeo->GetRowMax( layer, stack, sect );
   Int_t         nCol = fGeo->GetColMax( layer );
-  const Int_t kNTBin = AliTRDcalibDB::Instance()->GetNumberOfTimeBins();
+  const Int_t kNTBin = fDigitsParam->GetNTimeBins(det);
   Int_t       kCtype = 0;                       // Chamber type (0:C0, 1:C1)
   //Int_t          iEv = 0xA;                     // Event ID. Now fixed to 10, how do I get event id?
 
  
 
-  Bool_t tracklet_on = fFee->GetTracklet();     // **new**
+  Bool_t trackletOn = fFee->GetTracklet();     // **new**
 
   // Check the nCol and nRow.
   if ((nCol == 144) && 
@@ -816,7 +905,7 @@ Int_t AliTRDrawData::ProduceHcDataV3(AliTRDarrayADC *digits, Int_t side , Int_t
   // raw-data in one go; if tracklet-processing is enabled, first all tracklet-words of a half-chamber have to be processed before the
   // additional words (tracklet-endmarker,headers,...)are written. Raw-data is written in a second loop;
   
-  if (!tracklet_on) {
+  if (!trackletOn) {
       WriteIntermediateWords(buf,nw,of,maxSize,det,side); 
   }
   
@@ -856,7 +945,7 @@ Int_t AliTRDrawData::ProduceHcDataV3(AliTRDarrayADC *digits, Int_t side , Int_t
 //jkl  mcm[entry]->GeneratefZSM1Dim();
 //jkl  mcm[entry]->RestoreZeros();
 
-       if (tracklet_on) {
+       if (trackletOn) {
            mcm[entry]->Tracklet(); 
            Int_t tempNw =  mcm[entry]->ProduceTrackletStream( &buf[nw], maxSize - nw );
            //Int_t tempNw = 0;
@@ -891,7 +980,7 @@ Int_t AliTRDrawData::ProduceHcDataV3(AliTRDarrayADC *digits, Int_t side , Int_t
   }
 
   // if tracklets are switched on, raw-data can be written only after all tracklets
-  if (tracklet_on) {
+  if (trackletOn) {
       WriteIntermediateWords(buf,nw,of,maxSize,det,side); 
   
   
@@ -945,6 +1034,8 @@ AliTRDdigitsManager *AliTRDrawData::Raw2Digits(AliRawReader *rawReader)
   // Vx of the raw data reading
   //
 
+  rawReader->Select("TRD"); //[mj]
+
   AliTRDarrayADC *digits = 0;
   AliTRDarrayDictionary *track0 = 0;
   AliTRDarrayDictionary *track1 = 0;
@@ -955,6 +1046,15 @@ AliTRDdigitsManager *AliTRDrawData::Raw2Digits(AliRawReader *rawReader)
   AliTRDdigitsManager* digitsManager = new AliTRDdigitsManager();
   digitsManager->CreateArrays();
 
+  if (!fTrackletContainer) {
+  //if (!fTrackletContainer && ( fReconstructor->IsWritingTracklets() || fReconstructor->IsProcessingTracklets() )) {
+    // maximum tracklets for one HC
+    const Int_t kTrackletChmb=256;
+    fTrackletContainer = new UInt_t *[2];
+    fTrackletContainer[0] = new UInt_t[kTrackletChmb];
+    fTrackletContainer[1] = new UInt_t[kTrackletChmb];
+  }
+
   AliTRDrawStreamBase *pinput = AliTRDrawStreamBase::GetRawStream(rawReader);
   AliTRDrawStreamBase &input = *pinput;
   input.SetRawVersion( fFee->GetRAWversion() ); //<= ADDED by MinJung
@@ -966,7 +1066,12 @@ AliTRDdigitsManager *AliTRDrawData::Raw2Digits(AliRawReader *rawReader)
 
   while (det >= 0)
     {
-      det = input.NextChamber(digitsManager);
+      //det = input.NextChamber(digitsManager);
+      det = input.NextChamber(digitsManager,fTrackletContainer);
+
+    //if (!fReconstructor->IsWritingTracklets()) continue;
+    if (*(fTrackletContainer[0]) > 0 || *(fTrackletContainer[1]) > 0) WriteTracklets(det);
+
       if (det >= 0)
        {
          // get...
@@ -982,21 +1087,30 @@ AliTRDdigitsManager *AliTRDrawData::Raw2Digits(AliRawReader *rawReader)
        }
     }
 
+  if (fTrackletContainer){
+    delete [] fTrackletContainer[0];
+    delete [] fTrackletContainer[1];
+    delete [] fTrackletContainer;
+    fTrackletContainer = NULL;
+  }
+
   delete pinput;
   pinput = NULL;
 
   return digitsManager;
 }
 
-
 //_____________________________________________________________________________
 void AliTRDrawData::WriteIntermediateWords(UInt_t* buf, Int_t& nw, Int_t& of, const Int_t& maxSize, const Int_t& det, const Int_t& side) {
+    //
+    // write half-chamber headers 
+    //
     
     Int_t        layer = fGeo->GetLayer( det );   // Layer
     Int_t        stack = fGeo->GetStack( det );   // Stack
     Int_t         sect = fGeo->GetSector( det );  // Sector (=iDDL)
     Int_t           rv = fFee->GetRAWversion();
-    const Int_t kNTBin = AliTRDcalibDB::Instance()->GetNumberOfTimeBins();
+    const Int_t kNTBin = fDigitsParam->GetNTimeBins(det);
     UInt_t           x = 0;
 
     // Write end of tracklet marker
@@ -1049,13 +1163,17 @@ void AliTRDrawData::WriteIntermediateWords(UInt_t* buf, Int_t& nw, Int_t& of, co
 
 //_____________________________________________________________________________
 void AliTRDrawData::WriteIntermediateWordsV2(UInt_t* buf, Int_t& nw, Int_t& of, const Int_t& maxSize, const Int_t& det, const Int_t& side) {
+    // 
+    // write tracklet end marker(0x10001000) 
+    // and half chamber headers(H[0] and H[1])
+    //
     
     Int_t        layer = fGeo->GetLayer( det );   // Layer
     Int_t        stack = fGeo->GetStack( det );   // Stack
     Int_t         sect = fGeo->GetSector( det );  // Sector (=iDDL)
     Int_t           rv = fFee->GetRAWversion();
-    const Int_t kNTBin = AliTRDcalibDB::Instance()->GetNumberOfTimeBins();
-       Bool_t tracklet_on = fFee->GetTracklet();
+    const Int_t kNTBin = fDigitsParam->GetNTimeBins(det);
+    Bool_t  trackletOn = fFee->GetTracklet();
     UInt_t           x = 0;
 
     // Write end of tracklet marker
@@ -1081,11 +1199,11 @@ void AliTRDrawData::WriteIntermediateWordsV2(UInt_t* buf, Int_t& nw, Int_t& of,
        Int_t majorv = 0;       // The major version number 
     Int_t minorv = 0;  // The minor version number
     Int_t add    = 1;  // The number of additional header words to follow : now 1, previous 2
-       Int_t TP         = 0;   // test pattern (default=0)
-       Int_t ZS         = (rv==3) ? 1 : 0;                     // zero suppression
-       Int_t DT         = (tracklet_on) ? 0 : 1;       // disable tracklet 
+       Int_t tp         = 0;   // test pattern (default=0)
+       Int_t zs         = (rv==3) ? 1 : 0;                     // zero suppression
+       Int_t dt         = (trackletOn) ? 0 : 1;        // disable tracklet 
 
-       majorv = (TP<<6) | (ZS<<5) | (DT<<4) | 1;       // major version
+       majorv = (tp<<6) | (zs<<5) | (dt<<4) | 1;       // major version
 
     x = (1<<31) | (majorv<<24) | (minorv<<17) | (add<<14) | (sect<<9) | (layer<<6) | (stack<<3) | (side<<2) | 1;
     if (nw < maxSize) buf[nw++] = x; else of++;
@@ -1105,94 +1223,89 @@ void AliTRDrawData::WriteIntermediateWordsV2(UInt_t* buf, Int_t& nw, Int_t& of,
 }
 
 //_____________________________________________________________________________
-AliTRDdigitsManager *AliTRDrawData::Raw2DigitsOLD(AliRawReader *rawReader)
+Bool_t AliTRDrawData::WriteTracklets(Int_t det)
 {
   //
-  // Vx of the raw data reading
+  // Write the raw data tracklets into seperate file
   //
 
-  AliTRDarrayADC *digits = 0;
-  AliTRDarrayDictionary *track0 = 0;
-  AliTRDarrayDictionary *track1 = 0;
-  AliTRDarrayDictionary *track2 = 0; 
+  UInt_t **leaves = new UInt_t *[2];
+  for (Int_t i=0; i<2 ;i++){
+    leaves[i] = new UInt_t[258];
+    leaves[i][0] = det; // det
+    leaves[i][1] = i;   // side
+    memcpy(leaves[i]+2, fTrackletContainer[i], sizeof(UInt_t) * 256);
+  }
 
-  AliTRDSignalIndex *indexes = 0;
-  // Create the digits manager
-  AliTRDdigitsManager* digitsManager = new AliTRDdigitsManager();
-  digitsManager->CreateArrays();
+  if (!fTrackletTree){
+    AliDataLoader *dl = fRunLoader->GetLoader("TRDLoader")->GetDataLoader("tracklets");
+    dl->MakeTree();
+    fTrackletTree = dl->Tree();
+  }
 
-  AliTRDrawOldStream input(rawReader);
-  input.SetRawVersion( fFee->GetRAWversion() );
-  input.Init();
+  TBranch *trkbranch = fTrackletTree->GetBranch("trkbranch");
+  if (!trkbranch) {
+    trkbranch = fTrackletTree->Branch("trkbranch",leaves[0],"det/i:side/i:tracklets[256]/i");
+  }
 
-  AliInfo(Form("Stream version: %s", input.IsA()->GetName()));
+  for (Int_t i=0; i<2; i++){
+    if (leaves[i][2]>0) {
+      trkbranch->SetAddress(leaves[i]);
+      fTrackletTree->Fill();
+    }
+  }
 
-  // Loop through the digits
-  Int_t lastdet = -1;
-  Int_t det    = 0;
-  Int_t it = 0;
-  while (input.Next()) {
+  AliDataLoader *dl = fRunLoader->GetLoader("TRDLoader")->GetDataLoader("tracklets");
+  dl->WriteData("OVERWRITE");
+  //dl->Unload();
+  delete [] leaves;
 
-      det    = input.GetDet();
+  return kTRUE;
 
-      if (det != lastdet) { // If new detector found
-       
-         lastdet = det;
+}
 
-         if (digits) digits->Compress();
-         if (track0) track0->Compress();       
-         if (track1) track1->Compress();       
-         if (track2) track2->Compress();
-       
-         // Add a container for the digits of this detector
-         digits = (AliTRDarrayADC *) digitsManager->GetDigits(det);
-         track0 = (AliTRDarrayDictionary *) digitsManager->GetDictionary(det,0);
-         track1 = (AliTRDarrayDictionary *) digitsManager->GetDictionary(det,1);
-         track2 = (AliTRDarrayDictionary *) digitsManager->GetDictionary(det,2);
+//_____________________________________________________________________________
+Bool_t AliTRDrawData::OpenOutput()
+{
+  //
+  // Connect the output tree
+  //
 
-         // Allocate memory space for the digits buffer
-         if (digits->GetNtime() == 0) 
-           {
-             digits->Allocate(input.GetMaxRow(),input.GetMaxCol(), input.GetNumberOfTimeBins());
-             track0->Allocate(input.GetMaxRow(),input.GetMaxCol(), input.GetNumberOfTimeBins());
-             track1->Allocate(input.GetMaxRow(),input.GetMaxCol(), input.GetNumberOfTimeBins());
-             track2->Allocate(input.GetMaxRow(),input.GetMaxCol(), input.GetNumberOfTimeBins());
-           }
+  // tracklet writing
+  if (1){
+  //if (fReconstructor->IsWritingTracklets()){
+    TString evfoldname = AliConfig::GetDefaultEventFolderName();
+    fRunLoader         = AliRunLoader::GetRunLoader(evfoldname);
 
-         indexes = digitsManager->GetIndexes(det);
-         indexes->SetSM(input.GetSM());
-         indexes->SetStack(input.GetStack());
-         indexes->SetLayer(input.GetLayer());
-         indexes->SetDetNumber(det);
-         if (indexes->IsAllocated() == kFALSE)
-           indexes->Allocate(input.GetMaxRow(), input.GetMaxCol(), input.GetNumberOfTimeBins());
-       }
-    
-      // 3 timebin data are stored per word
-      for (it = 0; it < 3; it++)
-       {
-         if ( input.GetTimeBin() + it < input.GetNumberOfTimeBins() )
-           {
-             if (input.GetSignals()[it] > 0)
-               {
-                 digits->SetData(input.GetRow(), input.GetCol(),input.GetTimeBin() + it, input.GetSignals()[it]);
-
-                 indexes->AddIndexRC(input.GetRow(), input.GetCol());
-                 track0->SetData(input.GetRow(), input.GetCol(), input.GetTimeBin() + it, 0);
-                 track1->SetData(input.GetRow(), input.GetCol(), input.GetTimeBin() + it, 0);
-                 track2->SetData(input.GetRow(), input.GetCol(), input.GetTimeBin() + it, 0);
-               }
-           }
-       }
+    if (!fRunLoader) {
+      fRunLoader = AliRunLoader::Open("galice.root");
+    }
+    if (!fRunLoader) {
+      AliError(Form("Can not open session for file galice.root."));
+      return kFALSE;
+    }
+
+    UInt_t **leaves = new UInt_t *[2];
+    AliDataLoader *dl = fRunLoader->GetLoader("TRDLoader")->GetDataLoader("tracklets");
+    if (!dl) {
+      AliError("Could not get the tracklets data loader!");
+      dl = new AliDataLoader("TRD.Tracklets.root","tracklets", "tracklets");
+      fRunLoader->GetLoader("TRDLoader")->AddDataLoader(dl);
+    }
+    fTrackletTree = dl->Tree();
+    if (!fTrackletTree)
+      {
+       dl->MakeTree();
+       fTrackletTree = dl->Tree();
+      }
+    TBranch *trkbranch = fTrackletTree->GetBranch("trkbranch");
+    if (!trkbranch)
+      fTrackletTree->Branch("trkbranch",leaves[0],"det/i:side/i:tracklets[256]/i");
   }
+  return kTRUE;
 
-  if (digits) digits->Compress();
-  if (track0) track0->Compress();        
-  if (track1) track1->Compress();       
-  if (track2) track2->Compress();
+}
 
-  return digitsManager;
 
-}