Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 09:16:31

0001 //==========================================================================
0002 //  AIDA Detector description implementation 
0003 //--------------------------------------------------------------------------
0004 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
0005 // All rights reserved.
0006 //
0007 // For the licensing terms see $DD4hepINSTALL/LICENSE.
0008 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
0009 //
0010 // Author     : M.Frank
0011 //
0012 //==========================================================================
0013 
0014 // Framework include files
0015 #include <DD4hep/Printout.h>
0016 #include <DDCond/ConditionsSlice.h>
0017 #include <DDCond/ConditionsIOVPool.h>
0018 #include <DDCond/ConditionsRootPersistency.h>
0019 
0020 #include <TFile.h>
0021 #include <TTimeStamp.h>
0022 
0023 typedef dd4hep::cond::ConditionsRootPersistency __ConditionsRootPersistency;
0024 
0025 /// ROOT Class implementation directive
0026 ClassImp(__ConditionsRootPersistency)
0027 
0028 using namespace dd4hep::cond;
0029 
0030 // Local namespace for anonymous stuff
0031 namespace  {
0032 
0033   /// Helper to select conditions
0034   /*
0035    *  \author  M.Frank
0036    *  \version 1.0
0037    *  \ingroup DD4HEP_CONDITIONS
0038    */
0039   struct Scanner : public dd4hep::Condition::Processor   {
0040     ConditionsRootPersistency::pool_type& pool;
0041     /// Constructor
0042     Scanner(ConditionsRootPersistency::pool_type& p) : pool(p) {}
0043     /// Conditions callback for object processing
0044     virtual int process(dd4hep::Condition c)  const override  {
0045       pool.emplace_back(c.ptr());
0046       return 1;
0047     }
0048   };
0049   struct DurationStamp  {
0050     TTimeStamp start;
0051     ConditionsRootPersistency* object = 0;
0052     DurationStamp(ConditionsRootPersistency* obj) : object(obj)  {
0053     }
0054     ~DurationStamp()  {
0055       TTimeStamp stop;
0056       object->duration = stop.AsDouble()-start.AsDouble();
0057     }
0058   };
0059 }
0060 
0061 /// Default constructor
0062 ConditionsRootPersistency::ConditionsRootPersistency() : TNamed()   {
0063 }
0064 
0065 /// Initializing constructor
0066 ConditionsRootPersistency::ConditionsRootPersistency(const std::string& name, const std::string& title)
0067   : TNamed(name.c_str(), title.c_str())
0068 {
0069 }
0070 
0071 /// Default destructor
0072 ConditionsRootPersistency::~ConditionsRootPersistency()    {
0073   clear();
0074 }
0075 
0076 /// Add conditions content to be saved. Note, that dependent conditions shall not be saved!
0077 std::size_t ConditionsRootPersistency::add(const std::string& identifier,
0078                                            const IOV& iov,
0079                                            std::vector<Condition>& conditions)
0080 {
0081   DurationStamp stamp(this);
0082   conditionPools.emplace_back(std::pair<iov_key_type, pool_type>());
0083   pool_type&    ent = conditionPools.back().second;
0084   iov_key_type& key = conditionPools.back().first;
0085   key.first         = identifier;
0086   key.second.first  = make_pair(iov.iovType->name,iov.type);
0087   key.second.second = iov.key();
0088   ent               = conditions;
0089   for(auto c : ent) c.ptr()->addRef();
0090   return ent.size();
0091 }
0092 
0093 /// Add conditions content to the saved. Note, that dependent conditions shall not be saved!
0094 size_t ConditionsRootPersistency::add(const std::string& identifier, ConditionsPool& pool)    {
0095   DurationStamp stamp(this);
0096   conditionPools.emplace_back(std::pair<iov_key_type, pool_type>());
0097   pool_type&    ent = conditionPools.back().second;
0098   iov_key_type& key = conditionPools.back().first;
0099   const IOV*    iov = pool.iov;
0100   key.first         = identifier;
0101   key.second.first  = make_pair(iov->iovType->name,iov->type);
0102   key.second.second = iov->key();
0103   pool.select_all(ent);
0104   for(auto c : ent) c.ptr()->addRef();
0105   return ent.size();
0106 }
0107 
0108 /// Add conditions content to the saved. Note, that dependent conditions shall not be saved!
0109 size_t ConditionsRootPersistency::add(const std::string& identifier, const ConditionsIOVPool& pool)    {
0110   size_t count = 0;
0111   DurationStamp stamp(this);
0112   for( const auto& p : pool.elements )  {
0113     iovPools.emplace_back(std::pair<iov_key_type, pool_type>());
0114     pool_type&    ent = iovPools.back().second;
0115     iov_key_type& key = iovPools.back().first;
0116     const IOV*    iov = p.second->iov;
0117     key.first         = identifier;
0118     key.second.first  = make_pair(iov->iovType->name,iov->type);
0119     key.second.second = p.first;
0120     p.second->select_all(ent);
0121     for(auto c : ent) c.ptr()->addRef();
0122     count += ent.size();
0123   }
0124   return count;
0125 }
0126 
0127 /// Add conditions content to the saved. Note, that dependent conditions shall not be saved!
0128 size_t ConditionsRootPersistency::add(const std::string& identifier, const UserPool& pool)    {
0129   DurationStamp stamp(this);
0130   userPools.emplace_back(std::pair<iov_key_type, pool_type>());
0131   pool_type&    ent = userPools.back().second;
0132   iov_key_type& key = userPools.back().first;
0133   const IOV&    iov = pool.validity();
0134   key.first         = identifier;
0135   key.second.first  = make_pair(iov.iovType->name,iov.type);
0136   key.second.second = iov.key();
0137   pool.scan(Scanner(ent));
0138   for(auto c : ent) c.ptr()->addRef();
0139   return ent.size();
0140 }
0141 
0142 /// Open ROOT file in read mode
0143 TFile* ConditionsRootPersistency::openFile(const std::string& fname)     {
0144   TDirectory::TContext context;
0145   TFile* file = TFile::Open(fname.c_str());
0146   if ( file && !file->IsZombie()) return file;
0147   except("ConditionsRootPersistency","+++ FAILED to open ROOT file %s in read-mode.",fname.c_str());
0148   return 0;
0149 }
0150 
0151 /// Clear object content and release allocated memory
0152 void ConditionsRootPersistency::_clear(persistent_type& pool)  {
0153   /// Cleanup all the stuff not useful....
0154   for (auto& p : pool )  {
0155     for(Condition c : p.second )
0156       c.ptr()->release();
0157     p.second.clear();
0158   }
0159   pool.clear();
0160 }
0161 
0162 /// Clear object content and release allocated memory
0163 void ConditionsRootPersistency::clear()  {
0164   /// Cleanup all the stuff not useful....
0165   _clear(conditionPools);
0166   _clear(userPools);
0167   _clear(iovPools);
0168 }
0169 
0170 /// Add conditions content to the saved. Note, that dependent conditions shall not be saved!
0171 std::unique_ptr<ConditionsRootPersistency>
0172 ConditionsRootPersistency::load(TFile* file,const std::string& obj)   {
0173   std::unique_ptr<ConditionsRootPersistency> p;
0174   if ( file && !file->IsZombie())    {
0175     TTimeStamp start;
0176     p.reset((ConditionsRootPersistency*)file->Get(obj.c_str()));
0177     TTimeStamp stop;
0178     if ( p.get() )    {
0179       p->duration = stop.AsDouble()-start.AsDouble();
0180       return p;
0181     }
0182     except("ConditionsRootPersistency",
0183            "+++ FAILED to load object %s from file %s",
0184            obj.c_str(), file->GetName());
0185   }
0186   except("ConditionsRootPersistency",
0187          "+++ FAILED to load object %s from file [Invalid file]",obj.c_str());
0188   return p;
0189 }
0190 
0191 /// Load ConditionsPool(s) and populate conditions manager
0192 size_t ConditionsRootPersistency::_import(ImportStrategy     strategy,
0193                                           persistent_type&   persistent_pools,
0194                                           const std::string& id,
0195                                           const std::string& iov_type,
0196                                           const IOV::Key&    iov_key,
0197                                           ConditionsManager  mgr)   {
0198   size_t count = 0;
0199   std::pair<bool,const IOVType*> iovTyp(false,0);
0200   for (auto& iovp : persistent_pools )   {
0201     bool use = false;
0202     const iov_key_type& key = iovp.first;
0203     if ( !(id.empty() || id=="*" || key.first == id) )
0204       continue;
0205     if ( !(iov_type.empty() || iov_type == "*" || key.second.first.first == iov_type) )
0206       continue;
0207     switch(strategy)   {
0208     case IMPORT_ALL:
0209       use = true;
0210       break;
0211     case IMPORT_EXACT:
0212       use = key.second.second == iov_key;
0213       break;
0214     case IMPORT_CONTAINED:
0215       use = IOV::key_is_contained(key.second.second,iov_key);
0216       break;
0217     case IMPORT_EDGE_LOWER:
0218       use = IOV::key_overlaps_lower_end(key.second.second,iov_key);
0219       break;
0220     case IMPORT_EDGE_UPPER:
0221       use = IOV::key_overlaps_higher_end(key.second.second,iov_key);
0222       break;
0223     case IMPORT_CONTAINED_LOWER:
0224       use = IOV::key_is_contained(key.second.second,iov_key) ||
0225         IOV::key_overlaps_lower_end(key.second.second,iov_key);
0226       break;
0227     case IMPORT_CONTAINED_UPPER:
0228       use = IOV::key_is_contained(key.second.second,iov_key) ||
0229         IOV::key_overlaps_higher_end(key.second.second,iov_key);
0230       break;
0231     default:
0232       use = false;
0233       break;
0234     }
0235     if ( !use )
0236       continue;
0237 
0238     iovTyp = mgr.registerIOVType(key.second.first.second,key.second.first.first);
0239     if ( iovTyp.second )   {
0240       ConditionsPool* pool = mgr.registerIOV(*iovTyp.second, key.second.second);
0241       for (Condition c : iovp.second)   {
0242         Condition::Object* o = c.ptr();
0243         o->iov = pool->iov;
0244         if ( pool->insert(o->addRef()) )    {
0245           //printout(WARNING,"ConditionsRootPersistency","%p: [%016llX] %s -> %s",
0246           //         o, o->hash,o->GetName(),o->data.str().c_str());
0247           ++count;
0248         }
0249         else   {
0250           printout(WARNING,"ConditionsRootPersistency",
0251                    "+++ Ignore condition %s from %s iov:%s [Already present]",
0252                    c.name(),id.c_str(), iov_type.c_str());
0253         }
0254       }
0255     }
0256   }
0257   return count;
0258 }
0259 
0260 /// Load ConditionsIOVPool and populate conditions manager
0261 size_t ConditionsRootPersistency::importIOVPool(const std::string& identifier,
0262                                                 const std::string& iov_type,
0263                                                 ConditionsManager  mgr)
0264 {
0265   DurationStamp stamp(this);
0266   return _import(IMPORT_ALL,iovPools,identifier,iov_type,IOV::Key(),mgr);
0267 }
0268 
0269 /// Load ConditionsIOVPool and populate conditions manager
0270 size_t ConditionsRootPersistency::importUserPool(const std::string& identifier,
0271                                                  const std::string& iov_type,
0272                                                  ConditionsManager  mgr)
0273 {
0274   DurationStamp stamp(this);
0275   return _import(IMPORT_ALL,userPools,identifier,iov_type,IOV::Key(),mgr);
0276 }
0277 
0278 /// Load ConditionsIOVPool and populate conditions manager
0279 size_t ConditionsRootPersistency::importConditionsPool(const std::string& identifier,
0280                                                        const std::string& iov_type,
0281                                                        ConditionsManager  mgr)
0282 {
0283   DurationStamp stamp(this);
0284   return _import(IMPORT_ALL,conditionPools,identifier,iov_type,IOV::Key(),mgr);
0285 }
0286 
0287 /// Load conditions pool and populate conditions manager. Allow tro be selective also for the key
0288 size_t ConditionsRootPersistency::importConditionsPool(ImportStrategy     strategy,
0289                                                        const std::string& identifier,
0290                                                        const std::string& iov_type,
0291                                                        const IOV::Key&    iov_key,
0292                                                        ConditionsManager  mgr)   {
0293   return _import(strategy,conditionPools,identifier,iov_type,iov_key,mgr);
0294 }
0295 
0296 /// Save the data content to a root file
0297 int ConditionsRootPersistency::save(TFile* file)    {
0298   DurationStamp stamp(this);
0299   //TDirectory::TContext context;
0300   int nBytes = file->WriteTObject(this,GetName());
0301   return nBytes;
0302 }
0303 
0304 /// Save the data content to a root file
0305 int ConditionsRootPersistency::save(const std::string& fname)    {
0306   DurationStamp stamp(this);
0307   //TDirectory::TContext context;
0308   TFile* file = TFile::Open(fname.c_str(),"RECREATE");
0309   if ( file && !file->IsZombie())   {
0310     int nBytes = save(file);
0311     file->Close();
0312     delete file;
0313     return nBytes;
0314   }
0315   return -1;
0316 }