From 673addaace88e7d8639b9278d653e1af195ac7d2 Mon Sep 17 00:00:00 2001 From: Christopher Dilks Date: Tue, 29 Oct 2024 19:42:38 -0400 Subject: [PATCH 01/12] fix: centralize creator banks and set unique IDs --- src/iguana/algorithms/Algorithm.cc | 17 +++++++ src/iguana/algorithms/Algorithm.h | 10 ++++ src/iguana/algorithms/BankDefs.h | 49 +++++++++++++++++++ src/iguana/algorithms/meson.build | 1 + .../physics/InclusiveKinematics/Algorithm.cc | 9 +--- 5 files changed, 78 insertions(+), 8 deletions(-) create mode 100644 src/iguana/algorithms/BankDefs.h diff --git a/src/iguana/algorithms/Algorithm.cc b/src/iguana/algorithms/Algorithm.cc index b6f37a27..b49b2f0a 100644 --- a/src/iguana/algorithms/Algorithm.cc +++ b/src/iguana/algorithms/Algorithm.cc @@ -1,4 +1,5 @@ #include "Algorithm.h" +#include "BankDefs.h" #include @@ -248,6 +249,22 @@ namespace iguana { return bank_schema; } + hipo::schema Algorithm::CreateBankNew( + hipo::banklist& banks, + hipo::banklist::size_type& idx, + std::string const& bank_name) const noexcept(false) + { + for(auto const& bank_def : bank_defs) { + if(bank_def.name == bank_name) { + std::vector bank_cols; + for(auto const& entry : bank_def.entries) + bank_cols.push_back(entry.name + "/" + entry.type); + return CreateBank(banks, idx, bank_name, bank_cols, bank_def.group, bank_def.item); + } + } + throw std::runtime_error(fmt::format("bank {:?} not found in 'BankDefs.h'", bank_name)); + } + /////////////////////////////////////////////////////////////////////////////// void Algorithm::ShowBanks(hipo::banklist& banks, std::string_view message, Logger::Level const level) const diff --git a/src/iguana/algorithms/Algorithm.h b/src/iguana/algorithms/Algorithm.h index 1700ca5c..7e67703e 100644 --- a/src/iguana/algorithms/Algorithm.h +++ b/src/iguana/algorithms/Algorithm.h @@ -169,6 +169,16 @@ namespace iguana { int group_id, // FIXME: generalize group_id and item_id setting int item_id) const noexcept(false); + /// Create a new bank and push it to the bank list. The bank must be defined in `BankDefs.h`. + /// @param [out] banks the `hipo::banklist` onto which the new bank will be pushed + /// @param [out] bank_idx will be set to the `hipo::banklist` index of the new bank + /// @param [in] bank_name the new bank name + /// @returns the bank's schema + hipo::schema CreateBankNew( + hipo::banklist& banks, + hipo::banklist::size_type& idx, + std::string const& bank_name) const noexcept(false); + /// Dump all banks in a `hipo::banklist` /// @param banks the banks to show /// @param message if specified, print a header message diff --git a/src/iguana/algorithms/BankDefs.h b/src/iguana/algorithms/BankDefs.h new file mode 100644 index 00000000..1fca1b40 --- /dev/null +++ b/src/iguana/algorithms/BankDefs.h @@ -0,0 +1,49 @@ +#include +#include + + +// FIXME +// This really ought to be a YAML file, however: +// - could be handled by `ConfigFileReader`, but need to prevent users from accidentally overriding this +// file with `IGUANA_CONFIG_PATH` +// - we could generate this header file, given a YAML file, with Python (not Ruby, since Meson already needs Python, but Ruby +// is only needed for Chameleon) + +namespace iguana { + + struct BankColDef { + std::string name; + std::string type; + }; + + struct BankDef { + std::string name; + int group; + int item; + std::vector entries; + }; + + std::vector const bank_defs{ + { + .name = "physics::InclusiveKinematics", + .group = 30000, + .item = 1, + .entries = { + { .name = "pindex", .type = "S" }, + { .name = "Q2", .type = "D"}, + { .name = "x", .type = "D"}, + { .name = "y", .type = "D"}, + { .name = "W", .type = "D"}, + { .name = "nu", .type = "D"}, + { .name = "qx", .type = "D"}, + { .name = "qy", .type = "D"}, + { .name = "qz", .type = "D"}, + { .name = "qE", .type = "D"}, + { .name = "beamPz", .type = "D"}, + { .name = "targetM", .type = "D"} + } + } + }; + +} + diff --git a/src/iguana/algorithms/meson.build b/src/iguana/algorithms/meson.build index 7ac67bcb..8693c2e4 100644 --- a/src/iguana/algorithms/meson.build +++ b/src/iguana/algorithms/meson.build @@ -110,6 +110,7 @@ algo_headers = [ 'Algorithm.h', 'AlgorithmBoilerplate.h', 'TypeDefs.h', + 'BankDefs.h', 'AlgorithmSequence.h', ] if ROOT_dep.found() diff --git a/src/iguana/algorithms/physics/InclusiveKinematics/Algorithm.cc b/src/iguana/algorithms/physics/InclusiveKinematics/Algorithm.cc index 2d008db7..019bb30a 100644 --- a/src/iguana/algorithms/physics/InclusiveKinematics/Algorithm.cc +++ b/src/iguana/algorithms/physics/InclusiveKinematics/Algorithm.cc @@ -13,14 +13,7 @@ namespace iguana::physics { b_config = GetBankIndex(banks, "RUN::config"); // create the output bank - // FIXME: generalize the groupid and itemid - auto result_schema = CreateBank( - banks, - b_result, - GetClassName(), - {"pindex/S", "Q2/D", "x/D", "y/D", "W/D", "nu/D", "qx/D", "qy/D", "qz/D", "qE/D", "beamPz/D", "targetM/D"}, - 0xF000, - 1); + auto result_schema = CreateBankNew(banks, b_result, GetClassName()); i_pindex = result_schema.getEntryOrder("pindex"); i_Q2 = result_schema.getEntryOrder("Q2"); i_x = result_schema.getEntryOrder("x"); From a7a36a07d0b95c2de2a182f01ecf417f998362be Mon Sep 17 00:00:00 2001 From: Christopher Dilks Date: Wed, 30 Oct 2024 19:16:58 -0400 Subject: [PATCH 02/12] feat: switch to JSON format --- src/iguana/algorithms/BankDefs.h | 7 ----- src/iguana/bankdefs/generate.py | 44 +++++++++++++++++++++++++++ src/iguana/bankdefs/iguana_banks.json | 22 ++++++++++++++ 3 files changed, 66 insertions(+), 7 deletions(-) create mode 100755 src/iguana/bankdefs/generate.py create mode 100644 src/iguana/bankdefs/iguana_banks.json diff --git a/src/iguana/algorithms/BankDefs.h b/src/iguana/algorithms/BankDefs.h index 1fca1b40..c094d9d0 100644 --- a/src/iguana/algorithms/BankDefs.h +++ b/src/iguana/algorithms/BankDefs.h @@ -2,13 +2,6 @@ #include -// FIXME -// This really ought to be a YAML file, however: -// - could be handled by `ConfigFileReader`, but need to prevent users from accidentally overriding this -// file with `IGUANA_CONFIG_PATH` -// - we could generate this header file, given a YAML file, with Python (not Ruby, since Meson already needs Python, but Ruby -// is only needed for Chameleon) - namespace iguana { struct BankColDef { diff --git a/src/iguana/bankdefs/generate.py b/src/iguana/bankdefs/generate.py new file mode 100755 index 00000000..180e9d06 --- /dev/null +++ b/src/iguana/bankdefs/generate.py @@ -0,0 +1,44 @@ +#!/usr/bin/env python3 + +import sys +import json + +if(len(sys.argv) < 3): + print(f'USAGE {__file__} [INPUT_JSON] [OUTPUT]') + exit(2) +input_file_name = sys.argv[1] +output_file_name = sys.argv[2] + +def trailing_comma(arr, idx): + if(idx < len(arr)): + return ',' + else: + return '' + +with open(input_file_name) as input_file: + + try: + bank_defs = json.load(input_file) + + print(' std::vector const bank_defs{') + i_bank_def = 0 + for bank_def in bank_defs: + i_bank_def += 1 + trail_bank_def = trailing_comma(bank_defs, i_bank_def) + print(f' {{') + print(f' .name = "{bank_def["name"]}",') + print(f' .group = {bank_def["group"]},') + print(f' .item = {bank_def["item"]},') + print(f' .entries = {{') + i_entry = 0 + for entry in bank_def['entries']: + i_entry += 1 + trail_entry = trailing_comma(bank_def['entries'], i_entry) + print(f' {{ .name = "{entry["name"]}", .type = "{entry["type"]}" }}{trail_entry}') + print(f' }}') + print(f' }}{trail_bank_def}') + print(' };') + + except json.decoder.JSONDecodeError: + print(f'ERROR: failed to parse {input_file_name}; check its JSON syntax', file=sys.stderr) + exit(1) diff --git a/src/iguana/bankdefs/iguana_banks.json b/src/iguana/bankdefs/iguana_banks.json new file mode 100644 index 00000000..e1c48fbd --- /dev/null +++ b/src/iguana/bankdefs/iguana_banks.json @@ -0,0 +1,22 @@ +[ + { + "name": "physics::InclusiveKinematics", + "group": 30000, + "item": 1, + "info": "", + "entries": [ + { "name": "pindex", "type": "S", "info": ""}, + { "name": "Q2", "type": "D", "info": ""}, + { "name": "x", "type": "D", "info": ""}, + { "name": "y", "type": "D", "info": ""}, + { "name": "W", "type": "D", "info": ""}, + { "name": "nu", "type": "D", "info": ""}, + { "name": "qx", "type": "D", "info": ""}, + { "name": "qy", "type": "D", "info": ""}, + { "name": "qz", "type": "D", "info": ""}, + { "name": "qE", "type": "D", "info": ""}, + { "name": "beamPz", "type": "D", "info": ""}, + { "name": "targetM", "type": "D", "info": ""} + ] + } +] From 570843ba52ea49b6e379c1e7b12d26b88980a8d8 Mon Sep 17 00:00:00 2001 From: Christopher Dilks Date: Sat, 30 Nov 2024 01:13:18 -0500 Subject: [PATCH 03/12] refactor: all created banks are now in the JSON file --- src/iguana/algorithms/Algorithm.cc | 48 +++++++------------ src/iguana/algorithms/Algorithm.h | 18 +------ src/iguana/algorithms/BankDefs.h | 24 +--------- .../clas12/SectorFinder/Algorithm.cc | 3 +- .../physics/DihadronKinematics/Algorithm.cc | 22 +-------- .../physics/InclusiveKinematics/Algorithm.cc | 2 +- .../SingleHadronKinematics/Algorithm.cc | 18 +------ src/iguana/bankdefs/generate.py | 45 ++++++++++++----- src/iguana/bankdefs/iguana_banks.json | 48 ++++++++++++++++++- 9 files changed, 103 insertions(+), 125 deletions(-) diff --git a/src/iguana/algorithms/Algorithm.cc b/src/iguana/algorithms/Algorithm.cc index b49b2f0a..867eed28 100644 --- a/src/iguana/algorithms/Algorithm.cc +++ b/src/iguana/algorithms/Algorithm.cc @@ -222,44 +222,30 @@ namespace iguana { /////////////////////////////////////////////////////////////////////////////// hipo::schema Algorithm::CreateBank( - hipo::banklist& banks, - hipo::banklist::size_type& bank_idx, - std::string const& bank_name, - std::vector schema_def, - int group_id, - int item_id) const - { - if(!AlgorithmFactory::QueryNewBank(bank_name)) { - m_log->Error("{:?} creates bank {:?}, which is not registered; new banks must be included in `REGISTER_IGUANA_ALGORITHM` arguments", m_class_name, bank_name); - throw std::runtime_error("CreateBank failed"); - } - if(schema_def.empty()) { - m_log->Error("empty schema_def in CreateBank"); - throw std::runtime_error("CreateBank failed"); - } - hipo::schema bank_schema(bank_name.c_str(), group_id, item_id); - bank_schema.parse(std::accumulate( - std::next(schema_def.begin()), - schema_def.end(), - schema_def[0], - [](std::string a, std::string b) - { return a + "," + b; })); - banks.push_back({bank_schema}); - bank_idx = GetBankIndex(banks, bank_name); - return bank_schema; - } - - hipo::schema Algorithm::CreateBankNew( hipo::banklist& banks, hipo::banklist::size_type& idx, std::string const& bank_name) const noexcept(false) { + // loop over bank definitions for(auto const& bank_def : bank_defs) { if(bank_def.name == bank_name) { - std::vector bank_cols; + // make sure the new bank is in REGISTER_IGUANA_ALGORITHM + if(!AlgorithmFactory::QueryNewBank(bank_name)) { + m_log->Error("{:?} creates bank {:?}, which is not registered; new banks must be included in `REGISTER_IGUANA_ALGORITHM` arguments", m_class_name, bank_name); + throw std::runtime_error("CreateBank failed"); + } + // create the schema format string + std::vector schema_def; for(auto const& entry : bank_def.entries) - bank_cols.push_back(entry.name + "/" + entry.type); - return CreateBank(banks, idx, bank_name, bank_cols, bank_def.group, bank_def.item); + schema_def.push_back(entry.name + "/" + entry.type); + auto format_string = fmt::format("{}", fmt::join(schema_def, ",")); + // create the new bank schema + hipo::schema bank_schema(bank_name.c_str(), bank_def.group, bank_def.item); + bank_schema.parse(format_string); + // create the new bank + banks.push_back({bank_schema}); + idx = GetBankIndex(banks, bank_name); + return bank_schema; } } throw std::runtime_error(fmt::format("bank {:?} not found in 'BankDefs.h'", bank_name)); diff --git a/src/iguana/algorithms/Algorithm.h b/src/iguana/algorithms/Algorithm.h index 7e67703e..c9df17a1 100644 --- a/src/iguana/algorithms/Algorithm.h +++ b/src/iguana/algorithms/Algorithm.h @@ -153,28 +153,12 @@ namespace iguana { /// returns the `hipo::banklist` index of the bank hipo::banklist::size_type GetBankIndex(hipo::banklist& banks, std::string const& bank_name) const noexcept(false); - /// Create a new bank and push it to the bank list - /// @param [out] banks the `hipo::banklist` onto which the new bank will be pushed - /// @param [out] bank_idx will be set to the `hipo::banklist` index of the new bank - /// @param [in] bank_name the new bank name - /// @param [in] schema_def a list of variables for the schema - /// @param [in] group_id the group ID for the schema - /// @param [in] item_id the item ID for the schema - /// @returns the bank's schema - hipo::schema CreateBank( - hipo::banklist& banks, - hipo::banklist::size_type& bank_idx, - std::string const& bank_name, - std::vector schema_def, - int group_id, // FIXME: generalize group_id and item_id setting - int item_id) const noexcept(false); - /// Create a new bank and push it to the bank list. The bank must be defined in `BankDefs.h`. /// @param [out] banks the `hipo::banklist` onto which the new bank will be pushed /// @param [out] bank_idx will be set to the `hipo::banklist` index of the new bank /// @param [in] bank_name the new bank name /// @returns the bank's schema - hipo::schema CreateBankNew( + hipo::schema CreateBank( hipo::banklist& banks, hipo::banklist::size_type& idx, std::string const& bank_name) const noexcept(false); diff --git a/src/iguana/algorithms/BankDefs.h b/src/iguana/algorithms/BankDefs.h index c094d9d0..8369ca0d 100644 --- a/src/iguana/algorithms/BankDefs.h +++ b/src/iguana/algorithms/BankDefs.h @@ -1,7 +1,6 @@ #include #include - namespace iguana { struct BankColDef { @@ -16,27 +15,6 @@ namespace iguana { std::vector entries; }; - std::vector const bank_defs{ - { - .name = "physics::InclusiveKinematics", - .group = 30000, - .item = 1, - .entries = { - { .name = "pindex", .type = "S" }, - { .name = "Q2", .type = "D"}, - { .name = "x", .type = "D"}, - { .name = "y", .type = "D"}, - { .name = "W", .type = "D"}, - { .name = "nu", .type = "D"}, - { .name = "qx", .type = "D"}, - { .name = "qy", .type = "D"}, - { .name = "qz", .type = "D"}, - { .name = "qE", .type = "D"}, - { .name = "beamPz", .type = "D"}, - { .name = "targetM", .type = "D"} - } - } - }; + static std::vector const bank_defs; } - diff --git a/src/iguana/algorithms/clas12/SectorFinder/Algorithm.cc b/src/iguana/algorithms/clas12/SectorFinder/Algorithm.cc index 0adfa93b..7cce65a2 100644 --- a/src/iguana/algorithms/clas12/SectorFinder/Algorithm.cc +++ b/src/iguana/algorithms/clas12/SectorFinder/Algorithm.cc @@ -43,8 +43,7 @@ namespace iguana::clas12 { } // create the output bank - // FIXME: generalize the groupid and itemid - auto result_schema = CreateBank(banks, b_result, "REC::Particle::Sector", {"sector/I","pindex/S"}, 0xF000, 4); + auto result_schema = CreateBank(banks, b_result, "REC::Particle::Sector"); i_sector = result_schema.getEntryOrder("sector"); i_pindex = result_schema.getEntryOrder("pindex"); } diff --git a/src/iguana/algorithms/physics/DihadronKinematics/Algorithm.cc b/src/iguana/algorithms/physics/DihadronKinematics/Algorithm.cc index ecbecf05..c589ad13 100644 --- a/src/iguana/algorithms/physics/DihadronKinematics/Algorithm.cc +++ b/src/iguana/algorithms/physics/DihadronKinematics/Algorithm.cc @@ -13,27 +13,7 @@ namespace iguana::physics { b_inc_kin = GetBankIndex(banks, "physics::InclusiveKinematics"); // create the output bank - // FIXME: generalize the groupid and itemid - auto result_schema = CreateBank( - banks, - b_result, - GetClassName(), - { - "pindex_a/S", - "pindex_b/S", - "pdg_a/I", - "pdg_b/I", - "Mh/D", - "z/D", - "PhPerp/D", - "MX/D", - "xF/D", - "phiH/D", - "phiR/D", - "theta/D" - }, - 0xF000, - 5); + auto result_schema = CreateBank(banks, b_result, GetClassName()); i_pindex_a = result_schema.getEntryOrder("pindex_a"); i_pindex_b = result_schema.getEntryOrder("pindex_b"); i_pdg_a = result_schema.getEntryOrder("pdg_a"); diff --git a/src/iguana/algorithms/physics/InclusiveKinematics/Algorithm.cc b/src/iguana/algorithms/physics/InclusiveKinematics/Algorithm.cc index 019bb30a..3d9920cc 100644 --- a/src/iguana/algorithms/physics/InclusiveKinematics/Algorithm.cc +++ b/src/iguana/algorithms/physics/InclusiveKinematics/Algorithm.cc @@ -13,7 +13,7 @@ namespace iguana::physics { b_config = GetBankIndex(banks, "RUN::config"); // create the output bank - auto result_schema = CreateBankNew(banks, b_result, GetClassName()); + auto result_schema = CreateBank(banks, b_result, GetClassName()); i_pindex = result_schema.getEntryOrder("pindex"); i_Q2 = result_schema.getEntryOrder("Q2"); i_x = result_schema.getEntryOrder("x"); diff --git a/src/iguana/algorithms/physics/SingleHadronKinematics/Algorithm.cc b/src/iguana/algorithms/physics/SingleHadronKinematics/Algorithm.cc index 4b05acfe..d84aecf5 100644 --- a/src/iguana/algorithms/physics/SingleHadronKinematics/Algorithm.cc +++ b/src/iguana/algorithms/physics/SingleHadronKinematics/Algorithm.cc @@ -14,23 +14,7 @@ namespace iguana::physics { b_inc_kin = GetBankIndex(banks, "physics::InclusiveKinematics"); // create the output bank - // FIXME: generalize the groupid and itemid - auto result_schema = CreateBank( - banks, - b_result, - GetClassName(), - { - "pindex/S", - "pdg/I", - "z/D", - "PhPerp/D", - "MX/D", - "xF/D", - "phiH/D", - "xi/D" - }, - 0xF000, - 7); + auto result_schema = CreateBank(banks, b_result, GetClassName()); i_pindex = result_schema.getEntryOrder("pindex"); i_pdg = result_schema.getEntryOrder("pdg"); i_z = result_schema.getEntryOrder("z"); diff --git a/src/iguana/bankdefs/generate.py b/src/iguana/bankdefs/generate.py index 180e9d06..57bded11 100755 --- a/src/iguana/bankdefs/generate.py +++ b/src/iguana/bankdefs/generate.py @@ -1,7 +1,6 @@ #!/usr/bin/env python3 -import sys -import json +import sys, json, textwrap if(len(sys.argv) < 3): print(f'USAGE {__file__} [INPUT_JSON] [OUTPUT]') @@ -20,25 +19,47 @@ def trailing_comma(arr, idx): try: bank_defs = json.load(input_file) - print(' std::vector const bank_defs{') + out = open(output_file_name, 'w') + out.write(textwrap.dedent(f'''\ + #include "iguana/algorithms/BankDefs.h" + + namespace iguana {{ + std::vector const bank_defs = {{ + ''')) + i_bank_def = 0 + unique_item_ids = [] for bank_def in bank_defs: + i_bank_def += 1 trail_bank_def = trailing_comma(bank_defs, i_bank_def) - print(f' {{') - print(f' .name = "{bank_def["name"]}",') - print(f' .group = {bank_def["group"]},') - print(f' .item = {bank_def["item"]},') - print(f' .entries = {{') + + if(bank_def["item"] in unique_item_ids): + print(f'ERROR: item ID {bank_def["item"]} is not unique in {input_file_name}', file=sys.stderr) + exit(1) + unique_item_ids.append(bank_def["item"]) + + out.write(textwrap.indent(textwrap.dedent(f'''\ + {{ + .name = "{bank_def["name"]}", + .group = {bank_def["group"]}, + .item = {bank_def["item"]}, + .entries = {{ + '''), ' ')) i_entry = 0 for entry in bank_def['entries']: i_entry += 1 trail_entry = trailing_comma(bank_def['entries'], i_entry) - print(f' {{ .name = "{entry["name"]}", .type = "{entry["type"]}" }}{trail_entry}') - print(f' }}') - print(f' }}{trail_bank_def}') - print(' };') + out.write(f' {{ .name = "{entry["name"]}", .type = "{entry["type"]}" }}{trail_entry}\n') + out.write(f' }}\n') + out.write(f' }}{trail_bank_def}\n') + + out.write(' };\n') + out.write('}\n') + out.close() except json.decoder.JSONDecodeError: print(f'ERROR: failed to parse {input_file_name}; check its JSON syntax', file=sys.stderr) exit(1) + +print(f'Generated {output_file_name}') diff --git a/src/iguana/bankdefs/iguana_banks.json b/src/iguana/bankdefs/iguana_banks.json index e1c48fbd..5a48ea38 100644 --- a/src/iguana/bankdefs/iguana_banks.json +++ b/src/iguana/bankdefs/iguana_banks.json @@ -1,9 +1,19 @@ [ { - "name": "physics::InclusiveKinematics", + "name": "REC::Particle::Sector", "group": 30000, "item": 1, "info": "", + "entries": [ + { "name": "pindex", "type": "S", "info": ""}, + { "name": "sector", "type": "I", "info": ""} + ] + }, + { + "name": "physics::InclusiveKinematics", + "group": 30000, + "item": 2, + "info": "", "entries": [ { "name": "pindex", "type": "S", "info": ""}, { "name": "Q2", "type": "D", "info": ""}, @@ -18,5 +28,41 @@ { "name": "beamPz", "type": "D", "info": ""}, { "name": "targetM", "type": "D", "info": ""} ] + }, + { + "name": "physics::SingleHadronKinematics", + "group": 30000, + "item": 3, + "info": "", + "entries": [ + { "name": "pindex", "type": "S", "info": ""}, + { "name": "pdg", "type": "I", "info": ""}, + { "name": "z", "type": "D", "info": ""}, + { "name": "PhPerp", "type": "D", "info": ""}, + { "name": "MX", "type": "D", "info": ""}, + { "name": "xF", "type": "D", "info": ""}, + { "name": "phiH", "type": "D", "info": ""}, + { "name": "xi", "type": "D", "info": ""} + ] + }, + { + "name": "physics::DihadronKinematics", + "group": 30000, + "item": 4, + "info": "", + "entries": [ + { "name": "pindex_a", "type": "S", "info": ""}, + { "name": "pindex_b", "type": "S", "info": ""}, + { "name": "pdg_a", "type": "I", "info": ""}, + { "name": "pdg_b", "type": "I", "info": ""}, + { "name": "Mh", "type": "D", "info": ""}, + { "name": "z", "type": "D", "info": ""}, + { "name": "PhPerp", "type": "D", "info": ""}, + { "name": "MX", "type": "D", "info": ""}, + { "name": "xF", "type": "D", "info": ""}, + { "name": "phiH", "type": "D", "info": ""}, + { "name": "phiR", "type": "D", "info": ""}, + { "name": "theta", "type": "D", "info": ""} + ] } ] From f8d4bd90997449862d495027885ba5c0a486f7d7 Mon Sep 17 00:00:00 2001 From: Christopher Dilks Date: Sat, 30 Nov 2024 02:04:42 -0500 Subject: [PATCH 04/12] build: generate BankDefs.cpp --- meson.build | 1 + src/iguana/algorithms/Algorithm.cc | 8 +++----- src/iguana/algorithms/Algorithm.h | 2 +- src/iguana/algorithms/BankDefs.h | 11 ++++++++++- src/iguana/algorithms/meson.build | 1 + .../bankdefs/{generate.py => bank_def_gen.py} | 2 +- .../{iguana_banks.json => iguana.json} | 0 src/iguana/bankdefs/meson.build | 19 +++++++++++++++++++ 8 files changed, 36 insertions(+), 8 deletions(-) rename src/iguana/bankdefs/{generate.py => bank_def_gen.py} (97%) rename src/iguana/bankdefs/{iguana_banks.json => iguana.json} (100%) create mode 100644 src/iguana/bankdefs/meson.build diff --git a/meson.build b/meson.build index 50a6bb97..9b2c97a0 100644 --- a/meson.build +++ b/meson.build @@ -209,6 +209,7 @@ endif # build and install shared libraries subdir('src/iguana/services') +subdir('src/iguana/bankdefs') subdir('src/iguana/algorithms') subdir('src/iguana/tests') diff --git a/src/iguana/algorithms/Algorithm.cc b/src/iguana/algorithms/Algorithm.cc index 867eed28..81fe8226 100644 --- a/src/iguana/algorithms/Algorithm.cc +++ b/src/iguana/algorithms/Algorithm.cc @@ -1,8 +1,6 @@ #include "Algorithm.h" #include "BankDefs.h" -#include - namespace iguana { void Algorithm::Start() @@ -223,11 +221,11 @@ namespace iguana { hipo::schema Algorithm::CreateBank( hipo::banklist& banks, - hipo::banklist::size_type& idx, + hipo::banklist::size_type& bank_idx, std::string const& bank_name) const noexcept(false) { // loop over bank definitions - for(auto const& bank_def : bank_defs) { + for(auto const& bank_def : BANK_DEFS) { if(bank_def.name == bank_name) { // make sure the new bank is in REGISTER_IGUANA_ALGORITHM if(!AlgorithmFactory::QueryNewBank(bank_name)) { @@ -244,7 +242,7 @@ namespace iguana { bank_schema.parse(format_string); // create the new bank banks.push_back({bank_schema}); - idx = GetBankIndex(banks, bank_name); + bank_idx = GetBankIndex(banks, bank_name); return bank_schema; } } diff --git a/src/iguana/algorithms/Algorithm.h b/src/iguana/algorithms/Algorithm.h index c9df17a1..751e7eac 100644 --- a/src/iguana/algorithms/Algorithm.h +++ b/src/iguana/algorithms/Algorithm.h @@ -160,7 +160,7 @@ namespace iguana { /// @returns the bank's schema hipo::schema CreateBank( hipo::banklist& banks, - hipo::banklist::size_type& idx, + hipo::banklist::size_type& bank_idx, std::string const& bank_name) const noexcept(false); /// Dump all banks in a `hipo::banklist` diff --git a/src/iguana/algorithms/BankDefs.h b/src/iguana/algorithms/BankDefs.h index 8369ca0d..c6e91cb0 100644 --- a/src/iguana/algorithms/BankDefs.h +++ b/src/iguana/algorithms/BankDefs.h @@ -3,18 +3,27 @@ namespace iguana { + /// A bank column struct BankColDef { + /// @brief the name of the column std::string name; + /// @brief the type of the column std::string type; }; + /// The definition of a bank struct BankDef { + /// @brief the name of the bank std::string name; + /// @brief the group ID of the bank int group; + /// @brief the item ID of the bank int item; + /// @brief the set of columns std::vector entries; }; - static std::vector const bank_defs; + /// Definitions of banks that Iguana algorithms may create + extern std::vector const BANK_DEFS; } diff --git a/src/iguana/algorithms/meson.build b/src/iguana/algorithms/meson.build index 8693c2e4..ccaf44c6 100644 --- a/src/iguana/algorithms/meson.build +++ b/src/iguana/algorithms/meson.build @@ -105,6 +105,7 @@ algo_sources = [ 'Algorithm.cc', 'AlgorithmFactory.cc', 'AlgorithmSequence.cc', + bankdef_tgt[0], ] algo_headers = [ 'Algorithm.h', diff --git a/src/iguana/bankdefs/generate.py b/src/iguana/bankdefs/bank_def_gen.py similarity index 97% rename from src/iguana/bankdefs/generate.py rename to src/iguana/bankdefs/bank_def_gen.py index 57bded11..dc5f3341 100755 --- a/src/iguana/bankdefs/generate.py +++ b/src/iguana/bankdefs/bank_def_gen.py @@ -24,7 +24,7 @@ def trailing_comma(arr, idx): #include "iguana/algorithms/BankDefs.h" namespace iguana {{ - std::vector const bank_defs = {{ + std::vector const BANK_DEFS = {{ ''')) i_bank_def = 0 diff --git a/src/iguana/bankdefs/iguana_banks.json b/src/iguana/bankdefs/iguana.json similarity index 100% rename from src/iguana/bankdefs/iguana_banks.json rename to src/iguana/bankdefs/iguana.json diff --git a/src/iguana/bankdefs/meson.build b/src/iguana/bankdefs/meson.build new file mode 100644 index 00000000..46e10044 --- /dev/null +++ b/src/iguana/bankdefs/meson.build @@ -0,0 +1,19 @@ +bankdef_json = files('iguana.json') + +prog_bankdef_gen_sources = files('bank_def_gen.py') +prog_bankdef_gen = find_program(prog_bankdef_gen_sources) + +# generate BankDefs.cpp from the JSON data model file +bankdef_tgt = custom_target( + 'bankdefs', + input: [ + bankdef_json, + prog_bankdef_gen_sources, + meson.project_source_root() / 'src' / 'iguana' / 'algorithms' / 'BankDefs.h', + ], + output: [ 'BankDefs.cpp' ], + command: [ prog_bankdef_gen, '@INPUT0@', '@OUTPUT0@' ], +) + +# install the JSON data model file; iguana won't need it, but it can be useful for user reference +install_data(bankdef_json, install_dir: project_etc / 'bankdefs' / 'hipo4') From ebf1ff3f74cedb6bfda35c72cb8251d7f32e724b Mon Sep 17 00:00:00 2001 From: Christopher Dilks Date: Sat, 30 Nov 2024 02:33:01 -0500 Subject: [PATCH 05/12] doc: explain --- src/iguana/bankdefs/README.md | 5 +++++ src/iguana/bankdefs/{bank_def_gen.py => bankgen.py} | 7 +++++++ src/iguana/bankdefs/iguana.json | 8 ++++---- src/iguana/bankdefs/meson.build | 8 ++++---- 4 files changed, 20 insertions(+), 8 deletions(-) create mode 100644 src/iguana/bankdefs/README.md rename src/iguana/bankdefs/{bank_def_gen.py => bankgen.py} (87%) diff --git a/src/iguana/bankdefs/README.md b/src/iguana/bankdefs/README.md new file mode 100644 index 00000000..73d565b4 --- /dev/null +++ b/src/iguana/bankdefs/README.md @@ -0,0 +1,5 @@ +# Iguana Bank Definitions + +Creator algorithms create new banks; the files in this directory describe the new banks, in particular, [`iguana.json`](iguana.json). + +This `json` file follows the same format as the `coatjava` bank definitions and may be used as a reference; this `json` file is also installed in `etc/iguana/bankdefs/`. diff --git a/src/iguana/bankdefs/bank_def_gen.py b/src/iguana/bankdefs/bankgen.py similarity index 87% rename from src/iguana/bankdefs/bank_def_gen.py rename to src/iguana/bankdefs/bankgen.py index dc5f3341..6bb9b6ce 100755 --- a/src/iguana/bankdefs/bank_def_gen.py +++ b/src/iguana/bankdefs/bankgen.py @@ -1,5 +1,12 @@ #!/usr/bin/env python3 +""" +Reads an input JSON data model file, which defines the bank schema, +and generates a C++ source file setting the variable `iguana::BANK_DEFS`. +Doing this allows us to embed the bank schema definition in a library, +rather than having the library find the JSON file at runtime. +""" + import sys, json, textwrap if(len(sys.argv) < 3): diff --git a/src/iguana/bankdefs/iguana.json b/src/iguana/bankdefs/iguana.json index 5a48ea38..da1972f1 100644 --- a/src/iguana/bankdefs/iguana.json +++ b/src/iguana/bankdefs/iguana.json @@ -3,7 +3,7 @@ "name": "REC::Particle::Sector", "group": 30000, "item": 1, - "info": "", + "info": "The bank created by clas12::SectorFinder", "entries": [ { "name": "pindex", "type": "S", "info": ""}, { "name": "sector", "type": "I", "info": ""} @@ -13,7 +13,7 @@ "name": "physics::InclusiveKinematics", "group": 30000, "item": 2, - "info": "", + "info": "The bank created by physics::InclusiveKinematics", "entries": [ { "name": "pindex", "type": "S", "info": ""}, { "name": "Q2", "type": "D", "info": ""}, @@ -33,7 +33,7 @@ "name": "physics::SingleHadronKinematics", "group": 30000, "item": 3, - "info": "", + "info": "The bank created by physics::SingleHadronKinematics", "entries": [ { "name": "pindex", "type": "S", "info": ""}, { "name": "pdg", "type": "I", "info": ""}, @@ -49,7 +49,7 @@ "name": "physics::DihadronKinematics", "group": 30000, "item": 4, - "info": "", + "info": "The bank created by physics::DihadronKinematics", "entries": [ { "name": "pindex_a", "type": "S", "info": ""}, { "name": "pindex_b", "type": "S", "info": ""}, diff --git a/src/iguana/bankdefs/meson.build b/src/iguana/bankdefs/meson.build index 46e10044..faa7373c 100644 --- a/src/iguana/bankdefs/meson.build +++ b/src/iguana/bankdefs/meson.build @@ -1,18 +1,18 @@ bankdef_json = files('iguana.json') -prog_bankdef_gen_sources = files('bank_def_gen.py') -prog_bankdef_gen = find_program(prog_bankdef_gen_sources) +prog_bankgen_sources = files('bankgen.py') +prog_bankgen = find_program(prog_bankgen_sources) # generate BankDefs.cpp from the JSON data model file bankdef_tgt = custom_target( 'bankdefs', input: [ bankdef_json, - prog_bankdef_gen_sources, + prog_bankgen_sources, meson.project_source_root() / 'src' / 'iguana' / 'algorithms' / 'BankDefs.h', ], output: [ 'BankDefs.cpp' ], - command: [ prog_bankdef_gen, '@INPUT0@', '@OUTPUT0@' ], + command: [ prog_bankgen, '@INPUT0@', '@OUTPUT0@' ], ) # install the JSON data model file; iguana won't need it, but it can be useful for user reference From e855b88eba5b0194ba48eb47d6ec95b9077a6c12 Mon Sep 17 00:00:00 2001 From: Christopher Dilks Date: Sat, 30 Nov 2024 02:36:26 -0500 Subject: [PATCH 06/12] doc: link to main page --- doc/gen/mainpage.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/doc/gen/mainpage.md b/doc/gen/mainpage.md index 2159bb17..16215833 100644 --- a/doc/gen/mainpage.md +++ b/doc/gen/mainpage.md @@ -32,6 +32,9 @@ The algorithm types are defined based on what they do to HIPO bank data: **Creator** Create a new bank +The definitions of the new banks that are created by **Creator** algorithms are found in: +- [**Iguana Bank Definitions**](https://github.com/JeffersonLab/iguana/tree/main/src/iguana/bankdefs) + Most algorithms are configurable: - [**Algorithm Configuration Guide**](https://github.com/JeffersonLab/iguana/blob/main/doc/configuration.md) From b0b4419325efe1b4a72cf19d7a0a30a5b4d3974b Mon Sep 17 00:00:00 2001 From: Christopher Dilks Date: Sat, 30 Nov 2024 02:39:17 -0500 Subject: [PATCH 07/12] fix: misleading docstring --- src/iguana/algorithms/Algorithm.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/iguana/algorithms/Algorithm.h b/src/iguana/algorithms/Algorithm.h index 751e7eac..760f9a2d 100644 --- a/src/iguana/algorithms/Algorithm.h +++ b/src/iguana/algorithms/Algorithm.h @@ -153,7 +153,7 @@ namespace iguana { /// returns the `hipo::banklist` index of the bank hipo::banklist::size_type GetBankIndex(hipo::banklist& banks, std::string const& bank_name) const noexcept(false); - /// Create a new bank and push it to the bank list. The bank must be defined in `BankDefs.h`. + /// Create a new bank and push it to the bank list. The bank must be defined in `BANK_DEFS`, which is generated at build time from `src/iguana/bankdefs/iguana.json` /// @param [out] banks the `hipo::banklist` onto which the new bank will be pushed /// @param [out] bank_idx will be set to the `hipo::banklist` index of the new bank /// @param [in] bank_name the new bank name From 2fbb8b7f12336cf6ccfcbe3419e1821d8be23ea5 Mon Sep 17 00:00:00 2001 From: Christopher Dilks Date: Sat, 30 Nov 2024 02:52:22 -0500 Subject: [PATCH 08/12] doc: more clarification --- src/iguana/algorithms/BankDefs.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/iguana/algorithms/BankDefs.h b/src/iguana/algorithms/BankDefs.h index c6e91cb0..48df2425 100644 --- a/src/iguana/algorithms/BankDefs.h +++ b/src/iguana/algorithms/BankDefs.h @@ -23,7 +23,8 @@ namespace iguana { std::vector entries; }; - /// Definitions of banks that Iguana algorithms may create + /// Definitions of banks that Iguana algorithms may create; this variable is + /// defined in code generated by `src/iguana/bankdefs/bankgen.py` extern std::vector const BANK_DEFS; } From 1236031c9a9ace215a52521f546e76253966d04a Mon Sep 17 00:00:00 2001 From: Christopher Dilks Date: Sun, 1 Dec 2024 01:54:54 -0500 Subject: [PATCH 09/12] doc: include the JSON file --- doc/gen/Doxyfile.in | 3 ++- doc/gen/mainpage.md | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/doc/gen/Doxyfile.in b/doc/gen/Doxyfile.in index c923bc87..042a88e0 100644 --- a/doc/gen/Doxyfile.in +++ b/doc/gen/Doxyfile.in @@ -968,7 +968,8 @@ INPUT = @top_srcdir@/src/ \ @top_srcdir@/bind/ \ @top_srcdir@/doc/gen/ \ @top_srcdir@/doc/gen/mainpage.md \ - @top_srcdir@/examples/ + @top_srcdir@/examples/ \ + @top_srcdir@/src/iguana/bankdefs/iguana.json # This tag can be used to specify the character encoding of the source files # that doxygen parses. Internally doxygen uses the UTF-8 encoding. Doxygen uses diff --git a/doc/gen/mainpage.md b/doc/gen/mainpage.md index 16215833..98c2262d 100644 --- a/doc/gen/mainpage.md +++ b/doc/gen/mainpage.md @@ -33,7 +33,7 @@ The algorithm types are defined based on what they do to HIPO bank data: The definitions of the new banks that are created by **Creator** algorithms are found in: -- [**Iguana Bank Definitions**](https://github.com/JeffersonLab/iguana/tree/main/src/iguana/bankdefs) +- \link src/iguana/bankdefs/iguana.json **Iguana Bank Definitions:** `iguana.json` \endlink Most algorithms are configurable: - [**Algorithm Configuration Guide**](https://github.com/JeffersonLab/iguana/blob/main/doc/configuration.md) From 6ef420a204c90f981fb581204bf3a98f637f01c6 Mon Sep 17 00:00:00 2001 From: Christopher Dilks Date: Sun, 1 Dec 2024 03:43:13 -0500 Subject: [PATCH 10/12] feat: generate the structs too --- src/iguana/algorithms/Algorithm.cc | 1 - src/iguana/algorithms/Algorithm.h | 4 +- src/iguana/algorithms/BankDefs.h | 30 ----- src/iguana/algorithms/meson.build | 3 +- .../physics/DihadronKinematics/Algorithm.h | 31 ----- .../physics/InclusiveKinematics/Algorithm.h | 28 ----- .../SingleHadronKinematics/Algorithm.h | 21 ---- src/iguana/bankdefs/bankgen.py | 116 +++++++++++++++--- src/iguana/bankdefs/iguana.json | 16 ++- src/iguana/bankdefs/meson.build | 9 +- 10 files changed, 117 insertions(+), 142 deletions(-) delete mode 100644 src/iguana/algorithms/BankDefs.h diff --git a/src/iguana/algorithms/Algorithm.cc b/src/iguana/algorithms/Algorithm.cc index 81fe8226..a559006a 100644 --- a/src/iguana/algorithms/Algorithm.cc +++ b/src/iguana/algorithms/Algorithm.cc @@ -1,5 +1,4 @@ #include "Algorithm.h" -#include "BankDefs.h" namespace iguana { diff --git a/src/iguana/algorithms/Algorithm.h b/src/iguana/algorithms/Algorithm.h index 760f9a2d..4c9e34cd 100644 --- a/src/iguana/algorithms/Algorithm.h +++ b/src/iguana/algorithms/Algorithm.h @@ -6,8 +6,8 @@ #include - -#include "iguana/algorithms/AlgorithmBoilerplate.h" +#include "AlgorithmBoilerplate.h" +#include "iguana/bankdefs/BankDefs.h" #include "iguana/services/YAMLReader.h" #include diff --git a/src/iguana/algorithms/BankDefs.h b/src/iguana/algorithms/BankDefs.h deleted file mode 100644 index 48df2425..00000000 --- a/src/iguana/algorithms/BankDefs.h +++ /dev/null @@ -1,30 +0,0 @@ -#include -#include - -namespace iguana { - - /// A bank column - struct BankColDef { - /// @brief the name of the column - std::string name; - /// @brief the type of the column - std::string type; - }; - - /// The definition of a bank - struct BankDef { - /// @brief the name of the bank - std::string name; - /// @brief the group ID of the bank - int group; - /// @brief the item ID of the bank - int item; - /// @brief the set of columns - std::vector entries; - }; - - /// Definitions of banks that Iguana algorithms may create; this variable is - /// defined in code generated by `src/iguana/bankdefs/bankgen.py` - extern std::vector const BANK_DEFS; - -} diff --git a/src/iguana/algorithms/meson.build b/src/iguana/algorithms/meson.build index ccaf44c6..ecdd204a 100644 --- a/src/iguana/algorithms/meson.build +++ b/src/iguana/algorithms/meson.build @@ -105,13 +105,12 @@ algo_sources = [ 'Algorithm.cc', 'AlgorithmFactory.cc', 'AlgorithmSequence.cc', - bankdef_tgt[0], + bankdef_tgt[1], # BankDefs.cc ] algo_headers = [ 'Algorithm.h', 'AlgorithmBoilerplate.h', 'TypeDefs.h', - 'BankDefs.h', 'AlgorithmSequence.h', ] if ROOT_dep.found() diff --git a/src/iguana/algorithms/physics/DihadronKinematics/Algorithm.h b/src/iguana/algorithms/physics/DihadronKinematics/Algorithm.h index f1d75814..0c000215 100644 --- a/src/iguana/algorithms/physics/DihadronKinematics/Algorithm.h +++ b/src/iguana/algorithms/physics/DihadronKinematics/Algorithm.h @@ -7,37 +7,6 @@ namespace iguana::physics { - /// Set of dihadron kinematics variables - struct DihadronKinematicsVars { - /// @brief `REC::Particle` row (`pindex`) of hadron A - int pindex_a; - /// @brief `REC::Particle` row (`pindex`) of hadron B - int pindex_b; - /// @brief PDG code of hadron A - int pdg_a; - /// @brief PDG code of hadron B - int pdg_b; - /// @brief @latex{M_h}: Invariant mass of the dihadron - double Mh; - /// @brief @latex{z}: Momentum fraction of the fragmenting parton carried by the dihadron - double z; - /// @brief @latex{P_h^\perp}: transverse momentum of the dihadron in the @latex{\perp}-frame (transverse to @latex{\vec{q}}) - double PhPerp; - /// @brief @latex{M_X(ehhX)}: Missing mass of the dihadron - double MX; - /// @brief @latex{x_F}: Feynman-x of the dihadron - double xF; - /// @brief @latex{\phi_h}: @latex{q}-azimuthal angle between the lepton-scattering plane and the @latex{\vec{q}\times\vec{P}_h} plane; - /// if the value is `tools::UNDEF`, the calculation failed - double phiH; - /// @brief @latex{\phi_R}: @latex{q}-azimuthal angle between the lepton-scattering plane and dihadron plane; - /// if the value is `tools::UNDEF`, the calculation failed - double phiR; - /// @brief @latex{\theta}: The "decay" angle of hadron A in the dihadron rest frame, with respect; - /// to the dihadron momentum direction - double theta; - }; - /// @brief_algo Calculate semi-inclusive dihadron kinematic quantities defined in `iguana::physics::DihadronKinematicsVars` /// /// @begin_doc_algo{physics::DihadronKinematics | Creator} diff --git a/src/iguana/algorithms/physics/InclusiveKinematics/Algorithm.h b/src/iguana/algorithms/physics/InclusiveKinematics/Algorithm.h index c60c3e91..fead3fbb 100644 --- a/src/iguana/algorithms/physics/InclusiveKinematics/Algorithm.h +++ b/src/iguana/algorithms/physics/InclusiveKinematics/Algorithm.h @@ -7,34 +7,6 @@ namespace iguana::physics { - /// Set of inclusive kinematics variables - struct InclusiveKinematicsVars { - /// @brief `REC::Particle` row (`pindex`) of the scattered electron - int pindex; - /// @brief @latex{x}-component of virtual photon momentum @latex{q} - vector_element_t qx; - /// @brief @latex{y}-component of virtual photon momentum @latex{q} - vector_element_t qy; - /// @brief @latex{z}-component of virtual photon momentum @latex{q} - vector_element_t qz; - /// @brief @latex{E}-component of virtual photon momentum @latex{q} - vector_element_t qE; - /// @brief @latex{Q^2} (GeV@latex{^2}) - double Q2; - /// @brief @latex{x_B} - double x; - /// @brief @latex{y} - double y; - /// @brief @latex{W} (GeV) - double W; - /// @brief @latex{\nu} - double nu; - /// @brief beam momentum @latex{z}-component (GeV) - double beamPz; - /// @brief target mass (GeV) - double targetM; - }; - /// @brief_algo Calculate inclusive kinematics quantities defined in `iguana::physics::InclusiveKinematicsVars` /// /// @begin_doc_algo{physics::InclusiveKinematics | Creator} diff --git a/src/iguana/algorithms/physics/SingleHadronKinematics/Algorithm.h b/src/iguana/algorithms/physics/SingleHadronKinematics/Algorithm.h index d9fac73b..07356745 100644 --- a/src/iguana/algorithms/physics/SingleHadronKinematics/Algorithm.h +++ b/src/iguana/algorithms/physics/SingleHadronKinematics/Algorithm.h @@ -6,27 +6,6 @@ namespace iguana::physics { - /// Set of hadron kinematics variables - struct SingleHadronKinematicsVars { - /// @brief `REC::Particle` row (`pindex`) of the hadron - int pindex; - /// @brief PDG code of the hadron - int pdg; - /// @brief @latex{z}: Momentum fraction of the fragmenting parton carried by the hadron - double z; - /// @brief @latex{P_h^\perp}: transverse momentum of the hadron in the @latex{\perp}-frame (transverse to @latex{\vec{q}}) - double PhPerp; - /// @brief @latex{M_X(ehX)}: Missing mass of the hadron - double MX; - /// @brief @latex{x_F}: Feynman-x of the hadron - double xF; - /// @brief @latex{\phi_h}: @latex{q}-azimuthal angle between the lepton-scattering plane and the @latex{\vec{q}\times\vec{P}_h} plane; - /// if the value is `tools::UNDEF`, the calculation failed - double phiH; - /// @brief @latex{\xi_h}: Longitudinal momentum fraction of the nucleon carried by the hadron - double xi; - }; - /// @brief_algo Calculate semi-inclusive hadron kinematic quantities defined in `iguana::physics::SingleHadronKinematicsVars` /// /// @begin_doc_algo{physics::SingleHadronKinematics | Creator} diff --git a/src/iguana/bankdefs/bankgen.py b/src/iguana/bankdefs/bankgen.py index 6bb9b6ce..6c31ab9c 100755 --- a/src/iguana/bankdefs/bankgen.py +++ b/src/iguana/bankdefs/bankgen.py @@ -1,39 +1,93 @@ #!/usr/bin/env python3 """ -Reads an input JSON data model file, which defines the bank schema, -and generates a C++ source file setting the variable `iguana::BANK_DEFS`. -Doing this allows us to embed the bank schema definition in a library, -rather than having the library find the JSON file at runtime. +Reads an input JSON data model file, which defines the bank schema, and +generates C++ source code for handling these banks in Iguana. """ import sys, json, textwrap +# parse arguments if(len(sys.argv) < 3): - print(f'USAGE {__file__} [INPUT_JSON] [OUTPUT]') + print(f'USAGE {__file__} [INPUT_JSON] [OUTPUT_BASENAME]') exit(2) input_file_name = sys.argv[1] -output_file_name = sys.argv[2] +output_base_name = sys.argv[2] +# return a comma if `idx` does not point to the last element of list `arr` def trailing_comma(arr, idx): if(idx < len(arr)): return ',' else: return '' +# map `type` character to C++ type +type_dict = { + 'B': 'int', + 'S': 'int', + 'I': 'int', + 'F': 'float', + 'D': 'double', + 'L': 'long', +} + +# all iguana banks should have this group ID +iguana_group_id = 30000 + +# open the JSON file with open(input_file_name) as input_file: try: bank_defs = json.load(input_file) - out = open(output_file_name, 'w') - out.write(textwrap.dedent(f'''\ - #include "iguana/algorithms/BankDefs.h" + # start the output C++ files + out_h = open(f'{output_base_name}.h', 'w') + out_cc = open(f'{output_base_name}.cc', 'w') + + # start the header file with some common structs + out_h.write(textwrap.dedent('''\ + #include + #include + + namespace iguana { + + /// A bank column + struct BankColDef { + /// @brief the name of the column + std::string name; + /// @brief the type of the column + std::string type; + }; + + /// The definition of a bank + struct BankDef { + /// @brief the name of the bank + std::string name; + /// @brief the group ID of the bank + int group; + /// @brief the item ID of the bank + int item; + /// @brief the set of columns + std::vector entries; + }; + + /// Definitions of banks that Iguana algorithms may create; this variable is + /// defined in code generated by `src/iguana/bankdefs/bankgen.py` + extern std::vector const BANK_DEFS; + + } + + ''')) + + # the `.cc` source file will define `BANK_DEFS`; start its definition + out_cc.write(textwrap.dedent(f'''\ + #include "BankDefs.h" namespace iguana {{ std::vector const BANK_DEFS = {{ ''')) + # loop over bank definitions in the JSON file i_bank_def = 0 unique_item_ids = [] for bank_def in bank_defs: @@ -41,12 +95,17 @@ def trailing_comma(arr, idx): i_bank_def += 1 trail_bank_def = trailing_comma(bank_defs, i_bank_def) + # make sure the item ID is unique and the group ID is a expected if(bank_def["item"] in unique_item_ids): print(f'ERROR: item ID {bank_def["item"]} is not unique in {input_file_name}', file=sys.stderr) exit(1) unique_item_ids.append(bank_def["item"]) + if(bank_def["group"] != iguana_group_id): + print(f'ERROR: all group IDs must be {iguana_group_id}', file=sys.stderr) + exit(1) - out.write(textwrap.indent(textwrap.dedent(f'''\ + # append this bank to `BANK_DEFS` + out_cc.write(textwrap.indent(textwrap.dedent(f'''\ {{ .name = "{bank_def["name"]}", .group = {bank_def["group"]}, @@ -57,16 +116,39 @@ def trailing_comma(arr, idx): for entry in bank_def['entries']: i_entry += 1 trail_entry = trailing_comma(bank_def['entries'], i_entry) - out.write(f' {{ .name = "{entry["name"]}", .type = "{entry["type"]}" }}{trail_entry}\n') - out.write(f' }}\n') - out.write(f' }}{trail_bank_def}\n') + out_cc.write(f' {{ .name = "{entry["name"]}", .type = "{entry["type"]}" }}{trail_entry}\n') + out_cc.write(f' }}\n') + out_cc.write(f' }}{trail_bank_def}\n') + + # make a struct for this algorithm's action function output + algo_name = bank_def["algorithm"] + namespace_name = '::'.join(['iguana', *algo_name.split('::')[0:-1]]) + struct_name = algo_name.split('::')[-1] + 'Vars' + out_h.write(textwrap.dedent(f'''\ + namespace {namespace_name} {{ + /// Set of variables created by creator algorithm `iguana::{algo_name}` + struct {struct_name} {{ + ''')) + for entry in bank_def['entries']: + if entry["type"] in type_dict: + out_h.write(textwrap.indent(textwrap.dedent(f'''\ + /// @brief {entry["info"]} + {type_dict[entry["type"]]} {entry["name"]}; + '''), ' ')) + else: + print(f'ERROR: bank entry type "{entry["type"]}" is unknown', file=sys.stderr) + exit(1) + out_h.write(' };\n') + out_h.write('}\n\n') - out.write(' };\n') - out.write('}\n') - out.close() + out_cc.write(' };\n') + out_cc.write('}\n') + out_cc.close() + out_h.close() except json.decoder.JSONDecodeError: print(f'ERROR: failed to parse {input_file_name}; check its JSON syntax', file=sys.stderr) exit(1) -print(f'Generated {output_file_name}') +# for ext in ['h', 'cc']: +# print(f'Generated {output_base_name}.{ext}') diff --git a/src/iguana/bankdefs/iguana.json b/src/iguana/bankdefs/iguana.json index da1972f1..301c77fd 100644 --- a/src/iguana/bankdefs/iguana.json +++ b/src/iguana/bankdefs/iguana.json @@ -3,17 +3,19 @@ "name": "REC::Particle::Sector", "group": 30000, "item": 1, - "info": "The bank created by clas12::SectorFinder", + "info": "", + "algorithm": "clas12::SectorFinder", "entries": [ - { "name": "pindex", "type": "S", "info": ""}, - { "name": "sector", "type": "I", "info": ""} + { "name": "pindex", "type": "S", "info": "row number in the particle bank"}, + { "name": "sector", "type": "I", "info": "sector for this particle"} ] }, { "name": "physics::InclusiveKinematics", "group": 30000, "item": 2, - "info": "The bank created by physics::InclusiveKinematics", + "info": "", + "algorithm": "physics::InclusiveKinematics", "entries": [ { "name": "pindex", "type": "S", "info": ""}, { "name": "Q2", "type": "D", "info": ""}, @@ -33,7 +35,8 @@ "name": "physics::SingleHadronKinematics", "group": 30000, "item": 3, - "info": "The bank created by physics::SingleHadronKinematics", + "info": "", + "algorithm": "physics::SingleHadronKinematics", "entries": [ { "name": "pindex", "type": "S", "info": ""}, { "name": "pdg", "type": "I", "info": ""}, @@ -49,7 +52,8 @@ "name": "physics::DihadronKinematics", "group": 30000, "item": 4, - "info": "The bank created by physics::DihadronKinematics", + "info": "", + "algorithm": "physics::DihadronKinematics", "entries": [ { "name": "pindex_a", "type": "S", "info": ""}, { "name": "pindex_b", "type": "S", "info": ""}, diff --git a/src/iguana/bankdefs/meson.build b/src/iguana/bankdefs/meson.build index faa7373c..664ec46d 100644 --- a/src/iguana/bankdefs/meson.build +++ b/src/iguana/bankdefs/meson.build @@ -3,16 +3,17 @@ bankdef_json = files('iguana.json') prog_bankgen_sources = files('bankgen.py') prog_bankgen = find_program(prog_bankgen_sources) -# generate BankDefs.cpp from the JSON data model file +# generate BankDefs.{h,cc} from the JSON data model file bankdef_tgt = custom_target( 'bankdefs', input: [ bankdef_json, prog_bankgen_sources, - meson.project_source_root() / 'src' / 'iguana' / 'algorithms' / 'BankDefs.h', ], - output: [ 'BankDefs.cpp' ], - command: [ prog_bankgen, '@INPUT0@', '@OUTPUT0@' ], + output: [ 'BankDefs.h', 'BankDefs.cc' ], + command: [ prog_bankgen, '@INPUT0@', '@OUTDIR@/BankDefs' ], + install: true, + install_dir: [ get_option('includedir') / meson.project_name() / 'bankdefs', false ], ) # install the JSON data model file; iguana won't need it, but it can be useful for user reference From 100da72fccf6843cfde42f661813dd79e3836d53 Mon Sep 17 00:00:00 2001 From: Christopher Dilks Date: Sun, 1 Dec 2024 03:57:07 -0500 Subject: [PATCH 11/12] doc: fill out entry info --- doc/gen/Doxyfile.in | 1 + src/iguana/bankdefs/iguana.json | 68 ++++++++++++++++----------------- 2 files changed, 35 insertions(+), 34 deletions(-) diff --git a/doc/gen/Doxyfile.in b/doc/gen/Doxyfile.in index 042a88e0..1385851b 100644 --- a/doc/gen/Doxyfile.in +++ b/doc/gen/Doxyfile.in @@ -965,6 +965,7 @@ WARN_LOGFILE = INPUT = @top_srcdir@/src/ \ @top_builddir@/src/iguana/algorithms \ + @top_builddir@/src/iguana/bankdefs \ @top_srcdir@/bind/ \ @top_srcdir@/doc/gen/ \ @top_srcdir@/doc/gen/mainpage.md \ diff --git a/src/iguana/bankdefs/iguana.json b/src/iguana/bankdefs/iguana.json index 301c77fd..385cee7d 100644 --- a/src/iguana/bankdefs/iguana.json +++ b/src/iguana/bankdefs/iguana.json @@ -6,8 +6,8 @@ "info": "", "algorithm": "clas12::SectorFinder", "entries": [ - { "name": "pindex", "type": "S", "info": "row number in the particle bank"}, - { "name": "sector", "type": "I", "info": "sector for this particle"} + { "name": "pindex", "type": "S", "info": "row number in the particle bank" }, + { "name": "sector", "type": "I", "info": "sector for this particle" } ] }, { @@ -17,18 +17,18 @@ "info": "", "algorithm": "physics::InclusiveKinematics", "entries": [ - { "name": "pindex", "type": "S", "info": ""}, - { "name": "Q2", "type": "D", "info": ""}, - { "name": "x", "type": "D", "info": ""}, - { "name": "y", "type": "D", "info": ""}, - { "name": "W", "type": "D", "info": ""}, - { "name": "nu", "type": "D", "info": ""}, - { "name": "qx", "type": "D", "info": ""}, - { "name": "qy", "type": "D", "info": ""}, - { "name": "qz", "type": "D", "info": ""}, - { "name": "qE", "type": "D", "info": ""}, - { "name": "beamPz", "type": "D", "info": ""}, - { "name": "targetM", "type": "D", "info": ""} + { "name": "pindex", "type": "S", "info": "`REC::Particle` row (`pindex`) of the scattered electron" }, + { "name": "Q2", "type": "D", "info": "@latex{Q^2} (GeV@latex{^2})" }, + { "name": "x", "type": "D", "info": "@latex{x_B}" }, + { "name": "y", "type": "D", "info": "@latex{y}" }, + { "name": "W", "type": "D", "info": "@latex{W} (GeV)" }, + { "name": "nu", "type": "D", "info": "@latex{\\nu}" }, + { "name": "qx", "type": "D", "info": "@latex{x}-component of virtual photon momentum @latex{q}" }, + { "name": "qy", "type": "D", "info": "@latex{y}-component of virtual photon momentum @latex{q}" }, + { "name": "qz", "type": "D", "info": "@latex{z}-component of virtual photon momentum @latex{q}" }, + { "name": "qE", "type": "D", "info": "@latex{E}-component of virtual photon momentum @latex{q}" }, + { "name": "beamPz", "type": "D", "info": "beam momentum @latex{z}-component (GeV)" }, + { "name": "targetM", "type": "D", "info": "target mass (GeV)" } ] }, { @@ -38,14 +38,14 @@ "info": "", "algorithm": "physics::SingleHadronKinematics", "entries": [ - { "name": "pindex", "type": "S", "info": ""}, - { "name": "pdg", "type": "I", "info": ""}, - { "name": "z", "type": "D", "info": ""}, - { "name": "PhPerp", "type": "D", "info": ""}, - { "name": "MX", "type": "D", "info": ""}, - { "name": "xF", "type": "D", "info": ""}, - { "name": "phiH", "type": "D", "info": ""}, - { "name": "xi", "type": "D", "info": ""} + { "name": "pindex", "type": "S", "info": "`REC::Particle` row (`pindex`) of the hadron" }, + { "name": "pdg", "type": "I", "info": "PDG code of the hadron" }, + { "name": "z", "type": "D", "info": "@latex{z}: Momentum fraction of the fragmenting parton carried by the hadron" }, + { "name": "PhPerp", "type": "D", "info": "@latex{P_h^\\perp}: transverse momentum of the hadron in the @latex{\\perp}-frame (transverse to @latex{\\vec{q}})" }, + { "name": "MX", "type": "D", "info": "@latex{M_X(ehX)}: Missing mass of the hadron" }, + { "name": "xF", "type": "D", "info": "@latex{x_F}: Feynman-x of the hadron" }, + { "name": "phiH", "type": "D", "info": "@latex{\\phi_h}: @latex{q}-azimuthal angle between the lepton-scattering plane and the @latex{\\vec{q}\\times\\vec{P}_h} plane; if the value is `tools::UNDEF`, the calculation failed" }, + { "name": "xi", "type": "D", "info": "@latex{\\xi_h}: Longitudinal momentum fraction of the nucleon carried by the hadron" } ] }, { @@ -55,18 +55,18 @@ "info": "", "algorithm": "physics::DihadronKinematics", "entries": [ - { "name": "pindex_a", "type": "S", "info": ""}, - { "name": "pindex_b", "type": "S", "info": ""}, - { "name": "pdg_a", "type": "I", "info": ""}, - { "name": "pdg_b", "type": "I", "info": ""}, - { "name": "Mh", "type": "D", "info": ""}, - { "name": "z", "type": "D", "info": ""}, - { "name": "PhPerp", "type": "D", "info": ""}, - { "name": "MX", "type": "D", "info": ""}, - { "name": "xF", "type": "D", "info": ""}, - { "name": "phiH", "type": "D", "info": ""}, - { "name": "phiR", "type": "D", "info": ""}, - { "name": "theta", "type": "D", "info": ""} + { "name": "pindex_a", "type": "S", "info": "`REC::Particle` row (`pindex`) of hadron A" }, + { "name": "pindex_b", "type": "S", "info": "`REC::Particle` row (`pindex`) of hadron B" }, + { "name": "pdg_a", "type": "I", "info": "PDG code of hadron A" }, + { "name": "pdg_b", "type": "I", "info": "PDG code of hadron B" }, + { "name": "Mh", "type": "D", "info": "@latex{M_h}: Invariant mass of the dihadron" }, + { "name": "z", "type": "D", "info": "@latex{z}: Momentum fraction of the fragmenting parton carried by the dihadron" }, + { "name": "PhPerp", "type": "D", "info": "@latex{P_h^\\perp}: transverse momentum of the dihadron in the @latex{\\perp}-frame (transverse to @latex{\\vec{q}})" }, + { "name": "MX", "type": "D", "info": "@latex{M_X(ehhX)}: Missing mass of the dihadron" }, + { "name": "xF", "type": "D", "info": "@latex{x_F}: Feynman-x of the dihadron" }, + { "name": "phiH", "type": "D", "info": "@latex{\\phi_h}: @latex{q}-azimuthal angle between the lepton-scattering plane and the @latex{\\vec{q}\\times\\vec{P}_h} plane; if the value is `tools::UNDEF`, the calculation failed" }, + { "name": "phiR", "type": "D", "info": "@latex{\\phi_R}: @latex{q}-azimuthal angle between the lepton-scattering plane and dihadron plane; if the value is `tools::UNDEF`, the calculation failed" }, + { "name": "theta", "type": "D", "info": "@latex{\\theta}: The 'decay' angle of hadron A in the dihadron rest frame, with respect; to the dihadron momentum direction" } ] } ] From 5f18c7d1ab40d63cfc07ed4c8baa428112e9ce3e Mon Sep 17 00:00:00 2001 From: Christopher Dilks Date: Thu, 12 Dec 2024 09:55:53 -0500 Subject: [PATCH 12/12] fix: respect PR #308 --- src/iguana/bankdefs/iguana.json | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/iguana/bankdefs/iguana.json b/src/iguana/bankdefs/iguana.json index 385cee7d..a7a210c7 100644 --- a/src/iguana/bankdefs/iguana.json +++ b/src/iguana/bankdefs/iguana.json @@ -42,8 +42,9 @@ { "name": "pdg", "type": "I", "info": "PDG code of the hadron" }, { "name": "z", "type": "D", "info": "@latex{z}: Momentum fraction of the fragmenting parton carried by the hadron" }, { "name": "PhPerp", "type": "D", "info": "@latex{P_h^\\perp}: transverse momentum of the hadron in the @latex{\\perp}-frame (transverse to @latex{\\vec{q}})" }, - { "name": "MX", "type": "D", "info": "@latex{M_X(ehX)}: Missing mass of the hadron" }, + { "name": "MX2", "type": "D", "info": "@latex{M_X^2(ehX)}: Missing mass squared of the hadron" }, { "name": "xF", "type": "D", "info": "@latex{x_F}: Feynman-x of the hadron" }, + { "name": "yB", "type": "D", "info": "@latex{y_{h,B}}: Breit frame rapidity of the hadron" }, { "name": "phiH", "type": "D", "info": "@latex{\\phi_h}: @latex{q}-azimuthal angle between the lepton-scattering plane and the @latex{\\vec{q}\\times\\vec{P}_h} plane; if the value is `tools::UNDEF`, the calculation failed" }, { "name": "xi", "type": "D", "info": "@latex{\\xi_h}: Longitudinal momentum fraction of the nucleon carried by the hadron" } ] @@ -62,8 +63,9 @@ { "name": "Mh", "type": "D", "info": "@latex{M_h}: Invariant mass of the dihadron" }, { "name": "z", "type": "D", "info": "@latex{z}: Momentum fraction of the fragmenting parton carried by the dihadron" }, { "name": "PhPerp", "type": "D", "info": "@latex{P_h^\\perp}: transverse momentum of the dihadron in the @latex{\\perp}-frame (transverse to @latex{\\vec{q}})" }, - { "name": "MX", "type": "D", "info": "@latex{M_X(ehhX)}: Missing mass of the dihadron" }, + { "name": "MX2", "type": "D", "info": "@latex{M_X^2(ehhX)}: Missing mass squared of the dihadron" }, { "name": "xF", "type": "D", "info": "@latex{x_F}: Feynman-x of the dihadron" }, + { "name": "yB", "type": "D", "info": "@latex{y_{h,B}}: Breit frame rapidity of the dihadron" }, { "name": "phiH", "type": "D", "info": "@latex{\\phi_h}: @latex{q}-azimuthal angle between the lepton-scattering plane and the @latex{\\vec{q}\\times\\vec{P}_h} plane; if the value is `tools::UNDEF`, the calculation failed" }, { "name": "phiR", "type": "D", "info": "@latex{\\phi_R}: @latex{q}-azimuthal angle between the lepton-scattering plane and dihadron plane; if the value is `tools::UNDEF`, the calculation failed" }, { "name": "theta", "type": "D", "info": "@latex{\\theta}: The 'decay' angle of hadron A in the dihadron rest frame, with respect; to the dihadron momentum direction" }