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>
9 #include <AliTPCDigitsArray.h>
10 #include <AliTPCClustersArray.h>
11 #include <AliTPCcluster.h>
12 #include <AliTPCClustersRow.h>
13 #include <AliSimDigits.h>
15 #include "AliL3Logging.h"
16 #include "AliL3Transform.h"
17 #include "AliL3MemHandler.h"
18 #include "AliL3FileHandler.h"
19 #include "AliL3DigitData.h"
20 #include "AliL3TrackSegmentData.h"
21 #include "AliL3SpacePointData.h"
22 #include "AliL3TrackArray.h"
25 //_____________________________________________________________
28 // The HLT ROOT <-> binary files handling class
30 // This class provides the interface between AliROOT files,
31 // and HLT binary files. It should be used for converting
32 // TPC data stored in AliROOT format (outputfile from a simulation),
33 // into the data format currently used by in the HLT framework.
34 // This enables the possibility to always use the same data format,
35 // whether you are using a binary file as an input, or a AliROOT file.
37 // For example on how to create binary files from a AliROOT simulation,
38 // see example macro exa/Binary.C.
40 // For reading a AliROOT file into HLT format in memory, do the following:
42 // AliL3FileHandler file;
43 // file.Init(slice,patch);
44 // file.SetAliInput("galice.root");
45 // AliL3DigitRowData *dataPt = (AliL3DigitRowData*)file.AliDigits2Memory(nrows,eventnr);
47 // All the data are then stored in memory and accessible via the pointer dataPt.
48 // Accesing the data is then identical to the example 1) showed in AliL3MemHandler class.
50 // For converting the data back, and writing it to a new AliROOT file do:
52 // AliL3FileHandler file;
53 // file.Init(slice,patch);
54 // file.SetAliInput("galice.root");
55 // file.Init(slice,patch,NumberOfRowsInPatch);
56 // file.AliDigits2RootFile(dataPt,"new_galice.root");
57 // file.CloseAliInput();
59 ClassImp(AliL3FileHandler)
61 AliL3FileHandler::AliL3FileHandler()
72 AliL3FileHandler::~AliL3FileHandler()
75 if(fMC) CloseMCOutput();
77 if(fInAli) CloseAliInput();
81 void AliL3FileHandler::FreeDigitsTree()
85 LOG(AliL3Log::kWarning,"AliL3FileHandler::FreeDigitsTree()","Pointer")
86 <<"Cannot free digitstree, it is not present"<<ENDLOG;
90 fDigitsTree->Delete();
94 Bool_t AliL3FileHandler::SetMCOutput(char *name)
96 fMC = fopen(name,"w");
98 LOG(AliL3Log::kWarning,"AliL3FileHandler::SetMCOutput","File Open")
99 <<"Pointer to File = 0x0 "<<ENDLOG;
105 Bool_t AliL3FileHandler::SetMCOutput(FILE *file)
109 LOG(AliL3Log::kWarning,"AliL3FileHandler::SetMCOutput","File Open")
110 <<"Pointer to File = 0x0 "<<ENDLOG;
116 void AliL3FileHandler::CloseMCOutput()
119 LOG(AliL3Log::kWarning,"AliL3FileHandler::CloseMCOutPut","File Close")
120 <<"Nothing to Close"<<ENDLOG;
127 Bool_t AliL3FileHandler::SetAliInput()
129 if(!fInAli->IsOpen()){
130 LOG(AliL3Log::kError,"AliL3FileHandler::SetAliInput","File Open")
131 <<"Ali File "<<fInAli->GetName()<<" does not exist"<<ENDLOG;
134 fParam = (AliTPCParam*)fInAli->Get(AliL3Transform::GetParamName());
136 LOG(AliL3Log::kError,"AliL3FileHandler::SetAliInput","File Open")
137 <<"No AliTPCParam "<<AliL3Transform::GetParamName()<<" in File "<<fInAli->GetName()<<ENDLOG;
143 Bool_t AliL3FileHandler::SetAliInput(char *name)
145 //Open the AliROOT file with name.
147 fInAli= new TFile(name,"READ");
149 LOG(AliL3Log::kWarning,"AliL3FileHandler::SetAliInput","File Open")
150 <<"Pointer to TFile = 0x0 "<<ENDLOG;
153 return SetAliInput();
156 Bool_t AliL3FileHandler::SetAliInput(TFile *file)
158 //Specify already opened AliROOT file to use as an input.
162 LOG(AliL3Log::kWarning,"AliL3FileHandler::SetAliInput","File Open")
163 <<"Pointer to TFile = 0x0 "<<ENDLOG;
166 return SetAliInput();
169 void AliL3FileHandler::CloseAliInput()
172 LOG(AliL3Log::kWarning,"AliL3FileHandler::CloseAliInput","File Close")
173 <<"Nothing to Close"<<ENDLOG;
176 if(fInAli->IsOpen()) fInAli->Close();
182 Bool_t AliL3FileHandler::IsDigit(Int_t event)
184 //Check if there is a TPC digit tree in the current file.
185 //Return kTRUE if tree was found, and kFALSE if not found.
188 LOG(AliL3Log::kWarning,"AliL3FileHandler::IsDigit","File")
189 <<"Pointer to TFile = 0x0 "<<ENDLOG;
190 return kTRUE; //maybe you are using binary input which is Digits!!
193 sprintf(name,"TreeD_%s_%d",AliL3Transform::GetParamName(),event);
194 TTree *t=(TTree*)fInAli->Get(name);
196 LOG(AliL3Log::kInformational,"AliL3FileHandler::IsDigit","File Type")
197 <<"Found Digit Tree -> Use Fast Cluster Finder"<<ENDLOG;
201 LOG(AliL3Log::kInformational,"AliL3FileHandler::IsDigit","File Type")
202 <<"No Digit Tree -> Use Cluster Tree"<<ENDLOG;
207 ///////////////////////////////////////// Digit IO
208 Bool_t AliL3FileHandler::AliDigits2Binary(Int_t event)
212 AliL3DigitRowData* data = AliDigits2Memory(nrow,event);
213 out = Memory2Binary(nrow,data);
218 Bool_t AliL3FileHandler::AliDigits2CompBinary(Int_t event)
220 //Convert AliROOT TPC data, into HLT data format.
221 //event specifies the event you want in the aliroot file.
225 AliL3DigitRowData* digits = AliDigits2Memory(ndigits,event);
226 out = Memory2CompBinary(ndigits,digits);
231 AliL3DigitRowData * AliL3FileHandler::AliDigits2Memory(UInt_t & nrow,Int_t event)
233 //Read data from AliROOT file into memory, and store it in the HLT data format.
234 //Returns a pointer to the data.
236 AliL3DigitRowData *data = 0;
240 LOG(AliL3Log::kWarning,"AliL3FileHandler::AliDigits2Memory","File")
241 <<"No Input avalible: no object TFile"<<ENDLOG;
244 if(!fInAli->IsOpen()){
245 LOG(AliL3Log::kWarning,"AliL3FileHandler::AliDigits2Memory","File")
246 <<"No Input avalible: TFile not opened"<<ENDLOG;
251 GetDigitsTree(event);
254 Int_t time,pad,sector,row;
257 Int_t entries = (Int_t)fDigitsTree->GetEntries();
258 Int_t ndigits[entries];
262 for(Int_t n=fLastIndex; n<fDigitsTree->GetEntries(); n++)
264 fDigitsTree->GetEvent(n);
265 fParam->AdjustSectorRow(fDigits->GetID(),sector,row);
266 AliL3Transform::Sector2Slice(lslice,lrow,sector,row);
267 //if(fSlice != lslice || lrow<fRowMin || lrow>fRowMax) continue;
268 if(lslice < fSlice) continue;
269 if(lslice != fSlice) break;
270 if(lrow < fRowMin) continue;
271 if(lrow > fRowMax) break;
276 time=fDigits->CurrentRow();
277 pad=fDigits->CurrentColumn();
278 dig = fDigits->GetDigit(time,pad);
279 if(dig<=fParam->GetZeroSup()) continue;
281 AliL3Transform::Raw2Local(xyz,sector,row,pad,time);
282 if(fParam->GetPadRowRadii(sector,row)<230./250.*fabs(xyz[2]))
285 ndigits[lrow]++; //for this row only
286 ndigitcount++; //total number of digits to be published
288 } while (fDigits->Next());
289 //cout << lrow << " " << ndigits[lrow] << " - " << ndigitcount << endl;
293 Int_t size = sizeof(AliL3DigitData)*ndigitcount
294 + nrows*sizeof(AliL3DigitRowData);
296 LOG(AliL3Log::kDebug,"AliL3FileHandler::AliDigits2Memory","Digits")
297 <<AliL3Log::kDec<<"Found "<<ndigitcount<<" Digits"<<ENDLOG;
299 data=(AliL3DigitRowData*) Allocate(size);
300 nrow = (UInt_t)nrows;
301 AliL3DigitRowData *tempPt = data;
302 for(Int_t n=fLastIndex; n<fDigitsTree->GetEntries(); n++)
304 fDigitsTree->GetEvent(n);
305 fParam->AdjustSectorRow(fDigits->GetID(),sector,row);
306 AliL3Transform::Sector2Slice(lslice,lrow,sector,row);
307 //if(fSlice != lslice || lrow<fRowMin || lrow>fRowMax) continue;
308 if(lslice < fSlice) continue;
309 if(lslice != fSlice) break;
310 if(lrow < fRowMin) continue;
311 if(lrow > fRowMax) break;
314 tempPt->fNDigit = ndigits[lrow];
319 time=fDigits->CurrentRow();
320 pad=fDigits->CurrentColumn();
321 dig = fDigits->GetDigit(time,pad);
322 if (dig <= fParam->GetZeroSup()) continue;
324 //Exclude data outside cone:
325 AliL3Transform::Raw2Local(xyz,sector,row,pad,time);
326 if(fParam->GetPadRowRadii(sector,row)<230./250.*fabs(xyz[2]))
329 if(localcount >= ndigits[lrow])
330 LOG(AliL3Log::kFatal,"AliL3FileHandler::AliDigits2Binary","Memory")
331 <<AliL3Log::kDec<<"Mismatch: localcount "<<localcount<<" ndigits "
332 <<ndigits[lrow]<<ENDLOG;
334 tempPt->fDigitData[localcount].fCharge=dig;
335 tempPt->fDigitData[localcount].fPad=pad;
336 tempPt->fDigitData[localcount].fTime=time;
338 tempPt->fDigitData[localcount].fTrackID[0] = fDigits->GetTrackID(time,pad,0);
339 tempPt->fDigitData[localcount].fTrackID[1] = fDigits->GetTrackID(time,pad,1);
340 tempPt->fDigitData[localcount].fTrackID[2] = fDigits->GetTrackID(time,pad,2);
343 } while (fDigits->Next());
345 Byte_t *tmp = (Byte_t*)tempPt;
346 Int_t size = sizeof(AliL3DigitRowData)
347 + ndigits[lrow]*sizeof(AliL3DigitData);
349 tempPt = (AliL3DigitRowData*)tmp;
360 Bool_t AliL3FileHandler::GetDigitsTree(Int_t event)
362 //Connects to the TPC digit tree in the AliROOT file.
366 sprintf(dname,"TreeD_%s_%d",AliL3Transform::GetParamName(),event);
367 fDigitsTree = (TTree*)fInAli->Get(dname);
370 LOG(AliL3Log::kError,"AliL3FileHandler::GetDigitsTree","Digits Tree")
371 <<AliL3Log::kHex<<"Error getting digitstree "<<(Int_t)fDigitsTree<<ENDLOG;
374 fDigitsTree->GetBranch("Segment")->SetAddress(&fDigits);
378 void AliL3FileHandler::AliDigits2RootFile(AliL3DigitRowData *rowPt,Char_t *new_digitsfile)
380 //Write the data stored in rowPt, into a new AliROOT file.
381 //The data is stored in the AliROOT format
382 //This is specially a nice thing if you have modified data, and wants to run it
383 //through the offline reconstruction chain.
384 //The arguments is a pointer to the data, and the name of the new AliROOT file.
385 //Remember to pass the original AliROOT file (the one that contains the original
386 //simulated data) to this object, in order to retrieve the MC id's of the digits.
390 printf("AliL3FileHandler::AliDigits2RootFile : No rootfile\n");
395 printf("AliL3FileHandler::AliDigits2RootFile : No parameter object. Run on rootfile\n");
399 //Get the original digitstree:
401 AliTPCDigitsArray *old_array = new AliTPCDigitsArray();
402 old_array->Setup(fParam);
403 old_array->SetClass("AliSimDigits");
406 sprintf(dname,"TreeD_%s_0",AliL3Transform::GetParamName());
408 Bool_t ok = old_array->ConnectTree(dname);
411 printf("AliL3FileHandler::AliDigits2RootFile : No digits tree object\n");
415 Bool_t create=kFALSE;
418 digFile = TFile::Open(new_digitsfile,"NEW");
419 if(digFile->IsOpen())
422 fParam->Write(fParam->GetTitle());
426 LOG(AliL3Log::kDebug,"AliL3FileHandler::AliDigits2RootFile","Rootfile")
427 <<"Rootfile did already exist, so I will just open it for updates"<<ENDLOG;
428 digFile = TFile::Open(new_digitsfile,"UPDATE");
431 if(!digFile->IsOpen())
433 LOG(AliL3Log::kError,"AliL3FileHandler::AliDigits2RootFile","Rootfile")
434 <<"Error opening rootfile "<<new_digitsfile<<ENDLOG;
440 //setup a new one, or connect it to the existing one:
441 AliTPCDigitsArray *arr = new AliTPCDigitsArray;
442 arr->SetClass("AliSimDigits");
448 Bool_t ok = arr->ConnectTree(dname);
451 printf("AliL3FileHandler::AliDigits2RootFile : No digits tree object in existing file\n");
457 for(Int_t i=fRowMin; i<=fRowMax; i++)
460 if((Int_t)rowPt->fRow != i) printf("AliL3FileHandler::AliDigits2RootFile : Mismatching row numbering!!!\n");
463 AliL3Transform::Slice2Sector(fSlice,i,sector,row);
464 AliSimDigits * dig = (AliSimDigits*)arr->CreateRow(sector,row);
465 AliSimDigits *old_dig = (AliSimDigits*)old_array->LoadRow(sector,row);
467 printf("AliL3FileHandler::AliDigits2RootFile : No padrow %d %d\n",sector,row);
469 AliL3DigitData *digPt = rowPt->fDigitData;
471 for(UInt_t j=0; j<rowPt->fNDigit; j++)
473 Int_t charge = (Int_t)digPt[j].fCharge;
474 Int_t pad = (Int_t)digPt[j].fPad;
475 Int_t time = (Int_t)digPt[j].fTime;
477 if(charge == 0) //Only write the digits that has not been removed
480 dig->SetDigitFast(charge,time,pad);
482 Int_t trackID[3] = {old_dig->GetTrackID(time,pad,0),old_dig->GetTrackID(time,pad,1),old_dig->GetTrackID(time,pad,2)};
484 Int_t s_time = time - 1;
485 while(trackID[0] < 0)
487 if(s_time >= 0 && s_time < AliL3Transform::GetNTimeBins() && s_pad >= 0 && s_pad < AliL3Transform::GetNPads(i))
489 if(old_dig->GetTrackID(s_time,s_pad,0) > 0)
491 trackID[0]=old_dig->GetTrackID(s_time,s_pad,0);
492 trackID[1]=old_dig->GetTrackID(s_time,s_pad,1);
493 trackID[2]=old_dig->GetTrackID(s_time,s_pad,2);
496 if(s_pad == pad && s_time == time - 1)
498 else if(s_pad == pad && s_time == time + 1)
499 {s_pad = pad - 1; s_time = time;}
500 else if(s_pad == pad - 1 && s_time == time)
502 else if(s_pad == pad - 1 && s_time == time - 1)
504 else if(s_pad == pad - 1 && s_time == time + 1)
505 {s_pad = pad + 1; s_time = time;}
506 else if(s_pad == pad + 1 && s_time == time)
508 else if(s_pad == pad + 1 && s_time == time - 1)
514 dig->SetTrackIDFast(trackID[0],time,pad,0);
515 dig->SetTrackIDFast(trackID[1],time,pad,1);
516 dig->SetTrackIDFast(trackID[2],time,pad,2);
519 //cout<<"Wrote "<<digcounter<<" on row "<<i<<endl;
520 UpdateRowPointer(rowPt);
521 arr->StoreRow(sector,row);
522 arr->ClearRow(sector,row);
523 old_array->ClearRow(sector,row);
527 sprintf(treeName,"TreeD_%s_0",fParam->GetTitle());
528 printf("Writing tree to file.....");
529 arr->GetTree()->Write(treeName,TObject::kOverwrite);
532 //arr->GetTree()->Delete();
536 ///////////////////////////////////////// Point IO
537 Bool_t AliL3FileHandler::AliPoints2Binary(){
540 AliL3SpacePointData *data = AliPoints2Memory(npoint);
541 out = Memory2Binary(npoint,data);
546 AliL3SpacePointData * AliL3FileHandler::AliPoints2Memory(UInt_t & npoint){
547 AliL3SpacePointData *data = 0;
550 LOG(AliL3Log::kWarning,"AliL3FileHandler::AliPoints2Memory","File")
551 <<"No Input avalible: no object TFile"<<ENDLOG;
554 if(!fInAli->IsOpen()){
555 LOG(AliL3Log::kWarning,"AliL3FileHandler::AliPoints2Memory","File")
556 <<"No Input avalible: TFile not opend"<<ENDLOG;
560 TDirectory *savedir = gDirectory;
565 sprintf(cname,"TreeC_TPC_%d",eventn);
566 AliTPCClustersArray carray;
567 carray.Setup(fParam);
568 carray.SetClusterType("AliTPCcluster");
569 Bool_t clusterok = carray.ConnectTree(cname);
570 if(!clusterok) return 0;
572 AliTPCClustersRow ** clusterrow =
573 new AliTPCClustersRow*[ (int)carray.GetTree()->GetEntries()];
574 Int_t *rows = new int[ (int)carray.GetTree()->GetEntries()];
575 Int_t *sects = new int[ (int)carray.GetTree()->GetEntries()];
579 for(Int_t i=0; i<carray.GetTree()->GetEntries(); i++){
580 AliSegmentID *s = carray.LoadEntry(i);
582 fParam->AdjustSectorRow(s->GetID(),sector,row);
586 AliL3Transform::Sector2Slice(lslice,lrow,sector,row);
587 if(fSlice != lslice || lrow<fRowMin || lrow>fRowMax) continue;
588 clusterrow[i] = carray.GetRow(sector,row);
590 sum+=clusterrow[i]->GetArray()->GetEntriesFast();
592 UInt_t size = sum*sizeof(AliL3SpacePointData);
594 LOG(AliL3Log::kDebug,"AliL3FileHandler::AliPoints2Memory","File")
595 <<AliL3Log::kDec<<"Found "<<sum<<" SpacePoints"<<ENDLOG;
597 data = (AliL3SpacePointData *) Allocate(size);
600 for(Int_t i=0; i<carray.GetTree()->GetEntries(); i++){
601 if(!clusterrow[i]) continue;
603 Int_t sector = sects[i];
604 AliL3Transform::Sector2Slice(lslice,lrow,sector,row);
605 Int_t entries_in_row = clusterrow[i]->GetArray()->GetEntriesFast();
606 for(Int_t j = 0;j<entries_in_row;j++){
607 AliTPCcluster *c = (AliTPCcluster*)(*clusterrow[i])[j];
608 data[n].fZ = c->GetZ();
609 data[n].fY = c->GetY();
610 data[n].fX = fParam->GetPadRowRadii(sector,row);
611 data[n].fID = n+((fSlice&0x7f)<<25)+((fPatch&0x7)<<22);//uli
612 data[n].fPadRow = lrow;
613 data[n].fXYErr = c->GetSigmaY2();
614 data[n].fZErr = c->GetSigmaZ2();
615 if(fMC) fprintf(fMC,"%d %d\n",data[n].fID,c->GetLabel(0));
619 for(Int_t i=0;i<carray.GetTree()->GetEntries();i++){
621 Int_t sector = sects[i];
622 if(carray.GetRow(sector,row))
623 carray.ClearRow(sector,row);
626 delete [] clusterrow;