]> git.uio.no Git - u/mrichter/AliRoot.git/blame - HLT/src/AliL3FileHandler.cxx
Typo in the calculation of procetile-target distance is corrected
[u/mrichter/AliRoot.git] / HLT / src / AliL3FileHandler.cxx
CommitLineData
b661165c 1// Author: Uli Frankenfeld <mailto:franken@fi.uib.no>
2//*-- Copyright &copy Uli
108615fc 3
4#include <math.h>
5#include <iostream.h>
ee4604c0 6#include <TObject.h>
108615fc 7#include <TFile.h>
8#include <TTree.h>
9#include <stdio.h>
10
11#include "AliL3Transform.h"
12#include "AliL3Logging.h"
13#include "AliL3MemHandler.h"
14#include "AliL3FileHandler.h"
15
0d319e67 16#include "AliTPCDigitsArray.h"
108615fc 17#include "AliTPCClustersArray.h"
18#include "AliTPCcluster.h"
19#include "AliTPCClustersRow.h"
20#include "AliTPCParam.h"
21#include "AliSimDigits.h"
22
23#include "AliL3DigitData.h"
24#include "AliL3TrackSegmentData.h"
25#include "AliL3SpacePointData.h"
26#include "AliL3TrackArray.h"
b661165c 27
108615fc 28//_____________________________________________________________
b661165c 29// AliL3FileHandler
108615fc 30//
31// The L3 Binary File handler
32//
33
34ClassImp(AliL3FileHandler)
35
36AliL3FileHandler::AliL3FileHandler(){
37 //Default constructor
38 fInAli = 0;
39 fParam = 0;
40 fTransformer = 0;
41 fMC =0;
42}
43
44
45AliL3FileHandler::~AliL3FileHandler(){
46 //Destructor
47 if(fTransformer) delete fTransformer;
48 if(fMC) CloseMCOutput();
8f1a9904 49 if(fInAli) CloseAliInput();
108615fc 50}
51
52Bool_t AliL3FileHandler::SetMCOutput(char *name){
53 fMC = fopen(name,"w");
54 if(!fMC){
55 LOG(AliL3Log::kWarning,"AliL3FileHandler::SetMCOutput","File Open")
56 <<"Pointer to File = 0x0 "<<ENDLOG;
57 return kFALSE;
58 }
59 return kTRUE;
60}
61
62Bool_t AliL3FileHandler::SetMCOutput(FILE *file){
63 fMC = file;
64 if(!fMC){
65 LOG(AliL3Log::kWarning,"AliL3FileHandler::SetMCOutput","File Open")
66 <<"Pointer to File = 0x0 "<<ENDLOG;
67 return kFALSE;
68 }
69 return kTRUE;
70}
71
72void AliL3FileHandler::CloseMCOutput(){
73 if(!fMC){
74 LOG(AliL3Log::kWarning,"AliL3FileHandler::CloseMCOutPut","File Close")
75 <<"Nothing to Close"<<ENDLOG;
76 return;
77 }
78 fclose(fMC);
79 fMC =0;
80}
81
82Bool_t AliL3FileHandler::SetAliInput(){
83 if(!fInAli->IsOpen()){
84 LOG(AliL3Log::kError,"AliL3FileHandler::SetAliInput","File Open")
85 <<"Ali File "<<fInAli->GetName()<<" does not exist"<<ENDLOG;
86 return kFALSE;
87 }
88 fParam = (AliTPCParam*)fInAli->Get("75x40_100x60");
89 if(!fParam){
90 LOG(AliL3Log::kError,"AliL3FileHandler::SetAliInput","File Open")
91 <<"No AliTPCParam 75x40_100x60 in File "<<fInAli->GetName()<<ENDLOG;
92 return kFALSE;
93 }
94 fTransformer = new AliL3Transform();
95 return kTRUE;
96}
97
98Bool_t AliL3FileHandler::SetAliInput(char *name){
99 fInAli= new TFile(name,"READ");
100 if(!fInAli){
101 LOG(AliL3Log::kWarning,"AliL3FileHandler::SetAliInput","File Open")
102 <<"Pointer to TFile = 0x0 "<<ENDLOG;
103 return kFALSE;
104 }
105 return SetAliInput();
106}
107
108Bool_t AliL3FileHandler::SetAliInput(TFile *file){
109 fInAli=file;
110 if(!fInAli){
111 LOG(AliL3Log::kWarning,"AliL3FileHandler::SetAliInput","File Open")
112 <<"Pointer to TFile = 0x0 "<<ENDLOG;
113 return kFALSE;
114 }
115 return SetAliInput();
116}
117
118void AliL3FileHandler::CloseAliInput(){
119 if(!fInAli){
120 LOG(AliL3Log::kWarning,"AliL3FileHandler::CloseAliInput","File Close")
121 <<"Nothing to Close"<<ENDLOG;
122 return;
123 }
124 if(fInAli->IsOpen()) fInAli->Close();
125 delete fInAli;
126 fInAli = 0;
8f1a9904 127
108615fc 128}
129
130Bool_t AliL3FileHandler::IsDigit(){
131 if(!fInAli){
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!!
135 }
03b6adf7 136 TTree *t=(TTree*)fInAli->Get("TreeD_75x40_100x60_0");
108615fc 137 if(t){
138 LOG(AliL3Log::kInformational,"AliL3FileHandler::IsDigit","File Type")
139 <<"Found Digit Tree -> Use Fast Cluster Finder"<<ENDLOG;
140 return kTRUE;
141 }
142 else{
143 LOG(AliL3Log::kInformational,"AliL3FileHandler::IsDigit","File Type")
144 <<"No Digit Tree -> Use Cluster Tree"<<ENDLOG;
145 return kFALSE;
146 }
147}
148
149///////////////////////////////////////// Digit IO
150Bool_t AliL3FileHandler::AliDigits2Binary(){
151 Bool_t out = kTRUE;
152 UInt_t nrow;
153 AliL3DigitRowData* data = AliDigits2Memory(nrow);
154 out = Memory2Binary(nrow,data);
155 Free();
156 return out;
157}
158
159
160Bool_t AliL3FileHandler::AliDigits2CompBinary(){
161 Bool_t out = kTRUE;
162 UInt_t ndigits=0;
163 AliL3DigitRowData* digits = AliDigits2Memory(ndigits);
164 out = Memory2CompBinary(ndigits,digits);
165 Free();
166 return out;
167}
168
169
170AliL3DigitRowData * AliL3FileHandler::AliDigits2Memory(UInt_t & nrow){
171 AliL3DigitRowData *data = 0;
172 nrow=0;
173 if(!fInAli){
174 LOG(AliL3Log::kWarning,"AliL3FileHandler::AliDigits2Memory","File")
175 <<"No Input avalible: no object TFile"<<ENDLOG;
176 return 0;
177 }
178 if(!fInAli->IsOpen()){
179 LOG(AliL3Log::kWarning,"AliL3FileHandler::AliDigits2Memory","File")
180 <<"No Input avalible: TFile not opend"<<ENDLOG;
181 return 0;
182 }
183
184 TDirectory *savedir = gDirectory;
185 fInAli->cd();
03b6adf7 186 TTree *t=(TTree*)fInAli->Get("TreeD_75x40_100x60_0");
108615fc 187 if(!t){
188 LOG(AliL3Log::kWarning,"AliL3FileHandler::AliDigits2Binary","AliRoot")
189 <<"No Digit Tree inside!"<<ENDLOG;
190 return 0;
191 }
192 AliSimDigits digarr, *dummy=&digarr;
193 t->GetBranch("Segment")->SetAddress(&dummy);
194 UShort_t dig;
195 Int_t time,pad,sector,row;
196 Int_t nrows=0;
197 Int_t ndigitcount=0;
198 Int_t entries = (Int_t)t->GetEntries();
199 Int_t ndigits[entries];
200 Int_t lslice,lrow;
201 for(Int_t n=0; n<t->GetEntries(); n++)
202 {
203 t->GetEvent(n);
204 fParam->AdjustSectorRow(digarr.GetID(),sector,row);
205 fTransformer->Sector2Slice(lslice,lrow,sector,row);
206 if(fSlice != lslice || lrow<fRowMin || lrow>fRowMax) continue;
03b6adf7 207 // if(fSlice != lslice) continue;
108615fc 208
209 Float_t xyz[3];
210 ndigits[lrow] = 0;
211 digarr.First();
212 do {
213 time=digarr.CurrentRow();
214 pad=digarr.CurrentColumn();
215 dig = digarr.GetDigit(time,pad);
216 if(dig<=fParam->GetZeroSup()) continue;
217 if(time < fParam->GetMaxTBin()-1 && time > 0)
218 if(digarr.GetDigit(time+1,pad) <= fParam->GetZeroSup()
219 && digarr.GetDigit(time-1,pad) <= fParam->GetZeroSup())
220 continue;
221
222 fTransformer->Raw2Local(xyz,sector,row,pad,time);
223 if(fParam->GetPadRowRadii(sector,row)<230./250.*fabs(xyz[2]))
224 continue;
225
226 ndigits[lrow]++; //for this row only
227 ndigitcount++; //total number of digits to be published
228
229 } while (digarr.Next());
230
231 nrows++;
232 }
233 Int_t size = sizeof(AliL3DigitData)*ndigitcount
8f1a9904 234 + nrows*sizeof(AliL3DigitRowData);
108615fc 235
236 LOG(AliL3Log::kDebug,"AliL3FileHandler::AliDigits2Memory","Digits")
237 <<AliL3Log::kDec<<"Found "<<ndigitcount<<" Digits"<<ENDLOG;
238
239 data=(AliL3DigitRowData*) Allocate(size);
240 nrow = (UInt_t)nrows;
241 AliL3DigitRowData *tempPt = data;
242 for(Int_t n=0; n<t->GetEntries(); n++)
243 {
244 t->GetEvent(n);
245
246 Float_t xyz[3];
247 fParam->AdjustSectorRow(digarr.GetID(),sector,row);
248 fTransformer->Sector2Slice(lslice,lrow,sector,row);
249// if(fSlice != lslice) continue;
250 if(fSlice != lslice || lrow<fRowMin || lrow>fRowMax) continue;
251 tempPt->fRow = lrow;
252 tempPt->fNDigit = ndigits[lrow];
253
254 Int_t localcount=0;
255 digarr.First();
256 do {
03b6adf7 257 //dig=digarr.CurrentDigit();
258 //if (dig<=fParam->GetZeroSup()) continue;
108615fc 259 time=digarr.CurrentRow();
260 pad=digarr.CurrentColumn();
03b6adf7 261 dig = digarr.GetDigit(time,pad);
262 if (dig <= fParam->GetZeroSup()) continue;
263 if(time < fParam->GetMaxTBin()-1 && time > 0)
108615fc 264 if(digarr.GetDigit(time-1,pad) <= fParam->GetZeroSup() &&
265 digarr.GetDigit(time+1,pad) <= fParam->GetZeroSup()) continue;
266
267 //Exclude data outside cone:
268 fTransformer->Raw2Local(xyz,sector,row,pad,time);
269 if(fParam->GetPadRowRadii(sector,row)<230./250.*fabs(xyz[2]))
270 continue;
271
272 if(localcount >= ndigits[lrow])
273 LOG(AliL3Log::kFatal,"AliL3FileHandler::AliDigits2Binary","Memory")
03b6adf7 274 <<AliL3Log::kDec<<"Mismatch: localcount "<<localcount<<" ndigits "
275 <<ndigits[lrow]<<ENDLOG;
276
108615fc 277 tempPt->fDigitData[localcount].fCharge=dig;
278 tempPt->fDigitData[localcount].fPad=pad;
279 tempPt->fDigitData[localcount].fTime=time;
280 localcount++;
281 } while (digarr.Next());
282
283 Byte_t *tmp = (Byte_t*)tempPt;
284 Int_t size = sizeof(AliL3DigitRowData)
285 + ndigits[lrow]*sizeof(AliL3DigitData);
286 tmp += size;
287 tempPt = (AliL3DigitRowData*)tmp;
288 }
289 savedir->cd();
290 return data;
291}
292
0d319e67 293void AliL3FileHandler::AliDigits2RootFile(AliL3DigitRowData *rowPt,Char_t *new_digitsfile)
294{
295 //Write digits to a new alirootfile.
296
297 if(!fInAli)
298 {
8f1a9904 299 printf("AliL3FileHandler::AliDigits2RootFile : No rootfile\n");
0d319e67 300 return;
301 }
302 if(!fParam)
303 {
304 printf("AliL3FileHandler::AliDigits2RootFile : No parameter object. Run on rootfile\n");
305 return;
306 }
307 if(!fTransformer)
308 {
309 printf("AliL3FileHandler::AliDigits2RootFile : No transform object\n");
310 return;
311 }
312
313 //Get the original digitstree:
314 fInAli->cd();
315 AliTPCDigitsArray *old_array = new AliTPCDigitsArray();
316 old_array->Setup(fParam);
317 old_array->SetClass("AliSimDigits");
03b6adf7 318 Bool_t ok = old_array->ConnectTree("TreeD_75x40_100x60_0");
0d319e67 319 if(!ok)
320 {
321 printf("AliL3FileHandler::AliDigits2RootFile : No digits tree object\n");
322 return;
323 }
0d319e67 324
03b6adf7 325 Bool_t create=kFALSE;
326 TFile *digFile;
327
8f1a9904 328 digFile = TFile::Open(new_digitsfile,"NEW");
329 if(digFile->IsOpen())
03b6adf7 330 {
03b6adf7 331 create = kTRUE;
332 fParam->Write(fParam->GetTitle());
333 }
334 else
335 {
8f1a9904 336 LOG(AliL3Log::kDebug,"AliL3FileHandler::AliDigits2RootFile","Rootfile")
337 <<"Rootfile did already exist, so I will just open it for updates"<<ENDLOG;
03b6adf7 338 digFile = TFile::Open(new_digitsfile,"UPDATE");
339 create=kFALSE;
340 }
341 if(!digFile->IsOpen())
342 {
8f1a9904 343 LOG(AliL3Log::kError,"AliL3FileHandler::AliDigits2RootFile","Rootfile")
344 <<"Error opening rootfile "<<new_digitsfile<<ENDLOG;
03b6adf7 345 return;
346 }
347
348 digFile->cd();
349
350 //setup a new one, or connect it to the existing one:
0d319e67 351 AliTPCDigitsArray *arr = new AliTPCDigitsArray;
352 arr->SetClass("AliSimDigits");
353 arr->Setup(fParam);
03b6adf7 354 if(create)
355 arr->MakeTree();
356 else
357 {
358 Bool_t ok = arr->ConnectTree("TreeD_75x40_100x60_0");
359 if(!ok)
360 {
361 printf("AliL3FileHandler::AliDigits2RootFile : No digits tree object in existing file\n");
362 return;
363 }
364 }
0d319e67 365 for(Int_t i=fRowMin; i<=fRowMax; i++)
366 {
03b6adf7 367
368 if(rowPt->fRow != i) printf("AliL3FileHandler::AliDigits2RootFile : Mismatching row numbering!!!\n");
369
0d319e67 370 Int_t sector,row;
371 fTransformer->Slice2Sector(fSlice,i,sector,row);
372 AliDigits * dig = arr->CreateRow(sector,row);
373 AliDigits *old_dig = old_array->LoadRow(sector,row);
374 if(!old_dig)
375 printf("AliL3FileHandler::AliDigits2RootFile : No padrow %d %d\n",sector,row);
376
377 AliL3DigitData *digPt = rowPt->fDigitData;
378 for(UInt_t j=0; j<rowPt->fNDigit; j++)
379 {
380 UShort_t charge = digPt[j].fCharge;
381 UChar_t pad = digPt[j].fPad;
382 UShort_t time = digPt[j].fTime;
03b6adf7 383
384 if(charge == 0) //Only write the digits that has not been removed
385 continue;
386 dig->SetDigitFast(old_dig->GetDigit(time,pad),time,pad);
0d319e67 387 ((AliSimDigits*)dig)->SetTrackIDFast(((AliSimDigits*)old_dig)->GetTrackID((Int_t)time,(Int_t)pad,0),time,pad,0);
388 ((AliSimDigits*)dig)->SetTrackIDFast(((AliSimDigits*)old_dig)->GetTrackID((Int_t)time,(Int_t)pad,1),time,pad,1);
389 ((AliSimDigits*)dig)->SetTrackIDFast(((AliSimDigits*)old_dig)->GetTrackID((Int_t)time,(Int_t)pad,2),time,pad,2);
390
391 }
392 UpdateRowPointer(rowPt);
393 arr->StoreRow(sector,row);
394 arr->ClearRow(sector,row);
395 old_array->ClearRow(sector,row);
396 }
397 digFile->cd();
398 char treeName[100];
03b6adf7 399 sprintf(treeName,"TreeD_%s_0",fParam->GetTitle());
8f1a9904 400 printf("Writing tree to file.....");
0d319e67 401 arr->GetTree()->Write(treeName,TObject::kOverwrite);
8f1a9904 402 printf("done\n");
0d319e67 403 digFile->Close();
03b6adf7 404 //arr->GetTree()->Delete();
405 //delete arr;
0d319e67 406}
407
108615fc 408///////////////////////////////////////// Point IO
409Bool_t AliL3FileHandler::AliPoints2Binary(){
410 Bool_t out = kTRUE;
411 UInt_t npoint;
412 AliL3SpacePointData *data = AliPoints2Memory(npoint);
413 out = Memory2Binary(npoint,data);
414 Free();
415 return out;
416}
417
418AliL3SpacePointData * AliL3FileHandler::AliPoints2Memory(UInt_t & npoint){
419 AliL3SpacePointData *data = 0;
420 npoint=0;
421 if(!fInAli){
422 LOG(AliL3Log::kWarning,"AliL3FileHandler::AliPoints2Memory","File")
423 <<"No Input avalible: no object TFile"<<ENDLOG;
424 return 0;
425 }
426 if(!fInAli->IsOpen()){
427 LOG(AliL3Log::kWarning,"AliL3FileHandler::AliPoints2Memory","File")
428 <<"No Input avalible: TFile not opend"<<ENDLOG;
429 return 0;
430 }
431 TDirectory *savedir = gDirectory;
432 fInAli->cd();
8f1a9904 433
434 Char_t cname[100];
435 Int_t eventn = 0;
436 sprintf(cname,"TreeC_TPC_%d",eventn);
108615fc 437 AliTPCClustersArray carray;
438 carray.Setup(fParam);
439 carray.SetClusterType("AliTPCcluster");
8f1a9904 440 Bool_t clusterok = carray.ConnectTree(cname);
108615fc 441 if(!clusterok) return 0;
442
443 AliTPCClustersRow ** clusterrow =
444 new AliTPCClustersRow*[ (int)carray.GetTree()->GetEntries()];
445 Int_t *rows = new int[ (int)carray.GetTree()->GetEntries()];
446 Int_t *sects = new int[ (int)carray.GetTree()->GetEntries()];
447 Int_t sum=0;
448
449 Int_t lslice,lrow;
450 for(Int_t i=0; i<carray.GetTree()->GetEntries(); i++){
451 AliSegmentID *s = carray.LoadEntry(i);
452 Int_t sector,row;
453 fParam->AdjustSectorRow(s->GetID(),sector,row);
454 rows[i] = row;
455 sects[i] = sector;
456 clusterrow[i] = 0;
457 fTransformer->Sector2Slice(lslice,lrow,sector,row);
458 if(fSlice != lslice || lrow<fRowMin || lrow>fRowMax) continue;
459 clusterrow[i] = carray.GetRow(sector,row);
460 if(clusterrow[i])
461 sum+=clusterrow[i]->GetArray()->GetEntriesFast();
462 }
463 UInt_t size = sum*sizeof(AliL3SpacePointData);
464
465 LOG(AliL3Log::kDebug,"AliL3FileHandler::AliPoints2Memory","File")
466 <<AliL3Log::kDec<<"Found "<<sum<<" SpacePoints"<<ENDLOG;
467
468 data = (AliL3SpacePointData *) Allocate(size);
469 npoint = sum;
470 UInt_t n=0;
471 for(Int_t i=0; i<carray.GetTree()->GetEntries(); i++){
472 if(!clusterrow[i]) continue;
473 Int_t row = rows[i];
474 Int_t sector = sects[i];
475 fTransformer->Sector2Slice(lslice,lrow,sector,row);
476 Int_t entries_in_row = clusterrow[i]->GetArray()->GetEntriesFast();
477 for(Int_t j = 0;j<entries_in_row;j++){
478 AliTPCcluster *c = (AliTPCcluster*)(*clusterrow[i])[j];
479 data[n].fZ = c->GetZ();
480 data[n].fY = c->GetY();
481 data[n].fX = fParam->GetPadRowRadii(sector,row);
482 data[n].fID = n+((fSlice&0x7f)<<25)+((fPatch&0x7)<<22);//uli
483 data[n].fPadRow = lrow;
484 data[n].fXYErr = c->GetSigmaY2();
485 data[n].fZErr = c->GetSigmaZ2();
486 if(fMC) fprintf(fMC,"%d %d\n",data[n].fID,c->GetLabel(0));
487 n++;
488 }
489 }
490 for(Int_t i=0;i<carray.GetTree()->GetEntries();i++){
491 Int_t row = rows[i];
492 Int_t sector = sects[i];
493 if(carray.GetRow(sector,row))
494 carray.ClearRow(sector,row);
495 }
496
497 delete [] clusterrow;
498 delete [] rows;
499 delete [] sects;
500 savedir->cd();
501
502 return data;
503}
504