]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/TPCLib/AliHLTTPCClusterFinder.cxx
Alignment fixes
[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 "AliHLTTPCStandardIncludes.h"
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
21 using 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
95 ClassImp(AliHLTTPCClusterFinder)
96
97 AliHLTTPCClusterFinder::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
115 AliHLTTPCClusterFinder::~AliHLTTPCClusterFinder()
116 {
117   //destructor
118 }
119  
120 void 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
131 void 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
142 void AliHLTTPCClusterFinder::SetOutputArray(AliHLTTPCSpacePointData *pt)
143 {
144   //set pointer to output
145   fSpacePointData = pt;
146 }
147
148 void AliHLTTPCClusterFinder::Read(void* ptr,unsigned long size){
149   //set input pointer
150   fPtr = (UChar_t*)ptr;
151   fSize = size;
152 }
153
154 void 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
237       if(time >= AliHLTTPCTransform::GetNTimeBins()){
238         LOG(AliHLTTPCLog::kFatal,"AliHLTTPCClusterFinder::ProcessRow","Digits")
239           <<"Timebin out of range "<<(Int_t)time<<ENDLOG;
240         break;
241       }
242       
243       //Get the current ADC-value
244       if(fDeconvTime){
245
246         //Check if the last pixel in the sequence is smaller than this
247         if(charge > lastcharge){
248           if(lastwas_falling){
249             newbin = 1;
250             break;
251           }
252         }
253         else lastwas_falling = 1; //last pixel was larger than this
254         lastcharge = charge;
255       }
256           
257       //Sum the total charge of this sequence
258       seqcharge += charge;
259       seqaverage += time*charge;
260       seqerror += time*time*charge;
261       
262       readValue = fDigitReader->Next();
263       
264       //Check where to stop:
265       if(!readValue) break; //No more value
266
267       newPad = fDigitReader->GetPad();
268       newTime = fDigitReader->GetTime();
269       newRow = fDigitReader->GetRow() + rowOffset;
270
271       if(newPad != pad)break; //new pad
272       if(newTime != time+1) break; //end of sequence
273
274       // pad = newpad;    is equal
275       time = newTime;
276
277     }//end loop over sequence
278
279
280     //Calculate mean of sequence:
281     Int_t seqmean=0;
282     if(seqcharge)
283       seqmean = seqaverage/seqcharge;
284     else{
285       LOG(AliHLTTPCLog::kFatal,"AliHLTTPCClusterFinder::ProcessRow","Data")
286         <<"Error in data given to the cluster finder"<<ENDLOG;
287       seqmean = 1;
288       seqcharge = 1;
289     }
290
291     //Calculate mean in pad direction:
292     Int_t padmean = seqcharge*pad;
293     Int_t paderror = pad*padmean;
294
295
296     //Compare with results on previous pad:
297     for(UInt_t p=0; p<nprevious; p++){
298       
299       //dont merge sequences on the same pad twice
300       if(previousPt[p]->fLastMergedPad==pad) continue;
301
302       Int_t difference = seqmean - previousPt[p]->fMean;
303       if(difference < -fMatch) break;
304
305       if(difference <= fMatch){ //There is a match here!!
306         AliClusterData *local = previousPt[p];
307         
308         if(fDeconvPad){
309           if(seqcharge > local->fLastCharge){
310             if(local->fChargeFalling){ //The previous pad was falling
311               break; //create a new cluster
312             }               
313           }
314           else local->fChargeFalling = 1;
315           local->fLastCharge = seqcharge;
316         }
317               
318         //Don't create a new cluster, because we found a match
319         newcluster = kFALSE;
320               
321         //Update cluster on current pad with the matching one:
322         local->fTotalCharge += seqcharge;
323         local->fPad += padmean;
324         local->fPad2 += paderror;
325         local->fTime += seqaverage;
326         local->fTime2 += seqerror;
327         local->fMean = seqmean;
328         local->fFlags++; //means we have more than one pad 
329         local->fLastMergedPad = pad;
330
331         currentPt[ncurrent] = local;
332         ncurrent++;
333               
334         break;
335       } //Checking for match at previous pad
336     } //Loop over results on previous pad.
337
338
339     if(newcluster){
340       //Start a new cluster. Add it to the clusterlist, and update
341       //the list of pointers to clusters in current pad.
342       //current pad will be previous pad on next pad.
343
344       //Add to the clusterlist:
345       AliClusterData *tmp = &clusterlist[ntotal];
346       tmp->fTotalCharge = seqcharge;
347       tmp->fPad = padmean;
348       tmp->fPad2 = paderror;
349       tmp->fTime = seqaverage;
350       tmp->fTime2 = seqerror;
351       tmp->fMean = seqmean;
352       tmp->fFlags = 0;  //flags for single pad clusters
353       tmp->fLastMergedPad = pad;
354
355       if(fDeconvPad){
356         tmp->fChargeFalling = 0;
357         tmp->fLastCharge = seqcharge;
358       }
359
360       //Update list of pointers to previous pad:
361       currentPt[ncurrent] = &clusterlist[ntotal];
362       ntotal++;
363       ncurrent++;
364     }
365
366     if(fDeconvTime)
367       if(newbin >= 0) goto redo;
368   
369     // to prevent endless loop  
370     if(time >= AliHLTTPCTransform::GetNTimeBins()){
371       LOG(AliHLTTPCLog::kFatal,"AliHLTTPCClusterFinder::ProcessRow","Digits")
372         <<"Timebin out of range "<<(Int_t)time<<ENDLOG;
373       break;
374     }
375
376
377     if(!readValue) break; //No more value
378
379     if(fCurrentRow != newRow){
380       WriteClusters(ntotal,clusterlist);
381
382       lastpad = 123456789;
383
384       currentPt = pad2;
385       previousPt = pad1;
386       nprevious=0;
387       ncurrent=0;
388       ntotal=0;
389       
390       fCurrentRow = newRow;
391     }
392
393     pad = newPad;
394     time = newTime;
395   
396   } // END while(readValue)
397
398   WriteClusters(ntotal,clusterlist);
399
400   LOG(AliHLTTPCLog::kInformational,"AliHLTTPCClusterFinder::ProcessDigits","Space points") 
401     << "ClusterFinder found " << fNClusters << " clusters in slice " << fCurrentSlice << " patch " 
402     << fCurrentPatch << ENDLOG;
403
404 } // ENDEND
405
406 void AliHLTTPCClusterFinder::WriteClusters(Int_t nclusters,AliClusterData *list)
407 {
408   //write cluster to output pointer
409   Int_t thisrow,thissector;
410   UInt_t counter = fNClusters;
411   
412   for(int j=0; j<nclusters; j++)
413     {
414       if(!list[j].fFlags) continue; //discard single pad clusters
415       if(list[j].fTotalCharge < fThreshold) continue; //noise cluster
416
417       Float_t xyz[3];      
418       Float_t fpad =(Float_t)list[j].fPad / list[j].fTotalCharge;
419       Float_t fpad2=fXYErr*fXYErr; //fixed given error
420       Float_t ftime =(Float_t)list[j].fTime / list[j].fTotalCharge;
421       Float_t ftime2=fZErr*fZErr;  //fixed given error
422
423       if(fCalcerr) { //calc the errors, otherwice take the fixed error 
424         Int_t patch = AliHLTTPCTransform::GetPatch(fCurrentRow);
425         UInt_t q2=list[j].fTotalCharge*list[j].fTotalCharge;
426         Float_t sy2=list[j].fPad2 * list[j].fTotalCharge - list[j].fPad * list[j].fPad;
427         sy2/=q2;
428         if(sy2 < 0) {
429             LOG(AliHLTTPCLog::kError,"AliHLTTPCClusterFinder::WriteClusters","Cluster width")
430               <<"SigmaY2 negative "<<sy2<<" on row "<<fCurrentRow<<" "<<fpad<<" "<<ftime<<ENDLOG;
431             continue;
432         } else {
433           if(!fRawSP){
434             fpad2 = (sy2 + 1./12)*AliHLTTPCTransform::GetPadPitchWidth(patch)*AliHLTTPCTransform::GetPadPitchWidth(patch);
435             if(sy2 != 0){
436               fpad2*=0.108; //constants are from offline studies
437               if(patch<2)
438                 fpad2*=2.07;
439             }
440           } else fpad2=sy2; //take the width not the error
441         }
442         Float_t sz2=list[j].fTime2*list[j].fTotalCharge - list[j].fTime*list[j].fTime;
443         sz2/=q2;
444         if(sz2 < 0){
445           LOG(AliHLTTPCLog::kError,"AliHLTTPCClusterFinder::WriteClusters","Cluster width")
446             <<"SigmaZ2 negative "<<sz2<<" on row "<<fCurrentRow<<" "<<fpad<<" "<<ftime<<ENDLOG;
447           continue;
448         } else {
449           if(!fRawSP){
450             ftime2 = (sz2 + 1./12)*AliHLTTPCTransform::GetZWidth()*AliHLTTPCTransform::GetZWidth();
451             if(sz2 != 0) {
452               ftime2 *= 0.169; //constants are from offline studies
453               if(patch<2)
454                 ftime2 *= 1.77;
455             }
456           } else ftime2=sz2; //take the width, not the error
457         }
458       }
459       if(fStdout==kTRUE)
460         cout<<"WriteCluster: padrow "<<fCurrentRow<<" pad "<<fpad << " +- "<<fpad2<<" time "<<ftime<<" +- "<<ftime2<<" charge "<<list[j].fTotalCharge<<endl;
461       
462       if(!fRawSP){
463         AliHLTTPCTransform::Slice2Sector(fCurrentSlice,fCurrentRow,thissector,thisrow);
464         AliHLTTPCTransform::Raw2Local(xyz,thissector,thisrow,fpad,ftime);
465         
466         if(xyz[0]==0) LOG(AliHLTTPCLog::kError,"AliHLTTPCClustFinder","Cluster Finder")
467           <<AliHLTTPCLog::kDec<<"Zero cluster"<<ENDLOG;
468         if(fNClusters >= fMaxNClusters)
469           {
470             LOG(AliHLTTPCLog::kError,"AliHLTTPCClustFinder::WriteClusters","Cluster Finder")
471               <<AliHLTTPCLog::kDec<<"Too many clusters "<<fNClusters<<ENDLOG;
472             return;
473           }  
474         
475         fSpacePointData[counter].fX = xyz[0];
476         fSpacePointData[counter].fY = xyz[1];
477         fSpacePointData[counter].fZ = xyz[2];
478         
479       } else {
480         fSpacePointData[counter].fX = fCurrentRow;
481         fSpacePointData[counter].fY = fpad;
482         fSpacePointData[counter].fZ = ftime;
483       }
484       
485       fSpacePointData[counter].fCharge = list[j].fTotalCharge;
486       fSpacePointData[counter].fPadRow = fCurrentRow;
487       fSpacePointData[counter].fSigmaY2 = fpad2;
488       fSpacePointData[counter].fSigmaZ2  = ftime2;
489
490       fSpacePointData[counter].fUsed = kFALSE;         // only used / set in AliHLTTPCDisplay
491
492       Int_t patch=fCurrentPatch;
493       if(patch==-1) patch=0; //never store negative patch number
494       fSpacePointData[counter].fID = counter
495         +((fCurrentSlice&0x7f)<<25)+((patch&0x7)<<22);//Uli
496
497 #ifdef do_mc
498       Int_t trackID[3];
499       GetTrackID((Int_t)rint(fpad),(Int_t)rint(ftime),trackID);
500
501       fSpacePointData[counter].fTrackID[0] = trackID[0];
502       fSpacePointData[counter].fTrackID[1] = trackID[1];
503       fSpacePointData[counter].fTrackID[2] = trackID[2];
504
505       //cout<<"padrow "<<fCurrentRow<<" pad "<<(Int_t)rint(fpad)<<" time "<<(Int_t)rint(ftime)<<" Trackid "<<trackID[0]<<endl;
506 #endif
507       
508       fNClusters++;
509       counter++;
510     }
511 }
512
513 // STILL TO FIX  ----------------------------------------------------------------------------
514
515 #ifdef do_mc
516 void AliHLTTPCClusterFinder::GetTrackID(Int_t pad,Int_t time,Int_t *trackID)
517 {
518   //get mc id
519   AliHLTTPCDigitRowData *rowPt = (AliHLTTPCDigitRowData*)fDigitRowData;
520   
521   trackID[0]=trackID[1]=trackID[2]=-2;
522   //cout<<"Looking for pad "<<pad<<" time "<<time<<endl;
523   for(Int_t i=fFirstRow; i<=fLastRow; i++){
524     if(rowPt->fRow < (UInt_t)fCurrentRow){
525       AliHLTTPCMemHandler::UpdateRowPointer(rowPt);
526       continue;
527     }
528     AliHLTTPCDigitData *digPt = (AliHLTTPCDigitData*)rowPt->fDigitData;
529     for(UInt_t j=0; j<rowPt->fNDigit; j++){
530       Int_t cpad = digPt[j].fPad;
531       Int_t ctime = digPt[j].fTime;
532       if(cpad != pad) continue;
533       if(ctime != time) continue;
534
535       trackID[0] = digPt[j].fTrackID[0];
536       trackID[1] = digPt[j].fTrackID[1];
537       trackID[2] = digPt[j].fTrackID[2];
538       
539       //cout<<"Reading row "<<fCurrentRow<<" pad "<<cpad<<" time "<<ctime<<" trackID "<<digPt[j].fTrackID[0]<<endl;
540       break;
541     }
542     break;
543   }
544 }
545 #endif