CMS 3D CMS Logo

analyzer.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 import os
5 import logging
6 
7 from PhysicsTools.HeppyCore.statistics.counter import Counters
8 from PhysicsTools.HeppyCore.statistics.average import Averages
9 
11  """Base Analyzer class. Used in Looper.
12 
13  Your custom analyzers should inherit from this class
14  """
15 
16  def __init__(self, cfg_ana, cfg_comp, looperName ):
17  """Create an analyzer.
18 
19  Parameters (also stored as attributes for later use):
20  cfg_ana: configuration parameters for this analyzer (e.g. a pt cut)
21  cfg_comp: configuration parameters for the data or MC component (e.g. DYJets)
22  looperName: name of the Looper which runs this analyzer.
23 
24  Attributes:
25  dirName : analyzer directory, where you can write anything you want
26  """
27  self.class_object = cfg_ana.class_object
28  self.instance_label = cfg_ana.instance_label
29  self.name = cfg_ana.name
30  self.verbose = cfg_ana.verbose
31  self.cfg_ana = cfg_ana
32  self.cfg_comp = cfg_comp
33  self.looperName = looperName
34  if hasattr(cfg_ana,"nosubdir") and cfg_ana.nosubdir:
35  self.dirName = self.looperName
36  else:
37  self.dirName = '/'.join( [self.looperName, self.name] )
38  os.mkdir( self.dirName )
39 
40 
41  # this is the main logger corresponding to the looper.
42  # each analyzer could also declare its own logger
43  self.mainLogger = logging.getLogger( looperName )
44  # print self.mainLogger.handlers
45  self.beginLoopCalled = False
46 
47  def beginLoop(self, setup):
48  """Automatically called by Looper, for all analyzers."""
49  self.counters = Counters()
50  self.averages = Averages()
51  self.mainLogger.info( 'beginLoop ' + self.cfg_ana.name )
52  self.beginLoopCalled = True
53 
54  def endLoop(self, setup):
55  """Automatically called by Looper, for all analyzers."""
56  #print self.cfg_ana
57  self.mainLogger.info( '' )
58  self.mainLogger.info( str(self) )
59  self.mainLogger.info( '' )
60 
61  def process(self, event ):
62  """Automatically called by Looper, for all analyzers.
63  each analyzer in the sequence will be passed the same event instance.
64  each analyzer can access, modify, and store event information, of any type."""
65  print self.cfg_ana.name
66 
67 
68  def write(self, setup):
69  """Called by Looper.write, for all analyzers.
70  Just overload it if you have histograms to write."""
71  self.counters.write( self.dirName )
72  self.averages.write( self.dirName )
73 
74  def __str__(self):
75  """A multipurpose printout. Should do the job for most analyzers."""
76  ana = str( self.cfg_ana )
77  count = ''
78  ave = ''
79  if hasattr(self, 'counters') and len( self.counters.counters ) > 0:
80  count = '\n'.join(map(str, self.counters.counters))
81  if hasattr(self, 'averages') and len( self.averages ) > 0:
82  ave = '\n'.join(map(str, self.averages))
83  return '\n'.join( [ana, count, ave] )
def endLoop(self, setup)
Definition: analyzer.py:54
def process(self, event)
Definition: analyzer.py:61
def beginLoop(self, setup)
Definition: analyzer.py:47
static std::string join(char **cmd)
Definition: RemoteFile.cc:18
def __init__(self, cfg_ana, cfg_comp, looperName)
Definition: analyzer.py:16
def write(self, setup)
Definition: analyzer.py:68
def __str__(self)
Definition: analyzer.py:74