DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
ConditionsTextRepository.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 include files
17 #include "DDCond/ConditionsTags.h"
18 #include "DD4hep/Printout.h"
20 #include "XML/DocumentHandler.h"
21 #include "XML/XMLTags.h"
22 
23 // C/C++ include files
24 #include <cstring>
25 #include <fstream>
26 #include <climits>
27 #include <cerrno>
28 #include <map>
29 
30 using namespace dd4hep::cond;
31 
36 
37 typedef std::map<dd4hep::Condition::key_type,dd4hep::Condition> AllConditions;
38 
41 }
42 
45 }
46 
47 namespace {
48 
49  int createXML(const std::string& output, const AllConditions& all) {
50  char text[32];
51  const char comment[] = "\n"
52  " +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n"
53  " ++++ Linear collider detector description Detector in C++ ++++\n"
54  " ++++ dd4hep Detector description generator. ++++\n"
55  " ++++ ++++\n"
56  " ++++ ++++\n"
57  " ++++ M.Frank CERN/LHCb ++++\n"
58  " +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n ";
60  xml_doc_t doc = docH.create("collection", comment);
61  xml_elt_t root = doc.root(), cond(0);
62  for( const auto& c : all ) {
63  ::snprintf(text,sizeof(text),"0x%16llX",c.second.key());
64  root.append(cond = xml_elt_t(doc, _U(ref)));
65  cond.setAttr(_U(key), text);
66  cond.setAttr(_U(name), c.second.name());
67 #if !defined(DD4HEP_MINIMAL_CONDITIONS)
68  cond.setAttr(_U(ref), c.second.address());
69 #endif
70  }
71  dd4hep::printout(dd4hep::ALWAYS,"ConditionsRepository","++ Handled %ld conditions.",all.size());
72  if ( !output.empty() ) {
73  return docH.output(doc, output);
74  }
75  return 1;
76  }
77 
79  int readXML(const std::string& input, ConditionsTextRepository::Data& data) {
80  struct Conv {
84  Conv(ConditionsTextRepository::Data& p) : data(p) {}
86  void operator()(xml_h element) const {
87  std::string key = element.attr<std::string>(_U(key));
88  size_t cap = data.capacity();
90  ::sscanf(key.c_str(),"0x%16llX",&e.key);
91  e.name = element.attr<std::string>(_U(name));
92  e.address = element.attr<std::string>(_U(ref));
93  if ( data.size() == cap ) data.reserve(cap+500);
94  data.emplace_back(e);
95  }
96  };
98  xml_h root = doc.root();
99  xml_coll_t(root, _U(ref)).for_each(Conv(data));
100  return 1;
101  }
102 
103 #if defined(DD4HEP_MINIMAL_CONDITIONS)
104  int createText(const std::string& output, const AllConditions&, char)
105 #else
106  int createText(const std::string& output, const AllConditions& all, char sep)
107 #endif
108  {
109  std::ofstream out(output);
110 #if !defined(DD4HEP_MINIMAL_CONDITIONS)
111  std::size_t siz_nam=0, siz_add=0, siz_tot=0;
112  char fmt[64], text[2*PATH_MAX+64];
113  if ( !out.good() ) {
114  dd4hep::except("ConditionsTextRepository",
115  "++ Failed to open output file:%s [errno:%d %s]",
116  output.c_str(), errno, ::strerror(errno));
117  }
118  else if ( sep ) {
119  std::snprintf(fmt,sizeof(fmt),"%%16llX%c%%s%c%%s%c",sep,sep,sep);
120  }
121  else {
122  for( const auto& i : all ) {
123  dd4hep::Condition::Object* c = i.second.ptr();
124  std::size_t siz_n = c->name.length();
125  std::size_t siz_a = c->address.length();
126  if ( siz_add < siz_a ) siz_add = siz_a;
127  if ( siz_nam < siz_n ) siz_nam = siz_n;
128  if ( siz_tot < (siz_n+siz_a) ) siz_tot = siz_n+siz_a;
129  }
130  siz_tot += 8+2+1;
131  ::snprintf(fmt,sizeof(fmt),"%%16llX %%-%lds %%-%lds",long(siz_nam),long(siz_add));
132  }
133  out << "dd4hep." << char(sep ? sep : '-')
134  << "." << long(siz_nam)
135  << "." << long(siz_add)
136  << "." << long(siz_tot) << std::endl;
137  for( const auto& i : all ) {
138  dd4hep::Condition c = i.second;
139  std::snprintf(text, sizeof(text), fmt, c.key(), c.name(), c.address().c_str());
140  out << text << std::endl;
141  }
142 #endif
143  out.close();
144  return 1;
145  }
146 
148  int readText(const std::string& input, ConditionsTextRepository::Data& data) {
149  std::size_t idx;
151  std::size_t siz_nam, siz_add, siz_tot;
152  char sep, c, text[2*PATH_MAX+64];
153  std::ifstream in(input);
154  in >> c >> c >> c >> c >> c >> c >> c >> sep
155  >> c >> siz_nam
156  >> c >> siz_add
157  >> c >> siz_tot;
158  text[0] = 0;
159  in.getline(text,sizeof(text),'\n');
160  do {
161  text[0] = 0;
162  in.getline(text,sizeof(text),'\n');
163  if ( in.good() ) {
164  std::size_t idx_nam = 9+siz_nam<sizeof(text)-1 ? 9+siz_nam : 0;
165  std::size_t idx_add = 10+siz_nam+siz_add<sizeof(text)-1 ? 10+siz_nam+siz_add : 0;
166  if ( 9+siz_nam >= sizeof(text) )
167  dd4hep::except("ConditionsTextRepository","Inconsistent input data in %s: %s -> (%lld,%lld,%lld)",
168  __FILE__, input.c_str(), siz_nam, siz_add, siz_tot);
169  else if ( 10+siz_nam+siz_add >= sizeof(text) )
170  dd4hep::except("ConditionsTextRepository","Inconsistent input data in %s: %s -> (%lld,%lld,%lld)",
171  __FILE__, input.c_str(), siz_nam, siz_add, siz_tot);
172  else if ( siz_tot ) {
173  // Direct access mode with fixed record size
174  text[8] = text[idx_nam] = text[idx_add] = 0;
175  e.name = text+9;
176  e.address = text+idx_nam+1;
177  if ( (idx=e.name.find(' ')) != std::string::npos )
178  e.name[idx]=0;
179  if ( (idx=e.address.find(' ')) != std::string::npos )
180  e.address[idx]=0;
181  }
182  else {
183  // Variable record size
184  e.name=text+9;
185  if ( (idx=e.name.find(sep)) != std::string::npos && idx+10 < sizeof(text) )
186  text[9+idx]=0, e.address=text+idx+10, e.name=text+9;
187  if ( (idx=e.address.find(sep)) != std::string::npos )
188  e.address[idx]=0;
189  else if ( (idx=e.address.find('\n')) != std::string::npos )
190  e.address[idx]=0;
191  }
192  std::size_t cap = data.capacity();
193  std::sscanf(text,"%16llX",&e.key);
194  if ( data.size() == cap ) data.reserve(cap+500);
195  data.emplace_back(e);
196  }
197  } while(in.good() && !in.eof() );
198  in.close();
199  return 1;
200  }
201 }
202 
204 int ConditionsTextRepository::save(ConditionsManager manager, const std::string& output) const {
205  AllConditions all;
206  const auto types = manager.iovTypesUsed();
207  for( const IOVType* type : types ) {
208  if ( type ) {
209  ConditionsIOVPool* pool = manager.iovPool(*type);
210  if ( pool ) {
211  for ( const auto& cp : pool->elements ) {
212  RangeConditions rc;
213  cp.second->select_all(rc);
214  for(auto c : rc )
215  all[c.key()] = c;
216  }
217  }
218  }
219  }
220 
221  if ( output.find(".xml") != std::string::npos ) {
223  return createXML(output, all);
224  }
225  else if ( output.find(".txt") != std::string::npos ) {
227  return createText(output, all, 0);
228  }
229  else if ( output.find(".daf") != std::string::npos ) {
231  return createText(output, all, 0);
232  }
233  else if ( output.find(".csv") != std::string::npos ) {
235  return createText(output, all, ';');
236  }
237  return 0;
238 }
239 
241 int ConditionsTextRepository::load(const std::string& input, Data& data) const {
242  if ( input.find(".xml") != std::string::npos ) {
243  return readXML(input, data);
244  }
245  else if ( input.find(".txt") != std::string::npos ) {
246  return readText(input, data);
247  }
248  else if ( input.find(".daf") != std::string::npos ) {
249  return readText(input, data);
250  }
251  else if ( input.find(".csv") != std::string::npos ) {
252  return readText(input, data);
253  }
254  return 0;
255 }
dd4hep::xml::DocumentHandler::create
Document create(const char *tag, const char *comment=0) const
Create new XML document by parsing empty xml buffer.
Definition: DocumentHandler.cpp:681
dd4hep::RangeConditions
std::vector< Condition > RangeConditions
Definition: Conditions.h:491
dd4hep::xml::Collection_t
Class to support the access to collections of XmlNodes (or XmlElements)
Definition: XMLElements.h:636
cond
AlignmentCondition::Object * cond
Definition: AlignmentsCalculator.cpp:68
dd4hep::xml::Element::append
void append(Handle_t handle) const
Append a new element to the existing tree.
Definition: XMLElements.h:839
dd4hep::cond::ConditionsTextRepository::Entry::address
std::string address
Definition: ConditionsTextRepository.h:50
ConditionsInterna.h
xml_elt_t
dd4hep::xml::Element xml_elt_t
Definition: ConditionsTextRepository.cpp:33
dd4hep::cond::ConditionsTextRepository::Data
std::vector< Entry > Data
Definition of the entry collection.
Definition: ConditionsTextRepository.h:59
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::cond::ConditionsIOVPool::elements
Elements elements
Container of IOV dependent conditions pools.
Definition: ConditionsIOVPool.h:46
dd4hep::xml::Handle_t
Class to easily access the properties of single XmlElements.
Definition: XMLElements.h:380
dd4hep::Handle::name
const char * name() const
Access the object name (or "" if not supported by the object)
dd4hep::IOVType
Class describing the interval of validty type.
Definition: IOV.h:37
dd4hep::cond::ConditionsManager::iovTypesUsed
const std::vector< const IOVType * > iovTypesUsed() const
Access the used/registered IOV types.
Definition: ConditionsManager.cpp:209
xml_h
dd4hep::xml::Handle_t xml_h
Definition: ConditionsTextRepository.cpp:32
DocumentHandler.h
dd4hep::cond::ConditionsTextRepository::ConditionsTextRepository
ConditionsTextRepository()
Default constructor.
Definition: ConditionsTextRepository.cpp:40
dd4hep::cond::ConditionsManager::iovPool
ConditionsIOVPool * iovPool(const IOVType &type) const
Access conditions multi IOV pool by iov type.
Definition: ConditionsManager.cpp:204
dd4hep::Condition
Main condition object handle.
Definition: Conditions.h:51
dd4hep::cond
Namespace for implementation details of the AIDA detector description toolkit.
Definition: ConditionsCleanup.h:23
dd4hep::xml::Collection_t::for_each
void for_each(T oper) const
Loop processor using function object.
Definition: XMLElements.h:667
dd4hep::cond::ConditionsTextRepository::Entry::key
Condition::key_type key
Definition: ConditionsTextRepository.h:51
dd4hep::cond::ConditionsTextRepository::load
int load(const std::string &input, Data &data) const
Load the textrepository from file and fill user passed data structory.
Definition: ConditionsTextRepository.cpp:241
_U
#define _U(a)
Definition: Tags.h:23
ConditionsTextRepository.h
ConditionsIOVPool.h
dd4hep::cond::ConditionsTextRepository::save
int save(ConditionsManager m, const std::string &output) const
Save the textrepository to file.
Definition: ConditionsTextRepository.cpp:204
dd4hep::xml::DocumentHolder
Class supporting the basic functionality of an XML document including ownership.
Definition: XMLElements.h:741
xml_doc_t
dd4hep::xml::Document xml_doc_t
Definition: ConditionsTextRepository.cpp:34
dd4hep::xml::Document
Class supporting the basic functionality of an XML document.
Definition: XMLElements.h:697
dd4hep::detail::ConditionObject
The data class behind a conditions handle.
Definition: ConditionsInterna.h:68
dd4hep::cond::ConditionsManager
Manager class for condition handles.
Definition: ConditionsManager.h:46
dd4hep::xml::DocumentHandler::output
virtual int output(Document doc, const std::string &fname) const
Write xml document to output file (stdout if file name empty)
Definition: DocumentHandler.cpp:396
key
unsigned char key
Definition: AlignmentsCalculator.cpp:69
dd4hep::xml::DocumentHandler
Class supporting to read and parse XML documents.
Definition: DocumentHandler.h:39
AllConditions
std::map< dd4hep::Condition::key_type, dd4hep::Condition > AllConditions
Definition: ConditionsRepository.cpp:37
dd4hep::xml::Element
User abstraction class to manipulate XML elements within a document.
Definition: XMLElements.h:769
dd4hep::cond::ConditionsTextRepository::~ConditionsTextRepository
virtual ~ConditionsTextRepository()
Default destructor.
Definition: ConditionsTextRepository.cpp:44
xml_coll_t
dd4hep::xml::Collection_t xml_coll_t
Definition: ConditionsTextRepository.cpp:35
AllConditions
std::map< dd4hep::Condition::key_type, dd4hep::Condition > AllConditions
Definition: ConditionsTextRepository.cpp:37
XMLTags.h
ConditionsTags.h
dd4hep::cond::ConditionsIOVPool
Pool of conditions satisfying one IOV type (epoch, run, fill, etc)
Definition: ConditionsIOVPool.h:38
dd4hep::cond::ConditionsTextRepository::Entry
Definition of a single Entry in the conditions repository.
Definition: ConditionsTextRepository.h:48
dd4hep::cond::ConditionsTextRepository::Entry::name
std::string name
Definition: ConditionsTextRepository.h:50
dd4hep::xml::Handle_t::attr
T attr(const Attribute a) const
Access typed attribute value by the XmlAttr.
Definition: XMLElements.h:454
dd4hep::Condition::address
const std::string & address() const
Access the address string [e.g. database identifier].
Definition: Conditions.cpp:160
Printout.h
dd4hep::xml::Document::root
Handle_t root() const
Access the ROOT eleemnt of the DOM document.
Definition: XMLElements.cpp:1040