ROOT  6.06/09
Reference Guide
TPolyMarker.cxx
Go to the documentation of this file.
1 // @(#)root/hist:$Id$
2 // Author: Rene Brun 12/12/94
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 #include "Riostream.h"
13 #include "TROOT.h"
14 #include "TVirtualPad.h"
15 #include "TPolyMarker.h"
16 #include "TClass.h"
17 #include "TMath.h"
18 
20 
21 
22 /** \class TPolyMarker
23  \ingroup Hist
24 A PolyMarker is defined by an array on N points in a 2-D space.
25 At each point x[i], y[i] a marker is drawn.
26 Marker attributes are managed by TAttMarker.
27 See TMarker for the list of possible marker types.
28 */
29 
30 
31 ////////////////////////////////////////////////////////////////////////////////
32 /// Default constructor.
33 
35 {
36  fN = 0;
37  fX = fY = 0;
38  fLastPoint = -1;
39 }
40 
41 
42 ////////////////////////////////////////////////////////////////////////////////
43 /// Constructor.
44 
46  :TObject(), TAttMarker()
47 {
48  fOption = option;
50  fLastPoint = -1;
51  if (n <= 0) {
52  fN = 0;
53  fLastPoint = -1;
54  fX = fY = 0;
55  return;
56  }
57  fN = n;
58  fX = new Double_t [fN];
59  fY = new Double_t [fN];
60 }
61 
62 
63 ////////////////////////////////////////////////////////////////////////////////
64 /// Constructor.
65 
67  :TObject(), TAttMarker()
68 {
69  fOption = option;
71  fLastPoint = -1;
72  if (n <= 0) {
73  fN = 0;
74  fLastPoint = -1;
75  fX = fY = 0;
76  return;
77  }
78  fN = n;
79  fX = new Double_t [fN];
80  fY = new Double_t [fN];
81  if (!x || !y) return;
82  for (Int_t i=0; i<fN;i++) { fX[i] = x[i]; fY[i] = y[i]; }
83  fLastPoint = fN-1;
84 }
85 
86 
87 ////////////////////////////////////////////////////////////////////////////////
88 /// Constructor.
89 
91  :TObject(), TAttMarker()
92 {
93  fOption = option;
95  fLastPoint = -1;
96  if (n <= 0) {
97  fN = 0;
98  fLastPoint = -1;
99  fX = fY = 0;
100  return;
101  }
102  fN = n;
103  fX = new Double_t [fN];
104  fY = new Double_t [fN];
105  if (!x || !y) return;
106  for (Int_t i=0; i<fN;i++) { fX[i] = x[i]; fY[i] = y[i]; }
107  fLastPoint = fN-1;
108 }
109 
110 ////////////////////////////////////////////////////////////////////////////////
111 ///assignment operator
112 
114 {
115  if(this!=&pm) {
116  TObject::operator=(pm);
118  fN=pm.fN;
120  // delete first previous existing fX and fY
121  if (fX) delete [] fX;
122  if (fY) delete [] fY;
123  fX=pm.fX;
124  fY=pm.fY;
125  fOption=pm.fOption;
126  }
127  return *this;
128 }
129 
130 ////////////////////////////////////////////////////////////////////////////////
131 /// Desctructor.
132 
134 {
135  if (fX) delete [] fX;
136  if (fY) delete [] fY;
137  fLastPoint = -1;
138 }
139 
140 
141 ////////////////////////////////////////////////////////////////////////////////
142 /// Copy constructor.
143 
144 TPolyMarker::TPolyMarker(const TPolyMarker &polymarker) : TObject(polymarker), TAttMarker(polymarker)
145 {
146  fN = 0;
147  fX = fY = 0;
148  fLastPoint = -1;
149  ((TPolyMarker&)polymarker).Copy(*this);
150 }
151 
152 
153 ////////////////////////////////////////////////////////////////////////////////
154 
156 {
157  // Copy.
158 
159  TObject::Copy(obj);
160  TAttMarker::Copy(((TPolyMarker&)obj));
161  ((TPolyMarker&)obj).fN = fN;
162  // delete first previous existing fX and fY
163  if (((TPolyMarker&)obj).fX) delete [] (((TPolyMarker&)obj).fX);
164  if (((TPolyMarker&)obj).fY) delete [] (((TPolyMarker&)obj).fY);
165  if (fN > 0) {
166  ((TPolyMarker&)obj).fX = new Double_t [fN];
167  ((TPolyMarker&)obj).fY = new Double_t [fN];
168  for (Int_t i=0; i<fN;i++) { ((TPolyMarker&)obj).fX[i] = fX[i], ((TPolyMarker&)obj).fY[i] = fY[i]; }
169  } else {
170  ((TPolyMarker&)obj).fX = 0;
171  ((TPolyMarker&)obj).fY = 0;
172  }
173  ((TPolyMarker&)obj).fOption = fOption;
174  ((TPolyMarker&)obj).fLastPoint = fLastPoint;
175 }
176 
177 
178 ////////////////////////////////////////////////////////////////////////////////
179 /// Compute distance from point px,py to a polymarker.
180 ///
181 /// Compute the closest distance of approach from point px,py to each point
182 /// of the polymarker.
183 /// Returns when the distance found is below DistanceMaximum.
184 /// The distance is computed in pixels units.
185 
187 {
188  const Int_t big = 9999;
189 
190  // check if point is near one of the points
191  Int_t i, pxp, pyp, d;
192  Int_t distance = big;
193 
194  for (i=0;i<Size();i++) {
195  pxp = gPad->XtoAbsPixel(gPad->XtoPad(fX[i]));
196  pyp = gPad->YtoAbsPixel(gPad->YtoPad(fY[i]));
197  d = TMath::Abs(pxp-px) + TMath::Abs(pyp-py);
198  if (d < distance) distance = d;
199  }
200  return distance;
201 }
202 
203 
204 ////////////////////////////////////////////////////////////////////////////////
205 /// Draw.
206 
208 {
209  AppendPad(option);
210 }
211 
212 
213 ////////////////////////////////////////////////////////////////////////////////
214 /// Draw polymarker.
215 
217 {
218  TPolyMarker *newpolymarker = new TPolyMarker(n,x,y);
219  TAttMarker::Copy(*newpolymarker);
220  newpolymarker->fOption = fOption;
221  newpolymarker->SetBit(kCanDelete);
222  newpolymarker->AppendPad();
223 }
224 
225 
226 ////////////////////////////////////////////////////////////////////////////////
227 /// Execute action corresponding to one event.
228 ///
229 /// This member function must be implemented to realize the action
230 /// corresponding to the mouse click on the object in the window
231 
233 {
234 }
235 
236 
237 ////////////////////////////////////////////////////////////////////////////////
238 /// ls.
239 
241 {
243  printf("TPolyMarker N=%d\n",fN);
244 }
245 
246 
247 ////////////////////////////////////////////////////////////////////////////////
248 /// Merge polymarkers in the collection in this polymarker.
249 
251 {
252  if (!li) return 0;
253  TIter next(li);
254 
255  //first loop to count the number of entries
256  TPolyMarker *pm;
257  Int_t npoints = 0;
258  while ((pm = (TPolyMarker*)next())) {
259  if (!pm->InheritsFrom(TPolyMarker::Class())) {
260  Error("Add","Attempt to add object of class: %s to a %s",pm->ClassName(),this->ClassName());
261  return -1;
262  }
263  npoints += pm->Size();
264  }
265 
266  //extend this polymarker to hold npoints
267  SetPoint(npoints-1,0,0);
268 
269  //merge all polymarkers
270  next.Reset();
271  while ((pm = (TPolyMarker*)next())) {
272  Int_t np = pm->Size();
273  Double_t *x = pm->GetX();
274  Double_t *y = pm->GetY();
275  for (Int_t i=0;i<np;i++) {
276  SetPoint(i,x[i],y[i]);
277  }
278  }
279 
280  return npoints;
281 }
282 
283 
284 ////////////////////////////////////////////////////////////////////////////////
285 /// Paint.
286 
288 {
289  PaintPolyMarker(fLastPoint+1, fX, fY, option);
290 }
291 
292 
293 ////////////////////////////////////////////////////////////////////////////////
294 /// Paint polymarker.
295 
297 {
298  if (n <= 0) return;
299  TAttMarker::Modify(); //Change marker attributes only if necessary
300  Double_t *xx = x;
301  Double_t *yy = y;
302  if (gPad->GetLogx()) {
303  xx = new Double_t[n];
304  for (Int_t ix=0;ix<n;ix++) xx[ix] = gPad->XtoPad(x[ix]);
305  }
306  if (gPad->GetLogy()) {
307  yy = new Double_t[n];
308  for (Int_t iy=0;iy<n;iy++) yy[iy] = gPad->YtoPad(y[iy]);
309  }
310  gPad->PaintPolyMarker(n,xx,yy,option);
311  if (x != xx) delete [] xx;
312  if (y != yy) delete [] yy;
313 }
314 
315 
316 ////////////////////////////////////////////////////////////////////////////////
317 /// Print polymarker.
318 
320 {
321  printf("TPolyMarker N=%d\n",fN);
322 }
323 
324 
325 ////////////////////////////////////////////////////////////////////////////////
326 /// Save primitive as a C++ statement(s) on output stream out.
327 
328 void TPolyMarker::SavePrimitive(std::ostream &out, Option_t *option /*= ""*/)
329 {
330  char quote = '"';
331  out<<" "<<std::endl;
332  out<<" Double_t *dum = 0;"<<std::endl;
333  if (gROOT->ClassSaved(TPolyMarker::Class())) {
334  out<<" ";
335  } else {
336  out<<" TPolyMarker *";
337  }
338  out<<"pmarker = new TPolyMarker("<<fN<<",dum,dum,"<<quote<<fOption<<quote<<");"<<std::endl;
339 
340  SaveMarkerAttributes(out,"pmarker",1,1,1);
341 
342  for (Int_t i=0;i<Size();i++) {
343  out<<" pmarker->SetPoint("<<i<<","<<fX[i]<<","<<fY[i]<<");"<<std::endl;
344  }
345  out<<" pmarker->Draw("
346  <<quote<<option<<quote<<");"<<std::endl;
347 }
348 
349 
350 ////////////////////////////////////////////////////////////////////////////////
351 /// Set point following LastPoint to x, y.
352 /// Returns index of the point (new last point).
353 
355 {
356  fLastPoint++;
357  SetPoint(fLastPoint, x, y);
358  return fLastPoint;
359 }
360 
361 
362 ////////////////////////////////////////////////////////////////////////////////
363 /// Set point number n.
364 /// if n is greater than the current size, the arrays are automatically
365 /// extended
366 
368 {
369  if (n < 0) return;
370  if (!fX || !fY || n >= fN) {
371  // re-allocate the object
372  Int_t newN = TMath::Max(2*fN,n+1);
373  Double_t *savex = new Double_t [newN];
374  Double_t *savey = new Double_t [newN];
375  if (fX && fN){
376  memcpy(savex,fX,fN*sizeof(Double_t));
377  memset(&savex[fN],0,(newN-fN)*sizeof(Double_t));
378  delete [] fX;
379  }
380  if (fY && fN){
381  memcpy(savey,fY,fN*sizeof(Double_t));
382  memset(&savey[fN],0,(newN-fN)*sizeof(Double_t));
383  delete [] fY;
384  }
385  fX = savex;
386  fY = savey;
387  fN = newN;
388  }
389  fX[n] = x;
390  fY[n] = y;
392 }
393 
394 
395 ////////////////////////////////////////////////////////////////////////////////
396 /// If n <= 0 the current arrays of points are deleted.
397 
399 {
400  if (n <= 0) {
401  fN = 0;
402  fLastPoint = -1;
403  delete [] fX;
404  delete [] fY;
405  fX = fY = 0;
406  return;
407  }
408  SetPoint(n-1,0,0);
409 }
410 
411 
412 ////////////////////////////////////////////////////////////////////////////////
413 /// If n <= 0 the current arrays of points are deleted.
414 
416 {
417  if (n <= 0) {
418  fN = 0;
419  fLastPoint = -1;
420  delete [] fX;
421  delete [] fY;
422  fX = fY = 0;
423  return;
424  }
425  fN =n;
426  if (fX) delete [] fX;
427  if (fY) delete [] fY;
428  fX = new Double_t[fN];
429  fY = new Double_t[fN];
430  for (Int_t i=0; i<fN;i++) {
431  if (x) fX[i] = (Double_t)x[i];
432  if (y) fY[i] = (Double_t)y[i];
433  }
434  fOption = option;
435  fLastPoint = fN-1;
436 }
437 
438 
439 ////////////////////////////////////////////////////////////////////////////////
440 /// If n <= 0 the current arrays of points are deleted.
441 
443 {
444  if (n <= 0) {
445  fN = 0;
446  fLastPoint = -1;
447  delete [] fX;
448  delete [] fY;
449  fX = fY = 0;
450  return;
451  }
452  fN =n;
453  if (fX) delete [] fX;
454  if (fY) delete [] fY;
455  fX = new Double_t[fN];
456  fY = new Double_t[fN];
457  for (Int_t i=0; i<fN;i++) {
458  if (x) fX[i] = x[i];
459  if (y) fY[i] = y[i];
460  }
461  fOption = option;
462  fLastPoint = fN-1;
463 }
464 
465 
466 ////////////////////////////////////////////////////////////////////////////////
467 /// Stream a class object.
468 
469 void TPolyMarker::Streamer(TBuffer &R__b)
470 {
471  if (R__b.IsReading()) {
472  UInt_t R__s, R__c;
473  Version_t R__v = R__b.ReadVersion(&R__s, &R__c);
474  if (R__v > 1) {
475  R__b.ReadClassBuffer(TPolyMarker::Class(), this, R__v, R__s, R__c);
476  return;
477  }
478  //====process old versions before automatic schema evolution
479  TObject::Streamer(R__b);
480  TAttMarker::Streamer(R__b);
481  R__b >> fN;
482  fX = new Double_t[fN];
483  fY = new Double_t[fN];
484  Int_t i;
485  Float_t xold,yold;
486  for (i=0;i<fN;i++) {R__b >> xold; fX[i] = xold;}
487  for (i=0;i<fN;i++) {R__b >> yold; fY[i] = yold;}
488  fOption.Streamer(R__b);
489  R__b.CheckByteCount(R__s, R__c, TPolyMarker::IsA());
490  //====end of old versions
491 
492  } else {
494  }
495 }
void Copy(TAttMarker &attmarker) const
Copy this marker attributes to a new TAttMarker.
Definition: TAttMarker.cxx:191
virtual void Draw(Option_t *option="")
Draw.
virtual void ExecuteEvent(Int_t event, Int_t px, Int_t py)
Execute action corresponding to one event.
virtual Int_t WriteClassBuffer(const TClass *cl, void *pointer)=0
Bool_t IsReading() const
Definition: TBuffer.h:81
virtual Bool_t InheritsFrom(const char *classname) const
Returns kTRUE if object inherits from class "classname".
Definition: TObject.cxx:487
short Version_t
Definition: RtypesCore.h:61
ClassImp(TSeqCollection) Int_t TSeqCollection TIter next(this)
Return index of object in collection.
virtual void DrawPolyMarker(Int_t n, Double_t *x, Double_t *y, Option_t *option="")
Draw polymarker.
float Float_t
Definition: RtypesCore.h:53
const char Option_t
Definition: RtypesCore.h:62
virtual void PaintPolyMarker(Int_t n, Double_t *x, Double_t *y, Option_t *option="")
Paint polymarker.
Double_t distance(const TPoint2 &p1, const TPoint2 &p2)
Definition: CsgOps.cxx:467
TString fOption
Definition: TPolyMarker.h:43
virtual ~TPolyMarker()
Desctructor.
virtual void Copy(TObject &polymarker) const
Copy this to obj.
Buffer base class used for serializing objects.
Definition: TBuffer.h:40
virtual Int_t Merge(TCollection *list)
Merge polymarkers in the collection in this polymarker.
virtual Int_t CheckByteCount(UInt_t startpos, UInt_t bcnt, const TClass *clss)=0
#define gROOT
Definition: TROOT.h:340
int Int_t
Definition: RtypesCore.h:41
virtual void SavePrimitive(std::ostream &out, Option_t *option="")
Save primitive as a C++ statement(s) on output stream out.
virtual Int_t Size() const
Definition: TPolyMarker.h:75
Short_t Abs(Short_t d)
Definition: TMathBase.h:110
TPolyMarker & operator=(const TPolyMarker &)
assignment operator
void Reset()
Definition: TCollection.h:161
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
Definition: TObject.cxx:732
virtual void AppendPad(Option_t *option="")
Append graphics object to current pad.
Definition: TObject.cxx:164
Marker Attributes class.
Definition: TAttMarker.h:32
Double_t x[n]
Definition: legend1.C:17
Double_t * GetY() const
Definition: TPolyMarker.h:63
void Class()
Definition: Class.C:29
TObject & operator=(const TObject &rhs)
TObject assignment operator.
Definition: TObject.cxx:102
virtual void SaveMarkerAttributes(std::ostream &out, const char *name, Int_t coldef=1, Int_t stydef=1, Int_t sizdef=1)
Save line attributes as C++ statement(s) on output stream out.
Definition: TAttMarker.cxx:226
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:918
char * out
Definition: TBase64.cxx:29
virtual Int_t SetNextPoint(Double_t x, Double_t y)
Set point following LastPoint to x, y.
virtual void Copy(TObject &object) const
Copy this to obj.
Definition: TObject.cxx:122
virtual void Paint(Option_t *option="")
Paint.
virtual void SetPolyMarker(Int_t n)
If n <= 0 the current arrays of points are deleted.
virtual void Modify()
Change current marker attributes if necessary.
Definition: TAttMarker.cxx:201
Double_t * fX
Definition: TPolyMarker.h:41
virtual const char * ClassName() const
Returns name of class to which the object belongs.
Definition: TObject.cxx:187
Collection abstract base class.
Definition: TCollection.h:48
TClass * IsA() const
virtual void Print(Option_t *option="") const
Print polymarker.
unsigned int UInt_t
Definition: RtypesCore.h:42
virtual void SetPoint(Int_t point, Double_t x, Double_t y)
Set point number n.
virtual Int_t ReadClassBuffer(const TClass *cl, void *pointer, const TClass *onfile_class=0)=0
A PolyMarker is defined by an array on N points in a 2-D space.
Definition: TPolyMarker.h:37
#define ClassImp(name)
Definition: Rtypes.h:279
double Double_t
Definition: RtypesCore.h:55
virtual void ls(Option_t *option="") const
ls.
ClassImp(TMCParticle) void TMCParticle printf(": p=(%7.3f,%7.3f,%9.3f) ;", fPx, fPy, fPz)
Double_t y[n]
Definition: legend1.C:17
Binding & operator=(OUT(*fun)(void))
Mother of all ROOT objects.
Definition: TObject.h:58
virtual Int_t DistancetoPrimitive(Int_t px, Int_t py)
Compute distance from point px,py to a polymarker.
Double_t * fY
Definition: TPolyMarker.h:42
Short_t Max(Short_t a, Short_t b)
Definition: TMathBase.h:202
#define gPad
Definition: TVirtualPad.h:288
static void IndentLevel()
Functions used by ls() to indent an object hierarchy.
Definition: TROOT.cxx:2461
TObject * obj
Int_t fLastPoint
Definition: TPolyMarker.h:40
const Int_t n
Definition: legend1.C:16
Double_t * GetX() const
Definition: TPolyMarker.h:62
virtual Version_t ReadVersion(UInt_t *start=0, UInt_t *bcnt=0, const TClass *cl=0)=0