Added speed-up and protection suggested by Ruben in Savannah #48080
[u/mrichter/AliRoot.git] / ITS / AliITSClusterFinderV2SDD.cxx
CommitLineData
04366a57 1/**************************************************************************
2 * Copyright(c) 1998-2003, ALICE Experiment at CERN, All rights reserved. *
3 * *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
6 * *
7 * Permission to use, copy, modify and distribute this software and its *
8 * documentation strictly for non-commercial purposes is hereby granted *
9 * without fee, provided that the above copyright notice appears in all *
10 * copies and that both the copyright notice and this permission notice *
11 * appear in the supporting documentation. The authors make no claims *
12 * about the suitability of this software for any purpose. It is *
13 * provided "as is" without express or implied warranty. *
14 **************************************************************************/
d76cb3ad 15
16/* $Id$*/
17
04366a57 18////////////////////////////////////////////////////////////////////////////
19// Implementation of the ITS clusterer V2 class //
20// //
21// Origin: Iouri Belikov, CERN, Jouri.Belikov@cern.ch //
22// //
23///////////////////////////////////////////////////////////////////////////
24
25
04366a57 26
ae485645 27#include <TClonesArray.h>
04366a57 28#include "AliITSClusterFinderV2SDD.h"
00a7cc50 29#include "AliITSRecPoint.h"
7d62fb64 30#include "AliITSDetTypeRec.h"
04366a57 31#include "AliRawReader.h"
0599a018 32#include "AliITSRawStreamSDD.h"
2f8ed7ab 33#include "AliITSRawStreamSDDCompressed.h"
5bfe44ce 34#include "AliITSCalibrationSDD.h"
253e68a0 35#include "AliITSresponseSDD.h"
5bfe44ce 36#include "AliITSDetTypeRec.h"
debf7b78 37#include "AliITSReconstructor.h"
5bfe44ce 38#include "AliITSsegmentationSDD.h"
04366a57 39#include "AliITSdigitSDD.h"
b4704be3 40#include "AliITSgeomTGeo.h"
debf7b78 41
04366a57 42ClassImp(AliITSClusterFinderV2SDD)
43
5d2c2f86 44AliITSClusterFinderV2SDD::AliITSClusterFinderV2SDD(AliITSDetTypeRec* dettyp):AliITSClusterFinder(dettyp)
a66a0eb6 45{
04366a57 46
47 //Default constructor
48
04366a57 49}
50
51
52void AliITSClusterFinderV2SDD::FindRawClusters(Int_t mod){
53
54 //Find clusters V2
55 SetModule(mod);
56 FindClustersSDD(fDigits);
57
58}
59
60void AliITSClusterFinderV2SDD::FindClustersSDD(TClonesArray *digits) {
61 //------------------------------------------------------------
62 // Actual SDD cluster finder
63 //------------------------------------------------------------
4952f440 64 Int_t nAnodes = GetSeg()->NpzHalf();
65 Int_t nzBins = nAnodes+2;
66 Int_t nTimeBins = GetSeg()->Npx();
67 Int_t nxBins = nTimeBins+2;
f6b6d58e 68 const Int_t kMaxBin=nzBins*nxBins;
04366a57 69
70 AliBin *bins[2];
4952f440 71 bins[0]=new AliBin[kMaxBin];
72 bins[1]=new AliBin[kMaxBin];
404c1c29 73 AliITSCalibrationSDD* cal = (AliITSCalibrationSDD*)GetResp(fModule);
9c7f5947 74 if(cal==0){
75 AliError(Form("Calibration object not present for SDD module %d\n",fModule));
76 return;
77 }
83ec5e27 78
04366a57 79 AliITSdigitSDD *d=0;
80 Int_t i, ndigits=digits->GetEntriesFast();
81 for (i=0; i<ndigits; i++) {
82 d=(AliITSdigitSDD*)digits->UncheckedAt(i);
20f3f947 83 Int_t ian=d->GetCoord1();
84 Int_t itb=d->GetCoord2();
85 Int_t iSide=0;
86 if (ian >= nAnodes) iSide=1;
75065a60 87 Float_t gain=cal->GetChannelGain(ian)/fDetTypeRec->GetAverageGainSDD();
83ec5e27 88 Float_t charge=d->GetSignal(); // returns expanded signal
89 // (10 bit, low threshold already added)
20f3f947 90 Float_t baseline = cal->GetBaseline(ian);
91 if(charge>baseline) charge-=baseline;
92 else charge=0;
5683bd96 93
9c7f5947 94 if(gain>0.){ // Bad channels have gain=0.
95 charge/=gain;
20f3f947 96 if(charge<cal->GetThresholdAnode(ian)) continue;
9c7f5947 97 Int_t q=(Int_t)(charge+0.5);
20f3f947 98 Int_t y=itb+1;
99 Int_t z=ian+1;
9c7f5947 100 if (z <= nAnodes){
101 bins[0][y*nzBins+z].SetQ(q);
102 bins[0][y*nzBins+z].SetMask(1);
103 bins[0][y*nzBins+z].SetIndex(i);
104 } else {
105 z-=nAnodes;
106 bins[1][y*nzBins+z].SetQ(q);
107 bins[1][y*nzBins+z].SetMask(1);
108 bins[1][y*nzBins+z].SetIndex(i);
109 }
04366a57 110 }
111 }
112
4952f440 113 FindClustersSDD(bins, kMaxBin, nzBins, digits);
04366a57 114
115 delete[] bins[0];
116 delete[] bins[1];
117
118}
119
120void AliITSClusterFinderV2SDD::
121FindClustersSDD(AliBin* bins[2], Int_t nMaxBin, Int_t nzBins,
de075dae 122 TClonesArray *digits, TClonesArray *clusters, Int_t jitter) {
04366a57 123 //------------------------------------------------------------
124 // Actual SDD cluster finder
125 //------------------------------------------------------------
b4704be3 126
debf7b78 127 static AliITSRecoParam *repa = NULL;
128 if(!repa){
129 repa = (AliITSRecoParam*) AliITSReconstructor::GetRecoParam();
130 if(!repa){
ed446fa3 131 repa = AliITSRecoParam::GetHighFluxParam();
debf7b78 132 AliWarning("Using default AliITSRecoParam class");
133 }
134 }
b4704be3 135 const TGeoHMatrix *mT2L=AliITSgeomTGeo::GetTracking2LocalMatrix(fModule);
404c1c29 136 AliITSCalibrationSDD* cal = (AliITSCalibrationSDD*)GetResp(fModule);
9c7f5947 137 if(cal==0){
138 AliError(Form("Calibration object not present for SDD module %d\n",fModule));
139 return;
140 }
04366a57 141 Int_t ncl=0;
142 TClonesArray &cl=*clusters;
143 for (Int_t s=0; s<2; s++)
144 for (Int_t i=0; i<nMaxBin; i++) {
2b025386 145 if(NoiseSuppress(i,s,nzBins,bins[s],cal)) continue;
04366a57 146 if (bins[s][i].IsUsed()) continue;
147 Int_t idx[32]; UInt_t msk[32]; Int_t npeaks=0;
148 FindPeaks(i, nzBins, bins[s], idx, msk, npeaks);
149
150 if (npeaks>30) continue;
151 if (npeaks==0) continue;
152
153 Int_t k,l;
154 for (k=0; k<npeaks-1; k++){//mark adjacent peaks
155 if (idx[k] < 0) continue; //this peak is already removed
156 for (l=k+1; l<npeaks; l++) {
f6b6d58e 157 if (idx[l] < 0) continue; //this peak is already removed
158 Int_t ki=idx[k]/nzBins, kj=idx[k] - ki*nzBins;
159 Int_t li=idx[l]/nzBins, lj=idx[l] - li*nzBins;
160 Int_t di=TMath::Abs(ki - li);
161 Int_t dj=TMath::Abs(kj - lj);
162 if (di>1 || dj>1) continue;
163 if (bins[s][idx[k]].GetQ() > bins[s][idx[l]].GetQ()) {
164 msk[l]=msk[k];
165 idx[l]*=-1;
166 } else {
167 msk[k]=msk[l];
168 idx[k]*=-1;
169 break;
170 }
04366a57 171 }
172 }
173
174 for (k=0; k<npeaks; k++) {
debf7b78 175 if(repa->GetUseUnfoldingInClusterFinderSDD()==kFALSE) msk[k]=msk[0];
04366a57 176 MarkPeak(TMath::Abs(idx[k]), nzBins, bins[s], msk[k]);
177 }
178
179 for (k=0; k<npeaks; k++) {
f6b6d58e 180 if (idx[k] < 0) continue; //removed peak
181 AliITSRecPoint c;
182 MakeCluster(idx[k], nzBins, bins[s], msk[k], c);
183 //mi change
184 Int_t milab[10];
185 for (Int_t ilab=0;ilab<10;ilab++){
186 milab[ilab]=-2;
187 }
188 Int_t maxi=0,mini=0,maxj=0,minj=0;
2b025386 189
b779c7dc 190 for (Int_t di=-5; di<=5;di++){
191 for (Int_t dj=-10;dj<=10;dj++){
f6b6d58e 192 Int_t index = idx[k]+di+dj*nzBins;
193 if (index<0) continue;
194 if (index>=nMaxBin) continue;
195 AliBin *b=&bins[s][index];
196 Int_t nAnode=index%nzBins-1;
197 Int_t adcSignal=b->GetQ();
198 if(adcSignal>cal->GetThresholdAnode(nAnode)){
199 if (di>maxi) maxi=di;
200 if (di<mini) mini=di;
201 if (dj>maxj) maxj=dj;
202 if (dj<minj) minj=dj;
203 }
204 //
205 if(digits) {
206 if (TMath::Abs(di)<2&&TMath::Abs(dj)<2){
207 AliITSdigitSDD* d=(AliITSdigitSDD*)digits->UncheckedAt(b->GetIndex());
208 for (Int_t itrack=0;itrack<10;itrack++){
209 Int_t track = (d->GetTracks())[itrack];
210 if (track>=0) {
211 AddLabel(milab, track);
212 }
213 }
214 }
215 }
216 }
217 }
b779c7dc 218 Int_t clSizAnode=maxi-mini+1;
219 Int_t clSizTb=maxj-minj+1;
220 if(repa->GetUseSDDClusterSizeSelection()){
221 if(clSizTb==1) continue; // cut common mode noise spikes
222 if(clSizAnode>3) continue; // cut common mode noise spikes
223 if(clSizTb>10) continue; // cut clusters on noisy anodes
224 }
f6b6d58e 225
253e68a0 226 AliITSresponseSDD* rsdd = fDetTypeRec->GetResponseSDD();
227 Float_t y=c.GetY(),z=c.GetZ(), q=c.GetQ();
f6b6d58e 228 y/=q; z/=q;
229 Float_t zAnode=z-0.5; // to have anode in range 0.-255. and centered on the mid of the pitch
230 Float_t timebin=y-0.5; // to have time bin in range 0.-255. amd centered on the mid of the bin
231 if(s==1) zAnode += GetSeg()->NpzHalf(); // right side has anodes from 256. to 511.
232 Float_t zdet = GetSeg()->GetLocalZFromAnode(zAnode);
de075dae 233 Float_t driftTimeUncorr = GetSeg()->GetDriftTimeFromTb(timebin)+jitter*rsdd->GetCarlosRXClockPeriod();
3176641e 234 Float_t driftTime=driftTimeUncorr-rsdd->GetTimeZero(fModule);
69b82143 235 Float_t driftSpeed = cal->GetDriftSpeedAtAnode(zAnode) + rsdd->GetDeltaVDrift(fModule);
236 Float_t driftPathMicron = driftTime*driftSpeed;
f6b6d58e 237 const Double_t kMicronTocm = 1.0e-4;
238 Float_t xdet=(driftPathMicron-GetSeg()->Dx())*kMicronTocm; // xdet is negative
239 if (s==0) xdet=-xdet; // left side has positive local x
c9a38d3d 240
ba0a07bf 241 if(repa->GetUseSDDCorrectionMaps()){
242 Float_t corrx=0, corrz=0;
243 cal->GetCorrections(zdet,xdet,corrz,corrx,GetSeg());
244 zdet+=corrz;
245 xdet+=corrx;
246 }
f6b6d58e 247
248 Double_t loc[3]={xdet,0.,zdet},trk[3]={0.,0.,0.};
249 mT2L->MasterToLocal(loc,trk);
250 y=trk[1];
251 z=trk[2];
252
d644eb2d 253 q/=rsdd->GetADC2keV();
254 q+=(driftTime*rsdd->GetChargevsTime()); // correction for zero supp.
aebba721 255 if(cal-> IsAMAt20MHz()) q*=2.; // account for 1/2 sampling freq.
b779c7dc 256 if(q<repa->GetMinClusterChargeSDD()) continue; // remove noise clusters
257
f6b6d58e 258 Float_t hit[5] = {y, z, 0.0030*0.0030, 0.0020*0.0020, q};
b779c7dc 259 Int_t info[3] = {clSizTb, clSizAnode, fNlayer[fModule]};
f6b6d58e 260 if (digits) {
261 // AliBin *b=&bins[s][idx[k]];
262 // AliITSdigitSDD* d=(AliITSdigitSDD*)digits->UncheckedAt(b->GetIndex());
263 {
264 //Int_t lab[3];
265 //lab[0]=(d->GetTracks())[0];
266 //lab[1]=(d->GetTracks())[1];
267 //lab[2]=(d->GetTracks())[2];
268 //CheckLabels(lab);
269 CheckLabels2(milab);
270 }
271 }
272 milab[3]=fNdet[fModule];
f6b6d58e 273 AliITSRecPoint cc(milab,hit,info);
274 cc.SetType(npeaks);
b2558977 275 cc.SetDriftTime(driftTimeUncorr);
f6b6d58e 276 if(clusters) new (cl[ncl]) AliITSRecPoint(cc);
277 else {
278 fDetTypeRec->AddRecPoint(cc);
279 }
280 ncl++;
04366a57 281 }
282 }
24c98369 283
04366a57 284}
f6b6d58e 285//______________________________________________________________________
04366a57 286void AliITSClusterFinderV2SDD::RawdataToClusters(AliRawReader* rawReader,TClonesArray** clusters){
287 //------------------------------------------------------------
288 // This function creates ITS clusters from raw data
289 //------------------------------------------------------------
290 rawReader->Reset();
2f8ed7ab 291 AliITSRawStream* inputSDD;
292 if(fDetTypeRec->IsHLTmodeC()==kTRUE){
293 inputSDD=new AliITSRawStreamSDDCompressed(rawReader);
294 }else{
295 inputSDD=new AliITSRawStreamSDD(rawReader);
296 }
297
979b5a5f 298 AliITSDDLModuleMapSDD *ddlmap=(AliITSDDLModuleMapSDD*)fDetTypeRec->GetDDLModuleMapSDD();
2f8ed7ab 299 inputSDD->SetDDLModuleMap(ddlmap);
83ec5e27 300 for(Int_t iddl=0; iddl<AliITSDDLModuleMapSDD::GetNDDLs(); iddl++){
301 for(Int_t icar=0; icar<AliITSDDLModuleMapSDD::GetNModPerDDL();icar++){
302 Int_t iMod=ddlmap->GetModuleNumber(iddl,icar);
303 if(iMod==-1) continue;
304 AliITSCalibrationSDD* cal = (AliITSCalibrationSDD*)GetResp(iMod);
305 if(cal==0){
306 AliError(Form("Calibration object not present for SDD module %d\n",iMod));
307 continue;
308 }
253e68a0 309 Bool_t isZeroSupp=cal->GetZeroSupp();
310 if(isZeroSupp){
2f8ed7ab 311 for(Int_t iSid=0; iSid<2; iSid++) inputSDD->SetZeroSuppLowThreshold(iMod-240,iSid,cal->GetZSLowThreshold(iSid));
83ec5e27 312 }else{
2f8ed7ab 313 for(Int_t iSid=0; iSid<2; iSid++) inputSDD->SetZeroSuppLowThreshold(iMod-240,iSid,0);
83ec5e27 314 }
315 }
316 }
2f8ed7ab 317 FindClustersSDD(inputSDD,clusters);
318 delete inputSDD;
04366a57 319}
320
321void AliITSClusterFinderV2SDD::FindClustersSDD(AliITSRawStream* input,
322 TClonesArray** clusters)
323{
324 //------------------------------------------------------------
325 // Actual SDD cluster finder for raw data
326 //------------------------------------------------------------
327 Int_t nClustersSDD = 0;
4952f440 328 Int_t nAnodes = GetSeg()->NpzHalf();
329 Int_t nzBins = nAnodes+2;
330 Int_t nTimeBins = GetSeg()->Npx();
331 Int_t nxBins = nTimeBins+2;
f6b6d58e 332 const Int_t kMaxBin=nzBins*nxBins;
ae485645 333 AliBin *bins[2];
93a93288 334 AliBin *ddlbins[kHybridsPerDDL]; // 12 modules (=24 hybrids) of 1 DDL read "in parallel"
335 for(Int_t iHyb=0;iHyb<kHybridsPerDDL;iHyb++) ddlbins[iHyb]=new AliBin[kMaxBin];
de075dae 336 Int_t vectModId[kModulesPerDDL];
337 for(Int_t iMod=0; iMod<kModulesPerDDL; iMod++) vectModId[iMod]=-1;
338
04366a57 339 // read raw data input stream
14dceddf 340 while (input->Next()) {
e4da2288 341 Int_t iModule = input->GetModuleID();
93a93288 342 if(iModule<0){
343 AliWarning(Form("Invalid SDD module number %d\n", iModule));
344 continue;
345 }
6297cc01 346 Int_t iCarlos =input->GetCarlosId();
347 Int_t iSide = input->GetChannel();
14dceddf 348 Int_t iHybrid=iCarlos*2+iSide;
de075dae 349
14dceddf 350 if (input->IsCompletedModule()) {
de075dae 351 // store the module number
352 vectModId[iCarlos]=iModule;
353 }
354 else if (input->IsCompletedDDL()) {
355 // when all data from a DDL was read, search for clusters
356 Int_t jitter=input->GetJitter();
357 for(Int_t iMod=0; iMod<kModulesPerDDL; iMod++){
358 if(vectModId[iMod]>=0){
359 fModule = vectModId[iMod];
360 clusters[fModule] = new TClonesArray("AliITSRecPoint");
361 bins[0]=ddlbins[iMod*2]; // first hybrid of the module
362 bins[1]=ddlbins[iMod*2+1]; // second hybrid of the module
363 FindClustersSDD(bins, kMaxBin, nzBins, NULL, clusters[fModule],jitter);
364 Int_t nClusters = clusters[fModule]->GetEntriesFast();
365 nClustersSDD += nClusters;
366 vectModId[iMod]=-1;
367 }
368 for(Int_t iBin=0;iBin<kMaxBin; iBin++){
369 ddlbins[iMod*2][iBin].Reset();
370 ddlbins[iMod*2+1][iBin].Reset();
371 }
44c5b268 372 }
5dfa68c5 373 }else{
04366a57 374 // fill the current digit into the bins array
44c5b268 375 if(iHybrid<0 || iHybrid>=kHybridsPerDDL){
376 AliWarning(Form("Invalid SDD hybrid number %d on module %d\n", iHybrid,iModule));
377 continue;
378 }
e4da2288 379 AliITSCalibrationSDD* cal = (AliITSCalibrationSDD*)GetResp(iModule);
9c7f5947 380 if(cal==0){
381 AliError(Form("Calibration object not present for SDD module %d\n",iModule));
382 continue;
383 }
5683bd96 384 Float_t charge=input->GetSignal();
14dceddf 385 Int_t chan=input->GetCoord1()+nAnodes*iSide;
75065a60 386 Float_t gain=cal->GetChannelGain(chan)/fDetTypeRec->GetAverageGainSDD();;
20f3f947 387 Float_t baseline = cal->GetBaseline(chan);
388 if(charge>baseline) charge-=baseline;
389 else charge=0;
9c7f5947 390 if(gain>0.){ // Bad channels have gain=0
391 charge/=gain;
392 if(charge>=cal->GetThresholdAnode(chan)) {
393 Int_t q=(Int_t)(charge+0.5);
394 Int_t iz = input->GetCoord1();
395 Int_t itb = input->GetCoord2();
396 Int_t index = (itb+1) * nzBins + (iz+1);
397 if(index<kMaxBin){
398 ddlbins[iHybrid][index].SetQ(q);
399 ddlbins[iHybrid][index].SetMask(1);
400 ddlbins[iHybrid][index].SetIndex(index);
401 }else{
402 AliWarning(Form("Invalid SDD cell: Anode=%d TimeBin=%d",iz,itb));
403 }
93a93288 404 }
5dfa68c5 405 }
04366a57 406 }
407 }
93a93288 408 for(Int_t iHyb=0;iHyb<kHybridsPerDDL;iHyb++) delete [] ddlbins[iHyb];
04366a57 409 Info("FindClustersSDD", "found clusters in ITS SDD: %d", nClustersSDD);
04366a57 410}
411
e3ee4984 412//______________________________________________________________________
2b025386 413Bool_t AliITSClusterFinderV2SDD::NoiseSuppress(Int_t k, Int_t sid,Int_t nzBins, AliBin* bins, AliITSCalibrationSDD* cal) const {
e3ee4984 414 // applies zero suppression using the measured noise of each anode
415 // threshold values from ALICE-INT-1999-28 V10
2b025386 416 // returns kTRUE if the digit should eb noise suppressed, kFALSE if it should be kept
83ec5e27 417 Float_t xfactL=2.2;
418 Float_t xfactH=4.0;
e3ee4984 419 //
420
421 Int_t iAn=(k%nzBins)-1;
2b025386 422 if(iAn<0 || iAn>255) return kTRUE;
e3ee4984 423 if(sid==1) iAn+=256;
424 Int_t nLow=0, nHigh=0;
425 Float_t noise=cal->GetNoiseAfterElectronics(iAn);
426 Float_t noisem1=noise;
427 if(iAn>1) noisem1=cal->GetNoiseAfterElectronics(iAn-1);
428 Float_t noisep1=noise;
429 if(iAn<511) noisep1=cal->GetNoiseAfterElectronics(iAn+1);
83ec5e27 430 Float_t tL=noise*xfactL;
431 Float_t tH=noise*xfactH;
432 Float_t tLp1=noisep1*xfactL;
433 Float_t tHp1=noisep1*xfactH;
434 Float_t tLm1=noisem1*xfactL;
435 Float_t tHm1=noisem1*xfactH;
2b025386 436 Int_t cC=bins[k].GetQ();
e3ee4984 437 if(cC<=tL){
438 bins[k].SetQ(0);
439 bins[k].SetMask(0xFFFFFFFE);
2b025386 440 return kTRUE;;
e3ee4984 441 }
442 nLow++; // cC is greater than tL
443 if(cC>tH) nHigh++;
444 Int_t sS=bins[k-1].GetQ();
445 if(sS>tLm1) nLow++;
446 if(sS>tHm1) nHigh++;
447 Int_t nN=bins[k+1].GetQ();
448 if(nN>tLp1) nLow++;
449 if(nN>tHp1) nHigh++;
450 Int_t eE=bins[k-nzBins].GetQ();
451 if(eE>tL) nLow++;
452 if(eE>tH) nHigh++;
453 Int_t wW=bins[k+nzBins].GetQ();
454 if(wW>tL) nLow++;
455 if(wW>tH) nHigh++;
2b025386 456 if(nLow<3 || nHigh<1) return kTRUE;
457 else return kFALSE;
e3ee4984 458}
459
04366a57 460
d97eb3fe 461