Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TScatter.cxx
Go to the documentation of this file.
1// @(#)root/hist:$Id$
2// Author: Olivier Couet 18/05/2022
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 "TScatter.h"
15#include "TH2.h"
17
18 #include <iostream>
19
20
21////////////////////////////////////////////////////////////////////////////////
22
23/** \class TScatter
24 \ingroup Graphs
25A TScatter is able to draw four variables scatter plot on a single plot. The two first
26variables are the x and y position of the markers, the third is mapped on the current
27color map and the fourth on the marker size.
28
29The following example demonstrates how it works:
30
31Begin_Macro(source)
32../../../tutorials/visualisation/graphs/gr006_scatter.C
33End_Macro
34
35### TScatter's plotting options
36TScatter can be drawn with the following options:
37
38| Option | Description |
39|-----------|-------------------------------------------------------------------|
40| "A" | Produce a new plot with Axis around the graph |
41| "SKIPCOL" | Do not draw the points outside the color range. By default, such points' color is clipped to the minimum or maximum color, depending on whether the color is smaller or bigger than the color range |
42
43*/
44
45////////////////////////////////////////////////////////////////////////////////
46/// TScatter default constructor.
47
51
52////////////////////////////////////////////////////////////////////////////////
53/// TScatter normal constructor.
54///
55/// the arrays are preset to zero
56
58{
59 fGraph = new TGraph(n);
60 fNpoints = fGraph->GetN();
62
64 fSize = new Double_t[fMaxSize];
65
66 memset(fColor, 0, fNpoints * sizeof(Double_t));
67 memset(fSize, 0, fNpoints * sizeof(Double_t));
68 fMaxMarkerSize = 5.;
69 fMinMarkerSize = 1.;
70 fMargin = 0.1;
71}
72
73
74////////////////////////////////////////////////////////////////////////////////
75/// TScatter normal constructor.
76///
77/// if ex or ey are null, the corresponding arrays are preset to zero
78
79TScatter::TScatter(Int_t n, const Double_t *x, const Double_t *y, const Double_t *col, const Double_t *size)
80{
81 fGraph = new TGraph(n, x, y);
82 fNpoints = fGraph->GetN();
84
85 Int_t bufsize = sizeof(Double_t) * fNpoints;
86 if (col) {
88 memcpy(fColor, col, bufsize);
89 }
90 if (size) {
91 fSize = new Double_t[fMaxSize];
93 }
94
95 fMaxMarkerSize = 5.;
96 fMinMarkerSize = 1.;
97 fMargin = 0.1;
98}
99
100
101////////////////////////////////////////////////////////////////////////////////
102/// TScatter default destructor.
103
105{
106 delete fGraph;
107 delete fHistogram;
108 delete [] fColor;
109 delete [] fSize;
110}
111
112
113////////////////////////////////////////////////////////////////////////////////
114/// Compute distance from point px,py to a scatter plot.
115///
116/// Compute the closest distance of approach from point px,py to this scatter plot.
117/// The distance is computed in pixels units.
118
120{
121 // Are we on the axis?
123 if (this->GetHistogram()) {
124 distance = this->GetHistogram()->DistancetoPrimitive(px,py);
125 if (distance <= 5) return distance;
126 }
127
129 if (painter)
130 return painter->DistancetoPrimitiveHelper(this->GetGraph(), px, py);
131 return 0;
132}
133
134
135////////////////////////////////////////////////////////////////////////////////
136/// Execute action corresponding to one event.
137///
138/// This member function is called when a graph is clicked with the locator
139///
140/// If Left button clicked on one of the line end points, this point
141/// follows the cursor until button is released.
142///
143/// if Middle button clicked, the line is moved parallel to itself
144/// until the button is released.
145
147{
149 if (painter) painter->ExecuteEventHelper(this->GetGraph(), event, px, py);
150}
151
152
153////////////////////////////////////////////////////////////////////////////////
154/// Returns a pointer to the histogram used to draw the axis
155
157{
158 if (!fHistogram) {
159 // do not add the histogram to gDirectory
160 // use local TDirectory::TContect that will set temporarly gDirectory to a nullptr and
161 // will avoid that histogram is added in the global directory
162 TDirectory::TContext ctx(nullptr);
163 double rwxmin, rwymin, rwxmax, rwymax;
164 int npt = 50;
166 double dx = (rwxmax-rwxmin)*fMargin;
167 double dy = (rwymax-rwymin)*fMargin;
169 h->SetBit(TH1::kNoStats);
170 h->SetDirectory(nullptr);
171 h->Sumw2(kFALSE);
172 const_cast<TScatter *>(this)->fHistogram = h;
173 }
174 return fHistogram;
175}
176
177
178////////////////////////////////////////////////////////////////////////////////
179/// Get the scatter's x axis.
180
182{
183 auto h = GetHistogram();
184 return h ? h->GetXaxis() : nullptr;
185}
186
187
188////////////////////////////////////////////////////////////////////////////////
189/// Get the scatter's y axis.
190
192{
193 auto h = GetHistogram();
194 return h ? h->GetYaxis() : nullptr;
195}
196
197
198////////////////////////////////////////////////////////////////////////////////
199/// Get the scatter's z axis.
200
202{
203 auto h = GetHistogram();
204 return h ? h->GetZaxis() : nullptr;
205}
206
207
208////////////////////////////////////////////////////////////////////////////////
209/// Paint this scatter plot with its current attributes.
210
216
217
218////////////////////////////////////////////////////////////////////////////////
219/// Print graph and errors values.
220
222{
223 Double_t *X = fGraph->GetX();
224 Double_t *Y = fGraph->GetY();
225 for (Int_t i = 0; i < fNpoints; i++) {
226 printf("x[%d]=%g, y[%d]=%g", i, X[i], i, Y[i]);
227 if (fColor) printf(", color[%d]=%g", i, fColor[i]);
228 if (fSize) printf(", size[%d]=%g", i, fSize[i]);
229 printf("\n");
230 }
231}
232
233
234////////////////////////////////////////////////////////////////////////////////
235/// Set the margin around the plot in %
236
238{
239 if (fMargin != margin) {
240 delete fHistogram;
241 fHistogram = nullptr;
242 fMargin = margin;
243 }
244}
245
246
247////////////////////////////////////////////////////////////////////////////////
248/// Save primitive as a C++ statement(s) on output stream out
249
250void TScatter::SavePrimitive(std::ostream &out, Option_t *option)
251{
253 TString arr_y = SavePrimitiveVector(out, "scat_y", fNpoints, fGraph->GetY());
255 TString arr_size = SavePrimitiveVector(out, "scat_size", fNpoints, fSize);
256
257 SavePrimitiveConstructor(out, Class(), "scat",
258 TString::Format("%d, %s.data(), %s.data(), %s.data(), %s.data()", fNpoints, arr_x.Data(),
259 arr_y.Data(), arr_col.Data(), arr_size.Data()),
260 kFALSE);
261
262 SavePrimitiveNameTitle(out, "scat");
263 SaveFillAttributes(out, "scat", 0, 1001);
264 SaveLineAttributes(out, "scat", 1, 1, 1);
265 SaveMarkerAttributes(out, "scat", 1, 1, 1);
266
267 out << " scat->SetMargin(" << GetMargin() << ");\n";
268 out << " scat->SetMinMarkerSize(" << GetMinMarkerSize() << ");\n";
269 out << " scat->SetMaxMarkerSize(" << GetMaxMarkerSize() << ");\n";
270
271 if (fHistogram) {
272 static int histcnt = 0;
274 fHistogram->SetName(TString::Format("scat_stack_hist%d", histcnt++));
275 fHistogram->SavePrimitive(out, "nodraw");
276 out << " scat->SetHistogram(" << fHistogram->GetName() << ");\n";
277 out << " \n";
279 }
280
281 SavePrimitiveDraw(out, "scat", option);
282}
#define h(i)
Definition RSha256.hxx:106
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
TDirectory::TContext keeps track and restore the current directory.
Definition TDirectory.h:89
A TGraph is an object made of two arrays X and Y with npoints each.
Definition TGraph.h:41
Double_t * GetY() const
Definition TGraph.h:139
Int_t GetN() const
Definition TGraph.h:131
virtual void ComputeRange(Double_t &xmin, Double_t &ymin, Double_t &xmax, Double_t &ymax) const
Compute the x/y range of the points in this graph.
Definition TGraph.cxx:751
Double_t * GetX() const
Definition TGraph.h:138
Int_t GetMaxSize() const
Definition TGraph.h:130
Int_t DistancetoPrimitive(Int_t px, Int_t py) override
Compute distance from point px,py to a line.
Definition TH1.cxx:2805
@ kNoStats
Don't draw stats box.
Definition TH1.h:403
void SavePrimitive(std::ostream &out, Option_t *option="") override
Save primitive as a C++ statement(s) on output stream out.
Definition TH1.cxx:7312
void SetName(const char *name) override
Change the name of this histogram.
Definition TH1.cxx:9001
2-D histogram with a float per channel (see TH1 documentation)
Definition TH2.h:345
void SavePrimitiveNameTitle(std::ostream &out, const char *variable_name)
Save object name and title into the output stream "out".
Definition TNamed.cxx:135
const char * GetName() const override
Returns name of object.
Definition TNamed.h:49
const char * GetTitle() const override
Returns title of object.
Definition TNamed.h:50
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
A TScatter is able to draw four variables scatter plot on a single plot.
Definition TScatter.h:32
TAxis * GetXaxis() const
Get the scatter's x axis.
Definition TScatter.cxx:181
TGraph * GetGraph() const
Get the graph holding X and Y positions.
Definition TScatter.h:58
Int_t fMaxSize
!Current dimension of arrays fX and fY
Definition TScatter.h:35
Double_t GetMaxMarkerSize() const
Get the largest marker size used to paint the markers.
Definition TScatter.h:56
TAxis * GetYaxis() const
Get the scatter's y axis.
Definition TScatter.cxx:191
Double_t fMaxMarkerSize
Largest marker size used to paint the markers.
Definition TScatter.h:41
void Print(Option_t *chopt="") const override
Print graph and errors values.
Definition TScatter.cxx:221
void Paint(Option_t *chopt="") override
Paint this scatter plot with its current attributes.
Definition TScatter.cxx:211
void ExecuteEvent(Int_t event, Int_t px, Int_t py) override
Execute action corresponding to one event.
Definition TScatter.cxx:146
TGraph * fGraph
Pointer to graph holding X and Y positions.
Definition TScatter.h:38
TScatter()
TScatter default constructor.
Definition TScatter.cxx:48
TH2F * GetHistogram() const
Get the graph histogram used for drawing axis.
Definition TScatter.cxx:156
Double_t * fColor
[fNpoints] array of colors
Definition TScatter.h:39
TAxis * GetZaxis() const
Get the scatter's z axis.
Definition TScatter.cxx:201
~TScatter() override
TScatter default destructor.
Definition TScatter.cxx:104
void SetMargin(Double_t)
Set the margin around the plot in %.
Definition TScatter.cxx:237
Double_t GetMinMarkerSize() const
Get the smallest marker size used to paint the markers.
Definition TScatter.h:57
Double_t * fSize
[fNpoints] array of marker sizes
Definition TScatter.h:40
Double_t fMargin
Margin around the plot in %.
Definition TScatter.h:43
Double_t GetMargin() const
Set the margin around the plot in %.
Definition TScatter.h:55
Int_t DistancetoPrimitive(Int_t px, Int_t py) override
Compute distance from point px,py to a scatter plot.
Definition TScatter.cxx:119
static TClass * Class()
Double_t fMinMarkerSize
Smallest marker size used to paint the markers.
Definition TScatter.h:42
Int_t fNpoints
Number of points <= fMaxSize.
Definition TScatter.h:36
TH2F * fHistogram
Pointer to histogram used for drawing axis.
Definition TScatter.h:37
void SavePrimitive(std::ostream &out, Option_t *option="") override
Save primitive as a C++ statement(s) on output stream out.
Definition TScatter.cxx:250
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