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 2239 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 2241 of file plotting.py.

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

Member Function Documentation

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

Definition at line 2468 of file plotting.py.

References GeometryComparisonPlotter._legend.

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

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

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

References plotting.PlotGroup._ratioFactor.

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

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

Definition at line 2484 of file plotting.py.

References 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, ElectronMVAEstimatorRun2Phys14NonTrig._name, CutApplicatorBase._name, PhotonMVAEstimatorRun2Spring15NonTrig._name, PhotonMVAEstimatorRun2Phys14NonTrig._name, ElectronMVAEstimatorRun2Spring15Trig._name, ElectronMVAEstimatorRun2Spring15NonTrig._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().

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

Definition at line 2302 of file plotting.py.

Referenced by diclist.diclist.add().

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

Definition at line 2299 of file plotting.py.

References plotting.PlotGroup._plots.

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

References plotting.PlotGroup._plots.

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

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

Definition at line 2305 of file plotting.py.

References plotting.PlotGroup._plots.

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

Definition at line 2289 of file plotting.py.

References plotting.PlotGroup._plots.

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

Definition at line 2311 of file plotting.py.

References plotting.PlotFolder._onlyForPileup.

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

Definition at line 2292 of file plotting.py.

References 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, ElectronMVAEstimatorRun2Phys14NonTrig._name, CutApplicatorBase._name, PhotonMVAEstimatorRun2Spring15NonTrig._name, PhotonMVAEstimatorRun2Phys14NonTrig._name, ElectronMVAEstimatorRun2Spring15Trig._name, ElectronMVAEstimatorRun2Spring15NonTrig._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, and plotting.PlotGroup._plots.

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

Definition at line 2280 of file plotting.py.

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

Member Data Documentation

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