Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-09-07 04:36:12

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, chamberType);
0176         if (st_oh.isBad()) {
0177           LogDebug("GEMRawToDigiModule") << st_oh;
0178           if (keepDAQStatus_) {
0179             outOHStatus.get()->insertDigi(cId, st_oh);
0180           }
0181           continue;
0182         }
0183 
0184         //Read vfat data
0185         for (auto vfat : *optoHybrid.vFATs()) {
0186           // set vfat fw version
0187           if (chamberType < 10)
0188             vfat.setVersion(2);
0189           else
0190             vfat.setVersion(3);
0191           uint16_t vfatId = vfat.vfatId();
0192 
0193           if (!gemChMap->isValidVFAT(chamberType, vfatId)) {
0194             st_oh.inValidVFAT();
0195             continue;
0196           }
0197 
0198           GEMVFATStatus st_vfat(amc, optoHybrid, vfat, chamberType, readMultiBX_);
0199           if (st_vfat.isBad()) {
0200             LogDebug("GEMRawToDigiModule") << st_vfat;
0201             if (keepDAQStatus_) {
0202               outVFATStatus.get()->insertDigi(cId, st_vfat);
0203             }
0204             continue;
0205           }
0206 
0207           int bx(vfat.bc() - amcBx);
0208 
0209           for (int chan = 0; chan < GEMVFAT::nChannels; ++chan) {
0210             uint8_t chan0xf = 0;
0211             if (chan < 64)
0212               chan0xf = ((vfat.lsData() >> chan) & 0x1);
0213             else
0214               chan0xf = ((vfat.msData() >> (chan - 64)) & 0x1);
0215 
0216             // no hits
0217             if (chan0xf == 0)
0218               continue;
0219 
0220             auto stMap = gemChMap->getStrip(chamberType, vfatId, chan);
0221 
0222             int stripId = stMap.stNum;
0223             int ieta = stMap.iEta;
0224 
0225             GEMDetId gemId(cId.region(), cId.ring(), cId.station(), cId.layer(), cId.chamber(), ieta);
0226 
0227             GEMDigi digi(stripId, bx);
0228 
0229             LogDebug("GEMRawToDigiModule") << "fed: " << fedId << " amc:" << int(amcNum) << " geb:" << int(gebId)
0230                                            << " vfat id:" << int(vfatId) << ",type:" << chamberType << " id:" << gemId
0231                                            << " ch:" << chan << " st:" << digi.strip() << " bx:" << digi.bx();
0232 
0233             outGEMDigis.get()->insertDigi(gemId, digi);
0234 
0235           }  // end of channel loop
0236 
0237           if (keepDAQStatus_) {
0238             outVFATStatus.get()->insertDigi(cId, st_vfat);
0239           }
0240 
0241         }  // end of vfat loop
0242 
0243         if (keepDAQStatus_) {
0244           outOHStatus.get()->insertDigi(cId, st_oh);
0245         }
0246 
0247       }  // end of optohybrid loop
0248 
0249       if (keepDAQStatus_) {
0250         outAMCStatus.get()->insertDigi(fedId, st_amc);
0251       }
0252 
0253     }  // end of amc loop
0254 
0255     if (keepDAQStatus_) {
0256       outAMC13Status.get()->insertDigi(fedId, st_amc13);
0257     }
0258 
0259   }  // end of amc13
0260 
0261   iEvent.put(std::move(outGEMDigis));
0262 
0263   if (keepDAQStatus_) {
0264     iEvent.put(std::move(outAMC13Status), "AMC13Status");
0265     iEvent.put(std::move(outAMCStatus), "AMCStatus");
0266     iEvent.put(std::move(outOHStatus), "OHStatus");
0267     iEvent.put(std::move(outVFATStatus), "VFATStatus");
0268   }
0269 }