CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch9/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/ExternalInputSource.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   static unsigned int gtpEvmId_ =  FEDNumbering::MINTriggerGTPFEDID;
00036   //static unsigned int gtpeId_ =  FEDNumbering::MINTriggerEGTPFEDID; // unused
00037 }
00038 
00039 
00040 class ErrorStreamSource : public edm::ExternalInputSource
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   void setRunAndEventInfo();
00051   bool 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 };
00066 
00067 
00068 using namespace std;
00069 
00070 
00072 // construction/destruction
00074 
00075 //______________________________________________________________________________
00076 ErrorStreamSource::ErrorStreamSource(edm::ParameterSet const& pset,
00077                                      edm::InputSourceDescription const& desc)
00078   : ExternalInputSource(pset,desc)
00079 {
00080   itFileName_=fileNames().begin();
00081   openFile(*itFileName_);
00082   produces<FEDRawDataCollection>();
00083 }
00084 
00085 
00086 //______________________________________________________________________________
00087 ErrorStreamSource::~ErrorStreamSource()
00088 {
00089 
00090 }
00091 
00092 
00094 // implementation of member functions
00096 
00097 //______________________________________________________________________________
00098 void ErrorStreamSource::setRunAndEventInfo()
00099 {
00100   uint32_t version(1);
00101   uint32_t runNumber(0);
00102   uint32_t lumiNumber(1);
00103   uint32_t evtNumber(0);
00104   bool status;
00105   status = fin_.read((char*)&runNumber,sizeof(uint32_t));
00106   if (runNumber < 32) {
00107       version = runNumber;
00108       status = fin_.read((char*)&runNumber,sizeof(uint32_t));
00109   }
00110   if (version >= 2) {
00111       status = fin_.read((char*)&lumiNumber,sizeof(uint32_t));
00112   }
00113   status = fin_.read((char*)&evtNumber,sizeof(uint32_t));
00114   
00115   if (!status) {
00116     itFileName_++; if (itFileName_==fileNames().end()) { fin_.close(); return; }
00117     openFile(*itFileName_);
00118     status = fin_.read((char*)&runNumber,sizeof(uint32_t));
00119     if (runNumber < 32) {
00120         version = runNumber;
00121         status = fin_.read((char*)&runNumber,sizeof(uint32_t));
00122     }
00123     if (version >= 2) {
00124         status = fin_.read((char*)&lumiNumber,sizeof(uint32_t));
00125     }
00126     status = fin_.read((char*)&evtNumber,sizeof(uint32_t));
00127     if (!status) { fin_.close(); return; }
00128   }
00129   
00130   runNumber = (runNumber==0) ? 1 : runNumber;
00131   
00132   setRunNumber(runNumber);
00133   setEventNumber(evtNumber);
00134 }
00135 
00136 
00137 //______________________________________________________________________________
00138 bool ErrorStreamSource::produce(edm::Event& e)
00139 {
00140   unsigned int totalEventSize = 0;
00141   if (!fin_.is_open()) return false;
00142   
00143   auto_ptr<FEDRawDataCollection> result(new FEDRawDataCollection());
00144   
00145   uint32_t fedSize[1024];
00146   fin_.read((char*)fedSize,1024*sizeof(uint32_t));
00147   for (unsigned int i=0;i<1024;i++) {
00148     totalEventSize += fedSize[i];
00149   }
00150   unsigned int gtpevmsize = fedSize[errorstreamsource::gtpEvmId_];
00151   if(gtpevmsize>0)
00152     evf::evtn::evm_board_setformat(gtpevmsize);
00153   char *event = new char[totalEventSize];
00154   fin_.read(event,totalEventSize);
00155   while(totalEventSize>0) {
00156     totalEventSize -= 8;
00157     fedt_t *fedt = (fedt_t*)(event+totalEventSize);
00158     unsigned int fedsize = FED_EVSZ_EXTRACT(fedt->eventsize);
00159     fedsize *= 8; // fed size in bytes
00160     totalEventSize -= (fedsize - 8);
00161     fedh_t *fedh = (fedh_t *)(event+totalEventSize);
00162     unsigned int soid = FED_SOID_EXTRACT(fedh->sourceid);
00163     if(soid==errorstreamsource::gtpEvmId_){
00164       unsigned int gpsl = evf::evtn::getgpslow((unsigned char*)fedh);
00165       unsigned int gpsh = evf::evtn::getgpshigh((unsigned char*)fedh);
00166       edm::TimeValue_t time = gpsh;
00167       time = (time << 32) + gpsl;
00168       setTime(time);
00169     }
00170     FEDRawData& fedData=result->FEDData(soid);
00171     fedData.resize(fedsize);
00172     memcpy(fedData.data(),event+totalEventSize,fedsize);
00173   }
00174   e.put(result);
00175   delete[] event;
00176   return true;
00177 }
00178 
00179 
00180 //______________________________________________________________________________
00181 bool ErrorStreamSource::openFile(const string& fileName)
00182 {
00183   fin_.close();
00184   fin_.clear();
00185   size_t pos = fileName.find(':');
00186   if (pos!=string::npos) {
00187     string prefix = fileName.substr(0,pos);
00188     if (prefix!="file") return false;
00189     pos++;
00190   }
00191   else pos=0;
00192 
00193   fin_.open(fileName.substr(pos).c_str(),ios::in|ios::binary);
00194   return fin_.is_open();
00195 }
00196                                  
00197                                  
00199 // define this class as an input source
00201 DEFINE_FWK_INPUT_SOURCE(ErrorStreamSource);