A new improved cluster finder
[u/mrichter/AliRoot.git] / HLT / src / AliL3ClustFinderNew.cxx
CommitLineData
46fbeb8a 1//Author: Anders Strand Vestbo
2//Last Modified: 17.08.2001
3
4#include <iostream.h>
5#include "AliL3Logging.h"
6#include "AliL3ClustFinderNew.h"
7#include "AliL3DigitData.h"
8#include "AliL3Transform.h"
9#include "AliL3SpacePointData.h"
10
11ClassImp(AliL3ClustFinderNew)
12
13AliL3ClustFinderNew::AliL3ClustFinderNew()
14{
15 fMatch = 4;
16 fThreshold =10;
17}
18
19AliL3ClustFinderNew::AliL3ClustFinderNew(AliL3Transform *transform)
20{
21 fTransform = transform;
22 fMatch = 4;
23 fThreshold =10;
24}
25
26AliL3ClustFinderNew::~AliL3ClustFinderNew()
27{
28
29
30}
31
32void AliL3ClustFinderNew::InitSlice(Int_t slice,Int_t patch,Int_t firstrow, Int_t lastrow,Int_t nmaxpoints)
33{
34 fNClusters = 0;
35 fMaxNClusters = nmaxpoints;
36 fCurrentSlice = slice;
37 fCurrentPatch = patch;
38 fFirstRow = firstrow;
39 fLastRow = lastrow;
40}
41
42void AliL3ClustFinderNew::InitSlice(Int_t slice,Int_t patch,Int_t nmaxpoints)
43{
44 fNClusters = 0;
45 fMaxNClusters = nmaxpoints;
46 fCurrentSlice = slice;
47 fCurrentPatch = patch;
48}
49
50void AliL3ClustFinderNew::SetOutputArray(AliL3SpacePointData *pt)
51{
52
53 fSpacePointData = pt;
54}
55
56
57void AliL3ClustFinderNew::Read(UInt_t ndigits,AliL3DigitRowData *ptr)
58{
59 fNDigitRowData = ndigits;
60 fDigitRowData = ptr;
61
62}
63
64void AliL3ClustFinderNew::ProcessDigits()
65{
66 //Loop over rows, and call processrow
67
68
69 AliL3DigitRowData *tempPt = (AliL3DigitRowData*)fDigitRowData;
70
71 for(Int_t i=fFirstRow; i<=fLastRow; i++)
72 {
73 fCurrentRow = i;
74 ProcessRow(tempPt);
75 Byte_t *tmp = (Byte_t*)tempPt;
76 Int_t size = sizeof(AliL3DigitRowData) + tempPt->fNDigit*sizeof(AliL3DigitData);
77 tmp += size;
78 tempPt = (AliL3DigitRowData*)tmp;
79 }
80}
81
82
83
84void AliL3ClustFinderNew::ProcessRow(AliL3DigitRowData *tempPt)
85{
86
87 UInt_t last_pad = 123456789;
88
89 ClusterData *pad1[2500]; //2 lists for internal memory=2pads
90 ClusterData *pad2[2500]; //2 lists for internal memory=2pads
91 ClusterData clusterlist[5000]; //Clusterlist
92
93 ClusterData **currentPt; //List of pointers to the current pad
94 ClusterData **previousPt; //List of pointers to the previous pad
95 currentPt = pad2;
96 previousPt = pad1;
97 UInt_t n_previous=0,n_current=0,n_total=0;
98
99 //Loop over sequences of this row:
100 for(UInt_t bin=0; bin<tempPt->fNDigit; bin++)
101 {
102
103 AliL3DigitData *data = tempPt->fDigitData;
104 if(data[bin].fPad != last_pad)
105 {
106 //This is a new pad
107 last_pad = data[bin].fPad;
108
109 //Switch:
110 if(currentPt == pad2)
111 {
112 currentPt = pad1;
113 previousPt = pad2;
114
115 }
116 else
117 {
118 currentPt = pad2;
119 previousPt = pad1;
120 }
121 n_previous = n_current;
122 n_current = 0;
123 }
124
125 Bool_t new_cluster = kTRUE;
126
127 UInt_t seq_charge=0,seq_average=0;
128
129 while(1) //Loop over current sequence
130 {
131 //Get the current ADC-value
132 UInt_t charge = data[bin].fCharge;
133 //Sum the total charge of this sequence
134 seq_charge += charge;
135 //printf("starting sequence pad %d time %d\n",data[bin].fPad,data[bin].fTime);
136 seq_average += data[bin].fTime*charge;
137
138 //Check where to stop:
139 if(data[bin+1].fPad != data[bin].fPad) //new pad
140 break;
141 if(data[bin+1].fTime != data[bin].fTime+1) //end of sequence
142 break;
143
144 bin++;
145 }//end loop over sequence
146
147 //Calculate mean of sequence:
148 Int_t seq_mean=0;
149 if(seq_charge)
150 seq_mean = seq_average/seq_charge;
151 else
152 printf("AliL3ClustFinderNew : NO CHARGE!!!\n");
153
154 //Calculate mean in pad direction:
155 Int_t pad_mean = seq_charge*data[bin].fPad;
156
157 //Compare with results on previous pad:
158 for(UInt_t p=0; p<n_previous; p++)
159 {
160 Int_t difference = seq_mean - previousPt[p]->fMean;
161 if(difference < -fMatch)
162 break;
163
164 if(difference <= fMatch)//There is a match here!!
165 {
166 //Don't create a new cluster, because we found a match
167 new_cluster = kFALSE;
168
169 //Update cluster on current pad with the matching one:
170 currentPt[n_current] = previousPt[p];
171
172 currentPt[n_current]->fTotalCharge += seq_charge;
173 currentPt[n_current]->fPad += pad_mean;
174 currentPt[n_current]->fTime += seq_average;
175 currentPt[n_current]->fMean = seq_mean;
176 currentPt[n_current]->fFlags++; //means we have more than one pad
177 n_current++;
178
179
180 break;
181 }//Checking for match at previous pad
182 }//Loop over results on previous pad.
183
184 if(new_cluster)
185 {
186 //Start a new cluster. Add it to the clusterlist, and update
187 //the list of pointers to clusters in current pad.
188 //->will be previous pad on next pad.
189
190 //Add to the clusterlist:
191 ClusterData *tmp = &clusterlist[n_total];
192 tmp->fTotalCharge = seq_charge;
193 tmp->fPad = pad_mean;
194 tmp->fTime = seq_average;
195 tmp->fMean = seq_mean;
196 tmp->fFlags = 0; //flags for 1 pad clusters
197
198 //Update list of pointers to previous pad:
199 currentPt[n_current] = &clusterlist[n_total];
200 n_total++;
201 n_current++;
202 }
203 }//Loop over digits on this padrow
204
205 WriteClusters(n_total,clusterlist);
206
207
208}
209
210void AliL3ClustFinderNew::WriteClusters(Int_t n_clusters,ClusterData *list)
211{
212 Int_t thisrow,thissector;
213 UInt_t counter = fNClusters;
214
215 for(int j=0; j<n_clusters; j++)
216 {
217 if(!list[j].fFlags) continue; //discard 1 pad clusters
218 if(list[j].fTotalCharge < fThreshold) continue; //noise cluster
219 Float_t xyz[3];
220
221 Float_t fpad=(Float_t)list[j].fPad/(Float_t)list[j].fTotalCharge;
222 Float_t ftime=(Float_t)list[j].fTime/(Float_t)list[j].fTotalCharge;
223 //printf("padrow %d number of pads %d totalcharge %d\n",fCurrentRow,list[j].fFlags,list[j].fTotalCharge);
224 fTransform->Slice2Sector(fCurrentSlice,fCurrentRow,thissector,thisrow);
225 fTransform->Raw2Local(xyz,thissector,thisrow,fpad,ftime);
226 if(xyz[0]==0) LOG(AliL3Log::kError,"AliL3ClustFinder","Cluster Finder")
227 <<AliL3Log::kDec<<"Zero cluster"<<ENDLOG;
228 if(fNClusters >= fMaxNClusters)
229 {
230 LOG(AliL3Log::kError,"AliL3ClustFinder::WriteClusters","Cluster Finder")
231 <<AliL3Log::kDec<<"Too many clusters"<<ENDLOG;
232 return;
233 }
234 fSpacePointData[counter].fX = xyz[0];
235 fSpacePointData[counter].fY = xyz[1];
236 fSpacePointData[counter].fZ = xyz[2];
237 fSpacePointData[counter].fPadRow = fCurrentRow;
238 fSpacePointData[counter].fXYErr = fXYErr;
239 fSpacePointData[counter].fZErr = fZErr;
240 fSpacePointData[counter].fID = counter
241 +((fCurrentSlice&0x7f)<<25)+((fCurrentPatch&0x7)<<22);//uli
242
243
244 fNClusters++;
245 counter++;
246
247 }
248
249}