CMS 3D CMS Logo

RawEventOutputModuleForBU.h
Go to the documentation of this file.
1 #ifndef IOPool_Streamer_interface_RawEventOutputModuleForBU_h
2 #define IOPool_Streamer_interface_RawEventOutputModuleForBU_h
3 
4 #include <memory>
5 #include <vector>
6 
24 
25 template <class Consumer>
26 class RawEventOutputModuleForBU : public edm::one::OutputModule<edm::one::WatchRuns, edm::one::WatchLuminosityBlocks> {
27  typedef unsigned int uint32;
35 public:
36  explicit RawEventOutputModuleForBU(edm::ParameterSet const& ps);
37  ~RawEventOutputModuleForBU() override;
38 
39  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
40 
41 private:
42  void write(edm::EventForOutput const& e) override;
43  void beginRun(edm::RunForOutput const&) override;
44  void endRun(edm::RunForOutput const&) override;
45  void writeRun(const edm::RunForOutput&) override {}
47 
50 
51  std::unique_ptr<Consumer> templateConsumer_;
53  const unsigned int numEventsPerFile_;
54  const unsigned int frdVersion_;
55  unsigned long long totsize = 0LL;
56  unsigned long long writtensize = 0LL;
57  unsigned long long writtenSizeLast = 0LL;
58  unsigned int totevents = 0;
59  unsigned int index_ = 0;
60  timeval startOfLastLumi;
61  bool firstLumi_ = true;
62 };
63 
64 template <class Consumer>
67  edm::one::OutputModule<edm::one::WatchRuns, edm::one::WatchLuminosityBlocks>(ps),
68  templateConsumer_(new Consumer(ps)),
69  token_(consumes<FEDRawDataCollection>(ps.getParameter<edm::InputTag>("source"))),
70  numEventsPerFile_(ps.getParameter<unsigned int>("numEventsPerFile")),
71  frdVersion_(ps.getParameter<unsigned int>("frdVersion")) {}
72 
73 template <class Consumer>
75 
76 template <class Consumer>
78  using namespace edm::streamer;
79 
80  unsigned int ls = e.luminosityBlock();
81  if (totevents > 0 && totevents % numEventsPerFile_ == 0) {
82  index_++;
83  std::string filename = edm::Service<evf::EvFDaqDirector>()->getOpenRawFilePath(ls, index_);
84  std::string destinationDir = edm::Service<evf::EvFDaqDirector>()->buBaseRunDir();
85  templateConsumer_->initialize(destinationDir, filename, ls);
86  }
87  totevents++;
88  // serialize the FEDRawDataCollection into the format that we expect for
89  // FRDEventMsgView objects (may be better ways to do this)
91  e.getByToken(token_, fedBuffers);
92 
93  // determine the expected size of the FRDEvent IN BYTES !!!!!
95  int headerSize = edm::streamer::FRDHeaderVersionSize[frdVersion_];
96  int expectedSize = headerSize;
97  int nFeds = frdVersion_ < 3 ? 1024 : FEDNumbering::lastFEDId() + 1;
98 
99  for (int idx = 0; idx < nFeds; ++idx) {
100  FEDRawData singleFED = fedBuffers->FEDData(idx);
101  expectedSize += singleFED.size();
102  }
103 
104  totsize += expectedSize;
105  // build the FRDEvent into a temporary buffer
106  std::unique_ptr<std::vector<unsigned char>> workBuffer(
107  std::make_unique<std::vector<unsigned char>>(expectedSize + 256));
108  uint32* bufPtr = (uint32*)(workBuffer.get()->data());
109  if (frdVersion_ <= 5) {
110  *bufPtr++ = (uint32)frdVersion_; // version number only
111  } else {
112  uint16 flags = 0;
113  if (!e.eventAuxiliary().isRealData())
115  *(uint16*)bufPtr = (uint16)(frdVersion_ & 0xffff);
116  *((uint16*)bufPtr + 1) = flags;
117  bufPtr++;
118  }
119  *bufPtr++ = (uint32)e.id().run();
120  *bufPtr++ = (uint32)e.luminosityBlock();
121  *bufPtr++ = (uint32)e.id().event();
122  if (frdVersion_ == 4)
123  *bufPtr++ = 0; //64-bit event id high part
124 
125  if (frdVersion_ < 3) {
126  uint32 fedsize[1024];
127  for (int idx = 0; idx < 1024; ++idx) {
128  FEDRawData singleFED = fedBuffers->FEDData(idx);
129  fedsize[idx] = singleFED.size();
130  //std::cout << "fed size " << singleFED.size()<< std::endl;
131  }
132  memcpy(bufPtr, fedsize, 1024 * sizeof(uint32));
133  bufPtr += 1024;
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  } else if (frdVersion_ >= 3) {
153  //adler32 checksum
154  uint32 adlera = 1;
155  uint32 adlerb = 0;
156  cms::Adler32((const char*)payloadPtr, expectedSize - headerSize, adlera, adlerb);
157  *(payloadPtr - 1) = (adlerb << 16) | adlera;
158  }
159 
160  // create the FRDEventMsgView and use the template consumer to write it out
161  edm::streamer::FRDEventMsgView msg(workBuffer.get()->data());
162  writtensize += msg.size();
163 
164  templateConsumer_->doOutputEvent(msg);
165 }
166 
167 template <class Consumer>
169  // edm::Service<evf::EvFDaqDirector>()->updateBuLock(1);
170  templateConsumer_->start();
171 }
172 
173 template <class Consumer>
175  templateConsumer_->stop();
176 }
177 
178 template <class Consumer>
180  index_ = 0;
181  std::string filename = edm::Service<evf::EvFDaqDirector>()->getOpenRawFilePath(ls.id().luminosityBlock(), index_);
182  std::string destinationDir = edm::Service<evf::EvFDaqDirector>()->buBaseRunDir();
183  std::cout << " writing to destination dir " << destinationDir << " name: " << filename << std::endl;
184  templateConsumer_->initialize(destinationDir, filename, ls.id().luminosityBlock());
185  //edm::Service<evf::EvFDaqDirector>()->updateBuLock(ls.id().luminosityBlock()+1);
186  if (!firstLumi_) {
187  timeval now;
188  ::gettimeofday(&now, nullptr);
189  //long long elapsedusec = (now.tv_sec - startOfLastLumi.tv_sec)*1000000+now.tv_usec-startOfLastLumi.tv_usec;
190  /* std::cout << "(now.tv_sec - startOfLastLumi.tv_sec) " << now.tv_sec <<"-" << startOfLastLumi.tv_sec */
191  /* <<" (now.tv_usec-startOfLastLumi.tv_usec) " << now.tv_usec << "-" << startOfLastLumi.tv_usec << std::endl; */
192  /* std::cout << "elapsedusec " << elapsedusec << " totevents " << totevents << " size (GB)" << writtensize */
193  /* << " rate " << (writtensize-writtenSizeLast)/elapsedusec << " MB/s" <<std::endl; */
194  writtenSizeLast = writtensize;
195  ::gettimeofday(&startOfLastLumi, nullptr);
196  //edm::Service<evf::EvFDaqDirector>()->writeLsStatisticsBU(ls.id().luminosityBlock(), totevents, totsize, elapsedusec);
197  } else
198  ::gettimeofday(&startOfLastLumi, nullptr);
199  totevents = 0;
200  totsize = 0LL;
201  firstLumi_ = false;
202 }
203 
204 template <class Consumer>
206  // templateConsumer_->touchlock(ls.id().luminosityBlock(),basedir);
207  templateConsumer_->endOfLS(ls.id().luminosityBlock());
208 }
209 
210 template <class Consumer>
213  desc.add<edm::InputTag>("source", edm::InputTag("rawDataCollector"));
214  desc.add<unsigned int>("numEventsPerFile", 100);
215  desc.add<unsigned int>("frdVersion", 6);
216  Consumer::extendDescription(desc);
217 
218  descriptions.addWithDefaultLabel(desc);
219 }
220 
221 #endif // IOPool_Streamer_interface_RawEventOutputModuleForBU_h
std::unique_ptr< Consumer > templateConsumer_
const edm::EDGetTokenT< FEDRawDataCollection > token_
void addWithDefaultLabel(ParameterSetDescription const &psetDescription)
void endRun(edm::RunForOutput const &) override
void beginRun(edm::RunForOutput const &) override
constexpr std::array< uint32, FRDHeaderMaxVersion+1 > FRDHeaderVersionSize
constexpr size_t FRDHeaderMaxVersion
size_t size() const
Lenght of the data buffer in bytes.
Definition: FEDRawData.h:48
void write(edm::EventForOutput const &e) override
RawEventOutputModuleForBU(edm::ParameterSet const &ps)
assert(be >=bs)
void beginLuminosityBlock(edm::LuminosityBlockForOutput const &) override
const uint16 FRDEVENT_MASK_ISGENDATA
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
tuple msg
Definition: mps_check.py:286
HLT enums.
const unsigned char * data() const
Return a const pointer to the beginning of the data buffer.
Definition: FEDRawData.cc:24
unsigned short uint16
Definition: MsgTools.h:13
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
void writeLuminosityBlock(const edm::LuminosityBlockForOutput &) override
unsigned int uint32
Definition: MsgTools.h:14
void writeRun(const edm::RunForOutput &) override
static constexpr int lastFEDId()
Definition: FEDNumbering.h:19