Newer
Older
TB_Chris / TbIO / src / .svn / text-base / TbPacketRecycler.cpp.svn-base
  1. #include "TbPacketRecycler.h"
  2. #include "Event/TbTrack.h"
  3.  
  4. DECLARE_ALGORITHM_FACTORY(TbPacketRecycler)
  5.  
  6. //=============================================================================
  7. // Standard constructor, initializes variables
  8. //=============================================================================
  9. TbPacketRecycler::TbPacketRecycler(const std::string& name,
  10. ISvcLocator* pSvcLocator)
  11. : TbAlgorithm(name, pSvcLocator) {
  12.  
  13. // Declare algorithm properties - see header for description.
  14. declareProperty("HitLocation", m_hitLocation = LHCb::TbHitLocation::Default);
  15. declareProperty("TriggerLocation",
  16. m_trgLocation = LHCb::TbTriggerLocation::Default);
  17. declareProperty("ClusterLocation",
  18. m_clusLocation = LHCb::TbClusterLocation::Default);
  19. }
  20.  
  21. //=============================================================================
  22. // Initialisation
  23. //=============================================================================
  24. StatusCode TbPacketRecycler::initialize() {
  25.  
  26. // Initialise the base class.
  27. StatusCode sc = TbAlgorithm::initialize();
  28. if (sc.isFailure()) return sc;
  29. IToolSvc* toolsvc = nullptr;
  30. sc = service("ToolSvc", toolsvc);
  31. if (!sc.isSuccess()) {
  32. return Error("Unable to get a handle to the tool service", sc);
  33. }
  34. for (unsigned int i = 0; i < m_nPlanes; ++i) {
  35. TbRawStream* foo = nullptr;
  36. sc = toolSvc()->retrieveTool("TbRawStream/" + std::to_string(i), foo);
  37. if (!sc.isSuccess()) {
  38. return Error("Unable to retrieve TbRawStream for plane " +
  39. std::to_string(i), sc);
  40. }
  41. m_streams.push_back(foo);
  42. }
  43. return StatusCode::SUCCESS;
  44. }
  45.  
  46. //=============================================================================
  47. // Main execution
  48. //=============================================================================
  49. StatusCode TbPacketRecycler::execute() {
  50.  
  51. for (unsigned int i = 0; i < m_nPlanes; ++i) {
  52. const std::string hitLocation = m_hitLocation + std::to_string(i);
  53. const std::string trgLocation = m_trgLocation + std::to_string(i);
  54. const std::string clusLocation = m_clusLocation + std::to_string(i);
  55.  
  56. LHCb::TbHits* hits = getIfExists<LHCb::TbHits>(hitLocation);
  57. LHCb::TbTriggers* trgs = getIfExists<LHCb::TbTriggers>(trgLocation);
  58. LHCb::TbClusters* clusters = getIfExists<LHCb::TbClusters>(clusLocation);
  59.  
  60. removeClusters(clusters);
  61. recycle(hits, m_streams[i]);
  62. recycle(trgs, m_streams[i]);
  63. }
  64. return StatusCode::SUCCESS;
  65. }
  66.  
  67. //=============================================================================
  68. // Copy unused hits and triggers to the cache.
  69. //=============================================================================
  70. template <typename TYPE>
  71. void TbPacketRecycler::recycle(
  72. KeyedContainer<TYPE, Containers::HashMap>* container, TbRawStream* s) {
  73. if (!container) return;
  74. std::vector<TYPE*> tmp_cache;
  75. tmp_cache.clear();
  76. typename KeyedContainer<TYPE, Containers::HashMap>::reverse_iterator packet;
  77. for (packet = container->rbegin(); packet != container->rend(); ++packet) {
  78. // Break when outside the overlap time window.
  79. if (timingSvc()->beforeOverlap((*packet)->time())){
  80. break;
  81. }
  82. // If unused, add the hit/trigger to the temporary cache.
  83. if (!(*packet)->associated()) {
  84. tmp_cache.push_back(new TYPE(*packet));
  85. container->remove(*packet);
  86. }
  87. }
  88. std::reverse(tmp_cache.begin(), tmp_cache.end());
  89. s->cache<TYPE*>()->insert(s->cache<TYPE*>()->begin(), tmp_cache.begin(),
  90. tmp_cache.end());
  91. }
  92.  
  93. //=============================================================================
  94. // Remove non-associated clusters from a container
  95. //=============================================================================
  96. void TbPacketRecycler::removeClusters(LHCb::TbClusters* clusters) {
  97. if (!clusters) return;
  98. LHCb::TbClusters::reverse_iterator it;
  99. for (it = clusters->rbegin(); it != clusters->rend(); ++it) {
  100. // Break when outside the overlap time window.
  101. if (timingSvc()->beforeOverlap((*it)->time())) break;
  102. // If unused, remove the cluster from the container.
  103. if (!(*it)->associated()) clusters->remove(*it);
  104. }
  105. }