]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/AliMUONPreClusterFinderV2.cxx
Generation of generic AOD by the test script of MUON
[u/mrichter/AliRoot.git] / MUON / AliMUONPreClusterFinderV2.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 "AliMUONPreClusterFinderV2.h"
19
20 #include "AliLog.h"
21 #include "AliMUONCluster.h"
22 #include "AliMpVSegmentation.h"
23 #include "TClonesArray.h"
24 #include "TVector2.h"
25 #include "AliMUONPad.h"
26 #include "AliMUONVDigit.h"
27 #include "AliMUONVDigitStore.h"
28
29 //-----------------------------------------------------------------------------
30 /// \class AliMUONPreClusterFinderV2
31 ///
32 /// Implementation of AliMUONVClusterFinder
33 ///
34 /// This one ressembles the preclustering stage in the original ClusterFinderAZ
35 ///
36 /// \author Laurent Aphecetche
37 //-----------------------------------------------------------------------------
38
39 ClassImp(AliMUONPreClusterFinderV2)
40
41 //_____________________________________________________________________________
42 AliMUONPreClusterFinderV2::AliMUONPreClusterFinderV2()
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 AliMUONPreClusterFinderV2::~AliMUONPreClusterFinderV2()
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 AliMUONPreClusterFinderV2::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 AliMUONPreClusterFinderV2::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 AliMUONPreClusterFinderV2::AddPad(AliMUONCluster& cluster, AliMUONPad* pad)
146 {
147   /// Add a pad to a cluster
148   
149   cluster.AddPad(*pad);
150   pad->SetClusterId(cluster.GetUniqueID());
151   
152   Int_t cathode = pad->Cathode();
153   TClonesArray& padArray = *fPads[cathode];
154   padArray.Remove(pad);
155   TIter next(&padArray);
156   
157   // Check neighbours
158   TObjArray neighbours;
159   AliMpPad p = fSegmentations[pad->Cathode()]->PadByIndices(AliMpIntPair(pad->Ix(),pad->Iy()),kTRUE);
160   Int_t nn = fSegmentations[pad->Cathode()]->GetNeighbours(p,neighbours);
161   for (Int_t in = 0; in < nn; ++in) 
162   {
163     AliMpPad* p = static_cast<AliMpPad*>(neighbours.At(in));
164     
165     TIter next(&padArray);
166     AliMUONPad* p2;
167     
168     while ( ( p2 = static_cast<AliMUONPad*>(next()) ) )
169     {
170         if ( !p2->IsUsed() && p2->Ix()==p->GetIndices().GetFirst() 
171              && p2->Iy() == p->GetIndices().GetSecond() &&
172              p2->Cathode() == pad->Cathode() )
173         {
174           AddPad(cluster,p2);
175         }
176     }
177   } // for (Int_t in = 0;
178 }
179
180 namespace
181 {
182 //_____________________________________________________________________________
183 Bool_t
184 AreOverlapping(const AliMUONPad& pad, const AliMUONCluster& cluster)
185 {
186   /// Whether the pad overlaps with the cluster
187   
188   static Double_t precision = 1E-4; // cm
189   static TVector2 precisionAdjustment(precision,precision);//-precision,-precision);
190   for ( Int_t i = 0; i < cluster.Multiplicity(); ++i )
191   {
192     AliMUONPad* testPad = cluster.Pad(i);
193     // Note: we use negative precision numbers, meaning
194     // the area of the pads will be *increased* by these small numbers
195     // prior to check the overlap by the AreOverlapping method,
196     // so pads touching only by the corners will be considered as
197     // overlapping.    
198     if ( AliMUONPad::AreOverlapping(*testPad,pad,precisionAdjustment) )
199     {
200       return kTRUE;
201     }
202   }
203   return kFALSE;
204 }
205 }
206
207 //_____________________________________________________________________________
208 AliMUONCluster* 
209 AliMUONPreClusterFinderV2::NextCluster()
210 {
211   /// Builds the next cluster, and returns it.
212   
213   // Start a new cluster
214   Int_t id = fClusters->GetLast()+1;
215   AliMUONCluster* cluster = new ((*fClusters)[id]) AliMUONCluster;
216   cluster->SetUniqueID(id);
217   
218   AliMUONPad* pad;
219   TIter next(fPads[0]);
220   while (  ( pad = static_cast<AliMUONPad*>(next())) && pad->IsUsed() );
221
222   if (!pad) // protection against no pad in first cathode, which might happen
223   {
224     // try other cathode
225     TIter next(fPads[1]);
226     while (  ( pad = static_cast<AliMUONPad*>(next())) && pad->IsUsed() );
227     if (!pad) 
228     {
229       // we are done.
230       return 0x0;
231     }
232     // Builds (recursively) a cluster on second cathode only
233     AddPad(*cluster,pad);
234   }
235   else
236   {
237     // Builds (recursively) a cluster on first cathode only
238       
239     AddPad(*cluster,pad);
240
241     // On the 2nd cathode, only add pads overlapping with the current cluster
242     TIter next1(fPads[1]);
243     AliMUONPad* testPad;
244   
245     while ( ( testPad = static_cast<AliMUONPad*>(next1())))
246     {
247       if ( !testPad->IsUsed() && AreOverlapping(*testPad,*cluster) )
248       {
249         AddPad(*cluster,testPad);
250       }
251     }
252   }
253   
254   if ( cluster->Multiplicity() <= 1 )
255   {
256     if ( cluster->Multiplicity() == 0 ) 
257     {
258       // no pad is suspicious
259       AliWarning("Got an empty cluster...");
260     }
261     // else only 1 pad (not suspicious, but kind of useless, probably noise)
262     // so we remove it from our list
263     fClusters->Remove(cluster);
264     fClusters->Compress();
265     // then proceed further
266     return NextCluster();
267   }
268   
269   return cluster;
270 }