ATLAS Offline Software
Classes | Public Member Functions | Private Member Functions | Private Attributes | List of all members
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. More...
 
 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. More...
 
 ScatterH2 (const char *name, const char *title, Int_t nbinsx, const Double_t *xbins, Int_t nbinsy, Axis_t ylow, Axis_t yup)
 Constructor. More...
 
 ScatterH2 (const char *name, const char *title, const TArrayD &xbins, Int_t nbinsy, Axis_t ylow, Axis_t yup)
 Constructor. More...
 
virtual void Paint (Option_t *option="")
 Standard ROOT paint method. More...
 
virtual void Reset (Option_t *option="")
 Standard ROOT reset method. More...
 
virtual Int_t Fill (Axis_t x, Axis_t y, Stat_t w)
 Standard ROOT fill method. More...
 
virtual Int_t Fill (Axis_t x, Axis_t y)
 Standard ROOT fill method. More...
 
bool scatter () const
 Get the current value of the scatter flag. More...
 
bool scatter (bool flag)
 Set the scatter flag. More...
 
int shadestep () const
 Get the current value of shadestep. More...
 
int shadestep (int shadestep)
 Set value of shadestep. More...
 
ScatterH2rescale (const char *name, double xscale, double yscale) const
 Return a new plot with all data points multiplied by a constant. More...
 
double GetBinContent (int) const
 
void SetBinContent (int, double)
 

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. More...
 
int m_shadestep
 shadestep option. More...
 
std::vector< Pairm_vals
 The collection of points that have been plotted. More...
 

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 }

◆ 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 }

◆ 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  ,
 
)
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 286 of file ScatterH2.cxx.

287 {
288  return Fill (x, y, 1.0);
289 }

◆ 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 273 of file ScatterH2.cxx.

274 {
275  // Remember the point in m_vals.
276  m_vals.push_back (Pair (x, y));
277  return TH2F::Fill (x, y, w);
278 }

◆ 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;}

◆ GetBinContent()

double TH2F::GetBinContent ( int  i) const
inherited

Definition at line 425 of file rootspy.cxx.

425 { LOGCALLC; return ret(((double(*)(const void*,int))realaddr)(this,i)); }

◆ 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)
187  step = m_shadestep;
188  else
189  step = static_cast<float>(NCOLOR-1)/(maxcount-1);
190  }
191 
192  // Count the number of points for each color.
193  unsigned int ccounts[NCOLOR];
194  std::uninitialized_fill (ccounts, ccounts+NCOLOR, 0);
195  size_t ndxmax = counts.size();
196  for (unsigned int i=0; i < ndxmax; i++) {
197  unsigned int count = counts[i];
198  if (count > 0) {
199  unsigned int color = static_cast<unsigned int> ((count-1)*step);
200  if (color >= NCOLOR) color = NCOLOR-1;
201  ++ccounts[color];
202  }
203  }
204 
205  TPolyMarker* pms[NCOLOR];
206  std::uninitialized_fill (pms, pms+NCOLOR, (TPolyMarker*)0);
207 
208  // Fill in the polymarkers.
209  for (int py = pymin; py <= pymax; ++py) {
210  for (int px = pxmin; px <= pxmax; ++px) {
211  unsigned int ndx = (px-pxmin) + (py-pymin) * pxsize;
212  assert (ndx < ndxmax);
213  unsigned int count = counts[ndx];
214  if (count > 0) {
215  Double_t u = pad->AbsPixeltoX (px);
216  Double_t v = pad->AbsPixeltoY (py);
217  unsigned int color = static_cast<unsigned int> ((count-1)*step);
218  if (color >= NCOLOR) color = NCOLOR-1;
219 
220  TPolyMarker* pm = pms[color];
221  if (!pm) {
222  pm = new TPolyMarker (ccounts[color]);
223  ccounts[color] = 0;
224  pm->SetMarkerStyle (GetMarkerStyle());
225  pm->SetMarkerSize (GetMarkerSize());
226  if (color != 0)
227  pm->SetMarkerColor (TColor::GetColor (color, 0, 0));
228  pms[color] = pm;
229  }
230  pm->SetPoint (ccounts[color]++, u, v);
231  }
232  }
233  }
234 
235  // Draw the points.
236  for (unsigned int i = 0; i < NCOLOR; i++) {
237  if (pms[i]) {
238  pms[i]->Paint();
239  delete pms[i];
240  }
241  }
242 }

◆ 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 355 of file ScatterH2.cxx.

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

◆ 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 252 of file ScatterH2.cxx.

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

◆ 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 298 of file ScatterH2.cxx.

299 {
300  return m_scatter;
301 }

◆ 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 312 of file ScatterH2.cxx.

313 {
314  bool x = m_scatter;
315  m_scatter = flag;
316  return x;
317 }

◆ SetBinContent()

void TH2F::SetBinContent ( int  i,
double  d 
)
inherited

Definition at line 426 of file rootspy.cxx.

426 { LOGCALL; ((void(*)(void*,int,double))realaddr)(this,i,d);ret(); }

◆ 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 329 of file ScatterH2.cxx.

330 {
331  return m_shadestep;
332 }

◆ 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 346 of file ScatterH2.cxx.

347 {
348  int x = m_shadestep;
350  return x;
351 }

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:
WritePulseShapeToCool.yhi
yhi
Definition: WritePulseShapeToCool.py:153
python.SystemOfUnits.second
int second
Definition: SystemOfUnits.py:120
RootUtils::ScatterH2::m_shadestep
int m_shadestep
shadestep option.
Definition: ScatterH2.h:228
color
Definition: jFexInputByteStreamTool.cxx:25
test_pyathena.px
px
Definition: test_pyathena.py:18
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
hist_file_dump.d
d
Definition: hist_file_dump.py:137
RootUtils::ScatterH2::m_vals
std::vector< Pair > m_vals
The collection of points that have been plotted.
Definition: ScatterH2.h:240
RootUtils::ScatterH2::shadestep
int shadestep() const
Get the current value of shadestep.
Definition: ScatterH2.cxx:329
PlotCalibFromCool.vmin
vmin
Definition: PlotCalibFromCool.py:696
python.TrigEgammaMonitorHelper.TH2F
def TH2F(name, title, nxbins, bins_par2, bins_par3, bins_par4, bins_par5=None, bins_par6=None, path='', **kwargs)
Definition: TrigEgammaMonitorHelper.py:45
x
#define x
Trk::u
@ u
Enums for curvilinear frames.
Definition: ParamDefs.h:83
XMLtoHeader.count
count
Definition: XMLtoHeader.py:85
RootUtils::ScatterH2::m_scatter
bool m_scatter
The scatter flag.
Definition: ScatterH2.h:219
WritePulseShapeToCool.xhi
xhi
Definition: WritePulseShapeToCool.py:152
lumiFormat.i
int i
Definition: lumiFormat.py:92
ret
T ret(T t)
Definition: rootspy.cxx:260
beamspotman.n
n
Definition: beamspotman.py:731
LOGCALLC
#define LOGCALLC
Definition: rootspy.cxx:247
covarianceTool.title
title
Definition: covarianceTool.py:542
master.flag
bool flag
Definition: master.py:29
color
color
Definition: BinsDiffFromStripMedian.h:18
WritePulseShapeToCool.xlo
xlo
Definition: WritePulseShapeToCool.py:133
xAOD::double
double
Definition: CompositeParticle_v1.cxx:159
DeMoUpdate.tmp
string tmp
Definition: DeMoUpdate.py:1167
LOGCALL
#define LOGCALL
Definition: rootspy.cxx:246
WriteCalibToCool.swap
swap
Definition: WriteCalibToCool.py:94
WritePulseShapeToCool.ylo
ylo
Definition: WritePulseShapeToCool.py:134
Amg::py
@ py
Definition: GeoPrimitives.h:39
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
pmontree.opt
opt
Definition: pmontree.py:16
LArCellBinning.xbins
int xbins
Definition: LArCellBinning.py:163
python.PyAthena.v
v
Definition: PyAthena.py:157
y
#define y
DeMoScan.first
bool first
Definition: DeMoScan.py:534
LArCellBinning.step
step
Definition: LArCellBinning.py:158
RootUtils::ScatterH2::Fill
virtual Int_t Fill(Axis_t x, Axis_t y, Stat_t w)
Standard ROOT fill method.
Definition: ScatterH2.cxx:273
python.IoTestsLib.w
def w
Definition: IoTestsLib.py:200
RootUtils::ScatterH2::ScatterH2
ScatterH2()
Default constructor.
Definition: ScatterH2.cxx:34
PlotCalibFromCool.vmax
vmax
Definition: PlotCalibFromCool.py:697