]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/TPCLib/AliHLTTPCFileHandler.cxx
unnecessary include removed
[u/mrichter/AliRoot.git] / HLT / TPCLib / AliHLTTPCFileHandler.cxx
1 // @(#) $Id$
2 // Original: AliHLTFileHandler.cxx,v 1.49 2005/06/23 17:46:55 hristov 
3
4 /**************************************************************************
5  * This file is property of and copyright by the ALICE HLT Project        * 
6  * ALICE Experiment at CERN, All rights reserved.                         *
7  *                                                                        *
8  * Primary Authors: U. Frankenfeld, A. Vestbo, C. Loizides                *
9  *                  Matthias Richter <Matthias.Richter@ift.uib.no>        *
10  *                  for The ALICE HLT Project.                            *
11  *                                                                        *
12  * Permission to use, copy, modify and distribute this software and its   *
13  * documentation strictly for non-commercial purposes is hereby granted   *
14  * without fee, provided that the above copyright notice appears in all   *
15  * copies and that both the copyright notice and this permission notice   *
16  * appear in the supporting documentation. The authors make no claims     *
17  * about the suitability of this software for any purpose. It is          *
18  * provided "as is" without express or implied warranty.                  *
19  **************************************************************************/
20
21 /** @file   AliHLTTPCFileHandler.cxx
22     @author U. Frankenfeld, A. Vestbo, C. Loizides, maintained by
23             Matthias Richter
24     @date   
25     @brief  file input for the TPC tracking code before migration to the
26             HLT component framework
27
28 // see below for class documentation
29 // or
30 // refer to README to build package
31 // or
32 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
33                                                                           */
34 #include <cassert>
35 #include <TClonesArray.h>
36 #include <TSystem.h>
37 #include <TMath.h>
38
39 #include <AliRunLoader.h>
40 #include <TObject.h>
41 #include <TFile.h>
42 #include <TTree.h>
43 #include <AliTPCParamSR.h>
44 #include <AliTPCDigitsArray.h>
45 #include <AliTPCClustersArray.h>
46 #include <AliTPCcluster.h>
47 #include <AliTPCClustersRow.h>
48 #include <AliSimDigits.h>
49 #include "AliCDBManager.h"
50 #include "AliCDBEntry.h"
51
52 #include "AliHLTTPCLogging.h"
53 #include "AliHLTTPCTransform.h"
54 #include "AliHLTTPCDigitData.h"
55 //#include "AliHLTTPCTrackSegmentData.h"
56 #include "AliHLTTPCSpacePointData.h"
57 //#include "AliHLTTPCTrackArray.h"
58 #include "AliHLTTPCFileHandler.h"
59 #include "AliHLTTPCMapping.h"
60 #include "AliHLTAltroEncoder.h"
61
62 #if __GNUC__ >= 3
63 using namespace std;
64 #endif
65
66 /**
67 <pre>
68 //_____________________________________________________________
69 // AliHLTTPCFileHandler
70 //
71 // The HLT ROOT <-> binary files handling class
72 //
73 // This class provides the interface between AliROOT files,
74 // and HLT binary files. It should be used for converting 
75 // TPC data stored in AliROOT format (outputfile from a simulation),
76 // into the data format currently used by in the HLT framework. 
77 // This enables the possibility to always use the same data format, 
78 // whether you are using a binary file as an input, or a AliROOT file.
79 //
80 // For example on how to create binary files from a AliROOT simulation,
81 // see example macro exa/Binary.C.
82 //
83 // For reading a AliROOT file into HLT format in memory, do the following:
84 //
85 // AliHLTTPCFileHandler file;
86 // file.Init(slice,patch);
87 // file.SetAliInput("galice.root");
88 // AliHLTTPCDigitRowData *dataPt = (AliHLTTPCDigitRowData*)file.AliDigits2Memory(nrows,eventnr);
89 // 
90 // All the data are then stored in memory and accessible via the pointer dataPt.
91 // Accesing the data is then identical to the example 1) showed in AliHLTTPCMemHandler class.
92 //
93 // For converting the data back, and writing it to a new AliROOT file do:
94 //
95 // AliHLTTPCFileHandler file;
96 // file.Init(slice,patch);
97 // file.SetAliInput("galice.root");
98 // file.Init(slice,patch,NumberOfRowsInPatch);
99 // file.AliDigits2RootFile(dataPt,"new_galice.root");
100 // file.CloseAliInput();
101 </pre>
102 */
103
104 ClassImp(AliHLTTPCFileHandler)
105
106 AliHLTTPCFileHandler::AliHLTTPCFileHandler(Bool_t b)
107   :
108   fInAli(NULL),
109   fUseRunLoader(kFALSE),
110   fParam(NULL),
111   fDigits(NULL),
112   fDigitsTree(NULL),
113   fMC(NULL),
114   fIndexCreated(kFALSE),
115   fUseStaticIndex(b)
116 {
117   //Default constructor
118
119   for(Int_t i=0;i<AliHLTTPCTransform::GetNSlice();i++)
120     for(Int_t j=0;j<AliHLTTPCTransform::GetNRows();j++) 
121       fIndex[i][j]=-1;
122
123   if(fUseStaticIndex&&!fgStaticIndexCreated) CleanStaticIndex();
124 }
125
126 AliHLTTPCFileHandler::~AliHLTTPCFileHandler()
127 {
128   //Destructor
129   if(fMC) CloseMCOutput();
130   FreeDigitsTree();
131   if(fInAli) CloseAliInput();
132 }
133
134 // of course on start up the index is not created
135 Bool_t AliHLTTPCFileHandler::fgStaticIndexCreated=kFALSE;
136 Int_t  AliHLTTPCFileHandler::fgStaticIndex[36][159]; 
137
138 void AliHLTTPCFileHandler::CleanStaticIndex() 
139
140   // use this static call to clean static index after
141   // running over one event
142   for(Int_t i=0;i<AliHLTTPCTransform::GetNSlice();i++){
143     for(Int_t j=0;j<AliHLTTPCTransform::GetNRows();j++)
144       fgStaticIndex[i][j]=-1;
145   }
146   fgStaticIndexCreated=kFALSE;
147 }
148
149 Int_t AliHLTTPCFileHandler::SaveStaticIndex(Char_t *prefix,Int_t event) 
150
151   // use this static call to store static index after
152   if(!fgStaticIndexCreated) return -1;
153
154   Char_t fname[1024];
155   if(prefix)
156     sprintf(fname,"%s-%d.txt",prefix,event);
157   else
158     sprintf(fname,"TPC.Digits.staticindex-%d.txt",event);
159
160   ofstream file(fname,ios::trunc);
161   if(!file.good()) return -1;
162
163   for(Int_t i=0;i<AliHLTTPCTransform::GetNSlice();i++){
164     for(Int_t j=0;j<AliHLTTPCTransform::GetNRows();j++)
165       file << fgStaticIndex[i][j] << " ";
166     file << endl;
167   }
168   file.close();
169   return 0;
170 }
171
172 Int_t AliHLTTPCFileHandler::LoadStaticIndex(Char_t *prefix,Int_t event) 
173
174   // use this static call to store static index after
175   if(fgStaticIndexCreated){
176       LOG(AliHLTTPCLog::kWarning,"AliHLTTPCFileHandler::LoadStaticIndex","Inxed")
177         <<"Static index already created, will overwrite"<<ENDLOG;
178       CleanStaticIndex();
179   }
180
181   Char_t fname[1024];
182   if(prefix)
183     sprintf(fname,"%s-%d.txt",prefix,event);
184   else
185     sprintf(fname,"TPC.Digits.staticindex-%d.txt",event);
186
187   ifstream file(fname);
188   if(!file.good()) return -1;
189
190   for(Int_t i=0;i<AliHLTTPCTransform::GetNSlice();i++){
191     for(Int_t j=0;j<AliHLTTPCTransform::GetNRows();j++)
192       file >> fgStaticIndex[i][j];
193   }
194   file.close();
195
196   fgStaticIndexCreated=kTRUE;
197   return 0;
198 }
199
200 void AliHLTTPCFileHandler::FreeDigitsTree()
201
202   //free digits tree
203   if (fDigits) {
204     delete fDigits;
205   }
206   fDigits=0;
207   fDigitsTree=0;
208
209   for(Int_t i=0;i<AliHLTTPCTransform::GetNSlice();i++){
210     for(Int_t j=0;j<AliHLTTPCTransform::GetNRows();j++)
211       fIndex[i][j]=-1;
212   }
213   fIndexCreated=kFALSE;
214 }
215
216 Bool_t AliHLTTPCFileHandler::SetMCOutput(Char_t *name)
217
218   //set mc input
219   fMC = fopen(name,"w");
220   if(!fMC){
221     LOG(AliHLTTPCLog::kWarning,"AliHLTTPCFileHandler::SetMCOutput","File Open")
222       <<"Pointer to File = 0x0 "<<ENDLOG;
223     return kFALSE;
224   }
225   return kTRUE;
226 }
227
228 Bool_t AliHLTTPCFileHandler::SetMCOutput(FILE *file)
229
230   //set mc output
231   fMC = file;
232   if(!fMC){
233     LOG(AliHLTTPCLog::kWarning,"AliHLTTPCFileHandler::SetMCOutput","File Open")
234       <<"Pointer to File = 0x0 "<<ENDLOG;
235     return kFALSE;
236   }
237   return kTRUE;
238 }
239
240 void AliHLTTPCFileHandler::CloseMCOutput()
241
242   //close mc output
243   if(!fMC){
244     LOG(AliHLTTPCLog::kWarning,"AliHLTTPCFileHandler::CloseMCOutPut","File Close")
245       <<"Nothing to Close"<<ENDLOG;
246     return;
247   }
248   fclose(fMC);
249   fMC =0;
250 }
251
252 Bool_t AliHLTTPCFileHandler::SetAliInput()
253
254   //set ali input
255
256   // fParam is in all cases an external object
257   const char* cdbEntry="TPC/Calib/Parameters";
258   AliCDBManager* pMan=AliCDBManager::Instance();
259   if (pMan) {
260     AliCDBEntry *pEntry = pMan->Get(cdbEntry);
261     if (pEntry==NULL || 
262         pEntry->GetObject()==NULL ||
263         (fParam=dynamic_cast<AliTPCParam*>(pEntry->GetObject()))==NULL) {
264       LOG(AliHLTTPCLog::kWarning,"AliHLTTPCFileHandler::SetAliInput","File")
265         <<"can not load AliTPCParam object from OCDB entry " << cdbEntry <<ENDLOG;
266     }
267   }
268   if (!fParam) {
269   // the old solution until Nov 2008
270   fInAli->CdGAFile();
271   fParam = (AliTPCParam*)gFile->Get("75x40_100x60_150x60");
272   if(!fParam){
273     LOG(AliHLTTPCLog::kWarning,"AliHLTTPCFileHandler::SetAliInput","File")
274       <<"No TPC parameters found in \""<<gFile->GetName()
275       <<"\", creating standard parameters "
276       <<"which might not be what you want!"<<ENDLOG;
277     fParam = new AliTPCParamSR;
278   }
279   }
280   if(!fParam){ 
281     LOG(AliHLTTPCLog::kError,"AliHLTTPCFileHandler::SetAliInput","File Open")
282       <<"No AliTPCParam "<<AliHLTTPCTransform::GetParamName()<<" in File "<<gFile->GetName()<<ENDLOG;
283     return kFALSE;
284   }
285
286   return kTRUE;
287 }
288
289 Bool_t AliHLTTPCFileHandler::SetAliInput(Char_t *name)
290
291   //Open the AliROOT file with name.
292   fInAli= AliRunLoader::Open(name);
293   if(!fInAli){
294     LOG(AliHLTTPCLog::kWarning,"AliHLTTPCFileHandler::SetAliInput","File Open")
295     <<"Pointer to fInAli = 0x0 "<<ENDLOG;
296     return kFALSE;
297   }
298   return SetAliInput();
299 }
300
301 Bool_t AliHLTTPCFileHandler::SetAliInput(AliRunLoader *runLoader)
302
303   //set ali input as runloader
304   if(!runLoader){
305     LOG(AliHLTTPCLog::kWarning,"AliHLTTPCFileHandler::SetAliInput","File Open")
306       <<"invalid agument: pointer to AliRunLoader NULL "<<ENDLOG;
307     return kFALSE;
308   }
309   if (fInAli!=NULL && fInAli!=runLoader) {
310     LOG(AliHLTTPCLog::kError,"AliHLTTPCFileHandler::SetAliInput","File Open")
311     <<"Pointer to AliRunLoader already set"<<ENDLOG;
312     return kFALSE;
313   }
314   fInAli=runLoader;
315   fUseRunLoader = kTRUE;
316   return SetAliInput();
317 }
318
319 void AliHLTTPCFileHandler::CloseAliInput()
320
321   //close ali input
322   if(fUseRunLoader) return;
323   if(!fInAli){
324     LOG(AliHLTTPCLog::kWarning,"AliHLTTPCFileHandler::CloseAliInput","RunLoader")
325       <<"Nothing to Close"<<ENDLOG;
326     return;
327   }
328
329   delete fInAli;
330   fInAli = 0;
331 }
332
333 Bool_t AliHLTTPCFileHandler::IsDigit(Int_t event)
334 {
335   //Check if there is a TPC digit tree in the current file.
336   //Return kTRUE if tree was found, and kFALSE if not found.
337   
338   if(!fInAli){
339     LOG(AliHLTTPCLog::kWarning,"AliHLTTPCFileHandler::IsDigit","File")
340     <<"Pointer to fInAli = 0x0 "<<ENDLOG;
341     return kTRUE;  //maybe you are using binary input which is Digits!
342   }
343   AliLoader* tpcLoader = fInAli->GetLoader("TPCLoader");
344   if(!tpcLoader){
345     LOG(AliHLTTPCLog::kWarning,"AliHLTTPCFileHandlerNewIO::IsDigit","File")
346     <<"Pointer to AliLoader for TPC = 0x0 "<<ENDLOG;
347     return kFALSE;
348   }
349   fInAli->GetEvent(event);
350   tpcLoader->LoadDigits();
351   TTree *t=tpcLoader->TreeD();
352   if(t){
353     LOG(AliHLTTPCLog::kInformational,"AliHLTTPCFileHandlerNewIO::IsDigit","File Type")
354     <<"Found Digit Tree -> Use Fast Cluster Finder"<<ENDLOG;
355     return kTRUE;
356   }
357   else{
358     LOG(AliHLTTPCLog::kInformational,"AliHLTTPCFileHandlerNewIO::IsDigit","File Type")
359     <<"No Digit Tree -> Use Cluster Tree"<<ENDLOG;
360     return kFALSE;
361   }
362 }
363
364 ///////////////////////////////////////// Digit IO  
365 Bool_t AliHLTTPCFileHandler::AliDigits2BinaryFile(Int_t event,Bool_t altro)
366 {
367   //save alidigits as binary
368   Bool_t out = kTRUE;
369   UInt_t nrow;
370   AliHLTTPCDigitRowData* data = 0;
371   if(altro)
372     data = AliAltroDigits2Memory(nrow,event);
373   else
374     data = AliDigits2Memory(nrow,event);
375   out = Memory2BinaryFile(nrow,data);
376   Free();
377   return out;
378 }
379
380 Bool_t AliHLTTPCFileHandler::AliDigits2CompBinary(Int_t event,Bool_t altro)
381 {
382   //Convert AliROOT TPC data, into HLT data format.
383   //event specifies the event you want in the aliroot file.
384   
385   Bool_t out = kTRUE;
386   UInt_t ndigits=0;
387   AliHLTTPCDigitRowData *digits=0;
388   if(altro)
389     digits = AliAltroDigits2Memory(ndigits,event);
390   else
391     digits = AliDigits2Memory(ndigits,event);
392   out = Memory2CompBinary(ndigits,digits);
393   Free();
394   return out;
395 }
396
397 Bool_t AliHLTTPCFileHandler::CreateIndex()
398 {
399   //create the access index or copy from static index
400   fIndexCreated=kFALSE;
401
402   if(!fgStaticIndexCreated || !fUseStaticIndex) { //we have to create index 
403     LOG(AliHLTTPCLog::kInformational,"AliHLTTPCFileHandler::CreateIndex","Index")
404       <<"Starting to create index, this can take a while."<<ENDLOG;
405
406     //Int_t lslice,lrow;
407     for(Int_t n=0; n<fDigitsTree->GetEntries(); n++) {
408       Int_t sector, row;
409       Int_t lslice,lrow;
410       fDigitsTree->GetEvent(n);
411       fParam->AdjustSectorRow(fDigits->GetID(),sector,row);
412       if(!AliHLTTPCTransform::Sector2Slice(lslice,lrow,sector,row)){
413         LOG(AliHLTTPCLog::kError,"AliHLTTPCFileHandler::CreateIndex","Slice/Row")
414           <<AliHLTTPCLog::kDec<<"Index could not be created. Wrong values "
415           <<sector<<" "<<row<<ENDLOG;
416         return kFALSE;
417       }
418       // this is just to make sure the same array dimensions are
419       // used as in the AliHLTTPCTransform class. The check for
420       // correct bounds is done in AliHLTTPCTransform::Sector2Slice
421       assert(lslice>=0 && lslice<fgkNSlice);
422       assert(lrow>=0 && lrow<fgkNRow);
423       if(fIndex[lslice][lrow]==-1) {
424         fIndex[lslice][lrow]=n;
425       }
426     }
427     assert(AliHLTTPCTransform::GetNSlice()==fgkNSlice);
428     assert(AliHLTTPCTransform::GetNRows()==fgkNRow);
429     if(fUseStaticIndex) { // create static index
430       for(Int_t islice=0;islice<AliHLTTPCTransform::GetNSlice() && islice<fgkNSlice;islice++){
431         for(Int_t irow=0;irow<AliHLTTPCTransform::GetNRows() && irow<fgkNRow;irow++)
432           fgStaticIndex[islice][irow]=fIndex[islice][irow];
433       }
434       fgStaticIndexCreated=kTRUE; //remember that index has been created
435     }
436
437   LOG(AliHLTTPCLog::kInformational,"AliHLTTPCFileHandler::CreateIndex","Index")
438     <<"Index successfully created."<<ENDLOG;
439
440   } else if(fUseStaticIndex) { //simply copy static index
441     for(Int_t islice=0;islice<AliHLTTPCTransform::GetNSlice() && islice<fgkNSlice;islice++){
442       for(Int_t irow=0;irow<AliHLTTPCTransform::GetNRows() && irow<fgkNRow;irow++)
443         fIndex[islice][irow]=fgStaticIndex[islice][irow];
444     }
445
446   LOG(AliHLTTPCLog::kInformational,"AliHLTTPCFileHandler::CreateIndex","Index")
447     <<"Index successfully taken from static copy."<<ENDLOG;
448   }
449   fIndexCreated=kTRUE;
450   return kTRUE;
451 }
452
453 AliHLTTPCDigitRowData * AliHLTTPCFileHandler::AliDigits2Memory(UInt_t & nrow,Int_t event, Byte_t* tgtBuffer, UInt_t *pTgtSize)
454 {
455   //Read data from AliROOT file into memory, and store it in the HLT data format
456   //in the provided buffer or an allocated buffer.
457   //Returns a pointer to the data.
458
459   AliHLTTPCDigitRowData *data = 0;
460   nrow=0;
461   
462   if(!fInAli){
463     LOG(AliHLTTPCLog::kWarning,"AliHLTTPCFileHandler::AliDigits2Memory","File")
464     <<"No Input avalible: Pointer to fInAli == NULL"<<ENDLOG;
465     return 0; 
466   }
467
468   if(!fDigitsTree)
469     if(!GetDigitsTree(event)) return 0;
470
471   UShort_t dig;
472   Int_t time,pad,sector,row;
473   Int_t nrows=0;
474   Int_t ndigitcount=0;
475   Int_t entries = (Int_t)fDigitsTree->GetEntries();
476   if(entries==0) {
477     LOG(AliHLTTPCLog::kWarning,"AliHLTTPCFileHandler::AliDigits2Memory","ndigits")
478       <<"No TPC digits (entries==0)!"<<ENDLOG;
479     nrow = (UInt_t)(fRowMax-fRowMin+1);
480     UInt_t size = nrow*sizeof(AliHLTTPCDigitRowData);
481     if (tgtBuffer!=NULL && pTgtSize!=NULL && *pTgtSize>0) {
482       if (size<=*pTgtSize) {
483         data=reinterpret_cast<AliHLTTPCDigitRowData*>(tgtBuffer);
484       } else {
485       }
486     } else {
487       data=reinterpret_cast<AliHLTTPCDigitRowData*>(Allocate(size));
488     }
489     AliHLTTPCDigitRowData *tempPt = data;
490     if (data) {
491     if (pTgtSize) *pTgtSize=size;
492     for(Int_t r=fRowMin;r<=fRowMax;r++){
493       tempPt->fRow = r;
494       tempPt->fNDigit = 0;
495       tempPt++;
496     }
497     }
498     return data;
499   }
500
501   Int_t * ndigits = new Int_t[fRowMax+1];
502   Float_t xyz[3];
503
504   // The digits of the current event have been indexed: all digits are organized in
505   // rows, all digits of one row are stored in a AliSimDigits object (fDigit) which
506   // are stored in the digit tree.
507   // The index map relates the AliSimDigits objects in the tree to dedicated pad rows
508   // in the TPC
509   // This loop filters the pad rows according to the slice no set via Init
510   assert(fRowMax<fgkNRow);
511   for(Int_t r=fRowMin;r<=fRowMax && r<fgkNRow;r++){
512     Int_t n=fIndex[fSlice][r];
513     if(n!=-1){ // there is data on that row available
514       Int_t lslice,lrow;
515       fDigitsTree->GetEvent(n);
516       fParam->AdjustSectorRow(fDigits->GetID(),sector,row);
517       AliHLTTPCTransform::Sector2Slice(lslice,lrow,sector,row);
518 //       LOG(AliHLTTPCLog::kInformational,"AliHLTTPCFileHandler::AliDigits2Memory","Digits")
519 //      << "Sector "<<sector<<" Row " << row << " Slice " << lslice << " lrow " << lrow<<ENDLOG;
520
521       if(lrow!=r){
522         LOG(AliHLTTPCLog::kError,"AliHLTTPCFileHandler::AliDigits2Memory","Row")
523           <<AliHLTTPCLog::kDec<<"Rows in slice " << fSlice << " dont match "<<lrow<<" "<<r<<ENDLOG;
524         continue;
525       }
526
527       ndigits[lrow] = 0;
528       for (bool bHaveData=fDigits->First();
529            bHaveData;
530            bHaveData=fDigits->Next()) {
531         time=fDigits->CurrentRow();
532         pad=fDigits->CurrentColumn();
533         dig = fDigits->GetDigit(time,pad);
534         if(dig <= fParam->GetZeroSup()) continue;
535         if(dig >= AliHLTTPCTransform::GetADCSat())
536           dig = AliHLTTPCTransform::GetADCSat();
537
538         // if we switch to AliTPCTransform, this maybe needs to be 
539         // adjusted as well
540         AliHLTTPCTransform::Raw2Local(xyz,sector,row,pad,time);
541         //      if(fParam->GetPadRowRadii(sector,row)<230./250.*fabs(xyz[2]))
542         //        continue; // why 230???
543
544         ndigits[lrow]++; //for this row only
545         ndigitcount++;   //total number of digits to be published
546
547       }
548       //cout << lrow << " " << ndigits[lrow] << " - " << ndigitcount << endl;
549     }
550     //see comment below//nrows++;
551   }
552   // Matthias 05.11.2007
553   // The question is whether we should always return a AliHLTTPCDigitRowData
554   // for each row, even the empty ones or only for the ones filled with data.
555   // the AliHLTTPCDigitReaderUnpacked as the counnterpart so far assumes 
556   // empty RawData structs for empty rows. But some of the code here implies
557   // the latter approach, e.g. the count of nrows in the loop above (now
558   // commented). At least the two loops were not consistent, it's fixed now.
559   nrows=fRowMax-fRowMin+1;
560
561   UInt_t bufferSize = sizeof(AliHLTTPCDigitData)*ndigitcount
562     + nrows*sizeof(AliHLTTPCDigitRowData);
563
564   LOG(AliHLTTPCLog::kDebug,"AliHLTTPCFileHandler::AliDigits2Memory","Digits")
565     << "Found "<<ndigitcount<<" Digits in " << nrows << " rows out of [" << fRowMin << "," << fRowMax <<"]"<<ENDLOG;
566
567   if (tgtBuffer!=NULL && pTgtSize!=NULL && *pTgtSize>0) {
568     if (bufferSize<=*pTgtSize) {
569       data=reinterpret_cast<AliHLTTPCDigitRowData*>(tgtBuffer);
570     } else {
571     }
572   } else if (bufferSize>0) {
573     data=reinterpret_cast<AliHLTTPCDigitRowData*>(Allocate(bufferSize));
574   }
575   if (pTgtSize) *pTgtSize=bufferSize;
576   if (data==NULL) {
577     delete [] ndigits;
578     return NULL;
579   }
580   nrow = (UInt_t)nrows;
581   AliHLTTPCDigitRowData *tempPt = data;
582   memset(data, 0, bufferSize);
583
584   for(Int_t r=fRowMin;r<=fRowMax && r<fgkNRow;r++){
585     Int_t n=fIndex[fSlice][r];
586
587     AliHLTTPCTransform::Slice2Sector(fSlice,r,sector,row);
588     tempPt->fRow = row;
589     tempPt->fNDigit = 0;
590
591     if(n!=-1){//data on that row
592       Int_t lslice,lrow;
593       fDigitsTree->GetEvent(n);
594       fParam->AdjustSectorRow(fDigits->GetID(),sector,row);
595       AliHLTTPCTransform::Sector2Slice(lslice,lrow,sector,row);
596       if(lrow!=r){
597         LOG(AliHLTTPCLog::kError,"AliHLTTPCFileHandler::AliDigits2Memory","Row")
598           <<AliHLTTPCLog::kDec<<"Rows on slice " << fSlice << " dont match "<<lrow<<" "<<r<<ENDLOG;
599         continue;
600       }
601
602       // set the correct row no and digit count
603       tempPt->fRow = row;
604       tempPt->fNDigit = ndigits[lrow];
605
606       Int_t localcount=0;
607       for (bool bHaveData=fDigits->First();
608            bHaveData;
609            bHaveData=fDigits->Next()) {
610         time=fDigits->CurrentRow();
611         pad=fDigits->CurrentColumn();
612         dig = fDigits->GetDigit(time,pad);
613         if (dig <= fParam->GetZeroSup()) continue;
614         if(dig >= AliHLTTPCTransform::GetADCSat())
615           dig = AliHLTTPCTransform::GetADCSat();
616
617         //Exclude data outside cone:
618         AliHLTTPCTransform::Raw2Local(xyz,sector,row,pad,time);
619         //      if(fParam->GetPadRowRadii(sector,row)<230./250.*fabs(xyz[2]))
620         //        continue; // why 230???
621
622         if(localcount >= ndigits[lrow])
623           LOG(AliHLTTPCLog::kFatal,"AliHLTTPCFileHandler::AliDigits2Binary","Memory")
624             <<AliHLTTPCLog::kDec<<"Mismatch: localcount "<<localcount<<" ndigits "
625             <<ndigits[lrow]<<ENDLOG;
626         
627         tempPt->fDigitData[localcount].fCharge=dig;
628         tempPt->fDigitData[localcount].fPad=pad;
629         tempPt->fDigitData[localcount].fTime=time;
630         tempPt->fDigitData[localcount].fTrackID[0] = fDigits->GetTrackID(time,pad,0);
631         tempPt->fDigitData[localcount].fTrackID[1] = fDigits->GetTrackID(time,pad,1);
632         tempPt->fDigitData[localcount].fTrackID[2] = fDigits->GetTrackID(time,pad,2);
633         localcount++;
634       }
635     }
636
637     Byte_t *tmp = (Byte_t*)tempPt;
638     Int_t blockSize = sizeof(AliHLTTPCDigitRowData)
639       + tempPt->fNDigit*sizeof(AliHLTTPCDigitData);
640     tmp += blockSize;
641     tempPt = (AliHLTTPCDigitRowData*)tmp;
642   }
643   assert((Byte_t*)tempPt==((Byte_t*)data)+bufferSize);
644   delete [] ndigits;
645   return data;
646 }
647
648 int AliHLTTPCFileHandler::AliDigits2Altro(Int_t event, Byte_t* tgtBuffer, UInt_t tgtSize)
649 {
650   //Read data from AliROOT file into memory, and store it in the ALTRO data format
651   //in the provided buffer 
652   //Returns: size of the encoded data in bytes
653   int iResult=0;
654
655   if(!fInAli){
656     LOG(AliHLTTPCLog::kWarning,"AliHLTTPCFileHandler::AliDigits2Altro","File")
657     <<"No Input avalible: Pointer to fInAli == NULL"<<ENDLOG;
658     return 0; 
659   }
660
661   if (!tgtBuffer) {
662     return -EINVAL;
663   }
664
665   if(!fDigitsTree)
666     if(!GetDigitsTree(event)) return 0;
667
668   UShort_t dig;
669   Int_t time=0;
670   Int_t pad=0;
671   Int_t sector=0;
672
673   AliHLTTPCMapping mapper(fPatch);
674   AliHLTAltroEncoder encoder;
675   encoder.SetBuffer(tgtBuffer, tgtSize);
676
677   // The digits of the current event have been indexed: all digits are organized in
678   // rows, all digits of one row are stored in a AliSimDigits object (fDigit) which
679   // are stored in the digit tree.
680   // The index map relates the AliSimDigits objects in the tree to dedicated pad rows
681   // in the TPC
682   // This loop filters the pad rows according to the slice no set via Init
683
684   assert(fRowMax<fgkNRow);
685   for(Int_t r=fRowMin;r<=fRowMax && r<fgkNRow && iResult>=0;r++){
686     Int_t n=fIndex[fSlice][r];
687
688     Int_t row=0;
689     Int_t rowOffset=0;
690     AliHLTTPCTransform::Slice2Sector(fSlice,r,sector,row);
691     AliHLTTPCTransform::Slice2Sector(fSlice,AliHLTTPCTransform::GetFirstRow(fPatch),sector,rowOffset);
692
693     if(n!=-1){//data on that row
694       Int_t lslice,lrow;
695       fDigitsTree->GetEvent(n);
696       fParam->AdjustSectorRow(fDigits->GetID(),sector,row);
697       AliHLTTPCTransform::Sector2Slice(lslice,lrow,sector,row);
698       if(lrow!=r){
699         LOG(AliHLTTPCLog::kError,"AliHLTTPCFileHandler::AliDigits2Altro","Row")
700           <<AliHLTTPCLog::kDec<<"Rows on slice " << fSlice << " dont match "<<lrow<<" "<<r<<ENDLOG;
701         continue;
702       }
703
704       Int_t channelAddress=-1;
705       fDigits->First();
706       do {
707         time=fDigits->CurrentRow();
708         pad=fDigits->CurrentColumn();
709         dig = fDigits->GetDigit(time,pad);
710         if (dig <= fParam->GetZeroSup()) continue;
711         if(dig >= AliHLTTPCTransform::GetADCSat())
712           dig = AliHLTTPCTransform::GetADCSat();
713         
714         channelAddress=mapper.GetHwAddress(row-rowOffset, pad);
715         iResult=encoder.AddChannelSignal(dig, time, channelAddress);
716       } while (fDigits->Next() && iResult>=0);
717       if (iResult>=0 && channelAddress>=0) {
718         iResult=encoder.SetChannel(channelAddress);
719       }
720     }
721   }
722
723   if (iResult>=0) {
724     iResult=(encoder.GetTotal40bitWords()*5)/4;
725   }
726
727   return iResult;
728 }
729
730 AliHLTTPCDigitRowData * AliHLTTPCFileHandler::AliAltroDigits2Memory(UInt_t & nrow,Int_t event,Bool_t eventmerge)
731 {
732   //Read data from AliROOT file into memory, and store it in the HLT data format.
733   //Returns a pointer to the data.
734   //This functions filter out single timebins, which is noise. The timebins which
735   //are removed are timebins which have the 4 zero neighbours; 
736   //(pad-1,time),(pad+1,time),(pad,time-1),(pad,time+1).
737   
738   AliHLTTPCDigitRowData *data = 0;
739   nrow=0;
740   
741   if(!fInAli){
742     LOG(AliHLTTPCLog::kWarning,"AliHLTTPCFileHandler::AliAltroDigits2Memory","File")
743     <<"No Input avalible: Pointer to TFile == NULL"<<ENDLOG;
744     return 0; 
745   }
746   if(eventmerge == kTRUE && event >= 1024)
747     {
748       LOG(AliHLTTPCLog::kError,"AliHLTTPCFileHandler::AliAltroDigits2Memory","TrackIDs")
749         <<"Too many events if you want to merge!"<<ENDLOG;
750       return 0;
751     }
752   delete fDigits;
753   fDigits=0;
754   /* Dont understand why we have to do 
755      reload the tree, but otherwise the code crashes */
756   fDigitsTree=0;
757   if(!GetDigitsTree(event)) return 0;
758
759   UShort_t dig;
760   Int_t time,pad,sector,row;
761   Int_t nrows=0;
762   Int_t ndigitcount=0;
763   Int_t entries = (Int_t)fDigitsTree->GetEntries();
764   if(entries==0) {
765     LOG(AliHLTTPCLog::kWarning,"AliHLTTPCFileHandler::AliAltroDigits2Memory","ndigits")
766       <<"No TPC digits (entries==0)!"<<ENDLOG;
767     nrow = (UInt_t)(fRowMax-fRowMin+1);
768     Int_t size = nrow*sizeof(AliHLTTPCDigitRowData);
769     data=(AliHLTTPCDigitRowData*) Allocate(size);
770     AliHLTTPCDigitRowData *tempPt = data;
771     for(Int_t r=fRowMin;r<=fRowMax;r++){
772       tempPt->fRow = r;
773       tempPt->fNDigit = 0;
774       tempPt++;
775     }
776     return data;
777   }
778   Int_t * ndigits = new Int_t[fRowMax+1];
779   Int_t lslice,lrow;
780   Int_t zerosupval=AliHLTTPCTransform::GetZeroSup();
781   Float_t xyz[3];
782
783   for(Int_t r=fRowMin;r<=fRowMax && r<fgkNRow;r++){
784     Int_t n=fIndex[fSlice][r];
785
786     ndigits[r] = 0;
787
788     if(n!=-1){//data on that row
789       fDigitsTree->GetEvent(n);
790       fParam->AdjustSectorRow(fDigits->GetID(),sector,row);
791       AliHLTTPCTransform::Sector2Slice(lslice,lrow,sector,row);
792       //cout << lslice << " " << fSlice << " " << lrow << " " << r << " " << sector << " " << row << endl;
793       if((lslice!=fSlice)||(lrow!=r)){
794         LOG(AliHLTTPCLog::kError,"AliHLTTPCFileHandler::AliAltroDigits2Memory","Row")
795           <<AliHLTTPCLog::kDec<<"Rows on slice " << fSlice << " dont match "<<lrow<<" "<<r<<ENDLOG;
796         continue;
797       }
798
799       fDigits->ExpandBuffer();
800       fDigits->ExpandTrackBuffer();
801       for(Int_t i=0; i<fDigits->GetNCols(); i++){
802         for(Int_t j=0; j<fDigits->GetNRows(); j++){
803           pad=i;
804           time=j;
805           dig = fDigits->GetDigitFast(time,pad);
806           if(dig <= zerosupval) continue;
807           if(dig >= AliHLTTPCTransform::GetADCSat())
808             dig = AliHLTTPCTransform::GetADCSat();
809
810           //Check for single timebins, and remove them because they are noise for sure.
811           if(i>0 && i<fDigits->GetNCols()-1 && j>0 && j<fDigits->GetNRows()-1)
812             if(fDigits->GetDigitFast(time,pad-1)<=zerosupval &&
813                fDigits->GetDigitFast(time-1,pad)<=zerosupval &&
814                fDigits->GetDigitFast(time+1,pad)<=zerosupval &&
815                fDigits->GetDigitFast(time,pad+1)<=zerosupval)
816               continue;
817               
818           //Boundaries:
819           if(i==0) //pad==0
820             {
821               if(j < fDigits->GetNRows()-1 && j > 0) 
822                 {
823                   if(fDigits->GetDigitFast(time-1,pad)<=zerosupval &&
824                      fDigits->GetDigitFast(time+1,pad)<=zerosupval &&
825                      fDigits->GetDigitFast(time,pad+1)<=zerosupval)
826                     continue;
827                 }
828               else if(j > 0)
829                 {
830                   if(fDigits->GetDigitFast(time-1,pad)<=zerosupval &&
831                      fDigits->GetDigitFast(time,pad+1)<=zerosupval)
832                     continue;
833                 }
834             }
835           if(j==0)
836             {
837               if(i < fDigits->GetNCols()-1 && i > 0)
838                 {
839                   if(fDigits->GetDigitFast(time,pad-1)<=zerosupval &&
840                      fDigits->GetDigitFast(time,pad+1)<=zerosupval &&
841                      fDigits->GetDigitFast(time+1,pad)<=zerosupval)
842                     continue;
843                 }
844               else if(i > 0)
845                 {
846                   if(fDigits->GetDigitFast(time,pad-1)<=zerosupval &&
847                      fDigits->GetDigitFast(time+1,pad)<=zerosupval)
848                     continue;
849                 }
850             }
851
852           if(i==fDigits->GetNCols()-1)
853             {
854               if(j>0 && j<fDigits->GetNRows()-1)
855                 {
856                   if(fDigits->GetDigitFast(time-1,pad)<=zerosupval &&
857                      fDigits->GetDigitFast(time+1,pad)<=zerosupval &&
858                      fDigits->GetDigitFast(time,pad-1)<=zerosupval)
859                     continue;
860                 }
861               else if(j==0 && j<fDigits->GetNRows()-1)
862                 {
863                   if(fDigits->GetDigitFast(time+1,pad)<=zerosupval &&
864                      fDigits->GetDigitFast(time,pad-1)<=zerosupval)
865                     continue;
866                 }
867               else 
868                 {
869                   if(fDigits->GetDigitFast(time-1,pad)<=zerosupval &&
870                      fDigits->GetDigitFast(time,pad-1)<=zerosupval)
871                     continue;
872                 }
873             }
874         
875           if(j==fDigits->GetNRows()-1)
876             {
877               if(i>0 && i<fDigits->GetNCols()-1)
878                 {
879                   if(fDigits->GetDigitFast(time,pad-1)<=zerosupval &&
880                      fDigits->GetDigitFast(time,pad+1)<=zerosupval &&
881                      fDigits->GetDigitFast(time-1,pad)<=zerosupval)
882                     continue;
883                 }
884               else if(i==0 && fDigits->GetNCols()-1)
885                 {
886                   if(fDigits->GetDigitFast(time,pad+1)<=zerosupval &&
887                      fDigits->GetDigitFast(time-1,pad)<=zerosupval)
888                     continue;
889                 }
890               else 
891                 {
892                   if(fDigits->GetDigitFast(time,pad-1)<=zerosupval &&
893                      fDigits->GetDigitFast(time-1,pad)<=zerosupval)
894                     continue;
895                 }
896             }
897
898           AliHLTTPCTransform::Raw2Local(xyz,sector,row,pad,time);
899           //      if(fParam->GetPadRowRadii(sector,row)<230./250.*fabs(xyz[2]))
900           //      continue; 
901               
902           ndigits[lrow]++; //for this row only
903           ndigitcount++;   //total number of digits to be published
904         }
905       }
906     }
907     nrows++;
908   }
909   
910   Int_t size = sizeof(AliHLTTPCDigitData)*ndigitcount
911     + nrows*sizeof(AliHLTTPCDigitRowData);
912
913   LOG(AliHLTTPCLog::kDebug,"AliHLTTPCFileHandler::AliAltroDigits2Memory","Digits")
914     <<AliHLTTPCLog::kDec<<"Found "<<ndigitcount<<" Digits"<<ENDLOG;
915   
916   data=(AliHLTTPCDigitRowData*) Allocate(size);
917   nrow = (UInt_t)nrows;
918   AliHLTTPCDigitRowData *tempPt = data;
919  
920   for(Int_t r=fRowMin;r<=fRowMax;r++){
921     Int_t n=fIndex[fSlice][r];
922     tempPt->fRow = r;
923     tempPt->fNDigit = 0;
924     if(n!=-1){ //no data on that row
925       fDigitsTree->GetEvent(n);
926       fParam->AdjustSectorRow(fDigits->GetID(),sector,row);
927       AliHLTTPCTransform::Sector2Slice(lslice,lrow,sector,row);
928
929       if(lrow!=r){
930         LOG(AliHLTTPCLog::kError,"AliHLTTPCFileHandler::AliAltroDigits2Memory","Row")
931           <<AliHLTTPCLog::kDec<<"Rows on slice " << fSlice << " dont match "<<lrow<<" "<<r<<ENDLOG;
932         continue;
933       }
934
935       tempPt->fNDigit = ndigits[lrow];
936
937       Int_t localcount=0;
938       fDigits->ExpandBuffer();
939       fDigits->ExpandTrackBuffer();
940       for(Int_t i=0; i<fDigits->GetNCols(); i++){
941         for(Int_t j=0; j<fDigits->GetNRows(); j++){
942           pad=i;
943           time=j;
944           dig = fDigits->GetDigitFast(time,pad);
945           if(dig <= zerosupval) continue;
946           if(dig >= AliHLTTPCTransform::GetADCSat())
947             dig = AliHLTTPCTransform::GetADCSat();
948               
949           //Check for single timebins, and remove them because they are noise for sure.
950           if(i>0 && i<fDigits->GetNCols()-1 && j>0 && j<fDigits->GetNRows()-1)
951             if(fDigits->GetDigitFast(time,pad-1)<=zerosupval &&
952                fDigits->GetDigitFast(time-1,pad)<=zerosupval &&
953                fDigits->GetDigitFast(time+1,pad)<=zerosupval &&
954                fDigits->GetDigitFast(time,pad+1)<=zerosupval)
955               continue;
956           
957           //Boundaries:
958           if(i==0) //pad ==0
959             {
960               if(j < fDigits->GetNRows()-1 && j > 0) 
961                 {
962                   if(fDigits->GetDigitFast(time-1,pad)<=zerosupval &&
963                      fDigits->GetDigitFast(time+1,pad)<=zerosupval &&
964                      fDigits->GetDigitFast(time,pad+1)<=zerosupval)
965                     continue;
966                 }
967               else if(j > 0)
968                 {
969                   if(fDigits->GetDigitFast(time-1,pad)<=zerosupval &&
970                      fDigits->GetDigitFast(time,pad+1)<=zerosupval)
971                     continue;
972                 }
973             }
974           if(j==0)
975             {
976               if(i < fDigits->GetNCols()-1 && i > 0)
977                 {
978                   if(fDigits->GetDigitFast(time,pad-1)<=zerosupval &&
979                      fDigits->GetDigitFast(time,pad+1)<=zerosupval &&
980                      fDigits->GetDigitFast(time+1,pad)<=zerosupval)
981                     continue;
982                 }
983               else if(i > 0)
984                 {
985                   if(fDigits->GetDigitFast(time,pad-1)<=zerosupval &&
986                      fDigits->GetDigitFast(time+1,pad)<=zerosupval)
987                     continue;
988                 }
989             }
990         
991           if(i == fDigits->GetNCols()-1)
992             {
993               if(j>0 && j<fDigits->GetNRows()-1)
994                 {
995                   if(fDigits->GetDigitFast(time-1,pad)<=zerosupval &&
996                      fDigits->GetDigitFast(time+1,pad)<=zerosupval &&
997                      fDigits->GetDigitFast(time,pad-1)<=zerosupval)
998                     continue;
999                 }
1000               else if(j==0 && j<fDigits->GetNRows()-1)
1001                 {
1002                   if(fDigits->GetDigitFast(time+1,pad)<=zerosupval &&
1003                      fDigits->GetDigitFast(time,pad-1)<=zerosupval)
1004                     continue;
1005                 }
1006               else 
1007                 {
1008                   if(fDigits->GetDigitFast(time-1,pad)<=zerosupval &&
1009                      fDigits->GetDigitFast(time,pad-1)<=zerosupval)
1010                     continue;
1011                 }
1012             }
1013           if(j==fDigits->GetNRows()-1)
1014             {
1015               if(i>0 && i<fDigits->GetNCols()-1)
1016                 {
1017                   if(fDigits->GetDigitFast(time,pad-1)<=zerosupval &&
1018                      fDigits->GetDigitFast(time,pad+1)<=zerosupval &&
1019                      fDigits->GetDigitFast(time-1,pad)<=zerosupval)
1020                     continue;
1021                 }
1022               else if(i==0 && fDigits->GetNCols()-1)
1023                 {
1024                   if(fDigits->GetDigitFast(time,pad+1)<=zerosupval &&
1025                      fDigits->GetDigitFast(time-1,pad)<=zerosupval)
1026                     continue;
1027                 }
1028               else 
1029                 {
1030                   if(fDigits->GetDigitFast(time,pad-1)<=zerosupval &&
1031                      fDigits->GetDigitFast(time-1,pad)<=zerosupval)
1032                     continue;
1033                 }
1034             }
1035         
1036           AliHLTTPCTransform::Raw2Local(xyz,sector,row,pad,time);
1037           //      if(fParam->GetPadRowRadii(sector,row)<230./250.*fabs(xyz[2]))
1038           //        continue;
1039           
1040           if(localcount >= ndigits[lrow])
1041             LOG(AliHLTTPCLog::kFatal,"AliHLTTPCFileHandler::AliAltroDigits2Binary","Memory")
1042               <<AliHLTTPCLog::kDec<<"Mismatch: localcount "<<localcount<<" ndigits "
1043               <<ndigits[lrow]<<ENDLOG;
1044         
1045           tempPt->fDigitData[localcount].fCharge=dig;
1046           tempPt->fDigitData[localcount].fPad=pad;
1047           tempPt->fDigitData[localcount].fTime=time;
1048           tempPt->fDigitData[localcount].fTrackID[0] = (fDigits->GetTrackIDFast(time,pad,0)-2);
1049           tempPt->fDigitData[localcount].fTrackID[1] = (fDigits->GetTrackIDFast(time,pad,1)-2);
1050           tempPt->fDigitData[localcount].fTrackID[2] = (fDigits->GetTrackIDFast(time,pad,2)-2);
1051           if(eventmerge == kTRUE) //careful track mc info will be touched
1052             {//Event are going to be merged, so event number is stored in the upper 10 bits.
1053               tempPt->fDigitData[localcount].fTrackID[0] += 128; //leave some room
1054               tempPt->fDigitData[localcount].fTrackID[1] += 128; //for neg. numbers
1055               tempPt->fDigitData[localcount].fTrackID[2] += 128;
1056               tempPt->fDigitData[localcount].fTrackID[0] += ((event&0x3ff)<<22);
1057               tempPt->fDigitData[localcount].fTrackID[1] += ((event&0x3ff)<<22);
1058               tempPt->fDigitData[localcount].fTrackID[2] += ((event&0x3ff)<<22);
1059             }
1060           localcount++;
1061         }
1062       }
1063     }
1064     Byte_t *tmp = (Byte_t*)tempPt;
1065     tmp += sizeof(AliHLTTPCDigitRowData)
1066       + ndigits[r]*sizeof(AliHLTTPCDigitData);
1067     tempPt = (AliHLTTPCDigitRowData*)tmp;
1068   }
1069   delete [] ndigits;
1070   return data;
1071 }
1072  
1073 Bool_t AliHLTTPCFileHandler::GetDigitsTree(Int_t event)
1074 {
1075   //Connects to the TPC digit tree in the AliROOT file.
1076   AliLoader* tpcLoader = fInAli->GetLoader("TPCLoader");
1077   if(!tpcLoader){
1078     LOG(AliHLTTPCLog::kWarning,"AliHLTTPCFileHandler::GetDigitsTree","File")
1079     <<"Pointer to AliLoader for TPC = 0x0 "<<ENDLOG;
1080     return kFALSE;
1081   }
1082   fInAli->GetEvent(event);
1083   tpcLoader->LoadDigits();
1084   fDigitsTree = tpcLoader->TreeD();
1085   if(!fDigitsTree) 
1086     {
1087       LOG(AliHLTTPCLog::kError,"AliHLTTPCFileHandler::GetDigitsTree","Digits Tree")
1088         <<AliHLTTPCLog::kHex<<"Error getting digitstree "<<(void*)fDigitsTree<<ENDLOG;
1089       return kFALSE;
1090     }
1091   fDigitsTree->GetBranch("Segment")->SetAddress(&fDigits);
1092
1093   if(!fIndexCreated) return CreateIndex();
1094   else return kTRUE;
1095 }
1096
1097 void AliHLTTPCFileHandler::AliDigits2RootFile(AliHLTTPCDigitRowData *rowPt,Char_t *newDigitsfile)
1098 {
1099   //Write the data stored in rowPt, into a new AliROOT file.
1100   //The data is stored in the AliROOT format 
1101   //This is specially a nice thing if you have modified data, and wants to run it  
1102   //through the offline reconstruction chain.
1103   //The arguments is a pointer to the data, and the name of the new AliROOT file.
1104   //Remember to pass the original AliROOT file (the one that contains the original
1105   //simulated data) to this object, in order to retrieve the MC id's of the digits.
1106
1107   if(!fInAli)
1108     {
1109       LOG(AliHLTTPCLog::kError,"AliHLTTPCFileHandler::AliDigits2RootFile","File")
1110         <<"No rootfile "<<ENDLOG;
1111       return;
1112     }
1113   if(!fParam)
1114     {
1115       LOG(AliHLTTPCLog::kError,"AliHLTTPCFileHandler::AliDigits2RootFile","File")
1116         <<"No parameter object. Run on rootfile "<<ENDLOG;
1117       return;
1118     }
1119
1120   //Get the original digitstree:
1121   AliLoader* tpcLoader = fInAli->GetLoader("TPCLoader");
1122   if(!tpcLoader){
1123     LOG(AliHLTTPCLog::kWarning,"AliHLTTPCFileHandler::AliDigits2RootFile","File")
1124     <<"Pointer to AliLoader for TPC = 0x0 "<<ENDLOG;
1125     return;
1126   }
1127   tpcLoader->LoadDigits();
1128   TTree *treeD=tpcLoader->TreeD();
1129
1130   AliTPCDigitsArray *oldArray = new AliTPCDigitsArray();
1131   oldArray->Setup(fParam);
1132   oldArray->SetClass("AliSimDigits");
1133
1134   Bool_t ok = oldArray->ConnectTree(treeD);
1135   if(!ok)
1136     {
1137       LOG(AliHLTTPCLog::kError,"AliHLTTPCFileHandler::AliDigits2RootFile","File")
1138         << "No digits tree object" << ENDLOG;
1139       return;
1140     }
1141
1142   tpcLoader->SetDigitsFileName(newDigitsfile);
1143   tpcLoader->MakeDigitsContainer();
1144     
1145   //setup a new one, or connect it to the existing one:
1146   AliTPCDigitsArray *arr = new AliTPCDigitsArray(); 
1147   arr->SetClass("AliSimDigits");
1148   arr->Setup(fParam);
1149   arr->MakeTree(tpcLoader->TreeD());
1150
1151   Int_t digcounter=0,trackID[3];
1152
1153   for(Int_t i=fRowMin; i<=fRowMax; i++)
1154     {
1155       
1156       if((Int_t)rowPt->fRow != i) 
1157         LOG(AliHLTTPCLog::kWarning,"AliHLTTPCFileHandler::AliDigits2RootFile","Data")
1158           <<"Mismatching row numbering "<<(Int_t)rowPt->fRow<<" "<<i<<ENDLOG;
1159             
1160       Int_t sector,row;
1161       AliHLTTPCTransform::Slice2Sector(fSlice,i,sector,row);
1162       
1163       AliSimDigits *oldDig = (AliSimDigits*)oldArray->LoadRow(sector,row);
1164       AliSimDigits * dig = (AliSimDigits*)arr->CreateRow(sector,row);
1165       oldDig->ExpandBuffer();
1166       oldDig->ExpandTrackBuffer();
1167       dig->ExpandBuffer();
1168       dig->ExpandTrackBuffer();
1169       
1170       if(!oldDig)
1171         LOG(AliHLTTPCLog::kWarning,"AliHLTTPCFileHandler::AliDigits2RootFile","Data")
1172           <<"No padrow " << sector << " " << row <<ENDLOG;
1173
1174       AliHLTTPCDigitData *digPt = rowPt->fDigitData;
1175       digcounter=0;
1176       for(UInt_t j=0; j<rowPt->fNDigit; j++)
1177         {
1178           Short_t charge = (Short_t)digPt[j].fCharge;
1179           Int_t pad = (Int_t)digPt[j].fPad;
1180           Int_t time = (Int_t)digPt[j].fTime;
1181           
1182           if(charge == 0) //Only write the digits that has not been removed
1183             {
1184               LOG(AliHLTTPCLog::kWarning,"AliHLTTPCFileHandler::AliDigits2RootFile","Data")
1185                 <<"Zero charge" <<ENDLOG;
1186               continue;
1187             }
1188
1189           digcounter++;
1190           
1191           //Tricks to get and set the correct track id's. 
1192           for(Int_t track=0; track<3; track++)
1193             {
1194               Int_t label = oldDig->GetTrackIDFast(time,pad,track);
1195               if(label > 1)
1196                 trackID[track] = label - 2;
1197               else if(label==0)
1198                 trackID[track] = -2;
1199               else
1200                 trackID[track] = -1;
1201             }
1202           
1203           dig->SetDigitFast(charge,time,pad);
1204           
1205           for(Int_t track=0; track<3; track++)
1206             ((AliSimDigits*)dig)->SetTrackIDFast(trackID[track],time,pad,track);
1207           
1208         }
1209       //cout<<"Wrote "<<digcounter<<" on row "<<i<<endl;
1210       UpdateRowPointer(rowPt);
1211       arr->StoreRow(sector,row);
1212       arr->ClearRow(sector,row);  
1213       oldArray->ClearRow(sector,row);
1214     }
1215
1216   char treeName[100];
1217   sprintf(treeName,"TreeD_%s_0",fParam->GetTitle());
1218   
1219   arr->GetTree()->SetName(treeName);
1220   arr->GetTree()->AutoSave();
1221   tpcLoader->WriteDigits("OVERWRITE");
1222   delete arr;
1223   delete oldArray;
1224 }
1225
1226 ///////////////////////////////////////// Point IO  
1227 Bool_t AliHLTTPCFileHandler::AliPoints2Binary(Int_t eventn)
1228 {
1229   //points to binary
1230   Bool_t out = kTRUE;
1231   UInt_t npoint;
1232   AliHLTTPCSpacePointData *data = AliPoints2Memory(npoint,eventn);
1233   out = Memory2Binary(npoint,data);
1234   Free();
1235   return out;
1236 }
1237
1238 AliHLTTPCSpacePointData * AliHLTTPCFileHandler::AliPoints2Memory(UInt_t & npoint,Int_t eventn)
1239 {
1240   //points to memory
1241   AliHLTTPCSpacePointData *data = 0;
1242   npoint=0;
1243   if(!fInAli){
1244     LOG(AliHLTTPCLog::kWarning,"AliHLTTPCFileHandler::AliPoints2Memory","File")
1245     <<"No Input avalible: no object fInAli"<<ENDLOG;
1246     return 0;
1247   }
1248
1249   TDirectory *savedir = gDirectory;
1250   AliLoader* tpcLoader = fInAli->GetLoader("TPCLoader");
1251   if(!tpcLoader){
1252     LOG(AliHLTTPCLog::kWarning,"AliHLTTPCFileHandler::AliPoints2Memory","File")
1253     <<"Pointer to AliLoader for TPC = 0x0 "<<ENDLOG;
1254     return 0;
1255   }
1256   fInAli->GetEvent(eventn);
1257   tpcLoader->LoadRecPoints();
1258
1259   AliTPCClustersArray carray;
1260   carray.Setup(fParam);
1261   carray.SetClusterType("AliTPCcluster");
1262   Bool_t clusterok = carray.ConnectTree(tpcLoader->TreeR());
1263
1264   if(!clusterok) return 0;
1265
1266   AliTPCClustersRow ** clusterrow = 
1267                new AliTPCClustersRow*[ (int)carray.GetTree()->GetEntries()];
1268   Int_t *rows = new int[ (int)carray.GetTree()->GetEntries()];
1269   Int_t *sects = new int[  (int)carray.GetTree()->GetEntries()];
1270   Int_t sum=0;
1271
1272   Int_t lslice,lrow;
1273   for(Int_t i=0; i<carray.GetTree()->GetEntries(); i++){
1274     AliSegmentID *s = carray.LoadEntry(i);
1275     Int_t sector,row;
1276     fParam->AdjustSectorRow(s->GetID(),sector,row);
1277     rows[i] = row;
1278     sects[i] = sector;
1279     clusterrow[i] = 0;
1280     AliHLTTPCTransform::Sector2Slice(lslice,lrow,sector,row);
1281     if(fSlice != lslice || lrow<fRowMin || lrow>fRowMax) continue;
1282     clusterrow[i] = carray.GetRow(sector,row);
1283     if(clusterrow[i])
1284       sum+=clusterrow[i]->GetArray()->GetEntriesFast();
1285   }
1286   UInt_t size = sum*sizeof(AliHLTTPCSpacePointData);
1287
1288   LOG(AliHLTTPCLog::kDebug,"AliHLTTPCFileHandler::AliPoints2Memory","File")
1289   <<AliHLTTPCLog::kDec<<"Found "<<sum<<" SpacePoints"<<ENDLOG;
1290
1291   data = (AliHLTTPCSpacePointData *) Allocate(size);
1292   npoint = sum;
1293   UInt_t n=0; 
1294   Int_t pat=fPatch;
1295   if(fPatch==-1)
1296     pat=0;
1297   for(Int_t i=0; i<carray.GetTree()->GetEntries(); i++){
1298     if(!clusterrow[i]) continue;
1299     Int_t row = rows[i];
1300     Int_t sector = sects[i];
1301     AliHLTTPCTransform::Sector2Slice(lslice,lrow,sector,row);
1302     Int_t entriesInRow = clusterrow[i]->GetArray()->GetEntriesFast();
1303     for(Int_t j = 0;j<entriesInRow;j++){
1304       AliTPCcluster *c = (AliTPCcluster*)(*clusterrow[i])[j];
1305       data[n].fZ = c->GetZ();
1306       data[n].fY = c->GetY();
1307       data[n].fX = fParam->GetPadRowRadii(sector,row);
1308       data[n].fCharge = (UInt_t)c->GetQ();
1309       data[n].fID = n+((fSlice&0x7f)<<25)+((pat&0x7)<<22);//uli
1310       data[n].fPadRow = lrow;
1311       data[n].fSigmaY2 = c->GetSigmaY2();
1312       data[n].fSigmaZ2 = c->GetSigmaZ2();
1313 #ifdef do_mc
1314       data[n].fTrackID[0] = c->GetLabel(0);
1315       data[n].fTrackID[1] = c->GetLabel(1);
1316       data[n].fTrackID[2] = c->GetLabel(2);
1317 #endif
1318       if(fMC) fprintf(fMC,"%d %d\n",data[n].fID,c->GetLabel(0));
1319       n++;
1320     }
1321   }
1322   for(Int_t i=0;i<carray.GetTree()->GetEntries();i++){
1323     Int_t row = rows[i];
1324     Int_t sector = sects[i];
1325     if(carray.GetRow(sector,row))
1326       carray.ClearRow(sector,row);
1327   }
1328
1329   delete [] clusterrow;
1330   delete [] rows;
1331   delete [] sects;
1332   savedir->cd();   
1333
1334   return data;
1335 }
1336