3 // Author: Uli Frankenfeld <mailto:franken@fi.uib.no>, Anders Vestbo <mailto:vestbo$fi.uib.no>, C. Loizides <mailto:loizides@ikf.uni-frankfurt.de>
4 //*-- Copyright © ALICE HLT Group
6 #include "AliHLTStandardIncludes.h"
7 #include <TClonesArray.h>
12 #include <AliRunLoader.h>
14 #include <AliTPCParamSR.h>
15 #include <AliTPCDigitsArray.h>
16 #include <AliTPCClustersArray.h>
17 #include <AliTPCcluster.h>
18 #include <AliTPCClustersRow.h>
19 #include <AliSimDigits.h>
21 #include "AliHLTLogging.h"
22 #include "AliHLTTransform.h"
23 #include "AliHLTMemHandler.h"
24 #include "AliHLTDigitData.h"
25 #include "AliHLTTrackSegmentData.h"
26 #include "AliHLTSpacePointData.h"
27 #include "AliHLTTrackArray.h"
28 #include "AliHLTFileHandler.h"
34 /** \class AliHLTFileHandler
36 //_____________________________________________________________
39 // The HLT ROOT <-> binary files handling class
41 // This class provides the interface between AliROOT files,
42 // and HLT binary files. It should be used for converting
43 // TPC data stored in AliROOT format (outputfile from a simulation),
44 // into the data format currently used by in the HLT framework.
45 // This enables the possibility to always use the same data format,
46 // whether you are using a binary file as an input, or a AliROOT file.
48 // For example on how to create binary files from a AliROOT simulation,
49 // see example macro exa/Binary.C.
51 // For reading a AliROOT file into HLT format in memory, do the following:
53 // AliHLTFileHandler file;
54 // file.Init(slice,patch);
55 // file.SetAliInput("galice.root");
56 // AliHLTDigitRowData *dataPt = (AliHLTDigitRowData*)file.AliDigits2Memory(nrows,eventnr);
58 // All the data are then stored in memory and accessible via the pointer dataPt.
59 // Accesing the data is then identical to the example 1) showed in AliHLTMemHandler class.
61 // For converting the data back, and writing it to a new AliROOT file do:
63 // AliHLTFileHandler file;
64 // file.Init(slice,patch);
65 // file.SetAliInput("galice.root");
66 // file.Init(slice,patch,NumberOfRowsInPatch);
67 // file.AliDigits2RootFile(dataPt,"new_galice.root");
68 // file.CloseAliInput();
72 ClassImp(AliHLTFileHandler)
74 // of course on start up the index is not created
75 Bool_t AliHLTFileHandler::fgStaticIndexCreated=kFALSE;
76 Int_t AliHLTFileHandler::fgStaticIndex[36][159];
78 void AliHLTFileHandler::CleanStaticIndex()
80 // use this static call to clean static index after
81 // running over one event
82 for(Int_t i=0;i<AliHLTTransform::GetNSlice();i++){
83 for(Int_t j=0;j<AliHLTTransform::GetNRows();j++)
84 fgStaticIndex[i][j]=-1;
86 fgStaticIndexCreated=kFALSE;
89 Int_t AliHLTFileHandler::SaveStaticIndex(Char_t *prefix,Int_t event)
91 // use this static call to store static index after
92 if(!fgStaticIndexCreated) return -1;
96 sprintf(fname,"%s-%d.txt",prefix,event);
98 sprintf(fname,"TPC.Digits.staticindex-%d.txt",event);
100 ofstream file(fname,ios::trunc);
101 if(!file.good()) return -1;
103 for(Int_t i=0;i<AliHLTTransform::GetNSlice();i++){
104 for(Int_t j=0;j<AliHLTTransform::GetNRows();j++)
105 file << fgStaticIndex[i][j] << " ";
112 Int_t AliHLTFileHandler::LoadStaticIndex(Char_t *prefix,Int_t event)
114 // use this static call to store static index after
115 if(fgStaticIndexCreated){
116 LOG(AliHLTLog::kWarning,"AliHLTFileHandler::LoadStaticIndex","Inxed")
117 <<"Static index already created, will overwrite"<<ENDLOG;
123 sprintf(fname,"%s-%d.txt",prefix,event);
125 sprintf(fname,"TPC.Digits.staticindex-%d.txt",event);
127 ifstream file(fname);
128 if(!file.good()) return -1;
130 for(Int_t i=0;i<AliHLTTransform::GetNSlice();i++){
131 for(Int_t j=0;j<AliHLTTransform::GetNRows();j++)
132 file >> fgStaticIndex[i][j];
136 fgStaticIndexCreated=kTRUE;
140 AliHLTFileHandler::AliHLTFileHandler(Bool_t b)
142 //Default constructor
145 fUseRunLoader = kFALSE;
151 fIndexCreated=kFALSE;
154 for(Int_t i=0;i<AliHLTTransform::GetNSlice();i++)
155 for(Int_t j=0;j<AliHLTTransform::GetNRows();j++)
158 if(fUseStaticIndex&&!fgStaticIndexCreated) CleanStaticIndex();
161 AliHLTFileHandler::~AliHLTFileHandler()
164 if(fMC) CloseMCOutput();
166 if(fInAli) CloseAliInput();
169 void AliHLTFileHandler::FreeDigitsTree()
174 LOG(AliHLTLog::kInformational,"AliHLTFileHandler::FreeDigitsTree()","Pointer")
175 <<"Cannot free digitstree, it is not present"<<ENDLOG;
181 fDigitsTree->Delete();
185 for(Int_t i=0;i<AliHLTTransform::GetNSlice();i++){
186 for(Int_t j=0;j<AliHLTTransform::GetNRows();j++)
189 fIndexCreated=kFALSE;
192 Bool_t AliHLTFileHandler::SetMCOutput(Char_t *name)
195 fMC = fopen(name,"w");
197 LOG(AliHLTLog::kWarning,"AliHLTFileHandler::SetMCOutput","File Open")
198 <<"Pointer to File = 0x0 "<<ENDLOG;
204 Bool_t AliHLTFileHandler::SetMCOutput(FILE *file)
209 LOG(AliHLTLog::kWarning,"AliHLTFileHandler::SetMCOutput","File Open")
210 <<"Pointer to File = 0x0 "<<ENDLOG;
216 void AliHLTFileHandler::CloseMCOutput()
220 LOG(AliHLTLog::kWarning,"AliHLTFileHandler::CloseMCOutPut","File Close")
221 <<"Nothing to Close"<<ENDLOG;
228 Bool_t AliHLTFileHandler::SetAliInput()
233 fParam = (AliTPCParam*)gFile->Get("75x40_100x60_150x60");
235 LOG(AliHLTLog::kWarning,"AliHLTFileHandler::SetAliInput","File")
236 <<"No TPC parameters found in \""<<gFile->GetName()
237 <<"\", creating standard parameters "
238 <<"which might not be what you want!"<<ENDLOG;
239 fParam = new AliTPCParamSR;
242 LOG(AliHLTLog::kError,"AliHLTFileHandler::SetAliInput","File Open")
243 <<"No AliTPCParam "<<AliHLTTransform::GetParamName()<<" in File "<<gFile->GetName()<<ENDLOG;
247 if(!fInAli->IsOpen()){
248 LOG(AliHLTLog::kError,"AliHLTFileHandler::SetAliInput","File Open")
249 <<"Ali File "<<fInAli->GetName()<<" does not exist"<<ENDLOG;
252 fParam = (AliTPCParam*)fInAli->Get(AliHLTTransform::GetParamName());
254 LOG(AliHLTLog::kWarning,"AliHLTFileHandler::SetAliInput","File")
255 <<"No TPC parameters found in \""<<fInAli->GetName()
256 <<"\", creating standard parameters "
257 <<"which might not be what you want!"<<ENDLOG;
258 fParam = new AliTPCParamSR;
261 LOG(AliHLTLog::kError,"AliHLTFileHandler::SetAliInput","File Open")
262 <<"No AliTPCParam "<<AliHLTTransform::GetParamName()<<" in File "<<fInAli->GetName()<<ENDLOG;
270 Bool_t AliHLTFileHandler::SetAliInput(Char_t *name)
272 //Open the AliROOT file with name.
274 fInAli= AliRunLoader::Open(name);
276 fInAli= new TFile(name,"READ");
279 LOG(AliHLTLog::kWarning,"AliHLTFileHandler::SetAliInput","File Open")
280 <<"Pointer to fInAli = 0x0 "<<ENDLOG;
283 return SetAliInput();
287 Bool_t AliHLTFileHandler::SetAliInput(AliRunLoader *runLoader)
289 //set ali input as runloader
291 fUseRunLoader = kTRUE;
293 LOG(AliHLTLog::kWarning,"AliHLTFileHandler::SetAliInput","File Open")
294 <<"Pointer to AliRunLoader = 0x0 "<<ENDLOG;
297 return SetAliInput();
302 Bool_t AliHLTFileHandler::SetAliInput(TFile */*file*/)
304 //Specify already opened AliROOT file to use as an input.
305 LOG(AliHLTLog::kFatal,"AliHLTFileHandler::SetAliInput","File Open")
306 <<"This function is not supported for NEWIO, check ALIHLT_USENEWIO settings in Makefile.conf"<<ENDLOG;
310 Bool_t AliHLTFileHandler::SetAliInput(TFile *file)
312 //Specify already opened AliROOT file to use as an input.
315 LOG(AliHLTLog::kWarning,"AliHLTFileHandler::SetAliInput","File Open")
316 <<"Pointer to fInAli = 0x0 "<<ENDLOG;
319 return SetAliInput();
323 void AliHLTFileHandler::CloseAliInput()
327 if(fUseRunLoader) return;
330 LOG(AliHLTLog::kWarning,"AliHLTFileHandler::CloseAliInput","RunLoader")
331 <<"Nothing to Close"<<ENDLOG;
335 if(fInAli->IsOpen()) fInAli->Close();
342 Bool_t AliHLTFileHandler::IsDigit(Int_t event)
344 //Check if there is a TPC digit tree in the current file.
345 //Return kTRUE if tree was found, and kFALSE if not found.
348 LOG(AliHLTLog::kWarning,"AliHLTFileHandler::IsDigit","File")
349 <<"Pointer to fInAli = 0x0 "<<ENDLOG;
350 return kTRUE; //maybe you are using binary input which is Digits!
353 AliLoader* tpcLoader = fInAli->GetLoader("TPCLoader");
355 LOG(AliHLTLog::kWarning,"AliHLTFileHandlerNewIO::IsDigit","File")
356 <<"Pointer to AliLoader for TPC = 0x0 "<<ENDLOG;
359 fInAli->GetEvent(event);
360 tpcLoader->LoadDigits();
361 TTree *t=tpcLoader->TreeD();
364 sprintf(name,"TreeD_%s_%d",AliHLTTransform::GetParamName(),event);
365 TTree *t=(TTree*)fInAli->Get(name);
368 LOG(AliHLTLog::kInformational,"AliHLTFileHandlerNewIO::IsDigit","File Type")
369 <<"Found Digit Tree -> Use Fast Cluster Finder"<<ENDLOG;
373 LOG(AliHLTLog::kInformational,"AliHLTFileHandlerNewIO::IsDigit","File Type")
374 <<"No Digit Tree -> Use Cluster Tree"<<ENDLOG;
379 ///////////////////////////////////////// Digit IO
380 Bool_t AliHLTFileHandler::AliDigits2Binary(Int_t event,Bool_t altro)
382 //save alidigits as binary
385 AliHLTDigitRowData* data = 0;
387 data = AliAltroDigits2Memory(nrow,event);
389 data = AliDigits2Memory(nrow,event);
390 out = Memory2Binary(nrow,data);
395 Bool_t AliHLTFileHandler::AliDigits2CompBinary(Int_t event,Bool_t altro)
397 //Convert AliROOT TPC data, into HLT data format.
398 //event specifies the event you want in the aliroot file.
402 AliHLTDigitRowData *digits=0;
404 digits = AliAltroDigits2Memory(ndigits,event);
406 digits = AliDigits2Memory(ndigits,event);
407 out = Memory2CompBinary(ndigits,digits);
412 Bool_t AliHLTFileHandler::CreateIndex()
414 //create the access index or copy from static index
415 fIndexCreated=kFALSE;
417 if(!fgStaticIndexCreated || !fUseStaticIndex) { //we have to create index
418 LOG(AliHLTLog::kInformational,"AliHLTFileHandler::CreateIndex","Index")
419 <<"Starting to create index, this can take a while."<<ENDLOG;
421 for(Int_t n=0; n<fDigitsTree->GetEntries(); n++) {
424 fDigitsTree->GetEvent(n);
425 fParam->AdjustSectorRow(fDigits->GetID(),sector,row);
426 if(!AliHLTTransform::Sector2Slice(lslice,lrow,sector,row)){
427 LOG(AliHLTLog::kError,"AliHLTFileHandler::CreateIndex","Slice/Row")
428 <<AliHLTLog::kDec<<"Index could not be created. Wrong values "
429 <<sector<<" "<<row<<ENDLOG;
432 if(fIndex[lslice][lrow]==-1) {
433 fIndex[lslice][lrow]=n;
436 if(fUseStaticIndex) { // create static index
437 for(Int_t i=0;i<AliHLTTransform::GetNSlice();i++){
438 for(Int_t j=0;j<AliHLTTransform::GetNRows();j++)
439 fgStaticIndex[i][j]=fIndex[i][j];
441 fgStaticIndexCreated=kTRUE; //remember that index has been created
444 LOG(AliHLTLog::kInformational,"AliHLTFileHandler::CreateIndex","Index")
445 <<"Index successfully created."<<ENDLOG;
447 } else if(fUseStaticIndex) { //simply copy static index
448 for(Int_t i=0;i<AliHLTTransform::GetNSlice();i++){
449 for(Int_t j=0;j<AliHLTTransform::GetNRows();j++)
450 fIndex[i][j]=fgStaticIndex[i][j];
453 LOG(AliHLTLog::kInformational,"AliHLTFileHandler::CreateIndex","Index")
454 <<"Index successfully taken from static copy."<<ENDLOG;
460 AliHLTDigitRowData * AliHLTFileHandler::AliDigits2Memory(UInt_t & nrow,Int_t event)
462 //Read data from AliROOT file into memory, and store it in the HLT data format.
463 //Returns a pointer to the data.
465 AliHLTDigitRowData *data = 0;
469 LOG(AliHLTLog::kWarning,"AliHLTFileHandler::AliDigits2Memory","File")
470 <<"No Input avalible: Pointer to fInAli == NULL"<<ENDLOG;
475 if(!fInAli->IsOpen()){
476 LOG(AliHLTLog::kWarning,"AliHLTFileHandler::AliDigits2Memory","File")
477 <<"No Input avalible: TFile not opened"<<ENDLOG;
483 if(!GetDigitsTree(event)) return 0;
486 Int_t time,pad,sector,row;
490 Int_t entries = (Int_t)fDigitsTree->GetEntries();
492 LOG(AliHLTLog::kWarning,"AliHLTFileHandler::AliDigits2Memory","ndigits")
493 <<"No TPC digits (entries==0)!"<<ENDLOG;
494 nrow = (UInt_t)(fRowMax-fRowMin+1);
495 Int_t size = nrow*sizeof(AliHLTDigitRowData);
496 data=(AliHLTDigitRowData*) Allocate(size);
497 AliHLTDigitRowData *tempPt = data;
498 for(Int_t r=fRowMin;r<=fRowMax;r++){
506 Int_t * ndigits = new Int_t[fRowMax+1];
509 for(Int_t r=fRowMin;r<=fRowMax;r++){
510 Int_t n=fIndex[fSlice][r];
511 if(n!=-1){ //data on that row
512 fDigitsTree->GetEvent(n);
513 fParam->AdjustSectorRow(fDigits->GetID(),sector,row);
514 AliHLTTransform::Sector2Slice(lslice,lrow,sector,row);
517 LOG(AliHLTLog::kError,"AliHLTFileHandler::AliDigits2Memory","Row")
518 <<AliHLTLog::kDec<<"Rows in slice " << fSlice << " dont match "<<lrow<<" "<<r<<ENDLOG;
525 time=fDigits->CurrentRow();
526 pad=fDigits->CurrentColumn();
527 dig = fDigits->GetDigit(time,pad);
528 if(dig <= fParam->GetZeroSup()) continue;
529 if(dig >= AliHLTTransform::GetADCSat())
530 dig = AliHLTTransform::GetADCSat();
532 AliHLTTransform::Raw2Local(xyz,sector,row,pad,time);
533 // if(fParam->GetPadRowRadii(sector,row)<230./250.*fabs(xyz[2]))
534 // continue; // why 230???
536 ndigits[lrow]++; //for this row only
537 ndigitcount++; //total number of digits to be published
539 } while (fDigits->Next());
540 //cout << lrow << " " << ndigits[lrow] << " - " << ndigitcount << endl;
545 Int_t size = sizeof(AliHLTDigitData)*ndigitcount
546 + nrows*sizeof(AliHLTDigitRowData);
548 LOG(AliHLTLog::kDebug,"AliHLTFileHandler::AliDigits2Memory","Digits")
549 <<AliHLTLog::kDec<<"Found "<<ndigitcount<<" Digits"<<ENDLOG;
551 data=(AliHLTDigitRowData*) Allocate(size);
552 nrow = (UInt_t)nrows;
553 AliHLTDigitRowData *tempPt = data;
555 for(Int_t r=fRowMin;r<=fRowMax;r++){
556 Int_t n=fIndex[fSlice][r];
560 if(n!=-1){//data on that row
561 fDigitsTree->GetEvent(n);
562 fParam->AdjustSectorRow(fDigits->GetID(),sector,row);
563 AliHLTTransform::Sector2Slice(lslice,lrow,sector,row);
565 LOG(AliHLTLog::kError,"AliHLTFileHandler::AliDigits2Memory","Row")
566 <<AliHLTLog::kDec<<"Rows on slice " << fSlice << " dont match "<<lrow<<" "<<r<<ENDLOG;
570 tempPt->fNDigit = ndigits[lrow];
575 time=fDigits->CurrentRow();
576 pad=fDigits->CurrentColumn();
577 dig = fDigits->GetDigit(time,pad);
578 if (dig <= fParam->GetZeroSup()) continue;
579 if(dig >= AliHLTTransform::GetADCSat())
580 dig = AliHLTTransform::GetADCSat();
582 //Exclude data outside cone:
583 AliHLTTransform::Raw2Local(xyz,sector,row,pad,time);
584 // if(fParam->GetPadRowRadii(sector,row)<230./250.*fabs(xyz[2]))
585 // continue; // why 230???
587 if(localcount >= ndigits[lrow])
588 LOG(AliHLTLog::kFatal,"AliHLTFileHandler::AliDigits2Binary","Memory")
589 <<AliHLTLog::kDec<<"Mismatch: localcount "<<localcount<<" ndigits "
590 <<ndigits[lrow]<<ENDLOG;
592 tempPt->fDigitData[localcount].fCharge=dig;
593 tempPt->fDigitData[localcount].fPad=pad;
594 tempPt->fDigitData[localcount].fTime=time;
596 tempPt->fDigitData[localcount].fTrackID[0] = fDigits->GetTrackID(time,pad,0);
597 tempPt->fDigitData[localcount].fTrackID[1] = fDigits->GetTrackID(time,pad,1);
598 tempPt->fDigitData[localcount].fTrackID[2] = fDigits->GetTrackID(time,pad,2);
601 } while (fDigits->Next());
603 Byte_t *tmp = (Byte_t*)tempPt;
604 Int_t size = sizeof(AliHLTDigitRowData)
605 + ndigits[lrow]*sizeof(AliHLTDigitData);
607 tempPt = (AliHLTDigitRowData*)tmp;
613 AliHLTDigitRowData * AliHLTFileHandler::AliAltroDigits2Memory(UInt_t & nrow,Int_t event,Bool_t eventmerge)
615 //Read data from AliROOT file into memory, and store it in the HLT data format.
616 //Returns a pointer to the data.
617 //This functions filter out single timebins, which is noise. The timebins which
618 //are removed are timebins which have the 4 zero neighbours;
619 //(pad-1,time),(pad+1,time),(pad,time-1),(pad,time+1).
621 AliHLTDigitRowData *data = 0;
625 LOG(AliHLTLog::kWarning,"AliHLTFileHandler::AliAltroDigits2Memory","File")
626 <<"No Input avalible: Pointer to TFile == NULL"<<ENDLOG;
630 if(!fInAli->IsOpen()){
631 LOG(AliHLTLog::kWarning,"AliHLTFileHandler::AliAltroDigits2Memory","File")
632 <<"No Input avalible: TFile not opened"<<ENDLOG;
636 if(eventmerge == kTRUE && event >= 1024)
638 LOG(AliHLTLog::kError,"AliHLTFileHandler::AliAltroDigits2Memory","TrackIDs")
639 <<"Too many events if you want to merge!"<<ENDLOG;
645 /* Dont understand why we have to do
646 reload the tree, but otherwise the code crashes */
648 if(!GetDigitsTree(event)) return 0;
651 if(!GetDigitsTree(event)) return 0;
656 Int_t time,pad,sector,row;
659 Int_t entries = (Int_t)fDigitsTree->GetEntries();
661 LOG(AliHLTLog::kWarning,"AliHLTFileHandler::AliAltroDigits2Memory","ndigits")
662 <<"No TPC digits (entries==0)!"<<ENDLOG;
663 nrow = (UInt_t)(fRowMax-fRowMin+1);
664 Int_t size = nrow*sizeof(AliHLTDigitRowData);
665 data=(AliHLTDigitRowData*) Allocate(size);
666 AliHLTDigitRowData *tempPt = data;
667 for(Int_t r=fRowMin;r<=fRowMax;r++){
674 Int_t * ndigits = new Int_t[fRowMax+1];
676 Int_t zerosupval=AliHLTTransform::GetZeroSup();
679 for(Int_t r=fRowMin;r<=fRowMax;r++){
680 Int_t n=fIndex[fSlice][r];
684 if(n!=-1){//data on that row
685 fDigitsTree->GetEvent(n);
686 fParam->AdjustSectorRow(fDigits->GetID(),sector,row);
687 AliHLTTransform::Sector2Slice(lslice,lrow,sector,row);
688 //cout << lslice << " " << fSlice << " " << lrow << " " << r << " " << sector << " " << row << endl;
689 if((lslice!=fSlice)||(lrow!=r)){
690 LOG(AliHLTLog::kError,"AliHLTFileHandler::AliAltroDigits2Memory","Row")
691 <<AliHLTLog::kDec<<"Rows on slice " << fSlice << " dont match "<<lrow<<" "<<r<<ENDLOG;
695 fDigits->ExpandBuffer();
696 fDigits->ExpandTrackBuffer();
697 for(Int_t i=0; i<fDigits->GetNCols(); i++){
698 for(Int_t j=0; j<fDigits->GetNRows(); j++){
701 dig = fDigits->GetDigitFast(time,pad);
702 if(dig <= zerosupval) continue;
703 if(dig >= AliHLTTransform::GetADCSat())
704 dig = AliHLTTransform::GetADCSat();
706 //Check for single timebins, and remove them because they are noise for sure.
707 if(i>0 && i<fDigits->GetNCols()-1 && j>0 && j<fDigits->GetNRows()-1)
708 if(fDigits->GetDigitFast(time,pad-1)<=zerosupval &&
709 fDigits->GetDigitFast(time-1,pad)<=zerosupval &&
710 fDigits->GetDigitFast(time+1,pad)<=zerosupval &&
711 fDigits->GetDigitFast(time,pad+1)<=zerosupval)
717 if(j < fDigits->GetNRows()-1 && j > 0)
719 if(fDigits->GetDigitFast(time-1,pad)<=zerosupval &&
720 fDigits->GetDigitFast(time+1,pad)<=zerosupval &&
721 fDigits->GetDigitFast(time,pad+1)<=zerosupval)
726 if(fDigits->GetDigitFast(time-1,pad)<=zerosupval &&
727 fDigits->GetDigitFast(time,pad+1)<=zerosupval)
733 if(i < fDigits->GetNCols()-1 && i > 0)
735 if(fDigits->GetDigitFast(time,pad-1)<=zerosupval &&
736 fDigits->GetDigitFast(time,pad+1)<=zerosupval &&
737 fDigits->GetDigitFast(time+1,pad)<=zerosupval)
742 if(fDigits->GetDigitFast(time,pad-1)<=zerosupval &&
743 fDigits->GetDigitFast(time+1,pad)<=zerosupval)
748 if(i==fDigits->GetNCols()-1)
750 if(j>0 && j<fDigits->GetNRows()-1)
752 if(fDigits->GetDigitFast(time-1,pad)<=zerosupval &&
753 fDigits->GetDigitFast(time+1,pad)<=zerosupval &&
754 fDigits->GetDigitFast(time,pad-1)<=zerosupval)
757 else if(j==0 && j<fDigits->GetNRows()-1)
759 if(fDigits->GetDigitFast(time+1,pad)<=zerosupval &&
760 fDigits->GetDigitFast(time,pad-1)<=zerosupval)
765 if(fDigits->GetDigitFast(time-1,pad)<=zerosupval &&
766 fDigits->GetDigitFast(time,pad-1)<=zerosupval)
771 if(j==fDigits->GetNRows()-1)
773 if(i>0 && i<fDigits->GetNCols()-1)
775 if(fDigits->GetDigitFast(time,pad-1)<=zerosupval &&
776 fDigits->GetDigitFast(time,pad+1)<=zerosupval &&
777 fDigits->GetDigitFast(time-1,pad)<=zerosupval)
780 else if(i==0 && fDigits->GetNCols()-1)
782 if(fDigits->GetDigitFast(time,pad+1)<=zerosupval &&
783 fDigits->GetDigitFast(time-1,pad)<=zerosupval)
788 if(fDigits->GetDigitFast(time,pad-1)<=zerosupval &&
789 fDigits->GetDigitFast(time-1,pad)<=zerosupval)
794 AliHLTTransform::Raw2Local(xyz,sector,row,pad,time);
795 // if(fParam->GetPadRowRadii(sector,row)<230./250.*fabs(xyz[2]))
798 ndigits[lrow]++; //for this row only
799 ndigitcount++; //total number of digits to be published
806 Int_t size = sizeof(AliHLTDigitData)*ndigitcount
807 + nrows*sizeof(AliHLTDigitRowData);
809 LOG(AliHLTLog::kDebug,"AliHLTFileHandler::AliAltroDigits2Memory","Digits")
810 <<AliHLTLog::kDec<<"Found "<<ndigitcount<<" Digits"<<ENDLOG;
812 data=(AliHLTDigitRowData*) Allocate(size);
813 nrow = (UInt_t)nrows;
814 AliHLTDigitRowData *tempPt = data;
816 for(Int_t r=fRowMin;r<=fRowMax;r++){
817 Int_t n=fIndex[fSlice][r];
820 if(n!=-1){ //no data on that row
821 fDigitsTree->GetEvent(n);
822 fParam->AdjustSectorRow(fDigits->GetID(),sector,row);
823 AliHLTTransform::Sector2Slice(lslice,lrow,sector,row);
826 LOG(AliHLTLog::kError,"AliHLTFileHandler::AliAltroDigits2Memory","Row")
827 <<AliHLTLog::kDec<<"Rows on slice " << fSlice << " dont match "<<lrow<<" "<<r<<ENDLOG;
831 tempPt->fNDigit = ndigits[lrow];
834 fDigits->ExpandBuffer();
835 fDigits->ExpandTrackBuffer();
836 for(Int_t i=0; i<fDigits->GetNCols(); i++){
837 for(Int_t j=0; j<fDigits->GetNRows(); j++){
840 dig = fDigits->GetDigitFast(time,pad);
841 if(dig <= zerosupval) continue;
842 if(dig >= AliHLTTransform::GetADCSat())
843 dig = AliHLTTransform::GetADCSat();
845 //Check for single timebins, and remove them because they are noise for sure.
846 if(i>0 && i<fDigits->GetNCols()-1 && j>0 && j<fDigits->GetNRows()-1)
847 if(fDigits->GetDigitFast(time,pad-1)<=zerosupval &&
848 fDigits->GetDigitFast(time-1,pad)<=zerosupval &&
849 fDigits->GetDigitFast(time+1,pad)<=zerosupval &&
850 fDigits->GetDigitFast(time,pad+1)<=zerosupval)
856 if(j < fDigits->GetNRows()-1 && j > 0)
858 if(fDigits->GetDigitFast(time-1,pad)<=zerosupval &&
859 fDigits->GetDigitFast(time+1,pad)<=zerosupval &&
860 fDigits->GetDigitFast(time,pad+1)<=zerosupval)
865 if(fDigits->GetDigitFast(time-1,pad)<=zerosupval &&
866 fDigits->GetDigitFast(time,pad+1)<=zerosupval)
872 if(i < fDigits->GetNCols()-1 && i > 0)
874 if(fDigits->GetDigitFast(time,pad-1)<=zerosupval &&
875 fDigits->GetDigitFast(time,pad+1)<=zerosupval &&
876 fDigits->GetDigitFast(time+1,pad)<=zerosupval)
881 if(fDigits->GetDigitFast(time,pad-1)<=zerosupval &&
882 fDigits->GetDigitFast(time+1,pad)<=zerosupval)
887 if(i == fDigits->GetNCols()-1)
889 if(j>0 && j<fDigits->GetNRows()-1)
891 if(fDigits->GetDigitFast(time-1,pad)<=zerosupval &&
892 fDigits->GetDigitFast(time+1,pad)<=zerosupval &&
893 fDigits->GetDigitFast(time,pad-1)<=zerosupval)
896 else if(j==0 && j<fDigits->GetNRows()-1)
898 if(fDigits->GetDigitFast(time+1,pad)<=zerosupval &&
899 fDigits->GetDigitFast(time,pad-1)<=zerosupval)
904 if(fDigits->GetDigitFast(time-1,pad)<=zerosupval &&
905 fDigits->GetDigitFast(time,pad-1)<=zerosupval)
909 if(j==fDigits->GetNRows()-1)
911 if(i>0 && i<fDigits->GetNCols()-1)
913 if(fDigits->GetDigitFast(time,pad-1)<=zerosupval &&
914 fDigits->GetDigitFast(time,pad+1)<=zerosupval &&
915 fDigits->GetDigitFast(time-1,pad)<=zerosupval)
918 else if(i==0 && fDigits->GetNCols()-1)
920 if(fDigits->GetDigitFast(time,pad+1)<=zerosupval &&
921 fDigits->GetDigitFast(time-1,pad)<=zerosupval)
926 if(fDigits->GetDigitFast(time,pad-1)<=zerosupval &&
927 fDigits->GetDigitFast(time-1,pad)<=zerosupval)
932 AliHLTTransform::Raw2Local(xyz,sector,row,pad,time);
933 // if(fParam->GetPadRowRadii(sector,row)<230./250.*fabs(xyz[2]))
936 if(localcount >= ndigits[lrow])
937 LOG(AliHLTLog::kFatal,"AliHLTFileHandler::AliAltroDigits2Binary","Memory")
938 <<AliHLTLog::kDec<<"Mismatch: localcount "<<localcount<<" ndigits "
939 <<ndigits[lrow]<<ENDLOG;
941 tempPt->fDigitData[localcount].fCharge=dig;
942 tempPt->fDigitData[localcount].fPad=pad;
943 tempPt->fDigitData[localcount].fTime=time;
945 tempPt->fDigitData[localcount].fTrackID[0] = (fDigits->GetTrackIDFast(time,pad,0)-2);
946 tempPt->fDigitData[localcount].fTrackID[1] = (fDigits->GetTrackIDFast(time,pad,1)-2);
947 tempPt->fDigitData[localcount].fTrackID[2] = (fDigits->GetTrackIDFast(time,pad,2)-2);
948 if(eventmerge == kTRUE) //careful track mc info will be touched
949 {//Event are going to be merged, so event number is stored in the upper 10 bits.
950 tempPt->fDigitData[localcount].fTrackID[0] += 128; //leave some room
951 tempPt->fDigitData[localcount].fTrackID[1] += 128; //for neg. numbers
952 tempPt->fDigitData[localcount].fTrackID[2] += 128;
953 tempPt->fDigitData[localcount].fTrackID[0] += ((event&0x3ff)<<22);
954 tempPt->fDigitData[localcount].fTrackID[1] += ((event&0x3ff)<<22);
955 tempPt->fDigitData[localcount].fTrackID[2] += ((event&0x3ff)<<22);
962 Byte_t *tmp = (Byte_t*)tempPt;
963 Int_t size = sizeof(AliHLTDigitRowData)
964 + ndigits[r]*sizeof(AliHLTDigitData);
966 tempPt = (AliHLTDigitRowData*)tmp;
972 Bool_t AliHLTFileHandler::GetDigitsTree(Int_t event)
974 //Connects to the TPC digit tree in the AliROOT file.
976 AliLoader* tpcLoader = fInAli->GetLoader("TPCLoader");
978 LOG(AliHLTLog::kWarning,"AliHLTFileHandler::GetDigitsTree","File")
979 <<"Pointer to AliLoader for TPC = 0x0 "<<ENDLOG;
982 fInAli->GetEvent(event);
983 tpcLoader->LoadDigits();
984 fDigitsTree = tpcLoader->TreeD();
988 sprintf(dname,"TreeD_%s_%d",AliHLTTransform::GetParamName(),event);
989 fDigitsTree = (TTree*)fInAli->Get(dname);
993 LOG(AliHLTLog::kError,"AliHLTFileHandler::GetDigitsTree","Digits Tree")
994 <<AliHLTLog::kHex<<"Error getting digitstree "<<(void*)fDigitsTree<<ENDLOG;
997 fDigitsTree->GetBranch("Segment")->SetAddress(&fDigits);
999 if(!fIndexCreated) return CreateIndex();
1003 void AliHLTFileHandler::AliDigits2RootFile(AliHLTDigitRowData *rowPt,Char_t *new_digitsfile)
1005 //Write the data stored in rowPt, into a new AliROOT file.
1006 //The data is stored in the AliROOT format
1007 //This is specially a nice thing if you have modified data, and wants to run it
1008 //through the offline reconstruction chain.
1009 //The arguments is a pointer to the data, and the name of the new AliROOT file.
1010 //Remember to pass the original AliROOT file (the one that contains the original
1011 //simulated data) to this object, in order to retrieve the MC id's of the digits.
1015 LOG(AliHLTLog::kError,"AliHLTFileHandler::AliDigits2RootFile","File")
1016 <<"No rootfile "<<ENDLOG;
1021 LOG(AliHLTLog::kError,"AliHLTFileHandler::AliDigits2RootFile","File")
1022 <<"No parameter object. Run on rootfile "<<ENDLOG;
1027 //Get the original digitstree:
1028 AliLoader* tpcLoader = fInAli->GetLoader("TPCLoader");
1030 LOG(AliHLTLog::kWarning,"AliHLTFileHandler::AliDigits2RootFile","File")
1031 <<"Pointer to AliLoader for TPC = 0x0 "<<ENDLOG;
1034 tpcLoader->LoadDigits();
1035 TTree *t=tpcLoader->TreeD();
1037 AliTPCDigitsArray *old_array = new AliTPCDigitsArray();
1038 old_array->Setup(fParam);
1039 old_array->SetClass("AliSimDigits");
1041 Bool_t ok = old_array->ConnectTree(t);
1044 LOG(AliHLTLog::kError,"AliHLTFileHandler::AliDigits2RootFile","File")
1045 << "No digits tree object" << ENDLOG;
1049 tpcLoader->SetDigitsFileName(new_digitsfile);
1050 tpcLoader->MakeDigitsContainer();
1052 //setup a new one, or connect it to the existing one:
1053 AliTPCDigitsArray *arr = new AliTPCDigitsArray();
1054 arr->SetClass("AliSimDigits");
1056 arr->MakeTree(tpcLoader->TreeD());
1059 //Get the original digitstree:
1061 sprintf(dname,"TreeD_%s_0",AliHLTTransform::GetParamName());
1064 AliTPCDigitsArray *old_array = new AliTPCDigitsArray();
1065 old_array->Setup(fParam);
1066 old_array->SetClass("AliSimDigits");
1068 Bool_t ok = old_array->ConnectTree(dname);
1071 LOG(AliHLTLog::kError,"AliHLTFileHandler::AliDigits2RootFile","File")
1072 <<"No digits tree object." <<ENDLOG;
1076 Bool_t create=kFALSE;
1079 if(gSystem->AccessPathName(new_digitsfile))
1081 LOG(AliHLTLog::kInformational,"AliHLTFileHandler::AliDigits2RootFile","File")
1082 <<"Creating new file "<<new_digitsfile<<ENDLOG;
1084 digFile = TFile::Open(new_digitsfile,"RECREATE");
1085 fParam->Write(fParam->GetTitle());
1090 digFile = TFile::Open(new_digitsfile,"UPDATE");
1093 if(!digFile->IsOpen())
1095 LOG(AliHLTLog::kError,"AliHLTFileHandler::AliDigits2RootFile","Rootfile")
1096 <<"Error opening rootfile "<<new_digitsfile<<ENDLOG;
1102 //setup a new one, or connect it to the existing one:
1103 AliTPCDigitsArray *arr = new AliTPCDigitsArray();
1104 arr->SetClass("AliSimDigits");
1110 Bool_t ok = arr->ConnectTree(dname);
1113 LOG(AliHLTLog::kError,"AliHLTFileHandler::AliDigits2RootFile","Rootfile")
1114 <<"No digits tree object in existing file"<<ENDLOG;
1120 Int_t digcounter=0,trackID[3];
1122 for(Int_t i=fRowMin; i<=fRowMax; i++)
1125 if((Int_t)rowPt->fRow != i)
1126 LOG(AliHLTLog::kWarning,"AliHLTFileHandler::AliDigits2RootFile","Data")
1127 <<"Mismatching row numbering "<<(Int_t)rowPt->fRow<<" "<<i<<ENDLOG;
1130 AliHLTTransform::Slice2Sector(fSlice,i,sector,row);
1132 AliSimDigits *old_dig = (AliSimDigits*)old_array->LoadRow(sector,row);
1133 AliSimDigits * dig = (AliSimDigits*)arr->CreateRow(sector,row);
1134 old_dig->ExpandBuffer();
1135 old_dig->ExpandTrackBuffer();
1136 dig->ExpandBuffer();
1137 dig->ExpandTrackBuffer();
1140 LOG(AliHLTLog::kWarning,"AliHLTFileHandler::AliDigits2RootFile","Data")
1141 <<"No padrow " << sector << " " << row <<ENDLOG;
1143 AliHLTDigitData *digPt = rowPt->fDigitData;
1145 for(UInt_t j=0; j<rowPt->fNDigit; j++)
1147 Short_t charge = (Short_t)digPt[j].fCharge;
1148 Int_t pad = (Int_t)digPt[j].fPad;
1149 Int_t time = (Int_t)digPt[j].fTime;
1151 if(charge == 0) //Only write the digits that has not been removed
1153 LOG(AliHLTLog::kWarning,"AliHLTFileHandler::AliDigits2RootFile","Data")
1154 <<"Zero charge" <<ENDLOG;
1160 //Tricks to get and set the correct track id's.
1161 for(Int_t t=0; t<3; t++)
1163 Int_t label = old_dig->GetTrackIDFast(time,pad,t);
1165 trackID[t] = label - 2;
1172 dig->SetDigitFast(charge,time,pad);
1174 for(Int_t t=0; t<3; t++)
1175 ((AliSimDigits*)dig)->SetTrackIDFast(trackID[t],time,pad,t);
1178 //cout<<"Wrote "<<digcounter<<" on row "<<i<<endl;
1179 UpdateRowPointer(rowPt);
1180 arr->StoreRow(sector,row);
1181 arr->ClearRow(sector,row);
1182 old_array->ClearRow(sector,row);
1186 sprintf(treeName,"TreeD_%s_0",fParam->GetTitle());
1189 arr->GetTree()->SetName(treeName);
1190 arr->GetTree()->AutoSave();
1191 tpcLoader->WriteDigits("OVERWRITE");
1194 arr->GetTree()->SetName(treeName);
1195 arr->GetTree()->AutoSave();
1202 ///////////////////////////////////////// Point IO
1203 Bool_t AliHLTFileHandler::AliPoints2Binary(Int_t eventn)
1208 AliHLTSpacePointData *data = AliPoints2Memory(npoint,eventn);
1209 out = Memory2Binary(npoint,data);
1214 AliHLTSpacePointData * AliHLTFileHandler::AliPoints2Memory(UInt_t & npoint,Int_t eventn)
1217 AliHLTSpacePointData *data = 0;
1220 LOG(AliHLTLog::kWarning,"AliHLTFileHandler::AliPoints2Memory","File")
1221 <<"No Input avalible: no object fInAli"<<ENDLOG;
1225 if(!fInAli->IsOpen()){
1226 LOG(AliHLTLog::kWarning,"AliHLTFileHandler::AliPoints2Memory","File")
1227 <<"No Input avalible: TFile not opend"<<ENDLOG;
1232 TDirectory *savedir = gDirectory;
1234 AliLoader* tpcLoader = fInAli->GetLoader("TPCLoader");
1236 LOG(AliHLTLog::kWarning,"AliHLTFileHandler::AliPoints2Memory","File")
1237 <<"Pointer to AliLoader for TPC = 0x0 "<<ENDLOG;
1240 fInAli->GetEvent(eventn);
1241 tpcLoader->LoadRecPoints();
1243 AliTPCClustersArray carray;
1244 carray.Setup(fParam);
1245 carray.SetClusterType("AliTPCcluster");
1246 Bool_t clusterok = carray.ConnectTree(tpcLoader->TreeR());
1251 sprintf(cname,"TreeC_TPC_%d",eventn);
1252 AliTPCClustersArray carray;
1253 carray.Setup(fParam);
1254 carray.SetClusterType("AliTPCcluster");
1255 Bool_t clusterok = carray.ConnectTree(cname);
1258 if(!clusterok) return 0;
1260 AliTPCClustersRow ** clusterrow =
1261 new AliTPCClustersRow*[ (int)carray.GetTree()->GetEntries()];
1262 Int_t *rows = new int[ (int)carray.GetTree()->GetEntries()];
1263 Int_t *sects = new int[ (int)carray.GetTree()->GetEntries()];
1267 for(Int_t i=0; i<carray.GetTree()->GetEntries(); i++){
1268 AliSegmentID *s = carray.LoadEntry(i);
1270 fParam->AdjustSectorRow(s->GetID(),sector,row);
1274 AliHLTTransform::Sector2Slice(lslice,lrow,sector,row);
1275 if(fSlice != lslice || lrow<fRowMin || lrow>fRowMax) continue;
1276 clusterrow[i] = carray.GetRow(sector,row);
1278 sum+=clusterrow[i]->GetArray()->GetEntriesFast();
1280 UInt_t size = sum*sizeof(AliHLTSpacePointData);
1282 LOG(AliHLTLog::kDebug,"AliHLTFileHandler::AliPoints2Memory","File")
1283 <<AliHLTLog::kDec<<"Found "<<sum<<" SpacePoints"<<ENDLOG;
1285 data = (AliHLTSpacePointData *) Allocate(size);
1291 for(Int_t i=0; i<carray.GetTree()->GetEntries(); i++){
1292 if(!clusterrow[i]) continue;
1293 Int_t row = rows[i];
1294 Int_t sector = sects[i];
1295 AliHLTTransform::Sector2Slice(lslice,lrow,sector,row);
1296 Int_t entries_in_row = clusterrow[i]->GetArray()->GetEntriesFast();
1297 for(Int_t j = 0;j<entries_in_row;j++){
1298 AliTPCcluster *c = (AliTPCcluster*)(*clusterrow[i])[j];
1299 data[n].fZ = c->GetZ();
1300 data[n].fY = c->GetY();
1301 data[n].fX = fParam->GetPadRowRadii(sector,row);
1302 data[n].fCharge = (UInt_t)c->GetQ();
1303 data[n].fID = n+((fSlice&0x7f)<<25)+((pat&0x7)<<22);//uli
1304 data[n].fPadRow = lrow;
1305 data[n].fSigmaY2 = c->GetSigmaY2();
1306 data[n].fSigmaZ2 = c->GetSigmaZ2();
1308 data[n].fTrackID[0] = c->GetLabel(0);
1309 data[n].fTrackID[1] = c->GetLabel(1);
1310 data[n].fTrackID[2] = c->GetLabel(2);
1312 if(fMC) fprintf(fMC,"%d %d\n",data[n].fID,c->GetLabel(0));
1316 for(Int_t i=0;i<carray.GetTree()->GetEntries();i++){
1317 Int_t row = rows[i];
1318 Int_t sector = sects[i];
1319 if(carray.GetRow(sector,row))
1320 carray.ClearRow(sector,row);
1323 delete [] clusterrow;