Newer
Older
TB_Chris / TbEvent / Event / .svn / text-base / TbKalmanNode.h.svn-base
  1. #ifndef TBTRACKFITNODE_H
  2. #define TBTRACKFITNODE_H 1
  3.  
  4. // from TbEvent
  5. #include "Event/TbState.h"
  6. #include "Event/TbCluster.h"
  7. #include "Event/ChiSquare.h"
  8.  
  9. // forward declarations
  10. namespace LHCb { class TbKalmanTrack; }
  11.  
  12. namespace LHCb {
  13.  
  14. /** @class TbKalmanFitNode TbKalmanFitNode.h Event/TbKalmanFitNode.h
  15. *
  16. * This File contains the declaration of the TbKalmanFitNode.
  17. *
  18. * A TbKalmanFitNode represents a node in the kalman fit.
  19. *
  20. */
  21.  
  22. class TbKalmanNode {
  23. public:
  24. // important note: for the Forward fit, smoothed means
  25. // 'classical'. for the backward fit, it means 'bidirectional'.
  26. enum FilterStatus {
  27. Uninitialized,
  28. Initialized,
  29. Predicted,
  30. Filtered,
  31. Smoothed
  32. };
  33. enum Direction {
  34. Forward = 0,
  35. Backward = 1
  36. };
  37. enum Type {
  38. Reference,
  39. Measurement,
  40. Outlier
  41. };
  42. enum CachedBool {
  43. False = 0,
  44. True = 1,
  45. Unknown = 2
  46. };
  47.  
  48. typedef LHCb::TbState State;
  49. typedef Gaudi::Vector4 StateParameters;
  50. typedef Gaudi::SymMatrix4x4 StateCovariance;
  51.  
  52. // helper class that contains data for forward fit
  53. /* struct ForwardFitState { */
  54. /* ForwardFitState() : */
  55. /* deltaChi2(0), status(Initialized) {} */
  56. /* typedef */
  57. /* enum FilterStatus {Initialized, Predicted, Filtered, Smoothed } ; */
  58. /* enum CachedBool {False=0, True=1, Unknown=2;} ; */
  59. /* TbState predictedState ; ///< predicted state of
  60. * forward/backward filter */
  61. /* TbState filteredState ; ///< filtered state of forward filter
  62. */
  63. /* double deltaChi2 ; ///< chisq contribution in forward
  64. * filter */
  65. /* FilterStatus status ; ///< Status of the Node in the fit
  66. * process */
  67. /* } ; */
  68.  
  69. /// Default constructor
  70. TbKalmanNode();
  71.  
  72. /// Constructor from a z position
  73. TbKalmanNode(TbKalmanTrack& parent, double zPos);
  74.  
  75. /// Destructor
  76. virtual ~TbKalmanNode();
  77.  
  78. /// Clone the Node
  79. //virtual TbKalmanNode* clone() const;
  80.  
  81. /// set the seed matrix
  82. void setSeedCovariance(const StateCovariance& cov) {
  83. m_predictedState[Forward].covariance() = cov;
  84. m_predictedState[Backward].covariance() = cov;
  85. resetFilterStatus();
  86. }
  87.  
  88. /// set the seed
  89. void setSeed(const State& seedstate) {
  90. // just copy covariance (assuming it to be rather meaningless), but
  91. // transport the state
  92. State astate(seedstate);
  93. double dz = z() - seedstate.z();
  94. astate.parameters()(0) += dz * astate.parameters()(2);
  95. astate.parameters()(1) += dz * astate.parameters()(3);
  96. astate.setZ(z());
  97. m_predictedState[Forward] = astate;
  98. m_predictedState[Backward] = astate;
  99. resetFilterStatus();
  100. }
  101.  
  102. /// retrieve chisq contribution in upstream filter
  103. const LHCb::ChiSquare& deltaChi2(int direction) const {
  104. filteredState(direction);
  105. return m_deltaChi2[direction];
  106. }
  107.  
  108. /// get the z
  109. double z() const { return m_state.z(); }
  110.  
  111. /// get the filter status (only useful for debugging)
  112. FilterStatus filterStatus(int direction) const {
  113. return m_filterStatus[direction];
  114. }
  115.  
  116. /// return whether or not this node has active nodes upstream
  117. bool hasInfoUpstream(int direction) const;
  118.  
  119. /// Get the index of this node. For debugging only.
  120. int index() const;
  121.  
  122. /// Unlink this node
  123. void unLink() {
  124. m_prevNode = m_nextNode = 0;
  125. m_parent = 0;
  126. }
  127.  
  128. void link(TbKalmanNode* prevnode) {
  129. m_prevNode = prevnode;
  130. if (m_prevNode) m_prevNode->m_nextNode = this;
  131. m_nextNode = 0;
  132. }
  133.  
  134. /// set the parent
  135. void setParent(TbKalmanTrack* p) { m_parent = p; }
  136.  
  137. /// get the parent
  138. TbKalmanTrack* parent() { return m_parent; }
  139.  
  140. public:
  141. const TbKalmanNode* prevNode(int direction) const {
  142. return direction == Forward ? m_prevNode : m_nextNode;
  143. }
  144. const TbKalmanNode* nextNode(int direction) const {
  145. return direction == Forward ? m_nextNode : m_prevNode;
  146. }
  147.  
  148. /// retrieve the predicted state
  149. const State& predictedState(int direction) const {
  150. if (m_filterStatus[direction] < Predicted)
  151. unConst().computePredictedState(direction);
  152. return m_predictedState[direction];
  153. }
  154.  
  155. /// retrieve the filtered state
  156. const State& filteredState(int direction) const {
  157. if (m_filterStatus[direction] < Filtered)
  158. unConst().computeFilteredState(direction);
  159. return m_filteredState[direction];
  160. }
  161.  
  162. /// retrieve the bismoothed state
  163. const State& state() const {
  164. if (m_filterStatus[Backward] < Smoothed) unConst().computeBiSmoothedState();
  165. return m_state;
  166. }
  167.  
  168. /// This is used from the projectors (or from any set method?)
  169. void resetFilterStatus(FilterStatus s = Initialized) {
  170. resetFilterStatus(Forward, s);
  171. resetFilterStatus(Backward, s);
  172. }
  173.  
  174. /// Set the noise term
  175. void setNoise2(double noise2) { m_Q = noise2; }
  176.  
  177. protected:
  178. /// virtual function to be overloaded by Measurement implementations
  179. virtual void updateResidual(const State& /* state */) {}
  180. virtual LHCb::ChiSquare filter(State& /* state */) const {
  181. return LHCb::ChiSquare();
  182. }
  183. virtual bool hasInfo() const { return false; }
  184. virtual void deactivateMeasurement(bool /* deactivate */) {}
  185.  
  186. private:
  187. void computePredictedState(int direction);
  188. void computeFilteredState(int direction);
  189. void computeBiSmoothedState();
  190. void computeClassicalSmoothedState();
  191. TbKalmanNode& unConst() const { return const_cast<TbKalmanNode&>(*this); }
  192.  
  193. protected:
  194. /// reset the cache for the previous function
  195. void resetHasInfoUpstream(int direction);
  196.  
  197. /// reset the filter status
  198. void resetFilterStatus(int direction, FilterStatus s = Initialized);
  199.  
  200. private:
  201.  
  202. TbKalmanTrack* m_parent; ///< Owner
  203. FilterStatus m_filterStatus[2]; ///< Status of the Node in the fit process
  204. CachedBool m_hasInfoUpstream[
  205. 2]; ///< Are the nodes with active measurement upstream of this node?
  206. State m_predictedState[2]; ///< predicted state of forward/backward filter
  207. State m_filteredState[2]; ///< filtered state of forward filter
  208. State m_state; ///< Smoothed state
  209. LHCb::ChiSquare m_deltaChi2[2]; ///< chisq contribution in forward filter
  210. //LHCb::ChiSquare m_totalChi2[2]; ///< total chi2 after this
  211. //filterstep
  212. //Gaudi::TrackMatrix m_smootherGainMatrix ; ///< smoother gain matrix
  213. //(smoothedfit only)
  214. TbKalmanNode* m_prevNode; ///< Previous Node
  215. TbKalmanNode* m_nextNode; ///< Next Node
  216.  
  217. double m_Q; ///< noise between this node and the next node
  218. };
  219.  
  220. } // namespace LHCb
  221.  
  222. #endif // TRACKFITEVENT_FITNODE_H