]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - HLT/TPCLib/HWCFemulator/AliHLTTPCHWCFSupport.cxx
ALIROOT-5433 Transition to CDHv3 in HLT
[u/mrichter/AliRoot.git] / HLT / TPCLib / HWCFemulator / AliHLTTPCHWCFSupport.cxx
index 89bbb7b61571552eaa9378e8db61829cbfce2ed9..7264d1e00e6ed48562a1b9d3e95fe68e6d050a5e 100644 (file)
@@ -1,3 +1,4 @@
+// $Id$
 //****************************************************************************
 //* This file is property of and copyright by the ALICE HLT Project          * 
 //* ALICE Experiment at CERN, All rights reserved.                           *
 #include "AliHLTTPCDigitReaderUnpacked.h"
 #include "AliHLTTPCTransform.h"
 #include "AliHLTTPCDefinitions.h"
-#include "AliRawDataHeader.h"
+#include "AliHLTCDHWrapper.h"
 #include "AliHLTTPCHWCFEmulator.h"
-
-#if __GNUC__>= 3
-using namespace std;
-#endif
+#include "AliTPCcalibDB.h"
+#include "AliTPCCalPad.h"
+#include "AliTPCCalROC.h"
+#include "TMath.h"
 
 #include <cstdlib>
 #include <algorithm>
 #include <cerrno>
 #include <sys/time.h>
 
+using namespace std;
 
 AliHLTTPCHWCFSupport::AliHLTTPCHWCFSupport()
   : 
@@ -50,16 +52,17 @@ AliHLTTPCHWCFSupport::AliHLTTPCHWCFSupport()
   fEventMCMemory(0)
 {
   // see header file for class documentation
-  for( int i=0; i<fgkNPatches; i++ ) fMapping[i] = 0;
+  for( int i=0; i<fgkNSlices; i++ )
+    for( int j=0; j<fgkNPatches; j++ ) fMapping[i][j] = 0;
 }
 
 
 AliHLTTPCHWCFSupport::~AliHLTTPCHWCFSupport()
 {
   // see header file for class documentation
-  for( int i=0; i<fgkNPatches; i++ ) delete[] fMapping[i];
-  delete[] fEventMemory;
-  delete[] fEventMCMemory;
+  for( int i=0; i<fgkNSlices; i++ )
+    for( int j=0; j<fgkNPatches; j++ ) delete[] fMapping[i][j];
+  ReleaseEventMemory(); 
 }
 
 AliHLTTPCHWCFSupport::AliHLTTPCHWCFSupport(const AliHLTTPCHWCFSupport&)
@@ -77,19 +80,33 @@ AliHLTTPCHWCFSupport& AliHLTTPCHWCFSupport::operator=(const AliHLTTPCHWCFSupport
 }
 
 
-const AliHLTUInt32_t *AliHLTTPCHWCFSupport::GetMapping( int patch )
+void AliHLTTPCHWCFSupport::ReleaseEventMemory()
+{
+  // clean up 
+  if( fEventMemory ) delete[] fEventMemory;
+  if( fEventMCMemory )delete[] fEventMCMemory;
+  fEventMemory = 0;
+  fEventMCMemory = 0;
+}
+
+
+const AliHLTUInt32_t *AliHLTTPCHWCFSupport::GetMapping( int slice, int patch )
 { 
   // see header file for class documentation
+  if( slice<0 || slice>=fgkNSlices ){
+    HLTFatal("Wrong slice number %d, no mapping is provided.", slice);
+    return 0;
+  }
   if( patch<0 || patch>= fgkNPatches ){
     HLTFatal("Wrong patch number %d, no mapping is provided.", patch);
     return 0;
   }
-  if( !fMapping[patch] ) fMapping[patch] = ReadMapping(patch);
-  return fMapping[patch];
+  if( !fMapping[slice][patch] ) fMapping[slice][patch] = ReadMapping(slice,patch);
+return fMapping[slice][patch];
 }
 
 
-AliHLTUInt32_t *AliHLTTPCHWCFSupport::ReadMapping( int patch, const char *mappingFileName ) const
+AliHLTUInt32_t *AliHLTTPCHWCFSupport::ReadMapping( int slice, int patch, const char *mappingFileName ) const
 {
   // Create mapping array for one patch 
   // If no mapping file provided, reads from default file
@@ -110,10 +127,38 @@ AliHLTUInt32_t *AliHLTTPCHWCFSupport::ReadMapping( int patch, const char *mappin
   const AliHLTUInt32_t  kBorderFlag = (1 << 14); 
   const AliHLTUInt32_t  kActiveFlag = (1 << 15); 
   
+  if( slice<0 || slice>=fgkNSlices ){
+     HLTFatal("Wrong slice number %d, no mapping is provided.", slice);
+     return 0;
+  }
+
   if( patch<0 || patch>5 ){
      HLTFatal("Wrong patch number %d, no mapping is provided.", patch);
      return 0;
   }
+
+  // AliHLTTPCTransform::GetFirstRow returns first row in scheme A.
+  // We have to transform to scheme B by AliHLTTPCTransform::Slice2Sector.
+
+  UInt_t offsetSchemeB=0;
+  Int_t sector = 0;
+  {
+    Int_t tmp=0;
+    AliHLTTPCTransform::Slice2Sector(slice, AliHLTTPCTransform::GetFirstRow(patch),
+                                    sector, tmp);
+    offsetSchemeB = (UInt_t) tmp;
+  }
+  
+
+  AliTPCcalibDB *calib = AliTPCcalibDB::Instance();  
+  AliTPCCalPad * gainTPC = 0;
+  AliTPCCalROC * gainROC = 0;
+  if( calib ) gainTPC = calib->GetPadGainFactor();
+  if( gainTPC ) gainROC = gainTPC->GetCalROC(sector);  // pad gains per given sector
+  else{      
+    HLTWarning("No TPC gain calibration found");  
+  }
+
   TString filename;
   
   if( mappingFileName ){
@@ -130,16 +175,6 @@ AliHLTUInt32_t *AliHLTTPCHWCFSupport::ReadMapping( int patch, const char *mappin
     return 0;
   }
 
-  // AliHLTTPCTransform::GetFirstRow returns first row in scheme A.
-  // We have to transform to scheme B by AliHLTTPCTransform::Slice2Sector.
-
-  UInt_t offsetSchemeB=0;
-  {
-    Int_t tmp1=0, tmp2 = 0;
-    AliHLTTPCTransform::Slice2Sector(0, AliHLTTPCTransform::GetFirstRow(patch),
-                                    tmp1, tmp2);
-    offsetSchemeB = (UInt_t) tmp2;
-  }
 
   AliHLTUInt32_t *mapping = 0; 
   AliHLTUInt32_t *rowBranchPadHw = 0;
@@ -211,8 +246,18 @@ AliHLTUInt32_t *AliHLTTPCHWCFSupport::ReadMapping( int patch, const char *mappin
       }
 
       bool active = true; // Currently all channels are always active  
-      AliHLTUInt32_t  gainCalib = (1 << 12); // Gain calib identical for all pads currently
-       
+      //
+
+      AliHLTFloat64_t gain = 1.;
+      if( gainROC ){
+       gain = gainROC->GetValue(offsetSchemeB+row,pad);
+       if( gain>1.e-4 ) gain = 1./gain;
+       else gain = 0;
+      }
+      gain*= (1<<12);
+      AliHLTUInt32_t  gainCalib = TMath::Nint(gain); 
+      if( gainCalib > 0x1FFF ) gainCalib = 0x1FFF;
+
       AliHLTUInt32_t configWord = ( (row & 0x3F) << 8 ) | (pad & 0xFF);
       if ( active ) configWord |= kActiveFlag;
       configWord |= (gainCalib & 0x1FFF) << 16;        
@@ -221,6 +266,7 @@ AliHLTUInt32_t *AliHLTTPCHWCFSupport::ReadMapping( int patch, const char *mappin
        
       AliHLTUInt32_t branch = (hwAdd >> 11) & 0x1;     
       rowBranchPadHw[nRead] = (row<<25) | (branch<<24) | (pad<<16) | hwAdd;
+
       nRead++;
       err = 0;
     }
@@ -271,8 +317,7 @@ int AliHLTTPCHWCFSupport::CreateRawEvent
   // MC labels are provided if possible  
   //
 
-  delete[] fEventMemory;
-  delete[] fEventMCMemory;
+  ReleaseEventMemory();
   
   rawEvent = 0;
   rawEventSize32 = 0;
@@ -295,15 +340,14 @@ int AliHLTTPCHWCFSupport::CreateRawEvent
       // already raw format -> only set the pointers and estimate the size
 
       // read CDH header, estimate size of the data 
-      
-      AliHLTUInt64_t headerSize = sizeof(AliRawDataHeader);                  
+      AliHLTCDHWrapper cdhHeader(block->fPtr);
+
+      AliHLTUInt64_t headerSize = cdhHeader.GetHeaderSize();
  
-      AliRawDataHeader *cdhHeader = reinterpret_cast<AliRawDataHeader*>(block->fPtr);
-      
       AliHLTUInt64_t blockSize = block->fSize; // size of the raw data in bytes      
 
-      if( cdhHeader->fSize!=0xFFFFFFFF ){ // use size information from the header
-       blockSize = cdhHeader->fSize;
+      if( cdhHeader.GetDataSize()!=0xFFFFFFFF ){ // use size information from the header
+       blockSize = cdhHeader.GetDataSize();
        if( blockSize > block->fSize ){
          HLTWarning("%s Could not find a valid DDL header!",str);
          return 0;
@@ -368,6 +412,7 @@ int AliHLTTPCHWCFSupport::CreateRawEvent
       if( !fEventMCMemory ){
        HLTWarning("Not enougth memory: can not allocate %d bytes",totalNMC*sizeof(AliHLTTPCClusterMCLabel));
        delete[] fEventMemory;
+       fEventMemory = 0;
        return 0;
       }
 
@@ -510,10 +555,12 @@ int AliHLTTPCHWCFSupport::CheckRawData( const AliHLTUInt32_t *buffer,
   // The procedure checks consistency of the data
   //
 
-  const unsigned int headerSize32 = 8;
 
   if (!buffer) return 0;
 
+  AliHLTCDHWrapper cdh((void*)buffer);
+  const unsigned int headerSize32 = cdh.GetHeaderSize()/4;
+
   const char *str=Form("slice %d patch %d:", slice, patch);
   
   if( bufferSize32 < headerSize32 ){