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 delayed Vector resolution #29126

Merged
merged 1 commit into from Mar 13, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions DetectorDescription/DDCMS/data/testDDVectors.xml
@@ -1,6 +1,14 @@
<?xml version="1.0"?>
<DDDefinition xmlns="http://www.cern.ch/cms/DDL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.cern.ch/cms/DDL ../../../DetectorDescription/Schema/DDLSchema.xsd">
<ConstantsSection label="trackerParameters.xml" eval="true">
<Constant name="Rmin" value="1*cm"/>
<Constant name="Rmin1" value="[Rmin]"/>
<Constant name="Rmin2" value="2*[Rmin1]"/>
<Constant name="Rmin3" value="[Rmin]+[Rmin2]"/>
<Constant name="Rmin4" value="4*[Rmin3] - [Rmin1] + 2*cm"/>
<Vector name="RadiiTest" type="numeric" nEntries="5">
[testDDVectors:Rmin], [testDDVectors:Rmin1], [testDDVectors:Rmin2], [testDDVectors:Rmin3], [testDDVectors:Rmin4]
</Vector>
<Vector name="RadiusMixBoundary" type="numeric" nEntries="16">
[hgcal:radMixL0], [hgcal:radMixL1], [hgcal:radMixL2], [hgcal:radMixL3],
[hgcal:radMixL4], [hgcal:radMixL5], [hgcal:radMixL6], [hgcal:radMixL7],
Expand Down
1 change: 1 addition & 0 deletions DetectorDescription/DDCMS/interface/DDParsingContext.h
Expand Up @@ -68,6 +68,7 @@ namespace cms {
};

std::map<std::string, std::vector<CompositeMaterial>> unresolvedMaterials;
std::map<std::string, std::vector<std::string>> unresolvedVectors;

bool geo_inited = false;

Expand Down
55 changes: 52 additions & 3 deletions DetectorDescription/DDCMS/plugins/dd4hep/DDDefinitions2Objects.cc
Expand Up @@ -1687,6 +1687,21 @@ void for_each_token(InputIt first, InputIt last, ForwardIt s_first, ForwardIt s_

namespace {

std::vector<string> splitString(const string& str, const string& delims = ",") {
std::vector<string> output;

for_each_token(cbegin(str), cend(str), cbegin(delims), cend(delims), [&output](auto first, auto second) {
if (first != second) {
if (string(first, second).front() == '[' && string(first, second).back() == ']') {
first++;
second--;
}
output.emplace_back(string(first, second));
}
});
return output;
}

tbb::concurrent_vector<double> splitNumeric(const string& str, const string& delims = ",") {
tbb::concurrent_vector<double> output;

Expand Down Expand Up @@ -1724,9 +1739,22 @@ void Converter<DDLVector>::operator()(xml_h element) const {
name.c_str(),
nEntries.c_str(),
val.c_str());

tbb::concurrent_vector<double> results = splitNumeric(val);
registry->insert({name, results});
try {
tbb::concurrent_vector<double> results = splitNumeric(val);
registry->insert({name, results});
} catch (const exception& e) {
printout(INFO,
"DD4CMS",
"++ Unresolved Vector<%s>: %s[%s]: %s. Try to resolve later. [%s]",
type.c_str(),
name.c_str(),
nEntries.c_str(),
val.c_str(),
e.what());

std::vector<string> results = splitString(val);
context->unresolvedVectors.insert({name, results});
}
}

template <>
Expand Down Expand Up @@ -1866,6 +1894,27 @@ static long load_dddefinition(Detector& det, xml_h element) {
}
// Before we continue, we have to resolve all constants NOW!
Converter<DDRegistry>(det, &context, &res)(dddef);
{
DDVectorsMap* registry = context.description.load()->extension<DDVectorsMap>();

printout(context.debug_constants ? ALWAYS : DEBUG,
"DD4CMS",
"+++ RESOLVING %ld Vectors.....",
context.unresolvedVectors.size());

while (!context.unresolvedVectors.empty()) {
for (auto it = context.unresolvedVectors.begin(); it != context.unresolvedVectors.end();) {
auto const& name = it->first;
tbb::concurrent_vector<double> result;
for (const auto& i : it->second) {
result.emplace_back(dd4hep::_toDouble(i));
}
registry->insert({name, result});
// All components are resolved
it = context.unresolvedVectors.erase(it);
}
}
}
// Now we can process the include files one by one.....
for (xml::Document d : res.includes) {
print_doc((doc = d).root());
Expand Down