Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:23:27

0001 from PhysicsTools.Heppy.analyzers.core.Analyzer import Analyzer
0002 from PhysicsTools.Heppy.analyzers.core.AutoHandle import AutoHandle
0003 import PhysicsTools.HeppyCore.framework.config as cfg
0004 from DataFormats.FWLite import Handle
0005 from ROOT.gen import WeightsInfo
0006 
0007 class LHEWeightAnalyzer( Analyzer ):
0008     """Read the WeightsInfo objects of the LHE branch and store them
0009        in event.LHE_weights list.
0010 
0011        If the WeightsInfo.id is a string, replace it with an integer.
0012 
0013        So far the only allowed string format is "mg_reweight_X",
0014        which gets stored as str(10000+int(X))
0015 
0016        If w.id is an unknown string or anything but a string or int,
0017        a RuntimeError is raised.
0018     """
0019     def __init__(self, cfg_ana, cfg_comp, looperName ):
0020         super(LHEWeightAnalyzer,self).__init__(cfg_ana,cfg_comp,looperName)
0021 
0022     def declareHandles(self):
0023         super(LHEWeightAnalyzer, self).declareHandles()
0024         self.mchandles['LHEweights'] = AutoHandle('externalLHEProducer',
0025                                                   'LHEEventProduct',
0026                                                   mayFail=True,
0027                                                   fallbackLabel='source',
0028                                                   lazy=False )
0029 
0030     def beginLoop(self, setup):
0031         super(LHEWeightAnalyzer,self).beginLoop(setup)
0032 
0033     def process(self, event):
0034         self.readCollections( event.input )
0035 
0036         # if not MC, nothing to do
0037         if not self.cfg_comp.isMC:
0038             return True
0039 
0040         # Add LHE weight info
0041         event.LHE_weights = []
0042         event.LHE_originalWeight = 1.0
0043         if self.mchandles['LHEweights'].isValid():
0044             event.LHE_originalWeight = self.mchandles['LHEweights'].product().originalXWGTUP()
0045 
0046             for w in self.mchandles['LHEweights'].product().weights():
0047                 # Check if id is string or int and convert to int if it's a string
0048                 try:
0049                     int(w.id)
0050                     event.LHE_weights.append(w)
0051                 except ValueError:
0052                     if not isinstance(w.id, str):
0053                         raise RuntimeError('Non int or string type for LHE weight id')
0054 
0055                     newweight = WeightsInfo()
0056                     newweight.wgt = w.wgt
0057                     if w.id.startswith('mg_reweight'):
0058                         newid = str(10000 + int(w.id.rsplit('_',1)[1]))
0059                         newweight.id = newid
0060 
0061                     else: raise RuntimeError('Unknown string id in LHE weights')
0062                     event.LHE_weights.append(newweight)
0063 
0064         return True
0065 
0066 setattr(LHEWeightAnalyzer,"defaultConfig",
0067     cfg.Analyzer(LHEWeightAnalyzer,
0068     )
0069 )