3 // Author: Constantin Loizides <mailto:loizides@ikf.physik.uni-frankfurt.de>
4 //*-- Copyright & copy CL
12 #include "AliL3VHDLClusterFinder.h"
14 /** \class AliL3VHDLClusterFinder
16 //____________________________________________________
17 // AliL3VHDLClusterFinder
19 // The current VHDL cluster finder for HLT
22 // Most important parameters:
23 // fThreshold - threshold for noise clusters
24 // fMatch - length in time for overlapping sequences
28 ClassImp(AliL3VHDLClusterFinder)
30 AliL3VHDLClusterFinder::AliL3VHDLClusterFinder()
46 fdeb=fopen("vhdlclusterfinder.debug","w");
51 AliL3VHDLClusterFinder::~AliL3VHDLClusterFinder()
58 void AliL3VHDLClusterFinder::ProcessDigits()
60 //Loop over data like the analyzer of the VHDL code
62 UShort_t rrow=0,rtime=0;
64 UShort_t *charges=new UShort_t[n];
65 Int_t tc=0,mp=0,mt=0,sp=0,st=0;
72 //loop over input data
73 while(fAltromem.ReadSequence(rrow,rpad,rtime,i,&charges)){
74 tc=0;mp=0;mt=0;sp=0;st=0;
76 cout << "Padrow " << (int)rrow << " pad " << (int)rpad << " time " <<(int)rtime << " charges ";
77 for(UChar_t ii=0;ii<i;ii++) cout << (int)charges[ii] << " ";
81 fprintf(fdeb,"ProcessDigits: Input Data: %d %d %d charges:",(int)rrow,(int)rpad,(int)rtime);
82 for(UChar_t ii=0;ii<i;ii++) fprintf(fdeb," %d",(int)charges[ii]);
89 //calculate sequence values
90 //no deconvulution so far
91 for(UChar_t ii=0;ii<i;ii++){
93 mt+=(rtime-ii)*charges[ii];
94 st+=(rtime-ii)*(rtime-ii)*charges[ii];
105 //if(tc!=0) fSeq.fMean=mt/tc;
106 if(tc!=0) fSeq.fMean=rtime-i/2;
111 //work on this sequence
119 i=n; //store size of charges array
122 //flush everything left
124 while(fOP!=fFP) OutputMemory();
127 void AliL3VHDLClusterFinder::ProcessSequence()
129 if(fNRow!=fRow) FlushMemory();
130 else if(fNPad==fPad+1) PrepareMemory();
131 else if(fNPad!=fPad) FlushMemory();
133 //store new row and pad values
138 fprintf(fdeb,"ProcessSequence: Mean=%d TC=%d ",fSeq.fMean,fSeq.fTotalCharge);
141 CompareSeq(); //merge or insert
144 void AliL3VHDLClusterFinder::PrepareMemory()
147 fprintf(fdeb,"PrepareMemory %d %d %d\n",fRP,fEP,fWP);
153 void AliL3VHDLClusterFinder::FlushMemory()
156 fprintf(fdeb,"FlushMemory %d %d %d %d\n",fFP,fRP,fEP,fWP);
163 void AliL3VHDLClusterFinder::CompareSeq()
167 Int_t diff=fSeqs[fPList[fRP]].fMean-fSeq.fMean;
169 if(diff>fMatch){ //no match
170 IncRPointer(); //cluster finished
172 } else if(diff<-fMatch){ //no match
173 break; //insert new cluster
175 else { //match found, merge it
181 InsertSeq(); //start new cluster
184 void AliL3VHDLClusterFinder::MergeSeq()
187 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);
189 if(fSeqs[fPList[fRP]].fRow==fSeq.fRow){
190 LOG(AliL3Log::kWarning,"AliL3VHDLClusterFinder::","Memory Check")
191 <<"Sequences can be merged on the same rows only."<<ENDLOG;
193 if(fSeqs[fPList[fRP]].fLastPad+1!=fSeq.fLastPad){
194 LOG(AliL3Log::kWarning,"AliL3VHDLClusterFinder::","Memory Check")
195 <<"Sequences can be merged on consecutive pads only."<<ENDLOG;
198 fSeqs[fPList[fRP]].fMean=fSeq.fMean; //take the new mean
199 fSeqs[fPList[fRP]].fLastPad=fSeq.fLastPad;
200 fSeqs[fPList[fRP]].fTotalCharge+=fSeq.fTotalCharge;
201 fSeqs[fPList[fRP]].fPad+=fSeq.fPad;
202 fSeqs[fPList[fRP]].fTime+=fSeq.fTime;
203 fSeqs[fPList[fRP]].fPad2+=fSeq.fPad2;
204 fSeqs[fPList[fRP]].fTime2+=fSeq.fTime2;
205 fSeqs[fPList[fRP]].fMerge+=1;
207 fPList[fWP]=fPList[fRP];
208 fPList[fRP]=N_clmem; //mark it to be free
214 void AliL3VHDLClusterFinder::InsertSeq()
217 fprintf(fdeb,"inserted %d %d\n",fRow,fPad);
219 NextFreeIndex(); //get next index
220 fSeqs[fFirst]=fSeq; //store data
221 fPList[fWP]=fFirst; //store index
225 void AliL3VHDLClusterFinder::OutputMemory()
227 Float_t mtime=0,mpad=0;
228 Float_t mtime2=0,mpad2=0;
234 UInt_t index=fPList[fOP];
236 //cout << fOP << " - " << fFP << " - " << index << endl;
238 if(index>=N_clmem) return; //nothing to do
239 //if(index>=N_clmem) continue; //nothing to do
241 tc=fSeqs[index].fTotalCharge;
242 mno=fSeqs[index].fMerge;
243 row=fSeqs[index].fRow;
245 mtime=(Float_t)(fSeqs[index].fTime)/tc;
246 mtime2=sqrt((Float_t)(fSeqs[index].fTime2)/tc-mtime*mtime);
249 mpad=(Float_t)(fSeqs[index].fPad)/tc;
250 mpad2=sqrt((Float_t)(fSeqs[index].fPad2)/tc-mpad*mpad);
253 if(mno<fMinMerge){ //noise cluster
255 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);
262 if(tc<fThreshold){ //total charge below threshold
264 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);
272 fprintf(fdeb,"OutputMemory %d: Row %d Pad %.3f Time %.3f TC %d Merge %d\n",fNClusters,row,mpad,mtime,tc,mno);
276 cout<<"WriteCluster: padrow "<<row<<" pad "<<mpad<< " +- "<<mpad2<<" time "<<mtime<<" +- "<<mtime2<<" charge "<<tc<<endl;
283 inline void AliL3VHDLClusterFinder::FreeSeq(UShort_t i)
286 IncPointer(fLast,1,N_clmem);
289 void AliL3VHDLClusterFinder::Clear()
291 fFirst=0; //first list pointer
292 fLast=0; //last list pointer
294 fWP=0; //write pointer
295 fRP=0; //read pointer
296 fEP=0; //end read pointer
297 fFP=0; //end flush pointer
298 fOP=0; //output pointer
310 for(UInt_t i=0;i<N_mem;i++) fPList[i]=N_clmem;
311 for(UInt_t i=0;i<N_clmem;i++) ClearSeq(i);
314 void AliL3VHDLClusterFinder::ClearSeq(UShort_t i){
317 fSeqs[i].fTotalCharge=0;
326 void AliL3VHDLClusterFinder::IncPointer(UShort_t &p, Short_t add, UShort_t N){
329 if(pp>=N) p=UShort_t(pp-N);
330 else if(pp<0) p=UShort_t(pp+N);
334 inline void AliL3VHDLClusterFinder::IncRPointer(){
338 inline void AliL3VHDLClusterFinder::IncWPointer(){
342 LOG(AliL3Log::kWarning,"AliL3VHDLClusterFinder::IncWPointer","Memory Check")
343 <<"Write pointer overwrites output pointer."<<ENDLOG;
347 inline void AliL3VHDLClusterFinder::NextFreeIndex(){
348 IncPointer(fFirst,1,N_clmem);
350 LOG(AliL3Log::kFatal,"AliL3VHDLClusterFinder::GetFreeIndex","Memory Check")
351 <<"No space left in sequence list: "<<fFirst<<"=="<<fLast<<ENDLOG;
356 void AliL3ClustFinderNew::WriteClusters(Int_t n_clusters,ClusterData *list)
358 Int_t thisrow,thissector;
359 UInt_t counter = fNClusters;
361 for(int j=0; j<n_clusters; j++)
363 if(!list[j].fFlags) continue; //discard 1 pad clusters
364 if(list[j].fTotalCharge < fThreshold) continue; //noise cluster
367 Float_t fpad =(Float_t)list[j].fPad /(Float_t)list[j].fTotalCharge;
368 Float_t fpad2=fXYErr;
369 Float_t ftime =(Float_t)list[j].fTime /(Float_t)list[j].fTotalCharge;
370 Float_t ftime2=fZErr;
373 fpad2=(Float_t)list[j].fPad2/(Float_t)list[j].fTotalCharge - fpad*fpad;
375 ftime2=(Float_t)list[j].fTime2/(Float_t)list[j].fTotalCharge - ftime*ftime;
376 ftime2 = sqrt(ftime2);
380 cout<<"WriteCluster: padrow "<<fCurrentRow<<" pad "<<fpad << "+-"<<fpad2<<" time "<<ftime<<"+-"<<ftime2<<" charge "<<list[j].fTotalCharge<<endl;
382 AliL3Transform::Slice2Sector(fCurrentSlice,fCurrentRow,thissector,thisrow);
383 AliL3Transform::Raw2Local(xyz,thissector,thisrow,fpad,ftime);
384 if(xyz[0]==0) LOG(AliL3Log::kError,"AliL3ClustFinder","Cluster Finder")
385 <<AliL3Log::kDec<<"Zero cluster"<<ENDLOG;
386 if(fNClusters >= fMaxNClusters)
388 LOG(AliL3Log::kError,"AliL3ClustFinder::WriteClusters","Cluster Finder")
389 <<AliL3Log::kDec<<"Too many clusters"<<ENDLOG;
392 fSpacePointData[counter].fCharge = list[j].fTotalCharge;
393 fSpacePointData[counter].fX = xyz[0];
394 fSpacePointData[counter].fY = xyz[1];
395 fSpacePointData[counter].fZ = xyz[2];
396 fSpacePointData[counter].fPadRow = fCurrentRow;
397 fSpacePointData[counter].fXYErr = fpad2;
398 fSpacePointData[counter].fZErr = ftime2;
399 fSpacePointData[counter].fID = counter
400 +((fCurrentSlice&0x7f)<<25)+((fCurrentPatch&0x7)<<22);//Uli