Belle II Software  release-06-02-00
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 ROOT import Belle2 # noqa
40  from variables import variables as vm # noqa
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  self.EventExtraInfoEventExtraInfo.create()
62  for skim in self.skimsskims:
63  self.EventExtraInfoEventExtraInfo.addExtraInfo(skim.flag, 0)
64 
65 
66 class UpdateSkimFlag(b2.Module):
67  """
68  *[Module for skim expert usage]* Update the skim flag to be 1 if there is at least
69  one candidate in any of the skim lists.
70 
71  .. Note::
72 
73  Add this module to the post-skim path of each skim in the combined skim, as the
74  skim lists are only guaranteed to exist on the conditional path (if a
75  conditional path was used).
76  """
77 
78  def __init__(self, skim):
79  """
80  Initialise module.
81 
82  Parameters:
83  skim (skim.core.BaseSkim): Skim to update event flag for.
84  """
85 
86  from ROOT import Belle2 # noqa
87 
88  super().__init__()
89  self.skimskim = skim
90  self.EventExtraInfoEventExtraInfo = Belle2.PyStoreObj("EventExtraInfo")
91  self.ListObjectsListObjects = []
92 
93  def initialize(self):
94  """
95  Check EventExtraInfo and all the necessary ParticleLists have been registered previously.
96  The registration of EventExtraInfo should be done by InitialiseSkimFlag.
97  """
98  from ROOT import Belle2 # noqa
99 
100  self.EventExtraInfoEventExtraInfo.isRequired()
101  self.ListObjectsListObjects = [Belle2.PyStoreObj(lst) for lst in self.skimskim.SkimLists]
102  for ListObject in self.ListObjectsListObjects:
103  ListObject.isRequired()
104 
105  def event(self):
106  """
107  Check if at least one skim list is non-empty; if so, update the skim flag to 1.
108  """
109  flag = 0
110  for ListObject in self.ListObjectsListObjects:
111  if not ListObject.isValid():
112  b2.B2FATAL(
113  f"Error in UpdateSkimFlag for {self.skim}: particle lists not built. "
114  "Did you add this module to the pre-skim path rather than the post-skim path?"
115  )
116  elif ListObject.getListSize() > 0:
117  flag = 1
118 
119  self.EventExtraInfoEventExtraInfo.setExtraInfo(self.skimskim.flag, flag)
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:78
def initialize(self)
Definition: flags.py:93
def event(self)
Definition: flags.py:105