CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_10_patch2/src/IOPool/Streamer/interface/FRDEventOutputModule.h

Go to the documentation of this file.
00001 #ifndef IOPool_Streamer_FRDEventOutputModule_h
00002 #define IOPool_Streamer_FRDEventOutputModule_h
00003 
00004 #include "FWCore/Framework/interface/OutputModule.h"
00005 #include "DataFormats/Common/interface/Handle.h"
00006 #include "DataFormats/FEDRawData/interface/FEDRawDataCollection.h"
00007 #include "DataFormats/FEDRawData/interface/FEDRawData.h"
00008 
00009 #include "boost/shared_array.hpp"
00010 
00011 class FRDEventMsgView;
00012 template <class Consumer>
00013 class FRDEventOutputModule : public edm::OutputModule
00014 {
00015   typedef unsigned int uint32;
00023  public:
00024   explicit FRDEventOutputModule(edm::ParameterSet const& ps);  
00025   ~FRDEventOutputModule();
00026 
00027  private:
00028   virtual void write(edm::EventPrincipal const& e);
00029   virtual void beginRun(edm::RunPrincipal const&);
00030   virtual void endRun(edm::RunPrincipal const&);
00031   virtual void writeRun(edm::RunPrincipal const&) {}
00032   virtual void writeLuminosityBlock(edm::LuminosityBlockPrincipal const&) {}
00033 
00034   std::auto_ptr<Consumer> templateConsumer_;
00035   std::string label_;
00036   std::string instance_;
00037 };
00038 
00039 template <class Consumer>
00040 FRDEventOutputModule<Consumer>::FRDEventOutputModule(edm::ParameterSet const& ps) :
00041   edm::OutputModule(ps),
00042   templateConsumer_(new Consumer(ps)),
00043   label_(ps.getUntrackedParameter<std::string>("ProductLabel","source")),
00044   instance_(ps.getUntrackedParameter<std::string>("ProductInstance",""))
00045 {
00046 }
00047 
00048 template <class Consumer>
00049 FRDEventOutputModule<Consumer>::~FRDEventOutputModule() {}
00050 
00051 template <class Consumer>
00052 void FRDEventOutputModule<Consumer>::write(edm::EventPrincipal const& e) {
00053 
00054   // serialize the FEDRawDataCollection into the format that we expect for
00055   // FRDEventMsgView objects (may be better ways to do this)
00056   edm::Event event(const_cast<edm::EventPrincipal&>(e), description());
00057   edm::Handle<FEDRawDataCollection> fedBuffers;
00058   event.getByLabel(label_, instance_, fedBuffers);
00059 
00060   // determine the expected size of the FRDEvent
00061   int expectedSize = (4 + 1024) * sizeof(uint32);
00062   for (int idx = 0; idx < 1024; ++idx) {
00063     FEDRawData singleFED = fedBuffers->FEDData(idx);
00064     expectedSize += singleFED.size();
00065     //if (singleFED.size() > 0) {
00066     //  std::cout << "FED #" << idx << " size = " << singleFED.size() << std::endl;
00067     //}
00068   }
00069 
00070   // build the FRDEvent into a temporary buffer
00071   boost::shared_array<unsigned char> workBuffer(new unsigned char[expectedSize + 256]);
00072   uint32 *bufPtr = (uint32*) workBuffer.get();
00073   *bufPtr++ = (uint32) 2;  // version number
00074   *bufPtr++ = (uint32) event.id().run();
00075   *bufPtr++ = (uint32) event.luminosityBlock();
00076   *bufPtr++ = (uint32) event.id().event();
00077   for (int idx = 0; idx < 1024; ++idx) {
00078     FEDRawData singleFED = fedBuffers->FEDData(idx);
00079     *bufPtr++ = singleFED.size();
00080   }
00081   for (int idx = 0; idx < 1024; ++idx) {
00082     FEDRawData singleFED = fedBuffers->FEDData(idx);
00083     if (singleFED.size() > 0) {
00084       memcpy(bufPtr, singleFED.data(), singleFED.size());
00085       *bufPtr += singleFED.size();
00086     }
00087   }
00088 
00089   // create the FRDEventMsgView and use the template consumer to write it out
00090   FRDEventMsgView msg(workBuffer.get());
00091   templateConsumer_->doOutputEvent(msg);
00092 }
00093 
00094 template <class Consumer>
00095 void FRDEventOutputModule<Consumer>::beginRun(edm::RunPrincipal const&)
00096 {
00097   templateConsumer_->start();
00098 }
00099    
00100 template <class Consumer>
00101 void FRDEventOutputModule<Consumer>::endRun(edm::RunPrincipal const&)
00102 {
00103   templateConsumer_->stop();
00104 }
00105 
00106 #endif