CMS 3D CMS Logo

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