3 // Author: Uli Frankenfeld <mailto:franken@fi.uib.no>, Anders Vestbo <mailto:vestbo$fi.uib.no>
4 //*-- Copyright © Uli
6 #include "AliL3StandardIncludes.h"
7 #include <TClonesArray.h>
10 #include <AliTPCDigitsArray.h>
11 #include <AliTPCClustersArray.h>
12 #include <AliTPCcluster.h>
13 #include <AliTPCClustersRow.h>
14 #include <AliSimDigits.h>
16 #include "AliL3Logging.h"
17 #include "AliL3Transform.h"
18 #include "AliL3MemHandler.h"
19 #include "AliL3FileHandler.h"
20 #include "AliL3DigitData.h"
21 #include "AliL3TrackSegmentData.h"
22 #include "AliL3SpacePointData.h"
23 #include "AliL3TrackArray.h"
26 //_____________________________________________________________
29 // The HLT ROOT <-> binary files handling class
31 // This class provides the interface between AliROOT files,
32 // and HLT binary files. It should be used for converting
33 // TPC data stored in AliROOT format (outputfile from a simulation),
34 // into the data format currently used by in the HLT framework.
35 // This enables the possibility to always use the same data format,
36 // whether you are using a binary file as an input, or a AliROOT file.
38 // For example on how to create binary files from a AliROOT simulation,
39 // see example macro exa/Binary.C.
41 // For reading a AliROOT file into HLT format in memory, do the following:
43 // AliL3FileHandler file;
44 // file.Init(slice,patch);
45 // file.SetAliInput("galice.root");
46 // AliL3DigitRowData *dataPt = (AliL3DigitRowData*)file.AliDigits2Memory(nrows,eventnr);
48 // All the data are then stored in memory and accessible via the pointer dataPt.
49 // Accesing the data is then identical to the example 1) showed in AliL3MemHandler class.
51 // For converting the data back, and writing it to a new AliROOT file do:
53 // AliL3FileHandler file;
54 // file.Init(slice,patch);
55 // file.SetAliInput("galice.root");
56 // file.Init(slice,patch,NumberOfRowsInPatch);
57 // file.AliDigits2RootFile(dataPt,"new_galice.root");
58 // file.CloseAliInput();
60 ClassImp(AliL3FileHandler)
62 AliL3FileHandler::AliL3FileHandler()
73 AliL3FileHandler::~AliL3FileHandler()
76 if(fMC) CloseMCOutput();
78 if(fInAli) CloseAliInput();
82 void AliL3FileHandler::FreeDigitsTree()
86 LOG(AliL3Log::kWarning,"AliL3FileHandler::FreeDigitsTree()","Pointer")
87 <<"Cannot free digitstree, it is not present"<<ENDLOG;
91 fDigitsTree->Delete();
95 Bool_t AliL3FileHandler::SetMCOutput(char *name)
97 fMC = fopen(name,"w");
99 LOG(AliL3Log::kWarning,"AliL3FileHandler::SetMCOutput","File Open")
100 <<"Pointer to File = 0x0 "<<ENDLOG;
106 Bool_t AliL3FileHandler::SetMCOutput(FILE *file)
110 LOG(AliL3Log::kWarning,"AliL3FileHandler::SetMCOutput","File Open")
111 <<"Pointer to File = 0x0 "<<ENDLOG;
117 void AliL3FileHandler::CloseMCOutput()
120 LOG(AliL3Log::kWarning,"AliL3FileHandler::CloseMCOutPut","File Close")
121 <<"Nothing to Close"<<ENDLOG;
128 Bool_t AliL3FileHandler::SetAliInput()
130 if(!fInAli->IsOpen()){
131 LOG(AliL3Log::kError,"AliL3FileHandler::SetAliInput","File Open")
132 <<"Ali File "<<fInAli->GetName()<<" does not exist"<<ENDLOG;
135 fParam = (AliTPCParam*)fInAli->Get(AliL3Transform::GetParamName());
137 LOG(AliL3Log::kError,"AliL3FileHandler::SetAliInput","File Open")
138 <<"No AliTPCParam "<<AliL3Transform::GetParamName()<<" in File "<<fInAli->GetName()<<ENDLOG;
144 Bool_t AliL3FileHandler::SetAliInput(char *name)
146 //Open the AliROOT file with name.
148 fInAli= new TFile(name,"READ");
150 LOG(AliL3Log::kWarning,"AliL3FileHandler::SetAliInput","File Open")
151 <<"Pointer to TFile = 0x0 "<<ENDLOG;
154 return SetAliInput();
157 Bool_t AliL3FileHandler::SetAliInput(TFile *file)
159 //Specify already opened AliROOT file to use as an input.
163 LOG(AliL3Log::kWarning,"AliL3FileHandler::SetAliInput","File Open")
164 <<"Pointer to TFile = 0x0 "<<ENDLOG;
167 return SetAliInput();
170 void AliL3FileHandler::CloseAliInput()
173 LOG(AliL3Log::kWarning,"AliL3FileHandler::CloseAliInput","File Close")
174 <<"Nothing to Close"<<ENDLOG;
177 if(fInAli->IsOpen()) fInAli->Close();
183 Bool_t AliL3FileHandler::IsDigit(Int_t event)
185 //Check if there is a TPC digit tree in the current file.
186 //Return kTRUE if tree was found, and kFALSE if not found.
189 LOG(AliL3Log::kWarning,"AliL3FileHandler::IsDigit","File")
190 <<"Pointer to TFile = 0x0 "<<ENDLOG;
191 return kTRUE; //maybe you are using binary input which is Digits!!
194 sprintf(name,"TreeD_%s_%d",AliL3Transform::GetParamName(),event);
195 TTree *t=(TTree*)fInAli->Get(name);
197 LOG(AliL3Log::kInformational,"AliL3FileHandler::IsDigit","File Type")
198 <<"Found Digit Tree -> Use Fast Cluster Finder"<<ENDLOG;
202 LOG(AliL3Log::kInformational,"AliL3FileHandler::IsDigit","File Type")
203 <<"No Digit Tree -> Use Cluster Tree"<<ENDLOG;
208 ///////////////////////////////////////// Digit IO
209 Bool_t AliL3FileHandler::AliDigits2Binary(Int_t event)
213 AliL3DigitRowData* data = AliDigits2Memory(nrow,event);
214 out = Memory2Binary(nrow,data);
219 Bool_t AliL3FileHandler::AliDigits2CompBinary(Int_t event)
221 //Convert AliROOT TPC data, into HLT data format.
222 //event specifies the event you want in the aliroot file.
226 AliL3DigitRowData* digits = AliDigits2Memory(ndigits,event);
227 out = Memory2CompBinary(ndigits,digits);
232 AliL3DigitRowData * AliL3FileHandler::AliDigits2Memory(UInt_t & nrow,Int_t event)
234 //Read data from AliROOT file into memory, and store it in the HLT data format.
235 //Returns a pointer to the data.
237 AliL3DigitRowData *data = 0;
241 LOG(AliL3Log::kWarning,"AliL3FileHandler::AliDigits2Memory","File")
242 <<"No Input avalible: no object TFile"<<ENDLOG;
245 if(!fInAli->IsOpen()){
246 LOG(AliL3Log::kWarning,"AliL3FileHandler::AliDigits2Memory","File")
247 <<"No Input avalible: TFile not opened"<<ENDLOG;
252 GetDigitsTree(event);
255 Int_t time,pad,sector,row;
258 Int_t entries = (Int_t)fDigitsTree->GetEntries();
259 Int_t ndigits[entries];
263 for(Int_t n=fLastIndex; n<fDigitsTree->GetEntries(); n++)
265 fDigitsTree->GetEvent(n);
266 fParam->AdjustSectorRow(fDigits->GetID(),sector,row);
267 AliL3Transform::Sector2Slice(lslice,lrow,sector,row);
268 //if(fSlice != lslice || lrow<fRowMin || lrow>fRowMax) continue;
269 if(lslice < fSlice) continue;
270 if(lslice != fSlice) break;
271 if(lrow < fRowMin) continue;
272 if(lrow > fRowMax) break;
277 time=fDigits->CurrentRow();
278 pad=fDigits->CurrentColumn();
279 dig = fDigits->GetDigit(time,pad);
280 if(dig <= fParam->GetZeroSup()) continue;
281 if(dig >= AliL3Transform::GetADCSat())
282 dig = AliL3Transform::GetADCSat();
284 AliL3Transform::Raw2Local(xyz,sector,row,pad,time);
285 if(fParam->GetPadRowRadii(sector,row)<230./250.*fabs(xyz[2]))
288 ndigits[lrow]++; //for this row only
289 ndigitcount++; //total number of digits to be published
291 } while (fDigits->Next());
292 //cout << lrow << " " << ndigits[lrow] << " - " << ndigitcount << endl;
296 Int_t size = sizeof(AliL3DigitData)*ndigitcount
297 + nrows*sizeof(AliL3DigitRowData);
299 LOG(AliL3Log::kDebug,"AliL3FileHandler::AliDigits2Memory","Digits")
300 <<AliL3Log::kDec<<"Found "<<ndigitcount<<" Digits"<<ENDLOG;
302 data=(AliL3DigitRowData*) Allocate(size);
303 nrow = (UInt_t)nrows;
304 AliL3DigitRowData *tempPt = data;
305 for(Int_t n=fLastIndex; n<fDigitsTree->GetEntries(); n++)
307 fDigitsTree->GetEvent(n);
308 fParam->AdjustSectorRow(fDigits->GetID(),sector,row);
309 AliL3Transform::Sector2Slice(lslice,lrow,sector,row);
310 //if(fSlice != lslice || lrow<fRowMin || lrow>fRowMax) continue;
311 if(lslice < fSlice) continue;
312 if(lslice != fSlice) break;
313 if(lrow < fRowMin) continue;
314 if(lrow > fRowMax) break;
317 tempPt->fNDigit = ndigits[lrow];
322 time=fDigits->CurrentRow();
323 pad=fDigits->CurrentColumn();
324 dig = fDigits->GetDigit(time,pad);
325 if (dig <= fParam->GetZeroSup()) continue;
326 if(dig >= AliL3Transform::GetADCSat())
327 dig = AliL3Transform::GetADCSat();
329 //Exclude data outside cone:
330 AliL3Transform::Raw2Local(xyz,sector,row,pad,time);
331 if(fParam->GetPadRowRadii(sector,row)<230./250.*fabs(xyz[2]))
334 if(localcount >= ndigits[lrow])
335 LOG(AliL3Log::kFatal,"AliL3FileHandler::AliDigits2Binary","Memory")
336 <<AliL3Log::kDec<<"Mismatch: localcount "<<localcount<<" ndigits "
337 <<ndigits[lrow]<<ENDLOG;
339 tempPt->fDigitData[localcount].fCharge=dig;
340 tempPt->fDigitData[localcount].fPad=pad;
341 tempPt->fDigitData[localcount].fTime=time;
343 tempPt->fDigitData[localcount].fTrackID[0] = fDigits->GetTrackID(time,pad,0);
344 tempPt->fDigitData[localcount].fTrackID[1] = fDigits->GetTrackID(time,pad,1);
345 tempPt->fDigitData[localcount].fTrackID[2] = fDigits->GetTrackID(time,pad,2);
348 } while (fDigits->Next());
350 Byte_t *tmp = (Byte_t*)tempPt;
351 Int_t size = sizeof(AliL3DigitRowData)
352 + ndigits[lrow]*sizeof(AliL3DigitData);
354 tempPt = (AliL3DigitRowData*)tmp;
365 Bool_t AliL3FileHandler::GetDigitsTree(Int_t event)
367 //Connects to the TPC digit tree in the AliROOT file.
371 sprintf(dname,"TreeD_%s_%d",AliL3Transform::GetParamName(),event);
372 fDigitsTree = (TTree*)fInAli->Get(dname);
375 LOG(AliL3Log::kError,"AliL3FileHandler::GetDigitsTree","Digits Tree")
376 <<AliL3Log::kHex<<"Error getting digitstree "<<(Int_t)fDigitsTree<<ENDLOG;
379 fDigitsTree->GetBranch("Segment")->SetAddress(&fDigits);
383 void AliL3FileHandler::AliDigits2RootFile(AliL3DigitRowData *rowPt,Char_t *new_digitsfile)
385 //Write the data stored in rowPt, into a new AliROOT file.
386 //The data is stored in the AliROOT format
387 //This is specially a nice thing if you have modified data, and wants to run it
388 //through the offline reconstruction chain.
389 //The arguments is a pointer to the data, and the name of the new AliROOT file.
390 //Remember to pass the original AliROOT file (the one that contains the original
391 //simulated data) to this object, in order to retrieve the MC id's of the digits.
395 printf("AliL3FileHandler::AliDigits2RootFile : No rootfile\n");
400 printf("AliL3FileHandler::AliDigits2RootFile : No parameter object. Run on rootfile\n");
404 //Get the original digitstree:
406 sprintf(dname,"TreeD_%s_0",AliL3Transform::GetParamName());
409 AliTPCDigitsArray *old_array = new AliTPCDigitsArray();
410 old_array->Setup(fParam);
411 old_array->SetClass("AliSimDigits");
413 Bool_t ok = old_array->ConnectTree(dname);
416 printf("AliL3FileHandler::AliDigits2RootFile : No digits tree object\n");
420 Bool_t create=kFALSE;
423 if(gSystem->AccessPathName(new_digitsfile))
425 cout<<"AliL3FileHandler::AliDigits2RootFile : Creating new file :"<<new_digitsfile<<endl;
427 digFile = TFile::Open(new_digitsfile,"RECREATE");
428 fParam->Write(fParam->GetTitle());
433 digFile = TFile::Open(new_digitsfile,"UPDATE");
436 if(!digFile->IsOpen())
438 LOG(AliL3Log::kError,"AliL3FileHandler::AliDigits2RootFile","Rootfile")
439 <<"Error opening rootfile "<<new_digitsfile<<ENDLOG;
445 //setup a new one, or connect it to the existing one:
446 AliTPCDigitsArray *arr = new AliTPCDigitsArray();
447 arr->SetClass("AliSimDigits");
453 Bool_t ok = arr->ConnectTree(dname);
456 printf("AliL3FileHandler::AliDigits2RootFile : No digits tree object in existing file\n");
460 Int_t digcounter=0,trackID[3];
462 for(Int_t i=fRowMin; i<=fRowMax; i++)
465 if((Int_t)rowPt->fRow != i)
466 cerr<<"AliL3FileHandler::AliDigits2RootFile : Mismatching row numbering!!!"<<endl;
469 AliL3Transform::Slice2Sector(fSlice,i,sector,row);
471 AliSimDigits *old_dig = (AliSimDigits*)old_array->LoadRow(sector,row);
472 AliSimDigits * dig = (AliSimDigits*)arr->CreateRow(sector,row);
473 old_dig->ExpandBuffer();
474 old_dig->ExpandTrackBuffer();
476 dig->ExpandTrackBuffer();
479 printf("AliL3FileHandler::AliDigits2RootFile : No padrow %d %d\n",sector,row);
481 AliL3DigitData *digPt = rowPt->fDigitData;
483 for(UInt_t j=0; j<rowPt->fNDigit; j++)
485 Short_t charge = (Short_t)digPt[j].fCharge;
486 Int_t pad = (Int_t)digPt[j].fPad;
487 Int_t time = (Int_t)digPt[j].fTime;
489 if(charge == 0) //Only write the digits that has not been removed
491 cerr<<"AliL3FileHandler::AliDigits2RootFile : Zero charge!!! "<<endl;
497 //Tricks to get and set the correct track id's.
498 for(Int_t t=0; t<3; t++)
500 Int_t label = old_dig->GetTrackIDFast(time,pad,t);
502 trackID[t] = label - 2;
509 dig->SetDigitFast(charge,time,pad);
511 for(Int_t t=0; t<3; t++)
512 ((AliSimDigits*)dig)->SetTrackIDFast(trackID[t],time,pad,t);
515 //cout<<"Wrote "<<digcounter<<" on row "<<i<<endl;
516 UpdateRowPointer(rowPt);
517 arr->StoreRow(sector,row);
518 arr->ClearRow(sector,row);
519 old_array->ClearRow(sector,row);
523 sprintf(treeName,"TreeD_%s_0",fParam->GetTitle());
525 arr->GetTree()->SetName(treeName);
526 arr->GetTree()->AutoSave();
533 ///////////////////////////////////////// Point IO
534 Bool_t AliL3FileHandler::AliPoints2Binary(){
537 AliL3SpacePointData *data = AliPoints2Memory(npoint);
538 out = Memory2Binary(npoint,data);
543 AliL3SpacePointData * AliL3FileHandler::AliPoints2Memory(UInt_t & npoint){
544 AliL3SpacePointData *data = 0;
547 LOG(AliL3Log::kWarning,"AliL3FileHandler::AliPoints2Memory","File")
548 <<"No Input avalible: no object TFile"<<ENDLOG;
551 if(!fInAli->IsOpen()){
552 LOG(AliL3Log::kWarning,"AliL3FileHandler::AliPoints2Memory","File")
553 <<"No Input avalible: TFile not opend"<<ENDLOG;
557 TDirectory *savedir = gDirectory;
562 sprintf(cname,"TreeC_TPC_%d",eventn);
563 AliTPCClustersArray carray;
564 carray.Setup(fParam);
565 carray.SetClusterType("AliTPCcluster");
566 Bool_t clusterok = carray.ConnectTree(cname);
567 if(!clusterok) return 0;
569 AliTPCClustersRow ** clusterrow =
570 new AliTPCClustersRow*[ (int)carray.GetTree()->GetEntries()];
571 Int_t *rows = new int[ (int)carray.GetTree()->GetEntries()];
572 Int_t *sects = new int[ (int)carray.GetTree()->GetEntries()];
576 for(Int_t i=0; i<carray.GetTree()->GetEntries(); i++){
577 AliSegmentID *s = carray.LoadEntry(i);
579 fParam->AdjustSectorRow(s->GetID(),sector,row);
583 AliL3Transform::Sector2Slice(lslice,lrow,sector,row);
584 if(fSlice != lslice || lrow<fRowMin || lrow>fRowMax) continue;
585 clusterrow[i] = carray.GetRow(sector,row);
587 sum+=clusterrow[i]->GetArray()->GetEntriesFast();
589 UInt_t size = sum*sizeof(AliL3SpacePointData);
591 LOG(AliL3Log::kDebug,"AliL3FileHandler::AliPoints2Memory","File")
592 <<AliL3Log::kDec<<"Found "<<sum<<" SpacePoints"<<ENDLOG;
594 data = (AliL3SpacePointData *) Allocate(size);
597 for(Int_t i=0; i<carray.GetTree()->GetEntries(); i++){
598 if(!clusterrow[i]) continue;
600 Int_t sector = sects[i];
601 AliL3Transform::Sector2Slice(lslice,lrow,sector,row);
602 Int_t entries_in_row = clusterrow[i]->GetArray()->GetEntriesFast();
603 for(Int_t j = 0;j<entries_in_row;j++){
604 AliTPCcluster *c = (AliTPCcluster*)(*clusterrow[i])[j];
605 data[n].fZ = c->GetZ();
606 data[n].fY = c->GetY();
607 data[n].fX = fParam->GetPadRowRadii(sector,row);
608 data[n].fID = n+((fSlice&0x7f)<<25)+((fPatch&0x7)<<22);//uli
609 data[n].fPadRow = lrow;
610 data[n].fXYErr = sqrt(c->GetSigmaY2());
611 data[n].fZErr = sqrt(c->GetSigmaZ2());
612 if(fMC) fprintf(fMC,"%d %d\n",data[n].fID,c->GetLabel(0));
616 for(Int_t i=0;i<carray.GetTree()->GetEntries();i++){
618 Int_t sector = sects[i];
619 if(carray.GetRow(sector,row))
620 carray.ClearRow(sector,row);
623 delete [] clusterrow;