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