CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Classes | Functions | Variables
plotting Namespace Reference

Classes

class  AggregateBins
 
class  AggregateHistos
 
class  DQMSubFolder
 
class  FakeDuplicate
 
class  Frame
 
class  FrameRatio
 
class  FrameTGraph2D
 
class  GetDirectoryCode
 
class  Plot
 
class  PlotFolder
 
class  PlotGroup
 
class  Plotter
 
class  PlotterFolder
 
class  PlotterInstance
 
class  PlotterItem
 
class  PlotterTableItem
 
class  ROC
 
class  Subtract
 

Functions

def _copyStyle
 
def _createCanvas
 
def _drawFrame
 
def _findBounds
 
def _getDirectory
 
def _getDirectoryDetailed
 
def _getObject
 
def _getOrCreateObject
 
def _getXmax
 
def _getXmin
 
def _getYmax
 
def _getYmaxWithError
 
def _getYmin
 
def _getYminIgnoreOutlier
 

Variables

list _plotStylesColor = [4, 2, ROOT.kBlack, ROOT.kOrange+7, ROOT.kMagenta-3]
 
list _plotStylesMarker = [21, 20, 22, 34, 33]
 
 verbose = False
 

Function Documentation

def plotting._copyStyle (   src,
  dst 
)
private

Definition at line 889 of file plotting.py.

890 def _copyStyle(src, dst):
891  properties = []
892  if hasattr(src, "GetLineColor") and hasattr(dst, "SetLineColor"):
893  properties.extend(["LineColor", "LineStyle", "LineWidth"])
894  if hasattr(src, "GetFillColor") and hasattr(dst, "SetFillColor"):
895  properties.extend(["FillColor", "FillStyle"])
896  if hasattr(src, "GetMarkerColor") and hasattr(dst, "SetMarkerColor"):
897  properties.extend(["MarkerColor", "MarkerSize", "MarkerStyle"])
898 
899  for prop in properties:
900  getattr(dst, "Set"+prop)(getattr(src, "Get"+prop)())
def _copyStyle
Definition: plotting.py:889
def plotting._createCanvas (   name,
  width,
  height 
)
private

Definition at line 65 of file plotting.py.

Referenced by plotting.PlotGroup._drawSeparate(), and plotting.PlotGroup.draw().

65 
66 def _createCanvas(name, width, height):
67  # silence warning of deleting canvas with the same name
68  if not verbose:
69  backup = ROOT.gErrorIgnoreLevel
70  ROOT.gErrorIgnoreLevel = ROOT.kError
71  canvas = ROOT.TCanvas(name, name, width, height)
72  if not verbose:
73  ROOT.gErrorIgnoreLevel = backup
74  return canvas
75 
def _createCanvas
Definition: plotting.py:65
def plotting._drawFrame (   pad,
  bounds,
  xbinlabels = None,
  xbinlabelsize = None,
  xbinlabeloption = None,
  suffix = "" 
)
private
Function to draw a frame

Arguments:
pad    -- TPad to where the frame is drawn
bounds -- List or 4-tuple for (xmin, ymin, xmax, ymax)

Keyword arguments:
xbinlabels      -- Optional list of strings for x axis bin labels
xbinlabelsize   -- Optional number for the x axis bin label size
xbinlabeloption -- Optional string for the x axis bin options (passed to ROOT.TH1.LabelsOption())
suffix          -- Optional string for a postfix of the frame name

Definition at line 556 of file plotting.py.

557 def _drawFrame(pad, bounds, xbinlabels=None, xbinlabelsize=None, xbinlabeloption=None, suffix=""):
558  """Function to draw a frame
559 
560  Arguments:
561  pad -- TPad to where the frame is drawn
562  bounds -- List or 4-tuple for (xmin, ymin, xmax, ymax)
563 
564  Keyword arguments:
565  xbinlabels -- Optional list of strings for x axis bin labels
566  xbinlabelsize -- Optional number for the x axis bin label size
567  xbinlabeloption -- Optional string for the x axis bin options (passed to ROOT.TH1.LabelsOption())
568  suffix -- Optional string for a postfix of the frame name
569  """
570  if xbinlabels is None:
571  frame = pad.DrawFrame(*bounds)
572  else:
573  # Special form needed if want to set x axis bin labels
574  nbins = len(xbinlabels)
575  frame = ROOT.TH1F("hframe"+suffix, "", nbins, bounds[0], bounds[2])
576  frame.SetBit(ROOT.TH1.kNoStats)
577  frame.SetBit(ROOT.kCanDelete)
578  frame.SetMinimum(bounds[1])
579  frame.SetMaximum(bounds[3])
580  frame.GetYaxis().SetLimits(bounds[1], bounds[3])
581  frame.Draw("")
582 
583  xaxis = frame.GetXaxis()
584  for i in xrange(nbins):
585  xaxis.SetBinLabel(i+1, xbinlabels[i])
586  if xbinlabelsize is not None:
587  xaxis.SetLabelSize(xbinlabelsize)
588  if xbinlabeloption is not None:
589  frame.LabelsOption(xbinlabeloption)
590 
591  return frame
def _drawFrame
Definition: plotting.py:556
def plotting._findBounds (   th1s,
  ylog,
  xmin = None,
  xmax = None,
  ymin = None,
  ymax = None 
)
private
Find x-y axis boundaries encompassing a list of TH1s if the bounds are not given in arguments.

Arguments:
th1s -- List of TH1s
ylog -- Boolean indicating if y axis is in log scale or not (affects the automatic ymax)

Keyword arguments:
xmin -- Minimum x value; if None, take the minimum of TH1s
xmax -- Maximum x value; if None, take the maximum of TH1s
xmin -- Minimum y value; if None, take the minimum of TH1s
xmax -- Maximum y value; if None, take the maximum of TH1s

Definition at line 138 of file plotting.py.

References _getXmax(), _getXmin(), _getYmax(), _getYmin(), _getYminIgnoreOutlier(), alcazmumu_cfi.filter, bookConverter.max, and min().

139 def _findBounds(th1s, ylog, xmin=None, xmax=None, ymin=None, ymax=None):
140  """Find x-y axis boundaries encompassing a list of TH1s if the bounds are not given in arguments.
141 
142  Arguments:
143  th1s -- List of TH1s
144  ylog -- Boolean indicating if y axis is in log scale or not (affects the automatic ymax)
145 
146  Keyword arguments:
147  xmin -- Minimum x value; if None, take the minimum of TH1s
148  xmax -- Maximum x value; if None, take the maximum of TH1s
149  xmin -- Minimum y value; if None, take the minimum of TH1s
150  xmax -- Maximum y value; if None, take the maximum of TH1s
151  """
152 
153  def y_scale_max(y):
154  if ylog:
155  return 1.5*y
156  return 1.05*y
157 
158  def y_scale_min(y):
159  # assuming log
160  return 0.9*y
161 
162  if xmin is None or xmax is None or ymin is None or ymax is None or \
163  isinstance(xmin, list) or isinstance(max, list) or isinstance(ymin, list) or isinstance(ymax, list):
164  xmins = []
165  xmaxs = []
166  ymins = []
167  ymaxs = []
168  for th1 in th1s:
169  xmins.append(_getXmin(th1, limitToNonZeroContent=isinstance(xmin, list)))
170  xmaxs.append(_getXmax(th1, limitToNonZeroContent=isinstance(xmax, list)))
171  if ylog and isinstance(ymin, list):
172  ymins.append(_getYminIgnoreOutlier(th1))
173  else:
174  ymins.append(_getYmin(th1))
175  ymaxs.append(_getYmax(th1))
176 # ymaxs.append(_getYmaxWithError(th1))
177 
178  if xmin is None:
179  xmin = min(xmins)
180  elif isinstance(xmin, list):
181  xm = min(xmins)
182  xmins_below = filter(lambda x: x<=xm, xmin)
183  if len(xmins_below) == 0:
184  xmin = min(xmin)
185  if xm < xmin:
186  if verbose:
187  print "Histogram minimum x %f is below all given xmin values %s, using the smallest one" % (xm, str(xmin))
188  else:
189  xmin = max(xmins_below)
190 
191  if xmax is None:
192  xmax = max(xmaxs)
193  elif isinstance(xmax, list):
194  xm = max(xmaxs)
195  xmaxs_above = filter(lambda x: x>xm, xmax)
196  if len(xmaxs_above) == 0:
197  xmax = max(xmax)
198  if xm > xmax:
199  if verbose:
200  print "Histogram maximum x %f is above all given xmax values %s, using the maximum one" % (xm, str(xmax))
201  else:
202  xmax = min(xmaxs_above)
203 
204  if ymin is None:
205  ymin = min(ymins)
206  elif isinstance(ymin, list):
207  ym_unscaled = min(ymins)
208  ym = y_scale_min(ym_unscaled)
209  ymins_below = filter(lambda y: y<=ym, ymin)
210  if len(ymins_below) == 0:
211  ymin = min(ymin)
212  if ym_unscaled < ymin:
213  if verbose:
214  print "Histogram minimum y %f is below all given ymin values %s, using the smallest one" % (ym, str(ymin))
215  else:
216  ymin = max(ymins_below)
217 
218  if ymax is None:
219  ymax = y_scale_max(max(ymaxs))
220  elif isinstance(ymax, list):
221  ym_unscaled = max(ymaxs)
222  ym = y_scale_max(ym_unscaled)
223  ymaxs_above = filter(lambda y: y>ym, ymax)
224  if len(ymaxs_above) == 0:
225  ymax = max(ymax)
226  if ym_unscaled > ymax:
227  if verbose:
228  print "Histogram maximum y %f is above all given ymax values %s, using the maximum one" % (ym_unscaled, str(ymax))
229  else:
230  ymax = min(ymaxs_above)
231 
232  for th1 in th1s:
233  th1.GetXaxis().SetRangeUser(xmin, xmax)
234  th1.GetYaxis().SetRangeUser(ymin, ymax)
235 
236  return (xmin, ymin, xmax, ymax)
def _getYmin
Definition: plotting.py:104
def _getXmax
Definition: plotting.py:90
def _getYminIgnoreOutlier
Definition: plotting.py:121
def _getXmin
Definition: plotting.py:76
def _getYmax
Definition: plotting.py:111
def _findBounds
Definition: plotting.py:138
T min(T a, T b)
Definition: MathUtil.h:58
def plotting._getDirectory (   args,
  kwargs 
)
private

Definition at line 62 of file plotting.py.

References _getDirectoryDetailed().

Referenced by plotting.PlotterTableItem.create().

62 
63 def _getDirectory(*args, **kwargs):
64  return GetDirectoryCode.codesToNone(_getDirectoryDetailed(*args, **kwargs))
def _getDirectory
Definition: plotting.py:62
def _getDirectoryDetailed
Definition: plotting.py:38
def plotting._getDirectoryDetailed (   tfile,
  possibleDirs,
  subDir = None 
)
private
Get TDirectory from TFile.

Definition at line 38 of file plotting.py.

References join().

Referenced by _getDirectory(), and plotting.PlotterFolder.create().

38 
39 def _getDirectoryDetailed(tfile, possibleDirs, subDir=None):
40  """Get TDirectory from TFile."""
41  if tfile is None:
43  for pdf in possibleDirs:
44  d = tfile.Get(pdf)
45  if d:
46  if subDir is not None:
47  # Pick associator if given
48  d = d.Get(subDir)
49  if d:
50  return d
51  else:
52  if verbose:
53  print "Did not find subdirectory '%s' from directory '%s' in file %s" % (subDir, pdf, tfile.GetName())
54 # if "Step" in subDir:
55 # raise Exception("Foo")
57  else:
58  return d
59  if verbose:
60  print "Did not find any of directories '%s' from file %s" % (",".join(possibleDirs), tfile.GetName())
static std::string join(char **cmd)
Definition: RemoteFile.cc:18
def _getDirectoryDetailed
Definition: plotting.py:38
def plotting._getObject (   tdirectory,
  name 
)
private

Definition at line 14 of file plotting.py.

Referenced by _getOrCreateObject(), plotting.Subtract.create(), plotting.FakeDuplicate.create(), and plotting.AggregateHistos.create().

14 
15 def _getObject(tdirectory, name):
16  obj = tdirectory.Get(name)
17  if not obj:
18  if verbose:
19  print "Did not find {obj} from {dir}".format(obj=name, dir=tdirectory.GetPath())
20  return None
21  return obj
def _getObject
Definition: plotting.py:14
def plotting._getOrCreateObject (   tdirectory,
  nameOrCreator 
)
private

Definition at line 22 of file plotting.py.

References _getObject().

Referenced by plotting.Plot._createOne(), plotting.AggregateBins.create(), and plotting.ROC.create().

22 
23 def _getOrCreateObject(tdirectory, nameOrCreator):
24  if hasattr(nameOrCreator, "create"):
25  return nameOrCreator.create(tdirectory)
26  return _getObject(tdirectory, nameOrCreator)
def _getObject
Definition: plotting.py:14
def _getOrCreateObject
Definition: plotting.py:22
def plotting._getXmax (   obj,
  limitToNonZeroContent = False 
)
private

Definition at line 90 of file plotting.py.

References bookConverter.max.

Referenced by _findBounds().

90 
91 def _getXmax(obj, limitToNonZeroContent=False):
92  if isinstance(obj, ROOT.TH1):
93  xaxis = obj.GetXaxis()
94  if limitToNonZeroContent:
95  for i in xrange(obj.GetNbinsX(), 0, -1):
96  if obj.GetBinContent(i) != 0:
97  return xaxis.GetBinUpEdge(i)
98  return xaxis.GetBinUpEdge(xaxis.GetFirst())
99  else:
100  return xaxis.GetBinUpEdge(xaxis.GetLast())
101  elif isinstance(obj, ROOT.TGraph) or isinstance(obj, ROOT.TGraph2D):
102  return max([obj.GetX()[i] for i in xrange(0, obj.GetN())])*1.02
103  raise Exception("Unsupported type %s" % str(obj))
def _getXmax
Definition: plotting.py:90
def plotting._getXmin (   obj,
  limitToNonZeroContent = False 
)
private

Definition at line 76 of file plotting.py.

References min().

Referenced by _findBounds().

76 
77 def _getXmin(obj, limitToNonZeroContent=False):
78  if isinstance(obj, ROOT.TH1):
79  xaxis = obj.GetXaxis()
80  if limitToNonZeroContent:
81  for i in xrange(1, obj.GetNbinsX()+1):
82  if obj.GetBinContent(i) != 0:
83  return xaxis.GetBinLowEdge(i)
84  return xaxis.GetBinLowEdge(xaxis.GetLast())
85  else:
86  return xaxis.GetBinLowEdge(xaxis.GetFirst())
87  elif isinstance(obj, ROOT.TGraph) or isinstance(obj, ROOT.TGraph2D):
88  return min([obj.GetX()[i] for i in xrange(0, obj.GetN())])*0.9
89  raise Exception("Unsupported type %s" % str(obj))
def _getXmin
Definition: plotting.py:76
T min(T a, T b)
Definition: MathUtil.h:58
def plotting._getYmax (   obj)
private

Definition at line 111 of file plotting.py.

References bookConverter.max.

Referenced by _findBounds().

112 def _getYmax(obj):
113  if isinstance(obj, ROOT.TH1):
114  return obj.GetMaximum()
115  elif isinstance(obj, ROOT.TGraph) or isinstance(obj, ROOT.TGraph2D):
116  return max([obj.GetY()[i] for i in xrange(0, obj.GetN())])
117  raise Exception("Unsupported type %s" % str(obj))
def _getYmax
Definition: plotting.py:111
def plotting._getYmaxWithError (   th1)
private

Definition at line 118 of file plotting.py.

References bookConverter.max.

119 def _getYmaxWithError(th1):
120  return max([th1.GetBinContent(i)+th1.GetBinError(i) for i in xrange(1, th1.GetNbinsX()+1)])
def _getYmaxWithError
Definition: plotting.py:118
def plotting._getYmin (   obj)
private

Definition at line 104 of file plotting.py.

References min().

Referenced by _findBounds().

105 def _getYmin(obj):
106  if isinstance(obj, ROOT.TH1):
107  return obj.GetMinimum()
108  elif isinstance(obj, ROOT.TGraph) or isinstance(obj, ROOT.TGraph2D):
109  return min([obj.GetY()[i] for i in xrange(0, obj.GetN())])
110  raise Exception("Unsupported type %s" % str(obj))
def _getYmin
Definition: plotting.py:104
T min(T a, T b)
Definition: MathUtil.h:58
def plotting._getYminIgnoreOutlier (   th1)
private

Definition at line 121 of file plotting.py.

References alcazmumu_cfi.filter.

Referenced by _findBounds().

122 def _getYminIgnoreOutlier(th1):
123  yvals = filter(lambda n: n>0, [th1.GetBinContent(i) for i in xrange(1, th1.GetNbinsX()+1)])
124  yvals.sort()
125  if len(yvals) == 0:
126  return th1.GetMinimum()
127  if len(yvals) == 1:
128  return yvals[0]
129 
130  # Define outlier as being x10 less than minimum of the 95 % of the non-zero largest values
131  ind_min = len(yvals)-1 - int(len(yvals)*0.95)
132  min_val = yvals[ind_min]
133  for i in xrange(0, ind_min):
134  if yvals[i] > 0.1*min_val:
135  return yvals[i]
136 
137  return min_val
def _getYminIgnoreOutlier
Definition: plotting.py:121

Variable Documentation

list plotting._plotStylesColor = [4, 2, ROOT.kBlack, ROOT.kOrange+7, ROOT.kMagenta-3]

Definition at line 553 of file plotting.py.

list plotting._plotStylesMarker = [21, 20, 22, 34, 33]

Definition at line 554 of file plotting.py.

plotting.verbose = False

Definition at line 12 of file plotting.py.