CMS 3D CMS Logo

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