ATLAS Offline Software
ScatterH2.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 // $Id: ScatterH2.cxx,v 1.5 2008-01-17 20:56:38 ssnyder Exp $
14 #include "RootUtils/ScatterH2.h"
15 #include "TVirtualPad.h"
16 #include "TPolyMarker.h"
17 #include "TColor.h"
18 #include "TArrayD.h"
19 #include "TList.h"
20 #include "TROOT.h"
21 #include "TClass.h"
22 #include "TMemberInspector.h"
23 
24 #include <vector>
25 #include <cassert>
26 
27 
28 namespace RootUtils {
29 
30 
35  : m_scatter (true),
36  m_shadestep (0)
37 {
38 }
39 
40 
52 ScatterH2::ScatterH2 (const char *name, const char *title,
53  Int_t nbinsx, Axis_t xlow, Axis_t xup,
54  Int_t nbinsy, Axis_t ylow, Axis_t yup)
55  : TH2F (name, title, nbinsx, xlow, xup, nbinsy, ylow, yup),
56  m_scatter (true),
57  m_shadestep (0)
58 {
59 }
60 
61 
72 ScatterH2::ScatterH2 (const char *name, const char *title,
73  Int_t nbinsx, const Double_t* xbins,
74  Int_t nbinsy, Axis_t ylow, Axis_t yup)
75  : TH2F (name, title, nbinsx, xbins, nbinsy, ylow, yup),
76  m_scatter (true),
77  m_shadestep (0)
78 {
79 }
80 
81 
82 
92 ScatterH2::ScatterH2 (const char *name, const char *title,
93  const TArrayD& xbins,
94  Int_t nbinsy, Axis_t ylow, Axis_t yup)
95  : TH2F (name, title, xbins.GetSize()-1, xbins.GetArray(),
96  nbinsy, ylow, yup),
97  m_scatter (true),
98  m_shadestep (0)
99 {
100 }
101 
102 
110 void ScatterH2::Paint (Option_t* option /*= ""*/)
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 }
243 
244 
252 void ScatterH2::Reset (Option_t *option /*= ""*/)
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 }
262 
263 
273 Int_t ScatterH2::Fill (Axis_t x, Axis_t y, Stat_t w)
274 {
275  // Remember the point in m_vals.
276  m_vals.push_back (Pair (x, y));
277  return TH2F::Fill (x, y, w);
278 }
279 
280 
286 Int_t ScatterH2::Fill(Axis_t x, Axis_t y)
287 {
288  return Fill (x, y, 1.0);
289 }
290 
291 
298 bool ScatterH2::scatter () const
299 {
300  return m_scatter;
301 }
302 
303 
313 {
314  bool x = m_scatter;
315  m_scatter = flag;
316  return x;
317 }
318 
319 
330 {
331  return m_shadestep;
332 }
333 
334 
346 int ScatterH2::shadestep (int shadestep)
347 {
348  int x = m_shadestep;
350  return x;
351 }
352 
353 
354 ScatterH2*
355 ScatterH2::rescale (const char* name, double xscale, double yscale) const
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 }
383 
384 
385 } // namespace RootUtils
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
RootUtils
Definition: ILogger.h:20
color
Definition: jFexInputByteStreamTool.cxx:25
ScatterH2.h
A 2-D histogram that can draw a proper scatter plot.
test_pyathena.px
px
Definition: test_pyathena.py:18
RootUtils::ScatterH2::rescale
ScatterH2 * rescale(const char *name, double xscale, double yscale) const
Return a new plot with all data points multiplied by a constant.
Definition: ScatterH2.cxx:355
TH2F
Definition: rootspy.cxx:420
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
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
beamspotman.n
n
Definition: beamspotman.py:731
covarianceTool.title
title
Definition: covarianceTool.py:542
master.flag
bool flag
Definition: master.py:29
color
color
Definition: BinsDiffFromStripMedian.h:18
RootUtils::ScatterH2::Paint
virtual void Paint(Option_t *option="")
Standard ROOT paint method.
Definition: ScatterH2.cxx:110
RootUtils::ScatterH2::Pair
One point. Avoid std::pair so that we don't have duplicate dicts.
Definition: ScatterH2.h:233
WritePulseShapeToCool.xlo
xlo
Definition: WritePulseShapeToCool.py:133
DeMoUpdate.tmp
string tmp
Definition: DeMoUpdate.py:1167
RootUtils::ScatterH2::Reset
virtual void Reset(Option_t *option="")
Standard ROOT reset method.
Definition: ScatterH2.cxx:252
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
RootUtils::ScatterH2::scatter
bool scatter() const
Get the current value of the scatter flag.
Definition: ScatterH2.cxx:298
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
A 2-D histogram that can draw a proper scatter plot.
Definition: ScatterH2.h:68
RootUtils::ScatterH2::ScatterH2
ScatterH2()
Default constructor.
Definition: ScatterH2.cxx:34
PlotCalibFromCool.vmax
vmax
Definition: PlotCalibFromCool.py:697