File indexing completed on 2025-01-30 09:16:31
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015 #include <DD4hep/Printout.h>
0016 #include <DDCond/ConditionsSlice.h>
0017 #include <DDCond/ConditionsIOVPool.h>
0018 #include <DDCond/ConditionsTreePersistency.h>
0019
0020 #include <TFile.h>
0021 #include <TTimeStamp.h>
0022
0023 typedef dd4hep::cond::ConditionsTreePersistency __ConditionsTreePersistency;
0024
0025 ClassImp(__ConditionsTreePersistency)
0026
0027 using namespace dd4hep::cond;
0028
0029
0030 namespace {
0031
0032
0033
0034
0035
0036
0037
0038 struct Scanner : public dd4hep::Condition::Processor {
0039 ConditionsTreePersistency::pool_type& pool;
0040
0041 Scanner(ConditionsTreePersistency::pool_type& p) : pool(p) {}
0042
0043 virtual int process(dd4hep::Condition c) const override {
0044 pool.emplace_back(c.ptr());
0045 return 1;
0046 }
0047 };
0048 struct DurationStamp {
0049 TTimeStamp start;
0050 ConditionsTreePersistency* object = 0;
0051 DurationStamp(ConditionsTreePersistency* obj) : object(obj) {
0052 }
0053 ~DurationStamp() {
0054 TTimeStamp stop;
0055 object->duration = stop.AsDouble()-start.AsDouble();
0056 }
0057 };
0058 }
0059
0060
0061 ConditionsTreePersistency::ConditionsTreePersistency() : TNamed() {
0062 }
0063
0064
0065 ConditionsTreePersistency::ConditionsTreePersistency(const std::string& name, const std::string& title)
0066 : TNamed(name.c_str(), title.c_str())
0067 {
0068 }
0069
0070
0071 ConditionsTreePersistency::~ConditionsTreePersistency() {
0072 clear();
0073 }
0074
0075
0076 std::size_t ConditionsTreePersistency::add(const std::string& identifier,
0077 const IOV& iov,
0078 std::vector<Condition>& conditions)
0079 {
0080 DurationStamp stamp(this);
0081 conditionPools.emplace_back(std::pair<iov_key_type, pool_type>());
0082 pool_type& ent = conditionPools.back().second;
0083 iov_key_type& key = conditionPools.back().first;
0084 key.first = identifier;
0085 key.second.first = make_pair(iov.iovType->name,iov.type);
0086 key.second.second = iov.key();
0087 ent = conditions;
0088 for(auto c : ent) c->addRef();
0089 return ent.size();
0090 }
0091
0092
0093 std::size_t ConditionsTreePersistency::add(const std::string& identifier, ConditionsPool& pool) {
0094 DurationStamp stamp(this);
0095 conditionPools.emplace_back(std::pair<iov_key_type, pool_type>());
0096 pool_type& ent = conditionPools.back().second;
0097 iov_key_type& key = conditionPools.back().first;
0098 const IOV* iov = pool.iov;
0099 key.first = identifier;
0100 key.second.first = make_pair(iov->iovType->name,iov->type);
0101 key.second.second = iov->key();
0102 pool.select_all(ent);
0103 for(auto c : ent) c.ptr()->addRef();
0104 return ent.size();
0105 }
0106
0107
0108 std::size_t ConditionsTreePersistency::add(const std::string& identifier, const ConditionsIOVPool& pool) {
0109 std::size_t count = 0;
0110 DurationStamp stamp(this);
0111 for( const auto& p : pool.elements ) {
0112 iovPools.emplace_back(std::pair<iov_key_type, pool_type>());
0113 pool_type& ent = iovPools.back().second;
0114 iov_key_type& key = iovPools.back().first;
0115 const IOV* iov = p.second->iov;
0116 key.first = identifier;
0117 key.second.first = make_pair(iov->iovType->name,iov->type);
0118 key.second.second = p.first;
0119 p.second->select_all(ent);
0120 for(auto c : ent) c.ptr()->addRef();
0121 count += ent.size();
0122 }
0123 return count;
0124 }
0125
0126
0127 TFile* ConditionsTreePersistency::openFile(const std::string& fname) {
0128 TDirectory::TContext context;
0129 TFile* file = TFile::Open(fname.c_str());
0130 if ( file && !file->IsZombie()) return file;
0131 except("ConditionsTreePersistency","+++ FAILED to open ROOT file %s in read-mode.",fname.c_str());
0132 return 0;
0133 }
0134
0135
0136 void ConditionsTreePersistency::_clear(persistent_type& pool) {
0137
0138 for (auto& p : pool ) {
0139 for(Condition c : p.second )
0140 c.ptr()->release();
0141 p.second.clear();
0142 }
0143 pool.clear();
0144 }
0145
0146
0147 void ConditionsTreePersistency::clear() {
0148
0149 _clear(conditionPools);
0150 _clear(iovPools);
0151 }
0152
0153
0154 std::unique_ptr<ConditionsTreePersistency>
0155 ConditionsTreePersistency::load(TFile* file,const std::string& obj) {
0156 std::unique_ptr<ConditionsTreePersistency> p;
0157 if ( file && !file->IsZombie()) {
0158 TTimeStamp start;
0159 p.reset((ConditionsTreePersistency*)file->Get(obj.c_str()));
0160 TTimeStamp stop;
0161 if ( p.get() ) {
0162 p->duration = stop.AsDouble()-start.AsDouble();
0163 return p;
0164 }
0165 except("ConditionsTreePersistency",
0166 "+++ FAILED to load object %s from file %s",
0167 obj.c_str(), file->GetName());
0168 }
0169 except("ConditionsTreePersistency",
0170 "+++ FAILED to load object %s from file [Invalid file]",obj.c_str());
0171 return p;
0172 }
0173
0174
0175 std::size_t ConditionsTreePersistency::_import(ImportStrategy strategy,
0176 persistent_type& persistent_pools,
0177 const std::string& id,
0178 const std::string& iov_type,
0179 const IOV::Key& iov_key,
0180 ConditionsManager mgr)
0181 {
0182 std::size_t count = 0;
0183 std::pair<bool,const IOVType*> iovTyp(false,0);
0184 for (auto& iovp : persistent_pools ) {
0185 bool use = false;
0186 const iov_key_type& key = iovp.first;
0187 if ( !(id.empty() || id=="*" || key.first == id) )
0188 continue;
0189 if ( !(iov_type.empty() || iov_type == "*" || key.second.first.first == iov_type) )
0190 continue;
0191 switch(strategy) {
0192 case IMPORT_ALL:
0193 use = true;
0194 break;
0195 case IMPORT_EXACT:
0196 use = key.second.second == iov_key;
0197 break;
0198 case IMPORT_CONTAINED:
0199 use = IOV::key_is_contained(key.second.second,iov_key);
0200 break;
0201 case IMPORT_EDGE_LOWER:
0202 use = IOV::key_overlaps_lower_end(key.second.second,iov_key);
0203 break;
0204 case IMPORT_EDGE_UPPER:
0205 use = IOV::key_overlaps_higher_end(key.second.second,iov_key);
0206 break;
0207 case IMPORT_CONTAINED_LOWER:
0208 use = IOV::key_is_contained(key.second.second,iov_key) ||
0209 IOV::key_overlaps_lower_end(key.second.second,iov_key);
0210 break;
0211 case IMPORT_CONTAINED_UPPER:
0212 use = IOV::key_is_contained(key.second.second,iov_key) ||
0213 IOV::key_overlaps_higher_end(key.second.second,iov_key);
0214 break;
0215 default:
0216 use = false;
0217 break;
0218 }
0219 if ( !use )
0220 continue;
0221
0222 iovTyp = mgr.registerIOVType(key.second.first.second,key.second.first.first);
0223 if ( iovTyp.second ) {
0224 ConditionsPool* pool = mgr.registerIOV(*iovTyp.second, key.second.second);
0225 for (Condition c : iovp.second) {
0226 Condition::Object* o = c.ptr();
0227 o->iov = pool->iov;
0228 if ( pool->insert(o->addRef()) ) {
0229
0230
0231 ++count;
0232 }
0233 else {
0234 printout(WARNING,"ConditionsTreePersistency",
0235 "+++ Ignore condition %s from %s iov:%s [Already present]",
0236 c.name(),id.c_str(), iov_type.c_str());
0237 }
0238 }
0239 }
0240 }
0241 return count;
0242 }
0243
0244
0245 std::size_t ConditionsTreePersistency::importIOVPool(const std::string& identifier,
0246 const std::string& iov_type,
0247 ConditionsManager mgr)
0248 {
0249 DurationStamp stamp(this);
0250 return _import(IMPORT_ALL,iovPools,identifier,iov_type,IOV::Key(),mgr);
0251 }
0252
0253
0254 std::size_t ConditionsTreePersistency::importConditionsPool(const std::string& identifier,
0255 const std::string& iov_type,
0256 ConditionsManager mgr)
0257 {
0258 DurationStamp stamp(this);
0259 return _import(IMPORT_ALL,conditionPools,identifier,iov_type,IOV::Key(),mgr);
0260 }
0261
0262
0263 std::size_t ConditionsTreePersistency::importConditionsPool(ImportStrategy strategy,
0264 const std::string& identifier,
0265 const std::string& iov_type,
0266 const IOV::Key& iov_key,
0267 ConditionsManager mgr) {
0268 return _import(strategy,conditionPools,identifier,iov_type,iov_key,mgr);
0269 }
0270
0271
0272 int ConditionsTreePersistency::save(TFile* file) {
0273 DurationStamp stamp(this);
0274
0275 int nBytes = file->WriteTObject(this,GetName());
0276 return nBytes;
0277 }
0278
0279
0280 int ConditionsTreePersistency::save(const std::string& fname) {
0281 DurationStamp stamp(this);
0282
0283 TFile* file = TFile::Open(fname.c_str(),"RECREATE");
0284 if ( file && !file->IsZombie()) {
0285 int nBytes = save(file);
0286 file->Close();
0287 delete file;
0288 return nBytes;
0289 }
0290 return -1;
0291 }