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 "Riostream.h"
13#include "TROOT.h"
14#include "TBuffer.h"
15#include "TVirtualPad.h"
16#include "TPolyMarker.h"
17#include "TMath.h"
18
19
20
21/** \class TPolyMarker
22 \ingroup Graphs
23A PolyMarker is defined by an array on N points in a 2-D space.
24At each point x[i], y[i] a marker is drawn.
25Marker attributes are managed by TAttMarker.
26See TMarker for the list of possible marker types.
27*/
28
29////////////////////////////////////////////////////////////////////////////////
30/// Default constructor.
31
33{
34 fN = 0;
35 fX = fY = nullptr;
36 fLastPoint = -1;
37}
38
39////////////////////////////////////////////////////////////////////////////////
40/// Constructor.
41
43{
46 fLastPoint = -1;
47 if (n <= 0) {
48 fN = 0;
49 fLastPoint = -1;
50 fX = fY = nullptr;
51 return;
52 }
53 fN = n;
54 fX = new Double_t [fN];
55 fY = new Double_t [fN];
56}
57
58////////////////////////////////////////////////////////////////////////////////
59/// Constructor.
60
62{
65 fLastPoint = -1;
66 if (n <= 0) {
67 fN = 0;
68 fLastPoint = -1;
69 fX = fY = nullptr;
70 return;
71 }
72 fN = n;
73 fX = new Double_t [fN];
74 fY = new Double_t [fN];
75 if (!x || !y) return;
76 for (Int_t i=0; i<fN;i++) { fX[i] = x[i]; fY[i] = y[i]; }
77 fLastPoint = fN-1;
78}
79
80////////////////////////////////////////////////////////////////////////////////
81/// Constructor.
82
84{
87 fLastPoint = -1;
88 if (n <= 0) {
89 fN = 0;
90 fLastPoint = -1;
91 fX = fY = nullptr;
92 return;
93 }
94 fN = n;
95 fX = new Double_t [fN];
96 fY = new Double_t [fN];
97 if (!x || !y) return;
98 for (Int_t i=0; i<fN;i++) { fX[i] = x[i]; fY[i] = y[i]; }
99 fLastPoint = fN-1;
100}
101
102////////////////////////////////////////////////////////////////////////////////
103///assignment operator
104
106{
107 if(this != &pm)
108 pm.TPolyMarker::Copy(*this);
109
110 return *this;
111}
112
113////////////////////////////////////////////////////////////////////////////////
114/// Destructor.
115
117{
118 if (fX) delete [] fX;
119 if (fY) delete [] fY;
120 fLastPoint = -1;
121}
122
123////////////////////////////////////////////////////////////////////////////////
124/// Copy constructor.
125
127{
128 fN = 0;
129 fX = fY = nullptr;
130 fLastPoint = -1;
131 polymarker.TPolyMarker::Copy(*this);
132}
133
134////////////////////////////////////////////////////////////////////////////////
135// Copy TPolyMarker into provided object
136
138{
139 TObject::Copy(obj);
141 ((TPolyMarker&)obj).fN = fN;
142 // delete first previous existing fX and fY
143 if (((TPolyMarker&)obj).fX) delete [] (((TPolyMarker&)obj).fX);
144 if (((TPolyMarker&)obj).fY) delete [] (((TPolyMarker&)obj).fY);
145 if (fN > 0) {
146 ((TPolyMarker&)obj).fX = new Double_t [fN];
147 ((TPolyMarker&)obj).fY = new Double_t [fN];
148 for (Int_t i=0; i<fN;i++) {
149 ((TPolyMarker&)obj).fX[i] = fX[i];
150 ((TPolyMarker&)obj).fY[i] = fY[i];
151 }
152 } else {
153 ((TPolyMarker&)obj).fX = nullptr;
154 ((TPolyMarker&)obj).fY = nullptr;
155 }
156 ((TPolyMarker&)obj).fOption = fOption;
157 ((TPolyMarker&)obj).fLastPoint = fLastPoint;
158}
159
160////////////////////////////////////////////////////////////////////////////////
161/// Compute distance from point px,py to a polymarker.
162///
163/// Compute the closest distance of approach from point px,py to each point
164/// of the polymarker.
165/// Returns when the distance found is below DistanceMaximum.
166/// The distance is computed in pixels units.
167
169{
170 const Int_t big = 9999;
171
172 // check if point is near one of the points
173 Int_t i, pxp, pyp, d;
175
176 for (i=0;i<Size();i++) {
177 pxp = gPad->XtoAbsPixel(gPad->XtoPad(fX[i]));
178 pyp = gPad->YtoAbsPixel(gPad->YtoPad(fY[i]));
179 d = TMath::Abs(pxp-px) + TMath::Abs(pyp-py);
180 if (d < distance) distance = d;
181 }
182 return distance;
183}
184
185////////////////////////////////////////////////////////////////////////////////
186/// Draw.
187
192
193////////////////////////////////////////////////////////////////////////////////
194/// Draw polymarker.
195
204
205////////////////////////////////////////////////////////////////////////////////
206/// Execute action corresponding to one event.
207///
208/// This member function must be implemented to realize the action
209/// corresponding to the mouse click on the object in the window
210
214
215////////////////////////////////////////////////////////////////////////////////
216/// ls.
217
219{
221 printf("TPolyMarker N=%d\n",fN);
222}
223
224////////////////////////////////////////////////////////////////////////////////
225/// Merge polymarkers in the collection in this polymarker.
226
228{
229 if (!li) return 0;
230 TIter next(li);
231
232 //first loop to count the number of entries
234 Int_t npoints = 0;
235 while ((pm = (TPolyMarker*)next())) {
236 if (!pm->InheritsFrom(TPolyMarker::Class())) {
237 Error("Add","Attempt to add object of class: %s to a %s",pm->ClassName(),this->ClassName());
238 return -1;
239 }
240 npoints += pm->Size();
241 }
242
243 //extend this polymarker to hold npoints
244 SetPoint(npoints-1,0,0);
245
246 //merge all polymarkers
247 next.Reset();
248 while ((pm = (TPolyMarker*)next())) {
249 Int_t np = pm->Size();
250 Double_t *x = pm->GetX();
251 Double_t *y = pm->GetY();
252 for (Int_t i=0;i<np;i++) {
253 SetPoint(i,x[i],y[i]);
254 }
255 }
256
257 return npoints;
258}
259
260////////////////////////////////////////////////////////////////////////////////
261/// Paint.
262
267
268////////////////////////////////////////////////////////////////////////////////
269/// Paint polymarker.
270
272{
273 if ((n <= 0) || !gPad)
274 return;
275 std::vector <Double_t> xx, yy;
276 if (gPad->GetLogx()) {
277 xx.resize(n);
278 for (Int_t ix = 0; ix < n; ix++)
279 xx[ix] = gPad->XtoPad(x[ix]);
280 x = xx.data();
281 }
282 if (gPad->GetLogy()) {
283 yy.resize(n);
284 for (Int_t iy = 0; iy < n; iy++)
285 yy[iy] = gPad->YtoPad(y[iy]);
286 y = yy.data();
287 }
288 TAttMarker::ModifyOn(*gPad); //Change marker attributes only if necessary
289 gPad->PaintPolyMarker(n, x, y, option);
290}
291
292////////////////////////////////////////////////////////////////////////////////
293/// Print polymarker.
294
296{
297 printf("TPolyMarker N=%d\n",fN);
298}
299
300////////////////////////////////////////////////////////////////////////////////
301/// Save primitive as a C++ statement(s) on output stream out.
302
304{
305 TString args;
306 if (Size() > 0) {
307 TString arrxname = SavePrimitiveVector(out, "pmarker", Size(), fX, kTRUE);
308 TString arryname = SavePrimitiveVector(out, "pmarker", Size(), fY);
309 args.Form("%d, %s.data(), %s.data(), \"", Size(), arrxname.Data(), arryname.Data());
310 } else
311 args = "0, \"";
312 args.Append(TString(fOption).ReplaceSpecialCppChars() + "\"");
313
314 SavePrimitiveConstructor(out, Class(), "pmarker", args, Size() == 0);
315
316 SaveMarkerAttributes(out, "pmarker", 1, 1, 1);
317
318 SavePrimitiveDraw(out, "pmarker", option);
319}
320
321////////////////////////////////////////////////////////////////////////////////
322/// Set point following LastPoint to x, y.
323/// Returns index of the point (new last point).
324
331
332////////////////////////////////////////////////////////////////////////////////
333/// Set point number n.
334/// if n is greater than the current size, the arrays are automatically
335/// extended
336
338{
339 if (n < 0) return;
340 if (!fX || !fY || n >= fN) {
341 // re-allocate the object
342 Int_t newN = TMath::Max(2*fN,n+1);
343 Double_t *savex = new Double_t [newN];
344 Double_t *savey = new Double_t [newN];
345 if (fX && fN){
346 memcpy(savex,fX,fN*sizeof(Double_t));
347 memset(&savex[fN],0,(newN-fN)*sizeof(Double_t));
348 delete [] fX;
349 }
350 if (fY && fN){
351 memcpy(savey,fY,fN*sizeof(Double_t));
352 memset(&savey[fN],0,(newN-fN)*sizeof(Double_t));
353 delete [] fY;
354 }
355 fX = savex;
356 fY = savey;
357 fN = newN;
358 }
359 fX[n] = x;
360 fY[n] = y;
362}
363
364////////////////////////////////////////////////////////////////////////////////
365/// If n <= 0 the current arrays of points are deleted.
366
368{
369 if (n <= 0) {
370 fN = 0;
371 fLastPoint = -1;
372 delete [] fX;
373 delete [] fY;
374 fX = fY = nullptr;
375 return;
376 }
377 SetPoint(n-1,0,0);
378}
379
380////////////////////////////////////////////////////////////////////////////////
381/// If n <= 0 the current arrays of points are deleted.
382
384{
385 if (n <= 0) {
386 fN = 0;
387 fLastPoint = -1;
388 delete [] fX;
389 delete [] fY;
390 fX = fY = nullptr;
391 return;
392 }
393 fN =n;
394 if (fX) delete [] fX;
395 if (fY) delete [] fY;
396 fX = new Double_t[fN];
397 fY = new Double_t[fN];
398 for (Int_t i=0; i<fN;i++) {
399 if (x) fX[i] = (Double_t)x[i];
400 if (y) fY[i] = (Double_t)y[i];
401 }
402 fOption = option;
403 fLastPoint = fN-1;
404}
405
406////////////////////////////////////////////////////////////////////////////////
407/// If n <= 0 the current arrays of points are deleted.
408
410{
411 if (n <= 0) {
412 fN = 0;
413 fLastPoint = -1;
414 delete [] fX;
415 delete [] fY;
416 fX = fY = nullptr;
417 return;
418 }
419 fN =n;
420 if (fX) delete [] fX;
421 if (fY) delete [] fY;
422 fX = new Double_t[fN];
423 fY = new Double_t[fN];
424 for (Int_t i=0; i<fN;i++) {
425 if (x) fX[i] = x[i];
426 if (y) fY[i] = y[i];
427 }
428 fOption = option;
429 fLastPoint = fN-1;
430}
431
432////////////////////////////////////////////////////////////////////////////////
433/// Stream a class object.
434
436{
437 if (R__b.IsReading()) {
439 Version_t R__v = R__b.ReadVersion(&R__s, &R__c);
440 if (R__v > 1) {
441 R__b.ReadClassBuffer(TPolyMarker::Class(), this, R__v, R__s, R__c);
442 return;
443 }
444 //====process old versions before automatic schema evolution
447 R__b >> fN;
448 fX = new Double_t[fN];
449 fY = new Double_t[fN];
450 Int_t i;
452 for (i=0;i<fN;i++) {R__b >> xold; fX[i] = xold;}
453 for (i=0;i<fN;i++) {R__b >> yold; fY[i] = yold;}
455 R__b.CheckByteCount(R__s, R__c, TPolyMarker::IsA());
456 //====end of old versions
457
458 } else {
459 R__b.WriteClassBuffer(TPolyMarker::Class(),this);
460 }
461}
#define d(i)
Definition RSha256.hxx:102
short Version_t
Class version identifier (short)
Definition RtypesCore.h:79
float Float_t
Float 4 bytes (float)
Definition RtypesCore.h:71
double Double_t
Double 8 bytes.
Definition RtypesCore.h:73
constexpr Bool_t kTRUE
Definition RtypesCore.h:107
const char Option_t
Option string (const char)
Definition RtypesCore.h:80
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:21
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:997
virtual void AppendPad(Option_t *option="")
Append graphics object to current pad.
Definition TObject.cxx:204
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
Definition TObject.cxx:888
virtual void Copy(TObject &object) const
Copy this to obj.
Definition TObject.cxx:159
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition TObject.cxx:1098
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:845
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:777
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:796
@ 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:3052
Basic string class.
Definition TString.h:138
virtual void Streamer(TBuffer &)
Stream a string object.
Definition TString.cxx:1418
TString & Append(const char *cs)
Definition TString.h:581
void Form(const char *fmt,...)
Formats a string using a printf style format descriptor.
Definition TString.cxx:2363
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