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 "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"
24 /** \class AliHLTTPCClusterFinder
26 //_____________________________________________________________
27 // AliHLTTPCClusterFinder
29 // The current cluster finder for HLT
32 // The cluster finder is initialized with the Init function,
33 // providing the slice and patch information to work on.
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.
44 // There are several setters which control the behaviour:
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
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);
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();
95 ClassImp(AliHLTTPCClusterFinder)
97 AliHLTTPCClusterFinder::AliHLTTPCClusterFinder()
115 AliHLTTPCClusterFinder::~AliHLTTPCClusterFinder()
120 void AliHLTTPCClusterFinder::InitSlice(Int_t slice,Int_t patch,Int_t firstrow, Int_t lastrow,Int_t nmaxpoints)
124 fMaxNClusters = nmaxpoints;
125 fCurrentSlice = slice;
126 fCurrentPatch = patch;
127 fFirstRow = firstrow;
131 void AliHLTTPCClusterFinder::InitSlice(Int_t slice,Int_t patch,Int_t nmaxpoints)
135 fMaxNClusters = nmaxpoints;
136 fCurrentSlice = slice;
137 fCurrentPatch = patch;
138 fFirstRow=AliHLTTPCTransform::GetFirstRow(patch);
139 fLastRow=AliHLTTPCTransform::GetLastRow(patch);
142 void AliHLTTPCClusterFinder::SetOutputArray(AliHLTTPCSpacePointData *pt)
144 //set pointer to output
145 fSpacePointData = pt;
148 void AliHLTTPCClusterFinder::Read(void* ptr,unsigned long size){
150 fPtr = (UChar_t*)ptr;
154 void AliHLTTPCClusterFinder::ProcessDigits()
156 bool readValue = true;
160 UShort_t time,newTime=0;
161 UInt_t charge,newPad=0;
165 // initialize block for reading packed data
166 fDigitReader->InitBlock(fPtr,fSize,fFirstRow,fLastRow);
167 readValue = fDigitReader->Next();
169 if (!readValue)return;
171 pad = fDigitReader->GetPad();
172 time = fDigitReader->GetTime();
173 fCurrentRow = fDigitReader->GetRow();
175 if ( fCurrentPatch >= 2 ) // Outer sector, patches 2, 3, 4, 5
176 rowOffset = AliHLTTPCTransform::GetFirstRow( 2 );
178 fCurrentRow += rowOffset;
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
185 AliClusterData **currentPt; //List of pointers to the current pad
186 AliClusterData **previousPt; //List of pointers to the previous pad
189 UInt_t nprevious=0,ncurrent=0,ntotal=0;
191 while ( readValue ){ // Reads through all digits in block
197 if(currentPt == pad2){
205 nprevious = ncurrent;
207 if(pad != lastpad+1){
208 //this happens if there is a pad with no signal.
209 nprevious = ncurrent = 0;
214 Bool_t newcluster = kTRUE;
215 UInt_t seqcharge=0,seqaverage=0,seqerror=0;
216 UInt_t lastcharge=0,lastwas_falling=0;
221 redo: //This is a goto.
233 // LOOP OVER CURRENR SEQUENCE
234 while(1){ //Loop over current
235 charge = fDigitReader->GetSignal();
237 if(time >= AliHLTTPCTransform::GetNTimeBins()){
238 LOG(AliHLTTPCLog::kFatal,"AliHLTTPCClusterFinder::ProcessRow","Digits")
239 <<"Timebin out of range "<<(Int_t)time<<ENDLOG;
243 //Get the current ADC-value
246 //Check if the last pixel in the sequence is smaller than this
247 if(charge > lastcharge){
253 else lastwas_falling = 1; //last pixel was larger than this
257 //Sum the total charge of this sequence
259 seqaverage += time*charge;
260 seqerror += time*time*charge;
262 readValue = fDigitReader->Next();
264 //Check where to stop:
265 if(!readValue) break; //No more value
267 newPad = fDigitReader->GetPad();
268 newTime = fDigitReader->GetTime();
269 newRow = fDigitReader->GetRow() + rowOffset;
271 if(newPad != pad)break; //new pad
272 if(newTime != time+1) break; //end of sequence
274 // pad = newpad; is equal
277 }//end loop over sequence
280 //Calculate mean of sequence:
283 seqmean = seqaverage/seqcharge;
285 LOG(AliHLTTPCLog::kFatal,"AliHLTTPCClusterFinder::ProcessRow","Data")
286 <<"Error in data given to the cluster finder"<<ENDLOG;
291 //Calculate mean in pad direction:
292 Int_t padmean = seqcharge*pad;
293 Int_t paderror = pad*padmean;
296 //Compare with results on previous pad:
297 for(UInt_t p=0; p<nprevious; p++){
299 //dont merge sequences on the same pad twice
300 if(previousPt[p]->fLastMergedPad==pad) continue;
302 Int_t difference = seqmean - previousPt[p]->fMean;
303 if(difference < -fMatch) break;
305 if(difference <= fMatch){ //There is a match here!!
306 AliClusterData *local = previousPt[p];
309 if(seqcharge > local->fLastCharge){
310 if(local->fChargeFalling){ //The previous pad was falling
311 break; //create a new cluster
314 else local->fChargeFalling = 1;
315 local->fLastCharge = seqcharge;
318 //Don't create a new cluster, because we found a match
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;
331 currentPt[ncurrent] = local;
335 } //Checking for match at previous pad
336 } //Loop over results on previous pad.
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.
344 //Add to the clusterlist:
345 AliClusterData *tmp = &clusterlist[ntotal];
346 tmp->fTotalCharge = seqcharge;
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;
356 tmp->fChargeFalling = 0;
357 tmp->fLastCharge = seqcharge;
360 //Update list of pointers to previous pad:
361 currentPt[ncurrent] = &clusterlist[ntotal];
367 if(newbin >= 0) goto redo;
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;
377 if(!readValue) break; //No more value
379 if(fCurrentRow != newRow){
380 WriteClusters(ntotal,clusterlist);
390 fCurrentRow = newRow;
396 } // END while(readValue)
398 WriteClusters(ntotal,clusterlist);
400 LOG(AliHLTTPCLog::kInformational,"AliHLTTPCClusterFinder::ProcessDigits","Space points")
401 << "ClusterFinder found " << fNClusters << " clusters in slice " << fCurrentSlice << " patch "
402 << fCurrentPatch << ENDLOG;
406 void AliHLTTPCClusterFinder::WriteClusters(Int_t nclusters,AliClusterData *list)
408 //write cluster to output pointer
409 Int_t thisrow,thissector;
410 UInt_t counter = fNClusters;
412 for(int j=0; j<nclusters; j++)
414 if(!list[j].fFlags) continue; //discard single pad clusters
415 if(list[j].fTotalCharge < fThreshold) continue; //noise cluster
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
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;
429 LOG(AliHLTTPCLog::kError,"AliHLTTPCClusterFinder::WriteClusters","Cluster width")
430 <<"SigmaY2 negative "<<sy2<<" on row "<<fCurrentRow<<" "<<fpad<<" "<<ftime<<ENDLOG;
434 fpad2 = (sy2 + 1./12)*AliHLTTPCTransform::GetPadPitchWidth(patch)*AliHLTTPCTransform::GetPadPitchWidth(patch);
436 fpad2*=0.108; //constants are from offline studies
440 } else fpad2=sy2; //take the width not the error
442 Float_t sz2=list[j].fTime2*list[j].fTotalCharge - list[j].fTime*list[j].fTime;
445 LOG(AliHLTTPCLog::kError,"AliHLTTPCClusterFinder::WriteClusters","Cluster width")
446 <<"SigmaZ2 negative "<<sz2<<" on row "<<fCurrentRow<<" "<<fpad<<" "<<ftime<<ENDLOG;
450 ftime2 = (sz2 + 1./12)*AliHLTTPCTransform::GetZWidth()*AliHLTTPCTransform::GetZWidth();
452 ftime2 *= 0.169; //constants are from offline studies
456 } else ftime2=sz2; //take the width, not the error
460 cout<<"WriteCluster: padrow "<<fCurrentRow<<" pad "<<fpad << " +- "<<fpad2<<" time "<<ftime<<" +- "<<ftime2<<" charge "<<list[j].fTotalCharge<<endl;
463 AliHLTTPCTransform::Slice2Sector(fCurrentSlice,fCurrentRow,thissector,thisrow);
464 AliHLTTPCTransform::Raw2Local(xyz,thissector,thisrow,fpad,ftime);
466 if(xyz[0]==0) LOG(AliHLTTPCLog::kError,"AliHLTTPCClustFinder","Cluster Finder")
467 <<AliHLTTPCLog::kDec<<"Zero cluster"<<ENDLOG;
468 if(fNClusters >= fMaxNClusters)
470 LOG(AliHLTTPCLog::kError,"AliHLTTPCClustFinder::WriteClusters","Cluster Finder")
471 <<AliHLTTPCLog::kDec<<"Too many clusters "<<fNClusters<<ENDLOG;
475 fSpacePointData[counter].fX = xyz[0];
476 fSpacePointData[counter].fY = xyz[1];
477 fSpacePointData[counter].fZ = xyz[2];
480 fSpacePointData[counter].fX = fCurrentRow;
481 fSpacePointData[counter].fY = fpad;
482 fSpacePointData[counter].fZ = ftime;
485 fSpacePointData[counter].fCharge = list[j].fTotalCharge;
486 fSpacePointData[counter].fPadRow = fCurrentRow;
487 fSpacePointData[counter].fSigmaY2 = fpad2;
488 fSpacePointData[counter].fSigmaZ2 = ftime2;
490 Int_t patch=fCurrentPatch;
491 if(patch==-1) patch=0; //never store negative patch number
492 fSpacePointData[counter].fID = counter
493 +((fCurrentSlice&0x7f)<<25)+((patch&0x7)<<22);//Uli
497 GetTrackID((Int_t)rint(fpad),(Int_t)rint(ftime),trackID);
499 fSpacePointData[counter].fTrackID[0] = trackID[0];
500 fSpacePointData[counter].fTrackID[1] = trackID[1];
501 fSpacePointData[counter].fTrackID[2] = trackID[2];
503 //cout<<"padrow "<<fCurrentRow<<" pad "<<(Int_t)rint(fpad)<<" time "<<(Int_t)rint(ftime)<<" Trackid "<<trackID[0]<<endl;
511 // STILL TO FIX ----------------------------------------------------------------------------
514 void AliHLTTPCClusterFinder::GetTrackID(Int_t pad,Int_t time,Int_t *trackID)
517 AliHLTTPCDigitRowData *rowPt = (AliHLTTPCDigitRowData*)fDigitRowData;
519 trackID[0]=trackID[1]=trackID[2]=-2;
520 //cout<<"Looking for pad "<<pad<<" time "<<time<<endl;
521 for(Int_t i=fFirstRow; i<=fLastRow; i++){
522 if(rowPt->fRow < (UInt_t)fCurrentRow){
523 AliHLTTPCMemHandler::UpdateRowPointer(rowPt);
526 AliHLTTPCDigitData *digPt = (AliHLTTPCDigitData*)rowPt->fDigitData;
527 for(UInt_t j=0; j<rowPt->fNDigit; j++){
528 Int_t cpad = digPt[j].fPad;
529 Int_t ctime = digPt[j].fTime;
530 if(cpad != pad) continue;
531 if(ctime != time) continue;
533 trackID[0] = digPt[j].fTrackID[0];
534 trackID[1] = digPt[j].fTrackID[1];
535 trackID[2] = digPt[j].fTrackID[2];
537 //cout<<"Reading row "<<fCurrentRow<<" pad "<<cpad<<" time "<<ctime<<" trackID "<<digPt[j].fTrackID[0]<<endl;