]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/AliMUONPreClusterFinderV3.cxx
Initialisation.
[u/mrichter/AliRoot.git] / MUON / AliMUONPreClusterFinderV3.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 "AliMUONPreClusterFinderV3.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 <Riostream.h>
30 //#include "AliCodeTimer.h"
31
32 //-----------------------------------------------------------------------------
33 /// \class AliMUONPreClusterFinderV3
34 ///
35 /// Implementation of AliMUONVClusterFinder
36 ///
37 /// This version uses a 2 steps approach :
38 ///
39 /// we first clusterize each cathode independently to form proto-preclusters, 
40 /// and then we try to "merge" proto-preclusters from the two cathodes
41 /// when thoses proto-preclusters overlap, thus ending up with preclusters
42 /// spanning the two cathodes.
43 ///
44 /// This implementation, on the contrary to PreClusterFinder or PreClusterFinderV2
45 /// should not depend on the order of the input digits.
46 ///
47 /// \author Laurent Aphecetche
48 //-----------------------------------------------------------------------------
49
50 ClassImp(AliMUONPreClusterFinderV3)
51
52 namespace
53 {
54   //___________________________________________________________________________
55   Bool_t
56   AreOverlapping(const AliMUONPad& pad, const AliMUONCluster& cluster)
57   {
58     /// Whether the pad overlaps with the cluster
59     
60     static Double_t precision = 1E-4; // cm
61     static TVector2 precisionAdjustment(precision,precision);//-precision,-precision);
62       for ( Int_t i = 0; i < cluster.Multiplicity(); ++i )
63       {
64         AliMUONPad* testPad = cluster.Pad(i);
65         // Note: we use negative precision numbers, meaning
66         // the area of the pads will be *increased* by these small numbers
67         // prior to check the overlap by the AreOverlapping method,
68         // so pads touching only by the corners will be considered as
69         // overlapping.    
70         if ( AliMUONPad::AreOverlapping(*testPad,pad,precisionAdjustment) )
71         {
72           return kTRUE;
73         }
74       }
75       return kFALSE;
76   }
77 }
78
79 //_____________________________________________________________________________
80 AliMUONPreClusterFinderV3::AliMUONPreClusterFinderV3()
81 : AliMUONVClusterFinder(),
82   fClusters(new TClonesArray("AliMUONCluster",10)),
83   fSegmentations(0x0),
84   fDetElemId(0),
85   fIterator(0x0)
86 {
87     /// ctor
88     AliInfo("")
89   for ( Int_t i = 0; i < 2; ++i )
90   {
91     fPads[i] = new TClonesArray("AliMUONPad",100);
92     fPreClusters[i] = new TClonesArray("AliMUONCluster",10);
93   } 
94 }
95
96 //_____________________________________________________________________________
97 AliMUONPreClusterFinderV3::~AliMUONPreClusterFinderV3()
98 {
99   /// dtor : note we're owner of the pads and the clusters, but not of
100   /// the remaining objects (digits, segmentations)
101   delete fClusters;
102   for ( Int_t i = 0; i < 2; ++i )
103   {
104     delete fPads[i];
105     delete fPreClusters[i];
106   } 
107 }
108
109 //_____________________________________________________________________________
110 Bool_t
111 AliMUONPreClusterFinderV3::UsePad(const AliMUONPad& pad)
112 {
113   /// Add a pad to the list of pads to be considered
114   if ( pad.DetElemId() != fDetElemId )
115   {
116     AliError(Form("Cannot add pad from DE %d to this cluster finder which is "
117                   "currently dealing with DE %d",pad.DetElemId(),fDetElemId));
118     return kFALSE;
119   }
120   
121   AliMUONPad* p = new ((*fPads[pad.Cathode()])[fPads[pad.Cathode()]->GetLast()+1]) AliMUONPad(pad); 
122   p->SetClusterId(-1);
123   return kTRUE;
124 }
125
126 //_____________________________________________________________________________
127 Bool_t
128 AliMUONPreClusterFinderV3::Prepare(const AliMpVSegmentation* segmentations[2],
129                                  const AliMUONVDigitStore& digitStore)
130 {
131   /// Prepare for clustering, by giving access to segmentations and digit lists
132   // FIXME : add area on which to look for clusters here.
133   
134   fSegmentations = segmentations;
135   
136   fClusters->Clear("C");
137   for ( Int_t i = 0; i < 2; ++i )
138   {
139     fPads[i]->Clear("C");
140     fPreClusters[i]->Clear("C");
141   }
142   
143   fDetElemId = -1;
144   
145   TIter next(digitStore.CreateIterator());
146   AliMUONVDigit* d;
147   
148   while ( ( d = static_cast<AliMUONVDigit*>(next()) ) )
149   {
150     Int_t ix = d->PadX();
151     Int_t iy = d->PadY();
152     Int_t cathode = d->Cathode();
153     AliMpPad pad = fSegmentations[cathode]->PadByIndices(AliMpIntPair(ix,iy));
154     TClonesArray& padArray = *(fPads[cathode]);
155     if ( fDetElemId == -1 ) 
156     {
157       fDetElemId = d->DetElemId();
158     }
159     else
160     {
161       if ( d->DetElemId() != fDetElemId ) 
162       {
163         AliError("Something is seriously wrong with DE. Aborting clustering");
164         return kFALSE;
165       }
166     }
167     
168     AliMUONPad mpad(fDetElemId,cathode,
169                     ix,iy,pad.Position().X(),pad.Position().Y(),
170                     pad.Dimensions().X(),pad.Dimensions().Y(),
171                     d->Charge());
172     if ( d->IsSaturated() ) mpad.SetSaturated(kTRUE); 
173     mpad.SetUniqueID(d->GetUniqueID());
174     new (padArray[padArray.GetLast()+1]) AliMUONPad(mpad);      
175   }
176   if ( fPads[0]->GetLast() < 0 && fPads[1]->GetLast() < 0 )
177   {
178     // no pad at all, nothing to do...
179     return kFALSE;
180   }
181   
182   MakeCathodePreClusters(0);  
183   MakeCathodePreClusters(1);  
184   MakeClusters();
185   
186   delete fIterator;
187   fIterator = fClusters->MakeIterator();
188   
189   return kTRUE;
190 }
191
192 //_____________________________________________________________________________
193 void
194 AliMUONPreClusterFinderV3::DumpPreClusters() const
195 {
196   /// Dump preclusters 
197   AliMUONCluster *c;
198   TIter next0(fPreClusters[0]);
199   TIter next1(fPreClusters[1]);
200   cout << "Cath0" << endl;
201   while ( ( c = static_cast<AliMUONCluster*>(next0())) ) 
202   {
203     cout << c->AsString().Data() << endl;
204   }
205   cout << "Cath1" << endl;
206   while ( ( c = static_cast<AliMUONCluster*>(next1())) ) 
207   {
208     cout << c->AsString().Data() << endl;
209   }
210 }
211
212 //_____________________________________________________________________________
213 void
214 AliMUONPreClusterFinderV3::AddPreCluster(AliMUONCluster& cluster, AliMUONCluster* preCluster)
215 {
216   /// Add a pad to a cluster
217
218   AliMUONCluster a(*preCluster);
219
220   Int_t cathode = preCluster->Cathode();
221   if ( cathode <=1 && !fPreClusters[cathode]->Remove(preCluster) ) 
222   {
223     AliError(Form("Could not remove %s from preclusters[%d]",
224                   preCluster->AsString().Data(),cathode));
225     StdoutToAliDebug(1,DumpPreClusters());
226     AliFatal("");
227   }
228              
229   cluster.AddCluster(a);
230   
231   // loop on the *other* cathode
232   TIter next(fPreClusters[1-cathode]);
233   AliMUONCluster* testCluster;
234   
235   while ( ( testCluster = static_cast<AliMUONCluster*>(next())))
236   {
237     if ( AliMUONCluster::AreOverlapping(a,*testCluster) )
238     {
239       AddPreCluster(cluster,testCluster);
240     }
241   }
242 }
243
244
245 //_____________________________________________________________________________
246 void
247 AliMUONPreClusterFinderV3::AddPad(AliMUONCluster& cluster, AliMUONPad* pad)
248 {
249   /// Add a pad to a cluster
250   AliMUONPad* addedPad = cluster.AddPad(*pad);
251   
252   Int_t cathode = pad->Cathode();
253   TClonesArray& padArray = *fPads[cathode];
254   padArray.Remove(pad);
255   TIter next(&padArray);
256   AliMUONPad* testPad;
257   
258   while ( ( testPad = static_cast<AliMUONPad*>(next())))
259   {
260     if ( AliMUONPad::AreNeighbours(*testPad,*addedPad) )
261     {
262       AddPad(cluster,testPad);
263     }
264   }
265 }
266
267 //_____________________________________________________________________________
268 AliMUONCluster* 
269 AliMUONPreClusterFinderV3::NextCluster()
270 {
271   /// Returns the next cluster
272   
273   return static_cast<AliMUONCluster*>(fIterator->Next());
274 }
275
276 //_____________________________________________________________________________
277 void
278 AliMUONPreClusterFinderV3::MakeClusters()
279 {
280   /// Associate (proto)preclusters to form (pre)clusters
281   
282 //  AliCodeTimerAuto("")
283   
284   for ( Int_t cathode = 0; cathode < 2; ++cathode ) 
285   {
286     TClonesArray& preclusters = *(fPreClusters[cathode]);
287     
288     TIter next(&preclusters);
289     AliMUONCluster* preCluster(0x0);
290     
291     while ( ( preCluster = static_cast<AliMUONCluster*>(next()) ) )
292     {
293       Int_t id(fClusters->GetLast()+1);
294       AliMUONCluster* cluster = new((*fClusters)[id]) AliMUONCluster;
295       cluster->SetUniqueID(id);      
296       AddPreCluster(*cluster,preCluster);
297     }
298   }
299 }
300
301 //_____________________________________________________________________________
302 void
303 AliMUONPreClusterFinderV3::MakeCathodePreClusters(Int_t cathode)
304 {
305   /// Build (proto)preclusters from digits on a given cathode
306   
307 //  AliCodeTimerAuto(Form("Cathode %d",cathode))
308   
309   while ( fPads[cathode]->GetLast() > 0  )
310   {  
311     TIter next(fPads[cathode]);
312     AliMUONPad* pad = static_cast<AliMUONPad*>(next());
313   
314     if (!pad) AliFatal("");
315
316     Int_t id = fPreClusters[cathode]->GetLast()+1;
317     AliMUONCluster* cluster = new ((*fPreClusters[cathode])[id]) AliMUONCluster;
318     cluster->SetUniqueID(id);
319     
320     // Builds (recursively) a cluster on first cathode only
321     AddPad(*cluster,pad);
322     
323     if ( cluster->Multiplicity() <= 1 )
324     {
325       if ( cluster->Multiplicity() == 0 ) 
326       {
327         // no pad is suspicious
328         AliWarning("Got an empty cluster...");
329       }
330       // else only 1 pad (not suspicious, but kind of useless, probably noise)
331       // so we remove it from our list
332       fPreClusters[cathode]->Remove(cluster);
333       // then proceed further
334     }
335   }
336   
337 }