Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-05-14 02:34:42

0001 #include "FWCore/Framework/interface/ESWatcher.h"
0002 #include "FWCore/Framework/interface/global/EDProducer.h"
0003 #include "FWCore/Framework/interface/ESHandle.h"
0004 #include "FWCore/Framework/interface/EventSetup.h"
0005 #include "FWCore/Framework/interface/Event.h"
0006 #include "FWCore/Framework/interface/MakerMacros.h"
0007 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0008 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0009 #include "FWCore/Utilities/interface/InputTag.h"
0010 #include "FWCore/Utilities/interface/CPUTimer.h"
0011 #include "FWCore/Utilities/interface/thread_safety_macros.h"
0012 #include "FWCore/Utilities/interface/ESGetToken.h"
0013 
0014 #include "CondFormats/SiPixelObjects/interface/PixelFEDCabling.h"
0015 #include "CondFormats/DataRecord/interface/SiPixelFedCablingMapRcd.h"
0016 #include "CondFormats/SiPixelObjects/interface/SiPixelFedCablingMap.h"
0017 #include "CondFormats/SiPixelObjects/interface/SiPixelFedCablingTree.h"
0018 #include "CondFormats/SiPixelObjects/interface/SiPixelFrameReverter.h"
0019 
0020 #include "DataFormats/Common/interface/Handle.h"
0021 #include "DataFormats/Common/interface/DetSetVector.h"
0022 #include "DataFormats/FEDRawData/interface/FEDRawDataCollection.h"
0023 #include "DataFormats/FEDRawData/interface/FEDRawData.h"
0024 #include "DataFormats/SiPixelDetId/interface/PixelFEDChannel.h"
0025 #include "DataFormats/SiPixelDigi/interface/PixelDigi.h"
0026 
0027 #include "EventFilter/SiPixelRawToDigi/interface/PixelDataFormatter.h"
0028 
0029 #include <atomic>
0030 #include <memory>
0031 
0032 namespace sipixeldigitoraw {
0033   struct Cache {
0034     std::unique_ptr<SiPixelFedCablingTree> cablingTree_;
0035     std::unique_ptr<SiPixelFrameReverter> frameReverter_;
0036   };
0037 }  // namespace sipixeldigitoraw
0038 
0039 namespace pr = sipixeldigitoraw;
0040 
0041 class SiPixelDigiToRaw final : public edm::global::EDProducer<edm::LuminosityBlockCache<pr::Cache>> {
0042 public:
0043   /// ctor
0044   explicit SiPixelDigiToRaw(const edm::ParameterSet&);
0045 
0046   /// get data, convert to raw event, attach again to Event
0047   void produce(edm::StreamID, edm::Event&, const edm::EventSetup&) const final;
0048 
0049   std::shared_ptr<pr::Cache> globalBeginLuminosityBlock(edm::LuminosityBlock const&,
0050                                                         edm::EventSetup const& iES) const final;
0051 
0052   void globalEndLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const& iES) const final {}
0053 
0054   // Fill parameters descriptions
0055   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0056 
0057 private:
0058   mutable std::atomic_flag lock_ = ATOMIC_FLAG_INIT;
0059   CMS_THREAD_GUARD(lock_) mutable edm::ESWatcher<SiPixelFedCablingMapRcd> recordWatcher;
0060   CMS_THREAD_GUARD(lock_) mutable std::shared_ptr<pr::Cache> previousCache_;
0061   const edm::EDGetTokenT<edm::DetSetVector<PixelDigi>> tPixelDigi;
0062   const edm::EDGetTokenT<PixelFEDChannelCollection> theBadPixelFEDChannelsToken;
0063   const edm::ESGetToken<SiPixelFedCablingMap, SiPixelFedCablingMapRcd> cabelingMapToken_;
0064   const edm::EDPutTokenT<FEDRawDataCollection> putToken_;
0065   const bool usePilotBlade = false;  // I am not yet sure we need it here?
0066   const bool usePhase1;
0067 };
0068 
0069 using namespace std;
0070 
0071 SiPixelDigiToRaw::SiPixelDigiToRaw(const edm::ParameterSet& pset)
0072     : tPixelDigi{consumes<edm::DetSetVector<PixelDigi>>(pset.getParameter<edm::InputTag>("InputLabel"))},
0073       theBadPixelFEDChannelsToken{consumes<PixelFEDChannelCollection>(pset.getParameter<edm::InputTag>("InputLabel"))},
0074       cabelingMapToken_(
0075           esConsumes<SiPixelFedCablingMap, SiPixelFedCablingMapRcd, edm::Transition::BeginLuminosityBlock>()),
0076       putToken_{produces<FEDRawDataCollection>()},
0077       usePhase1{pset.getParameter<bool>("UsePhase1")} {
0078   // Define EDProduct type
0079 
0080   if (usePhase1)
0081     edm::LogInfo("SiPixelRawToDigi") << " Use pilot blade data (FED 40)";
0082 }
0083 
0084 // -----------------------------------------------------------------------------
0085 std::shared_ptr<pr::Cache> SiPixelDigiToRaw::globalBeginLuminosityBlock(edm::LuminosityBlock const&,
0086                                                                         edm::EventSetup const& es) const {
0087   while (lock_.test_and_set(std::memory_order_acquire))
0088     ;  //spin
0089   auto rel = [](std::atomic_flag* f) { f->clear(std::memory_order_release); };
0090   std::unique_ptr<std::atomic_flag, decltype(rel)> guard(&lock_, rel);
0091 
0092   if (recordWatcher.check(es)) {
0093     edm::ESHandle<SiPixelFedCablingMap> cablingMap = es.getHandle(cabelingMapToken_);
0094     previousCache_ = std::make_shared<pr::Cache>();
0095     previousCache_->cablingTree_ = cablingMap->cablingTree();
0096     previousCache_->frameReverter_ = std::make_unique<SiPixelFrameReverter>(cablingMap.product());
0097   }
0098   return previousCache_;
0099 }
0100 
0101 // -----------------------------------------------------------------------------
0102 void SiPixelDigiToRaw::produce(edm::StreamID, edm::Event& ev, const edm::EventSetup& es) const {
0103   using namespace sipixelobjects;
0104 
0105   edm::Handle<edm::DetSetVector<PixelDigi>> digiCollection;
0106   ev.getByToken(tPixelDigi, digiCollection);
0107 
0108   PixelDataFormatter::RawData rawdata;
0109   PixelDataFormatter::Digis digis;
0110 
0111   int digiCounter = 0;
0112   for (auto const& di : *digiCollection) {
0113     digiCounter += (di.data).size();
0114     digis[di.id] = di.data;
0115   }
0116 
0117   auto cache = luminosityBlockCache(ev.getLuminosityBlock().index());
0118 
0119   LogDebug("SiPixelDigiToRaw") << cache->cablingTree_->version();
0120 
0121   PixelDataFormatter::BadChannels badChannels;
0122   edm::Handle<PixelFEDChannelCollection> pixelFEDChannelCollectionHandle;
0123   if (usePhase1 && ev.getByToken(theBadPixelFEDChannelsToken, pixelFEDChannelCollectionHandle)) {
0124     for (auto const& fedChannels : *pixelFEDChannelCollectionHandle) {
0125       PixelDataFormatter::DetBadChannels detBadChannels;
0126       for (const auto& fedChannel : fedChannels) {
0127         sipixelobjects::CablingPathToDetUnit path = {fedChannel.fed, fedChannel.link, 1};
0128         if (cache->cablingTree_->findItem(path) != nullptr) {
0129           detBadChannels.push_back(fedChannel);
0130         } else {
0131           edm::LogError("SiPixelDigiToRaw")
0132               << " FED " << fedChannel.fed << " Link " << fedChannel.link << " for module " << fedChannels.detId()
0133               << " marked bad, but this channel does not exist in the cabling map" << endl;
0134         }
0135       }  // channels reading a module
0136       if (!detBadChannels.empty())
0137         badChannels.insert({fedChannels.detId(), std::move(detBadChannels)});
0138     }  // loop on detId-s
0139   }
0140 
0141   //PixelDataFormatter formatter(cablingTree_.get());
0142   PixelDataFormatter formatter(cache->cablingTree_.get(), usePhase1);
0143 
0144   formatter.passFrameReverter(cache->frameReverter_.get());
0145 
0146   // create product (raw data)
0147   FEDRawDataCollection buffers;
0148 
0149   // convert data to raw
0150   formatter.formatRawData(ev.id().event(), rawdata, digis, badChannels);
0151 
0152   // pack raw data into collection
0153   for (auto const* fed : cache->cablingTree_->fedList()) {
0154     LogDebug("SiPixelDigiToRaw") << " PRODUCE DATA FOR FED_id: " << fed->id();
0155     FEDRawData& fedRawData = buffers.FEDData(fed->id());
0156     PixelDataFormatter::RawData::iterator fedbuffer = rawdata.find(fed->id());
0157     if (fedbuffer != rawdata.end())
0158       fedRawData = fedbuffer->second;
0159     LogDebug("SiPixelDigiToRaw") << "size of data in fedRawData: " << fedRawData.size();
0160   }
0161 
0162   LogDebug("SiPixelDigiToRaw").log([&](auto& l) {
0163     l << "Words/Digis this ev: " << digiCounter << "(fm:" << formatter.nDigis() << ")/" << formatter.nWords();
0164   });
0165   ev.emplace(putToken_, std::move(buffers));
0166 }
0167 
0168 // -----------------------------------------------------------------------------
0169 void SiPixelDigiToRaw::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0170   edm::ParameterSetDescription desc;
0171   desc.add<edm::InputTag>("InputLabel");
0172   desc.add<bool>("UsePhase1", false);
0173   descriptions.add("siPixelRawData", desc);
0174 }
0175 
0176 // declare this as a framework plugin
0177 #include "FWCore/Framework/interface/MakerMacros.h"
0178 DEFINE_FWK_MODULE(SiPixelDigiToRaw);