File indexing completed on 2023-10-25 09:40:12
0001 #include "DetectorDescription/Core/interface/Specific.h"
0002
0003 #include <cassert>
0004 #include <ext/alloc_traits.h>
0005 #include <cstddef>
0006 #include <memory>
0007
0008 #include "DetectorDescription/Core/interface/DDBase.h"
0009 #include "DetectorDescription/Core/interface/DDCompactView.h"
0010 #include "DetectorDescription/Core/interface/DDCompactViewImpl.h"
0011 #include "DetectorDescription/Core/interface/DDExpandedView.h"
0012 #include "DetectorDescription/Core/interface/DDLogicalPart.h"
0013 #include "DetectorDescription/Core/interface/DDValue.h"
0014 #include "DetectorDescription/Core/interface/DDValuePair.h"
0015 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0016 #include "FWCore/Utilities/interface/Exception.h"
0017
0018 namespace DDI {
0019
0020 Specific::Specific(const std::vector<std::string>& selections, const DDsvalues_type& specs, bool doRegex)
0021 : specifics_(specs), partSelections_(0), valid_(false), doRegex_(doRegex) {
0022 for (const auto& it : selections) {
0023 createPartSelections(it);
0024 }
0025 }
0026
0027 Specific::Specific(const std::vector<DDPartSelection>& selections, const DDsvalues_type& specs)
0028 : specifics_(specs), partSelections_(selections), valid_(false), doRegex_(false) {}
0029
0030 void Specific::createPartSelections(const std::string& selString) {
0031 std::vector<DDPartSelRegExpLevel> regv;
0032 std::vector<DDPartSelection> temp;
0033 DDTokenize2(selString, regv);
0034
0035 if (regv.empty())
0036 throw cms::Exception("DDException") << "Could not evaluate the selection-std::string ->" << selString << "<-";
0037
0038 std::pair<bool, std::string> res;
0039 for (const auto& it : regv) {
0040 std::vector<DDLogicalPart> lpv;
0041 res = DDIsValid(it.ns_, it.nm_, lpv, doRegex_);
0042 if (!res.first) {
0043 std::string msg("Could not process q-name of a DDLogicalPart, reason:\n" + res.second);
0044 msg += "\nSpecPar selection is:\n" + selString + "\n";
0045
0046 edm::LogError("Specific") << msg;
0047 break;
0048 }
0049 addSelectionLevel(lpv, it.copyno_, it.selectionType_, temp);
0050 }
0051 if (res.first) {
0052 partSelections_.reserve(temp.size() + partSelections_.size());
0053 for (const auto& iit : temp) {
0054 partSelections_.emplace_back(iit);
0055 }
0056 }
0057 }
0058
0059 void Specific::addSelectionLevel(std::vector<DDLogicalPart>& lpv,
0060 int copyno,
0061 ddselection_type st,
0062 std::vector<DDPartSelection>& selv) {
0063 if (selv.empty()) {
0064 selv.emplace_back(DDPartSelection());
0065 }
0066 typedef std::vector<DDLogicalPart>::size_type lpv_sizetype;
0067 typedef std::vector<DDPartSelection>::size_type ps_sizetype;
0068 ps_sizetype ps_sz = selv.size();
0069 lpv_sizetype lpv_sz = lpv.size();
0070 lpv_sizetype lpv_i = 0;
0071 std::vector<DDPartSelection> result;
0072 for (; lpv_i < lpv_sz; ++lpv_i) {
0073 std::vector<DDPartSelection>::const_iterator ps_it = selv.begin();
0074 for (; ps_it != selv.end(); ++ps_it) {
0075 result.emplace_back(*ps_it);
0076 }
0077 }
0078 ps_sizetype ps_i = 0;
0079 for (lpv_i = 0; lpv_i < lpv_sz; ++lpv_i) {
0080 for (ps_i = ps_sz * lpv_i; ps_i < ps_sz * (lpv_i + 1); ++ps_i) {
0081 result[ps_i].emplace_back(DDPartSelectionLevel(lpv[lpv_i], copyno, st));
0082 }
0083 }
0084 selv = result;
0085 }
0086
0087 const std::vector<DDPartSelection>& Specific::selection() const { return partSelections_; }
0088
0089 void Specific::stream(std::ostream& os) const {
0090 os << " Size: " << specifics_.size() << std::endl;
0091 os << "\tSelections:" << std::endl;
0092 for (const auto& pit : partSelections_) {
0093 os << pit << std::endl;
0094 }
0095 for (const auto& vit : specifics_) {
0096 const DDValue& v = vit.second;
0097 os << "\tParameter name= \"" << v.name() << "\" " << std::endl;
0098 os << "\t\t Value pairs: " << std::endl;
0099 size_t s = v.size();
0100 size_t i = 0;
0101 if (v.isEvaluated()) {
0102 for (; i < s; ++i) {
0103 os << "\t\t\t\"" << v[i].first << "\""
0104 << ", " << v[i].second << std::endl;
0105 }
0106 } else {
0107 for (const auto& i : v.strings()) {
0108 os << "\t\t\t\"" << i << "\""
0109 << ", not evaluated" << std::endl;
0110 }
0111 }
0112 }
0113 }
0114
0115 void Specific::updateLogicalPart(
0116 std::vector<std::pair<DDLogicalPart, std::pair<const DDPartSelection*, const DDsvalues_type*> > >& result) const {
0117 if (!partSelections_.empty()) {
0118 const DDsvalues_type* sv = (&specifics_);
0119 for (const auto& it : partSelections_) {
0120 DDLogicalPart logp = it.back().lp_;
0121 const DDPartSelection* ps = (&it);
0122 assert(ps);
0123 assert(sv);
0124 std::pair<const DDPartSelection*, const DDsvalues_type*> pssv(ps, sv);
0125 result.emplace_back(std::make_pair(logp, pssv));
0126 }
0127 }
0128 }
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138 std::pair<bool, DDExpandedView> Specific::node() const {
0139 DDCompactView c;
0140 DDExpandedView e(c);
0141
0142 if (partSelections_.size() != 1) {
0143 edm::LogError("Specific") << " >> more or less than one part-selector, currently NOT SUPPORTED! <<" << std::endl;
0144 return std::make_pair(false, e);
0145 }
0146 const DDPartSelection& ps = partSelections_[0];
0147
0148 DDPartSelection::const_iterator it = ps.begin();
0149 DDPartSelection::const_iterator ed = ps.end();
0150 if ((it != ed) && (it->selectionType_ != ddanyposp)) {
0151 edm::LogError("Specific") << " >> part-selector must start with //Name[no] ! << " << std::endl;
0152 return std::make_pair(false, e);
0153 }
0154 ++it;
0155 for (; it != ps.end(); ++it) {
0156 if (it->selectionType_ != ddchildposp) {
0157 edm::LogError("Specific") << " >> part-selector must be a concatenation of direct children\n"
0158 << " including their copy-number only, CURRENT LIMITATION! <<" << std::endl;
0159 return std::make_pair(false, e);
0160 }
0161 }
0162
0163 it = ps.begin();
0164 bool result = true;
0165 for (; it != ed; ++it) {
0166 while (result) {
0167 if ((it->copyno_ == e.copyno()) && (it->lp_ == e.logicalPart())) {
0168 break;
0169 } else {
0170 result = e.nextSibling();
0171 }
0172 }
0173 if ((ed - it) > 1) {
0174 result = e.firstChild();
0175 }
0176 }
0177 return std::make_pair(result, e);
0178 }
0179 }