3 // Author: Uli Frankenfeld <mailto:franken@fi.uib.no>
4 //*-- Copyright © Uli
13 #include "AliL3Transform.h"
14 #include "AliL3Logging.h"
15 #include "AliL3MemHandler.h"
16 #include "AliL3FileHandler.h"
18 #include "AliTPCDigitsArray.h"
19 #include "AliTPCClustersArray.h"
20 #include "AliTPCcluster.h"
21 #include "AliTPCClustersRow.h"
22 #include "AliTPCParam.h"
23 #include "AliSimDigits.h"
25 #include "AliL3DigitData.h"
26 #include "AliL3TrackSegmentData.h"
27 #include "AliL3SpacePointData.h"
28 #include "AliL3TrackArray.h"
30 //_____________________________________________________________
33 // The L3 Binary File handler
36 ClassImp(AliL3FileHandler)
38 AliL3FileHandler::AliL3FileHandler(){
50 AliL3FileHandler::~AliL3FileHandler(){
52 if(fTransformer) delete fTransformer;
53 if(fMC) CloseMCOutput();
54 if(fDigitsTree) delete fDigitsTree;
55 if(fInAli) CloseAliInput();
59 Bool_t AliL3FileHandler::SetMCOutput(char *name){
60 fMC = fopen(name,"w");
62 LOG(AliL3Log::kWarning,"AliL3FileHandler::SetMCOutput","File Open")
63 <<"Pointer to File = 0x0 "<<ENDLOG;
69 Bool_t AliL3FileHandler::SetMCOutput(FILE *file){
72 LOG(AliL3Log::kWarning,"AliL3FileHandler::SetMCOutput","File Open")
73 <<"Pointer to File = 0x0 "<<ENDLOG;
79 void AliL3FileHandler::CloseMCOutput(){
81 LOG(AliL3Log::kWarning,"AliL3FileHandler::CloseMCOutPut","File Close")
82 <<"Nothing to Close"<<ENDLOG;
89 Bool_t AliL3FileHandler::SetAliInput(){
90 if(!fInAli->IsOpen()){
91 LOG(AliL3Log::kError,"AliL3FileHandler::SetAliInput","File Open")
92 <<"Ali File "<<fInAli->GetName()<<" does not exist"<<ENDLOG;
95 fParam = (AliTPCParam*)fInAli->Get("75x40_100x60");
97 LOG(AliL3Log::kError,"AliL3FileHandler::SetAliInput","File Open")
98 <<"No AliTPCParam 75x40_100x60 in File "<<fInAli->GetName()<<ENDLOG;
101 fTransformer = new AliL3Transform();
105 Bool_t AliL3FileHandler::SetAliInput(char *name){
106 fInAli= new TFile(name,"READ");
108 LOG(AliL3Log::kWarning,"AliL3FileHandler::SetAliInput","File Open")
109 <<"Pointer to TFile = 0x0 "<<ENDLOG;
112 return SetAliInput();
115 Bool_t AliL3FileHandler::SetAliInput(TFile *file){
118 LOG(AliL3Log::kWarning,"AliL3FileHandler::SetAliInput","File Open")
119 <<"Pointer to TFile = 0x0 "<<ENDLOG;
122 return SetAliInput();
125 void AliL3FileHandler::CloseAliInput(){
127 LOG(AliL3Log::kWarning,"AliL3FileHandler::CloseAliInput","File Close")
128 <<"Nothing to Close"<<ENDLOG;
131 if(fInAli->IsOpen()) fInAli->Close();
137 Bool_t AliL3FileHandler::IsDigit(){
139 LOG(AliL3Log::kWarning,"AliL3FileHandler::IsDigit","File")
140 <<"Pointer to TFile = 0x0 "<<ENDLOG;
141 return kTRUE; //may you are use binary input which is Digits!!
143 TTree *t=(TTree*)fInAli->Get("TreeD_75x40_100x60_0");
145 LOG(AliL3Log::kInformational,"AliL3FileHandler::IsDigit","File Type")
146 <<"Found Digit Tree -> Use Fast Cluster Finder"<<ENDLOG;
150 LOG(AliL3Log::kInformational,"AliL3FileHandler::IsDigit","File Type")
151 <<"No Digit Tree -> Use Cluster Tree"<<ENDLOG;
156 ///////////////////////////////////////// Digit IO
157 Bool_t AliL3FileHandler::AliDigits2Binary(Int_t event){
160 AliL3DigitRowData* data = AliDigits2Memory(nrow,event);
161 out = Memory2Binary(nrow,data);
167 Bool_t AliL3FileHandler::AliDigits2CompBinary(Int_t event){
170 AliL3DigitRowData* digits = AliDigits2Memory(ndigits,event);
171 out = Memory2CompBinary(ndigits,digits);
177 AliL3DigitRowData * AliL3FileHandler::AliDigits2Memory(UInt_t & nrow,Int_t event){
178 AliL3DigitRowData *data = 0;
181 LOG(AliL3Log::kWarning,"AliL3FileHandler::AliDigits2Memory","File")
182 <<"No Input avalible: no object TFile"<<ENDLOG;
185 if(!fInAli->IsOpen()){
186 LOG(AliL3Log::kWarning,"AliL3FileHandler::AliDigits2Memory","File")
187 <<"No Input avalible: TFile not opend"<<ENDLOG;
192 GetDigitsTree(event);
195 Int_t time,pad,sector,row;
198 Int_t entries = (Int_t)fDigitsTree->GetEntries();
199 Int_t ndigits[entries];
202 for(Int_t n=fLastIndex; n<fDigitsTree->GetEntries(); n++)
204 fDigitsTree->GetEvent(n);
205 fParam->AdjustSectorRow(fDigits->GetID(),sector,row);
206 fTransformer->Sector2Slice(lslice,lrow,sector,row);
207 //if(fSlice != lslice || lrow<fRowMin || lrow>fRowMax) continue;
208 if(lslice < fSlice) continue;
209 if(lslice != fSlice) break;
210 if(lrow < fRowMin) continue;
211 if(lrow > fRowMax) break;
217 time=fDigits->CurrentRow();
218 pad=fDigits->CurrentColumn();
219 dig = fDigits->GetDigit(time,pad);
220 if(dig<=fParam->GetZeroSup()) continue;
221 if(time < fParam->GetMaxTBin()-1 && time > 0)
222 if(fDigits->GetDigit(time+1,pad) <= fParam->GetZeroSup()
223 && fDigits->GetDigit(time-1,pad) <= fParam->GetZeroSup())
226 fTransformer->Raw2Local(xyz,sector,row,pad,time);
227 if(fParam->GetPadRowRadii(sector,row)<230./250.*fabs(xyz[2]))
230 ndigits[lrow]++; //for this row only
231 ndigitcount++; //total number of digits to be published
233 } while (fDigits->Next());
237 Int_t size = sizeof(AliL3DigitData)*ndigitcount
238 + nrows*sizeof(AliL3DigitRowData);
240 LOG(AliL3Log::kDebug,"AliL3FileHandler::AliDigits2Memory","Digits")
241 <<AliL3Log::kDec<<"Found "<<ndigitcount<<" Digits"<<ENDLOG;
243 data=(AliL3DigitRowData*) Allocate(size);
244 nrow = (UInt_t)nrows;
245 AliL3DigitRowData *tempPt = data;
246 for(Int_t n=fLastIndex; n<fDigitsTree->GetEntries(); n++)
248 fDigitsTree->GetEvent(n);
250 fParam->AdjustSectorRow(fDigits->GetID(),sector,row);
251 fTransformer->Sector2Slice(lslice,lrow,sector,row);
252 //if(fSlice != lslice || lrow<fRowMin || lrow>fRowMax) continue;
253 if(lslice < fSlice) continue;
254 if(lslice != fSlice) break;
255 if(lrow < fRowMin) continue;
256 if(lrow > fRowMax) break;
259 tempPt->fNDigit = ndigits[lrow];
264 //dig=fDigits->CurrentDigit();
265 //if (dig<=fParam->GetZeroSup()) continue;
266 time=fDigits->CurrentRow();
267 pad=fDigits->CurrentColumn();
268 dig = fDigits->GetDigit(time,pad);
269 if (dig <= fParam->GetZeroSup()) continue;
270 if(time < fParam->GetMaxTBin()-1 && time > 0)
271 if(fDigits->GetDigit(time-1,pad) <= fParam->GetZeroSup() &&
272 fDigits->GetDigit(time+1,pad) <= fParam->GetZeroSup()) continue;
274 //Exclude data outside cone:
275 fTransformer->Raw2Local(xyz,sector,row,pad,time);
276 if(fParam->GetPadRowRadii(sector,row)<230./250.*fabs(xyz[2]))
279 if(localcount >= ndigits[lrow])
280 LOG(AliL3Log::kFatal,"AliL3FileHandler::AliDigits2Binary","Memory")
281 <<AliL3Log::kDec<<"Mismatch: localcount "<<localcount<<" ndigits "
282 <<ndigits[lrow]<<ENDLOG;
284 tempPt->fDigitData[localcount].fCharge=dig;
285 tempPt->fDigitData[localcount].fPad=pad;
286 tempPt->fDigitData[localcount].fTime=time;
288 } while (fDigits->Next());
290 Byte_t *tmp = (Byte_t*)tempPt;
291 Int_t size = sizeof(AliL3DigitRowData)
292 + ndigits[lrow]*sizeof(AliL3DigitData);
294 tempPt = (AliL3DigitRowData*)tmp;
301 Bool_t AliL3FileHandler::GetDigitsTree(Int_t event)
306 sprintf(dname,"TreeD_75x40_100x60_%d",event);
307 fDigitsTree = (TTree*)fInAli->Get("TreeD_75x40_100x60_0");
310 LOG(AliL3Log::kError,"AliL3FileHandler::GetDigitsTree","Digits Tree")
311 <<AliL3Log::kHex<<"Error getting digitstree "<<(Int_t)fDigitsTree<<ENDLOG;
314 fDigitsTree->GetBranch("Segment")->SetAddress(&fDigits);
318 void AliL3FileHandler::AliDigits2RootFile(AliL3DigitRowData *rowPt,Char_t *new_digitsfile)
320 //Write digits to a new alirootfile.
324 printf("AliL3FileHandler::AliDigits2RootFile : No rootfile\n");
329 printf("AliL3FileHandler::AliDigits2RootFile : No parameter object. Run on rootfile\n");
334 printf("AliL3FileHandler::AliDigits2RootFile : No transform object\n");
338 //Get the original digitstree:
340 AliTPCDigitsArray *old_array = new AliTPCDigitsArray();
341 old_array->Setup(fParam);
342 old_array->SetClass("AliSimDigits");
343 Bool_t ok = old_array->ConnectTree("TreeD_75x40_100x60_0");
346 printf("AliL3FileHandler::AliDigits2RootFile : No digits tree object\n");
350 Bool_t create=kFALSE;
353 digFile = TFile::Open(new_digitsfile,"NEW");
354 if(digFile->IsOpen())
357 fParam->Write(fParam->GetTitle());
361 LOG(AliL3Log::kDebug,"AliL3FileHandler::AliDigits2RootFile","Rootfile")
362 <<"Rootfile did already exist, so I will just open it for updates"<<ENDLOG;
363 digFile = TFile::Open(new_digitsfile,"UPDATE");
366 if(!digFile->IsOpen())
368 LOG(AliL3Log::kError,"AliL3FileHandler::AliDigits2RootFile","Rootfile")
369 <<"Error opening rootfile "<<new_digitsfile<<ENDLOG;
375 //setup a new one, or connect it to the existing one:
376 AliTPCDigitsArray *arr = new AliTPCDigitsArray;
377 arr->SetClass("AliSimDigits");
383 Bool_t ok = arr->ConnectTree("TreeD_75x40_100x60_0");
386 printf("AliL3FileHandler::AliDigits2RootFile : No digits tree object in existing file\n");
390 for(Int_t i=fRowMin; i<=fRowMax; i++)
393 if(rowPt->fRow != i) printf("AliL3FileHandler::AliDigits2RootFile : Mismatching row numbering!!!\n");
396 fTransformer->Slice2Sector(fSlice,i,sector,row);
397 AliDigits * dig = arr->CreateRow(sector,row);
398 AliDigits *old_dig = old_array->LoadRow(sector,row);
400 printf("AliL3FileHandler::AliDigits2RootFile : No padrow %d %d\n",sector,row);
402 AliL3DigitData *digPt = rowPt->fDigitData;
403 for(UInt_t j=0; j<rowPt->fNDigit; j++)
405 UShort_t charge = digPt[j].fCharge;
406 UChar_t pad = digPt[j].fPad;
407 UShort_t time = digPt[j].fTime;
409 if(charge == 0) //Only write the digits that has not been removed
411 dig->SetDigitFast(old_dig->GetDigit(time,pad),time,pad);
412 ((AliSimDigits*)dig)->SetTrackIDFast(((AliSimDigits*)old_dig)->GetTrackID((Int_t)time,(Int_t)pad,0),time,pad,0);
413 ((AliSimDigits*)dig)->SetTrackIDFast(((AliSimDigits*)old_dig)->GetTrackID((Int_t)time,(Int_t)pad,1),time,pad,1);
414 ((AliSimDigits*)dig)->SetTrackIDFast(((AliSimDigits*)old_dig)->GetTrackID((Int_t)time,(Int_t)pad,2),time,pad,2);
417 UpdateRowPointer(rowPt);
418 arr->StoreRow(sector,row);
419 arr->ClearRow(sector,row);
420 old_array->ClearRow(sector,row);
424 sprintf(treeName,"TreeD_%s_0",fParam->GetTitle());
425 printf("Writing tree to file.....");
426 arr->GetTree()->Write(treeName,TObject::kOverwrite);
429 //arr->GetTree()->Delete();
433 ///////////////////////////////////////// Point IO
434 Bool_t AliL3FileHandler::AliPoints2Binary(){
437 AliL3SpacePointData *data = AliPoints2Memory(npoint);
438 out = Memory2Binary(npoint,data);
443 AliL3SpacePointData * AliL3FileHandler::AliPoints2Memory(UInt_t & npoint){
444 AliL3SpacePointData *data = 0;
447 LOG(AliL3Log::kWarning,"AliL3FileHandler::AliPoints2Memory","File")
448 <<"No Input avalible: no object TFile"<<ENDLOG;
451 if(!fInAli->IsOpen()){
452 LOG(AliL3Log::kWarning,"AliL3FileHandler::AliPoints2Memory","File")
453 <<"No Input avalible: TFile not opend"<<ENDLOG;
456 TDirectory *savedir = gDirectory;
461 sprintf(cname,"TreeC_TPC_%d",eventn);
462 AliTPCClustersArray carray;
463 carray.Setup(fParam);
464 carray.SetClusterType("AliTPCcluster");
465 Bool_t clusterok = carray.ConnectTree(cname);
466 if(!clusterok) return 0;
468 AliTPCClustersRow ** clusterrow =
469 new AliTPCClustersRow*[ (int)carray.GetTree()->GetEntries()];
470 Int_t *rows = new int[ (int)carray.GetTree()->GetEntries()];
471 Int_t *sects = new int[ (int)carray.GetTree()->GetEntries()];
475 for(Int_t i=0; i<carray.GetTree()->GetEntries(); i++){
476 AliSegmentID *s = carray.LoadEntry(i);
478 fParam->AdjustSectorRow(s->GetID(),sector,row);
482 fTransformer->Sector2Slice(lslice,lrow,sector,row);
483 if(fSlice != lslice || lrow<fRowMin || lrow>fRowMax) continue;
484 clusterrow[i] = carray.GetRow(sector,row);
486 sum+=clusterrow[i]->GetArray()->GetEntriesFast();
488 UInt_t size = sum*sizeof(AliL3SpacePointData);
490 LOG(AliL3Log::kDebug,"AliL3FileHandler::AliPoints2Memory","File")
491 <<AliL3Log::kDec<<"Found "<<sum<<" SpacePoints"<<ENDLOG;
493 data = (AliL3SpacePointData *) Allocate(size);
496 for(Int_t i=0; i<carray.GetTree()->GetEntries(); i++){
497 if(!clusterrow[i]) continue;
499 Int_t sector = sects[i];
500 fTransformer->Sector2Slice(lslice,lrow,sector,row);
501 Int_t entries_in_row = clusterrow[i]->GetArray()->GetEntriesFast();
502 for(Int_t j = 0;j<entries_in_row;j++){
503 AliTPCcluster *c = (AliTPCcluster*)(*clusterrow[i])[j];
504 data[n].fZ = c->GetZ();
505 data[n].fY = c->GetY();
506 data[n].fX = fParam->GetPadRowRadii(sector,row);
507 data[n].fID = n+((fSlice&0x7f)<<25)+((fPatch&0x7)<<22);//uli
508 data[n].fPadRow = lrow;
509 data[n].fXYErr = c->GetSigmaY2();
510 data[n].fZErr = c->GetSigmaZ2();
511 if(fMC) fprintf(fMC,"%d %d\n",data[n].fID,c->GetLabel(0));
515 for(Int_t i=0;i<carray.GetTree()->GetEntries();i++){
517 Int_t sector = sects[i];
518 if(carray.GetRow(sector,row))
519 carray.ClearRow(sector,row);
522 delete [] clusterrow;