Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TMarker.cxx
Go to the documentation of this file.
1// @(#)root/graf:$Id$
2// Author: Rene Brun 12/05/95
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 "TMarker.h"
16#include "TMath.h"
17#include "TPoint.h"
18#include "TText.h"
19
20#include <cstdlib>
21#include <iostream>
22#include <cstdio>
23
24
25/** \class TMarker
26\ingroup BasicGraphics
27
28Manages Markers.
29
30Use the TMarker constructor to create a marker.
31
32~~~ {.cpp}
33 TMarker(Double_t x,Double_t y,Int_t marker)
34~~~
35
36The parameters `x` and `y` are the marker coordinates and `marker` is the marker type.
37
38Use the TPolyMarker to create an array on N points in a 2D space.
39At each point `x[i]`, `y[i]` a marker is drawn.
40
41Use the TAttMarker class to change the attributes color, style and size of a marker.
42
43_**Example**_
44
45- Use the `TAttMarker::SetMarkerSize(size)` method to set the `size` of a marker.
46
47*/
48
49////////////////////////////////////////////////////////////////////////////////
50/// Marker default constructor.
51
53{
54 fX = 0;
55 fY = 0;
56}
57
58////////////////////////////////////////////////////////////////////////////////
59/// Marker normal constructor.
60
63{
64 fX = x;
65 fY = y;
66 fMarkerStyle = marker;
67}
68
69////////////////////////////////////////////////////////////////////////////////
70/// Marker default destructor.
71
75
76////////////////////////////////////////////////////////////////////////////////
77/// Marker copy constructor.
78
79TMarker::TMarker(const TMarker &marker) : TObject(marker), TAttMarker(marker), TAttBBox2D(marker)
80{
81 fX = 0;
82 fY = 0;
83 marker.TMarker::Copy(*this);
84}
85
86////////////////////////////////////////////////////////////////////////////////
87/// Copy this marker to marker.
88
89void TMarker::Copy(TObject &obj) const
90{
91 TObject::Copy(obj);
92 TAttMarker::Copy(((TMarker &)obj));
93 ((TMarker &)obj).fX = fX;
94 ((TMarker &)obj).fY = fY;
95}
96
97////////////////////////////////////////////////////////////////////////////////
98/// Display the table of markers with their numbers.
99
101{
102 TMarker marker;
103 TText text;
104 marker.SetMarkerSize(3);
105 text.SetTextFont(62);
106 text.SetTextAlign(22);
107 text.SetTextSize(0.1);
109 Double_t x = 0;
110 Double_t dx = 1/16.0;
111 for (Int_t i=1;i<16;i++) {
112 x += dx;
113 atext.Form("%d",i);
114 marker.SetMarkerStyle(i);
115 marker.DrawMarker(x,.25);
116 text.DrawText(x,.12,atext.Data());
117 atext.Form("%d",i+19);
118 marker.SetMarkerStyle(i+19);
119 marker.DrawMarker(x,.55);
120 text.DrawText(x,.42,atext.Data());
121 atext.Form("%d",i+34);
122 marker.SetMarkerStyle(i+34);
123 marker.DrawMarker(x,.85);
124 text.DrawText(x,.72,atext.Data());
125 }
126}
127
128////////////////////////////////////////////////////////////////////////////////
129/// Display the table of markers with different line widths and their numbers.
130
132{
133 TMarker marker;
134 TText text;
135 marker.SetMarkerSize(3);
136 text.SetTextFont(62);
137 text.SetTextAlign(22);
138 text.SetTextSize(0.075);
140 Double_t x = 0;
141 Double_t dx = 1/19.0;
142 for (Int_t i=1;i<19;i++) {
143 x += dx;
144 atext.Form("%d",i+49);
145 marker.SetMarkerStyle(i+49);
146 marker.DrawMarker(x,0.19);
147 text.DrawText(x,0.08,atext.Data());
148 atext.Form("%d",i+67);
149 marker.SetMarkerStyle(i+67);
150 marker.DrawMarker(x,0.42);
151 text.DrawText(x,0.31,atext.Data());
152 atext.Form("%d",i+85);
153 marker.SetMarkerStyle(i+85);
154 marker.DrawMarker(x,0.65);
155 text.DrawText(x,0.54,atext.Data());
156 atext.Form("%d",i+103);
157 marker.SetMarkerStyle(i+103);
158 marker.DrawMarker(x,0.88);
159 text.DrawText(x,0.77,atext.Data());
160 }
161}
162
163////////////////////////////////////////////////////////////////////////////////
164/// Compute distance from point px,py to a marker.
165///
166/// Compute the closest distance of approach from point px,py to this marker.
167/// The distance is computed in pixels units.
168/// \return 0 if the distance <= markerRadius
169/// exact computed distance if dist in [markerRadius+1, markerRadius+2]
170/// 999 if larger than markerRadius + 3
171/// 9999 if no pad
172
174{
175 if (!gPad) return 9999;
176 Int_t pxm, pym;
177 if (TestBit(kMarkerNDC)) {
178 pxm = gPad->UtoAbsPixel(fX);
179 pym = gPad->VtoAbsPixel(fY);
180 } else {
181 pxm = gPad->XtoAbsPixel(gPad->XtoPad(fX));
182 pym = gPad->YtoAbsPixel(gPad->YtoPad(fY));
183 }
184 Int_t dist = static_cast<Int_t>(std::min<Long64_t>(INT_MAX, TMath::Sqrt(Long64_t(px-pxm)*(px-pxm) + Long64_t(py-pym)*(py-pym))));
185
186 //marker size = 1 is about 8 pixels
188 if (dist <= markerRadius) return 0;
189 if (dist > markerRadius+3) return 999;
190 return dist;
191}
192
193////////////////////////////////////////////////////////////////////////////////
194/// Draw this marker with its current attributes.
195
200
201////////////////////////////////////////////////////////////////////////////////
202/// Draw this marker with new coordinates.
203
205{
206 TMarker *newmarker = new TMarker(x, y, 1);
208 newmarker->SetBit(kCanDelete);
209 newmarker->AppendPad();
210 return newmarker;
211}
212
213////////////////////////////////////////////////////////////////////////////////
214/// Execute action corresponding to one event.
215///
216/// This member function is called when a marker is clicked with the locator
217///
218/// If Left button is clicked on a marker, the marker is moved to
219/// a new position when the mouse button is released.
220
222{
223 if (!gPad || !gPad->IsEditable()) return;
224
225 auto &parent = *gPad;
226
227 Bool_t opaque = parent.OpaqueMoving();
228
229 auto action = [this, &parent](Bool_t paint, Int_t posx, Int_t posy) {
230 Double_t x = parent.AbsPixeltoX(posx);
231 Double_t y = parent.AbsPixeltoY(posy);
232 if (paint) {
234 Double_t dx = (parent.GetX2() - parent.GetX1()) / parent.GetPadWidth() * scale;
235 Double_t dy = (parent.GetY2() - parent.GetY1()) / parent.GetPadHeight() * scale;
236
237 Double_t xc[5] = { x, x + dx, x, x - dx, x };
238 Double_t yc[5] = { y - dy, y, y + dy, y, y - dy };
239 TAttLine(1,1,1).ModifyOn(parent);
240 parent.PaintPolyLine(5, xc, yc, "imarker");
241 } else if (TestBit(kMarkerNDC)) {
242 SetX((x - parent.GetX1()) / (parent.GetX2() - parent.GetX1()));
243 SetY((y - parent.GetY1()) / (parent.GetY2() - parent.GetY1()));
244 } else {
245 SetX(parent.PadtoX(x));
246 SetY(parent.PadtoY(y));
247 }
248 };
249
250 switch (event) {
251
252 case kButton1Down:
253 case kMouseMotion:
254 parent.SetCursor(kMove);
255 break;
256
257 case kButton1Motion:
258 if (opaque) {
259 action(false, px, py);
260 parent.ShowGuidelines(this, event, 'i', true);
261 parent.Modified();
262 } else {
263 action(true, px, py);
264 }
265 parent.UpdateAsync();
266 break;
267
268 case kButton1Up:
269 if (opaque) {
270 parent.ShowGuidelines(this, event);
271 } else {
272 action(false, px, py);
273 parent.Modified();
274 }
275 parent.UpdateAsync();
276 break;
277 }
278}
279
280////////////////////////////////////////////////////////////////////////////////
281/// List this marker with its attributes.
282
284{
286 printf("Marker X=%f Y=%f marker type=%d\n",fX,fY,fMarkerStyle);
287}
288
289////////////////////////////////////////////////////////////////////////////////
290/// Paint this marker with its current attributes.
291
293{
294 if (!gPad)
295 return;
296 if (TestBit(kMarkerNDC))
298 else
299 PaintMarker(gPad->XtoPad(GetX()), gPad->YtoPad(GetY()));
300}
301
302////////////////////////////////////////////////////////////////////////////////
303/// Draw this marker with new coordinates.
304
306{
307 TAttMarker::Modify(); //Change line attributes only if necessary
308 if (gPad) gPad->PaintPolyMarker(-1, &x, &y, "");
309}
310
311////////////////////////////////////////////////////////////////////////////////
312/// Draw this marker with new coordinates in NDC.
313
315{
316 Double_t x = gPad->GetX1() + u * (gPad->GetX2() - gPad->GetX1());
317 Double_t y = gPad->GetY1() + v * (gPad->GetY2() - gPad->GetY1());
318 PaintMarker(x, y);
319}
320
321////////////////////////////////////////////////////////////////////////////////
322/// Dump this marker with its attributes.
323
325{
326 printf("Marker X=%f Y=%f",fX,fY);
327 if (GetMarkerColor() != 1) printf(" Color=%d",GetMarkerColor());
328 if (GetMarkerStyle() != 1) printf(" MarkerStyle=%d",GetMarkerStyle());
329 if (GetMarkerSize() != 1) printf(" MarkerSize=%f",GetMarkerSize());
330 printf("\n");
331}
332
333////////////////////////////////////////////////////////////////////////////////
334/// Save primitive as a C++ statement(s) on output stream out
335
336void TMarker::SavePrimitive(std::ostream &out, Option_t *option)
337{
338 SavePrimitiveConstructor(out, Class(), "marker", TString::Format("%g, %g, %d", fX, fY, fMarkerStyle), kFALSE);
339
340 SaveMarkerAttributes(out, "marker", 1, 1, 1);
341
342 SavePrimitiveDraw(out, "marker", option);
343}
344
345////////////////////////////////////////////////////////////////////////////////
346/// Set NDC mode on if isNDC = kTRUE, off otherwise
347
349{
351 if (isNDC) SetBit(kMarkerNDC);
352}
353
354////////////////////////////////////////////////////////////////////////////////
355/// Stream an object of class TMarker.
356
358{
359 if (R__b.IsReading()) {
361 Version_t R__v = R__b.ReadVersion(&R__s, &R__c);
362 if (R__v > 1) {
363 R__b.ReadClassBuffer(TMarker::Class(), this, R__v, R__s, R__c);
364 return;
365 }
366 //====process old versions before automatic schema evolution
369 Float_t x,y;
370 R__b >> x; fX = x;
371 R__b >> y; fY = y;
372 //====end of old versions
373
374 } else {
375 R__b.WriteClassBuffer(TMarker::Class(),this);
376 }
377}
378
379////////////////////////////////////////////////////////////////////////////////
380/// Return the bounding Box of the marker
381
383{
384 auto size = GetMarkerSize();
385 Rectangle_t bbox{0, 0, (UShort_t) (2*size), (UShort_t) (2*size) };
386 if (gPad) {
387 if (TestBit(kMarkerNDC)) {
388 bbox.fX = gPad->UtoPixel(GetX()) - size;
389 bbox.fY = gPad->VtoPixel(GetY()) - size;
390 } else {
391 bbox.fX = gPad->XtoPixel(gPad->XtoPad(GetX())) - size;
392 bbox.fY = gPad->YtoPixel(gPad->YtoPad(GetY())) - size;
393 }
394 }
395 return bbox;
396}
397
398////////////////////////////////////////////////////////////////////////////////
399/// Set X coordinate of the center of the BoundingBox
400
405
406////////////////////////////////////////////////////////////////////////////////
407/// Set Y coordinate of the center of the BoundingBox
408
413
414////////////////////////////////////////////////////////////////////////////////
415/// Set left hand side of BoundingBox to a value
416/// (resize in x direction on left)
417
419{
421}
422
423////////////////////////////////////////////////////////////////////////////////
424/// Set right hand side of BoundingBox to a value
425/// (resize in x direction on right)
426
428{
430}
431
432////////////////////////////////////////////////////////////////////////////////
433/// Set top of BoundingBox to a value (resize in y direction on top)
434
436{
438}
439
440////////////////////////////////////////////////////////////////////////////////
441/// Set bottom of BoundingBox to a value
442/// (resize in y direction on bottom)
443
445{
447}
@ kMouseMotion
Definition Buttons.h:23
@ kButton1Motion
Definition Buttons.h:20
@ kButton1Up
Definition Buttons.h:19
@ kButton1Down
Definition Buttons.h:17
@ kMove
Definition GuiTypes.h:375
cudaEvent_t event
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
unsigned short UShort_t
Unsigned Short integer 2 bytes (unsigned short)
Definition RtypesCore.h:55
int Int_t
Signed integer 4 bytes (int)
Definition RtypesCore.h:60
short Version_t
Class version identifier (short)
Definition RtypesCore.h:80
float Float_t
Float 4 bytes (float)
Definition RtypesCore.h:72
constexpr Bool_t kFALSE
Definition RtypesCore.h:109
long long Long64_t
Portable signed long integer 8 bytes.
Definition RtypesCore.h:84
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 pxm
Option_t Option_t TPoint TPoint const char text
#define gPad
Abstract base class for elements drawn in the editor.
Definition TAttBBox2D.h:19
Double_t GetYCoord(const Int_t y, Bool_t is_ndc=kFALSE, Bool_t is_absolute=kFALSE)
Can return ndc or normal values Also one can specify to use absolute coordinates for input parameter ...
Double_t GetXCoord(const Int_t x, Bool_t is_ndc=kFALSE, Bool_t is_absolute=kFALSE)
Return user X coordinate for pixel X value Can return ndc or normal values Also one can specify to us...
Line Attributes class.
Definition TAttLine.h:21
virtual void ModifyOn(TVirtualPad &pad)
Change current line attributes on specified pad.
Definition TAttLine.cxx:255
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 Modify()
Change current marker attributes if necessary.
virtual Style_t GetMarkerStyle() const
Return the marker style.
Definition TAttMarker.h:35
virtual Color_t GetMarkerColor() const
Return the marker color.
Definition TAttMarker.h:34
virtual Size_t GetMarkerSize() const
Return the marker size.
Definition TAttMarker.h:36
void Copy(TAttMarker &attmarker) const
Copy this marker attributes to a new TAttMarker.
virtual void SetMarkerStyle(Style_t mstyle=1)
Set the marker style.
virtual void SetMarkerSize(Size_t msize=1)
Set the marker size.
virtual void Streamer(TBuffer &)
Size_t fMarkerSize
Marker size.
Definition TAttMarker.h:27
Style_t fMarkerStyle
Marker style.
Definition TAttMarker.h:26
Buffer base class used for serializing objects.
Definition TBuffer.h:43
Manages Markers.
Definition TMarker.h:22
void Streamer(TBuffer &) override
Stream an object of class TMarker.
Definition TMarker.cxx:357
Double_t fX
X position of marker (left,center,etc..)
Definition TMarker.h:25
void ExecuteEvent(Int_t event, Int_t px, Int_t py) override
Execute action corresponding to one event.
Definition TMarker.cxx:221
void ls(Option_t *option="") const override
List this marker with its attributes.
Definition TMarker.cxx:283
virtual void SetX(Double_t x)
Definition TMarker.h:53
void SetBBoxCenterY(const Int_t y) override
Set Y coordinate of the center of the BoundingBox.
Definition TMarker.cxx:409
virtual void SetNDC(Bool_t isNDC=kTRUE)
Set NDC mode on if isNDC = kTRUE, off otherwise.
Definition TMarker.cxx:348
void SavePrimitive(std::ostream &out, Option_t *option="") override
Save primitive as a C++ statement(s) on output stream out.
Definition TMarker.cxx:336
void Draw(Option_t *option="") override
Draw this marker with its current attributes.
Definition TMarker.cxx:196
~TMarker() override
Marker default destructor.
Definition TMarker.cxx:72
TMarker()
Marker default constructor.
Definition TMarker.cxx:52
Double_t fY
Y position of marker (left,center,etc..)
Definition TMarker.h:26
virtual void SetY(Double_t y)
Definition TMarker.h:54
void SetBBoxCenterX(const Int_t x) override
Set X coordinate of the center of the BoundingBox.
Definition TMarker.cxx:401
void SetBBoxY1(const Int_t y) override
Set top of BoundingBox to a value (resize in y direction on top)
Definition TMarker.cxx:435
void SetBBoxY2(const Int_t y) override
Set bottom of BoundingBox to a value (resize in y direction on bottom)
Definition TMarker.cxx:444
void Copy(TObject &marker) const override
Copy this marker to marker.
Definition TMarker.cxx:89
void Paint(Option_t *option="") override
Paint this marker with its current attributes.
Definition TMarker.cxx:292
virtual TMarker * DrawMarker(Double_t x, Double_t y)
Draw this marker with new coordinates.
Definition TMarker.cxx:204
virtual void PaintMarker(Double_t x, Double_t y)
Draw this marker with new coordinates.
Definition TMarker.cxx:305
Rectangle_t GetBBox() override
Return the bounding Box of the marker.
Definition TMarker.cxx:382
void SetBBoxX1(const Int_t x) override
Set left hand side of BoundingBox to a value (resize in x direction on left)
Definition TMarker.cxx:418
static TClass * Class()
static void DisplayMarkerLineWidths()
Display the table of markers with different line widths and their numbers.
Definition TMarker.cxx:131
void Print(Option_t *option="") const override
Dump this marker with its attributes.
Definition TMarker.cxx:324
Int_t DistancetoPrimitive(Int_t px, Int_t py) override
Compute distance from point px,py to a marker.
Definition TMarker.cxx:173
Double_t GetX() const
Definition TMarker.h:44
void SetBBoxX2(const Int_t x) override
Set right hand side of BoundingBox to a value (resize in x direction on right)
Definition TMarker.cxx:427
@ kMarkerNDC
Marker position is in NDC.
Definition TMarker.h:31
static void DisplayMarkerTypes()
Display the table of markers with their numbers.
Definition TMarker.cxx:100
virtual void PaintMarkerNDC(Double_t u, Double_t v)
Draw this marker with new coordinates in NDC.
Definition TMarker.cxx:314
Double_t GetY() const
Definition TMarker.h:45
Mother of all ROOT objects.
Definition TObject.h:42
R__ALWAYS_INLINE Bool_t TestBit(UInt_t f) const
Definition TObject.h:204
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
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
void ResetBit(UInt_t f)
Definition TObject.h:203
@ kCanDelete
if object in a list can be deleted
Definition TObject.h:71
static void IndentLevel()
Functions used by ls() to indent an object hierarchy.
Definition TROOT.cxx:3067
Basic string class.
Definition TString.h:137
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString.
Definition TString.cxx:2460
Base class for several text objects.
Definition TText.h:22
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
Short_t Max(Short_t a, Short_t b)
Returns the largest of a and b.
Definition TMathBase.h:249
Double_t Sqrt(Double_t x)
Returns the square root of x.
Definition TMath.h:675
Rectangle structure (maps to the X11 XRectangle structure)
Definition GuiTypes.h:362
Short_t fX
Definition GuiTypes.h:363