Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DD4hep] DD Filtered View: Add Debug Print, RegExp Speed Up and Clean Up #29208

Merged
merged 8 commits into from
Mar 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DetectorDescription/DDCMS/interface/DDCompactView.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace cms {
const cms::DDDetector* detector() const { return &m_det; }
DDSpecParRegistry const& specpars() const { return m_det.specpars(); }
template <typename T>
std::vector<T> getVector(std::string_view) const;
std::vector<T> getVector(const std::string&) const;

private:
const cms::DDDetector& m_det;
Expand Down
17 changes: 13 additions & 4 deletions DetectorDescription/DDCMS/interface/DDFilteredView.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,24 @@ namespace cms {
using Node = TGeoNode;
using Translation = ROOT::Math::DisplacementVector3D<ROOT::Math::Cartesian3D<double>>;
using RotationMatrix = ROOT::Math::Rotation3D;
using DDFilter = std::string_view;

struct DDFilter {
DDFilter(const std::string& attribute = "", const std::string& value = "")
: m_attribute(attribute), m_value(value) {}
const std::string& attribute() const { return m_attribute; }
const std::string& value() const { return m_value; }

private:
const std::string m_attribute;
const std::string m_value;
};

class DDFilteredView {
public:
using nav_type = std::vector<int>;

DDFilteredView(const DDDetector*, const Volume);
DDFilteredView(const DDCompactView&, const DDFilter& = "");
DDFilteredView(const DDCompactView&, const cms::DDFilter&);
DDFilteredView() = delete;

//! The numbering history of the current node
Expand Down Expand Up @@ -87,6 +97,7 @@ namespace cms {

//! User specific data
void mergedSpecifics(DDSpecParRefs const&);
const cms::DDSpecParRefs specpars() const { return refs_; }

//! set the current node to the first child
bool firstChild();
Expand Down Expand Up @@ -176,8 +187,6 @@ namespace cms {

private:
bool accept(std::string_view);
bool addPath(Node* const);
bool addNode(Node* const);
const TClass* getShape() const;

//! set the current node to the first sibling
Expand Down
3 changes: 1 addition & 2 deletions DetectorDescription/DDCMS/interface/DDSpecParRegistry.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ namespace cms {
using DDSpecParRefs = std::vector<const DDSpecPar*>;

struct DDSpecParRegistry {
void filter(DDSpecParRefs&, std::string_view, std::string_view) const;
void filter(DDSpecParRefs&, std::string_view) const;
void filter(DDSpecParRefs&, const std::string&, const std::string& = "") const;
std::vector<std::string_view> names() const;
std::vector<std::string_view> names(const std::string& path) const;
bool hasSpecPar(std::string_view) const;
Expand Down
7 changes: 5 additions & 2 deletions DetectorDescription/DDCMS/interface/Filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,25 @@
//
#include <memory>
#include <vector>
#include <regex>

namespace cms {
struct DDSpecPar;

struct Filter {
std::vector<std::string_view> keys;
std::vector<std::regex> keys;
std::unique_ptr<Filter> next;
struct Filter* up;
const DDSpecPar* spec = nullptr;
};

namespace dd {
bool accepted(std::vector<std::string_view> const&, std::string_view);
bool accepted(std::vector<std::regex> const&, std::string_view);
int contains(std::string_view, std::string_view);
bool isRegex(std::string_view);
bool isMatch(std::string_view, std::string_view);
bool compareEqual(std::string_view, std::string_view);
bool compareEqual(std::string_view, std::regex);
std::string_view realTopName(std::string_view);
std::vector<std::string_view> split(std::string_view, const char*);
std::string_view noNamespace(std::string_view);
Expand Down
27 changes: 15 additions & 12 deletions DetectorDescription/DDCMS/src/DDCompactView.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,32 @@
#include <cmath>

template <>
std::vector<int> cms::DDCompactView::getVector<int>(std::string_view key) const {
cms::DDVectorsMap vmap = this->detector()->vectors();
std::vector<int> temp;
std::vector<int> cms::DDCompactView::getVector<int>(const std::string& key) const {
const cms::DDVectorsMap& vmap = this->detector()->vectors();
std::vector<int> result;
for (auto const& it : vmap) {
if (cms::dd::compareEqual(cms::dd::noNamespace(it.first), key)) {
if (cms::dd::noNamespace(it.first) == key) {
for (const auto& i : it.second) {
temp.emplace_back(std::round(i));
result.emplace_back(std::round(i));
}
break;
}
}
return temp;
return result;
}

template <>
std::vector<double> cms::DDCompactView::getVector<double>(std::string_view key) const {
cms::DDVectorsMap vmap = this->detector()->vectors();
std::vector<double> temp;
std::vector<double> cms::DDCompactView::getVector<double>(const std::string& key) const {
const cms::DDVectorsMap& vmap = this->detector()->vectors();
std::vector<double> result;

for (auto const& it : vmap) {
if (cms::dd::compareEqual(cms::dd::noNamespace(it.first), key)) {
if (cms::dd::noNamespace(it.first) == key) {
for (const auto& i : it.second) {
temp.emplace_back(i);
result.emplace_back(i);
}
break;
}
}
return temp;
return result;
}
90 changes: 31 additions & 59 deletions DetectorDescription/DDCMS/src/DDFilteredView.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,13 @@ DDFilteredView::DDFilteredView(const DDDetector* det, const Volume volume) : reg
it_.emplace_back(Iterator(volume));
}

DDFilteredView::DDFilteredView(const DDCompactView& cpv, const DDFilter& attribute) : registry_(&cpv.specpars()) {
DDFilteredView::DDFilteredView(const DDCompactView& cpv, const cms::DDFilter& filter) : registry_(&cpv.specpars()) {
it_.emplace_back(Iterator(cpv.detector()->worldVolume()));
registry_->filter(refs_, attribute);
registry_->filter(refs_, filter.attribute(), filter.value());
mergedSpecifics(refs_);
LogVerbatim("Geometry").log([&](auto& log) {
log << "Filtered DD SpecPar Registry size: " << refs_.size() << "\n";
log << "Filtered by an attribute " << filter.attribute() << "==" << filter.value()
<< " DD SpecPar Registry size: " << refs_.size() << "\n";
for (const auto& t : refs_) {
log << "\nRegExps { ";
for (const auto& ki : t->paths)
Expand Down Expand Up @@ -143,31 +144,40 @@ void DDFilteredView::rot(dd4hep::Rotation3D& matrixOut) const {
}

void DDFilteredView::mergedSpecifics(DDSpecParRefs const& specs) {
if (!filters_.empty()) {
filters_.clear();
filters_.shrink_to_fit();
}
for (const auto& i : specs) {
for (const auto& j : i->paths) {
vector<string_view> toks = split(j, "/");
auto const& filter = find_if(begin(filters_), end(filters_), [&](auto const& f) {
auto const& k = find(begin(f->keys), end(f->keys), toks.front());
auto const& k = find_if(begin(f->keys), end(f->keys), [&](auto const& p) {
return std::regex_match(std::string({toks.front().data(), toks.front().size()}), p);
});
if (k != end(f->keys)) {
currentFilter_ = f.get();
return true;
}
return false;
});
if (filter == end(filters_)) {
filters_.emplace_back(unique_ptr<Filter>(new Filter{{toks.front()}, nullptr, nullptr, i}));
currentFilter_ = filters_.back().get();
filters_.emplace_back(unique_ptr<Filter>(
new Filter{{std::regex(std::string(toks.front().data(), toks.front().size()))}, nullptr, nullptr, i}));
}
// all next levels
for (size_t pos = 1; pos < toks.size(); ++pos) {
if (currentFilter_->next != nullptr) {
currentFilter_ = currentFilter_->next.get();
auto const& l = find(begin(currentFilter_->keys), end(currentFilter_->keys), toks[pos]);
auto const& l = find_if(begin(currentFilter_->keys), end(currentFilter_->keys), [&](auto const& p) {
return std::regex_match(std::string({toks.front().data(), toks.front().size()}), p);
});
if (l == end(currentFilter_->keys)) {
currentFilter_->keys.emplace_back(toks[pos]);
currentFilter_->keys.emplace_back(std::string(toks[pos].data(), toks[pos].size()));
}
} else {
currentFilter_->next.reset(new Filter{{toks[pos]}, nullptr, currentFilter_, i});
currentFilter_->next.reset(
new Filter{{std::regex(std::string(toks[pos].data(), toks[pos].size()))}, nullptr, currentFilter_, i});
}
}
}
Expand Down Expand Up @@ -341,7 +351,7 @@ template <>
std::string_view DDFilteredView::get<string_view>(const string& key) const {
std::string_view result;
DDSpecParRefs refs;
registry_->filter(refs, key);
registry_->filter(refs, key, "");
int level = it_.back().GetLevel();
for_each(begin(refs), end(refs), [&](auto const& i) {
auto k = find_if(begin(i->paths), end(i->paths), [&](auto const& j) {
Expand Down Expand Up @@ -383,6 +393,14 @@ std::vector<double> DDFilteredView::get<std::vector<double>>(const string& name,
return std::vector<double>();
}

template <>
std::vector<int> DDFilteredView::get<std::vector<int>>(const string& name, const string& key) const {
if (registry_->hasSpecPar(name))
return registry_->specPar(name)->value<std::vector<int>>(key);
else
return std::vector<int>();
}

template <>
std::vector<std::string> DDFilteredView::get<std::vector<std::string>>(const string& name, const string& key) const {
if (registry_->hasSpecPar(name))
Expand Down Expand Up @@ -415,33 +433,6 @@ DDFilteredView::nav_type DDFilteredView::navPos() const {
return pos;
}

bool DDFilteredView::addPath(Node* const node) {
assert(registry_);
node_ = node;
nodes_.tags.clear();
nodes_.offsets.clear();
nodes_.copyNos.clear();
bool result(false);

int level = it_.back().GetLevel();
for (int nit = level; nit > 0; --nit) {
for_each(begin(registry_->specpars), end(registry_->specpars), [&](auto const& i) {
auto k = find_if(begin(i.second.paths), end(i.second.paths), [&](auto const& j) {
return (compareEqual(noNamespace(it_.back().GetNode(nit)->GetVolume()->GetName()),
*begin(split(realTopName(j), "/"))) &&
(i.second.hasValue("CopyNoTag") || i.second.hasValue("CopyNoOffset")));
});
if (k != end(i.second.paths)) {
nodes_.tags.emplace_back(i.second.dblValue("CopyNoTag"));
nodes_.offsets.emplace_back(i.second.dblValue("CopyNoOffset"));
nodes_.copyNos.emplace_back(it_.back().GetNode(nit)->GetNumber());
result = true;
}
});
}
return result;
}

const ExpandedNodes& DDFilteredView::history() {
assert(registry_);
nodes_.tags.clear();
Expand All @@ -453,9 +444,9 @@ const ExpandedNodes& DDFilteredView::history() {
for (int nit = level; nit > 0; --nit) {
for_each(begin(registry_->specpars), end(registry_->specpars), [&](auto const& i) {
auto k = find_if(begin(i.second.paths), end(i.second.paths), [&](auto const& j) {
return (compareEqual(noNamespace(it_.back().GetNode(nit)->GetVolume()->GetName()),
*begin(split(realTopName(j), "/"))) &&
(i.second.hasValue("CopyNoTag") || i.second.hasValue("CopyNoOffset")));
return (
isMatch(noNamespace(it_.back().GetNode(nit)->GetVolume()->GetName()), *begin(split(realTopName(j), "/"))) &&
(i.second.hasValue("CopyNoTag") || i.second.hasValue("CopyNoOffset")));
});
if (k != end(i.second.paths)) {
nodes_.tags.emplace_back(i.second.dblValue("CopyNoTag"));
Expand All @@ -469,25 +460,6 @@ const ExpandedNodes& DDFilteredView::history() {
return nodes_;
}

bool DDFilteredView::addNode(Node* const node) {
assert(registry_);
node_ = node;
bool result(false);
for_each(begin(registry_->specpars), end(registry_->specpars), [&](auto const& i) {
auto k = find_if(begin(i.second.paths), end(i.second.paths), [&](auto const& j) {
return (compareEqual(node_->GetVolume()->GetName(), *begin(split(realTopName(j), "/"))) &&
(i.second.hasValue("CopyNoTag") || i.second.hasValue("CopyNoOffset")));
});
if (k != end(i.second.paths)) {
nodes_.tags.emplace_back(i.second.dblValue("CopyNoTag"));
nodes_.offsets.emplace_back(i.second.dblValue("CopyNoOffset"));
nodes_.copyNos.emplace_back(node_->GetNumber());
result = true;
}
});
return result;
}

const TClass* DDFilteredView::getShape() const {
assert(node_);
Volume currVol = node_->GetVolume();
Expand Down
49 changes: 28 additions & 21 deletions DetectorDescription/DDCMS/src/DDSpecparRegistry.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,25 @@ std::vector<double> DDSpecPar::value<std::vector<double>>(const string& key) con
return result;
}

template <>
std::vector<int> DDSpecPar::value<std::vector<int>>(const string& key) const {
std::vector<int> result;

auto const& nitem = numpars.find(key);
if (nitem != end(numpars)) {
return std::vector<int>(begin(nitem->second), end(nitem->second));
}

auto const& sitem = spars.find(key);
if (sitem != end(spars)) {
std::transform(begin(sitem->second), end(sitem->second), std::back_inserter(result), [](auto& i) -> int {
return dd4hep::_toInt(i);
});
}

return result;
}

template <>
std::vector<std::string> DDSpecPar::value<std::vector<std::string>>(const string& key) const {
std::vector<std::string> result;
Expand Down Expand Up @@ -76,32 +95,20 @@ double DDSpecPar::dblValue(const string& key) const {
return *begin(item->second);
}

void DDSpecParRegistry::filter(DDSpecParRefs& refs, string_view attribute, string_view value) const {
void DDSpecParRegistry::filter(DDSpecParRefs& refs, const std::string& attribute, const std::string& value) const {
bool found(false);
for_each(begin(specpars), end(specpars), [&refs, &attribute, &value, &found](auto& k) {
found = false;
for_each(begin(k.second.spars), end(k.second.spars), [&](const auto& l) {
if (l.first == attribute) {
for_each(begin(l.second), end(l.second), [&](const auto& m) {
if (m == value)
found = true;
});
}
});
if (found) {
k.second.name = k.first;
refs.emplace_back(&k.second);
}
});
}

void DDSpecParRegistry::filter(DDSpecParRefs& refs, string_view attribute) const {
bool found(false);
for_each(begin(specpars), end(specpars), [&refs, &attribute, &found](auto& k) {
found = false;
for_each(begin(k.second.spars), end(k.second.spars), [&](const auto& l) {
if (l.first == attribute) {
found = true;
if (value.empty()) {
found = true;
} else {
for_each(begin(l.second), end(l.second), [&](const auto& m) {
if (m == value)
found = true;
});
}
}
});
if (found) {
Expand Down
12 changes: 8 additions & 4 deletions DetectorDescription/DDCMS/src/Filter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ using namespace std;
namespace cms {
namespace dd {

bool compareEqual(string_view node, string_view name) {
bool isMatch(string_view node, string_view name) {
if (!isRegex(name)) {
return (name == node);
} else {
Expand All @@ -17,9 +17,13 @@ namespace cms {
}
}

bool accepted(vector<string_view> const& names, string_view node) {
return (find_if(begin(names), end(names), [&](const auto& n) -> bool { return compareEqual(node, n); }) !=
end(names));
bool compareEqual(string_view node, string_view name) { return (name == node); }

bool compareEqual(string_view node, regex pattern) { return regex_match(begin(node), end(node), pattern); }

bool accepted(vector<std::regex> const& keys, string_view node) {
return (find_if(begin(keys), end(keys), [&](const auto& n) -> bool { return compareEqual(node, n); }) !=
end(keys));
}

int contains(string_view input, string_view needle) {
Expand Down
Loading