#==========================================================================
#  AIDA Detector description implementation 
#--------------------------------------------------------------------------
# Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
# All rights reserved.
#
# For the licensing terms see $DD4hepINSTALL/LICENSE.
# For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
#
#==========================================================================
cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

IF(NOT TARGET DD4hep::DDCore)
  find_package ( DD4hep REQUIRED )
  include ( ${DD4hep_DIR}/cmake/DD4hep.cmake )
  include ( ${DD4hep_DIR}/cmake/DD4hepBuild.cmake )
  dd4hep_configure_output()
ENDIF()

dd4hep_set_compiler_flags()
#==========================================================================

if(TARGET XercesC::XercesC)
  SET(OPT_XERCESC XercesC::XercesC)
endif()
  
#-----------------------------------------------------------------------------------
dd4hep_add_plugin(ConditionsExample SOURCES src/*.cpp
  USES  DD4hep::DDCore DD4hep::DDCond DD4hep::DDAlign
        ROOT::Core ROOT::Geom ROOT::GenVector
        ${OPT_XERCESC}
  )
install(TARGETS ConditionsExample LIBRARY DESTINATION lib)
dd4hep_install_dir( xml  DESTINATION examples/Conditions )
dd4hep_install_dir( data DESTINATION examples/Conditions )
dd4hep_configure_scripts ( Conditions DEFAULT_SETUP WITH_TESTS )
#--------------------------------------------------------------------------
#
#---Testing: Load Telescope geometry and read conditions ------------------
dd4hep_add_test_reg( Conditions_Telescope_cond_dump_by_pool
  COMMAND    "${CMAKE_INSTALL_PREFIX}/bin/run_test_Conditions.sh"
  EXEC_ARGS  geoPluginRun -volmgr -destroy 
  -compact file:${CMAKE_INSTALL_PREFIX}/examples/AlignDet/compact/Telescope.xml 
  -plugin DD4hep_ConditionsXMLRepositoryParser file:${CMAKE_INSTALL_PREFIX}/examples/Conditions/data/repository.xml 
  -plugin DD4hep_ConditionsDump
  REGEX_PASS "Data\\(Translation-Rotation\\(Phi,Theta,Psi\\)-Pivot\\): \\[\\( 0 , 0 , 1 \\), \\( 3.14159265359 , 0 , 0 \\), \\( 0 , 0 , 0 \\)\\]"
  REGEX_FAIL " ERROR ;EXCEPTION;Exception"
  )
#
#---Testing: Load Telescope geometry and read conditions ------------------
dd4hep_add_test_reg( Conditions_Telescope_cond_dump_by_detelement
  COMMAND    "${CMAKE_INSTALL_PREFIX}/bin/run_test_Conditions.sh"
  EXEC_ARGS  geoPluginRun -volmgr -destroy 
  -compact file:${CMAKE_INSTALL_PREFIX}/examples/AlignDet/compact/Telescope.xml 
  -plugin DD4hep_ConditionsXMLRepositoryParser file:${CMAKE_INSTALL_PREFIX}/examples/Conditions/data/repository.xml 
  -plugin DD4hep_DetElementConditionsDump
  REGEX_PASS "Key\\:FA708B8A6E87AB8E Type\\:dd4hep\\:\\:Delta"
  REGEX_FAIL " ERROR ;EXCEPTION;Exception"
  )
#
#---Testing: Simple stress: Load Telescope geometry and have multiple runs on IOVs
dd4hep_add_test_reg( Conditions_Telescope_populate
  COMMAND    "${CMAKE_INSTALL_PREFIX}/bin/run_test_Conditions.sh"
  EXEC_ARGS  geoPluginRun  -destroy -plugin DD4hep_ConditionExample_populate
      -input file:${CMAKE_INSTALL_PREFIX}/examples/AlignDet/compact/Telescope.xml -iovs 5
  REGEX_PASS "Accessed a total of 900 conditions \\(S:   500,L:     0,C:   400,M:0\\)"
  REGEX_FAIL " ERROR ;EXCEPTION;Exception"
  )
#
#---Testing: Simple stress: Load Telescope geometry and have multiple runs on IOVs
dd4hep_add_test_reg( Conditions_Telescope_stress
  COMMAND    "${CMAKE_INSTALL_PREFIX}/bin/run_test_Conditions.sh"
  EXEC_ARGS  geoPluginRun  -destroy -plugin DD4hep_ConditionExample_stress 
    -input file:${CMAKE_INSTALL_PREFIX}/examples/AlignDet/compact/Telescope.xml -iovs 10 -runs 20
  REGEX_PASS "\\+  Accessed a total of 3600 conditions \\(S:  2880,L:     0,C:   720,M:0\\)"
  REGEX_FAIL " ERROR ;EXCEPTION;Exception"
  )
#
#---Testing: Simple stress: Load Telescope geometry and have multiple runs on IOVs
dd4hep_add_test_reg( Conditions_Telescope_stress2
  COMMAND    "${CMAKE_INSTALL_PREFIX}/bin/run_test_Conditions.sh"
  EXEC_ARGS  geoPluginRun  -destroy -plugin DD4hep_ConditionExample_stress2 
    -input file:${CMAKE_INSTALL_PREFIX}/examples/AlignDet/compact/Telescope.xml -iovs 10
  REGEX_PASS "\\+  Accessed a total of 1800 conditions \\(S:  1000,L:     0,C:   800,M:0\\)"
  REGEX_FAIL " ERROR ;EXCEPTION;Exception"
  )
#
#---Testing: Multi-threading test: Load CLICSiD geometry and have multiple parallel runs on IOVs
dd4hep_add_test_reg( Conditions_Telescope_MT_LONGTEST
  COMMAND    "${CMAKE_INSTALL_PREFIX}/bin/run_test_Conditions.sh"
  EXEC_ARGS  geoPluginRun  -destroy -plugin DD4hep_ConditionExample_MT 
    -input file:${CMAKE_INSTALL_PREFIX}/examples/AlignDet/compact/Telescope.xml -iovs 30 -runs 10 -threads 1
  REGEX_PASS "\\+  Accessed a total of 322200 conditions \\(S:298200,L:     0,C: 24000,M:0\\)"
  REGEX_FAIL " ERROR ;EXCEPTION;Exception"
  )
#
#---Testing: Save conditions to ROOT file
dd4hep_add_test_reg( Conditions_Telescope_root_save
  COMMAND    "${CMAKE_INSTALL_PREFIX}/bin/run_test_Conditions.sh"
  EXEC_ARGS  geoPluginRun -print WARNING -destroy -plugin DD4hep_ConditionExample_save
    -input file:${CMAKE_INSTALL_PREFIX}/examples/AlignDet/compact/Telescope.xml -iovs 30
    -conditions TelescopeConditions.root
  REGEX_PASS "\\+ Successfully saved 14400 condition to file."
  REGEX_FAIL " ERROR ;EXCEPTION;Exception"
  )
#
#---Testing: Save conditions to ROOT file
dd4hep_add_test_reg( Conditions_Telescope_root_load_iov
  COMMAND    "${CMAKE_INSTALL_PREFIX}/bin/run_test_Conditions.sh"
  EXEC_ARGS  geoPluginRun -print WARNING -destroy -plugin DD4hep_ConditionExample_load
    -input file:${CMAKE_INSTALL_PREFIX}/examples/AlignDet/compact/Telescope.xml
    -conditions TelescopeConditions.root -iovs 30 -restore iovpool
  DEPENDS Conditions_Telescope_root_save
  REGEX_PASS "\\+  Accessed a total of 5400 conditions \\(S:  4800,L:     0,C:   600,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 -destroy -plugin DD4hep_ConditionExample_load
    -input file:${CMAKE_INSTALL_PREFIX}/examples/AlignDet/compact/Telescope.xml
    -conditions TelescopeConditions.root -iovs 30 -restore userpool
  DEPENDS Conditions_Telescope_root_save
  REGEX_PASS "\\+  Accessed a total of 5400 conditions \\(S:  4800,L:     0,C:   600,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 -destroy -plugin DD4hep_ConditionExample_load
    -input file:${CMAKE_INSTALL_PREFIX}/examples/AlignDet/compact/Telescope.xml
    -conditions TelescopeConditions.root -iovs 30 -restore condpool
  DEPENDS Conditions_Telescope_root_save
  REGEX_PASS "\\+  Accessed a total of 5400 conditions \\(S:  4800,L:     0,C:   600,M:0\\)"
  REGEX_FAIL " ERROR ;EXCEPTION;Exception"
  )
#
#---Testing: Simple stress: Load CLICSiD geometry and have multiple runs on IOVs
dd4hep_add_test_reg( Conditions_CLICSiD_stress_LONGTEST
  COMMAND    "${CMAKE_INSTALL_PREFIX}/bin/run_test_Conditions.sh"
  EXEC_ARGS  geoPluginRun  -destroy -plugin DD4hep_ConditionExample_stress 
    -input file:$ENV{DD4hepINSTALL}/DDDetectors/compact/SiD.xml -iovs 10 -runs 100
  REGEX_PASS "\\+  Accessed a total of 31596300 conditions \\(S:30192020,L:     0,C:1404280,M:0\\)"
  REGEX_FAIL " ERROR ;EXCEPTION;Exception"
  )
#
#---Testing: Simple stress: Load CLICSiD geometry and have multiple runs on IOVs
dd4hep_add_test_reg( Conditions_CLICSiD_stress2_LONGTEST
  COMMAND    "${CMAKE_INSTALL_PREFIX}/bin/run_test_Conditions.sh"
  EXEC_ARGS  geoPluginRun  -destroy -plugin DD4hep_ConditionExample_stress2 
    -input file:$ENV{DD4hepINSTALL}/DDDetectors/compact/SiD.xml -iovs 20
  REGEX_PASS "\\+  Accessed a total of 6319260 conditions \\(S:3510700,L:     0,C:2808560,M:0\\)"
  REGEX_FAIL " ERROR ;EXCEPTION;Exception"
  )
#
#---Testing: Multi-threading test: Load CLICSiD geometry and have multiple parallel runs on IOVs
dd4hep_add_test_reg( Conditions_CLICSiD_MT_LONGTEST
  COMMAND    "${CMAKE_INSTALL_PREFIX}/bin/run_test_Conditions.sh"
  EXEC_ARGS  geoPluginRun  -destroy -plugin DD4hep_ConditionExample_MT 
    -input file:$ENV{DD4hepINSTALL}/DDDetectors/compact/SiD.xml -iovs 3 -runs 2 -threads 1
  REGEX_PASS "\\+  Accessed a total of 10742742 conditions \\(S:9900174,L:     0,C:842568,M:0\\)"
  REGEX_FAIL " ERROR ;EXCEPTION;Exception"
  )
#
#---Testing: Save conditions to ROOT file
dd4hep_add_test_reg( Conditions_CLICSiD_root_save_LONGTEST
  COMMAND    "${CMAKE_INSTALL_PREFIX}/bin/run_test_Conditions.sh"
  EXEC_ARGS  geoPluginRun -print WARNING -destroy -plugin DD4hep_ConditionExample_save
    -input file:$ENV{DD4hepINSTALL}/DDDetectors/compact/SiD.xml -iovs 3
    -conditions CLICSiDConditions.root
  REGEX_PASS "\\+ Successfully saved 2527704 condition to file."
  REGEX_FAIL " ERROR ;EXCEPTION;Exception"
  )
#
#---Testing: Save conditions to ROOT file
dd4hep_add_test_reg( Conditions_CLICSiD_root_load_iov_LONGTEST
  COMMAND    "${CMAKE_INSTALL_PREFIX}/bin/run_test_Conditions.sh"
  EXEC_ARGS  geoPluginRun -print WARNING -destroy -plugin DD4hep_ConditionExample_load
    -input file:$ENV{DD4hepINSTALL}/DDDetectors/compact/SiD.xml -iovs 3 -restore iovpool
    -conditions CLICSiDConditions.root
  DEPENDS Conditions_CLICSiD_root_save_LONGTEST
  REGEX_PASS "\\+  Accessed a total of 947889 conditions \\(S:842568,L:     0,C:105321,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 -destroy -plugin DD4hep_ConditionExample_load
    -input file:$ENV{DD4hepINSTALL}/DDDetectors/compact/SiD.xml -iovs 3 -restore userpool
    -conditions CLICSiDConditions.root
  DEPENDS Conditions_CLICSiD_root_save_LONGTEST
  REGEX_PASS "\\+  Accessed a total of 947889 conditions \\(S:842568,L:     0,C:105321,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 -destroy -plugin DD4hep_ConditionExample_load
    -input file:$ENV{DD4hepINSTALL}/DDDetectors/compact/SiD.xml -iovs 3 -restore condpool
    -conditions CLICSiDConditions.root
  DEPENDS Conditions_CLICSiD_root_save_LONGTEST
  REGEX_PASS "\\+  Accessed a total of 947889 conditions \\(S:842568,L:     0,C:105321,M:0\\)"
  REGEX_FAIL " ERROR ;EXCEPTION;Exception"
  )
