]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/AliMUONCluster.cxx
Comments for Doxygen (mostly added comments for inline functions)
[u/mrichter/AliRoot.git] / MUON / AliMUONCluster.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 <Riostream.h>
19 #include <TMath.h>
20 #include <TObjArray.h>
21 #include <TVirtualPad.h>
22 #include <TVirtualX.h>
23
24 #include "AliMUONCluster.h"
25
26 #include "AliLog.h"
27 #include "AliMUONPad.h"
28
29 /// \class AliMUONCluster
30 ///
31 /// A group of adjacent pads
32 ///
33 /// Besides holding an internal array of AliMUONPads, this object
34 /// also computes some global characteristics for that pad sets.
35 ///
36 /// \author Laurent Aphecetche
37 ///
38
39 /// \cond CLASSIMP
40 ClassImp(AliMUONCluster)
41 /// \endcond
42
43 namespace
44 {
45   //___________________________________________________________________________
46   Bool_t
47   ShouldUsePad(const AliMUONPad& pad, 
48                Int_t cathode, Int_t statusMask, Bool_t matchMask)
49   {
50
51       // FIXME : we should only use >=0 status, so we can fully
52       // use masking possibility ?
53     if ( pad.Status() < 0 ) return kFALSE;
54     
55     if ( pad.Cathode() == cathode && pad.IsReal() && !pad.IsSaturated() )
56     {
57       Bool_t test = ( ( pad.Status() & statusMask ) != 0 );
58       if ( !statusMask ) 
59       {
60         test = ( pad.Status() == 0 );
61       }
62       if ( ( test && matchMask ) || ( !test && !matchMask ) )
63       {
64         return kTRUE;
65       }
66     }
67     return kFALSE;
68   }
69
70   //___________________________________________________________________________
71   Int_t Unique(Int_t n, Double_t* array, Double_t precision)
72   {
73     /// Return the number of *different* elements in array 
74     /// where different is up to precision
75     /// Note that we assume that n is >= 1
76     
77     Int_t count(1);
78     
79     Int_t* index = new Int_t[n];
80     
81     TMath::Sort(n,array,index);
82         
83     for ( Int_t i = 1; i < n; ++i )
84     {
85       if ( array[index[i]] - array[index[i-1]] < -precision ) ++count;
86     }
87     
88     delete[] index;
89         
90     return count;
91   }
92 }
93
94 //_____________________________________________________________________________
95 AliMUONCluster::AliMUONCluster() 
96 : TObject(), 
97 fPads(0x0),
98 fHasPosition(kFALSE),
99 fPosition(1E9,1E9),
100 fPositionError(1E9,1E9),
101 fHasCharge(kFALSE),
102 fChi2(0),
103 fIsSorted(kFALSE)
104 {
105   /// ctor
106   fMultiplicity[0]=fMultiplicity[1]=0;
107   fRawCharge[0]=fRawCharge[1]=0;
108   fCharge[0]=fCharge[1]=0;
109   fIsSaturated[0]=fIsSaturated[1]=kFALSE;
110 }
111
112 //_____________________________________________________________________________
113 AliMUONCluster::AliMUONCluster(const AliMUONCluster& src)
114 : TObject(src),
115 fPads(0x0),
116 fHasPosition(kFALSE),
117 fPosition(1E9,1E9),
118 fPositionError(1E9,1E9),
119 fHasCharge(kFALSE),
120 fChi2(0),
121 fIsSorted(kFALSE)
122 {
123   /// copy ctor
124   src.Copy(*this);
125 }
126
127 //_____________________________________________________________________________
128 AliMUONCluster&
129 AliMUONCluster::operator=(const AliMUONCluster& src)
130 {
131   /// assignement operator
132   AliMUONCluster c(src);
133   c.Copy(*this);
134   return *this;
135 }
136
137 //_____________________________________________________________________________
138 AliMUONCluster::~AliMUONCluster()
139 {
140   /// dtor : note that we're owner of our pads
141   delete fPads;
142 }
143
144 //_____________________________________________________________________________
145 void
146 AliMUONCluster::AddPad(const AliMUONPad& pad)
147 {
148   /// Add a pad to our pad array, and update some internal information
149   /// accordingly.
150   /// If pad array was sorted prior to this call, we re-sort it after
151   /// actual addition.
152   
153   if (!fPads) 
154   {
155     fPads = new TObjArray(10);
156     fPads->SetOwner(kTRUE);
157   }
158   AliMUONPad* p = new AliMUONPad(pad);
159   fPads->AddLast(p);
160   p->SetClusterId(GetUniqueID());
161   Int_t cathode = p->Cathode();
162   ++(fMultiplicity[cathode]);
163   fRawCharge[cathode] += p->Charge();
164   if ( p->IsSaturated() )
165   {
166     fIsSaturated[p->Cathode()]=kTRUE;
167   }
168   if ( fIsSorted ) Sort();
169 }
170
171 //_____________________________________________________________________________
172 AliMpArea
173 AliMUONCluster::Area() const
174 {
175   /// Return the geometrical area covered by this cluster
176   
177   // Start by finding the (x,y) limits of this cluster
178   TVector2 lowerLeft(1E9,1E9);
179   TVector2 upperRight(-1E9,-1E9);
180   
181   for ( Int_t i = 0; i < Multiplicity(); ++i )
182   {
183     AliMUONPad* pad = Pad(i);
184     TVector2 ll = pad->Position() - pad->Dimensions();
185     TVector2 ur = pad->Position() + pad->Dimensions();
186     lowerLeft.Set( TMath::Min(ll.X(),lowerLeft.X()),
187                    TMath::Min(ll.Y(),lowerLeft.Y()) );
188     upperRight.Set( TMath::Max(ur.X(),upperRight.X()),
189                     TMath::Max(ur.Y(),upperRight.Y()) );
190   }
191
192   // then construct the area from those limits
193   return AliMpArea((lowerLeft+upperRight)/2,
194                    (upperRight-lowerLeft)/2);
195 }
196
197 //_____________________________________________________________________________
198 void
199 AliMUONCluster::Copy(TObject& obj) const
200 {
201   ///
202   /// Copy this cluster to (cluster&)obj
203   ///
204   TObject::Copy(obj);
205   AliMUONCluster& dest = static_cast<AliMUONCluster&>(obj);
206   dest.fPads = static_cast<TObjArray*>(fPads->Clone());
207   dest.fHasPosition = fHasPosition;
208   dest.fPosition = fPosition;
209   dest.fPositionError = fPositionError;
210   dest.fHasCharge = fHasCharge;
211   dest.fIsSorted = fIsSorted;
212   dest.fChi2 = fChi2;
213   for ( Int_t i = 0; i < 2; ++i )
214   {
215     dest.fRawCharge[i] = fRawCharge[i];
216     dest.fCharge[i] = fCharge[i];
217     dest.fMultiplicity[i] = fMultiplicity[i];
218     dest.fIsSaturated[i] = fIsSaturated[i];
219   }
220 }
221
222 //_____________________________________________________________________________
223 Float_t 
224 AliMUONCluster::Charge() const
225 {
226   /// Return the average charge over both cathodes
227   return (Charge(0)+Charge(1))/2.0;
228 }
229
230 //_____________________________________________________________________________
231 Float_t
232 AliMUONCluster::Charge(Int_t cathode) const
233 {
234   /// Returns the charge of a given cathode
235   if ( !fHasCharge ) return RawCharge(cathode);
236   
237   if ( cathode == 0 || cathode == 1 )
238   {
239     return fCharge[cathode];
240   }
241   return 0;
242 }
243
244 //_____________________________________________________________________________
245 Float_t
246 AliMUONCluster::ChargeAsymmetry() const
247 {
248   /// Returns the charge asymmetry
249   if ( Charge() > 0 )
250   {
251     return TMath::Abs(Charge(0)-Charge(1))/Charge();
252   }
253   return 0;
254 }
255
256 //_____________________________________________________________________________
257 TVector2
258 AliMUONCluster::MinPadDimensions(Int_t statusMask, Bool_t matchMask) const
259 {
260   /// Returns the minimum pad dimensions (half sizes), only considering
261   /// pads matching (or not, depending matchMask) a given mask
262   
263   TVector2 cath0(MinPadDimensions(0,statusMask,matchMask)); 
264   TVector2 cath1(MinPadDimensions(1,statusMask,matchMask)); 
265   
266   return TVector2( TMath::Min(cath0.X(),cath1.X()),
267                    TMath::Min(cath0.Y(),cath1.Y()) );
268 }
269
270 //_____________________________________________________________________________
271 TVector2
272 AliMUONCluster::MinPadDimensions(Int_t cathode, 
273                                  Int_t statusMask, Bool_t matchMask) const
274 {
275   /// Returns the minimum pad dimensions (half sizes), only considering
276   /// pads matching (or not, depending matchMask) a given mask, within a
277   /// given cathode
278   
279   Double_t xmin(1E9);
280   Double_t ymin(1E9);
281     
282   for ( Int_t i = 0; i < Multiplicity(); ++i )
283   {
284     AliMUONPad* pad = Pad(i);
285     if ( ShouldUsePad(*pad,cathode,statusMask,matchMask) )
286     {
287       xmin = TMath::Min(xmin,pad->DX());
288       ymin = TMath::Min(ymin,pad->DY());
289     }
290   }
291   return TVector2(xmin,ymin);
292 }
293
294 //_____________________________________________________________________________
295 Int_t 
296 AliMUONCluster::Multiplicity() const
297 {
298   /// Returns the total number of pads in this cluster
299   return Multiplicity(0)+Multiplicity(1);
300 }
301
302 //_____________________________________________________________________________
303 Int_t
304 AliMUONCluster::Multiplicity(Int_t cathode) const
305 {
306   /// Returns the number of pads in this cluster, in the given cathode
307   if ( cathode == 0 || cathode == 1 )
308   {
309     return fMultiplicity[cathode];
310   }
311   return 0;
312 }
313
314 //_____________________________________________________________________________
315 AliMpIntPair
316 AliMUONCluster::NofPads(Int_t statusMask, Bool_t matchMask) const
317 {
318   /// Number of pads satisfying (or not, depending matchMask) a
319   /// given mask
320   
321   Int_t nx, ny;
322   
323   TVector2 dim0(MinPadDimensions(0,statusMask,matchMask));
324   TVector2 dim1(MinPadDimensions(1,statusMask,matchMask));
325   
326   AliMpIntPair npad0(NofPads(0,statusMask,matchMask));
327   AliMpIntPair npad1(NofPads(1,statusMask,matchMask));
328   
329   if ( TMath::Abs( (dim0-dim1).X() ) < 1E-3 )
330   {
331     nx = TMath::Max( npad0.GetFirst(), npad1.GetFirst() );
332   }
333   else
334   {
335     nx = dim0.X() < dim1.X() ? npad0.GetFirst() : npad1.GetFirst();
336   }
337   
338   if ( TMath::Abs( (dim0-dim1).Y() ) < 1E-3 )
339   {
340     ny = TMath::Max( npad0.GetSecond(), npad1.GetSecond() );
341   }
342   else
343   {
344     ny = dim0.Y() < dim1.Y() ? npad0.GetSecond() : npad1.GetSecond();
345   }
346   
347   return AliMpIntPair(nx,ny);
348 }
349
350 //_____________________________________________________________________________
351 AliMpIntPair
352 AliMUONCluster::NofPads(Int_t cathode,
353                         Int_t statusMask, Bool_t matchMask) const
354 {
355   /// Number of pads of a given cathode, satisfying (or not, 
356   /// depending matchMask) a given mask
357
358   Int_t n = Multiplicity(cathode);
359   if (!n) 
360   {
361     return AliMpIntPair(0,0);
362   }
363   Double_t* x = new Double_t[n];
364   Double_t* y = new Double_t[n];
365   Int_t np(0);
366   
367   for ( Int_t i = 0; i < Multiplicity(); ++i )
368   {
369     AliMUONPad* pad = Pad(i);
370     if ( ShouldUsePad(*pad,cathode,statusMask,matchMask) )
371     {
372       x[np] = pad->X();
373       y[np] = pad->Y();
374       ++np;
375     }
376   }
377   
378   Int_t cx = Unique(np,x,0.01);
379   Int_t cy = Unique(np,y,0.01);
380   
381   delete[] x;
382   delete[] y;
383   
384   return AliMpIntPair(cx,cy);
385 }
386
387 //_____________________________________________________________________________
388 AliMUONPad*
389 AliMUONCluster::Pad(Int_t index) const
390 {
391   /// Returns the index-th pad
392   
393   if (!fPads) return 0x0;
394   if ( index < fPads->GetLast()+1 )
395   {
396     return static_cast<AliMUONPad*>(fPads->At(index));
397   }
398   else
399   {
400     AliError(Form("Requesting index %d out of bounds (%d)",index,fPads->GetLast()));
401   }
402   return 0x0;
403 }
404
405
406 //_____________________________________________________________________________
407 void
408 AliMUONCluster::Paint(Option_t*)
409 {
410   /// Paint this cluster   
411   if (!Multiplicity()) return;
412   
413   AliMpArea area(Area());
414   
415   gPad->Range(area.LeftBorder(),area.DownBorder(),area.RightBorder(),area.UpBorder());
416       
417   gVirtualX->SetFillStyle(0);
418   
419   gVirtualX->SetLineColor(2);
420   gVirtualX->SetLineWidth(4);  
421   for ( Int_t i = 0; i < Multiplicity(); ++i)
422   {
423     AliMUONPad* pad = Pad(i);
424     if ( pad->Cathode() == 0 ) pad->Paint();
425   }
426
427   gVirtualX->SetLineColor(4);
428   gVirtualX->SetLineWidth(2);  
429   for ( Int_t i = 0; i < Multiplicity(); ++i)
430   {
431     AliMUONPad* pad = Pad(i);
432     if ( pad->Cathode() == 1 ) pad->Paint();
433   }
434   
435 }
436
437 //_____________________________________________________________________________
438 void
439 AliMUONCluster::Print(Option_t* opt) const
440 {
441   /// printout
442   cout << "Cluster Id " << GetUniqueID() << " npads=" << Multiplicity() 
443   << "(" << Multiplicity(0) << "," << Multiplicity(1) << ") RawCharge=" 
444   << RawCharge() << " (" << RawCharge(0) << "," << RawCharge(1)
445   << ") Charge=(" << Charge(0) << "," << Charge(1) <<")";
446   if ( HasPosition() )
447   {
448     cout << " (x,y)=(" << Position().X() << "," << Position().Y() << ")";
449     cout << " (errX,errY)=(" << PositionError().X() << "," << PositionError().Y() << ")";
450   }
451   cout << " " << Area() << endl;
452   if (fPads) 
453   {
454     fPads->Print("",opt);
455   }
456 }
457
458 //_____________________________________________________________________________
459 void
460 AliMUONCluster::Sort()
461 {
462   /// Sort the pad array
463   fPads->Sort();
464   fIsSorted = kTRUE;
465 }
466
467 //_____________________________________________________________________________
468 void
469 AliMUONCluster::RemovePad(AliMUONPad* pad)
470 {
471   /// Remove a pad. 
472   /// As a consequence, some internal information must be updated
473   
474   fPads->Remove(pad);
475   fPads->Compress();
476   // update cluster's data
477   fIsSaturated[0]=fIsSaturated[1]=kFALSE;
478   fMultiplicity[0]=fMultiplicity[1]=0;
479   fRawCharge[0]=fRawCharge[1]=0;
480   for ( Int_t i = 0; i <= fPads->GetLast(); ++i )
481   {
482     AliMUONPad* p = Pad(i);
483     if ( p->IsSaturated() ) 
484     {
485       fIsSaturated[p->Cathode()] = kTRUE;
486     }
487     ++fMultiplicity[p->Cathode()];
488     fRawCharge[p->Cathode()] += p->Charge();
489   }
490   if (fIsSorted) Sort();
491 }
492
493 //_____________________________________________________________________________
494 Float_t 
495 AliMUONCluster::RawCharge() const
496 {
497   /// Returns the raw average charge
498   return (RawCharge(0)+RawCharge(1))/2.0;
499 }
500
501 //_____________________________________________________________________________
502 Float_t
503 AliMUONCluster::RawCharge(Int_t cathode) const
504 {
505   /// Returns the average charge of a given cathode
506   if ( cathode == 0 || cathode == 1 )
507   {
508     return fRawCharge[cathode];
509   }
510   return 0;
511 }
512
513 //_____________________________________________________________________________
514 Float_t
515 AliMUONCluster::RawChargeAsymmetry() const
516 {
517   /// Returns the raw charge asymmetry
518    if ( RawCharge() > 0 )
519    {
520      return TMath::Abs(RawCharge(0)-RawCharge(1))/RawCharge();
521    }
522   return 0;
523 }