GEMDigiToRawModule

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
/** \class GEMDigiToRawModule
 *  \packer for gem
 *  \based on CSCDigiToRawModule
 *  \author J. Lee - UoS
 */

#include "CondFormats/DataRecord/interface/GEMChMapRcd.h"
#include "CondFormats/GEMObjects/interface/GEMChMap.h"
#include "DataFormats/Common/interface/Handle.h"
#include "DataFormats/FEDRawData/interface/FEDHeader.h"
#include "DataFormats/FEDRawData/interface/FEDNumbering.h"
#include "DataFormats/FEDRawData/interface/FEDRawData.h"
#include "DataFormats/FEDRawData/interface/FEDRawDataCollection.h"
#include "DataFormats/FEDRawData/interface/FEDTrailer.h"
#include "DataFormats/GEMDigi/interface/GEMAMC13.h"
#include "DataFormats/GEMDigi/interface/GEMDigiCollection.h"
#include "FWCore/Framework/interface/Event.h"
#include "FWCore/Framework/interface/EventSetup.h"
#include "FWCore/Framework/interface/Run.h"
#include "FWCore/Framework/interface/global/EDProducer.h"
#include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
#include "FWCore/Utilities/interface/ESGetToken.h"
#include "FWCore/Utilities/interface/InputTag.h"
#include "FWCore/MessageLogger/interface/MessageLogger.h"

class GEMDigiToRawModule : public edm::global::EDProducer<edm::RunCache<GEMChMap>> {
public:
  /// Constructor
  GEMDigiToRawModule(const edm::ParameterSet& pset);

  // global::EDProducer
  std::shared_ptr<GEMChMap> globalBeginRun(edm::Run const&, edm::EventSetup const&) const override;
  void produce(edm::StreamID, edm::Event&, edm::EventSetup const&) const override;
  void globalEndRun(edm::Run const&, edm::EventSetup const&) const override {}

  // Fill parameters descriptions
  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);

private:
  const int event_type_;
  const int minBunch_;
  const int maxBunch_;
  const edm::EDGetTokenT<GEMDigiCollection> digiToken_;
  edm::ESGetToken<GEMChMap, GEMChMapRcd> gemChMapToken_;
  const bool useDBEMap_;
  const bool simulatePulseStretching_;
};

#include "FWCore/Framework/interface/MakerMacros.h"
DEFINE_FWK_MODULE(GEMDigiToRawModule);

GEMDigiToRawModule::GEMDigiToRawModule(const edm::ParameterSet& pset)
    : event_type_(pset.getParameter<int>("eventType")),
      minBunch_(pset.getParameter<int>("minBunch")),
      maxBunch_(pset.getParameter<int>("maxBunch")),
      digiToken_(consumes<GEMDigiCollection>(pset.getParameter<edm::InputTag>("gemDigi"))),
      useDBEMap_(pset.getParameter<bool>("useDBEMap")),
      simulatePulseStretching_(pset.getParameter<bool>("simulatePulseStretching")) {
  produces<FEDRawDataCollection>();
  if (useDBEMap_) {
    gemChMapToken_ = esConsumes<GEMChMap, GEMChMapRcd, edm::Transition::BeginRun>();
  }
}

void GEMDigiToRawModule::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
  edm::ParameterSetDescription desc;
  desc.add<edm::InputTag>("gemDigi", edm::InputTag("simMuonGEMDigis"));
  desc.add<int>("eventType", 0);

  // time window for pulse stretching simulation
  desc.add<int>("minBunch", -3);
  desc.add<int>("maxBunch", 4);

  desc.add<bool>("useDBEMap", false);
  desc.add<bool>("simulatePulseStretching", false);
  descriptions.add("gemPackerDefault", desc);
}

std::shared_ptr<GEMChMap> GEMDigiToRawModule::globalBeginRun(edm::Run const&, edm::EventSetup const& iSetup) const {
  if (useDBEMap_) {
    const auto& eMap = iSetup.getData(gemChMapToken_);
    auto gemChMap = std::make_shared<GEMChMap>(eMap);
    return gemChMap;
  } else {
    // no EMap in DB, using dummy
    auto gemChMap = std::make_shared<GEMChMap>();
    gemChMap->setDummy();
    return gemChMap;
  }
}

void GEMDigiToRawModule::produce(edm::StreamID iID, edm::Event& iEvent, edm::EventSetup const&) const {
  auto fedRawDataCol = std::make_unique<FEDRawDataCollection>();

  edm::Handle<GEMDigiCollection> gemDigis;
  iEvent.getByToken(digiToken_, gemDigis);
  if (!gemDigis.isValid()) {
    iEvent.put(std::move(fedRawDataCol));
    return;
  }

  auto gemChMap = runCache(iEvent.getRun().index());

  std::vector<std::unique_ptr<GEMAMC13>> amc13s;
  amc13s.reserve(FEDNumbering::MAXGEMFEDID - FEDNumbering::MINGEMFEDID + 1);

  int LV1_id = iEvent.id().event();
  uint8_t BX_id(iEvent.bunchCrossing());
  int OrN = iEvent.orbitNumber();

  // making map of bx GEMDigiCollection
  // each bx will be saved as new GEMAMC13, so GEMDigiCollection needs to be split into bx
  std::map<int, GEMDigiCollection> gemBxMap;
  for (auto const& etaPart : *gemDigis) {
    GEMDetId gemId = etaPart.first;
    const GEMDigiCollection::Range& digis = etaPart.second;
    for (auto digi = digis.first; digi != digis.second; ++digi) {
      int bx = digi->bx();
      if (simulatePulseStretching_) {
        if (bx < minBunch_ or bx > maxBunch_)
          continue;
        else
          bx = 0;
      }
      auto search = gemBxMap.find(bx);
      if (search != gemBxMap.end()) {
        search->second.insertDigi(gemId, *digi);
      } else {
        GEMDigiCollection newGDC;
        newGDC.insertDigi(gemId, *digi);
        gemBxMap.insert(std::pair<int, GEMDigiCollection>(bx, newGDC));
      }
    }
  }

  for (unsigned int fedId = FEDNumbering::MINGEMFEDID; fedId <= FEDNumbering::MAXGEMFEDID; ++fedId) {
    uint32_t amc13EvtLength = 0;
    std::unique_ptr<GEMAMC13> amc13 = std::make_unique<GEMAMC13>();

    for (uint8_t amcNum = 0; amcNum <= GEMChMap::maxAMCs_; ++amcNum) {
      uint32_t amcSize = 0;
      std::unique_ptr<GEMAMC> amc = std::make_unique<GEMAMC>();

      for (uint8_t gebId = 0; gebId <= GEMChMap::maxGEBs_; ++gebId) {
        std::unique_ptr<GEMOptoHybrid> optoH = std::make_unique<GEMOptoHybrid>();

        if (!gemChMap->isValidChamber(fedId, amcNum, gebId))
          continue;

        auto geb_dc = gemChMap->chamberPos(fedId, amcNum, gebId);
        GEMDetId cid = geb_dc.detId;
        int chamberType = geb_dc.chamberType;

        auto vfats = gemChMap->getVfats(chamberType);

        for (auto vfatId : vfats) {
          auto iEtas = gemChMap->getIEtas(chamberType, vfatId);
          for (auto iEta : iEtas) {
            GEMDetId gemId(cid.region(), cid.ring(), cid.station(), cid.layer(), cid.chamber(), iEta);

            for (auto const& gemBx : gemBxMap) {
              int bc = BX_id + gemBx.first;

              bool hasDigi = false;
              uint64_t lsData = 0;  ///<channels from 1to64
              uint64_t msData = 0;  ///<channels from 65to128

              GEMDigiCollection inBxGemDigis = gemBx.second;
              const GEMDigiCollection::Range& range = inBxGemDigis.get(gemId);

              for (GEMDigiCollection::const_iterator digiIt = range.first; digiIt != range.second; ++digiIt) {
                const GEMDigi& digi = (*digiIt);

                int strip = digi.strip();

                hasDigi = true;

                if (!gemChMap->isValidStrip(chamberType, iEta, strip))
                  continue;
                auto chMap = gemChMap->getChannel(chamberType, iEta, strip);

                if (chMap.vfatAdd != vfatId)
                  continue;

                if (chMap.chNum < 64)
                  lsData |= 1UL << chMap.chNum;
                else
                  msData |= 1UL << (chMap.chNum - 64);

                LogDebug("GEMDigiToRawModule")
                    << "fed: " << fedId << " amc:" << int(amcNum) << " geb:" << int(gebId) << " vfat id:" << int(vfatId)
                    << ",type:" << chamberType << " id:" << gemId << " ch:" << chMap.chNum << " st:" << digi.strip()
                    << " bx:" << digi.bx();
              }

              if (!hasDigi)
                continue;
              // only make vfat with hits
              amcSize += 3;
              int vfatVersion = (chamberType < 10) ? 2 : 3;
              auto vfat = std::make_unique<GEMVFAT>(vfatVersion, bc, LV1_id, vfatId, lsData, msData);
              optoH->addVFAT(*vfat);
            }
          }
        }  // end of vfats in GEB

        if (!optoH->vFATs()->empty()) {
          amcSize += 2;
          optoH->setChamberHeader(optoH->vFATs()->size() * 3, gebId);
          optoH->setChamberTrailer(LV1_id, BX_id, optoH->vFATs()->size() * 3);
          amc->addGEB(*optoH);
        }
      }  // end of GEB loop

      if (!amc->gebs()->empty()) {
        amcSize += 5;
        amc->setAMCheader1(amcSize, BX_id, LV1_id, amcNum);
        amc->setAMCheader2(amcNum, OrN, 1);
        amc->setGEMeventHeader(amc->gebs()->size(), 0);
        amc13->addAMCpayload(*amc);
        // AMC header in GEMAMC13
        amc13->addAMCheader(amcSize, 0, amcNum, 0);
        amc13EvtLength += amcSize + 1;  // AMC data size + AMC header size
      }
    }  // end of AMC loop

    if (!amc13->getAMCpayloads()->empty()) {
      // CDFHeader
      amc13->setCDFHeader(event_type_, LV1_id, BX_id, fedId);
      // AMC13header
      uint8_t nAMC = amc13->getAMCpayloads()->size();
      amc13->setAMC13Header(1, nAMC, OrN);
      amc13->setAMC13Trailer(BX_id, LV1_id, BX_id);
      //CDF trailer
      uint32_t EvtLength = amc13EvtLength + 4;  // 2 header and 2 trailer
      amc13->setCDFTrailer(EvtLength);
      amc13s.emplace_back(std::move(amc13));
    }  // finished making amc13 data
  }  // end of FED loop

  // read out amc13s into fedRawData
  for (const auto& amc13e : amc13s) {
    std::vector<uint64_t> words;
    words.emplace_back(amc13e->getCDFHeader());
    words.emplace_back(amc13e->getAMC13Header());

    for (const auto& w : *amc13e->getAMCheaders())
      words.emplace_back(w);

    for (const auto& amc : *amc13e->getAMCpayloads()) {
      words.emplace_back(amc.getAMCheader1());
      words.emplace_back(amc.getAMCheader2());
      words.emplace_back(amc.getGEMeventHeader());

      for (const auto& geb : *amc.gebs()) {
        words.emplace_back(geb.getChamberHeader());

        for (const auto& vfat : *geb.vFATs()) {
          words.emplace_back(vfat.get_fw());
          words.emplace_back(vfat.get_sw());
          words.emplace_back(vfat.get_tw());
        }

        words.emplace_back(geb.getChamberTrailer());
      }

      words.emplace_back(amc.getGEMeventTrailer());
      words.emplace_back(amc.getAMCTrailer());
    }

    words.emplace_back(amc13e->getAMC13Trailer());
    words.emplace_back(amc13e->getCDFTrailer());

    FEDRawData& fedRawData = fedRawDataCol->FEDData(amc13e->sourceId());

    int dataSize = (words.size()) * sizeof(uint64_t);
    fedRawData.resize(dataSize);

    uint64_t* w = reinterpret_cast<uint64_t*>(fedRawData.data());
    for (const auto& word : words) {
      *(w++) = word;
    }
    LogDebug("GEMDigiToRawModule") << "fedId:" << amc13e->sourceId() << " words:" << words.size();
  }

  iEvent.put(std::move(fedRawDataCol));
}