Loading [MathJax]/jax/input/TeX/config.js
Iguana 0.9.0
Implementation Guardian of Analysis Algorithms
iguana_ex_python_hipopy.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_hipopy.py
6@brief Python iguana example using HIPOPy: https://github.com/mfmceneaney/hipopy
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 hipopy.hipopy as hp
15import os.path as osp
16
17# include the header files that you need
18pyiguana.include(
19 'hipo4/reader.h',
20 'iguana/algorithms/clas12/EventBuilderFilter/Algorithm.h',
21 'iguana/algorithms/clas12/SectorFinder/Algorithm.h',
22 'iguana/algorithms/clas12/MomentumCorrection/Algorithm.h',
23 )
24# then import the bound namespaces (must be after including the headers)
25from cppyy.gbl import hipo, iguana
26# from here the syntax is analogous to the C++ example
27if len(sys.argv)<=1:
28 print('Usage: python3',osp.basename(__file__),' <inFile> <step> <nbatches>')
29 sys.exit(0)
30inFile = sys.argv[1] if len(sys.argv)>1 else 'data.hipo'
31step = int(sys.argv[2]) if len(sys.argv)>2 else 3
32nbatches = int(sys.argv[3]) if len(sys.argv)>3 else 1
33
34banks = [
35 "REC::Particle",
36 "RUN::config",
37 "REC::Track",
38 "REC::Calorimeter",
39 "REC::Scintillator",
40]
41
42# create the algorithms
43algo_eventbuilder_filter = iguana.clas12.EventBuilderFilter() # filter by Event Builder PID (a filter algorithm)
44algo_sector_finder = iguana.clas12.SectorFinder() # get the sector for each particle (a creator algorithm)
45algo_momentum_correction = iguana.clas12.MomentumCorrection() # momentum corrections (a transformer algorithm)
46
47# set log levels
48algo_eventbuilder_filter.SetOption('log', 'info')
49algo_sector_finder.SetOption('log', 'info')
50algo_momentum_correction.SetOption('log', 'info')
51
52# set algorithm options
53algo_eventbuilder_filter.SetOption('pids', [11, 211, -211])
54
55# start the algorithms
56algo_eventbuilder_filter.Start()
57algo_sector_finder.Start()
58algo_momentum_correction.Start()
59
60# run the algorithms on each event
61for iBatch, batch in enumerate(hp.iterate([inFile],banks=banks,step=step)):
62
63 for iEvent, pxs in enumerate(batch['REC::Particle_px']):
64
65 # verbose printout
66 print(f'evnum = {batch["RUN::config_event"][iEvent][0]}')
67 # print(f'iBatch={iBatch}, iEvent={iEvent}')
68 # for key in batch:
69 # print(key,':',batch[key][iEvent])
70
71 # loop over particles
72 for row, _px in enumerate(pxs):
73 pid = batch['REC::Particle_pid'][iEvent][row]
74
75 # check the PID with EventBuilderFilter
76 if(algo_eventbuilder_filter.Filter(pid)):
77
78 # get the sector for this particle; this is using a vector action function, so
79 # many of its arguments are full arrays
80 sector = algo_sector_finder.GetStandardSector(
81 batch['REC::Track_sector'][iEvent],
82 batch['REC::Track_pindex'][iEvent],
83 batch['REC::Calorimeter_sector'][iEvent],
84 batch['REC::Calorimeter_pindex'][iEvent],
85 batch['REC::Scintillator_sector'][iEvent],
86 batch['REC::Scintillator_pindex'][iEvent],
87 row)
88
89 # correct the particle momentum
90 p_corrected = algo_momentum_correction.Transform(
91 batch['REC::Particle_px'][iEvent][row],
92 batch['REC::Particle_py'][iEvent][row],
93 batch['REC::Particle_pz'][iEvent][row],
94 sector,
95 pid,
96 batch['RUN::config_torus'][iEvent][0]
97 )
98
99 # then print the result
100 print(f'Analysis Particle PDG = {pid}')
101 print(f' sector = {sector}')
102 print(f' p_old = ({batch["REC::Particle_px"][iEvent][row]:11.5f}, {batch["REC::Particle_py"][iEvent][row]:11.5f}, {batch["REC::Particle_pz"][iEvent][row]:11.5f})')
103 print(f' p_new = ({p_corrected.px:11.5f}, {p_corrected.py:11.5f}, {p_corrected.pz:11.5f})')
104
105 # End iteration if maximum number of batches reached
106 if (iBatch+1>=nbatches): break
107
108# stop the algorithms
109algo_eventbuilder_filter.Stop()
110algo_sector_finder.Stop()
111algo_momentum_correction.Stop()
112
113"""!@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