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
#include "EventFilter/CSCRawToDigi/interface/CSCDDUDataItr.h"
#include "EventFilter/CSCRawToDigi/interface/CSCDDUEventData.h"
#include "FWCore/MessageLogger/interface/MessageLogger.h"

// theCurrentCSC starts at -1, since user is expected to next() before he dereferences
// for the first time
CSCDDUDataItr::CSCDDUDataItr() : theDDUData(nullptr), theCurrentCSC(-1), theNumberOfCSCs(0), theDataIsOwnedByMe(true) {}

CSCDDUDataItr::CSCDDUDataItr(const char *buf)
    : theDDUData(nullptr), theCurrentCSC(-1), theNumberOfCSCs(0), theDataIsOwnedByMe(true) {
  // check if it's OK first
  const CSCDDUHeader *dduHeader = reinterpret_cast<const CSCDDUHeader *>(buf);
  if (dduHeader->check()) {
    theDDUData = new CSCDDUEventData((const uint16_t *)buf);
    theNumberOfCSCs = theDDUData->cscData().size();
  } else {
    LogTrace("CSCDDUDataItr|CSCRawToDigi") << "Failed DDU header check.";
  }
}

CSCDDUDataItr::CSCDDUDataItr(const CSCDDUEventData *dduData)
    : theDDUData(dduData),
      theCurrentCSC(-1),
      theNumberOfCSCs(theDDUData->cscData().size()),
      theDataIsOwnedByMe(false) {}

CSCDDUDataItr::~CSCDDUDataItr() {
  if (theDataIsOwnedByMe)
    delete theDDUData;
}

CSCDDUDataItr::CSCDDUDataItr(const CSCDDUDataItr &i)
    : theCurrentCSC(i.theCurrentCSC), theNumberOfCSCs(i.theNumberOfCSCs), theDataIsOwnedByMe(i.theDataIsOwnedByMe) {
  if (theDataIsOwnedByMe) {
    if (i.theDDUData != nullptr) {
      theDDUData = new CSCDDUEventData(*(i.theDDUData));
    }
  } else {
    theDDUData = i.theDDUData;
  }
}

void CSCDDUDataItr::operator=(const CSCDDUDataItr &i) {
  if (theDataIsOwnedByMe && theDDUData != i.theDDUData) {
    delete theDDUData;
    if (i.theDDUData != nullptr) {
      theDDUData = new CSCDDUEventData(*(i.theDDUData));
    }
  } else {
    theDDUData = i.theDDUData;
  }

  theDDUData = i.theDDUData;
  theCurrentCSC = i.theCurrentCSC;
  theNumberOfCSCs = i.theNumberOfCSCs;
  theDataIsOwnedByMe = i.theDataIsOwnedByMe;
}

bool CSCDDUDataItr::next() { return (++theCurrentCSC < theNumberOfCSCs); }

const CSCEventData &CSCDDUDataItr::operator*() {
  assert(theCurrentCSC >= 0 && theCurrentCSC < theNumberOfCSCs);
  return theDDUData->cscData()[theCurrentCSC];
}