Iguana 0.0.0
Implementation Guardian of Analysis Algorithms
Loading...
Searching...
No Matches
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@end_doc_example
8@doxygen_off
9"""
10
11import pyiguana
12import sys
13import hipopy.hipopy as hp
14import os.path as osp
15
16# include the header files that you need
17pyiguana.include(
18 'hipo4/reader.h',
19 'iguana/algorithms/clas12/EventBuilderFilter/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
25if len(sys.argv)<=1:
26 print('Usage: python3',osp.basename(__file__),' <inFile> <step> <nbatches>')
27 sys.exit(0)
28inFile = sys.argv[1] if len(sys.argv)>1 else 'data.hipo'
29step = int(sys.argv[2]) if len(sys.argv)>2 else 3
30nbatches = int(sys.argv[3]) if len(sys.argv)>3 else 1
31
32banks = ["REC::Particle", "RUN::config"]
33
34algo_eventbuilder_filter = iguana.clas12.EventBuilderFilter()
35algo_momentum_correction = iguana.clas12.MomentumCorrection()
36
37algo_eventbuilder_filter.SetOption('log', 'debug')
38algo_momentum_correction.SetOption('log', 'debug')
39algo_eventbuilder_filter.SetOption('pids', [11, 211, -211])
40
41algo_eventbuilder_filter.Start()
42algo_momentum_correction.Start()
43
44for iBatch, batch in enumerate(hp.iterate([inFile],banks=banks,step=step)):
45
46 for iEvent, pxs in enumerate(batch['REC::Particle_px']):
47 for key in batch:
48 print(key,':',batch[key][iEvent])
49
50 for row, _px in enumerate(pxs):
51 pid = batch['REC::Particle_pid'][iEvent][row]
52
53 if(algo_eventbuilder_filter.Filter(pid)):
54 sector = 1 # FIXME: get the sector number. The algorithm `clas12::SectorFinder` can do this, however
55 # it requires reading full `hipo::bank` objects, whereas this example is meant to demonstrate
56 # `iguana` usage operating _only_ on bank row elements
57
58 p_corrected = algo_momentum_correction.Transform(
59 batch['REC::Particle_px'][iEvent][row],
60 batch['REC::Particle_py'][iEvent][row],
61 batch['REC::Particle_pz'][iEvent][row],
62 sector,
63 pid,
64 batch['RUN::config_torus'][iEvent][0]
65 )
66
67 print(f'Accepted PID {pid}:')
68 print(f' p_old = ({batch["REC::Particle_px"][iEvent][row]}, {batch["REC::Particle_py"][iEvent][row]}, {batch["REC::Particle_pz"][iEvent][row]})')
69 print(f' p_new = ({p_corrected.px}, {p_corrected.py}, {p_corrected.pz})')
70
71 # End iteration if maximum number of batches reached
72 if (iBatch>=nbatches): break
73
74algo_eventbuilder_filter.Stop()
75algo_momentum_correction.Stop()
76
77"""!@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