DD4hep - The AIDA detector description toolkit for high energy physics experiments
DD4hep  Rev:Unversioneddirectory
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ConditionsTest.cpp
Go to the documentation of this file.
1 // $Id$
2 //==========================================================================
3 // AIDA Detector description implementation for LCD
4 //--------------------------------------------------------------------------
5 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
6 // All rights reserved.
7 //
8 // For the licensing terms see $DD4hepINSTALL/LICENSE.
9 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
10 //
11 // Author : M.Frank
12 //
13 //==========================================================================
14 
15 // Framework include files
16 #include "DD4hep/DetConditions.h"
17 #include "DD4hep/DetectorTools.h"
18 #include "DDCond/ConditionsTest.h"
21 
22 // C/C++ include files
23 #include <vector>
24 #include <list>
25 #include <set>
26 #include <map>
27 #include <deque>
28 
29 using namespace std;
30 using namespace DD4hep::Conditions;
31 
33 namespace DD4hep {
34 
36  namespace Conditions {
37 
39  namespace Test {
40 
41 
42  template <typename T> void print_bound_condition(Condition /* c */, const char* /* norm */) {}
43 
44 
45  template<typename T> void __print_bound_val(Condition c, const char* norm) {
46  char text_format[1024];
47  const T& value = access_val<T>(c);
48  if ( norm ) {
49  T val = _multiply(c.get<T>(),norm);
50  ::snprintf(text_format,sizeof(text_format)," Bound value %%s : value:%s [%s] Type: %%s",
52  printout(INFO,"Cond_Value",text_format, c.name(), value, val, typeName(c.typeInfo()).c_str());
53  return;
54  }
55  ::snprintf(text_format,sizeof(text_format)," Bound value %%s : value:%s Type: %%s",
57  printout(INFO,"Cond_Value",text_format, c.name(), value, typeName(c.typeInfo()).c_str());
58  }
59  template <> void __print_bound_val<string>(Condition c, const char*) {
60  const string& v = access_val<string>(c);
61  printout(INFO,"Cond_Value"," Bound value %s : string value:%s Type: %s Ptr:%016X",
62  c.name(), c.get<string>().c_str(),typeName(c.typeInfo()).c_str(),
63  (void*)&v);
64  }
65  template <typename T> void __print_bound_container(Condition c, const char*) {
66  const T& v = access_val<T>(c);
67  printout(INFO,"Cond_Value"," Bound value %s : size:%d = %s Type: %s Ptr:%016X",
68  c.name(), int(v.size()), c.data().str().c_str(),
69  typeName(c.typeInfo()).c_str(), (void*)&v);
70  }
71 
72 
73 #define TEMPLATE_SIMPLE_TYPE(x) \
74  template <> void print_bound_value<x>(Condition c, const char* norm) \
75  { __print_bound_val<x>(c, norm); }
76 
77 #define TEMPLATE_CONTAINER_TYPE(container,x) \
78  template void __print_bound_container<container >(Condition c, const char* norm); \
79  template <> void print_bound_value<container >(Condition c, const char* norm) \
80  { __print_bound_container<container >(c, norm); }
81 
82 #if defined(DD4HEP_HAVE_ALL_PARSERS)
83 #define TEMPLATE_TYPE(x,f) \
84  TEMPLATE_SIMPLE_TYPE(x) \
85  TEMPLATE_CONTAINER_TYPE(vector<x>,x) \
86  TEMPLATE_CONTAINER_TYPE(list<x>,x) \
87  TEMPLATE_CONTAINER_TYPE(set<x>,x) \
88  TEMPLATE_CONTAINER_TYPE(Primitive<x>::int_map_t,x) \
89  TEMPLATE_CONTAINER_TYPE(Primitive<x>::size_map_t,x) \
90  TEMPLATE_CONTAINER_TYPE(Primitive<x>::string_map_t,x)
91 
92  TEMPLATE_TYPE(char,"%c")
93  TEMPLATE_TYPE(unsigned char,"%02X")
94  TEMPLATE_TYPE(short,"%d")
95  TEMPLATE_TYPE(unsigned short,"%04X")
96  TEMPLATE_TYPE(unsigned int,"%08X")
97  TEMPLATE_TYPE(unsigned long,"%016X")
98 #else
99 #define TEMPLATE_TYPE(x,f) \
100  TEMPLATE_SIMPLE_TYPE(x) \
101  TEMPLATE_CONTAINER_TYPE(vector<x>,x) \
102  TEMPLATE_CONTAINER_TYPE(list<x>,x) \
103  TEMPLATE_CONTAINER_TYPE(set<x>,x) \
104  TEMPLATE_CONTAINER_TYPE(Primitive<x>::int_map_t,x) \
105  TEMPLATE_CONTAINER_TYPE(Primitive<x>::string_map_t,x)
106 #endif // DD4HEP_HAVE_ALL_PARSERS
107 
108  TEMPLATE_TYPE(int,"%d")
109  TEMPLATE_TYPE(long,"%ld")
110  TEMPLATE_TYPE(float,"%f")
111  TEMPLATE_TYPE(double,"%g")
112  TEMPLATE_TYPE(std::string,"%c")
113 
114  template <> void print_condition<void>(Condition c) {
115  string type = c.type();
116  printout(INFO,"Cond_Value","%-32s [%16s] : %s [%s] ",
117  c.name(), c.type().c_str(),
118  c.value().c_str(), c->validity.c_str());
119  if ( type == "alignment" )
120  print_bound_value<string>(c);
121  else if ( type == "temperature" )
122  print_bound_value<double>(c);
123  else if ( type == "pressure" )
124  print_bound_value<double>(c,"1.0/hPa");
125  else if ( type == "whatever" )
126  print_bound_value<vector<double> >(c);
127  else if ( type == "integer" )
128  print_bound_value<int>(c);
129  else if ( type == "long" )
130  print_bound_value<long>(c);
131 #if defined(DD4HEP_HAVE_ALL_PARSERS)
132  else if ( type == "short" )
133  print_bound_value<short>(c);
134  else if ( type == "unsigned short" )
135  print_bound_value<unsigned short>(c);
136  else if ( type == "unsigned integer" )
137  print_bound_value<unsigned int>(c);
138  else if ( type == "unsigned long" )
139  print_bound_value<unsigned long>(c);
140 #endif
141  else if ( type == "float" )
142  print_bound_value<float>(c);
143  else if ( type == "double" )
144  print_bound_value<double>(c);
145  else if ( type == "vector<double>" )
146  print_bound_value<vector<double> >(c);
147  }
148 
149  template <typename T> void print_conditions(const RangeConditions& rc) {
150  for(RangeConditions::const_iterator k=rc.begin(); k != rc.end(); ++k)
151  print_condition<T>(*k);
152  }
153 
154  template void print_conditions<void>(const RangeConditions& rc);
155 
156 
157  void check_discrete_condition(Condition c, const IOV& iov) {
158  const IOV* i = c->iov;
159  if ( i->is_discrete() ) {
160  if ( iov.contains(*i) ) {
161  return;
162  }
163  except("Example", "+++ The condition %s [%s] is not fully contained in iov:%s",
164  c->name.c_str(), i->str().c_str(), iov.str().c_str());
165  }
166  except("Example", "+++ The condition %s [%s] has no discrete type matching iov:%s",
167  c->name.c_str(), i->str().c_str(), iov.str().c_str());
168  }
169  }
170  }
171 }
172 
173 Test::TestEnv::TestEnv(LCDD& _lcdd, const string& detector_name)
174  : lcdd(_lcdd), detector(), manager()
175 {
177  manager["LoaderType"] = "multi";
178  manager["PoolType"] = "DD4hep_ConditionsLinearPool";
179  manager["UpdatePoolType"] = "DD4hep_ConditionsLinearUpdatePool";
180  manager["UserPoolType"] = "DD4hep_ConditionsLinearUserPool";
182  detector = lcdd.detector(detector_name);
183  if ( detector.isValid() ) {
184  pair<bool, const IOVType*> e = manager.registerIOVType(0, "epoch");
185  pair<bool, const IOVType*> r = manager.registerIOVType(1, "run");
186  loader = manager.loader();
187  if ( e.first && r.first ) {
188  epoch = e.second;
189  run = r.second;
190  return;
191  }
192  invalidHandleError<IOVType>();
193  }
194  invalidHandleError<DetElement>();
195 }
196 
198 DetElement Test::TestEnv::daughter(const string& sub_path) const {
200 }
201 
202 void Test::TestEnv::add_xml_data_source(const string& file, const string& iov_str) {
203  string source = "xml:"+string(::getenv("DD4hepINSTALL"))+file;
204  IOV iov(0);
205  this->manager->fromString(iov_str, iov);
206  this->loader->addSource(source, iov);
207 }
208 
210 {
211  typedef RangeConditions _R;
214  int cnt = 0;
215  const _P& p = manager->conditionsPool();
216  for(_P::const_iterator i=p.begin(); i != p.end(); ++i, ++cnt) {
217  const ConditionsIOVPool* pool = (*i);
218  if ( pool ) {
219  const _E& e = pool->elements;
220  const IOVType* typ = this->manager->iovType(cnt);
221  printout(INFO,"Example","+++ ConditionsIOVPool for type %s", typ->str().c_str());
222  for (_E::const_iterator j=e.begin(); j != e.end(); ++j) {
223  _R rc;
224  ConditionsPool* cp = (*j).second;
225  cp->select_all(rc);
226  printout(INFO,"Example","+++ Conditions for pool with IOV: %s age:%d",
227  cp->iov->str().c_str(), cp->age_value);
229  }
230  }
231  }
232  printout(INFO,"Example","SUCCESS: +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
233  printout(INFO,"Example","SUCCESS: +++ Conditions pools successfully dumped");
234  printout(INFO,"Example","SUCCESS: +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
235 }
236 
239 {
240  DetConditions c(elt);
241  Container conds = c.conditions();
242  printout(INFO,"conditions","DetElement:%s # of conditons keys:%d",elt.path().c_str(),int(conds.numKeys()));
243 #if 0
244  const Container::Elements& elements = conds.elements();
245  for(Container::Elements::const_iterator i=elements.begin(); i!=elements.end(); ++i) {
246  Condition cond((*i).second);
247  print_condition<void>(cond);
248  }
249 #endif
250 }
251 
254 {
255  const DetElement::Children& children = elt.children();
256  if ( !elt->conditions.isValid() )
257  printout(INFO,"conditions_tree","DetElement:%s NO CONDITIONS present",elt.path().c_str());
258  else
259  dump_detector_element(elt);
260  for(DetElement::Children::const_iterator j=children.begin(); j!=children.end(); ++j)
261  dump_conditions_tree((*j).second);
262 }
std::string str() const
Conversion to string.
Definition: IOV.cpp:36
Container conditions() const
Access to the conditions information.
OpaqueData & data() const
Access the IOV block.
Definition: Conditions.cpp:92
virtual void select_all(RangeConditions &result)=0
Select all conditions contained.
const char * name() const
Access the object name (or "" if not supported by the object)
Definition: Handle.inl:36
T & get()
Generic getter. Specify the exact type, not a polymorph type.
void print_condition< void >(Condition c)
std::string typeName(const std::type_info &type)
ABI information about type names.
Definition: Primitives.cpp:186
bool contains(const IOV &iov) const
Check for validity containment.
Definition: IOV.cpp:156
Main condition object handle.
Definition: Conditions.h:70
DetElement daughter(const std::string &sub_path) const
Find daughter element of the detector object.
Elements elements
Container of IOV dependent conditions pools.
Container class for condition handles aggregated by a detector element.
Definition: Conditions.h:206
#define _E(x)
Definition: XMLElements.cpp:46
T _multiply(const std::string &left, T right)
Generic multiplication using the evaluator: result = left * right.
template void print_conditions< void >(const RangeConditions &rc)
return e
Definition: Volumes.cpp:297
ConditionsManager & initialize()
Initialize the object after having set the properties.
int except(const std::string &src, const std::string &fmt,...)
Calls the display action with ERROR and throws an std::runtime_error exception.
Definition: Printout.cpp:217
void print_conditions(const RangeConditions &rc)
DD4hep::Geometry::DetElement DetElement
std::vector< ConditionsIOVPool * > TypedConditionPool
std::pair< bool, const IOVType * > registerIOVType(size_t iov_type, const std::string &iov_name)
Register new IOV type if it does not (yet) exist.
Class describing the interval of validty.
Definition: IOV.h:59
std::string str() const
Create string representation of the data block.
Definition: OpaqueData.cpp:58
DetElement findDaughterElement(DetElement parent, const std::string &subpath)
Find DetElement as child of a parent by it's relative or absolute path.
void print_condition(Condition condition)
void print_bound_condition(Condition, const char *)
const std::type_info & typeInfo() const
Access to the type information.
Definition: Conditions.cpp:127
std::map< IOV::Key, Element > Elements
IOV * iov
The IOV of the conditions hosted.
Class describing the interval of validty type.
Definition: IOV.h:33
void check_discrete_condition(Condition c, const IOV &iov)
Handle< Loader > loader() const
Access the conditions loader.
Class implementing the conditions collection for a given IOV type.
A bit of support for printing primitives.
Definition: Primitives.h:102
static const double g
Definition: DD4hepUnits.h:162
Handle class describing the access to DetElement dependent conditions.
Definition: DetConditions.h:43
const iov_type & iov() const
Access the IOV block.
Definition: Conditions.cpp:102
static ConditionsManager from(T &host)
Static accessor if installed as an extension.
#define TEMPLATE_TYPE(x, f)
Pool of conditions satisfying one IOV type (epoch, run, fill, etc)
View * v
Definition: MultiView.cpp:30
static void dump_conditions_tree(DetElement elt)
Dump conditions tree of a detector element.
void __print_bound_val(Condition c, const char *norm)
std::vector< Condition > RangeConditions
Definition: Conditions.h:359
std::map< std::string, DetElement > Children
Definition: Detector.h:202
int printout(PrintLevel severity, const char *src, const char *fmt,...)
Calls the display action with a given severity level.
Definition: Printout.cpp:111
bool is_discrete() const
Check if the IOV corresponds to a range.
Definition: IOV.h:92
static void dump_detector_element(DetElement elt)
Dump the conditions of one detectpr element.
void __print_bound_val< string >(Condition c, const char *)
void __print_bound_container(Condition c, const char *)
size_t numKeys() const
Access the number of conditons keys available for this detector element.
Definition: Conditions.cpp:171
void add_xml_data_source(const std::string &file, const std::string &iov)
Handle< ConditionsDataLoader > loader
std::string str() const
Create string representation of the IOV.
Definition: IOV.cpp:126