]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/mapping/AliMpZonePainter.cxx
New class - the factory for building mapping segmentations
[u/mrichter/AliRoot.git] / MUON / mapping / AliMpZonePainter.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 // $MpId: AliMpZonePainter.cxx,v 1.6 2005/08/26 15:43:36 ivana Exp $
18 // Category: graphics
19 //
20 // Class AliMpZonePainter
21 // ----------------------
22 // Class for drawing a zone into canvas
23 // Included in AliRoot: 2003/05/02
24 // Authors: David Guez, IPN Orsay
25  
26 #include <TVirtualX.h>
27 #include <TPad.h>
28 #include <TError.h>
29
30 #include "AliMpZonePainter.h"
31 #include "AliMpGraphContext.h"
32 #include "AliMpZone.h"
33 #include "AliMpSubZone.h"
34 #include "AliMpVRowSegment.h"
35
36 ClassImp(AliMpZonePainter)
37
38 //_______________________________________________________________________
39 AliMpZonePainter::AliMpZonePainter()
40   : AliMpVPainter(),
41     fZone(0)
42 {
43   /// Default constructor
44 }
45
46 //_______________________________________________________________________
47 AliMpZonePainter::AliMpZonePainter(AliMpZone *zone)
48   : AliMpVPainter(),
49     fZone(zone)
50 {
51   /// Standard constructor 
52
53 }
54
55 //_____________________________________________________________________________
56 AliMpZonePainter::AliMpZonePainter(const AliMpZonePainter& right) 
57   : AliMpVPainter(right) 
58 {  
59   /// Protected copy constructor (not provided)
60
61   Fatal("AliMpZonePainter", "Copy constructor not provided.");
62 }
63
64 //_______________________________________________________________________
65 AliMpZonePainter::~AliMpZonePainter()
66 {
67   /// Destructor 
68 }
69
70 //_____________________________________________________________________________
71 AliMpZonePainter& 
72 AliMpZonePainter::operator=(const AliMpZonePainter& right)
73 {
74   /// Assignment operator (not provided)
75
76   // check assignment to self
77   if (this == &right) return *this;
78
79   Fatal("operator =", "Assignment operator not provided.");
80     
81   return *this;  
82 }    
83
84 //_______________________________________________________________________
85 Int_t AliMpZonePainter::DistancetoPrimitive(Int_t x, Int_t y)
86 {
87   /// Distance to the nearest segment center if (x,y) is inside the zone
88   /// 9999 otherwise
89
90   if (fZone->GetNofSubZones()<1) return 9999;
91   AliMpGraphContext *gr = AliMpGraphContext::Instance();
92
93   gr->Push();
94   InitGraphContext();
95   
96   TVector2 point = TVector2(gPad->AbsPixeltoX(x), gPad->AbsPixeltoY(y));
97
98   Double_t res=9999.;
99   for (Int_t isub=0;isub<fZone->GetNofSubZones();++isub){
100     // for each sub-zones
101     AliMpSubZone* sub = fZone->GetSubZone(isub);
102     for (Int_t iseg=0;iseg<sub->GetNofRowSegments();++iseg){
103       //for each row segments
104       AliMpVRowSegment* seg = sub->GetRowSegment(iseg);
105
106       TVector2 pos,dim;
107       gr->RealToPad(seg->Position(),seg->Dimensions(),pos,dim);
108
109       if ( IsInside(point,pos,dim) ){
110         Double_t value = (point-pos).Mod();
111         if (value<res) res=value;
112       }
113     }
114   }
115   gr->Pop();
116   return (Int_t)res;
117 }
118
119 //_______________________________________________________________________
120 void AliMpZonePainter::DumpObject()
121 {
122   /// Dump the owned object
123
124   fZone->Dump();
125 }
126
127 //_______________________________________________________________________
128 TVector2 AliMpZonePainter::GetPosition() const
129 {
130   //// Get the owned object's position
131
132   if (fZone->GetNofSubZones()<1) return TVector2(0.,0.);
133
134   TVector2 bl(9999,9999),ur(-9999,-9999);
135
136   for (Int_t isub=0;isub<fZone->GetNofSubZones();++isub){
137     // for each sub-zones
138     AliMpSubZone* sub = fZone->GetSubZone(isub);
139     for (Int_t iseg=0;iseg<sub->GetNofRowSegments();++iseg){
140       //for each row segments
141       AliMpVRowSegment* seg = sub->GetRowSegment(iseg);
142
143       // update the bottom-left corner
144       if (bl.X()>seg->Position().X()-seg->Dimensions().X())
145         bl.Set(seg->Position().X()-seg->Dimensions().X(),bl.Y());
146       if (bl.Y()>seg->Position().Y()-seg->Dimensions().Y())
147         bl.Set(bl.X(),seg->Position().Y()-seg->Dimensions().Y());
148       // update the upper-right corner
149       if (ur.X()<seg->Position().X()+seg->Dimensions().X())
150         ur.Set(seg->Position().X()+seg->Dimensions().X(),ur.Y());
151       if (ur.Y()<seg->Position().Y()+seg->Dimensions().Y())
152         ur.Set(ur.X(),seg->Position().Y()+seg->Dimensions().Y());
153     } //iseg
154   } //isub
155   return (ur+bl)/2.;
156 }
157
158 //_______________________________________________________________________
159 TVector2 AliMpZonePainter::GetDimensions() const
160 {
161   //// Get the owned object's dimensions
162
163   if (fZone->GetNofSubZones()<1) return TVector2(0.,0.);
164
165   TVector2 bl(9999,9999),ur(-9999,-9999);
166
167   for (Int_t isub=0;isub<fZone->GetNofSubZones();++isub){
168     // for each sub-zones
169     AliMpSubZone* sub = fZone->GetSubZone(isub);
170     for (Int_t iseg=0;iseg<sub->GetNofRowSegments();++iseg){
171       //for each row segments
172       AliMpVRowSegment* seg = sub->GetRowSegment(iseg);
173
174       // update the bottom-left corner
175       if (bl.X()>seg->Position().X()-seg->Dimensions().X())
176         bl.Set(seg->Position().X()-seg->Dimensions().X(),bl.Y());
177       if (bl.Y()>seg->Position().Y()-seg->Dimensions().Y())
178         bl.Set(bl.X(),seg->Position().Y()-seg->Dimensions().Y());
179       // update the upper-right corner
180       if (ur.X()<seg->Position().X()+seg->Dimensions().X())
181         ur.Set(seg->Position().X()+seg->Dimensions().X(),ur.Y());
182       if (ur.Y()<seg->Position().Y()+seg->Dimensions().Y())
183         ur.Set(ur.X(),seg->Position().Y()+seg->Dimensions().Y());
184     } //iseg
185   } //isub
186   return (ur-bl)/2.;
187 }
188
189 //_______________________________________________________________________
190 void AliMpZonePainter::Draw(Option_t *option)
191 {
192 /// Draw the sector on the current pad
193 /// The first letter of <option> is treated as follows:
194 /// - case "S" : each sub zones are drawn separately
195 /// - case ""  : the whole zone is drawn at once
196 /// in both cases, the rest of the option is passed
197 /// as argument to the Draw function of respectively
198 /// zone or row objects.
199
200   AliMpGraphContext *gr = AliMpGraphContext::Instance();
201   if (!fZone) return;
202
203   gr->Push();
204   InitGraphContext();
205   switch (option[0]){
206   case 'S':
207     {
208       for (Int_t iSubZone=0;iSubZone<fZone->GetNofSubZones();++iSubZone){
209         AliMpSubZone *subZone = fZone->GetSubZone(iSubZone);
210         gr->Push();
211
212         Double_t blx=  9999,  bly=  9999;
213         Double_t urx= -9999,  ury= -9999;
214
215           for (Int_t iRowSeg=0;iRowSeg<subZone->GetNofRowSegments();++iRowSeg){
216             AliMpVRowSegment *rowSegment = subZone->GetRowSegment(iRowSeg);
217
218             TVector2 bl = rowSegment->Position();
219             bl-=rowSegment->Dimensions();
220             TVector2 ur = rowSegment->Position();
221             ur+=rowSegment->Dimensions();
222             
223             if (bl.X()<blx) blx=bl.X();
224             if (bl.Y()<bly) bly=bl.Y();
225             if (ur.X()>urx) urx=ur.X();
226             if (ur.Y()>ury) ury=ur.Y();
227           }
228           TVector2 position ( (urx+blx)/2.,(ury+bly)/2. );
229           TVector2 dimensions( (urx-blx)/2.,(ury-bly)/2. );
230       
231           gr->SetPadPosForReal(position,dimensions);
232           gr->SetColor((fZone->GetID()-1)*5+iSubZone+2);
233           DrawObject(subZone,option+1);
234       
235           gr->Pop();
236       }
237     }
238     break;
239   default: AppendPad(option);
240   }
241   gr->Pop();
242 }
243
244 //_______________________________________________________________________
245 void AliMpZonePainter::Paint(Option_t *option)
246 {
247 //// Paint the object
248
249   AliMpGraphContext *gr = AliMpGraphContext::Instance();
250   if (!fZone) return;
251   if (fZone->GetNofSubZones()<1) return;
252   gr->Push();
253   gPad->Range(0.,0.,1.,1.);
254
255   Int_t col=gVirtualX->GetFillColor();
256   InitGraphContext();
257   
258   gVirtualX->SetFillColor(GetColor());
259   Float_t textSize =   gVirtualX->GetTextSize();
260   for (Int_t iSubZone=0;iSubZone<fZone->GetNofSubZones();++iSubZone){
261     AliMpSubZone *subZone = fZone->GetSubZone(iSubZone);
262     for (Int_t iRowSeg=0;iRowSeg<subZone->GetNofRowSegments();++iRowSeg){
263       AliMpVRowSegment *rowSegment = subZone->GetRowSegment(iRowSeg);
264       TVector2 pos,dim;
265       gr->RealToPad(rowSegment->Position(),rowSegment->Dimensions(),
266                     pos,dim);
267       gPad->PaintBox(pos.X()-dim.X(),pos.Y()-dim.Y(),
268                      pos.X()+dim.X(),pos.Y()+dim.Y());
269       if (option[0]=='T'){
270         gVirtualX->SetTextSize(15);
271         gPad->PaintText(pos.X()-0.01,pos.Y()-0.01,
272                         Form("%d",fZone->GetID()));
273       }
274     }
275   }
276   gVirtualX->SetTextSize(textSize);
277   gVirtualX->SetFillColor(col);
278   gr->Pop();
279 }