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