ATLAS Offline Software
AsgEGammaConfigHelper.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3 */
4 
7 #include "TEnv.h"
8 #include <iostream>
9 #include <sstream>
10 #include <limits>
11 
12 namespace AsgConfigHelper {
13 
14 std::string
15 findConfigFile(const std::string& input,
16  const std::map<std::string, std::string>& configmap)
17 {
18  auto confFile_itr = configmap.find(input);
19  if (confFile_itr == configmap.end()) {
20  static const asg::AsgMessaging msg("Egamma::AsgConfigHelper");
21  msg.msg(MSG::WARNING) << "Key " << input
22  << " not found in map, no config file returned"
23  << endmsg;
24  return "";
25  }
26  return confFile_itr->second;
27 }
28 
29 unsigned int
30 findMask(const std::string& input,
31  const std::map<std::string, unsigned int>& maskmap)
32 {
33  auto mask_itr = maskmap.find(input);
34  if (mask_itr == maskmap.end()) {
35  static const asg::AsgMessaging msg("Egamma::AsgConfigHelper");
36  msg.msg(MSG::WARNING)
37  << "Key " << input
38  << " not found in map, egammaPID::EgPidUndefined mask returned"
39  << endmsg;
40  // mask has the choice to default to all 1 or all 0 bits, choose the former
42  }
43  return static_cast<unsigned int>(mask_itr->second);
44 }
45 
46 template<typename T>
47 bool
48 strtof(const std::string& input, T& f)
49 {
50  int diff = 0;
51  std::string tmp = input;
52  std::string::size_type first(0);
53  std::string::size_type last(0);
54  first = (input.find('#'));
55 
56  // if we do not find a comment character "#" we are fine
57  if (first == std::string::npos) {
58  std::istringstream buffer(tmp);
59  buffer >> f;
60  return true;
61  } else {
62  // if we have found comment character check if it is inlined between two "#"
63  last = (input.find('#', first + 1));
64  // if nor error
65  if (last == std::string::npos) {
66  static const asg::AsgMessaging msg("Egamma::AsgConfigHelper");
67  msg.msg(MSG::WARNING) << " Improper comment format , inline comment "
68  "should be enclosed between two # "
69  << endmsg;
70  return false;
71  }
72  // else if between two "#" remove this part
73  diff = last - first;
74  tmp = tmp.erase(first, diff + 1);
75  std::istringstream buffer(tmp);
76  buffer >> f;
77  return true;
78  }
79 }
80 
81 template<typename T>
82 std::vector<T>
83 Helper(const std::string& input, TEnv& env)
84 {
85  std::vector<T> CutVector;
86  std::string env_input(env.GetValue(input.c_str(), ""));
87  if (!env_input.empty()) {
88  std::string::size_type end;
89  do {
90  end = env_input.find(';');
91  T myValue{}; //default init
92  if (AsgConfigHelper::strtof(env_input.substr(0, end), myValue)) {
93  CutVector.push_back(myValue);
94  }
95  if (end != std::string::npos) {
96  env_input = env_input.substr(end + 1);
97  }
98  } while (end != std::string::npos);
99  }
100  return CutVector;
101 }
102 }
103 
104 // use the specializations
105 std::vector<double>
106 AsgConfigHelper::HelperDouble(const std::string& input, TEnv& env)
107 {
108  return AsgConfigHelper::Helper<double>(input, env);
109 }
110 std::vector<float>
111 AsgConfigHelper::HelperFloat(const std::string& input, TEnv& env)
112 {
113  return AsgConfigHelper::Helper<float>(input, env);
114 }
115 std::vector<int>
116 AsgConfigHelper::HelperInt(const std::string& input, TEnv& env)
117 {
118  return AsgConfigHelper::Helper<int>(input, env);
119 }
120 // template does not work for std::string because of the T myValue(0);
121 // declaration, so implement it again for std::string
122 std::vector<std::string>
123 AsgConfigHelper::HelperString(const std::string& input, TEnv& env)
124 {
125  return AsgConfigHelper::Helper<std::string>(input, env);
126 }
127 
AsgConfigHelper::HelperString
std::vector< std::string > HelperString(const std::string &input, TEnv &env)
Definition: AsgEGammaConfigHelper.cxx:123
AsgConfigHelper::Helper
std::vector< T > Helper(const std::string &input, TEnv &env)
Definition: AsgEGammaConfigHelper.cxx:83
max
constexpr double max()
Definition: ap_fixedTest.cxx:33
mc.diff
diff
Definition: mc.SFGenPy8_MuMu_DD.py:14
AsgEGammaConfigHelper.h
mergePhysValFiles.end
end
Definition: DataQuality/DataQualityUtils/scripts/mergePhysValFiles.py:92
AsgConfigHelper
Definition: AsgEGammaConfigHelper.h:21
AsgMessaging.h
createCoolChannelIdFile.buffer
buffer
Definition: createCoolChannelIdFile.py:11
endmsg
#define endmsg
Definition: AnalysisConfig_Ntuple.cxx:63
PlotPulseshapeFromCool.input
input
Definition: PlotPulseshapeFromCool.py:106
hist_file_dump.f
f
Definition: hist_file_dump.py:140
DeMoUpdate.tmp
string tmp
Definition: DeMoUpdate.py:1167
AsgConfigHelper::HelperInt
std::vector< int > HelperInt(const std::string &input, TEnv &env)
Definition: AsgEGammaConfigHelper.cxx:116
asg::AsgMessaging
Class mimicking the AthMessaging class from the offline software.
Definition: AsgMessaging.h:40
AsgConfigHelper::strtof
bool strtof(const std::string &input, T &f)
Definition: AsgEGammaConfigHelper.cxx:48
DeMoScan.first
bool first
Definition: DeMoScan.py:534
AsgConfigHelper::findMask
unsigned int findMask(const std::string &input, const std::map< std::string, unsigned int > &maskmap)
Definition: AsgEGammaConfigHelper.cxx:30
AsgConfigHelper::HelperFloat
std::vector< float > HelperFloat(const std::string &input, TEnv &env)
Definition: AsgEGammaConfigHelper.cxx:111
python.DataFormatRates.env
env
Definition: DataFormatRates.py:32
AsgConfigHelper::HelperDouble
std::vector< double > HelperDouble(const std::string &input, TEnv &env)
Definition: AsgEGammaConfigHelper.cxx:106
python.AutoConfigFlags.msg
msg
Definition: AutoConfigFlags.py:7
AsgConfigHelper::findConfigFile
std::string findConfigFile(const std::string &input, const std::map< std::string, std::string > &configmap)
Definition: AsgEGammaConfigHelper.cxx:15