]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - HLT/misc/AliL3VHDLClusterFinder.cxx
New version of SPD raw-data reconstruction. The format now correponds to the actual...
[u/mrichter/AliRoot.git] / HLT / misc / AliL3VHDLClusterFinder.cxx
index 781e9981bfbdcc27e2d1119ee35ab854469cf42e..21c28671610a71a9bc173a4eb878e13e7ccea4ca 100644 (file)
@@ -1,18 +1,9 @@
-// $Id$
-
-// Author: Constantin Loizides <mailto:loizides@ikf.physik.uni-frankfurt.de>
-//*-- Copyright & copy CL
-
-#include <math.h>
-#include <time.h>
-#include <iostream.h>
-#include <stdio.h>
-#include <string.h>
-
-#include "AliL3VHDLClusterFinder.h"
+// @(#) $Id$
 
+// Author: Constantin Loizides <mailto:loizides@ikf.uni-frankfurt.de>
+//*-- Copyright & copy ALICE HLT Group
 /** \class AliL3VHDLClusterFinder
-//<pre>
+<pre>
 //____________________________________________________
 // AliL3VHDLClusterFinder
 //
 // Most important parameters:
 // fThreshold - threshold for noise clusters
 // fMatch - length in time for overlapping sequences
-//</pre> 
+</pre> 
 */
 
+#include "AliL3StandardIncludes.h"
+#include "AliL3RootTypes.h"
+#include "AliL3Logging.h"
+#include "AliL3AltroMemHandler.h"
+
+//#define VHDLDEBUG
+#include "AliL3VHDLClusterFinder.h"
+
+#if __GNUC__ >= 3
+using namespace std;
+#endif
+
+
 ClassImp(AliL3VHDLClusterFinder)
 
 AliL3VHDLClusterFinder::AliL3VHDLClusterFinder()
 {
+  // default constructor
   fMatch = 4;
   fThreshold = 10;
   fMinMerge = 1;
@@ -42,7 +47,7 @@ AliL3VHDLClusterFinder::AliL3VHDLClusterFinder()
   fcalcerr = kTRUE;
 
   Clear();
-#ifdef DEBUG
+#ifdef VHDLDEBUG
   fdeb=fopen("vhdlclusterfinder.debug","w");
   //fdeb=stderr;
 #endif
@@ -50,7 +55,8 @@ AliL3VHDLClusterFinder::AliL3VHDLClusterFinder()
 
 AliL3VHDLClusterFinder::~AliL3VHDLClusterFinder()
 {
-#ifdef DEBUG
+  // destructor
+#ifdef VHDLDEBUG
   fclose(fdeb);
 #endif
 }
@@ -58,11 +64,10 @@ AliL3VHDLClusterFinder::~AliL3VHDLClusterFinder()
 void AliL3VHDLClusterFinder::ProcessDigits()
 {
   //Loop over data like the analyzer of the VHDL code
-  const UChar_t n=255;
+  const UChar_t kn=255;
   UShort_t rrow=0,rtime=0;
-  UChar_t rpad=0,i=n;
-  UShort_t *charges=new UShort_t[n];
-  Int_t tc=0,mp=0,mt=0,sp=0,st=0;
+  UChar_t rpad=0,i=kn;
+  UShort_t *charges=new UShort_t[kn];
 
   fNClusters=0;
   fRow=0;
@@ -71,13 +76,12 @@ void AliL3VHDLClusterFinder::ProcessDigits()
 
   //loop over input data
   while(fAltromem.ReadSequence(rrow,rpad,rtime,i,&charges)){
-    tc=0;mp=0;mt=0;sp=0;st=0;
 #if 0
     cout << "Padrow " << (int)rrow << " pad " << (int)rpad << " time " <<(int)rtime << " charges ";
     for(UChar_t ii=0;ii<i;ii++) cout << (int)charges[ii] << " ";
     cout << endl;
 #endif
-#ifdef DEBUG
+#ifdef VHDLDEBUG
     fprintf(fdeb,"ProcessDigits: Input Data: %d %d %d charges:",(int)rrow,(int)rpad,(int)rtime);
     for(UChar_t ii=0;ii<i;ii++) fprintf(fdeb," %d",(int)charges[ii]);
     fprintf(fdeb,"\n");
@@ -85,38 +89,53 @@ void AliL3VHDLClusterFinder::ProcessDigits()
 
     fNRow=rrow;
     fNPad=rpad;
+    fTC=0,fMT=0,fST=0;
 
     //calculate sequence values 
-    //no deconvulution so far
-    for(UChar_t ii=0;ii<i;ii++){
-      tc+=charges[ii];
-      mt+=(rtime-ii)*charges[ii];
-      st+=(rtime-ii)*(rtime-ii)*charges[ii];
+    if(fDeconvTime){
+      UChar_t ii=0;
+      Int_t sl=0;
+      Int_t charge=0,lcharge=0;
+      Bool_t falling=kFALSE;
+      while(ii<i){
+       charge=charges[ii];
+       if((falling)&&(charge>lcharge)){
+         fSM=rtime-sl/2;
+         MakeSequence();
+         ProcessSequence();
+         OutputMemory();
+
+         rtime=rtime-sl;      
+         falling=kFALSE;
+         sl=0;
+         fTC=0,fMT=0,fST=0;
+       } else if(charge<lcharge) falling=kTRUE;
+
+       fTC+=charge;
+       fMT+=(rtime-sl)*charge;
+       fST+=(rtime-sl)*(rtime-sl)*charge;
+       sl++;
+       ii++;
+       lcharge=charge;
+      } //end loop over sequence
+      fSM=rtime-sl/2;
+    } else { /* no deconvolution */
+      for(UChar_t ii=0;ii<i;ii++){
+       fTC+=charges[ii];
+       fMT+=(rtime-ii)*charges[ii];
+       fST+=(rtime-ii)*(rtime-ii)*charges[ii];
+      }
+      fSM=rtime-i/2;
     }
-    mp=rpad*tc;
-    sp=rpad*rpad*tc;
-
-    fSeq.fTotalCharge=tc;
-    fSeq.fPad=mp;
-    fSeq.fTime=mt;
-    fSeq.fPad2=sp;
-    fSeq.fTime2=st; 
-    fSeq.fMean=0;
-    //if(tc!=0) fSeq.fMean=mt/tc;
-    if(tc!=0) fSeq.fMean=rtime-i/2;    
-    fSeq.fMerge=0;
-    fSeq.fRow=rrow;
-    fSeq.fLastPad=rpad;
-    
-    //work on this sequence
+
+    MakeSequence();
     ProcessSequence();
-    //output one cluster
-    OutputMemory();
+    OutputMemory();      
 
-#ifdef DEBUG
+#ifdef VHDLDEBUG
     fflush(fdeb);
 #endif
-    i=n; //store size of charges array
+    i=kn; //store size of charges array
   } //loop over data
 
   //flush everything left
@@ -124,8 +143,31 @@ void AliL3VHDLClusterFinder::ProcessDigits()
   while(fOP!=fFP) OutputMemory();
 }
 
+void AliL3VHDLClusterFinder::MakeSequence(){
+  // makes the sequence
+  if(!fTC) return;
+
+  Int_t mp=fNPad*fTC;
+  Int_t sp=fNPad*fNPad*fTC;
+
+  fSeq.fTotalCharge=fTC;
+  fSeq.fPad=mp;
+  fSeq.fTime=fMT;
+  fSeq.fPad2=sp;
+  fSeq.fTime2=fST; 
+  fSeq.fMean=0;
+  fSeq.fMean=fSM;
+  fSeq.fMerge=0;
+  fSeq.fRow=fNRow;
+  fSeq.fLastPad=fNPad;
+  fSeq.fChargeFalling=0;
+  if(fDeconvPad) fSeq.fLastCharge=fTC;
+  else fSeq.fLastCharge=0;
+}
+
 void AliL3VHDLClusterFinder::ProcessSequence()
 {
+  // processes the sequence
   if(fNRow!=fRow) FlushMemory();
   else if(fNPad==fPad+1) PrepareMemory();
   else if(fNPad!=fPad) FlushMemory();
@@ -134,7 +176,7 @@ void AliL3VHDLClusterFinder::ProcessSequence()
   fRow=fNRow;
   fPad=fNPad;
 
-#ifdef DEBUG
+#ifdef VHDLDEBUG
   fprintf(fdeb,"ProcessSequence: Mean=%d TC=%d ",fSeq.fMean,fSeq.fTotalCharge);
 #endif
 
@@ -143,7 +185,8 @@ void AliL3VHDLClusterFinder::ProcessSequence()
 
 void AliL3VHDLClusterFinder::PrepareMemory()
 {
-#ifdef DEBUG
+  // prepares the memory
+#ifdef VHDLDEBUG
   fprintf(fdeb,"PrepareMemory %d %d %d\n",fRP,fEP,fWP);
 #endif
   fRP=fEP;
@@ -152,7 +195,8 @@ void AliL3VHDLClusterFinder::PrepareMemory()
 
 void AliL3VHDLClusterFinder::FlushMemory()
 {
-#ifdef DEBUG
+  // flushes the memory
+#ifdef VHDLDEBUG
   fprintf(fdeb,"FlushMemory %d %d %d %d\n",fFP,fRP,fEP,fWP);
 #endif
   fFP=fWP;
@@ -162,7 +206,7 @@ void AliL3VHDLClusterFinder::FlushMemory()
 
 void AliL3VHDLClusterFinder::CompareSeq()
 {
-
+  // compares sequences
   while(fRP!=fEP){
     Int_t diff=fSeqs[fPList[fRP]].fMean-fSeq.fMean;
 
@@ -171,8 +215,7 @@ void AliL3VHDLClusterFinder::CompareSeq()
       continue;
     } else if(diff<-fMatch){ //no match
       break;                 //insert new cluster       
-    }
-    else { //match found, merge it
+    } else { //match found, merge it
       MergeSeq(); 
       return;
     }
@@ -183,10 +226,11 @@ void AliL3VHDLClusterFinder::CompareSeq()
 
 void AliL3VHDLClusterFinder::MergeSeq()
 {
-#ifdef DEBUG
+  // merges sequences
+#ifdef VHDLDEBUG
   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);
 #endif
-  if(fSeqs[fPList[fRP]].fRow==fSeq.fRow){
+  if(fSeqs[fPList[fRP]].fRow!=fSeq.fRow){
     LOG(AliL3Log::kWarning,"AliL3VHDLClusterFinder::","Memory Check")
       <<"Sequences can be merged on the same rows only."<<ENDLOG;
   }
@@ -194,7 +238,20 @@ void AliL3VHDLClusterFinder::MergeSeq()
     LOG(AliL3Log::kWarning,"AliL3VHDLClusterFinder::","Memory Check")
       <<"Sequences can be merged on consecutive pads only."<<ENDLOG;
   }
+  
+  if(fDeconvPad){
+    if(fSeq.fTotalCharge > fSeqs[fPList[fRP]].fLastCharge){
+      if(fSeqs[fPList[fRP]].fChargeFalling){ //The previous pad was falling
+       IncRPointer();
+       InsertSeq(); //start a new cluster
+       return;
+      }                    
+    }
+    else fSeqs[fPList[fRP]].fChargeFalling = 1;
 
+    fSeqs[fPList[fRP]].fLastCharge = fSeq.fTotalCharge;
+  }
+  
   fSeqs[fPList[fRP]].fMean=fSeq.fMean; //take the new mean
   fSeqs[fPList[fRP]].fLastPad=fSeq.fLastPad;
   fSeqs[fPList[fRP]].fTotalCharge+=fSeq.fTotalCharge;
@@ -213,7 +270,8 @@ void AliL3VHDLClusterFinder::MergeSeq()
 
 void AliL3VHDLClusterFinder::InsertSeq()
 {
-#ifdef DEBUG
+  // inserts sequence
+#ifdef VHDLDEBUG
   fprintf(fdeb,"inserted %d %d\n",fRow,fPad);
 #endif
   NextFreeIndex();    //get next index
@@ -224,6 +282,7 @@ void AliL3VHDLClusterFinder::InsertSeq()
 
 void AliL3VHDLClusterFinder::OutputMemory()
 {
+  // output memory?
   Float_t mtime=0,mpad=0;
   Float_t mtime2=0,mpad2=0;
   UInt_t tc,row,mno;
@@ -251,7 +310,7 @@ void AliL3VHDLClusterFinder::OutputMemory()
     }
 
     if(mno<fMinMerge){ //noise cluster
-#ifdef DEBUG
+#ifdef VHDLDEBUG
     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);
 #endif          
       FreeSeq(index);  
@@ -260,7 +319,7 @@ void AliL3VHDLClusterFinder::OutputMemory()
     }
 
     if(tc<fThreshold){ //total charge below threshold
-#ifdef DEBUG
+#ifdef VHDLDEBUG
     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);
 #endif          
       FreeSeq(index);
@@ -268,26 +327,28 @@ void AliL3VHDLClusterFinder::OutputMemory()
       //continue;
     }
 
-#ifdef DEBUG
+#ifdef VHDLDEBUG
     fprintf(fdeb,"OutputMemory %d: Row %d Pad %.3f Time %.3f TC %d Merge %d\n",fNClusters,row,mpad,mtime,tc,mno);
 #endif    
 
-    if(stdout)
-      cout<<"WriteCluster: padrow "<<row<<" pad "<<mpad<< " +- "<<mpad2<<" time "<<mtime<<" +- "<<mtime2<<" charge "<<tc<<endl;
+    //    if(stdout)
+    //      cout<<"WriteCluster: padrow "<<row<<" pad "<<mpad<< " +- "<<mpad2<<" time "<<mtime<<" +- "<<mtime2<<" charge "<<tc<<endl;
     
     fNClusters++;
     FreeSeq(index);
   }
 }
 
-inline void AliL3VHDLClusterFinder::FreeSeq(UShort_t i)
+void AliL3VHDLClusterFinder::FreeSeq(UShort_t i)
 {
+  // frees the sequence
   ClearSeq(i);
   IncPointer(fLast,1,N_clmem);
 }
 
 void AliL3VHDLClusterFinder::Clear()
 {
+  // clears everything
   fFirst=0; //first list pointer
   fLast=0;  //last  list pointer
 
@@ -312,6 +373,7 @@ void AliL3VHDLClusterFinder::Clear()
 }
 
 void AliL3VHDLClusterFinder::ClearSeq(UShort_t i){
+  // clears a sequence
   fSeqs[i].fRow=0;
   fSeqs[i].fLastPad=0;
   fSeqs[i].fTotalCharge=0;
@@ -324,6 +386,7 @@ void AliL3VHDLClusterFinder::ClearSeq(UShort_t i){
 }
 
 void AliL3VHDLClusterFinder::IncPointer(UShort_t &p, Short_t add, UShort_t N){
+  // increments pointer by 'add' in a circular buffer 
   Short_t pp=p;
   pp+=add;  
   if(pp>=N) p=UShort_t(pp-N);
@@ -331,11 +394,13 @@ void AliL3VHDLClusterFinder::IncPointer(UShort_t &p, Short_t add, UShort_t N){
   else p=UShort_t(pp);
 }
 
-inline void AliL3VHDLClusterFinder::IncRPointer(){
+void AliL3VHDLClusterFinder::IncRPointer(){
+  // increments pointer fRP 
   IncPointer(fRP);
 }
 
-inline void AliL3VHDLClusterFinder::IncWPointer(){
+void AliL3VHDLClusterFinder::IncWPointer(){
+  // increments pointer fWP
   IncPointer(fWP);
 
   if(fWP==fOP){
@@ -344,7 +409,8 @@ inline void AliL3VHDLClusterFinder::IncWPointer(){
   }
 }
 
-inline void AliL3VHDLClusterFinder::NextFreeIndex(){
+void AliL3VHDLClusterFinder::NextFreeIndex(){
+  // finds next free index
   IncPointer(fFirst,1,N_clmem);
   if(fFirst==fLast) {
     LOG(AliL3Log::kFatal,"AliL3VHDLClusterFinder::GetFreeIndex","Memory Check")
@@ -355,6 +421,7 @@ inline void AliL3VHDLClusterFinder::NextFreeIndex(){
 #if 0
 void AliL3ClustFinderNew::WriteClusters(Int_t n_clusters,ClusterData *list)
 {
+  // writes clusters
   Int_t thisrow,thissector;
   UInt_t counter = fNClusters;