From 0ea4d8e1878255d62916485873383378eb1b2c65 Mon Sep 17 00:00:00 2001
From: Wouter Deconinck <wouter.deconinck@umanitoba.ca>
Date: Mon, 25 Oct 2021 02:11:24 +0000
Subject: [PATCH] File loader for Acts material maps

---
 CMakeLists.txt                          |  1 +
 compact/tracking_config_acadia.xml      | 11 ++++++
 compact/tracking_config_canyonlands.xml | 11 ++++++
 src/FileLoader.cpp                      | 51 +++++++++++++++++++++++++
 4 files changed, 74 insertions(+)
 create mode 100644 src/FileLoader.cpp

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 1997c071..3a563d63 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -35,6 +35,7 @@ dd4hep_add_plugin(${a_lib_name} SOURCES
   src/DIRC_geo.cpp
   src/DRich_geo.cpp
   src/ERich_geo.cpp
+  src/FileLoader.cpp
   src/FieldMapBrBz.cpp
   src/GaseousRICH_geo.cpp
   src/GeometryHelpers.cpp
diff --git a/compact/tracking_config_acadia.xml b/compact/tracking_config_acadia.xml
index be7e595d..b9abc3f8 100644
--- a/compact/tracking_config_acadia.xml
+++ b/compact/tracking_config_acadia.xml
@@ -48,6 +48,17 @@
 
   </detectors>
 
+  <documentation>
+      ### Material map for ACTS
+      https://eicweb.phy.anl.gov/EIC/detectors/athena/-/issues/128
+  </documentation>
+  <plugins>
+    <plugin name="FileLoader">
+      <arg value="file:calibrations/materials-map.cbor"/>
+      <arg value="url:https://eicweb.phy.anl.gov/EIC/detectors/athena/uploads/458fee1b8c95bf408159fe70d654b8c0/material-maps.cbor"/>
+    </plugin>
+  </plugins>
+
   <include ref="vertex_tracker.xml"/>
   <include ref="central_tracker.xml"/>
   <include ref="gem_tracker_endcap.xml"/>
diff --git a/compact/tracking_config_canyonlands.xml b/compact/tracking_config_canyonlands.xml
index fcdb10be..153c0670 100644
--- a/compact/tracking_config_canyonlands.xml
+++ b/compact/tracking_config_canyonlands.xml
@@ -64,6 +64,17 @@
 
   </detectors>
 
+  <documentation>
+      ### Material map for ACTS
+      https://eicweb.phy.anl.gov/EIC/detectors/athena/-/issues/127
+  </documentation>
+  <plugins>
+    <plugin name="FileLoader">
+      <arg value="file:calibrations/materials-map.cbor"/>
+      <arg value="url:https://eicweb.phy.anl.gov/EIC/detectors/athena/uploads/cff8f8d5919d8e78561e8caf2b4fad2b/material-maps.cbor"/>
+    </plugin>
+  </plugins>
+
   <include ref="vertex_tracker_3layers.xml"/>
   <include ref="central_tracker_hybrid_v2.xml"/>
 
diff --git a/src/FileLoader.cpp b/src/FileLoader.cpp
new file mode 100644
index 00000000..63c7017e
--- /dev/null
+++ b/src/FileLoader.cpp
@@ -0,0 +1,51 @@
+#include <DD4hep/DetFactoryHelper.h>
+#include <DD4hep/Factories.h>
+#include <DD4hep/Printout.h>
+#include <XML/Utilities.h>
+
+#include <filesystem>
+#include <iostream>
+#include <string>
+
+namespace fs = std::filesystem;
+
+using namespace dd4hep;
+
+void usage(int argc, char** argv)    {
+  std::cout <<
+    "Usage: -plugin <name> -arg [-arg]                                                  \n"
+    "     name:   factory name     FileLoader                                           \n"
+    "     file:<string>            file location                                        \n"
+    "     url:<string>             url location                                         \n"
+    "\tArguments given: " << arguments(argc,argv) << std::endl;
+  std::exit(EINVAL);
+}
+
+// Plugin to download files
+long load_file(
+    Detector& /* desc */,
+    int argc,
+    char** argv
+) {
+  std::string file, url;
+  for (int i = 0; i < argc && argv[i]; ++i) {
+    if      (0 == std::strncmp("file:",argv[i], 5)) file = (argv[i] + 5);
+    else if (0 == std::strncmp("url:", argv[i], 4)) url  = (argv[i] + 4);
+    else usage(argc, argv);
+  }
+  std::cout << "Loading " << file << " from " << url << std::endl;
+
+  if (!fs::exists(fs::path(file))) {
+    std::string parent_path = fs::path(file).parent_path();
+    auto ret = std::system(("mkdir -p " + parent_path + " && "
+                            "wget " + url + " -O " + file).c_str());
+    if (!fs::exists(fs::path(file))) {
+      std::cerr << "ERROR: file, " << file << ", does not exist\n";
+      std::quick_exit(1);
+    }
+  }
+
+  return 0;
+}
+
+DECLARE_APPLY(FileLoader, load_file)
-- 
GitLab