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