** Warning **

Issuing rollback() due to DESTROY without explicit disconnect() of DBD::mysql::db handle dbname=lxr at /lxr/lib/LXR/Common.pm line 1103.

Last-Modified: Tue, 26 Sep 2022 03:55:57 GMT Content-Type: text/html; charset=utf-8 /CMSSW_12_6_X_2022-09-25-2300/EventFilter/Playback/src/PlaybackRawDataProvider.cc
Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:26:12

0001 ////////////////////////////////////////////////////////////////////////////////
0002 //
0003 // PlaybackRawDataProvider
0004 // -----------------------
0005 //
0006 //            21/09/2006 Philipp Schieferdecker <philipp.schieferdecker@cern.ch>
0007 ////////////////////////////////////////////////////////////////////////////////
0008 
0009 #include "EventFilter/Playback/interface/PlaybackRawDataProvider.h"
0010 #include "DataFormats/FEDRawData/interface/FEDNumbering.h"
0011 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0012 #include "DataFormats/FEDRawData/interface/FEDRawDataCollection.h"
0013 #include <cstring>
0014 #include <iostream>
0015 
0016 using namespace std;
0017 using namespace edm;
0018 
0019 ////////////////////////////////////////////////////////////////////////////////
0020 // initialize static data members
0021 ////////////////////////////////////////////////////////////////////////////////
0022 
0023 PlaybackRawDataProvider* PlaybackRawDataProvider::instance_ = nullptr;
0024 
0025 ////////////////////////////////////////////////////////////////////////////////
0026 // construction/destruction
0027 ////////////////////////////////////////////////////////////////////////////////
0028 
0029 //______________________________________________________________________________
0030 PlaybackRawDataProvider::PlaybackRawDataProvider(const ParameterSet& iConfig)
0031     : queueSize_(0),
0032       eventQueue_(nullptr),
0033       runNumber_(nullptr),
0034       evtNumber_(nullptr),
0035       count_(0),
0036       writeIndex_(0),
0037       readIndex_(0),
0038       freeToEof_(false),
0039       filesClosed_(false),
0040       destroying_(false) {
0041   queueSize_ = iConfig.getUntrackedParameter<unsigned int>("queueSize", 32);
0042   sem_init(&lock_, 0, 1);
0043   sem_init(&writeSem_, 0, queueSize_);
0044   sem_init(&readSem_, 0, 0);
0045   runNumber_ = new unsigned int[queueSize_];
0046   evtNumber_ = new unsigned int[queueSize_];
0047   eventQueue_ = new FEDRawDataCollection*[queueSize_];
0048   for (unsigned int i = 0; i < queueSize_; i++)
0049     eventQueue_[i] = nullptr;
0050   edm::LogInfo("PbImpl") << "Created Concrete RawData Provider 0x" << hex << (unsigned long)this << dec << endl;
0051   instance_ = this;
0052 }
0053 
0054 //______________________________________________________________________________
0055 PlaybackRawDataProvider::~PlaybackRawDataProvider() {
0056   if (nullptr != runNumber_)
0057     delete[] runNumber_;
0058   if (nullptr != evtNumber_)
0059     delete[] evtNumber_;
0060   if (nullptr != eventQueue_) {
0061     for (unsigned int i = 0; i < queueSize_; i++)
0062       if (nullptr != eventQueue_[i])
0063         delete eventQueue_[i];
0064     delete[] eventQueue_;
0065   }
0066   edm::LogInfo("PbImpl") << "Destroyed Concrete RawData Provider 0x" << hex << (unsigned long)this << dec << endl;
0067   instance_ = nullptr;
0068 
0069   destroying_ = true;
0070   postReadSem();
0071   postWriteSem();
0072   unlock();
0073   sem_destroy(&lock_);
0074   sem_destroy(&writeSem_);
0075   sem_destroy(&readSem_);
0076   usleep(10000);
0077 }
0078 
0079 ////////////////////////////////////////////////////////////////////////////////
0080 // implementation of member functions
0081 ////////////////////////////////////////////////////////////////////////////////
0082 
0083 //______________________________________________________________________________
0084 void PlaybackRawDataProvider::analyze(const Event& iEvent, const EventSetup& iSetup) {
0085   if (freeToEof_)
0086     return;
0087   waitWriteSem();
0088   if (freeToEof_)
0089     return;
0090   runNumber_[writeIndex_] = iEvent.id().run();
0091   evtNumber_[writeIndex_] = iEvent.id().event();
0092 
0093   Handle<FEDRawDataCollection> pRawData;
0094   iEvent.getByLabel("rawDataCollector", pRawData);
0095 
0096   if (!pRawData.isValid()) {
0097     edm::LogError("InvalidHandle") << "no raw data found!" << endl;
0098     return;
0099   }
0100 
0101   // copy the raw data collection into rawData_, retrievable via getFEDRawData()
0102   assert(nullptr == eventQueue_[writeIndex_]);
0103   eventQueue_[writeIndex_] = new FEDRawDataCollection();
0104   for (unsigned int i = 0; i < (unsigned int)FEDNumbering::MAXFEDID + 1; i++) {
0105     unsigned int fedSize = pRawData->FEDData(i).size();
0106     if (fedSize > 0) {
0107       FEDRawData& fedData = eventQueue_[writeIndex_]->FEDData(i);
0108       fedData.resize(fedSize);
0109       memcpy(fedData.data(), pRawData->FEDData(i).data(), fedSize);
0110     }
0111   }
0112 
0113   lock();
0114   writeIndex_ = (writeIndex_ + 1) % queueSize_;
0115   count_++;
0116   unlock();
0117 
0118   postReadSem();
0119 
0120   return;
0121 }
0122 
0123 //______________________________________________________________________________
0124 void PlaybackRawDataProvider::beginJob() {}
0125 
0126 //______________________________________________________________________________
0127 void PlaybackRawDataProvider::endJob() { edm::LogInfo("Summary") << count_ << " events read." << endl; }
0128 
0129 //______________________________________________________________________________
0130 void PlaybackRawDataProvider::respondToCloseInputFile(edm::FileBlock const& fb) { filesClosed_ = true; }
0131 
0132 //______________________________________________________________________________
0133 FEDRawDataCollection* PlaybackRawDataProvider::getFEDRawData() {
0134   FEDRawDataCollection* result = nullptr;
0135   waitReadSem();
0136   //do not read data if destructor is called
0137   if (destroying_)
0138     return nullptr;
0139   lock();
0140   result = eventQueue_[readIndex_];
0141   eventQueue_[readIndex_] = nullptr;
0142   readIndex_ = (readIndex_ + 1) % queueSize_;
0143   unlock();
0144 
0145   postWriteSem();
0146   return result;
0147 }
0148 
0149 //______________________________________________________________________________
0150 FEDRawDataCollection* PlaybackRawDataProvider::getFEDRawData(unsigned int& runNumber, unsigned int& evtNumber) {
0151   FEDRawDataCollection* result = nullptr;
0152   waitReadSem();
0153   //do not read data if destructor is called
0154   if (destroying_)
0155     return nullptr;
0156   lock();
0157   runNumber = runNumber_[readIndex_];
0158   evtNumber = evtNumber_[readIndex_];
0159   result = eventQueue_[readIndex_];
0160   //  assert(0!=result);
0161   eventQueue_[readIndex_] = nullptr;
0162   readIndex_ = (readIndex_ + 1) % queueSize_;
0163 
0164   unlock();
0165 
0166   postWriteSem();
0167   return result;
0168 }
0169 
0170 //______________________________________________________________________________
0171 void PlaybackRawDataProvider::sem_print() {
0172   lock();
0173   int wsem, rsem;
0174   sem_getvalue(&writeSem_, &wsem);
0175   sem_getvalue(&readSem_, &rsem);
0176   cout << "sem_print():"
0177        << " wsem=" << wsem << " rsem=" << rsem << " writeIndex=" << writeIndex_ << " readIndex=" << readIndex_ << endl;
0178   unlock();
0179 }
0180 
0181 void PlaybackRawDataProvider::setFreeToEof() {
0182   //  cout << "  PlaybackRawDataProvider::setFreeToEof()" << endl;
0183   freeToEof_ = true;
0184   //  cout << "  PlaybackRawDataProvider::setFreeToEof() call postReadSem" << endl;
0185   postWriteSem();
0186 }
0187 
0188 bool PlaybackRawDataProvider::areFilesClosed() { return filesClosed_; }
0189 ////////////////////////////////////////////////////////////////////////////////
0190 // framework module implementation macro
0191 ////////////////////////////////////////////////////////////////////////////////