Newer
Older
TB_Chris / TbKernel / src / .svn / text-base / TbGeometrySvc.h.svn-base
  1. #ifndef TBGEOMETRYSVC_H
  2. #define TBGEOMETRYSVC_H
  3.  
  4. #include <map>
  5.  
  6. // Gaudi
  7. #include "GaudiKernel/Service.h"
  8. #include "GaudiKernel/Bootstrap.h"
  9. #include "GaudiKernel/Transform3DTypes.h"
  10.  
  11. // Local
  12. #include "TbKernel/ITbGeometrySvc.h"
  13. #include "TbKernel/TbModule.h"
  14.  
  15. template <class TYPE>
  16. class SvcFactory;
  17.  
  18. /** @class TbGeometrySvc TbGeometrySvc.h
  19. * Implementation of the testbeam geometry service.
  20. *
  21. */
  22.  
  23. class TbGeometrySvc : public extends1<Service, ITbGeometrySvc> {
  24.  
  25. public:
  26. /// Constructor
  27. TbGeometrySvc(const std::string& name, ISvcLocator* svc);
  28. /// Destructor
  29. virtual ~TbGeometrySvc();
  30.  
  31. virtual StatusCode initialize();
  32. virtual StatusCode finalize();
  33.  
  34. /// Transform a point in the local frame of a plane to global coordinates
  35. virtual Gaudi::XYZPoint localToGlobal(const Gaudi::XYZPoint& p,
  36. const unsigned int i) {
  37. return m_modules[i]->transform() * p;
  38. }
  39. /// Transform a point in the global frame to the local frame of a plane
  40. virtual Gaudi::XYZPoint globalToLocal(const Gaudi::XYZPoint& p,
  41. const unsigned int i) {
  42. return m_modules[i]->inverse() * p;
  43. }
  44.  
  45. /// Calculate the local x, y coordinates of a given pixel
  46. virtual bool pixelToPoint(const unsigned int scol, const unsigned int row,
  47. const unsigned int plane, double& x, double& y);
  48. /// Calculate the pixel and inter-pixel position of a given local point
  49. virtual bool pointToPixel(const double x, const double y,
  50. const unsigned int plane, unsigned int& scol,
  51. unsigned int& row);
  52.  
  53. /// Calculate the intercept of a track with a telescope plane
  54. virtual Gaudi::XYZPoint intercept(const LHCb::TbTrack* t,
  55. const unsigned int i);
  56.  
  57. /// Return a pointer to the module object for a given plane index
  58. virtual TbModule* module(const unsigned int i) { return m_modules[i]; }
  59. /// Return a pointer to the module object of a given plane identifier
  60. virtual TbModule* module(const std::string& det);
  61. /// Return the list of modules
  62. virtual const std::vector<TbModule*>& modules() { return m_modules; }
  63.  
  64. virtual unsigned int deviceIndex(const std::string& det) {
  65. auto p = m_deviceIndex.find(det);
  66. return p != m_deviceIndex.end() ? p->second : 999;
  67. }
  68. virtual unsigned int plane(const std::string& det) {
  69. auto p = m_planes.find(det);
  70. return p != m_planes.end() ? p->second : 999;
  71. }
  72.  
  73. virtual unsigned int nDevices() const { return m_nDevices; }
  74.  
  75. bool readConditions(const std::string& filename,
  76. std::vector<TbModule*>& modules);
  77. virtual void printAlignment(const std::vector<TbModule*>& modules);
  78.  
  79. virtual void setModule(unsigned int i, TbModule* module) {
  80. m_modules[i] = module;
  81. }
  82.  
  83. private:
  84. /// Allow SvcFactory to instantiate the service.
  85. friend class SvcFactory<TbGeometrySvc>;
  86. /// Transforms for each plane
  87. std::vector<TbModule*> m_modules;
  88. /// Map of plane names and their indices in the list
  89. std::map<std::string, unsigned int> m_moduleIndex;
  90. /// Map of device ids to their chip names
  91. std::map<std::string, unsigned int> m_deviceIndex;
  92. /// Map of device names to their planes indices
  93. std::map<std::string, unsigned int> m_planes;
  94. unsigned int m_nDevices;
  95.  
  96. /// Cached x-coordinates of pixels on sensor tiles.
  97. std::vector<double> m_xTriple;
  98.  
  99. /// Pointer to message stream
  100. mutable MsgStream* m_msg;
  101. /// On-demand access to message stream
  102. MsgStream& msg() const {
  103. if (!m_msg) m_msg = new MsgStream(msgSvc(), name());
  104. return *m_msg;
  105. }
  106.  
  107. /// Calculate the intercept of a straight line with a telescope plane
  108. Gaudi::XYZPoint intercept(const Gaudi::XYZPoint& p, const Gaudi::XYZVector& t,
  109. const unsigned int i) {
  110. const Gaudi::XYZVector n = m_modules[i]->normal();
  111. const double s = n.Dot(m_modules[i]->centre() - p) / n.Dot(t);
  112. return p + s * t;
  113. }
  114.  
  115. };
  116.  
  117. #endif