Iguana 0.9.0
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/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 = hipo.getBanklistIndex(banks, "REC::Particle")
44b_config = hipo.getBanklistIndex(banks, "RUN::config")
45b_track = hipo.getBanklistIndex(banks, "REC::Track");
46b_calorimeter = hipo.getBanklistIndex(banks, "REC::Calorimeter");
47b_scintillator = hipo.getBanklistIndex(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.MomentumCorrection() # momentum corrections (a transformer algorithm)
53
54# set log levels
55algo_eventbuilder_filter.SetOption('log', 'info')
56algo_sector_finder.SetOption('log', 'info')
57algo_momentum_correction.SetOption('log', 'info')
58
59# set algorithm options
60algo_eventbuilder_filter.SetOption('pids', [11, 211, -211])
61
62# start the algorithms
63algo_eventbuilder_filter.Start()
64algo_sector_finder.Start()
65algo_momentum_correction.Start()
66
67# run the algorithms on each event
68iEvent = 0
69while(reader.next(banks) and (numEvents==0 or iEvent < numEvents)):
70 iEvent += 1
71
72 # get the banks for this event
73 particleBank = banks[b_particle]
74 configBank = banks[b_config]
75 trackBank = banks[b_track]
76 calorimeterBank = banks[b_calorimeter]
77 scintillatorBank = banks[b_scintillator]
78
79 # show the particle bank
80 # particleBank.show()
81
82 # print the event number
83 print(f'evnum = {configBank.getInt("event",0)}')
84
85 # we'll need information from all the rows of REC::Track,Calorimeter,Scintilator,
86 # in order to get the sector information for each particle
87 # FIXME: there are vectorized accessors, but we cannot use them yet; see https://github.com/gavalian/hipo/issues/72
88 # until then, we fill lists manually
89 trackBank_sectors = []
90 trackBank_pindices = []
91 calorimeterBank_sectors = []
92 calorimeterBank_pindices = []
93 scintillatorBank_sectors = []
94 scintillatorBank_pindices = []
95 for r in trackBank.getRowList():
96 trackBank_sectors.append(trackBank.getByte("sector", r))
97 trackBank_pindices.append(trackBank.getShort("pindex", r))
98 for r in calorimeterBank.getRowList():
99 calorimeterBank_sectors.append(calorimeterBank.getByte("sector", r))
100 calorimeterBank_pindices.append(calorimeterBank.getShort("pindex", r))
101 for r in scintillatorBank.getRowList():
102 scintillatorBank_sectors.append(scintillatorBank.getByte("sector", r))
103 scintillatorBank_pindices.append(scintillatorBank.getShort("pindex", r))
104
105 # loop over bank rows
106 for row in particleBank.getRowList():
107
108 # check the PID with EventBuilderFilter
109 pid = particleBank.getInt('pid', row)
110 if(algo_eventbuilder_filter.Filter(pid)):
111
112 # get the sector for this particle; this is using a vector action function, so
113 # many of its arguments are arrays
114 sector = algo_sector_finder.GetStandardSector(
115 trackBank_sectors,
116 trackBank_pindices,
117 calorimeterBank_sectors,
118 calorimeterBank_pindices,
119 scintillatorBank_sectors,
120 scintillatorBank_pindices,
121 row)
122
123 # correct the particle momentum
124 p_corrected = algo_momentum_correction.Transform(
125 particleBank.getFloat("px", row),
126 particleBank.getFloat("py", row),
127 particleBank.getFloat("pz", row),
128 sector,
129 pid,
130 configBank.getFloat("torus", 0)
131 )
132
133 # then print the result
134 print(f'Analysis Particle PDG = {pid}')
135 print(f' sector = {sector}')
136 print(f' p_old = ({particleBank.getFloat("px", row):11.5f}, {particleBank.getFloat("py", row):11.5f}, {particleBank.getFloat("pz", row):11.5f})')
137 print(f' p_new = ({p_corrected.px:11.5f}, {p_corrected.py:11.5f}, {p_corrected.pz:11.5f})')
138
139# stop the algorithms
140algo_eventbuilder_filter.Stop()
141algo_sector_finder.Stop()
142algo_momentum_correction.Stop()
143
144"""!@doxygen_on"""
Algorithm: Filter the REC::Particle (or similar) bank by PID from the Event Builder
Definition Algorithm.h:18
Algorithm: Momentum Corrections
Definition Algorithm.h:17
Algorithm: Find the sector for all rows in REC::Particle
Definition Algorithm.h:34