Skip to content

Commit

Permalink
feat: optionally depend on ROOT, and use it for boosting in `LorentzT…
Browse files Browse the repository at this point in the history
…ransformer` (#109)
  • Loading branch information
c-dilks authored Feb 22, 2024
1 parent 6853069 commit ec55e14
Show file tree
Hide file tree
Showing 15 changed files with 295 additions and 152 deletions.
1 change: 1 addition & 0 deletions .github/install-dependency-packages.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ GENERAL_PACKAGE_LIST_LINUX=(
make
cmake
tree
which
pkgconf
ninja
meson
Expand Down
9 changes: 4 additions & 5 deletions bind/python/iguana-example-00-basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,17 @@
numEvents = int(sys.argv[2]) if len(sys.argv)>2 else 3

reader = hipo.reader(inFile)
banks = reader.getBanks(["REC::Particle", "REC::Calorimeter"]);
banks = reader.getBanks(["REC::Particle", "RUN::config"]);

seq = iguana.AlgorithmSequence('pyiguana')
seq.Add('clas12::EventBuilderFilter')
seq.Add('clas12::LorentzTransformer')
seq.Add('clas12::MomentumCorrection')
seq.PrintSequence()

seq.SetOption('clas12::EventBuilderFilter', 'log', 'debug')
seq.SetOption('clas12::LorentzTransformer', 'log', 'debug')
seq.SetOption('clas12::EventBuilderFilter', 'log', 'debug')
seq.SetOption('clas12::MomentumCorrection', 'log', 'debug')

seq.SetOption('clas12::EventBuilderFilter', 'pids', [11, 211, -211])
seq.SetOption('clas12::LorentzTransformer', 'frame', 'mirror')

def prettyPrint(message, bank):
print(f'{"="*30} {message} {"="*30}')
Expand Down
22 changes: 13 additions & 9 deletions bind/python/iguana-example-01-bank-rows.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
pyiguana.include(
'hipo4/reader.h',
'iguana/algorithms/clas12/EventBuilderFilter.h',
'iguana/algorithms/clas12/LorentzTransformer.h',
'iguana/algorithms/clas12/MomentumCorrection.h',
)
# then import the bound namespaces (must be after including the headers)
from cppyy.gbl import hipo, iguana
Expand All @@ -17,41 +17,45 @@
numEvents = int(sys.argv[2]) if len(sys.argv)>2 else 3

reader = hipo.reader(inFile)
banks = reader.getBanks(["REC::Particle", "REC::Calorimeter"]);
banks = reader.getBanks(["REC::Particle", "RUN::config"]);

algo_eventbuilder_filter = iguana.clas12.EventBuilderFilter()
algo_lorentz_transformer = iguana.clas12.LorentzTransformer()
algo_momentum_correction = iguana.clas12.MomentumCorrection()

algo_eventbuilder_filter.SetOption('log', 'debug')
algo_lorentz_transformer.SetOption('log', 'debug')
algo_momentum_correction.SetOption('log', 'debug')
algo_eventbuilder_filter.SetOption('pids', [11, 211, -211])
algo_lorentz_transformer.SetOption('frame', 'mirror')

algo_eventbuilder_filter.Start()
algo_lorentz_transformer.Start()
algo_momentum_correction.Start()

iEvent = 0
while(reader.next(banks) and (numEvents==0 or iEvent < numEvents)):
iEvent += 1

particleBank = banks[0]
configBank = banks[1]
particleBank.show()

for row in range(particleBank.getRows()):

pid = particleBank.getInt('pid', row)
if(algo_eventbuilder_filter.Filter(pid)):

px, py, pz, e = algo_lorentz_transformer.Transform(
sector = 1 # FIXME: get the sector number

px, py, pz, = algo_momentum_correction.Transform(
particleBank.getFloat("px", row),
particleBank.getFloat("py", row),
particleBank.getFloat("pz", row),
0.0,
sector,
pid,
configBank.getFloat("torus", 0)
)

print(f'Accepted PID {pid}:')
print(f' p_old = ({particleBank.getFloat("px", row)}, {particleBank.getFloat("py", row)}, {particleBank.getFloat("pz", row)})')
print(f' p_new = ({px}, {py}, {pz})')

algo_eventbuilder_filter.Stop()
algo_lorentz_transformer.Stop()
algo_momentum_correction.Stop()
9 changes: 4 additions & 5 deletions examples/iguana-example-00-basic.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,20 @@ int main(int argc, char **argv) {
hipo::reader reader(inFileName);

// set banks
hipo::banklist banks = reader.getBanks({ "REC::Particle", "REC::Calorimeter" });
enum banks_enum { b_particle, b_calo }; // TODO: users shouldn't have to do this
hipo::banklist banks = reader.getBanks({ "REC::Particle", "RUN::config" });
enum banks_enum { b_particle, b_config }; // TODO: users shouldn't have to do this

// iguana algorithm sequence
iguana::AlgorithmSequence seq;
seq.Add("clas12::EventBuilderFilter"); // filter by Event Builder PID
seq.Add("clas12::LorentzTransformer"); // Lorentz transform the momenta
seq.Add("clas12::MomentumCorrection"); // momentum corrections

// set log levels
seq.SetOption("clas12::EventBuilderFilter", "log", "debug");
seq.SetOption("clas12::LorentzTransformer", "log", "debug");
seq.SetOption("clas12::MomentumCorrection", "log", "debug");

// set algorithm options
seq.SetOption<std::vector<int>>("clas12::EventBuilderFilter", "pids", {11, 211, -211});
seq.SetOption("clas12::LorentzTransformer", "frame", "mirror");

// start the algorithms
seq.Start(banks);
Expand Down
26 changes: 15 additions & 11 deletions examples/iguana-example-01-bank-rows.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <iguana/algorithms/clas12/EventBuilderFilter.h>
#include <iguana/algorithms/clas12/LorentzTransformer.h>
#include <iguana/algorithms/clas12/MomentumCorrection.h>
#include <hipo4/reader.h>

int main(int argc, char **argv) {
Expand All @@ -13,31 +13,31 @@ int main(int argc, char **argv) {
hipo::reader reader(inFileName);

// set banks
hipo::banklist banks = reader.getBanks({ "REC::Particle", "REC::Calorimeter" });
enum banks_enum { b_particle, b_calo }; // TODO: users shouldn't have to do this
hipo::banklist banks = reader.getBanks({ "REC::Particle", "RUN::config" });
enum banks_enum { b_particle, b_config }; // TODO: users shouldn't have to do this

// create the algorithms
iguana::clas12::EventBuilderFilter algo_eventbuilder_filter;
iguana::clas12::LorentzTransformer algo_lorentz_transformer;
iguana::clas12::MomentumCorrection algo_momentum_correction;

// set log levels
algo_eventbuilder_filter.SetOption("log", "debug");
algo_lorentz_transformer.SetOption("log", "debug");
algo_momentum_correction.SetOption("log", "debug");

// set algorithm options
algo_eventbuilder_filter.SetOption<std::vector<int>>("pids", {11, 211, -211});
algo_lorentz_transformer.SetOption("frame", "mirror");

// start the algorithms
algo_eventbuilder_filter.Start();
algo_lorentz_transformer.Start();
algo_momentum_correction.Start();

// run the algorithm sequence on each event
int iEvent = 0;
while(reader.next(banks) && (numEvents==0 || iEvent++ < numEvents)) {

// show the particle bank
auto& particleBank = banks.at(b_particle);
auto& configBank = banks.at(b_config);
particleBank.show();

// loop over bank rows
Expand All @@ -47,12 +47,16 @@ int main(int argc, char **argv) {
auto pid = particleBank.getInt("pid", row);
if(algo_eventbuilder_filter.Filter(pid)) {

// if accepted PID, transform its momentum with LorentzTransformer
auto [px, py, pz, e] = algo_lorentz_transformer.Transform(
int sector = 1; // FIXME: get the sector number

// if accepted PID, correct its momentum
auto [px, py, pz] = algo_momentum_correction.Transform(
particleBank.getFloat("px", row),
particleBank.getFloat("py", row),
particleBank.getFloat("pz", row),
0.0 // (ignoring the energy)
sector,
pid,
configBank.getFloat("torus", 0)
);

// then print the result
Expand All @@ -70,6 +74,6 @@ int main(int argc, char **argv) {

// stop the algorithms
algo_eventbuilder_filter.Stop();
algo_lorentz_transformer.Stop();
algo_momentum_correction.Stop();
return 0;
}
3 changes: 2 additions & 1 deletion examples/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ foreach src : example_sources
executable(
src.split('.')[0],
src,
include_directories: project_inc,
include_directories: [ project_inc ] + ROOT_dep_inc_dirs,
dependencies: project_deps,
link_with: project_libs,
link_args: ROOT_dep_link_args,
install: true,
install_rpath: ':'.join(project_exe_rpath),
)
Expand Down
68 changes: 58 additions & 10 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -39,28 +39,76 @@ hipo_dep = dependency(
method: 'pkg-config',
version: '>=4.0.1',
)
ROOT_dep = dependency(
'ROOT',
required: false,
method: 'cmake',
version: '>=6.28'
)

# list of dependencies
# FIXME: for users which use LD_LIBRARY_PATH, we should try to keep this list
# ordered such that the ones users are *least likely* to try to build
# themselves are listed last (see FIXME in meson/this_iguana.sh.in)
full_dep_list = [
hipo_dep,
fmt_dep,
yamlcpp_dep,
]
dep_list = []
foreach dep : [ hipo_dep, fmt_dep, yamlcpp_dep, ROOT_dep ]
if dep.found()
dep_list += dep
endif
endforeach

# dependency libdirs
# make a list of dependency library and include directories
dep_lib_paths = []
foreach dep : full_dep_list
dep_lib_paths += dep.get_variable(pkgconfig: 'libdir')
dep_inc_paths = []
foreach dep : dep_list
### handle pkg-config deps
if dep.type_name() == 'pkgconfig'
libdirs = [ dep.get_variable(pkgconfig: 'libdir') ]
incdirs = [ dep.get_variable(pkgconfig: 'includedir') ]
### handle cmake deps
elif dep.type_name() == 'cmake'
libdirs = []
foreach lib : dep.get_variable(cmake: 'PACKAGE_LIBRARIES').split(';')
libdirs += run_command('dirname', lib, check: true).stdout().strip()
endforeach
incdirs = ROOT_dep.get_variable(cmake: 'PACKAGE_INCLUDE_DIRS').split(';')
### error, if unknown
else
error('Cannot determine how dependency "' + dep.name() + '" was found')
endif
### append to `dep_lib_paths` and `dep_inc_paths`, omitting duplicates
foreach libdir : libdirs
if not dep_lib_paths.contains(libdir)
dep_lib_paths += libdir
endif
endforeach
foreach incdir : incdirs
if not dep_inc_paths.contains(incdir)
dep_inc_paths += incdir
endif
endforeach
endforeach
message('Dependency library dirs = [', ', '.join(dep_lib_paths), ']')
message('Dependency include dirs = [', ', '.join(dep_inc_paths), ']')

# handle ROOT
ROOT_dep_inc_dirs = []
ROOT_dep_link_args = []
if ROOT_dep.found()
ROOT_dep_inc_dirs += include_directories(run_command('root-config', '--incdir', check: true).stdout().strip())
ROOT_dep_link_args += [
'-L' + run_command('root-config', '--libdir', check: true).stdout().strip(),
# ROOT libraries that we need (safer than `root-config --libs`)
'-lCore',
'-lGenVector',
]
endif

# general project vars
project_lib_rpath = '$ORIGIN'
project_inc = include_directories('src')
project_libs = []
project_deps = declare_dependency(dependencies: full_dep_list)
project_deps = declare_dependency(dependencies: [ fmt_dep, yamlcpp_dep, hipo_dep ] ) # do NOT include ROOT here
project_etc = get_option('sysconfdir') / meson.project_name()
project_pkg_vars = [
'dep_pkgconfigdirs=' + ':'.join(get_option('pkg_config_path')),
Expand Down Expand Up @@ -99,7 +147,7 @@ pkg.generate(
name: meson.project_name(),
description: project_description,
libraries: project_libs,
requires: full_dep_list,
requires: [ fmt_dep, yamlcpp_dep, hipo_dep ], # pkg-config dependencies only
variables: project_pkg_vars,
)

Expand Down
13 changes: 8 additions & 5 deletions src/iguana/algorithms/TypeDefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@

namespace iguana {

/// Lorentz vector element type, matching that of `REC::Particle` momentum components
using lorentz_element_t = float;

/// Generic Lorentz vector container type
using lorentz_vector_t = std::tuple<lorentz_element_t, lorentz_element_t, lorentz_element_t, lorentz_element_t>;
/// Vector element type
using vector_element_t = double;
/// 2-vector container type
using vector2_t = std::tuple<vector_element_t, vector_element_t>;
/// 3-vector container type
using vector3_t = std::tuple<vector_element_t, vector_element_t, vector_element_t>;
/// 4-vector container type
using vector4_t = std::tuple<vector_element_t, vector_element_t, vector_element_t, vector_element_t>;

/// Light-weight namespace for particle constants
namespace particle {
Expand Down
Loading

0 comments on commit ec55e14

Please sign in to comment.