CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
JSONAnalyzer.py
Go to the documentation of this file.
1 import json
2 from PhysicsTools.Heppy.analyzers.core.Analyzer import Analyzer
3 
4 from FWCore.PythonUtilities.LumiList import LumiList
5 from PhysicsTools.Heppy.utils.rltinfo import RLTInfo
6 
7 
8 class JSONAnalyzer( Analyzer ):
9  '''Apply a json filter, and creates an RLTInfo TTree.
10  See PhysicsTools.HeppyCore.utils.RLTInfo for more information
11 
12  example:
13 
14  jsonFilter = cfg.Analyzer(
15  "JSONAnalyzer",
16  )
17 
18  The path of the json file to be used is set as a component attribute.
19 
20  The process function returns:
21  - True if
22  - the component is MC or
23  - if the run/lumi pair is in the JSON file
24  - if the json file was not set for this component
25  - False if the component is MC or embed (for H->tau tau),
26  and if the run/lumi pair is not in the JSON file.
27  '''
28 
29  def __init__(self, cfg_ana, cfg_comp, looperName):
30  super(JSONAnalyzer, self).__init__(cfg_ana, cfg_comp, looperName)
31  if not cfg_comp.isMC:
32  if self.cfg_comp.json is None:
33  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))
34  self.lumiList = LumiList(self.cfg_comp.json)
35  else:
36  self.lumiList = None
37 
38 
39  self.rltInfo = RLTInfo()
40 
41  def beginLoop(self, setup):
42  super(JSONAnalyzer,self).beginLoop(setup)
43  self.counters.addCounter('JSON')
44  self.count = self.counters.counter('JSON')
45  self.count.register('All Lumis')
46  self.count.register('Passed Lumis')
47 
48  def process(self, event):
49  self.readCollections( event.input )
50  evid = event.input.eventAuxiliary().id()
51  run = evid.run()
52  lumi = evid.luminosityBlock()
53  eventId = evid.event()
54 
55  event.run = run
56  event.lumi = lumi
57  event.eventId = eventId
58 
59  if self.cfg_comp.isMC:
60  return True
61 
62  if self.lumiList is None:
63  return True
64 
65  self.count.inc('All Lumis')
66  if self.lumiList.contains(run,lumi):
67  self.count.inc('Passed Lumis')
68  self.rltInfo.add('dummy', run, lumi)
69  return True
70  else:
71  return False
72 
73 
74  def write(self, setup):
75  super(JSONAnalyzer, self).write(setup)
76  self.rltInfo.write( self.dirName )
77