Newer
Older
TB_Chris / TbAlgorithms / src / .svn / text-base / TbTriggerAssociator.cpp.svn-base
  1. // Gaudi
  2. #include "GaudiKernel/PhysicalConstants.h"
  3.  
  4. // Tb/TbEvent
  5. #include "Event/TbTrack.h"
  6.  
  7. // Local
  8. #include "TbTriggerAssociator.h"
  9.  
  10. DECLARE_ALGORITHM_FACTORY(TbTriggerAssociator)
  11.  
  12. //=============================================================================
  13. // Standard constructor
  14. //=============================================================================
  15. TbTriggerAssociator::TbTriggerAssociator(const std::string& name,
  16. ISvcLocator* pSvcLocator)
  17. : TbAlgorithm(name, pSvcLocator) {
  18.  
  19. declareProperty("TrackLocation",
  20. m_trackLocation = LHCb::TbTrackLocation::Default);
  21. declareProperty("TriggerLocation",
  22. m_triggerLocation = LHCb::TbTriggerLocation::Default);
  23.  
  24. declareProperty("TimeWindow", m_twindow = 1000. * Gaudi::Units::ns);
  25. declareProperty("TimeOffset", m_toffset = 0.);
  26. declareProperty("Plane", m_plane = 999);
  27. }
  28.  
  29. //=============================================================================
  30. // Initialization
  31. //=============================================================================
  32. StatusCode TbTriggerAssociator::initialize() {
  33.  
  34. // Initialise the base class.
  35. StatusCode sc = TbAlgorithm::initialize();
  36. if (sc.isFailure()) return sc;
  37. return StatusCode::SUCCESS;
  38. }
  39.  
  40. //=============================================================================
  41. // Main execution
  42. //=============================================================================
  43. StatusCode TbTriggerAssociator::execute() {
  44.  
  45. // Grab the tracks.
  46. LHCb::TbTracks* tracks = getIfExists<LHCb::TbTracks>(m_trackLocation);
  47. if (!tracks) {
  48. return Error("No tracks in " + m_trackLocation);
  49. }
  50. // Grab the triggers.
  51. std::vector<LHCb::TbTriggers*> triggers(m_nPlanes, nullptr);
  52. std::vector<LHCb::TbTriggers::iterator> begin(m_nPlanes);
  53. std::vector<LHCb::TbTriggers::iterator> end(m_nPlanes);
  54. for (unsigned int i = 0; i < m_nPlanes; ++i) {
  55. if (m_plane != 999 && i != m_plane) continue;
  56. const std::string location = m_triggerLocation + std::to_string(i);
  57. triggers[i] = getIfExists<LHCb::TbTriggers>(location);
  58. if (!triggers[i]) {
  59. return Error("No triggers in " + location);
  60. }
  61. begin[i] = triggers[i]->begin();
  62. end[i] = triggers[i]->end();
  63. }
  64.  
  65. // Loop over the tracks.
  66. for (LHCb::TbTrack* track : *tracks) {
  67. const double t = track->htime() + m_toffset;
  68. // Calculate the time window.
  69. const double tMin = t - m_twindow;
  70. const double tMax = t + m_twindow;
  71. for (unsigned int i = 0; i < m_nPlanes; ++i) {
  72. if (m_plane != 999 && i != m_plane) continue;
  73. if (triggers[i]->empty()) continue;
  74. // Get the first trigger within the time window (if any).
  75. LHCb::TbTriggers::iterator it =
  76. std::lower_bound(begin[i], end[i], tMin, lowerBound());
  77. if (it == end[i]) continue;
  78. // Associate all triggers within the window to the track.
  79. for (; it != end[i]; ++it) {
  80. // Stop when outside the time window.
  81. if ((*it)->htime() > tMax) break;
  82. track->addToTriggers(*it);
  83. (*it)->setAssociated(true);
  84. }
  85. }
  86. }
  87. return StatusCode::SUCCESS;
  88. }