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

Improve lookup speed of GeoHandler (issue #1291) #1298

Merged
merged 3 commits into from
Jul 22, 2024
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
6 changes: 6 additions & 0 deletions DDCore/include/DD4hep/GeoHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,12 @@ namespace dd4hep {

protected:
bool m_propagateRegions { false };

/// actual container with std::vector (preserves order)
std::map<int, std::vector<const TGeoNode*> >* m_data { nullptr };
/// redundant container with std::set (for lookup purpose)
std::map<int, std::set<const TGeoNode*> >* m_set_data { nullptr };

std::map<const TGeoNode*, std::vector<TGeoNode*> >* m_daughters { nullptr };
/// Internal helper to collect geometry information from traversal
GeoHandler& i_collect(const TGeoNode* parent,
Expand All @@ -109,6 +114,7 @@ namespace dd4hep {
GeoHandler();
/// Initializing constructor
GeoHandler(std::map<int, std::vector<const TGeoNode*> >* ptr,
std::map<int, std::set<const TGeoNode*> >* ptr_set,
std::map<const TGeoNode*, std::vector<TGeoNode*> >* daus = nullptr);
/// Default destructor
virtual ~GeoHandler();
Expand Down
24 changes: 20 additions & 4 deletions DDCore/src/GeoHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,25 +55,39 @@ namespace {
/// Default constructor
detail::GeoHandler::GeoHandler() {
m_data = new std::map<int, std::vector<const TGeoNode*> >();
m_set_data = new std::map<int, std::set<const TGeoNode*> >();
}

/// Initializing constructor
detail::GeoHandler::GeoHandler(std::map<int, std::vector<const TGeoNode*> >* ptr,
std::map<const TGeoNode*, std::vector<TGeoNode*> >* daus)
: m_data(ptr), m_daughters(daus)
std::map<int, std::set<const TGeoNode*> >* ptr_set,
std::map<const TGeoNode*, std::vector<TGeoNode*> >* daus)
: m_data(ptr), m_set_data(ptr_set), m_daughters(daus)
{
}

/// Default destructor
detail::GeoHandler::~GeoHandler() {
if (m_data)
delete m_data;
if (m_set_data)
delete m_set_data;

m_data = nullptr;
m_set_data = nullptr;
}

std::map<int, std::vector<const TGeoNode*> >* detail::GeoHandler::release() {
/// release the std::vector geometry container (preserves order)
std::map<int, std::vector<const TGeoNode*> >* d = m_data;
m_data = nullptr;

/// the std::set container (for lookup purpose) is not needed anymore, so delete it
/// the container is always present since the call of the constructor
/// we never expect to call release() twice (will release nullptr)
delete m_set_data;
andresailer marked this conversation as resolved.
Show resolved Hide resolved
m_set_data = nullptr;

return d;
}

Expand All @@ -88,13 +102,15 @@ detail::GeoHandler& detail::GeoHandler::collect(DetElement element) {
DetElement par = element.parent();
TGeoNode* par_node = par.isValid() ? par.placement().ptr() : nullptr;
m_data->clear();
m_set_data->clear();
return i_collect(par_node, element.placement().ptr(), 0, Region(), LimitSet());
}

detail::GeoHandler& detail::GeoHandler::collect(DetElement element, GeometryInfo& info) {
DetElement par = element.parent();
TGeoNode* par_node = par.isValid() ? par.placement().ptr() : nullptr;
m_data->clear();
m_set_data->clear();
i_collect(par_node, element.placement().ptr(), 0, Region(), LimitSet());
for ( auto i = m_data->rbegin(); i != m_data->rend(); ++i ) {
const auto& mapped = (*i).second;
Expand Down Expand Up @@ -150,8 +166,8 @@ detail::GeoHandler& detail::GeoHandler::i_collect(const TGeoNode* /* parent */,
}
}
/// Collect the hierarchy of placements
auto& vec = (*m_data)[level];
if(std::find(vec.begin(), vec.end(), current) == vec.end()) {
/// perform lookup using std::set::emplace (faster than std::find for very large number of volumes)
if ( (*m_set_data)[level].emplace(current).second ) {
(*m_data)[level].push_back(current);
}
int num = nodes ? nodes->GetEntriesFast() : 0;
Expand Down
1 change: 1 addition & 0 deletions DDG4/src/Geant4Converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1668,6 +1668,7 @@ Geant4Converter& Geant4Converter::create(DetElement top) {
World wrld = top.world();

m_data->clear();
m_set_data->clear();
m_daughters = &daughters;
geo.manager = &wrld.detectorDescription().manager();
this->collect(top, geo);
Expand Down
Loading