]> git.uio.no Git - u/mrichter/AliRoot.git/blob - ITS/AliITSclustererV2.cxx
Coverity
[u/mrichter/AliRoot.git] / ITS / AliITSclustererV2.cxx
1 //-------------------------------------------------------------------------
2 //            Implementation of the ITS clusterer V2 class
3 //
4 //          Origin: Iouri Belikov, CERN, Jouri.Belikov@cern.ch
5 //-------------------------------------------------------------------------
6
7
8 #include "AliLoader.h"
9 #include "AliRun.h"
10
11 #include "AliITSclustererV2.h"
12 #include "AliITSclusterV2.h"
13 #include "AliITSDetTypeRec.h"
14 #include "AliRawReader.h"
15 #include "AliITSRawStreamSPD.h"
16 #include "AliITSRawStreamSDD.h"
17 #include "AliITSRawStreamSSD.h"
18
19 #include <TFile.h>
20 #include <TTree.h>
21 #include <TClonesArray.h>
22 #include "AliITSgeomTGeo.h"
23 #include "AliITSdigitSPD.h"
24 #include "AliITSdigitSDD.h"
25 #include "AliITSdigitSSD.h"
26 #include "AliMC.h"
27
28 ClassImp(AliITSclustererV2)
29
30 extern AliRun *gAlice;
31
32 AliITSclustererV2::AliITSclustererV2():
33 fNModules(0),
34 fEvent(0),
35 fI(0),
36 fLastSPD1(0),
37 fNySPD(0),
38 fNzSPD(0),
39 fYpitchSPD(0),
40 fZ1pitchSPD(0),
41 fZ2pitchSPD(0),
42 fHwSPD(0),
43 fHlSPD(0),
44 fNySDD(0),
45 fNzSDD(0),
46 fYpitchSDD(0),
47 fZpitchSDD(0),
48 fHwSDD(0),
49 fHlSDD(0),
50 fYoffSDD(0),
51 fLastSSD1(0),
52 fYpitchSSD(0),
53 fHwSSD(0),
54 fHlSSD(0),
55 fTanP(0),
56 fTanN(0){
57    //default constructor
58   for(Int_t i=0;i<260;i++)fYSPD[i]=0.;
59   for(Int_t i=0;i<170;i++)fZSPD[i]=0.;
60   for(Int_t i=0;i<2200;i++){
61     fYshift[i]=0.;
62     fZshift[i]=0.;
63     fNdet[i]=0;
64     fNlayer[i]=0;
65   }
66
67 }
68 AliITSclustererV2::AliITSclustererV2(const Char_t *geom):
69 fNModules(AliITSgeomTGeo::GetNModules()),
70 fEvent(0),
71 fI(0),
72 fLastSPD1(AliITSgeomTGeo::GetModuleIndex(2,1,1)-1),
73 fNySPD(256),
74 fNzSPD(160),
75 fYpitchSPD(0.0050),
76 fZ1pitchSPD(0.0425),
77 fZ2pitchSPD(0.0625),
78 fHwSPD(0.64),
79 fHlSPD(3.48),
80 fNySDD(256),
81 fNzSDD(256),
82 fYpitchSDD(0.01825),
83 fZpitchSDD(0.02940),
84 fHwSDD(3.5085),
85 fHlSDD(3.7632),
86 fYoffSDD(0.0425),
87 fLastSSD1(AliITSgeomTGeo::GetModuleIndex(6,1,1)-1),
88 fYpitchSSD(0.0095),
89 fHwSSD(3.65),
90 fHlSSD(2.00),
91 fTanP(0.0275),
92 fTanN(0.0075) {
93   //------------------------------------------------------------
94   // Standard constructor
95   //------------------------------------------------------------
96   if (geom) {
97     AliWarning("\"geom\" is actually a dummy argument !");
98   }
99   Int_t m;
100   for (m=0; m<fNModules; m++) {
101      Int_t lay,lad,det; AliITSgeomTGeo::GetModuleId(m,lay,lad,det);
102      const TGeoHMatrix *tm=AliITSgeomTGeo::GetTracking2LocalMatrix(m);
103      fYshift[m] = (tm->Inverse()).GetTranslation()[1];
104      fZshift[m] = (tm->Inverse()).GetTranslation()[2];
105      fNdet[m] = (lad-1)*AliITSgeomTGeo::GetNDetectors(lay) + (det-1);
106      fNlayer[m] = lay-1;
107   }
108
109   //SPD geometry  
110   fYSPD[0]=0.5*fYpitchSPD;
111   for (m=1; m<fNySPD; m++) fYSPD[m]=fYSPD[m-1]+fYpitchSPD; 
112   fZSPD[0]=fZ1pitchSPD;
113   for (m=1; m<fNzSPD; m++) {
114     Double_t dz=fZ1pitchSPD;
115     if (m==31 || m==32 || m==63  || m==64  || m==95 || m==96 || 
116         m==127 || m==128) dz=fZ2pitchSPD; 
117     fZSPD[m]=fZSPD[m-1]+dz;
118   }
119   for (m=0; m<fNzSPD; m++) {
120     Double_t dz=0.5*fZ1pitchSPD;
121     if (m==31 || m==32 || m==63  || m==64  || m==95 || m==96 || 
122         m==127 || m==128) dz=0.5*fZ2pitchSPD; 
123     fZSPD[m]-=dz;
124   }
125
126 }
127
128
129 Int_t AliITSclustererV2::Digits2Clusters(TTree *dTree, TTree *cTree) {
130   //------------------------------------------------------------
131   // This function creates ITS clusters
132   //------------------------------------------------------------
133   Int_t ncl=0;
134
135   if (!dTree) {
136     Error("Digits2Clusters","Can't get the tree with digits !");
137     return 1;
138   }
139
140   TClonesArray *digitsSPD=new TClonesArray("AliITSdigitSPD",3000);
141   dTree->SetBranchAddress("ITSDigitsSPD",&digitsSPD);
142   TClonesArray *digitsSDD=new TClonesArray("AliITSdigitSDD",3000);
143   dTree->SetBranchAddress("ITSDigitsSDD",&digitsSDD);
144   TClonesArray *digitsSSD=new TClonesArray("AliITSdigitSSD",3000);
145   dTree->SetBranchAddress("ITSDigitsSSD",&digitsSSD);
146
147   TClonesArray *clusters=new TClonesArray("AliITSclusterV2",1000);
148   TBranch *branch=cTree->GetBranch("Clusters");
149   if (!branch) cTree->Branch("Clusters",&clusters);
150   else branch->SetAddress(&clusters);
151
152   Int_t mmax=(Int_t)dTree->GetEntries();
153   if (mmax!=fNModules) {
154     Error("Digits2Clusters","Number of entries != number of modules !");
155     return 1;
156   }
157
158   for (fI=0; fI<mmax; fI++) {
159     dTree->GetEvent(fI);
160
161     if     (digitsSPD->GetEntriesFast()!=0) 
162       FindClustersSPD(digitsSPD,clusters);
163     else 
164       if(digitsSDD->GetEntriesFast()!=0) 
165         FindClustersSDD(digitsSDD,clusters);
166       else if(digitsSSD->GetEntriesFast()!=0) 
167         FindClustersSSD(digitsSSD,clusters);
168     
169     ncl+=clusters->GetEntriesFast();
170
171     cTree->Fill();
172
173     digitsSPD->Clear();
174     digitsSDD->Clear();
175     digitsSSD->Clear();
176     clusters->Clear();
177   }
178
179   //cTree->Write();
180
181   delete clusters;
182
183   delete digitsSPD;
184   delete digitsSDD;
185   delete digitsSSD;
186
187   //delete dTree;
188
189   Info("Digits2Clusters","Number of found clusters : %d",ncl);
190
191   return 0;
192 }
193
194 void AliITSclustererV2::Digits2Clusters(AliRawReader* rawReader) {
195   //------------------------------------------------------------
196   // This function creates ITS clusters from raw data
197   //------------------------------------------------------------
198   AliRunLoader* runLoader = AliRunLoader::Instance();
199   if (!runLoader) {
200     Error("Digits2Clusters", "no run loader found");
201     return;
202   }
203   runLoader->LoadKinematics();
204   AliLoader* itsLoader = runLoader->GetLoader("ITSLoader");
205   if (!itsLoader) {
206     Error("Digits2Clusters", "no loader for ITS found");
207     return;
208   }
209   if (!itsLoader->TreeR()) itsLoader->MakeTree("R");
210   TTree* cTree = itsLoader->TreeR();
211
212   TClonesArray *array=new TClonesArray("AliITSclusterV2",1000);
213   cTree->Branch("Clusters",&array);
214   delete array;
215
216   TClonesArray** clusters = new TClonesArray*[fNModules]; 
217   for (Int_t iModule = 0; iModule < fNModules; iModule++) {
218     clusters[iModule] = NULL;
219   }
220   // one TClonesArray per module
221
222   rawReader->Reset();
223   AliITSRawStreamSPD inputSPD(rawReader);
224   FindClustersSPD(&inputSPD, clusters);
225
226   rawReader->Reset();
227   AliITSRawStreamSDD inputSDD(rawReader);
228   FindClustersSDD(&inputSDD, clusters);
229
230   rawReader->Reset();
231   AliITSRawStreamSSD inputSSD(rawReader);
232   FindClustersSSD(&inputSSD, clusters);
233
234   // write all clusters to the tree
235   Int_t nClusters = 0;
236   TClonesArray *emptyArray=new TClonesArray("AliITSclusterV2");
237   for (Int_t iModule = 0; iModule < fNModules; iModule++) {
238     array = clusters[iModule];
239     if (!array) {
240       Error("Digits2Clusters", "data for module %d missing!", iModule);
241       array = emptyArray;
242     }
243     cTree->SetBranchAddress("Clusters", &array);
244     cTree->Fill();
245     nClusters += array->GetEntriesFast();
246   }
247   delete emptyArray;
248
249   itsLoader->WriteRecPoints("OVERWRITE");
250
251   delete[] clusters;
252
253   Info("Digits2Clusters", "total number of found clusters in ITS: %d\n", 
254        nClusters);
255 }
256
257 //**** Fast clusters *******************************
258 #include "TParticle.h"
259
260 //#include "AliITS.h"
261 #include "AliITSmodule.h"
262 #include "AliITSRecPoint.h"
263 #include "AliITSsimulationFastPoints.h"
264 #include "AliITSRecPoint.h"
265
266
267 static void CheckLabels(Int_t lab[3]) {
268   //------------------------------------------------------------
269   // Tries to find mother's labels
270   //------------------------------------------------------------
271   AliRunLoader *rl = AliRunLoader::Instance();
272   TTree *trK=(TTree*)rl->TreeK();
273
274   if(trK){
275     Int_t label=lab[0];
276     if (label>=0) {
277       TParticle *part=(TParticle*)gAlice->GetMCApp()->Particle(label);
278       label=-3;
279       while (part->P() < 0.005) {
280         Int_t m=part->GetFirstMother();
281         if (m<0) {
282           Info("CheckLabels","Primary momentum: %f",part->P()); 
283           break;
284         }
285         if (part->GetStatusCode()>0) {
286           Info("CheckLabels","Primary momentum: %f",part->P()); 
287           break;
288         }
289         label=m;
290         part=(TParticle*)gAlice->GetMCApp()->Particle(label);
291       }
292       if(lab[1]<0){
293         lab[1]=label;
294       }
295       else if (lab[2]<0) {
296         lab[2]=label;
297       }
298       else {
299         //      cerr<<"CheckLabels : No empty labels !\n";
300       }
301     }
302   }
303 }
304
305 /*
306 static void CheckLabels(Int_t lab[3]) {
307   //------------------------------------------------------------
308   // Tries to find mother's labels
309   //------------------------------------------------------------
310
311   if(lab[0]<0 && lab[1]<0 && lab[2]<0) return; // In case of no labels just exit
312
313   Int_t ntracks = gAlice->GetMCApp()->GetNtrack();
314   for (Int_t i=0;i<3;i++){
315     Int_t label = lab[i];
316     if (label>=0 && label<ntracks) {
317       TParticle *part=(TParticle*)gAlice->GetMCApp()->Particle(label);
318       if (part->P() < 0.005) {
319         Int_t m=part->GetFirstMother();
320         if (m<0) {      
321           continue;
322         }
323         if (part->GetStatusCode()>0) {
324           continue;
325         }
326         lab[i]=m;       
327       }
328     }    
329   }
330   
331 }
332 */
333 static void CheckLabels2(Int_t lab[10]) {
334   //------------------------------------------------------------
335   // Tries to find mother's labels
336   //------------------------------------------------------------
337   AliRunLoader *rl = AliRunLoader::Instance();
338   TTree *trK=(TTree*)rl->TreeK();
339
340   if(trK){
341     Int_t nlabels =0; 
342     for (Int_t i=0;i<10;i++) if (lab[i]>=0) nlabels++;
343     if(nlabels == 0) return; // In case of no labels just exit
344
345     Int_t ntracks = gAlice->GetMCApp()->GetNtrack();
346
347     for (Int_t i=0;i<10;i++){
348       Int_t label = lab[i];
349       if (label>=0 && label<ntracks) {
350         TParticle *part=(TParticle*)gAlice->GetMCApp()->Particle(label);
351         if (part->P() < 0.02) {
352           Int_t m=part->GetFirstMother();
353           if (m<0) {    
354             continue;
355           }
356           if (part->GetStatusCode()>0) {
357             continue;
358           }
359           lab[i]=m;       
360         }
361         else
362           if (part->P() < 0.12 && nlabels>3) {
363             lab[i]=-2;
364             nlabels--;
365           } 
366       }
367       else{
368         if ( (label>ntracks||label <0) && nlabels>3) {
369           lab[i]=-2;
370           nlabels--;
371         } 
372       }
373     }  
374     if (nlabels>3){
375       for (Int_t i=0;i<10;i++){
376         if (nlabels>3){
377           Int_t label = lab[i];
378           if (label>=0 && label<ntracks) {
379             TParticle *part=(TParticle*)gAlice->GetMCApp()->Particle(label);
380             if (part->P() < 0.1) {
381               lab[i]=-2;
382               nlabels--;
383             }
384           }
385         }
386       }
387     }
388
389     //compress labels -- if multi-times the same
390     Int_t lab2[10];
391     for (Int_t i=0;i<10;i++) lab2[i]=-2;
392     for (Int_t i=0;i<10  ;i++){
393       if (lab[i]<0) continue;
394       for (Int_t j=0;j<10 &&lab2[j]!=lab[i];j++){
395         if (lab2[j]<0) {
396           lab2[j]= lab[i];
397           break;
398         }
399       }
400     }
401     for (Int_t j=0;j<10;j++) lab[j]=lab2[j];
402   }
403 }
404
405 static void AddLabel(Int_t lab[10], Int_t label) {
406 // add label of the particle in the kine tree which originates this cluster
407 // (used for reconstruction of MC data only, for comparison purposes)
408   AliRunLoader *rl = AliRunLoader::Instance();
409   TTree *trK=(TTree*)rl->TreeK();
410
411   if(trK){
412     if(label<0) return; // In case of no label just exit
413
414     Int_t ntracks = gAlice->GetMCApp()->GetNtrack();
415     if (label>ntracks) return;
416     for (Int_t i=0;i<10;i++){
417       //    if (label<0) break;
418       if (lab[i]==label) break;
419       if (lab[i]<0) {
420         lab[i]= label;
421         break;
422       }
423     }
424   }
425 }
426
427 void AliITSclustererV2::RecPoints2Clusters
428 (const TClonesArray *points, Int_t idx, TClonesArray *clusters) {
429   //------------------------------------------------------------
430   // Conversion AliITSRecPoint -> AliITSclusterV2 for the ITS 
431   // subdetector indexed by idx 
432   //------------------------------------------------------------
433   TClonesArray &cl=*clusters;
434   Int_t ncl=points->GetEntriesFast();
435   for (Int_t i=0; i<ncl; i++) {
436     AliITSRecPoint *p = (AliITSRecPoint *)points->UncheckedAt(i);
437     Float_t lp[5];
438     lp[0]=-(-p->GetDetLocalX()+fYshift[idx]); if (idx<=fLastSPD1) lp[0]*=-1; //SPD1
439     lp[1]=  -p->GetZ()+fZshift[idx];
440     lp[2]=p->GetSigmaDetLocX2();
441     lp[3]=p->GetSigmaZ2();
442     lp[4]=p->GetQ()*36./23333.;  //electrons -> ADC
443     Int_t lab[4]; 
444     lab[0]=p->GetLabel(0); lab[1]=p->GetLabel(1); lab[2]=p->GetLabel(2);
445     lab[3]=fNdet[idx];
446     CheckLabels(lab);
447     Int_t dummy[3]={0,0,0};
448     new (cl[i]) AliITSclusterV2(lab,lp, dummy);
449   }  
450
451
452 //***********************************
453
454
455 void AliITSclustererV2:: 
456 FindCluster(Int_t k,Int_t maxz,AliBin *bins,Int_t &n,Int_t *idx) {
457   //------------------------------------------------------------
458   // returns an array of indices of digits belonging to the cluster
459   // (needed when the segmentation is not regular) 
460   //------------------------------------------------------------
461   if (n<200) idx[n++]=bins[k].GetIndex();
462   bins[k].Use();
463
464   if (bins[k-maxz].IsNotUsed()) FindCluster(k-maxz,maxz,bins,n,idx);
465   if (bins[k-1   ].IsNotUsed()) FindCluster(k-1   ,maxz,bins,n,idx);
466   if (bins[k+maxz].IsNotUsed()) FindCluster(k+maxz,maxz,bins,n,idx);
467   if (bins[k+1   ].IsNotUsed()) FindCluster(k+1   ,maxz,bins,n,idx);
468   /*
469   if (bins[k-maxz-1].IsNotUsed()) FindCluster(k-maxz-1,maxz,bins,n,idx);
470   if (bins[k-maxz+1].IsNotUsed()) FindCluster(k-maxz+1,maxz,bins,n,idx);
471   if (bins[k+maxz-1].IsNotUsed()) FindCluster(k+maxz-1,maxz,bins,n,idx);
472   if (bins[k+maxz+1].IsNotUsed()) FindCluster(k+maxz+1,maxz,bins,n,idx);
473   */
474 }
475
476 void AliITSclustererV2::
477 FindClustersSPD(const TClonesArray *digits, TClonesArray *clusters) {
478   //------------------------------------------------------------
479   // Actual SPD cluster finder
480   //------------------------------------------------------------
481   Int_t kNzBins = fNzSPD + 2;
482   const Int_t kMAXBIN=kNzBins*(fNySPD+2);
483
484   Int_t ndigits=digits->GetEntriesFast();
485   AliBin *bins=new AliBin[kMAXBIN];
486
487   Int_t k;
488   AliITSdigitSPD *d=0;
489   for (k=0; k<ndigits; k++) {
490      d=(AliITSdigitSPD*)digits->UncheckedAt(k);
491      Int_t i=d->GetCoord2()+1;   //y
492      Int_t j=d->GetCoord1()+1;
493      bins[i*kNzBins+j].SetIndex(k);
494      bins[i*kNzBins+j].SetMask(1);
495   }
496    
497   Int_t n=0; TClonesArray &cl=*clusters;
498   for (k=0; k<kMAXBIN; k++) {
499      if (!bins[k].IsNotUsed()) continue;
500      Int_t ni=0, idx[200];
501      FindCluster(k,kNzBins,bins,ni,idx);
502      if (ni==200) {
503         Info("FindClustersSPD","Too big cluster !"); 
504         continue;
505      }
506      Int_t milab[10];
507      for (Int_t ilab=0;ilab<10;ilab++){
508        milab[ilab]=-2;
509      }
510
511      d=(AliITSdigitSPD*)digits->UncheckedAt(idx[0]);
512      Int_t ymin=d->GetCoord2(),ymax=ymin;
513      Int_t zmin=d->GetCoord1(),zmax=zmin;
514
515      for (Int_t l=0; l<ni; l++) {
516         d=(AliITSdigitSPD*)digits->UncheckedAt(idx[l]);
517
518         if (ymin > d->GetCoord2()) ymin=d->GetCoord2();
519         if (ymax < d->GetCoord2()) ymax=d->GetCoord2();
520         if (zmin > d->GetCoord1()) zmin=d->GetCoord1();
521         if (zmax < d->GetCoord1()) zmax=d->GetCoord1();
522         // MI addition - find all labels in cluster
523         for (Int_t dlab=0;dlab<10;dlab++){
524           Int_t digitlab = (d->GetTracks())[dlab];
525           if (digitlab<0) continue;
526           AddLabel(milab,digitlab);       
527         }
528         if (milab[9]>0) CheckLabels2(milab);
529      }
530      CheckLabels2(milab);
531      //
532      //Int_t idy = (fNlayer[fI]==0)? 2:3; 
533      //for (Int_t iz=zmin; iz<=zmax;iz+=2)
534      //Int_t idy = (ymax-ymin)/4.; // max 2 clusters
535      Int_t idy = 0; // max 2 clusters
536      if (fNlayer[fI]==0 &&idy<3) idy=3;
537      if (fNlayer[fI]==1 &&idy<4) idy=4; 
538      Int_t idz =3;
539      for (Int_t iz=zmin; iz<=zmax;iz+=idz)
540        for (Int_t iy=ymin; iy<=ymax;iy+=idy){
541          //
542          Int_t nodigits =0;
543          Float_t y=0.,z=0.,q=0.;         
544          for (Int_t l=0; l<ni; l++) {
545            d=(AliITSdigitSPD*)digits->UncheckedAt(idx[l]);
546            if (zmax-zmin>=idz || ymax-ymin>=idy){
547              if (TMath::Abs( d->GetCoord2()-iy)>0.75*idy) continue;
548              if (TMath::Abs( d->GetCoord1()-iz)>0.75*idz) continue;
549            }
550            nodigits++;
551            Float_t qq=d->GetSignal();
552            y+=qq*fYSPD[d->GetCoord2()]; z+=qq*fZSPD[d->GetCoord1()]; q+=qq;   
553           
554          }     
555          if (nodigits==0) continue;
556          y/=q; z/=q;
557          y-=fHwSPD; z-=fHlSPD;
558          
559          Float_t lp[5];
560          lp[0]=-(-y+fYshift[fI]); if (fI<=fLastSPD1) lp[0]=-lp[0];
561          lp[1]=  -z+fZshift[fI];
562          // Float_t factor=TMath::Max(double(ni-3.),1.5);
563          Float_t factory=TMath::Max(ymax-ymin,1);
564          Float_t factorz=TMath::Max(zmax-zmin,1);
565          factory*= factory;
566          factorz*= factorz;     
567          //lp[2]= (fYpitchSPD*fYpitchSPD/12.)*factory;
568          //lp[3]= (fZ1pitchSPD*fZ1pitchSPD/12.)*factorz;
569          lp[2]= (fYpitchSPD*fYpitchSPD/12.);
570          lp[3]= (fZ1pitchSPD*fZ1pitchSPD/12.);
571          //lp[4]= q;
572          lp[4]= (zmax-zmin+1)*100 + (ymax-ymin+1);
573          
574          milab[3]=fNdet[fI];
575          Int_t info[3] = {ymax-ymin+1,zmax-zmin+1,fNlayer[fI]};
576          new (cl[n]) AliITSclusterV2(milab,lp,info); n++;        
577        }
578   }
579   
580   delete [] bins;
581 }
582
583 void AliITSclustererV2::FindClustersSPD(AliITSRawStream* input, 
584                                         TClonesArray** clusters) 
585 {
586   //------------------------------------------------------------
587   // Actual SPD cluster finder for raw data
588   //------------------------------------------------------------
589
590   Int_t nClustersSPD = 0;
591   Int_t kNzBins = fNzSPD + 2;
592   Int_t kNyBins = fNySPD + 2;
593   Int_t kMaxBin = kNzBins * kNyBins;
594   AliBin *binsSPD = new AliBin[kMaxBin];
595   AliBin *binsSPDInit = new AliBin[kMaxBin];
596   AliBin *bins = NULL;
597
598   // read raw data input stream
599   while (kTRUE) {
600     Bool_t next = input->Next();
601     if (!next || input->IsNewModule()) {
602       Int_t iModule = input->GetPrevModuleID();
603
604       // when all data from a module was read, search for clusters
605       if (bins) { 
606         clusters[iModule] = new TClonesArray("AliITSclusterV2");
607         Int_t nClusters = 0;
608
609         for (Int_t iBin = 0; iBin < kMaxBin; iBin++) {
610           if (bins[iBin].IsUsed()) continue;
611           Int_t nBins = 0;
612           Int_t idxBins[200];
613           FindCluster(iBin, kNzBins, bins, nBins, idxBins);
614           if (nBins == 200) {
615             Error("FindClustersSPD", "SPD: Too big cluster !\n"); 
616             continue;
617           }
618
619           Int_t label[4]; 
620           label[0] = -2;
621           label[1] = -2;
622           label[2] = -2;
623 //        label[3] = iModule;
624           label[3] = fNdet[iModule];
625
626           Int_t ymin = (idxBins[0] / kNzBins) - 1;
627           Int_t ymax = ymin;
628           Int_t zmin = (idxBins[0] % kNzBins) - 1;
629           Int_t zmax = zmin;
630           for (Int_t idx = 0; idx < nBins; idx++) {
631             Int_t iy = (idxBins[idx] / kNzBins) - 1;
632             Int_t iz = (idxBins[idx] % kNzBins) - 1;
633             if (ymin > iy) ymin = iy;
634             if (ymax < iy) ymax = iy;
635             if (zmin > iz) zmin = iz;
636             if (zmax < iz) zmax = iz;
637           }
638
639           Int_t idy = 0; // max 2 clusters
640           if ((iModule <= fLastSPD1) &&idy<3) idy=3;
641           if ((iModule > fLastSPD1) &&idy<4) idy=4; 
642           Int_t idz =3;
643           for (Int_t iiz=zmin; iiz<=zmax;iiz+=idz)
644             for (Int_t iiy=ymin; iiy<=ymax;iiy+=idy){
645               //
646               Int_t ndigits =0;
647               Float_t y=0.,z=0.,q=0.;    
648               for (Int_t idx = 0; idx < nBins; idx++) {
649                 Int_t iy = (idxBins[idx] / kNzBins) - 1;
650                 Int_t iz = (idxBins[idx] % kNzBins) - 1;
651                 if (zmax-zmin>=idz || ymax-ymin>=idy){
652                   if (TMath::Abs(iy-iiy)>0.75*idy) continue;
653                   if (TMath::Abs(iz-iiz)>0.75*idz) continue;
654                 }
655                 ndigits++;
656                 Float_t qBin = bins[idxBins[idx]].GetQ();
657                 y += qBin * fYSPD[iy]; 
658                 z += qBin * fZSPD[iz]; 
659                 q += qBin;   
660               }
661               if (ndigits==0) continue;
662               y /= q; 
663               z /= q;
664               y -= fHwSPD; 
665               z -= fHlSPD;
666
667               Float_t hit[5];  // y, z, sigma(y)^2, sigma(z)^2, charge
668               hit[0] = -(-y+fYshift[iModule]); 
669               if (iModule <= fLastSPD1) hit[0] = -hit[0];
670               hit[1] = -z+fZshift[iModule];
671               hit[2] = fYpitchSPD*fYpitchSPD/12.;
672               hit[3] = fZ1pitchSPD*fZ1pitchSPD/12.;
673               //          hit[4] = q;
674               hit[4] = (zmax-zmin+1)*100 + (ymax-ymin+1);
675               //          CheckLabels(label);
676               Int_t info[3]={ymax-ymin+1,zmax-zmin+1,fNlayer[iModule]};
677               new (clusters[iModule]->AddrAt(nClusters)) 
678                 AliITSclusterV2(label, hit,info); 
679               nClusters++;
680             }
681         }
682
683         nClustersSPD += nClusters;
684         bins = NULL;
685       }
686
687       if (!next) break;
688       bins = binsSPD;
689       memcpy(binsSPD,binsSPDInit,sizeof(AliBin)*kMaxBin);
690     }
691
692     // fill the current digit into the bins array
693     if(bins){
694       Int_t index = (input->GetCoord2()+1) * kNzBins + (input->GetCoord1()+1);
695       bins[index].SetIndex(index);
696       bins[index].SetMask(1);
697       bins[index].SetQ(1);
698     }
699   }
700
701   delete [] binsSPDInit;
702   delete [] binsSPD;
703
704   Info("FindClustersSPD", "found clusters in ITS SPD: %d", nClustersSPD);
705 }
706
707
708 Bool_t AliITSclustererV2::IsMaximum(Int_t k,Int_t max,const AliBin *bins) {
709   //------------------------------------------------------------
710   //is this a local maximum ?
711   //------------------------------------------------------------
712   UShort_t q=bins[k].GetQ();
713   if (q==1023) return kFALSE;
714   if (bins[k-max].GetQ() > q) return kFALSE;
715   if (bins[k-1  ].GetQ() > q) return kFALSE; 
716   if (bins[k+max].GetQ() > q) return kFALSE; 
717   if (bins[k+1  ].GetQ() > q) return kFALSE; 
718   if (bins[k-max-1].GetQ() > q) return kFALSE;
719   if (bins[k+max-1].GetQ() > q) return kFALSE; 
720   if (bins[k+max+1].GetQ() > q) return kFALSE; 
721   if (bins[k-max+1].GetQ() > q) return kFALSE;
722   return kTRUE; 
723 }
724
725 void AliITSclustererV2::
726 FindPeaks(Int_t k,Int_t max,AliBin *b,Int_t *idx,UInt_t *msk,Int_t& n) {
727   //------------------------------------------------------------
728   //find local maxima
729   //------------------------------------------------------------
730   if (n<31)
731   if (IsMaximum(k,max,b)) {
732     idx[n]=k; msk[n]=(2<<n);
733     n++;
734   }
735   b[k].SetMask(0);
736   if (b[k-max].GetMask()&1) FindPeaks(k-max,max,b,idx,msk,n);
737   if (b[k-1  ].GetMask()&1) FindPeaks(k-1  ,max,b,idx,msk,n);
738   if (b[k+max].GetMask()&1) FindPeaks(k+max,max,b,idx,msk,n);
739   if (b[k+1  ].GetMask()&1) FindPeaks(k+1  ,max,b,idx,msk,n);
740 }
741
742 void AliITSclustererV2::
743 MarkPeak(Int_t k, Int_t max, AliBin *bins, UInt_t m) {
744   //------------------------------------------------------------
745   //mark this peak
746   //------------------------------------------------------------
747   UShort_t q=bins[k].GetQ();
748
749   bins[k].SetMask(bins[k].GetMask()|m); 
750
751   if (bins[k-max].GetQ() <= q)
752      if ((bins[k-max].GetMask()&m) == 0) MarkPeak(k-max,max,bins,m);
753   if (bins[k-1  ].GetQ() <= q)
754      if ((bins[k-1  ].GetMask()&m) == 0) MarkPeak(k-1  ,max,bins,m);
755   if (bins[k+max].GetQ() <= q)
756      if ((bins[k+max].GetMask()&m) == 0) MarkPeak(k+max,max,bins,m);
757   if (bins[k+1  ].GetQ() <= q)
758      if ((bins[k+1  ].GetMask()&m) == 0) MarkPeak(k+1  ,max,bins,m);
759 }
760
761 void AliITSclustererV2::
762 MakeCluster(Int_t k,Int_t max,AliBin *bins,UInt_t m,AliITSclusterV2 &c) {
763   //------------------------------------------------------------
764   //make cluster using digits of this peak
765   //------------------------------------------------------------
766   Float_t q=(Float_t)bins[k].GetQ();
767   Int_t i=k/max, j=k-i*max;
768
769   c.SetQ(c.GetQ()+q);
770   c.SetY(c.GetY()+i*q); 
771   c.SetZ(c.GetZ()+j*q); 
772   c.SetSigmaY2(c.GetSigmaY2()+i*i*q);
773   c.SetSigmaZ2(c.GetSigmaZ2()+j*j*q);
774
775   bins[k].SetMask(0xFFFFFFFE);
776   
777   if (bins[k-max].GetMask() == m) MakeCluster(k-max,max,bins,m,c);
778   if (bins[k-1  ].GetMask() == m) MakeCluster(k-1  ,max,bins,m,c);
779   if (bins[k+max].GetMask() == m) MakeCluster(k+max,max,bins,m,c);
780   if (bins[k+1  ].GetMask() == m) MakeCluster(k+1  ,max,bins,m,c);
781 }
782
783 void AliITSclustererV2::
784 FindClustersSDD(AliBin* bins[2], Int_t nMaxBin, Int_t nzBins, 
785                 const TClonesArray *digits, TClonesArray *clusters) {
786   //------------------------------------------------------------
787   // Actual SDD cluster finder
788   //------------------------------------------------------------
789   Int_t ncl=0; TClonesArray &cl=*clusters;
790   for (Int_t s=0; s<2; s++)
791     for (Int_t i=0; i<nMaxBin; i++) {
792       if (bins[s][i].IsUsed()) continue;
793       Int_t idx[32]; UInt_t msk[32]; Int_t npeaks=0;
794       FindPeaks(i, nzBins, bins[s], idx, msk, npeaks);
795
796       if (npeaks>30) continue;
797       if (npeaks==0) continue;
798
799       Int_t k,l;
800       for (k=0; k<npeaks-1; k++){//mark adjacent peaks
801         if (idx[k] < 0) continue; //this peak is already removed
802         for (l=k+1; l<npeaks; l++) {
803            if (idx[l] < 0) continue; //this peak is already removed
804            Int_t ki=idx[k]/nzBins, kj=idx[k] - ki*nzBins;
805            Int_t li=idx[l]/nzBins, lj=idx[l] - li*nzBins;
806            Int_t di=TMath::Abs(ki - li);
807            Int_t dj=TMath::Abs(kj - lj);
808            if (di>1 || dj>1) continue;
809            if (bins[s][idx[k]].GetQ() > bins[s][idx[l]].GetQ()) {
810               msk[l]=msk[k];
811               idx[l]*=-1;
812            } else {
813               msk[k]=msk[l];
814               idx[k]*=-1;
815               break;
816            } 
817         }
818       }
819
820       for (k=0; k<npeaks; k++) {
821         MarkPeak(TMath::Abs(idx[k]), nzBins, bins[s], msk[k]);
822       }
823         
824       for (k=0; k<npeaks; k++) {
825          if (idx[k] < 0) continue; //removed peak
826          AliITSclusterV2 c;
827          MakeCluster(idx[k], nzBins, bins[s], msk[k], c);
828          //mi change
829          Int_t milab[10];
830          for (Int_t ilab=0;ilab<10;ilab++){
831            milab[ilab]=-2;
832          }
833          Int_t maxi=0,mini=0,maxj=0,minj=0;
834          //AliBin *bmax=&bins[s][idx[k]];
835          //Float_t max = TMath::Max(TMath::Abs(bmax->GetQ())/5.,3.);
836          Float_t max=3;
837          for (Int_t di=-2; di<=2;di++)
838            for (Int_t dj=-3;dj<=3;dj++){
839              Int_t index = idx[k]+di+dj*nzBins;
840              if (index<0) continue;
841              if (index>=nMaxBin) continue;
842              AliBin *b=&bins[s][index];
843              if (TMath::Abs(b->GetQ())>max){
844                if (di>maxi) maxi=di;
845                if (di<mini) mini=di;
846                if (dj>maxj) maxj=dj;
847                if (dj<minj) minj=dj;
848                //
849                if(digits) {
850                  if (TMath::Abs(di)<2&&TMath::Abs(dj)<2){
851                    AliITSdigitSDD* d=(AliITSdigitSDD*)digits->UncheckedAt(b->GetIndex());
852                    for (Int_t itrack=0;itrack<10;itrack++){
853                      Int_t track = (d->GetTracks())[itrack];
854                      if (track>=0) {
855                        AddLabel(milab, track); 
856                      }
857                    }
858                  }
859                }
860              }
861            }
862          
863          /* 
864             Float_t s2 = c.GetSigmaY2()/c.GetQ() - c.GetY()*c.GetY();
865             Float_t w=par->GetPadPitchWidth(sec);
866             c.SetSigmaY2(s2);
867             if (s2 != 0.) {
868             c.SetSigmaY2(c.GetSigmaY2()*0.108);
869             if (sec<par->GetNInnerSector()) c.SetSigmaY2(c.GetSigmaY2()*2.07);
870             }    
871             s2 = c.GetSigmaZ2()/c.GetQ() - c.GetZ()*c.GetZ();
872             w=par->GetZWidth();
873             c.SetSigmaZ2(s2);
874             
875             if (s2 != 0.) {
876             c.SetSigmaZ2(c.GetSigmaZ2()*0.169);
877             if (sec<par->GetNInnerSector()) c.SetSigmaZ2(c.GetSigmaZ2()*1.77);
878             }
879          */
880
881          Float_t y=c.GetY(),z=c.GetZ(), q=c.GetQ();
882          y/=q; z/=q;
883          //
884          //Float_t s2 = c.GetSigmaY2()/c.GetQ() - y*y;
885          // c.SetSigmaY2(s2);
886          //s2 = c.GetSigmaZ2()/c.GetQ() - z*z;
887          //c.SetSigmaZ2(s2);
888          //
889          y=(y-0.5)*fYpitchSDD;
890          y-=fHwSDD;
891          y-=fYoffSDD;  //delay ?
892          if (s) y=-y;
893
894          z=(z-0.5)*fZpitchSDD;
895          z-=fHlSDD;
896
897          y=-(-y+fYshift[fI]);
898          z=  -z+fZshift[fI];
899
900          q/=12.7;  //this WAS consistent with SSD. To be reassessed 
901                    // 23-MAR-2007
902          Float_t hit[5] = {y, z, 0.0030*0.0030, 0.0020*0.0020, q};
903          Int_t  info[3] = {maxj-minj+1, maxi-mini+1, fNlayer[fI]};
904
905          if (c.GetQ() < 20.) continue; //noise cluster
906          
907          if (digits) {    
908            //      AliBin *b=&bins[s][idx[k]];
909            //      AliITSdigitSDD* d=(AliITSdigitSDD*)digits->UncheckedAt(b->GetIndex());
910            {
911              //Int_t lab[3];
912              //lab[0]=(d->GetTracks())[0];
913              //lab[1]=(d->GetTracks())[1];
914              //lab[2]=(d->GetTracks())[2];
915              //CheckLabels(lab);
916              CheckLabels2(milab); 
917            }
918          }
919          milab[3]=fNdet[fI];
920
921          AliITSclusterV2 *cc = new (cl[ncl]) AliITSclusterV2(milab,hit,info);
922          cc->SetType(npeaks); 
923          ncl++;
924       }
925     }
926 }
927
928 void AliITSclustererV2::
929 FindClustersSDD(const TClonesArray *digits, TClonesArray *clusters) {
930   //------------------------------------------------------------
931   // Actual SDD cluster finder
932   //------------------------------------------------------------
933   Int_t kNzBins = fNzSDD + 2;
934   const Int_t kMAXBIN=kNzBins*(fNySDD+2);
935
936   AliBin *bins[2];
937   bins[0]=new AliBin[kMAXBIN];
938   bins[1]=new AliBin[kMAXBIN];
939
940   AliITSdigitSDD *d=0;
941   Int_t i, ndigits=digits->GetEntriesFast();
942   for (i=0; i<ndigits; i++) {
943      d=(AliITSdigitSDD*)digits->UncheckedAt(i);
944      Int_t y=d->GetCoord2()+1;   //y
945      Int_t z=d->GetCoord1()+1;   //z
946      Int_t q=d->GetSignal();
947      if (q<3) continue;
948
949      if (z <= fNzSDD) {
950        bins[0][y*kNzBins+z].SetQ(q);
951        bins[0][y*kNzBins+z].SetMask(1);
952        bins[0][y*kNzBins+z].SetIndex(i);
953      } else {
954        z-=fNzSDD; 
955        bins[1][y*kNzBins+z].SetQ(q);
956        bins[1][y*kNzBins+z].SetMask(1);
957        bins[1][y*kNzBins+z].SetIndex(i);
958      }
959   }
960   
961   FindClustersSDD(bins, kMAXBIN, kNzBins, digits, clusters);
962
963   delete[] bins[0];
964   delete[] bins[1];
965
966 }
967
968 void AliITSclustererV2::FindClustersSDD(AliITSRawStream* input, 
969                                         TClonesArray** clusters) 
970 {
971   //------------------------------------------------------------
972   // Actual SDD cluster finder for raw data
973   //------------------------------------------------------------
974   Int_t nClustersSDD = 0;
975   Int_t kNzBins = fNzSDD + 2;
976   Int_t kMaxBin = kNzBins * (fNySDD+2);
977   AliBin *binsSDDInit = new AliBin[kMaxBin];
978   AliBin *binsSDD1 = new AliBin[kMaxBin];
979   AliBin *binsSDD2 = new AliBin[kMaxBin];
980   AliBin *bins[2] = {NULL, NULL};
981
982   // read raw data input stream
983   while (kTRUE) {
984     Bool_t next = input->Next();
985     if (!next || input->IsNewModule()) {
986       Int_t iModule = input->GetPrevModuleID();
987
988       // when all data from a module was read, search for clusters
989       if (bins[0]) { 
990         clusters[iModule] = new TClonesArray("AliITSclusterV2");
991         fI = iModule;
992         FindClustersSDD(bins, kMaxBin, kNzBins, NULL, clusters[iModule]);
993         Int_t nClusters = clusters[iModule]->GetEntriesFast();
994         nClustersSDD += nClusters;
995         bins[0] = bins[1] = NULL;
996       }
997
998       if (!next) break;
999       bins[0]=binsSDD1;
1000       bins[1]=binsSDD2;
1001       memcpy(binsSDD1,binsSDDInit,sizeof(AliBin)*kMaxBin);
1002       memcpy(binsSDD2,binsSDDInit,sizeof(AliBin)*kMaxBin);
1003     }
1004
1005     // fill the current digit into the bins array
1006     if(input->GetSignal()>=3) {
1007       Int_t iz = input->GetCoord1()+1;
1008       Int_t side = ((iz <= fNzSDD) ? 0 : 1);
1009       iz -= side*fNzSDD;
1010       Int_t index = (input->GetCoord2()+1) * kNzBins + iz;
1011       bins[side][index].SetQ(input->GetSignal());
1012       bins[side][index].SetMask(1);
1013       bins[side][index].SetIndex(index);
1014     }
1015   }
1016
1017   delete[] binsSDD1;
1018   delete[] binsSDD2;
1019   delete[] binsSDDInit;
1020
1021   Info("FindClustersSDD", "found clusters in ITS SDD: %d", nClustersSDD);
1022 }
1023
1024
1025
1026 void AliITSclustererV2::
1027 FindClustersSSD(Ali1Dcluster* neg, Int_t nn, 
1028                 Ali1Dcluster* pos, Int_t np,
1029                 TClonesArray *clusters) {
1030   //------------------------------------------------------------
1031   // Actual SSD cluster finder
1032   //------------------------------------------------------------
1033   TClonesArray &cl=*clusters;
1034   //
1035   Float_t tanp=fTanP, tann=fTanN;
1036   if (fI>fLastSSD1) {tann=fTanP; tanp=fTanN;}
1037   Int_t idet=fNdet[fI];
1038   Int_t ncl=0;
1039   //
1040   Int_t negativepair[30000];
1041   Int_t cnegative[3000];  
1042   Int_t cused1[3000];
1043   Int_t positivepair[30000];
1044   Int_t cpositive[3000];
1045   Int_t cused2[3000];
1046   for (Int_t i=0;i<3000;i++) {cnegative[i]=0; cused1[i]=0;}
1047   for (Int_t i=0;i<3000;i++) {cpositive[i]=0; cused2[i]=0;}
1048   for (Int_t i=0;i<30000;i++){negativepair[i]=positivepair[i]=0;}
1049   static Short_t pairs[1000][1000];
1050   memset(pairs,0,sizeof(Short_t)*1000000);
1051 //   Short_t ** pairs = new Short_t*[1000];
1052 //   for (Int_t i=0; i<1000; i++) {
1053 //     pairs[i] = new Short_t[1000];
1054 //     memset(pairs[i],0,sizeof(Short_t)*1000);
1055 //   }  
1056   //
1057   // find available pairs
1058   //
1059   for (Int_t i=0; i<np; i++) {
1060     Float_t yp=pos[i].GetY()*fYpitchSSD; 
1061     if (pos[i].GetQ()<3) continue;
1062     for (Int_t j=0; j<nn; j++) {
1063       if (neg[j].GetQ()<3) continue;
1064       Float_t yn=neg[j].GetY()*fYpitchSSD;
1065       Float_t zt=(2*fHlSSD*tanp + yp - yn)/(tann+tanp);
1066       Float_t yt=yn + tann*zt;
1067       zt-=fHlSSD; yt-=fHwSSD;
1068       if (TMath::Abs(yt)<fHwSSD+0.01)
1069       if (TMath::Abs(zt)<fHlSSD+0.01*(neg[j].GetNd()+pos[i].GetNd())) {
1070         negativepair[i*10+cnegative[i]] =j;  //index
1071         positivepair[j*10+cpositive[j]] =i;
1072         cnegative[i]++;  //counters
1073         cpositive[j]++; 
1074         pairs[i][j]=100;
1075       }
1076     }
1077   }
1078   //
1079   for (Int_t i=0; i<np; i++) {
1080     Float_t yp=pos[i].GetY()*fYpitchSSD; 
1081     if (pos[i].GetQ()<3) continue;
1082     for (Int_t j=0; j<nn; j++) {
1083       if (neg[j].GetQ()<3) continue;
1084       if (cpositive[j]&&cnegative[i]) continue;
1085       Float_t yn=neg[j].GetY()*fYpitchSSD;
1086       Float_t zt=(2*fHlSSD*tanp + yp - yn)/(tann+tanp);
1087       Float_t yt=yn + tann*zt;
1088       zt-=fHlSSD; yt-=fHwSSD;
1089       if (TMath::Abs(yt)<fHwSSD+0.1)
1090       if (TMath::Abs(zt)<fHlSSD+0.15) {
1091         if (cnegative[i]==0) pos[i].SetNd(100);  // not available pair
1092         if (cpositive[j]==0) neg[j].SetNd(100);  // not available pair
1093         negativepair[i*10+cnegative[i]] =j;  //index
1094         positivepair[j*10+cpositive[j]] =i;
1095         cnegative[i]++;  //counters
1096         cpositive[j]++; 
1097         pairs[i][j]=100;
1098       }
1099     }
1100   }
1101   //
1102   Float_t lp[5];
1103   Int_t milab[10];
1104   Double_t ratio;
1105   
1106   //
1107   // sign gold tracks
1108   //
1109   for (Int_t ip=0;ip<np;ip++){
1110     Float_t ybest=1000,zbest=1000,qbest=0;
1111     //
1112     // select gold clusters
1113     if ( (cnegative[ip]==1) && cpositive[negativepair[10*ip]]==1){ 
1114       Float_t yp=pos[ip].GetY()*fYpitchSSD; 
1115       Int_t j = negativepair[10*ip];      
1116       ratio = (pos[ip].GetQ()-neg[j].GetQ())/(pos[ip].GetQ()+neg[j].GetQ());
1117       //
1118       Float_t yn=neg[j].GetY()*fYpitchSSD;
1119       Float_t zt=(2*fHlSSD*tanp + yp - yn)/(tann+tanp);
1120       Float_t yt=yn + tann*zt;
1121       zt-=fHlSSD; yt-=fHwSSD;
1122       ybest=yt; zbest=zt; 
1123       qbest=0.5*(pos[ip].GetQ()+neg[j].GetQ());
1124       lp[0]=-(-ybest+fYshift[fI]);
1125       lp[1]=  -zbest+fZshift[fI];
1126       lp[2]=0.0025*0.0025;  //SigmaY2
1127       lp[3]=0.110*0.110;  //SigmaZ2
1128       
1129       lp[4]=qbest;        //Q
1130       for (Int_t ilab=0;ilab<10;ilab++) milab[ilab]=-2;
1131       for (Int_t ilab=0;ilab<3;ilab++){
1132         milab[ilab] = pos[ip].GetLabel(ilab);
1133         milab[ilab+3] = neg[j].GetLabel(ilab);
1134       }
1135       //
1136       CheckLabels2(milab);
1137       milab[3]=(((ip<<10) + j)<<10) + idet; // pos|neg|det
1138       Int_t info[3] = {pos[ip].GetNd(),neg[j].GetNd(),fNlayer[fI]};
1139       AliITSclusterV2 * cl2 = new (cl[ncl]) AliITSclusterV2(milab,lp,info);
1140       ncl++;
1141       cl2->SetChargeRatio(ratio);       
1142       cl2->SetType(1);
1143       pairs[ip][j]=1;
1144       if ((pos[ip].GetNd()+neg[j].GetNd())>6){ //multi cluster
1145         cl2->SetType(2);
1146         pairs[ip][j]=2;
1147       }
1148       cused1[ip]++;
1149       cused2[j]++;
1150     }
1151   }
1152     
1153   for (Int_t ip=0;ip<np;ip++){
1154     Float_t ybest=1000,zbest=1000,qbest=0;
1155     //
1156     //
1157     // select "silber" cluster
1158     if ( cnegative[ip]==1 && cpositive[negativepair[10*ip]]==2){
1159       Int_t in  = negativepair[10*ip];
1160       Int_t ip2 = positivepair[10*in];
1161       if (ip2==ip) ip2 =  positivepair[10*in+1];
1162       Float_t pcharge = pos[ip].GetQ()+pos[ip2].GetQ();
1163       if (TMath::Abs(pcharge-neg[in].GetQ())<10){
1164         //
1165         // add first pair
1166         if (pairs[ip][in]==100){  //
1167           Float_t yp=pos[ip].GetY()*fYpitchSSD; 
1168           Float_t yn=neg[in].GetY()*fYpitchSSD;
1169           Float_t zt=(2*fHlSSD*tanp + yp - yn)/(tann+tanp);
1170           Float_t yt=yn + tann*zt;
1171           zt-=fHlSSD; yt-=fHwSSD;
1172           ybest =yt;  zbest=zt; 
1173           qbest =pos[ip].GetQ();
1174           lp[0]=-(-ybest+fYshift[fI]);
1175           lp[1]=  -zbest+fZshift[fI];
1176           lp[2]=0.0025*0.0025;  //SigmaY2
1177           lp[3]=0.110*0.110;  //SigmaZ2
1178           
1179           lp[4]=qbest;        //Q
1180           for (Int_t ilab=0;ilab<10;ilab++) milab[ilab]=-2;
1181           for (Int_t ilab=0;ilab<3;ilab++){
1182             milab[ilab] = pos[ip].GetLabel(ilab);
1183             milab[ilab+3] = neg[in].GetLabel(ilab);
1184           }
1185           //
1186           CheckLabels2(milab);
1187           ratio = (pos[ip].GetQ()-neg[in].GetQ())/(pos[ip].GetQ()+neg[in].GetQ());
1188           milab[3]=(((ip<<10) + in)<<10) + idet; // pos|neg|det
1189           Int_t info[3] = {pos[ip].GetNd(),neg[in].GetNd(),fNlayer[fI]};
1190           AliITSclusterV2 * cl2 = new (cl[ncl]) AliITSclusterV2(milab,lp,info);
1191           ncl++;
1192           cl2->SetChargeRatio(ratio);           
1193           cl2->SetType(5);
1194           pairs[ip][in] = 5;
1195           if ((pos[ip].GetNd()+neg[in].GetNd())>6){ //multi cluster
1196             cl2->SetType(6);
1197             pairs[ip][in] = 6;
1198           }
1199         }
1200         //
1201         // add second pair
1202         
1203         //      if (!(cused1[ip2] || cused2[in])){  //
1204         if (pairs[ip2][in]==100){
1205           Float_t yp=pos[ip2].GetY()*fYpitchSSD;
1206           Float_t yn=neg[in].GetY()*fYpitchSSD;
1207           Float_t zt=(2*fHlSSD*tanp + yp - yn)/(tann+tanp);
1208           Float_t yt=yn + tann*zt;
1209           zt-=fHlSSD; yt-=fHwSSD;
1210           ybest =yt;  zbest=zt; 
1211           qbest =pos[ip2].GetQ();
1212           lp[0]=-(-ybest+fYshift[fI]);
1213           lp[1]=  -zbest+fZshift[fI];
1214           lp[2]=0.0025*0.0025;  //SigmaY2
1215           lp[3]=0.110*0.110;  //SigmaZ2
1216           
1217           lp[4]=qbest;        //Q
1218           for (Int_t ilab=0;ilab<10;ilab++) milab[ilab]=-2;
1219           for (Int_t ilab=0;ilab<3;ilab++){
1220             milab[ilab] = pos[ip2].GetLabel(ilab);
1221             milab[ilab+3] = neg[in].GetLabel(ilab);
1222           }
1223           //
1224           CheckLabels2(milab);
1225           ratio = (pos[ip2].GetQ()-neg[in].GetQ())/(pos[ip2].GetQ()+neg[in].GetQ());
1226           milab[3]=(((ip2<<10) + in)<<10) + idet; // pos|neg|det
1227           Int_t info[3] = {pos[ip2].GetNd(),neg[in].GetNd(),fNlayer[fI]};
1228           AliITSclusterV2 *cl2 = new (cl[ncl]) AliITSclusterV2(milab,lp,info);
1229           ncl++;
1230           cl2->SetChargeRatio(ratio);           
1231           cl2->SetType(5);
1232           pairs[ip2][in] =5;
1233           if ((pos[ip2].GetNd()+neg[in].GetNd())>6){ //multi cluster
1234             cl2->SetType(6);
1235             pairs[ip2][in] =6;
1236           }
1237         }       
1238         cused1[ip]++;
1239         cused1[ip2]++;
1240         cused2[in]++;
1241       }
1242     }    
1243   }
1244   
1245   //  
1246   for (Int_t jn=0;jn<nn;jn++){
1247     if (cused2[jn]) continue;
1248     Float_t ybest=1000,zbest=1000,qbest=0;
1249     // select "silber" cluster
1250     if ( cpositive[jn]==1 && cnegative[positivepair[10*jn]]==2){
1251       Int_t ip  = positivepair[10*jn];
1252       Int_t jn2 = negativepair[10*ip];
1253       if (jn2==jn) jn2 =  negativepair[10*ip+1];
1254       Float_t pcharge = neg[jn].GetQ()+neg[jn2].GetQ();
1255       //
1256       if (TMath::Abs(pcharge-pos[ip].GetQ())<10){
1257         //
1258         // add first pair
1259         //      if (!(cused1[ip]||cused2[jn])){
1260         if (pairs[ip][jn]==100){
1261           Float_t yn=neg[jn].GetY()*fYpitchSSD; 
1262           Float_t yp=pos[ip].GetY()*fYpitchSSD;
1263           Float_t zt=(2*fHlSSD*tanp + yp - yn)/(tann+tanp);
1264           Float_t yt=yn + tann*zt;
1265           zt-=fHlSSD; yt-=fHwSSD;
1266           ybest =yt;  zbest=zt; 
1267           qbest =neg[jn].GetQ();
1268           lp[0]=-(-ybest+fYshift[fI]);
1269           lp[1]=  -zbest+fZshift[fI];
1270           lp[2]=0.0025*0.0025;  //SigmaY2
1271           lp[3]=0.110*0.110;  //SigmaZ2
1272           
1273           lp[4]=qbest;        //Q
1274           for (Int_t ilab=0;ilab<10;ilab++) milab[ilab]=-2;
1275           for (Int_t ilab=0;ilab<3;ilab++){
1276             milab[ilab] = pos[ip].GetLabel(ilab);
1277             milab[ilab+3] = neg[jn].GetLabel(ilab);
1278           }
1279           //
1280           CheckLabels2(milab);
1281           ratio = (pos[ip].GetQ()-neg[jn].GetQ())/(pos[ip].GetQ()+neg[jn].GetQ());
1282           milab[3]=(((ip<<10) + jn)<<10) + idet; // pos|neg|det
1283           Int_t info[3] = {pos[ip].GetNd(),neg[jn].GetNd(),fNlayer[fI]};
1284           AliITSclusterV2 * cl2 = new (cl[ncl]) AliITSclusterV2(milab,lp,info);
1285           ncl++;
1286           cl2->SetChargeRatio(ratio);           
1287           cl2->SetType(7);
1288           pairs[ip][jn] =7;
1289           if ((pos[ip].GetNd()+neg[jn].GetNd())>6){ //multi cluster
1290             cl2->SetType(8);
1291             pairs[ip][jn]=8;
1292           }
1293         }
1294         //
1295         // add second pair
1296         //      if (!(cused1[ip]||cused2[jn2])){
1297         if (pairs[ip][jn2]==100){
1298           Float_t yn=neg[jn2].GetY()*fYpitchSSD; 
1299           Double_t yp=pos[ip].GetY()*fYpitchSSD; 
1300           Double_t zt=(2*fHlSSD*tanp + yp - yn)/(tann+tanp);
1301           Double_t yt=yn + tann*zt;
1302           zt-=fHlSSD; yt-=fHwSSD;
1303           ybest =yt;  zbest=zt; 
1304           qbest =neg[jn2].GetQ();
1305           lp[0]=-(-ybest+fYshift[fI]);
1306           lp[1]=  -zbest+fZshift[fI];
1307           lp[2]=0.0025*0.0025;  //SigmaY2
1308           lp[3]=0.110*0.110;  //SigmaZ2
1309           
1310           lp[4]=qbest;        //Q
1311           for (Int_t ilab=0;ilab<10;ilab++) milab[ilab]=-2;
1312           for (Int_t ilab=0;ilab<3;ilab++){
1313             milab[ilab] = pos[ip].GetLabel(ilab);
1314             milab[ilab+3] = neg[jn2].GetLabel(ilab);
1315           }
1316           //
1317           CheckLabels2(milab);
1318           ratio = (pos[ip].GetQ()-neg[jn2].GetQ())/(pos[ip].GetQ()+neg[jn2].GetQ());
1319           milab[3]=(((ip<<10) + jn2)<<10) + idet; // pos|neg|det
1320           Int_t info[3] = {pos[ip].GetNd(),neg[jn2].GetNd(),fNlayer[fI]};
1321           AliITSclusterV2* cl2 = new (cl[ncl]) AliITSclusterV2(milab,lp,info);
1322           ncl++;
1323           cl2->SetChargeRatio(ratio);           
1324           pairs[ip][jn2]=7;
1325           cl2->SetType(7);
1326           if ((pos[ip].GetNd()+neg[jn2].GetNd())>6){ //multi cluster
1327             cl2->SetType(8);
1328             pairs[ip][jn2]=8;
1329           }
1330         }
1331         cused1[ip]++;
1332         cused2[jn]++;
1333         cused2[jn2]++;
1334       }
1335     }    
1336   }
1337   
1338   for (Int_t ip=0;ip<np;ip++){
1339     Float_t ybest=1000,zbest=1000,qbest=0;
1340     //
1341     // 2x2 clusters
1342     //
1343     if ( (cnegative[ip]<5) && cpositive[negativepair[10*ip]]<5){ 
1344       Float_t minchargediff =4.;
1345       Int_t j=-1;
1346       for (Int_t di=0;di<cnegative[ip];di++){
1347         Int_t   jc = negativepair[ip*10+di];
1348         Float_t chargedif = pos[ip].GetQ()-neg[jc].GetQ();
1349         if (TMath::Abs(chargedif)<minchargediff){
1350           j =jc;
1351           minchargediff = TMath::Abs(chargedif);
1352         }
1353       }
1354       if (j<0) continue;  // not proper cluster      
1355       Int_t count =0;
1356       for (Int_t di=0;di<cnegative[ip];di++){
1357         Int_t   jc = negativepair[ip*10+di];
1358         Float_t chargedif = pos[ip].GetQ()-neg[jc].GetQ();
1359         if (TMath::Abs(chargedif)<minchargediff+3.) count++;
1360       }
1361       if (count>1) continue;  // more than one "proper" cluster for positive
1362       //
1363       count =0;
1364       for (Int_t dj=0;dj<cpositive[j];dj++){
1365         Int_t   ic  = positivepair[j*10+dj];
1366         Float_t chargedif = pos[ic].GetQ()-neg[j].GetQ();
1367         if (TMath::Abs(chargedif)<minchargediff+3.) count++;
1368       }
1369       if (count>1) continue;  // more than one "proper" cluster for negative
1370       
1371       Int_t jp = 0;
1372       
1373       count =0;
1374       for (Int_t dj=0;dj<cnegative[jp];dj++){
1375         Int_t   ic = positivepair[jp*10+dj];
1376         Float_t chargedif = pos[ic].GetQ()-neg[jp].GetQ();
1377         if (TMath::Abs(chargedif)<minchargediff+4.) count++;
1378       }
1379       if (count>1) continue;   
1380       if (pairs[ip][j]<100) continue;
1381       //
1382       //almost gold clusters
1383       Float_t yp=pos[ip].GetY()*fYpitchSSD; 
1384       Float_t yn=neg[j].GetY()*fYpitchSSD;
1385       Float_t zt=(2*fHlSSD*tanp + yp - yn)/(tann+tanp);
1386       Float_t yt=yn + tann*zt;
1387       zt-=fHlSSD; yt-=fHwSSD;
1388       ybest=yt; zbest=zt; 
1389       qbest=0.5*(pos[ip].GetQ()+neg[j].GetQ());
1390       lp[0]=-(-ybest+fYshift[fI]);
1391       lp[1]=  -zbest+fZshift[fI];
1392       lp[2]=0.0025*0.0025;  //SigmaY2
1393       lp[3]=0.110*0.110;  //SigmaZ2     
1394       lp[4]=qbest;        //Q
1395       for (Int_t ilab=0;ilab<10;ilab++) milab[ilab]=-2;
1396       for (Int_t ilab=0;ilab<3;ilab++){
1397         milab[ilab] = pos[ip].GetLabel(ilab);
1398         milab[ilab+3] = neg[j].GetLabel(ilab);
1399       }
1400       //
1401       CheckLabels2(milab);
1402       ratio = (pos[ip].GetQ()-neg[j].GetQ())/(pos[ip].GetQ()+neg[j].GetQ());
1403       milab[3]=(((ip<<10) + j)<<10) + idet; // pos|neg|det
1404       Int_t info[3] = {pos[ip].GetNd(),neg[j].GetNd(),fNlayer[fI]};
1405       AliITSclusterV2 * cl2 = new (cl[ncl]) AliITSclusterV2(milab,lp,info);
1406       ncl++;
1407       cl2->SetChargeRatio(ratio);       
1408       cl2->SetType(10);
1409       pairs[ip][j]=10;
1410       if ((pos[ip].GetNd()+neg[j].GetNd())>6){ //multi cluster
1411         cl2->SetType(11);
1412         pairs[ip][j]=11;
1413       }
1414       cused1[ip]++;
1415       cused2[j]++;      
1416     }
1417
1418   }
1419   
1420   //  
1421   for (Int_t i=0; i<np; i++) {
1422     Float_t ybest=1000,zbest=1000,qbest=0;
1423     Float_t yp=pos[i].GetY()*fYpitchSSD; 
1424     if (pos[i].GetQ()<3) continue;
1425     for (Int_t j=0; j<nn; j++) {
1426     //    for (Int_t di = 0;di<cpositive[i];di++){
1427     //  Int_t j = negativepair[10*i+di];
1428       if (neg[j].GetQ()<3) continue;
1429       if (cused2[j]||cused1[i]) continue;      
1430       if (pairs[i][j]>0 &&pairs[i][j]<100) continue;
1431       ratio = (pos[i].GetQ()-neg[j].GetQ())/(pos[i].GetQ()+neg[j].GetQ());      
1432       Float_t yn=neg[j].GetY()*fYpitchSSD;
1433       Float_t zt=(2*fHlSSD*tanp + yp - yn)/(tann+tanp);
1434       Float_t yt=yn + tann*zt;
1435       zt-=fHlSSD; yt-=fHwSSD;
1436       if (TMath::Abs(yt)<fHwSSD+0.01)
1437       if (TMath::Abs(zt)<fHlSSD+0.01*(neg[j].GetNd()+pos[i].GetNd())) {
1438         ybest=yt; zbest=zt; 
1439         qbest=0.5*(pos[i].GetQ()+neg[j].GetQ());
1440         lp[0]=-(-ybest+fYshift[fI]);
1441         lp[1]=  -zbest+fZshift[fI];
1442         lp[2]=0.0025*0.0025;  //SigmaY2
1443         lp[3]=0.110*0.110;  //SigmaZ2
1444
1445         lp[4]=qbest;        //Q
1446         for (Int_t ilab=0;ilab<10;ilab++) milab[ilab]=-2;
1447         for (Int_t ilab=0;ilab<3;ilab++){
1448           milab[ilab] = pos[i].GetLabel(ilab);
1449           milab[ilab+3] = neg[j].GetLabel(ilab);
1450         }
1451         //
1452         CheckLabels2(milab);
1453         milab[3]=(((i<<10) + j)<<10) + idet; // pos|neg|det
1454         Int_t info[3] = {pos[i].GetNd(),neg[j].GetNd(),fNlayer[fI]};
1455         AliITSclusterV2 * cl2 = new (cl[ncl]) AliITSclusterV2(milab,lp,info); 
1456         ncl++;
1457         cl2->SetChargeRatio(ratio);
1458         cl2->SetType(100+cpositive[j]+cnegative[i]);
1459         //cl2->SetType(0);
1460         /*
1461           if (pairs[i][j]<100){
1462           printf("problem:- %d\n", pairs[i][j]);
1463           }
1464           if (cnegative[i]<2&&cpositive[j]<2){
1465           printf("problem:- %d\n", pairs[i][j]);
1466           }
1467         */
1468       }
1469     }
1470   }
1471
1472 //   for (Int_t i=0; i<1000; i++) delete [] pairs[i];
1473 //   delete [] pairs;
1474
1475 }
1476
1477
1478 void AliITSclustererV2::
1479 FindClustersSSD(const TClonesArray *alldigits, TClonesArray *clusters) {
1480   //------------------------------------------------------------
1481   // Actual SSD cluster finder
1482   //------------------------------------------------------------
1483   Int_t smaxall=alldigits->GetEntriesFast();
1484   if (smaxall==0) return;
1485   TObjArray *digits = new TObjArray;
1486   for (Int_t i=0;i<smaxall; i++){
1487     AliITSdigitSSD *d=(AliITSdigitSSD*)alldigits->UncheckedAt(i);
1488     if (d->GetSignal()<3) continue;
1489     digits->AddLast(d);
1490   }
1491   Int_t smax = digits->GetEntriesFast();
1492   if (smax==0) return;
1493   
1494   const Int_t MAX=1000;
1495   Int_t np=0, nn=0; 
1496   Ali1Dcluster pos[MAX], neg[MAX];
1497   Float_t y=0., q=0., qmax=0.; 
1498   Int_t lab[4]={-2,-2,-2,-2};
1499   
1500   AliITSdigitSSD *d=(AliITSdigitSSD*)digits->UncheckedAt(0);
1501   q += d->GetSignal();
1502   y += d->GetCoord2()*d->GetSignal();
1503   qmax=d->GetSignal();
1504   lab[0]=d->GetTrack(0); lab[1]=d->GetTrack(1); lab[2]=d->GetTrack(2);
1505   Int_t curr=d->GetCoord2();
1506   Int_t flag=d->GetCoord1();
1507   Int_t *n=&nn;
1508   Ali1Dcluster *c=neg;
1509   Int_t nd=1;
1510   Int_t milab[10];
1511   for (Int_t ilab=0;ilab<10;ilab++){
1512     milab[ilab]=-2;
1513   }
1514   milab[0]=d->GetTrack(0); milab[1]=d->GetTrack(1); milab[2]=d->GetTrack(2);
1515
1516   for (Int_t s=1; s<smax; s++) {
1517       d=(AliITSdigitSSD*)digits->UncheckedAt(s);      
1518       Int_t strip=d->GetCoord2();
1519       if ((strip-curr) > 1 || flag!=d->GetCoord1()) {
1520          c[*n].SetY(y/q);
1521          c[*n].SetQ(q);
1522          c[*n].SetNd(nd);
1523          CheckLabels2(milab);
1524          c[*n].SetLabels(milab);
1525          //Split suspiciously big cluster
1526          /*
1527          if (nd>10&&nd<16){
1528            c[*n].SetY(y/q-0.3*nd);
1529            c[*n].SetQ(0.5*q);
1530            (*n)++;
1531            if (*n==MAX) {
1532              Error("FindClustersSSD","Too many 1D clusters !");
1533               return;
1534            }
1535            c[*n].SetY(y/q-0.0*nd);
1536            c[*n].SetQ(0.5*q);
1537            c[*n].SetNd(nd);
1538            (*n)++;
1539            if (*n==MAX) {
1540              Error("FindClustersSSD","Too many 1D clusters !");
1541               return;
1542            }
1543            //
1544            c[*n].SetY(y/q+0.3*nd);
1545            c[*n].SetQ(0.5*q);
1546            c[*n].SetNd(nd);
1547            c[*n].SetLabels(milab);
1548          }
1549          else{
1550          */
1551          if (nd>4&&nd<25) {
1552            c[*n].SetY(y/q-0.25*nd);
1553            c[*n].SetQ(0.5*q);
1554            (*n)++;
1555            if (*n==MAX) {
1556              Error("FindClustersSSD","Too many 1D clusters !");
1557              return;
1558            }
1559            c[*n].SetY(y/q+0.25*nd);
1560            c[*n].SetQ(0.5*q);
1561            c[*n].SetNd(nd);
1562            c[*n].SetLabels(milab);
1563          }       
1564          (*n)++;
1565          if (*n==MAX) {
1566           Error("FindClustersSSD","Too many 1D clusters !");
1567           return;
1568          }
1569          y=q=qmax=0.;
1570          nd=0;
1571          lab[0]=lab[1]=lab[2]=-2;
1572          //
1573          for (Int_t ilab=0;ilab<10;ilab++){
1574            milab[ilab]=-2;
1575          }
1576          //
1577          if (flag!=d->GetCoord1()) { n=&np; c=pos; }
1578       }
1579       flag=d->GetCoord1();
1580       q += d->GetSignal();
1581       y += d->GetCoord2()*d->GetSignal();
1582       nd++;
1583       if (d->GetSignal()>qmax) {
1584          qmax=d->GetSignal();
1585          lab[0]=d->GetTrack(0); lab[1]=d->GetTrack(1); lab[2]=d->GetTrack(2);
1586       }
1587       for (Int_t ilab=0;ilab<10;ilab++) {
1588         if (d->GetTrack(ilab)>=0) AddLabel(milab, (d->GetTrack(ilab))); 
1589       }
1590       curr=strip;
1591   }
1592   c[*n].SetY(y/q);
1593   c[*n].SetQ(q);
1594   c[*n].SetNd(nd);
1595   c[*n].SetLabels(lab);
1596   //Split suspiciously big cluster
1597   if (nd>4 && nd<25) {
1598      c[*n].SetY(y/q-0.25*nd);
1599      c[*n].SetQ(0.5*q);
1600      (*n)++;
1601      if (*n==MAX) {
1602         Error("FindClustersSSD","Too many 1D clusters !");
1603         return;
1604      }
1605      c[*n].SetY(y/q+0.25*nd);
1606      c[*n].SetQ(0.5*q);
1607      c[*n].SetNd(nd);
1608      c[*n].SetLabels(lab);
1609   }
1610   (*n)++;
1611   if (*n==MAX) {
1612      Error("FindClustersSSD","Too many 1D clusters !");
1613      return;
1614   }
1615
1616   FindClustersSSD(neg, nn, pos, np, clusters);
1617 }
1618
1619 void AliITSclustererV2::FindClustersSSD(AliITSRawStream* input, 
1620                                         TClonesArray** clusters) 
1621 {
1622   //------------------------------------------------------------
1623   // Actual SSD cluster finder for raw data
1624   //------------------------------------------------------------
1625   Int_t nClustersSSD = 0;
1626   const Int_t MAX = 1000;
1627   Ali1Dcluster clusters1D[2][MAX];
1628   Int_t nClusters[2] = {0, 0};
1629   Int_t lab[3]={-2,-2,-2};
1630   Float_t q = 0.;
1631   Float_t y = 0.;
1632   Int_t nDigits = 0;
1633   Int_t prevStrip = -1;
1634   Int_t prevFlag = -1;
1635   Int_t prevModule = -1;
1636
1637   // read raw data input stream
1638   while (kTRUE) {
1639     Bool_t next = input->Next();
1640
1641     if(input->GetSignal()<3 && next) continue;
1642     // check if a new cluster starts
1643     Int_t strip = input->GetCoord2();
1644     Int_t flag = input->GetCoord1();
1645     if ((!next || (input->GetModuleID() != prevModule)||
1646          (strip-prevStrip > 1) || (flag != prevFlag)) &&
1647         (nDigits > 0)) {
1648       if (nClusters[prevFlag] == MAX) {
1649         Error("FindClustersSSD", "Too many 1D clusters !");
1650         return;
1651       }
1652       Ali1Dcluster& cluster = clusters1D[prevFlag][nClusters[prevFlag]++];
1653       cluster.SetY(y/q);
1654       cluster.SetQ(q);
1655       cluster.SetNd(nDigits);
1656       cluster.SetLabels(lab);
1657
1658       //Split suspiciously big cluster
1659       if (nDigits > 4&&nDigits < 25) {
1660         cluster.SetY(y/q - 0.25*nDigits);
1661         cluster.SetQ(0.5*q);
1662         if (nClusters[prevFlag] == MAX) {
1663           Error("FindClustersSSD", "Too many 1D clusters !");
1664           return;
1665         }
1666         Ali1Dcluster& cluster2 = clusters1D[prevFlag][nClusters[prevFlag]++];
1667         cluster2.SetY(y/q + 0.25*nDigits);
1668         cluster2.SetQ(0.5*q);
1669         cluster2.SetNd(nDigits);
1670         cluster2.SetLabels(lab);
1671       }
1672       y = q = 0.;
1673       nDigits = 0;
1674     }
1675
1676     if (!next || (input->GetModuleID() != prevModule)) {
1677       Int_t iModule = prevModule;
1678
1679       // when all data from a module was read, search for clusters
1680       if (prevFlag >= 0) {
1681         clusters[iModule] = new TClonesArray("AliITSclusterV2");
1682         fI = iModule;
1683         FindClustersSSD(&clusters1D[0][0], nClusters[0], 
1684                         &clusters1D[1][0], nClusters[1], clusters[iModule]);
1685         Int_t noClusters = clusters[iModule]->GetEntriesFast();
1686         nClustersSSD += noClusters;
1687       }
1688
1689       if (!next) break;
1690       nClusters[0] = nClusters[1] = 0;
1691       y = q = 0.;
1692       nDigits = 0;
1693     }
1694
1695     // add digit to current cluster
1696     q += input->GetSignal();
1697     y += strip * input->GetSignal();
1698     nDigits++;
1699     prevStrip = strip;
1700     prevFlag = flag;
1701     prevModule = input->GetModuleID();
1702
1703   }
1704
1705   Info("FindClustersSSD", "found clusters in ITS SSD: %d", nClustersSSD);
1706 }
1707