3 // Author: Anders Vestbo <mailto:vestbo@fi.uib.no>, Constantin Loizides <mailto:loizides@ikf.uni-frankfurt.de>
4 //*-- Copyright © ALICE HLT Group
6 #include "AliL3StandardIncludes.h"
7 #include "AliL3RootTypes.h"
8 #include "AliL3Logging.h"
9 #include "AliL3ClustFinderNew.h"
10 #include "AliL3DigitData.h"
11 #include "AliL3Transform.h"
12 #include "AliL3SpacePointData.h"
13 #include "AliL3MemHandler.h"
19 /** \class AliL3ClustFinderNew
21 //_____________________________________________________________
22 // AliL3ClustFinderNew
24 // The current cluster finder for HLT
27 // The cluster finder is initialized with the Init function,
28 // providing the slice and patch information to work on.
29 // The input is a AliL3DigitRowData structure using the
30 // Read function. The resulting space points will be in the
31 // array given by the SetOutputArray function.
33 // There are several setters which control the behaviour:
35 // - SetXYError(Float_t): set fixed error in XY direction
36 // - SetZError(Float_t): set fixed error in Z direction
37 // (used if errors are not calculated)
38 // - SetDeconv(Bool_t): switch on/off deconvolution
39 // - SetThreshold(UInt_t): set charge threshold for cluster
40 // - SetMatchWidth(UInt_t): set the match distance in
41 // time for sequences to be merged
42 // - SetSTDOutput(Bool_t): switch on/off output about found clusters
43 // - SetCalcErr(Bool_t): switch on/off calculation of
44 // space point errors (or widths in raw system)
45 // - SetRawSP(Bool_t): switch on/off convertion to raw system
50 // AliL3FileHandler *file = new AliL3FileHandler();
51 // file->SetAliInput(digitfile); //give some input file
52 // for(int slice=0; slice<=35; slice++){
53 // for(int patch=0; pat<6; pat++){
54 // file->Init(slice,patch);
56 // UInt_t maxclusters=100000;
57 // UInt_t pointsize = maxclusters*sizeof(AliL3SpacePointData);
58 // AliL3SpacePointData *points = (AliL3SpacePointData*)memory->Allocate(pointsize);
59 // AliL3DigitRowData *digits = (AliL3DigitRowData*)file->AliAltroDigits2Memory(ndigits,event);
60 // AliL3ClustFinderNew *cf = new AliL3ClustFinderNew();
61 // cf->SetMatchWidth(2);
62 // cf->InitSlice(slice,patch,maxclusters);
63 // cf->SetSTDOutput(kTRUE); //Some output to standard IO
64 // cf->SetRawSP(kFALSE); //Convert space points to local system
65 // cf->SetThreshold(5); //Threshold of cluster charge
66 // cf->SetDeconv(kTRUE); //Deconv in pad and time direction
67 // cf->SetCalcErr(kTRUE); //Calculate the errors of the spacepoints
68 // cf->SetOutputArray(points); //Move the spacepoints to the array
69 // cf->Read(ndigits,digits); //give the data to the cf
70 // cf->ProcessDigits(); //process the rows given by init
71 // Int_t npoints = cf->GetNumberOfClusters();
72 // AliL3MemHandler *out= new AliL3MemHandler();
73 // out->SetBinaryOutput(fname);
74 // out->Memory2Binary(npoints,points); //store the spacepoints
75 // out->CloseBinaryOutput();
84 ClassImp(AliL3ClustFinderNew)
86 AliL3ClustFinderNew::AliL3ClustFinderNew()
102 AliL3ClustFinderNew::~AliL3ClustFinderNew()
108 void AliL3ClustFinderNew::InitSlice(Int_t slice,Int_t patch,Int_t firstrow, Int_t lastrow,Int_t nmaxpoints)
112 fMaxNClusters = nmaxpoints;
113 fCurrentSlice = slice;
114 fCurrentPatch = patch;
115 fFirstRow = firstrow;
119 void AliL3ClustFinderNew::InitSlice(Int_t slice,Int_t patch,Int_t nmaxpoints)
123 fMaxNClusters = nmaxpoints;
124 fCurrentSlice = slice;
125 fCurrentPatch = patch;
126 fFirstRow=AliL3Transform::GetFirstRow(patch);
127 fLastRow=AliL3Transform::GetLastRow(patch);
130 void AliL3ClustFinderNew::SetOutputArray(AliL3SpacePointData *pt)
132 //set pointer to output
133 fSpacePointData = pt;
136 void AliL3ClustFinderNew::Read(UInt_t ndigits,AliL3DigitRowData *ptr)
139 fNDigitRowData = ndigits;
143 void AliL3ClustFinderNew::ProcessDigits()
145 //Loop over rows, and call processrow
146 AliL3DigitRowData *tempPt = (AliL3DigitRowData*)fDigitRowData;
148 for(Int_t i=fFirstRow; i<=fLastRow; i++)
151 if((Int_t)tempPt->fRow!=fCurrentRow){
152 LOG(AliL3Log::kWarning,"AliL3ClustFinderNew::ProcessDigits","Digits")
153 <<"Row number should match! "<<tempPt->fRow<<" "<<fCurrentRow<<ENDLOG;
157 Byte_t *tmp = (Byte_t*)tempPt;
158 Int_t size = sizeof(AliL3DigitRowData) + tempPt->fNDigit*sizeof(AliL3DigitData);
160 tempPt = (AliL3DigitRowData*)tmp;
162 LOG(AliL3Log::kInformational,"AliL3ClustFinderNew::ProcessDigits","Space points")
163 <<"Cluster finder found "<<fNClusters<<" clusters in slice "<<fCurrentSlice
164 <<" patch "<<fCurrentPatch<<ENDLOG;
167 void AliL3ClustFinderNew::ProcessRow(AliL3DigitRowData *tempPt)
170 UInt_t lastpad = 123456789;
172 AliClusterData *pad1[5000]; //2 lists for internal memory=2pads
173 AliClusterData *pad2[5000]; //2 lists for internal memory=2pads
174 AliClusterData clusterlist[10000]; //Clusterlist
176 AliClusterData **currentPt; //List of pointers to the current pad
177 AliClusterData **previousPt; //List of pointers to the previous pad
180 UInt_t nprevious=0,ncurrent=0,ntotal=0;
182 //Loop over sequences of this row:
183 for(UInt_t bin=0; bin<tempPt->fNDigit; bin++)
185 AliL3DigitData *data = tempPt->fDigitData;
186 if(data[bin].fPad != lastpad)
191 if(currentPt == pad2)
201 nprevious = ncurrent;
203 if(bin[data].fPad != lastpad+1)
205 //this happens if there is a pad with no signal.
206 nprevious = ncurrent = 0;
208 lastpad = data[bin].fPad;
211 Bool_t newcluster = kTRUE;
212 UInt_t seqcharge=0,seqaverage=0,seqerror=0;
213 UInt_t lastcharge=0,lastwas_falling=0;
218 redo: //This is a goto.
229 while(1) //Loop over current sequence
231 if(data[bin].fTime >= AliL3Transform::GetNTimeBins())
233 LOG(AliL3Log::kFatal,"AliL3ClustFinderNew::ProcessRow","Digits")
234 <<"Timebin out of range "<<(Int_t)data[bin].fTime<<ENDLOG;
238 //Get the current ADC-value
239 UInt_t charge = data[bin].fCharge;
243 //Check if the last pixel in the sequence is smaller than this
244 if(charge > lastcharge)
252 else lastwas_falling = 1; //last pixel was larger than this
256 //Sum the total charge of this sequence
258 seqaverage += data[bin].fTime*charge;
259 seqerror += data[bin].fTime*data[bin].fTime*charge;
261 //Check where to stop:
262 if(bin >= tempPt->fNDigit - 1) //out of range
264 if(data[bin+1].fPad != data[bin].fPad) //new pad
266 if(data[bin+1].fTime != data[bin].fTime+1) //end of sequence
270 }//end loop over sequence
272 //Calculate mean of sequence:
275 seqmean = seqaverage/seqcharge;
278 LOG(AliL3Log::kFatal,"AliL3ClustFinderNew::ProcessRow","Data")
279 <<"Error in data given to the cluster finder"<<ENDLOG;
284 //Calculate mean in pad direction:
285 Int_t padmean = seqcharge*data[bin].fPad;
286 Int_t paderror = data[bin].fPad*padmean;
288 //Compare with results on previous pad:
289 for(UInt_t p=0; p<nprevious; p++)
291 //dont merge sequences on the same pad twice
292 if(previousPt[p]->fLastMergedPad==data[bin].fPad) continue;
294 Int_t difference = seqmean - previousPt[p]->fMean;
295 if(difference < -fMatch) break;
297 if(difference <= fMatch) //There is a match here!!
299 AliClusterData *local = previousPt[p];
303 if(seqcharge > local->fLastCharge)
305 if(local->fChargeFalling) //The previous pad was falling
307 break; //create a new cluster
311 local->fChargeFalling = 1;
312 local->fLastCharge = seqcharge;
315 //Don't create a new cluster, because we found a match
318 //Update cluster on current pad with the matching one:
319 local->fTotalCharge += seqcharge;
320 local->fPad += padmean;
321 local->fPad2 += paderror;
322 local->fTime += seqaverage;
323 local->fTime2 += seqerror;
324 local->fMean = seqmean;
325 local->fFlags++; //means we have more than one pad
326 local->fLastMergedPad = data[bin].fPad;
328 currentPt[ncurrent] = local;
332 } //Checking for match at previous pad
333 } //Loop over results on previous pad.
337 //Start a new cluster. Add it to the clusterlist, and update
338 //the list of pointers to clusters in current pad.
339 //current pad will be previous pad on next pad.
341 //Add to the clusterlist:
342 AliClusterData *tmp = &clusterlist[ntotal];
343 tmp->fTotalCharge = seqcharge;
345 tmp->fPad2 = paderror;
346 tmp->fTime = seqaverage;
347 tmp->fTime2 = seqerror;
348 tmp->fMean = seqmean;
349 tmp->fFlags = 0; //flags for single pad clusters
350 tmp->fLastMergedPad = data[bin].fPad;
354 tmp->fChargeFalling = 0;
355 tmp->fLastCharge = seqcharge;
358 //Update list of pointers to previous pad:
359 currentPt[ncurrent] = &clusterlist[ntotal];
365 if(newbin >= 0) goto redo;
366 }//Loop over digits on this padrow
368 WriteClusters(ntotal,clusterlist);
371 void AliL3ClustFinderNew::WriteClusters(Int_t nclusters,AliClusterData *list)
373 //write cluster to output pointer
374 Int_t thisrow,thissector;
375 UInt_t counter = fNClusters;
377 for(int j=0; j<nclusters; j++)
379 if(!list[j].fFlags) continue; //discard single pad clusters
380 if(list[j].fTotalCharge < fThreshold) continue; //noise cluster
383 Float_t fpad =(Float_t)list[j].fPad / list[j].fTotalCharge;
384 Float_t fpad2=fXYErr*fXYErr; //fixed given error
385 Float_t ftime =(Float_t)list[j].fTime / list[j].fTotalCharge;
386 Float_t ftime2=fZErr*fZErr; //fixed given error
388 if(fCalcerr) { //calc the errors, otherwice take the fixed error
389 Int_t patch = AliL3Transform::GetPatch(fCurrentRow);
390 UInt_t q2=list[j].fTotalCharge*list[j].fTotalCharge;
391 Float_t sy2=list[j].fPad2 * list[j].fTotalCharge - list[j].fPad * list[j].fPad;
394 LOG(AliL3Log::kError,"AliL3ClustFinderNew::WriteClusters","Cluster width")
395 <<"SigmaY2 negative "<<sy2<<" on row "<<fCurrentRow<<" "<<fpad<<" "<<ftime<<ENDLOG;
399 fpad2 = (sy2 + 1./12)*AliL3Transform::GetPadPitchWidth(patch)*AliL3Transform::GetPadPitchWidth(patch);
401 fpad2*=0.108; //constants are from offline studies
405 } else fpad2=sy2; //take the width not the error
407 Float_t sz2=list[j].fTime2*list[j].fTotalCharge - list[j].fTime*list[j].fTime;
410 LOG(AliL3Log::kError,"AliL3ClustFinderNew::WriteClusters","Cluster width")
411 <<"SigmaZ2 negative "<<sz2<<" on row "<<fCurrentRow<<" "<<fpad<<" "<<ftime<<ENDLOG;
415 ftime2 = (sz2 + 1./12)*AliL3Transform::GetZWidth()*AliL3Transform::GetZWidth();
417 ftime2 *= 0.169; //constants are from offline studies
421 } else ftime2=sz2; //take the width, not the error
425 cout<<"WriteCluster: padrow "<<fCurrentRow<<" pad "<<fpad << " +- "<<fpad2<<" time "<<ftime<<" +- "<<ftime2<<" charge "<<list[j].fTotalCharge<<endl;
428 AliL3Transform::Slice2Sector(fCurrentSlice,fCurrentRow,thissector,thisrow);
429 AliL3Transform::Raw2Local(xyz,thissector,thisrow,fpad,ftime);
431 if(xyz[0]==0) LOG(AliL3Log::kError,"AliL3ClustFinder","Cluster Finder")
432 <<AliL3Log::kDec<<"Zero cluster"<<ENDLOG;
433 if(fNClusters >= fMaxNClusters)
435 LOG(AliL3Log::kError,"AliL3ClustFinder::WriteClusters","Cluster Finder")
436 <<AliL3Log::kDec<<"Too many clusters "<<fNClusters<<ENDLOG;
440 fSpacePointData[counter].fX = xyz[0];
441 fSpacePointData[counter].fY = xyz[1];
442 fSpacePointData[counter].fZ = xyz[2];
445 fSpacePointData[counter].fX = fCurrentRow;
446 fSpacePointData[counter].fY = fpad;
447 fSpacePointData[counter].fZ = ftime;
450 fSpacePointData[counter].fCharge = list[j].fTotalCharge;
451 fSpacePointData[counter].fPadRow = fCurrentRow;
452 fSpacePointData[counter].fSigmaY2 = fpad2;
453 fSpacePointData[counter].fSigmaZ2 = ftime2;
455 Int_t patch=fCurrentPatch;
456 if(patch==-1) patch=0; //never store negative patch number
457 fSpacePointData[counter].fID = counter
458 +((fCurrentSlice&0x7f)<<25)+((patch&0x7)<<22);//Uli
461 GetTrackID((Int_t)rint(fpad),(Int_t)rint(ftime),trackID);
463 fSpacePointData[counter].fTrackID[0] = trackID[0];
464 fSpacePointData[counter].fTrackID[1] = trackID[1];
465 fSpacePointData[counter].fTrackID[2] = trackID[2];
467 //cout<<"padrow "<<fCurrentRow<<" pad "<<(Int_t)rint(fpad)<<" time "<<(Int_t)rint(ftime)<<" Trackid "<<trackID[0]<<endl;
476 void AliL3ClustFinderNew::GetTrackID(Int_t pad,Int_t time,Int_t *trackID)
479 AliL3DigitRowData *rowPt = (AliL3DigitRowData*)fDigitRowData;
481 trackID[0]=trackID[1]=trackID[2]=-2;
482 //cout<<"Looking for pad "<<pad<<" time "<<time<<endl;
483 for(Int_t i=fFirstRow; i<=fLastRow; i++){
484 if(rowPt->fRow < (UInt_t)fCurrentRow){
485 AliL3MemHandler::UpdateRowPointer(rowPt);
488 AliL3DigitData *digPt = (AliL3DigitData*)rowPt->fDigitData;
489 for(UInt_t j=0; j<rowPt->fNDigit; j++){
490 Int_t cpad = digPt[j].fPad;
491 Int_t ctime = digPt[j].fTime;
492 if(cpad != pad) continue;
493 if(ctime != time) continue;
495 trackID[0] = digPt[j].fTrackID[0];
496 trackID[1] = digPt[j].fTrackID[1];
497 trackID[2] = digPt[j].fTrackID[2];
499 //cout<<"Reading row "<<fCurrentRow<<" pad "<<cpad<<" time "<<ctime<<" trackID "<<digPt[j].fTrackID[0]<<endl;