CMS 3D CMS Logo

PlaybackRawDataProvider.cc

Go to the documentation of this file.
00001 
00002 //
00003 // PlaybackRawDataProvider
00004 // -----------------------
00005 //
00006 //            21/09/2006 Philipp Schieferdecker <philipp.schieferdecker@cern.ch>
00008 
00009 
00010 #include "EventFilter/Playback/interface/PlaybackRawDataProvider.h"
00011 #include "DataFormats/FEDRawData/interface/FEDNumbering.h"
00012 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00013 #include "DataFormats/FEDRawData/interface/FEDRawDataCollection.h"
00014 
00015 using namespace std;
00016 using namespace edm;
00017 
00019 // initialize static data members
00021 
00022 PlaybackRawDataProvider* PlaybackRawDataProvider::instance_=0;
00023 
00024 
00026 // construction/destruction
00028 
00029 //______________________________________________________________________________
00030 PlaybackRawDataProvider::PlaybackRawDataProvider(const ParameterSet& iConfig)
00031   : queueSize_(0)
00032   , eventQueue_(0)
00033   , runNumber_(0)
00034   , evtNumber_(0)
00035   , count_(0)
00036   , writeIndex_(0)
00037   , readIndex_(0)
00038   , freeToEof_(false)
00039 {
00040   queueSize_=iConfig.getUntrackedParameter<unsigned int>("queueSize",32);
00041   sem_init(&lock_,0,1);
00042   sem_init(&writeSem_,0,queueSize_);
00043   sem_init(&readSem_,0,0);
00044   runNumber_ =new unsigned int[queueSize_];
00045   evtNumber_ =new unsigned int[queueSize_];
00046   eventQueue_=new FEDRawDataCollection*[queueSize_];
00047   for (unsigned int i=0;i<queueSize_;i++) eventQueue_[i]=0;
00048   edm::LogInfo("PbImpl") << "Created Concrete RawData Provider 0x"<< hex << (unsigned int) this << dec << endl;
00049   instance_=this;
00050 }
00051 
00052 
00053 //______________________________________________________________________________
00054 PlaybackRawDataProvider::~PlaybackRawDataProvider()
00055 {
00056   if (0!=runNumber_) delete [] runNumber_;
00057   if (0!=evtNumber_) delete [] evtNumber_;
00058   if (0!=eventQueue_) {
00059     for (unsigned int i=0;i<queueSize_;i++)
00060       if (0!=eventQueue_[i]) delete eventQueue_[i];
00061     delete [] eventQueue_;
00062   }
00063   edm::LogInfo("PbImpl") << "Destroyed Concrete RawData Provider 0x"<< hex << (unsigned int) this << dec << endl;
00064   instance_=0;
00065 }
00066 
00067 
00069 // implementation of member functions
00071 
00072 //______________________________________________________________________________
00073 void PlaybackRawDataProvider::analyze(const Event& iEvent,
00074                                       const EventSetup& iSetup)
00075 {
00076   if(freeToEof_) return;
00077   waitWriteSem();
00078   if(freeToEof_) return;
00079   runNumber_[writeIndex_]=iEvent.id().run();
00080   evtNumber_[writeIndex_]=iEvent.id().event();
00081 
00082   Handle<FEDRawDataCollection> pRawData;
00083   iEvent.getByType(pRawData);
00084   
00085   if (!pRawData.isValid()) {
00086     edm::LogError("InvalidHandle")<<"no raw data found!"<<endl;
00087     return;
00088   }
00089   
00090   
00091   // copy the raw data collection into rawData_, retrievable via getFEDRawData()
00092   assert(0==eventQueue_[writeIndex_]);
00093   eventQueue_[writeIndex_]=new FEDRawDataCollection();
00094   for (unsigned int i=0;i<(unsigned int)FEDNumbering::lastFEDId()+1;i++) {
00095     unsigned int fedSize=pRawData->FEDData(i).size();
00096     if (fedSize>0) {
00097       FEDRawData& fedData=eventQueue_[writeIndex_]->FEDData(i);
00098       fedData.resize(fedSize);
00099       memcpy(fedData.data(),pRawData->FEDData(i).data(),fedSize);
00100     }
00101   }
00102   
00103   lock();
00104   writeIndex_=(writeIndex_+1)%queueSize_;
00105   count_++;
00106   unlock();
00107   
00108   postReadSem();
00109 
00110   return;
00111 }
00112 
00113 
00114 //______________________________________________________________________________
00115 void PlaybackRawDataProvider::beginJob(const EventSetup&)
00116 {
00117   
00118 }
00119 
00120 
00121 //______________________________________________________________________________
00122 void PlaybackRawDataProvider::endJob()
00123 {
00124   edm::LogInfo("Summary")<<count_<<" events read."<<endl;
00125 }
00126 
00127 
00128 //______________________________________________________________________________
00129 FEDRawDataCollection* PlaybackRawDataProvider::getFEDRawData()
00130 {
00131   FEDRawDataCollection* result = 0;
00132   waitReadSem();
00133   lock();
00134   result = eventQueue_[readIndex_];
00135   eventQueue_[readIndex_]=0;
00136   readIndex_=(readIndex_+1)%queueSize_;
00137   unlock();
00138   
00139   postWriteSem();
00140   return result;
00141 }
00142 
00143 
00144 //______________________________________________________________________________
00145 FEDRawDataCollection* PlaybackRawDataProvider::getFEDRawData(unsigned int& runNumber,
00146                                                              unsigned int& evtNumber)
00147 {
00148 
00149   FEDRawDataCollection* result = 0;
00150   waitReadSem();
00151   lock();
00152   runNumber=runNumber_[readIndex_];
00153   evtNumber=evtNumber_[readIndex_];
00154   result=eventQueue_[readIndex_];
00155   //  assert(0!=result);
00156   eventQueue_[readIndex_]=0;
00157   readIndex_=(readIndex_+1)%queueSize_;
00158 
00159   unlock();
00160   
00161   postWriteSem();
00162   return result;
00163 }
00164 
00165 
00166 //______________________________________________________________________________
00167 void PlaybackRawDataProvider::sem_print()
00168 {
00169   lock();
00170   int wsem,rsem;
00171   sem_getvalue(&writeSem_,&wsem);
00172   sem_getvalue(&readSem_,&rsem);
00173   cout<<"sem_print():"
00174       <<" wsem="<<wsem
00175       <<" rsem="<<rsem
00176       <<" writeIndex="<<writeIndex_
00177       <<" readIndex="<<readIndex_
00178       <<endl;
00179   unlock();
00180 }
00181 
00182 void PlaybackRawDataProvider::setFreeToEof()
00183 { 
00184   //  cout << "  PlaybackRawDataProvider::setFreeToEof()" << endl;
00185   freeToEof_ = true; 
00186   //  cout << "  PlaybackRawDataProvider::setFreeToEof() call postReadSem" << endl;  
00187   postWriteSem(); 
00188 }
00190 // framework module implementation macro
00192 

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