CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
List of all members | Public Member Functions | Private Member Functions | Private Attributes
plotting.PlotGroup Class Reference

Public Member Functions

def __init__
 
def create
 
def draw
 
def onlyForPileup
 

Private Member Functions

def _createLegend
 
def _drawSeparate
 
def _modifyPadForRatio
 
def _save
 

Private Attributes

 _name
 
 _plots
 
 _ratioFactor
 

Detailed Description

Group of plots, results a TCanvas

Definition at line 1352 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)
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 1354 of file plotting.py.

1355  def __init__(self, name, plots, **kwargs):
1356  """Constructor.
1357 
1358  Arguments:
1359  name -- String for name of the TCanvas, used also as the basename of the picture files
1360  plots -- List of Plot objects
1361 
1362  Keyword arguments:
1363  ncols -- Number of columns (default 2)
1364  legendDx -- Float for moving TLegend in x direction (default None)
1365  legendDy -- Float for moving TLegend in y direction (default None)
1366  legendDw -- Float for changing TLegend width (default None)
1367  legendDh -- Float for changing TLegend height (default None)
1368  overrideLegendLabels -- List of strings for legend labels, if given, these are used instead of the ones coming from Plotter (default None)
1369  onlyForPileup -- Plots this group only for pileup samples
1370  """
1371  self._name = name
1372  self._plots = plots
1373 
1374  def _set(attr, default):
1375  setattr(self, "_"+attr, kwargs.get(attr, default))
1376 
1377  _set("ncols", 2)
1378 
1379  _set("legendDx", None)
1380  _set("legendDy", None)
1381  _set("legendDw", None)
1382  _set("legendDh", None)
1383 
1384  _set("overrideLegendLabels", None)
1385 
1386  _set("onlyForPileup", False)
1388  self._ratioFactor = 1.25

Member Function Documentation

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

Definition at line 1567 of file plotting.py.

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

1568  def _createLegend(self, plot, legendLabels, lx1, ly1, lx2, ly2, textSize=0.016, denomUncertainty=True):
1569  l = ROOT.TLegend(lx1, ly1, lx2, ly2)
1570  l.SetTextSize(textSize)
1571  l.SetLineColor(1)
1572  l.SetLineWidth(1)
1573  l.SetLineStyle(1)
1574  l.SetFillColor(0)
1575  l.SetMargin(0.07)
1576 
1577  plot.addToLegend(l, legendLabels, denomUncertainty)
1578  l.Draw()
1579  return l
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 1479 of file plotting.py.

References plotting.PlotGroup._createLegend(), plotting.PlotGroup._modifyPadForRatio(), TrackerHitsObject._name, FP420HitsObject._name, PGeometricDet::Item._name, LikelihoodSpecies._name, LikelihoodPdfProduct._name, LikelihoodPdf._name, citk::IsolationConeDefinitionBase._name, DrellYanValidation._name, WValidation._name, HistoParams< T >._name, CutApplicatorBase._name, ElectronMVAEstimatorRun2Phys14NonTrig._name, PhotonMVAEstimatorRun2Spring15NonTrig._name, PhotonMVAEstimatorRun2Phys14NonTrig._name, GeometricDetExtra._name, ElectronMVAEstimatorRun2Spring15Trig._name, ElectronMVAEstimatorRun2Spring15NonTrig._name, HistoParams< TH2F >._name, plotting.Subtract._name, HistoParams< TProfile2D >._name, plotting.FakeDuplicate._name, plotting.AggregateBins._name, plotting.AggregateHistos._name, plotting.ROC._name, Vispa.Views.PropertyView.Property._name, SequenceTypes.SequencePlaceholder._name, plotting.Plot._name, plotting.PlotGroup._name, plotting.PlotGroup._plots, plotting.PlotGroup._ratioFactor, and plotting.PlotGroup._save().

Referenced by plotting.PlotGroup.draw().

1480  def _drawSeparate(self, legendLabels, prefix, saveFormat, ratio):
1481  """Internal method to do the drawing to separate files per Plot instead of a file per PlotGroup"""
1482  width = 500
1483  height = 500
1484  if ratio:
1485  height = int(height*self._ratioFactor)
1486 
1487  canvas = ROOT.TCanvas(self._name+"Single", self._name, width, height)
1488  # from TDRStyle
1489  canvas.SetTopMargin(0.05)
1490  canvas.SetBottomMargin(0.13)
1491  canvas.SetLeftMargin(0.16)
1492  canvas.SetRightMargin(0.05)
1493 
1494  lx1def = 0.6
1495  lx2def = 0.95
1496  ly1def = 0.85
1497  ly2def = 0.95
1498 
1499  ret = []
1500 
1501  for plot in self._plots:
1502  if plot.isEmpty():
1503  continue
1504 
1505  if ratio:
1506  canvas.cd()
1507  self._modifyPadForRatio(canvas)
1508 
1509  # Draw plot to canvas
1510  canvas.cd()
1511  plot.draw(canvas, ratio, self._ratioFactor, 1)
1512 
1513 
1514  # Setup legend
1515  lx1 = lx1def
1516  lx2 = lx2def
1517  ly1 = ly1def
1518  ly2 = ly2def
1519 
1520  if plot._legendDx is not None:
1521  lx1 += plot._legendDx
1522  lx2 += plot._legendDx
1523  if plot._legendDy is not None:
1524  ly1 += plot._legendDy
1525  ly2 += plot._legendDy
1526  if plot._legendDw is not None:
1527  lx2 += plot._legendDw
1528  if plot._legendDh is not None:
1529  ly1 -= plot._legendDh
1530 
1531  canvas.cd()
1532  legend = self._createLegend(plot, legendLabels, lx1, ly1, lx2, ly2, textSize=0.03,
1533  denomUncertainty=(ratio and plot.drawRatioUncertainty))
1534 
1535  ret.extend(self._save(canvas, saveFormat, prefix=prefix, postfix="_"+plot.getName(), single=True))
1536  return ret
def plotting.PlotGroup._modifyPadForRatio (   self,
  pad 
)
private
Internal method to set divide a pad to two for ratio plots

Definition at line 1537 of file plotting.py.

References plotting.PlotGroup._ratioFactor.

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

1538  def _modifyPadForRatio(self, pad):
1539  """Internal method to set divide a pad to two for ratio plots"""
1540  pad.Divide(1, 2)
1541 
1542  divisionPoint = 1-1/self._ratioFactor
1543 
1544  topMargin = pad.GetTopMargin()
1545  bottomMargin = pad.GetBottomMargin()
1546  divisionPoint += (1-divisionPoint)*bottomMargin # correct for (almost-)zeroing bottom margin of pad1
1547  divisionPointForPad1 = 1-( (1-divisionPoint) / (1-0.02) ) # then correct for the non-zero bottom margin, but for pad1 only
1548 
1549  # Set the lower point of the upper pad to divisionPoint
1550  pad1 = pad.cd(1)
1551  yup = 1.0
1552  ylow = divisionPointForPad1
1553  xup = 1.0
1554  xlow = 0.0
1555  pad1.SetPad(xlow, ylow, xup, yup)
1556  pad1.SetFillStyle(4000) # transparent
1557  pad1.SetBottomMargin(0.02) # need some bottom margin here for eps/pdf output (at least in ROOT 5.34)
1558 
1559  # Set the upper point of the lower pad to divisionPoint
1560  pad2 = pad.cd(2)
1561  yup = divisionPoint
1562  ylow = 0.0
1563  pad2.SetPad(xlow, ylow, xup, yup)
1564  pad2.SetFillStyle(4000) # transparent
1565  pad2.SetTopMargin(0.0)
1566  pad2.SetBottomMargin(bottomMargin/(self._ratioFactor*divisionPoint))
def plotting.PlotGroup._save (   self,
  canvas,
  saveFormat,
  prefix = None,
  postfix = None,
  single = False 
)
private

Definition at line 1580 of file plotting.py.

References FP420HitsObject._name, TrackerHitsObject._name, PGeometricDet::Item._name, LikelihoodSpecies._name, LikelihoodPdfProduct._name, LikelihoodPdf._name, citk::IsolationConeDefinitionBase._name, DrellYanValidation._name, WValidation._name, HistoParams< T >._name, CutApplicatorBase._name, ElectronMVAEstimatorRun2Phys14NonTrig._name, PhotonMVAEstimatorRun2Spring15NonTrig._name, PhotonMVAEstimatorRun2Phys14NonTrig._name, GeometricDetExtra._name, ElectronMVAEstimatorRun2Spring15Trig._name, ElectronMVAEstimatorRun2Spring15NonTrig._name, HistoParams< TH2F >._name, plotting.Subtract._name, HistoParams< TProfile2D >._name, plotting.FakeDuplicate._name, plotting.AggregateBins._name, plotting.AggregateHistos._name, plotting.ROC._name, Vispa.Views.PropertyView.Property._name, SequenceTypes.SequencePlaceholder._name, plotting.Plot._name, and plotting.PlotGroup._name.

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

1581  def _save(self, canvas, saveFormat, prefix=None, postfix=None, single=False):
1582  # Save the canvas to file and clear
1583  name = self._name
1584  if prefix is not None:
1585  name = prefix+name
1586  if postfix is not None:
1587  name = name+postfix
1588  canvas.SaveAs(name+saveFormat)
1589  if single:
1590  canvas.Clear()
1591  canvas.SetLogx(False)
1592  canvas.SetLogy(False)
1593  else:
1594  canvas.Clear("D") # keep subpads
1595 
1596  return [name+saveFormat]
def plotting.PlotGroup.create (   self,
  tdirectories,
  requireAllHistograms = False 
)
Create histograms from a list of TDirectories.

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

Definition at line 1393 of file plotting.py.

References plotting.PlotGroup._plots.

1394  def create(self, tdirectories, requireAllHistograms=False):
1395  """Create histograms from a list of TDirectories.
1396 
1397  Arguments:
1398  tdirectories -- List of TDirectory objects
1399  requireAllHistograms -- If True, a plot is produced if histograms from all files are present (default: False)
1400  """
1401  for plot in self._plots:
1402  plot.create(tdirectories, requireAllHistograms)
def plotting.PlotGroup.draw (   self,
  legendLabels,
  prefix = None,
  separate = False,
  saveFormat = ".pdf",
  ratio = False 
)
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 False)

Definition at line 1403 of file plotting.py.

References plotting.PlotGroup._createLegend(), plotting.PlotGroup._drawSeparate(), plotting.PlotGroup._modifyPadForRatio(), FP420HitsObject._name, TrackerHitsObject._name, PGeometricDet::Item._name, LikelihoodSpecies._name, LikelihoodPdfProduct._name, LikelihoodPdf._name, citk::IsolationConeDefinitionBase._name, DrellYanValidation._name, WValidation._name, HistoParams< T >._name, CutApplicatorBase._name, ElectronMVAEstimatorRun2Phys14NonTrig._name, PhotonMVAEstimatorRun2Spring15NonTrig._name, PhotonMVAEstimatorRun2Phys14NonTrig._name, ElectronMVAEstimatorRun2Spring15Trig._name, GeometricDetExtra._name, ElectronMVAEstimatorRun2Spring15NonTrig._name, HistoParams< TH2F >._name, plotting.Subtract._name, HistoParams< TProfile2D >._name, plotting.FakeDuplicate._name, plotting.AggregateBins._name, plotting.AggregateHistos._name, plotting.ROC._name, Vispa.Views.PropertyView.Property._name, SequenceTypes.SequencePlaceholder._name, plotting.Plot._name, plotting.PlotGroup._name, plotting.PlotGroup._plots, plotting.PlotGroup._ratioFactor, plotting.PlotGroup._save(), and bookConverter.max.

1404  def draw(self, legendLabels, prefix=None, separate=False, saveFormat=".pdf", ratio=False):
1405  """Draw the histograms using values for a given algorithm.
1406 
1407  Arguments:
1408  legendLabels -- List of strings for legend labels (corresponding to the tdirectories in create())
1409  prefix -- Optional string for file name prefix (default None)
1410  separate -- Save the plots of a group to separate files instead of a file per group (default False)
1411  saveFormat -- String specifying the plot format (default '.pdf')
1412  ratio -- Add ratio to the plot (default False)
1413  """
1414 
1415  if self._overrideLegendLabels is not None:
1416  legendLabels = self._overrideLegendLabels
1417 
1418  # Do not draw the group if it would be empty
1419  onlyEmptyPlots = True
1420  for plot in self._plots:
1421  if not plot.isEmpty():
1422  onlyEmptyPlots = False
1423  break
1424  if onlyEmptyPlots:
1425  return []
1426 
1427  if separate:
1428  return self._drawSeparate(legendLabels, prefix, saveFormat, ratio)
1429 
1430  cwidth = 500*self._ncols
1431  nrows = int((len(self._plots)+1)/self._ncols) # this should work also for odd n
1432  cheight = 500 * nrows
1433 
1434  if ratio:
1435  cheight = int(cheight*self._ratioFactor)
1436 
1437  canvas = ROOT.TCanvas(self._name, self._name, cwidth, cheight)
1438 
1439  canvas.Divide(self._ncols, nrows)
1440  if ratio:
1441  for i in xrange(0, len(self._plots)):
1442  pad = canvas.cd(i+1)
1443  self._modifyPadForRatio(pad)
1444 
1445  # Draw plots to canvas
1446  for i, plot in enumerate(self._plots):
1447  pad = canvas.cd(i+1)
1448  if not plot.isEmpty():
1449  plot.draw(pad, ratio, self._ratioFactor, nrows)
1450 
1451  # Setup legend
1452  canvas.cd()
1453  if len(self._plots) <= 4:
1454  lx1 = 0.2
1455  lx2 = 0.9
1456  ly1 = 0.48
1457  ly2 = 0.53
1458  else:
1459  lx1 = 0.1
1460  lx2 = 0.9
1461  ly1 = 0.64
1462  ly2 = 0.67
1463  if self._legendDx is not None:
1464  lx1 += self._legendDx
1465  lx2 += self._legendDx
1466  if self._legendDy is not None:
1467  ly1 += self._legendDy
1468  ly2 += self._legendDy
1469  if self._legendDw is not None:
1470  lx2 += self._legendDw
1471  if self._legendDh is not None:
1472  ly1 -= self._legendDh
1473  plot = max(self._plots, key=lambda p: p.getNumberOfHistograms())
1474  denomUnc = sum([p.drawRatioUncertainty() for p in self._plots]) > 0
1475  legend = self._createLegend(plot, legendLabels, lx1, ly1, lx2, ly2,
1476  denomUncertainty=(ratio and denomUnc))
1477 
1478  return self._save(canvas, saveFormat, prefix=prefix)
def plotting.PlotGroup.onlyForPileup (   self)
Return True if the PlotGroup is intended only for pileup samples

Definition at line 1389 of file plotting.py.

References plotting.PlotFolder._onlyForPileup.

1390  def onlyForPileup(self):
1391  """Return True if the PlotGroup is intended only for pileup samples"""
1392  return self._onlyForPileup

Member Data Documentation

plotting.PlotGroup._name
private

Definition at line 1370 of file plotting.py.

Referenced by plotting.PlotGroup._drawSeparate(), plotting.PlotGroup._save(), plotting.PlotGroup.draw(), plotting.PlotterFolder.getName(), plotting.PlotterFolder.getSelectionNameIterator(), validation.SimpleSample.name(), and plotting.PlotterItem.readDirs().

plotting.PlotGroup._plots
private

Definition at line 1371 of file plotting.py.

Referenced by plotting.PlotGroup._drawSeparate(), plotting.PlotGroup.create(), plotting.PlotGroup.draw(), and plotting.Plotter.readDirs().

plotting.PlotGroup._ratioFactor
private

Definition at line 1387 of file plotting.py.

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