Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-06-24 22:51:56

0001 /** \class GEMRawToDigiModule
0002  *  \unpacker for gem
0003  *  \based on CSCDigiToRawModule
0004  *  \author J. Lee - UoS
0005  */
0006 
0007 #include "CondFormats/DataRecord/interface/GEMChMapRcd.h"
0008 #include "CondFormats/GEMObjects/interface/GEMChMap.h"
0009 #include "DataFormats/Common/interface/Handle.h"
0010 #include "DataFormats/FEDRawData/interface/FEDNumbering.h"
0011 #include "DataFormats/FEDRawData/interface/FEDRawDataCollection.h"
0012 #include "DataFormats/FEDRawData/interface/FEDTrailer.h"
0013 #include "DataFormats/GEMDigi/interface/GEMAMC13StatusCollection.h"
0014 #include "DataFormats/GEMDigi/interface/GEMAMCStatusCollection.h"
0015 #include "DataFormats/GEMDigi/interface/GEMOHStatusCollection.h"
0016 #include "DataFormats/GEMDigi/interface/GEMVFATStatusCollection.h"
0017 #include "DataFormats/GEMDigi/interface/GEMDigiCollection.h"
0018 #include "EventFilter/GEMRawToDigi/interface/GEMRawToDigi.h"
0019 #include "FWCore/Framework/interface/Event.h"
0020 #include "FWCore/Framework/interface/EventSetup.h"
0021 #include "FWCore/Framework/interface/Run.h"
0022 #include "FWCore/Framework/interface/global/EDProducer.h"
0023 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0024 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0025 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
0026 #include "FWCore/Utilities/interface/ESGetToken.h"
0027 #include "FWCore/Utilities/interface/InputTag.h"
0028 #include "FWCore/Utilities/interface/Transition.h"
0029 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0030 
0031 class GEMRawToDigiModule : public edm::global::EDProducer<edm::RunCache<GEMChMap>> {
0032 public:
0033   /// Constructor
0034   GEMRawToDigiModule(const edm::ParameterSet& pset);
0035 
0036   // global::EDProducer
0037   std::shared_ptr<GEMChMap> globalBeginRun(edm::Run const&, edm::EventSetup const&) const override;
0038   void produce(edm::StreamID, edm::Event&, edm::EventSetup const&) const override;
0039   void globalEndRun(edm::Run const&, edm::EventSetup const&) const override{};
0040 
0041   // Fill parameters descriptions
0042   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0043 
0044 private:
0045   edm::EDGetTokenT<FEDRawDataCollection> fed_token;
0046   edm::ESGetToken<GEMChMap, GEMChMapRcd> gemChMapToken_;
0047   bool useDBEMap_, keepDAQStatus_, readMultiBX_, ge21Off_;
0048   unsigned int fedIdStart_, fedIdEnd_;
0049   std::unique_ptr<GEMRawToDigi> gemRawToDigi_;
0050 };
0051 
0052 #include "FWCore/Framework/interface/MakerMacros.h"
0053 DEFINE_FWK_MODULE(GEMRawToDigiModule);
0054 
0055 GEMRawToDigiModule::GEMRawToDigiModule(const edm::ParameterSet& pset)
0056     : fed_token(consumes<FEDRawDataCollection>(pset.getParameter<edm::InputTag>("InputLabel"))),
0057       useDBEMap_(pset.getParameter<bool>("useDBEMap")),
0058       keepDAQStatus_(pset.getParameter<bool>("keepDAQStatus")),
0059       readMultiBX_(pset.getParameter<bool>("readMultiBX")),
0060       ge21Off_(pset.getParameter<bool>("ge21Off")),
0061       fedIdStart_(pset.getParameter<unsigned int>("fedIdStart")),
0062       fedIdEnd_(pset.getParameter<unsigned int>("fedIdEnd")),
0063       gemRawToDigi_(std::make_unique<GEMRawToDigi>()) {
0064   produces<GEMDigiCollection>();
0065   if (keepDAQStatus_) {
0066     produces<GEMAMC13StatusCollection>("AMC13Status");
0067     produces<GEMAMCStatusCollection>("AMCStatus");
0068     produces<GEMOHStatusCollection>("OHStatus");
0069     produces<GEMVFATStatusCollection>("VFATStatus");
0070   }
0071   if (useDBEMap_) {
0072     gemChMapToken_ = esConsumes<GEMChMap, GEMChMapRcd, edm::Transition::BeginRun>();
0073   }
0074   if (ge21Off_ && fedIdStart_ == FEDNumbering::MINGEMFEDID && fedIdEnd_ == FEDNumbering::MAXGEMFEDID) {
0075     fedIdEnd_ = FEDNumbering::MINGE21FEDID - 1;
0076   } else if (ge21Off_) {
0077     edm::LogError("InvalidSettings") << "Turning GE2/1 off requires changing the FEDIDs the GEM unpacker looks at. If "
0078                                         "you wish to set the FEDIDs yourself, don't use the ge21Off switch.";
0079   }
0080 }
0081 
0082 void GEMRawToDigiModule::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0083   edm::ParameterSetDescription desc;
0084   desc.add<edm::InputTag>("InputLabel", edm::InputTag("rawDataCollector"));
0085   desc.add<bool>("useDBEMap", false);
0086   desc.add<bool>("keepDAQStatus", true);
0087   desc.add<bool>("readMultiBX", false);
0088   desc.add<bool>("ge21Off", false);
0089   desc.add<unsigned int>("fedIdStart", FEDNumbering::MINGEMFEDID);
0090   desc.add<unsigned int>("fedIdEnd", FEDNumbering::MAXGEMFEDID);
0091   descriptions.add("muonGEMDigisDefault", desc);
0092 }
0093 
0094 std::shared_ptr<GEMChMap> GEMRawToDigiModule::globalBeginRun(edm::Run const&, edm::EventSetup const& iSetup) const {
0095   if (useDBEMap_) {
0096     const auto& eMap = iSetup.getData(gemChMapToken_);
0097     auto gemChMap = std::make_shared<GEMChMap>(eMap);
0098     return gemChMap;
0099   } else {
0100     // no EMap in DB, using dummy
0101     auto gemChMap = std::make_shared<GEMChMap>();
0102     gemChMap->setDummy();
0103     return gemChMap;
0104   }
0105 }
0106 
0107 void GEMRawToDigiModule::produce(edm::StreamID iID, edm::Event& iEvent, edm::EventSetup const& iSetup) const {
0108   auto outGEMDigis = std::make_unique<GEMDigiCollection>();
0109   auto outAMC13Status = std::make_unique<GEMAMC13StatusCollection>();
0110   auto outAMCStatus = std::make_unique<GEMAMCStatusCollection>();
0111   auto outOHStatus = std::make_unique<GEMOHStatusCollection>();
0112   auto outVFATStatus = std::make_unique<GEMVFATStatusCollection>();
0113 
0114   // Take raw from the event
0115   edm::Handle<FEDRawDataCollection> fed_buffers;
0116   iEvent.getByToken(fed_token, fed_buffers);
0117 
0118   auto gemChMap = runCache(iEvent.getRun().index());
0119 
0120   for (unsigned int fedId = fedIdStart_; fedId <= fedIdEnd_; ++fedId) {
0121     const FEDRawData& fedData = fed_buffers->FEDData(fedId);
0122 
0123     int nWords = fedData.size() / sizeof(uint64_t);
0124     LogDebug("GEMRawToDigiModule") << "fedId:" << fedId << " words: " << nWords;
0125     GEMAMC13Status st_amc13(fedData);
0126     if (st_amc13.isBad()) {
0127       LogDebug("GEMRawToDigiModule") << st_amc13;
0128       if (keepDAQStatus_) {
0129         outAMC13Status.get()->insertDigi(fedId, st_amc13);
0130       }
0131       continue;
0132     }
0133 
0134     const uint64_t* word = reinterpret_cast<const uint64_t*>(fedData.data());
0135     auto amc13 = gemRawToDigi_->convertWordToGEMAMC13(word);
0136     LogDebug("GEMRawToDigiModule") << "Event bx:" << iEvent.bunchCrossing() << " lv1Id:" << iEvent.id().event()
0137                                    << " orbitNumber:" << iEvent.orbitNumber();
0138     LogDebug("GEMRawToDigiModule") << "AMC13 bx:" << amc13->bunchCrossing() << " lv1Id:" << int(amc13->lv1Id())
0139                                    << " orbitNumber:" << amc13->orbitNumber();
0140 
0141     // Read AMC data
0142     for (const auto& amc : *(amc13->getAMCpayloads())) {
0143       uint8_t amcNum = amc.amcNum();
0144       if (!gemChMap->isValidAMC(fedId, amcNum)) {
0145         st_amc13.inValidAMC();
0146         continue;
0147       }
0148 
0149       GEMAMCStatus st_amc(amc13.get(), amc);
0150       if (st_amc.isBad()) {
0151         LogDebug("GEMRawToDigiModule") << st_amc;
0152         if (keepDAQStatus_) {
0153           outAMCStatus.get()->insertDigi(fedId, st_amc);
0154         }
0155         continue;
0156       }
0157 
0158       uint16_t amcBx = amc.bunchCrossing();
0159       LogDebug("GEMRawToDigiModule") << "AMC no.:" << int(amc.amcNum()) << " bx:" << int(amc.bunchCrossing())
0160                                      << " lv1Id:" << int(amc.lv1Id()) << " orbitNumber:" << int(amc.orbitNumber());
0161 
0162       // Read GEB data
0163       for (const auto& optoHybrid : *amc.gebs()) {
0164         uint8_t gebId = optoHybrid.inputID();
0165 
0166         bool isValidChamber = gemChMap->isValidChamber(fedId, amcNum, gebId);
0167         if (!isValidChamber) {
0168           st_amc.inValidOH();
0169           continue;
0170         }
0171         auto geb_dc = gemChMap->chamberPos(fedId, amcNum, gebId);
0172         GEMDetId cId(geb_dc.detId);
0173         int chamberType = geb_dc.chamberType;
0174 
0175         GEMOHStatus st_oh(optoHybrid);
0176         if (st_oh.isBad()) {
0177           LogDebug("GEMRawToDigiModule") << st_oh;
0178           if (keepDAQStatus_) {
0179             outOHStatus.get()->insertDigi(cId, st_oh);
0180           }
0181         }
0182 
0183         //Read vfat data
0184         for (auto vfat : *optoHybrid.vFATs()) {
0185           // set vfat fw version
0186           if (chamberType < 10)
0187             vfat.setVersion(2);
0188           else
0189             vfat.setVersion(3);
0190           uint16_t vfatId = vfat.vfatId();
0191 
0192           if (!gemChMap->isValidVFAT(chamberType, vfatId)) {
0193             st_oh.inValidVFAT();
0194             continue;
0195           }
0196 
0197           GEMVFATStatus st_vfat(amc, vfat, vfat.phi(), readMultiBX_);
0198           if (st_vfat.isBad()) {
0199             LogDebug("GEMRawToDigiModule") << st_vfat;
0200             if (keepDAQStatus_) {
0201               outVFATStatus.get()->insertDigi(cId, st_vfat);
0202             }
0203             continue;
0204           }
0205 
0206           int bx(vfat.bc() - amcBx);
0207 
0208           for (int chan = 0; chan < GEMVFAT::nChannels; ++chan) {
0209             uint8_t chan0xf = 0;
0210             if (chan < 64)
0211               chan0xf = ((vfat.lsData() >> chan) & 0x1);
0212             else
0213               chan0xf = ((vfat.msData() >> (chan - 64)) & 0x1);
0214 
0215             // no hits
0216             if (chan0xf == 0)
0217               continue;
0218 
0219             auto stMap = gemChMap->getStrip(chamberType, vfatId, chan);
0220 
0221             int stripId = stMap.stNum;
0222             int ieta = stMap.iEta;
0223 
0224             GEMDetId gemId(cId.region(), cId.ring(), cId.station(), cId.layer(), cId.chamber(), ieta);
0225 
0226             GEMDigi digi(stripId, bx);
0227 
0228             LogDebug("GEMRawToDigiModule") << "fed: " << fedId << " amc:" << int(amcNum) << " geb:" << int(gebId)
0229                                            << " vfat id:" << int(vfatId) << ",type:" << chamberType << " id:" << gemId
0230                                            << " ch:" << chan << " st:" << digi.strip() << " bx:" << digi.bx();
0231 
0232             outGEMDigis.get()->insertDigi(gemId, digi);
0233 
0234           }  // end of channel loop
0235 
0236           if (keepDAQStatus_) {
0237             outVFATStatus.get()->insertDigi(cId, st_vfat);
0238           }
0239 
0240         }  // end of vfat loop
0241 
0242         if (keepDAQStatus_) {
0243           outOHStatus.get()->insertDigi(cId, st_oh);
0244         }
0245 
0246       }  // end of optohybrid loop
0247 
0248       if (keepDAQStatus_) {
0249         outAMCStatus.get()->insertDigi(fedId, st_amc);
0250       }
0251 
0252     }  // end of amc loop
0253 
0254     if (keepDAQStatus_) {
0255       outAMC13Status.get()->insertDigi(fedId, st_amc13);
0256     }
0257 
0258   }  // end of amc13
0259 
0260   iEvent.put(std::move(outGEMDigis));
0261 
0262   if (keepDAQStatus_) {
0263     iEvent.put(std::move(outAMC13Status), "AMC13Status");
0264     iEvent.put(std::move(outAMCStatus), "AMCStatus");
0265     iEvent.put(std::move(outOHStatus), "OHStatus");
0266     iEvent.put(std::move(outVFATStatus), "VFATStatus");
0267   }
0268 }