Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:23:30

0001 # Copyright (C) 2014 Colin Bernet
0002 # https://github.com/cbernet/heppy/blob/master/LICENSE
0003 
0004 class Histograms(object):
0005     '''Base class to handle writing and formatting of a set of histograms. 
0006 
0007     Subclass it, and simply add your histograms to the subclass.
0008     No need to put them in a list, they will be kept track of automatically
0009     by this base class. 
0010     Then, fill them. Finally, you can call FormatHistos and Write.'''
0011     def __init__(self, name):
0012         self.name = name
0013         self.hists = []
0014         self.named = []
0015         # attributes inheriting from TH1 and TNamed
0016         # are kept track of automagically, even if they are in
0017         # child classes
0018         # setting StatOverflows True for all histograms
0019         for var in vars( self ).values():
0020             try:
0021                 if var.InheritsFrom('TNamed'):
0022                     self.named.append(var)
0023                     if var.InheritsFrom('TH1'):
0024                         var.StatOverflows(True)
0025                         self.hists.append(var)
0026             except:
0027                 pass
0028         # print 'TH1     list:', self.hists
0029         # print 'TNamed  list:', self.named
0030 
0031     def FormatHistos(self, style ):
0032         '''Apply a style to all histograms.'''
0033         for hist in self.hists:
0034             style.FormatHisto( hist )
0035 
0036     def Write(self, dir ):
0037         '''Writes all histograms to a subdirectory of dir called self.name.'''
0038         self.dir = dir.mkdir( self.name )
0039         self.dir.cd()
0040         for hist in self.hists:
0041             hist.Write()
0042         dir.cd()
0043 
0044