Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
StringUtils.cxx
Go to the documentation of this file.
1/// \file ROOT/StringUtils.hxx
2/// \author Jonas Rembser <jonas.rembser@cern.ch>
3/// \date 2021-08-09
4
5/*************************************************************************
6 * Copyright (C) 1995-2019, Rene Brun and Fons Rademakers. *
7 * All rights reserved. *
8 * *
9 * For the licensing terms see $ROOTSYS/LICENSE. *
10 * For the list of contributors see $ROOTSYS/README/CREDITS. *
11 *************************************************************************/
12
13#include "ROOT/StringUtils.hxx"
14#include <sstream>
15#include <cmath>
16#include <ios>
17#include <cassert>
18
19namespace ROOT {
20
21/// Splits a string at each character in delims.
22/// The behavior mimics `str.split` from Python,
23/// \param[in] str String to tokenise.
24/// \param[in] delims One or more delimiters used to split the string.
25/// \param[in] skipEmpty Strip empty strings from the output.
26std::vector<std::string> Split(std::string_view str, std::string_view delims, bool skipEmpty /* =false */)
27{
28 std::vector<std::string> out;
29
30 std::size_t beg = 0;
31 std::size_t end = 0;
32 while ((end = str.find_first_of(delims, beg)) != std::string::npos) {
33 if (!skipEmpty || end > beg)
34 out.emplace_back(str.substr(beg, end - beg));
35 beg = end + 1;
36 }
37 if (!skipEmpty || str.size() > beg)
38 out.emplace_back(str.substr(beg, str.size() - beg));
39
40 return out;
41}
42
43/**
44 * \brief Convert (round) a value and its uncertainty to string using one or two significant digits of the error
45 * \param error the error. If the error is negative or zero, only the value is returned with no specific rounding
46 * applied, using std::to_string
47 * \param cutoff should lay between 0 and 9. If first significant digit of error starts with value <= cutoff,
48 * use two significant digits instead of two for rounding. Set this value to zero to always use a
49 * single digit; set this value to 9 to always use two digits.
50 * \param delim delimiter between value and error printed into returned string, leave default for using ROOT's latex
51 * mode.
52 * \return a string with printed rounded value and error separated by "+/-" in ROOT latex mode \note The return
53 * format is `A+-B` using ios::fixed with the proper precision; for very large or very small values of the error, the
54 * format is changed from `A+-B` to (A'+-B')*1eX, with X being multiple of 3, respecting the corresponding precision.
55 * \see https://www.bipm.org/en/doi/10.59161/jcgm100-2008e, https://physics.nist.gov/cuu/Uncertainty/
56 */
57std::string Round(double value, double error, unsigned int cutoff, std::string_view delim)
58{
59 if (error <= 0.) {
60 return std::to_string(value);
61 }
62
63 int error_exponent_base10_rounded = std::floor(std::log10(error));
64 const auto error_magnitude_base10 = std::pow(10., error_exponent_base10_rounded);
65 const auto error_first_digit = static_cast<unsigned int>(error / error_magnitude_base10);
68 const double rescaled_error = error * std::pow(10., -1. * error_exponent_base10_rounded);
69 if (static_cast<unsigned int>(std::round(rescaled_error * 10) / 10) <= cutoff)
71 } else if (cutoff == 0 && error_first_digit == 9) {
72 const double rounded_rescaled_error = std::round(error * std::pow(10., -1. * error_exponent_base10_rounded));
80 }
81 const int factored_out_exponent_base10 = error <= 1e-3 ? static_cast<int>(std::floor(std::log10(error) / 3)) * 3
82 : static_cast<int>(std::log10(error) / 3) * 3;
83
84 std::stringstream result;
85 result.setf(std::ios::fixed);
88 } else {
89 result.precision(0);
90 }
92 result << "(";
93 result << std::round(value * std::pow(10., -error_exponent_base10_rounded)) /
95 result << delim;
96 result << std::round(error * std::pow(10., -error_exponent_base10_rounded)) /
100 return result.str();
101}
102
103} // namespace ROOT
#define e(i)
Definition RSha256.hxx:103
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t result
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
std::string Round(double value, double error, unsigned int cutoff=1, std::string_view delim="#pm")
Convert (round) a value and its uncertainty to string using one or two significant digits of the erro...
std::vector< std::string > Split(std::string_view str, std::string_view delims, bool skipEmpty=false)
Splits a string at each character in delims.