From 7e6433d1fe45fa891f2ae22ab7b0ac878644cad6 Mon Sep 17 00:00:00 2001 From: Felice Pantaleo Date: Thu, 13 Jun 2024 14:51:06 +0200 Subject: [PATCH 1/3] Introduce vFloat and float types in ParameterSets --- FWCore/ParameterSet/interface/Entry.h | 8 ++ .../interface/ParameterDescription.h | 4 + .../interface/ParameterDescriptionNode.h | 4 +- FWCore/ParameterSet/interface/ParameterSet.h | 50 +++++++++++ FWCore/ParameterSet/interface/types.h | 8 ++ FWCore/ParameterSet/python/Config.py | 5 +- FWCore/ParameterSet/python/DummyCfis.py | 2 + FWCore/ParameterSet/python/Types.py | 85 ++++++++++++++----- FWCore/ParameterSet/src/Entry.cc | 72 +++++++++++++++- .../ParameterSet/src/ParameterDescription.cc | 10 +++ .../src/ParameterDescriptionNode.cc | 4 + FWCore/ParameterSet/src/ParameterSet.cc | 78 +++++++++++++++++ FWCore/ParameterSet/src/types.cc | 72 ++++++++++++++++ FWCore/ParameterSet/test/cmsconfig.py | 2 +- .../test_catch_ParameterSetDescription.cc | 16 ++++ .../PythonParameterSet/src/PyBind11Module.h | 4 + 16 files changed, 397 insertions(+), 27 deletions(-) diff --git a/FWCore/ParameterSet/interface/Entry.h b/FWCore/ParameterSet/interface/Entry.h index 2dedc814b3a9a..57a092c6df4f6 100644 --- a/FWCore/ParameterSet/interface/Entry.h +++ b/FWCore/ParameterSet/interface/Entry.h @@ -83,6 +83,14 @@ namespace edm { Entry(std::string const& name, std::vector const& val, bool is_tracked); std::vector getVDouble() const; + // Float + Entry(std::string const& name, float val, bool is_tracked); + float getFloat() const; + + // vFloat + Entry(std::string const& name, std::vector const& val, bool is_tracked); + std::vector getVFloat() const; + // String Entry(std::string const& name, std::string const& val, bool is_tracked); std::string getString() const; diff --git a/FWCore/ParameterSet/interface/ParameterDescription.h b/FWCore/ParameterSet/interface/ParameterDescription.h index b1f55782149c3..99187c183872c 100644 --- a/FWCore/ParameterSet/interface/ParameterDescription.h +++ b/FWCore/ParameterSet/interface/ParameterDescription.h @@ -58,6 +58,8 @@ namespace edm { ValueFormat format); void writeValue(std::ostream& os, int indentation, double const& value_, ValueFormat format); void writeValue(std::ostream& os, int indentation, std::vector const& value_, ValueFormat format); + void writeValue(std::ostream& os, int indentation, float const& value_, ValueFormat format); + void writeValue(std::ostream& os, int indentation, std::vector const& value_, ValueFormat format); void writeValue(std::ostream& os, int indentation, bool const& value_, ValueFormat format); void writeValue(std::ostream& os, int indentation, std::string const& value_, ValueFormat format); void writeValue(std::ostream& os, int indentation, std::vector const& value_, ValueFormat format); @@ -88,6 +90,8 @@ namespace edm { bool hasNestedContent(std::vector const& value); bool hasNestedContent(double const& value); bool hasNestedContent(std::vector const& value); + bool hasNestedContent(float const& value); + bool hasNestedContent(std::vector const& value); bool hasNestedContent(bool const& value); bool hasNestedContent(std::string const& value); bool hasNestedContent(std::vector const& value); diff --git a/FWCore/ParameterSet/interface/ParameterDescriptionNode.h b/FWCore/ParameterSet/interface/ParameterDescriptionNode.h index 95a9151da09b7..59609bf022e39 100644 --- a/FWCore/ParameterSet/interface/ParameterDescriptionNode.h +++ b/FWCore/ParameterSet/interface/ParameterDescriptionNode.h @@ -65,7 +65,9 @@ namespace edm { k_EventRange = 'R', k_VEventRange = 'r', k_PSet = 'Q', - k_VPSet = 'q' + k_VPSet = 'q', + k_float = 'H', + k_vfloat = 'h' }; std::string parameterTypeEnumToString(ParameterTypes iType); diff --git a/FWCore/ParameterSet/interface/ParameterSet.h b/FWCore/ParameterSet/interface/ParameterSet.h index f518f8c996523..6a6a2a23ce081 100644 --- a/FWCore/ParameterSet/interface/ParameterSet.h +++ b/FWCore/ParameterSet/interface/ParameterSet.h @@ -395,6 +395,15 @@ namespace edm { template <> std::vector ParameterSet::getParameter>(std::string const& name) const; + // ---------------------------------------------------------------------- + // Float, vFloat + + template <> + float ParameterSet::getParameter(std::string const& name) const; + + template <> + std::vector ParameterSet::getParameter>(std::string const& name) const; + // ---------------------------------------------------------------------- // String, vString @@ -618,6 +627,22 @@ namespace edm { template <> std::vector ParameterSet::getUntrackedParameter>(std::string const& name) const; + // ---------------------------------------------------------------------- + // Float, vFloat + + template <> + float ParameterSet::getUntrackedParameter(std::string const& name, float const& defaultValue) const; + + template <> + float ParameterSet::getUntrackedParameter(std::string const& name) const; + + template <> + std::vector ParameterSet::getUntrackedParameter>( + std::string const& name, std::vector const& defaultValue) const; + + template <> + std::vector ParameterSet::getUntrackedParameter>(std::string const& name) const; + // ---------------------------------------------------------------------- // String, vString @@ -778,6 +803,15 @@ namespace edm { template <> std::vector ParameterSet::getParameter>(char const* name) const; + // ---------------------------------------------------------------------- + // Float, vFloat + + template <> + float ParameterSet::getParameter(char const* name) const; + + template <> + std::vector ParameterSet::getParameter>(char const* name) const; + // ---------------------------------------------------------------------- // String, vString @@ -969,6 +1003,22 @@ namespace edm { template <> std::vector ParameterSet::getUntrackedParameter>(char const* name) const; + // ---------------------------------------------------------------------- + // Float, vFloat + + template <> + float ParameterSet::getUntrackedParameter(char const* name, float const& defaultValue) const; + + template <> + float ParameterSet::getUntrackedParameter(char const* name) const; + + template <> + std::vector ParameterSet::getUntrackedParameter>( + char const* name, std::vector const& defaultValue) const; + + template <> + std::vector ParameterSet::getUntrackedParameter>(char const* name) const; + // ---------------------------------------------------------------------- // String, vString diff --git a/FWCore/ParameterSet/interface/types.h b/FWCore/ParameterSet/interface/types.h index d33711460cc80..83614efe31e84 100644 --- a/FWCore/ParameterSet/interface/types.h +++ b/FWCore/ParameterSet/interface/types.h @@ -72,6 +72,14 @@ namespace edm { bool decode(std::vector&, std::string_view); bool encode(std::string&, std::vector const&); + // Float + bool decode(float&, std::string_view); + bool encode(std::string&, float); + + // vFloat + bool decode(std::vector&, std::string_view); + bool encode(std::string&, std::vector const&); + // String bool decode(std::string&, std::string_view); bool encode(std::string&, std::string const&); diff --git a/FWCore/ParameterSet/python/Config.py b/FWCore/ParameterSet/python/Config.py index 33f712b1a4df8..a4b03411590a3 100644 --- a/FWCore/ParameterSet/python/Config.py +++ b/FWCore/ParameterSet/python/Config.py @@ -7,7 +7,6 @@ from .Options import Options options = Options() - ## imports import sys from typing import Union @@ -2141,6 +2140,10 @@ def addDouble(self,tracked,label,value): self.__insertValue(tracked,label,value) def addVDouble(self,tracked,label,value): self.__insertValue(tracked,label,value) + def addFloat(self,tracked,label,value): + self.__insertValue(tracked,label,value) + def addVFloat(self,tracked,label,value): + self.__insertValue(tracked,label,value) def addBool(self,tracked,label,value): self.__insertValue(tracked,label,value) def addString(self,tracked,label,value): diff --git a/FWCore/ParameterSet/python/DummyCfis.py b/FWCore/ParameterSet/python/DummyCfis.py index d133216c2ae49..f1e9fb25e2e09 100644 --- a/FWCore/ParameterSet/python/DummyCfis.py +++ b/FWCore/ParameterSet/python/DummyCfis.py @@ -11,6 +11,8 @@ cms.string.dummyDefault="__default__" cms.double.dummyDefault = 9999 cms.vdouble.dummyDefault = [] +cms.float.dummyDefault = 9999 +cms.vfloat.dummyDefault = [] cms.vint32.dummyDefault = [] cms.vuint32.dummyDefault = [] cms.vint64.dummyDefault = [] diff --git a/FWCore/ParameterSet/python/Types.py b/FWCore/ParameterSet/python/Types.py index 1511dcb470e8c..120f5606d7ab8 100644 --- a/FWCore/ParameterSet/python/Types.py +++ b/FWCore/ParameterSet/python/Types.py @@ -323,13 +323,13 @@ def __nonzero__(self) -> bool: class double(_SimpleParameterTypeBase): @staticmethod def _isValid(value) -> bool: - return isinstance(value, (int, long, float)) + return isinstance(value, (int, long, builtins.float)) @staticmethod def _valueFromString(value:str): """only used for cfg-parsing""" - return double(float(value)) + return double(builtins.float(value)) def insertInto(self, parameterSet, myname:str): - parameterSet.addDouble(self.isTracked(), myname, float(self.value())) + parameterSet.addDouble(self.isTracked(), myname, builtins.float(self.value())) def __nonzero__(self) -> bool: return self.value()!=0. def configValue(self, options:PrintOptions=PrintOptions()) -> str: @@ -345,7 +345,30 @@ def _pythonValue(value) -> str: return "float('nan')" return str(value) - +class float(_SimpleParameterTypeBase): + @staticmethod + def _isValid(value) -> bool: + return isinstance(value, (int, long, builtins.float)) + @staticmethod + def _valueFromString(value:str): + """only used for cfg-parsing""" + return float(builtins.float(value)) + def insertInto(self, parameterSet, myname:str): + parameterSet.addFloat(self.isTracked(), myname, builtins.float(self.value())) + def __nonzero__(self) -> bool: + return self.value()!=0. + def configValue(self, options:PrintOptions=PrintOptions()) -> str: + return float._pythonValue(self._value) + @staticmethod + def _pythonValue(value) -> str: + if math.isinf(value): + if value > 0: + return "float('inf')" + else: + return "-float('inf')" + if math.isnan(value): + return "float('nan')" + return str(value) class bool(_SimpleParameterTypeBase): @staticmethod @@ -1022,7 +1045,6 @@ def _place(self,name:str,proc): proc._placePSet(name,self) def __str__(self) -> str: return object.__str__(self) - class PSet(_ParameterTypeBase,_Parameterizable,_ConfigureComponent,_Labelable): def __init__(self,*arg,**args): #need to call the inits separately @@ -1157,7 +1179,19 @@ def insertInto(self, parameterSet, myname:str): def pythonValueForItem(self,item, options) -> str: return double._pythonValue(item) - +class vfloat(_ValidatingParameterListBase): + def __init__(self,*arg,**args): + super(vfloat,self).__init__(*arg,**args) + @classmethod + def _itemIsValid(cls,item) -> builtins.bool: + return float._isValid(item) + @staticmethod + def _valueFromString(value:str): + return vfloat(*_ValidatingParameterListBase._itemsFromStrings(value,float._valueFromString)) + def insertInto(self, parameterSet, myname:str): + parameterSet.addVFloat(self.isTracked(), myname, self.value()) + def pythonValueForItem(self,item, options) -> str: + return float._pythonValue(item) class vbool(_ValidatingParameterListBase): @@ -1439,6 +1473,10 @@ def addDouble(self,tracked:bool,label:str,value): v = double(value) v.setIsTracked(tracked) setattr(self.pset,label,v) + def addFloat(self,tracked:bool,label:str,value): + v = float(value) + v.setIsTracked(tracked) + setattr(self.pset,label,v) def addString(self,tracked:bool,label:str,value): v = string(value) v.setIsTracked(tracked) @@ -1491,6 +1529,11 @@ def addVDouble(self,tracked:bool,label:str,value): v = vdouble(value) v.setIsTracked(tracked) setattr(self.pset,label,v) + + def addVFloat(self,tracked:bool,label:str,value): + v = vfloat(value) + v.setIsTracked(tracked) + setattr(self.pset,label,v) def addVString(self,tracked:bool,label:str,value): v = vstring(value) v.setIsTracked(tracked) @@ -1680,26 +1723,26 @@ def testdouble(self): d = double(1) self.assertEqual(d.value(),1) self.assertEqual(d.pythonValue(),'1') - d = double(float('Inf')) - self.assertEqual(d,float('Inf')) + d = double(builtins.float('Inf')) + self.assertEqual(d,builtins.float('Inf')) self.assertEqual(d.pythonValue(),"float('inf')") - d = double(-float('Inf')) - self.assertEqual(d,-float('Inf')) + d = double(-builtins.float('Inf')) + self.assertEqual(d,-builtins.float('Inf')) self.assertEqual(d.pythonValue(),"-float('inf')") - d = double(float('Nan')) + d = double(builtins.float('Nan')) self.assertTrue(math.isnan(d.value())) self.assertEqual(d.pythonValue(),"float('nan')") def testvdouble(self): d = vdouble(1) self.assertEqual(d.value(),[1]) self.assertEqual(d.dumpPython(),'cms.vdouble(1)') - d = vdouble(float('inf')) - self.assertEqual(d,[float('inf')]) + d = vdouble(builtins.float('inf')) + self.assertEqual(d,[builtins.float('inf')]) self.assertEqual(d.dumpPython(),"cms.vdouble(float('inf'))") - d = vdouble(-float('Inf')) - self.assertEqual(d,[-float('inf')]) + d = vdouble(-builtins.float('Inf')) + self.assertEqual(d,[-builtins.float('inf')]) self.assertEqual(d.dumpPython(),"cms.vdouble(-float('inf'))") - d = vdouble(float('nan')) + d = vdouble(builtins.float('nan')) self.assertTrue(math.isnan(d[0])) self.assertEqual(d.dumpPython(),"cms.vdouble(float('nan'))") def testvint32(self): @@ -2539,12 +2582,12 @@ def testincompatibletypes(self): with self.assertRaises(TypeError): 3 < string("I am a string") def testinfinity(self): - self.assertLess(1e99, double(float("inf"))) - self.assertLess(double(1e99), float("inf")) - self.assertGreater(1e99, double(float("-inf"))) - self.assertEqual(double(float("inf")), float("inf")) + self.assertLess(1e99, double(builtins.float("inf"))) + self.assertLess(double(1e99), builtins.float("inf")) + self.assertGreater(1e99, double(builtins.float("-inf"))) + self.assertEqual(double(builtins.float("inf")), builtins.float("inf")) def testnan(self): - nan = double(float("nan")) + nan = double(builtins.float("nan")) self.assertNotEqual(nan, nan) self.assertFalse(nan > 3 or nan < 3 or nan == 3) diff --git a/FWCore/ParameterSet/src/Entry.cc b/FWCore/ParameterSet/src/Entry.cc index 943985063d17c..e882221a5e2ed 100644 --- a/FWCore/ParameterSet/src/Entry.cc +++ b/FWCore/ParameterSet/src/Entry.cc @@ -51,11 +51,14 @@ enum : char { kTLuminosityBlockRange = 'A', kTVLuminosityBlockRange = 'a', kTEventRange = 'R', - kTVEventRange = 'r' + kTVEventRange = 'r', + kTfloat = 'H', + kTvfloat = 'h', }; -static constexpr const std::array s_types = {{"bool", +static constexpr const std::array s_types = {{"bool", "double", + "float", "int32", "int64", "path", @@ -64,6 +67,7 @@ static constexpr const std::array s_types = {{"bool", "uint32", "uint64", "vdouble", + "vfloat", "vint32", "vint64", "vstring", @@ -87,8 +91,9 @@ static constexpr const std::array s_types = {{"bool", "VLuminosityBlockID", "VLuminosityBlockRange"}}; -static constexpr const std::array s_codes = {{kTbool, +static constexpr const std::array s_codes = {{kTbool, kTdouble, + kTfloat, kTint32, kTint64, kTpath, @@ -97,6 +102,7 @@ static constexpr const std::array s_codes = {{kTbool, kTuint32, kTuint64, kTvdouble, + kTvfloat, kTvint32, kTvint64, kTvstringRaw, @@ -166,6 +172,10 @@ static constexpr std::array fillTable() { table_[kTvdouble] = c2t(kTvdouble); static_assert(not c2t(kTdouble).empty()); table_[kTdouble] = c2t(kTdouble); + static_assert(not c2t(kTvfloat).empty()); + table_[kTvfloat] = c2t(kTvfloat); + static_assert(not c2t(kTfloat).empty()); + table_[kTfloat] = c2t(kTfloat); static_assert(not c2t(kTvPSet).empty()); table_[kTvPSet] = c2t(kTvPSet); static_assert(not c2t(kTPSet).empty()); @@ -376,6 +386,18 @@ namespace edm { throwEntryError("vector", rep_); break; } + case kTfloat: { // Float + float val; + if (!decode(val, rep_)) + throwEntryError("float", rep_); + break; + } + case kTvfloat: { // vFloat + std::vector val; + if (!decode(val, rep_)) + throwEntryError("vector", rep_); + break; + } case kTPSet: { // ParameterSet ParameterSet val; if (!decode(val, rep_)) @@ -536,6 +558,26 @@ namespace edm { validate(); } + // ---------------------------------------------------------------------- + // Float + + Entry::Entry(std::string const& name, float val, bool is_tracked) + : name_(name), rep_(), type_(kTfloat), tracked_(is_tracked ? '+' : '-') { + if (!encode(rep_, val)) + throwEncodeError("float"); + validate(); + } + + // ---------------------------------------------------------------------- + // vFloat + + Entry::Entry(std::string const& name, std::vector const& val, bool is_tracked) + : name_(name), rep_(), type_(kTvfloat), tracked_(is_tracked ? '+' : '-') { + if (!encode(rep_, val)) + throwEncodeError("vector"); + validate(); + } + // ---------------------------------------------------------------------- // String @@ -973,6 +1015,30 @@ namespace edm { return val; } + // ---------------------------------------------------------------------- + // Float + + float Entry::getFloat() const { + if (type_ != kTfloat) + throwValueError("float"); + float val; + if (!decode(val, rep_)) + throwEntryError("float", rep_); + return val; + } + + // ---------------------------------------------------------------------- + // vFloat + + std::vector Entry::getVFloat() const { + if (type_ != kTvfloat) + throwValueError("vector"); + std::vector val; + if (!decode(val, rep_)) + throwEntryError("vector", rep_); + return val; + } + // ---------------------------------------------------------------------- // String diff --git a/FWCore/ParameterSet/src/ParameterDescription.cc b/FWCore/ParameterSet/src/ParameterDescription.cc index 88eb3eb3bd6df..6226e973998c3 100644 --- a/FWCore/ParameterSet/src/ParameterDescription.cc +++ b/FWCore/ParameterSet/src/ParameterDescription.cc @@ -685,6 +685,14 @@ namespace edm { writeVector(os, indentation, value_, format); } + void writeValue(std::ostream& os, int, float const& value_, ValueFormat format) { + writeValue(os, value_, format); + } + + void writeValue(std::ostream& os, int indentation, std::vector const& value_, ValueFormat format) { + writeVector(os, indentation, value_, format); + } + void writeValue(std::ostream& os, int, bool const& value_, ValueFormat format) { writeValue(os, value_, format); } @@ -765,6 +773,8 @@ namespace edm { bool hasNestedContent(std::vector const& value) { return value.size() > 5U; } bool hasNestedContent(double const&) { return false; } bool hasNestedContent(std::vector const& value) { return value.size() > 5U; } + bool hasNestedContent(float const&) { return false; } + bool hasNestedContent(std::vector const& value) { return value.size() > 5U; } bool hasNestedContent(bool const&) { return false; } bool hasNestedContent(std::string const&) { return false; } bool hasNestedContent(std::vector const& value) { return value.size() > 5U; } diff --git a/FWCore/ParameterSet/src/ParameterDescriptionNode.cc b/FWCore/ParameterSet/src/ParameterDescriptionNode.cc index a1320688163f1..e4ba4fd1ac609 100644 --- a/FWCore/ParameterSet/src/ParameterDescriptionNode.cc +++ b/FWCore/ParameterSet/src/ParameterDescriptionNode.cc @@ -42,6 +42,8 @@ namespace edm { TYPE_TO_ENUM(std::vector, k_vuint64) TYPE_TO_ENUM(double, k_double) TYPE_TO_ENUM(std::vector, k_vdouble) + TYPE_TO_ENUM(float, k_float) + TYPE_TO_ENUM(std::vector, k_vfloat) TYPE_TO_ENUM(bool, k_bool) TYPE_TO_ENUM(std::string, k_stringRaw) TYPE_TO_ENUM(std::vector, k_vstringRaw) @@ -76,6 +78,8 @@ namespace edm { TYPE_TO_NAME(vuint64); TYPE_TO_NAME(double); TYPE_TO_NAME(vdouble); + TYPE_TO_NAME(float); + TYPE_TO_NAME(vfloat); TYPE_TO_NAME(bool); TYPE_TO_NAME2(k_stringRaw, string); TYPE_TO_NAME2(k_vstringRaw, vstring); diff --git a/FWCore/ParameterSet/src/ParameterSet.cc b/FWCore/ParameterSet/src/ParameterSet.cc index 2061982506eea..0632951c009ff 100644 --- a/FWCore/ParameterSet/src/ParameterSet.cc +++ b/FWCore/ParameterSet/src/ParameterSet.cc @@ -1007,6 +1007,19 @@ namespace edm { return retrieve(name).getVDouble(); } + // ---------------------------------------------------------------------- + // Float, vFloat + + template <> + float ParameterSet::getParameter(std::string const& name) const { + return retrieve(name).getFloat(); + } + + template <> + std::vector ParameterSet::getParameter >(std::string const& name) const { + return retrieve(name).getVFloat(); + } + // ---------------------------------------------------------------------- // String, vString @@ -1310,6 +1323,32 @@ namespace edm { return getEntryPointerOrThrow_(name)->getVDouble(); } + // ---------------------------------------------------------------------- + // Float, vFloat + + template <> + float ParameterSet::getUntrackedParameter(std::string const& name, float const& defaultValue) const { + Entry const* entryPtr = retrieveUntracked(name); + return entryPtr == nullptr ? defaultValue : entryPtr->getFloat(); + } + + template <> + float ParameterSet::getUntrackedParameter(std::string const& name) const { + return getEntryPointerOrThrow_(name)->getFloat(); + } + + template <> + std::vector ParameterSet::getUntrackedParameter >( + std::string const& name, std::vector const& defaultValue) const { + Entry const* entryPtr = retrieveUntracked(name); + return entryPtr == nullptr ? defaultValue : entryPtr->getVFloat(); + } + + template <> + std::vector ParameterSet::getUntrackedParameter >(std::string const& name) const { + return getEntryPointerOrThrow_(name)->getVFloat(); + } + // ---------------------------------------------------------------------- // String, vString @@ -1589,6 +1628,19 @@ namespace edm { return retrieve(name).getVDouble(); } + // ---------------------------------------------------------------------- + // Float, vFloat + + template <> + float ParameterSet::getParameter(char const* name) const { + return retrieve(name).getFloat(); + } + + template <> + std::vector ParameterSet::getParameter >(char const* name) const { + return retrieve(name).getVFloat(); + } + // ---------------------------------------------------------------------- // String, vString @@ -1889,6 +1941,32 @@ namespace edm { return getEntryPointerOrThrow_(name)->getVDouble(); } + // ---------------------------------------------------------------------- + // Float, vFloat + + template <> + float ParameterSet::getUntrackedParameter(char const* name, float const& defaultValue) const { + Entry const* entryPtr = retrieveUntracked(name); + return entryPtr == nullptr ? defaultValue : entryPtr->getFloat(); + } + + template <> + float ParameterSet::getUntrackedParameter(char const* name) const { + return getEntryPointerOrThrow_(name)->getFloat(); + } + + template <> + std::vector ParameterSet::getUntrackedParameter >( + char const* name, std::vector const& defaultValue) const { + Entry const* entryPtr = retrieveUntracked(name); + return entryPtr == nullptr ? defaultValue : entryPtr->getVFloat(); + } + + template <> + std::vector ParameterSet::getUntrackedParameter >(char const* name) const { + return getEntryPointerOrThrow_(name)->getVFloat(); + } + // ---------------------------------------------------------------------- // String, vString diff --git a/FWCore/ParameterSet/src/types.cc b/FWCore/ParameterSet/src/types.cc index f9b98120e3569..ceea85e250abf 100644 --- a/FWCore/ParameterSet/src/types.cc +++ b/FWCore/ParameterSet/src/types.cc @@ -498,6 +498,78 @@ bool edm::encode(std::string& to, std::vector const& from) { return true; } // encode from vector +// ---------------------------------------------------------------------- +// Float +// ---------------------------------------------------------------------- + +bool edm::decode(float& to, std::string_view from) { + if (from == "NaN") { + to = std::numeric_limits::quiet_NaN(); + } else if (from == "+inf" || from == "inf") { + to = std::numeric_limits::has_infinity ? std::numeric_limits::infinity() + : std::numeric_limits::max(); + } else if (from == "-inf") { + to = std::numeric_limits::has_infinity ? -std::numeric_limits::infinity() + : -std::numeric_limits::max(); + } + + else { + try { + to = std::stof(std::string(from)); + } catch (const std::exception&) { + return false; + } + } + return true; +} + +// ---------------------------------------------------------------------- + +bool edm::encode(std::string& to, float from) { + std::ostringstream ost; + ost.precision(std::numeric_limits::max_digits10); + ost << from; + if (!ost) + return false; + to = ost.str(); + return true; +} + +// ---------------------------------------------------------------------- +// vFloat +// ---------------------------------------------------------------------- + +bool edm::decode(std::vector& to, std::string_view from) { + to.clear(); + to.reserve(std::count(from.begin(), from.end(), ',')); + return split(from, '{', ',', '}', [&to](auto t) { + float val; + if (!decode(val, t)) + return false; + to.push_back(val); + return true; + }); +} // decode to vector + +// ---------------------------------------------------------------------- + +bool edm::encode(std::string& to, std::vector const& from) { + to = "{"; + + std::string converted; + for (std::vector::const_iterator b = from.begin(), e = from.end(); b != e; ++b) { + if (!encode(converted, *b)) + return false; + + if (b != from.begin()) + to += ","; + to += converted; + } + + to += '}'; + return true; +} // encode from vector + // ---------------------------------------------------------------------- // String // ---------------------------------------------------------------------- diff --git a/FWCore/ParameterSet/test/cmsconfig.py b/FWCore/ParameterSet/test/cmsconfig.py index c18db9c0deebf..a397735567696 100644 --- a/FWCore/ParameterSet/test/cmsconfig.py +++ b/FWCore/ParameterSet/test/cmsconfig.py @@ -77,7 +77,7 @@ def __init__(self, aName, aValueTuple): self.trackedCode = "untracked " # trailing space is needed # We need special handling of some of the parameter types. - if self.type in ["vbool", "vint32", "vuint32", "vdouble", "vstring", "VInputTag", "VESInputTag"]: + if self.type in ["vbool", "vint32", "vuint32", "vfloat", "vdouble", "vstring", "VInputTag", "VESInputTag"]: # TODO: Consider using cStringIO, if this is observed # to be a bottleneck. This may happen if many large # vectors are used in parameter sets. diff --git a/FWCore/ParameterSet/test/test_catch_ParameterSetDescription.cc b/FWCore/ParameterSet/test/test_catch_ParameterSetDescription.cc index 00b4819bd936e..6dfbf19b4c193 100644 --- a/FWCore/ParameterSet/test/test_catch_ParameterSetDescription.cc +++ b/FWCore/ParameterSet/test/test_catch_ParameterSetDescription.cc @@ -1695,6 +1695,15 @@ p = dict( CHECK(par->isTracked() == true); CHECK(edm::parameterTypeEnumToString(par->type()) == std::string("double")); + float myfloat = 5; + par = psetDesc.addOptional(std::string("floatvalue"), myfloat); + pset.addParameter("floatvalue", myfloat); + CHECK(par != 0); + CHECK(par->label() == std::string("floatvalue")); + CHECK(par->type() == edm::k_float); + CHECK(par->isTracked() == true); + CHECK(edm::parameterTypeEnumToString(par->type()) == std::string("float")); + bool f = true; par = psetDesc.addOptional("bvalue", f); pset.addParameter("bvalue", f); @@ -1770,6 +1779,13 @@ p = dict( CHECK(par->type() == edm::k_vdouble); CHECK(edm::parameterTypeEnumToString(par->type()) == std::string("vdouble")); + // make the same for std::vector + std::vector v5f; + par = psetDesc.add>("v5f", v5f); + pset.addParameter>("v5f", v5f); + CHECK(par->type() == edm::k_vfloat); + CHECK(edm::parameterTypeEnumToString(par->type()) == std::string("vfloat")); + std::vector v6; par = psetDesc.add>("v6", v6); pset.addParameter>("v6", v6); diff --git a/FWCore/PythonParameterSet/src/PyBind11Module.h b/FWCore/PythonParameterSet/src/PyBind11Module.h index a91b74ea7c9ea..5e9a30c257891 100644 --- a/FWCore/PythonParameterSet/src/PyBind11Module.h +++ b/FWCore/PythonParameterSet/src/PyBind11Module.h @@ -112,6 +112,10 @@ PYBIND11_MODULE(libFWCorePythonParameterSet, m) { .def("getVInt64", &Python11ParameterSet::getParameters) .def("addVUInt64", &Python11ParameterSet::addParameters) .def("getVUInt64", &Python11ParameterSet::getParameters) + .def("addFloat", &Python11ParameterSet::addParameter) + .def("getFloat", &Python11ParameterSet::getParameter) + .def("addVFloat", &Python11ParameterSet::addParameters) + .def("getVFloat", &Python11ParameterSet::getParameters) .def("addDouble", &Python11ParameterSet::addParameter) .def("getDouble", &Python11ParameterSet::getParameter) .def("addVDouble", &Python11ParameterSet::addParameters) From 591ad8e7b87019be98faef9cd43bcee5b16af857 Mon Sep 17 00:00:00 2001 From: Felice Pantaleo Date: Mon, 16 Sep 2024 12:49:35 +0200 Subject: [PATCH 2/3] move existsAs from float to double in StripCPEfromTrackAngle --- .../SiStripRecHitConverter/src/StripCPEfromTrackAngle.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/RecoLocalTracker/SiStripRecHitConverter/src/StripCPEfromTrackAngle.cc b/RecoLocalTracker/SiStripRecHitConverter/src/StripCPEfromTrackAngle.cc index c9f47d549a352..c1baa166720f0 100644 --- a/RecoLocalTracker/SiStripRecHitConverter/src/StripCPEfromTrackAngle.cc +++ b/RecoLocalTracker/SiStripRecHitConverter/src/StripCPEfromTrackAngle.cc @@ -13,7 +13,7 @@ StripCPEfromTrackAngle::StripCPEfromTrackAngle(edm::ParameterSet& conf, const SiStripLatency& latency) : StripCPE(conf, mag, geom, lorentz, backPlaneCorrection, confObj, latency), useLegacyError(conf.existsAs("useLegacyError") ? conf.getParameter("useLegacyError") : true), - maxChgOneMIP(conf.existsAs("maxChgOneMIP") ? conf.getParameter("maxChgOneMIP") : -6000.), + maxChgOneMIP(conf.existsAs("maxChgOneMIP") ? conf.getParameter("maxChgOneMIP") : -6000.), m_algo(useLegacyError ? Algo::legacy : (maxChgOneMIP < 0 ? Algo::mergeCK : Algo::chargeCK)) { mLC_P[0] = conf.existsAs("mLC_P0") ? conf.getParameter("mLC_P0") : -.326; mLC_P[1] = conf.existsAs("mLC_P1") ? conf.getParameter("mLC_P1") : .618; From c97cd2912d52b8d16e5146fde8b298090dcc27df Mon Sep 17 00:00:00 2001 From: Felice Pantaleo Date: Mon, 16 Sep 2024 14:55:02 +0200 Subject: [PATCH 3/3] format --- FWCore/ParameterSet/src/Entry.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/FWCore/ParameterSet/src/Entry.cc b/FWCore/ParameterSet/src/Entry.cc index e882221a5e2ed..f9e08a675101f 100644 --- a/FWCore/ParameterSet/src/Entry.cc +++ b/FWCore/ParameterSet/src/Entry.cc @@ -442,7 +442,7 @@ namespace edm { break; } } // switch(type) - } // Entry::validate() + } // Entry::validate() // ---------------------------------------------------------------------- // constructors