CMS 3D CMS Logo

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