]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - HLT/TPCLib/AliHLTTPCFileHandler.cxx
Fixing trigg.class issue
[u/mrichter/AliRoot.git] / HLT / TPCLib / AliHLTTPCFileHandler.cxx
index 94bbe490c3e146a18cb8a88a6372000983dce0cf..032e326fef2fb7bd432a44b485f7ba6df38f7339 100644 (file)
@@ -1,23 +1,53 @@
-// @(#) $Id$
+// $Id$
 // Original: AliHLTFileHandler.cxx,v 1.49 2005/06/23 17:46:55 hristov 
 
-// Author: Uli Frankenfeld <mailto:franken@fi.uib.no>, Anders Vestbo <mailto:vestbo$fi.uib.no>, C. Loizides <mailto:loizides@ikf.uni-frankfurt.de>
-//*-- Copyright &copy ALICE HLT Group 
-
+//**************************************************************************
+//* This file is property of and copyright by the ALICE HLT Project        * 
+//* ALICE Experiment at CERN, All rights reserved.                         *
+//*                                                                        *
+//* Primary Authors: U. Frankenfeld, A. Vestbo, C. Loizides                *
+//*                  Matthias Richter <Matthias.Richter@ift.uib.no>        *
+//*                  for The ALICE HLT Project.                            *
+//*                                                                        *
+//* Permission to use, copy, modify and distribute this software and its   *
+//* documentation strictly for non-commercial purposes is hereby granted   *
+//* without fee, provided that the above copyright notice appears in all   *
+//* copies and that both the copyright notice and this permission notice   *
+//* appear in the supporting documentation. The authors make no claims     *
+//* about the suitability of this software for any purpose. It is          *
+//* provided "as is" without express or implied warranty.                  *
+//**************************************************************************
+
+/// @file   AliHLTTPCFileHandler.cxx
+/// @author U. Frankenfeld, A. Vestbo, C. Loizides, maintained by
+///         Matthias Richter
+/// @date   
+/// @brief  file input for the TPC tracking code before migration to the
+///         HLT component framework
+
+// see below for class documentation
+// or
+// refer to README to build package
+// or
+// visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
+
+#include <cassert>
 #include <TClonesArray.h>
 #include <TSystem.h>
 #include <TMath.h>
 
+#include "AliLoader.h"
 #include <AliRunLoader.h>
 #include <TObject.h>
 #include <TFile.h>
 #include <TTree.h>
 #include <AliTPCParamSR.h>
 #include <AliTPCDigitsArray.h>
-#include <AliTPCClustersArray.h>
-#include <AliTPCcluster.h>
+#include <AliTPCclusterMI.h>
 #include <AliTPCClustersRow.h>
 #include <AliSimDigits.h>
+#include "AliCDBManager.h"
+#include "AliCDBEntry.h"
 
 #include "AliHLTTPCLogging.h"
 #include "AliHLTTPCTransform.h"
 #include "AliHLTTPCSpacePointData.h"
 //#include "AliHLTTPCTrackArray.h"
 #include "AliHLTTPCFileHandler.h"
+#include "AliHLTTPCMapping.h"
+#include "AliHLTAltroEncoder.h"
 
 #if __GNUC__ >= 3
 using namespace std;
 #endif
 
-/** \class AliHLTTPCFileHandler
+/**
 <pre>
 //_____________________________________________________________
 // AliHLTTPCFileHandler
@@ -91,26 +123,6 @@ AliHLTTPCFileHandler::AliHLTTPCFileHandler(Bool_t b)
   if(fUseStaticIndex&&!fgStaticIndexCreated) CleanStaticIndex();
 }
 
-AliHLTTPCFileHandler::AliHLTTPCFileHandler(const AliHLTTPCFileHandler& ref)
-  :
-  fInAli(NULL),
-  fUseRunLoader(kFALSE),
-  fParam(NULL),
-  fDigits(NULL),
-  fDigitsTree(NULL),
-  fMC(NULL),
-  fIndexCreated(kFALSE),
-  fUseStaticIndex(ref.fUseStaticIndex)
-{
-  HLTFatal("copy constructor untested");
-}
-
-AliHLTTPCFileHandler& AliHLTTPCFileHandler::operator=(const AliHLTTPCFileHandler&)
-{ 
-  HLTFatal("assignment operator untested");
-  return *this;
-}
-
 AliHLTTPCFileHandler::~AliHLTTPCFileHandler()
 {
   //Destructor
@@ -139,11 +151,12 @@ Int_t AliHLTTPCFileHandler::SaveStaticIndex(Char_t *prefix,Int_t event)
   // use this static call to store static index after
   if(!fgStaticIndexCreated) return -1;
 
-  Char_t fname[1024];
+  const int fnamelen=1024;
+  Char_t fname[fnamelen];
   if(prefix)
-    sprintf(fname,"%s-%d.txt",prefix,event);
+    snprintf(fname,fnamelen, "%s-%d.txt",prefix,event);
   else
-    sprintf(fname,"TPC.Digits.staticindex-%d.txt",event);
+    snprintf(fname,fnamelen, "TPC.Digits.staticindex-%d.txt",event);
 
   ofstream file(fname,ios::trunc);
   if(!file.good()) return -1;
@@ -166,11 +179,12 @@ Int_t AliHLTTPCFileHandler::LoadStaticIndex(Char_t *prefix,Int_t event)
       CleanStaticIndex();
   }
 
-  Char_t fname[1024];
+  const int fnamelen=1024;
+  Char_t fname[fnamelen];
   if(prefix)
-    sprintf(fname,"%s-%d.txt",prefix,event);
+    snprintf(fname,fnamelen,"%s-%d.txt",prefix,event);
   else
-    sprintf(fname,"TPC.Digits.staticindex-%d.txt",event);
+    snprintf(fname,fnamelen,"TPC.Digits.staticindex-%d.txt",event);
 
   ifstream file(fname);
   if(!file.good()) return -1;
@@ -188,13 +202,9 @@ Int_t AliHLTTPCFileHandler::LoadStaticIndex(Char_t *prefix,Int_t event)
 void AliHLTTPCFileHandler::FreeDigitsTree()
 { 
   //free digits tree
-  if(!fDigitsTree)
-    {
-      LOG(AliHLTTPCLog::kInformational,"AliHLTTPCFileHandler::FreeDigitsTree()","Pointer")
-       <<"Cannot free digitstree, it is not present"<<ENDLOG;
-      return;
-    }
-  delete fDigits;
+  if (fDigits) {
+    delete fDigits;
+  }
   fDigits=0;
   fDigitsTree=0;
 
@@ -244,6 +254,21 @@ void AliHLTTPCFileHandler::CloseMCOutput()
 Bool_t AliHLTTPCFileHandler::SetAliInput()
 { 
   //set ali input
+
+  // fParam is in all cases an external object
+  const char* cdbEntry="TPC/Calib/Parameters";
+  AliCDBManager* pMan=AliCDBManager::Instance();
+  if (pMan) {
+    AliCDBEntry *pEntry = pMan->Get(cdbEntry);
+    if (pEntry==NULL || 
+       pEntry->GetObject()==NULL ||
+       (fParam=dynamic_cast<AliTPCParam*>(pEntry->GetObject()))==NULL) {
+      LOG(AliHLTTPCLog::kWarning,"AliHLTTPCFileHandler::SetAliInput","File")
+       <<"can not load AliTPCParam object from OCDB entry " << cdbEntry <<ENDLOG;
+    }
+  }
+  if (!fParam) {
+  // the old solution until Nov 2008
   fInAli->CdGAFile();
   fParam = (AliTPCParam*)gFile->Get("75x40_100x60_150x60");
   if(!fParam){
@@ -253,6 +278,7 @@ Bool_t AliHLTTPCFileHandler::SetAliInput()
       <<"which might not be what you want!"<<ENDLOG;
     fParam = new AliTPCParamSR;
   }
+  }
   if(!fParam){ 
     LOG(AliHLTTPCLog::kError,"AliHLTTPCFileHandler::SetAliInput","File Open")
       <<"No AliTPCParam "<<AliHLTTPCTransform::GetParamName()<<" in File "<<gFile->GetName()<<ENDLOG;
@@ -277,13 +303,18 @@ Bool_t AliHLTTPCFileHandler::SetAliInput(Char_t *name)
 Bool_t AliHLTTPCFileHandler::SetAliInput(AliRunLoader *runLoader)
 { 
   //set ali input as runloader
-  fInAli=runLoader;
-  fUseRunLoader = kTRUE;
-  if(!fInAli){
+  if(!runLoader){
     LOG(AliHLTTPCLog::kWarning,"AliHLTTPCFileHandler::SetAliInput","File Open")
-    <<"Pointer to AliRunLoader = 0x0 "<<ENDLOG;
+      <<"invalid agument: pointer to AliRunLoader NULL "<<ENDLOG;
     return kFALSE;
   }
+  if (fInAli!=NULL && fInAli!=runLoader) {
+    LOG(AliHLTTPCLog::kError,"AliHLTTPCFileHandler::SetAliInput","File Open")
+    <<"Pointer to AliRunLoader already set"<<ENDLOG;
+    return kFALSE;
+  }
+  fInAli=runLoader;
+  fUseRunLoader = kTRUE;
   return SetAliInput();
 }
 
@@ -333,7 +364,7 @@ Bool_t AliHLTTPCFileHandler::IsDigit(Int_t event)
 }
 
 ///////////////////////////////////////// Digit IO  
-Bool_t AliHLTTPCFileHandler::AliDigits2Binary(Int_t event,Bool_t altro)
+Bool_t AliHLTTPCFileHandler::AliDigits2BinaryFile(Int_t event,Bool_t altro)
 {
   //save alidigits as binary
   Bool_t out = kTRUE;
@@ -343,7 +374,7 @@ Bool_t AliHLTTPCFileHandler::AliDigits2Binary(Int_t event,Bool_t altro)
     data = AliAltroDigits2Memory(nrow,event);
   else
     data = AliDigits2Memory(nrow,event);
-  out = Memory2Binary(nrow,data);
+  out = Memory2BinaryFile(nrow,data);
   Free();
   return out;
 }
@@ -374,6 +405,7 @@ Bool_t AliHLTTPCFileHandler::CreateIndex()
     LOG(AliHLTTPCLog::kInformational,"AliHLTTPCFileHandler::CreateIndex","Index")
       <<"Starting to create index, this can take a while."<<ENDLOG;
 
+    //Int_t lslice,lrow;
     for(Int_t n=0; n<fDigitsTree->GetEntries(); n++) {
       Int_t sector, row;
       Int_t lslice,lrow;
@@ -385,14 +417,21 @@ Bool_t AliHLTTPCFileHandler::CreateIndex()
          <<sector<<" "<<row<<ENDLOG;
        return kFALSE;
       }
+      // this is just to make sure the same array dimensions are
+      // used as in the AliHLTTPCTransform class. The check for
+      // correct bounds is done in AliHLTTPCTransform::Sector2Slice
+      assert(lslice>=0 && lslice<fgkNSlice);
+      assert(lrow>=0 && lrow<fgkNRow);
       if(fIndex[lslice][lrow]==-1) {
        fIndex[lslice][lrow]=n;
       }
     }
+    assert(AliHLTTPCTransform::GetNSlice()==fgkNSlice);
+    assert(AliHLTTPCTransform::GetNRows()==fgkNRow);
     if(fUseStaticIndex) { // create static index
-      for(Int_t i=0;i<AliHLTTPCTransform::GetNSlice();i++){
-       for(Int_t j=0;j<AliHLTTPCTransform::GetNRows();j++)
-         fgStaticIndex[i][j]=fIndex[i][j];
+      for(Int_t islice=0;islice<AliHLTTPCTransform::GetNSlice() && islice<fgkNSlice;islice++){
+       for(Int_t irow=0;irow<AliHLTTPCTransform::GetNRows() && irow<fgkNRow;irow++)
+         fgStaticIndex[islice][irow]=fIndex[islice][irow];
       }
       fgStaticIndexCreated=kTRUE; //remember that index has been created
     }
@@ -401,9 +440,9 @@ Bool_t AliHLTTPCFileHandler::CreateIndex()
     <<"Index successfully created."<<ENDLOG;
 
   } else if(fUseStaticIndex) { //simply copy static index
-    for(Int_t i=0;i<AliHLTTPCTransform::GetNSlice();i++){
-      for(Int_t j=0;j<AliHLTTPCTransform::GetNRows();j++)
-       fIndex[i][j]=fgStaticIndex[i][j];
+    for(Int_t islice=0;islice<AliHLTTPCTransform::GetNSlice() && islice<fgkNSlice;islice++){
+      for(Int_t irow=0;irow<AliHLTTPCTransform::GetNRows() && irow<fgkNRow;irow++)
+       fIndex[islice][irow]=fgStaticIndex[islice][irow];
     }
 
   LOG(AliHLTTPCLog::kInformational,"AliHLTTPCFileHandler::CreateIndex","Index")
@@ -413,9 +452,10 @@ Bool_t AliHLTTPCFileHandler::CreateIndex()
   return kTRUE;
 }
 
-AliHLTTPCDigitRowData * AliHLTTPCFileHandler::AliDigits2Memory(UInt_t & nrow,Int_t event)
+AliHLTTPCDigitRowData * AliHLTTPCFileHandler::AliDigits2Memory(UInt_t & nrow,Int_t event, Byte_t* tgtBuffer, UInt_t *pTgtSize)
 {
-  //Read data from AliROOT file into memory, and store it in the HLT data format.
+  //Read data from AliROOT file into memory, and store it in the HLT data format
+  //in the provided buffer or an allocated buffer.
   //Returns a pointer to the data.
 
   AliHLTTPCDigitRowData *data = 0;
@@ -432,7 +472,6 @@ AliHLTTPCDigitRowData * AliHLTTPCFileHandler::AliDigits2Memory(UInt_t & nrow,Int
 
   UShort_t dig;
   Int_t time,pad,sector,row;
-  Int_t lslice,lrow;
   Int_t nrows=0;
   Int_t ndigitcount=0;
   Int_t entries = (Int_t)fDigitsTree->GetEntries();
@@ -440,26 +479,47 @@ AliHLTTPCDigitRowData * AliHLTTPCFileHandler::AliDigits2Memory(UInt_t & nrow,Int
     LOG(AliHLTTPCLog::kWarning,"AliHLTTPCFileHandler::AliDigits2Memory","ndigits")
       <<"No TPC digits (entries==0)!"<<ENDLOG;
     nrow = (UInt_t)(fRowMax-fRowMin+1);
-    Int_t size = nrow*sizeof(AliHLTTPCDigitRowData);
-    data=(AliHLTTPCDigitRowData*) Allocate(size);
+    UInt_t size = nrow*sizeof(AliHLTTPCDigitRowData);
+    if (tgtBuffer!=NULL && pTgtSize!=NULL && *pTgtSize>0) {
+      if (size<=*pTgtSize) {
+       data=reinterpret_cast<AliHLTTPCDigitRowData*>(tgtBuffer);
+      } else {
+      }
+    } else {
+      data=reinterpret_cast<AliHLTTPCDigitRowData*>(Allocate(size));
+    }
     AliHLTTPCDigitRowData *tempPt = data;
+    if (data) {
+    if (pTgtSize) *pTgtSize=size;
     for(Int_t r=fRowMin;r<=fRowMax;r++){
       tempPt->fRow = r;
       tempPt->fNDigit = 0;
       tempPt++;
     }
+    }
     return data;
   }
 
   Int_t * ndigits = new Int_t[fRowMax+1];
+  memset(ndigits, 0, (fRowMax+1)*sizeof(Int_t));
   Float_t xyz[3];
 
-  for(Int_t r=fRowMin;r<=fRowMax;r++){
+  // The digits of the current event have been indexed: all digits are organized in
+  // rows, all digits of one row are stored in a AliSimDigits object (fDigit) which
+  // are stored in the digit tree.
+  // The index map relates the AliSimDigits objects in the tree to dedicated pad rows
+  // in the TPC
+  // This loop filters the pad rows according to the slice no set via Init
+  assert(fRowMax<fgkNRow);
+  for(Int_t r=fRowMin;r<=fRowMax && r<fgkNRow;r++){
     Int_t n=fIndex[fSlice][r];
-    if(n!=-1){ //data on that row
+    if(n!=-1){ // there is data on that row available
+      Int_t lslice,lrow;
       fDigitsTree->GetEvent(n);
       fParam->AdjustSectorRow(fDigits->GetID(),sector,row);
       AliHLTTPCTransform::Sector2Slice(lslice,lrow,sector,row);
+//       LOG(AliHLTTPCLog::kInformational,"AliHLTTPCFileHandler::AliDigits2Memory","Digits")
+//     << "Sector "<<sector<<" Row " << row << " Slice " << lslice << " lrow " << lrow<<ENDLOG;
 
       if(lrow!=r){
        LOG(AliHLTTPCLog::kError,"AliHLTTPCFileHandler::AliDigits2Memory","Row")
@@ -468,15 +528,18 @@ AliHLTTPCDigitRowData * AliHLTTPCFileHandler::AliDigits2Memory(UInt_t & nrow,Int
       }
 
       ndigits[lrow] = 0;
-      fDigits->First();
-      do {
+      for (bool bHaveData=fDigits->First();
+          bHaveData;
+          bHaveData=fDigits->Next()) {
        time=fDigits->CurrentRow();
        pad=fDigits->CurrentColumn();
        dig = fDigits->GetDigit(time,pad);
        if(dig <= fParam->GetZeroSup()) continue;
        if(dig >= AliHLTTPCTransform::GetADCSat())
          dig = AliHLTTPCTransform::GetADCSat();
-      
+
+       // if we switch to AliTPCTransform, this maybe needs to be 
+       // adjusted as well
        AliHLTTPCTransform::Raw2Local(xyz,sector,row,pad,time);
        //      if(fParam->GetPadRowRadii(sector,row)<230./250.*fabs(xyz[2]))
        //        continue; // why 230???
@@ -484,28 +547,52 @@ AliHLTTPCDigitRowData * AliHLTTPCFileHandler::AliDigits2Memory(UInt_t & nrow,Int
        ndigits[lrow]++; //for this row only
        ndigitcount++;   //total number of digits to be published
 
-      } while (fDigits->Next());
+      }
       //cout << lrow << " " << ndigits[lrow] << " - " << ndigitcount << endl;
     }
-    nrows++;
+    //see comment below//nrows++;
   }
-
-  Int_t size = sizeof(AliHLTTPCDigitData)*ndigitcount
+  // Matthias 05.11.2007
+  // The question is whether we should always return a AliHLTTPCDigitRowData
+  // for each row, even the empty ones or only for the ones filled with data.
+  // the AliHLTTPCDigitReaderUnpacked as the counnterpart so far assumes 
+  // empty RawData structs for empty rows. But some of the code here implies
+  // the latter approach, e.g. the count of nrows in the loop above (now
+  // commented). At least the two loops were not consistent, it's fixed now.
+  nrows=fRowMax-fRowMin+1;
+
+  UInt_t bufferSize = sizeof(AliHLTTPCDigitData)*ndigitcount
     + nrows*sizeof(AliHLTTPCDigitRowData);
 
   LOG(AliHLTTPCLog::kDebug,"AliHLTTPCFileHandler::AliDigits2Memory","Digits")
-    <<AliHLTTPCLog::kDec<<"Found "<<ndigitcount<<" Digits"<<ENDLOG;
-  
-  data=(AliHLTTPCDigitRowData*) Allocate(size);
+    << "Found "<<ndigitcount<<" Digits in " << nrows << " rows out of [" << fRowMin << "," << fRowMax <<"]"<<ENDLOG;
+
+  if (tgtBuffer!=NULL && pTgtSize!=NULL && *pTgtSize>0) {
+    if (bufferSize<=*pTgtSize) {
+      data=reinterpret_cast<AliHLTTPCDigitRowData*>(tgtBuffer);
+    } else {
+    }
+  } else if (bufferSize>0) {
+    data=reinterpret_cast<AliHLTTPCDigitRowData*>(Allocate(bufferSize));
+  }
+  if (pTgtSize) *pTgtSize=bufferSize;
+  if (data==NULL) {
+    delete [] ndigits;
+    return NULL;
+  }
   nrow = (UInt_t)nrows;
   AliHLTTPCDigitRowData *tempPt = data;
+  memset(data, 0, bufferSize);
 
-  for(Int_t r=fRowMin;r<=fRowMax;r++){
+  for(Int_t r=fRowMin;r<=fRowMax && r<fgkNRow;r++){
     Int_t n=fIndex[fSlice][r];
-    tempPt->fRow = r;
+
+    AliHLTTPCTransform::Slice2Sector(fSlice,r,sector,row);
+    tempPt->fRow = row;
     tempPt->fNDigit = 0;
 
     if(n!=-1){//data on that row
+      Int_t lslice,lrow;
       fDigitsTree->GetEvent(n);
       fParam->AdjustSectorRow(fDigits->GetID(),sector,row);
       AliHLTTPCTransform::Sector2Slice(lslice,lrow,sector,row);
@@ -515,11 +602,14 @@ AliHLTTPCDigitRowData * AliHLTTPCFileHandler::AliDigits2Memory(UInt_t & nrow,Int
        continue;
       }
 
+      // set the correct row no and digit count
+      tempPt->fRow = row;
       tempPt->fNDigit = ndigits[lrow];
 
       Int_t localcount=0;
-      fDigits->First();
-      do {
+      for (bool bHaveData=fDigits->First();
+          bHaveData;
+          bHaveData=fDigits->Next()) {
        time=fDigits->CurrentRow();
        pad=fDigits->CurrentColumn();
        dig = fDigits->GetDigit(time,pad);
@@ -540,24 +630,106 @@ AliHLTTPCDigitRowData * AliHLTTPCFileHandler::AliDigits2Memory(UInt_t & nrow,Int
        tempPt->fDigitData[localcount].fCharge=dig;
        tempPt->fDigitData[localcount].fPad=pad;
        tempPt->fDigitData[localcount].fTime=time;
-#ifdef do_mc
        tempPt->fDigitData[localcount].fTrackID[0] = fDigits->GetTrackID(time,pad,0);
        tempPt->fDigitData[localcount].fTrackID[1] = fDigits->GetTrackID(time,pad,1);
        tempPt->fDigitData[localcount].fTrackID[2] = fDigits->GetTrackID(time,pad,2);
-#endif
        localcount++;
-      } while (fDigits->Next());
+      }
     }
+
     Byte_t *tmp = (Byte_t*)tempPt;
-    Int_t size = sizeof(AliHLTTPCDigitRowData)
-                                      + ndigits[lrow]*sizeof(AliHLTTPCDigitData);
-    tmp += size;
+    Int_t blockSize = sizeof(AliHLTTPCDigitRowData)
+      + tempPt->fNDigit*sizeof(AliHLTTPCDigitData);
+    tmp += blockSize;
     tempPt = (AliHLTTPCDigitRowData*)tmp;
   }
+  assert((Byte_t*)tempPt==((Byte_t*)data)+bufferSize);
   delete [] ndigits;
   return data;
 }
 
+int AliHLTTPCFileHandler::AliDigits2Altro(Int_t event, Byte_t* tgtBuffer, UInt_t tgtSize)
+{
+  //Read data from AliROOT file into memory, and store it in the ALTRO data format
+  //in the provided buffer 
+  //Returns: size of the encoded data in bytes
+  int iResult=0;
+
+  if(!fInAli){
+    LOG(AliHLTTPCLog::kWarning,"AliHLTTPCFileHandler::AliDigits2Altro","File")
+    <<"No Input avalible: Pointer to fInAli == NULL"<<ENDLOG;
+    return 0; 
+  }
+
+  if (!tgtBuffer) {
+    return -EINVAL;
+  }
+
+  if(!fDigitsTree)
+    if(!GetDigitsTree(event)) return 0;
+
+  UShort_t dig;
+  Int_t time=0;
+  Int_t pad=0;
+  Int_t sector=0;
+
+  AliHLTTPCMapping mapper(fPatch);
+  AliHLTAltroEncoder encoder;
+  encoder.SetBuffer(tgtBuffer, tgtSize);
+
+  // The digits of the current event have been indexed: all digits are organized in
+  // rows, all digits of one row are stored in a AliSimDigits object (fDigit) which
+  // are stored in the digit tree.
+  // The index map relates the AliSimDigits objects in the tree to dedicated pad rows
+  // in the TPC
+  // This loop filters the pad rows according to the slice no set via Init
+
+  assert(fRowMax<fgkNRow);
+  for(Int_t r=fRowMin;r<=fRowMax && r<fgkNRow && iResult>=0;r++){
+    Int_t n=fIndex[fSlice][r];
+
+    Int_t row=0;
+    Int_t rowOffset=0;
+    AliHLTTPCTransform::Slice2Sector(fSlice,r,sector,row);
+    AliHLTTPCTransform::Slice2Sector(fSlice,AliHLTTPCTransform::GetFirstRow(fPatch),sector,rowOffset);
+
+    if(n!=-1){//data on that row
+      Int_t lslice,lrow;
+      fDigitsTree->GetEvent(n);
+      fParam->AdjustSectorRow(fDigits->GetID(),sector,row);
+      AliHLTTPCTransform::Sector2Slice(lslice,lrow,sector,row);
+      if(lrow!=r){
+       LOG(AliHLTTPCLog::kError,"AliHLTTPCFileHandler::AliDigits2Altro","Row")
+         <<AliHLTTPCLog::kDec<<"Rows on slice " << fSlice << " dont match "<<lrow<<" "<<r<<ENDLOG;
+       continue;
+      }
+
+      Int_t channelAddress=-1;
+      fDigits->First();
+      do {
+       time=fDigits->CurrentRow();
+       pad=fDigits->CurrentColumn();
+       dig = fDigits->GetDigit(time,pad);
+       if (dig <= fParam->GetZeroSup()) continue;
+       if(dig >= AliHLTTPCTransform::GetADCSat())
+         dig = AliHLTTPCTransform::GetADCSat();
+       
+       channelAddress=mapper.GetHwAddress(row-rowOffset, pad);
+       iResult=encoder.AddChannelSignal(dig, time, channelAddress);
+      } while (fDigits->Next() && iResult>=0);
+      if (iResult>=0 && channelAddress>=0) {
+       iResult=encoder.SetChannel(channelAddress);
+      }
+    }
+  }
+
+  if (iResult>=0) {
+    iResult=(encoder.GetTotal40bitWords()*5)/4;
+  }
+
+  return iResult;
+}
+
 AliHLTTPCDigitRowData * AliHLTTPCFileHandler::AliAltroDigits2Memory(UInt_t & nrow,Int_t event,Bool_t eventmerge)
 {
   //Read data from AliROOT file into memory, and store it in the HLT data format.
@@ -611,7 +783,7 @@ AliHLTTPCDigitRowData * AliHLTTPCFileHandler::AliAltroDigits2Memory(UInt_t & nro
   Int_t zerosupval=AliHLTTPCTransform::GetZeroSup();
   Float_t xyz[3];
 
-  for(Int_t r=fRowMin;r<=fRowMax;r++){
+  for(Int_t r=fRowMin;r<=fRowMax && r<fgkNRow;r++){
     Int_t n=fIndex[fSlice][r];
 
     ndigits[r] = 0;
@@ -876,7 +1048,6 @@ AliHLTTPCDigitRowData * AliHLTTPCFileHandler::AliAltroDigits2Memory(UInt_t & nro
          tempPt->fDigitData[localcount].fCharge=dig;
          tempPt->fDigitData[localcount].fPad=pad;
          tempPt->fDigitData[localcount].fTime=time;
-#ifdef do_mc
          tempPt->fDigitData[localcount].fTrackID[0] = (fDigits->GetTrackIDFast(time,pad,0)-2);
          tempPt->fDigitData[localcount].fTrackID[1] = (fDigits->GetTrackIDFast(time,pad,1)-2);
          tempPt->fDigitData[localcount].fTrackID[2] = (fDigits->GetTrackIDFast(time,pad,2)-2);
@@ -889,15 +1060,13 @@ AliHLTTPCDigitRowData * AliHLTTPCFileHandler::AliAltroDigits2Memory(UInt_t & nro
              tempPt->fDigitData[localcount].fTrackID[1] += ((event&0x3ff)<<22);
              tempPt->fDigitData[localcount].fTrackID[2] += ((event&0x3ff)<<22);
            }
-#endif
          localcount++;
        }
       }
     }
     Byte_t *tmp = (Byte_t*)tempPt;
-    Int_t size = sizeof(AliHLTTPCDigitRowData)
+    tmp += sizeof(AliHLTTPCDigitRowData)
       + ndigits[r]*sizeof(AliHLTTPCDigitData);
-    tmp += size;
     tempPt = (AliHLTTPCDigitRowData*)tmp;
   }
   delete [] ndigits;
@@ -928,135 +1097,6 @@ Bool_t AliHLTTPCFileHandler::GetDigitsTree(Int_t event)
   else return kTRUE;
 }
 
-void AliHLTTPCFileHandler::AliDigits2RootFile(AliHLTTPCDigitRowData *rowPt,Char_t *newDigitsfile)
-{
-  //Write the data stored in rowPt, into a new AliROOT file.
-  //The data is stored in the AliROOT format 
-  //This is specially a nice thing if you have modified data, and wants to run it  
-  //through the offline reconstruction chain.
-  //The arguments is a pointer to the data, and the name of the new AliROOT file.
-  //Remember to pass the original AliROOT file (the one that contains the original
-  //simulated data) to this object, in order to retrieve the MC id's of the digits.
-
-  if(!fInAli)
-    {
-      LOG(AliHLTTPCLog::kError,"AliHLTTPCFileHandler::AliDigits2RootFile","File")
-       <<"No rootfile "<<ENDLOG;
-      return;
-    }
-  if(!fParam)
-    {
-      LOG(AliHLTTPCLog::kError,"AliHLTTPCFileHandler::AliDigits2RootFile","File")
-       <<"No parameter object. Run on rootfile "<<ENDLOG;
-      return;
-    }
-
-  //Get the original digitstree:
-  AliLoader* tpcLoader = fInAli->GetLoader("TPCLoader");
-  if(!tpcLoader){
-    LOG(AliHLTTPCLog::kWarning,"AliHLTTPCFileHandler::AliDigits2RootFile","File")
-    <<"Pointer to AliLoader for TPC = 0x0 "<<ENDLOG;
-    return;
-  }
-  tpcLoader->LoadDigits();
-  TTree *t=tpcLoader->TreeD();
-
-  AliTPCDigitsArray *oldArray = new AliTPCDigitsArray();
-  oldArray->Setup(fParam);
-  oldArray->SetClass("AliSimDigits");
-
-  Bool_t ok = oldArray->ConnectTree(t);
-  if(!ok)
-    {
-      LOG(AliHLTTPCLog::kError,"AliHLTTPCFileHandler::AliDigits2RootFile","File")
-       << "No digits tree object" << ENDLOG;
-      return;
-    }
-
-  tpcLoader->SetDigitsFileName(newDigitsfile);
-  tpcLoader->MakeDigitsContainer();
-    
-  //setup a new one, or connect it to the existing one:
-  AliTPCDigitsArray *arr = new AliTPCDigitsArray(); 
-  arr->SetClass("AliSimDigits");
-  arr->Setup(fParam);
-  arr->MakeTree(tpcLoader->TreeD());
-
-  Int_t digcounter=0,trackID[3];
-
-  for(Int_t i=fRowMin; i<=fRowMax; i++)
-    {
-      
-      if((Int_t)rowPt->fRow != i) 
-       LOG(AliHLTTPCLog::kWarning,"AliHLTTPCFileHandler::AliDigits2RootFile","Data")
-         <<"Mismatching row numbering "<<(Int_t)rowPt->fRow<<" "<<i<<ENDLOG;
-            
-      Int_t sector,row;
-      AliHLTTPCTransform::Slice2Sector(fSlice,i,sector,row);
-      
-      AliSimDigits *oldDig = (AliSimDigits*)oldArray->LoadRow(sector,row);
-      AliSimDigits * dig = (AliSimDigits*)arr->CreateRow(sector,row);
-      oldDig->ExpandBuffer();
-      oldDig->ExpandTrackBuffer();
-      dig->ExpandBuffer();
-      dig->ExpandTrackBuffer();
-      
-      if(!oldDig)
-       LOG(AliHLTTPCLog::kWarning,"AliHLTTPCFileHandler::AliDigits2RootFile","Data")
-         <<"No padrow " << sector << " " << row <<ENDLOG;
-
-      AliHLTTPCDigitData *digPt = rowPt->fDigitData;
-      digcounter=0;
-      for(UInt_t j=0; j<rowPt->fNDigit; j++)
-       {
-         Short_t charge = (Short_t)digPt[j].fCharge;
-         Int_t pad = (Int_t)digPt[j].fPad;
-         Int_t time = (Int_t)digPt[j].fTime;
-         
-         if(charge == 0) //Only write the digits that has not been removed
-           {
-             LOG(AliHLTTPCLog::kWarning,"AliHLTTPCFileHandler::AliDigits2RootFile","Data")
-               <<"Zero charge" <<ENDLOG;
-             continue;
-           }
-
-         digcounter++;
-         
-         //Tricks to get and set the correct track id's. 
-         for(Int_t t=0; t<3; t++)
-           {
-             Int_t label = oldDig->GetTrackIDFast(time,pad,t);
-             if(label > 1)
-               trackID[t] = label - 2;
-             else if(label==0)
-               trackID[t] = -2;
-             else
-               trackID[t] = -1;
-           }
-         
-         dig->SetDigitFast(charge,time,pad);
-         
-         for(Int_t t=0; t<3; t++)
-           ((AliSimDigits*)dig)->SetTrackIDFast(trackID[t],time,pad,t);
-         
-       }
-      //cout<<"Wrote "<<digcounter<<" on row "<<i<<endl;
-      UpdateRowPointer(rowPt);
-      arr->StoreRow(sector,row);
-      arr->ClearRow(sector,row);  
-      oldArray->ClearRow(sector,row);
-    }
-
-  char treeName[100];
-  sprintf(treeName,"TreeD_%s_0",fParam->GetTitle());
-  
-  arr->GetTree()->SetName(treeName);
-  arr->GetTree()->AutoSave();
-  tpcLoader->WriteDigits("OVERWRITE");
-  delete arr;
-  delete oldArray;
-}
-
 ///////////////////////////////////////// Point IO  
 Bool_t AliHLTTPCFileHandler::AliPoints2Binary(Int_t eventn)
 {
@@ -1069,6 +1109,8 @@ Bool_t AliHLTTPCFileHandler::AliPoints2Binary(Int_t eventn)
   return out;
 }
 
+#if 0
+// Is this really still used : JMT 2013-03-03
 AliHLTTPCSpacePointData * AliHLTTPCFileHandler::AliPoints2Memory(UInt_t & npoint,Int_t eventn)
 {
   //points to memory
@@ -1092,7 +1134,7 @@ AliHLTTPCSpacePointData * AliHLTTPCFileHandler::AliPoints2Memory(UInt_t & npoint
 
   AliTPCClustersArray carray;
   carray.Setup(fParam);
-  carray.SetClusterType("AliTPCcluster");
+  carray.SetClusterType("AliTPCclusterMI");
   Bool_t clusterok = carray.ConnectTree(tpcLoader->TreeR());
 
   if(!clusterok) return 0;
@@ -1135,12 +1177,12 @@ AliHLTTPCSpacePointData * AliHLTTPCFileHandler::AliPoints2Memory(UInt_t & npoint
     AliHLTTPCTransform::Sector2Slice(lslice,lrow,sector,row);
     Int_t entriesInRow = clusterrow[i]->GetArray()->GetEntriesFast();
     for(Int_t j = 0;j<entriesInRow;j++){
-      AliTPCcluster *c = (AliTPCcluster*)(*clusterrow[i])[j];
+      AliTPCclusterMI *c = (AliTPCclusterMI*)(*clusterrow[i])[j];
       data[n].fZ = c->GetZ();
       data[n].fY = c->GetY();
       data[n].fX = fParam->GetPadRowRadii(sector,row);
       data[n].fCharge = (UInt_t)c->GetQ();
-      data[n].fID = n+((fSlice&0x7f)<<25)+((pat&0x7)<<22);//uli
+      data[n].SetID( fSlice, pat, n );
       data[n].fPadRow = lrow;
       data[n].fSigmaY2 = c->GetSigmaY2();
       data[n].fSigmaZ2 = c->GetSigmaZ2();
@@ -1167,4 +1209,4 @@ AliHLTTPCSpacePointData * AliHLTTPCFileHandler::AliPoints2Memory(UInt_t & npoint
 
   return data;
 }
-
+#endif