Iguana 1.0.0
Implementation Guardian of Analysis Algorithms
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
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::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
47seq.SetOption('clas12::EventBuilderFilter', 'log', 'info')
48seq.SetOption('clas12::SectorFinder', 'log', 'info')
49seq.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
53seq.SetOption('clas12::EventBuilderFilter', 'pids', [11, 211, -211])
54
55# start the algorithms
56seq.Start(banks)
57
58# get the name of newly created banks (if you don't want to look them up in the documentation)
59sector_finder_bank_name = seq.GetCreatedBankName("clas12::SectorFinder");
60
61# get bank index, for each bank we want to use after Iguana algorithms run
62# NOTE: new banks from creator algorithms are initialized by `Start`
63b_config = hipo.getBanklistIndex(banks, 'RUN::config')
64b_particle = hipo.getBanklistIndex(banks, 'REC::Particle')
65b_sector = hipo.getBanklistIndex(banks, sector_finder_bank_name) # new created bank
66
67# run the algorithm sequence on each event
68iEvent = 0
69while(reader.next(banks) and (numEvents==0 or iEvent < numEvents)):
70 iEvent += 1
71
72 # print the event number
73 # NOTE: we use 'flush=True' in Python `print` calls here, otherwise these `print`
74 # calls will be out-of-order with respect to C++ printouts coming from, e.g.,
75 # Iguana and `hipo::bank::show()` (they use different buffers)
76 print(f'===== EVENT {banks[b_config].getInt("event", 0)} =====', flush=True)
77
78 # print the particle bank before Iguana algorithms
79 print('----- BEFORE IGUANA -----', flush=True)
80 banks[b_particle].show() # the original particle bank
81
82 # run the sequence of Iguana algorithms
83 seq.Run(banks)
84
85 # print the banks after Iguana algorithms
86 print('----- AFTER IGUANA -----', flush=True)
87 banks[b_particle].show() # the filtered particle bank, with corrected momenta
88 banks[b_sector].show() # the new sector bank
89
90 # print a table; first the header
91 print('----- Analysis Particles -----', flush=True)
92 print(f' {"row == pindex":<20} {"PDG":<20} {"|p|":<20} {"sector":<20}', flush=True)
93 # then print a row for each particle
94 # - use the `banks[b_particle].getRowList()` method to loop over the bank rows that PASS the filter; note
95 # that it needs to be converted via `list()` in order to be iterated
96 # - if you'd rather loop over ALL bank rows, iterate from `i=0` up to `i < banks[b_particle].getRows()` instead
97 for row in list(banks[b_particle].getRowList()):
98 p = math.hypot(
99 banks[b_particle].getFloat('px', row),
100 banks[b_particle].getFloat('py', row),
101 banks[b_particle].getFloat('pz', row))
102 pdg = banks[b_particle].getInt('pid', row)
103 sector = banks[b_sector].getInt('sector', row)
104 print(f' {row:<20} {pdg:<20} {p:<20.3f} {sector:<20}', flush=True)
105 print(flush=True)
106
107# stop algorithms
108seq.Stop()
109
110"""!@doxygen_on"""
Algorithm: An algorithm that can run a sequence of algorithms