Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:32:27

0001 from __future__ import print_function
0002 import itertools
0003 
0004 from PhysicsTools.Heppy.analyzers.core.Analyzer import Analyzer
0005 from PhysicsTools.Heppy.analyzers.core.AutoHandle import AutoHandle
0006 from PhysicsTools.HeppyCore.framework.event import Event
0007 from PhysicsTools.HeppyCore.statistics.counter import Counter, Counters
0008 
0009 from DataFormats.FWLite import Events, Handle,Lumis
0010 
0011 class SkimAnalyzerCount( Analyzer ):
0012     #---------------------------------------------
0013     # TO FINDS THE INITIAL EVENTS BEFORE THE SKIM
0014     #---------------------------------------------
0015     
0016     def __init__(self, cfg_ana, cfg_comp, looperName):
0017         super(SkimAnalyzerCount, self).__init__(cfg_ana, cfg_comp, looperName)
0018         self.useLumiBlocks = self.cfg_ana.useLumiBlocks if (hasattr(self.cfg_ana,'useLumiBlocks')) else False
0019         self.verbose = getattr(self.cfg_ana, 'verbose', False)
0020  
0021     def declareHandles(self):
0022         super(SkimAnalyzerCount, self).declareHandles()
0023         self.counterHandle = Handle("edm::MergeableCounter")
0024         self.mchandles['GenInfo'] = AutoHandle( ('generator','',''), 'GenEventInfoProduct' )
0025         
0026     def beginLoop(self, setup):
0027         super(SkimAnalyzerCount,self).beginLoop(setup)
0028 
0029         self.counters.addCounter('SkimReport')
0030         self.count = self.counters.counter('SkimReport')
0031         self.count.register('All Events')
0032         if self.cfg_comp.isMC: 
0033             self.count.register('Sum Weights')
0034 
0035         if not self.useLumiBlocks:
0036             #print 'Will actually count events instead of accessing lumi blocks'
0037             return True
0038 
0039         print('Counting the total events before the skim by accessing luminosity blocks')
0040         lumis = Lumis(self.cfg_comp.files)
0041         totalEvents=0
0042         
0043         for lumi in lumis:
0044             if lumi.getByLabel('prePathCounter',self.counterHandle):
0045                 totalEvents+=self.counterHandle.product().value
0046             else:
0047                 self.useLumiBlocks = False
0048                 break
0049             
0050        
0051         if self.useLumiBlocks:
0052             self.count.inc('All Events',totalEvents)
0053             if self.cfg_comp.isMC: 
0054                 self.count.inc('Sum Weights',totalEvents)
0055             print('Done -> proceeding with the analysis') 
0056         else:
0057             print('Failed -> will have to actually count events (this can happen if the input dataset is not a CMG one)')
0058 
0059 
0060 
0061     def process(self, event):
0062         if self.verbose:
0063             print("\nProcessing run:lumi:event %d:%d:%d" % (
0064                     event.input.eventAuxiliary().id().run(),
0065                     event.input.eventAuxiliary().id().luminosityBlock(),
0066                     event.input.eventAuxiliary().id().event()))  
0067         if not self.useLumiBlocks:
0068             self.readCollections( event.input )
0069             self.count.inc('All Events')
0070             if self.cfg_comp.isMC: 
0071                 self.count.inc('Sum Weights', self.mchandles['GenInfo'].product().weight())
0072         return True