TotemVFATRawToDigi

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
/****************************************************************************
*
* This is a part of TOTEM offline software.
* Authors:
*   Jan Kašpar (jan.kaspar@gmail.com)
*   Nicola Minafra
*   Laurent Forthomme
*
****************************************************************************/

#include "FWCore/Framework/interface/stream/EDProducer.h"
#include "FWCore/Framework/interface/Event.h"
#include "FWCore/Framework/interface/MakerMacros.h"
#include "FWCore/Utilities/interface/InputTag.h"
#include "FWCore/Framework/interface/ESHandle.h"
#include "FWCore/Framework/interface/EventSetup.h"
#include "FWCore/Utilities/interface/ESGetToken.h"

#include "DataFormats/FEDRawData/interface/FEDRawData.h"
#include "DataFormats/FEDRawData/interface/FEDRawDataCollection.h"
#include "DataFormats/FEDRawData/interface/FEDNumbering.h"

#include "DataFormats/CTPPSDigi/interface/TotemRPDigi.h"
#include "DataFormats/CTPPSDigi/interface/TotemVFATStatus.h"
#include "DataFormats/CTPPSDigi/interface/TotemFEDInfo.h"

#include "CondFormats/DataRecord/interface/TotemReadoutRcd.h"
#include "CondFormats/DataRecord/interface/TotemAnalysisMaskRcd.h"
#include "CondFormats/PPSObjects/interface/TotemDAQMapping.h"
#include "CondFormats/PPSObjects/interface/TotemAnalysisMask.h"

#include "EventFilter/CTPPSRawToDigi/interface/CTPPSRawToDigiErrorSummary.h"
#include "EventFilter/CTPPSRawToDigi/interface/SimpleVFATFrameCollection.h"
#include "EventFilter/CTPPSRawToDigi/interface/RawDataUnpacker.h"
#include "EventFilter/CTPPSRawToDigi/interface/RawToDigiConverter.h"

#include "DataFormats/CTPPSDigi/interface/TotemTimingDigi.h"
#include "DataFormats/TotemReco/interface/TotemT2Digi.h"

#include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
#include "FWCore/ParameterSet/interface/ParameterSetDescription.h"

#include <string>

class TotemVFATRawToDigi : public edm::stream::EDProducer<> {
public:
  explicit TotemVFATRawToDigi(const edm::ParameterSet &);
  ~TotemVFATRawToDigi() override;

  void produce(edm::Event &, const edm::EventSetup &) override;
  void endStream() override;
  static void fillDescriptions(edm::ConfigurationDescriptions &);

private:
  std::string subSystemName;

  enum { ssUndefined, ssTrackingStrip, ssTimingDiamond, ssTotemTiming, ssTotemT2 } subSystem;

  std::vector<unsigned int> fedIds;

  edm::EDGetTokenT<FEDRawDataCollection> fedDataToken;
  edm::ESGetToken<TotemDAQMapping, TotemReadoutRcd> totemMappingToken;
  edm::ESGetToken<TotemAnalysisMask, TotemAnalysisMaskRcd> analysisMaskToken;

  pps::RawDataUnpacker rawDataUnpacker;
  RawToDigiConverter rawToDigiConverter;
  CTPPSRawToDigiErrorSummary errSummary;

  template <typename DigiType>
  void run(edm::Event &, const edm::EventSetup &);
};

using namespace edm;
using namespace std;

TotemVFATRawToDigi::TotemVFATRawToDigi(const edm::ParameterSet &conf)
    : subSystemName(conf.getParameter<string>("subSystem")),
      subSystem(ssUndefined),
      fedIds(conf.getParameter<vector<unsigned int>>("fedIds")),
      rawDataUnpacker(conf.getParameterSet("RawUnpacking")),
      rawToDigiConverter(conf.getParameterSet("RawToDigi")),
      errSummary("TotemVFATRawToDigi", "[TotemVFATRawToDigi]", false) {
  fedDataToken = consumes<FEDRawDataCollection>(conf.getParameter<edm::InputTag>("rawDataTag"));

  // validate chosen subSystem
  if (subSystemName == "TrackingStrip")
    subSystem = ssTrackingStrip;
  else if (subSystemName == "TimingDiamond")
    subSystem = ssTimingDiamond;
  else if (subSystemName == "TotemTiming")
    subSystem = ssTotemTiming;
  else if (subSystemName == "TotemT2")
    subSystem = ssTotemT2;

  if (subSystem == ssUndefined)
    throw cms::Exception("TotemVFATRawToDigi::TotemVFATRawToDigi")
        << "Unknown sub-system string " << subSystemName << "." << endl;

  // FED (OptoRx) headers and footers
  produces<vector<TotemFEDInfo>>(subSystemName);

  // declare products
  if (subSystem == ssTrackingStrip)
    produces<DetSetVector<TotemRPDigi>>(subSystemName);

  else if (subSystem == ssTimingDiamond)
    produces<DetSetVector<CTPPSDiamondDigi>>(subSystemName);

  else if (subSystem == ssTotemTiming)
    produces<DetSetVector<TotemTimingDigi>>(subSystemName);

  else if (subSystem == ssTotemT2)
    produces<edmNew::DetSetVector<TotemT2Digi>>(subSystemName);

  // set default IDs
  if (fedIds.empty()) {
    if (subSystem == ssTrackingStrip) {
      for (int id = FEDNumbering::MINTotemRPHorizontalFEDID; id <= FEDNumbering::MAXTotemRPHorizontalFEDID; ++id)
        fedIds.push_back(id);

      for (int id = FEDNumbering::MINTotemRPVerticalFEDID; id <= FEDNumbering::MAXTotemRPVerticalFEDID; ++id)
        fedIds.push_back(id);
    }

    else if (subSystem == ssTimingDiamond) {
      for (int id = FEDNumbering::MINCTPPSDiamondFEDID; id <= FEDNumbering::MAXCTPPSDiamondFEDID; ++id)
        fedIds.push_back(id);
    }

    else if (subSystem == ssTotemTiming) {
      for (int id = FEDNumbering::MINTotemRPTimingVerticalFEDID; id <= FEDNumbering::MAXTotemRPTimingVerticalFEDID;
           ++id)
        fedIds.push_back(id);
    }

    else if (subSystem == ssTotemT2) {
      for (int id = FEDNumbering::MINTotemT2FEDID; id <= FEDNumbering::MAXTotemT2FEDID; ++id)
        fedIds.push_back(id);
    }
  }
  LogDebug("TotemVFATRawToDigi").log([this](auto &log) {
    log << "List of FEDs handled by this instance: ";
    string sep;
    for (const auto &fedId : fedIds)
      log << sep << fedId, sep = ", ";
  });

  // conversion status
  produces<DetSetVector<TotemVFATStatus>>(subSystemName);

  totemMappingToken = esConsumes<TotemDAQMapping, TotemReadoutRcd>(ESInputTag("", subSystemName));
  analysisMaskToken = esConsumes<TotemAnalysisMask, TotemAnalysisMaskRcd>(ESInputTag("", subSystemName));
}

TotemVFATRawToDigi::~TotemVFATRawToDigi() {}

void TotemVFATRawToDigi::produce(edm::Event &event, const edm::EventSetup &es) {
  if (subSystem == ssTrackingStrip)
    run<DetSetVector<TotemRPDigi>>(event, es);

  else if (subSystem == ssTimingDiamond)
    run<DetSetVector<CTPPSDiamondDigi>>(event, es);

  else if (subSystem == ssTotemTiming)
    run<DetSetVector<TotemTimingDigi>>(event, es);

  else if (subSystem == ssTotemT2)
    run<edmNew::DetSetVector<TotemT2Digi>>(event, es);
}

template <typename DigiType>
void TotemVFATRawToDigi::run(edm::Event &event, const edm::EventSetup &es) {
  // mapping and analysis mask
  ESHandle<TotemDAQMapping> mapping;
  ESHandle<TotemAnalysisMask> analysisMaskHandle;
  TotemAnalysisMask analysisMask;

  // raw data handle
  edm::Handle<FEDRawDataCollection> rawData;
  event.getByToken(fedDataToken, rawData);

  // book output products
  vector<TotemFEDInfo> fedInfo;
  DigiType digi;
  DetSetVector<TotemVFATStatus> conversionStatus;

  // raw-data unpacking
  bool data_exist = false;
  SimpleVFATFrameCollection vfatCollection;
  for (const auto &fedId : fedIds) {
    const FEDRawData &data = rawData->FEDData(fedId);
    if (data.size() > 0) {
      rawDataUnpacker.run(fedId, data, fedInfo, vfatCollection);
      data_exist = true;
    }
  }

  // get mapping records and do raw-to-digi conversion only if some data exists
  if (data_exist) {
    // get DAQ mapping
    mapping = es.getHandle(totemMappingToken);
    if (!mapping.isValid() || mapping.failedToGet()) {
      throw cms::Exception("TotemVFATRawToDigi::TotemVFATRawToDigi")
          << "No DAQMapping found for " << subSystemName << "." << endl;
    }

    // get analysis mask to mask channels
    analysisMaskHandle = es.getHandle(analysisMaskToken);
    if (analysisMaskHandle.isValid() && !analysisMaskHandle.failedToGet()) {
      analysisMask = *analysisMaskHandle;
    } else {
      errSummary.add(fmt::format("No AnalysisMask found for {0}", subSystemName), "");
      analysisMask = TotemAnalysisMask();
    }

    // raw-to-digi conversion
    rawToDigiConverter.run(vfatCollection, *mapping, analysisMask, digi, conversionStatus);
  }

  // commit products to event
  event.put(make_unique<vector<TotemFEDInfo>>(fedInfo), subSystemName);
  event.put(make_unique<DigiType>(digi), subSystemName);
  event.put(make_unique<DetSetVector<TotemVFATStatus>>(conversionStatus), subSystemName);
}

void TotemVFATRawToDigi::endStream() {
  rawToDigiConverter.printSummaries();
  errSummary.printSummary();
}

void TotemVFATRawToDigi::fillDescriptions(edm::ConfigurationDescriptions &descriptions) {
  // totemVFATRawToDigi
  edm::ParameterSetDescription desc;
  desc.add<edm::InputTag>("rawDataTag", edm::InputTag(""));
  desc.add<std::string>("subSystem", "")->setComment("options: RP");
  desc.add<std::vector<unsigned int>>("fedIds", {})
      ->setComment(
          "IMPORTANT: leave empty to load the default configuration from "
          "DataFormats/FEDRawData/interface/FEDNumbering.h");
  {
    edm::ParameterSetDescription psd0;
    psd0.addUntracked<unsigned int>("verbosity", 0);
    desc.add<edm::ParameterSetDescription>("RawUnpacking", psd0);
  }
  {
    edm::ParameterSetDescription psd0;
    psd0.addUntracked<unsigned int>("verbosity", 0)
        ->setComment(
            "0-3: 1=one line/event with some corrupted VFAT frame, 2=list all corrupt VFAT frames/event, 3=all "
            "problems with every corrupt frame");
    psd0.add<unsigned int>("testFootprint", 2)->setComment("0=no test, 1=warn only, 2=warn and skip");
    psd0.add<unsigned int>("testCRC", 2);
    psd0.add<unsigned int>("testID", 2)->setComment("compare the ID from data and mapping");
    psd0.add<unsigned int>("testECMostFrequent", 2)
        ->setComment("compare frame EC with the most frequent value in the event");
    psd0.add<unsigned int>("testBCMostFrequent", 2);
    psd0.addUntracked<unsigned int>("EC_min", 10)
        ->setComment("minimal number of frames to search for the most frequent counter value");
    psd0.addUntracked<unsigned int>("BC_min", 10);
    psd0.addUntracked<double>("EC_fraction", 0.6)
        ->setComment(
            "the most frequent counter value is accepted provided its relative occupancy is higher than this fraction");
    psd0.addUntracked<double>("BC_fraction", 0.6);
    psd0.add<bool>("useOlderT2TestFile", false)
        ->setComment("treat hwID field as two separate 8-bit fields instead of one 16-bit");
    psd0.addUntracked<bool>("printErrorSummary", false)->setComment("per-VFAT error summary at the end of the job");
    psd0.addUntracked<bool>("printUnknownFrameSummary", false)
        ->setComment("summary of frames found in data, but not in the mapping");
    desc.add<edm::ParameterSetDescription>("RawToDigi", psd0);
  }
  descriptions.add("totemVFATRawToDigi", desc);
  // or use the following to generate the label from the module's C++ type
  //descriptions.addWithDefaultLabel(desc);
}

DEFINE_FWK_MODULE(TotemVFATRawToDigi);