]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - TRD/AliTRDrawData.cxx
Change validity range
[u/mrichter/AliRoot.git] / TRD / AliTRDrawData.cxx
index 54aed2d4b5e98c0e415477b937ac9e507f9f5b0e..38f5e9b1a337a62d1ce753da021257a6e596fb46 100644 (file)
@@ -23,6 +23,7 @@
 
 #include <Riostream.h>
 #include <TMath.h>
+#include "TClass.h"
 
 #include "AliDAQ.h"
 #include "AliRawDataHeader.h"
 #include "AliTRDgeometry.h"
 #include "AliTRDdataArrayI.h"
 #include "AliTRDRawStream.h"
+#include "AliTRDRawStreamV2.h"
 
 #include "AliTRDcalibDB.h"
+#include "AliFstream.h"
+
+#include "AliTRDSignalIndex.h"
+
+#include "AliTRDfeeParam.h"
+#include "AliTRDmcmSim.h"
 
 ClassImp(AliTRDrawData)
 
 //_____________________________________________________________________________
 AliTRDrawData::AliTRDrawData()
   :TObject()
-  ,fRawVersion(2)    // Default Raw Data version set here
-  ,fGeo(0)
+  ,fGeo(NULL)
+  ,fFee(NULL)
   ,fNumberOfDDLs(0)
 {
   //
   // Default constructor
   //
-
+  fFee = AliTRDfeeParam::Instance();
+  fNumberOfDDLs = AliDAQ::NumberOfDdls("TRD");
 }
 
 //_____________________________________________________________________________
 AliTRDrawData::AliTRDrawData(const AliTRDrawData &r)
   :TObject(r)
-  ,fRawVersion(2)    // Default Raw Data version set here
-  ,fGeo(0)
+  ,fGeo(NULL)
+  ,fFee(NULL)
   ,fNumberOfDDLs(0)
 {
   //
   // Copy constructor
   //
-
+  fFee = AliTRDfeeParam::Instance();
+  fNumberOfDDLs = AliDAQ::NumberOfDdls("TRD");
 }
 
 //_____________________________________________________________________________
@@ -71,23 +81,6 @@ AliTRDrawData::~AliTRDrawData()
   //
   // Destructor
   //
-
-}
-
-//_____________________________________________________________________________
-Bool_t AliTRDrawData::SetRawVersion(Int_t v)
-{
-  //
-  // Set the raw data version (Currently only version 0, 1 and 2 are available)
-  //
-
-  if ( (v >= 0) && (v <= 2) ) {
-    fRawVersion = v;
-    return kTRUE;
-  }
-
-  return kFALSE;
-
 }
 
 //_____________________________________________________________________________
@@ -101,8 +94,6 @@ Bool_t AliTRDrawData::Digits2Raw(TTree *digitsTree, TTree *tracks )
   // will be supported in higher version simulator.
   //
 
-  fNumberOfDDLs = AliDAQ::NumberOfDdls("TRD");
-
   AliTRDdigitsManager* digitsManager = new AliTRDdigitsManager();
 
   if (!digitsManager->ReadDigits(digitsTree)) {
@@ -126,12 +117,13 @@ Bool_t AliTRDrawData::Digits2Raw(TTree *digitsTree, TTree *tracks )
   }
 
   Int_t retval = kTRUE;
+  Int_t rv     = fFee->GetRAWversion();
 
   // Call appropriate Raw Simulator
-  if ( fRawVersion > 0 && fRawVersion <= 2 ) retval = Digits2Raw(digitsManager); 
+  if ( rv > 0 && rv <= 3 ) retval = Digits2Raw(digitsManager); 
   else {
     retval = kFALSE;
-    AliWarning(Form("Unsupported raw version (fRawVersion=%d).",fRawVersion));
+    AliWarning(Form("Unsupported raw version (%d).", rv));
   }
 
   // Cleanup
@@ -161,16 +153,12 @@ Bool_t AliTRDrawData::Digits2Raw(AliTRDdigitsManager *digitsManager)
     char name[1024];
     sprintf(name,"TRD_%d.ddl",sect + AliTRDRawStream::kDDLOffset);
 
-#ifndef __DECCXX
-    ofstream *of = new ofstream(name, ios::binary);
-#else
-    ofstream *of = new ofstream(name);
-#endif
+    AliFstream* of = new AliFstream(name);
 
     // Write a dummy data header
     AliRawDataHeader  header;  // the event header
-    UInt_t hpos = of->tellp();
-    of->write((char *) (& header), sizeof(header));
+    UInt_t hpos = of->Tellp();
+    of->WriteBuffer((char *) (& header), sizeof(header));
 
     // Reset payload byte size (payload does not include header).
     Int_t npayloadbyte = 0;
@@ -185,7 +173,7 @@ Bool_t AliTRDrawData::Digits2Raw(AliTRDdigitsManager *digitsManager)
        if ( AliTRDcalibDB::Instance()->GetChamberStatus(iDet) )
          GtuCdh = GtuCdh | (3 << (2*plan));
       }
-      of->write((char *) (& GtuCdh), sizeof(GtuCdh));
+      of->WriteBuffer((char *) (& GtuCdh), sizeof(GtuCdh));
       npayloadbyte += 4;
     }
 
@@ -200,31 +188,33 @@ Bool_t AliTRDrawData::Digits2Raw(AliTRDdigitsManager *digitsManager)
         digits->Expand();
 
         Int_t hcwords = 0;
+       Int_t rv = fFee->GetRAWversion();
 
         // Process A side of the chamber
-       if ( fRawVersion >= 1 && fRawVersion <= 2 ) hcwords = ProduceHcDataV1andV2(digits,0,iDet,hc_buffer,kMaxHcWords);
-        of->write((char *) hc_buffer, hcwords*4);
+       if ( rv >= 1 && rv <= 2 ) hcwords = ProduceHcDataV1andV2(digits,0,iDet,hc_buffer,kMaxHcWords);
+       if ( rv == 3 )            hcwords = ProduceHcDataV3     (digits,0,iDet,hc_buffer,kMaxHcWords);
+
+        of->WriteBuffer((char *) hc_buffer, hcwords*4);
         npayloadbyte += hcwords*4;
 
         // Process B side of the chamber
-       if ( fRawVersion >= 1 && fRawVersion <= 2 ) hcwords = ProduceHcDataV1andV2(digits,1,iDet,hc_buffer,kMaxHcWords);
-        of->write((char *) hc_buffer, hcwords*4);
-        npayloadbyte += hcwords*4;
+       if ( rv >= 1 && rv <= 2 ) hcwords = ProduceHcDataV1andV2(digits,1,iDet,hc_buffer,kMaxHcWords);
+       if ( rv >= 3 )            hcwords = ProduceHcDataV3     (digits,1,iDet,hc_buffer,kMaxHcWords);
 
+        of->WriteBuffer((char *) hc_buffer, hcwords*4);
+        npayloadbyte += hcwords*4;
       }
     }
 
     // Complete header
-    header.fSize = UInt_t(of->tellp()) - hpos;
+    header.fSize = UInt_t(of->Tellp()) - hpos;
     header.SetAttribute(0);  // Valid data
-    of->seekp(hpos);         // Rewind to header position
-    of->write((char *) (& header), sizeof(header));
-    of->close();
+    of->Seekp(hpos);         // Rewind to header position
+    of->WriteBuffer((char *) (& header), sizeof(header));
     delete of;
-
   }
 
-  delete hc_buffer;
+  delete [] hc_buffer;
   return kTRUE;
 
 }
@@ -234,8 +224,8 @@ Int_t AliTRDrawData::ProduceHcDataV1andV2(AliTRDdataArrayI *digits, Int_t side
                                         , Int_t det, UInt_t *buf, Int_t maxSize)
 {
   //
-  // This function simulates: 1) SM-I commissiong data Oct. 06 (fRawVersion == 1).
-  //                          2) Full Raw Production Version   (fRawVersion == 2)
+  // This function simulates: 1) SM-I commissiong data Oct. 06 (Raw Version == 1).
+  //                          2) Full Raw Production Version   (Raw Version == 2)
   //
   // Produce half chamber data (= an ORI data) for the given chamber (det) and side (side)
   // where
@@ -263,6 +253,7 @@ Int_t AliTRDrawData::ProduceHcDataV1andV2(AliTRDdataArrayI *digits, Int_t side
   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
+  Int_t          rv = fFee->GetRAWversion();
 
   // Check the nCol and nRow.
   if ((nCol == 144) && 
@@ -289,7 +280,7 @@ Int_t AliTRDrawData::ProduceHcDataV1andV2(AliTRDdataArrayI *digits, Int_t side
   }
 
   // Half Chamber header
-  if      ( fRawVersion == 1 ) {
+  if      ( rv == 1 ) {
     // Now it is the same version as used in SM-I commissioning.
     Int_t  dcs = det+100;      // DCS Serial (in simulation, it is meaningless
     x = (dcs<<20) | (sect<<15) | (plan<<12) | (cham<<9) | (side<<8) | 1;
@@ -300,11 +291,11 @@ Int_t AliTRDrawData::ProduceHcDataV1andV2(AliTRDdataArrayI *digits, Int_t side
       of++;
     }
   } 
-  else if ( fRawVersion == 2 ) {
+  else if ( rv == 2 ) {
     // h[0] (there are 3 HC header)
     Int_t minorv = 0;      // The minor version number
-    Int_t add    = 1;      // The number of additional header words to follow
-    x = (1<<31) | (fRawVersion<<24) | (minorv<<17) | (add<<14) | (sect<<9) | (plan<<6) | (cham<<3) | (side<<2) | 1;
+    Int_t add    = 2;      // The number of additional header words to follow
+    x = (1<<31) | (rv<<24) | (minorv<<17) | (add<<14) | (sect<<9) | (plan<<6) | (cham<<3) | (side<<2) | 1;
     if (nw < maxSize) {
       buf[nw++] = x; 
     }
@@ -357,7 +348,7 @@ Int_t AliTRDrawData::ProduceHcDataV1andV2(AliTRDdataArrayI *digits, Int_t side
 
       // ADC data
       for (Int_t iAdc = 0; iAdc < 21; iAdc++ ) {
-       Int_t padcol = fGeo->GetPadColFromADC(iRob, iMcm, iAdc);
+       Int_t padcol = fFee->GetPadColFromADC(iRob, iMcm, iAdc);
        UInt_t aa = !(iAdc & 1) + 2;
         UInt_t *a = new UInt_t[nTBin+2];
         // 3 timebins are packed into one 32 bits word
@@ -393,7 +384,7 @@ Int_t AliTRDrawData::ProduceHcDataV1andV2(AliTRDdataArrayI *digits, Int_t side
           AliDebug(2,Form("Large RMS (>1.7)  (ROB,MCM,ADC)=(%02d,%02d,%02d), avg=%03.1f, rms=%03.1f"
                          ,iRob,iMcm,iAdc,avg,rms));
        }
-        delete a;
+        delete [] a;
       }
     }
   }
@@ -413,6 +404,147 @@ Int_t AliTRDrawData::ProduceHcDataV1andV2(AliTRDdataArrayI *digits, Int_t side
 
 }
 
+
+//_____________________________________________________________________________
+Int_t AliTRDrawData::ProduceHcDataV3(AliTRDdataArrayI *digits, Int_t side
+                                    , Int_t det, UInt_t *buf, Int_t maxSize)
+{
+  //
+  // This function simulates: Raw Version == 3 (Zero Suppression Prototype)
+  //
+
+  Int_t          nw = 0;                       // Number of written    words
+  Int_t          of = 0;                       // Number of overflowed words
+  Int_t        plan = fGeo->GetPlane( det );   // Plane
+  Int_t        cham = fGeo->GetChamber( det ); // Chamber
+  Int_t        sect = fGeo->GetSector( det );  // Sector (=iDDL)
+  Int_t        nRow = fGeo->GetRowMax( plan, cham, sect );
+  Int_t        nCol = fGeo->GetColMax( plan );
+  const Int_t nTBin = AliTRDcalibDB::Instance()->GetNumberOfTimeBins();
+  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
+  Int_t          rv = fFee->GetRAWversion();
+
+  // Check the nCol and nRow.
+  if ((nCol == 144) && 
+      (nRow == 16 || nRow == 12)) {
+    kCtype = (nRow-12) / 4;
+  } 
+  else {
+    AliError(Form("This type of chamber is not supported (nRow=%d, nCol=%d)."
+                 ,nRow,nCol));
+    return 0;
+  }
+
+  AliDebug(1,Form("Producing raw data for sect=%d plan=%d cham=%d side=%d"
+                 ,sect,plan,cham,side));
+
+  // Tracklet should be processed here but not implemented yet
+
+  // Write end of tracklet marker
+  if (nw < maxSize) {
+    buf[nw++] = kEndoftrackletmarker;
+  } 
+  else {
+    of++;
+  }
+
+  // Half Chamber header
+  // h[0] (there are 3 HC header)
+  Int_t minorv = 0;    // The minor version number
+  Int_t add    = 2;    // The number of additional header words to follow
+  x = (1<<31) | (rv<<24) | (minorv<<17) | (add<<14) | (sect<<9) | (plan<<6) | (cham<<3) | (side<<2) | 1;
+  if (nw < maxSize) {
+    buf[nw++] = x; 
+  }
+  else {
+    of++;
+  }
+  // h[1]
+  Int_t bc_ctr   = 99; // bunch crossing counter. Here it is set to 99 always for no reason
+  Int_t pt_ctr   = 15; // pretrigger counter. Here it is set to 15 always for no reason
+  Int_t pt_phase = 11; // pretrigger phase. Here it is set to 11 always for no reason
+  x = (bc_ctr<<16) | (pt_ctr<<12) | (pt_phase<<8) | ((nTBin-1)<<2) | 1;
+  if (nw < maxSize) {
+    buf[nw++] = x; 
+  }
+  else {
+    of++;
+  }
+  // h[2]
+  Int_t ped_setup       = 1;  // Pedestal filter setup (0:1). Here it is always 1 for no reason
+  Int_t gain_setup      = 1;  // Gain filter setup (0:1). Here it is always 1 for no reason
+  Int_t tail_setup      = 1;  // Tail filter setup (0:1). Here it is always 1 for no reason
+  Int_t xt_setup        = 0;  // Cross talk filter setup (0:1). Here it is always 0 for no reason
+  Int_t nonlin_setup    = 0;  // Nonlinearity filter setup (0:1). Here it is always 0 for no reason
+  Int_t bypass_setup    = 0;  // Filter bypass (for raw data) setup (0:1). Here it is always 0 for no reason
+  Int_t common_additive = 10; // Digital filter common additive (0:63). Here it is always 10 for no reason
+  x = (ped_setup<<31) | (gain_setup<<30) | (tail_setup<<29) | (xt_setup<<28) | (nonlin_setup<<27)
+    | (bypass_setup<<26) | (common_additive<<20) | 1;
+  if (nw < maxSize) {
+    buf[nw++] = x; 
+  }
+  else {
+    of++;
+  }
+
+
+  // Scan for ROB and MCM
+  for (Int_t iRobRow = 0; iRobRow < (kCtype + 3); iRobRow++ ) {
+    Int_t iRob = iRobRow * 2 + side;
+    for (Int_t iMcm = 0; iMcm < fGeo->MCMmax(); iMcm++ ) {
+
+      AliTRDmcmSim *mcm = new AliTRDmcmSim();
+      mcm->Init( det, iRob, iMcm );
+      Int_t padrow = mcm->GetRow();
+
+      // Copy ADC data to MCM simulator
+      for (Int_t iAdc = 0; iAdc < 21; iAdc++ ) {
+       Int_t padcol = mcm->GetCol( iAdc );
+       if ((padcol >=    0) && (padcol <  nCol)) {
+         for (Int_t iT = 0; iT < nTBin; iT++) { 
+           mcm->SetData( iAdc, iT, digits->GetDataUnchecked( padrow, padcol, iT) );
+         } 
+       } else {  // this means it is out of chamber, and masked ADC
+         mcm->SetDataPedestal( iAdc );
+       }
+      }
+      // Simulate process in MCM
+      mcm->Filter();     // Apply filter
+      mcm->ZSMapping();  // Calculate zero suppression mapping
+      //mcm->DumpData( "trdmcmdata.txt", "RFZS" ); // debugging purpose
+
+      // Write MCM data to buffer
+      Int_t temp_nw =  mcm->ProduceRawStream( &buf[nw], maxSize - nw );
+      if( temp_nw < 0 ) {
+       of += temp_nw;
+       nw += maxSize - nw;
+       AliError(Form("Buffer overflow detected. Please increase the buffer size and recompile."));
+      } else {
+       nw += temp_nw;
+      }
+
+      delete mcm;
+
+    }
+  }
+
+  // Write end of raw data marker
+  if (nw < maxSize) {
+    buf[nw++] = kEndofrawdatamarker; 
+  }
+  else {
+    of++;
+  }
+  if (of != 0) {
+    AliError("Buffer overflow. Data is truncated. Please increase buffer size and recompile.");
+  }
+
+  return nw;
+
+}
+
 //_____________________________________________________________________________
 AliTRDdigitsManager *AliTRDrawData::Raw2Digits(AliRawReader *rawReader)
 {
@@ -425,25 +557,27 @@ AliTRDdigitsManager *AliTRDrawData::Raw2Digits(AliRawReader *rawReader)
   AliTRDdataArrayI *track1 = 0;
   AliTRDdataArrayI *track2 = 0; 
 
+  AliTRDSignalIndex *indexes = 0;
   // Create the digits manager
   AliTRDdigitsManager* digitsManager = new AliTRDdigitsManager();
   digitsManager->CreateArrays();
 
-  AliTRDRawStream input(rawReader);
-  input.SetRawVersion( fRawVersion );
+  //AliTRDRawStream input(rawReader);
+  AliTRDRawStreamV2 input(rawReader);
+  input.SetRawVersion( fFee->GetRAWversion() );
   input.Init();
 
+  AliInfo(Form("Stream version: %s", input.IsA()->GetName()));
+
   // Loop through the digits
   Int_t lastdet = -1;
   Int_t det    = 0;
   Int_t it = 0;
-  while (input.Next()) 
-    {
+  while (input.Next()) {
 
       det    = input.GetDet();
 
-      if (det != lastdet) 
-       {       
+      if (det != lastdet) { // If new detector found
        
          lastdet = det;
 
@@ -466,21 +600,35 @@ AliTRDdigitsManager *AliTRDrawData::Raw2Digits(AliRawReader *rawReader)
              track1->Allocate(input.GetMaxRow(),input.GetMaxCol(), input.GetNumberOfTimeBins());
              track2->Allocate(input.GetMaxRow(),input.GetMaxCol(), input.GetNumberOfTimeBins());
            }
+
+         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() )
            {
-             digits->SetDataUnchecked(input.GetRow(), input.GetCol(),
-                                      input.GetTimeBin() + it, input.GetSignals()[it]);
-             track0->SetDataUnchecked(input.GetRow(), input.GetCol(),
-                                      input.GetTimeBin() + it, 0);
-             track1->SetDataUnchecked(input.GetRow(), input.GetCol(),
-                                      input.GetTimeBin() + it, 0);
-             track2->SetDataUnchecked(input.GetRow(), input.GetCol(),
-                                      input.GetTimeBin() + it, 0);
-
+             if (input.GetSignals()[it] > 0)
+               {
+                 digits->SetDataUnchecked(input.GetRow(), input.GetCol(),
+                                          input.GetTimeBin() + it, input.GetSignals()[it]);
+
+                 indexes->AddIndexTBin(input.GetRow(), input.GetCol(),
+                                       input.GetTimeBin() + it);
+                 track0->SetDataUnchecked(input.GetRow(), input.GetCol(),
+                                          input.GetTimeBin() + it, 0);
+                 track1->SetDataUnchecked(input.GetRow(), input.GetCol(),
+                                          input.GetTimeBin() + it, 0);
+                 track2->SetDataUnchecked(input.GetRow(), input.GetCol(),
+                                          input.GetTimeBin() + it, 0);
+               }
            }
        }
   }