Skip to content

Commit

Permalink
Merge pull request #31662 from ianna/dd4hep-filtered-view-performance-v3
Browse files Browse the repository at this point in the history
[DD4hep] Filtered View Performance - Step 3
  • Loading branch information
cmsbuild committed Oct 6, 2020
2 parents 8d89aa3 + e90200c commit 13187ed
Show file tree
Hide file tree
Showing 78 changed files with 679 additions and 340 deletions.
5 changes: 5 additions & 0 deletions DetectorDescription/DDCMS/interface/DDCompactView.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ namespace cms {
template <typename T>
std::vector<T> getVector(const std::string&) const;

template <typename T>
T const& get(const std::string&) const;
template <typename T>
T const& get(const std::string&, const std::string&) const;

private:
const cms::DDDetector& m_det;
};
Expand Down
5 changes: 3 additions & 2 deletions DetectorDescription/DDCMS/interface/DDDetector.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef DetectorDescription_DDCMS_DDDetector_h
#define DetectorDescription_DDCMS_DDDetector_h

#include "DetectorDescription/DDCMS/interface/DDVectorRegistry.h"
#include <DD4hep/Detector.h>
#include <DD4hep/SpecParRegistry.h>
#include <string>
Expand All @@ -13,7 +14,7 @@ namespace cms {
explicit DDDetector(const std::string&, const std::string&, bool bigXML = false);
DDDetector() = delete;

dd4hep::VectorsMap const& vectors() const { return m_vectors; }
cms::DDVectorsMap const& vectors() const { return m_vectors; }

dd4hep::PartSelectionMap const& partsels() const { return m_partsels; }

Expand All @@ -38,7 +39,7 @@ namespace cms {
void processXML(const std::string&);

dd4hep::Detector* m_description = nullptr;
dd4hep::VectorsMap m_vectors;
cms::DDVectorsMap m_vectors;
dd4hep::PartSelectionMap m_partsels;
dd4hep::SpecParRegistry m_specpars;
const std::string m_tag;
Expand Down
50 changes: 48 additions & 2 deletions DetectorDescription/DDCMS/interface/DDFilteredView.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
//
#include "DetectorDescription/DDCMS/interface/DDSolidShapes.h"
#include "DetectorDescription/DDCMS/interface/ExpandedNodes.h"
#include "DetectorDescription/DDCMS/interface/DDVectorRegistry.h"
#include <DD4hep/Filter.h>
#include <DD4hep/SpecParRegistry.h>
#include <DD4hep/Volumes.h>
#include <memory>
#include <tuple>
#include <vector>

namespace cms {
Expand All @@ -50,7 +50,6 @@ namespace cms {
using DDSpecPar = dd4hep::SpecPar;
using DDSpecParRefs = dd4hep::SpecParRefs;
using DDSpecParRegistry = dd4hep::SpecParRegistry;
using DDVectorsMap = dd4hep::VectorsMap;
using Iterator = TGeoIterator;
using Node = TGeoNode;
using Translation = ROOT::Math::DisplacementVector3D<ROOT::Math::Cartesian3D<double>>;
Expand Down Expand Up @@ -193,6 +192,36 @@ namespace cms {
template <typename T>
T get(const std::string&);

//! extract attribute value for current Node
// keep the maespace for comparison
// assume there are no regular expressions
template <typename T>
std::vector<T> getValuesNS(const std::string& key) {
DDSpecParRefs refs;
registry_->filter(refs, key);

std::string path = this->path();
for (const auto& specPar : refs) {
for (const auto& part : specPar->paths) {
bool flag(true);
std::size_t from = 0;
for (auto name : dd4hep::dd::split(part, "/")) {
auto const& to = path.find(name, from);
if (to == std::string::npos) {
flag = false;
break;
} else {
from = to;
}
}
if (flag) {
return specPar->value<std::vector<T>>(key);
}
}
}
return std::vector<T>();
}

//! extract another value from the same SpecPar
// call get<double> first to find a relevant one
double getNextValue(const std::string&) const;
Expand Down Expand Up @@ -222,6 +251,20 @@ namespace cms {
//! print Filter paths and selections
void printFilter() const;

//! find a current Node SpecPar that has at least
// one of the attributes
template <class T, class... Ts>
void findSpecPar(T const& first, Ts const&... rest) {
currentSpecPar_ = find(first);
if constexpr (sizeof...(rest) > 0) {
// this line will only be instantiated if there are further
// arguments. if rest... is empty, there will be no call to
// findSpecPar(next).
if (currentSpecPar_ == nullptr)
findSpecPar(rest...);
}
}

private:
bool accept(std::string_view);
int nodeCopyNo(const std::string_view) const;
Expand All @@ -236,6 +279,9 @@ namespace cms {
// speeding up avoiding the same search over
// the registry
const DDSpecPar* find(const std::string&) const;
void filter(DDSpecParRefs&, const std::string&) const;
std::string_view front(const std::string_view) const;
std::string_view back(const std::string_view) const;

ExpandedNodes nodes_;
std::vector<Iterator> it_;
Expand Down
6 changes: 3 additions & 3 deletions DetectorDescription/DDCMS/interface/DDNamespace.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
#include "DD4hep/Objects.h"
#include "DD4hep/Shapes.h"
#include "DD4hep/Volumes.h"
#include "tbb/concurrent_unordered_map.h"
#include "tbb/concurrent_vector.h"
#include <unordered_map>
#include <vector>

namespace cms {

class DDParsingContext;
using DDVectorsMap = tbb::concurrent_unordered_map<std::string, tbb::concurrent_vector<double>>;
using DDVectorsMap = std::unordered_map<std::string, std::vector<double>>;

class DDNamespace {
public:
Expand Down
31 changes: 12 additions & 19 deletions DetectorDescription/DDCMS/interface/DDPlugins.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,30 @@

namespace dd4hep {

class SensitiveDetector;

template <typename T>
class DDCMSDetElementFactory : public dd4hep::PluginFactoryBase {
public:
static long create(dd4hep::Detector& detector,
cms::DDParsingContext& context,
dd4hep::xml::Handle_t element,
dd4hep::SensitiveDetector& sensitive);
static long create(dd4hep::Detector& detector, cms::DDParsingContext& context, dd4hep::xml::Handle_t element);
};
} // namespace dd4hep

namespace {
template <typename P, typename S>
class Factory;
DD4HEP_PLUGIN_FACTORY_ARGS_4(long, dd4hep::Detector*, cms::DDParsingContext*, ns::xml_h*, dd4hep::SensitiveDetector*) {
return dd4hep::DDCMSDetElementFactory<P>::create(*a0, *a1, *a2, *a3);
DD4HEP_PLUGIN_FACTORY_ARGS_3(long, dd4hep::Detector*, cms::DDParsingContext*, ns::xml_h*) {
return dd4hep::DDCMSDetElementFactory<P>::create(*a0, *a1, *a2);
}
} // namespace

#define DECLARE_DDCMS_DETELEMENT(name, func) \
DD4HEP_OPEN_PLUGIN(dd4hep, ddcms_det_element_##name) { \
typedef DDCMSDetElementFactory<ddcms_det_element_##name> _IMP; \
template <> \
long _IMP::create(dd4hep::Detector& d, cms::DDParsingContext& c, xml::Handle_t e, dd4hep::SensitiveDetector& h) { \
return func(d, c, e, h); \
} \
DD4HEP_PLUGINSVC_FACTORY(ddcms_det_element_##name, \
name, \
long(dd4hep::Detector*, cms::DDParsingContext*, ns::xml_h*, dd4hep::SensitiveDetector*), \
__LINE__) \
#define DECLARE_DDCMS_DETELEMENT(name, func) \
DD4HEP_OPEN_PLUGIN(dd4hep, ddcms_det_element_##name) { \
typedef DDCMSDetElementFactory<ddcms_det_element_##name> _IMP; \
template <> \
long _IMP::create(dd4hep::Detector& d, cms::DDParsingContext& c, xml::Handle_t e) { \
return func(d, c, e); \
} \
DD4HEP_PLUGINSVC_FACTORY( \
ddcms_det_element_##name, name, long(dd4hep::Detector*, cms::DDParsingContext*, ns::xml_h*), __LINE__) \
}

#endif
8 changes: 5 additions & 3 deletions DetectorDescription/DDCMS/interface/DDVectorRegistry.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
#define DETECTOR_DESCRIPTION_DD_VECTOR_REGISTRY_H

#include <string>
#include "tbb/concurrent_unordered_map.h"
#include "tbb/concurrent_vector.h"
#include <unordered_map>
#include <vector>

namespace cms {
using DDVectorsMap = std::unordered_map<std::string, std::vector<double>>;

struct DDVectorRegistry {
tbb::concurrent_unordered_map<std::string, tbb::concurrent_vector<double> > vectors;
DDVectorsMap vectors;
};
} // namespace cms

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,15 @@
#include "DetectorDescription/DDCMS/interface/DDDetector.h"
#include "DD4hep/Detector.h"

#include <unordered_map>
#include <vector>

using namespace std;
using namespace cms;
using namespace edm;

using DDVectorsMap = std::unordered_map<std::string, std::vector<double>>;

class DDVectorRegistryESProducer : public edm::ESProducer {
public:
DDVectorRegistryESProducer(const edm::ParameterSet&);
Expand Down Expand Up @@ -62,7 +67,7 @@ void DDVectorRegistryESProducer::fillDescriptions(edm::ConfigurationDescriptions

DDVectorRegistryESProducer::ReturnType DDVectorRegistryESProducer::produce(const DDVectorRegistryRcd& iRecord) {
LogDebug("Geometry") << "DDVectorRegistryESProducer::produce\n";
const dd4hep::VectorsMap& registry = iRecord.get(m_token).vectors();
const auto& registry = iRecord.get(m_token).vectors();

auto product = std::make_unique<DDVectorRegistry>();
product->vectors.insert(registry.begin(), registry.end());
Expand Down
2 changes: 1 addition & 1 deletion DetectorDescription/DDCMS/plugins/dd4hep/DDAngular.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace {
}
} // namespace

static long algorithm(Detector& /* description */, cms::DDParsingContext& ctxt, xml_h e, SensitiveDetector& /* sens */) {
static long algorithm(Detector& /* description */, cms::DDParsingContext& ctxt, xml_h e) {
cms::DDNamespace ns(ctxt, e, true);
DDAlgoArguments args(ctxt, e);

Expand Down
Loading

0 comments on commit 13187ed

Please sign in to comment.