Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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 "TROOT.h"
13#include "TBuffer.h"
14#include "TVirtualPad.h"
15#include "TPolyMarker.h"
16#include "TMath.h"
17
18/** \class TPolyMarker
19 \ingroup Graphs
20A PolyMarker is defined by an array on N points in a 2-D space.
21At each point x[i], y[i] a marker is drawn.
22Marker attributes are managed by TAttMarker.
23See TMarker for the list of possible marker types.
24*/
25
26////////////////////////////////////////////////////////////////////////////////
27/// Default constructor.
28
30{
31 fN = 0;
32 fX = fY = nullptr;
33 fLastPoint = -1;
34}
35
36////////////////////////////////////////////////////////////////////////////////
37/// Constructor.
38
40{
43 fLastPoint = -1;
44 if (n <= 0) {
45 fN = 0;
46 fLastPoint = -1;
47 fX = fY = nullptr;
48 return;
49 }
50 fN = n;
51 fX = new Double_t [fN];
52 fY = new Double_t [fN];
53}
54
55////////////////////////////////////////////////////////////////////////////////
56/// Constructor.
57
59{
62 fLastPoint = -1;
63 if (n <= 0) {
64 fN = 0;
65 fLastPoint = -1;
66 fX = fY = nullptr;
67 return;
68 }
69 fN = n;
70 fX = new Double_t [fN];
71 fY = new Double_t [fN];
72 if (!x || !y) return;
73 for (Int_t i=0; i<fN;i++) { fX[i] = x[i]; fY[i] = y[i]; }
74 fLastPoint = fN-1;
75}
76
77////////////////////////////////////////////////////////////////////////////////
78/// Constructor.
79
81{
84 fLastPoint = -1;
85 if (n <= 0) {
86 fN = 0;
87 fLastPoint = -1;
88 fX = fY = nullptr;
89 return;
90 }
91 fN = n;
92 fX = new Double_t [fN];
93 fY = new Double_t [fN];
94 if (!x || !y) return;
95 for (Int_t i=0; i<fN;i++) { fX[i] = x[i]; fY[i] = y[i]; }
96 fLastPoint = fN-1;
97}
98
99////////////////////////////////////////////////////////////////////////////////
100///assignment operator
101
103{
104 if(this != &pm)
105 pm.TPolyMarker::Copy(*this);
106
107 return *this;
108}
109
110////////////////////////////////////////////////////////////////////////////////
111/// Destructor.
112
114{
115 if (fX) delete [] fX;
116 if (fY) delete [] fY;
117 fLastPoint = -1;
118}
119
120////////////////////////////////////////////////////////////////////////////////
121/// Copy constructor.
122
124{
125 fN = 0;
126 fX = fY = nullptr;
127 fLastPoint = -1;
128 polymarker.TPolyMarker::Copy(*this);
129}
130
131////////////////////////////////////////////////////////////////////////////////
132// Copy TPolyMarker into provided object
133
135{
136 TObject::Copy(obj);
138 ((TPolyMarker&)obj).fN = fN;
139 // delete first previous existing fX and fY
140 if (((TPolyMarker&)obj).fX) delete [] (((TPolyMarker&)obj).fX);
141 if (((TPolyMarker&)obj).fY) delete [] (((TPolyMarker&)obj).fY);
142 if (fN > 0) {
143 ((TPolyMarker&)obj).fX = new Double_t [fN];
144 ((TPolyMarker&)obj).fY = new Double_t [fN];
145 for (Int_t i=0; i<fN;i++) {
146 ((TPolyMarker&)obj).fX[i] = fX[i];
147 ((TPolyMarker&)obj).fY[i] = fY[i];
148 }
149 } else {
150 ((TPolyMarker&)obj).fX = nullptr;
151 ((TPolyMarker&)obj).fY = nullptr;
152 }
153 ((TPolyMarker&)obj).fOption = fOption;
154 ((TPolyMarker&)obj).fLastPoint = fLastPoint;
155}
156
157////////////////////////////////////////////////////////////////////////////////
158/// Compute distance from point px,py to a polymarker.
159///
160/// Compute the closest distance of approach from point px,py to each point
161/// of the polymarker.
162/// Returns when the distance found is below DistanceMaximum.
163/// The distance is computed in pixels units.
164
166{
167 const Int_t big = 9999;
168
169 // check if point is near one of the points
170 Int_t i, pxp, pyp, d;
172
173 for (i=0;i<Size();i++) {
174 pxp = gPad->XtoAbsPixel(gPad->XtoPad(fX[i]));
175 pyp = gPad->YtoAbsPixel(gPad->YtoPad(fY[i]));
176 d = TMath::Abs(pxp-px) + TMath::Abs(pyp-py);
177 if (d < distance) distance = d;
178 }
179 return distance;
180}
181
182////////////////////////////////////////////////////////////////////////////////
183/// Draw.
184
189
190////////////////////////////////////////////////////////////////////////////////
191/// Draw polymarker.
192
201
202////////////////////////////////////////////////////////////////////////////////
203/// Execute action corresponding to one event.
204///
205/// This member function must be implemented to realize the action
206/// corresponding to the mouse click on the object in the window
207
211
212////////////////////////////////////////////////////////////////////////////////
213/// ls.
214
216{
218 printf("TPolyMarker N=%d\n",fN);
219}
220
221////////////////////////////////////////////////////////////////////////////////
222/// Merge polymarkers in the collection in this polymarker.
223
225{
226 if (!li) return 0;
227 TIter next(li);
228
229 //first loop to count the number of entries
231 Int_t npoints = 0;
232 while ((pm = (TPolyMarker*)next())) {
233 if (!pm->InheritsFrom(TPolyMarker::Class())) {
234 Error("Add","Attempt to add object of class: %s to a %s",pm->ClassName(),this->ClassName());
235 return -1;
236 }
237 npoints += pm->Size();
238 }
239
240 //extend this polymarker to hold npoints
241 SetPoint(npoints-1,0,0);
242
243 //merge all polymarkers
244 next.Reset();
245 while ((pm = (TPolyMarker*)next())) {
246 Int_t np = pm->Size();
247 Double_t *x = pm->GetX();
248 Double_t *y = pm->GetY();
249 for (Int_t i=0;i<np;i++) {
250 SetPoint(i,x[i],y[i]);
251 }
252 }
253
254 return npoints;
255}
256
257////////////////////////////////////////////////////////////////////////////////
258/// Paint.
259
264
265////////////////////////////////////////////////////////////////////////////////
266/// Paint polymarker.
267
269{
270 if ((n <= 0) || !gPad)
271 return;
272 std::vector <Double_t> xx, yy;
273 if (gPad->GetLogx()) {
274 xx.resize(n);
275 for (Int_t ix = 0; ix < n; ix++)
276 xx[ix] = gPad->XtoPad(x[ix]);
277 x = xx.data();
278 }
279 if (gPad->GetLogy()) {
280 yy.resize(n);
281 for (Int_t iy = 0; iy < n; iy++)
282 yy[iy] = gPad->YtoPad(y[iy]);
283 y = yy.data();
284 }
285 TAttMarker::ModifyOn(*gPad); //Change marker attributes only if necessary
286 gPad->PaintPolyMarker(n, x, y, option);
287}
288
289////////////////////////////////////////////////////////////////////////////////
290/// Print polymarker.
291
293{
294 printf("TPolyMarker N=%d\n",fN);
295}
296
297////////////////////////////////////////////////////////////////////////////////
298/// Save primitive as a C++ statement(s) on output stream out.
299
301{
302 TString args;
303 if (Size() > 0) {
304 TString arrxname = SavePrimitiveVector(out, "pmarker", Size(), fX, kTRUE);
305 TString arryname = SavePrimitiveVector(out, "pmarker", Size(), fY);
306 args.Form("%d, %s.data(), %s.data(), \"", Size(), arrxname.Data(), arryname.Data());
307 } else
308 args = "0, \"";
309 args.Append(TString(fOption).ReplaceSpecialCppChars() + "\"");
310
311 SavePrimitiveConstructor(out, Class(), "pmarker", args, Size() == 0);
312
313 SaveMarkerAttributes(out, "pmarker", 1, 1, 1);
314
315 SavePrimitiveDraw(out, "pmarker", option);
316}
317
318////////////////////////////////////////////////////////////////////////////////
319/// Set point following LastPoint to x, y.
320/// Returns index of the point (new last point).
321
328
329////////////////////////////////////////////////////////////////////////////////
330/// Set point number n.
331/// if n is greater than the current size, the arrays are automatically
332/// extended
333
335{
336 if (n < 0) return;
337 if (!fX || !fY || n >= fN) {
338 // re-allocate the object
339 Int_t newN = TMath::Max(2*fN,n+1);
340 Double_t *savex = new Double_t [newN];
341 Double_t *savey = new Double_t [newN];
342 if (fX && fN){
343 memcpy(savex,fX,fN*sizeof(Double_t));
344 memset(&savex[fN],0,(newN-fN)*sizeof(Double_t));
345 delete [] fX;
346 }
347 if (fY && fN){
348 memcpy(savey,fY,fN*sizeof(Double_t));
349 memset(&savey[fN],0,(newN-fN)*sizeof(Double_t));
350 delete [] fY;
351 }
352 fX = savex;
353 fY = savey;
354 fN = newN;
355 }
356 fX[n] = x;
357 fY[n] = y;
359}
360
361////////////////////////////////////////////////////////////////////////////////
362/// If n <= 0 the current arrays of points are deleted.
363
365{
366 if (n <= 0) {
367 fN = 0;
368 fLastPoint = -1;
369 delete [] fX;
370 delete [] fY;
371 fX = fY = nullptr;
372 return;
373 }
374 SetPoint(n-1,0,0);
375}
376
377////////////////////////////////////////////////////////////////////////////////
378/// If n <= 0 the current arrays of points are deleted.
379
381{
382 if (n <= 0) {
383 fN = 0;
384 fLastPoint = -1;
385 delete [] fX;
386 delete [] fY;
387 fX = fY = nullptr;
388 return;
389 }
390 fN =n;
391 if (fX) delete [] fX;
392 if (fY) delete [] fY;
393 fX = new Double_t[fN];
394 fY = new Double_t[fN];
395 for (Int_t i=0; i<fN;i++) {
396 if (x) fX[i] = (Double_t)x[i];
397 if (y) fY[i] = (Double_t)y[i];
398 }
399 fOption = option;
400 fLastPoint = fN-1;
401}
402
403////////////////////////////////////////////////////////////////////////////////
404/// If n <= 0 the current arrays of points are deleted.
405
407{
408 if (n <= 0) {
409 fN = 0;
410 fLastPoint = -1;
411 delete [] fX;
412 delete [] fY;
413 fX = fY = nullptr;
414 return;
415 }
416 fN =n;
417 if (fX) delete [] fX;
418 if (fY) delete [] fY;
419 fX = new Double_t[fN];
420 fY = new Double_t[fN];
421 for (Int_t i=0; i<fN;i++) {
422 if (x) fX[i] = x[i];
423 if (y) fY[i] = y[i];
424 }
425 fOption = option;
426 fLastPoint = fN-1;
427}
428
429////////////////////////////////////////////////////////////////////////////////
430/// Stream a class object.
431
433{
434 if (R__b.IsReading()) {
436 Version_t R__v = R__b.ReadVersion(&R__s, &R__c);
437 if (R__v > 1) {
438 R__b.ReadClassBuffer(TPolyMarker::Class(), this, R__v, R__s, R__c);
439 return;
440 }
441 //====process old versions before automatic schema evolution
444 R__b >> fN;
445 fX = new Double_t[fN];
446 fY = new Double_t[fN];
447 Int_t i;
449 for (i=0;i<fN;i++) {R__b >> xold; fX[i] = xold;}
450 for (i=0;i<fN;i++) {R__b >> yold; fY[i] = yold;}
452 R__b.CheckByteCount(R__s, R__c, TPolyMarker::IsA());
453 //====end of old versions
454
455 } else {
456 R__b.WriteClassBuffer(TPolyMarker::Class(),this);
457 }
458}
#define d(i)
Definition RSha256.hxx:102
short Version_t
Class version identifier (short)
Definition RtypesCore.h:80
float Float_t
Float 4 bytes (float)
Definition RtypesCore.h:72
double Double_t
Double 8 bytes.
Definition RtypesCore.h:74
constexpr Bool_t kTRUE
Definition RtypesCore.h:108
const char Option_t
Option string (const char)
Definition RtypesCore.h:81
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t option
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t np
#define gPad
Marker Attributes class.
Definition TAttMarker.h:22
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.
virtual void ModifyOn(TVirtualPad &pad)
Change current marker attributes if necessary on specified pad.
void Copy(TAttMarker &attmarker) const
Copy this marker attributes to a new TAttMarker.
virtual void Streamer(TBuffer &)
Buffer base class used for serializing objects.
Definition TBuffer.h:43
Collection abstract base class.
Definition TCollection.h:65
void Reset()
Mother of all ROOT objects.
Definition TObject.h:42
virtual void Streamer(TBuffer &)
Stream an object of class TObject.
Definition TObject.cxx:995
virtual void AppendPad(Option_t *option="")
Append graphics object to current pad.
Definition TObject.cxx:203
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
Definition TObject.cxx:886
virtual void Copy(TObject &object) const
Copy this to obj.
Definition TObject.cxx:158
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition TObject.cxx:1096
static void SavePrimitiveDraw(std::ostream &out, const char *variable_name, Option_t *option=nullptr)
Save invocation of primitive Draw() method Skipped if option contains "nodraw" string.
Definition TObject.cxx:844
static void SavePrimitiveConstructor(std::ostream &out, TClass *cl, const char *variable_name, const char *constructor_agrs="", Bool_t empty_line=kTRUE)
Save object constructor in the output stream "out".
Definition TObject.cxx:776
static TString SavePrimitiveVector(std::ostream &out, const char *prefix, Int_t len, Double_t *arr, Int_t flag=0)
Save array in the output stream "out" as vector.
Definition TObject.cxx:795
@ kCanDelete
if object in a list can be deleted
Definition TObject.h:71
A PolyMarker is defined by an array on N points in a 2-D space.
Definition TPolyMarker.h:31
TPolyMarker & operator=(const TPolyMarker &)
assignment operator
virtual void PaintPolyMarker(Int_t n, Double_t *x, Double_t *y, Option_t *option="")
Paint polymarker.
void Copy(TObject &polymarker) const override
Copy this to obj.
void Print(Option_t *option="") const override
Print polymarker.
Double_t * fY
[fN] Array of Y coordinates
Definition TPolyMarker.h:36
virtual Int_t Size() const
Definition TPolyMarker.h:76
virtual Int_t Merge(TCollection *list)
Merge polymarkers in the collection in this polymarker.
TPolyMarker()
Default constructor.
virtual void DrawPolyMarker(Int_t n, Double_t *x, Double_t *y, Option_t *option="")
Draw polymarker.
virtual void SetPolyMarker(Int_t n)
If n <= 0 the current arrays of points are deleted.
void Streamer(TBuffer &) override
Stream a class object.
void SavePrimitive(std::ostream &out, Option_t *option="") override
Save primitive as a C++ statement(s) on output stream out.
Int_t fN
Number of points internally reserved (not necessarily used)
Definition TPolyMarker.h:33
void ExecuteEvent(Int_t event, Int_t px, Int_t py) override
Execute action corresponding to one event.
~TPolyMarker() override
Destructor.
virtual Int_t SetNextPoint(Double_t x, Double_t y)
Set point following LastPoint to x, y.
static TClass * Class()
Int_t fLastPoint
The index of the last filled point.
Definition TPolyMarker.h:34
TString fOption
Options.
Definition TPolyMarker.h:37
void Paint(Option_t *option="") override
Paint.
Double_t * fX
[fN] Array of X coordinates
Definition TPolyMarker.h:35
Int_t DistancetoPrimitive(Int_t px, Int_t py) override
Compute distance from point px,py to a polymarker.
void Draw(Option_t *option="") override
Draw.
virtual void SetPoint(Int_t point, Double_t x, Double_t y)
Set point number n.
TClass * IsA() const override
Definition TPolyMarker.h:78
void ls(Option_t *option="") const override
ls.
static void IndentLevel()
Functions used by ls() to indent an object hierarchy.
Definition TROOT.cxx:3067
Basic string class.
Definition TString.h:137
virtual void Streamer(TBuffer &)
Stream a string object.
Definition TString.cxx:1493
TString & Append(const char *cs)
Definition TString.h:582
void Form(const char *fmt,...)
Formats a string using a printf style format descriptor.
Definition TString.cxx:2438
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
const Int_t n
Definition legend1.C:16
Short_t Max(Short_t a, Short_t b)
Returns the largest of a and b.
Definition TMathBase.h:249
Short_t Abs(Short_t d)
Returns the absolute value of parameter Short_t d.
Definition TMathBase.h:122