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>
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  virtual bool setRunAndEventInfo(edm::EventID& id, edm::TimeValue_t& theTime);
51  virtual void 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  std::auto_ptr<FEDRawDataCollection> result_;
66 };
67 
69 // construction/destruction
71 
72 //______________________________________________________________________________
74  edm::InputSourceDescription const& desc)
75  : ProducerSourceFromFiles(pset,desc,true)
76 {
77  itFileName_=fileNames().begin();
79  produces<FEDRawDataCollection>();
80 }
81 
82 
83 //______________________________________________________________________________
85 {
86 
87 }
88 
89 
91 // implementation of member functions
93 
94 //______________________________________________________________________________
96 {
97  uint32_t version(1);
98  uint32_t runNumber(0);
99  uint32_t lumiNumber(1);
100  uint32_t evtNumber(0);
101  bool status;
102  status = fin_.read((char*)&runNumber,sizeof(uint32_t));
103  if (runNumber < 32) {
104  version = runNumber;
105  status = fin_.read((char*)&runNumber,sizeof(uint32_t));
106  }
107  if (version >= 2) {
108  status = fin_.read((char*)&lumiNumber,sizeof(uint32_t));
109  }
110  status = fin_.read((char*)&evtNumber,sizeof(uint32_t));
111 
112  if (!status) {
113  itFileName_++; if (itFileName_==fileNames().end()) { fin_.close(); return false; }
115  status = fin_.read((char*)&runNumber,sizeof(uint32_t));
116  if (runNumber < 32) {
117  version = runNumber;
118  status = fin_.read((char*)&runNumber,sizeof(uint32_t));
119  }
120  if (version >= 2) {
121  status = fin_.read((char*)&lumiNumber,sizeof(uint32_t));
122  }
123  status = fin_.read((char*)&evtNumber,sizeof(uint32_t));
124  if (!status) { fin_.close(); return false; }
125  }
126 
127  runNumber = (runNumber==0) ? 1 : runNumber;
128 
129  id = edm::EventID(runNumber, lumiNumber, evtNumber);
130 
131 //______________________________________________________________________________
132  unsigned int totalEventSize = 0;
133 
134  result_.reset(new FEDRawDataCollection());
135 
136  uint32_t fedSize[1024];
137  fin_.read((char*)fedSize,1024*sizeof(uint32_t));
138  for (unsigned int i=0;i<1024;i++) {
139  totalEventSize += fedSize[i];
140  }
141  unsigned int gtpevmsize = fedSize[errorstreamsource::gtpEvmId_];
142  if(gtpevmsize>0)
143  evf::evtn::evm_board_setformat(gtpevmsize);
144  char *event = new char[totalEventSize];
145  fin_.read(event,totalEventSize);
146  while(totalEventSize>0) {
147  totalEventSize -= 8;
148  fedt_t *fedt = (fedt_t*)(event+totalEventSize);
149  unsigned int fedsize = FED_EVSZ_EXTRACT(fedt->eventsize);
150  fedsize *= 8; // fed size in bytes
151  totalEventSize -= (fedsize - 8);
152  fedh_t *fedh = (fedh_t *)(event+totalEventSize);
153  unsigned int soid = FED_SOID_EXTRACT(fedh->sourceid);
155  unsigned int gpsl = evf::evtn::getgpslow((unsigned char*)fedh);
156  unsigned int gpsh = evf::evtn::getgpshigh((unsigned char*)fedh);
157  edm::TimeValue_t time = gpsh;
158  time = (time << 32) + gpsl;
159  theTime = time;
160  }
161  FEDRawData& fedData=result_->FEDData(soid);
162  fedData.resize(fedsize);
163  memcpy(fedData.data(),event+totalEventSize,fedsize);
164  }
165  delete[] event;
166  return true;
167 }
168 
170  e.put(result_);
171 }
172 
173 //______________________________________________________________________________
175 {
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") return false;
182  pos++;
183  }
184  else pos=0;
185 
186  fin_.open(fileName.substr(pos).c_str(),std::ios::in|std::ios::binary);
187  return fin_.is_open();
188 }
189 
190 
192 // define this class as an input source
int i
Definition: DBlmapReader.cc:9
unsigned int getgpshigh(const unsigned char *)
std::vector< std::string >::const_iterator itFileName_
unsigned int sourceid
Definition: fed_header.h:32
std::vector< std::string > const & fileNames() const
Definition: FromFiles.h:22
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:32
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:116
void beginLuminosityBlock(edm::LuminosityBlock &lb)
#define FED_EVSZ_EXTRACT(a)
Definition: fed_trailer.h:36
#define end
Definition: vmac.h:37
void endLuminosityBlock(edm::LuminosityBlock &lb)
unsigned long long TimeValue_t
Definition: Timestamp.h:28
virtual void produce(edm::Event &e)
void beginRun(edm::Run &r)
unsigned int eventsize
Definition: fed_trailer.h:33
EventNumber_t event() const
const unsigned char * data() const
Return a const pointer to the beginning of the data buffer.
Definition: FEDRawData.cc:28
constexpr unsigned int gtpEvmId_
tuple status
Definition: ntuplemaker.py:245
void endRun(edm::Run &r)
std::auto_ptr< FEDRawDataCollection > result_
unsigned int getgpslow(const unsigned char *)
#define constexpr
Definition: Run.h:41
#define FED_SOID_EXTRACT(a)
Definition: fed_header.h:53
virtual bool setRunAndEventInfo(edm::EventID &id, edm::TimeValue_t &theTime)