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 2238 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 2240 of file plotting.py.

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

Member Function Documentation

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

Definition at line 2467 of file plotting.py.

References GeometryComparisonPlotter._legend.

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

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

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

Definition at line 2463 of file plotting.py.

References plotting.PlotGroup._ratioFactor.

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

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

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

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

Definition at line 2301 of file plotting.py.

Referenced by diclist.diclist.add().

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

Definition at line 2298 of file plotting.py.

References plotting.PlotGroup._plots.

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

References plotting.PlotGroup._plots.

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

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

Definition at line 2304 of file plotting.py.

References plotting.PlotGroup._plots.

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

Definition at line 2288 of file plotting.py.

References plotting.PlotGroup._plots.

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

Definition at line 2310 of file plotting.py.

References plotting.PlotFolder._onlyForPileup.

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

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

2291  def remove(self, name):
2292  for i, plot in enumerate(self._plots):
2293  if plot.getName() == name:
2294  del self._plots[i]
2295  return
2296  raise Exception("Did not find Plot '%s' from PlotGroup '%s'" % (name, self._name))
2297 
def remove(self, name)
Definition: plotting.py:2291
def plotting.PlotGroup.setProperties (   self,
  kwargs 
)

Definition at line 2279 of file plotting.py.

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

Member Data Documentation

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