CMS 3D CMS Logo

preskimming.py
Go to the documentation of this file.
1 import json
2 import re
3 import ROOT
4 ROOT.PyConfig.IgnoreCommandLineOptions = True
5 
6 
7 class JSONFilter:
8  def __init__(self, fname="", runsAndLumis={}):
9  self.keep = {}
10  if fname != "":
11  self.runsAndLumis = json.load(open(fname, 'r'))
12  else:
13  self.runsAndLumis = runsAndLumis
14  for _run, lumis in self.runsAndLumis.items():
15  run = int(_run)
16  if run not in self.keep:
17  self.keep[run] = []
18  self.keep[run] += lumis
19  for run in list(self.keep.keys()):
20  if len(self.keep[run]) == 0:
21  del self.keep[run]
22 
23  def filterRunLumi(self, run, lumi):
24  try:
25  for (l1, l2) in self.keep[run]:
26  if l1 <= lumi and lumi <= l2:
27  return True
28  return False
29  except KeyError:
30  return False
31 
32  def filterRunOnly(self, run):
33  return (run in self.keep)
34 
35  def runCut(self):
36  return "%d <= run && run <= %s" % (min(self.keep.keys()), max(self.keep.keys()))
37 
38  def filterEList(self, tree, elist):
39  # FIXME this can be optimized for sure
40  tree.SetBranchStatus("*", 0)
41  tree.SetBranchStatus('run', 1)
42  tree.SetBranchStatus('luminosityBlock', 1)
43  filteredList = ROOT.TEntryList('filteredList', 'filteredList')
44  if elist:
45  for i in range(elist.GetN()):
46  entry = elist.GetEntry(0) if i == 0 else elist.Next()
47  tree.GetEntry(entry)
48  if self.filterRunLumi(tree.run, tree.luminosityBlock):
49  filteredList.Enter(entry)
50  else:
51  for entry in range(tree.GetEntries()):
52  tree.GetEntry(entry)
53  if self.filterRunLumi(tree.run, tree.luminosityBlock):
54  filteredList.Enter(entry)
55  tree.SetBranchStatus("*", 1)
56  return filteredList
57 
58 
59 def preSkim(tree, jsonInput=None, cutstring=None, maxEntries=None, firstEntry=0):
60  if jsonInput == None and cutstring == None:
61  return None, None
62  cut = None
63  jsonFilter = None
64  if jsonInput != None:
65  if type(jsonInput) is dict:
66  jsonFilter = JSONFilter(runsAndLumis=jsonInput)
67  else:
68  jsonFilter = JSONFilter(jsonInput)
69  cut = jsonFilter.runCut()
70  if cutstring != None:
71  cut = "(%s) && (%s)" % (cutstring, cut) if cut else cutstring
72  if maxEntries is None:
73  maxEntries = ROOT.TVirtualTreePlayer.kMaxEntries
74  while "AltBranch$" in cut:
75  m = re.search(r"AltBranch\$\(\s*(\w+)\s*,\s*(\w+)\s*\)", cut)
76  if not m:
77  raise RuntimeError(
78  "Error, found AltBranch$ in cut string, but it doesn't comply with the syntax this code can support. The cut is %r" % cut)
79  cut = cut.replace(m.group(0), m.group(
80  1) if tree.GetBranch(m.group(1)) else m.group(2))
81  tree.Draw('>>elist', cut, "entrylist", maxEntries, firstEntry)
82  elist = ROOT.gDirectory.Get('elist')
83  if jsonInput:
84  elist = jsonFilter.filterEList(tree, elist)
85  return elist, jsonFilter
def filterRunLumi(self, run, lumi)
Definition: preskimming.py:23
def filterEList(self, tree, elist)
Definition: preskimming.py:38
def __init__(self, fname="", runsAndLumis={})
Definition: preskimming.py:8
def preSkim(tree, jsonInput=None, cutstring=None, maxEntries=None, firstEntry=0)
Definition: preskimming.py:59
def filterRunOnly(self, run)
Definition: preskimming.py:32