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 "TBuffer.h"
15#include "TScatter.h"
16#include "TStyle.h"
17#include "TMath.h"
18#include "TVirtualPad.h"
19#include "TH2.h"
21#include "strtok.h"
22
23#include <iostream>
24#include <fstream>
25#include <cstring>
26#include <string>
27
29
30
31////////////////////////////////////////////////////////////////////////////////
32
33/** \class TScatter
34 \ingroup Graphs
35A TScatter is able to draw four variables scatter plot on a single plot. The two first
36variables are the x and y position of the markers, the third is mapped on the current
37color map and the fourth on the marker size.
38
39The following example demonstrates how it works:
40
41Begin_Macro(source)
42../../../tutorials/visualisation/graphs/gr006_scatter.C
43End_Macro
44
45### TScatter's plotting options
46TScatter can be drawn with the following options:
47
48| Option | Description |
49|-----------|-------------------------------------------------------------------|
50| "A" | Produce a new plot with Axis around the graph |
51| "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 |
52
53*/
54
55////////////////////////////////////////////////////////////////////////////////
56/// TScatter default constructor.
57
61
62////////////////////////////////////////////////////////////////////////////////
63/// TScatter normal constructor.
64///
65/// the arrays are preset to zero
66
68{
69 fGraph = new TGraph(n);
70 fNpoints = fGraph->GetN();
72
74 fSize = new Double_t[fMaxSize];
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/// TScatter normal constructor.
86///
87/// if ex or ey are null, the corresponding arrays are preset to zero
88
89TScatter::TScatter(Int_t n, const Double_t *x, const Double_t *y, const Double_t *col, const Double_t *size)
90{
91 fGraph = new TGraph(n, x, y);
92 fNpoints = fGraph->GetN();
94
95 Int_t bufsize = sizeof(Double_t) * fNpoints;
96 if (col) {
98 memcpy(fColor, col, bufsize);
99 }
100 if (size) {
101 fSize = new Double_t[fMaxSize];
103 }
104
105 fMaxMarkerSize = 5.;
106 fMinMarkerSize = 1.;
107 fMargin = 0.1;
108}
109
110
111////////////////////////////////////////////////////////////////////////////////
112/// TScatter default destructor.
113
115{
116 delete fGraph;
117 delete fHistogram;
118 delete [] fColor;
119 delete [] fSize;
120}
121
122
123////////////////////////////////////////////////////////////////////////////////
124/// Compute distance from point px,py to a scatter plot.
125///
126/// Compute the closest distance of approach from point px,py to this scatter plot.
127/// The distance is computed in pixels units.
128
130{
131 // Are we on the axis?
133 if (this->GetHistogram()) {
134 distance = this->GetHistogram()->DistancetoPrimitive(px,py);
135 if (distance <= 5) return distance;
136 }
137
139 if (painter)
140 return painter->DistancetoPrimitiveHelper(this->GetGraph(), px, py);
141 return 0;
142}
143
144
145////////////////////////////////////////////////////////////////////////////////
146/// Execute action corresponding to one event.
147///
148/// This member function is called when a graph is clicked with the locator
149///
150/// If Left button clicked on one of the line end points, this point
151/// follows the cursor until button is released.
152///
153/// if Middle button clicked, the line is moved parallel to itself
154/// until the button is released.
155
157{
159 if (painter) painter->ExecuteEventHelper(this->GetGraph(), event, px, py);
160}
161
162
163////////////////////////////////////////////////////////////////////////////////
164/// Returns a pointer to the histogram used to draw the axis
165
167{
168 if (!fHistogram) {
169 // do not add the histogram to gDirectory
170 // use local TDirectory::TContect that will set temporarly gDirectory to a nullptr and
171 // will avoid that histogram is added in the global directory
172 TDirectory::TContext ctx(nullptr);
173 double rwxmin, rwymin, rwxmax, rwymax;
174 int npt = 50;
176 double dx = (rwxmax-rwxmin)*fMargin;
177 double dy = (rwymax-rwymin)*fMargin;
179 h->SetBit(TH1::kNoStats);
180 h->SetDirectory(nullptr);
181 h->Sumw2(kFALSE);
182 const_cast<TScatter *>(this)->fHistogram = h;
183 }
184 return fHistogram;
185}
186
187
188////////////////////////////////////////////////////////////////////////////////
189/// Get the scatter's x axis.
190
192{
193 auto h = GetHistogram();
194 return h ? h->GetXaxis() : nullptr;
195}
196
197
198////////////////////////////////////////////////////////////////////////////////
199/// Get the scatter's y axis.
200
202{
203 auto h = GetHistogram();
204 return h ? h->GetYaxis() : nullptr;
205}
206
207
208////////////////////////////////////////////////////////////////////////////////
209/// Get the scatter's z axis.
210
212{
213 auto h = GetHistogram();
214 return h ? h->GetZaxis() : nullptr;
215}
216
217
218////////////////////////////////////////////////////////////////////////////////
219/// Paint this scatter plot with its current attributes.
220
226
227
228////////////////////////////////////////////////////////////////////////////////
229/// Print graph and errors values.
230
232{
233 Double_t *X = fGraph->GetX();
234 Double_t *Y = fGraph->GetY();
235 for (Int_t i = 0; i < fNpoints; i++) {
236 printf("x[%d]=%g, y[%d]=%g", i, X[i], i, Y[i]);
237 if (fColor) printf(", color[%d]=%g", i, fColor[i]);
238 if (fSize) printf(", size[%d]=%g", i, fSize[i]);
239 printf("\n");
240 }
241}
242
243
244////////////////////////////////////////////////////////////////////////////////
245/// Set the margin around the plot in %
246
248{
249 if (fMargin != margin) {
250 delete fHistogram;
251 fHistogram = nullptr;
252 fMargin = margin;
253 }
254}
255
256
257////////////////////////////////////////////////////////////////////////////////
258/// Save primitive as a C++ statement(s) on output stream out
259
260void TScatter::SavePrimitive(std::ostream &out, Option_t *option)
261{
263 TString arr_y = SavePrimitiveVector(out, "scat_y", fNpoints, fGraph->GetY());
265 TString arr_size = SavePrimitiveVector(out, "scat_size", fNpoints, fSize);
266
267 SavePrimitiveConstructor(out, Class(), "scat",
268 TString::Format("%d, %s.data(), %s.data(), %s.data(), %s.data()", fNpoints, arr_x.Data(),
269 arr_y.Data(), arr_col.Data(), arr_size.Data()),
270 kFALSE);
271
272 SavePrimitiveNameTitle(out, "scat");
273 SaveFillAttributes(out, "scat", 0, 1001);
274 SaveLineAttributes(out, "scat", 1, 1, 1);
275 SaveMarkerAttributes(out, "scat", 1, 1, 1);
276
277 out << " scat->SetMargin(" << GetMargin() << ");\n";
278 out << " scat->SetMinMarkerSize(" << GetMinMarkerSize() << ");\n";
279 out << " scat->SetMaxMarkerSize(" << GetMaxMarkerSize() << ");\n";
280
281 if (fHistogram) {
282 static int histcnt = 0;
284 fHistogram->SetName(TString::Format("scat_stack_hist%d", histcnt++));
285 fHistogram->SavePrimitive(out, "nodraw");
286 out << " scat->SetHistogram(" << fHistogram->GetName() << ");\n";
287 out << " \n";
289 }
290
291 SavePrimitiveDraw(out, "scat", option);
292}
#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 ClassImp(name)
Definition Rtypes.h:376
#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:239
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:275
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:733
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:2795
@ kNoStats
Don't draw stats box.
Definition TH1.h:404
void SavePrimitive(std::ostream &out, Option_t *option="") override
Save primitive as a C++ statement(s) on output stream out.
Definition TH1.cxx:7300
void SetName(const char *name) override
Change the name of this histogram.
Definition TH1.cxx:8989
2-D histogram with a float per channel (see TH1 documentation)
Definition TH2.h:307
void SavePrimitiveNameTitle(std::ostream &out, const char *variable_name)
Save object name and title into the output stream "out".
Definition TNamed.cxx:136
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:789
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:823
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:772
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:191
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:201
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:231
void Paint(Option_t *chopt="") override
Paint this scatter plot with its current attributes.
Definition TScatter.cxx:221
void ExecuteEvent(Int_t event, Int_t px, Int_t py) override
Execute action corresponding to one event.
Definition TScatter.cxx:156
TGraph * fGraph
Pointer to graph holding X and Y positions.
Definition TScatter.h:38
TScatter()
TScatter default constructor.
Definition TScatter.cxx:58
TH2F * GetHistogram() const
Get the graph histogram used for drawing axis.
Definition TScatter.cxx:166
Double_t * fColor
[fNpoints] array of colors
Definition TScatter.h:39
TAxis * GetZaxis() const
Get the scatter's z axis.
Definition TScatter.cxx:211
~TScatter() override
TScatter default destructor.
Definition TScatter.cxx:114
void SetMargin(Double_t)
Set the margin around the plot in %.
Definition TScatter.cxx:247
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:129
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:260
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:2385
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