3 // Author: Anders Vestbo <mailto:vestbo@fi.uib.no>
4 //*-- Copyright © ASV
8 #include "AliL3Logging.h"
9 #include "AliL3ClustFinderNew.h"
10 #include "AliL3DigitData.h"
11 #include "AliL3Transform.h"
12 #include "AliL3SpacePointData.h"
13 #include "AliL3MemHandler.h"
15 /** \class AliL3ClustFinderNew
17 //_____________________________________________________________
18 // AliL3ClustFinderNew
20 // The current cluster finder for HLT
24 ClassImp(AliL3ClustFinderNew)
26 AliL3ClustFinderNew::AliL3ClustFinderNew()
38 AliL3ClustFinderNew::~AliL3ClustFinderNew()
42 void AliL3ClustFinderNew::InitSlice(Int_t slice,Int_t patch,Int_t firstrow, Int_t lastrow,Int_t nmaxpoints)
45 fMaxNClusters = nmaxpoints;
46 fCurrentSlice = slice;
47 fCurrentPatch = patch;
52 void AliL3ClustFinderNew::InitSlice(Int_t slice,Int_t patch,Int_t nmaxpoints)
55 fMaxNClusters = nmaxpoints;
56 fCurrentSlice = slice;
57 fCurrentPatch = patch;
60 void AliL3ClustFinderNew::SetOutputArray(AliL3SpacePointData *pt)
65 void AliL3ClustFinderNew::Read(UInt_t ndigits,AliL3DigitRowData *ptr)
67 fNDigitRowData = ndigits;
71 void AliL3ClustFinderNew::ProcessDigits()
73 //Loop over rows, and call processrow
76 AliL3DigitRowData *tempPt = (AliL3DigitRowData*)fDigitRowData;
78 for(Int_t i=fFirstRow; i<=fLastRow; i++)
82 Byte_t *tmp = (Byte_t*)tempPt;
83 Int_t size = sizeof(AliL3DigitRowData) + tempPt->fNDigit*sizeof(AliL3DigitData);
85 tempPt = (AliL3DigitRowData*)tmp;
87 LOG(AliL3Log::kInformational,"AliL3ClustFinderNew::WriteClusters","Space points")
88 <<"Cluster finder found "<<fNClusters<<" clusters in slice "<<fCurrentSlice
89 <<" patch "<<fCurrentPatch<<ENDLOG;
92 void AliL3ClustFinderNew::ProcessRow(AliL3DigitRowData *tempPt)
95 UInt_t last_pad = 123456789;
97 ClusterData *pad1[2500]; //2 lists for internal memory=2pads
98 ClusterData *pad2[2500]; //2 lists for internal memory=2pads
99 ClusterData clusterlist[5000]; //Clusterlist
101 ClusterData **currentPt; //List of pointers to the current pad
102 ClusterData **previousPt; //List of pointers to the previous pad
105 UInt_t n_previous=0,n_current=0,n_total=0;
107 //Loop over sequences of this row:
108 for(UInt_t bin=0; bin<tempPt->fNDigit; bin++)
110 AliL3DigitData *data = tempPt->fDigitData;
111 if(data[bin].fPad != last_pad)
116 if(currentPt == pad2)
126 n_previous = n_current;
128 if(bin[data].fPad != last_pad+1)
130 //this happens if there is a pad with no signal.
131 n_previous = n_current = 0;
133 last_pad = data[bin].fPad;
136 Bool_t new_cluster = kTRUE;
137 UInt_t seq_charge=0,seq_average=0,seq_error=0;
138 UInt_t last_charge=0,last_was_falling=0;
143 redo: //This is a goto.
151 last_was_falling = 0;
154 while(1) //Loop over current sequence
156 if(data[bin].fTime >= AliL3Transform::GetNTimeBins())
158 LOG(AliL3Log::kFatal,"AliL3ClustFinderNew::ProcessRow","Digits")
159 <<"Timebin out of range "<<(Int_t)data[bin].fTime<<ENDLOG;
163 //Get the current ADC-value
164 UInt_t charge = data[bin].fCharge;
168 //Check if the last pixel in the sequence is smaller than this
169 if(charge > last_charge)
177 else last_was_falling = 1; //last pixel was larger than this
178 last_charge = charge;
181 //Sum the total charge of this sequence
182 seq_charge += charge;
183 seq_average += data[bin].fTime*charge;
184 seq_error += data[bin].fTime*data[bin].fTime*charge;
186 //Check where to stop:
187 if(data[bin+1].fPad != data[bin].fPad) //new pad
189 if(data[bin+1].fTime != data[bin].fTime+1) //end of sequence
193 }//end loop over sequence
195 //Calculate mean of sequence:
198 seq_mean = seq_average/seq_charge;
201 LOG(AliL3Log::kFatal,"AliL3ClustFinderNew::ProcessRow","Data")
202 <<"Error in data given to the cluster finder"<<ENDLOG;
207 //Calculate mean in pad direction:
208 Int_t pad_mean = seq_charge*data[bin].fPad;
209 Int_t pad_error = data[bin].fPad*pad_mean;
211 //Compare with results on previous pad:
212 for(UInt_t p=0; p<n_previous; p++)
214 //dont merge sequences on the same pad twice
215 if(previousPt[p]->fLastMergedPad==data[bin].fPad) continue;
217 Int_t difference = seq_mean - previousPt[p]->fMean;
218 if(difference < -fMatch) break;
220 if(difference <= fMatch) //There is a match here!!
222 ClusterData *local = previousPt[p];
226 if(seq_charge > local->fLastCharge)
228 if(local->fChargeFalling) //The previous pad was falling
230 break; //create a new cluster
234 local->fChargeFalling = 1;
235 local->fLastCharge = seq_charge;
238 //Don't create a new cluster, because we found a match
239 new_cluster = kFALSE;
241 //Update cluster on current pad with the matching one:
242 local->fTotalCharge += seq_charge;
243 local->fPad += pad_mean;
244 local->fPad2 += pad_error;
245 local->fTime += seq_average;
246 local->fTime2 += seq_error;
247 local->fMean = seq_mean;
248 local->fFlags++; //means we have more than one pad
249 local->fLastMergedPad = data[bin].fPad;
251 currentPt[n_current] = local;
255 } //Checking for match at previous pad
256 } //Loop over results on previous pad.
260 //Start a new cluster. Add it to the clusterlist, and update
261 //the list of pointers to clusters in current pad.
262 //current pad will be previous pad on next pad.
264 //Add to the clusterlist:
265 ClusterData *tmp = &clusterlist[n_total];
266 tmp->fTotalCharge = seq_charge;
267 tmp->fPad = pad_mean;
268 tmp->fPad2 = pad_error;
269 tmp->fTime = seq_average;
270 tmp->fTime2 = seq_error;
271 tmp->fMean = seq_mean;
272 tmp->fFlags = 0; //flags for 1 pad clusters
273 tmp->fLastMergedPad = data[bin].fPad;
276 tmp->fChargeFalling = 0;
277 tmp->fLastCharge = seq_charge;
280 //Update list of pointers to previous pad:
281 currentPt[n_current] = &clusterlist[n_total];
287 if(new_bin >= 0) goto redo;
288 }//Loop over digits on this padrow
290 WriteClusters(n_total,clusterlist);
293 void AliL3ClustFinderNew::WriteClusters(Int_t n_clusters,ClusterData *list)
295 Int_t thisrow,thissector;
296 UInt_t counter = fNClusters;
298 for(int j=0; j<n_clusters; j++)
300 if(!list[j].fFlags) continue; //discard 1 pad clusters
301 if(list[j].fTotalCharge < fThreshold) continue; //noise cluster
304 Float_t fpad =(Float_t)list[j].fPad /(Float_t)list[j].fTotalCharge;
305 Float_t fpad2=fXYErr;
306 Float_t ftime =(Float_t)list[j].fTime /(Float_t)list[j].fTotalCharge;
307 Float_t ftime2=fZErr;
310 fpad2=(Float_t)list[j].fPad2/(Float_t)list[j].fTotalCharge - fpad*fpad;
312 ftime2=(Float_t)list[j].fTime2/(Float_t)list[j].fTotalCharge - ftime*ftime;
313 ftime2 = sqrt(ftime2);
317 cout<<"WriteCluster: padrow "<<fCurrentRow<<" pad "<<fpad << " +- "<<fpad2<<" time "<<ftime<<" +- "<<ftime2<<" charge "<<list[j].fTotalCharge<<endl;
319 AliL3Transform::Slice2Sector(fCurrentSlice,fCurrentRow,thissector,thisrow);
320 AliL3Transform::Raw2Local(xyz,thissector,thisrow,fpad,ftime);
321 if(xyz[0]==0) LOG(AliL3Log::kError,"AliL3ClustFinder","Cluster Finder")
322 <<AliL3Log::kDec<<"Zero cluster"<<ENDLOG;
323 if(fNClusters >= fMaxNClusters)
325 LOG(AliL3Log::kError,"AliL3ClustFinder::WriteClusters","Cluster Finder")
326 <<AliL3Log::kDec<<"Too many clusters"<<ENDLOG;
329 fSpacePointData[counter].fCharge = list[j].fTotalCharge;
330 fSpacePointData[counter].fX = xyz[0];
331 fSpacePointData[counter].fY = xyz[1];
332 fSpacePointData[counter].fZ = xyz[2];
333 fSpacePointData[counter].fPadRow = fCurrentRow;
334 fSpacePointData[counter].fXYErr = fpad2;
335 fSpacePointData[counter].fZErr = ftime2;
336 fSpacePointData[counter].fID = counter
337 +((fCurrentSlice&0x7f)<<25)+((fCurrentPatch&0x7)<<22);//Uli
340 GetTrackID((Int_t)rint(fpad),(Int_t)rint(ftime),trackID);
341 //cout<<"padrow "<<fCurrentRow<<" pad "<<(Int_t)rint(fpad)<<" time "<<(Int_t)rint(ftime)<<" Trackid "<<trackID[0]<<endl;
342 fSpacePointData[counter].fTrackID[0] = trackID[0];
343 fSpacePointData[counter].fTrackID[1] = trackID[1];
344 fSpacePointData[counter].fTrackID[2] = trackID[2];
352 void AliL3ClustFinderNew::GetTrackID(Int_t pad,Int_t time,Int_t *trackID)
354 AliL3DigitRowData *rowPt = (AliL3DigitRowData*)fDigitRowData;
356 trackID[0]=trackID[1]=trackID[2]=-2;
357 //cout<<"Looking for pad "<<pad<<" time "<<time<<endl;
358 for(Int_t i=fFirstRow; i<=fLastRow; i++)
360 if(rowPt->fRow < (UInt_t)fCurrentRow)
362 AliL3MemHandler::UpdateRowPointer(rowPt);
365 AliL3DigitData *digPt = (AliL3DigitData*)rowPt->fDigitData;
366 for(UInt_t j=0; j<rowPt->fNDigit; j++)
368 Int_t cpad = digPt[j].fPad;
369 Int_t ctime = digPt[j].fTime;
370 if(cpad != pad) continue;
371 if(ctime != time) continue;
372 //if(cpad != pad && ctime != ctime) continue;
373 //cout<<"Reading row "<<fCurrentRow<<" pad "<<cpad<<" time "<<ctime<<" trackID "<<digPt[j].fTrackID[0]<<endl;
374 trackID[0] = digPt[j].fTrackID[0];
375 trackID[1] = digPt[j].fTrackID[1];
376 trackID[2] = digPt[j].fTrackID[2];
378 //cout<<"Reading trackID "<<trackID[0]<<endl;