CMS 3D CMS Logo

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