CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_2_SLHC2_patch1/src/EventFilter/Playback/src/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 #include <cstring>
00015 
00016 using namespace std;
00017 using namespace edm;
00018 
00020 // initialize static data members
00022 
00023 PlaybackRawDataProvider* PlaybackRawDataProvider::instance_=0;
00024 
00025 
00027 // construction/destruction
00029 
00030 //______________________________________________________________________________
00031 PlaybackRawDataProvider::PlaybackRawDataProvider(const ParameterSet& iConfig)
00032   : queueSize_(0)
00033   , eventQueue_(0)
00034   , runNumber_(0)
00035   , evtNumber_(0)
00036   , count_(0)
00037   , writeIndex_(0)
00038   , readIndex_(0)
00039   , freeToEof_(false)
00040   , filesClosed_(false)
00041   , destroying_(false)
00042 {
00043   queueSize_=iConfig.getUntrackedParameter<unsigned int>("queueSize",32);
00044   sem_init(&lock_,0,1);
00045   sem_init(&writeSem_,0,queueSize_);
00046   sem_init(&readSem_,0,0);
00047   runNumber_ =new unsigned int[queueSize_];
00048   evtNumber_ =new unsigned int[queueSize_];
00049   eventQueue_=new FEDRawDataCollection*[queueSize_];
00050   for (unsigned int i=0;i<queueSize_;i++) eventQueue_[i]=0;
00051   edm::LogInfo("PbImpl") << "Created Concrete RawData Provider 0x"<< hex << (unsigned long) this << dec << endl;
00052   instance_=this;
00053 }
00054 
00055 
00056 //______________________________________________________________________________
00057 PlaybackRawDataProvider::~PlaybackRawDataProvider()
00058 {
00059   if (0!=runNumber_) delete [] runNumber_;
00060   if (0!=evtNumber_) delete [] evtNumber_;
00061   if (0!=eventQueue_) {
00062     for (unsigned int i=0;i<queueSize_;i++)
00063       if (0!=eventQueue_[i]) delete eventQueue_[i];
00064     delete [] eventQueue_;
00065   }
00066   edm::LogInfo("PbImpl") << "Destroyed Concrete RawData Provider 0x"<< hex << (unsigned long) this << dec << endl;
00067   instance_=0;
00068 
00069   destroying_=true;
00070   postReadSem();
00071   postWriteSem();
00072   unlock(); 
00073   sem_destroy(&lock_);
00074   sem_destroy(&writeSem_);
00075   sem_destroy(&readSem_);
00076   usleep(10000);
00077 }
00078 
00079 
00081 // implementation of member functions
00083 
00084 //______________________________________________________________________________
00085 void PlaybackRawDataProvider::analyze(const Event& iEvent,
00086                                       const EventSetup& iSetup)
00087 {
00088   if(freeToEof_) return;
00089   waitWriteSem();
00090   if(freeToEof_) return;
00091   runNumber_[writeIndex_]=iEvent.id().run();
00092   evtNumber_[writeIndex_]=iEvent.id().event();
00093 
00094   Handle<FEDRawDataCollection> pRawData;
00095   iEvent.getByLabel("rawDataCollector", pRawData);
00096   
00097   if (!pRawData.isValid()) {
00098     edm::LogError("InvalidHandle")<<"no raw data found!"<<endl;
00099     return;
00100   }
00101   
00102   
00103   // copy the raw data collection into rawData_, retrievable via getFEDRawData()
00104   assert(0==eventQueue_[writeIndex_]);
00105   eventQueue_[writeIndex_]=new FEDRawDataCollection();
00106   for (unsigned int i=0;i<(unsigned int)FEDNumbering::MAXFEDID+1;i++) {
00107     unsigned int fedSize=pRawData->FEDData(i).size();
00108     if (fedSize>0) {
00109       FEDRawData& fedData=eventQueue_[writeIndex_]->FEDData(i);
00110       fedData.resize(fedSize);
00111       memcpy(fedData.data(),pRawData->FEDData(i).data(),fedSize);
00112     }
00113   }
00114   
00115   lock();
00116   writeIndex_=(writeIndex_+1)%queueSize_;
00117   count_++;
00118   unlock();
00119   
00120   postReadSem();
00121 
00122   return;
00123 }
00124 
00125 
00126 //______________________________________________________________________________
00127 void PlaybackRawDataProvider::beginJob()
00128 {
00129   
00130 }
00131 
00132 //______________________________________________________________________________
00133 void PlaybackRawDataProvider::endJob()
00134 {
00135   edm::LogInfo("Summary")<<count_<<" events read."<<endl;
00136 }
00137 
00138 //______________________________________________________________________________
00139 void PlaybackRawDataProvider::respondToCloseInputFile(edm::FileBlock const& fb)
00140 {
00141   filesClosed_ = true;
00142 }
00143 
00144 //______________________________________________________________________________
00145 FEDRawDataCollection* PlaybackRawDataProvider::getFEDRawData()
00146 {
00147   FEDRawDataCollection* result = 0;
00148   waitReadSem();
00149   //do not read data if destructor is called
00150   if (destroying_) return 0;
00151   lock();
00152   result = eventQueue_[readIndex_];
00153   eventQueue_[readIndex_]=0;
00154   readIndex_=(readIndex_+1)%queueSize_;
00155   unlock();
00156   
00157   postWriteSem();
00158   return result;
00159 }
00160 
00161 
00162 //______________________________________________________________________________
00163 FEDRawDataCollection* PlaybackRawDataProvider::getFEDRawData(unsigned int& runNumber,
00164                                                              unsigned int& evtNumber)
00165 {
00166 
00167   FEDRawDataCollection* result = 0;
00168   waitReadSem();
00169   //do not read data if destructor is called
00170   if (destroying_) return 0;
00171   lock();
00172   runNumber=runNumber_[readIndex_];
00173   evtNumber=evtNumber_[readIndex_];
00174   result=eventQueue_[readIndex_];
00175   //  assert(0!=result);
00176   eventQueue_[readIndex_]=0;
00177   readIndex_=(readIndex_+1)%queueSize_;
00178 
00179   unlock();
00180   
00181   postWriteSem();
00182   return result;
00183 }
00184 
00185 
00186 //______________________________________________________________________________
00187 void PlaybackRawDataProvider::sem_print()
00188 {
00189   lock();
00190   int wsem,rsem;
00191   sem_getvalue(&writeSem_,&wsem);
00192   sem_getvalue(&readSem_,&rsem);
00193   cout<<"sem_print():"
00194       <<" wsem="<<wsem
00195       <<" rsem="<<rsem
00196       <<" writeIndex="<<writeIndex_
00197       <<" readIndex="<<readIndex_
00198       <<endl;
00199   unlock();
00200 }
00201 
00202 void PlaybackRawDataProvider::setFreeToEof()
00203 { 
00204   //  cout << "  PlaybackRawDataProvider::setFreeToEof()" << endl;
00205   freeToEof_ = true; 
00206   //  cout << "  PlaybackRawDataProvider::setFreeToEof() call postReadSem" << endl;  
00207   postWriteSem(); 
00208 }
00209 
00210 bool PlaybackRawDataProvider::areFilesClosed()
00211 {
00212   return filesClosed_;
00213 }
00215 // framework module implementation macro
00217