Newer
Older
TB_Chris / TbIO / src / .svn / text-base / TbHeaderDecoder.cpp.svn-base
  1. // Boost
  2. #include "boost/format.hpp"
  3.  
  4. // Local
  5. #include "TbHeaderDecoder.h"
  6.  
  7. DECLARE_TOOL_FACTORY(TbHeaderDecoder)
  8.  
  9. //=============================================================================
  10. // Standard constructor, initializes variables
  11. //=============================================================================
  12. TbHeaderDecoder::TbHeaderDecoder(const std::string& type,
  13. const std::string& name,
  14. const IInterface* parent)
  15. : GaudiTool(type, name, parent),
  16. m_print(true), m_geomSvc(nullptr), m_pixelSvc(nullptr) {
  17.  
  18. declareInterface<TbHeaderDecoder>(this);
  19. }
  20.  
  21. //=============================================================================
  22. // Destructor
  23. //=============================================================================
  24. TbHeaderDecoder::~TbHeaderDecoder() {}
  25.  
  26. //=============================================================================
  27. // Initialisation
  28. //=============================================================================
  29. StatusCode TbHeaderDecoder::initialize() {
  30.  
  31. StatusCode sc = GaudiTool::initialize();
  32. if (sc.isFailure()) return sc;
  33.  
  34. return StatusCode::SUCCESS;
  35. }
  36.  
  37. //=============================================================================
  38. // Read the first fields of the Spidr header from a given file.
  39. //=============================================================================
  40. bool TbHeaderDecoder::readCommon(const char* data, unsigned int& size,
  41. unsigned int& fmt) {
  42.  
  43. struct CommonHeader {
  44. uint32_t headerId;
  45. uint32_t headerSizeTotal;
  46. uint32_t headerSize;
  47. uint32_t format;
  48. };
  49. CommonHeader* hdr = (CommonHeader*)data;
  50. size = hdr->headerSizeTotal;
  51. if (size > 66304) size = 66304;
  52. fmt = hdr->format;
  53. return true;
  54. }
  55.  
  56. //=============================================================================
  57. // Read the Spidr header from a given file.
  58. //=============================================================================
  59. bool TbHeaderDecoder::read(const char* data, const unsigned int& fmt,
  60. std::string& deviceId) {
  61.  
  62. if (fmt == 0x30 || fmt == 0x1) {
  63. return readV1(data, deviceId);
  64. }
  65. error() << format("Unknown format (0x%x)", fmt) << endmsg;
  66. return false;
  67. }
  68.  
  69.  
  70. //=============================================================================
  71. // Read version 1 of the Spidr header.
  72. //=============================================================================
  73. bool TbHeaderDecoder::readV1(const char* data, std::string& deviceId) {
  74.  
  75. struct Tpx3Header {
  76. uint32_t headerId;
  77. uint32_t headerSizeTotal;
  78. uint32_t headerSize;
  79. uint32_t format;
  80. uint32_t deviceId;
  81. uint32_t genConfig;
  82. uint32_t outblockConfig;
  83. uint32_t pllConfig;
  84. uint32_t testPulseConfig;
  85. uint32_t slvsConfig;
  86. uint32_t pwrPulseConfig;
  87. uint32_t dac[32];
  88. uint8_t ctpr[256 / 8];
  89. uint32_t unused[64 - 11 - 32 - (256 / 8) / 4];
  90. };
  91.  
  92. struct SpidrTpx3Header {
  93. uint32_t headerId;
  94. uint32_t headerSizeTotal;
  95. uint32_t headerSize;
  96. uint32_t format;
  97. uint32_t spidrId;
  98. uint32_t libVersion;
  99. uint32_t softwVersion;
  100. uint32_t firmwVersion;
  101. uint32_t ipAddress;
  102. uint32_t ipPort;
  103. uint32_t yyyyMmDd;
  104. uint32_t hhMmSsMs;
  105. uint32_t runNr;
  106. uint32_t seqNr;
  107. uint32_t spidrConfig; // Trigger mode, decoder on/off
  108. uint32_t spidrFilter;
  109. uint32_t spidrBiasVoltage;
  110. // Spare
  111. uint32_t unused[128 - 17 - 128 / 4];
  112. // Description string
  113. char descr[128];
  114. // Device header
  115. Tpx3Header devHeader;
  116. };
  117.  
  118. SpidrTpx3Header* hdr = (SpidrTpx3Header*)data;
  119. Tpx3Header devHdr = hdr->devHeader;
  120. // Get the chip id.
  121. const int devid0 = (hdr->devHeader.deviceId >> 8) & 0xFFF;
  122. const int devid1 = (hdr->devHeader.deviceId >> 4) & 0xF;
  123. const int devid2 = (hdr->devHeader.deviceId >> 0) & 0xF;
  124. const char devidchar = (char)(devid2 + 64);
  125. deviceId = str(boost::format("W%04d_%d%02d") % devid0 % devidchar % devid1);
  126.  
  127. // Transfer the header information to the pixel configuration service.
  128. const unsigned int deviceIndex = geomSvc()->deviceIndex(deviceId);
  129. if (deviceIndex == 999) {
  130. error() << deviceId << " is not listed in the alignment file." << endmsg;
  131. return false;
  132. }
  133. pixelSvc()->setPhase(deviceIndex, devHdr.pllConfig);
  134. if (devHdr.headerSizeTotal - devHdr.headerSize == 65536) {
  135. const char* trimDac = data + (hdr->headerSize + devHdr.headerSize);
  136. pixelSvc()->setTrim(deviceIndex, trimDac);
  137. }
  138. if (!m_print) return true;
  139.  
  140. boost::format fmts(" %|-30.30s|%|32t| %s ");
  141. boost::format fmtx(" %|-30.30s|%|32t| 0x%x ");
  142. boost::format fmt8x(" %|-30.30s|%|32t| 0x%08x ");
  143. boost::format fmtd(" %|-30.30s|%|32t| %d ");
  144. info() << std::string(70, '-') << endmsg;
  145. info() << " File header " << endmsg;
  146. info() << std::string(70, '-') << endmsg;
  147. info() << fmtx % "ID" % hdr->headerId << endmsg;
  148. info() << fmtd % "Total size" % hdr->headerSizeTotal << endmsg;
  149. info() << fmtd % "Size" % hdr->headerSize << endmsg;
  150. info() << fmt8x % "Format" % hdr->format << endmsg;
  151. info() << std::string(70, '-') << endmsg;
  152. info() << fmtx % "SPIDR ID" % hdr->spidrId << endmsg;
  153. info() << fmtx % "Library version" % hdr->libVersion << endmsg;
  154. info() << fmtx % "Software version" % hdr->softwVersion << endmsg;
  155. info() << fmtx % "Firmware version" % hdr->firmwVersion << endmsg;
  156. info() << std::string(70, '-') << endmsg;
  157. const int ip0 = (hdr->ipAddress >> 24) & 0xFF;
  158. const int ip1 = (hdr->ipAddress >> 16) & 0xFF;
  159. const int ip2 = (hdr->ipAddress >> 8) & 0xFF;
  160. const int ip3 = (hdr->ipAddress >> 0) & 0xFF;
  161. const std::string ip = std::to_string(ip0) + "." + std::to_string(ip1) + "." +
  162. std::to_string(ip2) + "." + std::to_string(ip3);
  163. info() << fmts % "IP address" % ip << endmsg;
  164. info() << fmtd % "IP port" % hdr->ipPort << endmsg;
  165. info() << std::string(70, '-') << endmsg;
  166. const int date0 = (hdr->yyyyMmDd >> 16) & 0xFFFF;
  167. const int date1 = (hdr->yyyyMmDd >> 8) & 0xFF;
  168. const int date2 = (hdr->yyyyMmDd >> 0) & 0xFF;
  169. const int time0 = (hdr->hhMmSsMs >> 24) & 0xFF;
  170. const int time1 = (hdr->hhMmSsMs >> 16) & 0xFF;
  171. const int time2 = (hdr->hhMmSsMs >> 8) & 0xFF;
  172. const int time3 = (hdr->hhMmSsMs >> 0) & 0xFF;
  173. info() << boost::format(" %|-30.30s|%|32t| %04x-%02x-%02x") % "Date" % date0 %
  174. date1 % date2 << endmsg;
  175. info() << boost::format(" %|-30.30s|%|32t| %02x:%02x:%02x.%02x") % "Time" %
  176. time0 % time1 % time2 % time3 << endmsg;
  177. info() << std::string(70, '-') << endmsg;
  178. info() << fmtd % "Run number" % hdr->runNr << endmsg;
  179. info() << fmtd % "Sequence number" % hdr->seqNr << endmsg;
  180. info() << fmts % "Run info" % hdr->descr << endmsg;
  181. info() << std::string(70, '-') << endmsg;
  182. info() << fmt8x % "Spidr configuration" % hdr->spidrConfig << endmsg;
  183. info() << fmt8x % "Data filter" % hdr->spidrFilter << endmsg;
  184. info() << std::string(70, '-') << endmsg;
  185. info() << " Device header" << endmsg;
  186. info() << std::string(70, '-') << endmsg;
  187. info() << fmtx % "Type ID" % devHdr.headerId << endmsg;
  188. info() << std::string(70, '-') << endmsg;
  189. info() << fmtx % "Device ID" % devHdr.deviceId << endmsg;
  190. info() << fmts % "" % deviceId << endmsg;
  191. info() << std::string(70, '-') << endmsg;
  192. info() << fmt8x % "General config" % devHdr.genConfig << endmsg;
  193. info() << fmt8x % "Output block config" % devHdr.outblockConfig << endmsg;
  194. info() << fmt8x % "PLL config" % devHdr.pllConfig << endmsg;
  195. info() << fmt8x % "Testpulse config" % devHdr.testPulseConfig << endmsg;
  196. info() << fmt8x % "SLVS config" % devHdr.slvsConfig << endmsg;
  197. info() << fmt8x % "Power pulsing config" % devHdr.pwrPulseConfig << endmsg;
  198. info() << std::string(70, '-') << endmsg;
  199. boost::format fmt(" %|-30.30s|%|32t| %03d %03d %03d %03d");
  200. std::string title = "DAC values";
  201. for (unsigned int i = 0; i < 5; ++i) {
  202. const unsigned int offset = 4 * i;
  203. info() << fmt % title % devHdr.dac[offset] % devHdr.dac[offset + 1] %
  204. devHdr.dac[offset + 2] % devHdr.dac[offset + 3] << endmsg;
  205. title = "";
  206. }
  207. info() << std::string(70, '-') << endmsg;
  208. return true;
  209. }