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