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{
132 if (painter)
133 return painter->DistancetoPrimitiveHelper(this->GetGraph(), px, py);
134 return 0;
135}
136
137
138////////////////////////////////////////////////////////////////////////////////
139/// Execute action corresponding to one event.
140///
141/// This member function is called when a graph is clicked with the locator
142///
143/// If Left button clicked on one of the line end points, this point
144/// follows the cursor until button is released.
145///
146/// if Middle button clicked, the line is moved parallel to itself
147/// until the button is released.
148
150{
152 if (painter) painter->ExecuteEventHelper(this->GetGraph(), event, px, py);
153}
154
155
156////////////////////////////////////////////////////////////////////////////////
157/// Returns a pointer to the histogram used to draw the axis
158
160{
161 if (!fHistogram) {
162 // do not add the histogram to gDirectory
163 // use local TDirectory::TContect that will set temporarly gDirectory to a nullptr and
164 // will avoid that histogram is added in the global directory
165 TDirectory::TContext ctx(nullptr);
166 double rwxmin, rwymin, rwxmax, rwymax;
167 int npt = 50;
168 fGraph->ComputeRange(rwxmin, rwymin, rwxmax, rwymax);
169 double dx = (rwxmax-rwxmin)*fMargin;
170 double dy = (rwymax-rwymin)*fMargin;
171 auto h = new TH2F(TString::Format("%s_h",GetName()),GetTitle(),npt,rwxmin-dx,rwxmax+dx,npt,rwymin-dy,rwymax+dy);
172// h->SetMinimum(rwymin-dy);
173// h->SetMaximum(rwymax+dy);
174 h->SetBit(TH1::kNoStats);
175 h->SetDirectory(nullptr);
176 h->Sumw2(kFALSE);
177 const_cast<TScatter *>(this)->fHistogram = h;
178 }
179 return fHistogram;
180}
181
182
183////////////////////////////////////////////////////////////////////////////////
184/// Paint this scatter plot with its current attributes.
185
187{
189 if (painter) painter->PaintScatter(this, option);
190}
191
192
193////////////////////////////////////////////////////////////////////////////////
194/// Print graph and errors values.
195
197{
198 Double_t *X = fGraph->GetX();
199 Double_t *Y = fGraph->GetY();
200 for (Int_t i = 0; i < fNpoints; i++) {
201 printf("x[%d]=%g, y[%d]=%g", i, X[i], i, Y[i]);
202 if (fColor) printf(", color[%d]=%g", i, fColor[i]);
203 if (fSize) printf(", size[%d]=%g", i, fSize[i]);
204 printf("\n");
205 }
206}
207
208
209////////////////////////////////////////////////////////////////////////////////
210/// Set the margin around the plot in %
211
213{
214 if (fMargin != margin) {
215 delete fHistogram;
216 fHistogram = nullptr;
217 fMargin = margin;
218 }
219}
220
221
222////////////////////////////////////////////////////////////////////////////////
223/// Save primitive as a C++ statement(s) on output stream out
224
225void TScatter::SavePrimitive(std::ostream &out, Option_t *option /*= ""*/)
226{
227 char quote = '"';
228 out << " " << std::endl;
229 static Int_t frameNumber = 1000;
230 frameNumber++;
231
232 Int_t i;
233 Double_t *X = fGraph->GetX();
234 Double_t *Y = fGraph->GetY();
235 TString fXName = TString::Format("%s_fx%d",GetName(),frameNumber);
236 TString fYName = TString::Format("%s_fy%d", GetName(),frameNumber);
237 TString fColorName = TString::Format("%s_fcolor%d",GetName(),frameNumber);
238 TString fSizeName = TString::Format("%s_fsize%d",GetName(),frameNumber);
239 out << " Double_t " << fXName << "[" << fNpoints << "] = {" << std::endl;
240 for (i = 0; i < fNpoints-1; i++) out << " " << X[i] << "," << std::endl;
241 out << " " << X[fNpoints-1] << "};" << std::endl;
242 out << " Double_t " << fYName << "[" << fNpoints << "] = {" << std::endl;
243 for (i = 0; i < fNpoints-1; i++) out << " " << Y[i] << "," << std::endl;
244 out << " " << Y[fNpoints-1] << "};" << std::endl;
245 out << " Double_t " << fColorName << "[" << fNpoints << "] = {" << std::endl;
246 for (i = 0; i < fNpoints-1; i++) out << " " << fColor[i] << "," << std::endl;
247 out << " " << fColor[fNpoints-1] << "};" << std::endl;
248 out << " Double_t " << fSizeName << "[" << fNpoints << "] = {" << std::endl;
249 for (i = 0; i < fNpoints-1; i++) out << " " << fSize[i] << "," << std::endl;
250 out << " " << fSize[fNpoints-1] << "};" << std::endl;
251
252 if (gROOT->ClassSaved(TScatter::Class()))
253 out << " ";
254 else
255 out << " TScatter *";
256 out << "scat = new TScatter(" << fNpoints << "," << fXName << "," << fYName << ","
257 << fColorName << "," << fSizeName << ");" << std::endl;
258
259 out << " scat->SetName(" << quote << GetName() << quote << ");" << std::endl;
260 out << " scat->SetTitle(" << quote << GetTitle() << quote << ");" << std::endl;
261 out << " scat->SetMargin(" << GetMargin() << ");" << std::endl;
262 out << " scat->SetMinMarkerSize(" << GetMinMarkerSize() << ");" << std::endl;
263 out << " scat->SetMaxMarkerSize(" << GetMaxMarkerSize() << ");" << std::endl;
264
265 SaveFillAttributes(out, "scat", 0, 1001);
266 SaveLineAttributes(out, "scat", 1, 1, 1);
267 SaveMarkerAttributes(out, "scat", 1, 1, 1);
268
269 if (fHistogram) {
270 TString hname = fHistogram->GetName();
271 fHistogram->SetName(TString::Format("Graph_%s%d", hname.Data(), frameNumber));
272 fHistogram->SavePrimitive(out, "nodraw");
273 out << " scat->SetHistogram(" << fHistogram->GetName() << ");" << std::endl;
274 out << " " << std::endl;
275 fHistogram->SetName(hname);
276 }
277
278 out << " scat->Draw(" << quote << option << quote << ");" << std::endl;
279}
#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:101
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.
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:714
Double_t * GetX() const
Definition TGraph.h:138
Int_t GetMaxSize() const
Definition TGraph.h:130
@ 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:7205
void SetName(const char *name) override
Change the name of this histogram.
Definition TH1.cxx:8928
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
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
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:196
void Paint(Option_t *chopt="") override
Paint this scatter plot with its current attributes.
Definition TScatter.cxx:186
void ExecuteEvent(Int_t event, Int_t px, Int_t py) override
Execute action corresponding to one event.
Definition TScatter.cxx:149
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:159
Double_t * fColor
[fNpoints] array of colors
Definition TScatter.h:39
~TScatter() override
TScatter default destructor.
Definition TScatter.cxx:114
void SetMargin(Double_t)
Set the margin around the plot in %.
Definition TScatter.cxx:212
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:225
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