TotemSampicConstant

TotemSampicFrame

Macros

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
/****************************************************************************
*
* This is a part of the TOTEM offline software.
* Authors:
*   Nicola Minafra
*
****************************************************************************/

#ifndef EventFilter_CTPPSRawToDigi_TotemSampicFrame
#define EventFilter_CTPPSRawToDigi_TotemSampicFrame

#include <vector>
#include <cstddef>
#include <cstdint>
#include <iomanip>
#include <bitset>
#include <iostream>

#include "EventFilter/CTPPSRawToDigi/interface/VFATFrame.h"
#include "FWCore/MessageLogger/interface/MessageLogger.h"

enum TotemSampicConstant {
  hwId_Position = 0,  // hwId_Size = 1,
  controlBits0_Position = 1,
  controlBits1_Position = 2,
  controlBits2_Position = 3,
  controlBits3_Position = 4,
  controlBits4_Position = 5,
  controlBits5_Position = 6,
  fpgaTime_Position = 7,     // fpgaTime_Size = 5,
  timestampA_Position = 12,  // timestampA_Size = 2,
  fpgaTime_Size = timestampA_Position - fpgaTime_Position,
  timestampB_Position = 14,  // timestampB_Size = 2,
  timestampA_Size = timestampB_Position - timestampA_Position,
  cellInfo_Position = 16,  // cellInfo_Size = 2,
  timestampB_Size = cellInfo_Position - timestampB_Position,
  planeChannelId_Position = 18,  // planeChannelId_Size = 1,
  cellInfo_Size = planeChannelId_Position - cellInfo_Position,
  reserved_Position = 19,  // reserved_Size = 5,

  boardId_Position = 0,       // boardId_Size = 1,
  l1ATimestamp_Position = 1,  // l1ATimestamp_Size = 5,
  bunchNumber_Position = 6,   // bunchNumber_Size = 2,
  l1ATimestamp_Size = bunchNumber_Position - l1ATimestamp_Position,
  orbitNumber_Position = 8,  // orbitNumber_Size = 4,
  bunchNumber_Size = orbitNumber_Position - bunchNumber_Position,
  eventNumber_Position = 12,  // eventNumber_Size = 4,
  orbitNumber_Size = eventNumber_Position - orbitNumber_Position,
  channelMap_Position = 16,  // channelMap_Size = 2,
  eventNumber_Size = channelMap_Position - eventNumber_Position,
  l1ALatency_Position = 18,  // l1ALatency_Size = 2,
  channelMap_Size = l1ALatency_Position - channelMap_Position,
  numberOfSamples_Position = 20,  // numberOfSamples_Size = 1,
  l1ALatency_Size = numberOfSamples_Position - l1ALatency_Position,
  offsetOfSamples_Position = 21,  // offsetOfSamples_Size = 1,
  fwVersion_Position = 22,        // fwVersion_Size = 1,
  pllInfo_Position = 23,          // pllInfo_Size = 1,

  numberOfSamples = 24,
  controlBits3 = 0x69,
  cellInfo_Mask = 0x3F,

};

template <typename T>
T grayToBinary(const T& gcode_data) {
  // assign b[0] = g[0]
  T binary = gcode_data & (0x0001 << (8 * sizeof(T) - 1));  // MSB is the same

  // assign b[i] = g[i] xor b[i-1]
  for (unsigned short int i = 1; i < 8 * sizeof(T); ++i)
    binary |= (gcode_data ^ (binary >> 1)) & (0x0001 << (8 * sizeof(T) - i - 1));

  return binary;
}

/**
 * This class is intended to handle the timing infromation of SAMPIC in the TOTEM implementation
**/
class TotemSampicFrame {
public:
  TotemSampicFrame(const uint8_t* chInfoPtr, const uint8_t* chDataPtr, const uint8_t* eventInfoPtr)
      : totemSampicInfoPtr_(chInfoPtr),
        totemSampicDataPtr_(chDataPtr),
        totemSampicEventInfoPtr_(eventInfoPtr),
        status_(0) {
    if (chInfoPtr != nullptr && chDataPtr != nullptr && eventInfoPtr != nullptr &&
        totemSampicInfoPtr_[TotemSampicConstant::controlBits3_Position] == TotemSampicConstant::controlBits3)
      status_ = 1;
  }
  ~TotemSampicFrame() {}

  /// Prints the frame.
  /// If binary is true, binary format is used.
  void printRaw(bool binary = false) const {
    edm::LogInfo("TotemSampicFrame") << "Event Info: \n";
    printRawBuffer((uint16_t*)totemSampicEventInfoPtr_);

    edm::LogInfo("TotemSampicFrame") << "Channel Info: \n";
    printRawBuffer((uint16_t*)totemSampicInfoPtr_);

    edm::LogInfo("TotemSampicFrame") << "Channel Data: \n";
    printRawBuffer((uint16_t*)totemSampicDataPtr_);
  }

  void print() const {
    std::bitset<16> bitsChannelMap(getChannelMap());
    std::bitset<16> bitsPLLInfo(getPLLInfo());
    edm::LogInfo("TotemSampicFrame") << "\nEvent:"
                                     << "\nHardwareId (Event):\t" << std::hex << (unsigned int)getEventHardwareId()
                                     << "\nL1A Timestamp:\t" << std::dec << getL1ATimestamp() << "\nL1A Latency:\t"
                                     << std::dec << getL1ALatency() << "\nBunch Number:\t" << std::dec
                                     << getBunchNumber() << "\nOrbit Number:\t" << std::dec << getOrbitNumber()
                                     << "\nEvent Number:\t" << std::dec << getEventNumber() << "\nChannels fired:\t"
                                     << std::hex << bitsChannelMap.to_string() << "\nNumber of Samples:\t" << std::dec
                                     << getNumberOfSentSamples() << "\nOffset of Samples:\t" << std::dec
                                     << (int)getOffsetOfSamples() << "\nFW Version:\t" << std::hex
                                     << (int)getFWVersion() << "\nChannel:\nHardwareId:\t" << std::hex
                                     << (unsigned int)getHardwareId() << "\nFPGATimestamp:\t" << std::dec
                                     << getFPGATimestamp() << "\nTimestampA:\t" << std::dec << getTimestampA()
                                     << "\nTimestampB:\t" << std::dec << getTimestampB() << "\nCellInfo:\t" << std::dec
                                     << getCellInfo() << "\nPlane:\t" << std::dec << getDetPlane() << "\nChannel:\t"
                                     << std::dec << getDetChannel() << "\nPLL Info:\t" << bitsPLLInfo.to_string()
                                     << "\n\n";
  }

  // All getters
  inline uint8_t getHardwareId() const {
    uint8_t tmp = 0;
    if (status_)
      tmp = totemSampicInfoPtr_[TotemSampicConstant::hwId_Position];
    return tmp;
  }

  template <class T>
  T extractDataFromBytesLSB(const uint8_t* array, int start, int size) const {
    T result = 0;
    for (int i = start + size - 1; i >= start; i--)
      result = (result << 8) | array[i];
    return result;
  }

  inline uint64_t getFPGATimestamp() const {
    uint64_t tmp = 0;
    if (status_)
      tmp =
          extractDataFromBytesLSB<uint64_t>(totemSampicInfoPtr_, TotemSampicConstant::fpgaTime_Position, fpgaTime_Size);
    return tmp;
  }

  inline uint16_t getTimestampA() const {
    uint16_t tmp = 0;
    if (status_)
      tmp = extractDataFromBytesLSB<uint16_t>(
          totemSampicInfoPtr_, TotemSampicConstant::timestampA_Position, timestampA_Size);
    tmp = 0xFFF - tmp;
    return grayToBinary<uint16_t>(tmp);
  }

  inline uint16_t getTimestampB() const {
    uint16_t tmp = 0;
    if (status_)
      tmp = extractDataFromBytesLSB<uint16_t>(
          totemSampicInfoPtr_, TotemSampicConstant::timestampB_Position, timestampB_Size);
    return grayToBinary<uint16_t>(tmp);
  }

  inline uint16_t getCellInfo() const {
    uint16_t tmp = 0;
    if (status_)
      tmp =
          extractDataFromBytesLSB<uint16_t>(totemSampicInfoPtr_, TotemSampicConstant::cellInfo_Position, cellInfo_Size);
    return tmp & TotemSampicConstant::cellInfo_Mask;
  }

  inline int getDetPlane() const {
    int tmp = 0;
    if (status_)
      tmp = (totemSampicInfoPtr_[planeChannelId_Position] & 0xF0) >> 4;
    return tmp;
  }

  inline int getDetChannel() const {
    int tmp = 0;
    if (status_)
      tmp = totemSampicInfoPtr_[planeChannelId_Position] & 0x0F;
    return tmp;
  }

  const std::vector<uint8_t> getSamples() const {
    std::vector<uint8_t> samples;
    if (status_) {
      samples.assign(totemSampicDataPtr_, totemSampicDataPtr_ + TotemSampicConstant::numberOfSamples);
      for (auto it = samples.begin(); it != samples.end(); ++it)
        *it = grayToBinary<uint8_t>(*it);
    }
    return samples;
  }

  inline unsigned int getNumberOfSamples() const { return status_ * TotemSampicConstant::numberOfSamples; }

  // Event Info
  inline uint8_t getEventHardwareId() const {
    uint8_t tmp = 0;
    if (status_)
      tmp = totemSampicEventInfoPtr_[TotemSampicConstant::boardId_Position];
    return tmp;
  }

  inline uint64_t getL1ATimestamp() const {
    uint64_t tmp = 0;
    if (status_)
      tmp = extractDataFromBytesLSB<uint64_t>(
          totemSampicEventInfoPtr_, TotemSampicConstant::l1ATimestamp_Position, l1ATimestamp_Size);
    return tmp;
  }

  inline uint16_t getBunchNumber() const {
    uint16_t tmp = 0;
    if (status_)
      tmp = extractDataFromBytesLSB<uint16_t>(
          totemSampicEventInfoPtr_, TotemSampicConstant::bunchNumber_Position, bunchNumber_Size);
    return tmp;
  }

  inline uint32_t getOrbitNumber() const {
    uint32_t tmp = 0;
    if (status_)
      tmp = extractDataFromBytesLSB<uint32_t>(
          totemSampicEventInfoPtr_, TotemSampicConstant::orbitNumber_Position, orbitNumber_Size);
    return tmp;
  }

  inline uint32_t getEventNumber() const {
    uint32_t tmp = 0;
    if (status_)
      tmp = extractDataFromBytesLSB<uint32_t>(
          totemSampicEventInfoPtr_, TotemSampicConstant::eventNumber_Position, eventNumber_Size);
    return tmp;
  }

  inline uint16_t getChannelMap() const {
    uint16_t tmp = 0;
    if (status_)
      tmp = extractDataFromBytesLSB<uint16_t>(
          totemSampicEventInfoPtr_, TotemSampicConstant::channelMap_Position, channelMap_Size);
    return tmp;
  }

  inline uint16_t getL1ALatency() const {
    uint16_t tmp = 0;
    if (status_)
      tmp = extractDataFromBytesLSB<uint16_t>(
          totemSampicEventInfoPtr_, TotemSampicConstant::l1ALatency_Position, l1ALatency_Size);
    return tmp;
  }

  inline uint8_t getNumberOfSentSamples() const {
    uint8_t tmp = 0;
    if (status_)
      tmp = totemSampicEventInfoPtr_[TotemSampicConstant::numberOfSamples_Position];
    return tmp;
  }

  inline uint8_t getOffsetOfSamples() const {
    uint8_t tmp = 0;
    if (status_)
      tmp = totemSampicEventInfoPtr_[TotemSampicConstant::offsetOfSamples_Position];
    return tmp;
  }

  inline uint8_t getPLLInfo() const {
    uint8_t tmp = 0;
    if (status_)
      tmp = totemSampicEventInfoPtr_[pllInfo_Position];
    return tmp;
  }

  inline uint8_t getFWVersion() const {
    uint8_t tmp = 0;
    if (status_)
      tmp = totemSampicEventInfoPtr_[fwVersion_Position];
    return tmp;
  }

  inline bool valid() const { return status_ != 0; }

protected:
  const uint8_t* totemSampicInfoPtr_;
  const uint8_t* totemSampicDataPtr_;
  const uint8_t* totemSampicEventInfoPtr_;

  int status_;

  inline void printRawBuffer(const uint16_t* buffer, const bool binary = false, const unsigned int size = 12) const {
    for (unsigned int i = 0; i < size; i++) {
      if (binary) {
        std::bitset<16> bits(*(buffer++));
        edm::LogInfo("TotemSampicFrame") << bits.to_string() << "\n";
      } else
        edm::LogInfo("TotemSampicFrame") << std::setfill('0') << std::setw(4) << std::hex << *(buffer++) << "\n";
    }
  }
};

#endif