2 // Author: Constantin Loizides <mailto:loizides@ikf.physik.uni-frankfurt.de>
11 #include "AliL3VHDLClusterFinder.h"
13 /** \class AliL3VHDLClusterFinder
15 //____________________________________________________
16 // AliL3VHDLClusterFinder
18 // The current VHDL cluster finder for HLT
21 // Most important parameters:
22 // fThreshold - threshold for noise clusters
23 // fMatch - length in time for overlapping sequences
27 ClassImp(AliL3VHDLClusterFinder)
29 AliL3VHDLClusterFinder::AliL3VHDLClusterFinder()
45 fdeb=fopen("vhdlclusterfinder.debug","w");
50 AliL3VHDLClusterFinder::~AliL3VHDLClusterFinder()
57 void AliL3VHDLClusterFinder::ProcessDigits()
59 //Loop over data like the analyzer of the VHDL code
61 UShort_t rrow=0,rtime=0;
63 UShort_t *charges=new UShort_t[n];
64 Int_t tc=0,mp=0,mt=0,sp=0,st=0;
71 //loop over input data
72 while(fAltromem.ReadSequence(rrow,rpad,rtime,i,&charges)){
73 tc=0;mp=0;mt=0;sp=0;st=0;
75 cout << "Padrow " << (int)rrow << " pad " << (int)rpad << " time " <<(int)rtime << " charges ";
76 for(UChar_t ii=0;ii<i;ii++) cout << (int)charges[ii] << " ";
80 fprintf(fdeb,"ProcessDigits: Input Data: %d %d %d charges:",(int)rrow,(int)rpad,(int)rtime);
81 for(UChar_t ii=0;ii<i;ii++) fprintf(fdeb," %d",(int)charges[ii]);
88 //calculate sequence values
89 //no deconvulution so far
90 for(UChar_t ii=0;ii<i;ii++){
92 mt+=(rtime-ii)*charges[ii];
93 st+=(rtime-ii)*(rtime-ii)*charges[ii];
104 //if(tc!=0) fSeq.fMean=mt/tc;
105 if(tc!=0) fSeq.fMean=rtime-i/2;
110 //work on this sequence
118 i=n; //store size of charges array
121 //flush everything left
123 while(fOP!=fFP) OutputMemory();
126 void AliL3VHDLClusterFinder::ProcessSequence()
128 if(fNRow!=fRow) FlushMemory();
129 else if(fNPad==fPad+1) PrepareMemory();
130 else if(fNPad!=fPad) FlushMemory();
132 //store new row and pad values
137 fprintf(fdeb,"ProcessSequence: Mean=%d TC=%d ",fSeq.fMean,fSeq.fTotalCharge);
140 CompareSeq(); //merge or insert
143 void AliL3VHDLClusterFinder::PrepareMemory()
146 fprintf(fdeb,"PrepareMemory %d %d %d\n",fRP,fEP,fWP);
152 void AliL3VHDLClusterFinder::FlushMemory()
155 fprintf(fdeb,"FlushMemory %d %d %d %d\n",fFP,fRP,fEP,fWP);
162 void AliL3VHDLClusterFinder::CompareSeq()
166 Int_t diff=fSeqs[fPList[fRP]].fMean-fSeq.fMean;
168 if(diff>fMatch){ //no match
169 IncRPointer(); //cluster finished
171 } else if(diff<-fMatch){ //no match
172 break; //insert new cluster
174 else { //match found, merge it
180 InsertSeq(); //start new cluster
183 void AliL3VHDLClusterFinder::MergeSeq()
186 fprintf(fdeb,"merged with Mean=%d TC=%d (new Merge=%d) %d %d\n",fSeqs[fPList[fRP]].fMean,fSeqs[fPList[fRP]].fTotalCharge,fSeqs[fPList[fRP]].fMerge+1,fRow,fPad);
188 if(fSeqs[fPList[fRP]].fRow==fSeq.fRow){
189 LOG(AliL3Log::kWarning,"AliL3VHDLClusterFinder::","Memory Check")
190 <<"Sequences can be merged on the same rows only."<<ENDLOG;
192 if(fSeqs[fPList[fRP]].fLastPad+1!=fSeq.fLastPad){
193 LOG(AliL3Log::kWarning,"AliL3VHDLClusterFinder::","Memory Check")
194 <<"Sequences can be merged on consecutive pads only."<<ENDLOG;
197 fSeqs[fPList[fRP]].fMean=fSeq.fMean; //take the new mean
198 fSeqs[fPList[fRP]].fLastPad=fSeq.fLastPad;
199 fSeqs[fPList[fRP]].fTotalCharge+=fSeq.fTotalCharge;
200 fSeqs[fPList[fRP]].fPad+=fSeq.fPad;
201 fSeqs[fPList[fRP]].fTime+=fSeq.fTime;
202 fSeqs[fPList[fRP]].fPad2+=fSeq.fPad2;
203 fSeqs[fPList[fRP]].fTime2+=fSeq.fTime2;
204 fSeqs[fPList[fRP]].fMerge+=1;
206 fPList[fWP]=fPList[fRP];
207 fPList[fRP]=N_clmem; //mark it to be free
213 void AliL3VHDLClusterFinder::InsertSeq()
216 fprintf(fdeb,"inserted %d %d\n",fRow,fPad);
218 NextFreeIndex(); //get next index
219 fSeqs[fFirst]=fSeq; //store data
220 fPList[fWP]=fFirst; //store index
224 void AliL3VHDLClusterFinder::OutputMemory()
226 Float_t mtime=0,mpad=0;
227 Float_t mtime2=0,mpad2=0;
233 UInt_t index=fPList[fOP];
235 //cout << fOP << " - " << fFP << " - " << index << endl;
237 if(index>=N_clmem) return; //nothing to do
238 //if(index>=N_clmem) continue; //nothing to do
240 tc=fSeqs[index].fTotalCharge;
241 mno=fSeqs[index].fMerge;
242 row=fSeqs[index].fRow;
244 mtime=(Float_t)(fSeqs[index].fTime)/tc;
245 mtime2=sqrt((Float_t)(fSeqs[index].fTime2)/tc-mtime*mtime);
248 mpad=(Float_t)(fSeqs[index].fPad)/tc;
249 mpad2=sqrt((Float_t)(fSeqs[index].fPad2)/tc-mpad*mpad);
252 if(mno<fMinMerge){ //noise cluster
254 fprintf(fdeb,"OutputMemory %d %d: noise cluster (merge): Row %d Pad %.3f Time %.3f TC %d Merge %d\n",fOP,fFP,row,mpad,mtime,tc,mno);
261 if(tc<fThreshold){ //total charge below threshold
263 fprintf(fdeb,"OutputMemory %d %d: noise cluster (threshold): Row %d Pad %.3f Time %.3f TC %d Merge %d\n",fOP,fFP,row,mpad,mtime,tc,mno);
271 fprintf(fdeb,"OutputMemory %d: Row %d Pad %.3f Time %.3f TC %d Merge %d\n",fNClusters,row,mpad,mtime,tc,mno);
275 cout<<"WriteCluster: padrow "<<row<<" pad "<<mpad<< " +- "<<mpad2<<" time "<<mtime<<" +- "<<mtime2<<" charge "<<tc<<endl;
282 inline void AliL3VHDLClusterFinder::FreeSeq(UShort_t i)
285 IncPointer(fLast,1,N_clmem);
288 void AliL3VHDLClusterFinder::Clear()
290 fFirst=0; //first list pointer
291 fLast=0; //last list pointer
293 fWP=0; //write pointer
294 fRP=0; //read pointer
295 fEP=0; //end read pointer
296 fFP=0; //end flush pointer
297 fOP=0; //output pointer
309 for(UInt_t i=0;i<N_mem;i++) fPList[i]=N_clmem;
310 for(UInt_t i=0;i<N_clmem;i++) ClearSeq(i);
313 void AliL3VHDLClusterFinder::ClearSeq(UShort_t i){
316 fSeqs[i].fTotalCharge=0;
325 void AliL3VHDLClusterFinder::IncPointer(UShort_t &p, Short_t add, UShort_t N){
328 if(pp>=N) p=UShort_t(pp-N);
329 else if(pp<0) p=UShort_t(pp+N);
333 inline void AliL3VHDLClusterFinder::IncRPointer(){
337 inline void AliL3VHDLClusterFinder::IncWPointer(){
341 LOG(AliL3Log::kWarning,"AliL3VHDLClusterFinder::IncWPointer","Memory Check")
342 <<"Write pointer overwrites output pointer."<<ENDLOG;
346 inline void AliL3VHDLClusterFinder::NextFreeIndex(){
347 IncPointer(fFirst,1,N_clmem);
349 LOG(AliL3Log::kFatal,"AliL3VHDLClusterFinder::GetFreeIndex","Memory Check")
350 <<"No space left in sequence list: "<<fFirst<<"=="<<fLast<<ENDLOG;
355 void AliL3ClustFinderNew::WriteClusters(Int_t n_clusters,ClusterData *list)
357 Int_t thisrow,thissector;
358 UInt_t counter = fNClusters;
360 for(int j=0; j<n_clusters; j++)
362 if(!list[j].fFlags) continue; //discard 1 pad clusters
363 if(list[j].fTotalCharge < fThreshold) continue; //noise cluster
366 Float_t fpad =(Float_t)list[j].fPad /(Float_t)list[j].fTotalCharge;
367 Float_t fpad2=fXYErr;
368 Float_t ftime =(Float_t)list[j].fTime /(Float_t)list[j].fTotalCharge;
369 Float_t ftime2=fZErr;
372 fpad2=(Float_t)list[j].fPad2/(Float_t)list[j].fTotalCharge - fpad*fpad;
374 ftime2=(Float_t)list[j].fTime2/(Float_t)list[j].fTotalCharge - ftime*ftime;
375 ftime2 = sqrt(ftime2);
379 cout<<"WriteCluster: padrow "<<fCurrentRow<<" pad "<<fpad << "+-"<<fpad2<<" time "<<ftime<<"+-"<<ftime2<<" charge "<<list[j].fTotalCharge<<endl;
381 AliL3Transform::Slice2Sector(fCurrentSlice,fCurrentRow,thissector,thisrow);
382 AliL3Transform::Raw2Local(xyz,thissector,thisrow,fpad,ftime);
383 if(xyz[0]==0) LOG(AliL3Log::kError,"AliL3ClustFinder","Cluster Finder")
384 <<AliL3Log::kDec<<"Zero cluster"<<ENDLOG;
385 if(fNClusters >= fMaxNClusters)
387 LOG(AliL3Log::kError,"AliL3ClustFinder::WriteClusters","Cluster Finder")
388 <<AliL3Log::kDec<<"Too many clusters"<<ENDLOG;
391 fSpacePointData[counter].fCharge = list[j].fTotalCharge;
392 fSpacePointData[counter].fX = xyz[0];
393 fSpacePointData[counter].fY = xyz[1];
394 fSpacePointData[counter].fZ = xyz[2];
395 fSpacePointData[counter].fPadRow = fCurrentRow;
396 fSpacePointData[counter].fXYErr = fpad2;
397 fSpacePointData[counter].fZErr = ftime2;
398 fSpacePointData[counter].fID = counter
399 +((fCurrentSlice&0x7f)<<25)+((fCurrentPatch&0x7)<<22);//Uli