2 //Author: Uli Frankenfeld
3 //Last Modified: 17.12.2000
12 #include "AliL3Transform.h"
13 #include "AliL3Logging.h"
14 #include "AliL3MemHandler.h"
15 #include "AliL3FileHandler.h"
17 #include "AliTPCDigitsArray.h"
18 #include "AliTPCClustersArray.h"
19 #include "AliTPCcluster.h"
20 #include "AliTPCClustersRow.h"
21 #include "AliTPCParam.h"
22 #include "AliSimDigits.h"
24 #include "AliL3DigitData.h"
25 #include "AliL3TrackSegmentData.h"
26 #include "AliL3SpacePointData.h"
27 #include "AliL3TrackArray.h"
28 //_____________________________________________________________
30 // The L3 Binary File handler
33 ClassImp(AliL3FileHandler)
35 AliL3FileHandler::AliL3FileHandler(){
44 AliL3FileHandler::~AliL3FileHandler(){
46 if(fTransformer) delete fTransformer;
47 if(fMC) CloseMCOutput();
50 Bool_t AliL3FileHandler::SetMCOutput(char *name){
51 fMC = fopen(name,"w");
53 LOG(AliL3Log::kWarning,"AliL3FileHandler::SetMCOutput","File Open")
54 <<"Pointer to File = 0x0 "<<ENDLOG;
60 Bool_t AliL3FileHandler::SetMCOutput(FILE *file){
63 LOG(AliL3Log::kWarning,"AliL3FileHandler::SetMCOutput","File Open")
64 <<"Pointer to File = 0x0 "<<ENDLOG;
70 void AliL3FileHandler::CloseMCOutput(){
72 LOG(AliL3Log::kWarning,"AliL3FileHandler::CloseMCOutPut","File Close")
73 <<"Nothing to Close"<<ENDLOG;
80 Bool_t AliL3FileHandler::SetAliInput(){
81 if(!fInAli->IsOpen()){
82 LOG(AliL3Log::kError,"AliL3FileHandler::SetAliInput","File Open")
83 <<"Ali File "<<fInAli->GetName()<<" does not exist"<<ENDLOG;
86 fParam = (AliTPCParam*)fInAli->Get("75x40_100x60");
88 LOG(AliL3Log::kError,"AliL3FileHandler::SetAliInput","File Open")
89 <<"No AliTPCParam 75x40_100x60 in File "<<fInAli->GetName()<<ENDLOG;
92 fTransformer = new AliL3Transform();
96 Bool_t AliL3FileHandler::SetAliInput(char *name){
97 fInAli= new TFile(name,"READ");
99 LOG(AliL3Log::kWarning,"AliL3FileHandler::SetAliInput","File Open")
100 <<"Pointer to TFile = 0x0 "<<ENDLOG;
103 return SetAliInput();
106 Bool_t AliL3FileHandler::SetAliInput(TFile *file){
109 LOG(AliL3Log::kWarning,"AliL3FileHandler::SetAliInput","File Open")
110 <<"Pointer to TFile = 0x0 "<<ENDLOG;
113 return SetAliInput();
116 void AliL3FileHandler::CloseAliInput(){
118 LOG(AliL3Log::kWarning,"AliL3FileHandler::CloseAliInput","File Close")
119 <<"Nothing to Close"<<ENDLOG;
122 if(fInAli->IsOpen()) fInAli->Close();
127 Bool_t AliL3FileHandler::IsDigit(){
129 LOG(AliL3Log::kWarning,"AliL3FileHandler::IsDigit","File")
130 <<"Pointer to TFile = 0x0 "<<ENDLOG;
131 return kTRUE; //may you are use binary input which is Digits!!
133 TTree *t=(TTree*)fInAli->Get("TreeD_75x40_100x60_0");
135 LOG(AliL3Log::kInformational,"AliL3FileHandler::IsDigit","File Type")
136 <<"Found Digit Tree -> Use Fast Cluster Finder"<<ENDLOG;
140 LOG(AliL3Log::kInformational,"AliL3FileHandler::IsDigit","File Type")
141 <<"No Digit Tree -> Use Cluster Tree"<<ENDLOG;
146 ///////////////////////////////////////// Digit IO
147 Bool_t AliL3FileHandler::AliDigits2Binary(){
150 AliL3DigitRowData* data = AliDigits2Memory(nrow);
151 out = Memory2Binary(nrow,data);
157 Bool_t AliL3FileHandler::AliDigits2CompBinary(){
160 AliL3DigitRowData* digits = AliDigits2Memory(ndigits);
161 out = Memory2CompBinary(ndigits,digits);
167 AliL3DigitRowData * AliL3FileHandler::AliDigits2Memory(UInt_t & nrow){
168 AliL3DigitRowData *data = 0;
171 LOG(AliL3Log::kWarning,"AliL3FileHandler::AliDigits2Memory","File")
172 <<"No Input avalible: no object TFile"<<ENDLOG;
175 if(!fInAli->IsOpen()){
176 LOG(AliL3Log::kWarning,"AliL3FileHandler::AliDigits2Memory","File")
177 <<"No Input avalible: TFile not opend"<<ENDLOG;
181 TDirectory *savedir = gDirectory;
183 TTree *t=(TTree*)fInAli->Get("TreeD_75x40_100x60_0");
185 LOG(AliL3Log::kWarning,"AliL3FileHandler::AliDigits2Binary","AliRoot")
186 <<"No Digit Tree inside!"<<ENDLOG;
189 AliSimDigits digarr, *dummy=&digarr;
190 t->GetBranch("Segment")->SetAddress(&dummy);
192 Int_t time,pad,sector,row;
195 Int_t entries = (Int_t)t->GetEntries();
196 Int_t ndigits[entries];
198 for(Int_t n=0; n<t->GetEntries(); n++)
201 fParam->AdjustSectorRow(digarr.GetID(),sector,row);
202 fTransformer->Sector2Slice(lslice,lrow,sector,row);
203 if(fSlice != lslice || lrow<fRowMin || lrow>fRowMax) continue;
204 // if(fSlice != lslice) continue;
210 time=digarr.CurrentRow();
211 pad=digarr.CurrentColumn();
212 dig = digarr.GetDigit(time,pad);
213 if(dig<=fParam->GetZeroSup()) continue;
214 if(time < fParam->GetMaxTBin()-1 && time > 0)
215 if(digarr.GetDigit(time+1,pad) <= fParam->GetZeroSup()
216 && digarr.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 (digarr.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=0; n<t->GetEntries(); n++)
244 fParam->AdjustSectorRow(digarr.GetID(),sector,row);
245 fTransformer->Sector2Slice(lslice,lrow,sector,row);
246 // if(fSlice != lslice) continue;
247 if(fSlice != lslice || lrow<fRowMin || lrow>fRowMax) continue;
249 tempPt->fNDigit = ndigits[lrow];
254 //dig=digarr.CurrentDigit();
255 //if (dig<=fParam->GetZeroSup()) continue;
256 time=digarr.CurrentRow();
257 pad=digarr.CurrentColumn();
258 dig = digarr.GetDigit(time,pad);
259 if (dig <= fParam->GetZeroSup()) continue;
260 if(time < fParam->GetMaxTBin()-1 && time > 0)
261 if(digarr.GetDigit(time-1,pad) <= fParam->GetZeroSup() &&
262 digarr.GetDigit(time+1,pad) <= fParam->GetZeroSup()) continue;
264 //Exclude data outside cone:
265 fTransformer->Raw2Local(xyz,sector,row,pad,time);
266 if(fParam->GetPadRowRadii(sector,row)<230./250.*fabs(xyz[2]))
269 if(localcount >= ndigits[lrow])
270 LOG(AliL3Log::kFatal,"AliL3FileHandler::AliDigits2Binary","Memory")
271 <<AliL3Log::kDec<<"Mismatch: localcount "<<localcount<<" ndigits "
272 <<ndigits[lrow]<<ENDLOG;
274 tempPt->fDigitData[localcount].fCharge=dig;
275 tempPt->fDigitData[localcount].fPad=pad;
276 tempPt->fDigitData[localcount].fTime=time;
278 } while (digarr.Next());
280 Byte_t *tmp = (Byte_t*)tempPt;
281 Int_t size = sizeof(AliL3DigitRowData)
282 + ndigits[lrow]*sizeof(AliL3DigitData);
284 tempPt = (AliL3DigitRowData*)tmp;
290 void AliL3FileHandler::AliDigits2RootFile(AliL3DigitRowData *rowPt,Char_t *new_digitsfile)
292 //Write digits to a new alirootfile.
296 printf("AliL3FileHandler::AliDigits2RootFile : No input alirootfile\n");
301 printf("AliL3FileHandler::AliDigits2RootFile : No parameter object. Run on rootfile\n");
306 printf("AliL3FileHandler::AliDigits2RootFile : No transform object\n");
310 //Get the original digitstree:
312 AliTPCDigitsArray *old_array = new AliTPCDigitsArray();
313 old_array->Setup(fParam);
314 old_array->SetClass("AliSimDigits");
315 Bool_t ok = old_array->ConnectTree("TreeD_75x40_100x60_0");
318 printf("AliL3FileHandler::AliDigits2RootFile : No digits tree object\n");
322 Bool_t create=kFALSE;
327 digFile = TFile::Open(new_digitsfile,"RECREATE");
329 fParam->Write(fParam->GetTitle());
333 digFile = TFile::Open(new_digitsfile,"UPDATE");
336 if(!digFile->IsOpen())
338 printf("AliL3FileHandler::AliDigits2RootFile : Error opening a new rootfile\n");
344 //setup a new one, or connect it to the existing one:
345 AliTPCDigitsArray *arr = new AliTPCDigitsArray;
346 arr->SetClass("AliSimDigits");
352 Bool_t ok = arr->ConnectTree("TreeD_75x40_100x60_0");
355 printf("AliL3FileHandler::AliDigits2RootFile : No digits tree object in existing file\n");
359 for(Int_t i=fRowMin; i<=fRowMax; i++)
362 if(rowPt->fRow != i) printf("AliL3FileHandler::AliDigits2RootFile : Mismatching row numbering!!!\n");
365 fTransformer->Slice2Sector(fSlice,i,sector,row);
366 AliDigits * dig = arr->CreateRow(sector,row);
367 AliDigits *old_dig = old_array->LoadRow(sector,row);
369 printf("AliL3FileHandler::AliDigits2RootFile : No padrow %d %d\n",sector,row);
371 AliL3DigitData *digPt = rowPt->fDigitData;
372 for(UInt_t j=0; j<rowPt->fNDigit; j++)
374 UShort_t charge = digPt[j].fCharge;
375 UChar_t pad = digPt[j].fPad;
376 UShort_t time = digPt[j].fTime;
378 if(charge == 0) //Only write the digits that has not been removed
380 dig->SetDigitFast(old_dig->GetDigit(time,pad),time,pad);
381 ((AliSimDigits*)dig)->SetTrackIDFast(((AliSimDigits*)old_dig)->GetTrackID((Int_t)time,(Int_t)pad,0),time,pad,0);
382 ((AliSimDigits*)dig)->SetTrackIDFast(((AliSimDigits*)old_dig)->GetTrackID((Int_t)time,(Int_t)pad,1),time,pad,1);
383 ((AliSimDigits*)dig)->SetTrackIDFast(((AliSimDigits*)old_dig)->GetTrackID((Int_t)time,(Int_t)pad,2),time,pad,2);
386 UpdateRowPointer(rowPt);
387 arr->StoreRow(sector,row);
388 arr->ClearRow(sector,row);
389 old_array->ClearRow(sector,row);
393 sprintf(treeName,"TreeD_%s_0",fParam->GetTitle());
394 arr->GetTree()->Write(treeName,TObject::kOverwrite);
396 //arr->GetTree()->Delete();
400 ///////////////////////////////////////// Point IO
401 Bool_t AliL3FileHandler::AliPoints2Binary(){
404 AliL3SpacePointData *data = AliPoints2Memory(npoint);
405 out = Memory2Binary(npoint,data);
410 AliL3SpacePointData * AliL3FileHandler::AliPoints2Memory(UInt_t & npoint){
411 AliL3SpacePointData *data = 0;
414 LOG(AliL3Log::kWarning,"AliL3FileHandler::AliPoints2Memory","File")
415 <<"No Input avalible: no object TFile"<<ENDLOG;
418 if(!fInAli->IsOpen()){
419 LOG(AliL3Log::kWarning,"AliL3FileHandler::AliPoints2Memory","File")
420 <<"No Input avalible: TFile not opend"<<ENDLOG;
423 TDirectory *savedir = gDirectory;
426 AliTPCClustersArray carray;
427 carray.Setup(fParam);
428 carray.SetClusterType("AliTPCcluster");
429 Bool_t clusterok = carray.ConnectTree("Segment Tree");
430 if(!clusterok) return 0;
432 AliTPCClustersRow ** clusterrow =
433 new AliTPCClustersRow*[ (int)carray.GetTree()->GetEntries()];
434 Int_t *rows = new int[ (int)carray.GetTree()->GetEntries()];
435 Int_t *sects = new int[ (int)carray.GetTree()->GetEntries()];
439 for(Int_t i=0; i<carray.GetTree()->GetEntries(); i++){
440 AliSegmentID *s = carray.LoadEntry(i);
442 fParam->AdjustSectorRow(s->GetID(),sector,row);
446 fTransformer->Sector2Slice(lslice,lrow,sector,row);
447 if(fSlice != lslice || lrow<fRowMin || lrow>fRowMax) continue;
448 clusterrow[i] = carray.GetRow(sector,row);
450 sum+=clusterrow[i]->GetArray()->GetEntriesFast();
452 UInt_t size = sum*sizeof(AliL3SpacePointData);
454 LOG(AliL3Log::kDebug,"AliL3FileHandler::AliPoints2Memory","File")
455 <<AliL3Log::kDec<<"Found "<<sum<<" SpacePoints"<<ENDLOG;
457 data = (AliL3SpacePointData *) Allocate(size);
460 for(Int_t i=0; i<carray.GetTree()->GetEntries(); i++){
461 if(!clusterrow[i]) continue;
463 Int_t sector = sects[i];
464 fTransformer->Sector2Slice(lslice,lrow,sector,row);
465 Int_t entries_in_row = clusterrow[i]->GetArray()->GetEntriesFast();
466 for(Int_t j = 0;j<entries_in_row;j++){
467 AliTPCcluster *c = (AliTPCcluster*)(*clusterrow[i])[j];
468 data[n].fZ = c->GetZ();
469 data[n].fY = c->GetY();
470 data[n].fX = fParam->GetPadRowRadii(sector,row);
471 data[n].fID = n+((fSlice&0x7f)<<25)+((fPatch&0x7)<<22);//uli
472 data[n].fPadRow = lrow;
473 data[n].fXYErr = c->GetSigmaY2();
474 data[n].fZErr = c->GetSigmaZ2();
475 if(fMC) fprintf(fMC,"%d %d\n",data[n].fID,c->GetLabel(0));
479 for(Int_t i=0;i<carray.GetTree()->GetEntries();i++){
481 Int_t sector = sects[i];
482 if(carray.GetRow(sector,row))
483 carray.ClearRow(sector,row);
486 delete [] clusterrow;