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 template <class Consumer>
24 class RawEventOutputModuleForBU : public edm::one::OutputModule<edm::one::WatchRuns, edm::one::WatchLuminosityBlocks> {
25  typedef unsigned int uint32;
33 public:
34  explicit RawEventOutputModuleForBU(edm::ParameterSet const& ps);
35  ~RawEventOutputModuleForBU() override;
36 
37 private:
38  void write(edm::EventForOutput const& e) override;
39  void beginRun(edm::RunForOutput const&) override;
40  void endRun(edm::RunForOutput const&) override;
41  void writeRun(const edm::RunForOutput&) override {}
43 
46 
47  std::unique_ptr<Consumer> templateConsumer_;
51  unsigned int numEventsPerFile_;
52  unsigned int frdVersion_;
53  unsigned long long totsize;
54  unsigned long long writtensize;
55  unsigned long long writtenSizeLast;
56  unsigned int totevents;
57  unsigned int index_;
58  timeval startOfLastLumi;
59  bool firstLumi_;
60 };
61 
62 template <class Consumer>
65  edm::one::OutputModule<edm::one::WatchRuns, edm::one::WatchLuminosityBlocks>(ps),
66  templateConsumer_(new Consumer(ps)),
67  label_(ps.getUntrackedParameter<std::string>("ProductLabel", "source")),
68  instance_(ps.getUntrackedParameter<std::string>("ProductInstance", "")),
69  token_(consumes<FEDRawDataCollection>(edm::InputTag(label_, instance_))),
70  numEventsPerFile_(ps.getUntrackedParameter<unsigned int>("numEventsPerFile", 100)),
71  frdVersion_(ps.getUntrackedParameter<unsigned int>("frdVersion", 6)),
72  totsize(0LL),
73  writtensize(0LL),
74  writtenSizeLast(0LL),
75  totevents(0),
76  index_(0),
77  firstLumi_(true) {}
78 
79 template <class Consumer>
81 
82 template <class Consumer>
84  unsigned int ls = e.luminosityBlock();
85  if (totevents > 0 && totevents % numEventsPerFile_ == 0) {
86  index_++;
87  std::string filename = edm::Service<evf::EvFDaqDirector>()->getOpenRawFilePath(ls, index_);
88  std::string destinationDir = edm::Service<evf::EvFDaqDirector>()->buBaseRunDir();
89  templateConsumer_->initialize(destinationDir, filename, ls);
90  }
91  totevents++;
92  // serialize the FEDRawDataCollection into the format that we expect for
93  // FRDEventMsgView objects (may be better ways to do this)
95  e.getByToken(token_, fedBuffers);
96 
97  // determine the expected size of the FRDEvent IN BYTES !!!!!
98  assert(frdVersion_ <= FRDHeaderMaxVersion);
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  if (frdVersion_ <= 5) {
113  *bufPtr++ = (uint32)frdVersion_; // version number only
114  } else {
115  uint16 flags = 0;
116  if (!e.eventAuxiliary().isRealData())
118  *(uint16*)bufPtr = (uint16)(frdVersion_ & 0xffff);
119  *((uint16*)bufPtr + 1) = flags;
120  bufPtr++;
121  }
122  *bufPtr++ = (uint32)e.id().run();
123  *bufPtr++ = (uint32)e.luminosityBlock();
124  *bufPtr++ = (uint32)e.id().event();
125  if (frdVersion_ == 4)
126  *bufPtr++ = 0; //64-bit event id high part
127 
128  if (frdVersion_ < 3) {
129  uint32 fedsize[1024];
130  for (int idx = 0; idx < 1024; ++idx) {
131  FEDRawData singleFED = fedBuffers->FEDData(idx);
132  fedsize[idx] = singleFED.size();
133  //std::cout << "fed size " << singleFED.size()<< std::endl;
134  }
135  memcpy(bufPtr, fedsize, 1024 * sizeof(uint32));
136  bufPtr += 1024;
137  } else {
138  *bufPtr++ = expectedSize - headerSize;
139  *bufPtr++ = 0;
140  if (frdVersion_ <= 4)
141  *bufPtr++ = 0;
142  }
143  uint32* payloadPtr = bufPtr;
144  for (int idx = 0; idx < nFeds; ++idx) {
145  FEDRawData singleFED = fedBuffers->FEDData(idx);
146  if (singleFED.size() > 0) {
147  memcpy(bufPtr, singleFED.data(), singleFED.size());
148  bufPtr += singleFED.size() / 4;
149  }
150  }
151  if (frdVersion_ > 4) {
152  //crc32c checksum
153  uint32_t crc = 0;
154  *(payloadPtr - 1) = crc32c(crc, (const unsigned char*)payloadPtr, expectedSize - headerSize);
155  } else if (frdVersion_ >= 3) {
156  //adler32 checksum
157  uint32 adlera = 1;
158  uint32 adlerb = 0;
159  cms::Adler32((const char*)payloadPtr, expectedSize - headerSize, adlera, adlerb);
160  *(payloadPtr - 1) = (adlerb << 16) | adlera;
161  }
162 
163  // create the FRDEventMsgView and use the template consumer to write it out
164  FRDEventMsgView msg(workBuffer.get());
165  writtensize += msg.size();
166 
167  if (templateConsumer_->sharedMode())
168  templateConsumer_->doOutputEvent(workBuffer);
169  else
170  templateConsumer_->doOutputEvent(msg);
171 }
172 
173 template <class Consumer>
175  // edm::Service<evf::EvFDaqDirector>()->updateBuLock(1);
176  templateConsumer_->start();
177 }
178 
179 template <class Consumer>
181  templateConsumer_->stop();
182 }
183 
184 template <class Consumer>
186  index_ = 0;
187  std::string filename = edm::Service<evf::EvFDaqDirector>()->getOpenRawFilePath(ls.id().luminosityBlock(), index_);
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; */
200  writtenSizeLast = writtensize;
201  ::gettimeofday(&startOfLastLumi, nullptr);
202  //edm::Service<evf::EvFDaqDirector>()->writeLsStatisticsBU(ls.id().luminosityBlock(), totevents, totsize, elapsedusec);
203  } else
204  ::gettimeofday(&startOfLastLumi, nullptr);
205  totevents = 0;
206  totsize = 0LL;
207  firstLumi_ = false;
208 }
209 template <class Consumer>
211  // templateConsumer_->touchlock(ls.id().luminosityBlock(),basedir);
212  templateConsumer_->endOfLS(ls.id().luminosityBlock());
213 }
214 #endif
edm::one::OutputModule
Definition: OutputModule.h:30
FEDNumbering.h
eostools.ls
def ls(path, rec=False)
Definition: eostools.py:349
Handle.h
ModuleCallingContext.h
OutputModule.h
FRDEventMsgView
Definition: FRDEventMessage.h:137
RawEventOutputModuleForBU
Definition: RawEventOutputModuleForBU.h:24
FEDNumbering::lastFEDId
static constexpr int lastFEDId()
Definition: FEDNumbering.h:19
EvFBuildingThrottle.h
edm::EDGetTokenT< FEDRawDataCollection >
FEDRawDataCollection
Definition: FEDRawDataCollection.h:18
edm
HLT enums.
Definition: AlignableModifier.h:19
gather_cfg.cout
cout
Definition: gather_cfg.py:144
RawEventOutputModuleForBU::totsize
unsigned long long totsize
Definition: RawEventOutputModuleForBU.h:53
RawEventOutputModuleForBU::writtensize
unsigned long long writtensize
Definition: RawEventOutputModuleForBU.h:54
RawEventOutputModuleForBU::instance_
std::string instance_
Definition: RawEventOutputModuleForBU.h:49
RawEventOutputModuleForBU::beginLuminosityBlock
void beginLuminosityBlock(edm::LuminosityBlockForOutput const &) override
Definition: RawEventOutputModuleForBU.h:185
cms::cuda::assert
assert(be >=bs)
RawEventOutputModuleForBU::beginRun
void beginRun(edm::RunForOutput const &) override
Definition: RawEventOutputModuleForBU.h:174
charmTagsComputerCvsB_cfi.idx
idx
Definition: charmTagsComputerCvsB_cfi.py:108
RawEventOutputModuleForBU::write
void write(edm::EventForOutput const &e) override
Definition: RawEventOutputModuleForBU.h:83
mps_check.msg
tuple msg
Definition: mps_check.py:285
FRDEventMessage.h
L1DTConfigFromDB_cfi.LL
LL
Definition: L1DTConfigFromDB_cfi.py:289
EventForOutput.h
FEDRawData.h
edm::Handle
Definition: AssociativeIterator.h:50
FRDHeaderVersionSize
constexpr std::array< uint32, FRDHeaderMaxVersion+1 > FRDHeaderVersionSize
Definition: FRDEventMessage.h:129
FEDRawData::data
const unsigned char * data() const
Return a const pointer to the beginning of the data buffer.
Definition: FEDRawData.cc:24
uint32
unsigned int uint32
Definition: MsgTools.h:13
FEDRawData
Definition: FEDRawData.h:19
FRDEVENT_MASK_ISGENDATA
const uint16 FRDEVENT_MASK_ISGENDATA
Definition: FRDEventMessage.h:126
edm::LuminosityBlockForOutput
Definition: LuminosityBlockForOutput.h:40
fileCollector.now
now
Definition: fileCollector.py:207
edm::RunForOutput
Definition: RunForOutput.h:39
RawEventOutputModuleForBU::endRun
void endRun(edm::RunForOutput const &) override
Definition: RawEventOutputModuleForBU.h:180
cms::Adler32
void Adler32(char const *data, size_t len, uint32_t &a, uint32_t &b)
Definition: Adler32Calculator.cc:10
Service.h
FRDHeaderMaxVersion
constexpr size_t FRDHeaderMaxVersion
Definition: FRDEventMessage.h:128
RawEventOutputModuleForBU::uint32
unsigned int uint32
Definition: RawEventOutputModuleForBU.h:25
uint16
unsigned short uint16
Definition: MsgTools.h:12
corrVsCorr.filename
filename
Definition: corrVsCorr.py:123
RawEventOutputModuleForBU::~RawEventOutputModuleForBU
~RawEventOutputModuleForBU() override
Definition: RawEventOutputModuleForBU.h:80
EDGetToken.h
FEDRawDataCollection::FEDData
const FEDRawData & FEDData(int fedid) const
retrieve data for fed
Definition: FEDRawDataCollection.cc:19
RawEventOutputModuleForBU::token_
edm::EDGetTokenT< FEDRawDataCollection > token_
Definition: RawEventOutputModuleForBU.h:50
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
OutputModuleBase
crc32c
uint32_t crc32c(uint32_t crc, const unsigned char *buf, size_t len)
Definition: crc32c.cc:340
funct::true
true
Definition: Factorize.h:173
RawEventOutputModuleForBU::numEventsPerFile_
unsigned int numEventsPerFile_
Definition: RawEventOutputModuleForBU.h:51
RawEventOutputModuleForBU::firstLumi_
bool firstLumi_
Definition: RawEventOutputModuleForBU.h:59
HLT_2018_cff.InputTag
InputTag
Definition: HLT_2018_cff.py:79016
edm::ParameterSet
Definition: ParameterSet.h:36
RawEventOutputModuleForBU::totevents
unsigned int totevents
Definition: RawEventOutputModuleForBU.h:56
RawEventOutputModuleForBU::RawEventOutputModuleForBU
RawEventOutputModuleForBU(edm::ParameterSet const &ps)
Definition: RawEventOutputModuleForBU.h:63
edm::Service
Definition: Service.h:30
createfilelist.int
int
Definition: createfilelist.py:10
EvFDaqDirector.h
FEDRawDataCollection.h
crc32c.h
RawEventOutputModuleForBU::writeRun
void writeRun(const edm::RunForOutput &) override
Definition: RawEventOutputModuleForBU.h:41
FEDRawData::size
size_t size() const
Lenght of the data buffer in bytes.
Definition: FEDRawData.h:45
edm::EventForOutput
Definition: EventForOutput.h:50
std
Definition: JetResolutionObject.h:76
RawEventOutputModuleForBU::writeLuminosityBlock
void writeLuminosityBlock(const edm::LuminosityBlockForOutput &) override
Definition: RawEventOutputModuleForBU.h:42
RawEventOutputModuleForBU::writtenSizeLast
unsigned long long writtenSizeLast
Definition: RawEventOutputModuleForBU.h:55
RawEventOutputModuleForBU::templateConsumer_
std::unique_ptr< Consumer > templateConsumer_
Definition: RawEventOutputModuleForBU.h:47
Adler32Calculator.h
LuminosityBlockForOutput.h
RawEventOutputModuleForBU::label_
std::string label_
Definition: RawEventOutputModuleForBU.h:48
RawEventOutputModuleForBU::endLuminosityBlock
void endLuminosityBlock(edm::LuminosityBlockForOutput const &) override
Definition: RawEventOutputModuleForBU.h:210
HLT_2018_cff.flags
flags
Definition: HLT_2018_cff.py:11758
RawEventOutputModuleForBU::index_
unsigned int index_
Definition: RawEventOutputModuleForBU.h:57
RawEventOutputModuleForBU::startOfLastLumi
timeval startOfLastLumi
Definition: RawEventOutputModuleForBU.h:58
RawEventOutputModuleForBU::frdVersion_
unsigned int frdVersion_
Definition: RawEventOutputModuleForBU.h:52
MillePedeFileConverter_cfg.e
e
Definition: MillePedeFileConverter_cfg.py:37