CMS 3D CMS Logo

DDLVector.cc
Go to the documentation of this file.
2 
3 #include <cstddef>
4 #include <map>
5 #include <memory>
6 #include <utility>
7 
13 
14 class DDCompactView;
15 
16 namespace {
17  template <typename F>
18  void parse(char const* str, F&& f) {
19  auto ptr = str;
20 
21  while (*ptr != 0) {
22  //remove any leading spaces
23  while (std::isspace(*ptr)) {
24  ++ptr;
25  }
26  char const* strt = ptr;
27 
28  //find either the end of the char array
29  // or a comma. Spaces are allowed
30  // between characters.
31  while ((*ptr != 0) and (*ptr != ',')) {
32  ++ptr;
33  }
34  char const* end = ptr;
35  if (*ptr == ',') {
36  ++ptr;
37  }
38 
39  if (strt == end) {
40  break;
41  }
42 
43  //strip off any ending spaces
44  while (strt != end - 1 and std::isspace(*(end - 1))) {
45  --end;
46  }
47  f(strt, end);
48  }
49  }
50 } // namespace
51 
52 bool DDLVector::parse_numbers(char const* str) {
53  parse(str, [this](char const* st, char const* end) { do_makeDouble(st, end); });
54  return true;
55 }
56 
57 bool DDLVector::parse_strings(char const* str) {
58  parse(str, [this](char const* st, char const* end) { do_makeString(st, end); });
59  return true;
60 }
61 
63 
65  pVector.clear();
66  pStrVector.clear();
67  pNameSpace = nmspace;
68 }
69 
72  bool isNumVec((atts.find("type") == atts.end() || atts.find("type")->second == "numeric") ? true : false);
73  bool isStringVec((!isNumVec && atts.find("type") != atts.end() && atts.find("type")->second == "string") ? true
74  : false);
75  std::string tTextToParse = getText();
76 
77  if (tTextToParse.empty()) {
78  errorOut(" EMPTY STRING ");
79  }
80 
81  if (isNumVec) {
82  if (!parse_numbers(tTextToParse.c_str())) {
83  errorOut(tTextToParse.c_str());
84  }
85  } else if (isStringVec) {
86  if (!parse_strings(tTextToParse.c_str())) {
87  errorOut(tTextToParse.c_str());
88  }
89  } else {
90  errorOut("Unexpected std::vector type. Only \"numeric\" and \"string\" are allowed.");
91  }
92 
93  if (parent() == "Algorithm" || parent() == "SpecPar") {
94  if (isNumVec) {
95  pVecMap[atts.find("name")->second] = pVector;
96  } else if (isStringVec) {
97  pStrVecMap[atts.find("name")->second] = pStrVector;
98  }
99  size_t expNEntries = 0;
100  if (atts.find("nEntries") != atts.end()) {
101  std::string nEntries = atts.find("nEntries")->second;
102  expNEntries = size_t(myRegistry_->evaluator().eval(pNameSpace, nEntries));
103  }
104  if ((isNumVec && pVector.size() != expNEntries) || (isStringVec && pStrVector.size() != expNEntries)) {
105  std::string msg("Number of entries found in Vector text does not match number in attribute nEntries.");
106  msg += "\n\tnEntries = " + atts.find("nEntries")->second;
107  msg += "\n------------------text---------\n";
108  msg += tTextToParse;
109  msg += "\n------------------text---------\n";
110  errorOut(msg.c_str());
111  }
112  } else if (parent() == "ConstantsSection" || parent() == "DDDefinition") {
113  if (atts.find("type") == atts.end() || atts.find("type")->second == "numeric") {
114  DDVector v(getDDName(nmspace), std::make_unique<std::vector<double>>(pVector));
115  } else {
116  DDStrVector v(getDDName(nmspace), std::make_unique<std::vector<std::string>>(pStrVector));
117  }
118  }
119  clear();
120 }
121 
123 
125 
126 void DDLVector::do_makeDouble(char const* str, char const* end) {
127  std::string ts(str, end);
128  double td = myRegistry_->evaluator().eval(pNameSpace, ts);
129  pVector.emplace_back(td);
130 }
131 
132 void DDLVector::do_makeString(char const* str, char const* end) {
133  std::string ts(str, end);
134  pStrVector.emplace_back(ts);
135 }
136 
137 void DDLVector::errorOut(const char* str) const {
138  std::string e("Failed to parse the following: \n");
139  e += std::string(str);
140  e += "\n as a Vector element (comma separated list).";
141  throwError(e);
142 }
143 
146  pVecMap.clear();
147  pStrVecMap.clear();
148 }
DDXMLElement::parent
const std::string & parent(void) const
access to parent element name
Definition: DDXMLElement.cc:201
DDXMLElement::clear
virtual void clear(void)
clear this element's contents.
Definition: DDXMLElement.cc:40
DDXMLElement::throwError
void throwError(const std::string &keyMessage) const
format std::string for throw an error.
Definition: DDXMLElement.cc:209
DDLVector::DDLVector
DDLVector(DDLElementRegistry *myreg)
Definition: DDLVector.cc:62
f
double f[11][100]
Definition: MuScleFitUtils.cc:78
DDVector
a named constant corresponding to the DDL-XML tag <Constant> and <ConstantsVector>
Definition: DDVector.h:18
DDLVector::pNameSpace
std::string pNameSpace
Definition: DDLVector.h:45
DDLVector::pStrVector
std::vector< std::string > pStrVector
Definition: DDLVector.h:42
ClhepEvaluator.h
DDLVector::processElement
void processElement(const std::string &name, const std::string &nmspace, DDCompactView &cpv) override
Processing the element.
Definition: DDLVector.cc:70
DDLVector::pVecMap
ReadMapType< std::vector< double > > pVecMap
Definition: DDLVector.h:43
DDLElementRegistry
The main class for processing parsed elements.
Definition: DDLElementRegistry.h:23
DDXMLElement::end
virtual std::vector< DDXMLAttribute >::const_iterator end(void)
Definition: DDXMLElement.cc:191
mps_check.msg
tuple msg
Definition: mps_check.py:285
DDLVector::clearall
void clearall()
Definition: DDLVector.cc:144
findQualityFiles.v
v
Definition: findQualityFiles.py:179
DDStrVector.h
DDXMLElement::getAttributeSet
virtual const DDXMLAttribute & getAttributeSet(size_t aIndex=0) const
Get a "row" of attributes, i.e. one attribute set.
Definition: DDXMLElement.cc:54
F
static uInt32 F(BLOWFISH_CTX *ctx, uInt32 x)
Definition: blowfish.cc:163
DDLElementRegistry::evaluator
ClhepEvaluator & evaluator()
Definition: DDLElementRegistry.h:42
DDStrVector
a named constant corresponding to the DDL-XML tag <Constant> and <ConstantsStrVector>
Definition: DDStrVector.h:18
DDLVector::errorOut
void errorOut(const char *str) const
Definition: DDLVector.cc:137
DDCompactView
Compact representation of the geometrical detector hierarchy.
Definition: DDCompactView.h:81
mps_fire.end
end
Definition: mps_fire.py:242
str
#define str(s)
Definition: TestProcessor.cc:51
DDXMLAttribute
std::map< std::string, std::string > DDXMLAttribute
Definition: DDXMLElement.h:45
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
dumpparser.parse
def parse(path, config)
Definition: dumpparser.py:13
DDXMLElement
This is a base class for processing XML elements in the DDD.
Definition: DDXMLElement.h:48
DDLVector::pVector
std::vector< double > pVector
Definition: DDLVector.h:41
DDLVector::do_makeDouble
void do_makeDouble(char const *str, char const *end)
Definition: DDLVector.cc:126
DDLVector::parse_strings
bool parse_strings(char const *str)
Definition: DDLVector.cc:57
DDLVector::do_makeString
void do_makeString(char const *str, char const *end)
Definition: DDLVector.cc:132
DDLVector.h
DDLElementRegistry.h
DDLVector::parse_numbers
bool parse_numbers(char const *str)
Definition: DDLVector.cc:52
DDXMLElement::getDDName
const virtual DDName getDDName(const std::string &defaultNS, const std::string &attname=std::string("name"), size_t aIndex=0)
Definition: DDXMLElement.cc:56
ReadMapType
a std::map<std::string,YourType> that offers a const operator[key]; if key is not stored in the std::...
Definition: DDReadMapType.h:14
DDLVector::pStrVecMap
ReadMapType< std::vector< std::string > > pStrVecMap
Definition: DDLVector.h:44
DDXMLElement::getText
const std::string getText(size_t tindex=0) const
retrieve the text blob.
Definition: DDXMLElement.cc:145
DDXMLElement.h
Skims_PA_cff.name
name
Definition: Skims_PA_cff.py:17
DDLVector::getMapOfVectors
ReadMapType< std::vector< double > > & getMapOfVectors()
Definition: DDLVector.cc:122
DDVector.h
DDXMLElement::myRegistry_
DDLElementRegistry * myRegistry_
Definition: DDXMLElement.h:173
DDLVector::preProcessElement
void preProcessElement(const std::string &name, const std::string &nmspace, DDCompactView &cpv) override
Called by loadAttributes AFTER attributes are loaded.
Definition: DDLVector.cc:64
ClhepEvaluator::eval
double eval(const std::string &ns, const std::string &expr)
Definition: ClhepEvaluator.cc:85
DDLVector::getMapOfStrVectors
ReadMapType< std::vector< std::string > > & getMapOfStrVectors()
Definition: DDLVector.cc:124
MillePedeFileConverter_cfg.e
e
Definition: MillePedeFileConverter_cfg.py:37