CMS 3D CMS Logo

FRDOutputModule.cc
Go to the documentation of this file.
2 
3 // system headers
4 #include <unistd.h>
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <fcntl.h>
8 
9 // C++ headers
10 #include <cstdio>
11 #include <fstream>
12 #include <memory>
13 #include <vector>
14 
24 
25 using namespace edm::streamer;
26 
29  edm::one::OutputModule<edm::one::WatchLuminosityBlocks>(ps),
30  token_(consumes<FEDRawDataCollection>(ps.getParameter<edm::InputTag>("source"))),
31  frdVersion_(ps.getUntrackedParameter<unsigned int>("frdVersion")),
32  frdFileVersion_(ps.getUntrackedParameter<unsigned int>("frdFileVersion")),
33  filePrefix_(ps.getUntrackedParameter<std::string>("filePrefix")),
34  fileName_(ps.getUntrackedParameter<std::string>("fileName")) {}
35 
37 
40  desc.add<edm::InputTag>("source", edm::InputTag("rawDataCollector"));
41  desc.addUntracked<unsigned int>("frdFileVersion", 1), desc.addUntracked<unsigned int>("frdVersion", 6);
42  desc.addUntracked<std::string>("filePrefix", "");
43  desc.addUntracked<std::string>("fileName", "");
44  descriptions.addWithDefaultLabel(desc);
45 }
46 
48  // serialize the FEDRawDataCollection into the format that we expect for
49  // FRDEventMsgView objects (may be better ways to do this)
51  e.getByToken(token_, fedBuffers);
52 
53  // determine the expected size of the FRDEvent IN BYTES !!!!!
55  int headerSize = FRDHeaderVersionSize[frdVersion_];
56  int expectedSize = headerSize;
57  int nFeds = frdVersion_ < 3 ? 1024 : FEDNumbering::lastFEDId() + 1;
58 
59  for (int idx = 0; idx < nFeds; ++idx) {
60  FEDRawData singleFED = fedBuffers->FEDData(idx);
61  expectedSize += singleFED.size();
62  }
63 
64  // build the FRDEvent into a temporary buffer
65  std::unique_ptr<std::vector<unsigned char>> workBuffer(
66  std::make_unique<std::vector<unsigned char>>(expectedSize + 256));
67  uint32* bufPtr = (uint32*)(workBuffer.get()->data());
68  if (frdVersion_ <= 5) {
69  *bufPtr++ = (uint32)frdVersion_; // version number only
70  } else {
71  uint16 flags = 0;
72  if (!e.eventAuxiliary().isRealData())
74  *(uint16*)bufPtr = (uint16)(frdVersion_ & 0xffff);
75  *((uint16*)bufPtr + 1) = flags;
76  bufPtr++;
77  }
78  *bufPtr++ = (uint32)e.id().run();
79  *bufPtr++ = (uint32)e.luminosityBlock();
80  *bufPtr++ = (uint32)e.id().event();
81  if (frdVersion_ == 4)
82  *bufPtr++ = 0; //64-bit event id high part
83 
84  if (frdVersion_ < 3) {
85  uint32 fedsize[1024];
86  for (int idx = 0; idx < 1024; ++idx) {
87  FEDRawData singleFED = fedBuffers->FEDData(idx);
88  fedsize[idx] = singleFED.size();
89  //std::cout << "fed size " << singleFED.size()<< std::endl;
90  }
91  memcpy(bufPtr, fedsize, 1024 * sizeof(uint32));
92  bufPtr += 1024;
93  } else {
94  *bufPtr++ = expectedSize - headerSize;
95  *bufPtr++ = 0;
96  if (frdVersion_ <= 4)
97  *bufPtr++ = 0;
98  }
99  uint32* payloadPtr = bufPtr;
100  for (int idx = 0; idx < nFeds; ++idx) {
101  FEDRawData singleFED = fedBuffers->FEDData(idx);
102  if (singleFED.size() > 0) {
103  memcpy(bufPtr, singleFED.data(), singleFED.size());
104  bufPtr += singleFED.size() / 4;
105  }
106  }
107  if (frdVersion_ > 4) {
108  //crc32c checksum
109  uint32_t crc = 0;
110  *(payloadPtr - 1) = crc32c(crc, (const unsigned char*)payloadPtr, expectedSize - headerSize);
111  } else if (frdVersion_ >= 3) {
112  //adler32 checksum
113  uint32 adlera = 1;
114  uint32 adlerb = 0;
115  cms::Adler32((const char*)payloadPtr, expectedSize - headerSize, adlera, adlerb);
116  *(payloadPtr - 1) = (adlerb << 16) | adlera;
117  }
118 
119  // create the FRDEventMsgView and use the template consumer to write it out
120  FRDEventMsgView msg(workBuffer.get()->data());
121 
122  //write
123  ssize_t retval = ::write(outfd_, (void*)msg.startAddress(), msg.size());
124 
125  if ((unsigned)retval != msg.size()) {
126  throw cms::Exception("FRDOutputModule", "write")
127  << "Error writing FED Raw Data event data to " << fileName_ << ". Possibly the output disk "
128  << "is full?" << std::endl;
129  }
130 
132  perFileSize_ += msg.size();
133 }
134 
136  int ls = lumiBlock.id().luminosityBlock();
137 
138  if (outfd_ != -1)
139  finishFileWrite(lumiBlock.run(), ls);
140 
141  if (fileWritten_)
142  throw cms::Exception("RawEventFileWriterForBU", "beginLuminosityBlock")
143  << "Multiple lumisections not supported in the same FRD file!";
144 
146  if (fileName_.empty()) {
147  std::stringstream ss;
148  ss << (filePrefix_.empty() ? "" : filePrefix_ + "_") << "run" << std::setfill('0') << std::setw(6)
149  << lumiBlock.run() << "_ls" << std::setfill('0') << std::setw(4) << ls << "_index000000.raw";
150  fileName = ss.str();
151  } else {
152  //use exact filename (will be overwritten by last LS content if input contains multiple lumisections)
154  }
155 
156  outfd_ = open(fileName.c_str(), O_WRONLY | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO);
157  ftruncate(outfd_, 0);
158 
159  if (outfd_ < 0) { //attention here... it may happen that outfd_ is *not* set (e.g. missing initialize call...)
160  throw cms::Exception("RawEventFileWriterForBU", "beginLuminosityBlock")
161  << "Error opening FED Raw Data event output file: " << fileName << ": " << strerror(errno) << "\n";
162  }
163  edm::LogInfo("RawEventFileWriterForBU") << " Opened " << fileName;
164 
165  perFileEventCount_ = 0;
166  perFileSize_ = 0;
167 
168  adlera_ = 1;
169  adlerb_ = 0;
170 
171  if (frdFileVersion_ == 1) {
172  //reserve space for file header
173  ftruncate(outfd_, sizeof(FRDFileHeader_v1));
174  lseek(outfd_, sizeof(FRDFileHeader_v1), SEEK_SET);
175  perFileSize_ = sizeof(FRDFileHeader_v1);
176  } else if (frdFileVersion_ == 2) {
177  ftruncate(outfd_, sizeof(FRDFileHeader_v2));
178  lseek(outfd_, sizeof(FRDFileHeader_v2), SEEK_SET);
179  perFileSize_ = sizeof(FRDFileHeader_v2);
180  } else
181  throw cms::Exception("RawEventFileWriterForBU", "beginLuminosityBlock")
182  << "Unsupported FRD version " << frdFileVersion_;
183 }
184 
186  finishFileWrite(lumiBlock.run(), lumiBlock.id().luminosityBlock());
187 }
188 
189 void FRDOutputModule::finishFileWrite(unsigned int run, int ls) {
190  if (outfd_ == -1)
191  return;
192 
193  if (frdFileVersion_ == 1) {
194  //rewind
195  lseek(outfd_, 0, SEEK_SET);
196  FRDFileHeader_v1 frdFileHeader(perFileEventCount_, (uint32_t)ls, perFileSize_);
197  ::write(outfd_, (char*)&frdFileHeader, sizeof(FRDFileHeader_v1));
198  } else if (frdFileVersion_ == 2) {
199  lseek(outfd_, 0, SEEK_SET);
200  FRDFileHeader_v2 frdFileHeader(0, perFileEventCount_, (uint32_t)run, (uint32_t)ls, perFileSize_);
201  ::write(outfd_, (char*)&frdFileHeader, sizeof(FRDFileHeader_v2));
202  }
203  close(outfd_);
204  outfd_ = -1;
205  if (!fileName_.empty())
206  fileWritten_ = true;
207 
208  edm::LogInfo("FRDOutputModule") << "closed RAW input file";
209 }
void addWithDefaultLabel(ParameterSetDescription const &psetDescription)
LuminosityBlockNumber_t luminosityBlock() const
void finishFileWrite(unsigned int run, int ls)
const uint32_t frdFileVersion_
uint32_t perFileEventCount_
constexpr std::array< uint32, FRDHeaderMaxVersion+1 > FRDHeaderVersionSize
uint64_t perFileSize_
constexpr size_t FRDHeaderMaxVersion
size_t size() const
Lenght of the data buffer in bytes.
Definition: FEDRawData.h:48
FRDOutputModule(edm::ParameterSet const &ps)
void endLuminosityBlock(edm::LuminosityBlockForOutput const &) override
assert(be >=bs)
~FRDOutputModule() override
void beginLuminosityBlock(edm::LuminosityBlockForOutput const &) override
std::string filePrefix_
const uint16 FRDEVENT_MASK_ISGENDATA
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
const FEDRawData & FEDData(int fedid) const
retrieve data for fed
std::string fileName_
uint32_t crc32c(uint32_t crc, const unsigned char *buf, size_t len)
Definition: crc32c.cc:340
Log< level::Info, false > LogInfo
void Adler32(char const *data, size_t len, uint32_t &a, uint32_t &b)
def ls(path, rec=False)
Definition: eostools.py:349
const edm::EDGetTokenT< FEDRawDataCollection > token_
LuminosityBlockID const & id() const
tuple msg
Definition: mps_check.py:286
const uint32_t frdVersion_
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
unsigned int uint32
Definition: MsgTools.h:14
static constexpr int lastFEDId()
Definition: FEDNumbering.h:19
void write(edm::EventForOutput const &e) override