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