-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
LeptonID #232
Draft
MarianaT27
wants to merge
18
commits into
JeffersonLab:main
Choose a base branch
from
MarianaT27:dev_Mariana
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
LeptonID #232
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
7492cc6
Added LeptonID .c
MarianaT27 c4a49d7
Add LeptonID
MarianaT27 f859da9
Merge branch 'main' into dev_Mariana
MarianaT27 3ffd7e1
Added algorithm to folder, added weight file
MarianaT27 b6b3827
Removed extra LeptonID files
MarianaT27 1883b27
Added config variables
MarianaT27 028e73b
Merge remote-tracking branch 'origin/main' into dev_Mariana
c-dilks 0e92759
renamed: LeptonIDFilter/config.yaml -> LeptonIDFilter/Config.yaml
c-dilks 53c3686
fix: compilation fixes
c-dilks 0563625
feat: install and use algorithm data files (viz., weight files)
c-dilks fcb3528
doc: some doxygen fixes
c-dilks 57e20df
Update src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.cc
MarianaT27 db2d441
fix: increase `algorithm` test timeout
c-dilks 703a593
Added: documentation descriptions
MarianaT27 d03a269
Merge branch 'dev_Mariana' of github.com:MarianaT27/iguana into dev_M…
MarianaT27 e576c70
Moved declaration of TMVAReader to header; Added documentation
MarianaT27 f41b895
Fixed some errors
MarianaT27 bcdfb1c
Fixed more errors
MarianaT27 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
152 changes: 152 additions & 0 deletions
152
src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
#include "Algorithm.h" | ||
|
||
#include <cmath> | ||
#include <Math/Vector4D.h> | ||
|
||
namespace iguana::clas12 { | ||
|
||
REGISTER_IGUANA_ALGORITHM(LeptonIDFilter , "clas12::LeptonIDFilter"); | ||
|
||
void LeptonIDFilter::Start(hipo::banklist& banks) | ||
{ | ||
//Get configuration | ||
ParseYAMLConfig(); | ||
o_pid = GetOptionScalar<int>("pid");//Obtain pid from config file (+11/-11) | ||
o_weightfile = GetOptionScalar<std::string>("weightfile");//Obtain weightfile from config file | ||
o_cut = GetOptionScalar<double>("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"); | ||
b_calorimeter = GetBankIndex(banks, "REC::Calorimeter"); | ||
|
||
|
||
} | ||
|
||
|
||
void LeptonIDFilter::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,&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); | ||
}); | ||
|
||
// dump the modified bank | ||
ShowBank(particleBank, Logger::Header("OUTPUT PARTICLES")); | ||
|
||
} | ||
|
||
|
||
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{ | ||
|
||
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 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"); | ||
|
||
return score; | ||
} | ||
|
||
bool LeptonIDFilter::Filter(double score) const{ | ||
if(score>=o_cut) | ||
return true; | ||
else | ||
return false; | ||
} | ||
|
||
|
||
|
||
void LeptonIDFilter::Stop() | ||
{ | ||
} | ||
|
||
} |
137 changes: 137 additions & 0 deletions
137
src/iguana/algorithms/clas12/LeptonIDFilter/Algorithm.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
#pragma once | ||
|
||
#include "iguana/algorithms/Algorithm.h" | ||
#include <TMVA/Reader.h> | ||
|
||
///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 { | ||
/// | ||
/// @brief_algo Filter the leptons from the pion contamination using TMVA models | ||
/// | ||
/// 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 | ||
/// @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 | ||
{ | ||
|
||
DEFINE_IGUANA_ALGORITHM(LeptonIDFilter, clas12::LeptonIDFilter) | ||
|
||
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; | ||
|
||
//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 ); | ||
|
||
|
||
private: | ||
|
||
|
||
/// `hipo::banklist` | ||
hipo::banklist::size_type b_particle; | ||
hipo::banklist::size_type b_calorimeter; | ||
|
||
|
||
|
||
|
||
/// 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; | ||
}; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
clas12::LeptonIDFilter: | ||
pid: -11 | ||
weightfile: "weights/9_BDT_positrons_S19.weights.xml" | ||
cut: 0.0 |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The header should only declare the variables, not define them; the definitions could go in the
Start()
method inAlgorithm.cc
.