DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
ConditionsRootPersistency.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
15 #include <DD4hep/Printout.h>
16 #include <DDCond/ConditionsSlice.h>
19 
20 #include <TFile.h>
21 #include <TTimeStamp.h>
22 
24 
27 
28 using namespace dd4hep::cond;
29 
30 // Local namespace for anonymous stuff
31 namespace {
32 
34  /*
35  * \author M.Frank
36  * \version 1.0
37  * \ingroup DD4HEP_CONDITIONS
38  */
44  virtual int process(dd4hep::Condition c) const override {
45  pool.emplace_back(c.ptr());
46  return 1;
47  }
48  };
49  struct DurationStamp {
50  TTimeStamp start;
53  }
55  TTimeStamp stop;
56  object->duration = stop.AsDouble()-start.AsDouble();
57  }
58  };
59 }
60 
63 }
64 
66 ConditionsRootPersistency::ConditionsRootPersistency(const std::string& name, const std::string& title)
67  : TNamed(name.c_str(), title.c_str())
68 {
69 }
70 
73  clear();
74 }
75 
77 std::size_t ConditionsRootPersistency::add(const std::string& identifier,
78  const IOV& iov,
79  std::vector<Condition>& conditions)
80 {
81  DurationStamp stamp(this);
82  conditionPools.emplace_back(std::pair<iov_key_type, pool_type>());
83  pool_type& ent = conditionPools.back().second;
84  iov_key_type& key = conditionPools.back().first;
85  key.first = identifier;
86  key.second.first = make_pair(iov.iovType->name,iov.type);
87  key.second.second = iov.key();
88  ent = conditions;
89  for(auto c : ent) c.ptr()->addRef();
90  return ent.size();
91 }
92 
94 size_t ConditionsRootPersistency::add(const std::string& identifier, ConditionsPool& pool) {
95  DurationStamp stamp(this);
96  conditionPools.emplace_back(std::pair<iov_key_type, pool_type>());
97  pool_type& ent = conditionPools.back().second;
98  iov_key_type& key = conditionPools.back().first;
99  const IOV* iov = pool.iov;
100  key.first = identifier;
101  key.second.first = make_pair(iov->iovType->name,iov->type);
102  key.second.second = iov->key();
103  pool.select_all(ent);
104  for(auto c : ent) c.ptr()->addRef();
105  return ent.size();
106 }
107 
109 size_t ConditionsRootPersistency::add(const std::string& identifier, const ConditionsIOVPool& pool) {
110  size_t count = 0;
111  DurationStamp stamp(this);
112  for( const auto& p : pool.elements ) {
113  iovPools.emplace_back(std::pair<iov_key_type, pool_type>());
114  pool_type& ent = iovPools.back().second;
115  iov_key_type& key = iovPools.back().first;
116  const IOV* iov = p.second->iov;
117  key.first = identifier;
118  key.second.first = make_pair(iov->iovType->name,iov->type);
119  key.second.second = p.first;
120  p.second->select_all(ent);
121  for(auto c : ent) c.ptr()->addRef();
122  count += ent.size();
123  }
124  return count;
125 }
126 
128 size_t ConditionsRootPersistency::add(const std::string& identifier, const UserPool& pool) {
129  DurationStamp stamp(this);
130  userPools.emplace_back(std::pair<iov_key_type, pool_type>());
131  pool_type& ent = userPools.back().second;
132  iov_key_type& key = userPools.back().first;
133  const IOV& iov = pool.validity();
134  key.first = identifier;
135  key.second.first = make_pair(iov.iovType->name,iov.type);
136  key.second.second = iov.key();
137  pool.scan(Scanner(ent));
138  for(auto c : ent) c.ptr()->addRef();
139  return ent.size();
140 }
141 
143 TFile* ConditionsRootPersistency::openFile(const std::string& fname) {
144  TDirectory::TContext context;
145  TFile* file = TFile::Open(fname.c_str());
146  if ( file && !file->IsZombie()) return file;
147  except("ConditionsRootPersistency","+++ FAILED to open ROOT file %s in read-mode.",fname.c_str());
148  return 0;
149 }
150 
154  for (auto& p : pool ) {
155  for(Condition c : p.second )
156  c.ptr()->release();
157  p.second.clear();
158  }
159  pool.clear();
160 }
161 
166  _clear(userPools);
167  _clear(iovPools);
168 }
169 
171 std::unique_ptr<ConditionsRootPersistency>
172 ConditionsRootPersistency::load(TFile* file,const std::string& obj) {
173  std::unique_ptr<ConditionsRootPersistency> p;
174  if ( file && !file->IsZombie()) {
175  TTimeStamp start;
176  p.reset((ConditionsRootPersistency*)file->Get(obj.c_str()));
177  TTimeStamp stop;
178  if ( p.get() ) {
179  p->duration = stop.AsDouble()-start.AsDouble();
180  return p;
181  }
182  except("ConditionsRootPersistency",
183  "+++ FAILED to load object %s from file %s",
184  obj.c_str(), file->GetName());
185  }
186  except("ConditionsRootPersistency",
187  "+++ FAILED to load object %s from file [Invalid file]",obj.c_str());
188  return p;
189 }
190 
193  persistent_type& persistent_pools,
194  const std::string& id,
195  const std::string& iov_type,
196  const IOV::Key& iov_key,
197  ConditionsManager mgr) {
198  size_t count = 0;
199  std::pair<bool,const IOVType*> iovTyp(false,0);
200  for (auto& iovp : persistent_pools ) {
201  bool use = false;
202  const iov_key_type& key = iovp.first;
203  if ( !(id.empty() || id=="*" || key.first == id) )
204  continue;
205  if ( !(iov_type.empty() || iov_type == "*" || key.second.first.first == iov_type) )
206  continue;
207  switch(strategy) {
208  case IMPORT_ALL:
209  use = true;
210  break;
211  case IMPORT_EXACT:
212  use = key.second.second == iov_key;
213  break;
214  case IMPORT_CONTAINED:
215  use = IOV::key_is_contained(key.second.second,iov_key);
216  break;
217  case IMPORT_EDGE_LOWER:
218  use = IOV::key_overlaps_lower_end(key.second.second,iov_key);
219  break;
220  case IMPORT_EDGE_UPPER:
221  use = IOV::key_overlaps_higher_end(key.second.second,iov_key);
222  break;
224  use = IOV::key_is_contained(key.second.second,iov_key) ||
225  IOV::key_overlaps_lower_end(key.second.second,iov_key);
226  break;
228  use = IOV::key_is_contained(key.second.second,iov_key) ||
229  IOV::key_overlaps_higher_end(key.second.second,iov_key);
230  break;
231  default:
232  use = false;
233  break;
234  }
235  if ( !use )
236  continue;
237 
238  iovTyp = mgr.registerIOVType(key.second.first.second,key.second.first.first);
239  if ( iovTyp.second ) {
240  ConditionsPool* pool = mgr.registerIOV(*iovTyp.second, key.second.second);
241  for (Condition c : iovp.second) {
242  Condition::Object* o = c.ptr();
243  o->iov = pool->iov;
244  if ( pool->insert(o->addRef()) ) {
245  //printout(WARNING,"ConditionsRootPersistency","%p: [%016llX] %s -> %s",
246  // o, o->hash,o->GetName(),o->data.str().c_str());
247  ++count;
248  }
249  else {
250  printout(WARNING,"ConditionsRootPersistency",
251  "+++ Ignore condition %s from %s iov:%s [Already present]",
252  c.name(),id.c_str(), iov_type.c_str());
253  }
254  }
255  }
256  }
257  return count;
258 }
259 
261 size_t ConditionsRootPersistency::importIOVPool(const std::string& identifier,
262  const std::string& iov_type,
263  ConditionsManager mgr)
264 {
265  DurationStamp stamp(this);
266  return _import(IMPORT_ALL,iovPools,identifier,iov_type,IOV::Key(),mgr);
267 }
268 
270 size_t ConditionsRootPersistency::importUserPool(const std::string& identifier,
271  const std::string& iov_type,
272  ConditionsManager mgr)
273 {
274  DurationStamp stamp(this);
275  return _import(IMPORT_ALL,userPools,identifier,iov_type,IOV::Key(),mgr);
276 }
277 
279 size_t ConditionsRootPersistency::importConditionsPool(const std::string& identifier,
280  const std::string& iov_type,
281  ConditionsManager mgr)
282 {
283  DurationStamp stamp(this);
284  return _import(IMPORT_ALL,conditionPools,identifier,iov_type,IOV::Key(),mgr);
285 }
286 
288 size_t ConditionsRootPersistency::importConditionsPool(ImportStrategy strategy,
289  const std::string& identifier,
290  const std::string& iov_type,
291  const IOV::Key& iov_key,
292  ConditionsManager mgr) {
293  return _import(strategy,conditionPools,identifier,iov_type,iov_key,mgr);
294 }
295 
298  DurationStamp stamp(this);
299  //TDirectory::TContext context;
300  int nBytes = file->WriteTObject(this,GetName());
301  return nBytes;
302 }
303 
305 int ConditionsRootPersistency::save(const std::string& fname) {
306  DurationStamp stamp(this);
307  //TDirectory::TContext context;
308  TFile* file = TFile::Open(fname.c_str(),"RECREATE");
309  if ( file && !file->IsZombie()) {
310  int nBytes = save(file);
311  file->Close();
312  delete file;
313  return nBytes;
314  }
315  return -1;
316 }
dd4hep::cond::ConditionsRootPersistency::iovPools
persistent_type iovPools
Definition: ConditionsRootPersistency.h:60
ClassImp::DurationStamp::DurationStamp
DurationStamp(ConditionsRootPersistency *obj)
Definition: ConditionsRootPersistency.cpp:52
dd4hep::cond::ConditionsManager::registerIOV
ConditionsPool * registerIOV(const IOV &iov) const
Register IOV with type and key.
Definition: ConditionsManager.cpp:231
ConditionsSlice.h
dd4hep::IOVType::name
std::string name
String name.
Definition: IOV.h:43
ClassImp::Scanner::pool
ConditionsRootPersistency::pool_type & pool
Definition: ConditionsRootPersistency.cpp:40
ClassImp::Scanner
Helper to select conditions.
Definition: ConditionsRootPersistency.cpp:39
dd4hep::cond::ConditionsRootPersistency::save
int save(TFile *file)
Save the data content to a root file.
Definition: ConditionsRootPersistency.cpp:297
ConditionsRootPersistency.h
dd4hep::cond::ConditionsRootPersistency::IMPORT_CONTAINED
@ IMPORT_CONTAINED
Definition: ConditionsRootPersistency.h:65
dd4hep::cond::ConditionsPool::insert
virtual bool insert(Condition cond)=0
Register a new condition to this pool.
ClassImp::Scanner::process
virtual int process(dd4hep::Condition c) const override
Conditions callback for object processing.
Definition: ConditionsRootPersistency.cpp:44
dd4hep::cond::ConditionsRootPersistency::IMPORT_CONTAINED_LOWER
@ IMPORT_CONTAINED_LOWER
Definition: ConditionsRootPersistency.h:66
dd4hep::IOV::Key
std::pair< Key_value_type, Key_value_type > Key
Definition: IOV.h:74
dd4hep::cond::ConditionsRootPersistency::ImportStrategy
ImportStrategy
Definition: ConditionsRootPersistency.h:62
dd4hep::cond::ConditionsIOVPool::elements
Elements elements
Container of IOV dependent conditions pools.
Definition: ConditionsIOVPool.h:46
dd4hep::cond::ConditionsRootPersistency::IMPORT_EDGE_LOWER
@ IMPORT_EDGE_LOWER
Definition: ConditionsRootPersistency.h:68
ClassImp::DurationStamp::start
TTimeStamp start
Definition: ConditionsRootPersistency.cpp:50
dd4hep::detail::ConditionObject::addRef
ConditionObject * addRef()
Increase reference counter (Used by persistency mechanism)
Definition: ConditionsInterna.h:105
dd4hep::cond::ConditionsRootPersistency::openFile
static TFile * openFile(const std::string &fname)
Open ROOT file in read mode.
Definition: ConditionsRootPersistency.cpp:143
dd4hep::Handle::name
const char * name() const
Access the object name (or "" if not supported by the object)
dd4hep::cond::UserPool::validity
const IOV & validity() const
Access the interval of validity for this user pool.
Definition: ConditionsPool.h:167
dd4hep::cond::ConditionsPool::select_all
virtual size_t select_all(RangeConditions &result)=0
Select all conditions contained.
dd4hep::Condition
Main condition object handle.
Definition: Conditions.h:51
dd4hep::cond::ConditionsManager::registerIOVType
std::pair< bool, const IOVType * > registerIOVType(size_t iov_type, const std::string &iov_name) const
Register new IOV type if it does not (yet) exist.
Definition: ConditionsManager.cpp:194
dd4hep::cond
Namespace for implementation details of the AIDA detector description toolkit.
Definition: ConditionsCleanup.h:23
dd4hep::IOV
Class describing the interval of validty.
Definition: IOV.h:67
dd4hep::cond::ConditionsRootPersistency::importUserPool
size_t importUserPool(const std::string &id, const std::string &iov_type, ConditionsManager mgr)
Load conditions user pool and populate conditions manager.
Definition: ConditionsRootPersistency.cpp:270
dd4hep::cond::ConditionsRootPersistency::iov_key_type
std::pair< std::string, std::pair< std::pair< std::string, int >, IOV::Key > > iov_key_type
Definition: ConditionsRootPersistency.h:55
dd4hep::cond::ConditionsRootPersistency::ConditionsRootPersistency
ConditionsRootPersistency()
Default constructor.
Definition: ConditionsRootPersistency.cpp:62
dd4hep::Utilities::GetName
const char * GetName(T *p)
Definition: Utilities.h:45
dd4hep::cond::ConditionsRootPersistency::IMPORT_CONTAINED_UPPER
@ IMPORT_CONTAINED_UPPER
Definition: ConditionsRootPersistency.h:67
ClassImp::DurationStamp
Definition: ConditionsRootPersistency.cpp:49
dd4hep::cond::ConditionsPool
Class implementing the conditions collection for a given IOV type.
Definition: ConditionsPool.h:54
dd4hep::IOV::key_overlaps_lower_end
static bool key_overlaps_lower_end(const Key &key, const Key &test)
Check if IOV 'test' has an overlap on the lower interval edge with IOV 'key'.
Definition: IOV.h:159
dd4hep::cond::ConditionsRootPersistency::IMPORT_EXACT
@ IMPORT_EXACT
Definition: ConditionsRootPersistency.h:64
TNamed
Class of the ROOT toolkit. See http://root.cern.ch/root/htmldoc/ClassIndex.html.
Definition: ROOTClasses.h:37
dd4hep::cond::ConditionsPool::iov
IOV * iov
The IOV of the conditions hosted.
Definition: ConditionsPool.h:65
dd4hep::IOV::key_overlaps_higher_end
static bool key_overlaps_higher_end(const Key &key, const Key &test)
Check if IOV 'test' has an overlap on the upper interval edge with IOV 'key'.
Definition: IOV.h:162
dd4hep::detail::ConditionObject::release
void release()
Release object (Used by persistency mechanism)
Definition: ConditionsInterna.cpp:63
ConditionsIOVPool.h
dd4hep::cond::ConditionsRootPersistency::_clear
void _clear(persistent_type &pool)
Clear object content and release allocated memory.
Definition: ConditionsRootPersistency.cpp:152
ClassImp::Scanner::Scanner
Scanner(ConditionsRootPersistency::pool_type &p)
Constructor.
Definition: ConditionsRootPersistency.cpp:42
dd4hep::IOV::key_is_contained
static bool key_is_contained(const Key &key, const Key &test)
Check if IOV 'test' is fully contained in IOV 'key'.
Definition: IOV.h:153
dd4hep::IOV::key
Key key() const
Get the local key of the IOV.
Definition: IOV.h:115
dd4hep::cond::ConditionsRootPersistency::_import
size_t _import(ImportStrategy strategy, persistent_type &pers, const std::string &id, const std::string &iov_type, const IOV::Key &iov_key, ConditionsManager mgr)
Load ConditionsIOVPool and populate conditions manager.
Definition: ConditionsRootPersistency.cpp:192
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
__ConditionsRootPersistency
dd4hep::cond::ConditionsRootPersistency __ConditionsRootPersistency
Definition: ConditionsRootPersistency.cpp:23
dd4hep::IOV::type
unsigned int type
IOV buffer type: Must be a bitmap!
Definition: IOV.h:86
dd4hep::cond::ConditionsRootPersistency::add
size_t add(const std::string &identifier, const IOV &iov, std::vector< Condition > &conditions)
Add conditions content to be saved. Note, that dependent conditions shall not be saved!
Definition: ConditionsRootPersistency.cpp:77
dd4hep::cond::UserPool
Interface for conditions pool optimized to host conditions updates.
Definition: ConditionsPool.h:134
dd4hep::cond::UserPool::scan
virtual void scan(const Condition::Processor &processor) const =0
ConditionsMap overload: Interface to scan data content of the conditions mapping.
key
unsigned char key
Definition: AlignmentsCalculator.cpp:69
dd4hep::cond::ConditionsRootPersistency::importConditionsPool
size_t importConditionsPool(const std::string &id, const std::string &iov_type, ConditionsManager mgr)
Load conditions pool and populate conditions manager.
dd4hep::cond::ConditionsRootPersistency
Helper to save conditions pools to ROOT.
Definition: ConditionsRootPersistency.h:51
dd4hep::Handle::ptr
T * ptr() const
Access to the held object.
Definition: Handle.h:153
dd4hep::cond::ConditionsRootPersistency::conditionPools
persistent_type conditionPools
Definition: ConditionsRootPersistency.h:58
dd4hep::IOV::iovType
const IOVType * iovType
Reference to IOV type.
Definition: IOV.h:80
dd4hep::cond::ConditionsRootPersistency::clear
void clear()
Clear object content and release allocated memory.
Definition: ConditionsRootPersistency.cpp:163
dd4hep::cond::ConditionsRootPersistency::IMPORT_ALL
@ IMPORT_ALL
Definition: ConditionsRootPersistency.h:63
ClassImp
ClassImp(DD4hepRootPersistency) using namespace dd4hep
dd4hep::cond::ConditionsRootPersistency::importIOVPool
size_t importIOVPool(const std::string &id, const std::string &iov_type, ConditionsManager mgr)
Load conditions IOV pool and populate conditions manager.
Definition: ConditionsRootPersistency.cpp:261
dd4hep::cond::ConditionsRootPersistency::IMPORT_EDGE_UPPER
@ IMPORT_EDGE_UPPER
Definition: ConditionsRootPersistency.h:69
dd4hep::detail::ConditionObject::iov
const IOV * iov
Interval of validity.
Definition: ConditionsInterna.h:83
dd4hep::cond::ConditionsIOVPool
Pool of conditions satisfying one IOV type (epoch, run, fill, etc)
Definition: ConditionsIOVPool.h:38
dd4hep::cond::ConditionsRootPersistency::userPools
persistent_type userPools
Definition: ConditionsRootPersistency.h:59
dd4hep::cond::ConditionsRootPersistency::persistent_type
std::list< std::pair< iov_key_type, pool_type > > persistent_type
Definition: ConditionsRootPersistency.h:56
dd4hep::Condition::Processor
Abstract base for processing callbacks to conditions objects.
Definition: Conditions.h:112
Printout.h
dd4hep::cond::ConditionsRootPersistency::load
static std::unique_ptr< ConditionsRootPersistency > load(TFile *file, const std::string &object)
Load conditions content from file.
dd4hep::cond::ConditionsRootPersistency::pool_type
std::vector< Condition > pool_type
Definition: ConditionsRootPersistency.h:53
dd4hep::cond::ConditionsRootPersistency::~ConditionsRootPersistency
virtual ~ConditionsRootPersistency()
Default destructor.
Definition: ConditionsRootPersistency.cpp:72
ClassImp::DurationStamp::~DurationStamp
~DurationStamp()
Definition: ConditionsRootPersistency.cpp:54