Skip to content
Snippets Groups Projects
Commit 79ad030b authored by Christos Anastopoulos's avatar Christos Anastopoulos
Browse files

Egamma Analysis. Tidy EgammaAnalysisHelpers code. Use empty, qualify auto

parent 4cd48938
No related branches found
No related tags found
No related merge requests found
/* /*
Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration
*/ */
// Dear emacs, this is -*-c++-*- // Dear emacs, this is -*-c++-*-
#ifndef ASGELEGAMMACONFIGHELPER_H #ifndef ASGELEGAMMACONFIGHELPER_H
#define ASGELEGAMMACONFIGHELPER_H #define ASGELEGAMMACONFIGHELPER_H
/** /**
@brief Tool to simplify the configuration of some egamma tools based on TEnv input @brief Tool to simplify the configuration of some egamma tools based on TEnv
@author Christos Anastopoulos (2017) Nils Gillwald (DESY, 2020) nils.gillwald@desy.de input
@author Christos Anastopoulos (2017) Nils Gillwald (DESY, 2020)
nils.gillwald@desy.de
@date February 2020 @date February 2020
*/ */
#include <map>
#include <string> #include <string>
#include <vector> #include <vector>
#include <map>
class TEnv; class TEnv;
namespace AsgConfigHelper{ namespace AsgConfigHelper {
std::string findConfigFile (const std::string& input, const std::map<std::string,std::string>& configmap); std::string
unsigned int findMask (const std::string& input, const std::map<std::string,unsigned int>& maskmap); findConfigFile(const std::string& input,
std::vector<double> HelperDouble(const std::string& input, TEnv& env); const std::map<std::string, std::string>& configmap);
std::vector<float> HelperFloat(const std::string& input, TEnv& env); unsigned int
std::vector<int> HelperInt(const std::string& input, TEnv& env); findMask(const std::string& input,
std::vector<std::string> HelperString(const std::string& input, TEnv& env); const std::map<std::string, unsigned int>& maskmap);
std::vector<double>
HelperDouble(const std::string& input, TEnv& env);
std::vector<float>
HelperFloat(const std::string& input, TEnv& env);
std::vector<int>
HelperInt(const std::string& input, TEnv& env);
std::vector<std::string>
HelperString(const std::string& input, TEnv& env);
} }
#endif // ASGEGAMMACONFIGHELPER_H
#endif //ASGEGAMMACONFIGHELPER_H
/* /*
Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration
*/ */
#include "EgammaAnalysisHelpers/AsgEGammaConfigHelper.h" #include "EgammaAnalysisHelpers/AsgEGammaConfigHelper.h"
#include "AsgMessaging/AsgMessaging.h" #include "AsgMessaging/AsgMessaging.h"
#include "TEnv.h" #include "TEnv.h"
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
namespace AsgConfigHelper{ namespace AsgConfigHelper {
std::string findConfigFile (const std::string& input, const std::map<std::string,std::string>& configmap){ std::string
auto confFile_itr=configmap.find(input); findConfigFile(const std::string& input,
if(confFile_itr == configmap.end()){ const std::map<std::string, std::string>& configmap)
static const asg::AsgMessaging msg("Egamma::AsgConfigHelper"); {
msg.msg(MSG::WARNING)<<"Key "<<input<<" not found in map, no config file returned"<<endmsg; auto confFile_itr = configmap.find(input);
return ""; if (confFile_itr == configmap.end()) {
} static const asg::AsgMessaging msg("Egamma::AsgConfigHelper");
return confFile_itr->second; msg.msg(MSG::WARNING) << "Key " << input
} << " not found in map, no config file returned"
<< endmsg;
unsigned int findMask (const std::string& input, const std::map<std::string,unsigned int>& maskmap){ return "";
auto mask_itr=maskmap.find(input);
if(mask_itr==maskmap.end()){
static const asg::AsgMessaging msg("Egamma::AsgConfigHelper");
msg.msg(MSG::WARNING)<<"Key "<<input<<" not found in map, egammaPID::EgPidUndefined mask returned"<<endmsg;
// mask has the choice to default to all 1 or all 0 bits, choose the former
return std::numeric_limits<unsigned int>::max();
}
return static_cast < unsigned int> (mask_itr->second);
} }
return confFile_itr->second;
}
template <typename T> unsigned int
bool strtof(const std::string& input, T& f){ findMask(const std::string& input,
int diff = 0 ; const std::map<std::string, unsigned int>& maskmap)
std::string tmp = input; {
std::string::size_type first(0); auto mask_itr = maskmap.find(input);
std::string::size_type last(0); if (mask_itr == maskmap.end()) {
first = ( input.find('#') ) ; static const asg::AsgMessaging msg("Egamma::AsgConfigHelper");
msg.msg(MSG::WARNING)
//if we do not find a comment character "#" we are fine << "Key " << input
if (first == std::string::npos) { << " not found in map, egammaPID::EgPidUndefined mask returned"
std::istringstream buffer (tmp); << endmsg;
buffer>>f; // mask has the choice to default to all 1 or all 0 bits, choose the former
return true; return std::numeric_limits<unsigned int>::max();
}
else {
//if we have found comment character check if it is inlined between two "#"
last = (input.find('#',first+1) );
//if nor error
if (last == std::string::npos) {
static const asg::AsgMessaging msg("Egamma::AsgConfigHelper");
msg.msg(MSG::WARNING)<<" Improper comment format , inline comment should be enclosed between two # "<<endmsg;
return false;
}
//else if between two "#" remove this part
diff = last - first ;
tmp= tmp.erase(first,diff+1);
std::istringstream buffer (tmp);
buffer>>f;
return true;
}
} }
return static_cast<unsigned int>(mask_itr->second);
}
template <typename T> template<typename T>
std::vector<T> Helper (const std::string& input, TEnv& env){ bool
std::vector<T> CutVector; strtof(const std::string& input, T& f)
std::string env_input(env.GetValue(input.c_str(), "")); {
if (!env_input.empty()) { int diff = 0;
std::string::size_type end; std::string tmp = input;
do { std::string::size_type first(0);
end = env_input.find(';'); std::string::size_type last(0);
T myValue(0); first = (input.find('#'));
if(AsgConfigHelper::strtof(env_input.substr(0,end),myValue)){
CutVector.push_back(myValue); // if we do not find a comment character "#" we are fine
} if (first == std::string::npos) {
if (end != std::string::npos) { std::istringstream buffer(tmp);
env_input= env_input.substr(end+1); buffer >> f;
} return true;
} while (end != std::string::npos); } else {
// if we have found comment character check if it is inlined between two "#"
last = (input.find('#', first + 1));
// if nor error
if (last == std::string::npos) {
static const asg::AsgMessaging msg("Egamma::AsgConfigHelper");
msg.msg(MSG::WARNING) << " Improper comment format , inline comment "
"should be enclosed between two # "
<< endmsg;
return false;
} }
return CutVector; // else if between two "#" remove this part
diff = last - first;
tmp = tmp.erase(first, diff + 1);
std::istringstream buffer(tmp);
buffer >> f;
return true;
} }
} }
//use the specializations template<typename T>
std::vector<double> AsgConfigHelper::HelperDouble(const std::string& input, TEnv& env){ std::vector<T>
return AsgConfigHelper::Helper<double> ( input,env); Helper(const std::string& input, TEnv& env)
} {
std::vector<float> AsgConfigHelper::HelperFloat(const std::string& input, TEnv& env){ std::vector<T> CutVector;
return AsgConfigHelper::Helper<float> (input, env);
}
std::vector<int> AsgConfigHelper::HelperInt(const std::string& input, TEnv& env){
return AsgConfigHelper::Helper<int> (input, env);
}
// template does not work for std::string because of the T myValue(0); declaration, so implement it again for std::string
std::vector<std::string> AsgConfigHelper::HelperString(const std::string& input, TEnv& env){
std::vector<std::string> CutVector;
std::string env_input(env.GetValue(input.c_str(), "")); std::string env_input(env.GetValue(input.c_str(), ""));
if (!env_input.empty()) { if (!env_input.empty()) {
std::string::size_type end; std::string::size_type end;
do { do {
end = env_input.find(';'); end = env_input.find(';');
std::string myValue(""); T myValue{}; //default init
if(AsgConfigHelper::strtof(env_input.substr(0,end),myValue)){ if (AsgConfigHelper::strtof(env_input.substr(0, end), myValue)) {
CutVector.push_back(myValue); CutVector.push_back(myValue);
} }
if (end != std::string::npos) { if (end != std::string::npos) {
env_input= env_input.substr(end+1); env_input = env_input.substr(end + 1);
} }
} while (end != std::string::npos); } while (end != std::string::npos);
} }
return CutVector; return CutVector;
} }
}
// use the specializations
std::vector<double>
AsgConfigHelper::HelperDouble(const std::string& input, TEnv& env)
{
return AsgConfigHelper::Helper<double>(input, env);
}
std::vector<float>
AsgConfigHelper::HelperFloat(const std::string& input, TEnv& env)
{
return AsgConfigHelper::Helper<float>(input, env);
}
std::vector<int>
AsgConfigHelper::HelperInt(const std::string& input, TEnv& env)
{
return AsgConfigHelper::Helper<int>(input, env);
}
// template does not work for std::string because of the T myValue(0);
// declaration, so implement it again for std::string
std::vector<std::string>
AsgConfigHelper::HelperString(const std::string& input, TEnv& env)
{
return AsgConfigHelper::Helper<std::string>(input, env);
}
...@@ -24,7 +24,7 @@ class AsgElectronSelectorTool final : public asg::AsgTool, ...@@ -24,7 +24,7 @@ class AsgElectronSelectorTool final : public asg::AsgTool,
public: public:
/** Standard constructor */ /** Standard constructor */
AsgElectronSelectorTool( const std::string myname ); AsgElectronSelectorTool( const std::string& myname );
/** Standard destructor */ /** Standard destructor */
......
...@@ -35,7 +35,7 @@ ...@@ -35,7 +35,7 @@
//============================================================================= //=============================================================================
// Standard constructor // Standard constructor
//============================================================================= //=============================================================================
AsgElectronSelectorTool::AsgElectronSelectorTool( std::string myname ) : AsgElectronSelectorTool::AsgElectronSelectorTool( const std::string& myname ) :
AsgTool(myname), AsgTool(myname),
m_configFile{""}, m_configFile{""},
m_mvaTool(nullptr) m_mvaTool(nullptr)
...@@ -86,7 +86,7 @@ StatusCode AsgElectronSelectorTool::initialize() ...@@ -86,7 +86,7 @@ StatusCode AsgElectronSelectorTool::initialize()
if (!m_configFile.empty()){ if (!m_configFile.empty()){
std::string configFile = PathResolverFindCalibFile(m_configFile); std::string configFile = PathResolverFindCalibFile(m_configFile);
if (configFile==""){ if (configFile.empty()){
ATH_MSG_ERROR("Could not locate " << m_configFile); ATH_MSG_ERROR("Could not locate " << m_configFile);
return StatusCode::FAILURE; return StatusCode::FAILURE;
} }
...@@ -170,7 +170,7 @@ StatusCode AsgElectronSelectorTool::initialize() ...@@ -170,7 +170,7 @@ StatusCode AsgElectronSelectorTool::initialize()
return StatusCode::FAILURE; return StatusCode::FAILURE;
} }
if (m_cutSCT.size()){ if (!m_cutSCT.empty()){
if (m_cutSCT.size() != numberOfExpectedEtaBins){ if (m_cutSCT.size() != numberOfExpectedEtaBins){
ATH_MSG_ERROR("Configuration issue : cutSCT expected size " << numberOfExpectedEtaBins << ATH_MSG_ERROR("Configuration issue : cutSCT expected size " << numberOfExpectedEtaBins <<
" input size " << m_cutSCT.size()); " input size " << m_cutSCT.size());
...@@ -178,7 +178,7 @@ StatusCode AsgElectronSelectorTool::initialize() ...@@ -178,7 +178,7 @@ StatusCode AsgElectronSelectorTool::initialize()
} }
} }
if (m_cutPi.size()){ if (!m_cutPi.empty()){
if (m_cutPi.size() != numberOfExpectedEtaBins){ if (m_cutPi.size() != numberOfExpectedEtaBins){
ATH_MSG_ERROR("Configuration issue : cutPi expected size " << numberOfExpectedEtaBins << ATH_MSG_ERROR("Configuration issue : cutPi expected size " << numberOfExpectedEtaBins <<
" input size " << m_cutPi.size()); " input size " << m_cutPi.size());
...@@ -186,7 +186,7 @@ StatusCode AsgElectronSelectorTool::initialize() ...@@ -186,7 +186,7 @@ StatusCode AsgElectronSelectorTool::initialize()
} }
} }
if (m_cutBL.size()){ if (!m_cutBL.empty()){
if (m_cutBL.size() != numberOfExpectedEtaBins){ if (m_cutBL.size() != numberOfExpectedEtaBins){
ATH_MSG_ERROR("Configuration issue : cutBL expected size " << numberOfExpectedEtaBins << ATH_MSG_ERROR("Configuration issue : cutBL expected size " << numberOfExpectedEtaBins <<
" input size " << m_cutBL.size()); " input size " << m_cutBL.size());
...@@ -194,7 +194,7 @@ StatusCode AsgElectronSelectorTool::initialize() ...@@ -194,7 +194,7 @@ StatusCode AsgElectronSelectorTool::initialize()
} }
} }
if (m_cutAmbiguity.size()){ if (!m_cutAmbiguity.empty()){
if (m_cutAmbiguity.size() != numberOfExpectedEtaBins){ if (m_cutAmbiguity.size() != numberOfExpectedEtaBins){
ATH_MSG_ERROR("Configuration issue : cutAmbiguity expected size " << numberOfExpectedEtaBins << ATH_MSG_ERROR("Configuration issue : cutAmbiguity expected size " << numberOfExpectedEtaBins <<
" input size " << m_cutAmbiguity.size()); " input size " << m_cutAmbiguity.size());
...@@ -364,7 +364,7 @@ asg::AcceptData AsgElectronSelectorTool::accept( const EventContext& ctx, const ...@@ -364,7 +364,7 @@ asg::AcceptData AsgElectronSelectorTool::accept( const EventContext& ctx, const
if (!passKine){return acceptData;} if (!passKine){return acceptData;}
// ambiguity bit // ambiguity bit
if (m_cutAmbiguity.size()){ if (!m_cutAmbiguity.empty()){
if (!ElectronSelectorHelpers::passAmbiguity((xAOD::AmbiguityTool::AmbiguityType)ambiguityBit, m_cutAmbiguity[etaBin])){ if (!ElectronSelectorHelpers::passAmbiguity((xAOD::AmbiguityTool::AmbiguityType)ambiguityBit, m_cutAmbiguity[etaBin])){
ATH_MSG_DEBUG("MVA macro: ambiguity Bit Failed."); ATH_MSG_DEBUG("MVA macro: ambiguity Bit Failed.");
passAmbiguity = false; passAmbiguity = false;
...@@ -372,21 +372,21 @@ asg::AcceptData AsgElectronSelectorTool::accept( const EventContext& ctx, const ...@@ -372,21 +372,21 @@ asg::AcceptData AsgElectronSelectorTool::accept( const EventContext& ctx, const
} }
// blayer cut // blayer cut
if (m_cutBL.size()) { if (!m_cutBL.empty()) {
if(m_cutBL[etaBin] == 1 && !passBLayerRequirement){ if(m_cutBL[etaBin] == 1 && !passBLayerRequirement){
ATH_MSG_DEBUG("MVA macro: Blayer cut failed."); ATH_MSG_DEBUG("MVA macro: Blayer cut failed.");
passNBlayer = false; passNBlayer = false;
} }
} }
// pixel cut // pixel cut
if (m_cutPi.size()){ if (!m_cutPi.empty()){
if (nPixHitsPlusDeadSensors < m_cutPi[etaBin]){ if (nPixHitsPlusDeadSensors < m_cutPi[etaBin]){
ATH_MSG_DEBUG("MVA macro: Pixels Failed."); ATH_MSG_DEBUG("MVA macro: Pixels Failed.");
passNPixel = false; passNPixel = false;
} }
} }
// SCT cut // SCT cut
if (m_cutSCT.size()){ if (!m_cutSCT.empty()){
if (nSiHitsPlusDeadSensors < m_cutSCT[etaBin]){ if (nSiHitsPlusDeadSensors < m_cutSCT[etaBin]){
ATH_MSG_DEBUG( "MVA macro: Silicon Failed."); ATH_MSG_DEBUG( "MVA macro: Silicon Failed.");
passNSilicon = false; passNSilicon = false;
...@@ -396,7 +396,7 @@ asg::AcceptData AsgElectronSelectorTool::accept( const EventContext& ctx, const ...@@ -396,7 +396,7 @@ asg::AcceptData AsgElectronSelectorTool::accept( const EventContext& ctx, const
double cutDiscriminant; double cutDiscriminant;
unsigned int ibin_combinedMVA = etBin*s_fnDiscEtaBins+etaBin; // Must change if number of eta bins changes!. unsigned int ibin_combinedMVA = etBin*s_fnDiscEtaBins+etaBin; // Must change if number of eta bins changes!.
if (m_cutSelector.size()){ if (!m_cutSelector.empty()){
// To protect against a binning mismatch, which should never happen // To protect against a binning mismatch, which should never happen
if (ibin_combinedMVA >= m_cutSelector.size()){ if (ibin_combinedMVA >= m_cutSelector.size()){
throw std::runtime_error("AsgElectronSelectorTool: The desired eta/pt bin is outside of the range specified by the input. This should never happen! This indicates a mismatch between the binning in the configuration file and the tool implementation." ); throw std::runtime_error("AsgElectronSelectorTool: The desired eta/pt bin is outside of the range specified by the input. This should never happen! This indicates a mismatch between the binning in the configuration file and the tool implementation." );
......
...@@ -31,7 +31,7 @@ ElectronDNNCalculator::ElectronDNNCalculator(AsgElectronSelectorTool* owner, ...@@ -31,7 +31,7 @@ ElectronDNNCalculator::ElectronDNNCalculator(AsgElectronSelectorTool* owner,
{ {
ATH_MSG_INFO("Initializing ElectronDNNCalculator..."); ATH_MSG_INFO("Initializing ElectronDNNCalculator...");
if (modelFileName.size() == 0){ if (modelFileName.empty()){
throw std::runtime_error("No file found at '" + modelFileName + "'"); throw std::runtime_error("No file found at '" + modelFileName + "'");
} }
...@@ -56,7 +56,7 @@ ElectronDNNCalculator::ElectronDNNCalculator(AsgElectronSelectorTool* owner, ...@@ -56,7 +56,7 @@ ElectronDNNCalculator::ElectronDNNCalculator(AsgElectronSelectorTool* owner,
m_graph = std::make_unique<lwt::generic::FastGraph<float>>(parsedGraph, order); m_graph = std::make_unique<lwt::generic::FastGraph<float>>(parsedGraph, order);
if (quantileFileName.size() == 0){ if (quantileFileName.empty()){
throw std::runtime_error("No file found at '" + quantileFileName + "'"); throw std::runtime_error("No file found at '" + quantileFileName + "'");
} }
...@@ -150,7 +150,7 @@ int ElectronDNNCalculator::readQuantileTransformer( TTree* tree, const std::vect ...@@ -150,7 +150,7 @@ int ElectronDNNCalculator::readQuantileTransformer( TTree* tree, const std::vect
sc = tree->SetBranchAddress("references", &references) == -5 ? 0 : 1; sc = tree->SetBranchAddress("references", &references) == -5 ? 0 : 1;
std::map<std::string, double> readVars; std::map<std::string, double> readVars;
for ( auto var : variables ){ for ( const auto& var : variables ){
sc = tree->SetBranchAddress(TString(var), &readVars[var]) == -5 ? 0 : 1; sc = tree->SetBranchAddress(TString(var), &readVars[var]) == -5 ? 0 : 1;
} }
for (int i = 0; i < tree->GetEntries(); i++){ for (int i = 0; i < tree->GetEntries(); i++){
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment