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