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