File indexing completed on 2024-04-06 12:23:26
0001 import json
0002 import os
0003 from PhysicsTools.Heppy.analyzers.core.Analyzer import Analyzer
0004
0005 from FWCore.PythonUtilities.LumiList import LumiList
0006 from PhysicsTools.Heppy.utils.rltinfo import RLTInfo
0007
0008 from DataFormats.FWLite import Lumis
0009
0010 class JSONAnalyzer( Analyzer ):
0011 '''Apply a json filter, and creates an RLTInfo TTree.
0012 See PhysicsTools.HeppyCore.utils.RLTInfo for more information
0013
0014 example:
0015
0016 jsonFilter = cfg.Analyzer(
0017 "JSONAnalyzer",
0018 )
0019
0020 The path of the json file to be used is set as a component attribute.
0021
0022 The process function returns:
0023 - True if
0024 - the component is MC or
0025 - if the run/lumi pair is in the JSON file
0026 - if the json file was not set for this component
0027 - False if the component is MC or embed (for H->tau tau),
0028 and if the run/lumi pair is not in the JSON file.
0029 '''
0030
0031 def __init__(self, cfg_ana, cfg_comp, looperName):
0032 super(JSONAnalyzer, self).__init__(cfg_ana, cfg_comp, looperName)
0033 if not cfg_comp.isMC:
0034 if self.cfg_comp.json is None:
0035 raise ValueError('component {cname} is not MC, and contains no JSON file. Either remove the JSONAnalyzer for your path or set the "json" attribute of this component'.format(cname=cfg_comp.name))
0036 self.lumiList = LumiList(os.path.expandvars(self.cfg_comp.json))
0037 else:
0038 self.lumiList = None
0039
0040 self.useLumiBlocks = self.cfg_ana.useLumiBlocks if (hasattr(self.cfg_ana,'useLumiBlocks')) else False
0041
0042 self.rltInfo = RLTInfo()
0043
0044 def beginLoop(self, setup):
0045 super(JSONAnalyzer,self).beginLoop(setup)
0046 self.counters.addCounter('JSON')
0047 self.count = self.counters.counter('JSON')
0048 self.count.register('All Events')
0049 self.count.register('Passed Events')
0050
0051 if self.useLumiBlocks and not self.cfg_comp.isMC and not self.lumiList is None:
0052 lumis = Lumis(self.cfg_comp.files)
0053 for lumi in lumis:
0054 lumiid = lumi.luminosityBlockAuxiliary().id()
0055 run, lumi = lumiid.run(), lumiid.luminosityBlock()
0056 if self.lumiList.contains(run,lumi):
0057 self.rltInfo.add('dummy', run, lumi)
0058
0059
0060 def process(self, event):
0061 self.readCollections( event.input )
0062 evid = event.input.eventAuxiliary().id()
0063 run = evid.run()
0064 lumi = evid.luminosityBlock()
0065 eventId = evid.event()
0066
0067 event.run = run
0068 event.lumi = lumi
0069 event.eventId = eventId
0070
0071 if self.cfg_comp.isMC:
0072 return True
0073
0074 if self.lumiList is None:
0075 return True
0076
0077 self.count.inc('All Events')
0078 if self.lumiList.contains(run,lumi):
0079 self.count.inc('Passed Events')
0080 if not self.useLumiBlocks:
0081 self.rltInfo.add('dummy', run, lumi)
0082 return True
0083 else:
0084 return False
0085
0086
0087 def write(self, setup):
0088 super(JSONAnalyzer, self).write(setup)
0089 self.rltInfo.write( self.dirName )
0090