Skip to content

Commit 2ce5551

Browse files
Introduce vFloat and float types in ParameterSets
1 parent 1234e95 commit 2ce5551

18 files changed

+400
-29
lines changed

FWCore/ParameterSet/interface/Entry.h

+8
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,14 @@ namespace edm {
8383
Entry(std::string const& name, std::vector<double> const& val, bool is_tracked);
8484
std::vector<double> getVDouble() const;
8585

86+
// Float
87+
Entry(std::string const& name, float val, bool is_tracked);
88+
float getFloat() const;
89+
90+
// vFloat
91+
Entry(std::string const& name, std::vector<float> const& val, bool is_tracked);
92+
std::vector<float> getVFloat() const;
93+
8694
// String
8795
Entry(std::string const& name, std::string const& val, bool is_tracked);
8896
std::string getString() const;

FWCore/ParameterSet/interface/ParameterDescription.h

+4
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ namespace edm {
5858
ValueFormat format);
5959
void writeValue(std::ostream& os, int indentation, double const& value_, ValueFormat format);
6060
void writeValue(std::ostream& os, int indentation, std::vector<double> const& value_, ValueFormat format);
61+
void writeValue(std::ostream& os, int indentation, float const& value_, ValueFormat format);
62+
void writeValue(std::ostream& os, int indentation, std::vector<float> const& value_, ValueFormat format);
6163
void writeValue(std::ostream& os, int indentation, bool const& value_, ValueFormat format);
6264
void writeValue(std::ostream& os, int indentation, std::string const& value_, ValueFormat format);
6365
void writeValue(std::ostream& os, int indentation, std::vector<std::string> const& value_, ValueFormat format);
@@ -88,6 +90,8 @@ namespace edm {
8890
bool hasNestedContent(std::vector<unsigned long long> const& value);
8991
bool hasNestedContent(double const& value);
9092
bool hasNestedContent(std::vector<double> const& value);
93+
bool hasNestedContent(float const& value);
94+
bool hasNestedContent(std::vector<float> const& value);
9195
bool hasNestedContent(bool const& value);
9296
bool hasNestedContent(std::string const& value);
9397
bool hasNestedContent(std::vector<std::string> const& value);

FWCore/ParameterSet/interface/ParameterDescriptionNode.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,9 @@ namespace edm {
6565
k_EventRange = 'R',
6666
k_VEventRange = 'r',
6767
k_PSet = 'Q',
68-
k_VPSet = 'q'
68+
k_VPSet = 'q',
69+
k_float = 'H',
70+
k_vfloat = 'h'
6971
};
7072

7173
std::string parameterTypeEnumToString(ParameterTypes iType);

FWCore/ParameterSet/interface/ParameterSet.h

+50
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,15 @@ namespace edm {
395395
template <>
396396
std::vector<double> ParameterSet::getParameter<std::vector<double>>(std::string const& name) const;
397397

398+
// ----------------------------------------------------------------------
399+
// Float, vFloat
400+
401+
template <>
402+
float ParameterSet::getParameter<float>(std::string const& name) const;
403+
404+
template <>
405+
std::vector<float> ParameterSet::getParameter<std::vector<float>>(std::string const& name) const;
406+
398407
// ----------------------------------------------------------------------
399408
// String, vString
400409

@@ -618,6 +627,22 @@ namespace edm {
618627
template <>
619628
std::vector<double> ParameterSet::getUntrackedParameter<std::vector<double>>(std::string const& name) const;
620629

630+
// ----------------------------------------------------------------------
631+
// Float, vFloat
632+
633+
template <>
634+
float ParameterSet::getUntrackedParameter<float>(std::string const& name, float const& defaultValue) const;
635+
636+
template <>
637+
float ParameterSet::getUntrackedParameter<float>(std::string const& name) const;
638+
639+
template <>
640+
std::vector<float> ParameterSet::getUntrackedParameter<std::vector<float>>(
641+
std::string const& name, std::vector<float> const& defaultValue) const;
642+
643+
template <>
644+
std::vector<float> ParameterSet::getUntrackedParameter<std::vector<float>>(std::string const& name) const;
645+
621646
// ----------------------------------------------------------------------
622647
// String, vString
623648

@@ -778,6 +803,15 @@ namespace edm {
778803
template <>
779804
std::vector<double> ParameterSet::getParameter<std::vector<double>>(char const* name) const;
780805

806+
// ----------------------------------------------------------------------
807+
// Float, vFloat
808+
809+
template <>
810+
float ParameterSet::getParameter<float>(char const* name) const;
811+
812+
template <>
813+
std::vector<float> ParameterSet::getParameter<std::vector<float>>(char const* name) const;
814+
781815
// ----------------------------------------------------------------------
782816
// String, vString
783817

@@ -969,6 +1003,22 @@ namespace edm {
9691003
template <>
9701004
std::vector<double> ParameterSet::getUntrackedParameter<std::vector<double>>(char const* name) const;
9711005

1006+
// ----------------------------------------------------------------------
1007+
// Float, vFloat
1008+
1009+
template <>
1010+
float ParameterSet::getUntrackedParameter<float>(char const* name, float const& defaultValue) const;
1011+
1012+
template <>
1013+
float ParameterSet::getUntrackedParameter<float>(char const* name) const;
1014+
1015+
template <>
1016+
std::vector<float> ParameterSet::getUntrackedParameter<std::vector<float>>(
1017+
char const* name, std::vector<float> const& defaultValue) const;
1018+
1019+
template <>
1020+
std::vector<float> ParameterSet::getUntrackedParameter<std::vector<float>>(char const* name) const;
1021+
9721022
// ----------------------------------------------------------------------
9731023
// String, vString
9741024

FWCore/ParameterSet/interface/types.h

+8
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ namespace edm {
7272
bool decode(std::vector<double>&, std::string_view);
7373
bool encode(std::string&, std::vector<double> const&);
7474

75+
// Float
76+
bool decode(float&, std::string_view);
77+
bool encode(std::string&, float);
78+
79+
// vFloat
80+
bool decode(std::vector<float>&, std::string_view);
81+
bool encode(std::string&, std::vector<float> const&);
82+
7583
// String
7684
bool decode(std::string&, std::string_view);
7785
bool encode(std::string&, std::string const&);

FWCore/ParameterSet/python/Config.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from .Options import Options
88
options = Options()
99

10-
1110
## imports
1211
import sys
1312
from typing import Union
@@ -2141,6 +2140,10 @@ def addDouble(self,tracked,label,value):
21412140
self.__insertValue(tracked,label,value)
21422141
def addVDouble(self,tracked,label,value):
21432142
self.__insertValue(tracked,label,value)
2143+
def addFloat(self,tracked,label,value):
2144+
self.__insertValue(tracked,label,value)
2145+
def addVFloat(self,tracked,label,value):
2146+
self.__insertValue(tracked,label,value)
21442147
def addBool(self,tracked,label,value):
21452148
self.__insertValue(tracked,label,value)
21462149
def addString(self,tracked,label,value):

FWCore/ParameterSet/python/DummyCfis.py

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
cms.string.dummyDefault="__default__"
1212
cms.double.dummyDefault = 9999
1313
cms.vdouble.dummyDefault = []
14+
cms.float.dummyDefault = 9999
15+
cms.vfloat.dummyDefault = []
1416
cms.vint32.dummyDefault = []
1517
cms.vuint32.dummyDefault = []
1618
cms.vint64.dummyDefault = []

FWCore/ParameterSet/python/Types.py

+64-21
Original file line numberDiff line numberDiff line change
@@ -323,13 +323,13 @@ def __nonzero__(self) -> bool:
323323
class double(_SimpleParameterTypeBase):
324324
@staticmethod
325325
def _isValid(value) -> bool:
326-
return isinstance(value, (int, long, float))
326+
return isinstance(value, (int, long, builtins.float))
327327
@staticmethod
328328
def _valueFromString(value:str):
329329
"""only used for cfg-parsing"""
330-
return double(float(value))
330+
return double(builtins.float(value))
331331
def insertInto(self, parameterSet, myname:str):
332-
parameterSet.addDouble(self.isTracked(), myname, float(self.value()))
332+
parameterSet.addDouble(self.isTracked(), myname, builtins.float(self.value()))
333333
def __nonzero__(self) -> bool:
334334
return self.value()!=0.
335335
def configValue(self, options:PrintOptions=PrintOptions()) -> str:
@@ -345,7 +345,30 @@ def _pythonValue(value) -> str:
345345
return "float('nan')"
346346
return str(value)
347347

348-
348+
class float(_SimpleParameterTypeBase):
349+
@staticmethod
350+
def _isValid(value) -> bool:
351+
return isinstance(value, (int, long, builtins.float))
352+
@staticmethod
353+
def _valueFromString(value:str):
354+
"""only used for cfg-parsing"""
355+
return float(builtins.float(value))
356+
def insertInto(self, parameterSet, myname:str):
357+
parameterSet.addFloat(self.isTracked(), myname, builtins.float(self.value()))
358+
def __nonzero__(self) -> bool:
359+
return self.value()!=0.
360+
def configValue(self, options:PrintOptions=PrintOptions()) -> str:
361+
return float._pythonValue(self._value)
362+
@staticmethod
363+
def _pythonValue(value) -> str:
364+
if math.isinf(value):
365+
if value > 0:
366+
return "float('inf')"
367+
else:
368+
return "-float('inf')"
369+
if math.isnan(value):
370+
return "float('nan')"
371+
return str(value)
349372

350373
class bool(_SimpleParameterTypeBase):
351374
@staticmethod
@@ -1010,7 +1033,6 @@ def _place(self,name:str,proc):
10101033
proc._placePSet(name,self)
10111034
def __str__(self) -> str:
10121035
return object.__str__(self)
1013-
10141036
class PSet(_ParameterTypeBase,_Parameterizable,_ConfigureComponent,_Labelable):
10151037
def __init__(self,*arg,**args):
10161038
#need to call the inits separately
@@ -1145,7 +1167,19 @@ def insertInto(self, parameterSet, myname:str):
11451167
def pythonValueForItem(self,item, options) -> str:
11461168
return double._pythonValue(item)
11471169

1148-
1170+
class vfloat(_ValidatingParameterListBase):
1171+
def __init__(self,*arg,**args):
1172+
super(vfloat,self).__init__(*arg,**args)
1173+
@classmethod
1174+
def _itemIsValid(cls,item) -> builtins.bool:
1175+
return float._isValid(item)
1176+
@staticmethod
1177+
def _valueFromString(value:str):
1178+
return vfloat(*_ValidatingParameterListBase._itemsFromStrings(value,float._valueFromString))
1179+
def insertInto(self, parameterSet, myname:str):
1180+
parameterSet.addVFloat(self.isTracked(), myname, self.value())
1181+
def pythonValueForItem(self,item, options) -> str:
1182+
return float._pythonValue(item)
11491183

11501184

11511185
class vbool(_ValidatingParameterListBase):
@@ -1427,6 +1461,10 @@ def addDouble(self,tracked:bool,label:str,value):
14271461
v = double(value)
14281462
v.setIsTracked(tracked)
14291463
setattr(self.pset,label,v)
1464+
def addFloat(self,tracked:bool,label:str,value):
1465+
v = float(value)
1466+
v.setIsTracked(tracked)
1467+
setattr(self.pset,label,v)
14301468
def addString(self,tracked:bool,label:str,value):
14311469
v = string(value)
14321470
v.setIsTracked(tracked)
@@ -1479,6 +1517,11 @@ def addVDouble(self,tracked:bool,label:str,value):
14791517
v = vdouble(value)
14801518
v.setIsTracked(tracked)
14811519
setattr(self.pset,label,v)
1520+
1521+
def addVFloat(self,tracked:bool,label:str,value):
1522+
v = vfloat(value)
1523+
v.setIsTracked(tracked)
1524+
setattr(self.pset,label,v)
14821525
def addVString(self,tracked:bool,label:str,value):
14831526
v = vstring(value)
14841527
v.setIsTracked(tracked)
@@ -1668,26 +1711,26 @@ def testdouble(self):
16681711
d = double(1)
16691712
self.assertEqual(d.value(),1)
16701713
self.assertEqual(d.pythonValue(),'1')
1671-
d = double(float('Inf'))
1672-
self.assertEqual(d,float('Inf'))
1714+
d = double(builtins.float('Inf'))
1715+
self.assertEqual(d,builtins.float('Inf'))
16731716
self.assertEqual(d.pythonValue(),"float('inf')")
1674-
d = double(-float('Inf'))
1675-
self.assertEqual(d,-float('Inf'))
1717+
d = double(-builtins.float('Inf'))
1718+
self.assertEqual(d,-builtins.float('Inf'))
16761719
self.assertEqual(d.pythonValue(),"-float('inf')")
1677-
d = double(float('Nan'))
1720+
d = double(builtins.float('Nan'))
16781721
self.assertTrue(math.isnan(d.value()))
16791722
self.assertEqual(d.pythonValue(),"float('nan')")
16801723
def testvdouble(self):
16811724
d = vdouble(1)
16821725
self.assertEqual(d.value(),[1])
16831726
self.assertEqual(d.dumpPython(),'cms.vdouble(1)')
1684-
d = vdouble(float('inf'))
1685-
self.assertEqual(d,[float('inf')])
1727+
d = vdouble(builtins.float('inf'))
1728+
self.assertEqual(d,[builtins.float('inf')])
16861729
self.assertEqual(d.dumpPython(),"cms.vdouble(float('inf'))")
1687-
d = vdouble(-float('Inf'))
1688-
self.assertEqual(d,[-float('inf')])
1730+
d = vdouble(-builtins.float('Inf'))
1731+
self.assertEqual(d,[-builtins.float('inf')])
16891732
self.assertEqual(d.dumpPython(),"cms.vdouble(-float('inf'))")
1690-
d = vdouble(float('nan'))
1733+
d = vdouble(builtins.float('nan'))
16911734
self.assertTrue(math.isnan(d[0]))
16921735
self.assertEqual(d.dumpPython(),"cms.vdouble(float('nan'))")
16931736
def testvint32(self):
@@ -2510,12 +2553,12 @@ def testincompatibletypes(self):
25102553
with self.assertRaises(TypeError):
25112554
3 < string("I am a string")
25122555
def testinfinity(self):
2513-
self.assertLess(1e99, double(float("inf")))
2514-
self.assertLess(double(1e99), float("inf"))
2515-
self.assertGreater(1e99, double(float("-inf")))
2516-
self.assertEqual(double(float("inf")), float("inf"))
2556+
self.assertLess(1e99, double(builtins.float("inf")))
2557+
self.assertLess(double(1e99), builtins.float("inf"))
2558+
self.assertGreater(1e99, double(builtins.float("-inf")))
2559+
self.assertEqual(double(builtins.float("inf")), builtins.float("inf"))
25172560
def testnan(self):
2518-
nan = double(float("nan"))
2561+
nan = double(builtins.float("nan"))
25192562
self.assertNotEqual(nan, nan)
25202563
self.assertFalse(nan > 3 or nan < 3 or nan == 3)
25212564

0 commit comments

Comments
 (0)