CMS 3D CMS Logo

RawEventOutputModuleForBU.h
Go to the documentation of this file.
1 #ifndef IOPool_Streamer_RawEventOutputModuleForBU_h
2 #define IOPool_Streamer_RawEventOutputModuleForBU_h
3 
14 
20 
21 #include "boost/shared_array.hpp"
22 
23 class FRDEventMsgView;
24 template <class Consumer>
25 class RawEventOutputModuleForBU : public edm::one::OutputModule<edm::one::WatchRuns, edm::one::WatchLuminosityBlocks>
26 {
27  typedef unsigned int uint32;
35  public:
36  explicit RawEventOutputModuleForBU(edm::ParameterSet const& ps);
37  ~RawEventOutputModuleForBU() override;
38 
39  private:
40  void write(edm::EventForOutput const& e) override;
41  void beginRun(edm::RunForOutput const&) override;
42  void endRun(edm::RunForOutput const&) override;
43  void writeRun(const edm::RunForOutput&) override {}
45 
48 
49  std::unique_ptr<Consumer> templateConsumer_;
53  unsigned int numEventsPerFile_;
54  unsigned int frdVersion_;
55  unsigned long long totsize;
56  unsigned long long writtensize;
57  unsigned long long writtenSizeLast;
58  unsigned int totevents;
59  unsigned int index_;
60  timeval startOfLastLumi;
61  bool firstLumi_;
62 };
63 
64 template <class Consumer>
67  edm::one::OutputModule<edm::one::WatchRuns, edm::one::WatchLuminosityBlocks>(ps),
68  templateConsumer_(new Consumer(ps)),
69  label_(ps.getUntrackedParameter<std::string>("ProductLabel","source")),
70  instance_(ps.getUntrackedParameter<std::string>("ProductInstance","")),
72  numEventsPerFile_(ps.getUntrackedParameter<unsigned int>("numEventsPerFile",100)),
73  frdVersion_(ps.getUntrackedParameter<unsigned int>("frdVersion",3)),
74  totsize(0LL),
75  writtensize(0LL),
76  writtenSizeLast(0LL),
77  totevents(0),
78  index_(0),
80 {
81 }
82 
83 template <class Consumer>
85 
86 template <class Consumer>
88 {
89  unsigned int ls = e.luminosityBlock();
91  index_++;
92  std::string filename = edm::Service<evf::EvFDaqDirector>()->getOpenRawFilePath( ls,index_);
93  std::string destinationDir = edm::Service<evf::EvFDaqDirector>()->buBaseRunDir();
94  templateConsumer_->initialize(destinationDir,filename,ls);
95  }
96  totevents++;
97  // serialize the FEDRawDataCollection into the format that we expect for
98  // FRDEventMsgView objects (may be better ways to do this)
100  e.getByToken(token_, fedBuffers);
101 
102  // determine the expected size of the FRDEvent IN BYTES !!!!!
103  int headerSize = FRDHeaderVersionSize[frdVersion_];
104  int expectedSize = headerSize;
105  int nFeds = frdVersion_<3? 1024 : FEDNumbering::lastFEDId()+1;
106 
107 
108  for (int idx = 0; idx < nFeds; ++idx) {
109  FEDRawData singleFED = fedBuffers->FEDData(idx);
110  expectedSize += singleFED.size();
111  }
112 
113  totsize += expectedSize;
114  // build the FRDEvent into a temporary buffer
115  boost::shared_array<unsigned char> workBuffer(new unsigned char[expectedSize + 256]);
116  uint32 *bufPtr = (uint32*) workBuffer.get();
117  *bufPtr++ = (uint32) frdVersion_; // version number
118  *bufPtr++ = (uint32) e.id().run();
119  *bufPtr++ = (uint32) e.luminosityBlock();
120  *bufPtr++ = (uint32) e.id().event();
121  if (frdVersion_==4)
122  *bufPtr++ = 0;//64-bit event id high part
123 
124  if (frdVersion_<3) {
125  uint32 fedsize[1024];
126  for (int idx = 0; idx < 1024; ++idx) {
127  FEDRawData singleFED = fedBuffers->FEDData(idx);
128  fedsize[idx] = singleFED.size();
129  //std::cout << "fed size " << singleFED.size()<< std::endl;
130  }
131  memcpy(bufPtr,fedsize,1024 * sizeof(uint32));
132  bufPtr += 1024;
133  }
134  else {
135  *bufPtr++ = expectedSize-headerSize;
136  *bufPtr++ = 0;
137  if (frdVersion_<=4)
138  *bufPtr++ = 0;
139  }
140  uint32 *payloadPtr=bufPtr;
141  for (int idx = 0; idx < nFeds; ++idx) {
142  FEDRawData singleFED = fedBuffers->FEDData(idx);
143  if (singleFED.size() > 0) {
144  memcpy(bufPtr, singleFED.data(), singleFED.size());
145  bufPtr += singleFED.size()/4;
146  }
147  }
148  if (frdVersion_>4) {
149  //crc32c checksum
150  uint32_t crc = 0;
151  *(payloadPtr-1) = crc32c(crc,(const unsigned char*) payloadPtr, expectedSize-headerSize);
152  }
153  else if (frdVersion_>=3) {
154  //adler32 checksum
155  uint32 adlera = 1;
156  uint32 adlerb = 0;
157  cms::Adler32((const char*) payloadPtr, expectedSize-headerSize, adlera, adlerb);
158  *(payloadPtr-1) = (adlerb << 16) | adlera;
159  }
160 
161  // create the FRDEventMsgView and use the template consumer to write it out
162  FRDEventMsgView msg(workBuffer.get());
163  writtensize+=msg.size();
164 
165  if (templateConsumer_->sharedMode())
166  templateConsumer_->doOutputEvent(workBuffer);
167  else
168  templateConsumer_->doOutputEvent(msg);
169 }
170 
171 template <class Consumer>
173 {
174  // edm::Service<evf::EvFDaqDirector>()->updateBuLock(1);
175  templateConsumer_->start();
176 }
177 
178 template <class Consumer>
180 {
181  templateConsumer_->stop();
182 }
183 
184 template <class Consumer>
186  index_ = 0;
188  std::string destinationDir = edm::Service<evf::EvFDaqDirector>()->buBaseRunDir();
189  std::cout << " writing to destination dir " << destinationDir << " name: " << filename << std::endl;
190  templateConsumer_->initialize(destinationDir,filename,ls.id().luminosityBlock());
191  //edm::Service<evf::EvFDaqDirector>()->updateBuLock(ls.id().luminosityBlock()+1);
192  if(!firstLumi_){
193  timeval now;
194  ::gettimeofday(&now,nullptr);
195  //long long elapsedusec = (now.tv_sec - startOfLastLumi.tv_sec)*1000000+now.tv_usec-startOfLastLumi.tv_usec;
196 /* std::cout << "(now.tv_sec - startOfLastLumi.tv_sec) " << now.tv_sec <<"-" << startOfLastLumi.tv_sec */
197 /* <<" (now.tv_usec-startOfLastLumi.tv_usec) " << now.tv_usec << "-" << startOfLastLumi.tv_usec << std::endl; */
198 /* std::cout << "elapsedusec " << elapsedusec << " totevents " << totevents << " size (GB)" << writtensize */
199 /* << " rate " << (writtensize-writtenSizeLast)/elapsedusec << " MB/s" <<std::endl; */
201  ::gettimeofday(&startOfLastLumi,nullptr);
202  //edm::Service<evf::EvFDaqDirector>()->writeLsStatisticsBU(ls.id().luminosityBlock(), totevents, totsize, elapsedusec);
203  }
204  else
205  ::gettimeofday(&startOfLastLumi,nullptr);
206  totevents = 0;
207  totsize = 0LL;
208  firstLumi_ = false;
209 }
210 template <class Consumer>
212 
213  // templateConsumer_->touchlock(ls.id().luminosityBlock(),basedir);
214  templateConsumer_->endOfLS(ls.id().luminosityBlock());
215 }
216 #endif
RunNumber_t run() const
Definition: EventID.h:39
std::unique_ptr< Consumer > templateConsumer_
EventNumber_t event() const
Definition: EventID.h:41
void endRun(edm::RunForOutput const &) override
EventID const & id() const
void beginRun(edm::RunForOutput const &) override
BasicHandle getByToken(EDGetToken token, TypeID const &typeID) const
const uint32 FRDHeaderVersionSize[6]
void write(edm::EventForOutput const &e) override
edm::EDGetTokenT< FEDRawDataCollection > token_
RawEventOutputModuleForBU(edm::ParameterSet const &ps)
size_t size() const
Lenght of the data buffer in bytes.
Definition: FEDRawData.h:47
void beginLuminosityBlock(edm::LuminosityBlockForOutput const &) override
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
const FEDRawData & FEDData(int fedid) const
retrieve data for fed
uint32_t crc32c(uint32_t crc, const unsigned char *buf, size_t len)
Definition: crc32c.cc:340
void Adler32(char const *data, size_t len, uint32_t &a, uint32_t &b)
def ls(path, rec=False)
Definition: eostools.py:349
void endLuminosityBlock(edm::LuminosityBlockForOutput const &) override
LuminosityBlockNumber_t luminosityBlock() const
tuple msg
Definition: mps_check.py:279
HLT enums.
const unsigned char * data() const
Return a const pointer to the beginning of the data buffer.
Definition: FEDRawData.cc:28
LuminosityBlockNumber_t luminosityBlock() const
void writeLuminosityBlock(const edm::LuminosityBlockForOutput &) override
LuminosityBlockID const & id() const
void writeRun(const edm::RunForOutput &) override
static constexpr int lastFEDId()
Definition: FEDNumbering.h:22