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/ConditionsTreePersistency.h>
0019 
0020 #include <TFile.h>
0021 #include <TTimeStamp.h>
0022 
0023 typedef dd4hep::cond::ConditionsTreePersistency __ConditionsTreePersistency;
0024 /// ROOT Class implementation directive
0025 ClassImp(__ConditionsTreePersistency)
0026 
0027 using namespace dd4hep::cond;
0028 
0029 // Local namespace for anonymous stuff
0030 namespace  {
0031 
0032   /// Helper to select conditions
0033   /*
0034    *  \author  M.Frank
0035    *  \version 1.0
0036    *  \ingroup DD4HEP_CONDITIONS
0037    */
0038   struct Scanner : public dd4hep::Condition::Processor   {
0039     ConditionsTreePersistency::pool_type& pool;
0040     /// Constructor
0041     Scanner(ConditionsTreePersistency::pool_type& p) : pool(p) {}
0042     /// Conditions callback for object processing
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 /// Default constructor
0061 ConditionsTreePersistency::ConditionsTreePersistency() : TNamed()   {
0062 }
0063 
0064 /// Initializing constructor
0065 ConditionsTreePersistency::ConditionsTreePersistency(const std::string& name, const std::string& title)
0066   : TNamed(name.c_str(), title.c_str())
0067 {
0068 }
0069 
0070 /// Default destructor
0071 ConditionsTreePersistency::~ConditionsTreePersistency()    {
0072   clear();
0073 }
0074 
0075 /// Add conditions content to be saved. Note, that dependent conditions shall not be saved!
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 /// Add conditions content to the saved. Note, that dependent conditions shall not be saved!
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 /// Add conditions content to the saved. Note, that dependent conditions shall not be saved!
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 /// Open ROOT file in read mode
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 /// Clear object content and release allocated memory
0136 void ConditionsTreePersistency::_clear(persistent_type& pool)  {
0137   /// Cleanup all the stuff not useful....
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 /// Clear object content and release allocated memory
0147 void ConditionsTreePersistency::clear()  {
0148   /// Cleanup all the stuff not useful....
0149   _clear(conditionPools);
0150   _clear(iovPools);
0151 }
0152 
0153 /// Add conditions content to the saved. Note, that dependent conditions shall not be saved!
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 /// Load ConditionsPool(s) and populate conditions manager
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           //printout(WARNING,"ConditionsTreePersistency","%p: [%016llX] %s -> %s",
0230           //         o, o->hash,o->GetName(),o->data.str().c_str());
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 /// Load ConditionsIOVPool and populate conditions manager
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 /// Load ConditionsIOVPool and populate conditions manager
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 /// Load conditions pool and populate conditions manager. Allow tro be selective also for the key
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 /// Save the data content to a root file
0272 int ConditionsTreePersistency::save(TFile* file)    {
0273   DurationStamp stamp(this);
0274   //TDirectory::TContext context;
0275   int nBytes = file->WriteTObject(this,GetName());
0276   return nBytes;
0277 }
0278 
0279 /// Save the data content to a root file
0280 int ConditionsTreePersistency::save(const std::string& fname)    {
0281   DurationStamp stamp(this);
0282   //TDirectory::TContext context;
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 }