Belle II Software  release-06-02-00
register_pystorearray.py
1 #!/usr/bin/env python
2 
3 
10 
11 
15 
16 import math
17 
18 import basf2
19 import ROOT
20 from ROOT import Belle2
21 
22 
23 class SillyGeneratorModule(basf2.Module):
24  """Small module to demonstrate how the registration of StoreArrays works from Python"""
25 
26  def initialize(self):
27  """ Register a StoreArray on the DataStore"""
28  mcParticles = Belle2.PyStoreArray(Belle2.MCParticle.Class())
29  mcParticles.registerInDataStore()
30 
31  def event(self):
32  """ Access and fill the registered StoreArray """
33  # Alternative access by name
34  mcParticles = Belle2.PyStoreArray("MCParticles")
35  mcParticle = mcParticles.appendNew()
36 
37  # Fill one test muon for demonstration
38  vertex = ROOT.TVector3(0, 0, 0)
39 
40  # Sample a random momentum vector of
41  # gaus distributed length and uniform distibuted direction
42  phi = ROOT.gRandom.Uniform(0.0, 2.0 * math.pi)
43  costheta = ROOT.gRandom.Uniform(-1.0, 1.0)
44  theta = math.acos(costheta)
45  momentum = ROOT.TVector3(0, 0, 0)
46  momentum.SetMagThetaPhi(1, theta, phi)
47 
48  mcParticle.setPDG(13)
49  mcParticle.setMassFromPDG()
50  mcParticle.addStatus(Belle2.MCParticle.c_PrimaryParticle)
51  mcParticle.addStatus(Belle2.MCParticle.c_StableInGenerator)
52  mcParticle.setProductionVertex(vertex)
53  mcParticle.setMomentum(momentum)
54  m = mcParticle.getMass()
55  mcParticle.setEnergy(math.sqrt(momentum * momentum + m * m))
56  mcParticle.setDecayTime(float("inf"))
57 
58 
59 class ParticleStatisticsModule(basf2.Module):
60  """Collect statistics on particles - also parallel processable"""
61 
62  def __init__(self):
63  """set module flags"""
64  super().__init__()
65  self.set_property_flags(
66  basf2.ModulePropFlags.PARALLELPROCESSINGCERTIFIED |
67  basf2.ModulePropFlags.TERMINATEINALLPROCESSES)
68 
69  def initialize(self):
70  """init"""
71 
72  self.tfiletfile = ROOT.TFile("ParticleStatistics.root", "recreate")
73 
74  ntuple = Belle2.PyStoreObj(Belle2.RootMergeable("TNtuple").Class(), "ParticleMomenta", Belle2.DataStore.c_Persistent)
75  ntuple.registerInDataStore()
76  print("IsValid", ntuple.isValid())
77  ntuple.create()
78  print("IsValid", ntuple.isValid())
79  ntuple.obj().assign(ROOT.TNtuple("Particles", # ROOT name
80  "Momentum compontents of the particles", # ROOT title
81  "px:py:pz" # Var list
82  ))
83 
84  hist = Belle2.PyStoreObj(Belle2.RootMergeable("TH1D").Class(), "AbsMomentum", Belle2.DataStore.c_Persistent)
85  hist.registerInDataStore()
86  print("IsValid", hist.isValid())
87  hist.create()
88  print("IsValid", hist.isValid())
89  hist.obj().assign(ROOT.TH1D("AbsMomentum", # ROOT name
90  "Absolute momentum of particles", # ROOT title
91  20, # n bins
92  0, # upper bound
93  2 # lower bound
94  ))
95 
96  print("IsValid", ntuple.isValid())
97  print("IsValid", hist.isValid())
98 
99  def event(self):
100  """actually collect info"""
101  ntuple = Belle2.PyStoreObj("ParticleMomenta", Belle2.DataStore.c_Persistent)
102  hist = Belle2.PyStoreObj("AbsMomentum", Belle2.DataStore.c_Persistent)
103 
104  mcParticles = Belle2.PyStoreArray(Belle2.MCParticle.Class())
105 
106  for mcParticle in mcParticles:
107  momentum = mcParticle.getMomentum()
108  ntuple.get().Fill(momentum.X(),
109  momentum.Y(),
110  momentum.Z())
111 
112  hist.get().Fill(momentum.Mag())
113 
114  def terminate(self):
115  """terminate"""
118  print("Writting objects")
119  ntuple = Belle2.PyStoreObj("ParticleMomenta", Belle2.DataStore.c_Persistent)
120  ntuple.write(self.tfiletfile)
121 
122  hist = Belle2.PyStoreObj("AbsMomentum", Belle2.DataStore.c_Persistent)
123  hist.write(self.tfiletfile)
124 
125  self.tfiletfile.Close()
126 
127 
128 def main():
129  path = basf2.create_path()
130  path.add_module('EventInfoSetter',
131  evtNumList=[1000],
132  runList=[1],
133  expList=[1]
134  )
135 
136  path.add_module(SillyGeneratorModule())
137  path.add_module(ParticleStatisticsModule())
138 
139  basf2.process(path)
140 
141  # Print call statistics
142  print(basf2.statistics)
143 
144 
145 if __name__ == "__main__":
146  main()
static bool isOutputProcess()
Return true if the process is an output process.
Definition: ProcHandler.cc:232
static bool parallelProcessingUsed()
Returns true if multiple processes have been spawned, false in single-core mode.
Definition: ProcHandler.cc:226
a (simplified) python wrapper for StoreArray.
Definition: PyStoreArray.h:56
a (simplified) python wrapper for StoreObjPtr.
Definition: PyStoreObj.h:67
Wrap a root histogram or TNtuple to make it mergeable.
Definition: RootMergeable.h:61
int main(int argc, char **argv)
Run all tests.
Definition: test_main.cc:75