From c57851bf671a2721f6e2803a45fecd449e91d5e2 Mon Sep 17 00:00:00 2001 From: Markus Frank <markus.frank@cern.ch> Date: Wed, 7 Sep 2016 09:58:29 +0000 Subject: [PATCH] Improce tests --- DDAlign/include/DDAlign/AlignmentsManager.h | 55 +++++++++++++-- DDAlign/src/AlignmentsManager.cpp | 70 +++++++------------ DDAlign/src/plugins/AlignmentPlugins.cpp | 75 ++++++++++++++------- DDCond/include/DDCond/ConditionsManager.h | 4 +- DDCond/src/plugins/ConditionsPlugins.cpp | 5 +- DDDB/src/DDDBAlignmentTest.cpp | 35 ++++------ DDDB/src/DDDBDerivedCondTest.cpp | 6 +- examples/DDDB/CMakeLists.txt | 2 + 8 files changed, 148 insertions(+), 104 deletions(-) diff --git a/DDAlign/include/DDAlign/AlignmentsManager.h b/DDAlign/include/DDAlign/AlignmentsManager.h index 4afc80f5c..a626bbefc 100644 --- a/DDAlign/include/DDAlign/AlignmentsManager.h +++ b/DDAlign/include/DDAlign/AlignmentsManager.h @@ -34,6 +34,7 @@ namespace DD4hep { namespace Alignments { /// Forward declarations + class AlignContext; class AlignmentsManagerObject; /// Alignment manager instance to handle alignment dependencies @@ -54,11 +55,22 @@ namespace DD4hep { typedef Conditions::ConditionsDependencyCollection Dependencies; public: - /// Initializing constructor - AlignmentsManager(Object* p) : Handle<Object>(p) {} - /// Constructor to be used when reading the already parsed object + + /// Static accessor if installed as an extension + /** + * Note: + * + * There may very well be several instances of the + * alignment manager. Do not think this is a sort of + * 'static constructor'. This would be a big mistake!. + */ + template <typename T> static AlignmentsManager from(T& host); + + /// Default constructor + AlignmentsManager() : Handle<Object>(0) {} + /// Constructor to be used for proper handles AlignmentsManager(const Handle<Object>& e) : Handle<Object>(e) {} - /// Constructor to be used when reading the already parsed object + /// Constructor to be used for proper handles template <typename Q> AlignmentsManager(const Handle<Q>& e) : Handle<Object>(e) {} /// Initializing constructor. Creates the object! AlignmentsManager(const std::string& name); @@ -83,6 +95,41 @@ namespace DD4hep { AlignmentCondition& con); }; + + /// Alignment manager. + /** + * Uses internally the conditions mechanism to manager the alignment conditions. + * + * \author M.Frank + * \version 1.0 + * \ingroup DD4HEP_ALIGNMENTS + */ + class AlignmentsManagerObject : public NamedObject { + public: + typedef AlignmentsManager::Dependencies Dependencies; + + /// Full list of alignment dependencies + Dependencies* dependencies; + /// References to all alignment possibilities known + AlignContext* all_alignments; + + protected: + /// Compute the transformation from the closest detector element of the alignment to the world system + void to_world(AlignContext& new_alignments, UserPool& pool, DetElement det, TGeoHMatrix& mat) const; + /// Compute all alignment conditions of the lower levels + void compute(AlignContext& new_alignments, UserPool& pool, DetElement child, int level) const; + + public: + /// Initializing constructor + AlignmentsManagerObject(); + /// Default destructor + virtual ~AlignmentsManagerObject(); + /// Compute all alignment conditions of the internal dependency list + void compute(UserPool& user_pool) const; + /// Compute all alignment conditions of the specified dependency list + void compute(UserPool& user_pool, const Dependencies& deps) const; + }; + } /* End namespace Geometry */ } /* End namespace DD4hep */ #endif /* DD4HEP_DDALIGN_ALIGNMENTMANAGER_H */ diff --git a/DDAlign/src/AlignmentsManager.cpp b/DDAlign/src/AlignmentsManager.cpp index 8eddd84f1..6faa5c149 100644 --- a/DDAlign/src/AlignmentsManager.cpp +++ b/DDAlign/src/AlignmentsManager.cpp @@ -13,6 +13,7 @@ //========================================================================== // Framework include files +#include "DD4hep/LCDD.h" #include "DD4hep/Handle.inl" #include "DD4hep/Printout.h" #include "DD4hep/MatrixHelpers.h" @@ -66,56 +67,35 @@ namespace DD4hep { } }; - /// Alignment manager. - /** - * Uses internally the conditions mechanism to manager the alignment conditions. - * - * \author M.Frank - * \version 1.0 - * \ingroup DD4HEP_ALIGNMENTS - */ - class AlignmentsManagerObject : public NamedObject { - public: - typedef AlignmentsManager::Dependencies Dependencies; - typedef Conditions::Condition Condition; - - /// Full list of alignment dependencies - Dependencies dependencies; - /// References to all alignment possibilities known - AlignContext all_alignments; - - /// Compute the transformation from the closest detector element of the alignment to the world system - void to_world(AlignContext& new_alignments, UserPool& pool, DetElement det, TGeoHMatrix& mat) const; - /// Compute all alignment conditions of the lower levels - void compute(AlignContext& new_alignments, UserPool& pool, DetElement child, int level) const; + /// Access specialization + template <> AlignmentsManager AlignmentsManager::from<LCDD>(LCDD& host) { + Object* obj = host.extension<Object>(); + if ( obj ) return AlignmentsManager(obj); + except("AlignmentsManager","+++ Failed to access manager from LCDD."); + return AlignmentsManager(); + } - public: - /// Initializing constructor - AlignmentsManagerObject(); - /// Default destructor - virtual ~AlignmentsManagerObject(); - /// Compute all alignment conditions of the internal dependency list - void compute(UserPool& user_pool) const; - /// Compute all alignment conditions of the specified dependency list - void compute(UserPool& user_pool, const Dependencies& deps) const; - }; - - } /* End namespace Geometry */ -} /* End namespace DD4hep */ + } /* End namespace Alignments */ +} /* End namespace DD4hep */ DD4HEP_INSTANTIATE_HANDLE_NAMED(AlignmentsManagerObject); /// Initializing constructor AlignmentsManagerObject::AlignmentsManagerObject() : NamedObject() { + all_alignments = new AlignContext(); + dependencies = new Dependencies(); } /// Default destructor AlignmentsManagerObject::~AlignmentsManagerObject() { - dependencies.clear(); + dependencies->clear(); + deletePtr(dependencies); + deletePtr(all_alignments); } - + void AlignmentsManagerObject::to_world(AlignContext& new_alignments, UserPool& pool, DetElement det, TGeoHMatrix& mat) const { + using Conditions::Condition; DetElement par = det.parent(); while( par.isValid() ) { // If we find that the parent also got updated, directly take this transformation. @@ -132,9 +112,9 @@ void AlignmentsManagerObject::to_world(AlignContext& new_alignments, UserPool& p // there is a still valid condition, which we can use to build the world transformation // The parent's alignment condition by defintiion must be present in the pool, // since it got updated in the past! - i = all_alignments.keys.find(par.key()); - if ( i != all_alignments.keys.end() ) { - const AlignContext::Entry& e = all_alignments.entries[(*i).second]; + i = all_alignments->keys.find(par.key()); + if ( i != all_alignments->keys.end() ) { + const AlignContext::Entry& e = all_alignments->entries[(*i).second]; Condition::key_type key = e.dep->target.hash; AlignmentCondition cond = pool.get(key); AlignmentData& align = cond.data(); @@ -150,7 +130,7 @@ void AlignmentsManagerObject::to_world(AlignContext& new_alignments, UserPool& p /// Compute all alignment conditions of the internal dependency list void AlignmentsManagerObject::compute(UserPool& user_pool) const { - compute(user_pool, dependencies); + compute(user_pool, *dependencies); } /// Compute all alignment conditions of the specified dependency list @@ -270,19 +250,19 @@ void AlignmentsManager::destroy() { /// Adopy alignment dependency for later recalculation void AlignmentsManager::adoptDependency(Dependency* dependency) const { Object* o = access(); - o->dependencies.insert(dependency); - o->all_alignments.newEntry(dependency->detector, dependency, 0); + o->dependencies->insert(dependency); + o->all_alignments->newEntry(dependency->detector, dependency, 0); } /// Access all known dependencies const AlignmentsManager::Dependencies& AlignmentsManager::knownDependencies() const { - return access()->dependencies; + return *(access()->dependencies); } /// Compute all alignment conditions of the internal dependency list void AlignmentsManager::compute(dd4hep_ptr<UserPool>& user_pool) const { Object* o = access(); - o->compute(*(user_pool.get()), o->dependencies); + o->compute(*(user_pool.get()), *(o->dependencies)); } /// Compute all alignment conditions of the specified dependency list diff --git a/DDAlign/src/plugins/AlignmentPlugins.cpp b/DDAlign/src/plugins/AlignmentPlugins.cpp index 14318c2e0..ab2e71950 100644 --- a/DDAlign/src/plugins/AlignmentPlugins.cpp +++ b/DDAlign/src/plugins/AlignmentPlugins.cpp @@ -20,34 +20,57 @@ using namespace std; using namespace DD4hep; -#include "DDAlign/AlignmentWriter.h" +// ====================================================================================== +#include "DDAlign/AlignmentsManager.h" -namespace DetectorTools = DD4hep::Geometry::DetectorTools; -static long create_alignment_file(Geometry::LCDD& lcdd, int argc, char** argv) { - Geometry::DetElement top; - string output, path = "/world"; - bool enable_transactions = false; - for(int i=1; i<argc;++i) { - if ( argv[i][0]=='-' || argv[i][0]=='/' ) { - const char* p = ::strchr(argv[i],'='); - if ( p && strncmp(argv[i]+1,"-output",7)==0 ) - output = p+1; - else if ( p && strncmp(argv[i]+1,"-path",5)==0 ) - path = p+1; - else if ( strncmp(argv[i]+1,"-transactions",5)==0 ) - enable_transactions = true; - else - throw runtime_error("AlignmentWriter: Invalid argument:"+string(argv[i])); +namespace { + /// Plugin function: + /// Install the alignment manager as an extension to the central LCDD object + int ddalign_install_align_mgr (Geometry::LCDD& lcdd, int /* argc */, char** /* argv */) { + using Alignments::AlignmentsManager; + using Alignments::AlignmentsManagerObject; + Handle<AlignmentsManagerObject> mgr(lcdd.extension<AlignmentsManagerObject>(false)); + if ( !mgr.isValid() ) { + AlignmentsManager mgr_handle("LCDD_AlignmentManager"); + lcdd.addExtension<AlignmentsManagerObject>(mgr_handle.ptr()); + printout(INFO,"AlignmentsManager","+++ Successfully installed alignments manager instance to LCDD."); } + return 1; } - printout(ALWAYS,"AlignmentWriter", - "++++ Writing DD4hep alignment constants of the \"%s\" DetElement tree to file \"%s\"", - path.c_str(), output.c_str()); - top = DetectorTools::findDaughterElement(lcdd.world(),path); - if ( top.isValid() ) { - Alignments::AlignmentWriter wr(lcdd); - return wr.write(wr.dump(top,enable_transactions), output); +} /* End anonymous namespace */ +DECLARE_APPLY(DD4hep_AlignmentsManagerInstaller,ddalign_install_align_mgr) +// ====================================================================================== + +#include "DDAlign/AlignmentWriter.h" +namespace { + namespace DetectorTools = DD4hep::Geometry::DetectorTools; + long create_alignment_file(Geometry::LCDD& lcdd, int argc, char** argv) { + Geometry::DetElement top; + string output, path = "/world"; + bool enable_transactions = false; + for(int i=1; i<argc;++i) { + if ( argv[i][0]=='-' || argv[i][0]=='/' ) { + const char* p = ::strchr(argv[i],'='); + if ( p && strncmp(argv[i]+1,"-output",7)==0 ) + output = p+1; + else if ( p && strncmp(argv[i]+1,"-path",5)==0 ) + path = p+1; + else if ( strncmp(argv[i]+1,"-transactions",5)==0 ) + enable_transactions = true; + else + throw runtime_error("AlignmentWriter: Invalid argument:"+string(argv[i])); + } + } + printout(ALWAYS,"AlignmentWriter", + "++++ Writing DD4hep alignment constants of the \"%s\" DetElement tree to file \"%s\"", + path.c_str(), output.c_str()); + top = DetectorTools::findDaughterElement(lcdd.world(),path); + if ( top.isValid() ) { + Alignments::AlignmentWriter wr(lcdd); + return wr.write(wr.dump(top,enable_transactions), output); + } + throw runtime_error("AlignmentWriter: Invalid top level element name:"+path); } - throw runtime_error("AlignmentWriter: Invalid top level element name:"+path); -} +} /* End anonymous namespace */ DECLARE_APPLY(DDAlignmentWriter, create_alignment_file) +// ====================================================================================== diff --git a/DDCond/include/DDCond/ConditionsManager.h b/DDCond/include/DDCond/ConditionsManager.h index 336431d99..45e409a2a 100644 --- a/DDCond/include/DDCond/ConditionsManager.h +++ b/DDCond/include/DDCond/ConditionsManager.h @@ -63,7 +63,7 @@ namespace DD4hep { * * There may very well be several instances of a * conditions manager. Do not think this is a sort of - * 'static constructor'. This woul dbe a big mistake!. + * 'static constructor'. This would be a big mistake!. */ template <typename T> static ConditionsManager from(T& host); @@ -137,6 +137,6 @@ namespace DD4hep { bool verify_dependencies=true); }; - } /* End namespace Geometry */ + } /* End namespace Conditions */ } /* End namespace DD4hep */ #endif /* DDCOND_CONDITIONSMANAGER_H */ diff --git a/DDCond/src/plugins/ConditionsPlugins.cpp b/DDCond/src/plugins/ConditionsPlugins.cpp index a6f0aab9b..b9e1b3721 100644 --- a/DDCond/src/plugins/ConditionsPlugins.cpp +++ b/DDCond/src/plugins/ConditionsPlugins.cpp @@ -29,13 +29,14 @@ using namespace DD4hep; using namespace DD4hep::Conditions; namespace { - + /// Plugin function: + /// Install the alignment manager as an extension to the central LCDD object int ddcond_install_cond_mgr (LCDD& lcdd, int /* argc */, char** /* argv */) { Handle<ConditionsManagerObject> mgr(lcdd.extension<ConditionsManagerObject>(false)); if ( !mgr.isValid() ) { ConditionsManager mgr_handle(lcdd); lcdd.addExtension<ConditionsManagerObject>(mgr_handle.ptr()); - printout(INFO,"ConditionsManager","+++ Successfully installed conditions manager instance."); + printout(INFO,"ConditionsManager","+++ Successfully installed conditions manager instance to LCDD."); } return 1; } diff --git a/DDDB/src/DDDBAlignmentTest.cpp b/DDDB/src/DDDBAlignmentTest.cpp index 598d48f0f..f45f21e98 100644 --- a/DDDB/src/DDDBAlignmentTest.cpp +++ b/DDDB/src/DDDBAlignmentTest.cpp @@ -171,35 +171,25 @@ namespace { }; //======================================================================== long make_time(int argc, char** argv) { - long int time = makeTime(2016,4,1,12); - if ( argc>0 ) { - struct tm tm; - char* c = ::strptime(argv[0],"%d-%m-%Y %H:%M:%S",&tm); - if ( 0 == c ) { - except("DerivedAlignmentsTest","Invalid time format given for update:%s " - " should be: %d-%m-%Y %H:%M:%S", argv[0]); - } - time = ::mktime(&tm); - } - return time; + return argc>0 ? makeTime(argv[0],"%d-%m-%Y %H:%M:%S") : makeTime(2016,4,1,12); } } //========================================================================== namespace { - /// Plugin function + /// Plugin function: /// Load dependent alignment conditions according to time stamps. long dddb_derived_alignments(LCDD& lcdd, int argc, char** argv) { long time = make_time(argc, argv); - AlignmentsManager align("Test"); - AlignmentSelector selector(lcdd); + AlignmentSelector selec(lcdd); + AlignmentsManager align(AlignmentsManager::from(lcdd)); ConditionsManager conds(ConditionsManager::from(lcdd)); - int ret = selector.collect(conds,align,time); + int ret = selec.collect(conds,align,time); if ( ret == 1 ) { for(int i=0; i<10; ++i) { { long ti = time + i*3600; dd4hep_ptr<UserPool> pool; - ret = selector.computeDependencies(pool,conds,align,ti); + ret = selec.computeDependencies(pool,conds,align,ti); pool->clear(); } DD4hep::InstanceCount::dump(); @@ -209,25 +199,24 @@ namespace { return ret; } } /* End anonymous namespace */ - DECLARE_APPLY(DDDB_DerivedAlignmentsTest,dddb_derived_alignments) //========================================================================== namespace { - /// Plugin function + /// Plugin function: /// Access dependent alignment conditions from DetElement object using global and local keys long dddb_access_alignments(LCDD& lcdd, int argc, char** argv) { long time = make_time(argc, argv); - AlignmentsManager align("Test"); - AlignmentSelector selector(lcdd); + AlignmentSelector selec(lcdd); + AlignmentsManager align(AlignmentsManager::from(lcdd)); ConditionsManager conds(ConditionsManager::from(lcdd)); - int ret = selector.collect(conds,align,time); + int ret = selec.collect(conds,align,time); if ( ret == 1 ) { - ret = selector.access(conds,align,time); + ret = selec.access(conds,align,time); } align.destroy(); return ret; } } /* End anonymous namespace */ - DECLARE_APPLY(DDDB_AlignmentsAccessTest,dddb_access_alignments) +//========================================================================== diff --git a/DDDB/src/DDDBDerivedCondTest.cpp b/DDDB/src/DDDBDerivedCondTest.cpp index 3871c3d97..b74677cb4 100644 --- a/DDDB/src/DDDBDerivedCondTest.cpp +++ b/DDDB/src/DDDBDerivedCondTest.cpp @@ -293,14 +293,16 @@ namespace { } }; + //======================================================================== /// Plugin function + /// Load, compute and access derived conditions long dddb_derived_alignments(LCDD& lcdd, int argc, char** argv) { - long int long init_time = argc>0 ? *(long*)argv[0] : makeTime(2016,4,1,12); + long time = argc>0 ? makeTime(argv[0],"%d-%m-%Y %H:%M:%S") : makeTime(2016,4,1,12); ConditionsManager manager = ConditionsManager::from(lcdd); ConditionsSelector selector(manager); int ret = selector.collectDependencies(lcdd.world(), 0); if ( ret == 1 ) { - ret = selector.computeDependencies(init_time); + ret = selector.computeDependencies(time); } return ret; } diff --git a/examples/DDDB/CMakeLists.txt b/examples/DDDB/CMakeLists.txt index c4c5e0079..622446342 100644 --- a/examples/DDDB/CMakeLists.txt +++ b/examples/DDDB/CMakeLists.txt @@ -127,6 +127,7 @@ if (DD4HEP_USE_XERCESC) COMMAND "${CMAKE_INSTALL_PREFIX}/bin/run_test_DDDB.sh" EXEC_ARGS ${CMAKE_INSTALL_PREFIX}/bin/run_dddb.sh -config DD4hep_ConditionsManagerInstaller + -config DD4hep_AlignmentsManagerInstaller -exec DDDB_DerivedAlignmentsTest REGEX_PASS "| 24950| 24950| 0|DD4hep::Alignments::Interna::AlignmentConditionObject" ) @@ -136,6 +137,7 @@ if (DD4HEP_USE_XERCESC) COMMAND "${CMAKE_INSTALL_PREFIX}/bin/run_test_DDDB.sh" EXEC_ARGS ${CMAKE_INSTALL_PREFIX}/bin/run_dddb.sh -config DD4hep_ConditionsManagerInstaller + -config DD4hep_AlignmentsManagerInstaller -exec DDDB_AlignmentsAccessTest REGEX_PASS "Accessed 4990 conditions from the DetElement objects" ) -- GitLab