]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - HLT/TPCLib/AliHLTTPCFileHandler.cxx
removing old CF configurations, ids redirected to HWCF; global tracking option enable...
[u/mrichter/AliRoot.git] / HLT / TPCLib / AliHLTTPCFileHandler.cxx
index a5fea83e648c6e5d0c09556314fb3abab60df264..114103be1b653e8a2cf2287de09c6e96ef6e6826 100644 (file)
@@ -1,37 +1,70 @@
-// @(#) $Id$
-// Original: AliL3FileHandler.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 
-
+// $Id$
+// Original: AliHLTFileHandler.cxx,v 1.49 2005/06/23 17:46:55 hristov 
+
+//**************************************************************************
+//* 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>
 
-#ifdef use_newio
+#include "AliLoader.h"
 #include <AliRunLoader.h>
-#endif
+#include <TObject.h>
+#include <TFile.h>
+#include <TTree.h>
 #include <AliTPCParamSR.h>
 #include <AliTPCDigitsArray.h>
 #include <AliTPCClustersArray.h>
 #include <AliTPCcluster.h>
 #include <AliTPCClustersRow.h>
 #include <AliSimDigits.h>
+#include "AliCDBManager.h"
+#include "AliCDBEntry.h"
 
 #include "AliHLTTPCLogging.h"
 #include "AliHLTTPCTransform.h"
-#include "AliHLTTPCMemHandler.h"
 #include "AliHLTTPCDigitData.h"
-#include "AliHLTTPCTrackSegmentData.h"
+//#include "AliHLTTPCTrackSegmentData.h"
 #include "AliHLTTPCSpacePointData.h"
-#include "AliHLTTPCTrackArray.h"
+//#include "AliHLTTPCTrackArray.h"
 #include "AliHLTTPCFileHandler.h"
+#include "AliHLTTPCMapping.h"
+#include "AliHLTAltroEncoder.h"
 
 #if __GNUC__ >= 3
 using namespace std;
 #endif
 
-/** \class AliHLTTPCFileHandler
+/**
 <pre>
 //_____________________________________________________________
 // AliHLTTPCFileHandler
@@ -71,6 +104,34 @@ using namespace std;
 
 ClassImp(AliHLTTPCFileHandler)
 
+AliHLTTPCFileHandler::AliHLTTPCFileHandler(Bool_t b)
+  :
+  fInAli(NULL),
+  fUseRunLoader(kFALSE),
+  fParam(NULL),
+  fDigits(NULL),
+  fDigitsTree(NULL),
+  fMC(NULL),
+  fIndexCreated(kFALSE),
+  fUseStaticIndex(b)
+{
+  //Default constructor
+
+  for(Int_t i=0;i<AliHLTTPCTransform::GetNSlice();i++)
+    for(Int_t j=0;j<AliHLTTPCTransform::GetNRows();j++) 
+      fIndex[i][j]=-1;
+
+  if(fUseStaticIndex&&!fgStaticIndexCreated) CleanStaticIndex();
+}
+
+AliHLTTPCFileHandler::~AliHLTTPCFileHandler()
+{
+  //Destructor
+  if(fMC) CloseMCOutput();
+  FreeDigitsTree();
+  if(fInAli) CloseAliInput();
+}
+
 // of course on start up the index is not created
 Bool_t AliHLTTPCFileHandler::fgStaticIndexCreated=kFALSE;
 Int_t  AliHLTTPCFileHandler::fgStaticIndex[36][159]; 
@@ -91,11 +152,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;
@@ -118,11 +180,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;
@@ -137,49 +200,13 @@ Int_t AliHLTTPCFileHandler::LoadStaticIndex(Char_t *prefix,Int_t event)
   return 0;
 }
 
-AliHLTTPCFileHandler::AliHLTTPCFileHandler(Bool_t b)
-{
-  //Default constructor
-  fInAli = 0;
-#ifdef use_newio
-  fUseRunLoader = kFALSE;
-#endif
-  fParam = 0;
-  fMC =0;
-  fDigits=0;
-  fDigitsTree=0;
-  fIndexCreated=kFALSE;
-  fUseStaticIndex=b;
-
-  for(Int_t i=0;i<AliHLTTPCTransform::GetNSlice();i++)
-    for(Int_t j=0;j<AliHLTTPCTransform::GetNRows();j++) 
-      fIndex[i][j]=-1;
-
-  if(fUseStaticIndex&&!fgStaticIndexCreated) CleanStaticIndex();
-}
-
-AliHLTTPCFileHandler::~AliHLTTPCFileHandler()
-{
-  //Destructor
-  if(fMC) CloseMCOutput();
-  FreeDigitsTree();
-  if(fInAli) CloseAliInput();
-}
-
 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;
-#ifndef use_newio
-  fDigitsTree->Delete();
-#endif
   fDigitsTree=0;
 
   for(Int_t i=0;i<AliHLTTPCTransform::GetNSlice();i++){
@@ -228,7 +255,21 @@ void AliHLTTPCFileHandler::CloseMCOutput()
 Bool_t AliHLTTPCFileHandler::SetAliInput()
 { 
   //set ali input
-#ifdef use_newio
+
+  // 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){
@@ -238,31 +279,12 @@ 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;
-    return kFALSE;
-  }
-#else
-  if(!fInAli->IsOpen()){
-    LOG(AliHLTTPCLog::kError,"AliHLTTPCFileHandler::SetAliInput","File Open")
-      <<"Ali File "<<fInAli->GetName()<<" does not exist"<<ENDLOG;
-    return kFALSE;
-  }
-  fParam = (AliTPCParam*)fInAli->Get(AliHLTTPCTransform::GetParamName());
-  if(!fParam){
-    LOG(AliHLTTPCLog::kWarning,"AliHLTTPCFileHandler::SetAliInput","File")
-      <<"No TPC parameters found in \""<<fInAli->GetName()
-      <<"\", creating standard parameters "
-      <<"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 "<<fInAli->GetName()<<ENDLOG;
+      <<"No AliTPCParam "<<AliHLTTPCTransform::GetParamName()<<" in File "<<gFile->GetName()<<ENDLOG;
     return kFALSE;
   }
-#endif
 
   return kTRUE;
 }
@@ -270,11 +292,7 @@ Bool_t AliHLTTPCFileHandler::SetAliInput()
 Bool_t AliHLTTPCFileHandler::SetAliInput(Char_t *name)
 { 
   //Open the AliROOT file with name.
-#ifdef use_newio
   fInAli= AliRunLoader::Open(name);
-#else
-  fInAli= new TFile(name,"READ");
-#endif
   if(!fInAli){
     LOG(AliHLTTPCLog::kWarning,"AliHLTTPCFileHandler::SetAliInput","File Open")
     <<"Pointer to fInAli = 0x0 "<<ENDLOG;
@@ -283,57 +301,33 @@ Bool_t AliHLTTPCFileHandler::SetAliInput(Char_t *name)
   return SetAliInput();
 }
 
-#ifdef use_newio
 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;
   }
-  return SetAliInput();
-}
-#endif
-
-#ifdef use_newio
-Bool_t AliHLTTPCFileHandler::SetAliInput(TFile */*file*/)
-{
-  //Specify already opened AliROOT file to use as an input.
-  LOG(AliHLTTPCLog::kFatal,"AliHLTTPCFileHandler::SetAliInput","File Open")
-    <<"This function is not supported for NEWIO, check ALIHLT_USENEWIO settings in Makefile.conf"<<ENDLOG;
-  return kFALSE;
-}
-#else
-Bool_t AliHLTTPCFileHandler::SetAliInput(TFile *file)
-{ 
-  //Specify already opened AliROOT file to use as an input.
-  fInAli=file;
-  if(!fInAli){
-    LOG(AliHLTTPCLog::kWarning,"AliHLTTPCFileHandler::SetAliInput","File Open")
-    <<"Pointer to fInAli = 0x0 "<<ENDLOG;
+  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();
 }
-#endif
 
 void AliHLTTPCFileHandler::CloseAliInput()
 { 
   //close ali input
-#ifdef use_newio
   if(fUseRunLoader) return;
-#endif
   if(!fInAli){
     LOG(AliHLTTPCLog::kWarning,"AliHLTTPCFileHandler::CloseAliInput","RunLoader")
       <<"Nothing to Close"<<ENDLOG;
     return;
   }
-#ifndef use_newio
-  if(fInAli->IsOpen()) fInAli->Close();
-#endif
 
   delete fInAli;
   fInAli = 0;
@@ -349,7 +343,6 @@ Bool_t AliHLTTPCFileHandler::IsDigit(Int_t event)
     <<"Pointer to fInAli = 0x0 "<<ENDLOG;
     return kTRUE;  //maybe you are using binary input which is Digits!
   }
-#ifdef use_newio
   AliLoader* tpcLoader = fInAli->GetLoader("TPCLoader");
   if(!tpcLoader){
     LOG(AliHLTTPCLog::kWarning,"AliHLTTPCFileHandlerNewIO::IsDigit","File")
@@ -359,11 +352,6 @@ Bool_t AliHLTTPCFileHandler::IsDigit(Int_t event)
   fInAli->GetEvent(event);
   tpcLoader->LoadDigits();
   TTree *t=tpcLoader->TreeD();
-#else
-  Char_t name[1024];
-  sprintf(name,"TreeD_%s_%d",AliHLTTPCTransform::GetParamName(),event);
-  TTree *t=(TTree*)fInAli->Get(name);
-#endif
   if(t){
     LOG(AliHLTTPCLog::kInformational,"AliHLTTPCFileHandlerNewIO::IsDigit","File Type")
     <<"Found Digit Tree -> Use Fast Cluster Finder"<<ENDLOG;
@@ -377,7 +365,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;
@@ -387,7 +375,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;
 }
@@ -418,6 +406,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;
@@ -429,14 +418,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
     }
@@ -445,9 +441,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")
@@ -457,9 +453,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;
@@ -471,20 +468,11 @@ AliHLTTPCDigitRowData * AliHLTTPCFileHandler::AliDigits2Memory(UInt_t & nrow,Int
     return 0; 
   }
 
-#ifndef use_newio
-  if(!fInAli->IsOpen()){
-    LOG(AliHLTTPCLog::kWarning,"AliHLTTPCFileHandler::AliDigits2Memory","File")
-    <<"No Input avalible: TFile not opened"<<ENDLOG;
-    return 0;
-  }
-#endif
-
   if(!fDigitsTree)
     if(!GetDigitsTree(event)) return 0;
 
   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();
@@ -492,26 +480,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")
@@ -520,15 +529,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???
@@ -536,28 +548,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);
@@ -567,11 +603,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);
@@ -592,24 +631,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.
@@ -626,13 +747,6 @@ AliHLTTPCDigitRowData * AliHLTTPCFileHandler::AliAltroDigits2Memory(UInt_t & nro
     <<"No Input avalible: Pointer to TFile == NULL"<<ENDLOG;
     return 0; 
   }
-#ifndef use_newio
-  if(!fInAli->IsOpen()){
-    LOG(AliHLTTPCLog::kWarning,"AliHLTTPCFileHandler::AliAltroDigits2Memory","File")
-    <<"No Input avalible: TFile not opened"<<ENDLOG;
-    return 0;
-  }
-#endif
   if(eventmerge == kTRUE && event >= 1024)
     {
       LOG(AliHLTTPCLog::kError,"AliHLTTPCFileHandler::AliAltroDigits2Memory","TrackIDs")
@@ -641,16 +755,10 @@ AliHLTTPCDigitRowData * AliHLTTPCFileHandler::AliAltroDigits2Memory(UInt_t & nro
     }
   delete fDigits;
   fDigits=0;
-#ifdef use_newio 
   /* Dont understand why we have to do 
      reload the tree, but otherwise the code crashes */
   fDigitsTree=0;
   if(!GetDigitsTree(event)) return 0;
-#else
-  if(!fDigitsTree){
-    if(!GetDigitsTree(event)) return 0;
-  }
-#endif
 
   UShort_t dig;
   Int_t time,pad,sector,row;
@@ -676,7 +784,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;
@@ -941,7 +1049,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);
@@ -954,15 +1061,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;
@@ -972,7 +1077,6 @@ AliHLTTPCDigitRowData * AliHLTTPCFileHandler::AliAltroDigits2Memory(UInt_t & nro
 Bool_t AliHLTTPCFileHandler::GetDigitsTree(Int_t event)
 {
   //Connects to the TPC digit tree in the AliROOT file.
-#ifdef use_newio
   AliLoader* tpcLoader = fInAli->GetLoader("TPCLoader");
   if(!tpcLoader){
     LOG(AliHLTTPCLog::kWarning,"AliHLTTPCFileHandler::GetDigitsTree","File")
@@ -982,12 +1086,6 @@ Bool_t AliHLTTPCFileHandler::GetDigitsTree(Int_t event)
   fInAli->GetEvent(event);
   tpcLoader->LoadDigits();
   fDigitsTree = tpcLoader->TreeD();
-#else  
-  fInAli->cd();
-  Char_t dname[100];
-  sprintf(dname,"TreeD_%s_%d",AliHLTTPCTransform::GetParamName(),event);
-  fDigitsTree = (TTree*)fInAli->Get(dname);
-#endif
   if(!fDigitsTree) 
     {
       LOG(AliHLTTPCLog::kError,"AliHLTTPCFileHandler::GetDigitsTree","Digits Tree")
@@ -1000,205 +1098,6 @@ Bool_t AliHLTTPCFileHandler::GetDigitsTree(Int_t event)
   else return kTRUE;
 }
 
-void AliHLTTPCFileHandler::AliDigits2RootFile(AliHLTTPCDigitRowData *rowPt,Char_t *new_digitsfile)
-{
-  //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;
-    }
-
-#ifdef use_newio
-  //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 *old_array = new AliTPCDigitsArray();
-  old_array->Setup(fParam);
-  old_array->SetClass("AliSimDigits");
-
-  Bool_t ok = old_array->ConnectTree(t);
-  if(!ok)
-    {
-      LOG(AliHLTTPCLog::kError,"AliHLTTPCFileHandler::AliDigits2RootFile","File")
-       << "No digits tree object" << ENDLOG;
-      return;
-    }
-
-  tpcLoader->SetDigitsFileName(new_digitsfile);
-  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());
-#else
-  
-  //Get the original digitstree:
-  Char_t dname[100];
-  sprintf(dname,"TreeD_%s_0",AliHLTTPCTransform::GetParamName());
-
-  fInAli->cd();
-  AliTPCDigitsArray *old_array = new AliTPCDigitsArray();
-  old_array->Setup(fParam);
-  old_array->SetClass("AliSimDigits");
-
-  Bool_t ok = old_array->ConnectTree(dname);
-  if(!ok)
-    {
-      LOG(AliHLTTPCLog::kError,"AliHLTTPCFileHandler::AliDigits2RootFile","File")
-       <<"No digits tree object." <<ENDLOG;
-      return;
-    }
-
-  Bool_t create=kFALSE;
-  TFile *digFile;
-  
-  if(gSystem->AccessPathName(new_digitsfile))
-    {
-      LOG(AliHLTTPCLog::kInformational,"AliHLTTPCFileHandler::AliDigits2RootFile","File")
-       <<"Creating new file "<<new_digitsfile<<ENDLOG;
-      create = kTRUE;
-      digFile = TFile::Open(new_digitsfile,"RECREATE");
-      fParam->Write(fParam->GetTitle());
-    }
-  else
-    {
-      create = kFALSE;
-      digFile = TFile::Open(new_digitsfile,"UPDATE");
-      
-    }
-  if(!digFile->IsOpen())
-    {
-      LOG(AliHLTTPCLog::kError,"AliHLTTPCFileHandler::AliDigits2RootFile","Rootfile")
-       <<"Error opening rootfile "<<new_digitsfile<<ENDLOG;
-      return;
-    }
-  
-  digFile->cd();
-    
-  //setup a new one, or connect it to the existing one:
-  AliTPCDigitsArray *arr = new AliTPCDigitsArray(); 
-  arr->SetClass("AliSimDigits");
-  arr->Setup(fParam);
-  if(create)
-    arr->MakeTree();
-  else
-    {
-      Bool_t ok = arr->ConnectTree(dname);
-      if(!ok)
-       {
-         LOG(AliHLTTPCLog::kError,"AliHLTTPCFileHandler::AliDigits2RootFile","Rootfile")
-           <<"No digits tree object in existing file"<<ENDLOG;
-         return;
-       }
-    }
-#endif
-
-  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 *old_dig = (AliSimDigits*)old_array->LoadRow(sector,row);
-      AliSimDigits * dig = (AliSimDigits*)arr->CreateRow(sector,row);
-      old_dig->ExpandBuffer();
-      old_dig->ExpandTrackBuffer();
-      dig->ExpandBuffer();
-      dig->ExpandTrackBuffer();
-      
-      if(!old_dig)
-       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 = old_dig->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);  
-      old_array->ClearRow(sector,row);
-    }
-
-  char treeName[100];
-  sprintf(treeName,"TreeD_%s_0",fParam->GetTitle());
-  
-#ifdef use_newio
-  arr->GetTree()->SetName(treeName);
-  arr->GetTree()->AutoSave();
-  tpcLoader->WriteDigits("OVERWRITE");
-#else
-  digFile->cd();
-  arr->GetTree()->SetName(treeName);
-  arr->GetTree()->AutoSave();
-  digFile->Close();
-#endif
-  delete arr;
-  delete old_array;
-}
-
 ///////////////////////////////////////// Point IO  
 Bool_t AliHLTTPCFileHandler::AliPoints2Binary(Int_t eventn)
 {
@@ -1221,16 +1120,8 @@ AliHLTTPCSpacePointData * AliHLTTPCFileHandler::AliPoints2Memory(UInt_t & npoint
     <<"No Input avalible: no object fInAli"<<ENDLOG;
     return 0;
   }
-#ifndef use_newio
-  if(!fInAli->IsOpen()){
-    LOG(AliHLTTPCLog::kWarning,"AliHLTTPCFileHandler::AliPoints2Memory","File")
-    <<"No Input avalible: TFile not opend"<<ENDLOG;
-    return 0;
-  }
-#endif
 
   TDirectory *savedir = gDirectory;
-#ifdef use_newio
   AliLoader* tpcLoader = fInAli->GetLoader("TPCLoader");
   if(!tpcLoader){
     LOG(AliHLTTPCLog::kWarning,"AliHLTTPCFileHandler::AliPoints2Memory","File")
@@ -1244,16 +1135,6 @@ AliHLTTPCSpacePointData * AliHLTTPCFileHandler::AliPoints2Memory(UInt_t & npoint
   carray.Setup(fParam);
   carray.SetClusterType("AliTPCcluster");
   Bool_t clusterok = carray.ConnectTree(tpcLoader->TreeR());
-#else
-  fInAli->cd();
-  
-  Char_t cname[100];
-  sprintf(cname,"TreeC_TPC_%d",eventn);
-  AliTPCClustersArray carray;
-  carray.Setup(fParam);
-  carray.SetClusterType("AliTPCcluster");
-  Bool_t clusterok = carray.ConnectTree(cname);
-#endif
 
   if(!clusterok) return 0;
 
@@ -1293,14 +1174,14 @@ AliHLTTPCSpacePointData * AliHLTTPCFileHandler::AliPoints2Memory(UInt_t & npoint
     Int_t row = rows[i];
     Int_t sector = sects[i];
     AliHLTTPCTransform::Sector2Slice(lslice,lrow,sector,row);
-    Int_t entries_in_row = clusterrow[i]->GetArray()->GetEntriesFast();
-    for(Int_t j = 0;j<entries_in_row;j++){
+    Int_t entriesInRow = clusterrow[i]->GetArray()->GetEntriesFast();
+    for(Int_t j = 0;j<entriesInRow;j++){
       AliTPCcluster *c = (AliTPCcluster*)(*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();