ATLAS Offline Software
Loading...
Searching...
No Matches
RootUtils::ScatterH2 Class Reference

A 2-D histogram that can draw a proper scatter plot. More...

#include <ScatterH2.h>

Inheritance diagram for RootUtils::ScatterH2:
Collaboration diagram for RootUtils::ScatterH2:

Classes

struct  Pair
 One point. Avoid std::pair so that we don't have duplicate dicts. More...

Public Member Functions

 ScatterH2 ()
 Default constructor.
 ScatterH2 (const char *name, const char *title, Int_t nbinsx, Axis_t xlow, Axis_t xup, Int_t nbinsy, Axis_t ylow, Axis_t yup)
 Constructor.
 ScatterH2 (const char *name, const char *title, Int_t nbinsx, const Double_t *xbins, Int_t nbinsy, Axis_t ylow, Axis_t yup)
 Constructor.
 ScatterH2 (const char *name, const char *title, const TArrayD &xbins, Int_t nbinsy, Axis_t ylow, Axis_t yup)
 Constructor.
virtual void Paint (Option_t *option="")
 Standard ROOT paint method.
virtual void Reset (Option_t *option="")
 Standard ROOT reset method.
virtual Int_t Fill (Axis_t x, Axis_t y, Stat_t w)
 Standard ROOT fill method.
virtual Int_t Fill (Axis_t x, Axis_t y)
 Standard ROOT fill method.
bool scatter () const
 Get the current value of the scatter flag.
bool scatter (bool flag)
 Set the scatter flag.
int shadestep () const
 Get the current value of shadestep.
int shadestep (int shadestep)
 Set value of shadestep.
ScatterH2rescale (const char *name, double xscale, double yscale) const
 Return a new plot with all data points multiplied by a constant.

Private Member Functions

virtual Int_t Fill (Axis_t, const char *, Stat_t)
virtual Int_t Fill (const char *, Axis_t, Stat_t)
virtual Int_t Fill (const char *, const char *, Stat_t)
Int_t Fill (Axis_t)
Int_t Fill (const char *, Stat_t)
 ClassDef (RootUtils::ScatterH2, 1)

Private Attributes

bool m_scatter
 The scatter flag.
int m_shadestep
 shadestep option.
std::vector< Pairm_vals
 The collection of points that have been plotted.

Detailed Description

A 2-D histogram that can draw a proper scatter plot.

This is a variation on TH2F that can draw itself as a proper scatter plot — that is, each point really corresponds exactly to one Fill() call. (The default behavior of TH2F bins the histogram, and then dithers in random points when asked to draw a scatter plot.)

This is, in essence, a combination of TGraph2D and TH2F.

Use scatter() to control whether we plot as a real scatter plot, or if we get the default root behavior.

Note that the implementation requires saving the values of all the points. Beware if you're trying to make plots with a really huge number of points.

If scatter plotting is on, it will be drawn by putting the default polymarker at each point. Note that once the plot starts getting dense, this can hide the structure of the distribution: if all points are set, then adding a new point makes no visible difference. To help with this, shading can also be used to show the distribution density. This is controlled by the parameter shadestep.

If shadestep is zero, no shading is done, and markers are drawn with the default color.

If shadestep is greater than zero, then the color used for each pixel is ((N-1)*shadestep, 0, 0), (as (R,G,B)), where N is the number of data points plotted on the pixel.

If shadestep is less then zero, the shading is done similarly, except that the scale factor is chosen to that the densest pixel will have a R channel of 255.

Definition at line 66 of file ScatterH2.h.

Constructor & Destructor Documentation

◆ ScatterH2() [1/4]

RootUtils::ScatterH2::ScatterH2 ( )

Default constructor.

Definition at line 34 of file ScatterH2.cxx.

35 : m_scatter (true),
36 m_shadestep (0)
37{
38}
int m_shadestep
shadestep option.
Definition ScatterH2.h:228
bool m_scatter
The scatter flag.
Definition ScatterH2.h:219

◆ ScatterH2() [2/4]

RootUtils::ScatterH2::ScatterH2 ( const char * name,
const char * title,
Int_t nbinsx,
Axis_t xlow,
Axis_t xup,
Int_t nbinsy,
Axis_t ylow,
Axis_t yup )

Constructor.

Parameters
nameHistogram name.
titleHistogram title.
nbinsxNumber of bins on x-axis.
xlowx-axis lower limit.
xupx-axis upper limit.
nbinsyNumber of bins on y-axis.
ylowy-axis lower limit.
yupy-axis upper limit.

Definition at line 52 of file ScatterH2.cxx.

55 : TH2F (name, title, nbinsx, xlow, xup, nbinsy, ylow, yup),
56 m_scatter (true),
57 m_shadestep (0)
58{
59}
TH2F(name, title, nxbins, bins_par2, bins_par3, bins_par4, bins_par5=None, bins_par6=None, path='', **kwargs)

◆ ScatterH2() [3/4]

RootUtils::ScatterH2::ScatterH2 ( const char * name,
const char * title,
Int_t nbinsx,
const Double_t * xbins,
Int_t nbinsy,
Axis_t ylow,
Axis_t yup )

Constructor.

Parameters
nameHistogram name.
titleHistogram title.
nbinsxNumber of bins on x-axis.
xbinsArray of bin boundaries.
nbinsyNumber of bins on y-axis.
ylowy-axis lower limit.
yupy-axis upper limit.

Definition at line 72 of file ScatterH2.cxx.

75 : TH2F (name, title, nbinsx, xbins, nbinsy, ylow, yup),
76 m_scatter (true),
77 m_shadestep (0)
78{
79}

◆ ScatterH2() [4/4]

RootUtils::ScatterH2::ScatterH2 ( const char * name,
const char * title,
const TArrayD & xbins,
Int_t nbinsy,
Axis_t ylow,
Axis_t yup )

Constructor.

Parameters
nameHistogram name.
titleHistogram title.
xbinsArray of bin boundaries.
nbinsyNumber of bins on y-axis.
ylowy-axis lower limit.
yupy-axis upper limit.

Definition at line 92 of file ScatterH2.cxx.

95 : TH2F (name, title, xbins.GetSize()-1, xbins.GetArray(),
96 nbinsy, ylow, yup),
97 m_scatter (true),
98 m_shadestep (0)
99{
100}

Member Function Documentation

◆ ClassDef()

RootUtils::ScatterH2::ClassDef ( RootUtils::ScatterH2 ,
1  )
private

◆ Fill() [1/7]

Int_t RootUtils::ScatterH2::Fill ( Axis_t x,
Axis_t y )
virtual

Standard ROOT fill method.

Parameters
xx-coordinate.
yy-coordinate.

Definition at line 288 of file ScatterH2.cxx.

289{
290 return Fill (x, y, 1.0);
291}
#define y
#define x
virtual Int_t Fill(Axis_t x, Axis_t y, Stat_t w)
Standard ROOT fill method.

◆ Fill() [2/7]

Int_t RootUtils::ScatterH2::Fill ( Axis_t x,
Axis_t y,
Stat_t w )
virtual

Standard ROOT fill method.

Parameters
xx-coordinate.
yy-coordinate.
wWeight.

This overrides the TH2 Fill method. The data point is saved in our list.

Definition at line 275 of file ScatterH2.cxx.

276{
277 // Remember the point in m_vals.
278 m_vals.push_back (Pair (x, y));
279 return TH2F::Fill (x, y, w);
280}
std::vector< Pair > m_vals
The collection of points that have been plotted.
Definition ScatterH2.h:240
One point. Avoid std::pair so that we don't have duplicate dicts.
Definition ScatterH2.h:233

◆ Fill() [3/7]

Int_t RootUtils::ScatterH2::Fill ( Axis_t )
inlineprivate

Definition at line 249 of file ScatterH2.h.

249{return -1;}

◆ Fill() [4/7]

virtual Int_t RootUtils::ScatterH2::Fill ( Axis_t ,
const char * ,
Stat_t  )
inlineprivatevirtual

Definition at line 246 of file ScatterH2.h.

246{ return -1; }

◆ Fill() [5/7]

virtual Int_t RootUtils::ScatterH2::Fill ( const char * ,
Axis_t ,
Stat_t  )
inlineprivatevirtual

Definition at line 247 of file ScatterH2.h.

247{ return -1; }

◆ Fill() [6/7]

virtual Int_t RootUtils::ScatterH2::Fill ( const char * ,
const char * ,
Stat_t  )
inlineprivatevirtual

Definition at line 248 of file ScatterH2.h.

248{ return -1; }

◆ Fill() [7/7]

Int_t RootUtils::ScatterH2::Fill ( const char * ,
Stat_t  )
inlineprivate

Definition at line 250 of file ScatterH2.h.

250{return -1;}

◆ Paint()

void RootUtils::ScatterH2::Paint ( Option_t * option = "")
virtual

Standard ROOT paint method.

Parameters
optionPaint options. See TH2.

This overrides the TH2 Paint method. It suppresses the normal histogram plot if scatter mode is on.

Definition at line 110 of file ScatterH2.cxx.

111{
112 if (!m_scatter) {
113 TH2F::Paint (option);
114 return;
115 }
116
117 // Muck with things to prevent the normal histogram output.
118 double oldmax = GetMaximumStored ();
119 double oldmin = GetMinimumStored ();
120 SetMaximum (0);
121 SetMinimum (0);
122
123 // Get the frame, etc., drawn.
124 TH2F::Paint (option);
125
126 SetMaximum (oldmax);
127 SetMinimum (oldmin);
128
129 // Gotta to this to get the coordinates set up properly.
130 gPad->Update();
131
132 // Work around a root bug.
133 // THistPainter::PaintTable2 will add a TPaveStats object
134 // to the function list. However, if we then try to paint
135 // the same histo again, then there is code in THistPainter
136 // that expects the things in the function list to derive
137 // from TF1. There's no checking on the downcast, so root
138 // crashes...
139 GetListOfFunctions()->Clear();
140
141 // Get coordinate bounds.
142 Double_t u, v;
143 Double_t umin = gPad->GetUxmin();
144 Double_t umax = gPad->GetUxmax();
145 Double_t vmin = gPad->GetUymin();
146 Double_t vmax = gPad->GetUymax();
147
148 int pxmin = gPad->XtoAbsPixel (umin);
149 int pxmax = gPad->XtoAbsPixel (umax);
150 int pymin = gPad->YtoAbsPixel (vmin);
151 int pymax = gPad->YtoAbsPixel (vmax);
152 if (pxmin > pxmax) std::swap (pxmin, pxmax);
153 if (pymin > pymax) std::swap (pymin, pymax);
154 int pxsize = pxmax - pxmin + 1;
155 int pysize = pymax - pymin + 1;
156 std::vector<unsigned int> counts (pxsize * pysize);
157
158 TVirtualPad* pad = gPad;
159
160 // Count the number of points on each pixel.
161 unsigned int maxcount = 0;
162 unsigned int n = m_vals.size();
163 for (unsigned int i=0; i<n; i++) {
164 u = pad->XtoPad (m_vals[i].first);
165 v = pad->YtoPad (m_vals[i].second);
166 if (u < umin) u = umin;
167 if (u > umax) u = umax;
168 if (v < vmin) v = vmin;
169 if (v > vmax) v = vmax;
170
171 int px = pad->XtoAbsPixel (u);
172 int py = pad->YtoAbsPixel (v);
173 if (px < pxmin || px > pxmax || py < pymin || py > pymax) continue;
174 unsigned ndx = (px-pxmin) + (py-pymin) * pxsize;
175 assert (ndx < counts.size());
176 ++counts[ndx];
177 if (counts[ndx] > maxcount)
178 maxcount = counts[ndx];
179 }
180
181 const unsigned int NCOLOR = 255;
182
183 // Figure out shading.
184 float step = 0;
185 if (m_shadestep) {
186 if (m_shadestep > 0)
188 else if (maxcount > 2)
189 step = static_cast<float>(NCOLOR-1)/(maxcount-1);
190 else
191 step = NCOLOR-1;
192 }
193
194 // Count the number of points for each color.
195 unsigned int ccounts[NCOLOR];
196 std::uninitialized_fill (ccounts, ccounts+NCOLOR, 0);
197 size_t ndxmax = counts.size();
198 for (unsigned int i=0; i < ndxmax; i++) {
199 unsigned int count = counts[i];
200 if (count > 0) {
201 unsigned int color = static_cast<unsigned int> ((count-1)*step);
202 if (color >= NCOLOR) color = NCOLOR-1;
203 ++ccounts[color];
204 }
205 }
206
207 TPolyMarker* pms[NCOLOR];
208 std::uninitialized_fill (pms, pms+NCOLOR, (TPolyMarker*)0);
209
210 // Fill in the polymarkers.
211 for (int py = pymin; py <= pymax; ++py) {
212 for (int px = pxmin; px <= pxmax; ++px) {
213 unsigned int ndx = (px-pxmin) + (py-pymin) * pxsize;
214 assert (ndx < ndxmax);
215 unsigned int count = counts[ndx];
216 if (count > 0) {
217 Double_t u = pad->AbsPixeltoX (px);
218 Double_t v = pad->AbsPixeltoY (py);
219 unsigned int color = static_cast<unsigned int> ((count-1)*step);
220 if (color >= NCOLOR) color = NCOLOR-1;
221
222 TPolyMarker* pm = pms[color];
223 if (!pm) {
224 pm = new TPolyMarker (ccounts[color]);
225 ccounts[color] = 0;
226 pm->SetMarkerStyle (GetMarkerStyle());
227 pm->SetMarkerSize (GetMarkerSize());
228 if (color != 0)
229 pm->SetMarkerColor (TColor::GetColor (color, 0, 0));
230 pms[color] = pm;
231 }
232 pm->SetPoint (ccounts[color]++, u, v);
233 }
234 }
235 }
236
237 // Draw the points.
238 for (unsigned int i = 0; i < NCOLOR; i++) {
239 if (pms[i]) {
240 pms[i]->Paint();
241 delete pms[i];
242 }
243 }
244}
int count(std::string s, const std::string &regx)
count how many occurances of a regx are in a string
Definition hcg.cxx:146
@ u
Enums for curvilinear frames.
Definition ParamDefs.h:77
void swap(ElementLinkVector< DOBJ > &lhs, ElementLinkVector< DOBJ > &rhs)

◆ rescale()

ScatterH2 * RootUtils::ScatterH2::rescale ( const char * name,
double xscale,
double yscale ) const

Return a new plot with all data points multiplied by a constant.

Parameters
nameName for the new plot.
xscalex-axis scale factor.
yscaley-axis scale factor.

Definition at line 357 of file ScatterH2.cxx.

358{
359 int nx = GetXaxis()->GetNbins();
360 double xlo = GetXaxis()->GetXmin();
361 double xhi = GetXaxis()->GetXmax();
362
363 int ny = GetYaxis()->GetNbins();
364 double ylo = GetYaxis()->GetXmin();
365 double yhi = GetYaxis()->GetXmax();
366
367#if 0
368 ScatterH2* hnew = new ScatterH2 (name, GetTitle(),
369 nx, xscale*xlo, xscale*xhi,
370 ny, yscale*ylo, yscale*yhi);
371#endif
372 ScatterH2* hnew = dynamic_cast<ScatterH2*> (this->Clone (name));
373 if (hnew == 0)
374 return 0;
375 hnew->GetXaxis()->Set (nx, xscale*xlo, xscale*xhi);
376 hnew->GetYaxis()->Set (ny, yscale*ylo, yscale*yhi);
377 size_t n = hnew->m_vals.size();
378 for (size_t i = 0; i < n; i++) {
379 hnew->m_vals[i].first *= xscale;
380 hnew->m_vals[i].second *= yscale;
381 }
382
383 return hnew;
384}
ScatterH2()
Default constructor.
Definition ScatterH2.cxx:34

◆ Reset()

void RootUtils::ScatterH2::Reset ( Option_t * option = "")
virtual

Standard ROOT reset method.

Parameters
optionReset options. See TH2.

This overrides the TH2 Reset method. We may need to clear the saved points, too.

Definition at line 254 of file ScatterH2.cxx.

255{
256 TString opt = option;
257 if (!opt.Contains("ICE")) {
258 // Clear out m_vals too, releasing memory.
259 std::vector<Pair> tmp;
260 m_vals.swap (tmp);
261 }
262 TH2F::Reset (option);
263}

◆ scatter() [1/2]

bool RootUtils::ScatterH2::scatter ( ) const

Get the current value of the scatter flag.

If it's true, then this histogram will draw as a real scatter plot. Otherwise, the default root behavior is used.

Definition at line 300 of file ScatterH2.cxx.

301{
302 return m_scatter;
303}

◆ scatter() [2/2]

bool RootUtils::ScatterH2::scatter ( bool flag)

Set the scatter flag.

Parameters
flagThe new value of the scatter flag.
Returns
The previous value of the scatter flag.

If it's true, then this histogram will draw as a real scatter plot. Otherwise, the default root behavior is used.

Definition at line 314 of file ScatterH2.cxx.

315{
316 bool x = m_scatter;
317 m_scatter = flag;
318 return x;
319}
bool flag
Definition master.py:29

◆ shadestep() [1/2]

int RootUtils::ScatterH2::shadestep ( ) const

Get the current value of shadestep.

If zero, no shading is done. If greater than zero, pixel brightness is increased by this much for each additional data point. If less than zero, pixel brightness is scaled so that pixels with the most data points have a brightness of 255.

Definition at line 331 of file ScatterH2.cxx.

332{
333 return m_shadestep;
334}

◆ shadestep() [2/2]

int RootUtils::ScatterH2::shadestep ( int shadestep)

Set value of shadestep.

Parameters
shadestepThe new value for shadestep.
Returns
The previous value of shadestep.

If zero, no shading is done. If greater than zero, pixel brightness is increased by this much for each additional data point. If less than zero, pixel brightness is scaled so that pixels with the most data points have a brightness of 255.

Definition at line 348 of file ScatterH2.cxx.

349{
350 int x = m_shadestep;
352 return x;
353}
int shadestep() const
Get the current value of shadestep.

Member Data Documentation

◆ m_scatter

bool RootUtils::ScatterH2::m_scatter
private

The scatter flag.

If true, plot as a scatter plot.

Definition at line 219 of file ScatterH2.h.

◆ m_shadestep

int RootUtils::ScatterH2::m_shadestep
private

shadestep option.

If zero, no shading is done. If greater than zero, pixel brightness is increased by this much for each additional data point. If less than zero, pixel brightness is scaled so that pixels with the most data points have a brightness of 255.

Definition at line 228 of file ScatterH2.h.

◆ m_vals

std::vector<Pair> RootUtils::ScatterH2::m_vals
private

The collection of points that have been plotted.

Definition at line 240 of file ScatterH2.h.


The documentation for this class was generated from the following files: