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