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/graphs/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
52*/
53
54
55////////////////////////////////////////////////////////////////////////////////
56/// TScatter default constructor.
57
59{
60}
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];
102 memcpy(fSize, size, bufsize);
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?
132 Int_t distance;
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;
175 fGraph->ComputeRange(rwxmin, rwymin, rwxmax, rwymax);
176 double dx = (rwxmax-rwxmin)*fMargin;
177 double dy = (rwymax-rwymin)*fMargin;
178 auto h = new TH2F(TString::Format("%s_h",GetName()),GetTitle(),npt,rwxmin-dx,rwxmax+dx,npt,rwymin-dy,rwymax+dy);
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
222{
224 if (painter) painter->PaintScatter(this, option);
225}
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{
262 char quote = '"';
263 out << " " << std::endl;
264 static Int_t frameNumber = 1000;
265 frameNumber++;
266
267 Int_t i;
268 Double_t *X = fGraph->GetX();
269 Double_t *Y = fGraph->GetY();
270 TString fXName = TString::Format("%s_fx%d",GetName(),frameNumber);
271 TString fYName = TString::Format("%s_fy%d", GetName(),frameNumber);
272 TString fColorName = TString::Format("%s_fcolor%d",GetName(),frameNumber);
273 TString fSizeName = TString::Format("%s_fsize%d",GetName(),frameNumber);
274 out << " Double_t " << fXName << "[" << fNpoints << "] = {" << std::endl;
275 for (i = 0; i < fNpoints-1; i++) out << " " << X[i] << "," << std::endl;
276 out << " " << X[fNpoints-1] << "};" << std::endl;
277 out << " Double_t " << fYName << "[" << fNpoints << "] = {" << std::endl;
278 for (i = 0; i < fNpoints-1; i++) out << " " << Y[i] << "," << std::endl;
279 out << " " << Y[fNpoints-1] << "};" << std::endl;
280 out << " Double_t " << fColorName << "[" << fNpoints << "] = {" << std::endl;
281 for (i = 0; i < fNpoints-1; i++) out << " " << fColor[i] << "," << std::endl;
282 out << " " << fColor[fNpoints-1] << "};" << std::endl;
283 out << " Double_t " << fSizeName << "[" << fNpoints << "] = {" << std::endl;
284 for (i = 0; i < fNpoints-1; i++) out << " " << fSize[i] << "," << std::endl;
285 out << " " << fSize[fNpoints-1] << "};" << std::endl;
286
287 if (gROOT->ClassSaved(TScatter::Class()))
288 out << " ";
289 else
290 out << " TScatter *";
291 out << "scat = new TScatter(" << fNpoints << "," << fXName << "," << fYName << ","
292 << fColorName << "," << fSizeName << ");" << std::endl;
293
294 out << " scat->SetName(" << quote << GetName() << quote << ");" << std::endl;
295 out << " scat->SetTitle(" << quote << GetTitle() << quote << ");" << std::endl;
296 out << " scat->SetMargin(" << GetMargin() << ");" << std::endl;
297 out << " scat->SetMinMarkerSize(" << GetMinMarkerSize() << ");" << std::endl;
298 out << " scat->SetMaxMarkerSize(" << GetMaxMarkerSize() << ");" << std::endl;
299
300 SaveFillAttributes(out, "scat", 0, 1001);
301 SaveLineAttributes(out, "scat", 1, 1, 1);
302 SaveMarkerAttributes(out, "scat", 1, 1, 1);
303
304 if (fHistogram) {
305 TString hname = fHistogram->GetName();
306 fHistogram->SetName(TString::Format("Graph_%s%d", hname.Data(), frameNumber));
307 fHistogram->SavePrimitive(out, "nodraw");
308 out << " scat->SetHistogram(" << fHistogram->GetName() << ");" << std::endl;
309 out << " " << std::endl;
310 fHistogram->SetName(hname);
311 }
312
313 out << " scat->Draw(" << quote << option << quote << ");" << std::endl;
314}
#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:94
double Double_t
Definition RtypesCore.h:59
const char Option_t
Definition RtypesCore.h:66
#define ClassImp(name)
Definition Rtypes.h:377
#define X(type, name)
Option_t Option_t option
#define gROOT
Definition TROOT.h:406
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:31
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:140
Int_t GetN() const
Definition TGraph.h:132
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:731
Double_t * GetX() const
Definition TGraph.h:139
Int_t GetMaxSize() const
Definition TGraph.h:131
Int_t DistancetoPrimitive(Int_t px, Int_t py) override
Compute distance from point px,py to a line.
Definition TH1.cxx:2823
@ kNoStats
Don't draw stats box.
Definition TH1.h:165
void SavePrimitive(std::ostream &out, Option_t *option="") override
Save primitive as a C++ statement(s) on output stream out.
Definition TH1.cxx:7228
void SetName(const char *name) override
Change the name of this histogram.
Definition TH1.cxx:8951
2-D histogram with a float per channel (see TH1 documentation)
Definition TH2.h:307
const char * GetName() const override
Returns name of object.
Definition TNamed.h:47
const char * GetTitle() const override
Returns title of object.
Definition TNamed.h:48
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:139
const char * Data() const
Definition TString.h:376
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:2378
Abstract interface to a histogram painter.
virtual void ExecuteEventHelper(TGraph *theGraph, Int_t event, Int_t px, Int_t py)=0
virtual Int_t DistancetoPrimitiveHelper(TGraph *theGraph, Int_t px, Int_t py)=0
virtual void PaintScatter(TScatter *theScatter, Option_t *option)=0
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