diff --git a/DDCond/include/DDCond/ConditionsRootPersistency.h b/DDCond/include/DDCond/ConditionsRootPersistency.h index 00c1b13518a0cea53f2b0098b226220a6c25a67d..ac0cee4ef7749b1fa2ea63daefc1aea7f1df968a 100644 --- a/DDCond/include/DDCond/ConditionsRootPersistency.h +++ b/DDCond/include/DDCond/ConditionsRootPersistency.h @@ -51,12 +51,23 @@ namespace dd4hep { persistent_type userPools; persistent_type iovPools; float duration; - + enum ImportStrategy { + IMPORT_ALL = 1<<0, + IMPORT_EXACT = 1<<1, + IMPORT_CONTAINED = 1<<2, + IMPORT_CONTAINED_LOWER = 1<<3, + IMPORT_CONTAINED_UPPER = 1<<4, + IMPORT_EDGE_LOWER = 1<<5, + IMPORT_EDGE_UPPER = 1<<6, + LAST + }; /// Load ConditionsIOVPool and populate conditions manager - size_t _import(persistent_type& pers, + size_t _import(ImportStrategy strategy, + persistent_type& pers, const std::string& id, const std::string& iov_type, - ConditionsManager mgr); + const IOV::Key& iov_key, + ConditionsManager mgr); /// Clear object content and release allocated memory void _clear(persistent_type& pool); @@ -78,11 +89,13 @@ namespace dd4hep { /// Open ROOT file in read mode static TFile* openFile(const std::string& fname); - /// Add conditions content to the saved. Note, that dependent conditions shall not be saved! + /// Add conditions content to be saved. Note, that dependent conditions shall not be saved! + 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! size_t add(const std::string& identifier, ConditionsPool& pool); - /// Add conditions content to the saved. Note, that dependent conditions shall not be saved! + /// Add conditions content to be saved. Note, that dependent conditions shall not be saved! size_t add(const std::string& identifier, const UserPool& pool); - /// Add conditions content to the saved. Note, that dependent conditions shall not be saved! + /// Add conditions content to be saved. Note, that dependent conditions shall not be saved! size_t add(const std::string& identifier, const ConditionsIOVPool& pool); /// Load conditions content from file. @@ -94,11 +107,18 @@ namespace dd4hep { } /// Load conditions IOV pool and populate conditions manager - size_t importIOVPool(const std::string& id,const std::string& iov_type,ConditionsManager mgr); + size_t importIOVPool(const std::string& id, const std::string& iov_type, ConditionsManager mgr); /// Load conditions user pool and populate conditions manager - size_t importUserPool(const std::string& id,const std::string& iov_type,ConditionsManager mgr); + size_t importUserPool(const std::string& id, const std::string& iov_type, ConditionsManager mgr); /// Load conditions pool and populate conditions manager - size_t importConditionsPool(const std::string& id,const std::string& iov_type,ConditionsManager mgr); + size_t importConditionsPool(const std::string& id, const std::string& iov_type, ConditionsManager mgr); + + /// Load conditions pool and populate conditions manager. Allow tro be selective also for the key + size_t importConditionsPool(ImportStrategy strategy, + const std::string& id, + const std::string& iov_type, + const IOV::Key& key, + ConditionsManager mgr); /// Save the data content to a root file int save(TFile* file); diff --git a/DDCond/src/ConditionsRootPersistency.cpp b/DDCond/src/ConditionsRootPersistency.cpp index 9a281add82fe5955bf0d5255ddad49431769bd6c..4ac7bd3831fed2628e6998eb8c29bfd2a1967643 100644 --- a/DDCond/src/ConditionsRootPersistency.cpp +++ b/DDCond/src/ConditionsRootPersistency.cpp @@ -72,6 +72,22 @@ ConditionsRootPersistency::~ConditionsRootPersistency() { clear(); } +/// Add conditions content to be saved. Note, that dependent conditions shall not be saved! +size_t ConditionsRootPersistency::add(const std::string& identifier, + const IOV& iov, + std::vector<Condition>& conditions) { + DurationStamp stamp(this); + conditionPools.push_back(pair<iov_key_type, pool_type>()); + pool_type& ent = conditionPools.back().second; + iov_key_type& key = conditionPools.back().first; + key.first = identifier; + key.second.first = make_pair(iov.iovType->name,iov.type); + key.second.second = iov.key(); + ent = conditions; + for(auto c : ent) c.ptr()->addRef(); + return ent.size(); +} + /// Add conditions content to the saved. Note, that dependent conditions shall not be saved! size_t ConditionsRootPersistency::add(const string& identifier, ConditionsPool& pool) { DurationStamp stamp(this); @@ -171,18 +187,52 @@ ConditionsRootPersistency::load(TFile* file,const string& obj) { } /// Load ConditionsPool(s) and populate conditions manager -size_t ConditionsRootPersistency::_import(persistent_type& persistent_pools, +size_t ConditionsRootPersistency::_import(ImportStrategy strategy, + persistent_type& persistent_pools, const std::string& id, const std::string& iov_type, - ConditionsManager mgr) { + const IOV::Key& iov_key, + ConditionsManager mgr) { size_t count = 0; std::pair<bool,const IOVType*> iovTyp(false,0); for (auto& iovp : persistent_pools ) { + bool use = false; const iov_key_type& key = iovp.first; if ( !(id.empty() || id=="*" || key.first == id) ) continue; if ( !(iov_type.empty() || iov_type == "*" || key.second.first.first == iov_type) ) continue; + switch(strategy) { + case IMPORT_ALL: + use = true; + break; + case IMPORT_EXACT: + use = key.second.second == iov_key; + break; + case IMPORT_CONTAINED: + use = IOV::key_is_contained(key.second.second,iov_key); + break; + case IMPORT_EDGE_LOWER: + use = IOV::key_overlaps_lower_end(key.second.second,iov_key); + break; + case IMPORT_EDGE_UPPER: + use = IOV::key_overlaps_higher_end(key.second.second,iov_key); + break; + case IMPORT_CONTAINED_LOWER: + use = IOV::key_is_contained(key.second.second,iov_key) || + IOV::key_overlaps_lower_end(key.second.second,iov_key); + break; + case IMPORT_CONTAINED_UPPER: + use = IOV::key_is_contained(key.second.second,iov_key) || + IOV::key_overlaps_higher_end(key.second.second,iov_key); + break; + default: + use = false; + break; + } + if ( !use ) + continue; + iovTyp = mgr.registerIOVType(key.second.first.second,key.second.first.first); if ( iovTyp.second ) { ConditionsPool* pool = mgr.registerIOV(*iovTyp.second, key.second.second); @@ -211,7 +261,7 @@ size_t ConditionsRootPersistency::importIOVPool(const std::string& identifier, ConditionsManager mgr) { DurationStamp stamp(this); - return _import(iovPools,identifier,iov_type,mgr); + return _import(IMPORT_ALL,iovPools,identifier,iov_type,IOV::Key(),mgr); } /// Load ConditionsIOVPool and populate conditions manager @@ -220,7 +270,7 @@ size_t ConditionsRootPersistency::importUserPool(const std::string& identifier, ConditionsManager mgr) { DurationStamp stamp(this); - return _import(userPools,identifier,iov_type,mgr); + return _import(IMPORT_ALL,userPools,identifier,iov_type,IOV::Key(),mgr); } /// Load ConditionsIOVPool and populate conditions manager @@ -229,7 +279,16 @@ size_t ConditionsRootPersistency::importConditionsPool(const std::string& identi ConditionsManager mgr) { DurationStamp stamp(this); - return _import(conditionPools,identifier,iov_type,mgr); + return _import(IMPORT_ALL,conditionPools,identifier,iov_type,IOV::Key(),mgr); +} + +/// Load conditions pool and populate conditions manager. Allow tro be selective also for the key +size_t ConditionsRootPersistency::importConditionsPool(ImportStrategy strategy, + const std::string& identifier, + const std::string& iov_type, + const IOV::Key& iov_key, + ConditionsManager mgr) { + return _import(strategy,conditionPools,identifier,iov_type,iov_key,mgr); } /// Save the data content to a root file diff --git a/examples/Conditions/CMakeLists.txt b/examples/Conditions/CMakeLists.txt index 8a256ca380a176bd0f4ee990af2c254a8dc5458f..de428837c62162ba8098cf9791b731ddf2062fa6 100644 --- a/examples/Conditions/CMakeLists.txt +++ b/examples/Conditions/CMakeLists.txt @@ -94,12 +94,32 @@ dd4hep_add_test_reg( Conditions_Telescope_root_save ) # #---Testing: Save conditions to ROOT file -dd4hep_add_test_reg( Conditions_Telescope_root_load +dd4hep_add_test_reg( Conditions_Telescope_root_load_iov COMMAND "${CMAKE_INSTALL_PREFIX}/bin/run_test_Conditions.sh" EXEC_ARGS geoPluginRun -print WARNING -volmgr -destroy -plugin DD4hep_ConditionExample_load -input file:${DD4hep_DIR}/examples/AlignDet/compact/Telescope.xml - -conditions TelescopeConditions.root - REGEX_PASS "\\+ Accessed a total of 1600 conditions \\(S: 1600,L: 0,C: 0,M:0\\)" + -conditions TelescopeConditions.root -iovs 30 -restore iovpool + REGEX_PASS "\\+ Accessed a total of 4800 conditions \\(S: 4800,L: 0,C: 0,M:0\\)" + REGEX_FAIL " ERROR ;EXCEPTION;Exception" + ) +# +#---Testing: Save conditions to ROOT file +dd4hep_add_test_reg( Conditions_Telescope_root_load_usr + COMMAND "${CMAKE_INSTALL_PREFIX}/bin/run_test_Conditions.sh" + EXEC_ARGS geoPluginRun -print WARNING -volmgr -destroy -plugin DD4hep_ConditionExample_load + -input file:${DD4hep_DIR}/examples/AlignDet/compact/Telescope.xml + -conditions TelescopeConditions.root -iovs 30 -restore userpool + REGEX_PASS "\\+ Accessed a total of 4800 conditions \\(S: 4800,L: 0,C: 0,M:0\\)" + REGEX_FAIL " ERROR ;EXCEPTION;Exception" + ) +# +#---Testing: Save conditions to ROOT file +dd4hep_add_test_reg( Conditions_Telescope_root_load_pool + COMMAND "${CMAKE_INSTALL_PREFIX}/bin/run_test_Conditions.sh" + EXEC_ARGS geoPluginRun -print WARNING -volmgr -destroy -plugin DD4hep_ConditionExample_load + -input file:${DD4hep_DIR}/examples/AlignDet/compact/Telescope.xml + -conditions TelescopeConditions.root -iovs 30 -restore condpool + REGEX_PASS "\\+ Accessed a total of 4800 conditions \\(S: 4800,L: 0,C: 0,M:0\\)" REGEX_FAIL " ERROR ;EXCEPTION;Exception" ) # @@ -141,10 +161,30 @@ dd4hep_add_test_reg( Conditions_CLICSiD_root_save_LONGTEST ) # #---Testing: Save conditions to ROOT file -dd4hep_add_test_reg( Conditions_CLICSiD_root_load_LONGTEST +dd4hep_add_test_reg( Conditions_CLICSiD_root_load_iov_LONGTEST COMMAND "${CMAKE_INSTALL_PREFIX}/bin/run_test_Conditions.sh" EXEC_ARGS geoPluginRun -print WARNING -volmgr -destroy -plugin DD4hep_ConditionExample_load - -input file:${DD4hep_DIR}/examples/CLICSiD/compact/compact.xml -iovs 3 + -input file:${DD4hep_DIR}/examples/CLICSiD/compact/compact.xml -iovs 3 -restore iovpool + -conditions CLICSiDConditions.root + REGEX_PASS "\\+ Accessed a total of 840264 conditions \\(S:840264,L: 0,C: 0,M:0\\)" + REGEX_FAIL " ERROR ;EXCEPTION;Exception" + ) +# +#---Testing: Save conditions to ROOT file +dd4hep_add_test_reg( Conditions_CLICSiD_root_load_usr_LONGTEST + COMMAND "${CMAKE_INSTALL_PREFIX}/bin/run_test_Conditions.sh" + EXEC_ARGS geoPluginRun -print WARNING -volmgr -destroy -plugin DD4hep_ConditionExample_load + -input file:${DD4hep_DIR}/examples/CLICSiD/compact/compact.xml -iovs 3 -restore userpool + -conditions CLICSiDConditions.root + REGEX_PASS "\\+ Accessed a total of 840264 conditions \\(S:840264,L: 0,C: 0,M:0\\)" + REGEX_FAIL " ERROR ;EXCEPTION;Exception" + ) +# +#---Testing: Save conditions to ROOT file +dd4hep_add_test_reg( Conditions_CLICSiD_root_load_cond_LONGTEST + COMMAND "${CMAKE_INSTALL_PREFIX}/bin/run_test_Conditions.sh" + EXEC_ARGS geoPluginRun -print WARNING -volmgr -destroy -plugin DD4hep_ConditionExample_load + -input file:${DD4hep_DIR}/examples/CLICSiD/compact/compact.xml -iovs 3 -restore condpool -conditions CLICSiDConditions.root REGEX_PASS "\\+ Accessed a total of 840264 conditions \\(S:840264,L: 0,C: 0,M:0\\)" REGEX_FAIL " ERROR ;EXCEPTION;Exception" diff --git a/examples/Conditions/src/ConditionExample_load.cpp b/examples/Conditions/src/ConditionExample_load.cpp index a333bc179ea3a3c1707f78a5d8ba4c3bc740b529..e5b81bd8ac1502443783b1c1f1d7ae9176a80a59 100644 --- a/examples/Conditions/src/ConditionExample_load.cpp +++ b/examples/Conditions/src/ConditionExample_load.cpp @@ -35,6 +35,19 @@ using namespace std; using namespace dd4hep; using namespace dd4hep::ConditionExamples; +static void help(int argc, char** argv) { + /// Help printout describing the basic command line interface + cout << + "Usage: -plugin <name> -arg [-arg] \n" + " name: factory name DD4hep_ConditionExample_load \n" + " -input <string> Geometry file \n" + " -conditions <string> Conditions input file \n" + " -iovs <number> Number of parallel IOV slots for processing. \n" + " -restore <string> Restore strategy: iovpool, userpool or condpool.\n" + "\tArguments given: " << arguments(argc,argv) << endl << flush; + ::exit(EINVAL); +} + /// Plugin function: Condition program example /** * Factory: DD4hep_ConditionExample_load @@ -44,7 +57,7 @@ using namespace dd4hep::ConditionExamples; * \date 01/12/2016 */ static int condition_example (Detector& description, int argc, char** argv) { - string input, conditions; + string input, conditions, restore="iovpool"; int num_iov = 10; bool arg_error = false; for(int i=0; i<argc && argv[i]; ++i) { @@ -52,22 +65,14 @@ static int condition_example (Detector& description, int argc, char** argv) { input = argv[++i]; else if ( 0 == ::strncmp("-conditions",argv[i],4) ) conditions = argv[++i]; + else if ( 0 == ::strncmp("-restore",argv[i],4) ) + restore = argv[++i]; else if ( 0 == ::strncmp("-iovs",argv[i],4) ) num_iov = ::atol(argv[++i]); else arg_error = true; } - if ( arg_error || input.empty() || conditions.empty() ) { - /// Help printout describing the basic command line interface - cout << - "Usage: -plugin <name> -arg [-arg] \n" - " name: factory name DD4hep_ConditionExample_load \n" - " -input <string> Geometry file \n" - " -conditions <string> Conditions input file \n" - " -iovs <number> Number of parallel IOV slots for processing. \n" - "\tArguments given: " << arguments(argc,argv) << endl << flush; - ::exit(EINVAL); - } + if ( arg_error || input.empty() || conditions.empty() ) help(argc,argv); // First we load the geometry description.fromXML(input); @@ -85,9 +90,18 @@ static int condition_example (Detector& description, int argc, char** argv) { printout(ALWAYS,"Statistics","+========================================================================="); printout(ALWAYS,"Statistics","+ Loaded conditions object from file %s. Took %8.3f seconds.", conditions.c_str(),pers->duration); - size_t num_cond = pers->importIOVPool("ConditionsIOVPool No 1","run",manager); - printout(ALWAYS,"Statistics","+ Imported %ld conditions to IOV pool. Took %8.3f seconds.", - num_cond, pers->duration); + size_t num_cond = 0; + if ( restore == "iovpool" ) + num_cond = pers->importIOVPool("ConditionsIOVPool No 1","run",manager); + else if ( restore == "userpool" ) + num_cond = pers->importUserPool("*","run",manager); + else if ( restore == "condpool" ) + num_cond = pers->importConditionsPool("*","run",manager); + else + help(argc,argv); + + printout(ALWAYS,"Statistics","+ Imported %ld conditions from %s to IOV pool. Took %8.3f seconds.", + num_cond, restore.c_str(), pers->duration); printout(ALWAYS,"Statistics","+========================================================================="); } // ++++++++++++++++++++++++ Now compute the conditions for each of these IOVs diff --git a/examples/Conditions/src/ConditionExample_save.cpp b/examples/Conditions/src/ConditionExample_save.cpp index 996cea289b5ac6ec8f54c5c0ac83d720c3ebb3e7..d9d7b4970f659b4d3453f29ab869f3c7cb2c4e2c 100644 --- a/examples/Conditions/src/ConditionExample_save.cpp +++ b/examples/Conditions/src/ConditionExample_save.cpp @@ -44,10 +44,13 @@ using namespace dd4hep::ConditionExamples; * \date 01/12/2016 */ static int condition_example (Detector& description, int argc, char** argv) { - string input, conditions; int num_iov = 10; bool arg_error = false; + bool output_iovpool = true; + bool output_userpool = true; + bool output_condpool = true; + for(int i=0; i<argc && argv[i]; ++i) { if ( 0 == ::strncmp("-input",argv[i],4) ) input = argv[++i]; @@ -95,9 +98,6 @@ static int condition_example (Detector& description, int argc, char** argv) { } char text[132]; - bool output_iovpool = true; - bool output_userpool = true; - bool output_condpool = true; size_t count = 0, total_count = 0; auto* persist = new cond::ConditionsRootPersistency("DD4hep Conditions"); // ++++++++++++++++++++++++ Now compute the conditions for each of these IOVs @@ -122,13 +122,27 @@ static int condition_example (Detector& description, int argc, char** argv) { } } if ( output_condpool ) { + int npool = 0; cond::ConditionsIOVPool* iov_pool = manager.iovPool(*iov_typ); for( const auto& p : iov_pool->elements ) { ::snprintf(text,sizeof(text),"Conditions pool %s:[%ld,%ld]", iov_typ->name.c_str(),p.second->iov->key().first,p.second->iov->key().second); - count = persist->add(text,*p.second); + if ( (npool%2) == 0 ) { /// Check here saving ConditionsPool objects + count = persist->add(text,*p.second); + printout(ALWAYS,"Example", + "+++ ConditionsPool: Added %ld conditions " + "to persistent conditions pool.",count); + } + else { /// Check here saving std::vector<Condition> + vector<Condition> entries; + p.second->select_all(entries); + count = persist->add(text,*p.second->iov,entries); + printout(ALWAYS,"Example", + "+++ std::vector<Condition>: Added %ld conditions " + "to persistent conditions pool.",count); + } total_count += count; - printout(ALWAYS,"Example","+++ Added %ld conditions to persistent conditions pool.",count); + ++npool; } } if ( output_iovpool ) {