3 // Author: Anders Vestbo <mailto:vestbo@fi.uib.no>
4 //*-- Copyright © ASV
10 #include "AliL3Modeller.h"
11 #include "AliL3MemHandler.h"
12 #include "AliL3TrackArray.h"
13 #include "AliL3ModelTrack.h"
14 #include "AliL3DigitData.h"
15 #include "AliL3Transform.h"
17 //_____________________________________________________________
20 // Class for modeling TPC data.
22 // This performs the cluster finding, based on track parameters.
23 // Basically it propagates the tracks to all padrows, and looks
24 // for a corresponding cluster. For the moment only cog is calculated,
25 // and no deconvolution is done.
27 ClassImp(AliL3Modeller)
29 AliL3Modeller::AliL3Modeller()
40 AliL3Modeller::~AliL3Modeller()
48 void AliL3Modeller::Init(Int_t slice,Int_t patch,Char_t *trackdata,Char_t *path,Bool_t houghtracks)
55 sprintf(fPath,"%s",path);
57 AliL3Transform::Init(fPath);
59 fTracks = new AliL3TrackArray("AliL3ModelTrack");
62 AliL3MemHandler *file = new AliL3MemHandler();
63 sprintf(fname,"%s/tracks_tr_%d_0.raw",trackdata,fSlice); //output tracks from the tracker (no merging)
64 if(!file->SetBinaryInput(fname))
66 cerr<<"AliL3Modeller::Init : Error opening trackfile: "<<fname<<endl;
69 file->Binary2TrackArray(fTracks);
70 file->CloseBinaryInput();
74 cout<<"AliL3Modeller is assuming local hough tracksegments!"<<endl;
76 cout<<"AliL3Modeller is assuming global tracks!"<<endl;
81 for(Int_t i=0; i<fTracks->GetNTracks(); i++)
83 AliL3ModelTrack *track = (AliL3ModelTrack*)fTracks->GetCheckedTrack(i);
85 track->Init(fSlice,fPatch);
87 //Only if the tracks has been merged across sector boundaries:
89 //track->Rotate(fSlice,kTRUE); //!!!!!!!!!!!!!!!!!!!
91 track->CalculateHelix();
94 CalculateCrossingPoints();
99 fMemHandler = new AliL3MemHandler();
100 sprintf(fname,"%sdigits_%d_%d.raw",fPath,fSlice,fPatch);
101 if(!fMemHandler->SetBinaryInput(fname))
103 cerr<<"AliL3Modeller::Init : Error opening file "<<fname<<endl;
107 AliL3DigitRowData *digits=(AliL3DigitRowData*)fMemHandler->CompBinary2Memory(ndigits);
109 SetInputData(digits);
112 void AliL3Modeller::FindClusters()
117 cerr<<"AliL3Modeller::Process : No tracks"<<endl;
122 cerr<<"AliL3Modeller::Process : No data "<<endl;
126 AliL3DigitRowData *rowPt = fRowData;
127 AliL3DigitData *digPt=0;
129 Int_t ntimes = AliL3Transform::GetNTimeBins()+1;
130 Int_t npads = AliL3Transform::GetNPads(AliL3Transform::GetLastRow(fPatch))+1;//Max num of pads.
131 Int_t bounds = ntimes*npads;
132 Digit *row = new Digit[bounds];
135 Int_t pad,time,index;
139 for(Int_t i=AliL3Transform::GetFirstRow(fPatch); i<=AliL3Transform::GetLastRow(fPatch); i++)
141 memset((void*)row,0,ntimes*npads*sizeof(Digit));
142 digPt = (AliL3DigitData*)rowPt->fDigitData;
143 for(UInt_t j=0; j<rowPt->fNDigit; j++)
146 time = digPt[j].fTime;
147 charge = digPt[j].fCharge;
148 row[ntimes*pad+time].fCharge = charge;
149 row[ntimes*pad+time].fUsed = kFALSE;
150 //cout<<"Row "<<i<<" pad "<<pad<<" time "<<time<<" charge "<<charge<<endl;
153 for(Int_t k=0; k<fTracks->GetNTracks(); k++)
155 AliL3ModelTrack *track = (AliL3ModelTrack*)fTracks->GetCheckedTrack(k);
157 //if(track->GetOverlap(i)>=0) continue;//Track is overlapping
159 if(track->GetPadHit(i)<0 || track->GetTimeHit(i)<0 || track->GetOverlap(i)>=0)
161 track->SetCluster(i,0,0,0,0,0,0); //The track has left the patch.
165 Int_t hitpad = (Int_t)rint(track->GetPadHit(i));
166 Int_t hittime = (Int_t)rint(track->GetTimeHit(i));
167 //cout<<"Checking track on row "<<i<<" with pad "<<hitpad<<" time "<<hittime<<endl;
173 memset(&cluster,0,sizeof(Cluster));
176 while(1)//Process this padrow
178 if(pad < 0 || pad >= AliL3Transform::GetNPads(i))
180 //cout<<"Pad = "<<pad<<" on row "<<i<<endl;
181 FillCluster(track,&cluster,i,npads);
188 while(1) //Process sequence on this pad:
191 index = ntimes*pad + time;
192 if(index < 0 || index >= bounds)
194 cerr<<"AliL3Modeller::FindClusters : Index out of range : "<<index
195 <<" on row "<<i<<" pad "<<pad<<" time "<<time<<endl;
200 if(row[index].fUsed == kTRUE)//Only use the digits once....
203 charge = row[index].fCharge;
206 charge = row[index].fCharge;
207 if(charge==0 && timesign==-1)
209 if(seq_charge==0 && abs(time-hittime) <= fTimeOverlap/2)
221 else if(charge==0 && timesign==1)
223 if(seq_charge==0 && abs(time-hittime) <= fTimeOverlap/2)
230 //cerr<<"Breaking off at pad "<<pad<<" and time "<<time<<endl;
235 //cout<<"Doing pad "<<pad<<" time "<<time<<" charge "<<charge<<endl;
237 seq_charge += charge;
239 cluster.fTime += time*charge;
240 cluster.fPad += pad*charge;
241 cluster.fCharge += charge;
242 cluster.fSigmaY2 += pad*pad*charge;
243 cluster.fSigmaZ2 += time*time*charge;
245 row[ntimes*pad+time].fUsed = kTRUE;
250 if(seq_charge)//There was something on this pad.
255 else //Nothing more on this pad, goto next pad
259 if(cluster.fCharge==0 && abs(pad-hitpad) <= fPadOverlap/2 && pad > 0)
261 pad--; //In this case, we haven't found anything yet,
262 } //so we will try to expand our search within the natural boundaries.
273 if(cluster.fCharge==0 && abs(pad-hitpad) <= fPadOverlap/2 && pad < AliL3Transform::GetNPads(i)-2)
275 pad++; //In this case, we haven't found anything yet,
276 continue; //so we will try to expand our search within the natural boundaries.
278 else //We are out of range, or cluster if finished.
280 //cout<<"Outof range; charge "<<cluster.fCharge<<" paddiff "<<abs(pad-hitpad)<<endl;
281 FillCluster(track,&cluster,i,npads);
285 else //Nothing more in this cluster
287 //cout<<"Filling final cluster"<<endl;
288 FillCluster(track,&cluster,i,npads);
293 //cout<<"done"<<endl;
296 FillZeros(rowPt,row);
297 fMemHandler->UpdateRowPointer(rowPt);
300 cout<<"done processing"<<endl;
304 for(Int_t i=0; i<fTracks->GetNTracks(); i++)
306 AliL3ModelTrack *track = (AliL3ModelTrack*)fTracks->GetCheckedTrack(i);
308 if(track->GetNClusters() != AliL3Transform::GetNRows(fPatch))
309 cerr<<endl<<"Mismatching hitcounts; nclusters: "<<track->GetNClusters()<<" nrows "<<AliL3Transform::GetNRows(fPatch)<<endl<<endl;
314 void AliL3Modeller::FillCluster(AliL3ModelTrack *track,Cluster *cluster,Int_t row,Int_t npads)
316 if(cluster->fCharge==0)
318 track->SetCluster(row,0,0,0,0,0,0);
321 Float_t fcharge = (Float_t)cluster->fCharge;
322 Float_t fpad = ((Float_t)cluster->fPad/fcharge);
323 Float_t ftime = ((Float_t)cluster->fTime/fcharge);
324 Float_t sigmaY2,sigmaZ2;
325 CalcClusterWidth(cluster,sigmaY2,sigmaZ2);
326 track->SetCluster(row,fpad,ftime,fcharge,sigmaY2,sigmaZ2,npads);
330 void AliL3Modeller::FillZeros(AliL3DigitRowData *rowPt,Digit *row)
332 //Fill zero where data has been used.
334 Int_t ntimes = AliL3Transform::GetNTimeBins()+1;
335 AliL3DigitData *digPt = (AliL3DigitData*)rowPt->fDigitData;
336 for(UInt_t j=0; j<rowPt->fNDigit; j++)
338 Int_t pad = digPt[j].fPad;
339 Int_t time = digPt[j].fTime;
340 if(row[ntimes*pad+time].fUsed==kTRUE)
341 digPt[j].fCharge = 0;
345 void AliL3Modeller::WriteRemaining()
347 //Write remaining (nonzero) digits to file.
349 AliL3DigitRowData *rowPt;
350 rowPt = (AliL3DigitRowData*)fRowData;
352 Int_t ndigits[(AliL3Transform::GetNRows(fPatch))];
353 for(Int_t i=AliL3Transform::GetFirstRow(fPatch); i<=AliL3Transform::GetLastRow(fPatch); i++)
355 AliL3DigitData *digPt = (AliL3DigitData*)rowPt->fDigitData;
356 ndigits[(i-AliL3Transform::GetFirstRow(fPatch))]=0;
357 for(UInt_t j=0; j<rowPt->fNDigit; j++)
359 if(digPt[j].fCharge==0) continue;
361 ndigits[(i-AliL3Transform::GetFirstRow(fPatch))]++;
363 //cout<<"Difference "<<(int)ndigits[(i-AliL3Transform::GetFirstRow(fPatch))]<<" "<<(int)rowPt->fNDigit<<endl;
364 fMemHandler->UpdateRowPointer(rowPt);
367 Int_t size = digitcount*sizeof(AliL3DigitData) + AliL3Transform::GetNRows(fPatch)*sizeof(AliL3DigitRowData);
368 Byte_t *data = new Byte_t[size];
370 AliL3DigitRowData *tempPt = (AliL3DigitRowData*)data;
371 rowPt = (AliL3DigitRowData*)fRowData;
373 for(Int_t i=AliL3Transform::GetFirstRow(fPatch); i<=AliL3Transform::GetLastRow(fPatch); i++)
377 tempPt->fNDigit = ndigits[(i-AliL3Transform::GetFirstRow(fPatch))];
378 AliL3DigitData *digPt = (AliL3DigitData*)rowPt->fDigitData;
379 for(UInt_t j=0; j<rowPt->fNDigit; j++)
381 if(digPt[j].fCharge==0) continue;
382 if(localcount >= ndigits[(i-AliL3Transform::GetFirstRow(fPatch))])
384 cerr<<"AliL3Modeller::WriteRemaining : Digitarray out of range!!"<<endl;
387 tempPt->fDigitData[localcount].fCharge = digPt[j].fCharge;
388 tempPt->fDigitData[localcount].fPad = digPt[j].fPad;
389 tempPt->fDigitData[localcount].fTime = digPt[j].fTime;
393 if(ndigits[(i-AliL3Transform::GetFirstRow(fPatch))] != localcount)
395 cerr<<"AliL3Modeller::WriteRemaining : Mismatch in digitcount"<<endl;
398 fMemHandler->UpdateRowPointer(rowPt);
399 Byte_t *tmp = (Byte_t*)tempPt;
400 Int_t size = sizeof(AliL3DigitRowData) + ndigits[(i-AliL3Transform::GetFirstRow(fPatch))]*sizeof(AliL3DigitData);
402 tempPt = (AliL3DigitRowData*)tmp;
406 AliL3MemHandler *mem = new AliL3MemHandler();
407 sprintf(fname,"%s/remains_%d_%d.raw",fPath,fSlice,fPatch);
408 mem->SetBinaryOutput(fname);
409 mem->Memory2CompBinary((UInt_t)AliL3Transform::GetNRows(fPatch),(AliL3DigitRowData*)data);
410 mem->CloseBinaryOutput();
416 void AliL3Modeller::CalculateCrossingPoints()
418 cout<<"Calculating crossing points on "<<fTracks->GetNTracks()<<" tracks"<<endl;
421 cerr<<"AliL3Modeller::CalculateCrossingPoints(): No tracks"<<endl;
427 //Remove tracks which are no good:
428 for(Int_t i=0; i<fTracks->GetNTracks(); i++)
430 AliL3ModelTrack *track = (AliL3ModelTrack*)fTracks->GetCheckedTrack(i);
432 if(track->GetFirstPointX() > AliL3Transform::Row2X(AliL3Transform::GetLastRow(fPatch)) || track->GetPt()<0.1)
434 if(track->GetNHits() < fTrackThreshold)
442 for(Int_t i=AliL3Transform::GetLastRow(fPatch); i>=AliL3Transform::GetFirstRow(fPatch); i--)
444 for(Int_t j=0; j<fTracks->GetNTracks(); j++)
446 AliL3ModelTrack *track = (AliL3ModelTrack*)fTracks->GetCheckedTrack(j);
449 if(!track->GetCrossingPoint(i,hit))
451 cerr<<"AliL3Modeller::CalculateCrossingPoints : Track "<<j<<" does not intersect row "<<i<<" :"<<endl<<
452 "First point "<<track->GetFirstPointX()<<
453 " nhits "<<track->GetNHits()<<endl;//" tgl "<<track->GetTgl()<<" psi "<<track->GetPsi()<<" charge "<<track->GetCharge()<<endl;
454 //"Center "<<track->GetCenterX()<<" "<<track->GetCenterY()<<endl<<endl<<
459 //cout<<" x "<<track->GetPointX()<<" y "<<track->GetPointY()<<" z "<<track->GetPointZ()<<endl;
461 AliL3Transform::Slice2Sector(fSlice,i,sector,row);
462 AliL3Transform::Local2Raw(hit,sector,row);
463 if(hit[1]<0 || hit[1]>AliL3Transform::GetNPads(i) ||
464 hit[2]<0 || hit[2]>AliL3Transform::GetNTimeBins())
465 {//Track is leaving the patch, so flag the track hits (<0)
466 track->SetPadHit(i,-1);
467 track->SetTimeHit(i,-1);
472 track->SetPadHit(i,hit[1]);
473 track->SetTimeHit(i,hit[2]);
475 //if(hit[1]<0 || hit[2]>445)
476 //if(hit[2]<0 || hit[2]>445)
477 //cout<<"pad "<<hit[1]<<" time "<<hit[2]<<" pt "<<track->GetPt()<<" psi "<<track->GetPsi()<<" tgl "<<track->GetTgl()<<" firstpoint "<<track->GetFirstPointX()<<" "<<track->GetFirstPointY()<<" "<<track->GetFirstPointZ()<<endl;
478 //cout<<"Crossing pad "<<hit[1]<<" time "<<hit[2]<<endl;
482 cout<<"And there are "<<fTracks->GetNTracks()<<" tracks remaining"<<endl;
485 void AliL3Modeller::CheckForOverlaps()
487 //Flag the tracks that overlap
489 cout<<"Checking for overlaps...";
491 for(Int_t i=0; i<fTracks->GetNTracks(); i++)
493 AliL3ModelTrack *track1 = (AliL3ModelTrack*)fTracks->GetCheckedTrack(i);
494 if(!track1) continue;
495 for(Int_t j=i+1; j<fTracks->GetNTracks(); j++)
497 AliL3ModelTrack *track2 = (AliL3ModelTrack*)fTracks->GetCheckedTrack(j);
498 if(!track2) continue;
499 for(Int_t k=AliL3Transform::GetFirstRow(fPatch); k<=AliL3Transform::GetLastRow(fPatch); k++)
501 if(track1->GetPadHit(k)<0 || track1->GetTimeHit(k)<0 ||
502 track2->GetPadHit(k)<0 || track2->GetTimeHit(k)<0)
504 if(fabs(track1->GetPadHit(k)-track2->GetPadHit(k)) <= fPadOverlap &&
505 fabs(track1->GetTimeHit(k)-track2->GetTimeHit(k)) <= fTimeOverlap)
507 track2->SetOverlap(k,i);
508 track1->SetOverlap(k,j);
517 void AliL3Modeller::CalcClusterWidth(Cluster *cl,Float_t &sigmaY2,Float_t &sigmaZ2)
522 padw = AliL3Transform::GetPadPitchWidth(fPatch);
524 Float_t charge = (Float_t)cl->fCharge;
525 Float_t pad = (Float_t)cl->fPad/charge;
526 Float_t time = (Float_t)cl->fTime/charge;
527 Float_t s2 = (Float_t)cl->fSigmaY2/charge - pad*pad;
529 //Save the sigmas in pad and time:
531 sigmaY2 = (s2);// + 1./12);//*padw*padw;
533 /*Constants added by offline
536 sigmaY2 = sigmaY2*0.108;
538 sigmaY2 = sigmaY2*2.07;
542 s2 = (Float_t)cl->fSigmaZ2/charge - time*time;
543 timew = AliL3Transform::GetZWidth();
544 sigmaZ2 = (s2);// +1./12);//*timew*timew;
548 /*Constants added by offline
551 sigmaZ2 = sigmaZ2*0.169;
553 sigmaZ2 = sigmaZ2*1.77;