ATLAS Offline Software
Loading...
Searching...
No Matches
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>
13
14
15namespace PMGTools
16{
17
18std::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 {"+", "plus"},
48 {"-", "minus"},
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
72std::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
Postprocess generator weight names to be name/ROOT friendly.
Tool providing sample cross-sections and k-factors etc.
constexpr char generatorWeightsPrefix[]
the prefix for generator weights
std::string weightNameCleanup(const std::string &name)
cleanup the weight name
std::string weightNameWithPrefix(const std::string &name)
cleanup the weight name and add prefix