Create the magnetic field map in the reconstruction macros (Yuri)
[u/mrichter/AliRoot.git] / MUON / AliMUONPreClusterFinder.cxx
CommitLineData
18466557 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
18466557 20#include "AliMUONCluster.h"
18466557 21#include "AliMUONPad.h"
3887d11e 22#include "AliMUONVDigit.h"
23#include "AliMUONVDigitStore.h"
e22f734a 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>
18466557 34
3d1463c8 35//-----------------------------------------------------------------------------
18466557 36/// \class AliMUONPreClusterFinder
37///
38/// Implementation of AliMUONVClusterFinder
39///
40/// This class simply find adjacent pads to form clusters
41///
42/// \author Laurent Aphecetche
3d1463c8 43//-----------------------------------------------------------------------------
18466557 44
45ClassImp(AliMUONPreClusterFinder)
46
47//_____________________________________________________________________________
48AliMUONPreClusterFinder::AliMUONPreClusterFinder()
49: AliMUONVClusterFinder(),
50 fClusters(0x0),
e22f734a 51 fPads(0x0),
52 fDetElemId(0),
861d6ce8 53 fArea(),
54 fShouldAbort(kFALSE)
18466557 55{
e22f734a 56 /// ctor
18466557 57}
58
59//_____________________________________________________________________________
60AliMUONPreClusterFinder::~AliMUONPreClusterFinder()
61{
e22f734a 62 /// dtor : note we're owner of the clusters, but not of the pads
18466557 63 delete fClusters;
18466557 64}
65
66//_____________________________________________________________________________
67Bool_t
68AliMUONPreClusterFinder::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//_____________________________________________________________________________
84Bool_t
e22f734a 85AliMUONPreClusterFinder::Prepare(Int_t detElemId,
86 TClonesArray* pads[2],
87 const AliMpArea& area)
18466557 88{
89 /// Prepare for clustering, by giving access to segmentations and digit lists
90
18466557 91 delete fClusters;
92 fClusters = new TClonesArray("AliMUONCluster");
e22f734a 93
94 fPads = pads;
95 fDetElemId = detElemId;
96 fArea = area;
d592a053 97
861d6ce8 98 fShouldAbort = kFALSE;
99
18466557 100 return kTRUE;
101}
102
103//_____________________________________________________________________________
104void
105AliMUONPreClusterFinder::AddPad(AliMUONCluster& cluster, AliMUONPad* pad)
106{
107 /// Add a pad to a cluster
861d6ce8 108
109 if ( cluster.Multiplicity() > 500 )
110 {
111 fShouldAbort = kTRUE;
112 return;
113 }
114
18466557 115 cluster.AddPad(*pad);
116
117 Int_t cathode = pad->Cathode();
118 TClonesArray& padArray = *fPads[cathode];
d592a053 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 !
18466557 122 padArray.Remove(pad);
d592a053 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// }
18466557 130 TIter next(&padArray);
131 AliMUONPad* testPad;
d592a053 132
18466557 133 while ( ( testPad = static_cast<AliMUONPad*>(next())))
134 {
135 if ( AliMUONPad::AreNeighbours(*testPad,*pad) )
136 {
137 AddPad(cluster,testPad);
138 }
139 }
140}
141
142//_____________________________________________________________________________
143Bool_t
144AreOverlapping(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//_____________________________________________________________________________
e22f734a 167AliMUONPad*
168AliMUONPreClusterFinder::GetNextPad(Int_t cathode) const
169{
5bec63e7 170/// Return the next unused pad of given cathode, which is within fArea
171
e22f734a 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//_____________________________________________________________________________
18466557 193AliMUONCluster*
194AliMUONPreClusterFinder::NextCluster()
195{
196 /// Builds the next cluster, and returns it.
2060b217 197// AliCodeTimerAuto("pre-clustering")
d592a053 198
18466557 199 // Start a new cluster
200 Int_t id = fClusters->GetLast()+1;
201 AliMUONCluster* cluster = new ((*fClusters)[id]) AliMUONCluster;
202 cluster->SetUniqueID(id);
203
e22f734a 204 AliMUONPad* pad = GetNextPad(0);
18466557 205
206 if (!pad) // protection against no pad in first cathode, which might happen
207 {
208 // try other cathode
e22f734a 209 pad = GetNextPad(1);
18466557 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
861d6ce8 223 if ( !ShouldAbort() )
18466557 224 {
861d6ce8 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() )
18466557 231 {
861d6ce8 232 if (AreOverlapping(*testPad,*cluster) )
233 {
234 AddPad(*cluster,testPad);
235 }
18466557 236 }
237 }
238 }
239
861d6ce8 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
18466557 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}