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 2093 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 2095 of file plotting.py.

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

Member Function Documentation

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

Definition at line 2319 of file plotting.py.

References GeometryComparisonPlotter._legend.

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

2319  def _createLegend(self, plot, legendLabels, lx1, ly1, lx2, ly2, textSize=0.016, denomUncertainty=True):
2320  if not self._legend:
2321  return None
2322 
2323  l = ROOT.TLegend(lx1, ly1, lx2, ly2)
2324  l.SetTextSize(textSize)
2325  l.SetLineColor(1)
2326  l.SetLineWidth(1)
2327  l.SetLineStyle(1)
2328  l.SetFillColor(0)
2329  l.SetMargin(0.07)
2330 
2331  plot.addToLegend(l, legendLabels, denomUncertainty)
2332  l.Draw()
2333  return l
2334 
def _createLegend(self, plot, legendLabels, lx1, ly1, lx2, ly2, textSize=0.016, denomUncertainty=True)
Definition: plotting.py:2319
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 2253 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().

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

References plotting.PlotGroup._ratioFactor.

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

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

Definition at line 2335 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().

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

Definition at line 2154 of file plotting.py.

Referenced by diclist.diclist.add().

2154  def append(self, plot):
2155  self._plots.append(plot)
2156 
def append(self, plot)
Definition: plotting.py:2154
def plotting.PlotGroup.clear (   self)

Definition at line 2151 of file plotting.py.

References plotting.PlotGroup._plots.

2151  def clear(self):
2152  self._plots = []
2153 
def clear(self)
Definition: plotting.py:2151
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 2167 of file plotting.py.

References plotting.PlotGroup._plots.

2167  def create(self, tdirectoryNEvents, requireAllHistograms=False):
2168  """Create histograms from a list of TDirectories.
2169 
2170  Arguments:
2171  tdirectoryNEvents -- List of (TDirectory, nevents) pairs
2172  requireAllHistograms -- If True, a plot is produced if histograms from all files are present (default: False)
2173  """
2174  for plot in self._plots:
2175  plot.create(tdirectoryNEvents, requireAllHistograms)
2176 
def create(self, tdirectoryNEvents, requireAllHistograms=False)
Definition: plotting.py:2167
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 2177 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.

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

Definition at line 2157 of file plotting.py.

References plotting.PlotGroup._plots.

2157  def getPlot(self, name):
2158  for plot in self._plots:
2159  if plot.getName() == name:
2160  return plot
2161  raise Exception("No Plot named '%s'" % name)
2162 
def getPlot(self, name)
Definition: plotting.py:2157
def plotting.PlotGroup.getPlots (   self)

Definition at line 2141 of file plotting.py.

References plotting.PlotGroup._plots.

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

Definition at line 2163 of file plotting.py.

References plotting.PlotFolder._onlyForPileup.

2163  def onlyForPileup(self):
2164  """Return True if the PlotGroup is intended only for pileup samples"""
2165  return self._onlyForPileup
2166 
def onlyForPileup(self)
Definition: plotting.py:2163
def plotting.PlotGroup.remove (   self,
  name 
)

Definition at line 2144 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.

2144  def remove(self, name):
2145  for i, plot in enumerate(self._plots):
2146  if plot.getName() == name:
2147  del self._plots[i]
2148  return
2149  raise Exception("Did not find Plot '%s' from PlotGroup '%s'" % (name, self._name))
2150 
def remove(self, name)
Definition: plotting.py:2144
def plotting.PlotGroup.setProperties (   self,
  kwargs 
)

Definition at line 2132 of file plotting.py.

2132  def setProperties(self, **kwargs):
2133  for name, value in kwargs.iteritems():
2134  if not hasattr(self, "_"+name):
2135  raise Exception("No attribute '%s'" % name)
2136  setattr(self, "_"+name, value)
2137 
def setProperties(self, kwargs)
Definition: plotting.py:2132

Member Data Documentation

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