HWCF emulator update according to the new hardware 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(1),
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(1),
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 ", d.fQ, d.fTime);      
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         fBunchIndex++;
151         break;
152       } else {  
153         if( d.fQ>qPeak ){
154           qPeak = d.fQ;
155           iPeak = fBunchIndex;
156         }
157       }
158     }
159   
160     // find next minimum
161
162     for( ; fBunchIndex<fkBunch->fData.size(); fBunchIndex++ ){
163       if( fDeconvolute ){
164         if( fkBunch->fData[fBunchIndex].fPeak != 0 ){
165           fBunchIndex++;
166           break;
167         }
168       }
169     }
170
171     AliHLTUInt32_t iEnd = fBunchIndex;
172     
173     if( fUseTimeBinWindow ){
174       if( iPeak > iStart + kHalfTimeBinWindow ) iStart = iPeak - kHalfTimeBinWindow;
175       if( iEnd  > iPeak + kHalfTimeBinWindow ) iEnd = iPeak + kHalfTimeBinWindow;
176     }
177
178     fOutput.fQmax = 0;
179     fOutput.fQ = 0;
180     fOutput.fT = 0;
181     fOutput.fT2 = 0;
182     fOutput.fP = 0;
183     fOutput.fP2 = 0;
184     fOutput.fTMean = fkBunch->fData[iPeak].fTime;
185     fOutput.fMC.clear();
186
187     for( AliHLTUInt32_t i=iStart; i<iEnd; i++ ){
188       const AliHLTTPCHWCFDigit &d = fkBunch->fData[i];
189       AliHLTUInt64_t q = d.fQ*fkBunch->fGain;
190       if (fOutput.fQmax < q) fOutput.fQmax = q;
191       fOutput.fQ += q;
192       fOutput.fT += q*d.fTime;
193       fOutput.fT2+= q*d.fTime*d.fTime;
194       fOutput.fP += q*fkBunch->fPad;
195       fOutput.fP2+= q*fkBunch->fPad*fkBunch->fPad;
196       fOutput.fMC.push_back(d.fMC);
197     }
198     
199     if( fkBunch->fData.size()==1 && fOutput.fQ < fSingleSeqLimit ) continue;  
200   
201     return &fOutput;
202   }
203   return 0;
204 }