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 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
/****************************************************************************
*
* This is a part of TOTEM offline software.
* Authors:
*   Jan Kašpar (jan.kaspar@gmail.com)
*   Seyed Mohsen Etesami (setesami@cern.ch)
*   Laurent Forthomme
****************************************************************************/

#include "FWCore/MessageLogger/interface/MessageLogger.h"

#include "EventFilter/CTPPSRawToDigi/interface/RawToDigiConverter.h"
#include "EventFilter/CTPPSRawToDigi/interface/CounterChecker.h"
#include "EventFilter/CTPPSRawToDigi/interface/DiamondVFATFrame.h"
#include "EventFilter/CTPPSRawToDigi/interface/TotemSampicFrame.h"
#include "EventFilter/CTPPSRawToDigi/interface/TotemT2VFATFrame.h"

#include "DataFormats/CTPPSDetId/interface/TotemRPDetId.h"
#include "DataFormats/CTPPSDetId/interface/CTPPSDiamondDetId.h"
#include "DataFormats/CTPPSDetId/interface/TotemTimingDetId.h"
#include "DataFormats/CTPPSDetId/interface/TotemT2DetId.h"

using namespace std;
using namespace edm;

RawToDigiConverter::RawToDigiConverter(const edm::ParameterSet &conf)
    : verbosity(conf.getUntrackedParameter<unsigned int>("verbosity", 0)),
      printErrorSummary(conf.getUntrackedParameter<bool>("printErrorSummary")),
      printUnknownFrameSummary(conf.getUntrackedParameter<bool>("printUnknownFrameSummary")),

      testFootprint(conf.getParameter<unsigned int>("testFootprint")),
      testCRC(conf.getParameter<unsigned int>("testCRC")),
      testID(conf.getParameter<unsigned int>("testID")),
      testECMostFrequent(conf.getParameter<unsigned int>("testECMostFrequent")),
      testBCMostFrequent(conf.getParameter<unsigned int>("testBCMostFrequent")),

      EC_min(conf.getUntrackedParameter<unsigned int>("EC_min", 10)),
      BC_min(conf.getUntrackedParameter<unsigned int>("BC_min", 10)),

      EC_fraction(conf.getUntrackedParameter<double>("EC_fraction", 0.6)),
      BC_fraction(conf.getUntrackedParameter<double>("BC_fraction", 0.6)),

      olderTotemT2FileTest(conf.getParameter<bool>("useOlderT2TestFile")) {}

void RawToDigiConverter::runCommon(const VFATFrameCollection &input,
                                   const TotemDAQMapping &mapping,
                                   map<TotemFramePosition, RawToDigiConverter::Record> &records) {
  // EC and BC checks (wrt. the most frequent value), BC checks per subsystem
  CounterChecker ECChecker(CounterChecker::ECChecker, "EC", EC_min, EC_fraction, verbosity);
  CounterChecker BCChecker(CounterChecker::BCChecker, "BC", BC_min, BC_fraction, verbosity);

  // initialise structure merging vfat frame data with the mapping
  for (auto &p : mapping.VFATMapping) {
    TotemVFATStatus st;
    st.setMissing(true);
    records[p.first] = {&p.second, nullptr, st};
  }

  // event error message buffer
  stringstream ees;

  // associate data frames with records
  for (VFATFrameCollection::Iterator fr(&input); !fr.IsEnd(); fr.Next()) {
    // frame error message buffer
    stringstream fes;

    bool problemsPresent = false;
    bool stopProcessing = false;

    // skip data frames not listed in the DAQ mapping
    auto records_it = records.find(fr.Position());
    if (records_it == records.end()) {
      unknownSummary[fr.Position()]++;
      continue;
    }

    // update record
    Record &record = records_it->second;
    record.frame = fr.Data();
    record.status.setMissing(false);
    record.status.setNumberOfClustersSpecified(record.frame->isNumberOfClustersPresent());
    record.status.setNumberOfClusters(record.frame->getNumberOfClusters());

    // check for T2 payload bits
    int rawT2 = fr.Position().getRawPosition();
    bool isT2Frame = (rawT2 >> 18);

    // check footprint
    if (((!isT2Frame) && testFootprint != tfNoTest && !record.frame->checkFootprint()) ||
        (isT2Frame && testFootprint != tfNoTest && !record.frame->checkFootprintT2())) {
      problemsPresent = true;

      if (verbosity > 0)
        fes << "    invalid footprint" << endl;

      if (testFootprint == tfErr) {
        record.status.setFootprintError();
        stopProcessing = true;
      }
    }

    // check CRC
    if (((!isT2Frame) && (testCRC != tfNoTest && !record.frame->checkCRC())) ||
        (isT2Frame && testCRC != tfNoTest && !record.frame->checkCRCT2())) {
      problemsPresent = true;

      if (verbosity > 0)
        fes << "    CRC failure" << endl;

      if (testCRC == tfErr) {
        record.status.setCRCError();
        stopProcessing = true;
      }
    }

    // check the id mismatch
    if (testID != tfNoTest && record.frame->isIDPresent() &&
        (record.frame->getChipID() & 0xFFF) != (record.info->hwID & 0xFFF)) {
      if (verbosity > 0)
        fes << "    ID mismatch (data: 0x" << hex << record.frame->getChipID() << ", mapping: 0x" << record.info->hwID
            << dec << ", symbId: " << record.info->symbolicID.symbolicID << ")" << endl;

      if (testID == tfErr) {
        record.status.setIDMismatch();
        stopProcessing = true;
      }
    }

    // if there were errors, put the information to ees buffer
    if (verbosity > 0 && problemsPresent) {
      string message = (stopProcessing) ? "(and will be dropped)" : "(but will be used though)";
      if (verbosity > 2) {
        if (isT2Frame && verbosity > 3)
          record.frame->PrintT2();
        ees << "  Frame at " << fr.Position() << " seems corrupted " << message << ":" << endl;
        ees << fes.rdbuf();
      } else
        ees << "  Frame at " << fr.Position() << " seems corrupted " << message << "." << endl;
    }

    // if there were serious errors, do not process this frame
    if (stopProcessing)
      continue;

    // fill EC and BC values to the statistics
    if (fr.Data()->isECPresent())
      ECChecker.Fill(fr.Data()->getEC(), fr.Position());

    if (fr.Data()->isBCPresent())
      BCChecker.Fill(fr.Data()->getBC(), fr.Position());
  }

  // analyze EC and BC statistics
  if (testECMostFrequent != tfNoTest)
    ECChecker.Analyze(records, (testECMostFrequent == tfErr), ees);

  if (testBCMostFrequent != tfNoTest)
    BCChecker.Analyze(records, (testBCMostFrequent == tfErr), ees);

  // add error message for missing frames
  if (verbosity > 1) {
    for (const auto &p : records) {
      if (p.second.status.isMissing())
        ees << "Frame for VFAT " << p.first << " is not present in the data." << endl;
    }
  }

  // print error message
  if (verbosity > 0 && !ees.rdbuf()->str().empty()) {
    if (verbosity > 1)
      LogWarning("Totem") << "Error in RawToDigiConverter::runCommon > "
                          << "event contains the following problems:" << endl
                          << ees.rdbuf() << endl;
    else
      LogWarning("Totem") << "Error in RawToDigiConverter::runCommon > "
                          << "event contains problems." << endl;
  }

  // increase error counters
  if (printErrorSummary) {
    for (const auto &it : records) {
      if (!it.second.status.isOK()) {
        auto &m = errorSummary[it.first];
        m[it.second.status]++;
      }
    }
  }
}

void RawToDigiConverter::run(const VFATFrameCollection &input,
                             const TotemDAQMapping &mapping,
                             const TotemAnalysisMask &analysisMask,
                             DetSetVector<TotemRPDigi> &rpData,
                             DetSetVector<TotemVFATStatus> &finalStatus) {
  // structure merging vfat frame data with the mapping
  map<TotemFramePosition, Record> records;

  // common processing - frame validation
  runCommon(input, mapping, records);

  // second loop over data
  for (auto &p : records) {
    Record &record = p.second;

    // calculate ids
    TotemRPDetId chipId(record.info->symbolicID.symbolicID);
    uint8_t chipPosition = chipId.chip();
    TotemRPDetId detId = chipId.planeId();

    // update chipPosition in status
    record.status.setChipPosition(chipPosition);

    // produce digi only for good frames
    if (record.status.isOK()) {
      // find analysis mask (needs a default=no mask, if not in present the mapping)
      TotemVFATAnalysisMask anMa;
      anMa.fullMask = false;

      auto analysisIter = analysisMask.analysisMask.find(record.info->symbolicID);
      if (analysisIter != analysisMask.analysisMask.end()) {
        // if there is some information about masked channels - save it into conversionStatus
        anMa = analysisIter->second;
        if (anMa.fullMask)
          record.status.setFullyMaskedOut();
        else
          record.status.setPartiallyMaskedOut();
      }

      // create the digi
      unsigned short offset = chipPosition * 128;
      const vector<unsigned char> &activeChannels = record.frame->getActiveChannels();

      for (auto ch : activeChannels) {
        // skip masked channels
        if (!anMa.fullMask && anMa.maskedChannels.find(ch) == anMa.maskedChannels.end()) {
          DetSet<TotemRPDigi> &digiDetSet = rpData.find_or_insert(detId);
          digiDetSet.emplace_back(offset + ch);
        }
      }
    }

    // save status
    DetSet<TotemVFATStatus> &statusDetSet = finalStatus.find_or_insert(detId);
    statusDetSet.push_back(record.status);
  }
}

void RawToDigiConverter::run(const VFATFrameCollection &coll,
                             const TotemDAQMapping &mapping,
                             const TotemAnalysisMask &mask,
                             edm::DetSetVector<CTPPSDiamondDigi> &digi,
                             edm::DetSetVector<TotemVFATStatus> &status) {
  // structure merging vfat frame data with the mapping
  map<TotemFramePosition, Record> records;

  // common processing - frame validation
  runCommon(coll, mapping, records);

  // second loop over data
  for (auto &p : records) {
    Record &record = p.second;

    // calculate ids
    CTPPSDiamondDetId detId(record.info->symbolicID.symbolicID);

    if (record.status.isOK()) {
      // update Event Counter in status
      record.status.setEC(record.frame->getEC() & 0xFF);

      // create the digi
      DetSet<CTPPSDiamondDigi> &digiDetSet = digi.find_or_insert(detId);
      digiDetSet.emplace_back(pps::diamond::vfat::getLeadingEdgeTime(*record.frame),
                              pps::diamond::vfat::getTrailingEdgeTime(*record.frame),
                              pps::diamond::vfat::getThresholdVoltage(*record.frame),
                              pps::diamond::vfat::getMultihit(*record.frame),
                              pps::diamond::vfat::getHptdcErrorFlag(*record.frame));
    }

    // save status
    DetSet<TotemVFATStatus> &statusDetSet = status.find_or_insert(detId);
    statusDetSet.push_back(record.status);
  }
}

void RawToDigiConverter::run(const VFATFrameCollection &coll,
                             const TotemDAQMapping &mapping,
                             const TotemAnalysisMask &mask,
                             edm::DetSetVector<TotemTimingDigi> &digi,
                             edm::DetSetVector<TotemVFATStatus> &status) {
  // structure merging vfat frame data with the mapping
  map<TotemFramePosition, Record> records;

  // common processing - frame validation
  runCommon(coll, mapping, records);

  // second loop over data
  for (auto &p : records) {
    Record &record = p.second;
    if (!record.status.isOK())
      continue;

    const TotemFramePosition *framepos = &p.first;

    if (((framepos->getIdxInFiber() % 2) == 0) && (framepos->getIdxInFiber() < 14)) {
      //corresponding channel data are always in the neighbouring idx in fiber

      TotemFramePosition frameposdata(framepos->getSubSystemId(),
                                      framepos->getTOTFEDId(),
                                      framepos->getOptoRxId(),
                                      framepos->getGOHId(),
                                      (framepos->getIdxInFiber() + 1));
      TotemFramePosition frameposEvtInfo(
          framepos->getSubSystemId(), framepos->getTOTFEDId(), framepos->getOptoRxId(), framepos->getGOHId(), 0xe);

      auto channelwaveformPtr = records.find(frameposdata);
      auto eventInfoPtr = records.find(frameposEvtInfo);

      if (channelwaveformPtr != records.end() && eventInfoPtr != records.end()) {
        Record &channelwaveform = records[frameposdata];
        Record &eventInfo = records[frameposEvtInfo];

        // Extract all the waveform information from the raw data
        TotemSampicFrame totemSampicFrame((const uint8_t *)record.frame->getData(),
                                          (const uint8_t *)channelwaveform.frame->getData(),
                                          (const uint8_t *)eventInfo.frame->getData());

        if (totemSampicFrame.valid()) {
          // create the digi
          TotemTimingEventInfo eventInfoTmp(totemSampicFrame.getEventHardwareId(),
                                            totemSampicFrame.getL1ATimestamp(),
                                            totemSampicFrame.getBunchNumber(),
                                            totemSampicFrame.getOrbitNumber(),
                                            totemSampicFrame.getEventNumber(),
                                            totemSampicFrame.getChannelMap(),
                                            totemSampicFrame.getL1ALatency(),
                                            totemSampicFrame.getNumberOfSentSamples(),
                                            totemSampicFrame.getOffsetOfSamples(),
                                            totemSampicFrame.getPLLInfo());
          TotemTimingDigi digiTmp(totemSampicFrame.getHardwareId(),
                                  totemSampicFrame.getFPGATimestamp(),
                                  totemSampicFrame.getTimestampA(),
                                  totemSampicFrame.getTimestampB(),
                                  totemSampicFrame.getCellInfo(),
                                  totemSampicFrame.getSamples(),
                                  eventInfoTmp);
          // calculate ids
          TotemTimingDetId detId(record.info->symbolicID.symbolicID);
          const TotemDAQMapping::TotemTimingPlaneChannelPair SWpair =
              mapping.getTimingChannel(totemSampicFrame.getHardwareId());
          // for FW Version > 0 plane and channel are encoded in the dataframe
          if (totemSampicFrame.getFWVersion() == 0)  // Mapping not present in HW, read from SW for FW versions == 0
          {
            if (SWpair.plane == -1 || SWpair.channel == -1) {
              if (verbosity > 0)
                LogWarning("Totem") << "Error in RawToDigiConverter::TotemTiming > "
                                    << "HwId not recognized!  hwId: " << std::hex
                                    << (unsigned int)totemSampicFrame.getHardwareId() << endl;
            } else {
              detId.setPlane(SWpair.plane % 4);
              detId.setChannel(SWpair.channel);
            }
          } else  // Mapping read from HW, checked by SW
          {
            const int HWplane = totemSampicFrame.getDetPlane() % 16;
            const int HWchannel = totemSampicFrame.getDetChannel() % 16;

            if (SWpair.plane == -1 || SWpair.channel == -1) {
              if (verbosity > 0)
                LogWarning("Totem") << "Warning in RawToDigiConverter::TotemTiming > "
                                    << "HwId not recognized!  hwId: " << std::hex
                                    << (unsigned int)totemSampicFrame.getHardwareId()
                                    << "\tUsing plane and ch from HW without check!" << endl;
            } else {
              if (verbosity > 0 && (SWpair.plane != HWplane || SWpair.channel != HWchannel))
                LogWarning("Totem") << "Warning in RawToDigiConverter::TotemTiming > "
                                    << "Hw mapping different from SW mapping. hwId: " << std::hex
                                    << (unsigned int)totemSampicFrame.getHardwareId() << "HW: " << std::dec << HWplane
                                    << ":" << HWchannel << "\tSW " << SWpair.plane << ":" << SWpair.channel
                                    << "\tUsing plane and ch from HW!" << endl;
            }
            detId.setPlane(HWplane % 4);
            detId.setChannel(HWchannel);
          }

          DetSet<TotemTimingDigi> &digiDetSet = digi.find_or_insert(detId);
          digiDetSet.push_back(digiTmp);
        }
      }
    }
  }
}

void RawToDigiConverter::run(const VFATFrameCollection &coll,
                             const TotemDAQMapping &mapping,
                             const TotemAnalysisMask &mask,
                             edmNew::DetSetVector<TotemT2Digi> &digi,
                             edm::DetSetVector<TotemVFATStatus> &status) {
  // structure merging vfat frame data with the mapping
  map<TotemFramePosition, Record> records;

  // common processing - frame validation
  runCommon(coll, mapping, records);

  int allT2 = 0;
  int goodT2 = 0;
  int foundT2 = 0;
  const int T2shiftOld = (olderTotemT2FileTest ? 8 : 0);  //Run on TOTEM T2 test file (ver 2.1) or final T2 data ver 2.3

  // second loop over data
  for (auto &p : records) {
    Record &record = p.second;

    allT2++;
    // calculate ids
    TotemT2DetId detId(record.info->symbolicID.symbolicID);

    if (record.status.isOK()) {
      // update Event Counter in status
      record.status.setEC(record.frame->getEC() & 0xFF);
      goodT2++;
      if (verbosity > 2) {
        LogWarning("Totem") << "RawToDigiConverter: VFAT frame number " << allT2
                            << " is OK , mapping HW_ID (decimal) is: " << (record.info->hwID)
                            << ", T2DetId arm/plane/channel = " << (detId) << endl;
        LogWarning("Totem") << "HW_id_16b CH0 (dec), LE CH0, TE CH0, marker CH0, HW_id_16b CH1 (dec), LE CH1,"
                            << " TE CH1, marker CH1 = ";
        for (size_t y = 0; y < 2; y++) {
          LogWarning("Totem") << ((unsigned int)totem::nt2::vfat::newChannelId(*record.frame, y)) << "/"
                              << ((unsigned int)totem::nt2::vfat::leadingEdgeTime(*record.frame, y)) << "/"
                              << ((unsigned int)totem::nt2::vfat::trailingEdgeTime(*record.frame, y)) << "/"
                              << ((unsigned int)totem::nt2::vfat::channelMarker(*record.frame, y)) << "/";
        }
      }
      for (size_t frame_id = 0; frame_id < totem::nt2::vfat::num_channels_per_payload; ++frame_id) {
        if (const uint16_t hw_id = totem::nt2::vfat::newChannelId(*record.frame, frame_id) >> T2shiftOld;
            hw_id == record.info->hwID) {  // only unpack the payload associated to this hardware ID
          // create the digi
          edmNew::DetSetVector<TotemT2Digi>::FastFiller(digi, detId)
              .emplace_back(hw_id,
                            totem::nt2::vfat::channelMarker(*record.frame, frame_id),
                            totem::nt2::vfat::leadingEdgeTime(*record.frame, frame_id),
                            totem::nt2::vfat::trailingEdgeTime(*record.frame, frame_id),
                            totem::nt2::vfat::statusMarker(*record.frame));
          foundT2++;
        } else {
          if (verbosity > 2)
            LogWarning("Totem") << "HW_ID comparison fail (CH#/Channel HwID/Mapping HwID): " << ((int)frame_id) << "/"
                                << ((unsigned int)hw_id) << "/" << (record.info->hwID) << endl;
        }
      }
    } else {
      if (verbosity > 1)
        LogWarning("Totem") << "Bad T2 record, is missing/IDmismatch/footprintError"
                            << "/CRCerror/ECprogressBad/BCprogressBad: " << record.status.isMissing() << "/"
                            << record.status.isIDMismatch() << "/" << record.status.isFootprintError() << "/"
                            << record.status.isCRCError() << "/" << record.status.isECProgressError() << "/"
                            << record.status.isBCProgressError() << "/" << endl;
    }

    // save status
    DetSet<TotemVFATStatus> &statusDetSet = status.find_or_insert(detId);
    statusDetSet.push_back(record.status);
  }
  if (verbosity > 1)
    LogWarning("Totem") << "RawToDigiConverter:: VFAT frames per event, total/good/matched the xml mapping"
                        << " (T2Digi created): " << allT2 << "/" << goodT2 << "/" << foundT2 << endl;
}

void RawToDigiConverter::printSummaries() const {
  // print error summary
  if (printErrorSummary) {
    if (!errorSummary.empty()) {
      stringstream ees;
      for (const auto &vit : errorSummary) {
        ees << vit.first << endl;

        for (const auto &it : vit.second)
          ees << "    " << it.first << " : " << it.second << endl;
      }

      LogWarning("Totem") << "RawToDigiConverter: error summary (error signature : number of such events)\n"
                          << endl
                          << ees.rdbuf();
    } else {
      LogInfo("Totem") << "RawToDigiConverter: no errors to be reported.";
    }
  }

  // print summary of unknown frames (found in data but not in the mapping)
  if (printUnknownFrameSummary) {
    if (!unknownSummary.empty()) {
      stringstream ees;
      for (const auto &it : unknownSummary)
        ees << "  " << it.first << " : " << it.second << endl;

      LogWarning("Totem")
          << "RawToDigiConverter: frames found in data, but not in the mapping (frame position : number of events)\n"
          << endl
          << ees.rdbuf();
    } else {
      LogInfo("Totem") << "RawToDigiConverter: no unknown frames to be reported.";
    }
  }
}