Newer
Older
TB_Chris / TbIO / src / .svn / text-base / TbEventBuilder.h.svn-base
  1. #ifndef TB_EVENTBUILDER_H
  2. #define TB_EVENTBUILDER_H 1
  3.  
  4. // Boost
  5.  
  6. // Tb/TbEvent
  7. #include "Event/TbHit.h"
  8. #include "Event/TbTrigger.h"
  9.  
  10. // Tb/TbKernel
  11. #include "TbKernel/TbAlgorithm.h"
  12. #include "TbKernel/TbModule.h"
  13.  
  14. // Local
  15. #include "TbRawStream.h"
  16. #include "TbRawFile.h"
  17. #include "TbHeaderDecoder.h"
  18. #include <ctime>
  19. #include <chrono>
  20. /** @class TbEventBuilder TbEventBuilder.h
  21. *
  22. * Algorithm to populate TES with time ordered gaudi events with data read in
  23. * in the SPIDR data format
  24. *
  25. * @author Tim Evans (timothy.david.evans@cern.ch)
  26. * @date 2014-04-01
  27. */
  28.  
  29. class TbEventBuilder : public TbAlgorithm {
  30. public:
  31. /// Standard constructor
  32. TbEventBuilder(const std::string& name, ISvcLocator* pSvcLocator);
  33. /// Destructor
  34. virtual ~TbEventBuilder();
  35.  
  36. virtual StatusCode initialize(); ///< Algorithm initialization
  37. virtual StatusCode execute(); ///< Algorithm execution
  38. virtual StatusCode finalize(); ///< Algorithm termination
  39. private:
  40. /// Max. time difference for resynchronisation
  41. static const int64_t m_maxTimeDifference;
  42. std::clock_t c_start;
  43. std::chrono::_V2::system_clock::time_point t_start;
  44. /// Input data files
  45. std::vector<std::string> m_input;
  46. /// TES location of output hits
  47. std::string m_hitLocation;
  48. /// TES location of output triggers
  49. std::string m_triggerLocation;
  50.  
  51.  
  52. /// The length of the time window used by the event definition
  53. uint64_t m_tick;
  54. /// The length of time looking in the the next event
  55. uint64_t m_cachelength;
  56. /// The length of time by which event definitions 'overlap',
  57. /// to be sorted out by tracking
  58. uint64_t m_overlapTime;
  59.  
  60. /// Time at which to start reading (in trigger FToA times)
  61. uint64_t m_startTime;
  62. /// Time at which to stop reading (specified in ms, converted later to FToA)
  63. uint64_t m_endTime;
  64.  
  65. /// Min. number of non-empty planes required to make an event
  66. unsigned int m_nMinPlanesWithHits;
  67.  
  68. /// Frequency to print event count.
  69. unsigned int m_printFreq;
  70. /// The total header size. If set to 0, is read from header.
  71. unsigned int m_headerSize;
  72. /// Flag to dump header or not.
  73. bool m_printHeader;
  74. /// Flag to activate detailed print-out and histograms.
  75. bool m_monitoring;
  76. /// Flag to activate pixel configuration histograms
  77. bool m_equal;
  78. /// Flag to ignore the global clock packets
  79. bool m_ignoreGlobalClock;
  80. /// The end of the time window used by the event definition
  81. uint64_t m_clock;
  82.  
  83. /// Number of processed events
  84. unsigned int m_nEvents = 0;
  85. /// Number of skipped noise events
  86. unsigned int m_nNoiseEvents = 0;
  87. /// Number of created hits
  88. uint64_t m_nData = 0;
  89. /// Number of read hit packets
  90. uint64_t m_nHitsRead = 0;
  91. /// Number of read trigger packets
  92. unsigned int m_nTriggersRead = 0;
  93. /// Number of unknown packets
  94. unsigned int m_unknownPackets = 0;
  95. /// Number of out-of-time packets
  96. unsigned int m_nLostPackets = 0;
  97. unsigned int m_nLostTimers = 0;
  98. /// Maximum number of lost packets / clock packets
  99. unsigned int m_maxLostPackets;
  100. unsigned int m_maxLostTimers;
  101. /// Another eof checksum
  102. uint64_t m_nDataInFiles = 0;
  103. /// Number of packets per event
  104. unsigned int m_nPackets = 0;
  105.  
  106. /// Forces the cache to update every cycle, useful
  107. /// for runs where a bad packet has confused the caching
  108. bool m_forceCaching;
  109. /// TbRawStreams for each plane
  110. std::vector<TbRawStream*> m_streams;
  111. /// Triggers in each plane
  112. std::vector<LHCb::TbTriggers*> m_triggers;
  113. /// Hits in each plane
  114. std::vector<LHCb::TbHits*> m_hits;
  115.  
  116. /// Header decoder tool
  117. TbHeaderDecoder* m_headerDecoder = nullptr;
  118.  
  119. bool fill(TbRawStream* f, LHCb::TbHits* hits, LHCb::TbTriggers* triggers, bool& eot);
  120.  
  121. /// Templated cache dump for triggers and hits (extendible to other
  122. /// data packets with a time field and a htime field
  123. template <typename T>
  124. bool dumpCache(TbRawStream* stream,
  125. KeyedContainer<T, Containers::HashMap>* container);
  126. /// Extend the timestamp of a packet to include the global time
  127. template <typename T>
  128. void extendTimeStamp(T* packet, uint64_t global_time);
  129.  
  130. /// Write a data packet to either a cache or the TES, depending on
  131. /// whether it is contained within the current event definition
  132. template <typename T>
  133. void writePacket(T* packet, TbRawStream* stream,
  134. KeyedContainer<T, Containers::HashMap>* container) {
  135. const uint64_t time = packet->time();
  136. if (time < m_clock + m_overlapTime && time >= m_clock - m_tick) {
  137. // Timestamp is inside the current event. Add the packet to the TES.
  138. packet->setHtime(timingSvc()->globalToLocal(time));
  139. container->insert(packet);
  140. } else {
  141. stream->insert(packet);
  142. }
  143. }
  144. LHCb::TbHit* decodeTPX3Hit(const uint64_t& packet,
  145. const unsigned int& pixelAddress,
  146. const unsigned int& device,
  147. const unsigned int& fCol);
  148. void syncTPX3(const uint64_t& thisPacketTime, TbRawStream* f);
  149. bool attemptResync(TbRawStream* f, const uint64_t& packet);
  150. /// Functor for sorting files by split index
  151. class lessBySplitIndex {
  152. public:
  153. bool operator()(const TbRawFile* a, const TbRawFile* b) const {
  154. return a->splitIndex() < b->splitIndex();
  155. }
  156. };
  157.  
  158. };
  159.  
  160. #endif