CMS 3D CMS Logo

ErrorStreamSource.cc

Go to the documentation of this file.
00001 
00002 //
00003 // ErrorStreamSource
00004 // -----------------
00005 //
00006 //            05/23/2008 Philipp Schieferdecker <philipp.schieferdecker@cern.ch>
00008 
00009 
00010 #include "FWCore/Framework/interface/Frameworkfwd.h"
00011 #include "FWCore/Framework/interface/Event.h"
00012 #include "FWCore/Framework/interface/LuminosityBlock.h"
00013 #include "FWCore/Framework/interface/Run.h"
00014 #include "FWCore/Framework/interface/InputSourceMacros.h"
00015 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00016 
00017 #include "FWCore/Sources/interface/ExternalInputSource.h"
00018 
00019 #include "DataFormats/FEDRawData/interface/FEDRawDataCollection.h"
00020 
00021 #include <unistd.h>
00022 #include <string>
00023 #include <vector>
00024 #include <fstream>
00025 #include "interface/shared/fed_header.h"
00026 #include "interface/shared/fed_trailer.h"
00027 
00028 
00029 class ErrorStreamSource : public edm::ExternalInputSource
00030 {
00031 public:
00032   // construction/destruction
00033   ErrorStreamSource(edm::ParameterSet const& pset,
00034                     edm::InputSourceDescription const& desc);
00035   virtual ~ErrorStreamSource();
00036   
00037 private:
00038   // member functions
00039   void setRunAndEventInfo();
00040   bool produce(edm::Event& e);
00041   
00042   void beginRun(edm::Run& r) {;}
00043   void endRun(edm::Run& r) {;} 
00044   void beginLuminosityBlock(edm::LuminosityBlock& lb) {;}
00045   void endLuminosityBlock(edm::LuminosityBlock& lb) {;}
00046   
00047   bool openFile(const std::string& fileName);
00048   
00049   
00050 private:
00051   // member data
00052   std::vector<std::string>::const_iterator itFileName_;
00053   std::ifstream fin_;
00054 };
00055 
00056 
00057 using namespace std;
00058 
00059 
00061 // construction/destruction
00063 
00064 //______________________________________________________________________________
00065 ErrorStreamSource::ErrorStreamSource(edm::ParameterSet const& pset,
00066                                      edm::InputSourceDescription const& desc)
00067   : ExternalInputSource(pset,desc)
00068 {
00069   itFileName_=fileNames().begin();
00070   openFile(*itFileName_);
00071   produces<FEDRawDataCollection>();
00072 }
00073 
00074 
00075 //______________________________________________________________________________
00076 ErrorStreamSource::~ErrorStreamSource()
00077 {
00078 
00079 }
00080 
00081 
00083 // implementation of member functions
00085 
00086 //______________________________________________________________________________
00087 void ErrorStreamSource::setRunAndEventInfo()
00088 {
00089   uint32_t version(1);
00090   uint32_t runNumber(0);
00091   uint32_t lumiNumber(1);
00092   uint32_t evtNumber(0);
00093   bool status;
00094   status = fin_.read((char*)&runNumber,sizeof(uint32_t));
00095   if (runNumber < 32) {
00096       version = runNumber;
00097       status = fin_.read((char*)&runNumber,sizeof(uint32_t));
00098   }
00099   if (version >= 2) {
00100       status = fin_.read((char*)&lumiNumber,sizeof(uint32_t));
00101   }
00102   status = fin_.read((char*)&evtNumber,sizeof(uint32_t));
00103   
00104   if (!status) {
00105     itFileName_++; if (itFileName_==fileNames().end()) { fin_.close(); return; }
00106     openFile(*itFileName_);
00107     status = fin_.read((char*)&runNumber,sizeof(uint32_t));
00108     if (runNumber < 32) {
00109         version = runNumber;
00110         status = fin_.read((char*)&runNumber,sizeof(uint32_t));
00111     }
00112     if (version >= 2) {
00113         status = fin_.read((char*)&lumiNumber,sizeof(uint32_t));
00114     }
00115     status = fin_.read((char*)&evtNumber,sizeof(uint32_t));
00116     if (!status) { fin_.close(); return; }
00117   }
00118   
00119   runNumber = (runNumber==0) ? 1 : runNumber;
00120   
00121   setRunNumber(runNumber);
00122   setEventNumber(evtNumber);
00123 }
00124 
00125 
00126 //______________________________________________________________________________
00127 bool ErrorStreamSource::produce(edm::Event& e)
00128 {
00129   unsigned int totalEventSize = 0;
00130   if (!fin_.is_open()) return false;
00131   
00132   auto_ptr<FEDRawDataCollection> result(new FEDRawDataCollection());
00133   
00134   uint32_t fedSize[1024];
00135   fin_.read((char*)fedSize,1024*sizeof(uint32_t));
00136   for (unsigned int i=0;i<1024;i++) {
00137     totalEventSize += fedSize[i];
00138   }
00139   char *event = new char[totalEventSize];
00140   fin_.read(event,totalEventSize);
00141   while(totalEventSize>0) {
00142     totalEventSize -= 8;
00143     fedt_t *fedt = (fedt_t*)(event+totalEventSize);
00144     unsigned int fedsize = FED_EVSZ_EXTRACT(fedt->eventsize);
00145     fedsize *= 8; // fed size in bytes
00146     totalEventSize -= (fedsize - 8);
00147     fedh_t *fedh = (fedh_t *)(event+totalEventSize);
00148     unsigned int soid = FED_SOID_EXTRACT(fedh->sourceid);
00149     FEDRawData& fedData=result->FEDData(soid);
00150     fedData.resize(fedsize);
00151     memcpy(fedData.data(),event+totalEventSize,fedsize);
00152   }
00153   e.put(result);
00154   delete[] event;
00155   return true;
00156 }
00157 
00158 
00159 //______________________________________________________________________________
00160 bool ErrorStreamSource::openFile(const string& fileName)
00161 {
00162   fin_.close();
00163   fin_.clear();
00164   unsigned int pos = fileName.find(':');
00165   if (pos!=string::npos) {
00166     string prefix = fileName.substr(0,pos);
00167     if (prefix!="file") return false;
00168     pos++;
00169   }
00170   else pos=0;
00171 
00172   fin_.open(fileName.substr(pos).c_str(),ios::in|ios::binary);
00173   return fin_.is_open();
00174 }
00175                                  
00176                                  
00178 // define this class as an input source
00180 DEFINE_FWK_INPUT_SOURCE(ErrorStreamSource);

Generated on Tue Jun 9 17:34:59 2009 for CMSSW by  doxygen 1.5.4