Record changes.
[u/mrichter/AliRoot.git] / HLT / TPCLib / AliHLTTPCClusterFinder.cxx
CommitLineData
a38a7850 1// @(#) $Id$
c2f37813 2// Original: AliL3ClustFinderNew.cxx,v 1.29 2005/06/14 10:55:21 cvetan Exp
a38a7850 3
4// Author: Anders Vestbo <mailto:vestbo@fi.uib.no>,
5// Constantin Loizides <mailto:loizides@ikf.uni-frankfurt.de>
6// Jochen Thaeder <mailto:thaeder@kip.uni-heidelberg.de>
7
8//*-- Copyright &copy ALICE HLT Group
9
10
11#include "AliHLTTPCDigitReader.h"
a38a7850 12#include "AliHLTTPCRootTypes.h"
13#include "AliHLTTPCLogging.h"
14#include "AliHLTTPCClusterFinder.h"
15#include "AliHLTTPCDigitData.h"
16#include "AliHLTTPCTransform.h"
17#include "AliHLTTPCSpacePointData.h"
18#include "AliHLTTPCMemHandler.h"
19
20#if __GNUC__ >= 3
21using namespace std;
22#endif
23
24/** \class AliHLTTPCClusterFinder
25<pre>
26//_____________________________________________________________
27// AliHLTTPCClusterFinder
28//
29// The current cluster finder for HLT
30// (Based on STAR L3)
31//
32// The cluster finder is initialized with the Init function,
33// providing the slice and patch information to work on.
34//
35// The input is a provided by the AliHLTTPCDigitReader class,
36// using the init() funktion, and the next() funktion in order
37// to get the next bin. Either packed or unpacked data can be
38// processed, dependent if one uses AliHLTTPCDigitReaderPacked
39// class or AliHLTTPCDigitReaderUnpacked class in the
40// Clusterfinder Component.
41// The resulting space points will be in the
42// array given by the SetOutputArray function.
43//
44// There are several setters which control the behaviour:
45//
46// - SetXYError(Float_t): set fixed error in XY direction
47// - SetZError(Float_t): set fixed error in Z direction
48// (used if errors are not calculated)
49// - SetDeconv(Bool_t): switch on/off deconvolution
50// - SetThreshold(UInt_t): set charge threshold for cluster
51// - SetMatchWidth(UInt_t): set the match distance in
52// time for sequences to be merged
53// - SetSTDOutput(Bool_t): switch on/off output about found clusters
54// - SetCalcErr(Bool_t): switch on/off calculation of
55// space point errors (or widths in raw system)
56// - SetRawSP(Bool_t): switch on/off convertion to raw system
57//
58//
59// Example Usage:
60//
61// AliHLTTPCFileHandler *file = new AliHLTTPCFileHandler();
62// file->SetAliInput(digitfile); //give some input file
63// for(int slice=0; slice<=35; slice++){
64// for(int patch=0; pat<6; pat++){
65// file->Init(slice,patch);
66// UInt_t ndigits=0;
67// UInt_t maxclusters=100000;
68// UInt_t pointsize = maxclusters*sizeof(AliHLTTPCSpacePointData);
69// AliHLTTPCSpacePointData *points = (AliHLTTPCSpacePointData*)memory->Allocate(pointsize);
70// AliHLTTPCDigitRowData *digits = (AliHLTTPCDigitRowData*)file->AliAltroDigits2Memory(ndigits,event);
71// AliHLTTPCClusterFinder *cf = new AliHLTTPCClusterFinder();
72// cf->SetMatchWidth(2);
73// cf->InitSlice( slice, patch, row[0], row[1], maxPoints );
74// cf->SetSTDOutput(kTRUE); //Some output to standard IO
75// cf->SetRawSP(kFALSE); //Convert space points to local system
76// cf->SetThreshold(5); //Threshold of cluster charge
77// cf->SetDeconv(kTRUE); //Deconv in pad and time direction
78// cf->SetCalcErr(kTRUE); //Calculate the errors of the spacepoints
79// cf->SetOutputArray(points); //Move the spacepoints to the array
80// cf->Read(iter->fPtr, iter->fSize ); //give the data to the cf
81// cf->ProcessDigits(); //process the rows given by init
82// Int_t npoints = cf->GetNumberOfClusters();
83// AliHLTTPCMemHandler *out= new AliHLTTPCMemHandler();
84// out->SetBinaryOutput(fname);
85// out->Memory2Binary(npoints,points); //store the spacepoints
86// out->CloseBinaryOutput();
87// delete out;
88// file->free();
89// delete cf;
90// }
91// }
92</pre>
93*/
94
95ClassImp(AliHLTTPCClusterFinder)
96
97AliHLTTPCClusterFinder::AliHLTTPCClusterFinder()
98{
99 //constructor
100 fMatch = 1;
101 fThreshold = 10;
102 fXYErr = 0.2;
103 fZErr = 0.3;
104 fDeconvPad = kTRUE;
105 fDeconvTime = kTRUE;
106 fStdout = kFALSE;
107 fCalcerr = kTRUE;
108 fRawSP = kFALSE;
109 fFirstRow=0;
110 fLastRow=0;
111 fDigitReader = 0;
112
113}
114
115AliHLTTPCClusterFinder::~AliHLTTPCClusterFinder()
116{
117 //destructor
118}
119
120void AliHLTTPCClusterFinder::InitSlice(Int_t slice,Int_t patch,Int_t firstrow, Int_t lastrow,Int_t nmaxpoints)
121{
122 //init slice
123 fNClusters = 0;
124 fMaxNClusters = nmaxpoints;
125 fCurrentSlice = slice;
126 fCurrentPatch = patch;
127 fFirstRow = firstrow;
128 fLastRow = lastrow;
129}
130
131void AliHLTTPCClusterFinder::InitSlice(Int_t slice,Int_t patch,Int_t nmaxpoints)
132{
133 //init slice
134 fNClusters = 0;
135 fMaxNClusters = nmaxpoints;
136 fCurrentSlice = slice;
137 fCurrentPatch = patch;
138 fFirstRow=AliHLTTPCTransform::GetFirstRow(patch);
139 fLastRow=AliHLTTPCTransform::GetLastRow(patch);
140}
141
142void AliHLTTPCClusterFinder::SetOutputArray(AliHLTTPCSpacePointData *pt)
143{
144 //set pointer to output
145 fSpacePointData = pt;
146}
147
148void AliHLTTPCClusterFinder::Read(void* ptr,unsigned long size){
149 //set input pointer
150 fPtr = (UChar_t*)ptr;
151 fSize = size;
152}
153
154void AliHLTTPCClusterFinder::ProcessDigits()
155{
156 bool readValue = true;
157 Int_t newRow = 0;
158 Int_t rowOffset = 0;
159 UChar_t pad;
160 UShort_t time,newTime=0;
161 UInt_t charge,newPad=0;
162
163 fNClusters = 0;
164
165 // initialize block for reading packed data
166 fDigitReader->InitBlock(fPtr,fSize,fFirstRow,fLastRow);
167 readValue = fDigitReader->Next();
168
169 if (!readValue)return;
170
171 pad = fDigitReader->GetPad();
172 time = fDigitReader->GetTime();
173 fCurrentRow = fDigitReader->GetRow();
174
175 if ( fCurrentPatch >= 2 ) // Outer sector, patches 2, 3, 4, 5
176 rowOffset = AliHLTTPCTransform::GetFirstRow( 2 );
177
178 fCurrentRow += rowOffset;
179
180 UInt_t lastpad = 123456789;
181 AliClusterData *pad1[5000]; //2 lists for internal memory=2pads
182 AliClusterData *pad2[5000]; //2 lists for internal memory=2pads
183 AliClusterData clusterlist[10000]; //Clusterlist
184
185 AliClusterData **currentPt; //List of pointers to the current pad
186 AliClusterData **previousPt; //List of pointers to the previous pad
187 currentPt = pad2;
188 previousPt = pad1;
189 UInt_t nprevious=0,ncurrent=0,ntotal=0;
190
191 while ( readValue ){ // Reads through all digits in block
192
193 if(pad != lastpad){
194 //This is a new pad
195
196 //Switch the lists:
197 if(currentPt == pad2){
198 currentPt = pad1;
199 previousPt = pad2;
200 }
201 else {
202 currentPt = pad2;
203 previousPt = pad1;
204 }
205 nprevious = ncurrent;
206 ncurrent = 0;
207 if(pad != lastpad+1){
208 //this happens if there is a pad with no signal.
209 nprevious = ncurrent = 0;
210 }
211 lastpad = pad;
212 }
213
214 Bool_t newcluster = kTRUE;
215 UInt_t seqcharge=0,seqaverage=0,seqerror=0;
216 UInt_t lastcharge=0,lastwas_falling=0;
217 Int_t newbin=-1;
218
219
220 if(fDeconvTime){
221 redo: //This is a goto.
222
223 if(newbin > -1){
224 //bin = newbin;
225 newbin = -1;
226 }
227
228 lastcharge=0;
229 lastwas_falling = 0;
230 }
231
232
233 // LOOP OVER CURRENR SEQUENCE
234 while(1){ //Loop over current
235 charge = fDigitReader->GetSignal();
236
738c049f 237
238 // CHARGE DEBUG
239 if (fDigitReader->GetRow() == 90){
240///// LOG(AliHLTTPCLog::kFatal,"AliHLTTPCClusterFinder::Row","row90") << "PAD=" << fDigitReader->GetPad() << " TIME=" << fDigitReader->GetTime()
241 // << " SIGNAL=" << fDigitReader->GetSignal() << ENDLOG;
242
243 }
244
245
a38a7850 246 if(time >= AliHLTTPCTransform::GetNTimeBins()){
247 LOG(AliHLTTPCLog::kFatal,"AliHLTTPCClusterFinder::ProcessRow","Digits")
248 <<"Timebin out of range "<<(Int_t)time<<ENDLOG;
249 break;
250 }
251
252 //Get the current ADC-value
253 if(fDeconvTime){
254
255 //Check if the last pixel in the sequence is smaller than this
256 if(charge > lastcharge){
257 if(lastwas_falling){
258 newbin = 1;
259 break;
260 }
261 }
262 else lastwas_falling = 1; //last pixel was larger than this
263 lastcharge = charge;
264 }
265
266 //Sum the total charge of this sequence
267 seqcharge += charge;
268 seqaverage += time*charge;
269 seqerror += time*time*charge;
270
271 readValue = fDigitReader->Next();
272
273 //Check where to stop:
274 if(!readValue) break; //No more value
275
276 newPad = fDigitReader->GetPad();
277 newTime = fDigitReader->GetTime();
278 newRow = fDigitReader->GetRow() + rowOffset;
279
280 if(newPad != pad)break; //new pad
281 if(newTime != time+1) break; //end of sequence
282
283 // pad = newpad; is equal
284 time = newTime;
285
286 }//end loop over sequence
287
288
289 //Calculate mean of sequence:
290 Int_t seqmean=0;
291 if(seqcharge)
292 seqmean = seqaverage/seqcharge;
293 else{
294 LOG(AliHLTTPCLog::kFatal,"AliHLTTPCClusterFinder::ProcessRow","Data")
295 <<"Error in data given to the cluster finder"<<ENDLOG;
296 seqmean = 1;
297 seqcharge = 1;
298 }
299
300 //Calculate mean in pad direction:
301 Int_t padmean = seqcharge*pad;
302 Int_t paderror = pad*padmean;
303
304
305 //Compare with results on previous pad:
306 for(UInt_t p=0; p<nprevious; p++){
307
308 //dont merge sequences on the same pad twice
309 if(previousPt[p]->fLastMergedPad==pad) continue;
310
311 Int_t difference = seqmean - previousPt[p]->fMean;
312 if(difference < -fMatch) break;
313
314 if(difference <= fMatch){ //There is a match here!!
315 AliClusterData *local = previousPt[p];
316
317 if(fDeconvPad){
318 if(seqcharge > local->fLastCharge){
319 if(local->fChargeFalling){ //The previous pad was falling
320 break; //create a new cluster
321 }
322 }
323 else local->fChargeFalling = 1;
324 local->fLastCharge = seqcharge;
325 }
326
327 //Don't create a new cluster, because we found a match
328 newcluster = kFALSE;
329
330 //Update cluster on current pad with the matching one:
331 local->fTotalCharge += seqcharge;
332 local->fPad += padmean;
333 local->fPad2 += paderror;
334 local->fTime += seqaverage;
335 local->fTime2 += seqerror;
336 local->fMean = seqmean;
337 local->fFlags++; //means we have more than one pad
338 local->fLastMergedPad = pad;
339
340 currentPt[ncurrent] = local;
341 ncurrent++;
342
343 break;
344 } //Checking for match at previous pad
345 } //Loop over results on previous pad.
346
347
348 if(newcluster){
349 //Start a new cluster. Add it to the clusterlist, and update
350 //the list of pointers to clusters in current pad.
351 //current pad will be previous pad on next pad.
352
353 //Add to the clusterlist:
354 AliClusterData *tmp = &clusterlist[ntotal];
355 tmp->fTotalCharge = seqcharge;
356 tmp->fPad = padmean;
357 tmp->fPad2 = paderror;
358 tmp->fTime = seqaverage;
359 tmp->fTime2 = seqerror;
360 tmp->fMean = seqmean;
361 tmp->fFlags = 0; //flags for single pad clusters
362 tmp->fLastMergedPad = pad;
363
364 if(fDeconvPad){
365 tmp->fChargeFalling = 0;
366 tmp->fLastCharge = seqcharge;
367 }
368
369 //Update list of pointers to previous pad:
370 currentPt[ncurrent] = &clusterlist[ntotal];
371 ntotal++;
372 ncurrent++;
373 }
374
375 if(fDeconvTime)
376 if(newbin >= 0) goto redo;
377
378 // to prevent endless loop
379 if(time >= AliHLTTPCTransform::GetNTimeBins()){
380 LOG(AliHLTTPCLog::kFatal,"AliHLTTPCClusterFinder::ProcessRow","Digits")
381 <<"Timebin out of range "<<(Int_t)time<<ENDLOG;
382 break;
383 }
384
385
386 if(!readValue) break; //No more value
387
388 if(fCurrentRow != newRow){
389 WriteClusters(ntotal,clusterlist);
390
391 lastpad = 123456789;
392
393 currentPt = pad2;
394 previousPt = pad1;
395 nprevious=0;
396 ncurrent=0;
397 ntotal=0;
398
399 fCurrentRow = newRow;
400 }
401
402 pad = newPad;
403 time = newTime;
404
405 } // END while(readValue)
406
407 WriteClusters(ntotal,clusterlist);
408
409 LOG(AliHLTTPCLog::kInformational,"AliHLTTPCClusterFinder::ProcessDigits","Space points")
410 << "ClusterFinder found " << fNClusters << " clusters in slice " << fCurrentSlice << " patch "
411 << fCurrentPatch << ENDLOG;
412
413} // ENDEND
414
415void AliHLTTPCClusterFinder::WriteClusters(Int_t nclusters,AliClusterData *list)
416{
417 //write cluster to output pointer
418 Int_t thisrow,thissector;
419 UInt_t counter = fNClusters;
420
421 for(int j=0; j<nclusters; j++)
422 {
423 if(!list[j].fFlags) continue; //discard single pad clusters
424 if(list[j].fTotalCharge < fThreshold) continue; //noise cluster
425
426 Float_t xyz[3];
427 Float_t fpad =(Float_t)list[j].fPad / list[j].fTotalCharge;
428 Float_t fpad2=fXYErr*fXYErr; //fixed given error
429 Float_t ftime =(Float_t)list[j].fTime / list[j].fTotalCharge;
430 Float_t ftime2=fZErr*fZErr; //fixed given error
431
738c049f 432
433
434
435
436
a38a7850 437 if(fCalcerr) { //calc the errors, otherwice take the fixed error
438 Int_t patch = AliHLTTPCTransform::GetPatch(fCurrentRow);
439 UInt_t q2=list[j].fTotalCharge*list[j].fTotalCharge;
440 Float_t sy2=list[j].fPad2 * list[j].fTotalCharge - list[j].fPad * list[j].fPad;
441 sy2/=q2;
442 if(sy2 < 0) {
443 LOG(AliHLTTPCLog::kError,"AliHLTTPCClusterFinder::WriteClusters","Cluster width")
444 <<"SigmaY2 negative "<<sy2<<" on row "<<fCurrentRow<<" "<<fpad<<" "<<ftime<<ENDLOG;
445 continue;
446 } else {
447 if(!fRawSP){
448 fpad2 = (sy2 + 1./12)*AliHLTTPCTransform::GetPadPitchWidth(patch)*AliHLTTPCTransform::GetPadPitchWidth(patch);
449 if(sy2 != 0){
450 fpad2*=0.108; //constants are from offline studies
451 if(patch<2)
452 fpad2*=2.07;
453 }
454 } else fpad2=sy2; //take the width not the error
455 }
456 Float_t sz2=list[j].fTime2*list[j].fTotalCharge - list[j].fTime*list[j].fTime;
457 sz2/=q2;
458 if(sz2 < 0){
459 LOG(AliHLTTPCLog::kError,"AliHLTTPCClusterFinder::WriteClusters","Cluster width")
460 <<"SigmaZ2 negative "<<sz2<<" on row "<<fCurrentRow<<" "<<fpad<<" "<<ftime<<ENDLOG;
461 continue;
462 } else {
463 if(!fRawSP){
464 ftime2 = (sz2 + 1./12)*AliHLTTPCTransform::GetZWidth()*AliHLTTPCTransform::GetZWidth();
465 if(sz2 != 0) {
466 ftime2 *= 0.169; //constants are from offline studies
467 if(patch<2)
468 ftime2 *= 1.77;
469 }
470 } else ftime2=sz2; //take the width, not the error
471 }
472 }
473 if(fStdout==kTRUE)
474 cout<<"WriteCluster: padrow "<<fCurrentRow<<" pad "<<fpad << " +- "<<fpad2<<" time "<<ftime<<" +- "<<ftime2<<" charge "<<list[j].fTotalCharge<<endl;
475
476 if(!fRawSP){
477 AliHLTTPCTransform::Slice2Sector(fCurrentSlice,fCurrentRow,thissector,thisrow);
478 AliHLTTPCTransform::Raw2Local(xyz,thissector,thisrow,fpad,ftime);
479
480 if(xyz[0]==0) LOG(AliHLTTPCLog::kError,"AliHLTTPCClustFinder","Cluster Finder")
481 <<AliHLTTPCLog::kDec<<"Zero cluster"<<ENDLOG;
482 if(fNClusters >= fMaxNClusters)
483 {
484 LOG(AliHLTTPCLog::kError,"AliHLTTPCClustFinder::WriteClusters","Cluster Finder")
485 <<AliHLTTPCLog::kDec<<"Too many clusters "<<fNClusters<<ENDLOG;
486 return;
487 }
488
489 fSpacePointData[counter].fX = xyz[0];
490 fSpacePointData[counter].fY = xyz[1];
491 fSpacePointData[counter].fZ = xyz[2];
492
493 } else {
494 fSpacePointData[counter].fX = fCurrentRow;
495 fSpacePointData[counter].fY = fpad;
496 fSpacePointData[counter].fZ = ftime;
497 }
498
499 fSpacePointData[counter].fCharge = list[j].fTotalCharge;
500 fSpacePointData[counter].fPadRow = fCurrentRow;
501 fSpacePointData[counter].fSigmaY2 = fpad2;
502 fSpacePointData[counter].fSigmaZ2 = ftime2;
503
44be0fde 504 fSpacePointData[counter].fUsed = kFALSE; // only used / set in AliHLTTPCDisplay
738c049f 505 fSpacePointData[counter].fTrackN = -1; // only used / set in AliHLTTPCDisplay
44be0fde 506
a38a7850 507 Int_t patch=fCurrentPatch;
508 if(patch==-1) patch=0; //never store negative patch number
509 fSpacePointData[counter].fID = counter
510 +((fCurrentSlice&0x7f)<<25)+((patch&0x7)<<22);//Uli
511
512#ifdef do_mc
513 Int_t trackID[3];
514 GetTrackID((Int_t)rint(fpad),(Int_t)rint(ftime),trackID);
515
516 fSpacePointData[counter].fTrackID[0] = trackID[0];
517 fSpacePointData[counter].fTrackID[1] = trackID[1];
518 fSpacePointData[counter].fTrackID[2] = trackID[2];
519
520 //cout<<"padrow "<<fCurrentRow<<" pad "<<(Int_t)rint(fpad)<<" time "<<(Int_t)rint(ftime)<<" Trackid "<<trackID[0]<<endl;
521#endif
522
523 fNClusters++;
524 counter++;
525 }
526}
527
528// STILL TO FIX ----------------------------------------------------------------------------
529
530#ifdef do_mc
531void AliHLTTPCClusterFinder::GetTrackID(Int_t pad,Int_t time,Int_t *trackID)
532{
533 //get mc id
534 AliHLTTPCDigitRowData *rowPt = (AliHLTTPCDigitRowData*)fDigitRowData;
535
536 trackID[0]=trackID[1]=trackID[2]=-2;
537 //cout<<"Looking for pad "<<pad<<" time "<<time<<endl;
538 for(Int_t i=fFirstRow; i<=fLastRow; i++){
539 if(rowPt->fRow < (UInt_t)fCurrentRow){
540 AliHLTTPCMemHandler::UpdateRowPointer(rowPt);
541 continue;
542 }
543 AliHLTTPCDigitData *digPt = (AliHLTTPCDigitData*)rowPt->fDigitData;
544 for(UInt_t j=0; j<rowPt->fNDigit; j++){
545 Int_t cpad = digPt[j].fPad;
546 Int_t ctime = digPt[j].fTime;
547 if(cpad != pad) continue;
548 if(ctime != time) continue;
549
550 trackID[0] = digPt[j].fTrackID[0];
551 trackID[1] = digPt[j].fTrackID[1];
552 trackID[2] = digPt[j].fTrackID[2];
553
554 //cout<<"Reading row "<<fCurrentRow<<" pad "<<cpad<<" time "<<ctime<<" trackID "<<digPt[j].fTrackID[0]<<endl;
555 break;
556 }
557 break;
558 }
559}
560#endif