CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
FRDStreamSource.cc
Go to the documentation of this file.
1 #include <zlib.h>
2 
4 
7 
12 
14 
15 
17  edm::InputSourceDescription const& desc)
18  : ProducerSourceFromFiles(pset,desc,true),
19  verifyAdler32_(pset.getUntrackedParameter<bool> ("verifyAdler32", true)),
20  useL1EventID_(pset.getUntrackedParameter<bool> ("useL1EventID", false))
21 {
22  itFileName_=fileNames().begin();
24  produces<FEDRawDataCollection>();
25 }
26 
27 
29 {
30  if ( fin_.peek() == EOF ) {
31  if ( ++itFileName_==fileNames().end() ) {
32  fin_.close();
33  return false;
34  }
35  if ( ! openFile(*itFileName_) ) {
36  throw cms::Exception("FRDStreamSource::setRunAndEventInfo") <<
37  "could not open file " << *itFileName_;
38  }
39  }
40 
41  const uint32_t headerSize[4]={0,2*sizeof(uint32),(4+1024)*sizeof(uint32_t),7*sizeof(uint32_t)};//FRD header size per version
42  if ( detectedFRDversion_==0) {
43  fin_.read((char*)&detectedFRDversion_,sizeof(uint32_t));
45  if ( buffer_.size() < headerSize[detectedFRDversion_] )
46  buffer_.resize(headerSize[detectedFRDversion_]);
47  *((uint32_t*)(&buffer_[0]))=detectedFRDversion_;
48  fin_.read(&buffer_[0] + sizeof(uint32_t),headerSize[detectedFRDversion_]-sizeof(uint32_t));
49  assert( fin_.gcount() == headerSize[detectedFRDversion_]-(unsigned int)(sizeof(uint32_t) ));
50  }
51  else {
52  if ( buffer_.size() < headerSize[detectedFRDversion_] )
53  buffer_.resize(headerSize[detectedFRDversion_]);
54  fin_.read(&buffer_[0],headerSize[detectedFRDversion_]);
55  assert( fin_.gcount() == headerSize[detectedFRDversion_] );
56  }
57 
58  std::unique_ptr<FRDEventMsgView> frdEventMsg(new FRDEventMsgView(&buffer_[0]));
59  if (useL1EventID_)
60  id = edm::EventID(frdEventMsg->run(), frdEventMsg->lumi(), frdEventMsg->event());
61 
62  const uint32_t totalSize = frdEventMsg->size();
63  if ( totalSize > buffer_.size() ) {
64  buffer_.resize(totalSize);
65  }
66  if ( totalSize > headerSize[detectedFRDversion_] ) {
67  fin_.read(&buffer_[0]+headerSize[detectedFRDversion_],totalSize-headerSize[detectedFRDversion_]);
68  if ( fin_.gcount() != totalSize-headerSize[detectedFRDversion_] ) {
69  throw cms::Exception("FRDStreamSource::setRunAndEventInfo") <<
70  "premature end of file " << *itFileName_;
71  }
72  frdEventMsg.reset(new FRDEventMsgView(&buffer_[0]));
73  }
74 
75  if ( verifyAdler32_ && frdEventMsg->version() >= 3 )
76  {
77  uint32_t adler = adler32(0L,Z_NULL,0);
78  adler = adler32(adler,(Bytef*)frdEventMsg->payload(),frdEventMsg->eventSize());
79 
80  if ( adler != frdEventMsg->adler32() ) {
81  throw cms::Exception("FRDStreamSource::setRunAndEventInfo") <<
82  "Found a wrong Adler32 checksum: expected 0x" << std::hex << frdEventMsg->adler32() <<
83  " but calculated 0x" << adler;
84  }
85  }
86 
87  rawData_.reset(new FEDRawDataCollection());
88 
89  uint32_t eventSize = frdEventMsg->eventSize();
90  char* event = (char*)frdEventMsg->payload();
91  bool foundTCDSFED=false;
92  bool foundGTPFED=false;
93 
94 
95  while (eventSize > 0) {
96  eventSize -= sizeof(fedt_t);
97  const fedt_t* fedTrailer = (fedt_t*) (event + eventSize);
98  const uint32_t fedSize = FED_EVSZ_EXTRACT(fedTrailer->eventsize) << 3; //trailer length counts in 8 bytes
99  eventSize -= (fedSize - sizeof(fedh_t));
100  const fedh_t* fedHeader = (fedh_t *) (event + eventSize);
101  const uint16_t fedId = FED_SOID_EXTRACT(fedHeader->sourceid);
102  if (fedId == FEDNumbering::MINTCDSuTCAFEDID) {
103  foundTCDSFED=true;
104  evf::evtn::TCDSRecord record((unsigned char *)(event + eventSize ));
105  id = edm::EventID(frdEventMsg->run(),record.getHeader().getData().header.lumiSection,
106  record.getHeader().getData().header.eventNumber);
107  //evf::evtn::evm_board_setformat(fedSize);
108  uint64_t gpsh = record.getBST().getBST().gpstimehigh;
109  uint32_t gpsl = record.getBST().getBST().gpstimelow;
110  theTime = static_cast<edm::TimeValue_t>((gpsh << 32) + gpsl);
111  }
112 
113  if (fedId == FEDNumbering::MINTriggerGTPFEDID && !foundTCDSFED) {
114  foundGTPFED=true;
115  const bool GTPEvmBoardSense=evf::evtn::evm_board_sense((unsigned char*) fedHeader,fedSize);
116  if (!useL1EventID_) {
117  if (GTPEvmBoardSense)
118  id = edm::EventID(frdEventMsg->run(), frdEventMsg->lumi(), evf::evtn::get((unsigned char*) fedHeader,true));
119  else
120  id = edm::EventID(frdEventMsg->run(), frdEventMsg->lumi(), evf::evtn::get((unsigned char*) fedHeader,false));
121  }
122  //evf::evtn::evm_board_setformat(fedSize);
123  const uint64_t gpsl = evf::evtn::getgpslow((unsigned char*) fedHeader);
124  const uint64_t gpsh = evf::evtn::getgpshigh((unsigned char*) fedHeader);
125  theTime = static_cast<edm::TimeValue_t>((gpsh << 32) + gpsl);
126  }
127 
128 
129 
130  //take event ID from GTPE FED
131  if (fedId == FEDNumbering::MINTriggerEGTPFEDID && !foundGTPFED && !foundTCDSFED && !useL1EventID_) {
132  if (evf::evtn::gtpe_board_sense((unsigned char*)fedHeader)) {
133  id = edm::EventID(frdEventMsg->run(), frdEventMsg->lumi(), evf::evtn::gtpe_get((unsigned char*) fedHeader));
134  }
135  }
136  FEDRawData& fedData = rawData_->FEDData(fedId);
137  fedData.resize(fedSize);
138  memcpy(fedData.data(), event + eventSize, fedSize);
139  }
140  assert(eventSize == 0);
141 
142  return true;
143 }
144 
145 
147  e.put(rawData_);
148 }
149 
150 
152 {
153  std::cout << " open file.. " << fileName << std::endl;
154  fin_.close();
155  fin_.clear();
156  size_t pos = fileName.find(':');
157  if (pos!=std::string::npos) {
158  std::string prefix = fileName.substr(0,pos);
159  if (prefix!="file") return false;
160  pos++;
161  }
162  else pos=0;
163 
164  fin_.open(fileName.substr(pos).c_str(),std::ios::in|std::ios::binary);
165  return fin_.is_open();
166 }
167 
168 
170 // define this class as an input source //
173 
174 // Keep old naming from DAQ1
unsigned int detectedFRDversion_
unsigned int getgpshigh(const unsigned char *)
TCDSBST & getBST()
Definition: FED1024.h:129
std::ifstream fin_
TCDSHeader & getHeader()
Definition: FED1024.h:127
bool gtpe_board_sense(const unsigned char *p)
struct fedh_struct fedh_t
JetCorrectorParameters::Record record
Definition: classes.h:7
unsigned int get(const unsigned char *, bool)
std::vector< char > buffer_
const bool useL1EventID_
virtual void produce(edm::Event &e)
std::auto_ptr< FEDRawDataCollection > rawData_
unsigned int sourceid
Definition: fed_header.h:32
std::vector< std::string > const & fileNames() const
Definition: FromFiles.h:22
uint32_t gpstimehigh
Definition: FED1024.h:96
#define DEFINE_FWK_INPUT_SOURCE(type)
const tcdsheader & getData()
Definition: FED1024.h:64
void resize(size_t newsize)
Definition: FEDRawData.cc:32
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:116
struct fedt_struct fedt_t
#define FED_EVSZ_EXTRACT(a)
Definition: fed_trailer.h:36
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)
unsigned int uint32
Definition: MsgTools.h:13
struct evf::evtn::TCDSHeader::tcdsheader::@296 header
unsigned long long uint64_t
Definition: Time.h:15
const bool verifyAdler32_
unsigned int eventsize
Definition: fed_trailer.h:33
const bst & getBST()
Definition: FED1024.h:112
EventNumber_t event() const
const unsigned char * data() const
Return a const pointer to the beginning of the data buffer.
Definition: FEDRawData.cc:28
tuple cout
Definition: gather_cfg.py:121
unsigned int gtpe_get(const unsigned char *)
volatile std::atomic< bool > shutdown_flag false
unsigned int getgpslow(const unsigned char *)
#define FED_SOID_EXTRACT(a)
Definition: fed_header.h:53
virtual bool setRunAndEventInfo(edm::EventID &id, edm::TimeValue_t &theTime)
FRDStreamSource(edm::ParameterSet const &pset, edm::InputSourceDescription const &desc)