Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:13:52

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 includes
0015 #include <DD4hep/Printout.h>
0016 #include <DD4hep/detail/ConditionsInterna.h>
0017 
0018 // C/C++ include files
0019 #include <climits>
0020 #include <iomanip>
0021 #include <cstdio>
0022 
0023 using namespace dd4hep;
0024 
0025 namespace {
0026   int s_have_inventory = 0;
0027   struct KeyTracer    {
0028     std::map<Condition::itemkey_type,std::string> item_names;
0029     std::mutex lock;
0030     void add(Condition::itemkey_type key,const std::string& item)   {
0031       if ( s_have_inventory > 0 )   {
0032         std::lock_guard<std::mutex> protect(lock);
0033         item_names.emplace(key, item);
0034       }
0035     }
0036     std::string get(Condition::itemkey_type key)    const    {
0037       auto i = item_names.find(key);
0038       if( i != item_names.end() )  {
0039         return (*i).second;
0040       }
0041       char text[32];
0042       std::snprintf(text,sizeof(text),"%08X",key);
0043       return text;
0044     }
0045   } s_key_tracer;
0046 }
0047 
0048 /// Setup conditions item name inventory for debugging
0049 int dd4hep::detail::have_condition_item_inventory(int value)     {
0050   if ( value >= 0 )   {
0051     s_have_inventory = value;
0052   }
0053   return s_have_inventory;
0054 }
0055 
0056 std::string dd4hep::detail::get_condition_item_name(Condition::key_type key)    {
0057   return s_key_tracer.get(ConditionKey::KeyMaker(key).values.item_key);
0058 }
0059 
0060 std::string dd4hep::detail::get_condition_item_name(Condition::itemkey_type key)    {
0061   return s_key_tracer.get(key);
0062 }
0063 
0064 /// Initializing constructor for a pure, undecorated conditions object
0065 Condition::Condition(key_type hash_key) : Handle<Object>()
0066 {
0067   if ( hash_key != 0 && hash_key != ~0x0ULL )  {
0068     Object* o = new Object();
0069     assign(o,"","");
0070     o->hash = hash_key;
0071     return;
0072   }
0073   except("Condition","+++ Cannot create a condition with an invalid key: %016llX",hash_key);
0074 }
0075 
0076 /// Initializing constructor for a pure, undecorated conditions object
0077 Condition::Condition(const std::string& nam, const std::string& typ) : Handle<Object>()
0078 {
0079   Object* o = new Object();
0080   assign(o,nam,typ);
0081   o->hash = 0;
0082 }
0083 
0084 /// Initializing constructor for a pure, undecorated conditions object with payload buffer
0085 Condition::Condition(const std::string& nam,const std::string& typ, size_t memory)
0086   : Handle<Object>()
0087 {
0088   void* ptr = ::operator new(sizeof(Object)+memory);
0089   Object* o = new(ptr) Object();
0090   assign(o,nam,typ);
0091   o->hash = 0;
0092 }
0093 
0094 /// Output method
0095 std::string Condition::str(int flags)  const   {
0096   std::stringstream output;
0097   Object* o = access(); 
0098 #if defined(DD4HEP_CONDITIONS_HAVE_NAME)
0099   if ( 0 == (flags&NO_NAME) )
0100     output << std::setw(16) << std::left << o->name;
0101 #endif
0102   if ( flags&WITH_IOV )  {
0103     const IOV* ptr_iov = o->iovData();
0104     output << " " << (ptr_iov ? ptr_iov->str().c_str() : "IOV:[UNKNOWN]");
0105   }
0106 #if !defined(DD4HEP_MINIMAL_CONDITIONS)
0107   if ( flags&WITH_TYPE )
0108     output << " (" << o->type << ")";
0109 #endif
0110   if ( flags&WITH_DATATYPE )
0111     output << " -> " << o->data.dataType();
0112   if ( flags&WITH_DATA )
0113     output << " Data:" << o->data.str();
0114 #if !defined(DD4HEP_MINIMAL_CONDITIONS)
0115   if ( flags&WITH_ADDRESS )
0116     output << " " << o->address;
0117   if ( flags&WITH_COMMENT )
0118     output << " \"" << o->comment << "\"";
0119 #endif
0120   return output.str();
0121 }
0122 
0123 /// Access the data type
0124 int Condition::dataType() const   {
0125   return access()->data.type;
0126 }
0127 
0128 /// Access the IOV block
0129 dd4hep::OpaqueDataBlock& Condition::data() const   {
0130   return access()->data;
0131 }
0132 
0133 /// Access the IOV type
0134 const dd4hep::IOVType& Condition::iovType() const   {
0135   return *(access()->iovType());
0136 }
0137 
0138 /// Access the IOV block
0139 const dd4hep::IOV& Condition::iov() const   {
0140   return *(access()->iovData());
0141 }
0142 
0143 #if !defined(DD4HEP_MINIMAL_CONDITIONS)
0144 /// Access the type field of the condition
0145 const std::string& Condition::type()  const   {
0146   return access()->type;
0147 }
0148 
0149 /// Access the value field of the condition as a string
0150 const std::string& Condition::value()  const   {
0151   return access()->value;
0152 }
0153 
0154 /// Access the comment field of the condition
0155 const std::string& Condition::comment()  const   {
0156   return access()->comment;
0157 }
0158 
0159 /// Access the address string [e.g. database identifier]
0160 const std::string& Condition::address()  const   {
0161   return access()->address;
0162 }
0163 #endif
0164 
0165 /// Access to the type information
0166 const std::type_info& Condition::typeInfo() const   {
0167   return descriptor().type();
0168 }
0169 
0170 /// Hash identifier
0171 Condition::key_type Condition::key()  const    {
0172   return access()->hash;
0173 }
0174 
0175 /// DetElement part of the identifier
0176 Condition::detkey_type Condition::detector_key()  const   {
0177   return ConditionKey::KeyMaker(access()->hash).values.det_key;
0178 }
0179 
0180 /// Item part of the identifier
0181 Condition::itemkey_type Condition::item_key()  const   {
0182   return ConditionKey::KeyMaker(access()->hash).values.item_key;
0183 }
0184 
0185 /// Flag operations: Get flags
0186 Condition::mask_type Condition::flags()  const    {
0187   return access()->flags;
0188 }
0189 
0190 /// Flag operations: Set a conditons flag
0191 void Condition::setFlag(mask_type option)   {
0192   access()->setFlag(option);
0193 }
0194 
0195 /// Flag operations: UN-Set a conditons flag
0196 void Condition::unFlag(mask_type option)   {
0197   access()->unFlag(option);
0198 }
0199 
0200 /// Flag operations: Test for a given a conditons flag
0201 bool Condition::testFlag(mask_type option) const {
0202   return access()->testFlag(option);
0203 }
0204 
0205 /// Access to the grammar type
0206 const dd4hep::BasicGrammar& Condition::descriptor() const   {
0207   const BasicGrammar* grammar = access()->data.grammar;
0208   if ( !grammar ) {
0209     invalidHandleError<Condition>();
0210     // This code is never reached, since function above throws exception!
0211     // Needed to satisfay CppCheck
0212     throw std::runtime_error("Null pointer in Grammar object");
0213   }
0214   return *grammar;
0215 }
0216 
0217 /// Default destructor. 
0218 ConditionsSelect::~ConditionsSelect()   {
0219 }
0220 
0221 /// Constructor from string
0222 ConditionKey::KeyMaker::KeyMaker(DetElement detector, const std::string& value)   {
0223   KeyMaker key_maker(detector.key(), detail::hash32(value));
0224   hash = key_maker.hash;
0225   s_key_tracer.add(key_maker.values.item_key, value);
0226 }
0227 
0228 /// Constructor from detector element and item sub-key
0229 ConditionKey::KeyMaker::KeyMaker(DetElement detector, Condition::itemkey_type item_key)  {
0230   hash = KeyMaker(detector.key(), item_key).hash;
0231 }
0232 
0233 /// Constructor from string
0234 ConditionKey::KeyMaker::KeyMaker(Condition::detkey_type det_key, const std::string& value)   {
0235   KeyMaker key_maker(det_key, detail::hash32(value));
0236   hash = key_maker.hash;
0237   s_key_tracer.add(key_maker.values.item_key, value);
0238 }
0239 
0240 /// Constructor from string
0241 ConditionKey::ConditionKey(DetElement detector, const std::string& value)  {
0242   KeyMaker key_maker(detector.key(), value);
0243   hash = key_maker.hash;
0244   s_key_tracer.add(key_maker.values.item_key, value);
0245 #ifdef DD4HEP_CONDITIONS_HAVE_NAME
0246   name = detector.path()+"#"+value;
0247 #endif
0248 }
0249 
0250 /// Constructor from detector element key and item sub-key
0251 ConditionKey::ConditionKey(Condition::detkey_type det_key, const std::string& value)    {
0252   KeyMaker key_maker(det_key, value);
0253   s_key_tracer.add(key_maker.values.item_key, value);
0254   hash = key_maker.hash;
0255 #ifdef DD4HEP_CONDITIONS_HAVE_NAME
0256   char text[32];
0257   ::snprintf(text,sizeof(text),"%08X#",det_key);
0258   name = text+value;
0259 #endif
0260 }
0261 
0262 /// Constructor from detector element key and item sub-key
0263 ConditionKey::ConditionKey(DetElement detector, Condition::itemkey_type item_key)  {
0264   hash = KeyMaker(detector.key(),item_key).hash;
0265 #ifdef DD4HEP_CONDITIONS_HAVE_NAME
0266   char text[32];
0267   ::snprintf(text,sizeof(text),"#%08X",item_key);
0268   name = detector.path()+text;
0269 #endif
0270 }
0271 
0272 /// Hash code generation from input string
0273 Condition::key_type ConditionKey::hashCode(DetElement detector, const char* value)  {
0274   KeyMaker key_maker(detector.key(), value);
0275   s_key_tracer.add(key_maker.values.item_key, value);
0276   return key_maker.hash;
0277 }
0278 
0279 /// Hash code generation from input string
0280 Condition::key_type ConditionKey::hashCode(DetElement detector, const std::string& value)  {
0281   KeyMaker key_maker(detector.key(), value);
0282   s_key_tracer.add(key_maker.values.item_key, value);
0283   return key_maker.hash;
0284 }
0285 
0286 /// 32 bit hashcode of the item
0287 Condition::itemkey_type ConditionKey::itemCode(const char* value)  {
0288   Condition::itemkey_type code = detail::hash32(value);
0289   s_key_tracer.add(code, value);
0290   return code;
0291 }
0292 
0293 /// 32 bit hashcode of the item
0294 Condition::itemkey_type ConditionKey::itemCode(const std::string& value)   {
0295   Condition::itemkey_type code = detail::hash32(value);
0296   s_key_tracer.add(code, value);
0297   return code;
0298 }
0299 
0300 /// Conversion to string
0301 std::string ConditionKey::toString()  const    {
0302   dd4hep::ConditionKey::KeyMaker key(hash);
0303   char text[64];
0304   ::snprintf(text,sizeof(text),"%08X-%08X",key.values.det_key, key.values.item_key);
0305 #if defined(DD4HEP_CONDITIONS_HAVE_NAME)
0306   if ( !name.empty() )   {
0307     std::stringstream str;
0308     str << "(" << name << ") " << text;
0309     return str.str();
0310   }
0311 #endif
0312   return text;
0313 }
0314