GeoSvc.cpp 5.56 KB
Newer Older
Whitney Armstrong's avatar
Whitney Armstrong committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//
//  GeoSvc.cxx
//
//
//  Created by Julia Hrdinka on 30/03/15.
//
//

#include "GeoSvc.h"
#include "GaudiKernel/Service.h"
//#include "GeoConstruction.h"
#include "TGeoManager.h"

#include "DD4hep/Printout.h"

Whitney Armstrong's avatar
Whitney Armstrong committed
16
17
18
19
#include "JugBase/ACTSLogger.h"

#include "Acts/Geometry/TrackingGeometry.hpp"
#include "Acts/Plugins/DD4hep/ConvertDD4hepDetector.hpp"
20
21
22
23
24
25
26
27
28
29
30
31
#include "Acts/Surfaces/PlaneSurface.hpp"


void draw_surfaces(std::shared_ptr<const Acts::TrackingGeometry> trk_geo, const std::string& fname)
{
  using namespace Acts;
  Acts::GeometryContext tgContext = Acts::GeometryContext();
  std::vector<const Surface*> surfaces;

  trk_geo->visitSurfaces([&](const Acts::Surface* surface) {
    // for now we just require a valid surface
    if (not surface) {
32
      std::cout << " Not a surface \n";
33
34
35
36
37
38
      return;
    }
    surfaces.push_back(surface);
  });
  std::ofstream os;
  os.open(fname);
39
  os << std::fixed << std::setprecision(6);
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
  size_t nVtx = 0;
  for (const auto& srfx : surfaces) {
    const PlaneSurface*                 srf    = dynamic_cast<const PlaneSurface*>(srfx);
    const PlanarBounds*                 bounds = dynamic_cast<const PlanarBounds*>(&srf->bounds());
    for (const auto& vtxloc : bounds->vertices()) {
      Vector3 vtx = srf->transform(tgContext) * Vector3(vtxloc.x(), vtxloc.y(), 0);
      os << "v " << vtx.x() << " " << vtx.y() << " " << vtx.z() << "\n";
    }
    // connect them
    os << "f";
    for (size_t i = 1; i <= bounds->vertices().size(); ++i) {
      os << " " << nVtx + i;
    }
    os << "\n";
    nVtx += bounds->vertices().size();
  }
  os.close();
}

Whitney Armstrong's avatar
Whitney Armstrong committed
59
60
61
62
63
64
using namespace Gaudi;

DECLARE_COMPONENT(GeoSvc)

GeoSvc::GeoSvc(const std::string& name, ISvcLocator* svc)
    : base_class(name, svc)
65
    //, m_incidentSvc("IncidentSvc", "GeoSvc")
66
    , m_trackingGeo(nullptr)
Whitney Armstrong's avatar
Whitney Armstrong committed
67
68
    , m_dd4hepgeo(0)
    //, m_geant4geo(0)
69
    , m_log(msgSvc(), name) {}
Whitney Armstrong's avatar
Whitney Armstrong committed
70
71

GeoSvc::~GeoSvc() {
72
  if (m_dd4hepgeo) {
Whitney Armstrong's avatar
Whitney Armstrong committed
73
74
75
    try {
      m_dd4hepgeo->destroyInstance();
      m_dd4hepgeo = 0;
76
77
    } catch (...) {
    }
Whitney Armstrong's avatar
Whitney Armstrong committed
78
79
80
81
82
  }
}

StatusCode GeoSvc::initialize() {
  StatusCode sc = Service::initialize();
83
84
  if (!sc.isSuccess())
    return sc;
Whitney Armstrong's avatar
Whitney Armstrong committed
85
86
87
88
89
90
  // Turn off TGeo printouts if appropriate for the msg level
  if (msgLevel() >= MSG::INFO) {
    TGeoManager::SetVerboseLevel(0);
  }
  uint printoutLevel = msgLevel();
  dd4hep::setPrintLevel(dd4hep::PrintLevel(printoutLevel));
91
  // m_incidentSvc->addListener(this, "GeometryFailure");
Whitney Armstrong's avatar
Whitney Armstrong committed
92
93
94
95
96
  if (buildDD4HepGeo().isFailure())
    m_log << MSG::ERROR << "Could not build DD4Hep geometry" << endmsg;
  else
    m_log << MSG::INFO << "DD4Hep geometry SUCCESSFULLY built" << endmsg;

97
  // if (buildGeant4Geo().isFailure())
Whitney Armstrong's avatar
Whitney Armstrong committed
98
  //  m_log << MSG::ERROR << "Could not build Geant4 geometry" << endmsg;
99
  // else
Whitney Armstrong's avatar
Whitney Armstrong committed
100
  //  m_log << MSG::INFO << "Geant4 geometry SUCCESSFULLY built" << endmsg;
101
  // if (m_failureFlag) {
102
103
  //  return StatusCode::FAILURE;
  //}
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
  Acts::Logging::Level geoMsgLevel;
  switch (msgLevel()) {
  case (MSG::DEBUG):
    geoMsgLevel = Acts::Logging::DEBUG;
    break;
  case (MSG::VERBOSE):
    geoMsgLevel = Acts::Logging::VERBOSE;
    break;
  case (MSG::INFO):
    geoMsgLevel = Acts::Logging::INFO;
    break;
  case (MSG::WARNING):
    geoMsgLevel = Acts::Logging::WARNING;
    break;
  case (MSG::FATAL):
    geoMsgLevel = Acts::Logging::FATAL;
    break;
  case (MSG::ERROR):
    geoMsgLevel = Acts::Logging::ERROR;
    break;
  default:
    geoMsgLevel = Acts::Logging::VERBOSE;
  }
127
  m_trackingGeo = std::move(Acts::convertDD4hepDetector(m_dd4hepgeo->world(), Acts::Logging::VERBOSE, Acts::equidistant,
128
                                                        Acts::equidistant, Acts::equidistant));
129
  if (m_trackingGeo) {
130
131
    draw_surfaces(m_trackingGeo, "tracking_geometry.obj");

132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
    debug() << "visiting all the surfaces  " << endmsg;
    m_trackingGeo->visitSurfaces([this](const Acts::Surface* surface) {
      // for now we just require a valid surface
      if (not surface) {
        return;
      }
      auto det_element =
          dynamic_cast<const Acts::DD4hepDetectorElement*>(surface->associatedDetectorElement());
      if (!det_element) {
        debug() << "invalid det_element!!! " << endmsg;
        return;
      }
      this->m_surfaces.insert_or_assign(det_element->identifier(), surface);
    });
  }
147

Whitney Armstrong's avatar
Whitney Armstrong committed
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
  return StatusCode::SUCCESS;
}

StatusCode GeoSvc::finalize() { return StatusCode::SUCCESS; }

StatusCode GeoSvc::buildDD4HepGeo() {
  // we retrieve the the static instance of the DD4HEP::Geometry
  m_dd4hepgeo = &(dd4hep::Detector::getInstance());
  m_dd4hepgeo->addExtension<IGeoSvc>(this);

  // load geometry
  for (auto& filename : m_xmlFileNames) {
    m_log << MSG::INFO << "loading geometry from file:  '" << filename << "'" << endmsg;
    m_dd4hepgeo->fromCompact(filename);
  }
  m_dd4hepgeo->volumeManager();
  m_dd4hepgeo->apply("DD4hepVolumeManager", 0, 0);
165
  m_cellid_converter = std::make_shared<const dd4hep::rec::CellIDPositionConverter>(*m_dd4hepgeo);
Whitney Armstrong's avatar
Whitney Armstrong committed
166
167
168
  return StatusCode::SUCCESS;
}

169
dd4hep::Detector* GeoSvc::detector() { return (m_dd4hepgeo); }
Whitney Armstrong's avatar
Whitney Armstrong committed
170

171
dd4hep::DetElement GeoSvc::getDD4HepGeo() { return (detector()->world()); }
Whitney Armstrong's avatar
Whitney Armstrong committed
172
173
174
175
176
177
178
179
180
181
182
183

//StatusCode GeoSvc::buildGeant4Geo() {
//  std::shared_ptr<G4VUserDetectorConstruction> detector(new det::GeoConstruction(*lcdd()));
//  m_geant4geo = detector;
//  if (m_geant4geo) {
//    return StatusCode::SUCCESS;
//  } else
//    return StatusCode::FAILURE;
//}

//G4VUserDetectorConstruction* GeoSvc::getGeant4Geo() { return (m_geant4geo.get()); }

184
185
186
187
188
189
//void GeoSvc::handle(const Incident& inc) {
//  error() << "Handling incident '" << inc.type() << "'" << endmsg;
//  if (!inc.type().compare("GeometryFailure")) {
//    m_failureFlag = true;
//  }
//}