ATLAS Offline Software
WeightHelpers.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 
7 
8 #include <unordered_map>
9 #include <algorithm>
10 #include <string>
11 #include <vector>
12 #include "PMGTools/WeightHelpers.h"
13 
14 
15 namespace PMGTools
16 {
17 
18 std::string weightNameCleanup(const std::string &name)
19 {
20  // empty weight is nominal
21  if (name.empty())
22  {
23  return {};
24  }
25 
26  // Trim trailing whitespace
27  size_t start = name.find_first_not_of(" \t\n\r");
28  size_t end = name.find_last_not_of(" \t\n\r");
29  std::string out = name.substr(start,end-start+1);
30  std::string outLowercase = out;
31  std::transform(outLowercase.begin(), outLowercase.end(), outLowercase.begin(),
32  [](unsigned char c){ return std::tolower(c); });
33 
34  // more cases of nominal weights
35  if (outLowercase == "default" // This is a primary weight in newer samples
36  || outLowercase == "nominal" // Powheg calls it "nominal"
37  || outLowercase == "weight" // Sherpa names the nominal weight just "Weight"
38  || outLowercase == "0")
39  {
40  return {};
41  }
42 
43  static const std::vector<std::pair<std::string, std::string>> substitutions =
44  {
45  {" set = ", "_"}, // Powheg
46  {" = ", "_"}, // Powheg
47  {"+", ""},
48  {"-", ""},
49  {"=", ""},
50  {",", ""},
51  {".", ""},
52  {":", ""},
53  {" ", "_"},
54  {"#", "num"},
55  {"\n", "_"},
56  {"/", "over"}, // MadGraph
57  };
58 
59  for (const auto &[before, after] : substitutions)
60  {
61  size_t location = out.find(before);
62  while (location != std::string::npos)
63  {
64  out.replace(location, before.length(), after);
65  location = out.find(before);
66  }
67  }
68 
69  return out;
70 }
71 
72 std::string weightNameWithPrefix(const std::string &name)
73 {
74  std::string out = weightNameCleanup(name);
75  if (out.empty())
76  {
77  return out;
78  }
79 
80  return generatorWeightsPrefix + out;
81 }
82 
83 } // namespace MC
WeightHelpers.h
Postprocess generator weight names to be name/ROOT friendly.
mergePhysValFiles.start
start
Definition: DataQuality/DataQualityUtils/scripts/mergePhysValFiles.py:14
python.AthDsoLogger.out
out
Definition: AthDsoLogger.py:71
PMGTools::weightNameCleanup
std::string weightNameCleanup(const std::string &name)
cleanup the weight name
Definition: WeightHelpers.cxx:18
mergePhysValFiles.end
end
Definition: DataQuality/DataQualityUtils/scripts/mergePhysValFiles.py:93
python.sizes.location
string location
Definition: sizes.py:11
PMGTools
Tool providing sample cross-sections and k-factors etc.
Definition: AnalysisCommon/PMGTools/PMGTools/IPMGCrossSectionTool.h:15
Amg::transform
Amg::Vector3D transform(Amg::Vector3D &v, Amg::Transform3D &tr)
Transform a point from a Trasformation3D.
Definition: GeoPrimitivesHelpers.h:156
PMGTools::weightNameWithPrefix
std::string weightNameWithPrefix(const std::string &name)
cleanup the weight name and add prefix
Definition: WeightHelpers.cxx:72
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:192
PMGTools::generatorWeightsPrefix
constexpr char generatorWeightsPrefix[]
the prefix for generator weights
Definition: WeightHelpers.h:19
python.compressB64.c
def c
Definition: compressB64.py:93