DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
Conditions.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 includes
15 #include <DD4hep/Printout.h>
17 
18 // C/C++ include files
19 #include <climits>
20 #include <iomanip>
21 #include <cstdio>
22 
23 using namespace dd4hep;
24 
25 namespace {
26  int s_have_inventory = 0;
27  struct KeyTracer {
28  std::map<Condition::itemkey_type,std::string> item_names;
29  std::mutex lock;
30  void add(Condition::itemkey_type key,const std::string& item) {
31  if ( s_have_inventory > 0 ) {
32  std::lock_guard<std::mutex> protect(lock);
33  item_names.emplace(key, item);
34  }
35  }
36  std::string get(Condition::itemkey_type key) const {
37  auto i = item_names.find(key);
38  if( i != item_names.end() ) {
39  return (*i).second;
40  }
41  char text[32];
42  std::snprintf(text,sizeof(text),"%08X",key);
43  return text;
44  }
45  } s_key_tracer;
46 }
47 
50  if ( value >= 0 ) {
51  s_have_inventory = value;
52  }
53  return s_have_inventory;
54 }
55 
57  return s_key_tracer.get(ConditionKey::KeyMaker(key).values.item_key);
58 }
59 
61  return s_key_tracer.get(key);
62 }
63 
66 {
67  if ( hash_key != 0 && hash_key != ~0x0ULL ) {
68  Object* o = new Object();
69  assign(o,"","");
70  o->hash = hash_key;
71  return;
72  }
73  except("Condition","+++ Cannot create a condition with an invalid key: %016llX",hash_key);
74 }
75 
77 Condition::Condition(const std::string& nam, const std::string& typ) : Handle<Object>()
78 {
79  Object* o = new Object();
80  assign(o,nam,typ);
81  o->hash = 0;
82 }
83 
85 Condition::Condition(const std::string& nam,const std::string& typ, size_t memory)
86  : Handle<Object>()
87 {
88  void* ptr = ::operator new(sizeof(Object)+memory);
89  Object* o = new(ptr) Object();
90  assign(o,nam,typ);
91  o->hash = 0;
92 }
93 
95 std::string Condition::str(int flags) const {
96  std::stringstream output;
97  Object* o = access();
98 #if defined(DD4HEP_CONDITIONS_HAVE_NAME)
99  if ( 0 == (flags&NO_NAME) )
100  output << std::setw(16) << std::left << o->name;
101 #endif
102  if ( flags&WITH_IOV ) {
103  const IOV* ptr_iov = o->iovData();
104  output << " " << (ptr_iov ? ptr_iov->str().c_str() : "IOV:[UNKNOWN]");
105  }
106 #if !defined(DD4HEP_MINIMAL_CONDITIONS)
107  if ( flags&WITH_TYPE )
108  output << " (" << o->type << ")";
109 #endif
110  if ( flags&WITH_DATATYPE )
111  output << " -> " << o->data.dataType();
112  if ( flags&WITH_DATA )
113  output << " Data:" << o->data.str();
114 #if !defined(DD4HEP_MINIMAL_CONDITIONS)
115  if ( flags&WITH_ADDRESS )
116  output << " " << o->address;
117  if ( flags&WITH_COMMENT )
118  output << " \"" << o->comment << "\"";
119 #endif
120  return output.str();
121 }
122 
124 int Condition::dataType() const {
125  return access()->data.type;
126 }
127 
130  return access()->data;
131 }
132 
135  return *(access()->iovType());
136 }
137 
139 const dd4hep::IOV& Condition::iov() const {
140  return *(access()->iovData());
141 }
142 
143 #if !defined(DD4HEP_MINIMAL_CONDITIONS)
144 const std::string& Condition::type() const {
146  return access()->type;
147 }
148 
150 const std::string& Condition::value() const {
151  return access()->value;
152 }
153 
155 const std::string& Condition::comment() const {
156  return access()->comment;
157 }
158 
160 const std::string& Condition::address() const {
161  return access()->address;
162 }
163 #endif
164 
166 const std::type_info& Condition::typeInfo() const {
167  return descriptor().type();
168 }
169 
172  return access()->hash;
173 }
174 
178 }
179 
183 }
184 
187  return access()->flags;
188 }
189 
192  access()->setFlag(option);
193 }
194 
197  access()->unFlag(option);
198 }
199 
201 bool Condition::testFlag(mask_type option) const {
202  return access()->testFlag(option);
203 }
204 
207  const BasicGrammar* grammar = access()->data.grammar;
208  if ( !grammar ) {
209  invalidHandleError<Condition>();
210  // This code is never reached, since function above throws exception!
211  // Needed to satisfay CppCheck
212  throw std::runtime_error("Null pointer in Grammar object");
213  }
214  return *grammar;
215 }
216 
219 }
220 
222 ConditionKey::KeyMaker::KeyMaker(DetElement detector, const std::string& value) {
223  KeyMaker key_maker(detector.key(), detail::hash32(value));
224  hash = key_maker.hash;
225  s_key_tracer.add(key_maker.values.item_key, value);
226 }
227 
230  hash = KeyMaker(detector.key(), item_key).hash;
231 }
232 
234 ConditionKey::KeyMaker::KeyMaker(Condition::detkey_type det_key, const std::string& value) {
235  KeyMaker key_maker(det_key, detail::hash32(value));
236  hash = key_maker.hash;
237  s_key_tracer.add(key_maker.values.item_key, value);
238 }
239 
241 ConditionKey::ConditionKey(DetElement detector, const std::string& value) {
242  KeyMaker key_maker(detector.key(), value);
243  hash = key_maker.hash;
244  s_key_tracer.add(key_maker.values.item_key, value);
245 #ifdef DD4HEP_CONDITIONS_HAVE_NAME
246  name = detector.path()+"#"+value;
247 #endif
248 }
249 
251 ConditionKey::ConditionKey(Condition::detkey_type det_key, const std::string& value) {
252  KeyMaker key_maker(det_key, value);
253  s_key_tracer.add(key_maker.values.item_key, value);
254  hash = key_maker.hash;
255 #ifdef DD4HEP_CONDITIONS_HAVE_NAME
256  char text[32];
257  ::snprintf(text,sizeof(text),"%08X#",det_key);
258  name = text+value;
259 #endif
260 }
261 
264  hash = KeyMaker(detector.key(),item_key).hash;
265 #ifdef DD4HEP_CONDITIONS_HAVE_NAME
266  char text[32];
267  ::snprintf(text,sizeof(text),"#%08X",item_key);
268  name = detector.path()+text;
269 #endif
270 }
271 
274  KeyMaker key_maker(detector.key(), value);
275  s_key_tracer.add(key_maker.values.item_key, value);
276  return key_maker.hash;
277 }
278 
280 Condition::key_type ConditionKey::hashCode(DetElement detector, const std::string& value) {
281  KeyMaker key_maker(detector.key(), value);
282  s_key_tracer.add(key_maker.values.item_key, value);
283  return key_maker.hash;
284 }
285 
288  Condition::itemkey_type code = detail::hash32(value);
289  s_key_tracer.add(code, value);
290  return code;
291 }
292 
295  Condition::itemkey_type code = detail::hash32(value);
296  s_key_tracer.add(code, value);
297  return code;
298 }
299 
301 std::string ConditionKey::toString() const {
303  char text[64];
304  ::snprintf(text,sizeof(text),"%08X-%08X",key.values.det_key, key.values.item_key);
305 #if defined(DD4HEP_CONDITIONS_HAVE_NAME)
306  if ( !name.empty() ) {
307  std::stringstream str;
308  str << "(" << name << ") " << text;
309  return str.str();
310  }
311 #endif
312  return text;
313 }
314 
dd4hep::detail::ConditionObject::value
std::string value
Condition value (in string form)
Definition: ConditionsInterna.h:71
dd4hep::Condition::dataType
int dataType() const
Access the data type.
Definition: Conditions.cpp:124
dd4hep::DetElement::path
const std::string & path() const
Path of the detector element (not necessarily identical to placement path!)
Definition: DetElement.cpp:158
dd4hep::Condition::value
const std::string & value() const
Access the value field of the condition as a string.
Definition: Conditions.cpp:150
dd4hep::ConditionKey::hashCode
static Condition::key_type hashCode(DetElement detector, const char *value)
Hash code generation from input string.
Definition: Conditions.cpp:273
dd4hep::ConditionsSelect::~ConditionsSelect
virtual ~ConditionsSelect()
Default destructor.
Definition: Conditions.cpp:218
dd4hep::detail::have_condition_item_inventory
int have_condition_item_inventory(int value=-1)
Setup conditions item name inventory for debugging.
Definition: Conditions.cpp:49
dd4hep::Condition::NO_NAME
@ NO_NAME
Definition: Conditions.h:71
dd4hep::DetElement::key
unsigned int key() const
Access hash key of this detector element (Only valid once geometry is closed!)
Definition: DetElement.cpp:134
dd4hep::Condition::typeInfo
const std::type_info & typeInfo() const
Access to the type information.
Definition: Conditions.cpp:166
dd4hep::Condition::flags
mask_type flags() const
Flag operations: Get condition flags.
Definition: Conditions.cpp:186
dd4hep::detail::ConditionObject::hash
Condition::key_type hash
Hash value of the name.
Definition: ConditionsInterna.h:85
dd4hep::Condition::WITH_DATA
@ WITH_DATA
Definition: Conditions.h:70
dd4hep::Handle< detail::ConditionObject >::Object
detail::ConditionObject Object
Extern accessible definition of the contained element type.
Definition: Handle.h:88
ConditionsInterna.h
dd4hep::Condition::testFlag
bool testFlag(mask_type option) const
Flag operations: Test for a given a conditons flag.
Definition: Conditions.cpp:201
dd4hep::Condition::comment
const std::string & comment() const
Access the comment field of the condition.
Definition: Conditions.cpp:155
dd4hep::OpaqueDataBlock::type
unsigned int type
Data buffer type: Must be a bitmap of enum _DataTypes!
Definition: OpaqueData.h:119
dd4hep::ConditionKey::hash
Condition::key_type hash
Hashed key representation.
Definition: Conditions.h:287
dd4hep::ConditionKey::ConditionKey
ConditionKey()=default
Default constructor.
dd4hep::detail::ConditionObject::flags
Condition::mask_type flags
Flags.
Definition: ConditionsInterna.h:87
dd4hep::OpaqueDataBlock
Class describing an opaque conditions data block.
Definition: OpaqueData.h:92
dd4hep::Handle
Handle: a templated class like a shared pointer, which allows specialized access to tgeometry objects...
Definition: Handle.h:84
dd4hep::IOV::str
std::string str() const
Create string representation of the IOV.
Definition: IOV.cpp:141
dd4hep::Condition::key
key_type key() const
Hash identifier.
Definition: Conditions.cpp:171
dd4hep::detail::ConditionObject::address
std::string address
Condition address.
Definition: ConditionsInterna.h:76
dd4hep::ConditionKey::toString
std::string toString() const
Conversion to string.
Definition: Conditions.cpp:301
dd4hep::ConditionKey::KeyMaker
Helper union to interprete conditions keys.
Definition: Conditions.h:295
dd4hep::IOVType
Class describing the interval of validty type.
Definition: IOV.h:37
dd4hep::Condition::detector_key
detkey_type detector_key() const
DetElement part of the identifier.
Definition: Conditions.cpp:176
dd4hep::detail::ConditionObject::unFlag
void unFlag(Condition::mask_type option)
Flag operations: UN-Set a conditons flag.
Definition: ConditionsInterna.h:124
dd4hep::Condition::WITH_ADDRESS
@ WITH_ADDRESS
Definition: Conditions.h:66
dd4hep::Condition::detkey_type
unsigned int detkey_type
High part of the key identifies the detector element.
Definition: Conditions.h:56
dd4hep::IOV
Class describing the interval of validty.
Definition: IOV.h:67
dd4hep::DetElement
Handle class describing a detector element.
Definition: DetElement.h:188
dd4hep::BasicGrammar
Base class describing string evaluation to C++ objects using boost::spirit.
Definition: Grammar.h:56
dd4hep::Handle< detail::ConditionObject >::assign
void assign(Object *n, const std::string &nam, const std::string &title)
Assign a new named object. Note: object references must be managed by the user.
dd4hep::detail::ConditionObject::iovType
const IOVType * iovType() const
Access safely the IOV-type.
Definition: ConditionsInterna.cpp:92
dd4hep::ConditionKey::itemCode
static Condition::itemkey_type itemCode(const char *value)
32 bit hashcode of the item
Definition: Conditions.cpp:287
dd4hep::Condition::item_key
itemkey_type item_key() const
Item part of the identifier.
Definition: Conditions.cpp:181
dd4hep::Condition::WITH_COMMENT
@ WITH_COMMENT
Definition: Conditions.h:68
dd4hep::Condition::WITH_IOV
@ WITH_IOV
Definition: Conditions.h:65
dd4hep::ConditionKey::KeyMaker::KeyMaker
KeyMaker()
Definition: Conditions.h:305
dd4hep::Condition::WITH_DATATYPE
@ WITH_DATATYPE
Definition: Conditions.h:69
dd4hep::Condition::WITH_TYPE
@ WITH_TYPE
Definition: Conditions.h:67
dd4hep::Condition::str
std::string str(int with_data=WITH_IOV|WITH_ADDRESS|WITH_DATATYPE) const
Output method.
Definition: Conditions.cpp:95
dd4hep::detail::ConditionObject::comment
std::string comment
Comment string.
Definition: ConditionsInterna.h:78
dd4hep::Condition::itemkey_type
unsigned int itemkey_type
Low part of the key identifies the item identifier.
Definition: Conditions.h:58
dd4hep::Condition::unFlag
void unFlag(mask_type option)
Flag operations: UN-Set a conditons flag.
Definition: Conditions.cpp:196
dd4hep::detail::ConditionObject::setFlag
void setFlag(Condition::mask_type option)
Flag operations: Set a conditons flag.
Definition: ConditionsInterna.h:122
dd4hep::Condition::key_type
unsigned long long int key_type
Forward definition of the key type.
Definition: Conditions.h:54
dd4hep::detail::ConditionObject::iovData
const IOV * iovData() const
Access safely the IOV.
Definition: ConditionsInterna.cpp:85
dd4hep::Condition::mask_type
unsigned int mask_type
Forward definition of the object properties.
Definition: Conditions.h:60
dd4hep::Condition::type
const std::string & type() const
Access the type field of the condition.
Definition: Conditions.cpp:145
dd4hep::detail::ConditionObject
The data class behind a conditions handle.
Definition: ConditionsInterna.h:68
dd4hep::BasicGrammar::type
virtual const std::type_info & type() const =0
Access to the type information.
dd4hep::Condition::setFlag
void setFlag(mask_type option)
Flag operations: Set a conditons flag.
Definition: Conditions.cpp:191
key
unsigned char key
Definition: AlignmentsCalculator.cpp:69
dd4hep::Handle< detail::ConditionObject >::access
detail::ConditionObject * access() const
Checked object access. Throws invalid handle runtime exception if invalid handle.
dd4hep::Handle< detail::ConditionObject >::ptr
detail::ConditionObject * ptr() const
Access to the held object.
Definition: Handle.h:153
dd4hep::ConditionKey::KeyMaker::det_key
Condition::detkey_type det_key
Definition: Conditions.h:303
dd4hep::detail::get_condition_item_name
std::string get_condition_item_name(Condition::itemkey_type key)
Resolve key from conditions item name inventory for debugging.
Definition: Conditions.cpp:60
dd4hep
Namespace for the AIDA detector description toolkit.
Definition: AlignmentsCalib.h:28
dd4hep::ConditionKey::KeyMaker::hash
Condition::key_type hash
Definition: Conditions.h:296
dd4hep::ConditionKey::item_key
Condition::itemkey_type item_key() const
Access the detector element part of the key.
Definition: Conditions.h:355
dd4hep::sim::hash
unsigned int hash(unsigned int initialSeed, unsigned int eventNumber, unsigned int runNumber)
calculate hash from initialSeed, eventID and runID
Definition: Geant4EventSeed.h:201
dd4hep::Condition::Condition
Condition()=default
Default constructor.
dd4hep::detail::ConditionObject::data
OpaqueDataBlock data
Data block.
Definition: ConditionsInterna.h:81
dd4hep::OpaqueData::grammar
const BasicGrammar * grammar
Data type.
Definition: OpaqueData.h:53
dd4hep::Condition::iovType
const IOVType & iovType() const
Access the IOV type.
Definition: Conditions.cpp:134
dd4hep::OpaqueData::dataType
const std::string & dataType() const
Access type name of the condition data block.
Definition: OpaqueData.cpp:50
dd4hep::OpaqueData::str
std::string str() const
Create string representation of the data block.
Definition: OpaqueData.cpp:34
dd4hep::ConditionKey::KeyMaker::values
struct dd4hep::ConditionKey::KeyMaker::@2 values
dd4hep::Condition::descriptor
const BasicGrammar & descriptor() const
Access to the grammar type.
Definition: Conditions.cpp:206
dd4hep::detail::ConditionObject::testFlag
bool testFlag(Condition::mask_type option) const
Flag operations: Test for a given a conditons flag.
Definition: ConditionsInterna.h:126
dd4hep::Condition::address
const std::string & address() const
Access the address string [e.g. database identifier].
Definition: Conditions.cpp:160
Printout.h
dd4hep::Condition::iov
const IOV & iov() const
Access the IOV block.
Definition: Conditions.cpp:139
dd4hep::Condition::data
OpaqueDataBlock & data() const
Access the opaque data block.
Definition: Conditions.cpp:129
dd4hep::ConditionKey::KeyMaker::item_key
Condition::itemkey_type item_key
Definition: Conditions.h:302