From ec86bde4ef1c2e6d1a369066f93e41368f08b308 Mon Sep 17 00:00:00 2001
From: Frank Gaede <frank.gaede@desy.de>
Date: Wed, 1 Oct 2014 18:03:51 +0000
Subject: [PATCH]  - added first example code for creating    Gear objects from
 DetElements with    data structures holding relevant information

---
 DDRec/CMakeLists.txt                |  2 +-
 DDRec/include/DDRec/DDGear.h        |  2 +-
 DDRec/include/DDRec/DetectorData.h  | 56 +++++++++++++++++++++++++
 DDRec/src/convertToGear.cc          | 18 +++++---
 DDRec/src/gear/DDGear.cpp           | 13 +++---
 DDRec/src/gear/createGearForILD.cpp | 65 +++++++++++++++++++++++++++++
 6 files changed, 143 insertions(+), 13 deletions(-)
 create mode 100644 DDRec/include/DDRec/DetectorData.h
 create mode 100644 DDRec/src/gear/createGearForILD.cpp

diff --git a/DDRec/CMakeLists.txt b/DDRec/CMakeLists.txt
index a04d2005a..db3387986 100644
--- a/DDRec/CMakeLists.txt
+++ b/DDRec/CMakeLists.txt
@@ -17,7 +17,7 @@ if(DD4HEP_USE_GEAR)
 
   include_directories(  ${GEAR_INCLUDE_DIRS} )
 
-  set(sources ${sources} src/gear/DDGear.cpp)
+  set(sources ${sources} src/gear/DDGear.cpp src/gear/createGearForILD.cpp)
 
   add_definitions("-D DD4HEP_USE_GEAR")
 
diff --git a/DDRec/include/DDRec/DDGear.h b/DDRec/include/DDRec/DDGear.h
index 40fa17b86..c84aabfe2 100644
--- a/DDRec/include/DDRec/DDGear.h
+++ b/DDRec/include/DDRec/DDGear.h
@@ -68,7 +68,7 @@ namespace DD4hep {
       @author  F.Gaede, DESY
       @version $Id$
   */
-  gear::GearMgr* createGearMgr( const Geometry::LCDD& lcdd ) ;
+  gear::GearMgr* createGearMgr( Geometry::LCDD& lcdd , const std::string& pluginName="GearForILD" ) ;
 
 }
 
diff --git a/DDRec/include/DDRec/DetectorData.h b/DDRec/include/DDRec/DetectorData.h
new file mode 100644
index 000000000..4f6519e1b
--- /dev/null
+++ b/DDRec/include/DDRec/DetectorData.h
@@ -0,0 +1,56 @@
+#ifndef DDRec_DetectorData_H_
+#define DDRec_DetectorData_H_
+
+
+namespace DD4hep {
+  namespace DDRec {
+    
+    /** Wrapper class for adding structs or pods as extensions to DetElements.
+     *  Provides default implementations of the c'tors required by the extension mechamism.
+     *  Structs (or classes) can be defined w/o any c'tors relying in the default ones 
+     *  created by the compiler.
+     * 
+     * @author F.Gaede, CERN/DESY
+     * @date Sep, 30 2014
+     * @version $Id: $
+     */
+    template< class T>
+    struct StructExtension : public T {
+      StructExtension() : T()  { } 
+      StructExtension(const StructExtension<T>& t) : T(t) {} 
+      StructExtension(const T& t) : T(t) {} 
+      StructExtension(const Geometry::DetElement& d) : T()  {}
+      StructExtension(const StructExtension<T>& t, const Geometry::DetElement& d) : T(t) {}
+    };
+
+    /** Simple data structure holding data that is relevant for
+     *  reconstruction of a cylindrical TPC with a pad plane that
+     *  is symmetrically populated with one size pads.
+     *  ( Can be used to instantiate a gear::TPCParmeter object
+     *    with a FixedPadSizeDiskLayout )
+     * 
+     * @author F.Gaede, CERN/DESY
+     * @date Sep, 30 2014
+     * @version $Id: $
+     */
+    struct FixedPadSizeTPCStruct{
+      double zHalf ;
+      double rMin ;
+      double rMax ;
+      double driftLength ;
+      double rMinReadout ;
+      double rMaxReadout ;
+      double innerWallThickness ;
+      double outerWallThickness ;
+      double padHeight ;
+      double padWidth ;
+      double maxRow ;
+      double padGap ;
+    };
+    typedef StructExtension<FixedPadSizeTPCStruct> FixedPadSizeTPCData ;
+
+
+  } /* namespace DDRec */
+} /* namespace DD4hep */
+
+#endif // DDRec_DetectorData_H_
diff --git a/DDRec/src/convertToGear.cc b/DDRec/src/convertToGear.cc
index fd7e6e7b1..7e1f34b59 100644
--- a/DDRec/src/convertToGear.cc
+++ b/DDRec/src/convertToGear.cc
@@ -26,21 +26,26 @@ using namespace dd4hep ;
 
 int main(int argc, char** argv ){
     
-  if( argc < 2 ) {
-    std::cout << " usage: convertToGear compact.xml [gear_file.xml]" << std::endl ;
+  if( argc < 3 ) {
+    std::cout << " usage: convertToGear plugin compact.xml [gear_file.xml]" << std::endl 
+	      << "  plugin: name of a plugin with a signature \"long int (LCDD*, int, char**)\" \n " 
+	      << "  or \"default\" " << std::endl ; 
+    
     exit(1) ;
   }
 
-  std::string inFile =  argv[1] ;
+  std::string pluginName =  argv[1] ;
+
+  std::string inFile =  argv[2] ;
 
-  std::string outFile = ( argc>2  ?  argv[2]  : ""  ) ;
+  std::string outFile = ( argc>3  ?  argv[3]  : ""  ) ;
 
   LCDD& lcdd = LCDD::getInstance();
 
   lcdd.fromCompact( inFile );
 
-  gear::GearMgr* gearMgr = createGearMgr( lcdd ) ;
-
+  gear::GearMgr* gearMgr = ( pluginName == "default" ) ?  createGearMgr( lcdd )  :   createGearMgr( lcdd , pluginName )  ;
+ 
   //----------------------------------------------------------------------------------
 
   // std::cout << "  ***************************** GEAR parameters **************************************** " << std::endl ;
@@ -56,6 +61,7 @@ int main(int argc, char** argv ){
   }
     
   gear::GearXML::createXMLFile ( gearMgr, outFile ) ;
+ 
 
   std::cout << "  ************************************************************** " << std::endl ;
   std::cout << "   created gear file : " << outFile << std::endl ;
diff --git a/DDRec/src/gear/DDGear.cpp b/DDRec/src/gear/DDGear.cpp
index 7e53c7a7e..dbdeee2b8 100644
--- a/DDRec/src/gear/DDGear.cpp
+++ b/DDRec/src/gear/DDGear.cpp
@@ -3,6 +3,7 @@
 #include "DD4hep/LCDD.h"
 #include "DD4hep/DD4hepUnits.h"
 #include "DD4hep/Fields.h"
+#include "DD4hep/Plugins.h"
 
 // ROOT
 //#include "TGeoManager.h"
@@ -24,12 +25,14 @@ namespace DD4hep{
   using namespace Geometry ;
 
 
-  gear::GearMgr* createGearMgr( const Geometry::LCDD& lcdd ){
+  gear::GearMgr* createGearMgr( Geometry::LCDD& lcdd , const std::string& pluginName ){
 
+    int argc(0); char** argv = 0 ;
 
+    lcdd.apply( pluginName.c_str() , argc, argv) ;
+    
     DetElement world = lcdd.world() ;
-
-
+    
     gear::GearMgrImpl* gearMgr = new gear::GearMgrImpl() ;
 
     gearMgr->setDetectorName(  lcdd.header().name() ) ; 
@@ -45,7 +48,7 @@ namespace DD4hep{
 
     DetElement::Children chMap = world.children() ;
 
-    // --- get all DetElements and their children into on vector
+    // --- get all DetElements and their children into one vector
     //     ( expect gear parameters to be only at top two levels )
     
     for ( DetElement::Children::const_iterator it=chMap.begin() ; it != chMap.end() ; ++it ){
@@ -75,7 +78,7 @@ namespace DD4hep{
 
       } catch( std::exception& e) {
       
-	std::cout << "  ***  " << e.what() << std::endl ;
+	//	std::cout << "  ***  subdetector " << dets[i].name() << " : " <<  e.what() << std::endl ;
 
 	continue ; // with next DetElement
       }
diff --git a/DDRec/src/gear/createGearForILD.cpp b/DDRec/src/gear/createGearForILD.cpp
new file mode 100644
index 000000000..8364988eb
--- /dev/null
+++ b/DDRec/src/gear/createGearForILD.cpp
@@ -0,0 +1,65 @@
+#include "DD4hep/LCDD.h"
+#include "DD4hep/Factories.h"
+
+#include "DDRec/DetectorData.h"
+
+#include "DDRec/DDGear.h"
+#include "gearimpl/TPCParametersImpl.h"
+#include "gearimpl/FixedPadSizeDiskLayout.h"
+
+
+#include <iostream>
+
+namespace DD4hep{
+  namespace DDRec{
+    
+    using namespace Geometry ;
+    //    using namespace gear ;
+
+    /** Plugin that creates Gear objects for DetElements and attaches them 
+     *  as extensions. Called from DDGear::createGearMgr().
+     *  NB: this code is for backward compatibility to run 
+     *  the old reconstruction that expects Gear information
+     *  and should eventually be phased out.
+     * 
+     *  @author  F.Gaede, CERN/DESY
+     *  @date Oct 2014
+     *  @version $Id: $
+     */
+    
+    static long createGearForILD(LCDD& lcdd, int argc, char** argv) {
+      
+      std::cout << " **** running plugin createGearForILD ! " <<  std::endl ;
+      
+
+      //***** TPC ********
+      DetElement tpcDE = lcdd.detector("TPC") ;
+
+      FixedPadSizeTPCData* tpc = tpcDE.extension<FixedPadSizeTPCData>() ;
+
+      gear::TPCParametersImpl* gearTPC = new gear::TPCParametersImpl( tpc->driftLength , gear::PadRowLayout2D::POLAR ) ;
+  
+      gearTPC->setPadLayout( new gear::FixedPadSizeDiskLayout( tpc->rMinReadout, tpc->rMaxReadout, tpc->padHeight, tpc->padWidth, tpc->maxRow, tpc->padGap  ) ) ;
+      
+      gearTPC->setDoubleVal("tpcInnerRadius", tpc->rMin )  ; // inner r of support tube
+      gearTPC->setDoubleVal("tpcOuterRadius", tpc->rMax )  ; // outer radius of TPC
+      gearTPC->setDoubleVal("tpcInnerWallThickness", tpc->innerWallThickness )  ;   // thickness of inner shell
+      gearTPC->setDoubleVal("tpcOuterWallThickness", tpc->outerWallThickness )  ;   // thickness of outer shell
+      
+      tpcDE.addExtension< GearHandle >( new GearHandle( gearTPC, "TPCParameters" ) ) ;
+      
+      //********************
+
+
+
+
+
+
+      // --- LCDD::apply() expects return code 1 if all went well ! ----
+      return 1;
+    }
+  }
+}
+DECLARE_APPLY( GearForILD, DD4hep::DDRec::createGearForILD )
+
+
-- 
GitLab