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 
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())
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)
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)
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();
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 }
ConfigurationDescriptions.h
FEDNumbering.h
eostools.ls
def ls(path, rec=False)
Definition: eostools.py:349
Handle.h
SiPixelPI::one
Definition: SiPixelPayloadInspectorHelper.h:39
MessageLogger.h
FRDEventMsgView
Definition: FRDEventMessage.h:137
FRDOutputModule::frdVersion_
const uint32_t frdVersion_
Definition: FRDOutputModule.h:37
FEDNumbering::lastFEDId
static constexpr int lastFEDId()
Definition: FEDNumbering.h:19
FEDRawDataCollection
Definition: FEDRawDataCollection.h:18
edm
HLT enums.
Definition: AlignableModifier.h:19
FRDOutputModule::endLuminosityBlock
void endLuminosityBlock(edm::LuminosityBlockForOutput const &) override
Definition: FRDOutputModule.cc:178
HLT_FULL_cff.InputTag
InputTag
Definition: HLT_FULL_cff.py:89301
edm::ParameterSetDescription
Definition: ParameterSetDescription.h:52
FRDOutputModule::beginLuminosityBlock
void beginLuminosityBlock(edm::LuminosityBlockForOutput const &) override
Definition: FRDOutputModule.cc:133
FRDOutputModule::fileWritten_
bool fileWritten_
Definition: FRDOutputModule.h:49
cms::cuda::assert
assert(be >=bs)
FRDOutputModule::perFileEventCount_
uint32_t perFileEventCount_
Definition: FRDOutputModule.h:46
mps_check.msg
tuple msg
Definition: mps_check.py:285
FRDEventMessage.h
edm::LogInfo
Log< level::Info, false > LogInfo
Definition: MessageLogger.h:125
MillePedeFileConverter_cfg.fileName
fileName
Definition: MillePedeFileConverter_cfg.py:32
edm::Handle< FEDRawDataCollection >
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
heavyIonCSV_trainingSettings.idx
idx
Definition: heavyIonCSV_trainingSettings.py:5
edm::LuminosityBlockForOutput
Definition: LuminosityBlockForOutput.h:40
contentValuesCheck.ss
ss
Definition: contentValuesCheck.py:33
FRDFileHeader.h
cms::Adler32
void Adler32(char const *data, size_t len, uint32_t &a, uint32_t &b)
Definition: Adler32Calculator.cc:10
FRDOutputModule::adlera_
uint32_t adlera_
Definition: FRDOutputModule.h:43
FRDOutputModule::frdFileVersion_
const uint32_t frdFileVersion_
Definition: FRDOutputModule.h:38
FRDHeaderMaxVersion
constexpr size_t FRDHeaderMaxVersion
Definition: FRDEventMessage.h:128
uint16
unsigned short uint16
Definition: MsgTools.h:12
EDGetToken.h
FRDOutputModule::finishFileWrite
void finishFileWrite(int ls)
Definition: FRDOutputModule.cc:183
FRDOutputModule::~FRDOutputModule
~FRDOutputModule() override
Definition: FRDOutputModule.cc:34
FEDRawDataCollection::FEDData
const FEDRawData & FEDData(int fedid) const
retrieve data for fed
Definition: FEDRawDataCollection.cc:19
FRDOutputModule::fileName_
std::string fileName_
Definition: FRDOutputModule.h:40
edm::ConfigurationDescriptions
Definition: ConfigurationDescriptions.h:28
OutputModuleBase
crc32c
uint32_t crc32c(uint32_t crc, const unsigned char *buf, size_t len)
Definition: crc32c.cc:340
edm::ParameterSet
Definition: ParameterSet.h:47
createfilelist.int
int
Definition: createfilelist.py:10
edm::LuminosityBlockID::luminosityBlock
LuminosityBlockNumber_t luminosityBlock() const
Definition: LuminosityBlockID.h:42
edm::LuminosityBlockForOutput::id
LuminosityBlockID const & id() const
Definition: LuminosityBlockForOutput.h:53
FRDOutputModule::filePrefix_
std::string filePrefix_
Definition: FRDOutputModule.h:39
crc32c.h
AlCaHLTBitMon_QueryRunRegistry.string
string string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
FEDRawData::size
size_t size() const
Lenght of the data buffer in bytes.
Definition: FEDRawData.h:45
FRDOutputModule::fillDescriptions
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
Definition: FRDOutputModule.cc:36
FRDFileHeader_v1
Definition: FRDFileHeader.h:24
FRDOutputModule.h
edm::EventForOutput
Definition: EventForOutput.h:50
submitPVResolutionJobs.desc
string desc
Definition: submitPVResolutionJobs.py:251
FRDOutputModule::outfd_
int outfd_
Definition: FRDOutputModule.h:42
std
Definition: JetResolutionObject.h:76
FRDOutputModule::adlerb_
uint32_t adlerb_
Definition: FRDOutputModule.h:44
Exception
Definition: hltDiff.cc:245
FRDOutputModule::token_
const edm::EDGetTokenT< FEDRawDataCollection > token_
Definition: FRDOutputModule.h:35
Adler32Calculator.h
FRDOutputModule::write
void write(edm::EventForOutput const &e) override
Definition: FRDOutputModule.cc:45
edm::LuminosityBlockForOutput::run
RunNumber_t run() const
Definition: LuminosityBlockForOutput.h:55
FRDOutputModule::perFileSize_
uint64_t perFileSize_
Definition: FRDOutputModule.h:47
HLT_FULL_cff.flags
flags
Definition: HLT_FULL_cff.py:13168
edm::InputTag
Definition: InputTag.h:15
FRDOutputModule::FRDOutputModule
FRDOutputModule(edm::ParameterSet const &ps)
Definition: FRDOutputModule.cc:25
edm::ConfigurationDescriptions::addWithDefaultLabel
void addWithDefaultLabel(ParameterSetDescription const &psetDescription)
Definition: ConfigurationDescriptions.cc:87
MillePedeFileConverter_cfg.e
e
Definition: MillePedeFileConverter_cfg.py:37