23 #include "TGeoManager.h"
26 using namespace DD4hep;
27 using namespace DD4hep::Alignments;
28 using namespace DD4hep::Alignments::DDAlign_standard_operations;
34 if ( p.isValid() && !p.parent().isValid() )
36 else if ( !p.isValid() )
40 throw runtime_error(
"DD4hep: DetElement cannot determine detector parent [Invalid handle]");
44 GlobalAlignmentCache::GlobalAlignmentCache(LCDD& lcdd,
const string& sdPath,
bool top)
45 : m_lcdd(lcdd), m_sdPath(sdPath), m_sdPathLen(sdPath.length()), m_refCount(1), m_top(top)
51 int nentries = (int)
m_cache.size();
56 "Destroy cache for subdetector %s [%d section(s), %d entrie(s)]",
92 TGeoPhysicalNode* pn = alignment.
ptr();
94 Cache::const_iterator i =
m_cache.find(index);
95 printout(
ALWAYS,
"GlobalAlignmentCache",
"Section: %s adding entry: %s",
96 name().c_str(),alignment->GetName());
107 if ( path_name[0] !=
'/' ) {
108 return section(
m_lcdd.world().placementPath()+
'/'+path_name);
110 else if ( (idx=path_name.find(
'/',1)) == string::npos ) {
116 if ( (idq=path_name.find(
'/',idx+1)) != string::npos ) --idq;
117 string path = path_name.substr(idx+1,idq-idx);
118 SubdetectorAlignments::const_iterator j =
m_detectors.find(path);
126 Cache::const_iterator i =
m_cache.find(index);
133 else if ( path_name[0] !=
'/' ) {
134 return get(
m_lcdd.world().placementPath()+
'/'+path_name);
136 else if ( (idx=path_name.find(
'/',1)) == string::npos ) {
140 if ( (idq=path_name.find(
'/',idx+1)) != string::npos ) --idq;
141 string path = path_name.substr(idx+1,idq-idx);
142 SubdetectorAlignments::const_iterator j =
m_detectors.find(path);
143 if ( j !=
m_detectors.end() )
return (*j).second->get(path_name);
149 vector<GlobalAlignment> result;
152 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;
168 TGeoManager& mgr =
m_lcdd.manager();
169 mgr.UnlockGeometry();
176 SubdetectorAlignments::const_iterator i =
m_detectors.find(nam);
187 typedef map<string,DetElement> DetElementUpdates;
188 typedef map<DetElement,vector<Entry*> > sd_entries_t;
189 TGeoManager& mgr =
m_lcdd.manager();
190 DetElementUpdates detelt_updates;
193 while(stack.
size() > 0) {
196 all[det].push_back(e);
201 for(sd_entries_t::iterator i=all.begin(); i!=all.end(); ++i) {
204 sd_cache->
apply( (*i).second );
208 printout(
INFO,
"GlobalAlignmentCache",
"Alignments were applied. Refreshing physical nodes....");
209 mgr.GetCurrentNavigator()->ResetAll();
210 mgr.GetCurrentNavigator()->BuildCache();
211 mgr.RefreshPhysicalNodes();
214 for(DetElementUpdates::iterator i=detelt_updates.begin(); i!=detelt_updates.end(); ++i) {
216 printout(
DEBUG,
"GlobalAlignmentCache",
"+++ Trigger placement update for %s [2]",elt.path().c_str());
220 string last_path =
"?????";
221 for(DetElementUpdates::iterator i=detelt_updates.begin(); i!=detelt_updates.end(); ++i) {
222 const string& path = (*i).first;
223 if ( path.find(last_path) == string::npos ) {
225 printout(
DEBUG,
"GlobalAlignmentCache",
"+++ Trigger placement update for %s [1]",elt.path().c_str());
227 last_path = (*i).first;
231 for(sd_entries_t::iterator i=all.begin(); i!=all.end(); ++i) {
233 printout(
DEBUG,
"GlobalAlignmentCache",
"+++ Trigger placement update for %s [0]",elt.path().c_str());
240 typedef map<string,pair<TGeoPhysicalNode*,Entry*> > Nodes;
247 for_each(changes.begin(),changes.end(),selector.
reset());
Act on selected alignment entries.
Cache m_cache
The subdetector specific map of alignments caches.
GlobalAlignmentCache(LCDD &lcdd, const std::string &sdPath, bool top)
Default constructor initializing variables.
const AlignmentSelector & reset() const
bool insert(GlobalAlignment alignment)
Add a new entry to the cache. The key is the placement path.
bool isValid() const
Check the validity of the object held by the handle.
const std::string & name() const
Access the section name.
Main handle class to hold a TGeo alignment object of type TGeoPhysicalNode.
void commit(AlignmentStack &stack)
Close existing transaction stack and apply all alignments.
DetElement detector
Reference to the detector element.
int m_refCount
Reference count.
std::vector< GlobalAlignment > matches(const std::string &path_match, bool exclude_exact=false) const
Return all entries matching a given path. Careful: Expensive operaton!
T * ptr() const
Access to the held object.
DD4hep::Geometry::DetElement DetElement
static void uninstall(LCDD &lcdd)
Unregister and delete a tree instance.
GlobalAlignment get(const std::string &path) const
Retrieve an alignment entry by its lacement path.
bool hasMatrix() const
Check if this alignment entry has a non unitary transformation matrix.
size_t size() const
Access size of the alignment stack.
size_t m_sdPathLen
The length of the branch name to optimize lookups....
bool needsReset() const
Check flag if the node location should be reset.
DetElement _detector(DetElement child)
int addRef()
Add reference count.
void releaseObjects(M &m)
int release()
Release object. If reference count goes to NULL, automatic deletion is triggered. ...
void apply(AlignmentStack &stack)
Population entry: Apply a complete stack of ordered alignments to the geometry structure.
DetElement parent() const
Access to the detector elements's parent.
virtual ~GlobalAlignmentCache()
Default destructor.
unsigned int hash32(const char *key)
We need it so often: one-at-time 32 bit hash function.
SubdetectorAlignments m_detectors
Cache of subdetectors.
Class caching all known alignment operations for one LCDD instance.
Handle class describing a detector element.
static GlobalAlignmentCache * install(LCDD &lcdd)
Create and install a new instance tree.
bool resetChildren() const
Check flag if the node location and all children should be reset.
GlobalAlignmentCache * subdetectorAlignments(const std::string &name)
Retrieve branch cache by name. If not present it will be created.
GlobalAlignmentCache * section(const std::string &path_name) const
Retrieve the cache section corresponding to the path of an entry.
AlignmentStack::StackEntry Entry
int printout(PrintLevel severity, const char *src, const char *fmt,...)
Calls the display action with a given severity level.
Alignment Stack object definition.
dd4hep_ptr< StackEntry > pop()
Retrieve an alignment entry of the current stack.
Select alignment operations according to certain criteria.