Loading [MathJax]/extensions/tex2jax.js
Iguana 0.9.0
Implementation Guardian of Analysis Algorithms
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
12import pyiguana
13import sys
14import math
15
16# include the header files that you need
17pyiguana.include('hipo4/reader.h', 'iguana/algorithms/AlgorithmSequence.h')
18# then import the bound namespaces (must be after including the headers)
19from cppyy.gbl import hipo, iguana
20# from here the syntax is analogous to the C++ example
21
22# parse arguments
23inFile = sys.argv[1] if len(sys.argv)>1 else 'data.hipo'
24numEvents = int(sys.argv[2]) if len(sys.argv)>2 else 3
25
26# read input file
27reader = hipo.reader(inFile)
28
29# set list of banks to be read
30banks = 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
39seq = iguana.AlgorithmSequence('pyiguana')
40seq.Add('clas12::EventBuilderFilter') # filter by Event Builder PID (a filter algorithm)
41seq.Add('clas12::SectorFinder') # get the sector for each particle (a creator algorithm)
42seq.Add('clas12::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
47seq.SetOption('clas12::EventBuilderFilter', 'log', 'info')
48seq.SetOption('clas12::SectorFinder', 'log', 'info')
49seq.SetOption('clas12::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
53seq.SetOption('clas12::EventBuilderFilter', 'pids', [11, 211, -211])
54
55# start the algorithms
56seq.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`
60b_config = hipo.getBanklistIndex(banks, 'RUN::config')
61b_particle = hipo.getBanklistIndex(banks, 'REC::Particle')
62b_sector = hipo.getBanklistIndex(banks, 'REC::Particle::Sector') # new created bank
63
64# run the algorithm sequence on each event
65iEvent = 0
66while(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
105seq.Stop()
106
107"""!@doxygen_on"""
An algorithm that can run a sequence of algorithms.