CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
histograms.py
Go to the documentation of this file.
1 # Copyright (C) 2014 Colin Bernet
2 # https://github.com/cbernet/heppy/blob/master/LICENSE
3 
4 class Histograms(object):
5  '''Base class to handle writing and formatting of a set of histograms.
6 
7  Subclass it, and simply add your histograms to the subclass.
8  No need to put them in a list, they will be kept track of automatically
9  by this base class.
10  Then, fill them. Finally, you can call FormatHistos and Write.'''
11  def __init__(self, name):
12  self.name = name
13  self.hists = []
14  self.named = []
15  # attributes inheriting from TH1 and TNamed
16  # are kept track of automagically, even if they are in
17  # child classes
18  # setting StatOverflows True for all histograms
19  for var in vars( self ).values():
20  try:
21  if var.InheritsFrom('TNamed'):
22  self.named.append(var)
23  if var.InheritsFrom('TH1'):
24  var.StatOverflows(True)
25  self.hists.append(var)
26  except:
27  pass
28  # print 'TH1 list:', self.hists
29  # print 'TNamed list:', self.named
30 
31  def FormatHistos(self, style ):
32  '''Apply a style to all histograms.'''
33  for hist in self.hists:
34  style.FormatHisto( hist )
35 
36  def Write(self, dir ):
37  '''Writes all histograms to a subdirectory of dir called self.name.'''
38  self.dir = dir.mkdir( self.name )
39  self.dir.cd()
40  for hist in self.hists:
41  hist.Write()
42  dir.cd()
43 
44