00001
00007 #include "GtTransformTool.h"
00008
00009 #include "DetDesc/ILVolume.h"
00010 #include "DetDesc/IDetectorElement.h"
00011 #include "DetDesc/IGeometryInfo.h"
00012
00013 #include "GaudiKernel/Point3DTypes.h"
00014
00015 #include "HepMC/GenEvent.h"
00016 #include "HepMC/GenVertex.h"
00017
00018 #include "CLHEP/Vector/LorentzVector.h"
00019
00020 GtTransformTool::GtTransformTool(const std::string& type,
00021 const std::string& name,
00022 const IInterface* parent)
00023 : GaudiTool(type,name,parent)
00024 , m_offset(3,0)
00025 , m_detelem(0)
00026 {
00027 declareInterface<IHepMCEventMutator>(this);
00028
00029 declareProperty("Volume",m_volume="",
00030 "Name of the DetectorElement to provide the local coordinate system.");
00031
00032 declareProperty("Offset",m_offset,
00033 "Spatial offset to apply to vertices before transformation");
00034
00035 declareProperty("IgnorePosition",m_ignorePosition = false,
00036 "Do not transform the position.");
00037
00038 declareProperty("IgnoreDirection",m_ignoreDirection = false,
00039 "Do not transform the direction.");
00040 }
00041
00042 const IDetectorElement* GtTransformTool::detelem()
00043 {
00044 if (m_detelem) return m_detelem;
00045
00046 if ("" == m_volume) return 0;
00047
00048 if (!existDet<IDetectorElement>(m_volume)) return 0;
00049
00050 m_detelem = getDet<IDetectorElement>(m_volume);
00051 return m_detelem;
00052 }
00053
00054 GtTransformTool::~GtTransformTool()
00055 {
00056 }
00057
00058 StatusCode GtTransformTool::initialize()
00059 {
00060 this->GaudiTool::initialize();
00061
00062 const IDetectorElement* detelem = this->detelem();
00063 if (!detelem) {
00064 fatal() << "Could not get detector element: \"" << m_volume << "\"" << endreq;
00065 return StatusCode::FAILURE;
00066 }
00067
00068 return StatusCode::SUCCESS;
00069 }
00070
00071 #include "CLHEP/Units/SystemOfUnits.h"
00072
00073 StatusCode GtTransformTool::mutate(HepMC::GenEvent& event)
00074 {
00075 if (!m_ignorePosition) {
00076
00077 debug() << "Transforming " << event.vertices_size() << " vertices:\n";
00078 HepMC::GenEvent::vertex_iterator vtx, done = event.vertices_end();
00079 for (vtx = event.vertices_begin(); vtx != done; ++vtx) {
00080 HepMC::FourVector pos = (*vtx)->position();
00081
00082
00083 Gaudi::XYZPoint local_point(pos.x()+m_offset[0],pos.y()+m_offset[1],pos.z()+m_offset[2]);
00084 Gaudi::XYZPoint global_point = m_detelem->geometry()->toGlobal(local_point);
00085
00086 debug() << "\ttransform("<< pos.x()<<","<<pos.y()<<","<<pos.z() << ")\n"
00087 << "\tlocal: " << local_point
00088 << "\tglobal: " << global_point << '\n'
00089 << "\t detelem: " << *m_detelem << "\n"
00090
00091 << "\n";
00092
00093 pos.setX(global_point.x());
00094 pos.setY(global_point.y());
00095 pos.setZ(global_point.z());
00096 (*vtx)->set_position(pos);
00097 debug() << "\tvertex @ " << "("
00098 << (*vtx)->position().x()/CLHEP::cm << ","
00099 << (*vtx)->position().y()/CLHEP::cm << ","
00100 << (*vtx)->position().z()/CLHEP::cm << ","
00101 << (*vtx)->position().t()/CLHEP::second << ") [cm,cm,cm,second]";
00102 }
00103 debug () << endreq;
00104 }
00105
00106 if (!m_ignoreDirection) {
00107
00108 HepMC::GenEvent::particle_iterator pit, pdone = event.particles_end();
00109 for (pit = event.particles_begin(); pit != pdone; ++pit) {
00110 HepMC::FourVector mom = (*pit)->momentum();
00111
00112
00113 Gaudi::XYZVector lmom(mom.px(),mom.py(),mom.pz());
00114 Gaudi::XYZVector gmom = m_detelem->geometry()->toGlobal(lmom);
00115 mom.set(gmom.x(),gmom.y(),gmom.z(),mom.e());
00116
00117 (*pit)->set_momentum(mom);
00118 }
00119 }
00120
00121 return StatusCode::SUCCESS;
00122 }
00123
00124 StatusCode GtTransformTool::finalize()
00125 {
00126
00127 return this->GaudiTool::finalize();
00128 }
00129