]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/AliMUONPreClusterFinder.cxx
- Adding comment lines to class description needed for Root documentation
[u/mrichter/AliRoot.git] / MUON / AliMUONPreClusterFinder.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 // $Id$
17
18 #include "AliMUONPreClusterFinder.h"
19
20 #include "AliLog.h"
21 #include "AliMUONCluster.h"
22 #include "AliMpVSegmentation.h"
23 #include "TClonesArray.h"
24 #include "AliMpArea.h"
25 #include "TVector2.h"
26 #include "AliMUONPad.h"
27 #include "AliMUONVDigit.h"
28 #include "AliMUONVDigitStore.h"
29 //#include "AliCodeTimer.h"
30
31 /// \class AliMUONPreClusterFinder
32 ///
33 /// Implementation of AliMUONVClusterFinder
34 ///
35 /// This class simply find adjacent pads to form clusters
36 ///
37 /// \author Laurent Aphecetche
38
39 ClassImp(AliMUONPreClusterFinder)
40
41 //_____________________________________________________________________________
42 AliMUONPreClusterFinder::AliMUONPreClusterFinder()
43 : AliMUONVClusterFinder(),
44   fClusters(0x0),
45   fSegmentations(0x0),
46   fDetElemId(0)
47 {
48     /// ctor
49   for ( Int_t i = 0; i < 2; ++i )
50   {
51     fPads[i] = 0x0;
52   } 
53 }
54
55 //_____________________________________________________________________________
56 AliMUONPreClusterFinder::~AliMUONPreClusterFinder()
57 {
58   /// dtor : note we're owner of the pads and the clusters, but not of
59   /// the remaining objects (digits, segmentations)
60   delete fClusters;
61   for ( Int_t i = 0; i < 2; ++i )
62   {
63     delete fPads[i];
64   }  
65 }
66
67 //_____________________________________________________________________________
68 Bool_t
69 AliMUONPreClusterFinder::UsePad(const AliMUONPad& pad)
70 {
71   /// Add a pad to the list of pads to be considered
72   if ( pad.DetElemId() != fDetElemId )
73   {
74     AliError(Form("Cannot add pad from DE %d to this cluster finder which is "
75                   "currently dealing with DE %d",pad.DetElemId(),fDetElemId));
76     return kFALSE;
77   }
78   
79   new ((*fPads[pad.Cathode()])[fPads[pad.Cathode()]->GetLast()+1]) AliMUONPad(pad); 
80   // FIXME: should set the ClusterId of that new pad to be -1
81   return kTRUE;
82 }
83
84 //_____________________________________________________________________________
85 Bool_t
86 AliMUONPreClusterFinder::Prepare(const AliMpVSegmentation* segmentations[2],
87                                  const AliMUONVDigitStore& digitStore)
88 // FIXME : add area on which to look for clusters here.
89 {
90   /// Prepare for clustering, by giving access to segmentations and digit lists
91   
92   fSegmentations = segmentations;
93   
94   delete fClusters;
95   fClusters = new TClonesArray("AliMUONCluster");
96   for ( Int_t i = 0; i < 2; ++i )
97   {
98     delete fPads[i];
99     fPads[i] = new TClonesArray("AliMUONPad");
100   }
101   
102   fDetElemId = -1;
103   
104   TIter next(digitStore.CreateIterator());
105   AliMUONVDigit* d;
106   
107   while ( ( d = static_cast<AliMUONVDigit*>(next()) ) )
108   {
109     Int_t ix = d->PadX();
110     Int_t iy = d->PadY();
111     Int_t cathode = d->Cathode();
112     AliMpPad pad = fSegmentations[cathode]->PadByIndices(AliMpIntPair(ix,iy));
113     TClonesArray& padArray = *(fPads[cathode]);
114     if ( fDetElemId == -1 ) 
115     {
116       fDetElemId = d->DetElemId();
117     }
118     else
119     {
120       if ( d->DetElemId() != fDetElemId ) 
121       {
122         AliError("Something is seriously wrong with DE. Aborting clustering");
123         return kFALSE;
124       }
125     }
126     
127     AliMUONPad mpad(fDetElemId,cathode,
128                     ix,iy,pad.Position().X(),pad.Position().Y(),
129                     pad.Dimensions().X(),pad.Dimensions().Y(),
130                     d->Charge());
131     if ( d->IsSaturated() ) mpad.SetSaturated(kTRUE); 
132     new (padArray[padArray.GetLast()+1]) AliMUONPad(mpad);      
133   }
134   if ( fPads[0]->GetLast() < 0 && fPads[1]->GetLast() < 0 )
135   {
136     // no pad at all, nothing to do...
137     return kFALSE;
138   }
139   
140   return kTRUE;
141 }
142
143 //_____________________________________________________________________________
144 void
145 AliMUONPreClusterFinder::AddPad(AliMUONCluster& cluster, AliMUONPad* pad)
146 {
147   /// Add a pad to a cluster
148   cluster.AddPad(*pad);
149   
150   Int_t cathode = pad->Cathode();
151   TClonesArray& padArray = *fPads[cathode];
152   // WARNING: this Remove method uses the AliMUONPad::IsEqual if that method is
153   // present (otherwise just compares pointers) : so that one must be correct
154   // if implemented !
155   padArray.Remove(pad);
156  // TObject* o = padArray.Remove(pad); 
157 //  if (!o)
158 //  {
159 //    AliFatal("Oups. Could not remove pad from pads to consider. Aborting as anyway "
160 //             " we'll get an infinite loop. Please check the AliMUONPad::IsEqual method"
161 //             " as the first suspect for failed remove");
162 //  }  
163   TIter next(&padArray);
164   AliMUONPad* testPad;
165
166   while ( ( testPad = static_cast<AliMUONPad*>(next())))
167   {
168     if ( AliMUONPad::AreNeighbours(*testPad,*pad) )
169     {
170       AddPad(cluster,testPad);
171     }
172   }
173 }
174
175 //_____________________________________________________________________________
176 Bool_t
177 AreOverlapping(const AliMUONPad& pad, const AliMUONCluster& cluster)
178 {
179   /// Whether the pad overlaps with the cluster
180   
181   static Double_t precision = 1E-4; // cm
182   static TVector2 precisionAdjustment(precision,precision);//-precision,-precision);
183   for ( Int_t i = 0; i < cluster.Multiplicity(); ++i )
184   {
185     AliMUONPad* testPad = cluster.Pad(i);
186     // Note: we use negative precision numbers, meaning
187     // the area of the pads will be *increased* by these small numbers
188     // prior to check the overlap by the AreOverlapping method,
189     // so pads touching only by the corners will be considered as
190     // overlapping.    
191     if ( AliMUONPad::AreOverlapping(*testPad,pad,precisionAdjustment) )
192     {
193       return kTRUE;
194     }
195   }
196   return kFALSE;
197 }
198
199 //_____________________________________________________________________________
200 AliMUONCluster* 
201 AliMUONPreClusterFinder::NextCluster()
202 {
203   /// Builds the next cluster, and returns it.
204   
205 //  AliCodeTimerAuto("")
206   
207   // Start a new cluster
208   Int_t id = fClusters->GetLast()+1;
209   AliMUONCluster* cluster = new ((*fClusters)[id]) AliMUONCluster;
210   cluster->SetUniqueID(id);
211   
212   TIter next(fPads[0]);
213   AliMUONPad* pad = static_cast<AliMUONPad*>(next());
214   
215   if (!pad) // protection against no pad in first cathode, which might happen
216   {
217     // try other cathode
218     TIter next(fPads[1]);
219     pad = static_cast<AliMUONPad*>(next());
220     if (!pad) 
221     {
222       // we are done.
223       return 0x0;
224     }
225     // Builds (recursively) a cluster on second cathode only
226     AddPad(*cluster,pad);
227   }
228   else
229   {
230     // Builds (recursively) a cluster on first cathode only
231     AddPad(*cluster,pad);
232     
233     // On the 2nd cathode, only add pads overlapping with the current cluster
234     TClonesArray& padArray = *fPads[1];
235     TIter next(&padArray);
236     AliMUONPad* testPad;
237   
238     while ( ( testPad = static_cast<AliMUONPad*>(next())))
239     {
240       if ( AreOverlapping(*testPad,*cluster) )
241       {
242         AddPad(*cluster,testPad);
243       }
244     }
245   }
246   
247   if ( cluster->Multiplicity() <= 1 )
248   {
249     if ( cluster->Multiplicity() == 0 ) 
250     {
251       // no pad is suspicious
252       AliWarning("Got an empty cluster...");
253     }
254     // else only 1 pad (not suspicious, but kind of useless, probably noise)
255     // so we remove it from our list
256     fClusters->Remove(cluster);
257     fClusters->Compress();
258     // then proceed further
259     return NextCluster();
260   }
261   
262   return cluster;
263 }