Newer
Older
TB_Chris / TbKernel / src / .svn / text-base / TbClusterFinder.cpp.svn-base
  1. // Local
  2. #include "TbKernel/ITbGeometrySvc.h"
  3. #include "TbClusterFinder.h"
  4.  
  5. DECLARE_TOOL_FACTORY(TbClusterFinder)
  6.  
  7. //=============================================================================
  8. // Constructor
  9. //=============================================================================
  10. TbClusterFinder::TbClusterFinder(const std::string& type,
  11. const std::string& name,
  12. const IInterface* parent)
  13. : GaudiTool(type, name, parent), m_searchAlgo(1) {
  14.  
  15. declareInterface<ITbClusterFinder>(this);
  16. }
  17.  
  18. //=============================================================================
  19. // Set the search algorithm
  20. //=============================================================================
  21. void TbClusterFinder::setSearchAlgorithm(const std::string& searchAlgorithm) {
  22.  
  23. if (searchAlgorithm == "seq") {
  24. m_searchAlgo = SearchMethod::Seq;
  25. } else if (searchAlgorithm == "adap_seq") {
  26. m_searchAlgo = SearchMethod::AdapSeq;
  27. }
  28. }
  29.  
  30. //=============================================================================
  31. // Initialisation
  32. //=============================================================================
  33. StatusCode TbClusterFinder::initialize() {
  34.  
  35. // Initialise the base class.
  36. StatusCode sc = GaudiTool::initialize();
  37. if (sc.isFailure()) return sc;
  38.  
  39. // Get the number of telescope planes from the geometry service.
  40. ITbGeometrySvc* geomSvc = svc<ITbGeometrySvc>("TbGeometrySvc", true);
  41. if (!geomSvc) {
  42. error() << "Cannot access geometry service" << endmsg;
  43. return StatusCode::FAILURE;
  44. }
  45. const unsigned int nPlanes = geomSvc->modules().size();
  46. m_first.resize(nPlanes);
  47. m_last.resize(nPlanes);
  48. m_end.resize(nPlanes);
  49. m_nClusters.resize(nPlanes);
  50. m_empty.resize(nPlanes);
  51. m_prev_ts.resize(nPlanes);
  52. m_prev.resize(nPlanes);
  53. return StatusCode::SUCCESS;
  54. }
  55.  
  56. //=============================================================================
  57. // Set the iterators and other properties for a given plane.
  58. //=============================================================================
  59. void TbClusterFinder::setClusters(LHCb::TbClusters* cs,
  60. const unsigned int& plane) {
  61.  
  62. m_nClusters[plane] = cs->size();
  63. m_empty[plane] = cs->empty();
  64. if (!cs->empty()) {
  65. m_first[plane] = cs->begin();
  66. m_last[plane] = cs->end() - 1;
  67. m_end[plane] = cs->end();
  68. m_prev[plane] = cs->begin();
  69. m_prev_ts[plane] = (*cs->begin())->htime();
  70. }
  71. }
  72.  
  73. //=============================================================================
  74. // Get iterator to first cluster on a given plane inside a given time window
  75. //=============================================================================
  76. TbClusterFinder::Iterator TbClusterFinder::getIterator(
  77. const double& t, const unsigned int& plane) {
  78.  
  79. if (m_searchAlgo == SearchMethod::Seq) {
  80. return seq_search(t, plane);
  81. } else if (m_searchAlgo == SearchMethod::AdapSeq) {
  82. return adap_seq_search(t, plane);
  83. }
  84. warning() << "Unknown search algorithm" << endmsg;
  85. return adap_seq_search(t, plane);
  86. }
  87.  
  88. //=============================================================================
  89. // Sequential search
  90. //=============================================================================
  91. TbClusterFinder::Iterator TbClusterFinder::seq_search(
  92. const double& t, const unsigned int& plane) {
  93.  
  94. Iterator first = m_first[plane];
  95. if (t <= (*first)->htime()) return first;
  96. Iterator last = m_last[plane];
  97. for (Iterator ic = first; ic != last; ++ic) {
  98. if ((*ic)->htime() >= t) return ic;
  99. }
  100. return last;
  101. }
  102.  
  103. //=============================================================================
  104. // Adaptive sequential search
  105. //=============================================================================
  106. TbClusterFinder::Iterator TbClusterFinder::adap_seq_search(
  107. const double& t, const unsigned int& plane) {
  108. // Returns the index in the clusters list of the cluster which arrived
  109. // CURRENTLY RETURNS SOMETHING NEAR, BUT ISNT PERFECT.
  110.  
  111. // Get (and update) the previous timestamp.
  112. const auto tPrev = m_prev_ts[plane];
  113. m_prev_ts[plane] = t;
  114.  
  115. Iterator first = m_first[plane];
  116. Iterator last = m_last[plane];
  117. // Handle cases where requested time is outside the range of this plane.
  118. if (t < (*first)->htime()) {
  119. m_prev[plane] = first;
  120. return first;
  121. } else if (t >= (*last)->htime()) {
  122. m_prev[plane] = last;
  123. return last;
  124. }
  125.  
  126. // Handle cases where requested time is inside the range of the clusters.
  127. if (t == tPrev) {
  128. // Case where t = previous asked t
  129. // (Covers the case where t is equal to the first cluster, since this is
  130. // default).
  131. return m_prev[plane];
  132. } else if (t < tPrev) {
  133. // Case where t < previous asked t. (Unwise usage):
  134. m_prev[plane] = first;
  135. return first;
  136. }
  137.  
  138. // Case where t > previous asked t (Majority of cases):
  139. for (Iterator ic = m_prev[plane]; ic != last; ++ic) {
  140. if ((*ic)->htime() >= t) {
  141. m_prev[plane] = ic;
  142. return ic;
  143. }
  144. }
  145. m_prev[plane] = first;
  146. return first;
  147. }