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  unsigned int ls = e.luminosityBlock();
79  if (totevents > 0 && totevents % numEventsPerFile_ == 0) {
80  index_++;
81  std::string filename = edm::Service<evf::EvFDaqDirector>()->getOpenRawFilePath(ls, index_);
82  std::string destinationDir = edm::Service<evf::EvFDaqDirector>()->buBaseRunDir();
83  templateConsumer_->initialize(destinationDir, filename, ls);
84  }
85  totevents++;
86  // serialize the FEDRawDataCollection into the format that we expect for
87  // FRDEventMsgView objects (may be better ways to do this)
89  e.getByToken(token_, fedBuffers);
90 
91  // determine the expected size of the FRDEvent IN BYTES !!!!!
92  assert(frdVersion_ <= FRDHeaderMaxVersion);
93  int headerSize = FRDHeaderVersionSize[frdVersion_];
94  int expectedSize = headerSize;
95  int nFeds = frdVersion_ < 3 ? 1024 : FEDNumbering::lastFEDId() + 1;
96 
97  for (int idx = 0; idx < nFeds; ++idx) {
98  FEDRawData singleFED = fedBuffers->FEDData(idx);
99  expectedSize += singleFED.size();
100  }
101 
102  totsize += expectedSize;
103  // build the FRDEvent into a temporary buffer
104  std::unique_ptr<std::vector<unsigned char>> workBuffer(
105  std::make_unique<std::vector<unsigned char>>(expectedSize + 256));
106  uint32* bufPtr = (uint32*)(workBuffer.get()->data());
107  if (frdVersion_ <= 5) {
108  *bufPtr++ = (uint32)frdVersion_; // version number only
109  } else {
110  uint16 flags = 0;
111  if (!e.eventAuxiliary().isRealData())
113  *(uint16*)bufPtr = (uint16)(frdVersion_ & 0xffff);
114  *((uint16*)bufPtr + 1) = flags;
115  bufPtr++;
116  }
117  *bufPtr++ = (uint32)e.id().run();
118  *bufPtr++ = (uint32)e.luminosityBlock();
119  *bufPtr++ = (uint32)e.id().event();
120  if (frdVersion_ == 4)
121  *bufPtr++ = 0; //64-bit event id high part
122 
123  if (frdVersion_ < 3) {
124  uint32 fedsize[1024];
125  for (int idx = 0; idx < 1024; ++idx) {
126  FEDRawData singleFED = fedBuffers->FEDData(idx);
127  fedsize[idx] = singleFED.size();
128  //std::cout << "fed size " << singleFED.size()<< std::endl;
129  }
130  memcpy(bufPtr, fedsize, 1024 * sizeof(uint32));
131  bufPtr += 1024;
132  } else {
133  *bufPtr++ = expectedSize - headerSize;
134  *bufPtr++ = 0;
135  if (frdVersion_ <= 4)
136  *bufPtr++ = 0;
137  }
138  uint32* payloadPtr = bufPtr;
139  for (int idx = 0; idx < nFeds; ++idx) {
140  FEDRawData singleFED = fedBuffers->FEDData(idx);
141  if (singleFED.size() > 0) {
142  memcpy(bufPtr, singleFED.data(), singleFED.size());
143  bufPtr += singleFED.size() / 4;
144  }
145  }
146  if (frdVersion_ > 4) {
147  //crc32c checksum
148  uint32_t crc = 0;
149  *(payloadPtr - 1) = crc32c(crc, (const unsigned char*)payloadPtr, expectedSize - headerSize);
150  } else if (frdVersion_ >= 3) {
151  //adler32 checksum
152  uint32 adlera = 1;
153  uint32 adlerb = 0;
154  cms::Adler32((const char*)payloadPtr, expectedSize - headerSize, adlera, adlerb);
155  *(payloadPtr - 1) = (adlerb << 16) | adlera;
156  }
157 
158  // create the FRDEventMsgView and use the template consumer to write it out
159  FRDEventMsgView msg(workBuffer.get()->data());
160  writtensize += msg.size();
161 
162  templateConsumer_->doOutputEvent(msg);
163 }
164 
165 template <class Consumer>
167  // edm::Service<evf::EvFDaqDirector>()->updateBuLock(1);
168  templateConsumer_->start();
169 }
170 
171 template <class Consumer>
173  templateConsumer_->stop();
174 }
175 
176 template <class Consumer>
178  index_ = 0;
179  std::string filename = edm::Service<evf::EvFDaqDirector>()->getOpenRawFilePath(ls.id().luminosityBlock(), index_);
180  std::string destinationDir = edm::Service<evf::EvFDaqDirector>()->buBaseRunDir();
181  std::cout << " writing to destination dir " << destinationDir << " name: " << filename << std::endl;
182  templateConsumer_->initialize(destinationDir, filename, ls.id().luminosityBlock());
183  //edm::Service<evf::EvFDaqDirector>()->updateBuLock(ls.id().luminosityBlock()+1);
184  if (!firstLumi_) {
185  timeval now;
186  ::gettimeofday(&now, nullptr);
187  //long long elapsedusec = (now.tv_sec - startOfLastLumi.tv_sec)*1000000+now.tv_usec-startOfLastLumi.tv_usec;
188  /* std::cout << "(now.tv_sec - startOfLastLumi.tv_sec) " << now.tv_sec <<"-" << startOfLastLumi.tv_sec */
189  /* <<" (now.tv_usec-startOfLastLumi.tv_usec) " << now.tv_usec << "-" << startOfLastLumi.tv_usec << std::endl; */
190  /* std::cout << "elapsedusec " << elapsedusec << " totevents " << totevents << " size (GB)" << writtensize */
191  /* << " rate " << (writtensize-writtenSizeLast)/elapsedusec << " MB/s" <<std::endl; */
192  writtenSizeLast = writtensize;
193  ::gettimeofday(&startOfLastLumi, nullptr);
194  //edm::Service<evf::EvFDaqDirector>()->writeLsStatisticsBU(ls.id().luminosityBlock(), totevents, totsize, elapsedusec);
195  } else
196  ::gettimeofday(&startOfLastLumi, nullptr);
197  totevents = 0;
198  totsize = 0LL;
199  firstLumi_ = false;
200 }
201 
202 template <class Consumer>
204  // templateConsumer_->touchlock(ls.id().luminosityBlock(),basedir);
205  templateConsumer_->endOfLS(ls.id().luminosityBlock());
206 }
207 
208 template <class Consumer>
211  desc.add<edm::InputTag>("source", edm::InputTag("rawDataCollector"));
212  desc.add<unsigned int>("numEventsPerFile", 100);
213  desc.add<unsigned int>("frdVersion", 6);
214  Consumer::extendDescription(desc);
215 
216  descriptions.addWithDefaultLabel(desc);
217 }
218 
219 #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 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
unsigned short uint16
Definition: MsgTools.h:12
const FEDRawData & FEDData(int fedid) const
retrieve data for fed
unsigned int uint32
Definition: MsgTools.h:13
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 uint16 FRDEVENT_MASK_ISGENDATA
const unsigned char * data() const
Return a const pointer to the beginning of the data buffer.
Definition: FEDRawData.cc:24
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
constexpr std::array< uint32, FRDHeaderMaxVersion+1 > FRDHeaderVersionSize
void writeLuminosityBlock(const edm::LuminosityBlockForOutput &) override
void writeRun(const edm::RunForOutput &) override
static constexpr int lastFEDId()
Definition: FEDNumbering.h:19