CMS 3D CMS Logo

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 
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 
counter
Definition: counter.py:1
LumiList
Definition: LumiList.py:1
core.JSONAnalyzer.JSONAnalyzer.useLumiBlocks
useLumiBlocks
Definition: JSONAnalyzer.py:40
core.JSONAnalyzer.JSONAnalyzer.count
count
Definition: JSONAnalyzer.py:47
remoteMonitoring_LED_IterMethod_cfg.Analyzer
Analyzer
Definition: remoteMonitoring_LED_IterMethod_cfg.py:32
PVValHelper::add
void add(std::map< std::string, TH1 * > &h, TH1 *hist)
Definition: PVValidationHelpers.cc:12
core.JSONAnalyzer.JSONAnalyzer.beginLoop
def beginLoop(self, setup)
Definition: JSONAnalyzer.py:44
core.JSONAnalyzer.JSONAnalyzer.rltInfo
rltInfo
Definition: JSONAnalyzer.py:42
edm::contains
bool contains(EventRange const &lh, EventID const &rh)
Definition: EventRange.cc:37
core.JSONAnalyzer.JSONAnalyzer.lumiList
lumiList
Definition: JSONAnalyzer.py:36
core.JSONAnalyzer.JSONAnalyzer.__init__
def __init__(self, cfg_ana, cfg_comp, looperName)
Definition: JSONAnalyzer.py:31
core.JSONAnalyzer.JSONAnalyzer.write
def write(self, setup)
Definition: JSONAnalyzer.py:87
core.JSONAnalyzer.JSONAnalyzer.process
def process(self, event)
Definition: JSONAnalyzer.py:60
triggerObjects_cff.id
id
Definition: triggerObjects_cff.py:29
format
core.JSONAnalyzer.JSONAnalyzer
Definition: JSONAnalyzer.py:10