Iguana LATEST
Implementation Guardian of Analysis Algorithms
Loading...
Searching...
No Matches
iguana_ex_python_01_action_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_01_action_functions.py
6@brief Python version of `iguana_ex_cpp_01_action_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
14
15# include the header files that you need
16pyiguana.include(
17 'hipo4/reader.h',
18 'iguana/algorithms/clas12/EventBuilderFilter/Algorithm.h',
19 'iguana/algorithms/clas12/SectorFinder/Algorithm.h',
20 'iguana/algorithms/clas12/rga/MomentumCorrection/Algorithm.h',
21 )
22# then import the bound namespaces (must be after including the headers)
23from cppyy.gbl import hipo, iguana
24# from here the syntax is analogous to the C++ example
25
26# parse arguments
27inFile = sys.argv[1] if len(sys.argv)>1 else 'data.hipo'
28numEvents = int(sys.argv[2]) if len(sys.argv)>2 else 3
29
30# read input file
31reader = hipo.reader(inFile)
32
33# set list of banks to be read
34banks = reader.getBanks([
35 "REC::Particle",
36 "RUN::config",
37 "REC::Track",
38 "REC::Calorimeter",
39 "REC::Scintillator",
40]);
41
42# get bank index, for each bank we want to use after Iguana algorithms run
43b_particle = iguana.tools.GetBankIndex(banks, "REC::Particle")
44b_config = iguana.tools.GetBankIndex(banks, "RUN::config")
45b_track = iguana.tools.GetBankIndex(banks, "REC::Track");
46b_calorimeter = iguana.tools.GetBankIndex(banks, "REC::Calorimeter");
47b_scintillator = iguana.tools.GetBankIndex(banks, "REC::Scintillator");
48
49# create the algorithms
50algo_eventbuilder_filter = iguana.clas12.EventBuilderFilter() # filter by Event Builder PID (a filter algorithm)
51algo_sector_finder = iguana.clas12.SectorFinder() # get the sector for each particle (a creator algorithm)
52algo_momentum_correction = iguana.clas12.rga.MomentumCorrection() # momentum corrections (a transformer algorithm)
53
54# configure algorithms with a custom YAML file
55# - in practice, specify the path(s) to your preferred configuration file(s); see documentation
56# on 'How to Configure Algorithms' for details, and alternative methods for algorithm configuration
57# - in this example, the file is from the source-code path `./config/examples/`, which was copied to
58# the installation subdirectory `etc/iguana/`, within the default configuration-file search path
59config_file = "examples/config_for_examples.yaml"
60algo_eventbuilder_filter.SetConfigFile(config_file)
61algo_sector_finder.SetConfigFile(config_file)
62algo_momentum_correction.SetConfigFile(config_file)
63
64# start the algorithms
65algo_eventbuilder_filter.Start()
66algo_sector_finder.Start()
67algo_momentum_correction.Start()
68
69# run the algorithms on each event
70iEvent = 0
71while(reader.next(banks) and (numEvents==0 or iEvent < numEvents)):
72 iEvent += 1
73
74 # get the banks for this event
75 particleBank = banks[b_particle]
76 configBank = banks[b_config]
77 trackBank = banks[b_track]
78 calorimeterBank = banks[b_calorimeter]
79 scintillatorBank = banks[b_scintillator]
80
81 # show the particle bank
82 # particleBank.show()
83
84 # print the event number
85 print(f'evnum = {configBank.getInt("event",0)}')
86
87 # we'll need information from all the rows of REC::Track,Calorimeter,Scintilator,
88 # in order to get the sector information for each particle
89 # FIXME: there are vectorized accessors, but we cannot use them yet; see https://github.com/gavalian/hipo/issues/72
90 # until then, we fill lists manually
91 trackBank_sectors = []
92 trackBank_pindices = []
93 calorimeterBank_sectors = []
94 calorimeterBank_pindices = []
95 scintillatorBank_sectors = []
96 scintillatorBank_pindices = []
97 for r in trackBank.getRowList():
98 trackBank_sectors.append(trackBank.getByte("sector", r))
99 trackBank_pindices.append(trackBank.getShort("pindex", r))
100 for r in calorimeterBank.getRowList():
101 calorimeterBank_sectors.append(calorimeterBank.getByte("sector", r))
102 calorimeterBank_pindices.append(calorimeterBank.getShort("pindex", r))
103 for r in scintillatorBank.getRowList():
104 scintillatorBank_sectors.append(scintillatorBank.getByte("sector", r))
105 scintillatorBank_pindices.append(scintillatorBank.getShort("pindex", r))
106
107 # loop over bank rows
108 for row in particleBank.getRowList():
109
110 # check the PID with EventBuilderFilter
111 pid = particleBank.getInt('pid', row)
112 if(algo_eventbuilder_filter.Filter(pid)):
113
114 # get the sector for this particle; this is using a vector action function, so
115 # many of its arguments are arrays
116 sector = algo_sector_finder.GetStandardSector(
117 trackBank_sectors,
118 trackBank_pindices,
119 calorimeterBank_sectors,
120 calorimeterBank_pindices,
121 scintillatorBank_sectors,
122 scintillatorBank_pindices,
123 row)
124
125 # correct the particle momentum
126 p_corrected = algo_momentum_correction.Transform(
127 particleBank.getFloat("px", row),
128 particleBank.getFloat("py", row),
129 particleBank.getFloat("pz", row),
130 sector,
131 pid,
132 configBank.getFloat("torus", 0)
133 )
134
135 # then print the result
136 print(f'Analysis Particle PDG = {pid}')
137 print(f' sector = {sector}')
138 print(f' p_old = ({particleBank.getFloat("px", row):11.5f}, {particleBank.getFloat("py", row):11.5f}, {particleBank.getFloat("pz", row):11.5f})')
139 print(f' p_new = ({p_corrected.px:11.5f}, {p_corrected.py:11.5f}, {p_corrected.pz:11.5f})')
140
141# stop the algorithms
142algo_eventbuilder_filter.Stop()
143algo_sector_finder.Stop()
144algo_momentum_correction.Stop()
145
146"""!@doxygen_on"""
Algorithm: Filter the particle bank (REC::Particle, or similar) bank by PID from the Event Builder
Definition Algorithm.h:12
Algorithm: Find the sector for all rows in REC::Particle
Definition Algorithm.h:27
Algorithm: Momentum Corrections
Definition Algorithm.h:12
hipo::banklist::size_type GetBankIndex(hipo::banklist &banks, std::string const &bank_name, unsigned int const &variant=0) noexcept(false)