Iguana
1.2.1
Implementation Guardian of Analysis Algorithms
Toggle main menu visibility
Loading...
Searching...
No Matches
iguana_ex_python_hipopy.py
Go to the documentation of this file.
1
#!/usr/bin/env python3
2
3
"""!
4
@begin_doc_example{python}
5
@file iguana_ex_python_hipopy.py
6
@brief Python iguana example using HIPOPy: https://github.com/mfmceneaney/hipopy
7
@note You may need to run this example using `stdbuf -o0` (preceding your command) if the output appears to be jumbled
8
@end_doc_example
9
@doxygen_off
10
"""
11
#NOTE: You must import hipopy, which imports hipopybind, BEFORE any cppyy libraries.
12
import
hipopy.hipopy
as
hp
13
import
pyiguana
14
import
sys
15
import
os.path
as
osp
16
17
# include the header files that you need
18
pyiguana.include(
19
'hipo4/reader.h'
,
20
'iguana/algorithms/clas12/EventBuilderFilter/Algorithm.h'
,
21
'iguana/algorithms/clas12/SectorFinder/Algorithm.h'
,
22
'iguana/algorithms/clas12/rga/MomentumCorrection/Algorithm.h'
,
23
)
24
# then import the bound namespaces (must be after including the headers)
25
from
cppyy.gbl
import
hipo, iguana
26
# from here the syntax is analogous to the C++ example
27
if
len(sys.argv)<=1:
28
print(
'Usage: python3'
,osp.basename(__file__),
' <inFile> <step> <nbatches>'
)
29
sys.exit(0)
30
inFile = sys.argv[1]
if
len(sys.argv)>1
else
'data.hipo'
31
step = int(sys.argv[2])
if
len(sys.argv)>2
else
3
32
nbatches = int(sys.argv[3])
if
len(sys.argv)>3
else
1
33
34
banks = [
35
"REC::Particle"
,
36
"RUN::config"
,
37
"REC::Track"
,
38
"REC::Calorimeter"
,
39
"REC::Scintillator"
,
40
]
41
42
# create the algorithms
43
algo_eventbuilder_filter =
iguana.clas12.EventBuilderFilter
()
# filter by Event Builder PID (a filter algorithm)
44
algo_sector_finder =
iguana.clas12.SectorFinder
()
# get the sector for each particle (a creator algorithm)
45
algo_momentum_correction =
iguana.clas12.rga.MomentumCorrection
()
# momentum corrections (a transformer algorithm)
46
47
# configure algorithms with a custom YAML file
48
config_file =
"examples/config_for_examples.yaml"
49
algo_eventbuilder_filter.SetConfigFile(config_file)
50
algo_sector_finder.SetConfigFile(config_file)
51
algo_momentum_correction.SetConfigFile(config_file)
52
53
# start the algorithms
54
algo_eventbuilder_filter.Start()
55
algo_sector_finder.Start()
56
algo_momentum_correction.Start()
57
58
# run the algorithms on each event
59
for
iBatch, batch
in
enumerate(hp.iterate([inFile],banks=banks,step=step)):
60
61
for
iEvent, pxs
in
enumerate(batch[
'REC::Particle_px'
]):
62
63
# verbose printout
64
print(f
'evnum = {batch["RUN::config_event"][iEvent][0]}'
)
65
# print(f'iBatch={iBatch}, iEvent={iEvent}')
66
# for key in batch:
67
# print(key,':',batch[key][iEvent])
68
69
# loop over particles
70
for
row, _px
in
enumerate(pxs):
71
pid = batch[
'REC::Particle_pid'
][iEvent][row]
72
73
# check the PID with EventBuilderFilter
74
if(algo_eventbuilder_filter.Filter(pid)):
75
76
# get the sector for this particle; this is using a vector action function, so
77
# many of its arguments are full arrays
78
sector = algo_sector_finder.GetStandardSector(
79
batch[
'REC::Track_sector'
][iEvent],
80
batch[
'REC::Track_pindex'
][iEvent],
81
batch[
'REC::Calorimeter_sector'
][iEvent],
82
batch[
'REC::Calorimeter_pindex'
][iEvent],
83
batch[
'REC::Scintillator_sector'
][iEvent],
84
batch[
'REC::Scintillator_pindex'
][iEvent],
85
row)
86
87
# correct the particle momentum
88
p_corrected = algo_momentum_correction.Transform(
89
batch[
'REC::Particle_px'
][iEvent][row],
90
batch[
'REC::Particle_py'
][iEvent][row],
91
batch[
'REC::Particle_pz'
][iEvent][row],
92
sector,
93
pid,
94
batch[
'RUN::config_torus'
][iEvent][0]
95
)
96
97
# then print the result
98
print(f
'Analysis Particle PDG = {pid}'
)
99
print(f
' sector = {sector}'
)
100
print(f
' p_old = ({batch["REC::Particle_px"][iEvent][row]:11.5f}, {batch["REC::Particle_py"][iEvent][row]:11.5f}, {batch["REC::Particle_pz"][iEvent][row]:11.5f})'
)
101
print(f
' p_new = ({p_corrected.px:11.5f}, {p_corrected.py:11.5f}, {p_corrected.pz:11.5f})'
)
102
103
# End iteration if maximum number of batches reached
104
if
(iBatch+1>=nbatches):
break
105
106
# stop the algorithms
107
algo_eventbuilder_filter.Stop()
108
algo_sector_finder.Stop()
109
algo_momentum_correction.Stop()
110
111
"""!@doxygen_on"""
iguana::clas12::EventBuilderFilter
Algorithm: Filter the particle bank (REC::Particle, or similar) bank by PID from the Event Builder
Definition
Algorithm.h:12
iguana::clas12::SectorFinder
Algorithm: Find the sector for all rows in REC::Particle
Definition
Algorithm.h:27
iguana::clas12::rga::MomentumCorrection
Algorithm: Momentum Corrections
Definition
Algorithm.h:12
iguana_v1.2.1
examples
iguana_ex_python_hipopy.py
Generated by
1.18.0