default parameters are set for the clusterfinder
[u/mrichter/AliRoot.git] / HLT / TPCLib / HWCFemulator / AliHLTTPCHWCFProcessorUnit.cxx
1 // $Id$
2 //****************************************************************************
3 //* This file is property of and copyright by the ALICE HLT Project          * 
4 //* ALICE Experiment at CERN, All rights reserved.                           *
5 //*                                                                          *
6 //* Primary Authors: Sergey Gorbunov, Torsten Alt                            *
7 //* Developers:      Sergey Gorbunov <sergey.gorbunov@fias.uni-frankfurt.de> *
8 //*                  Torsten Alt <talt@cern.ch>                              *
9 //*                  for The ALICE HLT Project.                              *
10 //*                                                                          *
11 //* Permission to use, copy, modify and distribute this software and its     *
12 //* documentation strictly for non-commercial purposes is hereby granted     *
13 //* without fee, provided that the above copyright notice appears in all     *
14 //* copies and that both the copyright notice and this permission notice     *
15 //* appear in the supporting documentation. The authors make no claims       *
16 //* about the suitability of this software for any purpose. It is            *
17 //* provided "as is" without express or implied warranty.                    *
18 //****************************************************************************
19
20 //  @file   AliHLTTPCHWCFProcessorUnit.cxx
21 //  @author Sergey Gorbunov <sergey.gorbunov@fias.uni-frankfurt.de>
22 //  @author Torsten Alt <talt@cern.ch> 
23 //  @date   
24 //  @brief  Channel Processor unit of FPGA ClusterFinder Emulator for TPC
25 //  @brief  ( see AliHLTTPCHWCFEmulator class )
26 //  @note 
27
28 #include "AliHLTTPCHWCFProcessorUnit.h"
29 #include <iostream>
30 #include <cstdio>
31
32
33 AliHLTTPCHWCFProcessorUnit::AliHLTTPCHWCFProcessorUnit()
34   :
35   fOutput(),
36   fkBunch(0),
37   fBunchIndex(0),
38   fDeconvolute(0),
39   fSingleSeqLimit(0),
40   fUseTimeBinWindow(0),
41   fDebug(0)
42 {
43   //constructor 
44   Init();
45 }
46
47
48 AliHLTTPCHWCFProcessorUnit::~AliHLTTPCHWCFProcessorUnit()
49 {   
50   //destructor 
51 }
52
53 AliHLTTPCHWCFProcessorUnit::AliHLTTPCHWCFProcessorUnit(const AliHLTTPCHWCFProcessorUnit&)
54   :
55   fOutput(),
56   fkBunch(0),
57   fBunchIndex(0),
58   fDeconvolute(0),
59   fSingleSeqLimit(0),
60   fUseTimeBinWindow(0),
61   fDebug(0)
62 {
63   // dummy
64   Init();
65 }
66
67 AliHLTTPCHWCFProcessorUnit& AliHLTTPCHWCFProcessorUnit::operator=(const AliHLTTPCHWCFProcessorUnit&)
68 {
69   // dummy  
70   return *this;
71 }
72
73 int AliHLTTPCHWCFProcessorUnit::Init()
74 {
75   // initialise  
76
77   fkBunch = 0;
78   fBunchIndex = 0;
79   return 0;
80 }
81
82 int AliHLTTPCHWCFProcessorUnit::InputStream( const AliHLTTPCHWCFBunch *bunch )
83 {
84   // input stream of data 
85   
86   if( bunch && fDebug ){
87     printf("\nHWCF Processor: input bunch F %1d R %3d P %3d  NS %2ld:\n",
88            bunch->fFlag, bunch->fRow, bunch->fPad, bunch->fData.size());
89     for( unsigned int i=0; i<bunch->fData.size(); i++ ){
90       AliHLTTPCHWCFDigit d =  bunch->fData[i];
91       printf("   q %2d t %3d peak %2d ", d.fQ, d.fTime, d.fPeak);      
92        printf("(");
93       for( int j=0; j<3; j++ ) printf(" {%d,%2.0f}",d.fMC.fClusterID[j].fMCID, d.fMC.fClusterID[j].fWeight );
94       printf(" )\n");    
95     }
96   }
97
98   fkBunch = bunch;
99   fBunchIndex = 0;
100   return 0;
101 }
102
103 const AliHLTTPCHWCFClusterFragment *AliHLTTPCHWCFProcessorUnit::OutputStream()
104
105   // output stream of data 
106
107   //const AliHLTUInt32_t kTimeBinWindow = 5;
108   const AliHLTUInt32_t kHalfTimeBinWindow = 2;
109  
110   if( !fkBunch ) return 0;
111   
112   fOutput.fFlag = fkBunch->fFlag;
113   fOutput.fRow = fkBunch->fRow;
114   fOutput.fPad = fkBunch->fPad;
115   fOutput.fBranch = fkBunch->fBranch;
116   fOutput.fBorder = fkBunch->fBorder;
117   fOutput.fQmax = 0;
118   fOutput.fQ = 0;
119   fOutput.fT = 0;
120   fOutput.fT2 = 0;
121   fOutput.fP = 0;
122   fOutput.fP2 = 0;
123   fOutput.fTMean = 0;
124   fOutput.fMC.clear();
125   
126   if( fkBunch->fFlag==2 && fkBunch->fData.size()==1 ){ // rcu trailer word, forward it 
127     fOutput.fRow = fkBunch->fData[0].fQ;
128   }
129   
130   if( fkBunch->fFlag >1 ){
131     fkBunch = 0;    
132     return &fOutput;
133   }
134
135   if( fkBunch->fFlag < 1 ) return 0;
136   
137   while( fBunchIndex<fkBunch->fData.size() ){
138     
139     AliHLTUInt32_t iStart = fBunchIndex;
140     AliHLTUInt32_t iPeak = fBunchIndex;
141     AliHLTUInt32_t qPeak = 0;
142         
143     // find next/best peak
144     
145     for( ; fBunchIndex<fkBunch->fData.size(); fBunchIndex++ ){
146       const AliHLTTPCHWCFDigit &d = fkBunch->fData[fBunchIndex];            
147       if( d.fPeak != 1 ) continue;
148       if( fDeconvolute ){
149         iPeak = fBunchIndex;
150         qPeak = d.fQ;
151         fBunchIndex++;
152         break;
153       } else {  
154         if( d.fQ>qPeak ){
155           qPeak = d.fQ;
156           iPeak = fBunchIndex;
157         }
158       }
159     }
160     
161     if( qPeak == 0 ) return 0;
162
163     // find next minimum !!! At the moment the minimum finder is on only when no timebin window set
164
165     if( !fUseTimeBinWindow ){
166       for( ; fBunchIndex<fkBunch->fData.size(); fBunchIndex++ ){
167         if( fDeconvolute ){
168           if( fkBunch->fData[fBunchIndex].fPeak != 0 ){
169             fBunchIndex++;
170             break;
171           }
172         }
173       }
174     } else{ 
175       if( !fDeconvolute ){
176         fBunchIndex = fkBunch->fData.size();
177       } else {
178         // find next peak
179         if( fBunchIndex+1<fkBunch->fData.size() && fkBunch->fData[fBunchIndex+1].fPeak==1 ){
180           fBunchIndex = fBunchIndex+1;
181         } else  if( fBunchIndex+2<fkBunch->fData.size() && fkBunch->fData[fBunchIndex+2].fPeak==1 ){
182           fBunchIndex = fBunchIndex+1;
183         } else  if( fBunchIndex+3<fkBunch->fData.size() && fkBunch->fData[fBunchIndex+3].fPeak==1 ){
184           fBunchIndex = fBunchIndex+2;
185         } else   if( fBunchIndex+1<fkBunch->fData.size() ){
186           fBunchIndex = fBunchIndex+2;
187         } else   if( fBunchIndex<fkBunch->fData.size() ){
188           fBunchIndex = fBunchIndex+1;
189         }
190       }
191     }
192     
193     AliHLTUInt32_t iEnd = fBunchIndex;
194
195     if( fUseTimeBinWindow ){
196       if( iPeak > iStart + kHalfTimeBinWindow ) iStart = iPeak - kHalfTimeBinWindow;
197       if( iEnd  > iPeak + kHalfTimeBinWindow + 1) iEnd = iPeak + kHalfTimeBinWindow + 1;
198     }
199
200     fOutput.fQmax = qPeak*fkBunch->fGain;
201     fOutput.fQ = 0;
202     fOutput.fT = 0;
203     fOutput.fT2 = 0;
204     fOutput.fP = 0;
205     fOutput.fP2 = 0;
206     fOutput.fTMean = fkBunch->fData[iPeak].fTime;
207     fOutput.fMC.clear();
208
209     for( AliHLTUInt32_t i=iStart; i<iEnd; i++ ){
210       const AliHLTTPCHWCFDigit &d = fkBunch->fData[i];
211       AliHLTUInt64_t q = d.fQ*fkBunch->fGain;      
212       fOutput.fQ += q;
213       fOutput.fT += q*d.fTime;
214       fOutput.fT2+= q*d.fTime*d.fTime;
215       fOutput.fP += q*fkBunch->fPad;
216       fOutput.fP2+= q*fkBunch->fPad*fkBunch->fPad;
217       fOutput.fMC.push_back(d.fMC);
218     }
219     
220     if( fkBunch->fData.size()==1 && fOutput.fQ < fSingleSeqLimit ) continue;  
221   
222     return &fOutput;
223   }
224   return 0;
225 }