]> git.uio.no Git - u/mrichter/AliRoot.git/blame - HLT/comp/AliL3DataCompressor.cxx
Checking in the seeds of new cluster fitting code.
[u/mrichter/AliRoot.git] / HLT / comp / AliL3DataCompressor.cxx
CommitLineData
5b3f37f6 1//$Id$
2
3// Author: Anders Vestbo <mailto:vestbo@fi.uib.no>
4//*-- Copyright &copy ASV
5
6#include "AliL3StandardIncludes.h"
7#include "AliL3DataCompressor.h"
8#include "AliL3FileHandler.h"
9#include "AliL3Transform.h"
10#include "AliL3SpacePointData.h"
11#include "AliL3Compress.h"
12#include "AliL3TrackArray.h"
13#include "AliL3ModelTrack.h"
14#include "AliL3Modeller.h"
bd53cfb7 15#include "AliL3Benchmark.h"
5b3f37f6 16
17#include <AliTPCParamSR.h>
18#include <AliTPCDigitsArray.h>
19#include <AliTPCClustersArray.h>
20#include <AliTPCcluster.h>
21#include <AliTPCClustersRow.h>
22#include <AliSimDigits.h>
23#include <AliTPC.h>
24#include <AliTPCv2.h>
25#include <AliRun.h>
26
27#include <TFile.h>
28#include <TMath.h>
29#include <TDirectory.h>
30#include <TSystem.h>
31//_____________________________________________________________
32//
33// AliL3DataCompression
34//
35// Interface class; binary <-> AliROOT handling of TPC data compression classes.
36//
37
38
39ClassImp(AliL3DataCompressor)
40
41AliL3DataCompressor::AliL3DataCompressor()
42{
43 fMemHandler=0;
44 fMinSlice=0;
45 fMaxSlice=0;
bd53cfb7 46 fBenchmark=0;
5b3f37f6 47}
48
49AliL3DataCompressor::AliL3DataCompressor(Char_t *path,Int_t minslice,Int_t maxslice)
50{
51 fMinSlice=minslice;
52 fMaxSlice=maxslice;
53 strcpy(fPath,path);
bd53cfb7 54 fMemHandler = new AliL3FileHandler();
55 fBenchmark = new AliL3Benchmark();
5b3f37f6 56}
57
58AliL3DataCompressor::~AliL3DataCompressor()
59{
60 if(fMemHandler)
61 delete fMemHandler;
bd53cfb7 62 if(fBenchmark)
63 delete fBenchmark;
64}
65
66void AliL3DataCompressor::DoBench(Char_t *fname)
67{
68 fBenchmark->Analyze(fname);
5b3f37f6 69}
70
71void AliL3DataCompressor::ProcessData(Char_t *trackpath,Int_t padoverlap,Int_t timeoverlap,Int_t padsearch,Int_t timesearch)
72{
73 //Find clusters based on the input tracks, and write them to file.
74
75 for(Int_t slice=fMinSlice; slice<=fMaxSlice; slice++)
76 {
77 for(Int_t patch=0; patch<AliL3Transform::GetNPatches(); patch++)
78 {
79 AliL3Modeller *modeller = new AliL3Modeller();
80
81 modeller->SetOverlap(padoverlap,timeoverlap);
82 modeller->SetSearchRange(padsearch,timesearch);
83 modeller->Init(slice,patch,trackpath,fPath,kTRUE,kTRUE);
bd53cfb7 84 fBenchmark->Start("Calclulate Crossing");
85 modeller->CalculateCrossingPoints();
86 fBenchmark->Stop("Calclulate Crossing");
87 fBenchmark->Start("Check for overlaps");
88 modeller->CheckForOverlaps();
89 fBenchmark->Stop("Check for overlaps");
90 fBenchmark->Start("Find clusters");
5b3f37f6 91 modeller->FindClusters();
bd53cfb7 92 fBenchmark->Stop("Find clusters");
5b3f37f6 93 modeller->WriteRemaining();
94
95 AliL3TrackArray *tracks = modeller->GetTracks();
96
97 AliL3Compress *comp = new AliL3Compress(slice,patch,fPath);
98
99 comp->WriteFile(tracks);
100
101 delete comp;
102 delete modeller;
103 }
104 }
105
106}
107
108void AliL3DataCompressor::CompressAndExpand(Int_t bitspad,Int_t bitstime,Int_t bitscharge,Int_t bitsshape)
109{
110 //Read tracks/clusters from file, compress data and uncompress it. Write compression rates to file.
111 //Input parameters are number of bits to use to code the pad/time/charge/shape residuals.
112
113 Char_t filename[1024];
114 sprintf(filename,"%s/comprates.txt",fPath);
115 FILE *file = fopen(filename,"w");
116 for(Int_t slice=fMinSlice; slice<=fMaxSlice; slice++)
117 {
118 for(Int_t patch=0; patch < AliL3Transform::GetNPatches(); patch++)
119 {
120 AliL3Compress *comp = new AliL3Compress(slice,patch,fPath);
121 comp->SetBitNumbers(bitspad,bitstime,bitscharge,bitsshape);
122
123 comp->CompressFile();
124 comp->ExpandFile();
125 comp->PrintCompRatio(file);
126
127 delete comp;
128 }
129 }
130 fclose(file);
131}
132
133void AliL3DataCompressor::WriteRemainingDigits()
134{
135 //Write the remaining digits resulting from ProcessData to a rootfile
136 //which serves as an input to the offline cluster finder.
137
138 Char_t filename[1024];
139 sprintf(filename,"rm %s/comp/remains.root",fPath);
140 gSystem->Exec(filename);
141 sprintf(filename,"%s/comp/remains.root",fPath);
142
143 cout<<"AliL3DataCompressor::WriteRemainingDigits : Removing old file : "<<filename<<endl;
144 for(Int_t slice=fMinSlice; slice<=fMaxSlice; slice++)
145 {
146 for(Int_t patch=0; patch<AliL3Transform::GetNPatches(); patch++)
147 {
148 AliL3Compress *comp = new AliL3Compress(slice,patch,fPath);
149 comp->WriteRootFile(filename);
150 delete comp;
151 }
152 }
153
154}
155
156void AliL3DataCompressor::FindOfflineClusters(Bool_t remains)
157{
158 //Code taken from macro AliTPCFindClusters.
159 //remains = kTRUE : Find remaining clusters after comression
160 //remains = kFALSE : Find offline clusters before compression.
161
162 Char_t fname[1024];
163 if(!remains)
164 sprintf(fname,"%s/AliTPCclusters.root",fPath);
165 else
166 sprintf(fname,"%s/comp/AliTPCclusters_remains.root",fPath);
167
168 TFile *out=TFile::Open(fname,"RECREATE");
169
170 sprintf(fname,"%s/alirunfile.root",fPath);
171 TFile *in = TFile::Open(fname);
172
173 if (!(gAlice=(AliRun*)in->Get("gAlice")))
174 {
175 cerr<<"AliL3DataCompressor::FindOfflineClusters : gAlice have not been found on file "<<fname<<endl;
176 return;
177 }
178
179 AliTPC *TPC = (AliTPC*)gAlice->GetDetector("TPC");
180 Int_t ver = TPC->IsVersion();
181 cerr<<"TPC version "<<ver<<" has been found !\n";
182
183 AliTPCParamSR *dig=(AliTPCParamSR *)in->Get("75x40_100x60");
184 if(dig){
185 cerr<<"2 pad-length geom hits with 3 pad-lengths geom digits\n";
186 delete dig;
187 dig = new AliTPCParamSR();
188 }
189 else
190 {
191 dig=(AliTPCParamSR *)gDirectory->Get("75x40_100x60_150x60");
192 }
193 if (!dig) {cerr<<"TPC parameters have not been found !\n"; return;}
194
195 if(!remains)
196 sprintf(fname,"%s/digitfile.root",fPath);
197 else
198 sprintf(fname,"%s/comp/remains.root",fPath);
199 TFile *dfile = TFile::Open(fname);
200
201 cerr<<"Looking for clusters...\n";
202 AliTPCv2 tpc;
203 tpc.SetParam(dig);
204 dfile->cd();
205
206 tpc.Digits2Clusters(out,0); //event 0
207
208
209 delete gAlice; gAlice=0;
210 out->Close();
211 in->Close();
212}
213
5b3f37f6 214void AliL3DataCompressor::RestoreData()
215{
216 //Restore the uncompressed data together with the remaining clusters,
217 //and write to a final cluster file which serves as an input to the
218 //final offline tracker.
219
220 Char_t filename[1024];
221
bd53cfb7 222 sprintf(filename,"%s/comp/AliTPCclusters_remains.root",fPath);
223 if(!fMemHandler->SetAliInput(filename))
224 cerr<<"AliL3DataCompressor::RestoreData : Problems opening "<<filename<<endl;
225
5b3f37f6 226 sprintf(filename,"%s/digitfile.root",fPath);
227 TFile *rootfile = TFile::Open(filename);
228 rootfile->cd();
229 AliTPCParam *param = (AliTPCParam*)rootfile->Get(AliL3Transform::GetParamName());
230 AliTPCDigitsArray *darray = new AliTPCDigitsArray();
231 darray->Setup(param);
232 darray->SetClass("AliSimDigits");
233 sprintf(filename,"TreeD_%s_0",AliL3Transform::GetParamName());
234 Bool_t ok = darray->ConnectTree(filename);
235 if(!ok)
236 {
237 cerr<<"AliL3DataCompressor::RestoreData : Problems connecting tree"<<endl;
238 return;
239 }
240
241 TDirectory *savedir = gDirectory;
242
243 //Create new file for storing the final clusters:
244 sprintf(filename,"%s/comp/AliTPCclusters.root",fPath);
245 TFile *clusterfile = TFile::Open(filename,"RECREATE");
246 clusterfile->cd();
247 param->Write(param->GetTitle());
248
249 AliTPCClustersArray carray;
250 carray.Setup(param);
251 carray.SetClusterType("AliTPCcluster");
252 carray.MakeTree();
bd53cfb7 253
5b3f37f6 254 //Now start the loop:
255 for(Int_t slice=fMinSlice; slice<=fMaxSlice; slice++)
256 {
257 for(Int_t patch=0; patch < AliL3Transform::GetNPatches(); patch++)
258 {
259 //Get the remaining clusters:
260 Int_t nclusters = FindRemaining(slice,patch);
261 UInt_t size=0;
262 AliL3SpacePointData *points = (AliL3SpacePointData*)fMemHandler->GetDataPointer(size);
263 Int_t counter=0;
264 cout<<"Found "<<nclusters<<" clusters in slice "<<slice<<" patch "<<patch<<endl;
265
266 //Get the uncompressed data:
5b3f37f6 267 AliL3Compress *comp = new AliL3Compress(slice,patch,fPath);
5b3f37f6 268 comp->ReadFile('u');
269
270 AliL3TrackArray *tracks = comp->GetTracks();
bd53cfb7 271 Short_t *used = new Short_t[tracks->GetNTracks()];
272
5b3f37f6 273 for(Int_t padrow=AliL3Transform::GetFirstRow(patch); padrow<=AliL3Transform::GetLastRow(patch); padrow++)
274 {
275 Int_t sec,row;
276 AliL3Transform::Slice2Sector(slice,padrow,sec,row);
277 AliTPCClustersRow *clrow=carray.CreateRow(sec,row);
278 AliSimDigits *digits = (AliSimDigits*)darray->LoadRow(sec,row);
279
280 Float_t pad,time,xywidth,zwidth;
281 Int_t charge;
bd53cfb7 282
283 //Get the remaining clusters:
284 memset(used,0,tracks->GetNTracks()*sizeof(Short_t));
285 while(counter < nclusters && points[counter].fPadRow == padrow)
286 {
287 Float_t temp[3] = {points[counter].fX,points[counter].fY,points[counter].fZ};
288 AliL3Transform::Local2Raw(temp,sec,row);
289 Int_t tpad,ttime;
290 tpad = TMath::Nint(temp[1]);
291 ttime = TMath::Nint(temp[2]);
292
293 //Get the track data, if the order is such:
294 for(Int_t i=0; i<tracks->GetNTracks(); i++)
295 {
296 if(used[i] == 1) continue;
297 AliL3ModelTrack *track = (AliL3ModelTrack*)tracks->GetCheckedTrack(i);
298 if(!track) continue;
299 if(!track->IsPresent(padrow)) continue;
300 track->GetPad(padrow,pad);
301 track->GetTime(padrow,time);
302 track->GetClusterCharge(padrow,charge);
303
304 Float_t xyz[3];
305 AliL3Transform::Raw2Local(xyz,sec,row,pad,time);
306
307 if(TMath::Nint(pad) > tpad) continue;
308 if(TMath::Nint(pad) == tpad &&
309 TMath::Nint(time) > ttime) continue;
310
311 used[i]=1;
312
313 track->GetXYWidth(padrow,xywidth);
314 track->GetZWidth(padrow,zwidth);
315
316 Int_t trpad,trtime;
317 trpad = TMath::Nint(pad);
318 trtime = TMath::Nint(time);
319 if(trpad < 0 || trpad >= AliL3Transform::GetNPads(padrow) ||
320 trtime < 0 || trtime >= AliL3Transform::GetNTimeBins())
321 {
322 cerr<<"AliL3DataCompressor::RestoreData : Wrong pad "<<trpad<<" or time "<<trtime<<endl;
323 track->Print();
324 return;
325 }
326
327 xywidth = (xywidth+1./12)*pow(AliL3Transform::GetPadPitchWidth(patch),2);
328 zwidth = (zwidth+1./12)*pow(AliL3Transform::GetZWidth(),2);
329
330 AliTPCcluster *c = new AliTPCcluster();
331 c->SetY(xyz[1]);
332 c->SetZ(xyz[2]);
333 c->SetSigmaY2(xywidth);
334 c->SetSigmaZ2(zwidth);
335 c->SetQ(charge);
336
337
338 c->SetLabel(digits->GetTrackID(trtime,trpad,0),0);
339 c->SetLabel(digits->GetTrackID(trtime,trpad,1),1);
340 c->SetLabel(digits->GetTrackID(trtime,trpad,2),2);
341
342 clrow->InsertCluster(c);
343 delete c;
344 }
345
346 AliTPCcluster *c = new AliTPCcluster();
347 c->SetY(points[counter].fY);
348 c->SetZ(points[counter].fZ);
349 c->SetQ(points[counter].fCharge);
350
351 xywidth = points[counter].fXYErr * points[counter].fXYErr;
352 zwidth = points[counter].fZErr * points[counter].fZErr;
353 c->SetSigmaY2(xywidth);
354 c->SetSigmaZ2(zwidth);
355
356 c->SetLabel(digits->GetTrackID(ttime,tpad,0),0);
357 c->SetLabel(digits->GetTrackID(ttime,tpad,1),1);
358 c->SetLabel(digits->GetTrackID(ttime,tpad,2),2);
359
360 clrow->InsertCluster(c);
361 delete c;
362 counter++;
363 }
364
365 //Fill the remaining tracks:
5b3f37f6 366 for(Int_t i=0; i<tracks->GetNTracks(); i++)
367 {
bd53cfb7 368 if(used[i] == 1) continue;
5b3f37f6 369 AliL3ModelTrack *track = (AliL3ModelTrack*)tracks->GetCheckedTrack(i);
370 if(!track) continue;
371 if(!track->IsPresent(padrow)) continue;
372 track->GetPad(padrow,pad);
373 track->GetTime(padrow,time);
374 track->GetClusterCharge(padrow,charge);
bd53cfb7 375
5b3f37f6 376 Float_t xyz[3];
377 AliL3Transform::Raw2Local(xyz,sec,row,pad,time);
378
bd53cfb7 379 used[i]=1;
5b3f37f6 380
5b3f37f6 381 track->GetXYWidth(padrow,xywidth);
382 track->GetZWidth(padrow,zwidth);
bd53cfb7 383
384 Int_t trpad,trtime;
385 trpad = TMath::Nint(pad);
386 trtime = TMath::Nint(time);
387 if(trpad < 0 || trpad >= AliL3Transform::GetNPads(padrow) ||
388 trtime < 0 || trtime >= AliL3Transform::GetNTimeBins())
5b3f37f6 389 {
bd53cfb7 390 cerr<<"AliL3DataCompressor::RestoreData : Wrong pad "<<trpad<<" or time "<<trtime<<endl;
5b3f37f6 391 track->Print();
392 return;
393 }
394
395 xywidth = (xywidth+1./12)*pow(AliL3Transform::GetPadPitchWidth(patch),2);
396 zwidth = (zwidth+1./12)*pow(AliL3Transform::GetZWidth(),2);
397
398 AliTPCcluster *c = new AliTPCcluster();
399 c->SetY(xyz[1]);
400 c->SetZ(xyz[2]);
401 c->SetSigmaY2(xywidth);
402 c->SetSigmaZ2(zwidth);
403 c->SetQ(charge);
404
bd53cfb7 405 c->SetLabel(digits->GetTrackID(trtime,trpad,0),0);
406 c->SetLabel(digits->GetTrackID(trtime,trpad,1),1);
407 c->SetLabel(digits->GetTrackID(trtime,trpad,2),2);
5b3f37f6 408
5b3f37f6 409 clrow->InsertCluster(c);
410 delete c;
411 }
bd53cfb7 412
5b3f37f6 413 carray.StoreRow(sec,row);
414 carray.ClearRow(sec,row);
415 darray->ClearRow(sec,row);
bd53cfb7 416 for(Int_t tr=0; tr<tracks->GetNTracks(); tr++)
417 {
418 AliL3ModelTrack *track = (AliL3ModelTrack*)tracks->GetCheckedTrack(tr);
419 if(!track) continue;
420 if(track->IsPresent(padrow) && used[tr]==0)
421 cerr<<"AliL3DataCompressor::RestoreData : Track "<<tr<<" in sector "<<sec<<" row "<<row<<" was not used"<<endl;
422 }
5b3f37f6 423 }
bd53cfb7 424 delete [] used;
5b3f37f6 425 delete comp;
426 fMemHandler->Free();
427 if(nclusters != counter)
428 cerr<<"AliL3DataCompressor::RestoreData : Mismatching counters : "<<nclusters<<" "<<counter<<endl;
429 }
430 }
431
432 //Write the tree to file:
433 sprintf(filename,"TreeC_TPC_%d",0);
434 carray.GetTree()->SetName(filename);
435 carray.GetTree()->Write();
436 savedir->cd();
437 delete darray;
438 rootfile->Close();
439 // clusterfile->Close();
440
441}
442
5b3f37f6 443Int_t AliL3DataCompressor::FindRemaining(Int_t slice,Int_t patch)
444{
5b3f37f6 445 fMemHandler->Init(slice,patch);
bd53cfb7 446 UInt_t npoints=0;
447 if(!fMemHandler->AliPoints2Memory(npoints))
448 cerr<<"AliL3DataCompressor::FindRemaining : Problems loading clusters "<<endl;
5b3f37f6 449
450 return (Int_t)npoints;
451}
bd53cfb7 452