CMS 3D CMS Logo

FRDStreamSource.cc
Go to the documentation of this file.
1 #include <iostream>
2 #include <memory>
3 #include <zlib.h>
4 
7 
12 
14 
16 
19 
21  : ProducerSourceFromFiles(pset, desc, true),
22  verifyAdler32_(pset.getUntrackedParameter<bool>("verifyAdler32", true)),
23  verifyChecksum_(pset.getUntrackedParameter<bool>("verifyChecksum", true)),
24  useL1EventID_(pset.getUntrackedParameter<bool>("useL1EventID", false)) {
26  endFileName_ = fileNames_.end();
28  produces<FEDRawDataCollection>();
29 }
30 
32  edm::TimeValue_t& theTime,
34  if (fin_.peek() == EOF) {
35  if (++itFileName_ == endFileName_) {
36  fin_.close();
37  return false;
38  }
39  if (!openFile(*itFileName_)) {
40  throw cms::Exception("FRDStreamSource::setRunAndEventInfo") << "could not open file " << *itFileName_;
41  }
42  }
43  //look for FRD header at beginning of the file and skip it
44  if (fin_.tellg() == 0) {
45  constexpr size_t buf_sz = sizeof(FRDFileHeaderIdentifier);
46  char hdr[sizeof(FRDFileHeader_v2)];
47  fin_.read(hdr, buf_sz);
48 
49  if (fin_.gcount() == 0)
50  throw cms::Exception("FRDStreamSource::setRunAndEventInfo")
51  << "Unable to read file or empty file" << *itFileName_;
52  else if (fin_.gcount() < (ssize_t)buf_sz) {
53  //no header, very small file, go to event parsing
54  fin_.seekg(0);
55  } else {
57  uint16_t frd_version = getFRDFileHeaderVersion(fileId->id_, fileId->version_);
58 
59  if (frd_version == 1) {
60  constexpr size_t buf_sz_cont = sizeof(FRDFileHeaderContent_v1);
61  fin_.read(hdr, buf_sz_cont);
63  if (fin_.gcount() != buf_sz_cont || fhContent->headerSize_ != sizeof(FRDFileHeader_v1))
64  throw cms::Exception("FRDStreamSource::setRunAndEventInfo")
65  << "Invalid FRD file header (size mismatch) in file " << *itFileName_;
66  } else if (frd_version == 2) {
67  constexpr size_t buf_sz_cont = sizeof(FRDFileHeaderContent_v2);
68  fin_.read(hdr, buf_sz_cont);
70  if (fin_.gcount() != buf_sz_cont || fhContent->headerSize_ != sizeof(FRDFileHeader_v2))
71  throw cms::Exception("FRDStreamSource::setRunAndEventInfo")
72  << "Invalid FRD file header (size mismatch) in file " << *itFileName_;
73  } else if (frd_version > 2) {
74  throw cms::Exception("FRDStreamSource::setRunAndEventInfo") << "Unknown header version " << frd_version;
75  } else {
76  //no header
77  fin_.seekg(0, fin_.beg);
78  }
79  }
80  }
81 
82  if (detectedFRDversion_ == 0) {
83  fin_.read((char*)&detectedFRDversion_, sizeof(uint16_t));
84  fin_.read((char*)&flags_, sizeof(uint16_t));
88  *((uint32_t*)(&buffer_[0])) = detectedFRDversion_;
89  fin_.read(&buffer_[0] + sizeof(uint32_t), FRDHeaderVersionSize[detectedFRDversion_] - sizeof(uint32_t));
90  assert(fin_.gcount() == FRDHeaderVersionSize[detectedFRDversion_] - (unsigned int)(sizeof(uint32_t)));
91  } else {
96  }
97 
98  std::unique_ptr<FRDEventMsgView> frdEventMsg(new FRDEventMsgView(&buffer_[0]));
99  if (useL1EventID_)
100  id = edm::EventID(frdEventMsg->run(), frdEventMsg->lumi(), frdEventMsg->event());
101 
102  const uint32_t totalSize = frdEventMsg->size();
103  if (totalSize > buffer_.size()) {
104  buffer_.resize(totalSize);
105  }
106  if (totalSize > FRDHeaderVersionSize[detectedFRDversion_]) {
109  if (fin_.gcount() != totalSize - FRDHeaderVersionSize[detectedFRDversion_]) {
110  throw cms::Exception("FRDStreamSource::setRunAndEventInfo") << "premature end of file " << *itFileName_;
111  }
112  frdEventMsg = std::make_unique<FRDEventMsgView>(&buffer_[0]);
113  }
114 
115  if (verifyChecksum_ && frdEventMsg->version() >= 5) {
116  uint32_t crc = 0;
117  crc = crc32c(crc, (const unsigned char*)frdEventMsg->payload(), frdEventMsg->eventSize());
118  if (crc != frdEventMsg->crc32c()) {
119  throw cms::Exception("FRDStreamSource::getNextEvent") << "Found a wrong crc32c checksum: expected 0x" << std::hex
120  << frdEventMsg->crc32c() << " but calculated 0x" << crc;
121  }
122  } else if (verifyAdler32_ && frdEventMsg->version() >= 3) {
123  uint32_t adler = adler32(0L, Z_NULL, 0);
124  adler = adler32(adler, (Bytef*)frdEventMsg->payload(), frdEventMsg->eventSize());
125 
126  if (adler != frdEventMsg->adler32()) {
127  throw cms::Exception("FRDStreamSource::setRunAndEventInfo")
128  << "Found a wrong Adler32 checksum: expected 0x" << std::hex << frdEventMsg->adler32() << " but calculated 0x"
129  << adler;
130  }
131  }
132 
133  rawData_ = std::make_unique<FEDRawDataCollection>();
134 
135  uint32_t eventSize = frdEventMsg->eventSize();
136  unsigned char* event = (unsigned char*)frdEventMsg->payload();
137  bool foundTCDSFED = false;
138  bool foundGTPFED = false;
139 
140  while (eventSize > 0) {
141  assert(eventSize >= FEDTrailer::length);
142  eventSize -= FEDTrailer::length;
143  const FEDTrailer fedTrailer(event + eventSize);
144  const uint32_t fedSize = fedTrailer.fragmentLength() << 3; //trailer length counts in 8 bytes
145  assert(eventSize >= fedSize - FEDHeader::length);
146  eventSize -= (fedSize - FEDHeader::length);
147  const FEDHeader fedHeader(event + eventSize);
148  const uint16_t fedId = fedHeader.sourceID();
150  throw cms::Exception("FedRawDataInputSource::fillFEDRawDataCollection") << "Out of range FED ID : " << fedId;
151  }
153  foundTCDSFED = true;
154  tcds::Raw_v1 const* tcds = reinterpret_cast<tcds::Raw_v1 const*>(event + eventSize + FEDHeader::length);
155  id = edm::EventID(frdEventMsg->run(), tcds->header.lumiSection, tcds->header.eventNumber);
156  eType = static_cast<edm::EventAuxiliary::ExperimentType>(fedHeader.triggerType());
157  theTime = static_cast<edm::TimeValue_t>(((uint64_t)tcds->bst.gpstimehigh << 32) | tcds->bst.gpstimelow);
158  }
159 
160  if (fedId == FEDNumbering::MINTriggerGTPFEDID && !foundTCDSFED) {
161  foundGTPFED = true;
162  const bool GTPEvmBoardSense = evf::evtn::evm_board_sense(event + eventSize, fedSize);
163  if (!useL1EventID_) {
164  if (GTPEvmBoardSense)
165  id = edm::EventID(frdEventMsg->run(), frdEventMsg->lumi(), evf::evtn::get(event + eventSize, true));
166  else
167  id = edm::EventID(frdEventMsg->run(), frdEventMsg->lumi(), evf::evtn::get(event + eventSize, false));
168  }
169  //evf::evtn::evm_board_setformat(fedSize);
170  const uint64_t gpsl = evf::evtn::getgpslow(event + eventSize);
171  const uint64_t gpsh = evf::evtn::getgpshigh(event + eventSize);
172  theTime = static_cast<edm::TimeValue_t>((gpsh << 32) + gpsl);
173  }
174 
175  //take event ID from GTPE FED
176  if (fedId == FEDNumbering::MINTriggerEGTPFEDID && !foundGTPFED && !foundTCDSFED && !useL1EventID_) {
177  if (evf::evtn::gtpe_board_sense(event + eventSize)) {
178  id = edm::EventID(frdEventMsg->run(), frdEventMsg->lumi(), evf::evtn::gtpe_get(event + eventSize));
179  }
180  }
181  FEDRawData& fedData = rawData_->FEDData(fedId);
182  fedData.resize(fedSize);
183  memcpy(fedData.data(), event + eventSize, fedSize);
184  }
185  assert(eventSize == 0);
186 
187  return true;
188 }
189 
191 
193  std::cout << " open file.. " << fileName << std::endl;
194  fin_.close();
195  fin_.clear();
196  size_t pos = fileName.find(':');
197  if (pos != std::string::npos) {
198  std::string prefix = fileName.substr(0, pos);
199  if (prefix != "file")
200  return false;
201  pos++;
202  } else
203  pos = 0;
204 
205  fin_.open(fileName.substr(pos).c_str(), std::ios::in | std::ios::binary);
206  return fin_.is_open();
207 }
208 
210 // define this class as an input source //
213 
214 // Keep old naming from DAQ1
uint16_t getFRDFileHeaderVersion(const std::array< uint8_t, 4 > &id, const std::array< uint8_t, 4 > &version)
Definition: FRDFileHeader.h:80
uint8_t triggerType() const
Event Trigger type identifier.
Definition: FEDHeader.cc:13
unsigned int getgpshigh(const unsigned char *)
std::ifstream fin_
bool gtpe_board_sense(const unsigned char *p)
constexpr size_t FRDHeaderMaxVersion
std::vector< std::string > fileNames_
static const uint32_t length
Definition: FEDTrailer.h:57
std::unique_ptr< FEDRawDataCollection > rawData_
unsigned int get(const unsigned char *, bool)
static const uint32_t length
Definition: FEDHeader.h:54
uint16_t sourceID() const
Identifier of the FED.
Definition: FEDHeader.cc:19
void produce(edm::Event &e) override
std::vector< char > buffer_
const bool useL1EventID_
assert(be >=bs)
uint32_t fragmentLength() const
The length of the event fragment counted in 64-bit words including header and trailer.
Definition: FEDTrailer.cc:13
std::array< uint8_t, 4 > id_
Definition: FRDFileHeader.h:29
Definition: TCDSRaw.h:16
#define DEFINE_FWK_INPUT_SOURCE(type)
void resize(size_t newsize)
Definition: FEDRawData.cc:28
bool setRunAndEventInfo(edm::EventID &id, edm::TimeValue_t &theTime, edm::EventAuxiliary::ExperimentType &eType) override
std::array< uint8_t, 4 > version_
Definition: FRDFileHeader.h:30
bool openFile(const std::string &fileName)
unsigned long long TimeValue_t
Definition: Timestamp.h:21
std::vector< std::string >::const_iterator itFileName_
bool evm_board_sense(const unsigned char *p, size_t size)
const bool verifyChecksum_
uint32_t crc32c(uint32_t crc, const unsigned char *buf, size_t len)
Definition: crc32c.cc:340
std::vector< std::string >::const_iterator endFileName_
unsigned long long uint64_t
Definition: Time.h:13
const bool verifyAdler32_
std::vector< std::string > fileNames(unsigned iCatalog) const
Definition: FromFiles.h:22
const unsigned char * data() const
Return a const pointer to the beginning of the data buffer.
Definition: FEDRawData.cc:24
unsigned int gtpe_get(const unsigned char *)
uint16_t detectedFRDversion_
constexpr std::array< uint32, FRDHeaderMaxVersion+1 > FRDHeaderVersionSize
unsigned int getgpslow(const unsigned char *)
def move(src, dest)
Definition: eostools.py:511
Definition: event.py:1
FRDStreamSource(edm::ParameterSet const &pset, edm::InputSourceDescription const &desc)