Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-11-25 02:29:49

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