CMS 3D CMS Logo

List of all members | Public Member Functions | Private Member Functions | Private Attributes
plotting.PlotGroup Class Reference

Public Member Functions

def __init__ (self, name, plots, kwargs)
 
def append (self, plot)
 
def clear (self)
 
def create (self, tdirectoryNEvents, requireAllHistograms=False)
 
def draw (self, legendLabels, prefix=None, separate=False, saveFormat=".pdf", ratio=True)
 
def getName (self)
 
def getPlot (self, name)
 
def getPlots (self)
 
def onlyForPileup (self)
 
def remove (self, name)
 
def setProperties (self, kwargs)
 

Private Member Functions

def _createLegend (self, plot, legendLabels, lx1, ly1, lx2, ly2, textSize=0.016, denomUncertainty=True)
 
def _drawSeparate (self, legendLabels, prefix, saveFormat, ratio)
 
def _modifyPadForRatio (self, pad)
 
def _save (self, canvas, saveFormat, prefix=None, postfix=None, single=False)
 

Private Attributes

 _name
 
 _plots
 
 _ratioFactor
 

Detailed Description

Group of plots, results a TCanvas

Definition at line 2101 of file plotting.py.

Constructor & Destructor Documentation

def plotting.PlotGroup.__init__ (   self,
  name,
  plots,
  kwargs 
)
Constructor.

Arguments:
name  -- String for name of the TCanvas, used also as the basename of the picture files
plots -- List of Plot objects

Keyword arguments:
ncols    -- Number of columns (default 2)
legendDx -- Float for moving TLegend in x direction (default None)
legendDy -- Float for moving TLegend in y direction (default None)
legendDw -- Float for changing TLegend width (default None)
legendDh -- Float for changing TLegend height (default None)
legend   -- Bool for disabling legend (default True for legend being enabled)
overrideLegendLabels -- List of strings for legend labels, if given, these are used instead of the ones coming from Plotter (default None)
onlyForPileup  -- Plots this group only for pileup samples

Definition at line 2103 of file plotting.py.

2103  def __init__(self, name, plots, **kwargs):
2104  """Constructor.
2105 
2106  Arguments:
2107  name -- String for name of the TCanvas, used also as the basename of the picture files
2108  plots -- List of Plot objects
2109 
2110  Keyword arguments:
2111  ncols -- Number of columns (default 2)
2112  legendDx -- Float for moving TLegend in x direction (default None)
2113  legendDy -- Float for moving TLegend in y direction (default None)
2114  legendDw -- Float for changing TLegend width (default None)
2115  legendDh -- Float for changing TLegend height (default None)
2116  legend -- Bool for disabling legend (default True for legend being enabled)
2117  overrideLegendLabels -- List of strings for legend labels, if given, these are used instead of the ones coming from Plotter (default None)
2118  onlyForPileup -- Plots this group only for pileup samples
2119  """
2120  self._name = name
2121  self._plots = plots
2122 
2123  def _set(attr, default):
2124  setattr(self, "_"+attr, kwargs.get(attr, default))
2125 
2126  _set("ncols", 2)
2127 
2128  _set("legendDx", None)
2129  _set("legendDy", None)
2130  _set("legendDw", None)
2131  _set("legendDh", None)
2132  _set("legend", True)
2133 
2134  _set("overrideLegendLabels", None)
2135 
2136  _set("onlyForPileup", False)
2137 
2138  self._ratioFactor = 1.25
2139 
def __init__(self, name, plots, kwargs)
Definition: plotting.py:2103

Member Function Documentation

def plotting.PlotGroup._createLegend (   self,
  plot,
  legendLabels,
  lx1,
  ly1,
  lx2,
  ly2,
  textSize = 0.016,
  denomUncertainty = True 
)
private

Definition at line 2327 of file plotting.py.

References GeometryComparisonPlotter._legend.

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

2327  def _createLegend(self, plot, legendLabels, lx1, ly1, lx2, ly2, textSize=0.016, denomUncertainty=True):
2328  if not self._legend:
2329  return None
2330 
2331  l = ROOT.TLegend(lx1, ly1, lx2, ly2)
2332  l.SetTextSize(textSize)
2333  l.SetLineColor(1)
2334  l.SetLineWidth(1)
2335  l.SetLineStyle(1)
2336  l.SetFillColor(0)
2337  l.SetMargin(0.07)
2338 
2339  plot.addToLegend(l, legendLabels, denomUncertainty)
2340  l.Draw()
2341  return l
2342 
def _createLegend(self, plot, legendLabels, lx1, ly1, lx2, ly2, textSize=0.016, denomUncertainty=True)
Definition: plotting.py:2327
def plotting.PlotGroup._drawSeparate (   self,
  legendLabels,
  prefix,
  saveFormat,
  ratio 
)
private
Internal method to do the drawing to separate files per Plot instead of a file per PlotGroup

Definition at line 2261 of file plotting.py.

References plotting._createCanvas(), plotting.PlotGroup._createLegend(), plotting.PlotGroup._modifyPadForRatio(), FP420HitsObject._name, TrackerHitsObject._name, PGeometricDet::Item._name, TrackingRecHitAlgorithm._name, LikelihoodSpecies._name, L1TMuon::PtAssignmentUnit._name, L1TMuon::PtRefinementUnit._name, LikelihoodPdfProduct._name, LikelihoodPdf._name, citk::IsolationConeDefinitionBase._name, Logger._name, hcaldqm::DQModule._name, DrellYanValidation._name, WValidation._name, HistoParams< T >._name, hcaldqm::flag::Flag._name, hcaldqm::quantity::Quantity._name, CutApplicatorBase._name, ElectronMVAEstimatorRun2Phys14NonTrig._name, PhotonMVAEstimatorRun2Spring15NonTrig._name, PhotonMVAEstimatorRun2Phys14NonTrig._name, ElectronMVAEstimatorRun2Spring15Trig._name, GeometricDetExtra._name, ElectronMVAEstimatorRun2Spring15NonTrig._name, HistoParams< TH2F >._name, HistoParams< TProfile2D >._name, Vispa.Views.PropertyView.Property._name, SequenceTypes.SequencePlaceholder._name, plotting.Subtract._name, plotting.Transform._name, plotting.FakeDuplicate._name, plotting.CutEfficiency._name, plotting.AggregateBins._name, plotting.AggregateHistos._name, plotting.ROC._name, plotting.Plot._name, plotting.PlotGroup._name, plotting.PlotGroup._plots, plotting.PlotGroup._ratioFactor, plotting.PlotGroup._save(), and createfilelist.int.

Referenced by plotting.PlotGroup.draw().

2261  def _drawSeparate(self, legendLabels, prefix, saveFormat, ratio):
2262  """Internal method to do the drawing to separate files per Plot instead of a file per PlotGroup"""
2263  width = 500
2264  height = 500
2265 
2266  canvas = _createCanvas(self._name+"Single", width, height)
2267  canvasRatio = _createCanvas(self._name+"SingleRatio", width, int(height*self._ratioFactor))
2268 
2269  # from TDRStyle
2270  for c in [canvas, canvasRatio]:
2271  c.SetTopMargin(0.05)
2272  c.SetBottomMargin(0.13)
2273  c.SetLeftMargin(0.16)
2274  c.SetRightMargin(0.05)
2275 
2276  lx1def = 0.6
2277  lx2def = 0.95
2278  ly1def = 0.85
2279  ly2def = 0.95
2280 
2281  ret = []
2282 
2283  for plot in self._plots:
2284  if plot.isEmpty():
2285  continue
2286 
2287  ratioForThisPlot = plot.isRatio(ratio)
2288  c = canvas
2289  if ratioForThisPlot:
2290  c = canvasRatio
2291  c.cd()
2292  self._modifyPadForRatio(c)
2293 
2294  # Draw plot to canvas
2295  c.cd()
2296  plot.draw(c, ratioForThisPlot, self._ratioFactor, 1)
2297 
2298  if plot._legend:
2299  # Setup legend
2300  lx1 = lx1def
2301  lx2 = lx2def
2302  ly1 = ly1def
2303  ly2 = ly2def
2304 
2305  if plot._legendDx is not None:
2306  lx1 += plot._legendDx
2307  lx2 += plot._legendDx
2308  if plot._legendDy is not None:
2309  ly1 += plot._legendDy
2310  ly2 += plot._legendDy
2311  if plot._legendDw is not None:
2312  lx2 += plot._legendDw
2313  if plot._legendDh is not None:
2314  ly1 -= plot._legendDh
2315 
2316  c.cd()
2317  legend = self._createLegend(plot, legendLabels, lx1, ly1, lx2, ly2, textSize=0.03,
2318  denomUncertainty=(ratioForThisPlot and plot.drawRatioUncertainty))
2319 
2320  ret.extend(self._save(c, saveFormat, prefix=prefix, postfix="_"+plot.getName(), single=True))
2321  return ret
2322 
def _save(self, canvas, saveFormat, prefix=None, postfix=None, single=False)
Definition: plotting.py:2343
def _modifyPadForRatio(self, pad)
Definition: plotting.py:2323
def _createLegend(self, plot, legendLabels, lx1, ly1, lx2, ly2, textSize=0.016, denomUncertainty=True)
Definition: plotting.py:2327
def _drawSeparate(self, legendLabels, prefix, saveFormat, ratio)
Definition: plotting.py:2261
def _createCanvas(name, width, height)
Definition: plotting.py:105
def plotting.PlotGroup._modifyPadForRatio (   self,
  pad 
)
private
Internal method to set divide a pad to two for ratio plots

Definition at line 2323 of file plotting.py.

References plotting.PlotGroup._ratioFactor.

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

2323  def _modifyPadForRatio(self, pad):
2324  """Internal method to set divide a pad to two for ratio plots"""
2325  _modifyPadForRatio(pad, self._ratioFactor)
2326 
def _modifyPadForRatio(self, pad)
Definition: plotting.py:2323
def plotting.PlotGroup._save (   self,
  canvas,
  saveFormat,
  prefix = None,
  postfix = None,
  single = False 
)
private

Definition at line 2343 of file plotting.py.

References FP420HitsObject._name, TrackerHitsObject._name, PGeometricDet::Item._name, TrackingRecHitAlgorithm._name, LikelihoodSpecies._name, L1TMuon::PtAssignmentUnit._name, L1TMuon::PtRefinementUnit._name, LikelihoodPdfProduct._name, LikelihoodPdf._name, citk::IsolationConeDefinitionBase._name, Logger._name, hcaldqm::DQModule._name, DrellYanValidation._name, WValidation._name, HistoParams< T >._name, hcaldqm::flag::Flag._name, hcaldqm::quantity::Quantity._name, CutApplicatorBase._name, ElectronMVAEstimatorRun2Phys14NonTrig._name, PhotonMVAEstimatorRun2Spring15NonTrig._name, PhotonMVAEstimatorRun2Phys14NonTrig._name, ElectronMVAEstimatorRun2Spring15Trig._name, GeometricDetExtra._name, ElectronMVAEstimatorRun2Spring15NonTrig._name, HistoParams< TH2F >._name, HistoParams< TProfile2D >._name, Vispa.Views.PropertyView.Property._name, SequenceTypes.SequencePlaceholder._name, plotting.Subtract._name, plotting.Transform._name, plotting.FakeDuplicate._name, plotting.CutEfficiency._name, plotting.AggregateBins._name, plotting.AggregateHistos._name, plotting.ROC._name, plotting.Plot._name, and plotting.PlotGroup._name.

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

2343  def _save(self, canvas, saveFormat, prefix=None, postfix=None, single=False):
2344  # Save the canvas to file and clear
2345  name = self._name
2346  if prefix is not None:
2347  name = prefix+name
2348  if postfix is not None:
2349  name = name+postfix
2350 
2351  if not verbose: # silence saved file printout
2352  backup = ROOT.gErrorIgnoreLevel
2353  ROOT.gErrorIgnoreLevel = ROOT.kWarning
2354  canvas.SaveAs(name+saveFormat)
2355  if not verbose:
2356  ROOT.gErrorIgnoreLevel = backup
2357 
2358  if single:
2359  canvas.Clear()
2360  canvas.SetLogx(False)
2361  canvas.SetLogy(False)
2362  else:
2363  canvas.Clear("D") # keep subpads
2364 
2365  return [name+saveFormat]
2366 
def _save(self, canvas, saveFormat, prefix=None, postfix=None, single=False)
Definition: plotting.py:2343
def plotting.PlotGroup.append (   self,
  plot 
)

Definition at line 2162 of file plotting.py.

Referenced by diclist.diclist.add().

2162  def append(self, plot):
2163  self._plots.append(plot)
2164 
def append(self, plot)
Definition: plotting.py:2162
def plotting.PlotGroup.clear (   self)

Definition at line 2159 of file plotting.py.

References plotting.PlotGroup._plots.

2159  def clear(self):
2160  self._plots = []
2161 
def clear(self)
Definition: plotting.py:2159
def plotting.PlotGroup.create (   self,
  tdirectoryNEvents,
  requireAllHistograms = False 
)
Create histograms from a list of TDirectories.

Arguments:
tdirectoryNEvents    -- List of (TDirectory, nevents) pairs
requireAllHistograms -- If True, a plot is produced if histograms from all files are present (default: False)

Definition at line 2175 of file plotting.py.

References plotting.PlotGroup._plots.

2175  def create(self, tdirectoryNEvents, requireAllHistograms=False):
2176  """Create histograms from a list of TDirectories.
2177 
2178  Arguments:
2179  tdirectoryNEvents -- List of (TDirectory, nevents) pairs
2180  requireAllHistograms -- If True, a plot is produced if histograms from all files are present (default: False)
2181  """
2182  for plot in self._plots:
2183  plot.create(tdirectoryNEvents, requireAllHistograms)
2184 
def create(self, tdirectoryNEvents, requireAllHistograms=False)
Definition: plotting.py:2175
def plotting.PlotGroup.draw (   self,
  legendLabels,
  prefix = None,
  separate = False,
  saveFormat = ".pdf",
  ratio = True 
)
Draw the histograms using values for a given algorithm.

Arguments:
legendLabels  -- List of strings for legend labels (corresponding to the tdirectories in create())
prefix        -- Optional string for file name prefix (default None)
separate      -- Save the plots of a group to separate files instead of a file per group (default False)
saveFormat   -- String specifying the plot format (default '.pdf')
ratio        -- Add ratio to the plot (default True)

Definition at line 2185 of file plotting.py.

References plotting._createCanvas(), plotting.PlotGroup._createLegend(), plotting.PlotGroup._drawSeparate(), plotting.PlotGroup._modifyPadForRatio(), FP420HitsObject._name, TrackerHitsObject._name, PGeometricDet::Item._name, TrackingRecHitAlgorithm._name, LikelihoodSpecies._name, L1TMuon::PtAssignmentUnit._name, L1TMuon::PtRefinementUnit._name, LikelihoodPdfProduct._name, LikelihoodPdf._name, citk::IsolationConeDefinitionBase._name, Logger._name, hcaldqm::DQModule._name, DrellYanValidation._name, WValidation._name, HistoParams< T >._name, hcaldqm::flag::Flag._name, hcaldqm::quantity::Quantity._name, CutApplicatorBase._name, ElectronMVAEstimatorRun2Phys14NonTrig._name, PhotonMVAEstimatorRun2Spring15NonTrig._name, PhotonMVAEstimatorRun2Phys14NonTrig._name, GeometricDetExtra._name, ElectronMVAEstimatorRun2Spring15Trig._name, ElectronMVAEstimatorRun2Spring15NonTrig._name, HistoParams< TH2F >._name, HistoParams< TProfile2D >._name, Vispa.Views.PropertyView.Property._name, SequenceTypes.SequencePlaceholder._name, plotting.Subtract._name, plotting.Transform._name, plotting.FakeDuplicate._name, plotting.CutEfficiency._name, plotting.AggregateBins._name, plotting.AggregateHistos._name, plotting.ROC._name, plotting.Plot._name, plotting.PlotGroup._name, plotting.PlotGroup._plots, plotting.PlotGroup._ratioFactor, plotting.PlotGroup._save(), createfilelist.int, and hpstanc_transforms.max.

2185  def draw(self, legendLabels, prefix=None, separate=False, saveFormat=".pdf", ratio=True):
2186  """Draw the histograms using values for a given algorithm.
2187 
2188  Arguments:
2189  legendLabels -- List of strings for legend labels (corresponding to the tdirectories in create())
2190  prefix -- Optional string for file name prefix (default None)
2191  separate -- Save the plots of a group to separate files instead of a file per group (default False)
2192  saveFormat -- String specifying the plot format (default '.pdf')
2193  ratio -- Add ratio to the plot (default True)
2194  """
2195 
2196  if self._overrideLegendLabels is not None:
2197  legendLabels = self._overrideLegendLabels
2198 
2199  # Do not draw the group if it would be empty
2200  onlyEmptyPlots = True
2201  for plot in self._plots:
2202  if not plot.isEmpty():
2203  onlyEmptyPlots = False
2204  break
2205  if onlyEmptyPlots:
2206  return []
2207 
2208  if separate:
2209  return self._drawSeparate(legendLabels, prefix, saveFormat, ratio)
2210 
2211  cwidth = 500*self._ncols
2212  nrows = int((len(self._plots)+self._ncols-1)/self._ncols) # this should work also for odd n
2213  cheight = 500 * nrows
2214 
2215  if ratio:
2216  cheight = int(cheight*self._ratioFactor)
2217 
2218  canvas = _createCanvas(self._name, cwidth, cheight)
2219 
2220  canvas.Divide(self._ncols, nrows)
2221  if ratio:
2222  for i, plot in enumerate(self._plots):
2223  pad = canvas.cd(i+1)
2224  self._modifyPadForRatio(pad)
2225 
2226  # Draw plots to canvas
2227  for i, plot in enumerate(self._plots):
2228  pad = canvas.cd(i+1)
2229  if not plot.isEmpty():
2230  plot.draw(pad, ratio, self._ratioFactor, nrows)
2231 
2232  # Setup legend
2233  canvas.cd()
2234  if len(self._plots) <= 4:
2235  lx1 = 0.2
2236  lx2 = 0.9
2237  ly1 = 0.48
2238  ly2 = 0.53
2239  else:
2240  lx1 = 0.1
2241  lx2 = 0.9
2242  ly1 = 0.64
2243  ly2 = 0.67
2244  if self._legendDx is not None:
2245  lx1 += self._legendDx
2246  lx2 += self._legendDx
2247  if self._legendDy is not None:
2248  ly1 += self._legendDy
2249  ly2 += self._legendDy
2250  if self._legendDw is not None:
2251  lx2 += self._legendDw
2252  if self._legendDh is not None:
2253  ly1 -= self._legendDh
2254  plot = max(self._plots, key=lambda p: p.getNumberOfHistograms())
2255  denomUnc = sum([p.drawRatioUncertainty() for p in self._plots]) > 0
2256  legend = self._createLegend(plot, legendLabels, lx1, ly1, lx2, ly2,
2257  denomUncertainty=(ratio and denomUnc))
2258 
2259  return self._save(canvas, saveFormat, prefix=prefix)
2260 
def _save(self, canvas, saveFormat, prefix=None, postfix=None, single=False)
Definition: plotting.py:2343
def _modifyPadForRatio(self, pad)
Definition: plotting.py:2323
def draw(self, legendLabels, prefix=None, separate=False, saveFormat=".pdf", ratio=True)
Definition: plotting.py:2185
def _createLegend(self, plot, legendLabels, lx1, ly1, lx2, ly2, textSize=0.016, denomUncertainty=True)
Definition: plotting.py:2327
def _drawSeparate(self, legendLabels, prefix, saveFormat, ratio)
Definition: plotting.py:2261
def _createCanvas(name, width, height)
Definition: plotting.py:105
def plotting.PlotGroup.getName (   self)
def plotting.PlotGroup.getPlot (   self,
  name 
)

Definition at line 2165 of file plotting.py.

References plotting.PlotGroup._plots.

2165  def getPlot(self, name):
2166  for plot in self._plots:
2167  if plot.getName() == name:
2168  return plot
2169  raise Exception("No Plot named '%s'" % name)
2170 
def getPlot(self, name)
Definition: plotting.py:2165
def plotting.PlotGroup.getPlots (   self)

Definition at line 2149 of file plotting.py.

References plotting.PlotGroup._plots.

2149  def getPlots(self):
2150  return self._plots
2151 
def getPlots(self)
Definition: plotting.py:2149
def plotting.PlotGroup.onlyForPileup (   self)
Return True if the PlotGroup is intended only for pileup samples

Definition at line 2171 of file plotting.py.

References plotting.PlotFolder._onlyForPileup.

2171  def onlyForPileup(self):
2172  """Return True if the PlotGroup is intended only for pileup samples"""
2173  return self._onlyForPileup
2174 
def onlyForPileup(self)
Definition: plotting.py:2171
def plotting.PlotGroup.remove (   self,
  name 
)

Definition at line 2152 of file plotting.py.

References FP420HitsObject._name, TrackerHitsObject._name, PGeometricDet::Item._name, TrackingRecHitAlgorithm._name, LikelihoodSpecies._name, L1TMuon::PtAssignmentUnit._name, L1TMuon::PtRefinementUnit._name, LikelihoodPdfProduct._name, LikelihoodPdf._name, citk::IsolationConeDefinitionBase._name, Logger._name, hcaldqm::DQModule._name, DrellYanValidation._name, WValidation._name, HistoParams< T >._name, hcaldqm::flag::Flag._name, hcaldqm::quantity::Quantity._name, CutApplicatorBase._name, ElectronMVAEstimatorRun2Phys14NonTrig._name, PhotonMVAEstimatorRun2Spring15NonTrig._name, PhotonMVAEstimatorRun2Phys14NonTrig._name, ElectronMVAEstimatorRun2Spring15Trig._name, GeometricDetExtra._name, ElectronMVAEstimatorRun2Spring15NonTrig._name, HistoParams< TH2F >._name, HistoParams< TProfile2D >._name, Vispa.Views.PropertyView.Property._name, SequenceTypes.SequencePlaceholder._name, plotting.Subtract._name, plotting.Transform._name, plotting.FakeDuplicate._name, plotting.CutEfficiency._name, plotting.AggregateBins._name, plotting.AggregateHistos._name, plotting.ROC._name, plotting.Plot._name, plotting.PlotGroup._name, and plotting.PlotGroup._plots.

2152  def remove(self, name):
2153  for i, plot in enumerate(self._plots):
2154  if plot.getName() == name:
2155  del self._plots[i]
2156  return
2157  raise Exception("Did not find Plot '%s' from PlotGroup '%s'" % (name, self._name))
2158 
def remove(self, name)
Definition: plotting.py:2152
def plotting.PlotGroup.setProperties (   self,
  kwargs 
)

Definition at line 2140 of file plotting.py.

2140  def setProperties(self, **kwargs):
2141  for name, value in kwargs.iteritems():
2142  if not hasattr(self, "_"+name):
2143  raise Exception("No attribute '%s'" % name)
2144  setattr(self, "_"+name, value)
2145 
def setProperties(self, kwargs)
Definition: plotting.py:2140

Member Data Documentation

plotting.PlotGroup._name
private
plotting.PlotGroup._plots
private
plotting.PlotGroup._ratioFactor
private