Iguana
1.1.0
Implementation Guardian of Analysis Algorithms
Toggle main menu visibility
Loading...
Searching...
No Matches
iguana_ex_python_00_run_functions.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_00_run_functions.py
6
@brief Python version of `iguana_ex_cpp_00_run_functions.cc` (for more details, see this `.cc` file)
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
math
15
16
# include the header files that you need
17
pyiguana.include(
'hipo4/reader.h'
,
'iguana/algorithms/AlgorithmSequence.h'
)
18
# then import the bound namespaces (must be after including the headers)
19
from
cppyy.gbl
import
hipo, iguana
20
# from here the syntax is analogous to the C++ example
21
22
# parse arguments
23
inFile = sys.argv[1]
if
len(sys.argv)>1
else
'data.hipo'
24
numEvents = int(sys.argv[2])
if
len(sys.argv)>2
else
3
25
26
# read input file
27
reader = hipo.reader(inFile)
28
29
# set list of banks to be read
30
banks = reader.getBanks([
31
"RUN::config"
,
32
"REC::Particle"
,
33
"REC::Calorimeter"
,
34
"REC::Track"
,
35
"REC::Scintillator"
])
36
37
# iguana algorithm sequence
38
# NOTE: the order that they are added to the sequence here will be the same order in which they will be run
39
seq =
iguana.AlgorithmSequence
(
'pyiguana'
)
40
seq.Add(
'clas12::EventBuilderFilter'
)
# filter by Event Builder PID (a filter algorithm)
41
seq.Add(
'clas12::SectorFinder'
)
# get the sector for each particle (a creator algorithm)
42
seq.Add(
'clas12::rga::MomentumCorrection'
)
# momentum corrections (a transformer algorithm)
43
# seq.PrintSequence()
44
45
# set log levels
46
# NOTE: this can also be done in a config file
47
seq.SetOption(
'clas12::EventBuilderFilter'
,
'log'
,
'info'
)
48
seq.SetOption(
'clas12::SectorFinder'
,
'log'
,
'info'
)
49
seq.SetOption(
'clas12::rga::MomentumCorrection'
,
'log'
,
'info'
)
50
51
# set algorithm options
52
# NOTE: this can also be done in a config file, but setting options here OVERRIDES config file settings
53
seq.SetOption(
'clas12::EventBuilderFilter'
,
'pids'
, [11, 211, -211])
54
55
# start the algorithms
56
seq.Start(banks)
57
58
# get bank index, for each bank we want to use after Iguana algorithms run
59
# NOTE: new banks from creator algorithms are initialized by `Start`
60
b_config =
iguana.tools.GetBankIndex
(banks,
'RUN::config'
)
61
b_particle =
iguana.tools.GetBankIndex
(banks,
'REC::Particle'
)
62
b_sector = seq.GetCreatedBankIndex(banks,
"clas12::SectorFinder"
)
# newly created bank; string parameter is algorithm name, not bank name
63
64
# run the algorithm sequence on each event
65
iEvent = 0
66
while(reader.next(banks)
and
(numEvents==0
or
iEvent < numEvents)):
67
iEvent += 1
68
69
# print the event number
70
# NOTE: we use 'flush=True' in Python `print` calls here, otherwise these `print`
71
# calls will be out-of-order with respect to C++ printouts coming from, e.g.,
72
# Iguana and `hipo::bank::show()` (they use different buffers)
73
print(f
'===== EVENT {banks[b_config].getInt("event", 0)} ====='
, flush=
True
)
74
75
# print the particle bank before Iguana algorithms
76
print(
'----- BEFORE IGUANA -----'
, flush=
True
)
77
banks[b_particle].show()
# the original particle bank
78
79
# run the sequence of Iguana algorithms
80
seq.Run(banks)
81
82
# print the banks after Iguana algorithms
83
print(
'----- AFTER IGUANA -----'
, flush=
True
)
84
banks[b_particle].show()
# the filtered particle bank, with corrected momenta
85
banks[b_sector].show()
# the new sector bank
86
87
# print a table; first the header
88
print(
'----- Analysis Particles -----'
, flush=
True
)
89
print(f
' {"row == pindex":<20} {"PDG":<20} {"|p|":<20} {"sector":<20}'
, flush=
True
)
90
# then print a row for each particle
91
# - use the `banks[b_particle].getRowList()` method to loop over the bank rows that PASS the filter; note
92
# that it needs to be converted via `list()` in order to be iterated
93
# - if you'd rather loop over ALL bank rows, iterate from `i=0` up to `i < banks[b_particle].getRows()` instead
94
for
row
in
list(banks[b_particle].getRowList()):
95
p = math.hypot(
96
banks[b_particle].getFloat(
'px'
, row),
97
banks[b_particle].getFloat(
'py'
, row),
98
banks[b_particle].getFloat(
'pz'
, row))
99
pdg = banks[b_particle].getInt(
'pid'
, row)
100
sector = banks[b_sector].getInt(
'sector'
, row)
101
print(f
' {row:<20} {pdg:<20} {p:<20.3f} {sector:<20}'
, flush=
True
)
102
print(flush=
True
)
103
104
# stop algorithms
105
seq.Stop()
106
107
"""!@doxygen_on"""
iguana::AlgorithmSequence
Algorithm: An algorithm that can run a sequence of algorithms
Definition
AlgorithmSequence.h:46
iguana::tools::GetBankIndex
hipo::banklist::size_type GetBankIndex(hipo::banklist &banks, std::string const &bank_name, unsigned int const &variant=0) noexcept(false)
iguana_v1.1.0
bind
python
iguana_ex_python_00_run_functions.py
Generated by
1.18.0