3 // Author: Constantin Loizides <mailto:loizides@ikf.physik.uni-frankfurt.de>
4 //*-- Copyright & copy CL
6 #include "AliL3StandardIncludes.h"
8 #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
29 ClassImp(AliL3VHDLClusterFinder)
31 AliL3VHDLClusterFinder::AliL3VHDLClusterFinder()
47 fdeb=fopen("vhdlclusterfinder.debug","w");
52 AliL3VHDLClusterFinder::~AliL3VHDLClusterFinder()
59 void AliL3VHDLClusterFinder::ProcessDigits()
61 //Loop over data like the analyzer of the VHDL code
63 UShort_t rrow=0,rtime=0;
65 UShort_t *charges=new UShort_t[n];
72 //loop over input data
73 while(fAltromem.ReadSequence(rrow,rpad,rtime,i,&charges)){
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]);
89 //calculate sequence values
93 Int_t charge=0,lcharge=0;
94 Bool_t falling=kFALSE;
97 if((falling)&&(charge>lcharge)){
107 } else if(charge<lcharge) falling=kTRUE;
110 fMT+=(rtime-sl)*charge;
111 fST+=(rtime-sl)*(rtime-sl)*charge;
115 } //end loop over sequence
117 } else { /* no deconvolution */
118 for(UChar_t ii=0;ii<i;ii++){
120 fMT+=(rtime-ii)*charges[ii];
121 fST+=(rtime-ii)*(rtime-ii)*charges[ii];
133 i=n; //store size of charges array
136 //flush everything left
138 while(fOP!=fFP) OutputMemory();
141 void AliL3VHDLClusterFinder::MakeSequence(){
145 Int_t sp=fNPad*fNPad*fTC;
147 fSeq.fTotalCharge=fTC;
157 fSeq.fChargeFalling=0;
158 if(fDeconvPad) fSeq.fLastCharge=fTC;
159 else fSeq.fLastCharge=0;
162 void AliL3VHDLClusterFinder::ProcessSequence()
164 if(fNRow!=fRow) FlushMemory();
165 else if(fNPad==fPad+1) PrepareMemory();
166 else if(fNPad!=fPad) FlushMemory();
168 //store new row and pad values
173 fprintf(fdeb,"ProcessSequence: Mean=%d TC=%d ",fSeq.fMean,fSeq.fTotalCharge);
176 CompareSeq(); //merge or insert
179 void AliL3VHDLClusterFinder::PrepareMemory()
182 fprintf(fdeb,"PrepareMemory %d %d %d\n",fRP,fEP,fWP);
188 void AliL3VHDLClusterFinder::FlushMemory()
191 fprintf(fdeb,"FlushMemory %d %d %d %d\n",fFP,fRP,fEP,fWP);
198 void AliL3VHDLClusterFinder::CompareSeq()
201 Int_t diff=fSeqs[fPList[fRP]].fMean-fSeq.fMean;
203 if(diff>fMatch){ //no match
204 IncRPointer(); //cluster finished
206 } else if(diff<-fMatch){ //no match
207 break; //insert new cluster
208 } else { //match found, merge it
214 InsertSeq(); //start new cluster
217 void AliL3VHDLClusterFinder::MergeSeq()
220 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);
222 if(fSeqs[fPList[fRP]].fRow!=fSeq.fRow){
223 LOG(AliL3Log::kWarning,"AliL3VHDLClusterFinder::","Memory Check")
224 <<"Sequences can be merged on the same rows only."<<ENDLOG;
226 if(fSeqs[fPList[fRP]].fLastPad+1!=fSeq.fLastPad){
227 LOG(AliL3Log::kWarning,"AliL3VHDLClusterFinder::","Memory Check")
228 <<"Sequences can be merged on consecutive pads only."<<ENDLOG;
232 if(fSeq.fTotalCharge > fSeqs[fPList[fRP]].fLastCharge){
233 if(fSeqs[fPList[fRP]].fChargeFalling){ //The previous pad was falling
235 InsertSeq(); //start a new cluster
239 else fSeqs[fPList[fRP]].fChargeFalling = 1;
241 fSeqs[fPList[fRP]].fLastCharge = fSeq.fTotalCharge;
244 fSeqs[fPList[fRP]].fMean=fSeq.fMean; //take the new mean
245 fSeqs[fPList[fRP]].fLastPad=fSeq.fLastPad;
246 fSeqs[fPList[fRP]].fTotalCharge+=fSeq.fTotalCharge;
247 fSeqs[fPList[fRP]].fPad+=fSeq.fPad;
248 fSeqs[fPList[fRP]].fTime+=fSeq.fTime;
249 fSeqs[fPList[fRP]].fPad2+=fSeq.fPad2;
250 fSeqs[fPList[fRP]].fTime2+=fSeq.fTime2;
251 fSeqs[fPList[fRP]].fMerge+=1;
253 fPList[fWP]=fPList[fRP];
254 fPList[fRP]=N_clmem; //mark it to be free
260 void AliL3VHDLClusterFinder::InsertSeq()
263 fprintf(fdeb,"inserted %d %d\n",fRow,fPad);
265 NextFreeIndex(); //get next index
266 fSeqs[fFirst]=fSeq; //store data
267 fPList[fWP]=fFirst; //store index
271 void AliL3VHDLClusterFinder::OutputMemory()
273 Float_t mtime=0,mpad=0;
274 Float_t mtime2=0,mpad2=0;
280 UInt_t index=fPList[fOP];
282 //cout << fOP << " - " << fFP << " - " << index << endl;
284 if(index>=N_clmem) return; //nothing to do
285 //if(index>=N_clmem) continue; //nothing to do
287 tc=fSeqs[index].fTotalCharge;
288 mno=fSeqs[index].fMerge;
289 row=fSeqs[index].fRow;
291 mtime=(Float_t)(fSeqs[index].fTime)/tc;
292 mtime2=sqrt((Float_t)(fSeqs[index].fTime2)/tc-mtime*mtime);
295 mpad=(Float_t)(fSeqs[index].fPad)/tc;
296 mpad2=sqrt((Float_t)(fSeqs[index].fPad2)/tc-mpad*mpad);
299 if(mno<fMinMerge){ //noise cluster
301 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);
308 if(tc<fThreshold){ //total charge below threshold
310 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);
318 fprintf(fdeb,"OutputMemory %d: Row %d Pad %.3f Time %.3f TC %d Merge %d\n",fNClusters,row,mpad,mtime,tc,mno);
322 cout<<"WriteCluster: padrow "<<row<<" pad "<<mpad<< " +- "<<mpad2<<" time "<<mtime<<" +- "<<mtime2<<" charge "<<tc<<endl;
329 inline void AliL3VHDLClusterFinder::FreeSeq(UShort_t i)
332 IncPointer(fLast,1,N_clmem);
335 void AliL3VHDLClusterFinder::Clear()
337 fFirst=0; //first list pointer
338 fLast=0; //last list pointer
340 fWP=0; //write pointer
341 fRP=0; //read pointer
342 fEP=0; //end read pointer
343 fFP=0; //end flush pointer
344 fOP=0; //output pointer
356 for(UInt_t i=0;i<N_mem;i++) fPList[i]=N_clmem;
357 for(UInt_t i=0;i<N_clmem;i++) ClearSeq(i);
360 void AliL3VHDLClusterFinder::ClearSeq(UShort_t i){
363 fSeqs[i].fTotalCharge=0;
372 void AliL3VHDLClusterFinder::IncPointer(UShort_t &p, Short_t add, UShort_t N){
375 if(pp>=N) p=UShort_t(pp-N);
376 else if(pp<0) p=UShort_t(pp+N);
380 inline void AliL3VHDLClusterFinder::IncRPointer(){
384 inline void AliL3VHDLClusterFinder::IncWPointer(){
388 LOG(AliL3Log::kWarning,"AliL3VHDLClusterFinder::IncWPointer","Memory Check")
389 <<"Write pointer overwrites output pointer."<<ENDLOG;
393 inline void AliL3VHDLClusterFinder::NextFreeIndex(){
394 IncPointer(fFirst,1,N_clmem);
396 LOG(AliL3Log::kFatal,"AliL3VHDLClusterFinder::GetFreeIndex","Memory Check")
397 <<"No space left in sequence list: "<<fFirst<<"=="<<fLast<<ENDLOG;
402 void AliL3ClustFinderNew::WriteClusters(Int_t n_clusters,ClusterData *list)
404 Int_t thisrow,thissector;
405 UInt_t counter = fNClusters;
407 for(int j=0; j<n_clusters; j++)
409 if(!list[j].fFlags) continue; //discard 1 pad clusters
410 if(list[j].fTotalCharge < fThreshold) continue; //noise cluster
413 Float_t fpad =(Float_t)list[j].fPad /(Float_t)list[j].fTotalCharge;
414 Float_t fpad2=fXYErr;
415 Float_t ftime =(Float_t)list[j].fTime /(Float_t)list[j].fTotalCharge;
416 Float_t ftime2=fZErr;
419 fpad2=(Float_t)list[j].fPad2/(Float_t)list[j].fTotalCharge - fpad*fpad;
421 ftime2=(Float_t)list[j].fTime2/(Float_t)list[j].fTotalCharge - ftime*ftime;
422 ftime2 = sqrt(ftime2);
426 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);
430 if(xyz[0]==0) LOG(AliL3Log::kError,"AliL3ClustFinder","Cluster Finder")
431 <<AliL3Log::kDec<<"Zero cluster"<<ENDLOG;
432 if(fNClusters >= fMaxNClusters)
434 LOG(AliL3Log::kError,"AliL3ClustFinder::WriteClusters","Cluster Finder")
435 <<AliL3Log::kDec<<"Too many clusters"<<ENDLOG;
438 fSpacePointData[counter].fCharge = list[j].fTotalCharge;
439 fSpacePointData[counter].fX = xyz[0];
440 fSpacePointData[counter].fY = xyz[1];
441 fSpacePointData[counter].fZ = xyz[2];
442 fSpacePointData[counter].fPadRow = fCurrentRow;
443 fSpacePointData[counter].fXYErr = fpad2;
444 fSpacePointData[counter].fZErr = ftime2;
445 fSpacePointData[counter].fID = counter
446 +((fCurrentSlice&0x7f)<<25)+((fCurrentPatch&0x7)<<22);//Uli