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: Add access to vectors in SecPars from DDFilteredView #28108

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions DetectorDescription/DDCMS/data/cms-geometry-2021.xml
Expand Up @@ -10,6 +10,7 @@
<debug_namespaces/>
<debug_placements/>
<debug_algorithms/>
<debug_specpars/>
<debug_visattr/>
</debug>

Expand Down
4 changes: 4 additions & 0 deletions DetectorDescription/DDCMS/interface/DDFilteredView.h
Expand Up @@ -133,6 +133,10 @@ namespace cms {
template <typename T>
T get(const char*) const;

//! extract attribute value in SpecPar
template <typename T>
T get(const char*, const char*) const;

std::string_view getString(const std::string&) const;

//! return the stack of sibling numbers which indicates
Expand Down
6 changes: 6 additions & 0 deletions DetectorDescription/DDCMS/interface/DDSpecParRegistry.h
Expand Up @@ -16,6 +16,9 @@ namespace cms {
bool hasValue(const char* key) const;
double dblValue(const char*) const;

template <typename T>
T value(const char*) const;

DDPaths paths;
DDPartSelectionMap spars;
DDVectorsMap numpars;
Expand All @@ -27,6 +30,9 @@ namespace cms {
struct DDSpecParRegistry {
void filter(DDSpecParRefs&, std::string_view, std::string_view) const;
void filter(DDSpecParRefs&, std::string_view) const;
std::vector<std::string_view> names() const;
bool hasSpecPar(std::string_view) const;
const DDSpecPar* specPar(std::string_view) const;

DDSpecParMap specpars;
};
Expand Down
16 changes: 16 additions & 0 deletions DetectorDescription/DDCMS/src/DDFilteredView.cc
Expand Up @@ -328,6 +328,22 @@ double DDFilteredView::get<double>(const char* key) const {
return result;
}

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

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

std::string_view DDFilteredView::getString(const std::string& key) const {
assert(currentFilter_);
assert(currentFilter_->spec);
Expand Down
68 changes: 66 additions & 2 deletions DetectorDescription/DDCMS/src/DDSpecparRegistry.cc
@@ -1,8 +1,7 @@
#include "DetectorDescription/DDCMS/interface/DDSpecParRegistry.h"
#include "FWCore/MessageLogger/interface/MessageLogger.h"
#include "DD4hep/Detector.h"
#include <algorithm>
#include <iterator>
#include <regex>

using namespace std;
using namespace cms;
Expand All @@ -22,6 +21,46 @@ bool DDSpecPar::hasValue(const char* key) const {
return false;
}

template <>
std::vector<double> DDSpecPar::value<std::vector<double>>(const char* key) const {
std::vector<double> result;

auto const& nitem = numpars.find(key);
if (nitem != end(numpars)) {
return std::vector<double>(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) -> double {
return dd4hep::_toDouble(i);
});
}

return result;
}

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

auto const& nitem = numpars.find(key);
if (nitem != end(numpars)) {
std::transform(begin(nitem->second), end(nitem->second), std::back_inserter(result), [](auto& i) -> std::string {
return std::to_string(i);
});

return result;
}

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

return result;
}

double DDSpecPar::dblValue(const char* key) const {
auto const& item = numpars.find(key);
if (item == end(numpars))
Expand Down Expand Up @@ -61,3 +100,28 @@ void DDSpecParRegistry::filter(DDSpecParRefs& refs, string_view attribute) const
}
});
}

std::vector<std::string_view> DDSpecParRegistry::names() const {
std::vector<std::string_view> result;
for_each(begin(specpars), end(specpars), [&result](const auto& i) { result.emplace_back(i.first); });
return result;
}

bool DDSpecParRegistry::hasSpecPar(std::string_view name) const {
auto const& result =
find_if(begin(specpars), end(specpars), [&name](const auto& i) { return (i.first.compare(name) == 0); });
if (result != end(specpars))
return true;
else
return false;
}

const DDSpecPar* DDSpecParRegistry::specPar(std::string_view name) const {
auto const& result =
find_if(begin(specpars), end(specpars), [&name](const auto& i) { return (i.first.compare(name) == 0); });
if (result != end(specpars)) {
return &result->second;
} else {
return nullptr;
}
}
74 changes: 73 additions & 1 deletion DetectorDescription/DDCMS/test/DDFilteredView.cppunit.cc
Expand Up @@ -26,12 +26,41 @@ class testDDFilteredView : public CppUnit::TestFixture {

private:
string fileName_;
vector<double> refdattl_;
vector<string> refsattl_;
vector<double> refdLongFL_;
vector<string> refsLongFL_;
};

CPPUNIT_TEST_SUITE_REGISTRATION(testDDFilteredView);

void testDDFilteredView::setUp() {
fileName_ = edm::FileInPath("DetectorDescription/DDCMS/data/cms-2015-muon-geometry.xml").fullPath();
fileName_ = edm::FileInPath("DetectorDescription/DDCMS/data/cms-geometry-2021.xml").fullPath();
refdattl_ = {0.000809653, 0.000713002, 0.000654918, 0.000602767, 0.000566295, 0.000541647, 0.000516174, 0.000502512,
0.000504225, 0.000506212, 0.000506275, 0.000487621, 0.000473034, 0.000454002, 0.000442383, 0.000441043,
0.000443609, 0.000433124, 0.000440188, 0.000435257, 0.000439224, 0.000431385, 0.00041707, 0.000415677,
0.000408389, 0.000400293, 0.000400989, 0.000395417, 0.00038936, 0.000383942};
refsattl_ = {"0.8096535E-03/cm", "0.7130018E-03/cm", "0.6549183E-03/cm", "0.6027666E-03/cm", "0.5662951E-03/cm",
"0.5416475E-03/cm", "0.5161745E-03/cm", "0.5025120E-03/cm", "0.5042249E-03/cm", "0.5062117E-03/cm",
"0.5062750E-03/cm", "0.4876212E-03/cm", "0.4730343E-03/cm", "0.4540021E-03/cm", "0.4423832E-03/cm",
"0.4410428E-03/cm", "0.4436095E-03/cm", "0.4331241E-03/cm", "0.4401879E-03/cm", "0.4352570E-03/cm",
"0.4392237E-03/cm", "0.4313847E-03/cm", "0.4170704E-03/cm", "0.4156771E-03/cm", "0.4083890E-03/cm",
"0.4002930E-03/cm", "0.4009888E-03/cm", "0.3954170E-03/cm", "0.3893599E-03/cm", "0.3839422E-03/cm"};
refdLongFL_ = {
227.993, 237.122, 241.701, 256.48, 266.754, 275.988, 276.982, 284.989, 286.307, 290.478, 290.5, 292, 295.5};
refsLongFL_ = {"227.9925651*cm",
"237.1215213*cm",
"241.7005445*cm",
"256.47981*cm",
"266.7540042*cm",
"275.987715*cm",
"276.9823529*cm",
"284.9889299*cm",
"286.3065327*cm",
"290.4779412*cm",
"290.5*cm",
"292.0*cm",
"295.5*cm"};
}

void testDDFilteredView::checkFilteredView() {
Expand All @@ -41,4 +70,47 @@ void testDDFilteredView::checkFilteredView() {
std::cout << fview.name() << " is a " << cms::dd::name(cms::DDSolidShapeMap, fview.shape()) << "\n";
fview.parent();
std::cout << fview.name() << " is a " << cms::dd::name(cms::DDSolidShapeMap, fview.shape()) << "\n";

std::cout << "All SpecPar names:\n";
for (auto const n : det->specpars().names())
std::cout << n << "\n";

//
// SpecPar Reference values
//
// Name: hf
// Paths: //HVQX, //HVQF
// LongFL = 227.9925651*cm, 237.1215213*cm, 241.7005445*cm, 256.47981*cm, 266.7540042*cm, 275.987715*cm, 276.9823529*cm, 284.9889299*cm, 286.3065327*cm, 290.4779412*cm, 290.5*cm, 292.0*cm, 295.5*cm
// ShortFL = 206.0*cm, 211.8810861*cm, 220.3822464*cm, 235.5520581*cm, 245.6204691*cm, 253.9086538*cm, 255.0117647*cm, 263.0073529*cm, 264.3480392*cm, 268.5*cm, 268.5*cm, 270.0*cm, 273.5*cm
// Volume = HF
// lambLim = 300.0, 600.0
// attl = 0.8096535E-03/cm, 0.7130018E-03/cm, 0.6549183E-03/cm, 0.6027666E-03/cm, 0.5662951E-03/cm, 0.5416475E-03/cm, 0.5161745E-03/cm, 0.5025120E-03/cm, 0.5042249E-03/cm, 0.5062117E-03/cm, 0.5062750E-03/cm, 0.4876212E-03/cm, 0.4730343E-03/cm, 0.4540021E-03/cm, 0.4423832E-03/cm, 0.4410428E-03/cm, 0.4436095E-03/cm, 0.4331241E-03/cm, 0.4401879E-03/cm, 0.4352570E-03/cm, 0.4392237E-03/cm, 0.4313847E-03/cm, 0.4170704E-03/cm, 0.4156771E-03/cm, 0.4083890E-03/cm, 0.4002930E-03/cm, 0.4009888E-03/cm, 0.3954170E-03/cm, 0.3893599E-03/cm, 0.3839422E-03/cm
// Levels => 4, 5

cout << "Get attl from hf as double values:\n";
vector<double> attl = fview.get<vector<double>>("hf", "attl");
int count(0);
for (auto const& i : attl) {
std::cout << "attl " << i << " == " << refdattl_[count] << "\n";
CPPUNIT_ASSERT(abs(i - refdattl_[count]) < 10e-6);
count++;
}

std::cout << "Get LongFL from hf as double values:\n";
count = 0;
std::vector<double> LongFL = fview.get<std::vector<double>>("hf", "LongFL");
for (auto const& i : LongFL) {
std::cout << "LongFL " << i << " == " << refdLongFL_[count] << "\n";
CPPUNIT_ASSERT(abs(i - refdLongFL_[count]) < 10e-2);
count++;
}

std::cout << "Get LongFL from hf as string values:\n";
count = 0;
std::vector<std::string> sLongFL = fview.get<std::vector<std::string>>("hf", "LongFL");
for (auto const& i : sLongFL) {
std::cout << "LongFL " << i << " == " << refsLongFL_[count] << "\n";
CPPUNIT_ASSERT(i.compare(refsLongFL_[count]) == 0);
count++;
}
}
8 changes: 4 additions & 4 deletions DetectorDescription/DDCMS/test/python/testDDSpecPars.py
Expand Up @@ -39,16 +39,16 @@
)

process.DDDetectorESProducer = cms.ESSource("DDDetectorESProducer",
confGeomXMLFiles = cms.FileInPath('DetectorDescription/DDCMS/data/cms-2015-muon-geometry.xml'),
appendToDataLabel = cms.string('MUON')
confGeomXMLFiles = cms.FileInPath('DetectorDescription/DDCMS/data/cms-geometry-2021.xml'),
appendToDataLabel = cms.string('CMS')
)

process.DDSpecParRegistryESProducer = cms.ESProducer("DDSpecParRegistryESProducer",
appendToDataLabel = cms.string('MUON')
appendToDataLabel = cms.string('CMS')
)

process.test = cms.EDAnalyzer("DDTestSpecPars",
DDDetector = cms.ESInputTag('','MUON')
DDDetector = cms.ESInputTag('','CMS')
)

process.p = cms.Path(process.test)
8 changes: 4 additions & 4 deletions DetectorDescription/DDCMS/test/python/testDDSpecParsFilter.py
Expand Up @@ -38,16 +38,16 @@
)

process.DDDetectorESProducer = cms.ESSource("DDDetectorESProducer",
confGeomXMLFiles = cms.FileInPath('DetectorDescription/DDCMS/data/cms-2015-muon-geometry.xml'),
appendToDataLabel = cms.string('MUON')
confGeomXMLFiles = cms.FileInPath('DetectorDescription/DDCMS/data/cms-geometry-2021.xml'),
appendToDataLabel = cms.string('CMS')
)

process.DDSpecParRegistryESProducer = cms.ESProducer("DDSpecParRegistryESProducer",
appendToDataLabel = cms.string('MUON')
appendToDataLabel = cms.string('CMS')
)

process.test = cms.EDAnalyzer("DDTestSpecParsFilter",
DDDetector = cms.ESInputTag('','MUON'),
DDDetector = cms.ESInputTag('','CMS'),
attribute = cms.untracked.string('MuStructure'),
value = cms.untracked.string('MuonBarrelDT')
)
Expand Down