CMS 3D CMS Logo

LHEWeightAnalyzer.py
Go to the documentation of this file.
1 from PhysicsTools.Heppy.analyzers.core.Analyzer import Analyzer
2 from PhysicsTools.Heppy.analyzers.core.AutoHandle import AutoHandle
3 import PhysicsTools.HeppyCore.framework.config as cfg
4 from DataFormats.FWLite import Handle
5 from ROOT.gen import WeightsInfo
6 
8  """Read the WeightsInfo objects of the LHE branch and store them
9  in event.LHE_weights list.
10 
11  If the WeightsInfo.id is a string, replace it with an integer.
12 
13  So far the only allowed string format is "mg_reweight_X",
14  which gets stored as str(10000+int(X))
15 
16  If w.id is an unknown string or anything but a string or int,
17  a RuntimeError is raised.
18  """
19  def __init__(self, cfg_ana, cfg_comp, looperName ):
20  super(LHEWeightAnalyzer,self).__init__(cfg_ana,cfg_comp,looperName)
21 
22  def declareHandles(self):
23  super(LHEWeightAnalyzer, self).declareHandles()
24  self.mchandles['LHEweights'] = AutoHandle('externalLHEProducer',
25  'LHEEventProduct',
26  mayFail=True,
27  fallbackLabel='source',
28  lazy=False )
29 
30  def beginLoop(self, setup):
31  super(LHEWeightAnalyzer,self).beginLoop(setup)
32 
33  def process(self, event):
34  self.readCollections( event.input )
35 
36  # if not MC, nothing to do
37  if not self.cfg_comp.isMC:
38  return True
39 
40  # Add LHE weight info
41  event.LHE_weights = []
42  event.LHE_originalWeight = 1.0
43  if self.mchandles['LHEweights'].isValid():
44  event.LHE_originalWeight = self.mchandles['LHEweights'].product().originalXWGTUP()
45 
46  for w in self.mchandles['LHEweights'].product().weights():
47  # Check if id is string or int and convert to int if it's a string
48  try:
49  int(w.id)
50  event.LHE_weights.append(w)
51  except ValueError:
52  if not isinstance(w.id, str):
53  raise RuntimeError('Non int or string type for LHE weight id')
54 
55  newweight = WeightsInfo()
56  newweight.wgt = w.wgt
57  if w.id.startswith('mg_reweight'):
58  newid = str(10000 + int(w.id.rsplit('_',1)[1]))
59  newweight.id = newid
60 
61  else: raise RuntimeError('Unknown string id in LHE weights')
62  event.LHE_weights.append(newweight)
63 
64  return True
65 
66 setattr(LHEWeightAnalyzer,"defaultConfig",
67  cfg.Analyzer(LHEWeightAnalyzer,
68  )
69 )
const bool isValid(const Frame &aFrame, const FrameQuality &aQuality, const uint16_t aExpectedPos)
def __init__(self, cfg_ana, cfg_comp, looperName)
#define str(s)