File indexing completed on 2024-11-25 02:29:50
0001
0002
0003
0004 import os
0005 import logging
0006
0007 from PhysicsTools.HeppyCore.statistics.counter import Counters
0008 from PhysicsTools.HeppyCore.statistics.average import Averages
0009
0010 class Analyzer(object):
0011 """Base Analyzer class. Used in Looper.
0012
0013 Your custom analyzers should inherit from this class
0014 """
0015
0016 def __init__(self, cfg_ana, cfg_comp, looperName ):
0017 """Create an analyzer.
0018
0019 Parameters (also stored as attributes for later use):
0020 cfg_ana: configuration parameters for this analyzer (e.g. a pt cut)
0021 cfg_comp: configuration parameters for the data or MC component (e.g. DYJets)
0022 looperName: name of the Looper which runs this analyzer.
0023
0024 Attributes:
0025 dirName : analyzer directory, where you can write anything you want
0026 """
0027 self.class_object = cfg_ana.class_object
0028 self.instance_label = cfg_ana.instance_label
0029 self.name = cfg_ana.name
0030 self.verbose = cfg_ana.verbose
0031 self.cfg_ana = cfg_ana
0032 self.cfg_comp = cfg_comp
0033 self.looperName = looperName
0034 if hasattr(cfg_ana,"nosubdir") and cfg_ana.nosubdir:
0035 self.dirName = self.looperName
0036 else:
0037 self.dirName = '/'.join( [self.looperName, self.name] )
0038 os.mkdir( self.dirName )
0039
0040
0041
0042
0043 self.mainLogger = logging.getLogger( looperName )
0044
0045 self.beginLoopCalled = False
0046
0047 def beginLoop(self, setup):
0048 """Automatically called by Looper, for all analyzers."""
0049 self.counters = Counters()
0050 self.averages = Averages()
0051 self.mainLogger.info( 'beginLoop ' + self.cfg_ana.name )
0052 self.beginLoopCalled = True
0053
0054 def endLoop(self, setup):
0055 """Automatically called by Looper, for all analyzers."""
0056
0057 self.mainLogger.info( '' )
0058 self.mainLogger.info( str(self) )
0059 self.mainLogger.info( '' )
0060
0061 def process(self, event ):
0062 """Automatically called by Looper, for all analyzers.
0063 each analyzer in the sequence will be passed the same event instance.
0064 each analyzer can access, modify, and store event information, of any type."""
0065 print(self.cfg_ana.name)
0066
0067
0068 def write(self, setup):
0069 """Called by Looper.write, for all analyzers.
0070 Just overload it if you have histograms to write."""
0071 self.counters.write( self.dirName )
0072 self.averages.write( self.dirName )
0073
0074 def __str__(self):
0075 """A multipurpose printout. Should do the job for most analyzers."""
0076 ana = str( self.cfg_ana )
0077 count = ''
0078 ave = ''
0079 if hasattr(self, 'counters') and len( self.counters.counters ) > 0:
0080 count = '\n'.join(map(str, self.counters.counters))
0081 if hasattr(self, 'averages') and len( self.averages ) > 0:
0082 ave = '\n'.join(map(str, self.averages))
0083 return '\n'.join( [ana, count, ave] )