Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TScatter2D.cxx
Go to the documentation of this file.
1// @(#)root/hist:$Id$
2// Author: Olivier Couet 23/09/2025
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
13#include "TROOT.h"
14#include "TScatter2D.h"
15#include "TH1.h"
17
18#include <iostream>
19
20
21////////////////////////////////////////////////////////////////////////////////
22
23/** \class TScatter2D
24 \ingroup Graphs
25A TScatter2D is able to draw five variables scatter plot on a single plot. The three first
26variables are the x, y and z position of the markers (stored in a TGraph2D), the fourth is
27mapped on the current color map and the fifth on the marker size.
28
29The following example demonstrates how it works:
30
31Begin_Macro(source)
32../../../tutorials/visualisation/graphs/gr019_scatter2d.C
33End_Macro
34
35### TScatter2D's plotting options
36TScatter2D can be drawn with the following options:
37
38| Option | Description |
39|----------|-------------------------------------------------------------------|
40| "SAME" | Superimpose on previous picture in the same pad.|
41| "LOGC" | Log scale for colors.|
42| "LOGS" | Log scale for sizes.|
43| "FB" | Suppress the Front-Box.|
44| "BB" | Suppress the Back-Box.|
45| "A" | Suppress the axis.|
46| "P" | Suppress the palette.|
47
48In the case of the SAME option, the log scale for color and size is inherited from the
49previously drawn TScatter2D. For example, if a TScatter2D is drawn on top of another one
50that uses a log scale for color, the second TScatter2D will also use a log scale for its
51colors, even if the log scale for color is not explicitly specified for the second plot.
52
53*/
54
55
56////////////////////////////////////////////////////////////////////////////////
57/// TScatter2D default constructor.
58
62
63////////////////////////////////////////////////////////////////////////////////
64/// TScatter2D normal constructor.
65///
66/// the arrays are preset to zero
67
69{
70 fGraph = new TGraph2D(n);
71 fNpoints = fGraph->GetN();
72
74 fSize = new Double_t[fNpoints];
75
76 memset(fColor, 0, fNpoints * sizeof(Double_t));
77 memset(fSize, 0, fNpoints * sizeof(Double_t));
78 fMaxMarkerSize = 5.;
79 fMinMarkerSize = 1.;
80 fMargin = 0.1;
81}
82
83
84////////////////////////////////////////////////////////////////////////////////
85/// TScatter2D normal constructor.
86
88{
91 fGraph = new TGraph2D(n, x, y, z);
92 fNpoints = fGraph->GetN();
93
94 Int_t bufsize = sizeof(Double_t) * fNpoints;
95 if (col) {
97 memcpy(fColor, col, bufsize);
98 }
99 if (size) {
100 fSize = new Double_t[fNpoints];
102 }
103
104 fMaxMarkerSize = 5.;
105 fMinMarkerSize = 1.;
106 fMargin = 0.1;
107
108 TH1::AddDirectory(status);
109}
110
111
112////////////////////////////////////////////////////////////////////////////////
113/// TScatter2D default destructor.
114
116{
117 delete fGraph;
118 delete [] fColor;
119 delete [] fSize;
120}
121
122
123////////////////////////////////////////////////////////////////////////////////
124/// Compute distance from point px,py,pz to a scatter plot.
125///
126/// Compute the closest distance of approach from point px,py,pz to this scatter plot.
127/// The distance is computed in pixels units.
128
130{
131 Int_t distance = 9999;
133 return distance;
134}
135
136
137////////////////////////////////////////////////////////////////////////////////
138/// Execute action corresponding to one event.
139///
140/// This member function is called when a graph is clicked with the locator
141///
142/// If Left button clicked on one of the line end points, this point
143/// follows the cursor until button is released.
144///
145/// if Middle button clicked, the line is moved parallel to itself
146/// until the button is released.
147
149{
150 if (fGraph) fGraph->ExecuteEvent(event, px, py);
151}
152
153
154////////////////////////////////////////////////////////////////////////////////
155/// Returns a pointer to the histogram used to draw the axis
156
158{
159 if (fGraph) return fGraph->GetHistogram();
160 else return nullptr;
161}
162
163
164////////////////////////////////////////////////////////////////////////////////
165/// Get the scatter's x axis.
166
168{
169 if (fGraph) return fGraph->GetXaxis();
170 else return nullptr;
171}
172
173
174////////////////////////////////////////////////////////////////////////////////
175/// Get the scatter's y axis.
176
178{
179 if (fGraph) return fGraph->GetYaxis();
180 else return nullptr;
181}
182
183
184////////////////////////////////////////////////////////////////////////////////
185/// Get the scatter's z axis.
186
188{
189 if (fGraph) return fGraph->GetZaxis();
190 else return nullptr;
191}
192
193
194////////////////////////////////////////////////////////////////////////////////
195/// Paint this scatter plot with its current attributes.
196
202
203
204////////////////////////////////////////////////////////////////////////////////
205/// Print graph and errors values.
206
208{
209 if (!fGraph) return;
210
211 Double_t *X = fGraph->GetX();
212 Double_t *Y = fGraph->GetY();
213 Double_t *Z = fGraph->GetZ();
214 for (Int_t i = 0; i < fNpoints; i++) {
215 printf("x[%d]=%g, y[%d]=%g, z[%d]=%g", i, X[i], i, Y[i], i, Z[i]);
216 if (fColor) printf(", color[%d]=%g", i, fColor[i]);
217 if (fSize) printf(", size[%d]=%g", i, fSize[i]);
218 printf("\n");
219 }
220}
221
222
223////////////////////////////////////////////////////////////////////////////////
224/// Set the margin around the plot in %
225
227{
228 if (fMargin != margin) {
229 fMargin = margin;
230 }
231}
232
233
234////////////////////////////////////////////////////////////////////////////////
235/// Save primitive as a C++ statement(s) on output stream out
236
237void TScatter2D::SavePrimitive(std::ostream &out, Option_t *option /*= ""*/)
238{
240 TString arr_y = SavePrimitiveVector(out, "scat_y", fNpoints, fGraph->GetY());
241 TString arr_z = SavePrimitiveVector(out, "scat_z", fNpoints, fGraph->GetZ());
243 TString arr_size = SavePrimitiveVector(out, "scat_size", fNpoints, fSize);
244
245 SavePrimitiveConstructor(out, Class(), "scat",
246 TString::Format("%d, %s.data(), %s.data(), %s.data(), %s.data(), %s.data()", fNpoints, arr_x.Data(),
247 arr_y.Data(), arr_z.Data(), arr_col.Data(), arr_size.Data()),
248 kFALSE);
249
250 SavePrimitiveNameTitle(out, "scat");
251 SaveFillAttributes(out, "scat", 0, 1001);
252 SaveLineAttributes(out, "scat", 1, 1, 1);
253 SaveMarkerAttributes(out, "scat", 1, 1, 1);
254
255 out << " scat->SetMargin(" << GetMargin() << ");\n";
256 out << " scat->SetMinMarkerSize(" << GetMinMarkerSize() << ");\n";
257 out << " scat->SetMaxMarkerSize(" << GetMaxMarkerSize() << ");\n";
258
259 SavePrimitiveDraw(out, "scat", option);
260}
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
constexpr Bool_t kFALSE
Definition RtypesCore.h:108
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
#define X(type, name)
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t option
virtual void SaveFillAttributes(std::ostream &out, const char *name, Int_t coldef=1, Int_t stydef=1001)
Save fill attributes as C++ statement(s) on output stream out.
Definition TAttFill.cxx:238
virtual void SaveLineAttributes(std::ostream &out, const char *name, Int_t coldef=1, Int_t stydef=1, Int_t widdef=1)
Save line attributes as C++ statement(s) on output stream out.
Definition TAttLine.cxx:274
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.
Class to manage histogram axis.
Definition TAxis.h:32
Graphics object made of three arrays X, Y and Z with the same number of points each.
Definition TGraph2D.h:41
Double_t * GetY() const
Definition TGraph2D.h:123
Double_t * GetX() const
Definition TGraph2D.h:122
TH2D * GetHistogram(Option_t *option="")
By default returns a pointer to the Delaunay histogram.
TAxis * GetZaxis() const
Get z axis of the graph.
Definition TGraph2D.cxx:902
TAxis * GetYaxis() const
Get y axis of the graph.
Definition TGraph2D.cxx:891
Int_t GetN() const
Definition TGraph2D.h:121
Int_t DistancetoPrimitive(Int_t px, Int_t py) override
Computes distance from point px,py to a graph.
Definition TGraph2D.cxx:727
void ExecuteEvent(Int_t event, Int_t px, Int_t py) override
Executes action corresponding to one event.
Definition TGraph2D.cxx:779
TAxis * GetXaxis() const
Get x axis of the graph.
Definition TGraph2D.cxx:880
Double_t * GetZ() const
Definition TGraph2D.h:124
static void AddDirectory(Bool_t add=kTRUE)
Sets the flag controlling the automatic add of histograms in memory.
Definition TH1.cxx:1263
static Bool_t AddDirectoryStatus()
Static function: cannot be inlined on Windows/NT.
Definition TH1.cxx:741
2-D histogram with a double per channel (see TH1 documentation)
Definition TH2.h:400
void SavePrimitiveNameTitle(std::ostream &out, const char *variable_name)
Save object name and title into the output stream "out".
Definition TNamed.cxx:135
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
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
void Paint(Option_t *chopt="") override
Paint this scatter plot with its current attributes.
TH2D * GetHistogram() const
Get the graph histogram used for drawing axis.
void SetMargin(Double_t)
Set the margin around the plot in %.
Double_t fMargin
Margin around the plot in %.
Definition TScatter2D.h:41
TGraph2D * fGraph
Pointer to graph holding X, Y and Z positions.
Definition TScatter2D.h:36
Double_t fMaxMarkerSize
Largest marker size used to paint the markers.
Definition TScatter2D.h:39
Double_t GetMaxMarkerSize() const
Get the largest marker size used to paint the markers.
Definition TScatter2D.h:54
void Print(Option_t *chopt="") const override
Print graph and errors values.
Double_t GetMinMarkerSize() const
Get the smallest marker size used to paint the markers.
Definition TScatter2D.h:55
Int_t DistancetoPrimitive(Int_t px, Int_t py) override
Compute distance from point px,py,pz to a scatter plot.
TAxis * GetXaxis() const
Get the scatter's x axis.
~TScatter2D() override
TScatter2D default destructor.
Double_t * fSize
[fNpoints] array of marker sizes
Definition TScatter2D.h:38
Int_t fNpoints
Number of points of arrays fX, fY and fZ.
Definition TScatter2D.h:35
TAxis * GetYaxis() const
Get the scatter's y axis.
void SavePrimitive(std::ostream &out, Option_t *option="") override
Save primitive as a C++ statement(s) on output stream out.
Double_t GetMargin() const
Set the margin around the plot in %.
Definition TScatter2D.h:53
TAxis * GetZaxis() const
Get the scatter's z axis.
TScatter2D()
TScatter2D default constructor.
Double_t fMinMarkerSize
Smallest marker size used to paint the markers.
Definition TScatter2D.h:40
Double_t * fColor
[fNpoints] array of colors
Definition TScatter2D.h:37
void ExecuteEvent(Int_t event, Int_t px, Int_t py) override
Execute action corresponding to one event.
static TClass * Class()
Basic string class.
Definition TString.h:138
const char * Data() const
Definition TString.h:384
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:2384
Abstract interface to a histogram painter.
static TVirtualGraphPainter * GetPainter()
Static function returning a pointer to the current graph painter.
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
const Int_t n
Definition legend1.C:16