]> git.uio.no Git - u/mrichter/AliRoot.git/blob - ITS/AliITSClusterFinder.cxx
Bug fix
[u/mrichter/AliRoot.git] / ITS / AliITSClusterFinder.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15 ////////////////////////////////////////////////////////////////////////////
16 //                                                                        //
17 // Base Class used to find                                                //
18 // the reconstructed points for ITS                                       //
19 // See also AliITSClusterFinderSPD, AliITSClusterFinderSDD,               //
20 // AliITSClusterFinderSDD  AliITSClusterFinderV2                          //
21 ////////////////////////////////////////////////////////////////////////////
22
23 #include "AliITSClusterFinder.h"
24 #include "AliITSRecPoint.h"
25 #include "AliITSdigit.h"
26 #include "AliITSDetTypeRec.h"
27 #include "AliITSMap.h"
28 #include "AliITSgeomTGeo.h"
29
30 ClassImp(AliITSClusterFinder)
31
32 //----------------------------------------------------------------------
33 AliITSClusterFinder::AliITSClusterFinder():
34 TObject(),
35 fDebug(0),
36 fModule(0),
37 fDigits(0),
38 fNdigits(0),
39 fDetTypeRec(0),
40 fClusters(0),
41 fNRawClusters(0),
42 fMap(0),
43 fNperMax(0),
44 fDeclusterFlag(0),
45 fClusterSize(0),
46 fNPeaks(-1){
47     // default cluster finder
48     // Input:
49     //   none.
50     // Output:
51     //   none.
52     // Return:
53     //   A default constructed AliITSCulsterFinder
54 }
55 //----------------------------------------------------------------------
56 AliITSClusterFinder::AliITSClusterFinder(AliITSDetTypeRec* dettyp):
57 TObject(),
58 fDebug(0),
59 fModule(0),
60 fDigits(0),
61 fNdigits(0),
62 fDetTypeRec(dettyp),
63 fClusters(0),
64 fNRawClusters(0),
65 fMap(0),
66 fNperMax(0),
67 fDeclusterFlag(0),
68 fClusterSize(0),
69 fNPeaks(-1){
70     // Standard constructor for cluster finder
71     // Input:
72     //   AliITSsegmentation *seg  The segmentation class to be used
73     //   AliITSresponse     *res  The response class to be used
74     // Output:
75     //   none.
76     // Return:
77     //   A Standard constructed AliITSCulsterFinder
78
79     SetNperMax();
80     SetClusterSize();
81     SetDeclusterFlag();
82 }
83 //----------------------------------------------------------------------
84 AliITSClusterFinder::AliITSClusterFinder(AliITSDetTypeRec* dettyp,
85                                          TClonesArray *digits):
86 TObject(),
87 fDebug(0),
88 fModule(0),
89 fDigits(digits),
90 fNdigits(0),
91 fDetTypeRec(dettyp),
92 fClusters(0),
93 fNRawClusters(0),
94 fMap(0),
95 fNperMax(0),
96 fDeclusterFlag(0),
97 fClusterSize(0),
98 fNPeaks(-1){
99     // Standard + cluster finder constructor
100     // Input:
101     //   AliITSsegmentation *seg  The segmentation class to be used
102     //   AliITSresponse     *res  The response class to be used
103     //   TClonesArray    *digits  Array of digits to be used
104     // Output:
105     //   none.
106     // Return:
107     //   A Standard constructed AliITSCulsterFinder
108
109     fNdigits = fDigits->GetEntriesFast();
110     SetNperMax();
111     SetClusterSize();
112     SetDeclusterFlag();
113 }
114
115 //______________________________________________________________________
116 AliITSClusterFinder::AliITSClusterFinder(const AliITSClusterFinder &source) : TObject(source),
117 fDebug(source.fDebug),
118 fModule(source.fModule),
119 fDigits(),
120 fNdigits(source.fNdigits),
121 fDetTypeRec(),
122 fClusters(),
123 fNRawClusters(source.fNRawClusters),
124 fMap(),
125 fNperMax(source.fNperMax),
126 fDeclusterFlag(source.fDeclusterFlag),
127 fClusterSize(source.fClusterSize),
128 fNPeaks(source.fNPeaks) {
129   // Copy constructor
130   // Copies are not allowed. The method is protected to avoid misuse.
131   AliError("Copy constructor not allowed\n");
132 }
133
134
135 //______________________________________________________________________
136 //AliITSClusterFinder& AliITSClusterFinder::operator=(const AliITSClusterFinder& /* source */){
137   // Assignment operator
138   // Assignment is not allowed. The method is protected to avoid misuse.
139 //  Fatal("= operator","Assignment operator not allowed\n");
140 //  return *this;
141 //}
142
143 //----------------------------------------------------------------------
144 AliITSClusterFinder::~AliITSClusterFinder(){
145     // destructor cluster finder
146     // Input:
147     //   none.
148     // Output:
149     //   none.
150     // Return:
151     //   none.
152
153     if(fMap) {delete fMap;}
154     // Zero local pointers. Other classes own these pointers.
155     fMap          = 0;
156     fDigits       = 0;
157     fNdigits      = 0;
158     fNRawClusters = 0;
159     fNperMax      = 0;
160     fDeclusterFlag= 0;
161     fClusterSize  = 0;
162     fNPeaks       = 0;
163     fDetTypeRec   = 0;
164
165 }
166 //__________________________________________________________________________
167 void AliITSClusterFinder::InitGeometry(){
168  //
169  // Initialisation of ITS geometry
170  //
171   Int_t mmax=AliITSgeomTGeo::GetNModules();
172   for (Int_t m=0; m<mmax; m++) {
173     Int_t lay,lad,det; AliITSgeomTGeo::GetModuleId(m,lay,lad,det);
174     fNdet[m] = (lad-1)*AliITSgeomTGeo::GetNDetectors(lay) + (det-1);
175     fNlayer[m] = lay-1;
176   }
177 }
178
179
180
181 //----------------------------------------------------------------------
182 void AliITSClusterFinder::AddCluster(Int_t branch, AliITSRawCluster *c){
183     // Add a raw cluster copy to the list
184     // Input:
185     //   Int_t       branch  The branch to which the cluster is to be added to
186     //   AliITSRawCluster *c The cluster to be added to the array of clusters
187     // Output:
188     //   none.
189     // Return:
190     //   none.
191
192    if(!fDetTypeRec) {
193     Error("AddCluster","fDetTypeRec is null!");
194     return;
195   }
196   fDetTypeRec->AddCluster(branch,c); 
197   fNRawClusters++;
198 }
199 //----------------------------------------------------------------------
200 void AliITSClusterFinder::AddCluster(Int_t branch, AliITSRawCluster *c, 
201                                      AliITSRecPoint &rp){
202     // Add a raw cluster copy to the list and the RecPoint
203     // Input:
204     //   Int_t       branch  The branch to which the cluster is to be added to
205     //   AliITSRawCluster *c The cluster to be added to the array of clusters
206     //   AliITSRecPoint  &rp The RecPoint to be added to the array of RecPoints
207     // Output:
208     //   none.
209     // Return:
210     //   none.
211   if(!fDetTypeRec) {
212     Error("AddCluster","fDetTypeRec is null!");
213     return;
214   }
215
216   fDetTypeRec->AddCluster(branch,c); 
217   fNRawClusters++;
218   fDetTypeRec->AddRecPoint(rp); 
219
220 }
221 /*
222 //______________________________________________________________________
223 void AliITSClusterFinder::CheckLabels(Int_t lab[3]) {
224   //------------------------------------------------------------
225   // Tries to find mother's labels
226   //------------------------------------------------------------
227
228   if(lab[0]<0 && lab[1]<0 && lab[2]<0) return; // In case of no labels just exit
229   // Check if simulation
230   AliMC* mc = gAlice->GetMCApp();
231   if(!mc)return;
232
233   Int_t ntracks = mc->GetNtrack();
234   for (Int_t i=0;i<3;i++){
235     Int_t label = lab[i];
236     if (label>=0 && label<ntracks) {
237       TParticle *part=(TParticle*)mc->Particle(label);
238       if (part->P() < 0.005) {
239         Int_t m=part->GetFirstMother();
240         if (m<0) {      
241           continue;
242         }
243         if (part->GetStatusCode()>0) {
244           continue;
245         }
246         lab[i]=m;       
247       }
248     }    
249   }
250   
251 }
252 */
253 //______________________________________________________________________
254 void AliITSClusterFinder::FindRawClusters(Int_t module){
255     // Default Cluster finder.
256     // Input:
257     //   Int_t module   Module number for which culster are to be found.
258     // Output:
259     //   none.
260     // Return:
261     //   none.
262     const Int_t kelms = 10;
263     Int_t ndigits = fDigits->GetEntriesFast();
264     TObjArray *digs = new TObjArray(ndigits);
265     TObjArray *clusts = new TObjArray(ndigits); // max # cluster
266     TObjArray *clust0=0; // A spacific cluster of digits
267     TObjArray *clust1=0; // A spacific cluster of digits
268     AliITSdigit *dig=0; // locat pointer to a digit
269     Int_t i=0,nc=0,j[4],k,k2=0;
270
271     // Copy all digits for this module into a local TObjArray.
272     for(i=0;i<ndigits;i++) digs->AddAt(new AliITSdigit(*(GetDigit(i))),i);
273     digs->Sort();
274     // First digit is a cluster.
275     i  = 0;
276     nc = 0;
277     clusts->AddAt(new TObjArray(kelms),nc);
278     clust0 = (TObjArray*)(clusts->At(nc));
279     clust0->AddAtFree(digs->At(i)); // move owner ship from digs to clusts
280     nc++;
281     for(i=1;i<ndigits;i++){
282         if(IsNeighbor(digs,i,j)){
283             dig = (AliITSdigit*)(digs->At(j[0]));
284             // Add to existing cluster. Find which cluster this digis 
285             for(k=0;k<nc;k++){
286                 clust0 = ((TObjArray*)(clusts->At(k)));
287                 if(clust0->IndexOf(dig)>=0) break;
288             } // end for k
289             if(k>=nc){
290                 Fatal("FindRawClusters","Digit not found as expected");
291             } // end if
292             if(j[1]>=0){
293                 dig = (AliITSdigit*)(digs->At(j[1]));
294                 // Add to existing cluster. Find which cluster this digis 
295                 for(k2=0;k2<nc;k2++){
296                     clust1 = ((TObjArray*)(clusts->At(k2)));
297                     if(clust1->IndexOf(dig)>=0) break;
298                 } // end for k2
299                 if(k2>=nc){
300                     Fatal("FindRawClusters","Digit not found as expected");
301                 } // end if
302             } // end if j[1]>=0
303             // Found cluster with neighboring digits add this one to it.
304             if(clust0==clust1){ // same cluster
305                 clust0->AddAtFree(digs->At(i));
306                 clust0 = 0; // finished with cluster. zero for safty
307                 clust1 = 0; // finished wit hcluster. zero for safty
308             }else{ // two different clusters which need to be merged.
309                 clust0->AddAtFree(digs->At(i)); // Add digit to this cluster.
310                 for(k=0;k<clust1->GetEntriesFast();k++){
311                     // move clust1 into clust0
312                     //move digit to this cluster
313                     clust0->AddAtFree(clust1->At(k));
314                     clust1->AddAt(0,k); // zero this one
315                 } // end for k
316                 delete clust1;
317                 clusts->AddAt(0,k2); // zero array of clusters element clust1
318                 clust0 = 0; // finished with cluster. zero for safty
319                 clust1 = 0; // finished wit hcluster. zero for safty
320             } // end if clust0==clust1
321         }else{// New cluster
322             clusts->AddAt(new TObjArray(kelms),nc);
323             clust0 = ((TObjArray*)(clusts->At(nc)));
324             // move owner ship from digs to clusts
325             clust0->AddAtFree(digs->At(i));
326             clust0 = 0; // finished with cluster. zero for safty
327             nc++;
328         } // End if IsNeighbor
329     } // end for i
330     // There are now nc clusters in clusts. Each element of clust is an
331     // array of digits which are clustered together.
332
333     // For each cluster call detector specific CreateRecPoints
334     for(i=0;i<nc;i++) CreateRecPoints((TObjArray*)(clusts->At(i)),module);
335
336     // clean up at the end.
337     for(i=0;i<nc;i++){ 
338         clust0 =(TObjArray*)(clusts->At(i));
339         // Digits deleted below, so zero this TObjArray
340         for(k=0;k<clust0->GetEntriesFast();k++) clust0->AddAt(0,k);
341         delete clust0; // Delete this TObjArray
342         clusts->AddAt(0,i); // Contents deleted above, so zero it.
343     } // end for i
344     delete clusts; // Delete this TObjArray/
345     // Delete the digits then the TObjArray which containted them.
346     for(i=0;i<ndigits;i++) delete ((AliITSdigit*)(digs->At(i)));
347     delete digs;
348 }
349 //______________________________________________________________________
350 Bool_t AliITSClusterFinder::IsNeighbor(TObjArray *digs,Int_t i,Int_t n[])const{
351     // Locagical function which checks to see if digit i has a neighbor.
352     // If so, then it returns kTRUE and its neighbor index j.
353     // This routine checks if the digits are side by side or one before the
354     // other. Requires that the array of digits be in proper order.
355     // Returns kTRUE in the following cases.
356     //                 ji   0j   if kdiagonal  j0    0i
357     //                 00   0i   if kdiagonal  0i    j0
358     // Inputs:
359     //    TObjArray *digs   Array to search for neighbors in
360     //    Int_t      i      Index of digit for which we are searching for
361     //                      a neighbor of.
362     // Output:
363     //    Int_t      j[4]   Index of one or more of the digits which is a
364     //                      neighbor of digit a index i.
365     // Return:
366     //    Bool_t            kTRUE if a neighbor was found kFALSE otherwise.
367     Int_t ix,jx,iz,jz,j;
368     const Bool_t kdiagonal=kFALSE;
369     Bool_t nei[4];
370
371     // No neighbors found if array empty.
372     if(digs->GetEntriesFast()<=0) return kFALSE;
373     // can not be a digit with first element or elements out or range
374     if(i<=0 || i>=digs->GetEntriesFast()) return kFALSE;
375
376     for(j=0;j<4;j++){n[j] = -1;nei[j]=kFALSE;}
377     ix = ((AliITSdigit*)(digs->At(i)))->GetCoord1();
378     iz = ((AliITSdigit*)(digs->At(i)))->GetCoord2();
379     for(j=0;j<i;j++){
380         jx = ((AliITSdigit*)(digs->At(j)))->GetCoord1();
381         jz = ((AliITSdigit*)(digs->At(j)))->GetCoord2();
382         if(jx+1==ix && jz  ==iz){n[0] = j;nei[0] = kTRUE;}
383         if(jx  ==ix && jz+1==iz){n[1] = j;nei[1] = kTRUE;}
384         if(jx+1==ix && jz+1==iz){n[2] = j;nei[2] = kTRUE;}
385         if(jx+1==ix && jz-1==iz){n[3] = j;nei[3] = kTRUE;}
386     } // end for k
387     if(nei[0]||nei[1]) return kTRUE;
388     if(kdiagonal&&(nei[2]||nei[3])) return kTRUE;
389     // no Neighbors found.
390     return kFALSE;
391 }
392
393 //______________________________________________________________________
394 void AliITSClusterFinder::Print(ostream *os) const{
395     //Standard output format for this class
396     // Inputs:
397     //    ostream *os   Output stream
398     // Output:
399     //    ostream *os   Output stream
400     // Return:
401     //    none.
402
403     *os << fDebug<<",";
404     *os << fModule<<",";
405     *os << fNdigits<<",";
406     *os << fNRawClusters<<",";
407     *os << fNperMax<<",";
408     *os << fDeclusterFlag<<",";
409     *os << fClusterSize<<",";
410     *os << fNPeaks<<endl;
411 }
412 //______________________________________________________________________
413 void AliITSClusterFinder::Read(istream *is)  {
414     //Standard input for this class
415     // Inputs:
416     //    istream *is   Input stream
417     // Output:
418     //    istream *is   Input stream
419     // Return:
420     //    none.
421
422     *is >> fDebug;
423     *is >> fModule;
424     *is >> fNdigits;
425     *is >> fNRawClusters;
426     *is >> fNperMax;
427     *is >> fDeclusterFlag;
428     *is >> fClusterSize;
429     *is >> fNPeaks;
430 }
431 //______________________________________________________________________
432 ostream &operator<<(ostream &os,AliITSClusterFinder &source){
433     // Standard output streaming function.
434     // Inputs:
435     //    ostream             *os     Output stream
436     //    AliITSClusterFinder &source Class to be printed
437     // Output:
438     //    ostream             *os     Output stream
439     // Return:
440     //    none.
441
442     source.Print(&os);
443     return os;
444 }
445 //______________________________________________________________________
446 istream &operator>>(istream &is,AliITSClusterFinder &source){
447     // Standard output streaming function.
448     // Inputs:
449     //    istream              *is      Input stream
450     //     AliITSClusterFinder &source  Class to be read in.
451     // Output:
452     //    istream              *is      Input stream
453     // Return:
454     //    none.
455
456     source.Read(&is);
457     return is;
458 }