3 // Author: Uli Frankenfeld <mailto:franken@fi.uib.no>
4 //*-- Copyright © Uli
9 #include "AliL3Transform.h"
10 #include "AliL3Logging.h"
11 #include "AliL3MemHandler.h"
12 #include "AliL3FileHandler.h"
14 #include "AliTPCDigitsArray.h"
15 #include "AliTPCClustersArray.h"
16 #include "AliTPCcluster.h"
17 #include "AliTPCClustersRow.h"
19 #include "AliL3DigitData.h"
20 #include "AliL3TrackSegmentData.h"
21 #include "AliL3SpacePointData.h"
22 #include "AliL3TrackArray.h"
24 //_____________________________________________________________
27 // The L3 Binary File handler
30 ClassImp(AliL3FileHandler)
32 AliL3FileHandler::AliL3FileHandler(){
43 AliL3FileHandler::~AliL3FileHandler(){
45 if(fTransformer) delete fTransformer;
46 if(fMC) CloseMCOutput();
47 if(fDigitsTree) delete fDigitsTree;
48 if(fInAli) CloseAliInput();
52 Bool_t AliL3FileHandler::SetMCOutput(char *name){
53 fMC = fopen(name,"w");
55 LOG(AliL3Log::kWarning,"AliL3FileHandler::SetMCOutput","File Open")
56 <<"Pointer to File = 0x0 "<<ENDLOG;
62 Bool_t AliL3FileHandler::SetMCOutput(FILE *file){
65 LOG(AliL3Log::kWarning,"AliL3FileHandler::SetMCOutput","File Open")
66 <<"Pointer to File = 0x0 "<<ENDLOG;
72 void AliL3FileHandler::CloseMCOutput(){
74 LOG(AliL3Log::kWarning,"AliL3FileHandler::CloseMCOutPut","File Close")
75 <<"Nothing to Close"<<ENDLOG;
82 Bool_t AliL3FileHandler::SetAliInput(){
83 if(!fInAli->IsOpen()){
84 LOG(AliL3Log::kError,"AliL3FileHandler::SetAliInput","File Open")
85 <<"Ali File "<<fInAli->GetName()<<" does not exist"<<ENDLOG;
88 fParam = (AliTPCParam*)fInAli->Get("75x40_100x60");
90 LOG(AliL3Log::kError,"AliL3FileHandler::SetAliInput","File Open")
91 <<"No AliTPCParam 75x40_100x60 in File "<<fInAli->GetName()<<ENDLOG;
94 fTransformer = new AliL3Transform();
98 Bool_t AliL3FileHandler::SetAliInput(char *name){
99 fInAli= new TFile(name,"READ");
101 LOG(AliL3Log::kWarning,"AliL3FileHandler::SetAliInput","File Open")
102 <<"Pointer to TFile = 0x0 "<<ENDLOG;
105 return SetAliInput();
108 Bool_t AliL3FileHandler::SetAliInput(TFile *file){
111 LOG(AliL3Log::kWarning,"AliL3FileHandler::SetAliInput","File Open")
112 <<"Pointer to TFile = 0x0 "<<ENDLOG;
115 return SetAliInput();
118 void AliL3FileHandler::CloseAliInput(){
120 LOG(AliL3Log::kWarning,"AliL3FileHandler::CloseAliInput","File Close")
121 <<"Nothing to Close"<<ENDLOG;
124 if(fInAli->IsOpen()) fInAli->Close();
130 Bool_t AliL3FileHandler::IsDigit(){
132 LOG(AliL3Log::kWarning,"AliL3FileHandler::IsDigit","File")
133 <<"Pointer to TFile = 0x0 "<<ENDLOG;
134 return kTRUE; //may you are use binary input which is Digits!!
136 TTree *t=(TTree*)fInAli->Get("TreeD_75x40_100x60_0");
138 LOG(AliL3Log::kInformational,"AliL3FileHandler::IsDigit","File Type")
139 <<"Found Digit Tree -> Use Fast Cluster Finder"<<ENDLOG;
143 LOG(AliL3Log::kInformational,"AliL3FileHandler::IsDigit","File Type")
144 <<"No Digit Tree -> Use Cluster Tree"<<ENDLOG;
149 ///////////////////////////////////////// Digit IO
150 Bool_t AliL3FileHandler::AliDigits2Binary(Int_t event){
153 AliL3DigitRowData* data = AliDigits2Memory(nrow,event);
154 out = Memory2Binary(nrow,data);
160 Bool_t AliL3FileHandler::AliDigits2CompBinary(Int_t event){
163 AliL3DigitRowData* digits = AliDigits2Memory(ndigits,event);
164 out = Memory2CompBinary(ndigits,digits);
170 AliL3DigitRowData * AliL3FileHandler::AliDigits2Memory(UInt_t & nrow,Int_t event){
171 AliL3DigitRowData *data = 0;
174 LOG(AliL3Log::kWarning,"AliL3FileHandler::AliDigits2Memory","File")
175 <<"No Input avalible: no object TFile"<<ENDLOG;
178 if(!fInAli->IsOpen()){
179 LOG(AliL3Log::kWarning,"AliL3FileHandler::AliDigits2Memory","File")
180 <<"No Input avalible: TFile not opend"<<ENDLOG;
185 GetDigitsTree(event);
188 Int_t time,pad,sector,row;
191 Int_t entries = (Int_t)fDigitsTree->GetEntries();
192 Int_t ndigits[entries];
195 for(Int_t n=fLastIndex; n<fDigitsTree->GetEntries(); n++)
197 fDigitsTree->GetEvent(n);
198 fParam->AdjustSectorRow(fDigits->GetID(),sector,row);
199 fTransformer->Sector2Slice(lslice,lrow,sector,row);
200 //if(fSlice != lslice || lrow<fRowMin || lrow>fRowMax) continue;
201 if(lslice < fSlice) continue;
202 if(lslice != fSlice) break;
203 if(lrow < fRowMin) continue;
204 if(lrow > fRowMax) break;
210 time=fDigits->CurrentRow();
211 pad=fDigits->CurrentColumn();
212 dig = fDigits->GetDigit(time,pad);
213 if(dig<=fParam->GetZeroSup()) continue;
214 if(time < fParam->GetMaxTBin()-1 && time > 0)
215 if(fDigits->GetDigit(time+1,pad) <= fParam->GetZeroSup()
216 && fDigits->GetDigit(time-1,pad) <= fParam->GetZeroSup())
219 fTransformer->Raw2Local(xyz,sector,row,pad,time);
220 if(fParam->GetPadRowRadii(sector,row)<230./250.*fabs(xyz[2]))
223 ndigits[lrow]++; //for this row only
224 ndigitcount++; //total number of digits to be published
226 } while (fDigits->Next());
230 Int_t size = sizeof(AliL3DigitData)*ndigitcount
231 + nrows*sizeof(AliL3DigitRowData);
233 LOG(AliL3Log::kDebug,"AliL3FileHandler::AliDigits2Memory","Digits")
234 <<AliL3Log::kDec<<"Found "<<ndigitcount<<" Digits"<<ENDLOG;
236 data=(AliL3DigitRowData*) Allocate(size);
237 nrow = (UInt_t)nrows;
238 AliL3DigitRowData *tempPt = data;
239 for(Int_t n=fLastIndex; n<fDigitsTree->GetEntries(); n++)
241 fDigitsTree->GetEvent(n);
243 fParam->AdjustSectorRow(fDigits->GetID(),sector,row);
244 fTransformer->Sector2Slice(lslice,lrow,sector,row);
245 //if(fSlice != lslice || lrow<fRowMin || lrow>fRowMax) continue;
246 if(lslice < fSlice) continue;
247 if(lslice != fSlice) break;
248 if(lrow < fRowMin) continue;
249 if(lrow > fRowMax) break;
252 tempPt->fNDigit = ndigits[lrow];
257 //dig=fDigits->CurrentDigit();
258 //if (dig<=fParam->GetZeroSup()) continue;
259 time=fDigits->CurrentRow();
260 pad=fDigits->CurrentColumn();
261 dig = fDigits->GetDigit(time,pad);
262 if (dig <= fParam->GetZeroSup()) continue;
263 if(time < fParam->GetMaxTBin()-1 && time > 0)
264 if(fDigits->GetDigit(time-1,pad) <= fParam->GetZeroSup() &&
265 fDigits->GetDigit(time+1,pad) <= fParam->GetZeroSup()) continue;
267 //Exclude data outside cone:
268 fTransformer->Raw2Local(xyz,sector,row,pad,time);
269 if(fParam->GetPadRowRadii(sector,row)<230./250.*fabs(xyz[2]))
272 if(localcount >= ndigits[lrow])
273 LOG(AliL3Log::kFatal,"AliL3FileHandler::AliDigits2Binary","Memory")
274 <<AliL3Log::kDec<<"Mismatch: localcount "<<localcount<<" ndigits "
275 <<ndigits[lrow]<<ENDLOG;
277 tempPt->fDigitData[localcount].fCharge=dig;
278 tempPt->fDigitData[localcount].fPad=pad;
279 tempPt->fDigitData[localcount].fTime=time;
281 tempPt->fDigitData[localcount].fTrackID[0] = fDigits->GetTrackID(time,pad,0);
282 tempPt->fDigitData[localcount].fTrackID[1] = fDigits->GetTrackID(time,pad,1);
283 tempPt->fDigitData[localcount].fTrackID[2] = fDigits->GetTrackID(time,pad,2);
286 } while (fDigits->Next());
288 Byte_t *tmp = (Byte_t*)tempPt;
289 Int_t size = sizeof(AliL3DigitRowData)
290 + ndigits[lrow]*sizeof(AliL3DigitData);
292 tempPt = (AliL3DigitRowData*)tmp;
299 Bool_t AliL3FileHandler::GetDigitsTree(Int_t event)
304 sprintf(dname,"TreeD_75x40_100x60_%d",event);
305 fDigitsTree = (TTree*)fInAli->Get("TreeD_75x40_100x60_0");
308 LOG(AliL3Log::kError,"AliL3FileHandler::GetDigitsTree","Digits Tree")
309 <<AliL3Log::kHex<<"Error getting digitstree "<<(Int_t)fDigitsTree<<ENDLOG;
312 fDigitsTree->GetBranch("Segment")->SetAddress(&fDigits);
316 void AliL3FileHandler::AliDigits2RootFile(AliL3DigitRowData *rowPt,Char_t *new_digitsfile)
318 //Write digits to a new alirootfile.
322 printf("AliL3FileHandler::AliDigits2RootFile : No rootfile\n");
327 printf("AliL3FileHandler::AliDigits2RootFile : No parameter object. Run on rootfile\n");
332 printf("AliL3FileHandler::AliDigits2RootFile : No transform object\n");
336 //Get the original digitstree:
338 AliTPCDigitsArray *old_array = new AliTPCDigitsArray();
339 old_array->Setup(fParam);
340 old_array->SetClass("AliSimDigits");
341 Bool_t ok = old_array->ConnectTree("TreeD_75x40_100x60_0");
344 printf("AliL3FileHandler::AliDigits2RootFile : No digits tree object\n");
348 Bool_t create=kFALSE;
351 digFile = TFile::Open(new_digitsfile,"NEW");
352 if(digFile->IsOpen())
355 fParam->Write(fParam->GetTitle());
359 LOG(AliL3Log::kDebug,"AliL3FileHandler::AliDigits2RootFile","Rootfile")
360 <<"Rootfile did already exist, so I will just open it for updates"<<ENDLOG;
361 digFile = TFile::Open(new_digitsfile,"UPDATE");
364 if(!digFile->IsOpen())
366 LOG(AliL3Log::kError,"AliL3FileHandler::AliDigits2RootFile","Rootfile")
367 <<"Error opening rootfile "<<new_digitsfile<<ENDLOG;
373 //setup a new one, or connect it to the existing one:
374 AliTPCDigitsArray *arr = new AliTPCDigitsArray;
375 arr->SetClass("AliSimDigits");
381 Bool_t ok = arr->ConnectTree("TreeD_75x40_100x60_0");
384 printf("AliL3FileHandler::AliDigits2RootFile : No digits tree object in existing file\n");
388 for(Int_t i=fRowMin; i<=fRowMax; i++)
391 if(rowPt->fRow != i) printf("AliL3FileHandler::AliDigits2RootFile : Mismatching row numbering!!!\n");
394 fTransformer->Slice2Sector(fSlice,i,sector,row);
395 AliDigits * dig = arr->CreateRow(sector,row);
396 AliDigits *old_dig = old_array->LoadRow(sector,row);
398 printf("AliL3FileHandler::AliDigits2RootFile : No padrow %d %d\n",sector,row);
400 AliL3DigitData *digPt = rowPt->fDigitData;
401 for(UInt_t j=0; j<rowPt->fNDigit; j++)
403 UShort_t charge = digPt[j].fCharge;
404 UChar_t pad = digPt[j].fPad;
405 UShort_t time = digPt[j].fTime;
407 if(charge == 0) //Only write the digits that has not been removed
409 dig->SetDigitFast(old_dig->GetDigit(time,pad),time,pad);
410 ((AliSimDigits*)dig)->SetTrackIDFast(((AliSimDigits*)old_dig)->GetTrackID((Int_t)time,(Int_t)pad,0),time,pad,0);
411 ((AliSimDigits*)dig)->SetTrackIDFast(((AliSimDigits*)old_dig)->GetTrackID((Int_t)time,(Int_t)pad,1),time,pad,1);
412 ((AliSimDigits*)dig)->SetTrackIDFast(((AliSimDigits*)old_dig)->GetTrackID((Int_t)time,(Int_t)pad,2),time,pad,2);
415 UpdateRowPointer(rowPt);
416 arr->StoreRow(sector,row);
417 arr->ClearRow(sector,row);
418 old_array->ClearRow(sector,row);
422 sprintf(treeName,"TreeD_%s_0",fParam->GetTitle());
423 printf("Writing tree to file.....");
424 arr->GetTree()->Write(treeName,TObject::kOverwrite);
427 //arr->GetTree()->Delete();
431 ///////////////////////////////////////// Point IO
432 Bool_t AliL3FileHandler::AliPoints2Binary(){
435 AliL3SpacePointData *data = AliPoints2Memory(npoint);
436 out = Memory2Binary(npoint,data);
441 AliL3SpacePointData * AliL3FileHandler::AliPoints2Memory(UInt_t & npoint){
442 AliL3SpacePointData *data = 0;
445 LOG(AliL3Log::kWarning,"AliL3FileHandler::AliPoints2Memory","File")
446 <<"No Input avalible: no object TFile"<<ENDLOG;
449 if(!fInAli->IsOpen()){
450 LOG(AliL3Log::kWarning,"AliL3FileHandler::AliPoints2Memory","File")
451 <<"No Input avalible: TFile not opend"<<ENDLOG;
454 TDirectory *savedir = gDirectory;
459 sprintf(cname,"TreeC_TPC_%d",eventn);
460 AliTPCClustersArray carray;
461 carray.Setup(fParam);
462 carray.SetClusterType("AliTPCcluster");
463 Bool_t clusterok = carray.ConnectTree(cname);
464 if(!clusterok) return 0;
466 AliTPCClustersRow ** clusterrow =
467 new AliTPCClustersRow*[ (int)carray.GetTree()->GetEntries()];
468 Int_t *rows = new int[ (int)carray.GetTree()->GetEntries()];
469 Int_t *sects = new int[ (int)carray.GetTree()->GetEntries()];
473 for(Int_t i=0; i<carray.GetTree()->GetEntries(); i++){
474 AliSegmentID *s = carray.LoadEntry(i);
476 fParam->AdjustSectorRow(s->GetID(),sector,row);
480 fTransformer->Sector2Slice(lslice,lrow,sector,row);
481 if(fSlice != lslice || lrow<fRowMin || lrow>fRowMax) continue;
482 clusterrow[i] = carray.GetRow(sector,row);
484 sum+=clusterrow[i]->GetArray()->GetEntriesFast();
486 UInt_t size = sum*sizeof(AliL3SpacePointData);
488 LOG(AliL3Log::kDebug,"AliL3FileHandler::AliPoints2Memory","File")
489 <<AliL3Log::kDec<<"Found "<<sum<<" SpacePoints"<<ENDLOG;
491 data = (AliL3SpacePointData *) Allocate(size);
494 for(Int_t i=0; i<carray.GetTree()->GetEntries(); i++){
495 if(!clusterrow[i]) continue;
497 Int_t sector = sects[i];
498 fTransformer->Sector2Slice(lslice,lrow,sector,row);
499 Int_t entries_in_row = clusterrow[i]->GetArray()->GetEntriesFast();
500 for(Int_t j = 0;j<entries_in_row;j++){
501 AliTPCcluster *c = (AliTPCcluster*)(*clusterrow[i])[j];
502 data[n].fZ = c->GetZ();
503 data[n].fY = c->GetY();
504 data[n].fX = fParam->GetPadRowRadii(sector,row);
505 data[n].fID = n+((fSlice&0x7f)<<25)+((fPatch&0x7)<<22);//uli
506 data[n].fPadRow = lrow;
507 data[n].fXYErr = c->GetSigmaY2();
508 data[n].fZErr = c->GetSigmaZ2();
509 if(fMC) fprintf(fMC,"%d %d\n",data[n].fID,c->GetLabel(0));
513 for(Int_t i=0;i<carray.GetTree()->GetEntries();i++){
515 Int_t sector = sects[i];
516 if(carray.GetRow(sector,row))
517 carray.ClearRow(sector,row);
520 delete [] clusterrow;