From 9579714fae069a51bff84733752e6f6589c595b4 Mon Sep 17 00:00:00 2001 From: MarianaT27 Date: Thu, 6 Jun 2024 11:20:07 -0400 Subject: [PATCH 01/36] Added LeptonID .c --- src/iguana/algorithms/clas12/LeptonID.cc | 163 +++++++++++++++++++++ src/iguana/algorithms/clas12/LeptonID.h | 71 +++++++++ src/iguana/algorithms/clas12/LeptonID.yaml | 3 + 3 files changed, 237 insertions(+) create mode 100644 src/iguana/algorithms/clas12/LeptonID.cc create mode 100644 src/iguana/algorithms/clas12/LeptonID.h create mode 100644 src/iguana/algorithms/clas12/LeptonID.yaml diff --git a/src/iguana/algorithms/clas12/LeptonID.cc b/src/iguana/algorithms/clas12/LeptonID.cc new file mode 100644 index 000000000..e45988ba9 --- /dev/null +++ b/src/iguana/algorithms/clas12/LeptonID.cc @@ -0,0 +1,163 @@ +#include "LeptonID.h" + +#include + +namespace iguana::clas12 { + + //REGISTER_IGUANA_ALGORITHM(LeptonID); //not using it + REGISTER_IGUANA_ALGORITHM(LeptonID , "clas12::leptonID"); // this algorithm creates 1 new banks + + void LeptonID::Start(hipo::banklist& banks) + { + //Get configuration + ParseYAMLConfig(); + o_pid = GetOptionScalar("pid");//Obtain pid from config file + o_weightfile = GetOptionScalar("weightfile");//Obtain weightfile from config file + //o_exampleDouble = GetOptionScalar("exampleDouble"); + + + + //Get Banks that we are going to use + b_particle = GetBankIndex(banks, "REC::Particle"); + b_calorimeter = GetBankIndex(banks, "REC::Calorimeter"); + + //Create bank to store score (should we add the variables?) + auto leptonID_schema = CreateBank( + banks, + b_leptonID, + GetClassName(), + {"pindex/S", "score/D","p/D","theta/D","phi/D","sfpcal/D","sfecin/D","sfecout/D","m2pcal/D","m2ecin/D","m2ecout/D"}, + 0xF000, + 1); + i_pindex = leptonID_schema.getEntryOrder("pindex"); + i_score = leptonID_schema.getEntryOrder("score"); + i_p = leptonID_schema.getEntryOrder("p"); + i_theta = leptonID_schema.getEntryOrder("theta"); + i_phi = leptonID_schema.getEntryOrder("phi"); + i_sfpcal = leptonID_schema.getEntryOrder("sfpcal"); + + } + + + void LeptonID::Run(hipo::banklist& banks) const + { + auto& particleBank = GetBank(banks, b_particle, "REC::Particle"); + auto& calorimeterBank = GetBank(banks, b_calorimeter, "REC::Calorimeter"); + + + ShowBank(particleBank, Logger::Header("INPUT PARTICLES")); + + auto lepton_pindex = FindLepton(particleBank); + if(lepton_pindex < 0) { + ShowBank(leptonID_schema, Logger::Header("CREATED BANK IS EMPTY")); + return; + } + + auto lepton_vars=GetLeptonIDVariables(lepton_pindex,particleBank,calorimeterBank); + + result_bank.setRows(1); + result_bank.putShort(i_pindex, 0, static_cast(lepton_pindex)); + result_bank.putDouble(i_score, 0, lepton_vars.score); + + ShowBank(result_bank, Logger::Header("CREATED BANK")); + + ShowBank(particleBank, Logger::Header("OUTPUT PARTICLES")); + } + + + int FindLepton(hipo::bank const& particle_bank) const{ + int lepton_pindex= -1; + for(int row = 0; row < particle_bank.getRows(); row++) { + auto status = particle_bank.getShort("status", row); + if(particle_bank.getInt("pid", row) == o_pid && abs(status)>=2000 && abs(status)<4000) { + lepton_pindex=row; + break; + } + } + if(lepton_pindex >= 0) + m_log->Debug("Found lepton: pindex={}", lepton_pindex); + else + m_log->Debug("Lepton not found"); + return lepton_pindex; + } + + LeptonIDVars CalculateScore(int const plepton, hipo::bank const& particle_bank, hipo::bank const& calorimeter_bank) const{ + + double px = particle_bank.getFloat("px", plepton); + double py = particle_bank.getFloat("py", plepton); + double pz = particle_bank.getFloat("pz", plepton); + double E = std::sqrt(std::pow(px, 2) + std::pow(py, 2) + std::pow(pz, 2) + std::pow(0.000511, 2)); + ROOT::Math::PxPyPzMVector vec_lepton=(px, py, pz, E); + + LeptonIDVars lepton; + + lepton.P =vec_lepton.P(); + lepton.Theta=vec_lepton.Theta(); + lepton.Phi =vec_lepton.Phi(); + + m_log->Debug("Variables obtained from particle bank"); + + + lepton.m2pcal=-1; + lepton.m2ecin=-1; + lepton.m2ecout=-1; + + for(int row = 0; row < calorimeter_bank.getRows(); row++) { + auto pindex = calorimeter_bank.getShort("pindex",row); + auto layer = calorimeter_bank.getByte("layer",row); + auto energy = calorimeter_bank.getFloat("energy",row); + auto m2u = calorimeter_bank.getFloat("m2u",row); + auto m2v = calorimeter_bank.getFloat("m2v",row); + auto m2w = calorimeter_bank.getFloat("m2w",row); + + if(pindex==plepton && layer=1) { + lepton.SFpcal=energy/vec_lepton.P(); + lepton.m2pcal=(m2u+m2v+m2w)/3; + } + + if(pindex==plepton && layer==4) { + lepton.SFecin=energy/vec_lepton.P(); + lepton.m2ecin=(m2u+m2v+m2w)/3; + } + if(pindex==plepton && layer==7) { + lepton.SFecout=energy/vec_lepton.P(); + lepton.m2ecout=(m2u+m2v+m2w)/3; + } + + } + + + m_log->Debug("Variables obtained from calorimeter bank"); + + //Get TMVA reader + TMVA::Reader *readerTMVA = new TMVA::Reader( "!Color:!Silent" ); + // Create a set of variables and declare them to the reader + Float_t P, Theta, Phi, PCAL,ECIN,ECOUT,m2PCAL,m2ECIN,m2ECOUT; + + readerTMVA->AddVariable( "P",&P ); + readerTMVA->AddVariable( "Theta",&Theta); + readerTMVA->AddVariable( "Phi",&Phi); + readerTMVA->AddVariable( "SFPCAL",&PCAL); + readerTMVA->AddVariable( "SFECIN",&ECIN); + readerTMVA->AddVariable( "SFECOUT",&ECOUT ); + readerTMVA->AddVariable( "m2PCAL",&m2PCAL); + readerTMVA->AddVariable( "m2ECIN",&m2ECIN); + readerTMVA->AddVariable( "m2ECOUT",&m2ECOUT); + + m_log->Debug("Add variables to readerTMVA"); + + readerTMVA->BookMVA( "BDT", o_weightfile ); + + m_log->Debug("TMVA method booked"); + + lepton.score=readerTMVA->EvaluateMVA("BDT"); + + return lepton; + } + + + void LeptonID::Stop() + { + } + +} diff --git a/src/iguana/algorithms/clas12/LeptonID.h b/src/iguana/algorithms/clas12/LeptonID.h new file mode 100644 index 000000000..a9d44c6e7 --- /dev/null +++ b/src/iguana/algorithms/clas12/LeptonID.h @@ -0,0 +1,71 @@ +#pragma once + +#include "iguana/algorithms/Algorithm.h" + +namespace iguana::clas12 { + + /// Set of inclusive kinematics variables + struct LeptonIDVars { + double P; + double Theta; + double Phi; + double SFpcal; + double SFecin; + double SFecout; + double m2pcal; + double m2ecin; + double m2ecout; + double score; + }; + + /// + /// @brief_algo This is a template algorithm, used as an example showing how to write an algorithm. + /// + /// Provide a more detailed description of your algorithm here. + /// + /// @begin_doc_algo{Filter} + /// @input_banks{REC::Particle} + /// @output_banks{REC::Particle} + /// @end_doc + /// + /// @begin_doc_config + /// @config_param{exampleInt | int | an example `integer` configuration parameter} + /// @config_param{exampleDouble | double | an example `double` configuration parameter} + /// @end_doc + class LeptonID : public Algorithm + { + + DEFINE_IGUANA_ALGORITHM(LeptonID, clas12::LeptonID) + + public: + + void Start(hipo::banklist& banks) override; + void Run(hipo::banklist& banks) const override; + void Stop() override; + + /// **FindLepton function**: returns the pindex of the lepton + /// @param particle_bank the particle bank + /// @returns pindex of the lepton, -1 if there is no lepton + int FindLepton(hipo::bank const& particle_bank) const; + + /// **CalculateScore function**: Using the pindex retrieves the necessary variables from banks + ///to do the Lepton ID + /// @param plepton pindex of the lepton + /// @param particle_bank the particle bank + /// @param calorimeter_bank the calorimeter bank + /// @returns LeptonIDVars, the variables required for identification + LeptonIDVars CalculateScore(int const plepton, hipo::bank const& particle_bank, hipo::bank const& calorimeter_bank) const; + + private: + + /// `hipo::banklist` index for the particle bank (as an example) + hipo::banklist::size_type b_particle; + hipo::banklist::size_type b_calorimeter; + + /// Example integer configuration option + int o_pid; + /// Example double configuration option + //double o_exampleDouble; + }; + +} diff --git a/src/iguana/algorithms/clas12/LeptonID.yaml b/src/iguana/algorithms/clas12/LeptonID.yaml new file mode 100644 index 000000000..079e872b9 --- /dev/null +++ b/src/iguana/algorithms/clas12/LeptonID.yaml @@ -0,0 +1,3 @@ +clas12::LeptonID: + exampleInt: 8 + exampleDouble: 7.5 From d6abc5d5ea6dd85578800d5e87765c6340994a50 Mon Sep 17 00:00:00 2001 From: MarianaT27 Date: Thu, 13 Jun 2024 10:49:43 -0400 Subject: [PATCH 02/36] Add LeptonID --- src/iguana/algorithms/LeptonID.cc | 163 ++++++++++++++++++++++++++++ src/iguana/algorithms/LeptonID.h | 71 ++++++++++++ src/iguana/algorithms/LeptonID.yaml | 3 + 3 files changed, 237 insertions(+) create mode 100644 src/iguana/algorithms/LeptonID.cc create mode 100644 src/iguana/algorithms/LeptonID.h create mode 100644 src/iguana/algorithms/LeptonID.yaml diff --git a/src/iguana/algorithms/LeptonID.cc b/src/iguana/algorithms/LeptonID.cc new file mode 100644 index 000000000..e45988ba9 --- /dev/null +++ b/src/iguana/algorithms/LeptonID.cc @@ -0,0 +1,163 @@ +#include "LeptonID.h" + +#include + +namespace iguana::clas12 { + + //REGISTER_IGUANA_ALGORITHM(LeptonID); //not using it + REGISTER_IGUANA_ALGORITHM(LeptonID , "clas12::leptonID"); // this algorithm creates 1 new banks + + void LeptonID::Start(hipo::banklist& banks) + { + //Get configuration + ParseYAMLConfig(); + o_pid = GetOptionScalar("pid");//Obtain pid from config file + o_weightfile = GetOptionScalar("weightfile");//Obtain weightfile from config file + //o_exampleDouble = GetOptionScalar("exampleDouble"); + + + + //Get Banks that we are going to use + b_particle = GetBankIndex(banks, "REC::Particle"); + b_calorimeter = GetBankIndex(banks, "REC::Calorimeter"); + + //Create bank to store score (should we add the variables?) + auto leptonID_schema = CreateBank( + banks, + b_leptonID, + GetClassName(), + {"pindex/S", "score/D","p/D","theta/D","phi/D","sfpcal/D","sfecin/D","sfecout/D","m2pcal/D","m2ecin/D","m2ecout/D"}, + 0xF000, + 1); + i_pindex = leptonID_schema.getEntryOrder("pindex"); + i_score = leptonID_schema.getEntryOrder("score"); + i_p = leptonID_schema.getEntryOrder("p"); + i_theta = leptonID_schema.getEntryOrder("theta"); + i_phi = leptonID_schema.getEntryOrder("phi"); + i_sfpcal = leptonID_schema.getEntryOrder("sfpcal"); + + } + + + void LeptonID::Run(hipo::banklist& banks) const + { + auto& particleBank = GetBank(banks, b_particle, "REC::Particle"); + auto& calorimeterBank = GetBank(banks, b_calorimeter, "REC::Calorimeter"); + + + ShowBank(particleBank, Logger::Header("INPUT PARTICLES")); + + auto lepton_pindex = FindLepton(particleBank); + if(lepton_pindex < 0) { + ShowBank(leptonID_schema, Logger::Header("CREATED BANK IS EMPTY")); + return; + } + + auto lepton_vars=GetLeptonIDVariables(lepton_pindex,particleBank,calorimeterBank); + + result_bank.setRows(1); + result_bank.putShort(i_pindex, 0, static_cast(lepton_pindex)); + result_bank.putDouble(i_score, 0, lepton_vars.score); + + ShowBank(result_bank, Logger::Header("CREATED BANK")); + + ShowBank(particleBank, Logger::Header("OUTPUT PARTICLES")); + } + + + int FindLepton(hipo::bank const& particle_bank) const{ + int lepton_pindex= -1; + for(int row = 0; row < particle_bank.getRows(); row++) { + auto status = particle_bank.getShort("status", row); + if(particle_bank.getInt("pid", row) == o_pid && abs(status)>=2000 && abs(status)<4000) { + lepton_pindex=row; + break; + } + } + if(lepton_pindex >= 0) + m_log->Debug("Found lepton: pindex={}", lepton_pindex); + else + m_log->Debug("Lepton not found"); + return lepton_pindex; + } + + LeptonIDVars CalculateScore(int const plepton, hipo::bank const& particle_bank, hipo::bank const& calorimeter_bank) const{ + + double px = particle_bank.getFloat("px", plepton); + double py = particle_bank.getFloat("py", plepton); + double pz = particle_bank.getFloat("pz", plepton); + double E = std::sqrt(std::pow(px, 2) + std::pow(py, 2) + std::pow(pz, 2) + std::pow(0.000511, 2)); + ROOT::Math::PxPyPzMVector vec_lepton=(px, py, pz, E); + + LeptonIDVars lepton; + + lepton.P =vec_lepton.P(); + lepton.Theta=vec_lepton.Theta(); + lepton.Phi =vec_lepton.Phi(); + + m_log->Debug("Variables obtained from particle bank"); + + + lepton.m2pcal=-1; + lepton.m2ecin=-1; + lepton.m2ecout=-1; + + for(int row = 0; row < calorimeter_bank.getRows(); row++) { + auto pindex = calorimeter_bank.getShort("pindex",row); + auto layer = calorimeter_bank.getByte("layer",row); + auto energy = calorimeter_bank.getFloat("energy",row); + auto m2u = calorimeter_bank.getFloat("m2u",row); + auto m2v = calorimeter_bank.getFloat("m2v",row); + auto m2w = calorimeter_bank.getFloat("m2w",row); + + if(pindex==plepton && layer=1) { + lepton.SFpcal=energy/vec_lepton.P(); + lepton.m2pcal=(m2u+m2v+m2w)/3; + } + + if(pindex==plepton && layer==4) { + lepton.SFecin=energy/vec_lepton.P(); + lepton.m2ecin=(m2u+m2v+m2w)/3; + } + if(pindex==plepton && layer==7) { + lepton.SFecout=energy/vec_lepton.P(); + lepton.m2ecout=(m2u+m2v+m2w)/3; + } + + } + + + m_log->Debug("Variables obtained from calorimeter bank"); + + //Get TMVA reader + TMVA::Reader *readerTMVA = new TMVA::Reader( "!Color:!Silent" ); + // Create a set of variables and declare them to the reader + Float_t P, Theta, Phi, PCAL,ECIN,ECOUT,m2PCAL,m2ECIN,m2ECOUT; + + readerTMVA->AddVariable( "P",&P ); + readerTMVA->AddVariable( "Theta",&Theta); + readerTMVA->AddVariable( "Phi",&Phi); + readerTMVA->AddVariable( "SFPCAL",&PCAL); + readerTMVA->AddVariable( "SFECIN",&ECIN); + readerTMVA->AddVariable( "SFECOUT",&ECOUT ); + readerTMVA->AddVariable( "m2PCAL",&m2PCAL); + readerTMVA->AddVariable( "m2ECIN",&m2ECIN); + readerTMVA->AddVariable( "m2ECOUT",&m2ECOUT); + + m_log->Debug("Add variables to readerTMVA"); + + readerTMVA->BookMVA( "BDT", o_weightfile ); + + m_log->Debug("TMVA method booked"); + + lepton.score=readerTMVA->EvaluateMVA("BDT"); + + return lepton; + } + + + void LeptonID::Stop() + { + } + +} diff --git a/src/iguana/algorithms/LeptonID.h b/src/iguana/algorithms/LeptonID.h new file mode 100644 index 000000000..a9d44c6e7 --- /dev/null +++ b/src/iguana/algorithms/LeptonID.h @@ -0,0 +1,71 @@ +#pragma once + +#include "iguana/algorithms/Algorithm.h" + +namespace iguana::clas12 { + + /// Set of inclusive kinematics variables + struct LeptonIDVars { + double P; + double Theta; + double Phi; + double SFpcal; + double SFecin; + double SFecout; + double m2pcal; + double m2ecin; + double m2ecout; + double score; + }; + + /// + /// @brief_algo This is a template algorithm, used as an example showing how to write an algorithm. + /// + /// Provide a more detailed description of your algorithm here. + /// + /// @begin_doc_algo{Filter} + /// @input_banks{REC::Particle} + /// @output_banks{REC::Particle} + /// @end_doc + /// + /// @begin_doc_config + /// @config_param{exampleInt | int | an example `integer` configuration parameter} + /// @config_param{exampleDouble | double | an example `double` configuration parameter} + /// @end_doc + class LeptonID : public Algorithm + { + + DEFINE_IGUANA_ALGORITHM(LeptonID, clas12::LeptonID) + + public: + + void Start(hipo::banklist& banks) override; + void Run(hipo::banklist& banks) const override; + void Stop() override; + + /// **FindLepton function**: returns the pindex of the lepton + /// @param particle_bank the particle bank + /// @returns pindex of the lepton, -1 if there is no lepton + int FindLepton(hipo::bank const& particle_bank) const; + + /// **CalculateScore function**: Using the pindex retrieves the necessary variables from banks + ///to do the Lepton ID + /// @param plepton pindex of the lepton + /// @param particle_bank the particle bank + /// @param calorimeter_bank the calorimeter bank + /// @returns LeptonIDVars, the variables required for identification + LeptonIDVars CalculateScore(int const plepton, hipo::bank const& particle_bank, hipo::bank const& calorimeter_bank) const; + + private: + + /// `hipo::banklist` index for the particle bank (as an example) + hipo::banklist::size_type b_particle; + hipo::banklist::size_type b_calorimeter; + + /// Example integer configuration option + int o_pid; + /// Example double configuration option + //double o_exampleDouble; + }; + +} diff --git a/src/iguana/algorithms/LeptonID.yaml b/src/iguana/algorithms/LeptonID.yaml new file mode 100644 index 000000000..079e872b9 --- /dev/null +++ b/src/iguana/algorithms/LeptonID.yaml @@ -0,0 +1,3 @@ +clas12::LeptonID: + exampleInt: 8 + exampleDouble: 7.5 From 5d82b0688fc944910c31a9401eec480c9bc40457 Mon Sep 17 00:00:00 2001 From: MarianaT27 Date: Thu, 27 Jun 2024 16:45:22 -0400 Subject: [PATCH 03/36] Added algorithm to folder, added weight file --- .../clas12/LeptonIDFilter/Algorithm.cc | 169 + .../clas12/LeptonIDFilter/Algorithm.h | 84 + .../clas12/LeptonIDFilter/config.yaml | 4 + .../weights/9_BDT_positrons_S19.weights.xml | 13538 ++++++++++++++++ 4 files changed, 13795 insertions(+) create mode 100644 src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.cc create mode 100644 src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.h create mode 100644 src/iguana/algorithms/clas12/LeptonIDFilter/config.yaml create mode 100644 src/iguana/algorithms/clas12/LeptonIDFilter/weights/9_BDT_positrons_S19.weights.xml diff --git a/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.cc b/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.cc new file mode 100644 index 000000000..a616f86b3 --- /dev/null +++ b/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.cc @@ -0,0 +1,169 @@ +#include "Algorithm.h" + +#include + +namespace iguana::clas12 { + + //REGISTER_IGUANA_ALGORITHM(LeptonID); //not using it + REGISTER_IGUANA_ALGORITHM(LeptonID , "clas12::leptonID"); // this algorithm creates 1 new banks + + void LeptonID::Start(hipo::banklist& banks) + { + //Get configuration + ParseYAMLConfig(); + o_pid = GetOptionScalar("pid");//Obtain pid from config file (+11/-11) + o_weightfile = GetOptionScalar("weightfile");//Obtain weightfile from config file + o_cut = GetOptionScalar("cut"); + + + + //Get Banks that we are going to use + b_particle = GetBankIndex(banks, "REC::Particle"); + b_calorimeter = GetBankIndex(banks, "REC::Calorimeter"); + + + } + + + void LeptonID::Run(hipo::banklist& banks) const + { + auto& particleBank = GetBank(banks, b_particle, "REC::Particle"); + auto& calorimeterBank = GetBank(banks, b_calorimeter, "REC::Calorimeter"); + + ShowBank(particleBank, Logger::Header("INPUT PARTICLES")); + + // + particleBank.getMutableRowList().filter([this](auto bank, auto row) { + auto lepton_pindex = FindLepton(particleBank); + auto lepton_vars=GetLeptonIDVariables(lepton_pindex,particleBank,calorimeterBank); + lepton_vars.score=CalculateScore(lepton_vars); + + return Filter(lepton_vars.score); + }); + + // dump the modified bank + ShowBank(particleBank, Logger::Header("OUTPUT PARTICLES")); + + } + + + int FindLepton(hipo::bank const& particle_bank) const{ + int lepton_pindex= -1; + for(int row = 0; row < particle_bank.getRows(); row++) { + auto status = particle_bank.getShort("status", row); + if(particle_bank.getInt("pid", row) == o_pid && abs(status)>=2000 && abs(status)<4000) { + lepton_pindex=row; + break; + } + } + if(lepton_pindex >= 0) + m_log->Debug("Found lepton: pindex={}", lepton_pindex); + else + m_log->Debug("Lepton not found"); + return lepton_pindex; + } + + LeptonIDVars GetLeptonIDVariables(int const plepton, hipo::bank const& particle_bank, hipo::bank const& calorimeter_bank) const{ + + double px = particle_bank.getFloat("px", plepton); + double py = particle_bank.getFloat("py", plepton); + double pz = particle_bank.getFloat("pz", plepton); + double E = std::sqrt(std::pow(px, 2) + std::pow(py, 2) + std::pow(pz, 2) + std::pow(0.000511, 2)); + ROOT::Math::PxPyPzMVector vec_lepton=(px, py, pz, E); + + LeptonIDVars lepton; + + lepton.P =vec_lepton.P(); + lepton.Theta=vec_lepton.Theta(); + lepton.Phi =vec_lepton.Phi(); + + m_log->Debug("Variables obtained from particle bank"); + + + lepton.m2pcal=-1; + lepton.m2ecin=-1; + lepton.m2ecout=-1; + + for(int row = 0; row < calorimeter_bank.getRows(); row++) { + auto pindex = calorimeter_bank.getShort("pindex",row); + auto layer = calorimeter_bank.getByte("layer",row); + auto energy = calorimeter_bank.getFloat("energy",row); + auto m2u = calorimeter_bank.getFloat("m2u",row); + auto m2v = calorimeter_bank.getFloat("m2v",row); + auto m2w = calorimeter_bank.getFloat("m2w",row); + + if(pindex==plepton && layer==1) { + lepton.SFpcal=energy/vec_lepton.P(); + lepton.m2pcal=(m2u+m2v+m2w)/3; + } + + if(pindex==plepton && layer==4) { + lepton.SFecin=energy/vec_lepton.P(); + lepton.m2ecin=(m2u+m2v+m2w)/3; + } + if(pindex==plepton && layer==7) { + lepton.SFecout=energy/vec_lepton.P(); + lepton.m2ecout=(m2u+m2v+m2w)/3; + } + + } + + + m_log->Debug("Variables obtained from calorimeter bank"); + + return lepton; + + } + + double CalculateScore(LeptonIDVars lepton_vars) const{ + + //Get TMVA reader + TMVA::Reader *readerTMVA = new TMVA::Reader( "!Color:!Silent" ); + // Create a set of variables and declare them to the reader + Float_t P, Theta, Phi, PCAL,ECIN,ECOUT,m2PCAL,m2ECIN,m2ECOUT; + + P=lepton_vars.P; + Theta=lepton_vars.Theta; + Phi=lepton_vars.Phi; + PCAL=lepton_vars.SFpcal; + ECIN=lepton_vars.SFecin; + ECOUT=lepton_vars.SFecout; + m2PCAL=lepton_vars.m2pcal; + m2ECIN=lepton_vars.m2ecin; + m2ECOUT=lepton_vars.m2ecout; + + readerTMVA->AddVariable( "P",&P ); + readerTMVA->AddVariable( "Theta",&Theta); + readerTMVA->AddVariable( "Phi",&Phi); + readerTMVA->AddVariable( "SFPCAL",&PCAL); + readerTMVA->AddVariable( "SFECIN",&ECIN); + readerTMVA->AddVariable( "SFECOUT",&ECOUT ); + readerTMVA->AddVariable( "m2PCAL",&m2PCAL); + readerTMVA->AddVariable( "m2ECIN",&m2ECIN); + readerTMVA->AddVariable( "m2ECOUT",&m2ECOUT); + + m_log->Debug("Add variables to readerTMVA"); + + readerTMVA->BookMVA( "BDT", o_weightfile ); + + m_log->Debug("TMVA method booked"); + + auto score=readerTMVA->EvaluateMVA("BDT"); + + return score; + } + + bool Filter(double score) const{ + if(score>=o_cut) + return true; + else + return false; + } + + + + void LeptonID::Stop() + { + } + +} diff --git a/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.h b/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.h new file mode 100644 index 000000000..30ca1ca31 --- /dev/null +++ b/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.h @@ -0,0 +1,84 @@ +#pragma once + +#include "iguana/algorithms/Algorithm.h" +#include "iguana/algorithms/TypeDefs.h" + +namespace iguana::clas12 { + + /// Set of inclusive kinematics variables + struct LeptonIDVars { + double P; + double Theta; + double Phi; + double SFpcal; + double SFecin; + double SFecout; + double m2pcal; + double m2ecin; + double m2ecout; + double score; + }; + + /// + /// @brief_algo This is a template algorithm, used as an example showing how to write an algorithm. + /// + /// Provide a more detailed description of your algorithm here. + /// + /// @begin_doc_algo{Filter} + /// @input_banks{REC::Particle} + /// @output_banks{REC::Particle} + /// @end_doc + /// + /// @begin_doc_config + /// @config_param{exampleInt | int | an example `integer` configuration parameter} + /// @config_param{exampleDouble | double | an example `double` configuration parameter} + /// @end_doc + class LeptonID : public Algorithm + { + + DEFINE_IGUANA_ALGORITHM(LeptonID, clas12::LeptonID) + + public: + + void Start(hipo::banklist& banks) override; + void Run(hipo::banklist& banks) const override; + void Stop() override; + + /// **FindLepton function**: returns the pindex of the lepton + /// @param particle_bank the particle bank + /// @returns pindex of the lepton, -1 if there is no lepton + int FindLepton(hipo::bank const& particle_bank) const; + + + /// **GetLeptonIDVariables function**: Using the pindex retrieves the necessary variables from banks + /// @param plepton pindex of the lepton + /// @param particle_bank the particle bank + /// @param calorimeter_bank the calorimeter bank + /// @returns LeptonIDVars, the variables required for identification + LeptonIDVars GetLeptonIDVariables(int const plepton, hipo::bank const& particle_bank, hipo::bank const& calorimeter_bank) const; + + + /// **CalculateScore function**: Using the LeptonIDVars variables calculate the score + /// @param lepton_vars LeptonIDVars variables + /// @returns double, the score + double CalculateScore(LeptonIDVars lepton_vars) const; + + /// **Filter function**: Returns true if the particle passed the cut + /// @param score the score obtained from the CalculateScore function + /// @returns bool, true if score>=cut, false otherwise + bool Filter(double score) const + + + private: + + /// `hipo::banklist` index for the particle bank (as an example) + hipo::banklist::size_type b_particle; + hipo::banklist::size_type b_calorimeter; + + /// Example integer configuration option + int o_pid; + /// Example double configuration option + //double o_exampleDouble; + }; + +} diff --git a/src/iguana/algorithms/clas12/LeptonIDFilter/config.yaml b/src/iguana/algorithms/clas12/LeptonIDFilter/config.yaml new file mode 100644 index 000000000..aaa6d6e5c --- /dev/null +++ b/src/iguana/algorithms/clas12/LeptonIDFilter/config.yaml @@ -0,0 +1,4 @@ +clas12::LeptonIDFilter: + pid: -11 + weightfile: "/weights/9_BDT_positrons_S19.weights.xml" + cut: 0.0 diff --git a/src/iguana/algorithms/clas12/LeptonIDFilter/weights/9_BDT_positrons_S19.weights.xml b/src/iguana/algorithms/clas12/LeptonIDFilter/weights/9_BDT_positrons_S19.weights.xml new file mode 100644 index 000000000..f4b4ac60c --- /dev/null +++ b/src/iguana/algorithms/clas12/LeptonIDFilter/weights/9_BDT_positrons_S19.weights.xml @@ -0,0 +1,13538 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + From 089b655a17ec379b3c7d31fc99be16bcd818cb45 Mon Sep 17 00:00:00 2001 From: MarianaT27 Date: Thu, 27 Jun 2024 16:49:49 -0400 Subject: [PATCH 04/36] Removed extra LeptonID files --- src/iguana/algorithms/LeptonID.cc | 163 --------------------- src/iguana/algorithms/LeptonID.h | 71 --------- src/iguana/algorithms/LeptonID.yaml | 3 - src/iguana/algorithms/clas12/LeptonID.cc | 163 --------------------- src/iguana/algorithms/clas12/LeptonID.h | 71 --------- src/iguana/algorithms/clas12/LeptonID.yaml | 3 - 6 files changed, 474 deletions(-) delete mode 100644 src/iguana/algorithms/LeptonID.cc delete mode 100644 src/iguana/algorithms/LeptonID.h delete mode 100644 src/iguana/algorithms/LeptonID.yaml delete mode 100644 src/iguana/algorithms/clas12/LeptonID.cc delete mode 100644 src/iguana/algorithms/clas12/LeptonID.h delete mode 100644 src/iguana/algorithms/clas12/LeptonID.yaml diff --git a/src/iguana/algorithms/LeptonID.cc b/src/iguana/algorithms/LeptonID.cc deleted file mode 100644 index e45988ba9..000000000 --- a/src/iguana/algorithms/LeptonID.cc +++ /dev/null @@ -1,163 +0,0 @@ -#include "LeptonID.h" - -#include - -namespace iguana::clas12 { - - //REGISTER_IGUANA_ALGORITHM(LeptonID); //not using it - REGISTER_IGUANA_ALGORITHM(LeptonID , "clas12::leptonID"); // this algorithm creates 1 new banks - - void LeptonID::Start(hipo::banklist& banks) - { - //Get configuration - ParseYAMLConfig(); - o_pid = GetOptionScalar("pid");//Obtain pid from config file - o_weightfile = GetOptionScalar("weightfile");//Obtain weightfile from config file - //o_exampleDouble = GetOptionScalar("exampleDouble"); - - - - //Get Banks that we are going to use - b_particle = GetBankIndex(banks, "REC::Particle"); - b_calorimeter = GetBankIndex(banks, "REC::Calorimeter"); - - //Create bank to store score (should we add the variables?) - auto leptonID_schema = CreateBank( - banks, - b_leptonID, - GetClassName(), - {"pindex/S", "score/D","p/D","theta/D","phi/D","sfpcal/D","sfecin/D","sfecout/D","m2pcal/D","m2ecin/D","m2ecout/D"}, - 0xF000, - 1); - i_pindex = leptonID_schema.getEntryOrder("pindex"); - i_score = leptonID_schema.getEntryOrder("score"); - i_p = leptonID_schema.getEntryOrder("p"); - i_theta = leptonID_schema.getEntryOrder("theta"); - i_phi = leptonID_schema.getEntryOrder("phi"); - i_sfpcal = leptonID_schema.getEntryOrder("sfpcal"); - - } - - - void LeptonID::Run(hipo::banklist& banks) const - { - auto& particleBank = GetBank(banks, b_particle, "REC::Particle"); - auto& calorimeterBank = GetBank(banks, b_calorimeter, "REC::Calorimeter"); - - - ShowBank(particleBank, Logger::Header("INPUT PARTICLES")); - - auto lepton_pindex = FindLepton(particleBank); - if(lepton_pindex < 0) { - ShowBank(leptonID_schema, Logger::Header("CREATED BANK IS EMPTY")); - return; - } - - auto lepton_vars=GetLeptonIDVariables(lepton_pindex,particleBank,calorimeterBank); - - result_bank.setRows(1); - result_bank.putShort(i_pindex, 0, static_cast(lepton_pindex)); - result_bank.putDouble(i_score, 0, lepton_vars.score); - - ShowBank(result_bank, Logger::Header("CREATED BANK")); - - ShowBank(particleBank, Logger::Header("OUTPUT PARTICLES")); - } - - - int FindLepton(hipo::bank const& particle_bank) const{ - int lepton_pindex= -1; - for(int row = 0; row < particle_bank.getRows(); row++) { - auto status = particle_bank.getShort("status", row); - if(particle_bank.getInt("pid", row) == o_pid && abs(status)>=2000 && abs(status)<4000) { - lepton_pindex=row; - break; - } - } - if(lepton_pindex >= 0) - m_log->Debug("Found lepton: pindex={}", lepton_pindex); - else - m_log->Debug("Lepton not found"); - return lepton_pindex; - } - - LeptonIDVars CalculateScore(int const plepton, hipo::bank const& particle_bank, hipo::bank const& calorimeter_bank) const{ - - double px = particle_bank.getFloat("px", plepton); - double py = particle_bank.getFloat("py", plepton); - double pz = particle_bank.getFloat("pz", plepton); - double E = std::sqrt(std::pow(px, 2) + std::pow(py, 2) + std::pow(pz, 2) + std::pow(0.000511, 2)); - ROOT::Math::PxPyPzMVector vec_lepton=(px, py, pz, E); - - LeptonIDVars lepton; - - lepton.P =vec_lepton.P(); - lepton.Theta=vec_lepton.Theta(); - lepton.Phi =vec_lepton.Phi(); - - m_log->Debug("Variables obtained from particle bank"); - - - lepton.m2pcal=-1; - lepton.m2ecin=-1; - lepton.m2ecout=-1; - - for(int row = 0; row < calorimeter_bank.getRows(); row++) { - auto pindex = calorimeter_bank.getShort("pindex",row); - auto layer = calorimeter_bank.getByte("layer",row); - auto energy = calorimeter_bank.getFloat("energy",row); - auto m2u = calorimeter_bank.getFloat("m2u",row); - auto m2v = calorimeter_bank.getFloat("m2v",row); - auto m2w = calorimeter_bank.getFloat("m2w",row); - - if(pindex==plepton && layer=1) { - lepton.SFpcal=energy/vec_lepton.P(); - lepton.m2pcal=(m2u+m2v+m2w)/3; - } - - if(pindex==plepton && layer==4) { - lepton.SFecin=energy/vec_lepton.P(); - lepton.m2ecin=(m2u+m2v+m2w)/3; - } - if(pindex==plepton && layer==7) { - lepton.SFecout=energy/vec_lepton.P(); - lepton.m2ecout=(m2u+m2v+m2w)/3; - } - - } - - - m_log->Debug("Variables obtained from calorimeter bank"); - - //Get TMVA reader - TMVA::Reader *readerTMVA = new TMVA::Reader( "!Color:!Silent" ); - // Create a set of variables and declare them to the reader - Float_t P, Theta, Phi, PCAL,ECIN,ECOUT,m2PCAL,m2ECIN,m2ECOUT; - - readerTMVA->AddVariable( "P",&P ); - readerTMVA->AddVariable( "Theta",&Theta); - readerTMVA->AddVariable( "Phi",&Phi); - readerTMVA->AddVariable( "SFPCAL",&PCAL); - readerTMVA->AddVariable( "SFECIN",&ECIN); - readerTMVA->AddVariable( "SFECOUT",&ECOUT ); - readerTMVA->AddVariable( "m2PCAL",&m2PCAL); - readerTMVA->AddVariable( "m2ECIN",&m2ECIN); - readerTMVA->AddVariable( "m2ECOUT",&m2ECOUT); - - m_log->Debug("Add variables to readerTMVA"); - - readerTMVA->BookMVA( "BDT", o_weightfile ); - - m_log->Debug("TMVA method booked"); - - lepton.score=readerTMVA->EvaluateMVA("BDT"); - - return lepton; - } - - - void LeptonID::Stop() - { - } - -} diff --git a/src/iguana/algorithms/LeptonID.h b/src/iguana/algorithms/LeptonID.h deleted file mode 100644 index a9d44c6e7..000000000 --- a/src/iguana/algorithms/LeptonID.h +++ /dev/null @@ -1,71 +0,0 @@ -#pragma once - -#include "iguana/algorithms/Algorithm.h" - -namespace iguana::clas12 { - - /// Set of inclusive kinematics variables - struct LeptonIDVars { - double P; - double Theta; - double Phi; - double SFpcal; - double SFecin; - double SFecout; - double m2pcal; - double m2ecin; - double m2ecout; - double score; - }; - - /// - /// @brief_algo This is a template algorithm, used as an example showing how to write an algorithm. - /// - /// Provide a more detailed description of your algorithm here. - /// - /// @begin_doc_algo{Filter} - /// @input_banks{REC::Particle} - /// @output_banks{REC::Particle} - /// @end_doc - /// - /// @begin_doc_config - /// @config_param{exampleInt | int | an example `integer` configuration parameter} - /// @config_param{exampleDouble | double | an example `double` configuration parameter} - /// @end_doc - class LeptonID : public Algorithm - { - - DEFINE_IGUANA_ALGORITHM(LeptonID, clas12::LeptonID) - - public: - - void Start(hipo::banklist& banks) override; - void Run(hipo::banklist& banks) const override; - void Stop() override; - - /// **FindLepton function**: returns the pindex of the lepton - /// @param particle_bank the particle bank - /// @returns pindex of the lepton, -1 if there is no lepton - int FindLepton(hipo::bank const& particle_bank) const; - - /// **CalculateScore function**: Using the pindex retrieves the necessary variables from banks - ///to do the Lepton ID - /// @param plepton pindex of the lepton - /// @param particle_bank the particle bank - /// @param calorimeter_bank the calorimeter bank - /// @returns LeptonIDVars, the variables required for identification - LeptonIDVars CalculateScore(int const plepton, hipo::bank const& particle_bank, hipo::bank const& calorimeter_bank) const; - - private: - - /// `hipo::banklist` index for the particle bank (as an example) - hipo::banklist::size_type b_particle; - hipo::banklist::size_type b_calorimeter; - - /// Example integer configuration option - int o_pid; - /// Example double configuration option - //double o_exampleDouble; - }; - -} diff --git a/src/iguana/algorithms/LeptonID.yaml b/src/iguana/algorithms/LeptonID.yaml deleted file mode 100644 index 079e872b9..000000000 --- a/src/iguana/algorithms/LeptonID.yaml +++ /dev/null @@ -1,3 +0,0 @@ -clas12::LeptonID: - exampleInt: 8 - exampleDouble: 7.5 diff --git a/src/iguana/algorithms/clas12/LeptonID.cc b/src/iguana/algorithms/clas12/LeptonID.cc deleted file mode 100644 index e45988ba9..000000000 --- a/src/iguana/algorithms/clas12/LeptonID.cc +++ /dev/null @@ -1,163 +0,0 @@ -#include "LeptonID.h" - -#include - -namespace iguana::clas12 { - - //REGISTER_IGUANA_ALGORITHM(LeptonID); //not using it - REGISTER_IGUANA_ALGORITHM(LeptonID , "clas12::leptonID"); // this algorithm creates 1 new banks - - void LeptonID::Start(hipo::banklist& banks) - { - //Get configuration - ParseYAMLConfig(); - o_pid = GetOptionScalar("pid");//Obtain pid from config file - o_weightfile = GetOptionScalar("weightfile");//Obtain weightfile from config file - //o_exampleDouble = GetOptionScalar("exampleDouble"); - - - - //Get Banks that we are going to use - b_particle = GetBankIndex(banks, "REC::Particle"); - b_calorimeter = GetBankIndex(banks, "REC::Calorimeter"); - - //Create bank to store score (should we add the variables?) - auto leptonID_schema = CreateBank( - banks, - b_leptonID, - GetClassName(), - {"pindex/S", "score/D","p/D","theta/D","phi/D","sfpcal/D","sfecin/D","sfecout/D","m2pcal/D","m2ecin/D","m2ecout/D"}, - 0xF000, - 1); - i_pindex = leptonID_schema.getEntryOrder("pindex"); - i_score = leptonID_schema.getEntryOrder("score"); - i_p = leptonID_schema.getEntryOrder("p"); - i_theta = leptonID_schema.getEntryOrder("theta"); - i_phi = leptonID_schema.getEntryOrder("phi"); - i_sfpcal = leptonID_schema.getEntryOrder("sfpcal"); - - } - - - void LeptonID::Run(hipo::banklist& banks) const - { - auto& particleBank = GetBank(banks, b_particle, "REC::Particle"); - auto& calorimeterBank = GetBank(banks, b_calorimeter, "REC::Calorimeter"); - - - ShowBank(particleBank, Logger::Header("INPUT PARTICLES")); - - auto lepton_pindex = FindLepton(particleBank); - if(lepton_pindex < 0) { - ShowBank(leptonID_schema, Logger::Header("CREATED BANK IS EMPTY")); - return; - } - - auto lepton_vars=GetLeptonIDVariables(lepton_pindex,particleBank,calorimeterBank); - - result_bank.setRows(1); - result_bank.putShort(i_pindex, 0, static_cast(lepton_pindex)); - result_bank.putDouble(i_score, 0, lepton_vars.score); - - ShowBank(result_bank, Logger::Header("CREATED BANK")); - - ShowBank(particleBank, Logger::Header("OUTPUT PARTICLES")); - } - - - int FindLepton(hipo::bank const& particle_bank) const{ - int lepton_pindex= -1; - for(int row = 0; row < particle_bank.getRows(); row++) { - auto status = particle_bank.getShort("status", row); - if(particle_bank.getInt("pid", row) == o_pid && abs(status)>=2000 && abs(status)<4000) { - lepton_pindex=row; - break; - } - } - if(lepton_pindex >= 0) - m_log->Debug("Found lepton: pindex={}", lepton_pindex); - else - m_log->Debug("Lepton not found"); - return lepton_pindex; - } - - LeptonIDVars CalculateScore(int const plepton, hipo::bank const& particle_bank, hipo::bank const& calorimeter_bank) const{ - - double px = particle_bank.getFloat("px", plepton); - double py = particle_bank.getFloat("py", plepton); - double pz = particle_bank.getFloat("pz", plepton); - double E = std::sqrt(std::pow(px, 2) + std::pow(py, 2) + std::pow(pz, 2) + std::pow(0.000511, 2)); - ROOT::Math::PxPyPzMVector vec_lepton=(px, py, pz, E); - - LeptonIDVars lepton; - - lepton.P =vec_lepton.P(); - lepton.Theta=vec_lepton.Theta(); - lepton.Phi =vec_lepton.Phi(); - - m_log->Debug("Variables obtained from particle bank"); - - - lepton.m2pcal=-1; - lepton.m2ecin=-1; - lepton.m2ecout=-1; - - for(int row = 0; row < calorimeter_bank.getRows(); row++) { - auto pindex = calorimeter_bank.getShort("pindex",row); - auto layer = calorimeter_bank.getByte("layer",row); - auto energy = calorimeter_bank.getFloat("energy",row); - auto m2u = calorimeter_bank.getFloat("m2u",row); - auto m2v = calorimeter_bank.getFloat("m2v",row); - auto m2w = calorimeter_bank.getFloat("m2w",row); - - if(pindex==plepton && layer=1) { - lepton.SFpcal=energy/vec_lepton.P(); - lepton.m2pcal=(m2u+m2v+m2w)/3; - } - - if(pindex==plepton && layer==4) { - lepton.SFecin=energy/vec_lepton.P(); - lepton.m2ecin=(m2u+m2v+m2w)/3; - } - if(pindex==plepton && layer==7) { - lepton.SFecout=energy/vec_lepton.P(); - lepton.m2ecout=(m2u+m2v+m2w)/3; - } - - } - - - m_log->Debug("Variables obtained from calorimeter bank"); - - //Get TMVA reader - TMVA::Reader *readerTMVA = new TMVA::Reader( "!Color:!Silent" ); - // Create a set of variables and declare them to the reader - Float_t P, Theta, Phi, PCAL,ECIN,ECOUT,m2PCAL,m2ECIN,m2ECOUT; - - readerTMVA->AddVariable( "P",&P ); - readerTMVA->AddVariable( "Theta",&Theta); - readerTMVA->AddVariable( "Phi",&Phi); - readerTMVA->AddVariable( "SFPCAL",&PCAL); - readerTMVA->AddVariable( "SFECIN",&ECIN); - readerTMVA->AddVariable( "SFECOUT",&ECOUT ); - readerTMVA->AddVariable( "m2PCAL",&m2PCAL); - readerTMVA->AddVariable( "m2ECIN",&m2ECIN); - readerTMVA->AddVariable( "m2ECOUT",&m2ECOUT); - - m_log->Debug("Add variables to readerTMVA"); - - readerTMVA->BookMVA( "BDT", o_weightfile ); - - m_log->Debug("TMVA method booked"); - - lepton.score=readerTMVA->EvaluateMVA("BDT"); - - return lepton; - } - - - void LeptonID::Stop() - { - } - -} diff --git a/src/iguana/algorithms/clas12/LeptonID.h b/src/iguana/algorithms/clas12/LeptonID.h deleted file mode 100644 index a9d44c6e7..000000000 --- a/src/iguana/algorithms/clas12/LeptonID.h +++ /dev/null @@ -1,71 +0,0 @@ -#pragma once - -#include "iguana/algorithms/Algorithm.h" - -namespace iguana::clas12 { - - /// Set of inclusive kinematics variables - struct LeptonIDVars { - double P; - double Theta; - double Phi; - double SFpcal; - double SFecin; - double SFecout; - double m2pcal; - double m2ecin; - double m2ecout; - double score; - }; - - /// - /// @brief_algo This is a template algorithm, used as an example showing how to write an algorithm. - /// - /// Provide a more detailed description of your algorithm here. - /// - /// @begin_doc_algo{Filter} - /// @input_banks{REC::Particle} - /// @output_banks{REC::Particle} - /// @end_doc - /// - /// @begin_doc_config - /// @config_param{exampleInt | int | an example `integer` configuration parameter} - /// @config_param{exampleDouble | double | an example `double` configuration parameter} - /// @end_doc - class LeptonID : public Algorithm - { - - DEFINE_IGUANA_ALGORITHM(LeptonID, clas12::LeptonID) - - public: - - void Start(hipo::banklist& banks) override; - void Run(hipo::banklist& banks) const override; - void Stop() override; - - /// **FindLepton function**: returns the pindex of the lepton - /// @param particle_bank the particle bank - /// @returns pindex of the lepton, -1 if there is no lepton - int FindLepton(hipo::bank const& particle_bank) const; - - /// **CalculateScore function**: Using the pindex retrieves the necessary variables from banks - ///to do the Lepton ID - /// @param plepton pindex of the lepton - /// @param particle_bank the particle bank - /// @param calorimeter_bank the calorimeter bank - /// @returns LeptonIDVars, the variables required for identification - LeptonIDVars CalculateScore(int const plepton, hipo::bank const& particle_bank, hipo::bank const& calorimeter_bank) const; - - private: - - /// `hipo::banklist` index for the particle bank (as an example) - hipo::banklist::size_type b_particle; - hipo::banklist::size_type b_calorimeter; - - /// Example integer configuration option - int o_pid; - /// Example double configuration option - //double o_exampleDouble; - }; - -} diff --git a/src/iguana/algorithms/clas12/LeptonID.yaml b/src/iguana/algorithms/clas12/LeptonID.yaml deleted file mode 100644 index 079e872b9..000000000 --- a/src/iguana/algorithms/clas12/LeptonID.yaml +++ /dev/null @@ -1,3 +0,0 @@ -clas12::LeptonID: - exampleInt: 8 - exampleDouble: 7.5 From 1b339218a3e956d8b87a1947f30ff35269247783 Mon Sep 17 00:00:00 2001 From: MarianaT27 Date: Thu, 27 Jun 2024 17:03:17 -0400 Subject: [PATCH 05/36] Added config variables --- src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.h b/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.h index 30ca1ca31..1781bb0e5 100644 --- a/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.h +++ b/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.h @@ -77,8 +77,8 @@ namespace iguana::clas12 { /// Example integer configuration option int o_pid; - /// Example double configuration option - //double o_exampleDouble; + std::string o_weightfile; + double o_cut; }; } From d6a9feac719616ba3d8c2d2b01c276db5d061115 Mon Sep 17 00:00:00 2001 From: Christopher Dilks Date: Fri, 28 Jun 2024 15:54:05 -0400 Subject: [PATCH 06/36] renamed: LeptonIDFilter/config.yaml -> LeptonIDFilter/Config.yaml --- .../algorithms/clas12/LeptonIDFilter/{config.yaml => Config.yaml} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/iguana/algorithms/clas12/LeptonIDFilter/{config.yaml => Config.yaml} (100%) diff --git a/src/iguana/algorithms/clas12/LeptonIDFilter/config.yaml b/src/iguana/algorithms/clas12/LeptonIDFilter/Config.yaml similarity index 100% rename from src/iguana/algorithms/clas12/LeptonIDFilter/config.yaml rename to src/iguana/algorithms/clas12/LeptonIDFilter/Config.yaml From 52eac3d8ca5efcf8b4317cb4df0e4df22e645c15 Mon Sep 17 00:00:00 2001 From: Christopher Dilks Date: Fri, 28 Jun 2024 16:24:32 -0400 Subject: [PATCH 07/36] fix: compilation fixes --- meson.build | 1 + .../clas12/LeptonIDFilter/Algorithm.cc | 23 ++++++++++--------- .../clas12/LeptonIDFilter/Algorithm.h | 7 +++--- src/iguana/algorithms/meson.build | 7 ++++++ 4 files changed, 23 insertions(+), 15 deletions(-) diff --git a/meson.build b/meson.build index 50a6bb97c..55703f559 100644 --- a/meson.build +++ b/meson.build @@ -150,6 +150,7 @@ if ROOT_dep.found() '-lGenVector', '-lROOTDataFrame', '-lROOTVecOps', + '-lTMVA', '-lTreePlayer', ] # additional ROOT libraries for validators (namely, graphics libraries) diff --git a/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.cc b/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.cc index a616f86b3..56a3ed088 100644 --- a/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.cc +++ b/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.cc @@ -1,13 +1,14 @@ #include "Algorithm.h" #include +#include +#include namespace iguana::clas12 { - //REGISTER_IGUANA_ALGORITHM(LeptonID); //not using it - REGISTER_IGUANA_ALGORITHM(LeptonID , "clas12::leptonID"); // this algorithm creates 1 new banks + REGISTER_IGUANA_ALGORITHM(LeptonIDFilter , "clas12::LeptonIDFilter"); - void LeptonID::Start(hipo::banklist& banks) + void LeptonIDFilter::Start(hipo::banklist& banks) { //Get configuration ParseYAMLConfig(); @@ -25,7 +26,7 @@ namespace iguana::clas12 { } - void LeptonID::Run(hipo::banklist& banks) const + void LeptonIDFilter::Run(hipo::banklist& banks) const { auto& particleBank = GetBank(banks, b_particle, "REC::Particle"); auto& calorimeterBank = GetBank(banks, b_calorimeter, "REC::Calorimeter"); @@ -33,7 +34,7 @@ namespace iguana::clas12 { ShowBank(particleBank, Logger::Header("INPUT PARTICLES")); // - particleBank.getMutableRowList().filter([this](auto bank, auto row) { + particleBank.getMutableRowList().filter([this,&particleBank,&calorimeterBank](auto bank, auto row) { auto lepton_pindex = FindLepton(particleBank); auto lepton_vars=GetLeptonIDVariables(lepton_pindex,particleBank,calorimeterBank); lepton_vars.score=CalculateScore(lepton_vars); @@ -47,7 +48,7 @@ namespace iguana::clas12 { } - int FindLepton(hipo::bank const& particle_bank) const{ + int LeptonIDFilter::FindLepton(hipo::bank const& particle_bank) const{ int lepton_pindex= -1; for(int row = 0; row < particle_bank.getRows(); row++) { auto status = particle_bank.getShort("status", row); @@ -63,13 +64,13 @@ namespace iguana::clas12 { return lepton_pindex; } - LeptonIDVars GetLeptonIDVariables(int const plepton, hipo::bank const& particle_bank, hipo::bank const& calorimeter_bank) const{ + LeptonIDVars LeptonIDFilter::GetLeptonIDVariables(int const plepton, hipo::bank const& particle_bank, hipo::bank const& calorimeter_bank) const{ double px = particle_bank.getFloat("px", plepton); double py = particle_bank.getFloat("py", plepton); double pz = particle_bank.getFloat("pz", plepton); double E = std::sqrt(std::pow(px, 2) + std::pow(py, 2) + std::pow(pz, 2) + std::pow(0.000511, 2)); - ROOT::Math::PxPyPzMVector vec_lepton=(px, py, pz, E); + ROOT::Math::PxPyPzMVector vec_lepton(px, py, pz, E); LeptonIDVars lepton; @@ -115,7 +116,7 @@ namespace iguana::clas12 { } - double CalculateScore(LeptonIDVars lepton_vars) const{ + double LeptonIDFilter::CalculateScore(LeptonIDVars lepton_vars) const{ //Get TMVA reader TMVA::Reader *readerTMVA = new TMVA::Reader( "!Color:!Silent" ); @@ -153,7 +154,7 @@ namespace iguana::clas12 { return score; } - bool Filter(double score) const{ + bool LeptonIDFilter::Filter(double score) const{ if(score>=o_cut) return true; else @@ -162,7 +163,7 @@ namespace iguana::clas12 { - void LeptonID::Stop() + void LeptonIDFilter::Stop() { } diff --git a/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.h b/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.h index 1781bb0e5..c343ba4c9 100644 --- a/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.h +++ b/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.h @@ -1,7 +1,6 @@ #pragma once #include "iguana/algorithms/Algorithm.h" -#include "iguana/algorithms/TypeDefs.h" namespace iguana::clas12 { @@ -33,10 +32,10 @@ namespace iguana::clas12 { /// @config_param{exampleInt | int | an example `integer` configuration parameter} /// @config_param{exampleDouble | double | an example `double` configuration parameter} /// @end_doc - class LeptonID : public Algorithm + class LeptonIDFilter : public Algorithm { - DEFINE_IGUANA_ALGORITHM(LeptonID, clas12::LeptonID) + DEFINE_IGUANA_ALGORITHM(LeptonIDFilter, clas12::LeptonIDFilter) public: @@ -66,7 +65,7 @@ namespace iguana::clas12 { /// **Filter function**: Returns true if the particle passed the cut /// @param score the score obtained from the CalculateScore function /// @returns bool, true if score>=cut, false otherwise - bool Filter(double score) const + bool Filter(double score) const; private: diff --git a/src/iguana/algorithms/meson.build b/src/iguana/algorithms/meson.build index 7ac67bcbc..bdee54519 100644 --- a/src/iguana/algorithms/meson.build +++ b/src/iguana/algorithms/meson.build @@ -98,6 +98,13 @@ algo_dict = [ 'has_action_yaml': false, 'test_args': {'banks': [ 'REC::Particle', 'REC::Calorimeter', 'RUN::config' ]}, }, + { + 'name': 'clas12::LeptonIDFilter', + 'has_c_bindings': false, + 'algorithm_needs_ROOT': true, + 'has_validator': false, + 'test_args': { 'banks': ['REC::Particle', 'REC::Calorimeter'] }, + }, ] # make lists of objects to build; inclusion depends on whether ROOT is needed or not, and if we have ROOT From e069eb16ffc86f20a3ba9af45c58a20a4faca86f Mon Sep 17 00:00:00 2001 From: Christopher Dilks Date: Mon, 1 Jul 2024 14:46:15 -0400 Subject: [PATCH 08/36] feat: install and use algorithm data files (viz., weight files) --- meson.build | 27 ++++++--- src/iguana/algorithms/Algorithm.cc | 11 ++++ src/iguana/algorithms/Algorithm.h | 11 ++++ .../clas12/LeptonIDFilter/Algorithm.cc | 6 +- .../clas12/LeptonIDFilter/Algorithm.h | 1 + .../clas12/LeptonIDFilter/Config.yaml | 2 +- src/iguana/algorithms/meson.build | 20 ++++++- src/iguana/services/ConfigFileReader.cc | 55 +++++++++++-------- src/iguana/services/ConfigFileReader.h | 8 ++- src/iguana/services/DataFileReader.cc | 15 +++++ src/iguana/services/DataFileReader.h | 19 +++++++ src/iguana/services/meson.build | 5 +- 12 files changed, 140 insertions(+), 40 deletions(-) create mode 100644 src/iguana/services/DataFileReader.cc create mode 100644 src/iguana/services/DataFileReader.h diff --git a/meson.build b/meson.build index 55703f559..8756d9f70 100644 --- a/meson.build +++ b/meson.build @@ -24,6 +24,14 @@ project_description = 'Implementation Guardian of Analysis Algorithms' add_languages('fortran', native: false, required: get_option('bind_fortran')) use_chameleon = get_option('bind_fortran') +# check built-in build options +if get_option('libdir') != 'lib' + warning('build option "libdir" = "' + get_option('libdir') + '", which is not "lib"; if you experience any issues, please report them') +endif +if get_option('datadir') != 'share' + error('build option "datadir" must be "share", but it is set to "' + get_option('datadir') + '"') +endif + # meson modules pkg = import('pkgconfig') fs = import('fs') @@ -164,12 +172,13 @@ if ROOT_dep.found() endif # general project vars -project_inc = include_directories('src') -project_libs = [] -project_deps = declare_dependency(dependencies: dep_list) # do NOT include ROOT here -project_etc = get_option('sysconfdir') / meson.project_name() -project_test_env = environment() -project_pkg_vars = [ +project_inc = include_directories('src') +project_libs = [] +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_data = get_option('datadir') / meson.project_name() +project_test_env = environment() +project_pkg_vars = [ 'bindir=' + '${prefix}' / get_option('bindir'), 'dep_pkgconfigdirs=' + ':'.join(dep_pkgconfig_dirs), 'dep_libdirs=' + ':'.join(dep_lib_dirs), @@ -199,8 +208,10 @@ project_test_env.set( # set preprocessor macros add_project_arguments( - '-DIGUANA_ETCDIR="' + get_option('prefix') / project_etc + '"', - language: [ 'cpp' ], + '-DIGUANA_PREFIX="' + get_option('prefix') + '"', + '-DIGUANA_ETCDIR="' + project_etc + '"', + '-DIGUANA_DATADIR="' + project_data + '"', + language: ['cpp'], ) # start chameleon diff --git a/src/iguana/algorithms/Algorithm.cc b/src/iguana/algorithms/Algorithm.cc index b6f37a277..22ab346e0 100644 --- a/src/iguana/algorithms/Algorithm.cc +++ b/src/iguana/algorithms/Algorithm.cc @@ -106,6 +106,17 @@ namespace iguana { /////////////////////////////////////////////////////////////////////////////// + std::string Algorithm::GetDataFile(std::string const& name) + { + if(!m_datafile_reader) { + m_datafile_reader = std::make_unique(ConfigFileReader::ConvertAlgoNameToConfigDir(m_class_name), "data|" + m_name); + m_datafile_reader->SetLogLevel(m_log->GetLevel()); + } + return m_datafile_reader->FindFile(name); + } + + /////////////////////////////////////////////////////////////////////////////// + void Algorithm::ParseYAMLConfig() { diff --git a/src/iguana/algorithms/Algorithm.h b/src/iguana/algorithms/Algorithm.h index 1700ca5cc..2cf3a37c0 100644 --- a/src/iguana/algorithms/Algorithm.h +++ b/src/iguana/algorithms/Algorithm.h @@ -10,6 +10,7 @@ #include "iguana/algorithms/AlgorithmBoilerplate.h" #include "iguana/services/YAMLReader.h" #include +#include "iguana/services/DataFileReader.h" namespace iguana { @@ -135,6 +136,11 @@ namespace iguana { /// @param name the directory name void SetConfigDirectory(std::string const& name); + /// Get the full path to a data file, such as a machine-learning model + /// @param name the name of the file; if found in the user's current working directory (`./`), that will be the file that is used; + /// otherwise the _installed_ file (in `$IGUANA/share/`) will be used by default + std::string GetDataFile(std::string const& name); + protected: // methods /// Parse YAML configuration files. Sets `m_yaml_config`. @@ -229,9 +235,14 @@ namespace iguana { /// YAML reader std::unique_ptr m_yaml_config; +<<<<<<< HEAD /// Data structure to hold configuration options set by `Algorithm::SetOption` std::unordered_map m_option_cache; +======= + /// Data file reader + std::unique_ptr m_datafile_reader; +>>>>>>> 0563625 (feat: install and use algorithm data files (viz., weight files)) }; ////////////////////////////////////////////////////////////////////////////// diff --git a/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.cc b/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.cc index 56a3ed088..598d2cc2f 100644 --- a/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.cc +++ b/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.cc @@ -16,7 +16,9 @@ namespace iguana::clas12 { o_weightfile = GetOptionScalar("weightfile");//Obtain weightfile from config file o_cut = GetOptionScalar("cut"); - + // load the weights file + o_weightfile_fullpath = GetDataFile(o_weightfile); + m_log->Debug("Loaded weight file {}", o_weightfile_fullpath); //Get Banks that we are going to use b_particle = GetBankIndex(banks, "REC::Particle"); @@ -145,7 +147,7 @@ namespace iguana::clas12 { m_log->Debug("Add variables to readerTMVA"); - readerTMVA->BookMVA( "BDT", o_weightfile ); + readerTMVA->BookMVA( "BDT", o_weightfile_fullpath ); m_log->Debug("TMVA method booked"); diff --git a/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.h b/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.h index c343ba4c9..a18426ade 100644 --- a/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.h +++ b/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.h @@ -77,6 +77,7 @@ namespace iguana::clas12 { /// Example integer configuration option int o_pid; std::string o_weightfile; + std::string o_weightfile_fullpath; double o_cut; }; diff --git a/src/iguana/algorithms/clas12/LeptonIDFilter/Config.yaml b/src/iguana/algorithms/clas12/LeptonIDFilter/Config.yaml index aaa6d6e5c..0c1169942 100644 --- a/src/iguana/algorithms/clas12/LeptonIDFilter/Config.yaml +++ b/src/iguana/algorithms/clas12/LeptonIDFilter/Config.yaml @@ -1,4 +1,4 @@ clas12::LeptonIDFilter: pid: -11 - weightfile: "/weights/9_BDT_positrons_S19.weights.xml" + weightfile: "weights/9_BDT_positrons_S19.weights.xml" cut: 0.0 diff --git a/src/iguana/algorithms/meson.build b/src/iguana/algorithms/meson.build index bdee54519..f362889e5 100644 --- a/src/iguana/algorithms/meson.build +++ b/src/iguana/algorithms/meson.build @@ -12,6 +12,7 @@ # 'validator_needs_ROOT': bool # whether this validator needs ROOT or not (default=true) # 'add_algorithm_sources': list[str] # list of additional algorithm source files (default=[]) # 'add_algorithm_headers': list[str] # list of additional algorithm header files (default=[]) +# 'add_algorithm_data': list[str] # list of additional files to install (default=[]) # 'add_validator_sources': list[str] # list of additional validator source files (default=[]) # 'add_validator_headers': list[str] # list of additional validator header files (default=[]) # 'test_args': { dict[str], # if excluded, tests won't run for this algorithm @@ -103,11 +104,15 @@ algo_dict = [ 'has_c_bindings': false, 'algorithm_needs_ROOT': true, 'has_validator': false, + 'add_algorithm_data': [ + 'weights/9_BDT_positrons_S19.weights.xml', + ], 'test_args': { 'banks': ['REC::Particle', 'REC::Calorimeter'] }, }, ] # make lists of objects to build; inclusion depends on whether ROOT is needed or not, and if we have ROOT +<<<<<<< HEAD algo_sources = [ 'Algorithm.cc', 'AlgorithmFactory.cc', @@ -125,6 +130,7 @@ if ROOT_dep.found() endif vdor_sources = [ 'Validator.cc' ] vdor_headers = [ 'Validator.h' ] +algo_data = [] algo_configs = [] algo_bind_c_sources = [] @@ -141,7 +147,7 @@ foreach algo : algo_dict # algorithms: if (algo_needs_ROOT and ROOT_dep.found()) or not algo_needs_ROOT - # sources and headers + # sources, headers, and data algo_sources += algo_dir / 'Algorithm.cc' algo_headers += algo_dir / 'Algorithm.h' foreach src_file : algo.get('add_algorithm_sources', []) @@ -150,6 +156,9 @@ foreach algo : algo_dict foreach src_file : algo.get('add_algorithm_headers', []) algo_headers += algo_dir / src_file endforeach + foreach src_file : algo.get('add_algorithm_data', []) + algo_data += algo_dir / src_file + endforeach # config files if algo_has_config @@ -205,7 +214,14 @@ algo_lib = shared_library( install: true, build_rpath: ROOT_dep_rpath, ) -install_headers(algo_headers, subdir: meson.project_name() / 'algorithms', preserve_path: true) + +foreach algo_datum : algo_data + install_data( + algo_data, + install_dir: project_data / 'algorithms', + preserve_path: true, + ) +endforeach project_libs += algo_lib # build and install validators diff --git a/src/iguana/services/ConfigFileReader.cc b/src/iguana/services/ConfigFileReader.cc index a97fd0b98..86e0500a7 100644 --- a/src/iguana/services/ConfigFileReader.cc +++ b/src/iguana/services/ConfigFileReader.cc @@ -5,35 +5,37 @@ namespace iguana { - ConfigFileReader::ConfigFileReader(std::string_view name) + ConfigFileReader::ConfigFileReader(std::string_view name, bool set_default_dirs) : Object(name) { - // the algorithms need to know where the config files are; - // first, add config files from installation prefix; since this - // is added first, it's the lowest priority in the configuration - // search path - AddDirectory(GetConfigInstallationPrefix()); - // next, add `IGUANA_CONFIG_PATH` paths, which provides: - // - user override of the configuration path(s) - // - a fallback, if `GetConfigInstallationPrefix` is wrong, which happens - // if the Iguana installation is relocated - auto user_paths_env_var = std::getenv("IGUANA_CONFIG_PATH"); - if(user_paths_env_var != nullptr) { - std::istringstream user_paths_stream(user_paths_env_var); - std::string user_path_token; - decltype(m_directories) user_paths; - while(getline(user_paths_stream, user_path_token, ':')) // tokenize `IGUANA_CONFIG_PATH` - user_paths.push_front(user_path_token); - for(auto const& user_path : user_paths) // add the paths in the correct order - AddDirectory(user_path); - // after these default paths have been added, the user - // may still override by calling `AddDirectory` etc. themselves + if(set_default_dirs) { + // the algorithms need to know where the config files are; + // first, add config files from installation prefix; since this + // is added first, it's the lowest priority in the configuration + // search path + AddDirectory(GetConfigInstallationPrefix()); + // next, add `IGUANA_CONFIG_PATH` paths, which provides: + // - user override of the configuration path(s) + // - a fallback, if `GetConfigInstallationPrefix` is wrong, which happens + // if the Iguana installation is relocated + auto user_paths_env_var = std::getenv("IGUANA_CONFIG_PATH"); + if(user_paths_env_var != nullptr) { + std::istringstream user_paths_stream(user_paths_env_var); + std::string user_path_token; + decltype(m_directories) user_paths; + while(getline(user_paths_stream, user_path_token, ':')) // tokenize `IGUANA_CONFIG_PATH` + user_paths.push_front(user_path_token); + for(auto const& user_path : user_paths) // add the paths in the correct order + AddDirectory(user_path); + // after these default paths have been added, the user + // may still override by calling `AddDirectory` etc. themselves + } } } std::string ConfigFileReader::GetConfigInstallationPrefix() { - return IGUANA_ETCDIR; + return std::string(IGUANA_PREFIX) + "/" + std::string(IGUANA_ETCDIR); } void ConfigFileReader::AddDirectory(std::string const& dir) @@ -88,13 +90,18 @@ namespace iguana { throw std::runtime_error("configuration file not found"); } - std::string ConfigFileReader::ConvertAlgoNameToConfigName(std::string_view algo_name, std::string_view ext) + std::string ConfigFileReader::ConvertAlgoNameToConfigDir(std::string_view algo_name) { std::string result = std::string(algo_name); std::string::size_type it = 0; while((it = result.find("::", it)) != std::string::npos) result.replace(it, 2, "/"); - return "algorithms/" + result + "/Config." + std::string(ext); + return "algorithms/" + result; + } + + std::string ConfigFileReader::ConvertAlgoNameToConfigName(std::string_view algo_name, std::string_view ext) + { + return ConvertAlgoNameToConfigDir(algo_name) + "/Config." + std::string(ext); } } diff --git a/src/iguana/services/ConfigFileReader.h b/src/iguana/services/ConfigFileReader.h index 6df04ad52..c37979ad0 100644 --- a/src/iguana/services/ConfigFileReader.h +++ b/src/iguana/services/ConfigFileReader.h @@ -12,7 +12,8 @@ namespace iguana { public: /// @param name the name of this configuration file handler - ConfigFileReader(std::string_view name = "config"); + /// @param set_default_dirs if true, add the default configuration directories + ConfigFileReader(std::string_view name = "config", bool set_default_dirs = true); /// Get the config files' _fixed_ installation prefix /// @warning if the Iguana installation is _relocated_, this directory will **not** be correct, @@ -42,6 +43,11 @@ namespace iguana { /// @return the found configuration file (with the directory) std::string FindFile(std::string const& name); + /// Convert a full algorithm name to its corresponding default config file subdirectory + /// @param algo_name the algorithm name + /// @return the config file subdirectory + static std::string ConvertAlgoNameToConfigDir(std::string_view algo_name); + /// Convert a full algorithm name to its corresponding default config file name /// @param algo_name the algorithm name /// @param ext the file extension diff --git a/src/iguana/services/DataFileReader.cc b/src/iguana/services/DataFileReader.cc new file mode 100644 index 000000000..4e1f18e49 --- /dev/null +++ b/src/iguana/services/DataFileReader.cc @@ -0,0 +1,15 @@ +#include "DataFileReader.h" + +namespace iguana { + DataFileReader::DataFileReader(std::string_view datadir_subdir, std::string_view name) : ConfigFileReader(name, false) + { + // first, add the "fallback" directory; this is the lowest priority directory, relying + // on the environment variable '$IGUANA', in case the higher-priority, build-time path fails + auto user_prefix = std::getenv("IGUANA"); + if(user_prefix != nullptr) + AddDirectory(std::string(user_prefix) + "/" + std::string(IGUANA_DATADIR) + "/" + std::string(datadir_subdir)); + // then add the hard-coded path, which is set at build time; if Iguana installation is + // relocated, this path will be wrong and the above fallback will be used instead + AddDirectory(std::string(IGUANA_PREFIX) + "/" + std::string(IGUANA_DATADIR) + "/" + std::string(datadir_subdir)); + } +} diff --git a/src/iguana/services/DataFileReader.h b/src/iguana/services/DataFileReader.h new file mode 100644 index 000000000..a2d31143c --- /dev/null +++ b/src/iguana/services/DataFileReader.h @@ -0,0 +1,19 @@ +#pragma once + +#include "ConfigFileReader.h" + +namespace iguana { + + /// @brief A data file reader, for example, weights files for machine learning-dependent algorithms + class DataFileReader : public ConfigFileReader + { + + public: + + /// @param datadir_subdir the subdirectory within build option `datadir` where the file may be found + /// @param name of this reader (for `Logger`) + DataFileReader(std::string_view datadir_subdir = "", std::string_view name = "data_file"); + ~DataFileReader() {} + + }; +} diff --git a/src/iguana/services/meson.build b/src/iguana/services/meson.build index f3635e94c..c579c6580 100644 --- a/src/iguana/services/meson.build +++ b/src/iguana/services/meson.build @@ -1,10 +1,11 @@ -services_sources = [ 'Logger.cc', 'Object.cc', 'ConfigFileReader.cc', 'YAMLReader.cc', 'ConcurrentParam.cc', 'GlobalParam.cc', 'RCDBReader.cc' ] -services_headers = [ 'Logger.h', 'Object.h', 'ConfigFileReader.h', 'YAMLReader.h', 'ConcurrentParam.h', 'GlobalParam.h', 'RCDBReader.h' ] +services_sources = [ 'Logger.cc', 'Object.cc', 'ConfigFileReader.cc', 'YAMLReader.cc', 'ConcurrentParam.cc', 'GlobalParam.cc', 'RCDBReader.cc','DataFileReader.cc'] +services_headers = [ 'Logger.h', 'Object.h', 'ConfigFileReader.h', 'YAMLReader.h', 'ConcurrentParam.h', 'GlobalParam.h', 'RCDBReader.h', 'DataFileReader.h' ] if rcdb_dep.found() add_project_arguments('-DUSE_RCDB', language: ['cpp']) endif + services_lib = shared_library( 'IguanaServices', services_sources, From ae7e1f2667f8955409abec9dcd852b82cd6d290f Mon Sep 17 00:00:00 2001 From: Christopher Dilks Date: Mon, 1 Jul 2024 14:52:40 -0400 Subject: [PATCH 09/36] doc: some doxygen fixes --- src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.h b/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.h index a18426ade..ece8efd41 100644 --- a/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.h +++ b/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.h @@ -4,7 +4,7 @@ namespace iguana::clas12 { - /// Set of inclusive kinematics variables + /// Set of lepton ID variables struct LeptonIDVars { double P; double Theta; @@ -23,7 +23,7 @@ namespace iguana::clas12 { /// /// Provide a more detailed description of your algorithm here. /// - /// @begin_doc_algo{Filter} + /// @begin_doc_algo{clas12::LeptonIDFilter | Filter} /// @input_banks{REC::Particle} /// @output_banks{REC::Particle} /// @end_doc From cf3ec1dce06619bc7fac0ad3df647b1a8498e854 Mon Sep 17 00:00:00 2001 From: MarianaT27 <84591454+MarianaT27@users.noreply.github.com> Date: Mon, 1 Jul 2024 16:58:13 -0400 Subject: [PATCH 10/36] Update src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.cc Co-authored-by: Christopher Dilks --- src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.cc b/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.cc index 598d2cc2f..646e19efa 100644 --- a/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.cc +++ b/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.cc @@ -121,7 +121,8 @@ namespace iguana::clas12 { double LeptonIDFilter::CalculateScore(LeptonIDVars lepton_vars) const{ //Get TMVA reader - TMVA::Reader *readerTMVA = new TMVA::Reader( "!Color:!Silent" ); + TString silent_opt = m_log->GetLevel() <= Logger::Level::trace ? "!Silent" : "Silent"; + TMVA::Reader *readerTMVA = new TMVA::Reader( "!Color" + silent_opt ); // Create a set of variables and declare them to the reader Float_t P, Theta, Phi, PCAL,ECIN,ECOUT,m2PCAL,m2ECIN,m2ECOUT; From 9d34a085628b3ec8b511e055ebbd3dc4c5c92478 Mon Sep 17 00:00:00 2001 From: MarianaT27 Date: Tue, 2 Jul 2024 10:22:01 -0400 Subject: [PATCH 11/36] Added: documentation descriptions --- .../algorithms/clas12/LeptonIDFilter/Algorithm.cc | 3 ++- .../algorithms/clas12/LeptonIDFilter/Algorithm.h | 13 +++++++------ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.cc b/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.cc index 646e19efa..477f76057 100644 --- a/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.cc +++ b/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.cc @@ -125,7 +125,7 @@ namespace iguana::clas12 { TMVA::Reader *readerTMVA = new TMVA::Reader( "!Color" + silent_opt ); // Create a set of variables and declare them to the reader Float_t P, Theta, Phi, PCAL,ECIN,ECOUT,m2PCAL,m2ECIN,m2ECOUT; - + @doxygen_off P=lepton_vars.P; Theta=lepton_vars.Theta; Phi=lepton_vars.Phi; @@ -135,6 +135,7 @@ namespace iguana::clas12 { m2PCAL=lepton_vars.m2pcal; m2ECIN=lepton_vars.m2ecin; m2ECOUT=lepton_vars.m2ecout; + @doxygen_on readerTMVA->AddVariable( "P",&P ); readerTMVA->AddVariable( "Theta",&Theta); diff --git a/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.h b/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.h index ece8efd41..94c15c4a9 100644 --- a/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.h +++ b/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.h @@ -19,18 +19,19 @@ namespace iguana::clas12 { }; /// - /// @brief_algo This is a template algorithm, used as an example showing how to write an algorithm. + /// @brief_algo Filter the leptons from the pion contamination using TMVA models /// - /// Provide a more detailed description of your algorithm here. + /// For each lepton, either positron or electron, it takes some variables from `REC::Particle` (P, Theta and Phi) and `REC::Particle` (Sampling fraction and second moments). + /// Using those variables, it call the TMVA method using the weight file, and it computes a score. By a pplying a cut to the score we can separate leptons from pions. /// /// @begin_doc_algo{clas12::LeptonIDFilter | Filter} - /// @input_banks{REC::Particle} - /// @output_banks{REC::Particle} + /// @input_banks{REC::Particle,REC::Calorimeter} /// @end_doc /// /// @begin_doc_config - /// @config_param{exampleInt | int | an example `integer` configuration parameter} - /// @config_param{exampleDouble | double | an example `double` configuration parameter} + /// @config_param{o_pid | int | PID of the particle; -11 for positrons and 11 for electrons} + /// @config_param{o_weightfile | std::string | Location of the weight file of the classifier} + /// @config_param{o_cut | double | Value of the score to apply the cut. The algorith will keep all particles that have a score grater than ths value} /// @end_doc class LeptonIDFilter : public Algorithm { From 2ae3b9d1768ab830e41b16b92e748eb391c0fa04 Mon Sep 17 00:00:00 2001 From: Christopher Dilks Date: Tue, 2 Jul 2024 09:03:38 -0400 Subject: [PATCH 12/36] fix: increase `algorithm` test timeout --- src/iguana/tests/meson.build | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/iguana/tests/meson.build b/src/iguana/tests/meson.build index cb7eeca80..151925df3 100644 --- a/src/iguana/tests/meson.build +++ b/src/iguana/tests/meson.build @@ -43,9 +43,9 @@ foreach algo : algo_dict '-'.join(['algorithm', test_name_algo]), test_exe, suite: [ 'algorithm', 'single_threaded' ], - args: [ 'algorithm', '-n', get_option('test_num_events') ] + test_args, + args: [ 'algorithm', '-n', get_option('test_num_events') ] + test_args, env: project_test_env, - timeout: 0, + timeout: 120, ) benchmark( '-'.join(['benchmark', 'single_threaded', test_name_algo]), From 7ee9977a3a35399edad8fd18118cf96fcc6624e6 Mon Sep 17 00:00:00 2001 From: MarianaT27 Date: Tue, 2 Jul 2024 12:14:31 -0400 Subject: [PATCH 13/36] Moved declaration of TMVAReader to header; Added documentation --- .../clas12/LeptonIDFilter/Algorithm.cc | 23 +---- .../clas12/LeptonIDFilter/Algorithm.h | 83 +++++++++++++++---- 2 files changed, 66 insertions(+), 40 deletions(-) diff --git a/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.cc b/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.cc index 477f76057..207328b82 100644 --- a/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.cc +++ b/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.cc @@ -120,12 +120,7 @@ namespace iguana::clas12 { double LeptonIDFilter::CalculateScore(LeptonIDVars lepton_vars) const{ - //Get TMVA reader - TString silent_opt = m_log->GetLevel() <= Logger::Level::trace ? "!Silent" : "Silent"; - TMVA::Reader *readerTMVA = new TMVA::Reader( "!Color" + silent_opt ); - // Create a set of variables and declare them to the reader - Float_t P, Theta, Phi, PCAL,ECIN,ECOUT,m2PCAL,m2ECIN,m2ECOUT; - @doxygen_off + ///Assing variables from lepton_vars for TMVA method P=lepton_vars.P; Theta=lepton_vars.Theta; Phi=lepton_vars.Phi; @@ -135,24 +130,8 @@ namespace iguana::clas12 { m2PCAL=lepton_vars.m2pcal; m2ECIN=lepton_vars.m2ecin; m2ECOUT=lepton_vars.m2ecout; - @doxygen_on - - readerTMVA->AddVariable( "P",&P ); - readerTMVA->AddVariable( "Theta",&Theta); - readerTMVA->AddVariable( "Phi",&Phi); - readerTMVA->AddVariable( "SFPCAL",&PCAL); - readerTMVA->AddVariable( "SFECIN",&ECIN); - readerTMVA->AddVariable( "SFECOUT",&ECOUT ); - readerTMVA->AddVariable( "m2PCAL",&m2PCAL); - readerTMVA->AddVariable( "m2ECIN",&m2ECIN); - readerTMVA->AddVariable( "m2ECOUT",&m2ECOUT); m_log->Debug("Add variables to readerTMVA"); - - readerTMVA->BookMVA( "BDT", o_weightfile_fullpath ); - - m_log->Debug("TMVA method booked"); - auto score=readerTMVA->EvaluateMVA("BDT"); return score; diff --git a/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.h b/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.h index 94c15c4a9..340b5b59e 100644 --- a/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.h +++ b/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.h @@ -3,21 +3,6 @@ #include "iguana/algorithms/Algorithm.h" namespace iguana::clas12 { - - /// Set of lepton ID variables - struct LeptonIDVars { - double P; - double Theta; - double Phi; - double SFpcal; - double SFecin; - double SFecout; - double m2pcal; - double m2ecin; - double m2ecout; - double score; - }; - /// /// @brief_algo Filter the leptons from the pion contamination using TMVA models /// @@ -70,15 +55,77 @@ namespace iguana::clas12 { private: - - /// `hipo::banklist` index for the particle bank (as an example) + ///Struct to store variables + struct LeptonIDVars { + /// @brief momentum + double P; + /// @brief Theta angle + double Theta; + /// @brief Phi angle + double Phi; + /// @brief Sampling fraction on the PCAL + double SFpcal; + /// @brief Sampling fraction on the ECIN + double SFecin; + /// @brief Sampling fraction on the ECOUT + double SFecout; + /// @brief Second-momenta of PCAL + double m2pcal; + /// @brief Second-momenta of ECIN + double m2ecin; + /// @brief Second-momenta of ECOUT + double m2ecout; + /// @brief Score + double score; + }; + + /// `hipo::banklist` hipo::banklist::size_type b_particle; hipo::banklist::size_type b_calorimeter; - /// Example integer configuration option + //Create TMVA reader + TMVA::Reader *readerTMVA = new TMVA::Reader(); + + ///Set of variables for the reader + ///Momentum + Float_t P; + ///Theta angle + Float_t Theta; + ///Phi angle + Float_t Phi; + ///Sampling fraction on the PCAL + Float_t PCAL; + ///Sampling fraction on the ECIN + Float_t ECIN; + ///Sampling fraction on the ECOUT + Float_t ECOUT; + ///Second-momenta of PCAL + Float_t m2PCAL; + ///Second-momenta of ECIN + Float_t m2ECIN; + ///Second-momenta of ECOUT + Float_t m2ECOUT; + + /// @brief Add variables to the readerTMVA + readerTMVA->AddVariable( "P",&P ); + readerTMVA->AddVariable( "Theta",&Theta); + readerTMVA->AddVariable( "Phi",&Phi); + readerTMVA->AddVariable( "SFPCAL",&PCAL); + readerTMVA->AddVariable( "SFECIN",&ECIN); + readerTMVA->AddVariable( "SFECOUT",&ECOUT ); + readerTMVA->AddVariable( "m2PCAL",&m2PCAL); + readerTMVA->AddVariable( "m2ECIN",&m2ECIN); + readerTMVA->AddVariable( "m2ECOUT",&m2ECOUT); + + readerTMVA->BookMVA( "BDT", o_weightfile_fullpath ); + + + /// pid of the lepton int o_pid; + /// Location of the weight file std::string o_weightfile; std::string o_weightfile_fullpath; + /// Value of the cut double o_cut; }; From efb276df8513026df38b2de33309748000042525 Mon Sep 17 00:00:00 2001 From: MarianaT27 Date: Tue, 2 Jul 2024 14:24:39 -0400 Subject: [PATCH 14/36] Fixed some errors --- .../clas12/LeptonIDFilter/Algorithm.cc | 1 - .../clas12/LeptonIDFilter/Algorithm.h | 49 ++++++++++--------- 2 files changed, 26 insertions(+), 24 deletions(-) diff --git a/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.cc b/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.cc index 207328b82..d1e315996 100644 --- a/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.cc +++ b/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.cc @@ -2,7 +2,6 @@ #include #include -#include namespace iguana::clas12 { diff --git a/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.h b/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.h index 340b5b59e..9b088cf39 100644 --- a/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.h +++ b/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.h @@ -1,6 +1,31 @@ #pragma once #include "iguana/algorithms/Algorithm.h" +#include + +///Struct to store variables + struct LeptonIDVars { + /// @brief momentum + double P; + /// @brief Theta angle + double Theta; + /// @brief Phi angle + double Phi; + /// @brief Sampling fraction on the PCAL + double SFpcal; + /// @brief Sampling fraction on the ECIN + double SFecin; + /// @brief Sampling fraction on the ECOUT + double SFecout; + /// @brief Second-momenta of PCAL + double m2pcal; + /// @brief Second-momenta of ECIN + double m2ecin; + /// @brief Second-momenta of ECOUT + double m2ecout; + /// @brief Score + double score; + }; namespace iguana::clas12 { /// @@ -55,29 +80,7 @@ namespace iguana::clas12 { private: - ///Struct to store variables - struct LeptonIDVars { - /// @brief momentum - double P; - /// @brief Theta angle - double Theta; - /// @brief Phi angle - double Phi; - /// @brief Sampling fraction on the PCAL - double SFpcal; - /// @brief Sampling fraction on the ECIN - double SFecin; - /// @brief Sampling fraction on the ECOUT - double SFecout; - /// @brief Second-momenta of PCAL - double m2pcal; - /// @brief Second-momenta of ECIN - double m2ecin; - /// @brief Second-momenta of ECOUT - double m2ecout; - /// @brief Score - double score; - }; + /// `hipo::banklist` hipo::banklist::size_type b_particle; From d900c1d1eaa634b8327a4629676567858159af31 Mon Sep 17 00:00:00 2001 From: MarianaT27 Date: Tue, 2 Jul 2024 15:44:24 -0400 Subject: [PATCH 15/36] Fixed more errors --- .../clas12/LeptonIDFilter/Algorithm.h | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.h b/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.h index 9b088cf39..5c776923c 100644 --- a/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.h +++ b/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.h @@ -78,14 +78,6 @@ namespace iguana::clas12 { /// @returns bool, true if score>=cut, false otherwise bool Filter(double score) const; - - private: - - - /// `hipo::banklist` - hipo::banklist::size_type b_particle; - hipo::banklist::size_type b_calorimeter; - //Create TMVA reader TMVA::Reader *readerTMVA = new TMVA::Reader(); @@ -122,6 +114,16 @@ namespace iguana::clas12 { readerTMVA->BookMVA( "BDT", o_weightfile_fullpath ); + + private: + + + /// `hipo::banklist` + hipo::banklist::size_type b_particle; + hipo::banklist::size_type b_calorimeter; + + + /// pid of the lepton int o_pid; From 40a84e0fb175ec0f69edcf494f38faf92f1d9397 Mon Sep 17 00:00:00 2001 From: Mariana Tenorio Pita Date: Tue, 14 Jan 2025 11:27:21 -0500 Subject: [PATCH 16/36] Fix typo in meson.build --- src/iguana/algorithms/meson.build | 1 - 1 file changed, 1 deletion(-) diff --git a/src/iguana/algorithms/meson.build b/src/iguana/algorithms/meson.build index f362889e5..b409a7283 100644 --- a/src/iguana/algorithms/meson.build +++ b/src/iguana/algorithms/meson.build @@ -112,7 +112,6 @@ algo_dict = [ ] # make lists of objects to build; inclusion depends on whether ROOT is needed or not, and if we have ROOT -<<<<<<< HEAD algo_sources = [ 'Algorithm.cc', 'AlgorithmFactory.cc', From 017521519b06ad2654ef27e823dbded69ee14bd8 Mon Sep 17 00:00:00 2001 From: Mariana Tenorio Pita Date: Tue, 14 Jan 2025 11:41:26 -0500 Subject: [PATCH 17/36] Remove line in src/iguana/services/meson.build --- src/iguana/services/meson.build | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/iguana/services/meson.build b/src/iguana/services/meson.build index c579c6580..38695b7f4 100644 --- a/src/iguana/services/meson.build +++ b/src/iguana/services/meson.build @@ -1,5 +1,5 @@ -services_sources = [ 'Logger.cc', 'Object.cc', 'ConfigFileReader.cc', 'YAMLReader.cc', 'ConcurrentParam.cc', 'GlobalParam.cc', 'RCDBReader.cc','DataFileReader.cc'] -services_headers = [ 'Logger.h', 'Object.h', 'ConfigFileReader.h', 'YAMLReader.h', 'ConcurrentParam.h', 'GlobalParam.h', 'RCDBReader.h', 'DataFileReader.h' ] +services_sources = [ 'Logger.cc', 'Object.cc', 'ConfigFileReader.cc', 'YAMLReader.cc', 'ConcurrentParam.cc', 'GlobalParam.cc', 'DataFileReader.cc'] +services_headers = [ 'Logger.h', 'Object.h', 'ConfigFileReader.h', 'YAMLReader.h', 'ConcurrentParam.h', 'GlobalParam.h', 'DataFileReader.h' ] if rcdb_dep.found() add_project_arguments('-DUSE_RCDB', language: ['cpp']) From befabef848d397aa5409c8f42b79e677a2765eff Mon Sep 17 00:00:00 2001 From: Mariana Tenorio Pita Date: Tue, 14 Jan 2025 11:52:49 -0500 Subject: [PATCH 18/36] Fix error in src/iguana/algorithms/Algorithm.h --- src/iguana/algorithms/Algorithm.h | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/iguana/algorithms/Algorithm.h b/src/iguana/algorithms/Algorithm.h index 2cf3a37c0..8330427a0 100644 --- a/src/iguana/algorithms/Algorithm.h +++ b/src/iguana/algorithms/Algorithm.h @@ -235,14 +235,10 @@ namespace iguana { /// YAML reader std::unique_ptr m_yaml_config; -<<<<<<< HEAD /// Data structure to hold configuration options set by `Algorithm::SetOption` std::unordered_map m_option_cache; - -======= /// Data file reader std::unique_ptr m_datafile_reader; ->>>>>>> 0563625 (feat: install and use algorithm data files (viz., weight files)) }; ////////////////////////////////////////////////////////////////////////////// From 6b0fe8f9e63dd772bf513b4f94e927034752bf0c Mon Sep 17 00:00:00 2001 From: Mariana Tenorio Pita Date: Thu, 23 Jan 2025 14:46:45 -0500 Subject: [PATCH 19/36] I fix the declaration of the TMVA reader --- .../clas12/LeptonIDFilter/Algorithm.cc | 14 ++++++ .../clas12/LeptonIDFilter/Algorithm.h | 46 ++++++++----------- 2 files changed, 33 insertions(+), 27 deletions(-) diff --git a/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.cc b/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.cc index d1e315996..55877dc21 100644 --- a/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.cc +++ b/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.cc @@ -7,6 +7,20 @@ namespace iguana::clas12 { REGISTER_IGUANA_ALGORITHM(LeptonIDFilter , "clas12::LeptonIDFilter"); +void LeptonIDFilter::initializeTMVA() { + readerTMVA = std::make_unique("V"); + // Initialize the variables for the TMVA reader + readerTMVA->AddVariable( "P", &P ); + readerTMVA->AddVariable( "Theta", &Theta ); + readerTMVA->AddVariable( "Phi", &Phi ); + readerTMVA->AddVariable( "SFPCAL", &PCAL ); + readerTMVA->AddVariable( "SFECIN", &ECIN ); + readerTMVA->AddVariable( "SFECOUT", &ECOUT ); + readerTMVA->AddVariable( "m2PCAL", &m2PCAL ); + readerTMVA->AddVariable( "m2ECIN", &m2ECIN ); + readerTMVA->AddVariable( "m2ECOUT", &m2ECOUT ); + } + void LeptonIDFilter::Start(hipo::banklist& banks) { //Get configuration diff --git a/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.h b/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.h index 5c776923c..621fe8e62 100644 --- a/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.h +++ b/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.h @@ -49,11 +49,14 @@ namespace iguana::clas12 { DEFINE_IGUANA_ALGORITHM(LeptonIDFilter, clas12::LeptonIDFilter) public: + // Constructor void Start(hipo::banklist& banks) override; void Run(hipo::banklist& banks) const override; void Stop() override; + void initializeTMVA(); + /// **FindLepton function**: returns the pindex of the lepton /// @param particle_bank the particle bank /// @returns pindex of the lepton, -1 if there is no lepton @@ -78,44 +81,33 @@ namespace iguana::clas12 { /// @returns bool, true if score>=cut, false otherwise bool Filter(double score) const; - //Create TMVA reader - TMVA::Reader *readerTMVA = new TMVA::Reader(); + + + + + private: + std::unique_ptr readerTMVA; + ///Set of variables for the reader ///Momentum - Float_t P; + mutable Float_t P; ///Theta angle - Float_t Theta; + mutable Float_t Theta; ///Phi angle - Float_t Phi; + mutable Float_t Phi; ///Sampling fraction on the PCAL - Float_t PCAL; + mutable Float_t PCAL; ///Sampling fraction on the ECIN - Float_t ECIN; + mutable Float_t ECIN; ///Sampling fraction on the ECOUT - Float_t ECOUT; + mutable Float_t ECOUT; ///Second-momenta of PCAL - Float_t m2PCAL; + mutable Float_t m2PCAL; ///Second-momenta of ECIN - Float_t m2ECIN; + mutable Float_t m2ECIN; ///Second-momenta of ECOUT - Float_t m2ECOUT; - - /// @brief Add variables to the readerTMVA - readerTMVA->AddVariable( "P",&P ); - readerTMVA->AddVariable( "Theta",&Theta); - readerTMVA->AddVariable( "Phi",&Phi); - readerTMVA->AddVariable( "SFPCAL",&PCAL); - readerTMVA->AddVariable( "SFECIN",&ECIN); - readerTMVA->AddVariable( "SFECOUT",&ECOUT ); - readerTMVA->AddVariable( "m2PCAL",&m2PCAL); - readerTMVA->AddVariable( "m2ECIN",&m2ECIN); - readerTMVA->AddVariable( "m2ECOUT",&m2ECOUT); - - readerTMVA->BookMVA( "BDT", o_weightfile_fullpath ); - - - private: + mutable Float_t m2ECOUT; /// `hipo::banklist` From a47415ff74cd12c4eaa17668d045301663b21a6a Mon Sep 17 00:00:00 2001 From: Christopher Dilks Date: Tue, 28 Jan 2025 20:14:23 -0500 Subject: [PATCH 20/36] fix: don't look for `Action.yaml` --- src/iguana/algorithms/meson.build | 1 + src/iguana/tests/meson.build | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/iguana/algorithms/meson.build b/src/iguana/algorithms/meson.build index b409a7283..064d07320 100644 --- a/src/iguana/algorithms/meson.build +++ b/src/iguana/algorithms/meson.build @@ -104,6 +104,7 @@ algo_dict = [ 'has_c_bindings': false, 'algorithm_needs_ROOT': true, 'has_validator': false, + 'has_action_yaml': false, 'add_algorithm_data': [ 'weights/9_BDT_positrons_S19.weights.xml', ], diff --git a/src/iguana/tests/meson.build b/src/iguana/tests/meson.build index 151925df3..2d257a96b 100644 --- a/src/iguana/tests/meson.build +++ b/src/iguana/tests/meson.build @@ -43,7 +43,7 @@ foreach algo : algo_dict '-'.join(['algorithm', test_name_algo]), test_exe, suite: [ 'algorithm', 'single_threaded' ], - args: [ 'algorithm', '-n', get_option('test_num_events') ] + test_args, + args: [ 'algorithm', '-n', get_option('test_num_events') ] + test_args, env: project_test_env, timeout: 120, ) From 861f03725806324aa98ea02298b262834d0af868 Mon Sep 17 00:00:00 2001 From: Christopher Dilks Date: Tue, 28 Jan 2025 20:23:20 -0500 Subject: [PATCH 21/36] fix: RCDB inclusion and linking --- meson.build | 2 +- src/iguana/algorithms/meson.build | 2 +- src/iguana/services/meson.build | 5 ++--- src/iguana/tests/meson.build | 2 +- 4 files changed, 5 insertions(+), 6 deletions(-) diff --git a/meson.build b/meson.build index 8756d9f70..d6eccbda7 100644 --- a/meson.build +++ b/meson.build @@ -174,7 +174,7 @@ endif # general project vars project_inc = include_directories('src') project_libs = [] -project_deps = declare_dependency(dependencies: [ fmt_dep, yamlcpp_dep, hipo_dep ] ) # do NOT include ROOT here +project_deps = declare_dependency(dependencies: dep_list) # do NOT include ROOT here project_etc = get_option('sysconfdir') / meson.project_name() project_data = get_option('datadir') / meson.project_name() project_test_env = environment() diff --git a/src/iguana/algorithms/meson.build b/src/iguana/algorithms/meson.build index 064d07320..d4067e331 100644 --- a/src/iguana/algorithms/meson.build +++ b/src/iguana/algorithms/meson.build @@ -214,7 +214,7 @@ algo_lib = shared_library( install: true, build_rpath: ROOT_dep_rpath, ) - +install_headers(algo_headers, subdir: meson.project_name() / 'algorithms', preserve_path: true) foreach algo_datum : algo_data install_data( algo_data, diff --git a/src/iguana/services/meson.build b/src/iguana/services/meson.build index 38695b7f4..384bce09c 100644 --- a/src/iguana/services/meson.build +++ b/src/iguana/services/meson.build @@ -1,11 +1,10 @@ -services_sources = [ 'Logger.cc', 'Object.cc', 'ConfigFileReader.cc', 'YAMLReader.cc', 'ConcurrentParam.cc', 'GlobalParam.cc', 'DataFileReader.cc'] -services_headers = [ 'Logger.h', 'Object.h', 'ConfigFileReader.h', 'YAMLReader.h', 'ConcurrentParam.h', 'GlobalParam.h', 'DataFileReader.h' ] +services_sources = [ 'Logger.cc', 'Object.cc', 'ConfigFileReader.cc', 'YAMLReader.cc', 'ConcurrentParam.cc', 'GlobalParam.cc', 'RCDBReader.cc', 'DataFileReader.cc' ] +services_headers = [ 'Logger.h', 'Object.h', 'ConfigFileReader.h', 'YAMLReader.h', 'ConcurrentParam.h', 'GlobalParam.h', 'RCDBReader.h', 'DataFileReader.h' ] if rcdb_dep.found() add_project_arguments('-DUSE_RCDB', language: ['cpp']) endif - services_lib = shared_library( 'IguanaServices', services_sources, diff --git a/src/iguana/tests/meson.build b/src/iguana/tests/meson.build index 2d257a96b..cb7eeca80 100644 --- a/src/iguana/tests/meson.build +++ b/src/iguana/tests/meson.build @@ -45,7 +45,7 @@ foreach algo : algo_dict suite: [ 'algorithm', 'single_threaded' ], args: [ 'algorithm', '-n', get_option('test_num_events') ] + test_args, env: project_test_env, - timeout: 120, + timeout: 0, ) benchmark( '-'.join(['benchmark', 'single_threaded', test_name_algo]), From 13b3119a08c7f27dce2d6f6d55266c83c564729e Mon Sep 17 00:00:00 2001 From: Christopher Dilks Date: Tue, 28 Jan 2025 21:03:01 -0500 Subject: [PATCH 22/36] doc: clarify environment variables and consuming --- doc/setup.md | 33 ++++++++++++++++++++++++------- examples/meson.build | 2 +- meson.build | 10 +++++----- src/iguana/algorithms/meson.build | 4 ++-- 4 files changed, 34 insertions(+), 15 deletions(-) diff --git a/doc/setup.md b/doc/setup.md index aa937cdf8..35ffe8119 100644 --- a/doc/setup.md +++ b/doc/setup.md @@ -1,10 +1,11 @@ # Setup Guide -| **Table of Contents** | -| --- | -| 🟠 [**Dependencies**](#dependencies) | -| 🟠 [**Building and Installing**](#building) | -| 🟠 [**Environment Variables** (optional)](#env) | +| **Table of Contents** | +| --- | +| 🟠 [**Dependencies**](#dependencies) | +| 🟠 [**Building and Installing**](#building) | +| 🟠 [**Environment Variables** (optional)](#env) | +| 🟠 [**Connect to a C++ Analysis (consuming)**](#consuming) | ## 🟠 Dependencies @@ -209,5 +210,23 @@ The following environment variables are set or modified; not all of them are nee | `LD_LIBRARY_PATH` (Linux) or `DYLD_LIBRARY_PATH` (macOS) | adds paths to dependency and Iguana libraries | | `PYTHONPATH` | adds paths to dependency and Iguana Python packages, if Python bindings are installed | | `ROOT_INCLUDE_PATH` | adds paths to dependency and Iguana header files, for usage in ROOT | -| `IGUANA_CONFIG_PATH` | path to iguana algorithm configuration files (`.yaml`); users may override this with their own path; multiple paths may be specified, delimited by colons (`:`), where paths listed first will override paths listed later (similar behavior as `$PATH`); this variable is _only_ necessary if the Iguana installation has been relocated | -| `IGUANA` | the path to the Iguana installation prefix, equivalent to `pkg-config iguana --variable prefix`; this is only for consumers that do not use `pkg-config` or the other standard environment variables, however usage of this variable is _discouraged_ since the installation layout may vary | +| `IGUANA_CONFIG_PATH` | path to iguana algorithm configuration files (`.yaml`); users may override this with their own path; multiple paths may be specified, delimited by colons (`:`), where paths listed first will override paths listed later (similar behavior as `$PATH`) | +| `IGUANA` | the path to the Iguana installation prefix | + +> [!IMPORTANT] +> If the Iguana installation is _relocated_, the following environment variables become _necessary_: +> - `IGUANA` +> - `IGUANA_CONFIG_PATH` + +> [!WARNING] +> Avoid using the `$IGUANA` environment variable for locating Iguana libraries, headers, _etc._, since the installation +> tree may vary. Instead, use `pkg-config`, following the [consuming section](#consuming). + + +## 🟠 Connect to a C++ Analysis (consuming) + +To integrate Iguana with your own C++ analysis code, _i.e._ to "consume" Iguana as a dependency, follow +one of the examples, depending on your build tool: +- [CMake](../examples/build_with_cmake) +- [Meson](../examples/build_with_meson) +- [Makefile](../examples/build_with_make) diff --git a/examples/meson.build b/examples/meson.build index 7b3abb925..814b3bf86 100644 --- a/examples/meson.build +++ b/examples/meson.build @@ -1,5 +1,5 @@ # install config files -example_config_files_prefix = project_etc / 'examples' +example_config_files_prefix = project_etc_dir / 'examples' install_subdir('config', install_dir: example_config_files_prefix, strip_directory: true) # example source information diff --git a/meson.build b/meson.build index d6eccbda7..89c439616 100644 --- a/meson.build +++ b/meson.build @@ -175,8 +175,8 @@ endif project_inc = include_directories('src') project_libs = [] project_deps = declare_dependency(dependencies: dep_list) # do NOT include ROOT here -project_etc = get_option('sysconfdir') / meson.project_name() -project_data = get_option('datadir') / meson.project_name() +project_etc_dir = get_option('sysconfdir') / meson.project_name() +project_data_dir = get_option('datadir') / meson.project_name() project_test_env = environment() project_pkg_vars = [ 'bindir=' + '${prefix}' / get_option('bindir'), @@ -209,8 +209,8 @@ project_test_env.set( # set preprocessor macros add_project_arguments( '-DIGUANA_PREFIX="' + get_option('prefix') + '"', - '-DIGUANA_ETCDIR="' + project_etc + '"', - '-DIGUANA_DATADIR="' + project_data + '"', + '-DIGUANA_ETCDIR="' + project_etc_dir + '"', + '-DIGUANA_DATADIR="' + project_data_dir + '"', language: ['cpp'], ) @@ -269,7 +269,7 @@ if get_option('z_install_envfile') 'python': get_option('bind_python') ? 'true' : 'false', 'libdir': get_option('libdir'), 'root': ROOT_dep.found() ? 'true' : 'false', - 'etcdir': project_etc, + 'etcdir': project_etc_dir, }, ) foreach shell : [ 'csh', 'tcsh' ] diff --git a/src/iguana/algorithms/meson.build b/src/iguana/algorithms/meson.build index d4067e331..a3b912d64 100644 --- a/src/iguana/algorithms/meson.build +++ b/src/iguana/algorithms/meson.build @@ -218,7 +218,7 @@ install_headers(algo_headers, subdir: meson.project_name() / 'algorithms', prese foreach algo_datum : algo_data install_data( algo_data, - install_dir: project_data / 'algorithms', + install_dir: project_data_dir / 'algorithms', preserve_path: true, ) endforeach @@ -239,5 +239,5 @@ install_headers(vdor_headers, subdir: meson.project_name() / 'algorithms', prese # install config files foreach algo_config : algo_configs - install_data(algo_config, install_dir: project_etc / 'algorithms', preserve_path: true) + install_data(algo_config, install_dir: project_etc_dir / 'algorithms', preserve_path: true) endforeach From dfda3ceff870248a850d47a483a6d461a5b86d13 Mon Sep 17 00:00:00 2001 From: Christopher Dilks Date: Wed, 29 Jan 2025 11:31:33 -0500 Subject: [PATCH 23/36] build: minor changes --- meson.build | 1 + src/iguana/algorithms/Algorithm.h | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/meson.build b/meson.build index 89c439616..5427d5074 100644 --- a/meson.build +++ b/meson.build @@ -8,6 +8,7 @@ project( 'cpp_std': 'c++17', 'buildtype': 'release', 'libdir': 'lib', + 'datadir': 'share', 'licensedir': 'share/licenses/iguana', 'pkgconfig.relocatable': 'true', 'force_fallback_for': ['rcdb'], diff --git a/src/iguana/algorithms/Algorithm.h b/src/iguana/algorithms/Algorithm.h index 8330427a0..edba08e92 100644 --- a/src/iguana/algorithms/Algorithm.h +++ b/src/iguana/algorithms/Algorithm.h @@ -9,7 +9,7 @@ #include "iguana/algorithms/AlgorithmBoilerplate.h" #include "iguana/services/YAMLReader.h" -#include +#include "iguana/services/GlobalParam.h" #include "iguana/services/DataFileReader.h" namespace iguana { From 7a2f98b974af27f4e744de1c3a358d9634ac3142 Mon Sep 17 00:00:00 2001 From: Christopher Dilks Date: Thu, 11 Dec 2025 18:32:39 -0500 Subject: [PATCH 24/36] fix: `project_etcdir -> project_etc_dir` --- src/iguana/bankdefs/meson.build | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/iguana/bankdefs/meson.build b/src/iguana/bankdefs/meson.build index 720a82bdd..81d0dd452 100644 --- a/src/iguana/bankdefs/meson.build +++ b/src/iguana/bankdefs/meson.build @@ -17,4 +17,4 @@ bankdef_tgt = custom_target( ) # 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_etcdir / 'bankdefs' / 'hipo4') +install_data(bankdef_json, install_dir: project_etc_dir / 'bankdefs' / 'hipo4') From b446aff0758171af9519bf04b5e0430457e0954e Mon Sep 17 00:00:00 2001 From: Christopher Dilks Date: Thu, 11 Dec 2025 18:33:19 -0500 Subject: [PATCH 25/36] fix: remove stray conflict marker --- src/iguana/algorithms/meson.build | 1 - 1 file changed, 1 deletion(-) diff --git a/src/iguana/algorithms/meson.build b/src/iguana/algorithms/meson.build index a365e67aa..3f63267bc 100644 --- a/src/iguana/algorithms/meson.build +++ b/src/iguana/algorithms/meson.build @@ -159,7 +159,6 @@ algo_dict = [ 'test_args': { 'banks': [ 'REC::Particle', 'MC::Particle' ], }, ->>>>>>> origin/main }, ] From 8d3f61d44a057a2fe88fdfe5ec8d00e531bf9d05 Mon Sep 17 00:00:00 2001 From: Christopher Dilks Date: Thu, 11 Dec 2025 18:35:58 -0500 Subject: [PATCH 26/36] style: clang auto-format --- src/iguana/algorithms/Algorithm.h | 5 +- .../clas12/LeptonIDFilter/Algorithm.cc | 199 +++++++++--------- .../clas12/LeptonIDFilter/Algorithm.h | 84 ++++---- src/iguana/services/DataFileReader.cc | 3 +- src/iguana/services/DataFileReader.h | 1 - 5 files changed, 144 insertions(+), 148 deletions(-) diff --git a/src/iguana/algorithms/Algorithm.h b/src/iguana/algorithms/Algorithm.h index 57475c946..15322fd29 100644 --- a/src/iguana/algorithms/Algorithm.h +++ b/src/iguana/algorithms/Algorithm.h @@ -8,10 +8,10 @@ #include "AlgorithmBoilerplate.h" #include "iguana/bankdefs/BankDefs.h" +#include "iguana/services/DataFileReader.h" +#include "iguana/services/GlobalParam.h" #include "iguana/services/RCDBReader.h" #include "iguana/services/YAMLReader.h" -#include "iguana/services/GlobalParam.h" -#include "iguana/services/DataFileReader.h" namespace iguana { @@ -320,7 +320,6 @@ namespace iguana { /// Data file reader std::unique_ptr m_datafile_reader; - }; ////////////////////////////////////////////////////////////////////////////// diff --git a/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.cc b/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.cc index 55877dc21..da4a2262e 100644 --- a/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.cc +++ b/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.cc @@ -1,164 +1,163 @@ #include "Algorithm.h" -#include #include +#include namespace iguana::clas12 { - REGISTER_IGUANA_ALGORITHM(LeptonIDFilter , "clas12::LeptonIDFilter"); - -void LeptonIDFilter::initializeTMVA() { - readerTMVA = std::make_unique("V"); - // Initialize the variables for the TMVA reader - readerTMVA->AddVariable( "P", &P ); - readerTMVA->AddVariable( "Theta", &Theta ); - readerTMVA->AddVariable( "Phi", &Phi ); - readerTMVA->AddVariable( "SFPCAL", &PCAL ); - readerTMVA->AddVariable( "SFECIN", &ECIN ); - readerTMVA->AddVariable( "SFECOUT", &ECOUT ); - readerTMVA->AddVariable( "m2PCAL", &m2PCAL ); - readerTMVA->AddVariable( "m2ECIN", &m2ECIN ); - readerTMVA->AddVariable( "m2ECOUT", &m2ECOUT ); - } + REGISTER_IGUANA_ALGORITHM(LeptonIDFilter, "clas12::LeptonIDFilter"); + + void LeptonIDFilter::initializeTMVA() + { + readerTMVA = std::make_unique("V"); + // Initialize the variables for the TMVA reader + readerTMVA->AddVariable("P", &P); + readerTMVA->AddVariable("Theta", &Theta); + readerTMVA->AddVariable("Phi", &Phi); + readerTMVA->AddVariable("SFPCAL", &PCAL); + readerTMVA->AddVariable("SFECIN", &ECIN); + readerTMVA->AddVariable("SFECOUT", &ECOUT); + readerTMVA->AddVariable("m2PCAL", &m2PCAL); + readerTMVA->AddVariable("m2ECIN", &m2ECIN); + readerTMVA->AddVariable("m2ECOUT", &m2ECOUT); + } void LeptonIDFilter::Start(hipo::banklist& banks) { - //Get configuration + // Get configuration ParseYAMLConfig(); - o_pid = GetOptionScalar("pid");//Obtain pid from config file (+11/-11) - o_weightfile = GetOptionScalar("weightfile");//Obtain weightfile from config file + o_pid = GetOptionScalar("pid"); // Obtain pid from config file (+11/-11) + o_weightfile = GetOptionScalar("weightfile"); // Obtain weightfile from config file o_cut = GetOptionScalar("cut"); // load the weights file o_weightfile_fullpath = GetDataFile(o_weightfile); m_log->Debug("Loaded weight file {}", o_weightfile_fullpath); - //Get Banks that we are going to use - b_particle = GetBankIndex(banks, "REC::Particle"); + // Get Banks that we are going to use + b_particle = GetBankIndex(banks, "REC::Particle"); b_calorimeter = GetBankIndex(banks, "REC::Calorimeter"); - - } void LeptonIDFilter::Run(hipo::banklist& banks) const { - auto& particleBank = GetBank(banks, b_particle, "REC::Particle"); + auto& particleBank = GetBank(banks, b_particle, "REC::Particle"); auto& calorimeterBank = GetBank(banks, b_calorimeter, "REC::Calorimeter"); - + ShowBank(particleBank, Logger::Header("INPUT PARTICLES")); // - particleBank.getMutableRowList().filter([this,&particleBank,&calorimeterBank](auto bank, auto row) { - auto lepton_pindex = FindLepton(particleBank); - auto lepton_vars=GetLeptonIDVariables(lepton_pindex,particleBank,calorimeterBank); - lepton_vars.score=CalculateScore(lepton_vars); + particleBank.getMutableRowList().filter([this, &particleBank, &calorimeterBank](auto bank, auto row) { + auto lepton_pindex = FindLepton(particleBank); + auto lepton_vars = GetLeptonIDVariables(lepton_pindex, particleBank, calorimeterBank); + lepton_vars.score = CalculateScore(lepton_vars); - return Filter(lepton_vars.score); - }); + return Filter(lepton_vars.score); + }); // dump the modified bank ShowBank(particleBank, Logger::Header("OUTPUT PARTICLES")); - } - int LeptonIDFilter::FindLepton(hipo::bank const& particle_bank) const{ - int lepton_pindex= -1; + int LeptonIDFilter::FindLepton(hipo::bank const& particle_bank) const + { + int lepton_pindex = -1; for(int row = 0; row < particle_bank.getRows(); row++) { - auto status = particle_bank.getShort("status", row); - if(particle_bank.getInt("pid", row) == o_pid && abs(status)>=2000 && abs(status)<4000) { - lepton_pindex=row; - break; - } + auto status = particle_bank.getShort("status", row); + if(particle_bank.getInt("pid", row) == o_pid && abs(status) >= 2000 && abs(status) < 4000) { + lepton_pindex = row; + break; } - if(lepton_pindex >= 0) - m_log->Debug("Found lepton: pindex={}", lepton_pindex); - else - m_log->Debug("Lepton not found"); - return lepton_pindex; + } + if(lepton_pindex >= 0) + m_log->Debug("Found lepton: pindex={}", lepton_pindex); + else + m_log->Debug("Lepton not found"); + return lepton_pindex; } - LeptonIDVars LeptonIDFilter::GetLeptonIDVariables(int const plepton, hipo::bank const& particle_bank, hipo::bank const& calorimeter_bank) const{ - - double px = particle_bank.getFloat("px", plepton); - double py = particle_bank.getFloat("py", plepton); - double pz = particle_bank.getFloat("pz", plepton); - double E = std::sqrt(std::pow(px, 2) + std::pow(py, 2) + std::pow(pz, 2) + std::pow(0.000511, 2)); - ROOT::Math::PxPyPzMVector vec_lepton(px, py, pz, E); + LeptonIDVars LeptonIDFilter::GetLeptonIDVariables(int const plepton, hipo::bank const& particle_bank, hipo::bank const& calorimeter_bank) const + { - LeptonIDVars lepton; + double px = particle_bank.getFloat("px", plepton); + double py = particle_bank.getFloat("py", plepton); + double pz = particle_bank.getFloat("pz", plepton); + double E = std::sqrt(std::pow(px, 2) + std::pow(py, 2) + std::pow(pz, 2) + std::pow(0.000511, 2)); + ROOT::Math::PxPyPzMVector vec_lepton(px, py, pz, E); - lepton.P =vec_lepton.P(); - lepton.Theta=vec_lepton.Theta(); - lepton.Phi =vec_lepton.Phi(); + LeptonIDVars lepton; - m_log->Debug("Variables obtained from particle bank"); + lepton.P = vec_lepton.P(); + lepton.Theta = vec_lepton.Theta(); + lepton.Phi = vec_lepton.Phi(); + m_log->Debug("Variables obtained from particle bank"); - lepton.m2pcal=-1; - lepton.m2ecin=-1; - lepton.m2ecout=-1; - for(int row = 0; row < calorimeter_bank.getRows(); row++) { - auto pindex = calorimeter_bank.getShort("pindex",row); - auto layer = calorimeter_bank.getByte("layer",row); - auto energy = calorimeter_bank.getFloat("energy",row); - auto m2u = calorimeter_bank.getFloat("m2u",row); - auto m2v = calorimeter_bank.getFloat("m2v",row); - auto m2w = calorimeter_bank.getFloat("m2w",row); + lepton.m2pcal = -1; + lepton.m2ecin = -1; + lepton.m2ecout = -1; - if(pindex==plepton && layer==1) { - lepton.SFpcal=energy/vec_lepton.P(); - lepton.m2pcal=(m2u+m2v+m2w)/3; - } + for(int row = 0; row < calorimeter_bank.getRows(); row++) { + auto pindex = calorimeter_bank.getShort("pindex", row); + auto layer = calorimeter_bank.getByte("layer", row); + auto energy = calorimeter_bank.getFloat("energy", row); + auto m2u = calorimeter_bank.getFloat("m2u", row); + auto m2v = calorimeter_bank.getFloat("m2v", row); + auto m2w = calorimeter_bank.getFloat("m2w", row); - if(pindex==plepton && layer==4) { - lepton.SFecin=energy/vec_lepton.P(); - lepton.m2ecin=(m2u+m2v+m2w)/3; - } - if(pindex==plepton && layer==7) { - lepton.SFecout=energy/vec_lepton.P(); - lepton.m2ecout=(m2u+m2v+m2w)/3; - } + if(pindex == plepton && layer == 1) { + lepton.SFpcal = energy / vec_lepton.P(); + lepton.m2pcal = (m2u + m2v + m2w) / 3; + } + if(pindex == plepton && layer == 4) { + lepton.SFecin = energy / vec_lepton.P(); + lepton.m2ecin = (m2u + m2v + m2w) / 3; + } + if(pindex == plepton && layer == 7) { + lepton.SFecout = energy / vec_lepton.P(); + lepton.m2ecout = (m2u + m2v + m2w) / 3; } + } - - m_log->Debug("Variables obtained from calorimeter bank"); - return lepton; + m_log->Debug("Variables obtained from calorimeter bank"); + return lepton; } - double LeptonIDFilter::CalculateScore(LeptonIDVars lepton_vars) const{ - - ///Assing variables from lepton_vars for TMVA method - P=lepton_vars.P; - Theta=lepton_vars.Theta; - Phi=lepton_vars.Phi; - PCAL=lepton_vars.SFpcal; - ECIN=lepton_vars.SFecin; - ECOUT=lepton_vars.SFecout; - m2PCAL=lepton_vars.m2pcal; - m2ECIN=lepton_vars.m2ecin; - m2ECOUT=lepton_vars.m2ecout; - - m_log->Debug("Add variables to readerTMVA"); - auto score=readerTMVA->EvaluateMVA("BDT"); + double LeptonIDFilter::CalculateScore(LeptonIDVars lepton_vars) const + { - return score; + /// Assing variables from lepton_vars for TMVA method + P = lepton_vars.P; + Theta = lepton_vars.Theta; + Phi = lepton_vars.Phi; + PCAL = lepton_vars.SFpcal; + ECIN = lepton_vars.SFecin; + ECOUT = lepton_vars.SFecout; + m2PCAL = lepton_vars.m2pcal; + m2ECIN = lepton_vars.m2ecin; + m2ECOUT = lepton_vars.m2ecout; + + m_log->Debug("Add variables to readerTMVA"); + auto score = readerTMVA->EvaluateMVA("BDT"); + + return score; } - bool LeptonIDFilter::Filter(double score) const{ - if(score>=o_cut) + bool LeptonIDFilter::Filter(double score) const + { + if(score >= o_cut) return true; else return false; } - void LeptonIDFilter::Stop() { } diff --git a/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.h b/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.h index 621fe8e62..9c3eb9c2d 100644 --- a/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.h +++ b/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.h @@ -3,29 +3,29 @@ #include "iguana/algorithms/Algorithm.h" #include -///Struct to store variables - struct LeptonIDVars { - /// @brief momentum - double P; - /// @brief Theta angle - double Theta; - /// @brief Phi angle - double Phi; - /// @brief Sampling fraction on the PCAL - double SFpcal; - /// @brief Sampling fraction on the ECIN - double SFecin; - /// @brief Sampling fraction on the ECOUT - double SFecout; - /// @brief Second-momenta of PCAL - double m2pcal; - /// @brief Second-momenta of ECIN - double m2ecin; - /// @brief Second-momenta of ECOUT - double m2ecout; - /// @brief Score - double score; - }; +/// Struct to store variables +struct LeptonIDVars { + /// @brief momentum + double P; + /// @brief Theta angle + double Theta; + /// @brief Phi angle + double Phi; + /// @brief Sampling fraction on the PCAL + double SFpcal; + /// @brief Sampling fraction on the ECIN + double SFecin; + /// @brief Sampling fraction on the ECOUT + double SFecout; + /// @brief Second-momenta of PCAL + double m2pcal; + /// @brief Second-momenta of ECIN + double m2ecin; + /// @brief Second-momenta of ECOUT + double m2ecout; + /// @brief Score + double score; +}; namespace iguana::clas12 { /// @@ -62,7 +62,7 @@ namespace iguana::clas12 { /// @returns pindex of the lepton, -1 if there is no lepton int FindLepton(hipo::bank const& particle_bank) const; - + /// **GetLeptonIDVariables function**: Using the pindex retrieves the necessary variables from banks /// @param plepton pindex of the lepton /// @param particle_bank the particle bank @@ -76,46 +76,44 @@ namespace iguana::clas12 { /// @returns double, the score double CalculateScore(LeptonIDVars lepton_vars) const; - /// **Filter function**: Returns true if the particle passed the cut + /// **Filter function**: Returns true if the particle passed the cut /// @param score the score obtained from the CalculateScore function /// @returns bool, true if score>=cut, false otherwise bool Filter(double score) const; - - + + private: - std::unique_ptr readerTMVA; - + std::unique_ptr readerTMVA; - ///Set of variables for the reader - ///Momentum + + /// Set of variables for the reader + /// Momentum mutable Float_t P; - ///Theta angle + /// Theta angle mutable Float_t Theta; - ///Phi angle + /// Phi angle mutable Float_t Phi; - ///Sampling fraction on the PCAL + /// Sampling fraction on the PCAL mutable Float_t PCAL; - ///Sampling fraction on the ECIN + /// Sampling fraction on the ECIN mutable Float_t ECIN; - ///Sampling fraction on the ECOUT + /// Sampling fraction on the ECOUT mutable Float_t ECOUT; - ///Second-momenta of PCAL + /// Second-momenta of PCAL mutable Float_t m2PCAL; - ///Second-momenta of ECIN + /// Second-momenta of ECIN mutable Float_t m2ECIN; - ///Second-momenta of ECOUT + /// Second-momenta of ECOUT mutable Float_t m2ECOUT; - - /// `hipo::banklist` + + /// `hipo::banklist` hipo::banklist::size_type b_particle; hipo::banklist::size_type b_calorimeter; - - /// pid of the lepton int o_pid; diff --git a/src/iguana/services/DataFileReader.cc b/src/iguana/services/DataFileReader.cc index 4e1f18e49..797bc954f 100644 --- a/src/iguana/services/DataFileReader.cc +++ b/src/iguana/services/DataFileReader.cc @@ -1,7 +1,8 @@ #include "DataFileReader.h" namespace iguana { - DataFileReader::DataFileReader(std::string_view datadir_subdir, std::string_view name) : ConfigFileReader(name, false) + DataFileReader::DataFileReader(std::string_view datadir_subdir, std::string_view name) + : ConfigFileReader(name, false) { // first, add the "fallback" directory; this is the lowest priority directory, relying // on the environment variable '$IGUANA', in case the higher-priority, build-time path fails diff --git a/src/iguana/services/DataFileReader.h b/src/iguana/services/DataFileReader.h index a2d31143c..11bb0f18b 100644 --- a/src/iguana/services/DataFileReader.h +++ b/src/iguana/services/DataFileReader.h @@ -14,6 +14,5 @@ namespace iguana { /// @param name of this reader (for `Logger`) DataFileReader(std::string_view datadir_subdir = "", std::string_view name = "data_file"); ~DataFileReader() {} - }; } From 315a9850d118036743d373a3ca9006f0c137d73c Mon Sep 17 00:00:00 2001 From: Christopher Dilks Date: Thu, 11 Dec 2025 18:51:18 -0500 Subject: [PATCH 27/36] fix: docstrings, `Run` function, and particle bank config param --- .../clas12/LeptonIDFilter/Algorithm.cc | 24 ++++++---- .../clas12/LeptonIDFilter/Algorithm.h | 44 ++++++++----------- .../clas12/LeptonIDFilter/Config.yaml | 3 ++ 3 files changed, 37 insertions(+), 34 deletions(-) diff --git a/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.cc b/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.cc index da4a2262e..4893fb945 100644 --- a/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.cc +++ b/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.cc @@ -10,7 +10,6 @@ namespace iguana::clas12 { void LeptonIDFilter::initializeTMVA() { readerTMVA = std::make_unique("V"); - // Initialize the variables for the TMVA reader readerTMVA->AddVariable("P", &P); readerTMVA->AddVariable("Theta", &Theta); readerTMVA->AddVariable("Phi", &Phi); @@ -22,28 +21,36 @@ namespace iguana::clas12 { readerTMVA->AddVariable("m2ECOUT", &m2ECOUT); } + void LeptonIDFilter::Start(hipo::banklist& banks) { // Get configuration ParseYAMLConfig(); - o_pid = GetOptionScalar("pid"); // Obtain pid from config file (+11/-11) - o_weightfile = GetOptionScalar("weightfile"); // Obtain weightfile from config file - o_cut = GetOptionScalar("cut"); + o_pid = GetOptionScalar("pid"); // Obtain pid from config file (+11/-11) + o_weightfile = GetOptionScalar("weightfile"); // Obtain weightfile from config file + o_cut = GetOptionScalar("cut"); + o_particle_bank = GetOptionScalar("particle_bank"); // load the weights file o_weightfile_fullpath = GetDataFile(o_weightfile); m_log->Debug("Loaded weight file {}", o_weightfile_fullpath); // Get Banks that we are going to use - b_particle = GetBankIndex(banks, "REC::Particle"); + b_particle = GetBankIndex(banks, o_particle_bank); b_calorimeter = GetBankIndex(banks, "REC::Calorimeter"); } - void LeptonIDFilter::Run(hipo::banklist& banks) const + bool LeptonIDFilter::Run(hipo::banklist& banks) const + { + return Run( + GetBank(banks, b_particle, o_particle_bank), + GetBank(banks, b_calorimeter, "REC::Calorimeter")); + } + + + bool LeptonIDFilter::Run(hipo::bank& particleBank, hipo::bank const& calorimeterBank) const { - auto& particleBank = GetBank(banks, b_particle, "REC::Particle"); - auto& calorimeterBank = GetBank(banks, b_calorimeter, "REC::Calorimeter"); ShowBank(particleBank, Logger::Header("INPUT PARTICLES")); @@ -58,6 +65,7 @@ namespace iguana::clas12 { // dump the modified bank ShowBank(particleBank, Logger::Header("OUTPUT PARTICLES")); + return !particleBank.getRowList().empty(); } diff --git a/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.h b/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.h index 9c3eb9c2d..140163791 100644 --- a/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.h +++ b/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.h @@ -28,19 +28,15 @@ struct LeptonIDVars { }; namespace iguana::clas12 { - /// - /// @brief_algo Filter the leptons from the pion contamination using TMVA models + /// @algo_brief{Filter the leptons from the pion contamination using TMVA models} + /// @algo_type_filter /// /// For each lepton, either positron or electron, it takes some variables from `REC::Particle` (P, Theta and Phi) and `REC::Particle` (Sampling fraction and second moments). /// Using those variables, it call the TMVA method using the weight file, and it computes a score. By a pplying a cut to the score we can separate leptons from pions. /// - /// @begin_doc_algo{clas12::LeptonIDFilter | Filter} - /// @input_banks{REC::Particle,REC::Calorimeter} - /// @end_doc - /// - /// @begin_doc_config + /// @begin_doc_config{clas12/LeptonIDFilter} /// @config_param{o_pid | int | PID of the particle; -11 for positrons and 11 for electrons} - /// @config_param{o_weightfile | std::string | Location of the weight file of the classifier} + /// @config_param{o_weightfile | string | Location of the weight file of the classifier} /// @config_param{o_cut | double | Value of the score to apply the cut. The algorith will keep all particles that have a score grater than ths value} /// @end_doc class LeptonIDFilter : public Algorithm @@ -49,46 +45,45 @@ namespace iguana::clas12 { DEFINE_IGUANA_ALGORITHM(LeptonIDFilter, clas12::LeptonIDFilter) public: - // Constructor void Start(hipo::banklist& banks) override; - void Run(hipo::banklist& banks) const override; + bool Run(hipo::banklist& banks) const override; void Stop() override; + /// @run_function + /// @param [in,out] particleBank particle bank (_viz._, `REC::Particle`), which will be filtered + /// @param [in] calorimeterBank `REC::Calorimeter` bank + /// @returns `false` if all particles are filtered out + bool Run(hipo::bank& particleBank, hipo::bank const& calorimeterBank) const; + + /// @brief Initialize the variables for the TMVA reader void initializeTMVA(); - /// **FindLepton function**: returns the pindex of the lepton + /// @brief returns the pindex of the lepton /// @param particle_bank the particle bank /// @returns pindex of the lepton, -1 if there is no lepton int FindLepton(hipo::bank const& particle_bank) const; - - /// **GetLeptonIDVariables function**: Using the pindex retrieves the necessary variables from banks + /// @brief Using the pindex, retrieves the necessary variables from banks /// @param plepton pindex of the lepton /// @param particle_bank the particle bank /// @param calorimeter_bank the calorimeter bank /// @returns LeptonIDVars, the variables required for identification LeptonIDVars GetLeptonIDVariables(int const plepton, hipo::bank const& particle_bank, hipo::bank const& calorimeter_bank) const; - - /// **CalculateScore function**: Using the LeptonIDVars variables calculate the score + /// @brief Using the LeptonIDVars, variables calculate the score /// @param lepton_vars LeptonIDVars variables /// @returns double, the score double CalculateScore(LeptonIDVars lepton_vars) const; - /// **Filter function**: Returns true if the particle passed the cut + /// @brief Returns true if the particle passed the cut /// @param score the score obtained from the CalculateScore function /// @returns bool, true if score>=cut, false otherwise bool Filter(double score) const; - - - - private: std::unique_ptr readerTMVA; - /// Set of variables for the reader /// Momentum mutable Float_t P; @@ -109,19 +104,16 @@ namespace iguana::clas12 { /// Second-momenta of ECOUT mutable Float_t m2ECOUT; - /// `hipo::banklist` hipo::banklist::size_type b_particle; hipo::banklist::size_type b_calorimeter; - - /// pid of the lepton + // config options int o_pid; - /// Location of the weight file std::string o_weightfile; std::string o_weightfile_fullpath; - /// Value of the cut double o_cut; + std::string o_particle_bank; }; } diff --git a/src/iguana/algorithms/clas12/LeptonIDFilter/Config.yaml b/src/iguana/algorithms/clas12/LeptonIDFilter/Config.yaml index 0c1169942..1d869be5d 100644 --- a/src/iguana/algorithms/clas12/LeptonIDFilter/Config.yaml +++ b/src/iguana/algorithms/clas12/LeptonIDFilter/Config.yaml @@ -2,3 +2,6 @@ clas12::LeptonIDFilter: pid: -11 weightfile: "weights/9_BDT_positrons_S19.weights.xml" cut: 0.0 + + # the name of the particle bank, only needed for users of `hipo::banklist` + particle_bank: 'REC::Particle' From ee1b99b01b5cfd0c20283acb3e2770ad89620fdf Mon Sep 17 00:00:00 2001 From: Christopher Dilks Date: Thu, 11 Dec 2025 18:56:31 -0500 Subject: [PATCH 28/36] fix: CODEOWNERS --- CODEOWNERS | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CODEOWNERS b/CODEOWNERS index 266912123..dca59b932 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -6,6 +6,8 @@ # @RichCap Richard Capobianco # @rtysonCLAS12 Richard Tyson # @Gregtom3 Gregory Matousek +# @MarianaT27 Mariana Tenorio Pita +# @tbhayward Timothy Hayward ################################################################################# * @c-dilks @@ -13,6 +15,7 @@ src/iguana/algorithms/clas12/CalorimeterLinker/* @c-dilks src/iguana/algorithms/clas12/EventBuilderFilter/* @c-dilks src/iguana/algorithms/clas12/FTEnergyCorrection/* @asligonulacar src/iguana/algorithms/clas12/FiducialFilter/* @Gregtom3 +src/iguana/algorithms/clas12/LeptonIDFilter/* @MarianaT27 src/iguana/algorithms/clas12/MatchParticleProximity/* @c-dilks src/iguana/algorithms/clas12/MomentumCorrection/* @RichCap @c-dilks src/iguana/algorithms/clas12/PhotonGBTFilter/* @Gregtom3 From 8e6f824e8732260403f7faf4b82c6147ef0faa64 Mon Sep 17 00:00:00 2001 From: Christopher Dilks Date: Wed, 17 Dec 2025 17:36:19 -0500 Subject: [PATCH 29/36] fix: book the model and mutex-lock the `Run` function --- .../clas12/LeptonIDFilter/Algorithm.cc | 21 ++++++++++++++++--- .../clas12/LeptonIDFilter/Algorithm.h | 9 +++++--- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.cc b/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.cc index 4893fb945..ae6d6123b 100644 --- a/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.cc +++ b/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.cc @@ -9,7 +9,11 @@ namespace iguana::clas12 { void LeptonIDFilter::initializeTMVA() { + // initialize the reader + if(readerTMVA) + return; readerTMVA = std::make_unique("V"); + // initialize the variables readerTMVA->AddVariable("P", &P); readerTMVA->AddVariable("Theta", &Theta); readerTMVA->AddVariable("Phi", &Phi); @@ -19,6 +23,8 @@ namespace iguana::clas12 { readerTMVA->AddVariable("m2PCAL", &m2PCAL); readerTMVA->AddVariable("m2ECIN", &m2ECIN); readerTMVA->AddVariable("m2ECOUT", &m2ECOUT); + // book the BDT method with the weights file + readerTMVA->BookMVA("BDT", o_weightfile_fullpath); } @@ -38,6 +44,9 @@ namespace iguana::clas12 { // Get Banks that we are going to use b_particle = GetBankIndex(banks, o_particle_bank); b_calorimeter = GetBankIndex(banks, "REC::Calorimeter"); + + // Initialize the TMVA reader + initializeTMVA(); } @@ -51,10 +60,13 @@ namespace iguana::clas12 { bool LeptonIDFilter::Run(hipo::bank& particleBank, hipo::bank const& calorimeterBank) const { + // mutex-lock the whole `Run` function, since it needs to mutate and use several `mutable Float_t` members + std::scoped_lock lock(m_mutex); + // particle bank before filtering ShowBank(particleBank, Logger::Header("INPUT PARTICLES")); - // + // filter the particle bank particleBank.getMutableRowList().filter([this, &particleBank, &calorimeterBank](auto bank, auto row) { auto lepton_pindex = FindLepton(particleBank); auto lepton_vars = GetLeptonIDVariables(lepton_pindex, particleBank, calorimeterBank); @@ -63,7 +75,7 @@ namespace iguana::clas12 { return Filter(lepton_vars.score); }); - // dump the modified bank + // particle bank after filtering ShowBank(particleBank, Logger::Header("OUTPUT PARTICLES")); return !particleBank.getRowList().empty(); } @@ -86,6 +98,7 @@ namespace iguana::clas12 { return lepton_pindex; } + LeptonIDVars LeptonIDFilter::GetLeptonIDVariables(int const plepton, hipo::bank const& particle_bank, hipo::bank const& calorimeter_bank) const { @@ -137,10 +150,11 @@ namespace iguana::clas12 { return lepton; } + double LeptonIDFilter::CalculateScore(LeptonIDVars lepton_vars) const { - /// Assing variables from lepton_vars for TMVA method + // Assigning variables from lepton_vars for TMVA method P = lepton_vars.P; Theta = lepton_vars.Theta; Phi = lepton_vars.Phi; @@ -157,6 +171,7 @@ namespace iguana::clas12 { return score; } + bool LeptonIDFilter::Filter(double score) const { if(score >= o_cut) diff --git a/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.h b/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.h index 140163791..dc30f96df 100644 --- a/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.h +++ b/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.h @@ -54,11 +54,9 @@ namespace iguana::clas12 { /// @param [in,out] particleBank particle bank (_viz._, `REC::Particle`), which will be filtered /// @param [in] calorimeterBank `REC::Calorimeter` bank /// @returns `false` if all particles are filtered out + /// @note Multithreading users should be aware that this entire function is mutex-locked. bool Run(hipo::bank& particleBank, hipo::bank const& calorimeterBank) const; - /// @brief Initialize the variables for the TMVA reader - void initializeTMVA(); - /// @brief returns the pindex of the lepton /// @param particle_bank the particle bank /// @returns pindex of the lepton, -1 if there is no lepton @@ -82,6 +80,11 @@ namespace iguana::clas12 { bool Filter(double score) const; private: + + /// @brief Initialize the variables for the TMVA reader + void initializeTMVA(); + + /// TMVA reader std::unique_ptr readerTMVA; /// Set of variables for the reader From e8a3e4e9d0f91716493a82297baf8375a8c2118c Mon Sep 17 00:00:00 2001 From: Christopher Dilks Date: Wed, 17 Dec 2025 17:49:16 -0500 Subject: [PATCH 30/36] fix: suppress `TMVA` leak --- meson/lsan.supp | 1 + 1 file changed, 1 insertion(+) diff --git a/meson/lsan.supp b/meson/lsan.supp index 0bf7473ac..f62dc34b2 100644 --- a/meson/lsan.supp +++ b/meson/lsan.supp @@ -1 +1,2 @@ leak:libCling.so +leak:TMVA::DataSetFactory::BuildDynamicDataSet From ace50c58b371cd1ae562ce2deb859160c0bcecc2 Mon Sep 17 00:00:00 2001 From: Christopher Dilks Date: Fri, 19 Dec 2025 19:50:55 -0500 Subject: [PATCH 31/36] feat: add `Algorithm::GetOptionNode` and support multiple weights files --- src/iguana/algorithms/Algorithm.cc | 13 ++++ src/iguana/algorithms/Algorithm.h | 5 ++ .../clas12/LeptonIDFilter/Algorithm.cc | 60 ++++++++++--------- .../clas12/LeptonIDFilter/Algorithm.h | 10 ++-- .../clas12/LeptonIDFilter/Config.yaml | 33 +++++++++- src/iguana/services/YAMLReader.cc | 28 +++++---- src/iguana/services/YAMLReader.h | 5 ++ 7 files changed, 108 insertions(+), 46 deletions(-) diff --git a/src/iguana/algorithms/Algorithm.cc b/src/iguana/algorithms/Algorithm.cc index 9ca4e30a0..96e1f5e49 100644 --- a/src/iguana/algorithms/Algorithm.cc +++ b/src/iguana/algorithms/Algorithm.cc @@ -86,6 +86,19 @@ namespace iguana { /////////////////////////////////////////////////////////////////////////////// + YAML::Node Algorithm::GetOptionNode(YAMLReader::node_path_t node_path) const + { + CompleteOptionNodePath("", node_path); + auto node = m_yaml_config->GetNode(node_path); + if(!node.has_value()) { + m_log->Error("Algorithm::GetOptionNode failed to find node"); + throw std::runtime_error("config file parsing issue"); + } + return node.value(); + } + + /////////////////////////////////////////////////////////////////////////////// + void Algorithm::SetName(std::string_view name) { Object::SetName(name); diff --git a/src/iguana/algorithms/Algorithm.h b/src/iguana/algorithms/Algorithm.h index 15322fd29..dfb134e18 100644 --- a/src/iguana/algorithms/Algorithm.h +++ b/src/iguana/algorithms/Algorithm.h @@ -145,6 +145,11 @@ namespace iguana { template std::set GetOptionSet(std::string const& key, YAMLReader::node_path_t node_path = {}) const; + /// Get the `YAML::Node` for an option + /// @param node_path the `YAML::Node` identifier path to search + /// @returns the `YAML::Node` + YAML::Node GetOptionNode(YAMLReader::node_path_t node_path) const; + /// Set the name of this algorithm /// @param name the new name void SetName(std::string_view name); diff --git a/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.cc b/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.cc index ae6d6123b..42e70abb2 100644 --- a/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.cc +++ b/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.cc @@ -7,46 +7,52 @@ namespace iguana::clas12 { REGISTER_IGUANA_ALGORITHM(LeptonIDFilter, "clas12::LeptonIDFilter"); - void LeptonIDFilter::initializeTMVA() - { - // initialize the reader - if(readerTMVA) - return; - readerTMVA = std::make_unique("V"); - // initialize the variables - readerTMVA->AddVariable("P", &P); - readerTMVA->AddVariable("Theta", &Theta); - readerTMVA->AddVariable("Phi", &Phi); - readerTMVA->AddVariable("SFPCAL", &PCAL); - readerTMVA->AddVariable("SFECIN", &ECIN); - readerTMVA->AddVariable("SFECOUT", &ECOUT); - readerTMVA->AddVariable("m2PCAL", &m2PCAL); - readerTMVA->AddVariable("m2ECIN", &m2ECIN); - readerTMVA->AddVariable("m2ECOUT", &m2ECOUT); - // book the BDT method with the weights file - readerTMVA->BookMVA("BDT", o_weightfile_fullpath); - } - - void LeptonIDFilter::Start(hipo::banklist& banks) { // Get configuration ParseYAMLConfig(); o_pid = GetOptionScalar("pid"); // Obtain pid from config file (+11/-11) - o_weightfile = GetOptionScalar("weightfile"); // Obtain weightfile from config file o_cut = GetOptionScalar("cut"); o_particle_bank = GetOptionScalar("particle_bank"); - - // load the weights file - o_weightfile_fullpath = GetDataFile(o_weightfile); - m_log->Debug("Loaded weight file {}", o_weightfile_fullpath); + o_runnum = ConcurrentParamFactory::Create(); + o_weightfile = ConcurrentParamFactory::Create(); // Get Banks that we are going to use b_particle = GetBankIndex(banks, o_particle_bank); b_calorimeter = GetBankIndex(banks, "REC::Calorimeter"); // Initialize the TMVA reader - initializeTMVA(); + readerTMVA = std::make_unique("V"); + + // initialize TMVA variables + readerTMVA->AddVariable("P", &P); + readerTMVA->AddVariable("Theta", &Theta); + readerTMVA->AddVariable("Phi", &Phi); + readerTMVA->AddVariable("SFPCAL", &PCAL); + readerTMVA->AddVariable("SFECIN", &ECIN); + readerTMVA->AddVariable("SFECOUT", &ECOUT); + readerTMVA->AddVariable("m2PCAL", &m2PCAL); + readerTMVA->AddVariable("m2ECIN", &m2ECIN); + readerTMVA->AddVariable("m2ECOUT", &m2ECOUT); + + // find all the unique weights files in the configuration YAML + std::set weightfile_list; + for(auto const& node : GetOptionNode({"weightfile"})) { + for(std::string const particle : {"electron", "positron"}) { + if(node[particle]) { + weightfile_list.insert(node[particle].as()); + } + } + } + + // book the weights files + // use the name of weightfile as the method tag, for simplicity + m_log->Debug("Booking weight files:"); + for(auto const& weightfile_name : weightfile_list) { + auto weightfile_path = GetDataFile(weightfile_name); + m_log->Debug(" - {}", weightfile_path); + readerTMVA->BookMVA(weightfile_name, weightfile_path); + } } diff --git a/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.h b/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.h index dc30f96df..9fabf5599 100644 --- a/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.h +++ b/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.h @@ -1,6 +1,7 @@ #pragma once #include "iguana/algorithms/Algorithm.h" +#include "iguana/services/ConcurrentParam.h" #include /// Struct to store variables @@ -37,7 +38,7 @@ namespace iguana::clas12 { /// @begin_doc_config{clas12/LeptonIDFilter} /// @config_param{o_pid | int | PID of the particle; -11 for positrons and 11 for electrons} /// @config_param{o_weightfile | string | Location of the weight file of the classifier} - /// @config_param{o_cut | double | Value of the score to apply the cut. The algorith will keep all particles that have a score grater than ths value} + /// @config_param{o_cut | double | Value of the score to apply the cut. The algorithm will keep all particles that have a score grater than ths value} /// @end_doc class LeptonIDFilter : public Algorithm { @@ -81,9 +82,6 @@ namespace iguana::clas12 { private: - /// @brief Initialize the variables for the TMVA reader - void initializeTMVA(); - /// TMVA reader std::unique_ptr readerTMVA; @@ -113,8 +111,8 @@ namespace iguana::clas12 { // config options int o_pid; - std::string o_weightfile; - std::string o_weightfile_fullpath; + mutable std::unique_ptr> o_runnum; + mutable std::unique_ptr> o_weightfile; double o_cut; std::string o_particle_bank; }; diff --git a/src/iguana/algorithms/clas12/LeptonIDFilter/Config.yaml b/src/iguana/algorithms/clas12/LeptonIDFilter/Config.yaml index 1d869be5d..2ec4efbd9 100644 --- a/src/iguana/algorithms/clas12/LeptonIDFilter/Config.yaml +++ b/src/iguana/algorithms/clas12/LeptonIDFilter/Config.yaml @@ -1,6 +1,37 @@ clas12::LeptonIDFilter: pid: -11 - weightfile: "weights/9_BDT_positrons_S19.weights.xml" + + # weight file, one for each lepton and dataset + weightfile: + # + # + # FIXME: double check these run number ranges + # FIXME: use the correct weights files for each + # + # + - runs: [3211, 3293] # RG-A Spring 2018 Outbending Part 1 + electron: 'weights/9_BDT_positrons_S19.weights.xml' + positron: 'weights/9_BDT_positrons_S19.weights.xml' + - runs: [3306, 3817] # RG-A Spring 2018 Inbending Part 1 + electron: 'weights/9_BDT_positrons_S19.weights.xml' + positron: 'weights/9_BDT_positrons_S19.weights.xml' + - runs: [3863, 3987] # RG-A Spring 2018 Outbending Part 2 + electron: 'weights/9_BDT_positrons_S19.weights.xml' + positron: 'weights/9_BDT_positrons_S19.weights.xml' + - runs: [4003, 4325] # RG-A Spring 2018 Inbending Part 2 + electron: 'weights/9_BDT_positrons_S19.weights.xml' + positron: 'weights/9_BDT_positrons_S19.weights.xml' + - runs: [5032, 5419] # RG-A Fall 2018 Inbending + electron: 'weights/9_BDT_positrons_S19.weights.xml' + positron: 'weights/9_BDT_positrons_S19.weights.xml' + - runs: [5422, 5666] # RG-A Fall 2018 Outbending + electron: 'weights/9_BDT_positrons_S19.weights.xml' + positron: 'weights/9_BDT_positrons_S19.weights.xml' + - runs: [6616, 6783] # RG-A Spring 2019 Inbending + electron: 'weights/9_BDT_positrons_S19.weights.xml' + positron: 'weights/9_BDT_positrons_S19.weights.xml' + + # value of the score to apply the cut cut: 0.0 # the name of the particle bank, only needed for users of `hipo::banklist` diff --git a/src/iguana/services/YAMLReader.cc b/src/iguana/services/YAMLReader.cc index 37d301606..d0edd8c26 100644 --- a/src/iguana/services/YAMLReader.cc +++ b/src/iguana/services/YAMLReader.cc @@ -21,6 +21,18 @@ namespace iguana { /////////////////////////////////////////////////////////////////////////////// + std::optional YAMLReader::GetNode(node_path_t node_path) + { + for(auto const& [config, filename] : m_configs) { + auto node = FindNode(config, node_path); + if(node.IsDefined() && !node.IsNull()) + return node; + } + return std::nullopt; + } + + /////////////////////////////////////////////////////////////////////////////// + template std::optional YAMLReader::GetScalar(YAML::Node node) { @@ -46,12 +58,8 @@ namespace iguana { template std::optional YAMLReader::GetScalar(node_path_t node_path) { - for(auto const& [config, filename] : m_configs) { - auto node = FindNode(config, node_path); - if(node.IsDefined() && !node.IsNull()) - return GetScalar(node); - } - return std::nullopt; + auto node = GetNode(node_path); + return node ? GetScalar(node.value()) : std::nullopt; } template std::optional YAMLReader::GetScalar(node_path_t node_path); template std::optional YAMLReader::GetScalar(node_path_t node_path); @@ -87,12 +95,8 @@ namespace iguana { template std::optional> YAMLReader::GetVector(node_path_t node_path) { - for(auto const& [config, filename] : m_configs) { - auto node = FindNode(config, node_path); - if(node.IsDefined() && !node.IsNull()) - return GetVector(node); - } - return std::nullopt; + auto node = GetNode(node_path); + return node ? GetVector(node.value()) : std::nullopt; } template std::optional> YAMLReader::GetVector(node_path_t node_path); template std::optional> YAMLReader::GetVector(node_path_t node_path); diff --git a/src/iguana/services/YAMLReader.h b/src/iguana/services/YAMLReader.h index 79408d0d9..4f30713cf 100644 --- a/src/iguana/services/YAMLReader.h +++ b/src/iguana/services/YAMLReader.h @@ -36,6 +36,11 @@ namespace iguana { /// Parse the YAML files added by `ConfigFileReader::AddFile` void LoadFiles(); + /// Get a `YAML::Node`, given a `YAML::Node` path + /// @param node_path the `YAML::Node` path + /// @return the `YAML::Node`, if found + std::optional GetNode(node_path_t node_path); + /// Read a scalar value from a `YAML::Node` /// @param node the `YAML::Node` to read /// @return the scalar, if found From 122b2c066be3af8759918e3bd6bd0072b187effc Mon Sep 17 00:00:00 2001 From: Christopher Dilks Date: Sun, 21 Dec 2025 11:38:04 -0500 Subject: [PATCH 32/36] style: `-DOPT` -> `-D OPT` --- meson.build | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/meson.build b/meson.build index 290007245..368fc4b50 100644 --- a/meson.build +++ b/meson.build @@ -192,13 +192,13 @@ project_test_env.set( # set preprocessor macros add_project_arguments( - '-DIGUANA_PREFIX="' + get_option('prefix') + '"', - '-DIGUANA_ETCDIR="' + project_etc_dir + '"', - '-DIGUANA_DATADIR="' + project_data_dir + '"', + '-D IGUANA_PREFIX="' + get_option('prefix') + '"', + '-D IGUANA_ETCDIR="' + project_etc_dir + '"', + '-D IGUANA_DATADIR="' + project_data_dir + '"', language: ['cpp'], ) if ROOT_dep.found() - add_project_arguments('-DIGUANA_ROOT_FOUND', language: [ 'cpp' ]) # currently only used for Validator plot styles + add_project_arguments('-D IGUANA_ROOT_FOUND', language: [ 'cpp' ]) # currently only used for Validator plot styles endif # start chameleon From f290cb3b70ebd4dfdb5b74840c56f12432cd5789 Mon Sep 17 00:00:00 2001 From: Christopher Dilks Date: Sun, 21 Dec 2025 12:35:40 -0500 Subject: [PATCH 33/36] refactor: improve multithreading support --- .../clas12/LeptonIDFilter/Algorithm.cc | 43 +++-------- .../clas12/LeptonIDFilter/Algorithm.h | 71 +++++++++++-------- .../clas12/LeptonIDFilter/Config.yaml | 6 +- 3 files changed, 53 insertions(+), 67 deletions(-) diff --git a/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.cc b/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.cc index 42e70abb2..98cfb0812 100644 --- a/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.cc +++ b/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.cc @@ -11,29 +11,19 @@ namespace iguana::clas12 { { // Get configuration ParseYAMLConfig(); - o_pid = GetOptionScalar("pid"); // Obtain pid from config file (+11/-11) - o_cut = GetOptionScalar("cut"); - o_particle_bank = GetOptionScalar("particle_bank"); - o_runnum = ConcurrentParamFactory::Create(); - o_weightfile = ConcurrentParamFactory::Create(); + o_pid = GetOptionScalar("pid"); // Obtain pid from config file (+11/-11) + o_cut = GetOptionScalar("cut"); + o_tmva_reader_options = GetOptionScalar("tmva_reader_options"); + o_particle_bank = GetOptionScalar("particle_bank"); + o_runnum = ConcurrentParamFactory::Create(); + o_weightfile = ConcurrentParamFactory::Create(); // Get Banks that we are going to use b_particle = GetBankIndex(banks, o_particle_bank); b_calorimeter = GetBankIndex(banks, "REC::Calorimeter"); // Initialize the TMVA reader - readerTMVA = std::make_unique("V"); - - // initialize TMVA variables - readerTMVA->AddVariable("P", &P); - readerTMVA->AddVariable("Theta", &Theta); - readerTMVA->AddVariable("Phi", &Phi); - readerTMVA->AddVariable("SFPCAL", &PCAL); - readerTMVA->AddVariable("SFECIN", &ECIN); - readerTMVA->AddVariable("SFECOUT", &ECOUT); - readerTMVA->AddVariable("m2PCAL", &m2PCAL); - readerTMVA->AddVariable("m2ECIN", &m2ECIN); - readerTMVA->AddVariable("m2ECOUT", &m2ECOUT); + readerTMVA = std::make_unique(LeptonIDVars::names, o_tmva_reader_options); // find all the unique weights files in the configuration YAML std::set weightfile_list; @@ -66,9 +56,6 @@ namespace iguana::clas12 { bool LeptonIDFilter::Run(hipo::bank& particleBank, hipo::bank const& calorimeterBank) const { - // mutex-lock the whole `Run` function, since it needs to mutate and use several `mutable Float_t` members - std::scoped_lock lock(m_mutex); - // particle bank before filtering ShowBank(particleBank, Logger::Header("INPUT PARTICLES")); @@ -159,22 +146,8 @@ namespace iguana::clas12 { double LeptonIDFilter::CalculateScore(LeptonIDVars lepton_vars) const { - // Assigning variables from lepton_vars for TMVA method - P = lepton_vars.P; - Theta = lepton_vars.Theta; - Phi = lepton_vars.Phi; - PCAL = lepton_vars.SFpcal; - ECIN = lepton_vars.SFecin; - ECOUT = lepton_vars.SFecout; - m2PCAL = lepton_vars.m2pcal; - m2ECIN = lepton_vars.m2ecin; - m2ECOUT = lepton_vars.m2ecout; - - m_log->Debug("Add variables to readerTMVA"); - auto score = readerTMVA->EvaluateMVA("BDT"); - - return score; + return readerTMVA->EvaluateMVA(lepton_vars.GetValues(), "BDT"); } diff --git a/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.h b/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.h index 9fabf5599..1c887889a 100644 --- a/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.h +++ b/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.h @@ -7,25 +7,54 @@ /// Struct to store variables struct LeptonIDVars { /// @brief momentum - double P; + Double_t P; /// @brief Theta angle - double Theta; + Double_t Theta; /// @brief Phi angle - double Phi; + Double_t Phi; /// @brief Sampling fraction on the PCAL - double SFpcal; + Double_t SFpcal; /// @brief Sampling fraction on the ECIN - double SFecin; + Double_t SFecin; /// @brief Sampling fraction on the ECOUT - double SFecout; + Double_t SFecout; /// @brief Second-momenta of PCAL - double m2pcal; + Double_t m2pcal; /// @brief Second-momenta of ECIN - double m2ecin; + Double_t m2ecin; /// @brief Second-momenta of ECOUT - double m2ecout; + Double_t m2ecout; /// @brief Score - double score; + Double_t score; + + /// @return list of variable values, to pass to `TMVA::Reader::EvaluateMVA` + std::vector GetValues() + { + return { // NOTE: order must be consistent with `names` + P, + Theta, + Phi, + SFpcal, + SFecin, + SFecout, + m2pcal, + m2ecin, + m2ecout, + }; + } + + /// list of variable names, to pass to `TMVA::Reader` constructor + inline static std::vector names = { // NOTE: order must be consistent with `GetValues` + "P", + "Theta", + "Phi", + "PCAL", + "ECIN", + "ECOUT", + "m2PCAL", + "m2ECIN", + "m2ECOUT", + }; }; namespace iguana::clas12 { @@ -55,7 +84,6 @@ namespace iguana::clas12 { /// @param [in,out] particleBank particle bank (_viz._, `REC::Particle`), which will be filtered /// @param [in] calorimeterBank `REC::Calorimeter` bank /// @returns `false` if all particles are filtered out - /// @note Multithreading users should be aware that this entire function is mutex-locked. bool Run(hipo::bank& particleBank, hipo::bank const& calorimeterBank) const; /// @brief returns the pindex of the lepton @@ -85,26 +113,6 @@ namespace iguana::clas12 { /// TMVA reader std::unique_ptr readerTMVA; - /// Set of variables for the reader - /// Momentum - mutable Float_t P; - /// Theta angle - mutable Float_t Theta; - /// Phi angle - mutable Float_t Phi; - /// Sampling fraction on the PCAL - mutable Float_t PCAL; - /// Sampling fraction on the ECIN - mutable Float_t ECIN; - /// Sampling fraction on the ECOUT - mutable Float_t ECOUT; - /// Second-momenta of PCAL - mutable Float_t m2PCAL; - /// Second-momenta of ECIN - mutable Float_t m2ECIN; - /// Second-momenta of ECOUT - mutable Float_t m2ECOUT; - /// `hipo::banklist` hipo::banklist::size_type b_particle; hipo::banklist::size_type b_calorimeter; @@ -114,6 +122,7 @@ namespace iguana::clas12 { mutable std::unique_ptr> o_runnum; mutable std::unique_ptr> o_weightfile; double o_cut; + std::string o_tmva_reader_options; std::string o_particle_bank; }; diff --git a/src/iguana/algorithms/clas12/LeptonIDFilter/Config.yaml b/src/iguana/algorithms/clas12/LeptonIDFilter/Config.yaml index 2ec4efbd9..3231ec9d2 100644 --- a/src/iguana/algorithms/clas12/LeptonIDFilter/Config.yaml +++ b/src/iguana/algorithms/clas12/LeptonIDFilter/Config.yaml @@ -1,5 +1,5 @@ clas12::LeptonIDFilter: - pid: -11 + pid: -11 ########################### FIXME # weight file, one for each lepton and dataset weightfile: @@ -34,5 +34,9 @@ clas12::LeptonIDFilter: # value of the score to apply the cut cut: 0.0 + # option string to pass to `TMVA::Reader` constructor, + # e.g., "V" to set the "Verbose flag" + tmva_reader_options: '' + # the name of the particle bank, only needed for users of `hipo::banklist` particle_bank: 'REC::Particle' From 77d474ad224ea0474c852c08f10ff522ac628959 Mon Sep 17 00:00:00 2001 From: Christopher Dilks Date: Sun, 21 Dec 2025 12:51:42 -0500 Subject: [PATCH 34/36] feat: apply to multiple PIDs --- .../clas12/LeptonIDFilter/Algorithm.cc | 44 ++++++++----------- .../clas12/LeptonIDFilter/Algorithm.h | 13 ++---- .../clas12/LeptonIDFilter/Config.yaml | 4 +- 3 files changed, 26 insertions(+), 35 deletions(-) diff --git a/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.cc b/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.cc index 98cfb0812..ded20137a 100644 --- a/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.cc +++ b/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.cc @@ -11,7 +11,7 @@ namespace iguana::clas12 { { // Get configuration ParseYAMLConfig(); - o_pid = GetOptionScalar("pid"); // Obtain pid from config file (+11/-11) + o_pids = GetOptionSet("pids"); o_cut = GetOptionScalar("cut"); o_tmva_reader_options = GetOptionScalar("tmva_reader_options"); o_particle_bank = GetOptionScalar("particle_bank"); @@ -60,12 +60,24 @@ namespace iguana::clas12 { ShowBank(particleBank, Logger::Header("INPUT PARTICLES")); // filter the particle bank - particleBank.getMutableRowList().filter([this, &particleBank, &calorimeterBank](auto bank, auto row) { - auto lepton_pindex = FindLepton(particleBank); - auto lepton_vars = GetLeptonIDVariables(lepton_pindex, particleBank, calorimeterBank); - lepton_vars.score = CalculateScore(lepton_vars); - - return Filter(lepton_vars.score); + particleBank.getMutableRowList().filter([this, &calorimeterBank](auto bank, auto row) { + auto pid = bank.getInt("pid", row); + // check if this is a lepton in `o_pids` + if(o_pids.find(pid) != o_pids.end()) { + auto status = bank.getShort("status", row); + // status cut + if(std::abs(status) >= 2000 && std::abs(status) < 4000) { + m_log->Trace("Found lepton: pindex={}", row); + auto lepton_vars = GetLeptonIDVariables(row, bank, calorimeterBank); + lepton_vars.score = CalculateScore(lepton_vars); + return Filter(lepton_vars.score) ? 1 : 0; + } + else { + m_log->Trace("Lepton at pindex={} did not pass status cut", row); + return 0; + } + } + return 1; // not a lepton in `o_pids`, let it pass the filter }); // particle bank after filtering @@ -74,24 +86,6 @@ namespace iguana::clas12 { } - int LeptonIDFilter::FindLepton(hipo::bank const& particle_bank) const - { - int lepton_pindex = -1; - for(int row = 0; row < particle_bank.getRows(); row++) { - auto status = particle_bank.getShort("status", row); - if(particle_bank.getInt("pid", row) == o_pid && abs(status) >= 2000 && abs(status) < 4000) { - lepton_pindex = row; - break; - } - } - if(lepton_pindex >= 0) - m_log->Debug("Found lepton: pindex={}", lepton_pindex); - else - m_log->Debug("Lepton not found"); - return lepton_pindex; - } - - LeptonIDVars LeptonIDFilter::GetLeptonIDVariables(int const plepton, hipo::bank const& particle_bank, hipo::bank const& calorimeter_bank) const { diff --git a/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.h b/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.h index 1c887889a..972978dd0 100644 --- a/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.h +++ b/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.h @@ -65,9 +65,9 @@ namespace iguana::clas12 { /// Using those variables, it call the TMVA method using the weight file, and it computes a score. By a pplying a cut to the score we can separate leptons from pions. /// /// @begin_doc_config{clas12/LeptonIDFilter} - /// @config_param{o_pid | int | PID of the particle; -11 for positrons and 11 for electrons} - /// @config_param{o_weightfile | string | Location of the weight file of the classifier} - /// @config_param{o_cut | double | Value of the score to apply the cut. The algorithm will keep all particles that have a score grater than ths value} + /// @config_param{pids | int | PIDs of the particle; -11 for positrons and 11 for electrons} + /// @config_param{weightfile | string | Location of the weight file of the classifier} + /// @config_param{cut | double | Value of the score to apply the cut. The algorithm will keep all particles that have a score grater than ths value} /// @end_doc class LeptonIDFilter : public Algorithm { @@ -86,11 +86,6 @@ namespace iguana::clas12 { /// @returns `false` if all particles are filtered out bool Run(hipo::bank& particleBank, hipo::bank const& calorimeterBank) const; - /// @brief returns the pindex of the lepton - /// @param particle_bank the particle bank - /// @returns pindex of the lepton, -1 if there is no lepton - int FindLepton(hipo::bank const& particle_bank) const; - /// @brief Using the pindex, retrieves the necessary variables from banks /// @param plepton pindex of the lepton /// @param particle_bank the particle bank @@ -118,7 +113,7 @@ namespace iguana::clas12 { hipo::banklist::size_type b_calorimeter; // config options - int o_pid; + std::set o_pids; mutable std::unique_ptr> o_runnum; mutable std::unique_ptr> o_weightfile; double o_cut; diff --git a/src/iguana/algorithms/clas12/LeptonIDFilter/Config.yaml b/src/iguana/algorithms/clas12/LeptonIDFilter/Config.yaml index 3231ec9d2..173c72be5 100644 --- a/src/iguana/algorithms/clas12/LeptonIDFilter/Config.yaml +++ b/src/iguana/algorithms/clas12/LeptonIDFilter/Config.yaml @@ -1,5 +1,7 @@ clas12::LeptonIDFilter: - pid: -11 ########################### FIXME + + # choose which lepton PDG codes to filter + pids: [-11, 11] # weight file, one for each lepton and dataset weightfile: From ee6b735ec07cbc1fdad28d09ca075706e57b7bd2 Mon Sep 17 00:00:00 2001 From: Christopher Dilks Date: Sun, 21 Dec 2025 13:24:59 -0500 Subject: [PATCH 35/36] fix: use run number to choose appropriate weights files --- .../clas12/LeptonIDFilter/Algorithm.cc | 60 ++++++++++++++++--- .../clas12/LeptonIDFilter/Algorithm.h | 27 +++++++-- src/iguana/algorithms/meson.build | 2 +- 3 files changed, 75 insertions(+), 14 deletions(-) diff --git a/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.cc b/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.cc index ded20137a..2e9ee10d9 100644 --- a/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.cc +++ b/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.cc @@ -16,11 +16,13 @@ namespace iguana::clas12 { o_tmva_reader_options = GetOptionScalar("tmva_reader_options"); o_particle_bank = GetOptionScalar("particle_bank"); o_runnum = ConcurrentParamFactory::Create(); - o_weightfile = ConcurrentParamFactory::Create(); + o_weightfile_electron = ConcurrentParamFactory::Create(); + o_weightfile_positron = ConcurrentParamFactory::Create(); // Get Banks that we are going to use b_particle = GetBankIndex(banks, o_particle_bank); b_calorimeter = GetBankIndex(banks, "REC::Calorimeter"); + b_config = GetBankIndex(banks, "RUN::config"); // Initialize the TMVA reader readerTMVA = std::make_unique(LeptonIDVars::names, o_tmva_reader_options); @@ -50,17 +52,21 @@ namespace iguana::clas12 { { return Run( GetBank(banks, b_particle, o_particle_bank), - GetBank(banks, b_calorimeter, "REC::Calorimeter")); + GetBank(banks, b_calorimeter, "REC::Calorimeter"), + GetBank(banks, b_config, "RUN::config")); } - bool LeptonIDFilter::Run(hipo::bank& particleBank, hipo::bank const& calorimeterBank) const + bool LeptonIDFilter::Run(hipo::bank& particleBank, hipo::bank const& calorimeterBank, hipo::bank const& configBank) const { // particle bank before filtering ShowBank(particleBank, Logger::Header("INPUT PARTICLES")); + // prepare the event, reloading configuration parameters if the run number changed or is not yet known + auto key = PrepareEvent(configBank.getInt("run", 0)); + // filter the particle bank - particleBank.getMutableRowList().filter([this, &calorimeterBank](auto bank, auto row) { + particleBank.getMutableRowList().filter([this, &calorimeterBank, key](auto bank, auto row) { auto pid = bank.getInt("pid", row); // check if this is a lepton in `o_pids` if(o_pids.find(pid) != o_pids.end()) { @@ -69,7 +75,8 @@ namespace iguana::clas12 { if(std::abs(status) >= 2000 && std::abs(status) < 4000) { m_log->Trace("Found lepton: pindex={}", row); auto lepton_vars = GetLeptonIDVariables(row, bank, calorimeterBank); - lepton_vars.score = CalculateScore(lepton_vars); + lepton_vars.pid = pid; + lepton_vars.score = CalculateScore(lepton_vars, key); return Filter(lepton_vars.score) ? 1 : 0; } else { @@ -86,6 +93,34 @@ namespace iguana::clas12 { } + concurrent_key_t LeptonIDFilter::PrepareEvent(int const runnum) const + { + m_log->Trace("calling PrepareEvent({})", runnum); + if(o_runnum->NeedsHashing()) { + std::hash hash_ftn; + auto hash_key = hash_ftn(runnum); + if(!o_runnum->HasKey(hash_key)) + Reload(runnum, hash_key); + return hash_key; + } + else { + if(o_runnum->IsEmpty() || o_runnum->Load(0) != runnum) + Reload(runnum, 0); + return 0; + } + } + + + void LeptonIDFilter::Reload(int const runnum, concurrent_key_t key) const + { + std::lock_guard const lock(m_mutex); // NOTE: be sure to lock successive `ConcurrentParam::Save` calls !!! + m_log->Trace("-> calling Reload({}, {})", runnum, key); + o_runnum->Save(runnum, key); + o_weightfile_electron->Save(GetOptionScalar("weightfile:electron", {"weightfile", GetConfig()->InRange("runs", runnum), "electron"}), key); + o_weightfile_positron->Save(GetOptionScalar("weightfile:positron", {"weightfile", GetConfig()->InRange("runs", runnum), "positron"}), key); + } + + LeptonIDVars LeptonIDFilter::GetLeptonIDVariables(int const plepton, hipo::bank const& particle_bank, hipo::bank const& calorimeter_bank) const { @@ -138,10 +173,21 @@ namespace iguana::clas12 { } - double LeptonIDFilter::CalculateScore(LeptonIDVars lepton_vars) const + double LeptonIDFilter::CalculateScore(LeptonIDVars lepton_vars, concurrent_key_t const key) const { // Assigning variables from lepton_vars for TMVA method - return readerTMVA->EvaluateMVA(lepton_vars.GetValues(), "BDT"); + std::string weightsfile; + switch(lepton_vars.pid) { + case 11: + weightsfile = o_weightfile_electron->Load(key); + break; + case -11: + weightsfile = o_weightfile_positron->Load(key); + break; + default: + throw std::runtime_error(fmt::format("unknown lepton PDG code {}", lepton_vars.pid)); + } + return readerTMVA->EvaluateMVA(lepton_vars.GetValues(), weightsfile); } diff --git a/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.h b/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.h index 972978dd0..715337602 100644 --- a/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.h +++ b/src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.h @@ -26,6 +26,8 @@ struct LeptonIDVars { Double_t m2ecout; /// @brief Score Double_t score; + /// @brief PDG code + Int_t pid; /// @return list of variable values, to pass to `TMVA::Reader::EvaluateMVA` std::vector GetValues() @@ -48,9 +50,9 @@ struct LeptonIDVars { "P", "Theta", "Phi", - "PCAL", - "ECIN", - "ECOUT", + "SFPCAL", + "SFECIN", + "SFECOUT", "m2PCAL", "m2ECIN", "m2ECOUT", @@ -83,8 +85,15 @@ namespace iguana::clas12 { /// @run_function /// @param [in,out] particleBank particle bank (_viz._, `REC::Particle`), which will be filtered /// @param [in] calorimeterBank `REC::Calorimeter` bank + /// @param [in] configBank `RUN::config` bank /// @returns `false` if all particles are filtered out - bool Run(hipo::bank& particleBank, hipo::bank const& calorimeterBank) const; + bool Run(hipo::bank& particleBank, hipo::bank const& calorimeterBank, hipo::bank const& configBank) const; + + /// @action_function{reload} prepare the event + /// @when_to_call{for each event} + /// @param runnum the run number + /// @returns the key to be used in `::Filter` + concurrent_key_t PrepareEvent(int const runnum) const; /// @brief Using the pindex, retrieves the necessary variables from banks /// @param plepton pindex of the lepton @@ -95,8 +104,9 @@ namespace iguana::clas12 { /// @brief Using the LeptonIDVars, variables calculate the score /// @param lepton_vars LeptonIDVars variables + /// @param key the return value of `::PrepareEvent` /// @returns double, the score - double CalculateScore(LeptonIDVars lepton_vars) const; + double CalculateScore(LeptonIDVars lepton_vars, concurrent_key_t const key) const; /// @brief Returns true if the particle passed the cut /// @param score the score obtained from the CalculateScore function @@ -105,17 +115,22 @@ namespace iguana::clas12 { private: + // Reload function + void Reload(int const runnum, concurrent_key_t key) const; + /// TMVA reader std::unique_ptr readerTMVA; /// `hipo::banklist` hipo::banklist::size_type b_particle; hipo::banklist::size_type b_calorimeter; + hipo::banklist::size_type b_config; // config options std::set o_pids; mutable std::unique_ptr> o_runnum; - mutable std::unique_ptr> o_weightfile; + mutable std::unique_ptr> o_weightfile_electron; + mutable std::unique_ptr> o_weightfile_positron; double o_cut; std::string o_tmva_reader_options; std::string o_particle_bank; diff --git a/src/iguana/algorithms/meson.build b/src/iguana/algorithms/meson.build index 3f63267bc..a5de54df8 100644 --- a/src/iguana/algorithms/meson.build +++ b/src/iguana/algorithms/meson.build @@ -138,7 +138,7 @@ algo_dict = [ 'add_algorithm_data': [ 'weights/9_BDT_positrons_S19.weights.xml', ], - 'test_args': { 'banks': ['REC::Particle', 'REC::Calorimeter'] }, + 'test_args': { 'banks': ['REC::Particle', 'REC::Calorimeter', 'RUN::config'] }, }, { 'name': 'clas12::rga::FiducialFilterPass2', From 9a61b56f48fe66e1fad62ed1338f8bee84047145 Mon Sep 17 00:00:00 2001 From: Christopher Dilks Date: Sun, 21 Dec 2025 13:32:55 -0500 Subject: [PATCH 36/36] fix: add `default` dataset to config --- src/iguana/algorithms/clas12/LeptonIDFilter/Config.yaml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/iguana/algorithms/clas12/LeptonIDFilter/Config.yaml b/src/iguana/algorithms/clas12/LeptonIDFilter/Config.yaml index 173c72be5..1e400882a 100644 --- a/src/iguana/algorithms/clas12/LeptonIDFilter/Config.yaml +++ b/src/iguana/algorithms/clas12/LeptonIDFilter/Config.yaml @@ -9,8 +9,12 @@ clas12::LeptonIDFilter: # # FIXME: double check these run number ranges # FIXME: use the correct weights files for each + # FIXME: warn the user if run number falls in 'default' data set # # + - default: # run number is not in any other data set below + electron: 'weights/9_BDT_positrons_S19.weights.xml' + positron: 'weights/9_BDT_positrons_S19.weights.xml' - runs: [3211, 3293] # RG-A Spring 2018 Outbending Part 1 electron: 'weights/9_BDT_positrons_S19.weights.xml' positron: 'weights/9_BDT_positrons_S19.weights.xml'