CMS 3D CMS Logo

List of all members | Public Member Functions | Private Member Functions | Private Attributes
plotting.PlotGroup Class Reference
Inheritance diagram for plotting.PlotGroup:
plotting.PlotOnSideGroup

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, directory="")
 
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, directory)
 
def _modifyPadForRatio (self, pad)
 
def _save (self, canvas, saveFormat, prefix=None, postfix=None, single=False, directory="")
 

Private Attributes

 _name
 
 _plots
 
 _ratioFactor
 

Detailed Description

Group of plots, results a TCanvas

Definition at line 2240 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 2242 of file plotting.py.

2242  def __init__(self, name, plots, **kwargs):
2243  """Constructor.
2244 
2245  Arguments:
2246  name -- String for name of the TCanvas, used also as the basename of the picture files
2247  plots -- List of Plot objects
2248 
2249  Keyword arguments:
2250  ncols -- Number of columns (default 2)
2251  legendDx -- Float for moving TLegend in x direction (default None)
2252  legendDy -- Float for moving TLegend in y direction (default None)
2253  legendDw -- Float for changing TLegend width (default None)
2254  legendDh -- Float for changing TLegend height (default None)
2255  legend -- Bool for disabling legend (default True for legend being enabled)
2256  overrideLegendLabels -- List of strings for legend labels, if given, these are used instead of the ones coming from Plotter (default None)
2257  onlyForPileup -- Plots this group only for pileup samples
2258  """
2259  super(PlotGroup, self).__init__()
2260 
2261  self._name = name
2262  self._plots = plots
2263 
2264  def _set(attr, default):
2265  setattr(self, "_"+attr, kwargs.get(attr, default))
2266 
2267  _set("ncols", 2)
2268 
2269  _set("legendDx", None)
2270  _set("legendDy", None)
2271  _set("legendDw", None)
2272  _set("legendDh", None)
2273  _set("legend", True)
2274 
2275  _set("overrideLegendLabels", None)
2276 
2277  _set("onlyForPileup", False)
2278 
2279  self._ratioFactor = 1.25
2280 
def __init__(self, name, plots, kwargs)
Definition: plotting.py:2242

Member Function Documentation

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

Definition at line 2469 of file plotting.py.

References GeometryComparisonPlotter._legend.

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

2469  def _createLegend(self, plot, legendLabels, lx1, ly1, lx2, ly2, textSize=0.016, denomUncertainty=True):
2470  if not self._legend:
2471  return None
2472 
2473  l = ROOT.TLegend(lx1, ly1, lx2, ly2)
2474  l.SetTextSize(textSize)
2475  l.SetLineColor(1)
2476  l.SetLineWidth(1)
2477  l.SetLineStyle(1)
2478  l.SetFillColor(0)
2479  l.SetMargin(0.07)
2480 
2481  plot.addToLegend(l, legendLabels, denomUncertainty)
2482  l.Draw()
2483  return l
2484 
def _createLegend(self, plot, legendLabels, lx1, ly1, lx2, ly2, textSize=0.016, denomUncertainty=True)
Definition: plotting.py:2469
def plotting.PlotGroup._drawSeparate (   self,
  legendLabels,
  prefix,
  saveFormat,
  ratio,
  directory 
)
private
Internal method to do the drawing to separate files per Plot instead of a file per PlotGroup

Definition at line 2403 of file plotting.py.

References plotting._createCanvas(), plotting.PlotGroup._createLegend(), plotting.PlotGroup._modifyPadForRatio(), FP420HitsObject._name, TrackerHitsObject._name, PGeometricDet::Item._name, TrackingRecHitAlgorithm._name, LikelihoodSpecies._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, GeometricTimingDetExtra._name, CutApplicatorBase._name, GeometricDetExtra._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, SequenceTypes.TaskPlaceholder._name, plotting.Plot._name, plotting.PlotGroup._name, plotting.PlotGroup._plots, plotting.PlotGroup._ratioFactor, plotting.PlotGroup._save(), and createfilelist.int.

Referenced by plotting.PlotGroup.draw().

2403  def _drawSeparate(self, legendLabels, prefix, saveFormat, ratio, directory):
2404  """Internal method to do the drawing to separate files per Plot instead of a file per PlotGroup"""
2405  width = 500
2406  height = 500
2407 
2408  canvas = _createCanvas(self._name+"Single", width, height)
2409  canvasRatio = _createCanvas(self._name+"SingleRatio", width, int(height*self._ratioFactor))
2410 
2411  # from TDRStyle
2412  for c in [canvas, canvasRatio]:
2413  c.SetTopMargin(0.05)
2414  c.SetBottomMargin(0.13)
2415  c.SetLeftMargin(0.16)
2416  c.SetRightMargin(0.05)
2417 
2418  lx1def = 0.6
2419  lx2def = 0.95
2420  ly1def = 0.85
2421  ly2def = 0.95
2422 
2423  ret = []
2424 
2425  for plot in self._plots:
2426  if plot.isEmpty():
2427  continue
2428 
2429  ratioForThisPlot = plot.isRatio(ratio)
2430  c = canvas
2431  if ratioForThisPlot:
2432  c = canvasRatio
2433  c.cd()
2434  self._modifyPadForRatio(c)
2435 
2436  # Draw plot to canvas
2437  c.cd()
2438  plot.draw(c, ratioForThisPlot, self._ratioFactor, 1)
2439 
2440  if plot._legend:
2441  # Setup legend
2442  lx1 = lx1def
2443  lx2 = lx2def
2444  ly1 = ly1def
2445  ly2 = ly2def
2446 
2447  if plot._legendDx is not None:
2448  lx1 += plot._legendDx
2449  lx2 += plot._legendDx
2450  if plot._legendDy is not None:
2451  ly1 += plot._legendDy
2452  ly2 += plot._legendDy
2453  if plot._legendDw is not None:
2454  lx2 += plot._legendDw
2455  if plot._legendDh is not None:
2456  ly1 -= plot._legendDh
2457 
2458  c.cd()
2459  legend = self._createLegend(plot, legendLabels, lx1, ly1, lx2, ly2, textSize=0.03,
2460  denomUncertainty=(ratioForThisPlot and plot.drawRatioUncertainty))
2461 
2462  ret.extend(self._save(c, saveFormat, prefix=prefix, postfix="_"+plot.getName(), single=True, directory=directory))
2463  return ret
2464 
def _modifyPadForRatio(self, pad)
Definition: plotting.py:2465
def _save(self, canvas, saveFormat, prefix=None, postfix=None, single=False, directory="")
Definition: plotting.py:2485
def _createLegend(self, plot, legendLabels, lx1, ly1, lx2, ly2, textSize=0.016, denomUncertainty=True)
Definition: plotting.py:2469
def _drawSeparate(self, legendLabels, prefix, saveFormat, ratio, directory)
Definition: plotting.py:2403
def _createCanvas(name, width, height)
Definition: plotting.py:108
def plotting.PlotGroup._modifyPadForRatio (   self,
  pad 
)
private
Internal method to set divide a pad to two for ratio plots

Definition at line 2465 of file plotting.py.

References plotting.PlotGroup._ratioFactor.

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

2465  def _modifyPadForRatio(self, pad):
2466  """Internal method to set divide a pad to two for ratio plots"""
2467  _modifyPadForRatio(pad, self._ratioFactor)
2468 
def _modifyPadForRatio(self, pad)
Definition: plotting.py:2465
def plotting.PlotGroup._save (   self,
  canvas,
  saveFormat,
  prefix = None,
  postfix = None,
  single = False,
  directory = "" 
)
private

Definition at line 2485 of file plotting.py.

References FP420HitsObject._name, TrackerHitsObject._name, PGeometricDet::Item._name, LikelihoodSpecies._name, TrackingRecHitAlgorithm._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, GeometricTimingDetExtra._name, CutApplicatorBase._name, GeometricDetExtra._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, SequenceTypes.TaskPlaceholder._name, plotting.Plot._name, and plotting.PlotGroup._name.

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

2485  def _save(self, canvas, saveFormat, prefix=None, postfix=None, single=False, directory=""):
2486  # Save the canvas to file and clear
2487  name = self._name
2488  if prefix is not None:
2489  name = prefix+name
2490  if postfix is not None:
2491  name = name+postfix
2492  name = os.path.join(directory, name)
2493 
2494  if not verbose: # silence saved file printout
2495  backup = ROOT.gErrorIgnoreLevel
2496  ROOT.gErrorIgnoreLevel = ROOT.kWarning
2497  canvas.SaveAs(name+saveFormat)
2498  if not verbose:
2499  ROOT.gErrorIgnoreLevel = backup
2500 
2501  if single:
2502  canvas.Clear()
2503  canvas.SetLogx(False)
2504  canvas.SetLogy(False)
2505  else:
2506  canvas.Clear("D") # keep subpads
2507 
2508  return [name+saveFormat]
2509 
def _save(self, canvas, saveFormat, prefix=None, postfix=None, single=False, directory="")
Definition: plotting.py:2485
def plotting.PlotGroup.append (   self,
  plot 
)

Definition at line 2303 of file plotting.py.

Referenced by diclist.diclist.add().

2303  def append(self, plot):
2304  self._plots.append(plot)
2305 
def append(self, plot)
Definition: plotting.py:2303
def plotting.PlotGroup.clear (   self)

Definition at line 2300 of file plotting.py.

References plotting.PlotGroup._plots.

2300  def clear(self):
2301  self._plots = []
2302 
def clear(self)
Definition: plotting.py:2300
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 2316 of file plotting.py.

References plotting.PlotGroup._plots.

2316  def create(self, tdirectoryNEvents, requireAllHistograms=False):
2317  """Create histograms from a list of TDirectories.
2318 
2319  Arguments:
2320  tdirectoryNEvents -- List of (TDirectory, nevents) pairs
2321  requireAllHistograms -- If True, a plot is produced if histograms from all files are present (default: False)
2322  """
2323  for plot in self._plots:
2324  plot.create(tdirectoryNEvents, requireAllHistograms)
2325 
def create(self, tdirectoryNEvents, requireAllHistograms=False)
Definition: plotting.py:2316
def plotting.PlotGroup.draw (   self,
  legendLabels,
  prefix = None,
  separate = False,
  saveFormat = ".pdf",
  ratio = True,
  directory = "" 
)
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)
directory     -- Directory where to save the file (default "")

Definition at line 2326 of file plotting.py.

References plotting._createCanvas(), plotting.PlotGroup._createLegend(), plotting.PlotGroup._drawSeparate(), plotting.PlotGroup._modifyPadForRatio(), FP420HitsObject._name, TrackerHitsObject._name, PGeometricDet::Item._name, LikelihoodSpecies._name, TrackingRecHitAlgorithm._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, GeometricTimingDetExtra._name, CutApplicatorBase._name, GeometricDetExtra._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, SequenceTypes.TaskPlaceholder._name, plotting.Plot._name, plotting.PlotGroup._name, plotting.PlotGroup._plots, plotting.PlotGroup._ratioFactor, plotting.PlotGroup._save(), createfilelist.int, and SiStripPI.max.

2326  def draw(self, legendLabels, prefix=None, separate=False, saveFormat=".pdf", ratio=True, directory=""):
2327  """Draw the histograms using values for a given algorithm.
2328 
2329  Arguments:
2330  legendLabels -- List of strings for legend labels (corresponding to the tdirectories in create())
2331  prefix -- Optional string for file name prefix (default None)
2332  separate -- Save the plots of a group to separate files instead of a file per group (default False)
2333  saveFormat -- String specifying the plot format (default '.pdf')
2334  ratio -- Add ratio to the plot (default True)
2335  directory -- Directory where to save the file (default "")
2336  """
2337 
2338  if self._overrideLegendLabels is not None:
2339  legendLabels = self._overrideLegendLabels
2340 
2341  # Do not draw the group if it would be empty
2342  onlyEmptyPlots = True
2343  for plot in self._plots:
2344  if not plot.isEmpty():
2345  onlyEmptyPlots = False
2346  break
2347  if onlyEmptyPlots:
2348  return []
2349 
2350  if separate:
2351  return self._drawSeparate(legendLabels, prefix, saveFormat, ratio, directory)
2352 
2353  cwidth = 500*self._ncols
2354  nrows = int((len(self._plots)+self._ncols-1)/self._ncols) # this should work also for odd n
2355  cheight = 500 * nrows
2356 
2357  if ratio:
2358  cheight = int(cheight*self._ratioFactor)
2359 
2360  canvas = _createCanvas(self._name, cwidth, cheight)
2361 
2362  canvas.Divide(self._ncols, nrows)
2363  if ratio:
2364  for i, plot in enumerate(self._plots):
2365  pad = canvas.cd(i+1)
2366  self._modifyPadForRatio(pad)
2367 
2368  # Draw plots to canvas
2369  for i, plot in enumerate(self._plots):
2370  pad = canvas.cd(i+1)
2371  if not plot.isEmpty():
2372  plot.draw(pad, ratio, self._ratioFactor, nrows)
2373 
2374  # Setup legend
2375  canvas.cd()
2376  if len(self._plots) <= 4:
2377  lx1 = 0.2
2378  lx2 = 0.9
2379  ly1 = 0.48
2380  ly2 = 0.53
2381  else:
2382  lx1 = 0.1
2383  lx2 = 0.9
2384  ly1 = 0.64
2385  ly2 = 0.67
2386  if self._legendDx is not None:
2387  lx1 += self._legendDx
2388  lx2 += self._legendDx
2389  if self._legendDy is not None:
2390  ly1 += self._legendDy
2391  ly2 += self._legendDy
2392  if self._legendDw is not None:
2393  lx2 += self._legendDw
2394  if self._legendDh is not None:
2395  ly1 -= self._legendDh
2396  plot = max(self._plots, key=lambda p: p.getNumberOfHistograms())
2397  denomUnc = sum([p.drawRatioUncertainty() for p in self._plots]) > 0
2398  legend = self._createLegend(plot, legendLabels, lx1, ly1, lx2, ly2,
2399  denomUncertainty=(ratio and denomUnc))
2400 
2401  return self._save(canvas, saveFormat, prefix=prefix, directory=directory)
2402 
def draw(self, legendLabels, prefix=None, separate=False, saveFormat=".pdf", ratio=True, directory="")
Definition: plotting.py:2326
def _modifyPadForRatio(self, pad)
Definition: plotting.py:2465
def _save(self, canvas, saveFormat, prefix=None, postfix=None, single=False, directory="")
Definition: plotting.py:2485
def _createLegend(self, plot, legendLabels, lx1, ly1, lx2, ly2, textSize=0.016, denomUncertainty=True)
Definition: plotting.py:2469
def _drawSeparate(self, legendLabels, prefix, saveFormat, ratio, directory)
Definition: plotting.py:2403
def _createCanvas(name, width, height)
Definition: plotting.py:108
def plotting.PlotGroup.getName (   self)
def plotting.PlotGroup.getPlot (   self,
  name 
)

Definition at line 2306 of file plotting.py.

References plotting.PlotGroup._plots.

2306  def getPlot(self, name):
2307  for plot in self._plots:
2308  if plot.getName() == name:
2309  return plot
2310  raise Exception("No Plot named '%s'" % name)
2311 
def getPlot(self, name)
Definition: plotting.py:2306
def plotting.PlotGroup.getPlots (   self)

Definition at line 2290 of file plotting.py.

References plotting.PlotGroup._plots.

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

Definition at line 2312 of file plotting.py.

References plotting.PlotFolder._onlyForPileup.

2312  def onlyForPileup(self):
2313  """Return True if the PlotGroup is intended only for pileup samples"""
2314  return self._onlyForPileup
2315 
def onlyForPileup(self)
Definition: plotting.py:2312
def plotting.PlotGroup.remove (   self,
  name 
)
def plotting.PlotGroup.setProperties (   self,
  kwargs 
)

Definition at line 2281 of file plotting.py.

2281  def setProperties(self, **kwargs):
2282  for name, value in six.iteritems(kwargs):
2283  if not hasattr(self, "_"+name):
2284  raise Exception("No attribute '%s'" % name)
2285  setattr(self, "_"+name, value)
2286 
def setProperties(self, kwargs)
Definition: plotting.py:2281

Member Data Documentation

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