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"
24 //_____________________________________________________________
27 // The HLT ROOT <-> binary files handling class
29 // This class provides the interface between AliROOT files,
30 // and HLT binary files. It should be used for converting
31 // TPC data stored in AliROOT format (outputfile from a simulation),
32 // into the data format currently used by in the HLT framework.
33 // This enables the possibility to always use the same data format,
34 // whether you are using a binary file as an input, or a AliROOT file.
36 // For example on how to create binary files from a AliROOT simulation,
37 // see example macro exa/Binary.C.
39 // For reading a AliROOT file into HLT format in memory, do the following:
41 // AliL3FileHandler file;
42 // file.SetAliInput("galice.root");
43 // AliL3DigitRowData *dataPt = (AliL3DigitRowData*)file.AliDigits2Memory(nrows,eventnr);
45 // All the data are then stored in memory and accessible via the pointer dataPt.
46 // Accesing the data is then identical to the example 1) showed in AliL3MemHandler class.
48 // For converting the data back, and writing it to a new AliROOT file do:
50 // AliL3FileHandler file;
51 // file.SetAliInput("galice.root");
52 // file.Init(slice,patch,NumberOfRowsInPatch);
53 // file.AliDigits2RootFile(dataPt,"new_galice.root");
54 // file.CloseAliInput();
56 ClassImp(AliL3FileHandler)
58 AliL3FileHandler::AliL3FileHandler()
69 AliL3FileHandler::~AliL3FileHandler()
72 if(fMC) CloseMCOutput();
74 if(fInAli) CloseAliInput();
78 void AliL3FileHandler::FreeDigitsTree()
82 LOG(AliL3Log::kWarning,"AliL3FileHandler::FreeDigitsTree()","Pointer")
83 <<"Cannot free digitstree, it is not present"<<ENDLOG;
87 fDigitsTree->Delete();
91 Bool_t AliL3FileHandler::SetMCOutput(char *name)
93 fMC = fopen(name,"w");
95 LOG(AliL3Log::kWarning,"AliL3FileHandler::SetMCOutput","File Open")
96 <<"Pointer to File = 0x0 "<<ENDLOG;
102 Bool_t AliL3FileHandler::SetMCOutput(FILE *file)
106 LOG(AliL3Log::kWarning,"AliL3FileHandler::SetMCOutput","File Open")
107 <<"Pointer to File = 0x0 "<<ENDLOG;
113 void AliL3FileHandler::CloseMCOutput()
116 LOG(AliL3Log::kWarning,"AliL3FileHandler::CloseMCOutPut","File Close")
117 <<"Nothing to Close"<<ENDLOG;
124 Bool_t AliL3FileHandler::SetAliInput()
126 if(!fInAli->IsOpen()){
127 LOG(AliL3Log::kError,"AliL3FileHandler::SetAliInput","File Open")
128 <<"Ali File "<<fInAli->GetName()<<" does not exist"<<ENDLOG;
131 fParam = (AliTPCParam*)fInAli->Get(AliL3Transform::GetParamName());
133 LOG(AliL3Log::kError,"AliL3FileHandler::SetAliInput","File Open")
134 <<"No AliTPCParam "<<AliL3Transform::GetParamName()<<"in File "<<fInAli->GetName()<<ENDLOG;
140 Bool_t AliL3FileHandler::SetAliInput(char *name)
142 //Open the AliROOT file with name.
144 fInAli= new TFile(name,"READ");
146 LOG(AliL3Log::kWarning,"AliL3FileHandler::SetAliInput","File Open")
147 <<"Pointer to TFile = 0x0 "<<ENDLOG;
150 return SetAliInput();
153 Bool_t AliL3FileHandler::SetAliInput(TFile *file)
155 //Specify already opened AliROOT file to use as an input.
159 LOG(AliL3Log::kWarning,"AliL3FileHandler::SetAliInput","File Open")
160 <<"Pointer to TFile = 0x0 "<<ENDLOG;
163 return SetAliInput();
166 void AliL3FileHandler::CloseAliInput()
169 LOG(AliL3Log::kWarning,"AliL3FileHandler::CloseAliInput","File Close")
170 <<"Nothing to Close"<<ENDLOG;
173 if(fInAli->IsOpen()) fInAli->Close();
179 Bool_t AliL3FileHandler::IsDigit(Int_t event)
181 //Check if there is a TPC digit tree in the current file.
182 //Return kTRUE if tree was found, and kFALSE if not found.
185 LOG(AliL3Log::kWarning,"AliL3FileHandler::IsDigit","File")
186 <<"Pointer to TFile = 0x0 "<<ENDLOG;
187 return kTRUE; //may you are use binary input which is Digits!!
190 sprintf(name,"TreeD_%s_%d",AliL3Transform::GetParamName(),event);
191 TTree *t=(TTree*)fInAli->Get(name);
193 LOG(AliL3Log::kInformational,"AliL3FileHandler::IsDigit","File Type")
194 <<"Found Digit Tree -> Use Fast Cluster Finder"<<ENDLOG;
198 LOG(AliL3Log::kInformational,"AliL3FileHandler::IsDigit","File Type")
199 <<"No Digit Tree -> Use Cluster Tree"<<ENDLOG;
204 ///////////////////////////////////////// Digit IO
205 Bool_t AliL3FileHandler::AliDigits2Binary(Int_t event)
210 AliL3DigitRowData* data = AliDigits2Memory(nrow,event);
211 out = Memory2Binary(nrow,data);
216 Bool_t AliL3FileHandler::AliDigits2CompBinary(Int_t event)
218 //Convert AliROOT TPC data, into HLT data format.
219 //event specifies the event you want in the aliroot file.
223 AliL3DigitRowData* digits = AliDigits2Memory(ndigits,event);
224 out = Memory2CompBinary(ndigits,digits);
229 AliL3DigitRowData * AliL3FileHandler::AliDigits2Memory(UInt_t & nrow,Int_t event)
231 //Read data from AliROOT file into memory, and store it in the HLT data format.
232 //Returns a pointer to the data.
234 AliL3DigitRowData *data = 0;
238 LOG(AliL3Log::kWarning,"AliL3FileHandler::AliDigits2Memory","File")
239 <<"No Input avalible: no object TFile"<<ENDLOG;
242 if(!fInAli->IsOpen()){
243 LOG(AliL3Log::kWarning,"AliL3FileHandler::AliDigits2Memory","File")
244 <<"No Input avalible: TFile not opend"<<ENDLOG;
249 GetDigitsTree(event);
252 Int_t time,pad,sector,row;
255 Int_t entries = (Int_t)fDigitsTree->GetEntries();
256 Int_t ndigits[entries];
260 for(Int_t n=fLastIndex; n<fDigitsTree->GetEntries(); n++)
262 fDigitsTree->GetEvent(n);
263 fParam->AdjustSectorRow(fDigits->GetID(),sector,row);
264 AliL3Transform::Sector2Slice(lslice,lrow,sector,row);
265 //if(fSlice != lslice || lrow<fRowMin || lrow>fRowMax) continue;
266 if(lslice < fSlice) continue;
267 if(lslice != fSlice) break;
268 if(lrow < fRowMin) continue;
269 if(lrow > fRowMax) break;
274 time=fDigits->CurrentRow();
275 pad=fDigits->CurrentColumn();
276 dig = fDigits->GetDigit(time,pad);
277 if(dig<=fParam->GetZeroSup()) continue;
279 AliL3Transform::Raw2Local(xyz,sector,row,pad,time);
280 if(fParam->GetPadRowRadii(sector,row)<230./250.*fabs(xyz[2]))
283 ndigits[lrow]++; //for this row only
284 ndigitcount++; //total number of digits to be published
286 } while (fDigits->Next());
287 //cout << lrow << " " << ndigits[lrow] << " - " << ndigitcount << endl;
291 Int_t size = sizeof(AliL3DigitData)*ndigitcount
292 + nrows*sizeof(AliL3DigitRowData);
294 LOG(AliL3Log::kDebug,"AliL3FileHandler::AliDigits2Memory","Digits")
295 <<AliL3Log::kDec<<"Found "<<ndigitcount<<" Digits"<<ENDLOG;
297 data=(AliL3DigitRowData*) Allocate(size);
298 nrow = (UInt_t)nrows;
299 AliL3DigitRowData *tempPt = data;
300 for(Int_t n=fLastIndex; n<fDigitsTree->GetEntries(); n++)
302 fDigitsTree->GetEvent(n);
303 fParam->AdjustSectorRow(fDigits->GetID(),sector,row);
304 AliL3Transform::Sector2Slice(lslice,lrow,sector,row);
305 //if(fSlice != lslice || lrow<fRowMin || lrow>fRowMax) continue;
306 if(lslice < fSlice) continue;
307 if(lslice != fSlice) break;
308 if(lrow < fRowMin) continue;
309 if(lrow > fRowMax) break;
312 tempPt->fNDigit = ndigits[lrow];
317 time=fDigits->CurrentRow();
318 pad=fDigits->CurrentColumn();
319 dig = fDigits->GetDigit(time,pad);
320 if (dig <= fParam->GetZeroSup()) continue;
322 //Exclude data outside cone:
323 AliL3Transform::Raw2Local(xyz,sector,row,pad,time);
324 if(fParam->GetPadRowRadii(sector,row)<230./250.*fabs(xyz[2]))
327 if(localcount >= ndigits[lrow])
328 LOG(AliL3Log::kFatal,"AliL3FileHandler::AliDigits2Binary","Memory")
329 <<AliL3Log::kDec<<"Mismatch: localcount "<<localcount<<" ndigits "
330 <<ndigits[lrow]<<ENDLOG;
332 tempPt->fDigitData[localcount].fCharge=dig;
333 tempPt->fDigitData[localcount].fPad=pad;
334 tempPt->fDigitData[localcount].fTime=time;
336 tempPt->fDigitData[localcount].fTrackID[0] = fDigits->GetTrackID(time,pad,0);
337 tempPt->fDigitData[localcount].fTrackID[1] = fDigits->GetTrackID(time,pad,1);
338 tempPt->fDigitData[localcount].fTrackID[2] = fDigits->GetTrackID(time,pad,2);
341 } while (fDigits->Next());
343 Byte_t *tmp = (Byte_t*)tempPt;
344 Int_t size = sizeof(AliL3DigitRowData)
345 + ndigits[lrow]*sizeof(AliL3DigitData);
347 tempPt = (AliL3DigitRowData*)tmp;
354 Bool_t AliL3FileHandler::GetDigitsTree(Int_t event)
356 //Connects to the TPC digit tree in the AliROOT file.
360 sprintf(dname,"TreeD_%s_%d",AliL3Transform::GetParamName(),event);
361 fDigitsTree = (TTree*)fInAli->Get(dname);
364 LOG(AliL3Log::kError,"AliL3FileHandler::GetDigitsTree","Digits Tree")
365 <<AliL3Log::kHex<<"Error getting digitstree "<<(Int_t)fDigitsTree<<ENDLOG;
368 fDigitsTree->GetBranch("Segment")->SetAddress(&fDigits);
372 void AliL3FileHandler::AliDigits2RootFile(AliL3DigitRowData *rowPt,Char_t *new_digitsfile)
374 //Write the data stored in rowPt, into a new AliROOT file.
375 //The data is stored in the AliROOT format
376 //This is specially a nice thing if you have modified data, and wants to run it
377 //through the offline reconstruction chain.
378 //The arguments is a pointer to the data, and the name of the new AliROOT file.
379 //Remember to pass the original AliROOT file (the one that contains the original
380 //simulated data) to this object, in order to retrieve the MC id's of the digits.
384 printf("AliL3FileHandler::AliDigits2RootFile : No rootfile\n");
389 printf("AliL3FileHandler::AliDigits2RootFile : No parameter object. Run on rootfile\n");
393 //Get the original digitstree:
395 AliTPCDigitsArray *old_array = new AliTPCDigitsArray();
396 old_array->Setup(fParam);
397 old_array->SetClass("AliSimDigits");
400 sprintf(dname,"TreeD_%s_0",AliL3Transform::GetParamName());
402 Bool_t ok = old_array->ConnectTree(dname);
405 printf("AliL3FileHandler::AliDigits2RootFile : No digits tree object\n");
409 Bool_t create=kFALSE;
412 digFile = TFile::Open(new_digitsfile,"NEW");
413 if(digFile->IsOpen())
416 fParam->Write(fParam->GetTitle());
420 LOG(AliL3Log::kDebug,"AliL3FileHandler::AliDigits2RootFile","Rootfile")
421 <<"Rootfile did already exist, so I will just open it for updates"<<ENDLOG;
422 digFile = TFile::Open(new_digitsfile,"UPDATE");
425 if(!digFile->IsOpen())
427 LOG(AliL3Log::kError,"AliL3FileHandler::AliDigits2RootFile","Rootfile")
428 <<"Error opening rootfile "<<new_digitsfile<<ENDLOG;
434 //setup a new one, or connect it to the existing one:
435 AliTPCDigitsArray *arr = new AliTPCDigitsArray;
436 arr->SetClass("AliSimDigits");
442 Bool_t ok = arr->ConnectTree(dname);
445 printf("AliL3FileHandler::AliDigits2RootFile : No digits tree object in existing file\n");
451 for(Int_t i=fRowMin; i<=fRowMax; i++)
454 if((Int_t)rowPt->fRow != i) printf("AliL3FileHandler::AliDigits2RootFile : Mismatching row numbering!!!\n");
457 AliL3Transform::Slice2Sector(fSlice,i,sector,row);
458 AliSimDigits * dig = (AliSimDigits*)arr->CreateRow(sector,row);
459 AliSimDigits *old_dig = (AliSimDigits*)old_array->LoadRow(sector,row);
461 printf("AliL3FileHandler::AliDigits2RootFile : No padrow %d %d\n",sector,row);
463 AliL3DigitData *digPt = rowPt->fDigitData;
465 for(UInt_t j=0; j<rowPt->fNDigit; j++)
467 Int_t charge = (Int_t)digPt[j].fCharge;
468 Int_t pad = (Int_t)digPt[j].fPad;
469 Int_t time = (Int_t)digPt[j].fTime;
471 if(charge == 0) //Only write the digits that has not been removed
474 dig->SetDigitFast(charge,time,pad);
476 Int_t trackID[3] = {old_dig->GetTrackID(time,pad,0),old_dig->GetTrackID(time,pad,1),old_dig->GetTrackID(time,pad,2)};
478 Int_t s_time = time - 1;
479 while(trackID[0] < 0)
481 if(s_time >= 0 && s_time < AliL3Transform::GetNTimeBins() && s_pad >= 0 && s_pad < AliL3Transform::GetNPads(i))
483 if(old_dig->GetTrackID(s_time,s_pad,0) > 0)
485 trackID[0]=old_dig->GetTrackID(s_time,s_pad,0);
486 trackID[1]=old_dig->GetTrackID(s_time,s_pad,1);
487 trackID[2]=old_dig->GetTrackID(s_time,s_pad,2);
490 if(s_pad == pad && s_time == time - 1)
492 else if(s_pad == pad && s_time == time + 1)
493 {s_pad = pad - 1; s_time = time;}
494 else if(s_pad == pad - 1 && s_time == time)
496 else if(s_pad == pad - 1 && s_time == time - 1)
498 else if(s_pad == pad - 1 && 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)
508 dig->SetTrackIDFast(trackID[0],time,pad,0);
509 dig->SetTrackIDFast(trackID[1],time,pad,1);
510 dig->SetTrackIDFast(trackID[2],time,pad,2);
513 //cout<<"Wrote "<<digcounter<<" on row "<<i<<endl;
514 UpdateRowPointer(rowPt);
515 arr->StoreRow(sector,row);
516 arr->ClearRow(sector,row);
517 old_array->ClearRow(sector,row);
521 sprintf(treeName,"TreeD_%s_0",fParam->GetTitle());
522 printf("Writing tree to file.....");
523 arr->GetTree()->Write(treeName,TObject::kOverwrite);
526 //arr->GetTree()->Delete();
530 ///////////////////////////////////////// Point IO
531 Bool_t AliL3FileHandler::AliPoints2Binary(){
534 AliL3SpacePointData *data = AliPoints2Memory(npoint);
535 out = Memory2Binary(npoint,data);
540 AliL3SpacePointData * AliL3FileHandler::AliPoints2Memory(UInt_t & npoint){
541 AliL3SpacePointData *data = 0;
544 LOG(AliL3Log::kWarning,"AliL3FileHandler::AliPoints2Memory","File")
545 <<"No Input avalible: no object TFile"<<ENDLOG;
548 if(!fInAli->IsOpen()){
549 LOG(AliL3Log::kWarning,"AliL3FileHandler::AliPoints2Memory","File")
550 <<"No Input avalible: TFile not opend"<<ENDLOG;
554 TDirectory *savedir = gDirectory;
559 sprintf(cname,"TreeC_TPC_%d",eventn);
560 AliTPCClustersArray carray;
561 carray.Setup(fParam);
562 carray.SetClusterType("AliTPCcluster");
563 Bool_t clusterok = carray.ConnectTree(cname);
564 if(!clusterok) return 0;
566 AliTPCClustersRow ** clusterrow =
567 new AliTPCClustersRow*[ (int)carray.GetTree()->GetEntries()];
568 Int_t *rows = new int[ (int)carray.GetTree()->GetEntries()];
569 Int_t *sects = new int[ (int)carray.GetTree()->GetEntries()];
573 for(Int_t i=0; i<carray.GetTree()->GetEntries(); i++){
574 AliSegmentID *s = carray.LoadEntry(i);
576 fParam->AdjustSectorRow(s->GetID(),sector,row);
580 AliL3Transform::Sector2Slice(lslice,lrow,sector,row);
581 if(fSlice != lslice || lrow<fRowMin || lrow>fRowMax) continue;
582 clusterrow[i] = carray.GetRow(sector,row);
584 sum+=clusterrow[i]->GetArray()->GetEntriesFast();
586 UInt_t size = sum*sizeof(AliL3SpacePointData);
588 LOG(AliL3Log::kDebug,"AliL3FileHandler::AliPoints2Memory","File")
589 <<AliL3Log::kDec<<"Found "<<sum<<" SpacePoints"<<ENDLOG;
591 data = (AliL3SpacePointData *) Allocate(size);
594 for(Int_t i=0; i<carray.GetTree()->GetEntries(); i++){
595 if(!clusterrow[i]) continue;
597 Int_t sector = sects[i];
598 AliL3Transform::Sector2Slice(lslice,lrow,sector,row);
599 Int_t entries_in_row = clusterrow[i]->GetArray()->GetEntriesFast();
600 for(Int_t j = 0;j<entries_in_row;j++){
601 AliTPCcluster *c = (AliTPCcluster*)(*clusterrow[i])[j];
602 data[n].fZ = c->GetZ();
603 data[n].fY = c->GetY();
604 data[n].fX = fParam->GetPadRowRadii(sector,row);
605 data[n].fID = n+((fSlice&0x7f)<<25)+((fPatch&0x7)<<22);//uli
606 data[n].fPadRow = lrow;
607 data[n].fXYErr = c->GetSigmaY2();
608 data[n].fZErr = c->GetSigmaZ2();
609 if(fMC) fprintf(fMC,"%d %d\n",data[n].fID,c->GetLabel(0));
613 for(Int_t i=0;i<carray.GetTree()->GetEntries();i++){
615 Int_t sector = sects[i];
616 if(carray.GetRow(sector,row))
617 carray.ClearRow(sector,row);
620 delete [] clusterrow;