Newer
Older
TB_Chris / TbIO / src / .svn / text-base / TbRawStream.h.svn-base
  1. #pragma once
  2.  
  3. #include "TbRawFile.h"
  4. #include "Event/TbHit.h"
  5. #include "Event/TbTrigger.h"
  6. #include "TbKernel/TbFunctors.h"
  7. #include "GaudiAlg/GaudiTool.h"
  8. #include "TbKernel/TbConstants.h"
  9.  
  10. static const InterfaceID IID_TbRawStream("TbRawStream", 1, 0);
  11.  
  12. class TbRawStream : public GaudiTool {
  13.  
  14. public:
  15. /// Constructor
  16. TbRawStream(const std::string& type, const std::string& name,
  17. const IInterface* parent);
  18.  
  19. static const InterfaceID& interfaceID() { return IID_TbRawStream; }
  20.  
  21. uint64_t timer() const { return m_timer; }
  22. unsigned int lsb() const { return m_lsb; }
  23. // this is for explicitly setting the clock, if device sync fails
  24. void setGlobalClock(const uint64_t timer) { m_timer = timer; }
  25. unsigned int size() const { return m_size; }
  26. void setLSB(const unsigned int lsb) { m_lsb = lsb; }
  27. bool setMSB(uint64_t msb);
  28.  
  29. std::vector<LHCb::TbHit*>& hitCache() { return m_hitCache; }
  30. std::vector<LHCb::TbTrigger*>& trgCache() { return m_trgCache; }
  31. void addFile(TbRawFile* file) { m_files.push_back(file); }
  32. void close() {
  33. for (auto& raw_file : m_files) {
  34. if (raw_file->is_open()) raw_file->close();
  35. }
  36. }
  37.  
  38. uint64_t getNext() { return (*m_currentFile)->getNext(); }
  39. uint64_t getPrevious() { return (*m_currentFile)->getPrevious(); }
  40.  
  41. bool eos() {
  42. if (unlikely( m_currentFile == m_files.end() ) ) return true;
  43. if (unlikely((*m_currentFile)->eof() == true)) {
  44. (*m_currentFile)->close();
  45. m_currentFile++;
  46. if (m_currentFile != m_files.end()) (*m_currentFile)->initialise();
  47. }
  48. return unlikely(m_currentFile == m_files.end());
  49. }
  50. double hClock() const {
  51. return (double)m_timer / (double) Tb::second;
  52. }
  53. void prepare();
  54.  
  55. // these functions collectively control fast forwarding to some point in the
  56. // stream
  57. void fastForward(const uint64_t timeToSkipTo);
  58. void coarseFastForward(const double timeToSkipTo);
  59. void fineFastForward(const uint64_t timeToSkipTo);
  60. uint64_t getCurrentTime();
  61.  
  62. int addTimingPacket(const uint64_t data_packet);
  63. std::vector<TbRawFile*>& files() { return m_files; }
  64. unsigned int plane() const { return m_plane; }
  65. void setOffset(const int colOffset) {
  66. m_colOffset = colOffset;
  67. }
  68. void setDevice(const unsigned int device) { m_device = device; }
  69. void setPlane(const unsigned int plane) { m_plane = plane; }
  70. unsigned int col() const { return m_colOffset; }
  71. unsigned int device() const { return m_device; }
  72.  
  73. template <typename T>
  74. std::vector<T>* cache();
  75.  
  76. void insert(LHCb::TbTrigger* packet);
  77. void insert(LHCb::TbHit* packet);
  78. uint64_t m_tpx3Timer = 0;
  79. private:
  80. static const int64_t m_tenSeconds;
  81. static const int64_t m_maxTimeDifference;
  82.  
  83. std::vector<TbRawFile*>::iterator m_currentFile;
  84. std::vector<TbRawFile*> m_files;
  85. std::vector<LHCb::TbHit*> m_hitCache;
  86. std::vector<LHCb::TbTrigger*> m_trgCache;
  87. unsigned int m_plane;
  88. unsigned int m_device;
  89. uint64_t m_size = 0;
  90. /// Temporary lsb of the global timer
  91. unsigned int m_lsb = 0;
  92. uint64_t m_timer = 0;
  93. unsigned int m_colOffset;
  94. };