From 4b3f4ee89c1c8270292b15f1d35abed82b6f4305 Mon Sep 17 00:00:00 2001
From: Markus Frank <markus.frank@cern.ch>
Date: Fri, 17 May 2013 20:29:47 +0000
Subject: [PATCH] Remove materials from LCDD. Look them directly up in the
 TGeoManager instance and avoid double caching.

---
 DDCore/src/plugins/Compact2Objects.cpp       | 38 +++++++++-----------
 DDExamples/UtilityApps/src/converter.cpp     |  7 ++++
 DDExamples/UtilityApps/src/display.cpp       | 11 ++++--
 DDExamples/UtilityApps/src/plugin_runner.cpp |  6 ++++
 4 files changed, 37 insertions(+), 25 deletions(-)

diff --git a/DDCore/src/plugins/Compact2Objects.cpp b/DDCore/src/plugins/Compact2Objects.cpp
index b53cd36aa..b915351f3 100644
--- a/DDCore/src/plugins/Compact2Objects.cpp
+++ b/DDCore/src/plugins/Compact2Objects.cpp
@@ -83,7 +83,7 @@ static Ref_t create_GlobalGridXY(lcdd_t& /* lcdd */, xml_h e)  {
   return obj;
 }
 DECLARE_XMLELEMENT(GlobalGridXY,create_GlobalGridXY);
-  
+
 static Ref_t create_CartesianGridXY(lcdd_t& /* lcdd */, xml_h e)  {
   CartesianGridXY obj;
   if ( e.hasAttr(_U(gridSizeX)) ) obj.setGridSizeX(e.attr<double>(_U(gridSizeX)));
@@ -231,11 +231,11 @@ template <> void Converter<Header>::operator()(xml_h e)  const  {
  */
 template <> void Converter<Material>::operator()(xml_h e)  const  {
   xml_ref_t         m(e);
-  TGeoManager*      mgr      = gGeoManager;
+  TGeoManager&      mgr      = lcdd.manager();
   xml_tag_t         mname    = m.name();
   const char*       matname  = mname.c_str();
-  TGeoElementTable* table    = mgr->GetElementTable();
-  TGeoMaterial*     mat      = mgr->GetMaterial(matname);
+  TGeoElementTable* table    = mgr.GetElementTable();
+  TGeoMaterial*     mat      = mgr.GetMaterial(matname);
   TGeoMixture*      mix      = dynamic_cast<TGeoMixture*>(mat);
   xml_coll_t        fractions(m,_U(fraction));
   xml_coll_t        composites(m,_U(composite));
@@ -264,7 +264,7 @@ template <> void Converter<Material>::operator()(xml_h e)  const  {
     for(fractions.reset(); fractions; ++fractions)  {
       std::string nam = fractions.attr<string>(_U(ref));
       double fraction = fractions.attr<double>(_U(n));
-      if ( 0 != (comp_mat=mgr->GetMaterial(nam.c_str())) )
+      if ( 0 != (comp_mat=mgr.GetMaterial(nam.c_str())) )
 	mix->AddElement(comp_mat,fraction);
       else if ( 0 != (comp_elt=table->FindElement(nam.c_str())) )
 	mix->AddElement(comp_elt,fraction);
@@ -276,12 +276,12 @@ template <> void Converter<Material>::operator()(xml_h e)  const  {
       double dens = 0.0;
       for(composites.reset(); composites; ++composites)  {
 	std::string nam = composites.attr<string>(_U(ref));
-	comp_mat = mgr->GetMaterial(nam.c_str());
+	comp_mat = mgr.GetMaterial(nam.c_str());
 	dens += composites.attr<double>(_U(n)) * comp_mat->GetDensity();
       }
       for(fractions.reset(); fractions; ++fractions)  {
 	std::string nam = fractions.attr<string>(_U(ref));
-	comp_mat = mgr->GetMaterial(nam.c_str());
+	comp_mat = mgr.GetMaterial(nam.c_str());
 	dens +=  composites.attr<double>(_U(n)) * comp_mat->GetDensity();
       }
       cout << "Compact2Objects[WARNING]: Material:" << matname << " with NO density."
@@ -289,30 +289,24 @@ template <> void Converter<Material>::operator()(xml_h e)  const  {
       mix->SetDensity(dens);
     }
   }
-  TGeoMedium* medium = mgr->GetMedium(matname);
+  TGeoMedium* medium = mgr.GetMedium(matname);
   if ( 0 == medium )  {
     --unique_mat_id;
     medium = new TGeoMedium(matname,unique_mat_id,mat);
     medium->SetTitle("material");
     medium->SetUniqueID(unique_mat_id);
   }
-  lcdd.addMaterial(Ref_t(medium));
-
   // TGeo has no notion of a material "formula"
   // Hence, treat the formula the same way as the material itself
   if ( m.hasAttr(_U(formula)) ) {
     string form = m.attr<string>(_U(formula));
     if ( form != matname ) {
-      LCDD::HandleMap::const_iterator im=lcdd.materials().find(form);
-      if ( im == lcdd.materials().end() ) {
-	medium = mgr->GetMedium(form.c_str());
-	if ( 0 == medium ) {
-	  --unique_mat_id;
-	  medium = new TGeoMedium(form.c_str(),unique_mat_id,mat);
-	  medium->SetTitle("material");
-	  medium->SetUniqueID(unique_mat_id);      
-	}
-	lcdd.addMaterial(Ref_t(medium));
+      medium = mgr.GetMedium(form.c_str());
+      if ( 0 == medium ) {
+	--unique_mat_id;
+	medium = new TGeoMedium(form.c_str(),unique_mat_id,mat);
+	medium->SetTitle("material");
+	medium->SetUniqueID(unique_mat_id);      
       }
     }
   }
@@ -325,8 +319,8 @@ template <> void Converter<Material>::operator()(xml_h e)  const  {
 template <> void Converter<Atom>::operator()(xml_h e)  const  {
   xml_ref_t    elem(e);
   xml_tag_t    eltname  = elem.name();
-  TGeoManager* mgr      = gGeoManager;
-  TGeoElementTable* tab = mgr->GetElementTable();
+  TGeoManager& mgr      = lcdd.manager();
+  TGeoElementTable* tab = mgr.GetElementTable();
   TGeoElement*  element = tab->FindElement(eltname.c_str());
   if ( !element )  {
     xml_ref_t atom(elem.child(_U(atom)));
diff --git a/DDExamples/UtilityApps/src/converter.cpp b/DDExamples/UtilityApps/src/converter.cpp
index 3fd49125f..c306fc1a3 100644
--- a/DDExamples/UtilityApps/src/converter.cpp
+++ b/DDExamples/UtilityApps/src/converter.cpp
@@ -25,14 +25,18 @@ namespace {
       "                                    device is stdout.                         \n"
       "        -ascii          [OPTIONAL]  Dump visualisation attrs in csv format.   \n"
       "                                    [Only valid for -compact2vis]             \n"
+      "        -destroy        [OPTIONAL]  Force destruction of the LCDD instance    \n"
+      "                                    before exiting the application            \n"
 	 << endl;
     exit(EINVAL);
   }
 }
 
+
 //______________________________________________________________________________
 int main(int argc,char** argv)  {
   bool ascii = false;
+  bool destroy      = false;
   bool compact2lcdd = false;
   bool compact2gdml = false;
   bool compact2pand = false;
@@ -55,6 +59,8 @@ int main(int argc,char** argv)  {
         output = ++i;
       else if ( strncmp(argv[i],"-ascii",5)==0 )
         ascii = true;
+      else if ( strncmp(argv[i],"-destroy",2)==0 )
+        destroy = true;
       else
 	usage();
     }
@@ -78,5 +84,6 @@ int main(int argc,char** argv)  {
     run_plugin(lcdd,"DD4hepGeometry2VISASCII",output,&argv[output]);
   else if ( compact2vis )
     run_plugin(lcdd,"DD4hepGeometry2VIS",output,&argv[output]);
+  if ( destroy ) delete &lcdd;
   return 0;
 }
diff --git a/DDExamples/UtilityApps/src/display.cpp b/DDExamples/UtilityApps/src/display.cpp
index d4b381f17..46eaf350b 100644
--- a/DDExamples/UtilityApps/src/display.cpp
+++ b/DDExamples/UtilityApps/src/display.cpp
@@ -16,9 +16,11 @@ namespace {
   void usage() {
     cout << "displayGeo -opt [-opt]                                                  \n"
       "        -compact       <file>       Specify the compact geometry file         \n"
-      "                                    At least one compact geo file is required!\n"
-      "        -load_only                  Dry-run to only load geometry without     \n"
+      "                     [REQUIRED]     At least one compact geo file is required!\n"
+      "        -load_only   [OPTIONAL]     Dry-run to only load geometry without     \n"
       "                                    starting the dispay.                      \n"
+      "        -destroy     [OPTIONAL]     Force destruction of the LCDD instance    \n"
+      "                                    before exiting the application            \n"
 	 << endl;
     exit(EINVAL);
   }
@@ -26,7 +28,7 @@ namespace {
 
 //______________________________________________________________________________
 int main(int argc,char** argv)  {
-  bool dry_run = false;
+  bool dry_run = false, destroy = false;
   vector<char*> geo_files;
   for(int i=1; i<argc;++i) {
     if ( argv[i][0]=='-' ) {
@@ -34,6 +36,8 @@ int main(int argc,char** argv)  {
 	geo_files.push_back(argv[++i]);
       else if ( strncmp(argv[i],"-load_only",2)==0 )
         dry_run = true;
+      else if ( strncmp(argv[i],"-destroy",2)==0 )
+        destroy = true;
       else
 	usage();
     }
@@ -57,5 +61,6 @@ int main(int argc,char** argv)  {
   else {
     cout << "The geometry was loaded. Application now exiting." << endl;
   }
+  if ( destroy ) delete &lcdd;
   return 0;
 }
diff --git a/DDExamples/UtilityApps/src/plugin_runner.cpp b/DDExamples/UtilityApps/src/plugin_runner.cpp
index 8afa9b81e..d5787d258 100644
--- a/DDExamples/UtilityApps/src/plugin_runner.cpp
+++ b/DDExamples/UtilityApps/src/plugin_runner.cpp
@@ -16,6 +16,8 @@ namespace {
     cout << "geoPlugin -opt [-opt]                                                   \n"
       "        -plugin <name>  [REQUIRED]  Plugin to be executed and applied.        \n"
       "        -input  <file>  [REQUIRED]  Specify input file.                       \n"
+      "        -destroy        [OPTIONAL]  Force destruction of the LCDD instance    \n"
+      "                                    before exiting the application            \n"
 	 << endl;
     exit(EINVAL);
   }
@@ -24,6 +26,7 @@ namespace {
 //______________________________________________________________________________
 int main(int argc,char** argv)  {
   string plugin;
+  bool destroy = false;
   vector<char*> geo_files;
   for(int i=1; i<argc;++i) {
     if ( argv[i][0]=='-' ) {
@@ -31,6 +34,8 @@ int main(int argc,char** argv)  {
 	geo_files.push_back(argv[++i]);
       else if ( strncmp(argv[i],"-plugin",2)==0 )
         plugin = argv[++i];
+      else if ( strncmp(argv[i],"-destroy",2)==0 )
+        destroy = true;
       else
 	usage();
     }
@@ -46,5 +51,6 @@ int main(int argc,char** argv)  {
   run_plugin(lcdd,"DD4hepCompactLoader",int(geo_files.size()),&geo_files[0]);
   // Execute plugin
   run_plugin(lcdd,plugin.c_str(),0,0);
+  if ( destroy ) delete &lcdd;
   return 0;
 }
-- 
GitLab