CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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  self.beginLoopCalled = False
45 
46  def beginLoop(self, setup):
47  """Automatically called by Looper, for all analyzers."""
48  self.counters = Counters()
49  self.averages = Averages()
50  self.mainLogger.warning( 'beginLoop ' + self.cfg_ana.name )
51  self.beginLoopCalled = True
52 
53  def endLoop(self, setup):
54  """Automatically called by Looper, for all analyzers."""
55  #print self.cfg_ana
56  self.mainLogger.warning( '' )
57  self.mainLogger.warning( str(self) )
58  self.mainLogger.warning( '' )
59 
60  def process(self, event ):
61  """Automatically called by Looper, for all analyzers.
62  each analyzer in the sequence will be passed the same event instance.
63  each analyzer can access, modify, and store event information, of any type."""
64  print self.cfg_ana.name
65 
66 
67  def write(self, setup):
68  """Called by Looper.write, for all analyzers.
69  Just overload it if you have histograms to write."""
70  self.counters.write( self.dirName )
71  self.averages.write( self.dirName )
72 
73  def __str__(self):
74  """A multipurpose printout. Should do the job for most analyzers."""
75  ana = str( self.cfg_ana )
76  count = ''
77  ave = ''
78  if hasattr(self, 'counters') and len( self.counters.counters ) > 0:
79  count = '\n'.join(map(str, self.counters.counters))
80  if hasattr(self, 'averages') and len( self.averages ) > 0:
81  ave = '\n'.join(map(str, self.averages))
82  return '\n'.join( [ana, count, ave] )
static std::string join(char **cmd)
Definition: RemoteFile.cc:18
list object
Definition: dbtoconf.py:77