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 2242 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 2244 of file plotting.py.

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

Member Function Documentation

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

Definition at line 2471 of file plotting.py.

References GeometryComparisonPlotter._legend.

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

2471  def _createLegend(self, plot, legendLabels, lx1, ly1, lx2, ly2, textSize=0.016, denomUncertainty=True):
2472  if not self._legend:
2473  return None
2474 
2475  l = ROOT.TLegend(lx1, ly1, lx2, ly2)
2476  l.SetTextSize(textSize)
2477  l.SetLineColor(1)
2478  l.SetLineWidth(1)
2479  l.SetLineStyle(1)
2480  l.SetFillColor(0)
2481  l.SetMargin(0.07)
2482 
2483  plot.addToLegend(l, legendLabels, denomUncertainty)
2484  l.Draw()
2485  return l
2486 
def _createLegend(self, plot, legendLabels, lx1, ly1, lx2, ly2, textSize=0.016, denomUncertainty=True)
Definition: plotting.py:2471
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 2405 of file plotting.py.

References plotting._createCanvas(), plotting.PlotGroup._createLegend(), plotting.PlotGroup._modifyPadForRatio(), TrackerHitsObject._name, FP420HitsObject._name, PGeometricDet::Item._name, TrackingRecHitAlgorithm._name, LikelihoodSpecies._name, LikelihoodPdfProduct._name, Logger._name, hcaldqm::DQModule._name, LikelihoodPdf._name, citk::IsolationConeDefinitionBase._name, DrellYanValidation._name, WValidation._name, hcaldqm::flag::Flag._name, hcaldqm::quantity::Quantity._name, HistoParams< T >._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().

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

Definition at line 2467 of file plotting.py.

References plotting.PlotGroup._ratioFactor.

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

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

Definition at line 2487 of file plotting.py.

References TrackerHitsObject._name, FP420HitsObject._name, PGeometricDet::Item._name, LikelihoodSpecies._name, TrackingRecHitAlgorithm._name, LikelihoodPdfProduct._name, Logger._name, hcaldqm::DQModule._name, LikelihoodPdf._name, citk::IsolationConeDefinitionBase._name, DrellYanValidation._name, WValidation._name, hcaldqm::flag::Flag._name, hcaldqm::quantity::Quantity._name, HistoParams< T >._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().

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

Definition at line 2305 of file plotting.py.

Referenced by diclist.diclist.add().

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

Definition at line 2302 of file plotting.py.

References plotting.PlotGroup._plots.

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

References plotting.PlotGroup._plots.

2318  def create(self, tdirectoryNEvents, requireAllHistograms=False):
2319  """Create histograms from a list of TDirectories.
2320 
2321  Arguments:
2322  tdirectoryNEvents -- List of (TDirectory, nevents) pairs
2323  requireAllHistograms -- If True, a plot is produced if histograms from all files are present (default: False)
2324  """
2325  for plot in self._plots:
2326  plot.create(tdirectoryNEvents, requireAllHistograms)
2327 
def create(self, tdirectoryNEvents, requireAllHistograms=False)
Definition: plotting.py:2318
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 2328 of file plotting.py.

References plotting._createCanvas(), plotting.PlotGroup._createLegend(), plotting.PlotGroup._drawSeparate(), plotting.PlotGroup._modifyPadForRatio(), TrackerHitsObject._name, FP420HitsObject._name, PGeometricDet::Item._name, LikelihoodSpecies._name, TrackingRecHitAlgorithm._name, LikelihoodPdfProduct._name, Logger._name, hcaldqm::DQModule._name, LikelihoodPdf._name, citk::IsolationConeDefinitionBase._name, DrellYanValidation._name, WValidation._name, hcaldqm::flag::Flag._name, hcaldqm::quantity::Quantity._name, HistoParams< T >._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.

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

Definition at line 2308 of file plotting.py.

References plotting.PlotGroup._plots.

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

Definition at line 2292 of file plotting.py.

References plotting.PlotGroup._plots.

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

Definition at line 2314 of file plotting.py.

References plotting.PlotFolder._onlyForPileup.

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

Definition at line 2283 of file plotting.py.

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

Member Data Documentation

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