Belle II Software  release-06-00-14
flags.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 
4 
11 
12 """
13 Modules required for calculating skim flags. Skim flags track whether an event passes a
14 skim, without the need to directly remove those events from processing.
15 """
16 
17 import basf2 as b2
18 
19 
20 class InitialiseSkimFlag(b2.Module):
21  """
22  *[Module for skim expert usage]* Create the EventExtraInfo DataStore object, and set
23  all required flag variables to zero.
24 
25  .. Note::
26 
27  Add this module to the path before adding any skims, so that the skim flags are
28  defined in the datastore for all events.
29  """
30 
31  def __init__(self, *skims):
32  """
33  Initialise module.
34 
35  Parameters:
36  skims (skim.core.BaseSkim): Skim to initialise event flag for.
37  """
38 
39  from variables import variables as vm
40  from ROOT import Belle2
41 
42  super().__init__()
43  self.skimsskims = skims
44  self.EventExtraInfoEventExtraInfo = Belle2.PyStoreObj("EventExtraInfo")
45 
46  # Create aliases for convenience
47  for skim in skims:
48  vm.addAlias(skim.flag, f"eventExtraInfo({skim.flag})")
49 
50  def initialize(self):
51  """
52  Register EventExtraInfo in datastore if it has not been registered already.
53  """
54  if not self.EventExtraInfoEventExtraInfo.isValid():
55  self.EventExtraInfoEventExtraInfo.registerInDataStore()
56 
57  def event(self):
58  """
59  Initialise flags to zero.
60  """
61 
62  self.EventExtraInfoEventExtraInfo.create()
63  for skim in self.skimsskims:
64  self.EventExtraInfoEventExtraInfo.addExtraInfo(skim.flag, 0)
65 
66 
67 class UpdateSkimFlag(b2.Module):
68  """
69  *[Module for skim expert usage]* Update the skim flag to be 1 if there is at least
70  one candidate in any of the skim lists.
71 
72  .. Note::
73 
74  Add this module to the post-skim path of each skim in the combined skim, as the
75  skim lists are only guaranteed to exist on the conditional path (if a
76  conditional path was used).
77  """
78 
79  def __init__(self, skim):
80  """
81  Initialise module.
82 
83  Parameters:
84  skim (skim.core.BaseSkim): Skim to update event flag for.
85  """
86 
87  from ROOT import Belle2
88 
89  super().__init__()
90  self.skimskim = skim
91  self.EventExtraInfoEventExtraInfo = Belle2.PyStoreObj("EventExtraInfo")
92 
93  def initialize(self):
94  """
95  Check EventExtraInfo has been registered previously. This registration should be
96  done by InitialiseSkimFlag.
97  """
98  self.EventExtraInfoEventExtraInfo.isRequired()
99 
100  def event(self):
101  """
102  Check if at least one skim list is non-empty; if so, update the skim flag to 1.
103  """
104 
105  from ROOT import Belle2
106 
107  ListObjects = [Belle2.PyStoreObj(lst) for lst in self.skimskim.SkimLists]
108 
109  # Check required skim lists have been built on this path
110  if any([not ListObj.isValid() for ListObj in ListObjects]):
111  b2.B2FATAL(
112  f"Error in UpdateSkimFlag for {self.skim}: particle lists not built. "
113  "Did you add this module to the pre-skim path rather than the post-skim path?"
114  )
115 
116  nCandidates = sum(ListObj.getListSize() for ListObj in ListObjects)
117 
118  # Override ExtraInfo flag if at least one candidate from any list passed
119  if nCandidates > 0:
120  self.EventExtraInfoEventExtraInfo.setExtraInfo(self.skimskim.flag, 1)
a (simplified) python wrapper for StoreObjPtr.
Definition: PyStoreObj.h:67
def __init__(self, *skims)
Definition: flags.py:31
def initialize(self)
Definition: flags.py:50
def __init__(self, skim)
Definition: flags.py:79
def initialize(self)
Definition: flags.py:93
def event(self)
Definition: flags.py:100