15 #define DETECTORTOOLS_CPP
30 #include <TGeoMatrix.h>
40 namespace detail {
namespace tools {
62 using ChildPaths = std::map<std::string, detail::tools::PlacementPath, std::less<>>;
64 thread_local std::unique_ptr<std::map<const TGeoNode*, ChildPaths>> s_pathCache;
68 std::set<std::string, std::less<>>& names, ChildPaths& paths) {
69 const Int_t num_daughters = node->GetNdaughters();
70 for( Int_t i = 0; i < num_daughters && !names.empty(); ++i ) {
71 TGeoNode* daughter = node->GetDaughter(i);
72 auto name = daughter ? names.find(daughter->GetName()) : names.end();
73 if ( name != names.end() ) {
75 path.emplace_back(daughter);
76 path.insert(path.end(), branch.rbegin(), branch.rend());
80 for( Int_t i = 0; i < num_daughters && !names.empty(); ++i ) {
81 if ( TGeoNode* daughter = node->GetDaughter(i) ) {
82 branch.emplace_back(daughter);
83 collectPaths(daughter, branch, names, paths);
92 const char* name = child_place->GetName();
93 if ( 0 == ::strcmp(parent_place->GetName(), name) )
95 auto [entry, is_new] = s_pathCache->try_emplace(parent_place.
ptr());
97 std::set<std::string, std::less<>> names;
98 for(
const auto& child : parent.
children() ) {
100 if ( place.
isValid() && 0 != ::strcmp(place->GetName(), parent_place->GetName()) )
101 names.emplace(place->GetName());
104 collectPaths(parent_place.
ptr(), branch, names, entry->second);
106 auto found = entry->second.find(name);
107 if ( found == entry->second.end() )
109 path.insert(path.end(), found->second.begin(), found->second.end());
115 if ( !s_pathCache ) {
116 s_pathCache = std::make_unique<std::map<const TGeoNode*, ChildPaths>>();
129 s_pathCache->clear();
131 element->
path.clear();
135 for(
const auto& child : element->
children )
143 if ( parent.
ptr() == child.
ptr() )
return true;
145 if ( par.ptr() == parent.
ptr() )
return true;
148 throw std::runtime_error(
"Search for parent detector element with invalid handles not allowed.");
155 if ( parent.
ptr() == child.
ptr() ) {
156 path.emplace_back(child);
159 TIter next(parent->GetVolume()->GetNodes());
161 for (TGeoNode *daughter = (TGeoNode*) next(); daughter; daughter = (TGeoNode*) next()) {
162 if ( daughter == child.
ptr() ) {
163 path.emplace_back(daughter);
169 for (TGeoNode *daughter = (TGeoNode*) next(); daughter; daughter = (TGeoNode*) next()) {
171 bool res =
findChild(daughter, child, sub_path);
173 path.insert(path.end(), sub_path.begin(), sub_path.end());
174 path.emplace_back(daughter);
186 if ( 0 == ::strcmp(parent.
ptr()->GetName(),child.
ptr()->GetName()) ) {
187 path.emplace_back(child);
190 TIter next(parent->GetVolume()->GetNodes());
192 for (TGeoNode *daughter = (TGeoNode*) next(); daughter; daughter = (TGeoNode*) next()) {
193 if ( 0 == ::strcmp(daughter->GetName(),child.
ptr()->GetName()) ) {
194 path.emplace_back(daughter);
200 for (TGeoNode *daughter = (TGeoNode*) next(); daughter; daughter = (TGeoNode*) next()) {
202 bool res = findChildByName(daughter, child, sub_path);
204 path.insert(path.end(), sub_path.begin(), sub_path.end());
205 path.emplace_back(daughter);
223 if ( parent.
ptr() == child.
ptr() ) {
229 elements.emplace_back(par);
230 if ( par.ptr() == parent.
ptr() ) {
235 throw std::runtime_error(std::string(
"The detector element ")+parent.
name()+std::string(
" is no parent of ")+child.
name());
237 throw std::runtime_error(
"Search for parent detector element with invalid handles not allowed.");
244 parent = element.
world();
249 det_nodes.emplace_back(pv);
251 if ( par.ptr() == parent.
ptr() )
return;
253 throw std::runtime_error((
"The detector element "+std::string(parent.
name())+
" is no parent of ")+element.
name());
255 throw std::runtime_error(
"Cannot access element path [Invalid DetElement]");
263 det_nodes.emplace_back(pv);
270 std::string path =
"";
272 for(
auto i=nodes.rbegin(); i != nodes.rend(); ++i)
273 path +=
"/" + std::string((*i).name());
276 for(
auto i=begin(nodes); i != end(nodes); ++i)
277 path +=
"/" + std::string((*i)->GetName());
284 std::string path =
"";
286 for(ElementPath::const_reverse_iterator i=nodes.rbegin();i!=nodes.rend();++i)
287 path +=
"/" + std::string((*i)->GetName());
290 for(ElementPath::const_iterator i=nodes.begin();i!=nodes.end();++i)
291 path +=
"/" + std::string((*i)->GetName());
311 size_t idx = subpath.find(
'/',1);
312 if ( subpath[0] ==
'/' ) {
314 if ( idx == std::string::npos )
return top;
317 if ( idx == std::string::npos )
318 return parent.
child(subpath);
319 std::string name = subpath.substr(0,idx);
324 throw std::runtime_error(
"dd4hep: DetElement "+parent.
path()+
" has no child named:"+name+
" [No such child]");
326 throw std::runtime_error(
"dd4hep: Cannot determine child with path "+subpath+
" from invalid parent [invalid handle]");
336 throw std::runtime_error(
"dd4hep: DetElement cannot determine top parent (world) [invalid handle]");
342 for (
size_t i = 0, n = det_nodes.size(); n > 0 && i < n-1; ++i, elt = elt.
parent()) {
344 bool found = s_pathCache && par.
isValid() && par.
placement().
ptr() == det_nodes[i + 1].ptr()
345 && cachedPath(par, det_nodes[i + 1], det_nodes[i], all_nodes);
346 if (!found && !findChildByName(det_nodes[i + 1], det_nodes[i], all_nodes)) {
347 throw std::runtime_error(
"dd4hep: DetElement cannot determine placement path of "
348 + std::string(det_nodes[i].name()) +
" [internal error]");
351 if ( det_nodes.size() > 0 ) {
352 all_nodes.emplace_back(det_nodes.back());
360 makePlacementPath(element, std::move(det_nodes), all_nodes);
367 makePlacementPath(element, std::move(det_nodes), all_nodes);
379 std::string path =
"";
381 for(PlacementPath::const_reverse_iterator i=nodes.rbegin();i!=nodes.rend();++i)
382 path +=
"/" + std::string((*i)->GetName());
385 for(PlacementPath::const_iterator i=nodes.begin();i!=nodes.end();++i)
386 path +=
"/" + std::string((*i)->GetName());
393 std::string path =
"";
395 for(std::vector<const TGeoNode*>::const_reverse_iterator i=nodes.rbegin();i!=nodes.rend();++i)
396 path +=
"/" + std::string((*i)->GetName());
399 for(
const auto* n : nodes )
400 path +=
"/" + std::string(n->GetName());
406 if ( !mat ) mat =
new TGeoHMatrix(*gGeoIdentity);
413 if (nodes.size() > 0) {
414 for (
size_t i = 0, n=nodes.size(); n>0 && i < n-1; ++i) {
416 mat.MultiplyLeft(p->GetMatrix());
418 if ( inverse ) mat = mat.Inverse();
424 TGeoNode* top = top_place.
ptr();
425 const char* path = place.c_str();
427 Int_t length = strlen(path);
428 if (!length)
return 0;
429 TString spath = path;
432 Int_t ind1 = spath.Index(
"/");
435 if ( strcmp(path,top->GetName()) )
return 0;
440 if (ind1>0) ind1 = -1;
441 else ind2 = spath.Index(
"/", ind1+1);
443 if (ind2<0) ind2 = length;
444 TString name(spath(ind1+1, ind2-ind1-1));
445 if ( name == top->GetName() ) {
446 if (ind2>=length-1)
return top;
452 TGeoNode *node = top;
455 ind2 = spath.Index(
"/", ind1+1);
460 vol = node->GetVolume();
461 name = spath(ind1+1, ind2-ind1-1);
462 node = vol->GetNode(name.Data());
465 else if (ind2>=length-1)
474 std::stringstream log;
475 for(
const auto&
v : ids )
476 log <<
v.first <<
"=" <<
v.second <<
"; ";
482 std::stringstream log;
483 for(
const auto&
id : ids ) {
486 log <<
id.first <<
"=" <<
id.second <<
"," << value <<
" [" << f->
offset() <<
"," << f->
width() <<
"] ";
493 std::vector<std::string> result;
494 if ( !path.empty() ) {
495 std::string tmp = path[0]==
'/' ? path.substr(1) : path;
496 for(
size_t idx=tmp.find(
'/'); idx != std::string::npos; idx=tmp.find(
'/')) {
497 std::string val = tmp.substr(0,idx);
498 result.emplace_back(val);
499 tmp = tmp.length()>idx ? tmp.substr(idx+1) : std::string();
501 if ( !tmp.empty() ) {
502 result.emplace_back(tmp);