DD4hep  1.28.0
Detector Description Toolkit for High Energy Physics
GlobalAlignmentCache.cpp
Go to the documentation of this file.
1 //==========================================================================
2 // AIDA Detector description implementation
3 //--------------------------------------------------------------------------
4 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
5 // All rights reserved.
6 //
7 // For the licensing terms see $DD4hepINSTALL/LICENSE.
8 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
9 //
10 // Author : M.Frank
11 //
12 //==========================================================================
13 
14 // Framework include files
15 #include <DD4hep/Detector.h>
16 #include <DD4hep/Printout.h>
20 
21 // ROOT include files
22 #include <TGeoManager.h>
23 
24 using namespace dd4hep::align;
26 
28  if ( child.isValid() ) {
29  dd4hep::DetElement p(child.parent());
30  if ( p.isValid() && !p.parent().isValid() )
31  return child;
32  else if ( !p.isValid() ) // World detector element...
33  return child;
34  return _detector(p);
35  }
36  dd4hep::except("GlobalAlignmentCache", "DetElement cannot determine detector parent [Invalid handle]");
37  return {};
38 }
39 
41 GlobalAlignmentCache::GlobalAlignmentCache(Detector& description, const std::string& sdPath, bool top)
42  : m_detDesc(description), m_sdPath(sdPath), m_sdPathLen(sdPath.length()), m_refCount(1), m_top(top)
43 {
44 }
45 
48  int nentries = (int)m_cache.size();
49  int nsect = (int)m_detectors.size();
50  detail::releaseObjects(m_detectors);
51  m_cache.clear();
52  printout(INFO,"GlobalAlignmentCache",
53  "Destroy cache for subdetector %s [%d section(s), %d entrie(s)]",
54  m_sdPath.c_str(),nsect,nentries);
55 }
56 
59  return ++m_refCount;
60 }
61 
64  int value = --m_refCount;
65  if ( value == 0 ) {
66  delete this;
67  }
68  return value;
69 }
70 
73  GlobalAlignmentCache* cache = description.extension<GlobalAlignmentCache>(false);
74  if ( !cache ) {
75  cache = new GlobalAlignmentCache(description,"world",true);
77  description.addUserExtension(detail::typeHash64<GlobalAlignmentCache>(),e);
78  }
79  return cache;
80 }
81 
84  if ( description.extension<GlobalAlignmentCache>(false) ) {
85  description.removeExtension<GlobalAlignmentCache>(true);
86  }
87 }
88 
91  TGeoPhysicalNode* pn = alignment.ptr();
92  unsigned int index = detail::hash32(pn->GetName()+m_sdPathLen);
93  Cache::const_iterator i = m_cache.find(index);
94  printout(ALWAYS,"GlobalAlignmentCache","Section: %s adding entry: %s",
95  name().c_str(),alignment->GetName());
96  if ( i == m_cache.end() ) {
97  m_cache[index] = pn;
98  return true;
99  }
100  return false;
101 }
102 
104 GlobalAlignmentCache* GlobalAlignmentCache::section(const std::string& path_name) const {
105  std::size_t idx, idq;
106  if ( path_name[0] != '/' ) {
107  return section(m_detDesc.world().placementPath()+'/'+path_name);
108  }
109  else if ( (idx=path_name.find('/',1)) == std::string::npos ) {
110  return (m_sdPath == path_name.c_str()+1) ? (GlobalAlignmentCache*)this : 0;
111  }
112  else if ( m_detectors.empty() ) {
113  return 0;
114  }
115  if ( (idq=path_name.find('/',idx+1)) != std::string::npos ) --idq;
116  std::string path = path_name.substr(idx+1,idq-idx);
117  SubdetectorAlignments::const_iterator j = m_detectors.find(path);
118  return (j==m_detectors.end()) ? 0 : (*j).second;
119 }
120 
122 GlobalAlignment GlobalAlignmentCache::get(const std::string& path_name) const {
123  std::size_t idx, idq;
124  unsigned int index = detail::hash32(path_name.c_str()+m_sdPathLen);
125  Cache::const_iterator i = m_cache.find(index);
126  if ( i != m_cache.end() ) {
127  return GlobalAlignment((*i).second);
128  }
129  else if ( m_detectors.empty() ) {
130  return GlobalAlignment(0);
131  }
132  else if ( path_name[0] != '/' ) {
133  return get(m_detDesc.world().placementPath()+'/'+path_name);
134  }
135  else if ( (idx=path_name.find('/',1)) == std::string::npos ) {
136  // Escape: World volume and not found in cache --> not present
137  return GlobalAlignment(0);
138  }
139  if ( (idq=path_name.find('/',idx+1)) != std::string::npos ) --idq;
140  std::string path = path_name.substr(idx+1, idq-idx);
141  SubdetectorAlignments::const_iterator j = m_detectors.find(path);
142  if ( j != m_detectors.end() ) return (*j).second->get(path_name);
143  return GlobalAlignment(0);
144 }
145 
147 std::vector<GlobalAlignment>
148 GlobalAlignmentCache::matches(const std::string& match, bool exclude_exact) const {
149  std::vector<GlobalAlignment> result;
150  GlobalAlignmentCache* c = section(match);
151  if ( c ) {
152  std::size_t len = match.length();
153  result.reserve(c->m_cache.size());
154  for(Cache::const_iterator i=c->m_cache.begin(); i!=c->m_cache.end();++i) {
155  const Cache::value_type& v = *i;
156  const char* n = v.second->GetName();
157  if ( 0 == ::strncmp(n,match.c_str(),len) ) {
158  if ( exclude_exact && len == ::strlen(n) ) continue;
159  result.emplace_back(GlobalAlignment(v.second));
160  }
161  }
162  }
163  return result;
164 }
165 
168  TGeoManager& mgr = m_detDesc.manager();
169  mgr.UnlockGeometry();
170  apply(stack);
171  mgr.LockGeometry();
172 }
173 
176  SubdetectorAlignments::const_iterator i = m_detectors.find(nam);
177  if ( i == m_detectors.end() ) {
179  m_detectors.emplace(nam,ptr);
180  return ptr;
181  }
182  return (*i).second;
183 }
184 
187  typedef std::map<std::string,DetElement> DetElementUpdates;
188  typedef std::map<DetElement,std::vector<Entry*> > sd_entries_t;
189  TGeoManager& mgr = m_detDesc.manager();
190  DetElementUpdates detelt_updates;
191  sd_entries_t all;
192 
193  while(stack.size() > 0) {
194  Entry* e = stack.pop().release();
196  all[det].emplace_back(e);
197  if ( stack.hasMatrix(*e) || stack.needsReset(*e) || stack.resetChildren(*e) ) {
198  detelt_updates.emplace(e->detector.path(),e->detector);
199  }
200  }
201  for(sd_entries_t::iterator i=all.begin(); i!=all.end(); ++i) {
202  DetElement det((*i).first);
203  GlobalAlignmentCache* sd_cache = subdetectorAlignments(det.placement().name());
204  sd_cache->apply( (*i).second );
205  (*i).second.clear();
206  }
207 
208  printout(INFO,"GlobalAlignmentCache","Alignments were applied. Refreshing physical nodes....");
209  mgr.GetCurrentNavigator()->ResetAll();
210  mgr.GetCurrentNavigator()->BuildCache();
211  mgr.RefreshPhysicalNodes();
212 
213  // Provide update callback for every detector element with a changed placement
214  for(DetElementUpdates::iterator i=detelt_updates.begin(); i!=detelt_updates.end(); ++i) {
215  DetElement elt((*i).second);
216  printout(DEBUG,"GlobalAlignmentCache","+++ Trigger placement update for %s [2]",elt.path().c_str());
218  }
219  // Provide update callback for the highest detector element
220  std::string last_path = "?????";
221  for(DetElementUpdates::iterator i=detelt_updates.begin(); i!=detelt_updates.end(); ++i) {
222  const std::string& path = (*i).first;
223  if ( path.find(last_path) == std::string::npos ) {
224  DetElement elt((*i).second);
225  printout(DEBUG,"GlobalAlignmentCache","+++ Trigger placement update for %s [1]",elt.path().c_str());
227  last_path = (*i).first;
228  }
229  }
230  // Provide update callback at the detector level
231  for(sd_entries_t::iterator i=all.begin(); i!=all.end(); ++i) {
232  DetElement elt((*i).first);
233  printout(DEBUG,"GlobalAlignmentCache","+++ Trigger placement update for %s [0]",elt.path().c_str());
235  }
236 }
237 
239 void GlobalAlignmentCache::apply(const std::vector<Entry*>& changes) {
240  std::map<std::string,std::pair<TGeoPhysicalNode*,Entry*> > nodes;
242  GlobalAlignmentSelector selector(*this,nodes,changes);
243 
244  for_each(m_cache.begin(),m_cache.end(),selector.reset());
245  for_each(nodes.begin(),nodes.end(),GlobalAlignmentActor<ops::node_print>(*this,nodes));
246  for_each(nodes.begin(),nodes.end(),GlobalAlignmentActor<ops::node_reset>(*this,nodes));
247 
248  for_each(changes.begin(),changes.end(),selector.reset());
249  for_each(nodes.begin(),nodes.end(),GlobalAlignmentActor<ops::node_align>(*this,nodes));
250  for_each(nodes.begin(),nodes.end(),GlobalAlignmentActor<ops::node_delete>(*this,nodes));
251 }
dd4hep::Detector::manager
virtual TGeoManager & manager() const =0
Access the geometry manager of this instance.
dd4hep::DetElement::path
const std::string & path() const
Path of the detector element (not necessarily identical to placement path!)
Definition: DetElement.cpp:157
dd4hep::align::GlobalAlignmentCache::uninstall
static void uninstall(Detector &description)
Unregister and delete a tree instance.
Definition: GlobalAlignmentCache.cpp:83
dd4hep::ExtensionEntry
Definition of the extension entry interface class.
Definition: ExtensionEntry.h:39
dd4hep::align::GlobalAlignmentStack::StackEntry::detector
DetElement detector
Reference to the detector element.
Definition: GlobalAlignmentStack.h:56
dd4hep::Detector::world
virtual DetElement world() const =0
Return reference to the top-most (world) detector element.
dd4hep::align::GlobalAlignmentStack::hasMatrix
static bool hasMatrix(const StackEntry &e)
Check if this alignment entry has a non unitary transformation matrix.
Definition: GlobalAlignmentStack.h:128
dd4hep::align::GlobalAlignmentStack
Alignment Stack object definition.
Definition: GlobalAlignmentStack.h:35
v
View * v
Definition: MultiView.cpp:28
dd4hep::align::GlobalAlignment
Main handle class to hold a TGeo alignment object of type TGeoPhysicalNode.
Definition: GlobalAlignment.h:38
dd4hep::DetElement::parent
DetElement parent() const
Access to the detector elements's parent.
Definition: DetElement.cpp:238
dd4hep::align::GlobalAlignmentActor
Act on selected alignment entries.
Definition: GlobalAlignmentOperators.h:79
Detector.h
dd4hep::align::GlobalAlignmentCache::matches
std::vector< GlobalAlignment > matches(const std::string &path_match, bool exclude_exact=false) const
Return all entries matching a given path. Careful: Expensive operaton!
Definition: GlobalAlignmentCache.cpp:148
dd4hep::align::GlobalAlignmentCache::release
int release()
Release object. If reference count goes to NULL, automatic deletion is triggered.
Definition: GlobalAlignmentCache.cpp:63
GlobalAlignmentOperators.h
dd4hep::align::GlobalAlignmentCache::m_cache
Cache m_cache
The subdetector specific map of alignments caches.
Definition: GlobalAlignmentCache.h:57
dd4hep::align::GlobalAlignmentCache::addRef
int addRef()
Add reference count.
Definition: GlobalAlignmentCache.cpp:58
dd4hep::align::GlobalAlignmentCache::m_detDesc
Detector & m_detDesc
Definition: GlobalAlignmentCache.h:53
dd4hep::Handle::isValid
bool isValid() const
Check the validity of the object held by the handle.
Definition: Handle.h:128
DetectorInterna.h
dd4hep::DetElement::PLACEMENT_ELEMENT
@ PLACEMENT_ELEMENT
Definition: DetElement.h:221
dd4hep::align::GlobalAlignmentStack::size
size_t size() const
Access size of the alignment stack.
Definition: GlobalAlignmentStack.h:141
dd4hep::Detector::addUserExtension
virtual void * addUserExtension(unsigned long long int key, ExtensionEntry *entry)=0
Add an extension object to the detector element (low level member function)
dd4hep::DetElement::PLACEMENT_HIGHEST
@ PLACEMENT_HIGHEST
Definition: DetElement.h:222
dd4hep::align::GlobalAlignmentCache::m_sdPathLen
size_t m_sdPathLen
The length of the branch name to optimize lookups....
Definition: GlobalAlignmentCache.h:62
dd4hep::align::GlobalAlignmentCache::m_detectors
SubdetectorAlignments m_detectors
Cache of subdetectors.
Definition: GlobalAlignmentCache.h:55
dd4hep::align::GlobalAlignmentCache::apply
void apply(GlobalAlignmentStack &stack)
Population entry: Apply a complete stack of ordered alignments to the geometry structure.
Definition: GlobalAlignmentCache.cpp:186
GlobalAlignmentCache.h
dd4hep::align::GlobalAlignmentCache::subdetectorAlignments
GlobalAlignmentCache * subdetectorAlignments(const std::string &name)
Retrieve branch cache by name. If not present it will be created.
Definition: GlobalAlignmentCache.cpp:175
dd4hep::DetElement
Handle class describing a detector element.
Definition: DetElement.h:188
dd4hep::detail::DeleteExtension
Implementation class for the object extension mechanism.
Definition: ExtensionEntry.h:122
dd4hep::align::DDAlign_standard_operations
Helper namespace to specialize functionality.
Definition: GlobalAlignmentOperators.h:94
dd4hep::align::GlobalAlignmentCache::commit
void commit(GlobalAlignmentStack &stack)
Close existing transaction stack and apply all alignments.
Definition: GlobalAlignmentCache.cpp:167
dd4hep::align::GlobalAlignmentCache::get
GlobalAlignment get(const std::string &path) const
Retrieve an alignment entry by its placement path.
Definition: GlobalAlignmentCache.cpp:122
dd4hep::DetElement::placementPath
const std::string & placementPath() const
Access to the full path to the placed object.
Definition: DetElement.cpp:85
dd4hep::align::GlobalAlignmentCache::~GlobalAlignmentCache
virtual ~GlobalAlignmentCache()
Default destructor.
Definition: GlobalAlignmentCache.cpp:47
_detector
dd4hep::DetElement _detector(dd4hep::DetElement child)
Definition: GlobalAlignmentCache.cpp:27
StackEntry
GlobalAlignmentStack::StackEntry StackEntry
Definition: GlobalAlignmentParser.cpp:60
dd4hep::align::GlobalAlignmentCache
Class caching all known alignment operations for one Detector instance.
Definition: GlobalAlignmentCache.h:41
dd4hep::align::GlobalAlignmentStack::needsReset
static bool needsReset(const StackEntry &e)
Check flag if the node location should be reset.
Definition: GlobalAlignmentStack.h:130
dd4hep::align::GlobalAlignmentSelector::reset
const GlobalAlignmentSelector & reset() const
Definition: GlobalAlignmentOperators.h:63
dd4hep::align::GlobalAlignmentCache::GlobalAlignmentCache
GlobalAlignmentCache(Detector &description, const std::string &sdPath, bool top)
Default constructor initializing variables.
Definition: GlobalAlignmentCache.cpp:41
dd4hep::Handle::ptr
T * ptr() const
Access to the held object.
Definition: Handle.h:153
dd4hep::align::GlobalAlignmentCache::m_refCount
int m_refCount
Reference count.
Definition: GlobalAlignmentCache.h:64
dd4hep::Detector::extension
IFACE * extension(bool alert=true) const
Access extension element by the type.
Definition: Detector.h:342
det
DetElement::Object * det
Definition: AlignmentsCalculator.cpp:66
dd4hep::align::GlobalAlignmentStack::pop
dd4hep_ptr< StackEntry > pop()
Retrieve an alignment entry of the current stack.
Definition: GlobalAlignmentStack.cpp:160
dd4hep::Detector
The main interface to the dd4hep detector description package.
Definition: Detector.h:90
dd4hep::align::GlobalAlignmentCache::section
GlobalAlignmentCache * section(const std::string &path_name) const
Retrieve the cache section corresponding to the path of an entry.
Definition: GlobalAlignmentCache.cpp:104
dd4hep::align::GlobalAlignmentCache::name
const std::string & name() const
Access the section name.
Definition: GlobalAlignmentCache.h:94
dd4hep::align::GlobalAlignmentSelector
Select alignment operations according to certain criteria.
Definition: GlobalAlignmentOperators.h:55
dd4hep::align::GlobalAlignmentCache::insert
bool insert(GlobalAlignment alignment)
Add a new entry to the cache. The key is the placement path.
Definition: GlobalAlignmentCache.cpp:90
dd4hep::align::GlobalAlignmentStack::StackEntry
Stack entry definition.
Definition: GlobalAlignmentStack.h:53
Printout.h
dd4hep::align::GlobalAlignmentCache::m_sdPath
std::string m_sdPath
Definition: GlobalAlignmentCache.h:60
dd4hep::align
Namespace for implementation details of the AIDA detector description toolkit.
Definition: AlignmentsCalib.h:31
dd4hep::align::GlobalAlignmentStack::resetChildren
static bool resetChildren(const StackEntry &e)
Check flag if the node location and all children should be reset.
Definition: GlobalAlignmentStack.h:132
dd4hep::DetElement::PLACEMENT_CHANGED
@ PLACEMENT_CHANGED
Definition: DetElement.h:219
dd4hep::Detector::removeExtension
IFACE * removeExtension(bool destroy=true)
Remove an existing extension object from the Detector instance. If not destroyed, the instance is ret...
Definition: Detector.h:337
dd4hep::align::GlobalAlignmentCache::install
static GlobalAlignmentCache * install(Detector &description)
Create and install a new instance tree.
Definition: GlobalAlignmentCache.cpp:72
dd4hep::DetElement::PLACEMENT_DETECTOR
@ PLACEMENT_DETECTOR
Definition: DetElement.h:223