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  typedef unsigned int uint32;
34 public:
35  explicit RawEventOutputModuleForBU(edm::ParameterSet const& ps);
36  ~RawEventOutputModuleForBU() override;
37 
38 private:
39  void write(edm::EventForOutput const& e) override;
40  void beginRun(edm::RunForOutput const&) override;
41  void endRun(edm::RunForOutput const&) override;
42  void writeRun(const edm::RunForOutput&) override {}
44 
47 
48  std::unique_ptr<Consumer> templateConsumer_;
52  unsigned int numEventsPerFile_;
53  unsigned int frdVersion_;
54  unsigned long long totsize;
55  unsigned long long writtensize;
56  unsigned long long writtenSizeLast;
57  unsigned int totevents;
58  unsigned int index_;
59  timeval startOfLastLumi;
60  bool firstLumi_;
61 };
62 
63 template <class Consumer>
66  edm::one::OutputModule<edm::one::WatchRuns, edm::one::WatchLuminosityBlocks>(ps),
67  templateConsumer_(new Consumer(ps)),
68  label_(ps.getUntrackedParameter<std::string>("ProductLabel", "source")),
69  instance_(ps.getUntrackedParameter<std::string>("ProductInstance", "")),
71  numEventsPerFile_(ps.getUntrackedParameter<unsigned int>("numEventsPerFile", 100)),
72  frdVersion_(ps.getUntrackedParameter<unsigned int>("frdVersion", 3)),
73  totsize(0LL),
74  writtensize(0LL),
76  totevents(0),
77  index_(0),
78  firstLumi_(true) {}
79 
80 template <class Consumer>
82 
83 template <class Consumer>
85  unsigned int ls = e.luminosityBlock();
86  if (totevents > 0 && totevents % numEventsPerFile_ == 0) {
87  index_++;
88  std::string filename = edm::Service<evf::EvFDaqDirector>()->getOpenRawFilePath(ls, index_);
89  std::string destinationDir = edm::Service<evf::EvFDaqDirector>()->buBaseRunDir();
90  templateConsumer_->initialize(destinationDir, filename, ls);
91  }
92  totevents++;
93  // serialize the FEDRawDataCollection into the format that we expect for
94  // FRDEventMsgView objects (may be better ways to do this)
96  e.getByToken(token_, fedBuffers);
97 
98  // determine the expected size of the FRDEvent IN BYTES !!!!!
99  int headerSize = FRDHeaderVersionSize[frdVersion_];
100  int expectedSize = headerSize;
101  int nFeds = frdVersion_ < 3 ? 1024 : FEDNumbering::lastFEDId() + 1;
102 
103  for (int idx = 0; idx < nFeds; ++idx) {
104  FEDRawData singleFED = fedBuffers->FEDData(idx);
105  expectedSize += singleFED.size();
106  }
107 
108  totsize += expectedSize;
109  // build the FRDEvent into a temporary buffer
110  boost::shared_array<unsigned char> workBuffer(new unsigned char[expectedSize + 256]);
111  uint32* bufPtr = (uint32*)workBuffer.get();
112  *bufPtr++ = (uint32)frdVersion_; // version number
113  *bufPtr++ = (uint32)e.id().run();
114  *bufPtr++ = (uint32)e.luminosityBlock();
115  *bufPtr++ = (uint32)e.id().event();
116  if (frdVersion_ == 4)
117  *bufPtr++ = 0; //64-bit event id high part
118 
119  if (frdVersion_ < 3) {
120  uint32 fedsize[1024];
121  for (int idx = 0; idx < 1024; ++idx) {
122  FEDRawData singleFED = fedBuffers->FEDData(idx);
123  fedsize[idx] = singleFED.size();
124  //std::cout << "fed size " << singleFED.size()<< std::endl;
125  }
126  memcpy(bufPtr, fedsize, 1024 * sizeof(uint32));
127  bufPtr += 1024;
128  } else {
129  *bufPtr++ = expectedSize - headerSize;
130  *bufPtr++ = 0;
131  if (frdVersion_ <= 4)
132  *bufPtr++ = 0;
133  }
134  uint32* payloadPtr = bufPtr;
135  for (int idx = 0; idx < nFeds; ++idx) {
136  FEDRawData singleFED = fedBuffers->FEDData(idx);
137  if (singleFED.size() > 0) {
138  memcpy(bufPtr, singleFED.data(), singleFED.size());
139  bufPtr += singleFED.size() / 4;
140  }
141  }
142  if (frdVersion_ > 4) {
143  //crc32c checksum
144  uint32_t crc = 0;
145  *(payloadPtr - 1) = crc32c(crc, (const unsigned char*)payloadPtr, expectedSize - headerSize);
146  } else if (frdVersion_ >= 3) {
147  //adler32 checksum
148  uint32 adlera = 1;
149  uint32 adlerb = 0;
150  cms::Adler32((const char*)payloadPtr, expectedSize - headerSize, adlera, adlerb);
151  *(payloadPtr - 1) = (adlerb << 16) | adlera;
152  }
153 
154  // create the FRDEventMsgView and use the template consumer to write it out
155  FRDEventMsgView msg(workBuffer.get());
156  writtensize += msg.size();
157 
158  if (templateConsumer_->sharedMode())
159  templateConsumer_->doOutputEvent(workBuffer);
160  else
161  templateConsumer_->doOutputEvent(msg);
162 }
163 
164 template <class Consumer>
166  // edm::Service<evf::EvFDaqDirector>()->updateBuLock(1);
167  templateConsumer_->start();
168 }
169 
170 template <class Consumer>
172  templateConsumer_->stop();
173 }
174 
175 template <class Consumer>
177  index_ = 0;
179  std::string destinationDir = edm::Service<evf::EvFDaqDirector>()->buBaseRunDir();
180  std::cout << " writing to destination dir " << destinationDir << " name: " << filename << std::endl;
181  templateConsumer_->initialize(destinationDir, filename, ls.id().luminosityBlock());
182  //edm::Service<evf::EvFDaqDirector>()->updateBuLock(ls.id().luminosityBlock()+1);
183  if (!firstLumi_) {
184  timeval now;
185  ::gettimeofday(&now, nullptr);
186  //long long elapsedusec = (now.tv_sec - startOfLastLumi.tv_sec)*1000000+now.tv_usec-startOfLastLumi.tv_usec;
187  /* std::cout << "(now.tv_sec - startOfLastLumi.tv_sec) " << now.tv_sec <<"-" << startOfLastLumi.tv_sec */
188  /* <<" (now.tv_usec-startOfLastLumi.tv_usec) " << now.tv_usec << "-" << startOfLastLumi.tv_usec << std::endl; */
189  /* std::cout << "elapsedusec " << elapsedusec << " totevents " << totevents << " size (GB)" << writtensize */
190  /* << " rate " << (writtensize-writtenSizeLast)/elapsedusec << " MB/s" <<std::endl; */
192  ::gettimeofday(&startOfLastLumi, nullptr);
193  //edm::Service<evf::EvFDaqDirector>()->writeLsStatisticsBU(ls.id().luminosityBlock(), totevents, totsize, elapsedusec);
194  } else
195  ::gettimeofday(&startOfLastLumi, nullptr);
196  totevents = 0;
197  totsize = 0LL;
198  firstLumi_ = false;
199 }
200 template <class Consumer>
202  // templateConsumer_->touchlock(ls.id().luminosityBlock(),basedir);
203  templateConsumer_->endOfLS(ls.id().luminosityBlock());
204 }
205 #endif
RunNumber_t run() const
Definition: EventID.h:38
std::unique_ptr< Consumer > templateConsumer_
EventNumber_t event() const
Definition: EventID.h:40
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:45
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:285
HLT enums.
const unsigned char * data() const
Return a const pointer to the beginning of the data buffer.
Definition: FEDRawData.cc:24
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:19