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