CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_1/src/EventFilter/Utilities/plugins/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/ProducerSourceFromFiles.h"
00018 
00019 #include "DataFormats/FEDRawData/interface/FEDRawDataCollection.h"
00020 #include "DataFormats/FEDRawData/interface/FEDNumbering.h"
00021 #include "DataFormats/Provenance/interface/Timestamp.h"
00022 
00023 #include "EventFilter/FEDInterface/interface/GlobalEventNumber.h"
00024 
00025 #include <unistd.h>
00026 #include <string>
00027 #include <vector>
00028 #include <fstream>
00029 #include <cstring>
00030 #include "interface/shared/fed_header.h"
00031 #include "interface/shared/fed_trailer.h"
00032 
00033 
00034 namespace errorstreamsource{
00035   constexpr unsigned int gtpEvmId_ =  FEDNumbering::MINTriggerGTPFEDID;
00036   //static unsigned int gtpeId_ =  FEDNumbering::MINTriggerEGTPFEDID; // unused
00037 }
00038 
00039 
00040 class ErrorStreamSource : public edm::ProducerSourceFromFiles
00041 {
00042 public:
00043   // construction/destruction
00044   ErrorStreamSource(edm::ParameterSet const& pset,
00045                     edm::InputSourceDescription const& desc);
00046   virtual ~ErrorStreamSource();
00047   
00048 private:
00049   // member functions
00050   virtual bool setRunAndEventInfo(edm::EventID& id, edm::TimeValue_t& theTime);
00051   virtual void produce(edm::Event& e);
00052   
00053   void beginRun(edm::Run& r) {;}
00054   void endRun(edm::Run& r) {;} 
00055   void beginLuminosityBlock(edm::LuminosityBlock& lb) {;}
00056   void endLuminosityBlock(edm::LuminosityBlock& lb) {;}
00057   
00058   bool openFile(const std::string& fileName);
00059   
00060   
00061 private:
00062   // member data
00063   std::vector<std::string>::const_iterator itFileName_;
00064   std::ifstream fin_;
00065   std::auto_ptr<FEDRawDataCollection> result_;
00066 };
00067 
00069 // construction/destruction
00071 
00072 //______________________________________________________________________________
00073 ErrorStreamSource::ErrorStreamSource(edm::ParameterSet const& pset,
00074                                      edm::InputSourceDescription const& desc)
00075   : ProducerSourceFromFiles(pset,desc,true)
00076 {
00077   itFileName_=fileNames().begin();
00078   openFile(*itFileName_);
00079   produces<FEDRawDataCollection>();
00080 }
00081 
00082 
00083 //______________________________________________________________________________
00084 ErrorStreamSource::~ErrorStreamSource()
00085 {
00086 
00087 }
00088 
00089 
00091 // implementation of member functions
00093 
00094 //______________________________________________________________________________
00095 bool ErrorStreamSource::setRunAndEventInfo(edm::EventID& id, edm::TimeValue_t& theTime)
00096 {
00097   uint32_t version(1);
00098   uint32_t runNumber(0);
00099   uint32_t lumiNumber(1);
00100   uint32_t evtNumber(0);
00101   bool status;
00102   status = fin_.read((char*)&runNumber,sizeof(uint32_t));
00103   if (runNumber < 32) {
00104       version = runNumber;
00105       status = fin_.read((char*)&runNumber,sizeof(uint32_t));
00106   }
00107   if (version >= 2) {
00108       status = fin_.read((char*)&lumiNumber,sizeof(uint32_t));
00109   }
00110   status = fin_.read((char*)&evtNumber,sizeof(uint32_t));
00111   
00112   if (!status) {
00113     itFileName_++; if (itFileName_==fileNames().end()) { fin_.close(); return false; }
00114     openFile(*itFileName_);
00115     status = fin_.read((char*)&runNumber,sizeof(uint32_t));
00116     if (runNumber < 32) {
00117         version = runNumber;
00118         status = fin_.read((char*)&runNumber,sizeof(uint32_t));
00119     }
00120     if (version >= 2) {
00121         status = fin_.read((char*)&lumiNumber,sizeof(uint32_t));
00122     }
00123     status = fin_.read((char*)&evtNumber,sizeof(uint32_t));
00124     if (!status) { fin_.close(); return false; }
00125   }
00126   
00127   runNumber = (runNumber==0) ? 1 : runNumber;
00128   
00129   id = edm::EventID(runNumber, lumiNumber, evtNumber);
00130 
00131 //______________________________________________________________________________
00132   unsigned int totalEventSize = 0;
00133   
00134   result_.reset(new FEDRawDataCollection());
00135   
00136   uint32_t fedSize[1024];
00137   fin_.read((char*)fedSize,1024*sizeof(uint32_t));
00138   for (unsigned int i=0;i<1024;i++) {
00139     totalEventSize += fedSize[i];
00140   }
00141   unsigned int gtpevmsize = fedSize[errorstreamsource::gtpEvmId_];
00142   if(gtpevmsize>0)
00143     evf::evtn::evm_board_setformat(gtpevmsize);
00144   char *event = new char[totalEventSize];
00145   fin_.read(event,totalEventSize);
00146   while(totalEventSize>0) {
00147     totalEventSize -= 8;
00148     fedt_t *fedt = (fedt_t*)(event+totalEventSize);
00149     unsigned int fedsize = FED_EVSZ_EXTRACT(fedt->eventsize);
00150     fedsize *= 8; // fed size in bytes
00151     totalEventSize -= (fedsize - 8);
00152     fedh_t *fedh = (fedh_t *)(event+totalEventSize);
00153     unsigned int soid = FED_SOID_EXTRACT(fedh->sourceid);
00154     if(soid==errorstreamsource::gtpEvmId_){
00155       unsigned int gpsl = evf::evtn::getgpslow((unsigned char*)fedh);
00156       unsigned int gpsh = evf::evtn::getgpshigh((unsigned char*)fedh);
00157       edm::TimeValue_t time = gpsh;
00158       time = (time << 32) + gpsl;
00159       theTime = time;
00160     }
00161     FEDRawData& fedData=result_->FEDData(soid);
00162     fedData.resize(fedsize);
00163     memcpy(fedData.data(),event+totalEventSize,fedsize);
00164   }
00165   delete[] event;
00166   return true;
00167 }
00168 
00169 void ErrorStreamSource::produce(edm::Event& e) {
00170   e.put(result_);
00171 }
00172 
00173 //______________________________________________________________________________
00174 bool ErrorStreamSource::openFile(const std::string& fileName)
00175 {
00176   fin_.close();
00177   fin_.clear();
00178   size_t pos = fileName.find(':');
00179   if (pos!=std::string::npos) {
00180     std::string prefix = fileName.substr(0,pos);
00181     if (prefix!="file") return false;
00182     pos++;
00183   }
00184   else pos=0;
00185 
00186   fin_.open(fileName.substr(pos).c_str(),std::ios::in|std::ios::binary);
00187   return fin_.is_open();
00188 }
00189                                  
00190                                  
00192 // define this class as an input source
00194 DEFINE_FWK_INPUT_SOURCE(ErrorStreamSource);