CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
SkimAnalyzerCount.py
Go to the documentation of this file.
1 import itertools
2 
3 from PhysicsTools.Heppy.analyzers.core.Analyzer import Analyzer
4 from PhysicsTools.Heppy.analyzers.core.AutoHandle import AutoHandle
5 from PhysicsTools.HeppyCore.framework.event import Event
6 from PhysicsTools.HeppyCore.statistics.counter import Counter, Counters
7 
8 from DataFormats.FWLite import Events, Handle,Lumis
9 
10 class SkimAnalyzerCount( Analyzer ):
11  #---------------------------------------------
12  # TO FINDS THE INITIAL EVENTS BEFORE THE SKIM
13  #---------------------------------------------
14 
15  def __init__(self, cfg_ana, cfg_comp, looperName):
16  super(SkimAnalyzerCount, self).__init__(cfg_ana, cfg_comp, looperName)
17  self.useLumiBlocks = self.cfg_ana.useLumiBlocks if (hasattr(self.cfg_ana,'useLumiBlocks')) else False
18 
19  def declareHandles(self):
20  super(SkimAnalyzerCount, self).declareHandles()
21  self.counterHandle = Handle("edm::MergeableCounter")
22  self.mchandles['GenInfo'] = AutoHandle( ('generator','',''), 'GenEventInfoProduct' )
23 
24  def beginLoop(self, setup):
25  super(SkimAnalyzerCount,self).beginLoop(setup)
26 
27  self.counters.addCounter('SkimReport')
28  self.count = self.counters.counter('SkimReport')
29  self.count.register('All Events')
30  if self.cfg_comp.isMC:
31  self.count.register('Sum Weights')
32 
33  if not self.useLumiBlocks:
34  #print 'Will actually count events instead of accessing lumi blocks'
35  return True
36 
37  print 'Counting the total events before the skim by accessing luminosity blocks'
38  lumis = Lumis(self.cfg_comp.files)
39  totalEvents=0
40 
41  for lumi in lumis:
42  if lumi.getByLabel('prePathCounter',self.counterHandle):
43  totalEvents+=self.counterHandle.product().value
44  else:
45  self.useLumiBlocks = False
46  break
47 
48 
49  if self.useLumiBlocks:
50  self.count.inc('All Events',totalEvents)
51  if self.cfg_comp.isMC:
52  self.count.inc('Sum Weights',totalEvents)
53  print 'Done -> proceeding with the analysis'
54  else:
55  print 'Failed -> will have to actually count events (this can happen if the input dataset is not a CMG one)'
56 
57 
58 
59  def process(self, event):
60  if not self.useLumiBlocks:
61  self.readCollections( event.input )
62  self.count.inc('All Events')
63  if self.cfg_comp.isMC:
64  self.count.inc('Sum Weights', self.mchandles['GenInfo'].product().weight())
65  return True
Definition: weight.py:1