CMS 3D CMS Logo

PlaybackRawDataProvider.cc
Go to the documentation of this file.
1 //
3 // PlaybackRawDataProvider
4 // -----------------------
5 //
6 // 21/09/2006 Philipp Schieferdecker <philipp.schieferdecker@cern.ch>
8 
13 #include <cstring>
14 #include <iostream>
15 
16 using namespace std;
17 using namespace edm;
18 
20 // initialize static data members
22 
24 
26 // construction/destruction
28 
29 //______________________________________________________________________________
31  : queueSize_(0),
32  eventQueue_(nullptr),
33  runNumber_(nullptr),
34  evtNumber_(nullptr),
35  count_(0),
36  writeIndex_(0),
37  readIndex_(0),
38  freeToEof_(false),
39  filesClosed_(false),
40  destroying_(false) {
41  queueSize_ = iConfig.getUntrackedParameter<unsigned int>("queueSize", 32);
42  sem_init(&lock_, 0, 1);
43  sem_init(&writeSem_, 0, queueSize_);
44  sem_init(&readSem_, 0, 0);
45  runNumber_ = new unsigned int[queueSize_];
46  evtNumber_ = new unsigned int[queueSize_];
48  for (unsigned int i = 0; i < queueSize_; i++)
49  eventQueue_[i] = nullptr;
50  edm::LogInfo("PbImpl") << "Created Concrete RawData Provider 0x" << hex << (unsigned long)this << dec << endl;
51  instance_ = this;
52 }
53 
54 //______________________________________________________________________________
56  if (nullptr != runNumber_)
57  delete[] runNumber_;
58  if (nullptr != evtNumber_)
59  delete[] evtNumber_;
60  if (nullptr != eventQueue_) {
61  for (unsigned int i = 0; i < queueSize_; i++)
62  if (nullptr != eventQueue_[i])
63  delete eventQueue_[i];
64  delete[] eventQueue_;
65  }
66  edm::LogInfo("PbImpl") << "Destroyed Concrete RawData Provider 0x" << hex << (unsigned long)this << dec << endl;
67  instance_ = nullptr;
68 
69  destroying_ = true;
70  postReadSem();
71  postWriteSem();
72  unlock();
73  sem_destroy(&lock_);
74  sem_destroy(&writeSem_);
75  sem_destroy(&readSem_);
76  usleep(10000);
77 }
78 
80 // implementation of member functions
82 
83 //______________________________________________________________________________
85  if (freeToEof_)
86  return;
87  waitWriteSem();
88  if (freeToEof_)
89  return;
90  runNumber_[writeIndex_] = iEvent.id().run();
91  evtNumber_[writeIndex_] = iEvent.id().event();
92 
94  iEvent.getByLabel("rawDataCollector", pRawData);
95 
96  if (!pRawData.isValid()) {
97  edm::LogError("InvalidHandle") << "no raw data found!" << endl;
98  return;
99  }
100 
101  // copy the raw data collection into rawData_, retrievable via getFEDRawData()
102  assert(nullptr == eventQueue_[writeIndex_]);
104  for (unsigned int i = 0; i < (unsigned int)FEDNumbering::MAXFEDID + 1; i++) {
105  unsigned int fedSize = pRawData->FEDData(i).size();
106  if (fedSize > 0) {
108  fedData.resize(fedSize);
109  memcpy(fedData.data(), pRawData->FEDData(i).data(), fedSize);
110  }
111  }
112 
113  lock();
115  count_++;
116  unlock();
117 
118  postReadSem();
119 
120  return;
121 }
122 
123 //______________________________________________________________________________
125 
126 //______________________________________________________________________________
127 void PlaybackRawDataProvider::endJob() { edm::LogInfo("Summary") << count_ << " events read." << endl; }
128 
129 //______________________________________________________________________________
131 
132 //______________________________________________________________________________
134  FEDRawDataCollection* result = nullptr;
135  waitReadSem();
136  //do not read data if destructor is called
137  if (destroying_)
138  return nullptr;
139  lock();
141  eventQueue_[readIndex_] = nullptr;
142  readIndex_ = (readIndex_ + 1) % queueSize_;
143  unlock();
144 
145  postWriteSem();
146  return result;
147 }
148 
149 //______________________________________________________________________________
151  FEDRawDataCollection* result = nullptr;
152  waitReadSem();
153  //do not read data if destructor is called
154  if (destroying_)
155  return nullptr;
156  lock();
158  evtNumber = evtNumber_[readIndex_];
160  // assert(0!=result);
161  eventQueue_[readIndex_] = nullptr;
162  readIndex_ = (readIndex_ + 1) % queueSize_;
163 
164  unlock();
165 
166  postWriteSem();
167  return result;
168 }
169 
170 //______________________________________________________________________________
172  lock();
173  int wsem, rsem;
174  sem_getvalue(&writeSem_, &wsem);
175  sem_getvalue(&readSem_, &rsem);
176  cout << "sem_print():"
177  << " wsem=" << wsem << " rsem=" << rsem << " writeIndex=" << writeIndex_ << " readIndex=" << readIndex_ << endl;
178  unlock();
179 }
180 
182  // cout << " PlaybackRawDataProvider::setFreeToEof()" << endl;
183  freeToEof_ = true;
184  // cout << " PlaybackRawDataProvider::setFreeToEof() call postReadSem" << endl;
185  postWriteSem();
186 }
187 
190 // framework module implementation macro
static const char runNumber_[]
FEDRawDataCollection ** eventQueue_
void analyze(const edm::Event &, const edm::EventSetup &) override
PlaybackRawDataProvider(const edm::ParameterSet &)
virtual FEDRawDataCollection * getFEDRawData()
size_t size() const
Lenght of the data buffer in bytes.
Definition: FEDRawData.h:45
Log< level::Error, false > LogError
assert(be >=bs)
T getUntrackedParameter(std::string const &, T const &) const
int iEvent
Definition: GenABIO.cc:224
static PlaybackRawDataProvider * instance_
void resize(size_t newsize)
Definition: FEDRawData.cc:28
void respondToCloseInputFile(edm::FileBlock const &fb) override
const FEDRawData & FEDData(int fedid) const
retrieve data for fed
Log< level::Info, false > LogInfo
bool isValid() const
Definition: HandleBase.h:70
HLT enums.
const unsigned char * data() const
Return a const pointer to the beginning of the data buffer.
Definition: FEDRawData.cc:24