DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
ConditionsTreePersistency.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 
26 
27 using namespace dd4hep::cond;
28 
29 // Local namespace for anonymous stuff
30 namespace {
31 
33  /*
34  * \author M.Frank
35  * \version 1.0
36  * \ingroup DD4HEP_CONDITIONS
37  */
38  struct Scanner : public dd4hep::Condition::Processor {
43  virtual int process(dd4hep::Condition c) const override {
44  pool.emplace_back(c.ptr());
45  return 1;
46  }
47  };
48  struct DurationStamp {
49  TTimeStamp start;
52  }
54  TTimeStamp stop;
55  object->duration = stop.AsDouble()-start.AsDouble();
56  }
57  };
58 }
59 
62 }
63 
65 ConditionsTreePersistency::ConditionsTreePersistency(const std::string& name, const std::string& title)
66  : TNamed(name.c_str(), title.c_str())
67 {
68 }
69 
72  clear();
73 }
74 
76 std::size_t ConditionsTreePersistency::add(const std::string& identifier,
77  const IOV& iov,
78  std::vector<Condition>& conditions)
79 {
80  DurationStamp stamp(this);
81  conditionPools.emplace_back(std::pair<iov_key_type, pool_type>());
82  pool_type& ent = conditionPools.back().second;
83  iov_key_type& key = conditionPools.back().first;
84  key.first = identifier;
85  key.second.first = make_pair(iov.iovType->name,iov.type);
86  key.second.second = iov.key();
87  ent = conditions;
88  for(auto c : ent) c->addRef();
89  return ent.size();
90 }
91 
93 std::size_t ConditionsTreePersistency::add(const std::string& identifier, ConditionsPool& pool) {
94  DurationStamp stamp(this);
95  conditionPools.emplace_back(std::pair<iov_key_type, pool_type>());
96  pool_type& ent = conditionPools.back().second;
97  iov_key_type& key = conditionPools.back().first;
98  const IOV* iov = pool.iov;
99  key.first = identifier;
100  key.second.first = make_pair(iov->iovType->name,iov->type);
101  key.second.second = iov->key();
102  pool.select_all(ent);
103  for(auto c : ent) c.ptr()->addRef();
104  return ent.size();
105 }
106 
108 std::size_t ConditionsTreePersistency::add(const std::string& identifier, const ConditionsIOVPool& pool) {
109  std::size_t count = 0;
110  DurationStamp stamp(this);
111  for( const auto& p : pool.elements ) {
112  iovPools.emplace_back(std::pair<iov_key_type, pool_type>());
113  pool_type& ent = iovPools.back().second;
114  iov_key_type& key = iovPools.back().first;
115  const IOV* iov = p.second->iov;
116  key.first = identifier;
117  key.second.first = make_pair(iov->iovType->name,iov->type);
118  key.second.second = p.first;
119  p.second->select_all(ent);
120  for(auto c : ent) c.ptr()->addRef();
121  count += ent.size();
122  }
123  return count;
124 }
125 
127 TFile* ConditionsTreePersistency::openFile(const std::string& fname) {
128  TDirectory::TContext context;
129  TFile* file = TFile::Open(fname.c_str());
130  if ( file && !file->IsZombie()) return file;
131  except("ConditionsTreePersistency","+++ FAILED to open ROOT file %s in read-mode.",fname.c_str());
132  return 0;
133 }
134 
138  for (auto& p : pool ) {
139  for(Condition c : p.second )
140  c.ptr()->release();
141  p.second.clear();
142  }
143  pool.clear();
144 }
145 
150  _clear(iovPools);
151 }
152 
154 std::unique_ptr<ConditionsTreePersistency>
155 ConditionsTreePersistency::load(TFile* file,const std::string& obj) {
156  std::unique_ptr<ConditionsTreePersistency> p;
157  if ( file && !file->IsZombie()) {
158  TTimeStamp start;
159  p.reset((ConditionsTreePersistency*)file->Get(obj.c_str()));
160  TTimeStamp stop;
161  if ( p.get() ) {
162  p->duration = stop.AsDouble()-start.AsDouble();
163  return p;
164  }
165  except("ConditionsTreePersistency",
166  "+++ FAILED to load object %s from file %s",
167  obj.c_str(), file->GetName());
168  }
169  except("ConditionsTreePersistency",
170  "+++ FAILED to load object %s from file [Invalid file]",obj.c_str());
171  return p;
172 }
173 
176  persistent_type& persistent_pools,
177  const std::string& id,
178  const std::string& iov_type,
179  const IOV::Key& iov_key,
180  ConditionsManager mgr)
181 {
182  std::size_t count = 0;
183  std::pair<bool,const IOVType*> iovTyp(false,0);
184  for (auto& iovp : persistent_pools ) {
185  bool use = false;
186  const iov_key_type& key = iovp.first;
187  if ( !(id.empty() || id=="*" || key.first == id) )
188  continue;
189  if ( !(iov_type.empty() || iov_type == "*" || key.second.first.first == iov_type) )
190  continue;
191  switch(strategy) {
192  case IMPORT_ALL:
193  use = true;
194  break;
195  case IMPORT_EXACT:
196  use = key.second.second == iov_key;
197  break;
198  case IMPORT_CONTAINED:
199  use = IOV::key_is_contained(key.second.second,iov_key);
200  break;
201  case IMPORT_EDGE_LOWER:
202  use = IOV::key_overlaps_lower_end(key.second.second,iov_key);
203  break;
204  case IMPORT_EDGE_UPPER:
205  use = IOV::key_overlaps_higher_end(key.second.second,iov_key);
206  break;
208  use = IOV::key_is_contained(key.second.second,iov_key) ||
209  IOV::key_overlaps_lower_end(key.second.second,iov_key);
210  break;
212  use = IOV::key_is_contained(key.second.second,iov_key) ||
213  IOV::key_overlaps_higher_end(key.second.second,iov_key);
214  break;
215  default:
216  use = false;
217  break;
218  }
219  if ( !use )
220  continue;
221 
222  iovTyp = mgr.registerIOVType(key.second.first.second,key.second.first.first);
223  if ( iovTyp.second ) {
224  ConditionsPool* pool = mgr.registerIOV(*iovTyp.second, key.second.second);
225  for (Condition c : iovp.second) {
226  Condition::Object* o = c.ptr();
227  o->iov = pool->iov;
228  if ( pool->insert(o->addRef()) ) {
229  //printout(WARNING,"ConditionsTreePersistency","%p: [%016llX] %s -> %s",
230  // o, o->hash,o->GetName(),o->data.str().c_str());
231  ++count;
232  }
233  else {
234  printout(WARNING,"ConditionsTreePersistency",
235  "+++ Ignore condition %s from %s iov:%s [Already present]",
236  c.name(),id.c_str(), iov_type.c_str());
237  }
238  }
239  }
240  }
241  return count;
242 }
243 
245 std::size_t ConditionsTreePersistency::importIOVPool(const std::string& identifier,
246  const std::string& iov_type,
247  ConditionsManager mgr)
248 {
249  DurationStamp stamp(this);
250  return _import(IMPORT_ALL,iovPools,identifier,iov_type,IOV::Key(),mgr);
251 }
252 
254 std::size_t ConditionsTreePersistency::importConditionsPool(const std::string& identifier,
255  const std::string& iov_type,
256  ConditionsManager mgr)
257 {
258  DurationStamp stamp(this);
259  return _import(IMPORT_ALL,conditionPools,identifier,iov_type,IOV::Key(),mgr);
260 }
261 
263 std::size_t ConditionsTreePersistency::importConditionsPool(ImportStrategy strategy,
264  const std::string& identifier,
265  const std::string& iov_type,
266  const IOV::Key& iov_key,
267  ConditionsManager mgr) {
268  return _import(strategy,conditionPools,identifier,iov_type,iov_key,mgr);
269 }
270 
273  DurationStamp stamp(this);
274  //TDirectory::TContext context;
275  int nBytes = file->WriteTObject(this,GetName());
276  return nBytes;
277 }
278 
280 int ConditionsTreePersistency::save(const std::string& fname) {
281  DurationStamp stamp(this);
282  //TDirectory::TContext context;
283  TFile* file = TFile::Open(fname.c_str(),"RECREATE");
284  if ( file && !file->IsZombie()) {
285  int nBytes = save(file);
286  file->Close();
287  delete file;
288  return nBytes;
289  }
290  return -1;
291 }
dd4hep::cond::ConditionsTreePersistency::IMPORT_EDGE_LOWER
@ IMPORT_EDGE_LOWER
Definition: ConditionsTreePersistency.h:143
dd4hep::cond::ConditionsManager::registerIOV
ConditionsPool * registerIOV(const IOV &iov) const
Register IOV with type and key.
Definition: ConditionsManager.cpp:231
dd4hep::cond::ConditionsTreePersistency::openFile
static TFile * openFile(const std::string &fname)
Open ROOT file in read mode.
Definition: ConditionsTreePersistency.cpp:127
ConditionsSlice.h
dd4hep::IOVType::name
std::string name
String name.
Definition: IOV.h:43
dd4hep::cond::ConditionsTreePersistency::conditionPools
persistent_type conditionPools
Definition: ConditionsTreePersistency.h:134
ClassImp::Scanner::pool
ConditionsTreePersistency::pool_type & pool
Definition: ConditionsTreePersistency.cpp:39
dd4hep::cond::ConditionsTreePersistency::_clear
void _clear(persistent_type &pool)
Clear object content and release allocated memory.
Definition: ConditionsTreePersistency.cpp:136
__ConditionsTreePersistency
dd4hep::cond::ConditionsTreePersistency __ConditionsTreePersistency
Definition: ConditionsTreePersistency.cpp:23
dd4hep::cond::ConditionsTreePersistency::importConditionsPool
size_t importConditionsPool(const std::string &id, const std::string &iov_type, ConditionsManager mgr)
Load conditions pool and populate conditions manager.
dd4hep::cond::ConditionsTreePersistency::_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: ConditionsTreePersistency.cpp:175
dd4hep::cond::ConditionsTreePersistency::clear
void clear()
Clear object content and release allocated memory.
Definition: ConditionsTreePersistency.cpp:147
dd4hep::cond::ConditionsTreePersistency::IMPORT_ALL
@ IMPORT_ALL
Definition: ConditionsTreePersistency.h:138
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: ConditionsTreePersistency.cpp:43
dd4hep::IOV::Key
std::pair< Key_value_type, Key_value_type > Key
Definition: IOV.h:74
dd4hep::cond::ConditionsTreePersistency::IMPORT_EDGE_UPPER
@ IMPORT_EDGE_UPPER
Definition: ConditionsTreePersistency.h:144
dd4hep::cond::ConditionsIOVPool::elements
Elements elements
Container of IOV dependent conditions pools.
Definition: ConditionsIOVPool.h:46
dd4hep::detail::ConditionObject::addRef
ConditionObject * addRef()
Increase reference counter (Used by persistency mechanism)
Definition: ConditionsInterna.h:105
dd4hep::Handle::name
const char * name() const
Access the object name (or "" if not supported by the object)
dd4hep::cond::ConditionsTreePersistency::add
size_t add(const std::string &tag, const IOV &iov, std::vector< Condition > &conditions)
Add conditions content to be saved. Note, that dependent conditions shall not be saved!
dd4hep::cond::ConditionsTreePersistency::save
int save(TFile *file)
Save the data content to a root file.
Definition: ConditionsTreePersistency.cpp:272
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::ConditionsTreePersistency::load
static std::unique_ptr< ConditionsTreePersistency > load(TFile *file, const std::string &object)
Load conditions content from file.
dd4hep::Utilities::GetName
const char * GetName(T *p)
Definition: Utilities.h:45
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
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::cond::ConditionsTreePersistency::IMPORT_CONTAINED
@ IMPORT_CONTAINED
Definition: ConditionsTreePersistency.h:140
dd4hep::detail::ConditionObject::release
void release()
Release object (Used by persistency mechanism)
Definition: ConditionsInterna.cpp:63
ConditionsIOVPool.h
dd4hep::cond::ConditionsTreePersistency::IMPORT_CONTAINED_UPPER
@ IMPORT_CONTAINED_UPPER
Definition: ConditionsTreePersistency.h:142
dd4hep::cond::ConditionsTreePersistency::persistent_type
std::list< std::pair< iov_key_type, pool_type > > persistent_type
Definition: ConditionsTreePersistency.h:132
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
ClassImp::Scanner::Scanner
Scanner(ConditionsTreePersistency::pool_type &p)
Constructor.
Definition: ConditionsTreePersistency.cpp:41
dd4hep::IOV::key
Key key() const
Get the local key of the IOV.
Definition: IOV.h:115
dd4hep::cond::ConditionsTreePersistency
Helper to save conditions pools to ROOT.
Definition: ConditionsTreePersistency.h:127
dd4hep::cond::ConditionsTreePersistency::iovPools
persistent_type iovPools
Definition: ConditionsTreePersistency.h:135
dd4hep::detail::ConditionObject
The data class behind a conditions handle.
Definition: ConditionsInterna.h:68
dd4hep::cond::ConditionsTreePersistency::pool_type
std::vector< Condition > pool_type
Definition: ConditionsTreePersistency.h:129
dd4hep::cond::ConditionsManager
Manager class for condition handles.
Definition: ConditionsManager.h:46
dd4hep::IOV::type
unsigned int type
IOV buffer type: Must be a bitmap!
Definition: IOV.h:86
key
unsigned char key
Definition: AlignmentsCalculator.cpp:69
dd4hep::cond::ConditionsTreePersistency::ImportStrategy
ImportStrategy
Definition: ConditionsTreePersistency.h:137
dd4hep::cond::ConditionsTreePersistency::~ConditionsTreePersistency
virtual ~ConditionsTreePersistency()
Default destructor.
Definition: ConditionsTreePersistency.cpp:71
dd4hep::Handle::ptr
T * ptr() const
Access to the held object.
Definition: Handle.h:153
dd4hep::IOV::iovType
const IOVType * iovType
Reference to IOV type.
Definition: IOV.h:80
ClassImp
ClassImp(DD4hepRootPersistency) using namespace dd4hep
ClassImp::DurationStamp::DurationStamp
DurationStamp(ConditionsTreePersistency *obj)
Definition: ConditionsTreePersistency.cpp:51
dd4hep::cond::ConditionsTreePersistency::iov_key_type
std::pair< std::string, std::pair< std::pair< std::string, int >, IOV::Key > > iov_key_type
Definition: ConditionsTreePersistency.h:131
dd4hep::cond::ConditionsTreePersistency::IMPORT_EXACT
@ IMPORT_EXACT
Definition: ConditionsTreePersistency.h:139
dd4hep::cond::ConditionsTreePersistency::IMPORT_CONTAINED_LOWER
@ IMPORT_CONTAINED_LOWER
Definition: ConditionsTreePersistency.h:141
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::ConditionsTreePersistency::ConditionsTreePersistency
ConditionsTreePersistency()
Default constructor.
Definition: ConditionsTreePersistency.cpp:61
ConditionsTreePersistency.h
dd4hep::Condition::Processor
Abstract base for processing callbacks to conditions objects.
Definition: Conditions.h:112
Printout.h
dd4hep::cond::ConditionsTreePersistency::importIOVPool
size_t importIOVPool(const std::string &id, const std::string &iov_type, ConditionsManager mgr)
Load conditions IOV pool and populate conditions manager.
Definition: ConditionsTreePersistency.cpp:245
ClassImp::DurationStamp::~DurationStamp
~DurationStamp()
Definition: ConditionsTreePersistency.cpp:53