CMS 3D CMS Logo

DDPartSelection.cc
Go to the documentation of this file.
6 #include "boost/spirit/include/classic.hpp"
7 
8 namespace boost {
9  namespace spirit {
10  namespace classic {}
11  } // namespace spirit
12 } // namespace boost
13 using namespace boost::spirit::classic;
14 
18  int copyNo_;
20  bool isChild_;
21  std::vector<DDPartSelRegExpLevel>* p_;
22 
23  std::vector<DDPartSelRegExpLevel>* path(std::vector<DDPartSelRegExpLevel>* p = nullptr) {
24  if (p) {
25  p_ = p;
26  namespace_ = "";
27  name_ = "";
28  copyNo_ = 0;
29  isCopyNoValid_ = false;
30  isChild_ = false;
31  }
32  return p_;
33  }
34 };
35 
36 void noNameSpace(char const* /*first*/, char const* /*last*/) {
38 }
39 
40 /* Functor for the parser; it does not consume memory -
41  pointers are only used to store references to memory
42  managed elsewhere
43 */
45  DDSelLevelFtor() : c_(DDI::Singleton<DDSelLevelCollector>::instance()) {}
46 
47  // parser calls this whenever a selection has been parsed ( //ns:nm[cn], /nm, //ns:nm, .... )
48  void operator()(char const* /*first*/, char const* /*last*/) const {
49  if (c_.path()) {
50  if (c_.isCopyNoValid_ && c_.isChild_) {
51  c_.path()->emplace_back(DDPartSelRegExpLevel(c_.namespace_, c_.name_, c_.copyNo_, ddchildposp));
52  //edm::LogInfo("DDPartSelection") << namespace_ << name_ << copyNo_ << ' ' << ddchildposp << std::endl;
53  } else if (c_.isCopyNoValid_ && !c_.isChild_) {
54  c_.path()->emplace_back(DDPartSelRegExpLevel(c_.namespace_, c_.name_, c_.copyNo_, ddanyposp));
55  // edm::LogInfo("DDPartSelection") << namespace_ << name_ << copyNo_ << ' ' << ddanyposp << std::endl;
56  } else if (!c_.isCopyNoValid_ && c_.isChild_) {
57  c_.path()->emplace_back(DDPartSelRegExpLevel(c_.namespace_, c_.name_, c_.copyNo_, ddchildlogp));
58  // edm::LogInfo("DDPartSelection") << namespace_ << name_ << copyNo_ << ' ' << ddchildlogp << std::endl;
59  } else if (!c_.isCopyNoValid_ && !c_.isChild_) {
60  c_.path()->emplace_back(DDPartSelRegExpLevel(c_.namespace_, c_.name_, c_.copyNo_, ddanylogp));
61  // edm::LogInfo("DDPartSelection") << namespace_ << name_ << copyNo_ << ' ' << ddanylogp << std::endl;
62  }
63  c_.namespace_ = "";
64  c_.name_ = "";
65  c_.isCopyNoValid_ = false;
66  }
67  }
68 
70 };
71 
72 struct DDIsChildFtor {
73  void operator()(char const* first, char const* last) const {
75  if ((last - first) > 1)
76  sl.isChild_ = false;
77  if ((last - first) == 1)
78  sl.isChild_ = true;
79  //edm::LogInfo("DDPartSelection") << "DDIsChildFtor isChild=" << (last-first) << std::endl;
80  }
81 };
82 
84  void operator()(char const* first, char const* last) const {
86  sl.namespace_.assign(first, last);
87  // edm::LogInfo("DDPartSelection") << "DDNameSpaceFtor singletonname=" << DDI::Singleton<DDSelLevelCollector>::instance().namespace_ << std::endl;
88  }
89 
91 };
92 
93 struct DDNameFtor {
94  void operator()(char const* first, char const* last) const {
96  sl.name_.assign(first, last);
97  // edm::LogInfo("DDPartSelection") << "DDNameFtor singletonname=" << Singleton<DDSelLevelCollector>::instance().name_ << std::endl;
98  }
99 };
100 
101 struct DDCopyNoFtor {
102  void operator()(int i) const {
104  sl.copyNo_ = i;
105  sl.isCopyNoValid_ = true;
106  // edm::LogInfo("DDPartSelection") << "DDCopyNoFtor ns=" << i;
107  }
108 };
109 
111 struct SpecParParser : public grammar<SpecParParser> {
112  template <typename ScannerT>
113  struct definition {
114  definition(SpecParParser const& /*self*/) {
115  Selection //= FirstStep[selLevelFtor()]
116  //>> *SelectionStep[selLevelFtor()]
117  = +SelectionStep[selLevelFtor()];
118 
119  FirstStep = Descendant >> Part;
120 
121  Part = PartNameCopyNumber | PartName;
122 
123  PartNameCopyNumber = PartName >> CopyNumber;
124 
125  SelectionStep = NavigationalElement[isChildFtor()] >> Part;
126 
127  NavigationalElement = Descendant | Child;
128 
129  CopyNumber = ch_p('[') >> int_p[copyNoFtor()] >> ch_p(']');
130 
131  PartName = NameSpaceName | SimpleName[nameFtor()][&noNameSpace];
132 
133  SimpleName = +(alnum_p | ch_p('_') | ch_p('.') | ch_p('*'));
134 
135  NameSpaceName = SimpleName[nameSpaceFtor()] >> ':' >> SimpleName[nameFtor()];
136 
137  Descendant = ch_p('/') >> ch_p('/');
138 
139  Child = ch_p('/');
140  }
141 
142  rule<ScannerT> Selection, FirstStep, Part, SelectionStep, NavigationalElement, CopyNumber, PartName,
143  PartNameCopyNumber, NameSpaceName, SimpleName, Descendant, Child;
144 
145  rule<ScannerT> const& start() const { return Selection; }
146 
148 
150  static DDNameFtor f_;
151  return f_;
152  }
153 
155  static DDNameSpaceFtor f_;
156  return f_;
157  }
158 
160  static DDIsChildFtor f_;
161  return f_;
162  }
163 
165  static DDCopyNoFtor f_;
166  return f_;
167  }
168  };
169 };
170 
172  : lp_(lp), copyno_(c), selectionType_(t) {}
173 
174 void DDTokenize2(const std::string& sel, std::vector<DDPartSelRegExpLevel>& path) {
175  static SpecParParser parser;
177  bool result = parse(sel.c_str(), parser).full;
178  if (!result) {
179  edm::LogError("DDPartSelection") << "DDTokenize2() error in parsing of " << sel << std::endl;
180  }
181 }
182 
183 std::ostream& operator<<(std::ostream& o, const DDPartSelection& p) {
184  DDPartSelection::const_iterator it(p.begin()), ed(p.end());
185  for (; it != ed; ++it) {
186  const DDPartSelectionLevel lv = *it;
187  switch (lv.selectionType_) {
188  case ddanylogp:
189  o << "//" << lv.lp_.ddname();
190  break;
191  case ddanyposp:
192  o << "//" << lv.lp_.ddname() << '[' << lv.copyno_ << ']';
193  break;
194  case ddchildlogp:
195  o << "/" << lv.lp_.ddname();
196  break;
197  case ddchildposp:
198  o << "/" << lv.lp_.ddname() << '[' << lv.copyno_ << ']';
199  break;
200  default:
201  o << "{Syntax ERROR}";
202  }
203  }
204  return o;
205 }
206 
207 std::ostream& operator<<(std::ostream& os, const std::vector<DDPartSelection>& v) {
208  std::vector<DDPartSelection>::const_iterator it(v.begin()), ed(v.end());
209  for (; it != (ed - 1); ++it) {
210  os << *it << std::endl;
211  }
212  if (it != ed) {
213  ++it;
214  os << *it;
215  }
216  return os;
217 }
218 
219 // explicit template instantiation.
220 
221 template class DDI::Singleton<DDSelLevelFtor>;
222 //template class DDI::Singleton<DDI::Store<DDName, DDSelLevelCollector> >;
224 #include <DetectorDescription/Core/interface/Singleton.icc>
DDSelLevelFtor * selLevelFtor_
DDPartSelectionLevel(const DDLogicalPart &, int, ddselection_type)
std::ostream & operator<<(std::ostream &o, const DDPartSelection &p)
Definition: CLHEP.h:16
void operator()(char const *first, char const *last) const
static PFTauRenderPlugin instance
void operator()(int i) const
DDIsChildFtor & isChildFtor()
static value_type & instance()
void noNameSpace(char const *, char const *)
std::vector< DDPartSelRegExpLevel > * path(std::vector< DDPartSelRegExpLevel > *p=0)
ddselection_type
definition(SpecParParser const &)
Definition: GenABIO.cc:168
void operator()(char const *first, char const *last) const
A DDLogicalPart aggregates information concerning material, solid and sensitveness ...
Definition: DDLogicalPart.h:93
ddselection_type selectionType_
DDSelLevelCollector & c_
rule< ScannerT > const & start() const
def parse(path, config)
Definition: dumpparser.py:13
void DDTokenize2(const std::string &sel, std::vector< DDPartSelRegExpLevel > &path)
Templated helper class to allow a selection on a certain object collection.
void operator()(char const *first, char const *last) const
DDNameSpaceFtor & nameSpaceFtor()
void operator()(char const *, char const *) const
std::vector< DDPartSelRegExpLevel > * p_
DDSelLevelFtor & selLevelFtor()
const N & ddname() const
Definition: DDBase.h:61