Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 09:16:25

0001 //==========================================================================
0002 //  AIDA Detector description implementation 
0003 //--------------------------------------------------------------------------
0004 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
0005 // All rights reserved.
0006 //
0007 // For the licensing terms see $DD4hepINSTALL/LICENSE.
0008 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
0009 //
0010 // Author     : M.Frank
0011 //
0012 //==========================================================================
0013 
0014 // Framework include files
0015 #include <DD4hep/Detector.h>
0016 #include <DD4hep/Objects.h>
0017 #include <DD4hep/Printout.h>
0018 #include <DD4hep/InstanceCount.h>
0019 #include <DDAlign/GlobalAlignmentStack.h>
0020 
0021 using namespace dd4hep::align;
0022 
0023 static dd4hep::dd4hep_ptr<GlobalAlignmentStack>& _stack()  {
0024   static dd4hep::dd4hep_ptr<GlobalAlignmentStack> s;
0025   return s;
0026 }
0027 static dd4hep::dd4hep_ptr<GlobalAlignmentStack>& _stack(GlobalAlignmentStack* obj)  {
0028   dd4hep::dd4hep_ptr<GlobalAlignmentStack>& stk = _stack();
0029   stk.adopt(obj);
0030   return stk;
0031 }
0032 
0033 /// Constructor with partial initialization
0034 GlobalAlignmentStack::StackEntry::StackEntry(DetElement element, const std::string& p, const Delta& del, double ov)
0035   : detector(element), delta(del), path(p), overlap(ov)
0036 {
0037   InstanceCount::increment(this);
0038 }
0039 
0040 /// Copy constructor
0041 GlobalAlignmentStack::StackEntry::StackEntry(const StackEntry& e)
0042   : detector(e.detector), delta(e.delta), path(e.path), overlap(e.overlap)
0043 {
0044   InstanceCount::increment(this);
0045 }
0046 
0047 /// Default destructor
0048 GlobalAlignmentStack::StackEntry::~StackEntry() {
0049   InstanceCount::decrement(this);
0050 }
0051 #if 0
0052 /// Set flag to reset the entry to its ideal geometrical position
0053 GlobalAlignmentStack::StackEntry& GlobalAlignmentStack::StackEntry::setReset(bool new_value)   {
0054   new_value ? (delta.flags |= RESET_VALUE) : (delta.flags &= ~RESET_VALUE);
0055   return *this;
0056 }
0057 
0058 
0059 /// Set flag to reset the entry's children to their ideal geometrical position
0060 GlobalAlignmentStack::StackEntry& GlobalAlignmentStack::StackEntry::setResetChildren(bool new_value)   {
0061   new_value ? (delta.flags |= RESET_CHILDREN) : (delta.flags &= ~RESET_CHILDREN);
0062   return *this;
0063 }
0064 
0065 
0066 /// Set flag to check overlaps
0067 GlobalAlignmentStack::StackEntry& GlobalAlignmentStack::StackEntry::setOverlapCheck(bool new_value)   {
0068   new_value ? (delta.flags |= CHECKOVL_DEFINED) : (delta.flags &= ~CHECKOVL_DEFINED);
0069   return *this;
0070 }
0071 
0072 
0073 /// Set the precision for the overlap check (otherwise the default is 0.001 cm)
0074 GlobalAlignmentStack::StackEntry& GlobalAlignmentStack::StackEntry::setOverlapPrecision(double precision)   {
0075   delta.flags |= CHECKOVL_DEFINED;
0076   delta.flags |= CHECKOVL_VALUE;
0077   overlap = precision;
0078   return *this;
0079 }
0080 #endif
0081 
0082 /// Default constructor
0083 GlobalAlignmentStack::GlobalAlignmentStack()
0084 {
0085   InstanceCount::increment(this);
0086 }
0087 
0088 /// Default destructor
0089 GlobalAlignmentStack::~GlobalAlignmentStack()   {
0090   detail::destroyObjects(m_stack);
0091   InstanceCount::decrement(this);
0092 }
0093 
0094 /// Static client accessor
0095 GlobalAlignmentStack& GlobalAlignmentStack::get()  {
0096   if ( _stack().get() ) return *_stack();
0097   except("GlobalAlignmentStack", "Stack not allocated -- may not be retrieved!");
0098   throw std::runtime_error("Stack not allocated");
0099 }
0100 
0101 /// Create an alignment stack instance. The creation of a second instance will be refused.
0102 void GlobalAlignmentStack::create()   {
0103   if ( _stack().get() )   {
0104     except("GlobalAlignmentStack", "Stack already allocated. Multiple copies are not allowed!");
0105   }
0106   _stack(new GlobalAlignmentStack());
0107 }
0108 
0109 /// Check existence of alignment stack
0110 bool GlobalAlignmentStack::exists()   {
0111   return _stack().get() != 0;
0112 }
0113 
0114 /// Clear data content and remove the slignment stack
0115 void GlobalAlignmentStack::release()    {
0116   if ( _stack().get() )  {
0117     _stack(0);
0118     return;
0119   }
0120   except("GlobalAlignmentStack", "Attempt to delete non existing stack.");
0121 }
0122 
0123 /// Add a new entry to the cache. The key is the placement path
0124 bool GlobalAlignmentStack::insert(const std::string& full_path, dd4hep_ptr<StackEntry>& entry)  {
0125   if ( entry.get() && !full_path.empty() )  {
0126     entry->path = full_path;
0127     return add(entry);
0128   }
0129   except("GlobalAlignmentStack", "Attempt to apply an invalid alignment entry.");
0130   return false;
0131 }
0132 
0133 /// Add a new entry to the cache. The key is the placement path
0134 bool GlobalAlignmentStack::insert(dd4hep_ptr<StackEntry>& entry)  {
0135   return add(entry);
0136 }
0137 
0138 /// Add a new entry to the cache. The key is the placement path
0139 bool GlobalAlignmentStack::add(dd4hep_ptr<StackEntry>& entry)  {
0140   if ( entry.get() && !entry->path.empty() )  {
0141     Stack::const_iterator i = m_stack.find(entry->path);
0142     if ( i == m_stack.end() )   {
0143       StackEntry* e = entry.get();
0144       // Need to make some checks BEFORE insertion
0145       if ( !e->detector.isValid() )   {
0146         except("GlobalAlignmentStack", "Invalid alignment entry [No such detector]");
0147       }
0148       printout(INFO,"GlobalAlignmentStack","Add node:%s",e->path.c_str());
0149       m_stack.emplace(e->path,entry.release());
0150       return true;
0151     }
0152     except("GlobalAlignmentStack", "The entry with path "+entry->path+
0153            " cannot be re-aligned twice in one transaction.");
0154   }
0155   except("GlobalAlignmentStack", "Attempt to apply an invalid alignment entry.");
0156   return false;
0157 }
0158 
0159 /// Retrieve an alignment entry of the current stack
0160 dd4hep::dd4hep_ptr<GlobalAlignmentStack::StackEntry> GlobalAlignmentStack::pop()   {
0161   Stack::iterator i = m_stack.begin();
0162   if ( i != m_stack.end() )   {
0163     dd4hep_ptr<StackEntry> e((*i).second);
0164     m_stack.erase(i);
0165     return e;
0166   }
0167   except("GlobalAlignmentStack", "Alignment stack is empty. "
0168          "Cannot pop entries - check size first!");
0169   return {};
0170 }
0171 
0172 /// Get all pathes to be aligned
0173 std::vector<const GlobalAlignmentStack::StackEntry*> GlobalAlignmentStack::entries() const    {
0174   std::vector<const StackEntry*> result;
0175   result.reserve(m_stack.size());
0176   transform(begin(m_stack),end(m_stack),back_inserter(result),detail::select2nd(m_stack));
0177   return result;
0178 }
0179