Iguana
1.0.0
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
12
import
pyiguana
13
import
sys
14
import
hipopy.hipopy
as
hp
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
# set log levels
48
algo_eventbuilder_filter.SetOption(
'log'
,
'info'
)
49
algo_sector_finder.SetOption(
'log'
,
'info'
)
50
algo_momentum_correction.SetOption(
'log'
,
'info'
)
51
52
# set algorithm options
53
algo_eventbuilder_filter.SetOption(
'pids'
, [11, 211, -211])
54
55
# start the algorithms
56
algo_eventbuilder_filter.Start()
57
algo_sector_finder.Start()
58
algo_momentum_correction.Start()
59
60
# run the algorithms on each event
61
for
iBatch, batch
in
enumerate(hp.iterate([inFile],banks=banks,step=step)):
62
63
for
iEvent, pxs
in
enumerate(batch[
'REC::Particle_px'
]):
64
65
# verbose printout
66
print(f
'evnum = {batch["RUN::config_event"][iEvent][0]}'
)
67
# print(f'iBatch={iBatch}, iEvent={iEvent}')
68
# for key in batch:
69
# print(key,':',batch[key][iEvent])
70
71
# loop over particles
72
for
row, _px
in
enumerate(pxs):
73
pid = batch[
'REC::Particle_pid'
][iEvent][row]
74
75
# check the PID with EventBuilderFilter
76
if(algo_eventbuilder_filter.Filter(pid)):
77
78
# get the sector for this particle; this is using a vector action function, so
79
# many of its arguments are full arrays
80
sector = algo_sector_finder.GetStandardSector(
81
batch[
'REC::Track_sector'
][iEvent],
82
batch[
'REC::Track_pindex'
][iEvent],
83
batch[
'REC::Calorimeter_sector'
][iEvent],
84
batch[
'REC::Calorimeter_pindex'
][iEvent],
85
batch[
'REC::Scintillator_sector'
][iEvent],
86
batch[
'REC::Scintillator_pindex'
][iEvent],
87
row)
88
89
# correct the particle momentum
90
p_corrected = algo_momentum_correction.Transform(
91
batch[
'REC::Particle_px'
][iEvent][row],
92
batch[
'REC::Particle_py'
][iEvent][row],
93
batch[
'REC::Particle_pz'
][iEvent][row],
94
sector,
95
pid,
96
batch[
'RUN::config_torus'
][iEvent][0]
97
)
98
99
# then print the result
100
print(f
'Analysis Particle PDG = {pid}'
)
101
print(f
' sector = {sector}'
)
102
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})'
)
103
print(f
' p_new = ({p_corrected.px:11.5f}, {p_corrected.py:11.5f}, {p_corrected.pz:11.5f})'
)
104
105
# End iteration if maximum number of batches reached
106
if
(iBatch+1>=nbatches):
break
107
108
# stop the algorithms
109
algo_eventbuilder_filter.Stop()
110
algo_sector_finder.Stop()
111
algo_momentum_correction.Stop()
112
113
"""!@doxygen_on"""
iguana::clas12::EventBuilderFilter
Algorithm: Filter the REC::Particle (or similar) bank by PID from the Event Builder
Definition
Algorithm.h:14
iguana::clas12::SectorFinder
Algorithm: Find the sector for all rows in REC::Particle
Definition
Algorithm.h:30
iguana::clas12::rga::MomentumCorrection
Algorithm: Momentum Corrections
Definition
Algorithm.h:12
iguana_v1.0.0
bind
python
iguana_ex_python_hipopy.py
Generated by
1.18.0