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 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions DetectorDescription/DDCMS/interface/DDFilteredView.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ namespace cms {

const std::vector<int> copyNumbers() { return copyNos(); }

//! Debug filter
void printFilter() const { printFilter(currentFilter_); };

//! The absolute translation of the current node
// Return value is Double_t translation[3] with x, y, z elements.
const Double_t* trans() const;
Expand Down Expand Up @@ -176,12 +179,11 @@ 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
bool firstSibling();
void printFilter(const Filter* filter) const;

ExpandedNodes nodes_;
std::vector<Iterator> it_;
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::pair<std::string_view, std::regex>> keys;
ianna marked this conversation as resolved.
Show resolved Hide resolved
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::pair<std::string_view, 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
83 changes: 28 additions & 55 deletions DetectorDescription/DDCMS/src/DDFilteredView.cc
Original file line number Diff line number Diff line change
Expand Up @@ -147,33 +147,52 @@ void DDFilteredView::mergedSpecifics(DDSpecParRefs const& 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 p.first == toks.front(); });
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::pair<std::string_view, std::regex>(
toks.front(), 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 p.first == toks[pos];
});
if (l == end(currentFilter_->keys)) {
currentFilter_->keys.emplace_back(toks[pos]);
currentFilter_->keys.emplace_back(toks[pos], 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::pair<std::string_view, std::regex>(
toks[pos], regex(std::string(toks[pos].data(), toks[pos].size())))},
nullptr,
currentFilter_,
i});
}
}
}
}
}

void DDFilteredView::printFilter(const Filter* filter) const {
if (filter != nullptr) {
for (auto it : filter->keys)
std::cout << "//" << std::string(it.first.data(), it.first.size()) << "\n";
if (filter->next != nullptr)
printFilter(filter->next.get());
}
}

bool DDFilteredView::firstChild() {
if (it_.empty()) {
LogVerbatim("DDFilteredView") << "Iterator vector has zero size.";
Expand Down Expand Up @@ -415,33 +434,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 +445,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 +461,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
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::pair<std::string_view, std::regex>> const& keys, string_view node) {
return (find_if(begin(keys), end(keys), [&](const auto& n) -> bool { return compareEqual(node, n.second); }) !=
end(keys));
}

int contains(string_view input, string_view needle) {
Expand Down
22 changes: 15 additions & 7 deletions DetectorDescription/DDCMS/test/Filter.cppunit.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,35 +44,43 @@ void testFilter::setUp() {
vector<string_view> toks = split(i, "/");
unique_ptr<Filter> f = nullptr;
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 p.first == toks.front(); });
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}));
filters_.emplace_back(unique_ptr<Filter>(
new Filter{{std::pair<std::string_view, std::regex>(
toks.front(), regex(std::string(toks.front().data(), toks.front().size())))},
nullptr,
nullptr}));
currentFilter = filters_.back().get();
}
// 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 p.first == toks[pos]; });
if (l == end(currentFilter->keys)) {
currentFilter->keys.emplace_back(toks[pos]);
currentFilter->keys.emplace_back(toks[pos], regex(std::string(toks.front().data(), toks.front().size())));
}
} else {
currentFilter->next.reset(new Filter{{toks[pos]}, nullptr, currentFilter});
currentFilter->next.reset(new Filter{
{std::pair<std::string_view, std::regex>(toks[pos], regex({toks[pos].data(), toks[pos].size()}))},
nullptr,
currentFilter});
}
}
}
}

void testFilter::checkFilter() {
string_view name = "MB2P.*"sv;
CPPUNIT_ASSERT(filters_.front()->keys.front() == name);
CPPUNIT_ASSERT(filters_.front()->keys.front().first == name);
CPPUNIT_ASSERT(filters_.front()->up == nullptr);
CPPUNIT_ASSERT(filters_.front()->next != nullptr);
CPPUNIT_ASSERT(filters_.size() == 1);
Expand All @@ -90,7 +98,7 @@ void testFilter::checkFilter() {

void testFilter::print(Filter* filter) {
for (auto const& it : filter->keys) {
cout << it << " ";
cout << it.first << " ";
}
cout << "\n";
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,13 @@ void DTGeometryBuilder::buildGeometry(DDFilteredView& fview, DTGeometry& geom, c

// Loop on SLs
bool doSL = fview.nextSibling();

while (doSL) {
DTSuperLayer* sl = buildSuperLayer(fview, chamber, num);

// Loop on Layers
fview.down();

bool doLayers = fview.sibling();
while (doLayers) {
DTLayer* l = buildLayer(fview, sl, num);
Expand Down
5 changes: 4 additions & 1 deletion Geometry/DTGeometryBuilder/test/python/testDTGeometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
process.MessageLogger = cms.Service(
"MessageLogger",
statistics = cms.untracked.vstring('cout', 'dtGeometry'),
categories = cms.untracked.vstring('DTGeometryTest'),
categories = cms.untracked.vstring('DTGeometryTest', 'Geometry'),
cout = cms.untracked.PSet(
threshold = cms.untracked.string('WARNING'),
noLineBreaks = cms.untracked.bool(True)
Expand All @@ -31,6 +31,9 @@
),
threshold = cms.untracked.string('INFO'),
DTGeometryTest = cms.untracked.PSet(
limit = cms.untracked.int32(-1)
),
Geometry = cms.untracked.PSet(
limit = cms.untracked.int32(-1)
)
),
Expand Down