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>
7 //*-- Copyright © ALICE HLT Group
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"
23 /** \class AliHLTTPCClusterFinder
25 //_____________________________________________________________
26 // AliHLTTPCClusterFinder
28 // The current cluster finder for HLT
31 // The cluster finder is initialized with the Init function,
32 // providing the slice and patch information to work on.
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.
43 // There are several setters which control the behaviour:
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
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);
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();
94 ClassImp(AliHLTTPCClusterFinder)
96 AliHLTTPCClusterFinder::AliHLTTPCClusterFinder()
114 AliHLTTPCClusterFinder::~AliHLTTPCClusterFinder()
119 void AliHLTTPCClusterFinder::InitSlice(Int_t slice,Int_t patch,Int_t firstrow, Int_t lastrow,Int_t nmaxpoints)
123 fMaxNClusters = nmaxpoints;
124 fCurrentSlice = slice;
125 fCurrentPatch = patch;
126 fFirstRow = firstrow;
130 void AliHLTTPCClusterFinder::InitSlice(Int_t slice,Int_t patch,Int_t nmaxpoints)
134 fMaxNClusters = nmaxpoints;
135 fCurrentSlice = slice;
136 fCurrentPatch = patch;
137 fFirstRow=AliHLTTPCTransform::GetFirstRow(patch);
138 fLastRow=AliHLTTPCTransform::GetLastRow(patch);
141 void AliHLTTPCClusterFinder::SetOutputArray(AliHLTTPCSpacePointData *pt)
143 //set pointer to output
144 fSpacePointData = pt;
147 void AliHLTTPCClusterFinder::Read(void* ptr,unsigned long size){
149 fPtr = (UChar_t*)ptr;
153 void AliHLTTPCClusterFinder::ProcessDigits()
155 bool readValue = true;
159 UShort_t time,newTime=0;
160 UInt_t charge,newPad=0;
164 // initialize block for reading packed data
165 fDigitReader->InitBlock(fPtr,fSize,fFirstRow,fLastRow);
166 readValue = fDigitReader->Next();
168 if (!readValue)return;
170 pad = fDigitReader->GetPad();
171 time = fDigitReader->GetTime();
172 fCurrentRow = fDigitReader->GetRow();
174 if ( fCurrentPatch >= 2 ) // Outer sector, patches 2, 3, 4, 5
175 rowOffset = AliHLTTPCTransform::GetFirstRow( 2 );
177 fCurrentRow += rowOffset;
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
184 AliClusterData **currentPt; //List of pointers to the current pad
185 AliClusterData **previousPt; //List of pointers to the previous pad
188 UInt_t nprevious=0,ncurrent=0,ntotal=0;
190 while ( readValue ){ // Reads through all digits in block
196 if(currentPt == pad2){
204 nprevious = ncurrent;
206 if(pad != lastpad+1){
207 //this happens if there is a pad with no signal.
208 nprevious = ncurrent = 0;
213 Bool_t newcluster = kTRUE;
214 UInt_t seqcharge=0,seqaverage=0,seqerror=0;
215 UInt_t lastcharge=0,lastwas_falling=0;
220 redo: //This is a goto.
232 // LOOP OVER CURRENR SEQUENCE
233 while(1){ //Loop over current
234 charge = fDigitReader->GetSignal();
238 if (fDigitReader->GetRow() == 90){
239 ///// LOG(AliHLTTPCLog::kFatal,"AliHLTTPCClusterFinder::Row","row90") << "PAD=" << fDigitReader->GetPad() << " TIME=" << fDigitReader->GetTime()
240 // << " SIGNAL=" << fDigitReader->GetSignal() << ENDLOG;
245 if(time >= AliHLTTPCTransform::GetNTimeBins()){
246 LOG(AliHLTTPCLog::kFatal,"AliHLTTPCClusterFinder::ProcessRow","Digits")
247 <<"Timebin out of range "<<(Int_t)time<<ENDLOG;
251 //Get the current ADC-value
254 //Check if the last pixel in the sequence is smaller than this
255 if(charge > lastcharge){
261 else lastwas_falling = 1; //last pixel was larger than this
265 //Sum the total charge of this sequence
267 seqaverage += time*charge;
268 seqerror += time*time*charge;
270 readValue = fDigitReader->Next();
272 //Check where to stop:
273 if(!readValue) break; //No more value
275 newPad = fDigitReader->GetPad();
276 newTime = fDigitReader->GetTime();
277 newRow = fDigitReader->GetRow() + rowOffset;
279 if(newPad != pad)break; //new pad
280 if(newTime != time+1) break; //end of sequence
282 // pad = newpad; is equal
285 }//end loop over sequence
288 //Calculate mean of sequence:
291 seqmean = seqaverage/seqcharge;
293 LOG(AliHLTTPCLog::kFatal,"AliHLTTPCClusterFinder::ProcessRow","Data")
294 <<"Error in data given to the cluster finder"<<ENDLOG;
299 //Calculate mean in pad direction:
300 Int_t padmean = seqcharge*pad;
301 Int_t paderror = pad*padmean;
304 //Compare with results on previous pad:
305 for(UInt_t p=0; p<nprevious; p++){
307 //dont merge sequences on the same pad twice
308 if(previousPt[p]->fLastMergedPad==pad) continue;
310 Int_t difference = seqmean - previousPt[p]->fMean;
311 if(difference < -fMatch) break;
313 if(difference <= fMatch){ //There is a match here!!
314 AliClusterData *local = previousPt[p];
317 if(seqcharge > local->fLastCharge){
318 if(local->fChargeFalling){ //The previous pad was falling
319 break; //create a new cluster
322 else local->fChargeFalling = 1;
323 local->fLastCharge = seqcharge;
326 //Don't create a new cluster, because we found a match
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;
339 currentPt[ncurrent] = local;
343 } //Checking for match at previous pad
344 } //Loop over results on previous pad.
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.
352 //Add to the clusterlist:
353 AliClusterData *tmp = &clusterlist[ntotal];
354 tmp->fTotalCharge = seqcharge;
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;
364 tmp->fChargeFalling = 0;
365 tmp->fLastCharge = seqcharge;
368 //Update list of pointers to previous pad:
369 currentPt[ncurrent] = &clusterlist[ntotal];
375 if(newbin >= 0) goto redo;
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;
385 if(!readValue) break; //No more value
387 if(fCurrentRow != newRow){
388 WriteClusters(ntotal,clusterlist);
398 fCurrentRow = newRow;
404 } // END while(readValue)
406 WriteClusters(ntotal,clusterlist);
408 LOG(AliHLTTPCLog::kInformational,"AliHLTTPCClusterFinder::ProcessDigits","Space points")
409 << "ClusterFinder found " << fNClusters << " clusters in slice " << fCurrentSlice << " patch "
410 << fCurrentPatch << ENDLOG;
414 void AliHLTTPCClusterFinder::WriteClusters(Int_t nclusters,AliClusterData *list)
416 //write cluster to output pointer
417 Int_t thisrow,thissector;
418 UInt_t counter = fNClusters;
420 for(int j=0; j<nclusters; j++)
422 if(!list[j].fFlags) continue; //discard single pad clusters
423 if(list[j].fTotalCharge < fThreshold) continue; //noise cluster
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
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;
442 LOG(AliHLTTPCLog::kError,"AliHLTTPCClusterFinder::WriteClusters","Cluster width")
443 <<"SigmaY2 negative "<<sy2<<" on row "<<fCurrentRow<<" "<<fpad<<" "<<ftime<<ENDLOG;
447 fpad2 = (sy2 + 1./12)*AliHLTTPCTransform::GetPadPitchWidth(patch)*AliHLTTPCTransform::GetPadPitchWidth(patch);
449 fpad2*=0.108; //constants are from offline studies
453 } else fpad2=sy2; //take the width not the error
455 Float_t sz2=list[j].fTime2*list[j].fTotalCharge - list[j].fTime*list[j].fTime;
458 LOG(AliHLTTPCLog::kError,"AliHLTTPCClusterFinder::WriteClusters","Cluster width")
459 <<"SigmaZ2 negative "<<sz2<<" on row "<<fCurrentRow<<" "<<fpad<<" "<<ftime<<ENDLOG;
463 ftime2 = (sz2 + 1./12)*AliHLTTPCTransform::GetZWidth()*AliHLTTPCTransform::GetZWidth();
465 ftime2 *= 0.169; //constants are from offline studies
469 } else ftime2=sz2; //take the width, not the error
473 cout<<"WriteCluster: padrow "<<fCurrentRow<<" pad "<<fpad << " +- "<<fpad2<<" time "<<ftime<<" +- "<<ftime2<<" charge "<<list[j].fTotalCharge<<endl;
476 AliHLTTPCTransform::Slice2Sector(fCurrentSlice,fCurrentRow,thissector,thisrow);
477 AliHLTTPCTransform::Raw2Local(xyz,thissector,thisrow,fpad,ftime);
479 if(xyz[0]==0) LOG(AliHLTTPCLog::kError,"AliHLTTPCClustFinder","Cluster Finder")
480 <<AliHLTTPCLog::kDec<<"Zero cluster"<<ENDLOG;
481 if(fNClusters >= fMaxNClusters)
483 LOG(AliHLTTPCLog::kError,"AliHLTTPCClustFinder::WriteClusters","Cluster Finder")
484 <<AliHLTTPCLog::kDec<<"Too many clusters "<<fNClusters<<ENDLOG;
488 fSpacePointData[counter].fX = xyz[0];
489 fSpacePointData[counter].fY = xyz[1];
490 fSpacePointData[counter].fZ = xyz[2];
493 fSpacePointData[counter].fX = fCurrentRow;
494 fSpacePointData[counter].fY = fpad;
495 fSpacePointData[counter].fZ = ftime;
498 fSpacePointData[counter].fCharge = list[j].fTotalCharge;
499 fSpacePointData[counter].fPadRow = fCurrentRow;
500 fSpacePointData[counter].fSigmaY2 = fpad2;
501 fSpacePointData[counter].fSigmaZ2 = ftime2;
503 fSpacePointData[counter].fUsed = kFALSE; // only used / set in AliHLTTPCDisplay
504 fSpacePointData[counter].fTrackN = -1; // only used / set in AliHLTTPCDisplay
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
513 GetTrackID((Int_t)rint(fpad),(Int_t)rint(ftime),trackID);
515 fSpacePointData[counter].fTrackID[0] = trackID[0];
516 fSpacePointData[counter].fTrackID[1] = trackID[1];
517 fSpacePointData[counter].fTrackID[2] = trackID[2];
519 //cout<<"padrow "<<fCurrentRow<<" pad "<<(Int_t)rint(fpad)<<" time "<<(Int_t)rint(ftime)<<" Trackid "<<trackID[0]<<endl;
527 // STILL TO FIX ----------------------------------------------------------------------------
530 void AliHLTTPCClusterFinder::GetTrackID(Int_t pad,Int_t time,Int_t *trackID)
533 AliHLTTPCDigitRowData *rowPt = (AliHLTTPCDigitRowData*)fDigitRowData;
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);
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;
549 trackID[0] = digPt[j].fTrackID[0];
550 trackID[1] = digPt[j].fTrackID[1];
551 trackID[2] = digPt[j].fTrackID[2];
553 //cout<<"Reading row "<<fCurrentRow<<" pad "<<cpad<<" time "<<ctime<<" trackID "<<digPt[j].fTrackID[0]<<endl;