CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ErrorStreamSource.cc
Go to the documentation of this file.
1 //
3 // ErrorStreamSource
4 // -----------------
5 //
6 // 05/23/2008 Philipp Schieferdecker <philipp.schieferdecker@cern.ch>
8 
9 
16 
18 
22 
24 
25 #include <unistd.h>
26 #include <string>
27 #include <vector>
28 #include <fstream>
29 #include <cstring>
30 #include "interface/shared/fed_header.h"
31 #include "interface/shared/fed_trailer.h"
32 
33 
34 namespace errorstreamsource{
36  //static unsigned int gtpeId_ = FEDNumbering::MINTriggerEGTPFEDID; // unused
37 }
38 
39 
41 {
42 public:
43  // construction/destruction
45  edm::InputSourceDescription const& desc);
46  virtual ~ErrorStreamSource();
47 
48 private:
49  // member functions
50  void setRunAndEventInfo();
51  bool produce(edm::Event& e);
52 
53  void beginRun(edm::Run& r) {;}
54  void endRun(edm::Run& r) {;}
57 
58  bool openFile(const std::string& fileName);
59 
60 
61 private:
62  // member data
63  std::vector<std::string>::const_iterator itFileName_;
64  std::ifstream fin_;
65 };
66 
67 
68 using namespace std;
69 
70 
72 // construction/destruction
74 
75 //______________________________________________________________________________
77  edm::InputSourceDescription const& desc)
78  : ExternalInputSource(pset,desc)
79 {
80  itFileName_=fileNames().begin();
82  produces<FEDRawDataCollection>();
83 }
84 
85 
86 //______________________________________________________________________________
88 {
89 
90 }
91 
92 
94 // implementation of member functions
96 
97 //______________________________________________________________________________
99 {
100  uint32_t version(1);
101  uint32_t runNumber(0);
102  uint32_t lumiNumber(1);
103  uint32_t evtNumber(0);
104  bool status;
105  status = fin_.read((char*)&runNumber,sizeof(uint32_t));
106  if (runNumber < 32) {
107  version = runNumber;
108  status = fin_.read((char*)&runNumber,sizeof(uint32_t));
109  }
110  if (version >= 2) {
111  status = fin_.read((char*)&lumiNumber,sizeof(uint32_t));
112  }
113  status = fin_.read((char*)&evtNumber,sizeof(uint32_t));
114 
115  if (!status) {
116  itFileName_++; if (itFileName_==fileNames().end()) { fin_.close(); return; }
118  status = fin_.read((char*)&runNumber,sizeof(uint32_t));
119  if (runNumber < 32) {
120  version = runNumber;
121  status = fin_.read((char*)&runNumber,sizeof(uint32_t));
122  }
123  if (version >= 2) {
124  status = fin_.read((char*)&lumiNumber,sizeof(uint32_t));
125  }
126  status = fin_.read((char*)&evtNumber,sizeof(uint32_t));
127  if (!status) { fin_.close(); return; }
128  }
129 
130  runNumber = (runNumber==0) ? 1 : runNumber;
131 
132  setRunNumber(runNumber);
133  setEventNumber(evtNumber);
134 }
135 
136 
137 //______________________________________________________________________________
139 {
140  unsigned int totalEventSize = 0;
141  if (!fin_.is_open()) return false;
142 
143  auto_ptr<FEDRawDataCollection> result(new FEDRawDataCollection());
144 
145  uint32_t fedSize[1024];
146  fin_.read((char*)fedSize,1024*sizeof(uint32_t));
147  for (unsigned int i=0;i<1024;i++) {
148  totalEventSize += fedSize[i];
149  }
150  unsigned int gtpevmsize = fedSize[errorstreamsource::gtpEvmId_];
151  if(gtpevmsize>0)
152  evf::evtn::evm_board_setformat(gtpevmsize);
153  char *event = new char[totalEventSize];
154  fin_.read(event,totalEventSize);
155  while(totalEventSize>0) {
156  totalEventSize -= 8;
157  fedt_t *fedt = (fedt_t*)(event+totalEventSize);
158  unsigned int fedsize = FED_EVSZ_EXTRACT(fedt->eventsize);
159  fedsize *= 8; // fed size in bytes
160  totalEventSize -= (fedsize - 8);
161  fedh_t *fedh = (fedh_t *)(event+totalEventSize);
162  unsigned int soid = FED_SOID_EXTRACT(fedh->sourceid);
164  unsigned int gpsl = evf::evtn::getgpslow((unsigned char*)fedh);
165  unsigned int gpsh = evf::evtn::getgpshigh((unsigned char*)fedh);
166  edm::TimeValue_t time = gpsh;
167  time = (time << 32) + gpsl;
168  setTime(time);
169  }
170  FEDRawData& fedData=result->FEDData(soid);
171  fedData.resize(fedsize);
172  memcpy(fedData.data(),event+totalEventSize,fedsize);
173  }
174  e.put(result);
175  delete[] event;
176  return true;
177 }
178 
179 
180 //______________________________________________________________________________
182 {
183  fin_.close();
184  fin_.clear();
185  size_t pos = fileName.find(':');
186  if (pos!=string::npos) {
187  string prefix = fileName.substr(0,pos);
188  if (prefix!="file") return false;
189  pos++;
190  }
191  else pos=0;
192 
193  fin_.open(fileName.substr(pos).c_str(),ios::in|ios::binary);
194  return fin_.is_open();
195 }
196 
197 
199 // define this class as an input source
bool produce(edm::Event &e)
int i
Definition: DBlmapReader.cc:9
unsigned int getgpshigh(const unsigned char *)
unsigned int gtpEvmId_
std::vector< std::string >::const_iterator itFileName_
unsigned int sourceid
Definition: fed_header.h:32
bool openFile(const std::string &fileName)
virtual ~ErrorStreamSource()
void evm_board_setformat(size_t size)
ErrorStreamSource(edm::ParameterSet const &pset, edm::InputSourceDescription const &desc)
#define DEFINE_FWK_INPUT_SOURCE(type)
void resize(size_t newsize)
Definition: FEDRawData.cc:33
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:85
void beginLuminosityBlock(edm::LuminosityBlock &lb)
tuple result
Definition: query.py:137
#define end
Definition: vmac.h:38
void endLuminosityBlock(edm::LuminosityBlock &lb)
unsigned long long TimeValue_t
Definition: Timestamp.h:27
void beginRun(edm::Run &r)
void setRunNumber(RunNumber_t r)
Set the run number.
Definition: InputSource.h:143
unsigned int eventsize
Definition: fed_trailer.h:33
void setEventNumber(EventNumber_t e)
const unsigned char * data() const
Return a const pointer to the beginning of the data buffer.
Definition: FEDRawData.cc:29
tuple status
Definition: ntuplemaker.py:245
void endRun(edm::Run &r)
std::vector< std::string > const & fileNames() const
unsigned int getgpslow(const unsigned char *)
#define constexpr
Definition: Run.h:33