Skip to content

Commit 995b5fc

Browse files
committed
Add new ValsFromRange function: like MassesFromRange but supports -ve numbers
- fixes #606
1 parent ae5dda7 commit 995b5fc

File tree

3 files changed

+76
-1
lines changed

3 files changed

+76
-1
lines changed

CombineHarvester/CombineTools/interface/Utilities.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,52 @@ std::vector<std::vector<unsigned>> GenerateCombinations(
116116

117117
std::vector<std::string> ParseFileLines(std::string const& file_name);
118118

119+
/**
120+
* Generate a vector of mass values using ranges and intervals specified in a
121+
* string
122+
*
123+
* The input string should be of the format:
124+
*
125+
* m1-m2:r1,m3,m4,m5-m6:r2,...
126+
*
127+
* where mi and ri must both be positive. A term like mi-mj:r (where mi must
128+
* be < mj) genrates masses starting at mi and increasing by an interval r up
129+
* to mj.
130+
*
131+
* This function returns a vector of ordered mass values converted to strings.
132+
*
133+
* \note Use the function ch::ValsFromRange if you need to include negative
134+
* numbers - this uses a different syntax for the ranges so doesn't suffer
135+
* from the amiguity of the `-` sign
136+
*
137+
* @param input The input string to decode
138+
* @param fmt The format specifier for converting floating-point mass values
139+
* to strings
140+
*/
119141
std::vector<std::string> MassesFromRange(std::string const& input,
120142
std::string const& fmt = "%.0f");
121143

144+
/**
145+
* Generate a vector of values using ranges and intervals specified in a
146+
* string
147+
*
148+
* The input string should be of the format:
149+
*
150+
* m1:m2|r1,m3,m4,m5:m6|r2,...
151+
*
152+
* where mi and ri can be positive or negative. A term like mi-mj:r (where mi
153+
* must be < mj) genrates values starting at mi and increasing by an interval
154+
* r up to mj.
155+
*
156+
* This function returns a vector of ordered values converted to strings.
157+
*
158+
* @param input The input string to decode
159+
* @param fmt The format specifier for converting floating-point values to
160+
* strings
161+
*/
162+
std::vector<std::string> ValsFromRange(std::string const& input,
163+
std::string const& fmt = "%.0f");
164+
122165
bool HasNegativeBins(TH1 const* h);
123166

124167
void ZeroNegativeBins(TH1 *h);

CombineHarvester/CombineTools/src/Utilities.cc

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,38 @@ std::vector<std::string> MassesFromRange(std::string const& input,
252252
return result;
253253
}
254254

255+
std::vector<std::string> ValsFromRange(std::string const& input,
256+
std::string const& fmt) {
257+
std::set<double> mass_set;
258+
std::vector<std::string> tokens;
259+
boost::split(tokens, input, boost::is_any_of(","));
260+
for (auto const& t : tokens) {
261+
std::vector<std::string> sub_tokens;
262+
boost::split(sub_tokens, t, boost::is_any_of(":|"));
263+
if (sub_tokens.size() == 1) {
264+
double mass_val = boost::lexical_cast<double>(sub_tokens[0]);
265+
mass_set.insert(mass_val);
266+
} else if (sub_tokens.size() == 3) {
267+
double lo = boost::lexical_cast<double>(sub_tokens[0]);
268+
double hi = boost::lexical_cast<double>(sub_tokens[1]);
269+
double step = boost::lexical_cast<double>(sub_tokens[2]);
270+
if (hi <= lo)
271+
throw std::runtime_error(
272+
"[ValsFromRange] High mass is smaller than low mass!");
273+
double start = lo;
274+
while (start < hi + 1E-4) {
275+
mass_set.insert(start);
276+
start += step;
277+
}
278+
}
279+
}
280+
std::vector<std::string> result;
281+
for (auto const& m : mass_set) {
282+
result.push_back((boost::format(fmt) % m).str());
283+
}
284+
return result;
285+
}
286+
255287
boost::filesystem::path make_relative(boost::filesystem::path p_from,
256288
boost::filesystem::path p_to) {
257289
p_from = boost::filesystem::absolute(p_from);

CombineHarvester/CombineTools/test/SMLegacyExample.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ int main() {
9494
{0, "tauTau_1jet_high_mediumhiggs"}, {1, "tauTau_1jet_high_highhiggs"},
9595
{2, "tauTau_vbf"}};
9696

97-
vector<string> masses = ch::MassesFromRange("110-145:5");
97+
vector<string> masses = ch::ValsFromRange("110:145|5");
9898

9999
cout << ">> Creating processes and observations...\n";
100100
for (string era : {"7TeV", "8TeV"}) {

0 commit comments

Comments
 (0)