Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
__init__.py
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file __init__.py
1 import os
2 import shutil
3 from typing import List, Union
4 import contextlib
5 
6 import acts
7 from acts.examples import IAlgorithm
8 
9 geant4Enabled = (
10  any(v.startswith("G4") for v in os.environ.keys())
11  or "GEANT4_DATA_DIR" in os.environ
12 )
13 if geant4Enabled:
14  try:
16  except ImportError:
17  geant4Enabled = False
18 
19 try:
20  import ROOT
21 
22  rootEnabled = True
23 except ImportError:
24  rootEnabled = False
25 
26  if "ROOTSYS" in os.environ: # ROOT seems to be set up, but no PyROOT
27  import warnings
28 
29  warnings.warn(
30  "ROOT likely built without/with incompatible PyROOT. Skipping tests that need ROOT"
31  )
32 
33 dd4hepEnabled = "DD4hep_DIR" in os.environ
34 if dd4hepEnabled:
35  try:
37  except ImportError:
38  dd4hepEnabled = False
39 
40 try:
42 
43  hepmc3Enabled = True
44 except ImportError:
45  hepmc3Enabled = False
46 
47 try:
49 
50  edm4hepEnabled = True
51 except ImportError:
52  edm4hepEnabled = False
53 
54 try:
55  import acts.examples.onnx
56 
57  onnxEnabled = True
58 except ImportError:
59  onnxEnabled = False
60 
61 
62 try:
63  import acts.examples
64 
65  pythia8Enabled = hasattr(acts.examples, "pythia8")
66 except ImportError:
67  pythia8Enabled = False
68 
69 
70 exatrkxEnabled = shutil.which("nvidia-smi") is not None
71 if exatrkxEnabled:
72  try:
73  from acts.examples import TrackFindingAlgorithmExaTrkX
74  except ImportError:
75  exatrkxEnabled = False
76 
77 try:
78  import podio
79 
80  podioEnabled = True
81 except ModuleNotFoundError:
82  podioEnabled = False
83 
84 isCI = os.environ.get("CI", "false") == "true"
85 
86 if isCI:
87  for k, v in dict(locals()).items():
88  if k.endswith("Enabled"):
89  locals()[k] = True
90 
91 
92 class AssertCollectionExistsAlg(IAlgorithm):
93  events_seen = 0
94  collections: List[str]
95 
96  def __init__(
97  self,
98  collections: Union[List[str], str],
99  name="check_alg",
100  level=acts.logging.INFO,
101  *args,
102  **kwargs,
103  ):
104  if isinstance(collections, str):
105  self.collections = [collections]
106  else:
107  self.collections = collections
108  IAlgorithm.__init__(self, name=name, level=level, *args, **kwargs)
109 
110  def execute(self, ctx):
111  for collection in self.collections:
112  assert ctx.eventStore.exists(collection), f"{collection} does not exist"
113  self.events_seen += 1
114  return acts.examples.ProcessCode.SUCCESS
115 
116 
117 doHashChecks = os.environ.get("ROOT_HASH_CHECKS", "") != "" or "CI" in os.environ
118 
119 
120 @contextlib.contextmanager
121 def failure_threshold(level: acts.logging.Level, enabled: bool = True):
122  prev = acts.logging.getFailureThreshold()
123  if enabled and prev != level:
124  try:
125  acts.logging.setFailureThreshold(level)
126  except RuntimeError:
127  # Repackage with different error string
128  raise RuntimeError(
129  "Runtime log failure threshold could not be set. "
130  "Compile-time value is probably set via CMake, i.e. "
131  f"`ACTS_LOG_FAILURE_THRESHOLD={acts.logging.getFailureThreshold().name}` is set, "
132  "or `ACTS_ENABLE_LOG_FAILURE_THRESHOLD=OFF`. "
133  "The pytest test-suite will not work in this configuration."
134  )
135 
136  yield
137  acts.logging.setFailureThreshold(prev)
138  else:
139  yield