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(){
47 AliL3FileHandler::~AliL3FileHandler(){
49 if(fTransformer) delete fTransformer;
50 if(fMC) CloseMCOutput();
51 if(fInAli) CloseAliInput();
54 Bool_t AliL3FileHandler::SetMCOutput(char *name){
55 fMC = fopen(name,"w");
57 LOG(AliL3Log::kWarning,"AliL3FileHandler::SetMCOutput","File Open")
58 <<"Pointer to File = 0x0 "<<ENDLOG;
64 Bool_t AliL3FileHandler::SetMCOutput(FILE *file){
67 LOG(AliL3Log::kWarning,"AliL3FileHandler::SetMCOutput","File Open")
68 <<"Pointer to File = 0x0 "<<ENDLOG;
74 void AliL3FileHandler::CloseMCOutput(){
76 LOG(AliL3Log::kWarning,"AliL3FileHandler::CloseMCOutPut","File Close")
77 <<"Nothing to Close"<<ENDLOG;
84 Bool_t AliL3FileHandler::SetAliInput(){
85 if(!fInAli->IsOpen()){
86 LOG(AliL3Log::kError,"AliL3FileHandler::SetAliInput","File Open")
87 <<"Ali File "<<fInAli->GetName()<<" does not exist"<<ENDLOG;
90 fParam = (AliTPCParam*)fInAli->Get("75x40_100x60");
92 LOG(AliL3Log::kError,"AliL3FileHandler::SetAliInput","File Open")
93 <<"No AliTPCParam 75x40_100x60 in File "<<fInAli->GetName()<<ENDLOG;
96 fTransformer = new AliL3Transform();
100 Bool_t AliL3FileHandler::SetAliInput(char *name){
101 fInAli= new TFile(name,"READ");
103 LOG(AliL3Log::kWarning,"AliL3FileHandler::SetAliInput","File Open")
104 <<"Pointer to TFile = 0x0 "<<ENDLOG;
107 return SetAliInput();
110 Bool_t AliL3FileHandler::SetAliInput(TFile *file){
113 LOG(AliL3Log::kWarning,"AliL3FileHandler::SetAliInput","File Open")
114 <<"Pointer to TFile = 0x0 "<<ENDLOG;
117 return SetAliInput();
120 void AliL3FileHandler::CloseAliInput(){
122 LOG(AliL3Log::kWarning,"AliL3FileHandler::CloseAliInput","File Close")
123 <<"Nothing to Close"<<ENDLOG;
126 if(fInAli->IsOpen()) fInAli->Close();
132 Bool_t AliL3FileHandler::IsDigit(){
134 LOG(AliL3Log::kWarning,"AliL3FileHandler::IsDigit","File")
135 <<"Pointer to TFile = 0x0 "<<ENDLOG;
136 return kTRUE; //may you are use binary input which is Digits!!
138 TTree *t=(TTree*)fInAli->Get("TreeD_75x40_100x60_0");
140 LOG(AliL3Log::kInformational,"AliL3FileHandler::IsDigit","File Type")
141 <<"Found Digit Tree -> Use Fast Cluster Finder"<<ENDLOG;
145 LOG(AliL3Log::kInformational,"AliL3FileHandler::IsDigit","File Type")
146 <<"No Digit Tree -> Use Cluster Tree"<<ENDLOG;
151 ///////////////////////////////////////// Digit IO
152 Bool_t AliL3FileHandler::AliDigits2Binary(Int_t event){
155 AliL3DigitRowData* data = AliDigits2Memory(nrow,event);
156 out = Memory2Binary(nrow,data);
162 Bool_t AliL3FileHandler::AliDigits2CompBinary(Int_t event){
165 AliL3DigitRowData* digits = AliDigits2Memory(ndigits,event);
166 out = Memory2CompBinary(ndigits,digits);
172 AliL3DigitRowData * AliL3FileHandler::AliDigits2Memory(UInt_t & nrow,Int_t event){
173 AliL3DigitRowData *data = 0;
176 LOG(AliL3Log::kWarning,"AliL3FileHandler::AliDigits2Memory","File")
177 <<"No Input avalible: no object TFile"<<ENDLOG;
180 if(!fInAli->IsOpen()){
181 LOG(AliL3Log::kWarning,"AliL3FileHandler::AliDigits2Memory","File")
182 <<"No Input avalible: TFile not opend"<<ENDLOG;
187 TDirectory *savedir = gDirectory;
189 //TTree *t=(TTree*)fInAli->Get("TreeD_75x40_100x60_0");
191 sprintf(dname,"TreeD_75x40_100x60_%d",event);
192 TTree *t=(TTree*)fInAli->Get(dname);
194 LOG(AliL3Log::kWarning,"AliL3FileHandler::AliDigits2Binary","AliRoot")
195 <<"No Digit Tree inside!"<<ENDLOG;
198 AliSimDigits digarr, *dummy=&digarr;
199 t->GetBranch("Segment")->SetAddress(&dummy);
201 Int_t time,pad,sector,row;
204 Int_t entries = (Int_t)t->GetEntries();
205 Int_t ndigits[entries];
207 for(Int_t n=0; n<t->GetEntries(); n++)
210 fParam->AdjustSectorRow(digarr.GetID(),sector,row);
211 fTransformer->Sector2Slice(lslice,lrow,sector,row);
212 if(fSlice != lslice || lrow<fRowMin || lrow>fRowMax) continue;
213 // if(fSlice != lslice) continue;
219 time=digarr.CurrentRow();
220 pad=digarr.CurrentColumn();
221 dig = digarr.GetDigit(time,pad);
222 if(dig<=fParam->GetZeroSup()) continue;
223 if(time < fParam->GetMaxTBin()-1 && time > 0)
224 if(digarr.GetDigit(time+1,pad) <= fParam->GetZeroSup()
225 && digarr.GetDigit(time-1,pad) <= fParam->GetZeroSup())
228 fTransformer->Raw2Local(xyz,sector,row,pad,time);
229 if(fParam->GetPadRowRadii(sector,row)<230./250.*fabs(xyz[2]))
232 ndigits[lrow]++; //for this row only
233 ndigitcount++; //total number of digits to be published
235 } while (digarr.Next());
239 Int_t size = sizeof(AliL3DigitData)*ndigitcount
240 + nrows*sizeof(AliL3DigitRowData);
242 LOG(AliL3Log::kDebug,"AliL3FileHandler::AliDigits2Memory","Digits")
243 <<AliL3Log::kDec<<"Found "<<ndigitcount<<" Digits"<<ENDLOG;
245 data=(AliL3DigitRowData*) Allocate(size);
246 nrow = (UInt_t)nrows;
247 AliL3DigitRowData *tempPt = data;
248 for(Int_t n=0; n<t->GetEntries(); n++)
253 fParam->AdjustSectorRow(digarr.GetID(),sector,row);
254 fTransformer->Sector2Slice(lslice,lrow,sector,row);
255 // if(fSlice != lslice) continue;
256 if(fSlice != lslice || lrow<fRowMin || lrow>fRowMax) continue;
258 tempPt->fNDigit = ndigits[lrow];
263 //dig=digarr.CurrentDigit();
264 //if (dig<=fParam->GetZeroSup()) continue;
265 time=digarr.CurrentRow();
266 pad=digarr.CurrentColumn();
267 dig = digarr.GetDigit(time,pad);
268 if (dig <= fParam->GetZeroSup()) continue;
269 if(time < fParam->GetMaxTBin()-1 && time > 0)
270 if(digarr.GetDigit(time-1,pad) <= fParam->GetZeroSup() &&
271 digarr.GetDigit(time+1,pad) <= fParam->GetZeroSup()) continue;
273 //Exclude data outside cone:
274 fTransformer->Raw2Local(xyz,sector,row,pad,time);
275 if(fParam->GetPadRowRadii(sector,row)<230./250.*fabs(xyz[2]))
278 if(localcount >= ndigits[lrow])
279 LOG(AliL3Log::kFatal,"AliL3FileHandler::AliDigits2Binary","Memory")
280 <<AliL3Log::kDec<<"Mismatch: localcount "<<localcount<<" ndigits "
281 <<ndigits[lrow]<<ENDLOG;
283 tempPt->fDigitData[localcount].fCharge=dig;
284 tempPt->fDigitData[localcount].fPad=pad;
285 tempPt->fDigitData[localcount].fTime=time;
287 } while (digarr.Next());
289 Byte_t *tmp = (Byte_t*)tempPt;
290 Int_t size = sizeof(AliL3DigitRowData)
291 + ndigits[lrow]*sizeof(AliL3DigitData);
293 tempPt = (AliL3DigitRowData*)tmp;
301 void AliL3FileHandler::AliDigits2RootFile(AliL3DigitRowData *rowPt,Char_t *new_digitsfile)
303 //Write digits to a new alirootfile.
307 printf("AliL3FileHandler::AliDigits2RootFile : No rootfile\n");
312 printf("AliL3FileHandler::AliDigits2RootFile : No parameter object. Run on rootfile\n");
317 printf("AliL3FileHandler::AliDigits2RootFile : No transform object\n");
321 //Get the original digitstree:
323 AliTPCDigitsArray *old_array = new AliTPCDigitsArray();
324 old_array->Setup(fParam);
325 old_array->SetClass("AliSimDigits");
326 Bool_t ok = old_array->ConnectTree("TreeD_75x40_100x60_0");
329 printf("AliL3FileHandler::AliDigits2RootFile : No digits tree object\n");
333 Bool_t create=kFALSE;
336 digFile = TFile::Open(new_digitsfile,"NEW");
337 if(digFile->IsOpen())
340 fParam->Write(fParam->GetTitle());
344 LOG(AliL3Log::kDebug,"AliL3FileHandler::AliDigits2RootFile","Rootfile")
345 <<"Rootfile did already exist, so I will just open it for updates"<<ENDLOG;
346 digFile = TFile::Open(new_digitsfile,"UPDATE");
349 if(!digFile->IsOpen())
351 LOG(AliL3Log::kError,"AliL3FileHandler::AliDigits2RootFile","Rootfile")
352 <<"Error opening rootfile "<<new_digitsfile<<ENDLOG;
358 //setup a new one, or connect it to the existing one:
359 AliTPCDigitsArray *arr = new AliTPCDigitsArray;
360 arr->SetClass("AliSimDigits");
366 Bool_t ok = arr->ConnectTree("TreeD_75x40_100x60_0");
369 printf("AliL3FileHandler::AliDigits2RootFile : No digits tree object in existing file\n");
373 for(Int_t i=fRowMin; i<=fRowMax; i++)
376 if(rowPt->fRow != i) printf("AliL3FileHandler::AliDigits2RootFile : Mismatching row numbering!!!\n");
379 fTransformer->Slice2Sector(fSlice,i,sector,row);
380 AliDigits * dig = arr->CreateRow(sector,row);
381 AliDigits *old_dig = old_array->LoadRow(sector,row);
383 printf("AliL3FileHandler::AliDigits2RootFile : No padrow %d %d\n",sector,row);
385 AliL3DigitData *digPt = rowPt->fDigitData;
386 for(UInt_t j=0; j<rowPt->fNDigit; j++)
388 UShort_t charge = digPt[j].fCharge;
389 UChar_t pad = digPt[j].fPad;
390 UShort_t time = digPt[j].fTime;
392 if(charge == 0) //Only write the digits that has not been removed
394 dig->SetDigitFast(old_dig->GetDigit(time,pad),time,pad);
395 ((AliSimDigits*)dig)->SetTrackIDFast(((AliSimDigits*)old_dig)->GetTrackID((Int_t)time,(Int_t)pad,0),time,pad,0);
396 ((AliSimDigits*)dig)->SetTrackIDFast(((AliSimDigits*)old_dig)->GetTrackID((Int_t)time,(Int_t)pad,1),time,pad,1);
397 ((AliSimDigits*)dig)->SetTrackIDFast(((AliSimDigits*)old_dig)->GetTrackID((Int_t)time,(Int_t)pad,2),time,pad,2);
400 UpdateRowPointer(rowPt);
401 arr->StoreRow(sector,row);
402 arr->ClearRow(sector,row);
403 old_array->ClearRow(sector,row);
407 sprintf(treeName,"TreeD_%s_0",fParam->GetTitle());
408 printf("Writing tree to file.....");
409 arr->GetTree()->Write(treeName,TObject::kOverwrite);
412 //arr->GetTree()->Delete();
416 ///////////////////////////////////////// Point IO
417 Bool_t AliL3FileHandler::AliPoints2Binary(){
420 AliL3SpacePointData *data = AliPoints2Memory(npoint);
421 out = Memory2Binary(npoint,data);
426 AliL3SpacePointData * AliL3FileHandler::AliPoints2Memory(UInt_t & npoint){
427 AliL3SpacePointData *data = 0;
430 LOG(AliL3Log::kWarning,"AliL3FileHandler::AliPoints2Memory","File")
431 <<"No Input avalible: no object TFile"<<ENDLOG;
434 if(!fInAli->IsOpen()){
435 LOG(AliL3Log::kWarning,"AliL3FileHandler::AliPoints2Memory","File")
436 <<"No Input avalible: TFile not opend"<<ENDLOG;
439 TDirectory *savedir = gDirectory;
444 sprintf(cname,"TreeC_TPC_%d",eventn);
445 AliTPCClustersArray carray;
446 carray.Setup(fParam);
447 carray.SetClusterType("AliTPCcluster");
448 Bool_t clusterok = carray.ConnectTree(cname);
449 if(!clusterok) return 0;
451 AliTPCClustersRow ** clusterrow =
452 new AliTPCClustersRow*[ (int)carray.GetTree()->GetEntries()];
453 Int_t *rows = new int[ (int)carray.GetTree()->GetEntries()];
454 Int_t *sects = new int[ (int)carray.GetTree()->GetEntries()];
458 for(Int_t i=0; i<carray.GetTree()->GetEntries(); i++){
459 AliSegmentID *s = carray.LoadEntry(i);
461 fParam->AdjustSectorRow(s->GetID(),sector,row);
465 fTransformer->Sector2Slice(lslice,lrow,sector,row);
466 if(fSlice != lslice || lrow<fRowMin || lrow>fRowMax) continue;
467 clusterrow[i] = carray.GetRow(sector,row);
469 sum+=clusterrow[i]->GetArray()->GetEntriesFast();
471 UInt_t size = sum*sizeof(AliL3SpacePointData);
473 LOG(AliL3Log::kDebug,"AliL3FileHandler::AliPoints2Memory","File")
474 <<AliL3Log::kDec<<"Found "<<sum<<" SpacePoints"<<ENDLOG;
476 data = (AliL3SpacePointData *) Allocate(size);
479 for(Int_t i=0; i<carray.GetTree()->GetEntries(); i++){
480 if(!clusterrow[i]) continue;
482 Int_t sector = sects[i];
483 fTransformer->Sector2Slice(lslice,lrow,sector,row);
484 Int_t entries_in_row = clusterrow[i]->GetArray()->GetEntriesFast();
485 for(Int_t j = 0;j<entries_in_row;j++){
486 AliTPCcluster *c = (AliTPCcluster*)(*clusterrow[i])[j];
487 data[n].fZ = c->GetZ();
488 data[n].fY = c->GetY();
489 data[n].fX = fParam->GetPadRowRadii(sector,row);
490 data[n].fID = n+((fSlice&0x7f)<<25)+((fPatch&0x7)<<22);//uli
491 data[n].fPadRow = lrow;
492 data[n].fXYErr = c->GetSigmaY2();
493 data[n].fZErr = c->GetSigmaZ2();
494 if(fMC) fprintf(fMC,"%d %d\n",data[n].fID,c->GetLabel(0));
498 for(Int_t i=0;i<carray.GetTree()->GetEntries();i++){
500 Int_t sector = sects[i];
501 if(carray.GetRow(sector,row))
502 carray.ClearRow(sector,row);
505 delete [] clusterrow;