Iguana LATEST
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# configure algorithms with a custom YAML file
46# - in practice, specify the path(s) to your preferred configuration file(s); see documentation
47# on 'How to Configure Algorithms' for details, and alternative methods for algorithm configuration
48# - in this example, the file is from the source-code path `./config/examples/`, which was copied to
49# the installation subdirectory `etc/iguana/`, within the default configuration-file search path
50seq.SetConfigFileForEachAlgorithm("examples/config_for_examples.yaml")
51# alternatively: use this configuration for the algorithm that needs it
52# seq.Get("clas12::EventBuilderFilter").SetConfigFile("examples/config_for_examples.yaml")
53
54# start the algorithms
55seq.Start(banks)
56
57# get bank index, for each bank we want to use after Iguana algorithms run
58# NOTE: new banks from creator algorithms are initialized by `Start`
59b_config = iguana.tools.GetBankIndex(banks, 'RUN::config')
60b_particle = iguana.tools.GetBankIndex(banks, 'REC::Particle')
61b_sector = seq.GetCreatedBankIndex(banks, "clas12::SectorFinder") # newly created bank; string parameter is algorithm name, not bank name
62
63# run the algorithm sequence on each event
64iEvent = 0
65while(reader.next(banks) and (numEvents==0 or iEvent < numEvents)):
66 iEvent += 1
67
68 # print the event number
69 # NOTE: we use 'flush=True' in Python `print` calls here, otherwise these `print`
70 # calls will be out-of-order with respect to C++ printouts coming from, e.g.,
71 # Iguana and `hipo::bank::show()` (they use different buffers)
72 print(f'===== EVENT {banks[b_config].getInt("event", 0)} =====', flush=True)
73
74 # print the particle bank before Iguana algorithms
75 print('----- BEFORE IGUANA -----', flush=True)
76 banks[b_particle].show() # the original particle bank
77
78 # run the sequence of Iguana algorithms
79 seq.Run(banks)
80
81 # print the banks after Iguana algorithms
82 print('----- AFTER IGUANA -----', flush=True)
83 banks[b_particle].show() # the filtered particle bank, with corrected momenta
84 banks[b_sector].show() # the new sector bank
85
86 # print a table; first the header
87 print('----- Analysis Particles -----', flush=True)
88 print(f' {"row == pindex":<20} {"PDG":<20} {"|p|":<20} {"sector":<20}', flush=True)
89 # then print a row for each particle
90 # - use the `banks[b_particle].getRowList()` method to loop over the bank rows that PASS the filter; note
91 # that it needs to be converted via `list()` in order to be iterated
92 # - if you'd rather loop over ALL bank rows, iterate from `i=0` up to `i < banks[b_particle].getRows()` instead
93 for row in list(banks[b_particle].getRowList()):
94 p = math.hypot(
95 banks[b_particle].getFloat('px', row),
96 banks[b_particle].getFloat('py', row),
97 banks[b_particle].getFloat('pz', row))
98 pdg = banks[b_particle].getInt('pid', row)
99 sector = banks[b_sector].getInt('sector', row)
100 print(f' {row:<20} {pdg:<20} {p:<20.3f} {sector:<20}', flush=True)
101 print(flush=True)
102
103# stop algorithms
104seq.Stop()
105
106"""!@doxygen_on"""
Algorithm: An algorithm that can run a sequence of algorithms
hipo::banklist::size_type GetBankIndex(hipo::banklist &banks, std::string const &bank_name, unsigned int const &variant=0) noexcept(false)