Logo ROOT  
Reference Guide
 
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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
20
21
22/** \class TPolyMarker
23 \ingroup Graphs
24A PolyMarker is defined by an array on N points in a 2-D space.
25At each point x[i], y[i] a marker is drawn.
26Marker attributes are managed by TAttMarker.
27See TMarker for the list of possible marker types.
28*/
29
30////////////////////////////////////////////////////////////////////////////////
31/// Default constructor.
32
34{
35 fN = 0;
36 fX = fY = nullptr;
37 fLastPoint = -1;
38}
39
40////////////////////////////////////////////////////////////////////////////////
41/// Constructor.
42
44{
47 fLastPoint = -1;
48 if (n <= 0) {
49 fN = 0;
50 fLastPoint = -1;
51 fX = fY = nullptr;
52 return;
53 }
54 fN = n;
55 fX = new Double_t [fN];
56 fY = new Double_t [fN];
57}
58
59////////////////////////////////////////////////////////////////////////////////
60/// Constructor.
61
63{
66 fLastPoint = -1;
67 if (n <= 0) {
68 fN = 0;
69 fLastPoint = -1;
70 fX = fY = nullptr;
71 return;
72 }
73 fN = n;
74 fX = new Double_t [fN];
75 fY = new Double_t [fN];
76 if (!x || !y) return;
77 for (Int_t i=0; i<fN;i++) { fX[i] = x[i]; fY[i] = y[i]; }
78 fLastPoint = fN-1;
79}
80
81////////////////////////////////////////////////////////////////////////////////
82/// Constructor.
83
85{
88 fLastPoint = -1;
89 if (n <= 0) {
90 fN = 0;
91 fLastPoint = -1;
92 fX = fY = nullptr;
93 return;
94 }
95 fN = n;
96 fX = new Double_t [fN];
97 fY = new Double_t [fN];
98 if (!x || !y) return;
99 for (Int_t i=0; i<fN;i++) { fX[i] = x[i]; fY[i] = y[i]; }
100 fLastPoint = fN-1;
101}
102
103////////////////////////////////////////////////////////////////////////////////
104///assignment operator
105
107{
108 if(this != &pm)
109 pm.TPolyMarker::Copy(*this);
110
111 return *this;
112}
113
114////////////////////////////////////////////////////////////////////////////////
115/// Destructor.
116
118{
119 if (fX) delete [] fX;
120 if (fY) delete [] fY;
121 fLastPoint = -1;
122}
123
124////////////////////////////////////////////////////////////////////////////////
125/// Copy constructor.
126
128{
129 fN = 0;
130 fX = fY = nullptr;
131 fLastPoint = -1;
132 polymarker.TPolyMarker::Copy(*this);
133}
134
135////////////////////////////////////////////////////////////////////////////////
136// Copy TPolyMarker into provided object
137
139{
140 TObject::Copy(obj);
142 ((TPolyMarker&)obj).fN = fN;
143 // delete first previous existing fX and fY
144 if (((TPolyMarker&)obj).fX) delete [] (((TPolyMarker&)obj).fX);
145 if (((TPolyMarker&)obj).fY) delete [] (((TPolyMarker&)obj).fY);
146 if (fN > 0) {
147 ((TPolyMarker&)obj).fX = new Double_t [fN];
148 ((TPolyMarker&)obj).fY = new Double_t [fN];
149 for (Int_t i=0; i<fN;i++) {
150 ((TPolyMarker&)obj).fX[i] = fX[i];
151 ((TPolyMarker&)obj).fY[i] = fY[i];
152 }
153 } else {
154 ((TPolyMarker&)obj).fX = nullptr;
155 ((TPolyMarker&)obj).fY = nullptr;
156 }
157 ((TPolyMarker&)obj).fOption = fOption;
158 ((TPolyMarker&)obj).fLastPoint = fLastPoint;
159}
160
161////////////////////////////////////////////////////////////////////////////////
162/// Compute distance from point px,py to a polymarker.
163///
164/// Compute the closest distance of approach from point px,py to each point
165/// of the polymarker.
166/// Returns when the distance found is below DistanceMaximum.
167/// The distance is computed in pixels units.
168
170{
171 const Int_t big = 9999;
172
173 // check if point is near one of the points
174 Int_t i, pxp, pyp, d;
176
177 for (i=0;i<Size();i++) {
178 pxp = gPad->XtoAbsPixel(gPad->XtoPad(fX[i]));
179 pyp = gPad->YtoAbsPixel(gPad->YtoPad(fY[i]));
180 d = TMath::Abs(pxp-px) + TMath::Abs(pyp-py);
181 if (d < distance) distance = d;
182 }
183 return distance;
184}
185
186////////////////////////////////////////////////////////////////////////////////
187/// Draw.
188
193
194////////////////////////////////////////////////////////////////////////////////
195/// Draw polymarker.
196
205
206////////////////////////////////////////////////////////////////////////////////
207/// Execute action corresponding to one event.
208///
209/// This member function must be implemented to realize the action
210/// corresponding to the mouse click on the object in the window
211
215
216////////////////////////////////////////////////////////////////////////////////
217/// ls.
218
220{
222 printf("TPolyMarker N=%d\n",fN);
223}
224
225////////////////////////////////////////////////////////////////////////////////
226/// Merge polymarkers in the collection in this polymarker.
227
229{
230 if (!li) return 0;
231 TIter next(li);
232
233 //first loop to count the number of entries
235 Int_t npoints = 0;
236 while ((pm = (TPolyMarker*)next())) {
237 if (!pm->InheritsFrom(TPolyMarker::Class())) {
238 Error("Add","Attempt to add object of class: %s to a %s",pm->ClassName(),this->ClassName());
239 return -1;
240 }
241 npoints += pm->Size();
242 }
243
244 //extend this polymarker to hold npoints
245 SetPoint(npoints-1,0,0);
246
247 //merge all polymarkers
248 next.Reset();
249 while ((pm = (TPolyMarker*)next())) {
250 Int_t np = pm->Size();
251 Double_t *x = pm->GetX();
252 Double_t *y = pm->GetY();
253 for (Int_t i=0;i<np;i++) {
254 SetPoint(i,x[i],y[i]);
255 }
256 }
257
258 return npoints;
259}
260
261////////////////////////////////////////////////////////////////////////////////
262/// Paint.
263
268
269////////////////////////////////////////////////////////////////////////////////
270/// Paint polymarker.
271
273{
274 if (n <= 0) return;
275 TAttMarker::Modify(); //Change marker attributes only if necessary
276 Double_t *xx = x;
277 Double_t *yy = y;
278 if (gPad->GetLogx()) {
279 xx = new Double_t[n];
280 for (Int_t ix=0;ix<n;ix++) xx[ix] = gPad->XtoPad(x[ix]);
281 }
282 if (gPad->GetLogy()) {
283 yy = new Double_t[n];
284 for (Int_t iy=0;iy<n;iy++) yy[iy] = gPad->YtoPad(y[iy]);
285 }
286 gPad->PaintPolyMarker(n,xx,yy,option);
287 if (x != xx) delete [] xx;
288 if (y != yy) delete [] yy;
289}
290
291////////////////////////////////////////////////////////////////////////////////
292/// Print polymarker.
293
295{
296 printf("TPolyMarker N=%d\n",fN);
297}
298
299////////////////////////////////////////////////////////////////////////////////
300/// Save primitive as a C++ statement(s) on output stream out.
301
303{
304 TString args;
305 if (Size() > 0) {
306 TString arrxname = SavePrimitiveVector(out, "pmarker", Size(), fX, kTRUE);
307 TString arryname = SavePrimitiveVector(out, "pmarker", Size(), fY);
308 args.Form("%d, %s.data(), %s.data(), \"", Size(), arrxname.Data(), arryname.Data());
309 } else
310 args = "0, \"";
311 args.Append(TString(fOption).ReplaceSpecialCppChars() + "\"");
312
313 SavePrimitiveConstructor(out, Class(), "pmarker", args, Size() == 0);
314
315 SaveMarkerAttributes(out, "pmarker", 1, 1, 1);
316
317 SavePrimitiveDraw(out, "pmarker", option);
318}
319
320////////////////////////////////////////////////////////////////////////////////
321/// Set point following LastPoint to x, y.
322/// Returns index of the point (new last point).
323
330
331////////////////////////////////////////////////////////////////////////////////
332/// Set point number n.
333/// if n is greater than the current size, the arrays are automatically
334/// extended
335
337{
338 if (n < 0) return;
339 if (!fX || !fY || n >= fN) {
340 // re-allocate the object
341 Int_t newN = TMath::Max(2*fN,n+1);
342 Double_t *savex = new Double_t [newN];
343 Double_t *savey = new Double_t [newN];
344 if (fX && fN){
345 memcpy(savex,fX,fN*sizeof(Double_t));
346 memset(&savex[fN],0,(newN-fN)*sizeof(Double_t));
347 delete [] fX;
348 }
349 if (fY && fN){
350 memcpy(savey,fY,fN*sizeof(Double_t));
351 memset(&savey[fN],0,(newN-fN)*sizeof(Double_t));
352 delete [] fY;
353 }
354 fX = savex;
355 fY = savey;
356 fN = newN;
357 }
358 fX[n] = x;
359 fY[n] = y;
361}
362
363////////////////////////////////////////////////////////////////////////////////
364/// If n <= 0 the current arrays of points are deleted.
365
367{
368 if (n <= 0) {
369 fN = 0;
370 fLastPoint = -1;
371 delete [] fX;
372 delete [] fY;
373 fX = fY = nullptr;
374 return;
375 }
376 SetPoint(n-1,0,0);
377}
378
379////////////////////////////////////////////////////////////////////////////////
380/// If n <= 0 the current arrays of points are deleted.
381
383{
384 if (n <= 0) {
385 fN = 0;
386 fLastPoint = -1;
387 delete [] fX;
388 delete [] fY;
389 fX = fY = nullptr;
390 return;
391 }
392 fN =n;
393 if (fX) delete [] fX;
394 if (fY) delete [] fY;
395 fX = new Double_t[fN];
396 fY = new Double_t[fN];
397 for (Int_t i=0; i<fN;i++) {
398 if (x) fX[i] = (Double_t)x[i];
399 if (y) fY[i] = (Double_t)y[i];
400 }
401 fOption = option;
402 fLastPoint = fN-1;
403}
404
405////////////////////////////////////////////////////////////////////////////////
406/// If n <= 0 the current arrays of points are deleted.
407
409{
410 if (n <= 0) {
411 fN = 0;
412 fLastPoint = -1;
413 delete [] fX;
414 delete [] fY;
415 fX = fY = nullptr;
416 return;
417 }
418 fN =n;
419 if (fX) delete [] fX;
420 if (fY) delete [] fY;
421 fX = new Double_t[fN];
422 fY = new Double_t[fN];
423 for (Int_t i=0; i<fN;i++) {
424 if (x) fX[i] = x[i];
425 if (y) fY[i] = y[i];
426 }
427 fOption = option;
428 fLastPoint = fN-1;
429}
430
431////////////////////////////////////////////////////////////////////////////////
432/// Stream a class object.
433
435{
436 if (R__b.IsReading()) {
438 Version_t R__v = R__b.ReadVersion(&R__s, &R__c);
439 if (R__v > 1) {
440 R__b.ReadClassBuffer(TPolyMarker::Class(), this, R__v, R__s, R__c);
441 return;
442 }
443 //====process old versions before automatic schema evolution
446 R__b >> fN;
447 fX = new Double_t[fN];
448 fY = new Double_t[fN];
449 Int_t i;
451 for (i=0;i<fN;i++) {R__b >> xold; fX[i] = xold;}
452 for (i=0;i<fN;i++) {R__b >> yold; fY[i] = yold;}
454 R__b.CheckByteCount(R__s, R__c, TPolyMarker::IsA());
455 //====end of old versions
456
457 } else {
458 R__b.WriteClassBuffer(TPolyMarker::Class(),this);
459 }
460}
#define d(i)
Definition RSha256.hxx:102
short Version_t
Definition RtypesCore.h:65
float Float_t
Definition RtypesCore.h:57
double Double_t
Definition RtypesCore.h:59
constexpr Bool_t kTRUE
Definition RtypesCore.h:93
const char Option_t
Definition RtypesCore.h:66
#define ClassImp(name)
Definition Rtypes.h:374
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:20
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 Modify()
Change current marker attributes if necessary.
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:41
static TString SavePrimitiveVector(std::ostream &out, const char *prefix, Int_t len, Double_t *arr, Bool_t empty_line=kFALSE)
Save array in the output stream "out" as vector.
Definition TObject.cxx:788
virtual void Streamer(TBuffer &)
Stream an object of class TObject.
Definition TObject.cxx:972
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:864
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:1071
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:822
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:771
@ kCanDelete
if object in a list can be deleted
Definition TObject.h:68
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:2894
Basic string class.
Definition TString.h:139
virtual void Streamer(TBuffer &)
Stream a string object.
Definition TString.cxx:1412
TString & Append(const char *cs)
Definition TString.h:572
void Form(const char *fmt,...)
Formats a string using a printf style format descriptor.
Definition TString.cxx:2356
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:250
Short_t Abs(Short_t d)
Returns the absolute value of parameter Short_t d.
Definition TMathBase.h:123