CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/src/EventFilter/ShmReader/src/FUShmReader.cc

Go to the documentation of this file.
00001 
00002 //
00003 // FUShmReader
00004 // -----------
00005 //
00006 //            11/03/2006 Philipp Schieferdecker <philipp.schieferdecker@cern.ch>
00008 
00009 
00010 #include "EventFilter/ShmReader/interface/FUShmReader.h"
00011 
00012 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00013 #include "FWCore/PluginManager/interface/ModuleDef.h"
00014 #include "IORawData/DaqSource/interface/DaqReaderPluginFactory.h"
00015 #include "DataFormats/FEDRawData/interface/FEDRawDataCollection.h"
00016 
00017 
00018 #include <iostream>
00019 
00020 
00021 using namespace std;
00022 using namespace evf;
00023 using namespace edm;
00024 
00025 
00027 // construction/destruction
00029 
00030 //______________________________________________________________________________
00031 FUShmReader::FUShmReader()
00032   : event_(0)
00033   , shmBuffer_(0)
00034   , runNumber_(0xffffffff)
00035   , evtNumber_(0xffffffff)
00036   , lastCellIndex_(0xffffffff)
00037 {
00038   //  shmBuffer_=FUShmBuffer::getShmBuffer();
00039 }
00040 
00041 
00042 //______________________________________________________________________________
00043 FUShmReader::~FUShmReader()
00044 {
00045   if (0!=shmBuffer_) {
00046     edm::LogInfo("FUShmReader")<<"detach from shared memory segment."<<endl;
00047     if (lastCellIndex_<0xffffffff) {
00048       shmBuffer_->writeErrorEventData(runNumber_,getpid(),lastCellIndex_);
00049       shmBuffer_->removeClientPrcId(getpid());
00050     }
00051     shmdt(shmBuffer_);
00052   }
00053 }
00054 
00055 
00057 // implementation of member functions
00059 
00060 //______________________________________________________________________________
00061 int FUShmReader::fillRawData(EventID& eID,
00062                              Timestamp& tstamp, 
00063                              FEDRawDataCollection*& data)
00064 {
00065   // just in case the reader hasn't yet attached to the shm segment
00066   if (0==shmBuffer_) {
00067     shmBuffer_=FUShmBuffer::getShmBuffer();
00068     if(0==shmBuffer_) {
00069       edm::LogError("NoShmBuffer")<<"Failed to retrieve shm segment."<<endl;
00070       throw cms::Exception("NullPointer")<<"Failed to retrieve shm segment."<<endl;
00071     }
00072   }
00073   
00074   // discard old event
00075   if(0!=event_) {
00076     shmBuffer_->scheduleRawCellForDiscard(lastCellIndex_);
00077     event_ = 0;
00078   }
00079   
00080   // wait for an event to become available, retrieve it
00081   FUShmRawCell* newCell=shmBuffer_->rawCellToRead();
00082   
00083   // if the event is 'stop', the reader is being told to shut down!
00084   evt::State_t state=shmBuffer_->evtState(newCell->index());
00085   if (state==evt::STOP) {
00086     edm::LogInfo("ShutDown")<<"Received STOP event, shut down."<<endl;
00087     std::cout << getpid() << " Received STOP event, shut down." << std::endl;
00088     if(newCell->getEventType() != evt::STOPPER){
00089       edm::LogError("InconsistentLsCell") << getpid() 
00090                                           << " GOT what claims to be a STOPPER event but eventType is " 
00091                                           <<  newCell->getEventType()
00092                                           << std::endl;
00093     }
00094     shmBuffer_->scheduleRawEmptyCellForDiscard(newCell);
00095     shmdt(shmBuffer_);
00096     shmBuffer_=0;
00097     event_=0;
00098     lastCellIndex_=0xffffffff;
00099     return 0;
00100   }
00101   else if(state==evt::LUMISECTION){
00102     unsigned int ls = newCell->getLumiSection();
00103     if(newCell->getEventType() != evt::EOL){
00104       edm::LogError("InconsistentLsCell") << getpid() 
00105                                           << " GOT what claims to be an EOL event but eventType is " 
00106                                           <<  newCell->getEventType()
00107                                           << " and ls is " << ls
00108                                           << std::endl;
00109       shmBuffer_->sem_print();
00110     }
00111     //shmBuffer_->setEvtState(newCell->index(),evt::PROCESSING);
00112     shmBuffer_->scheduleRawCellForDiscard(newCell->index());
00113     if(ls==0){
00114       edm::LogError("ZeroLsCell") << getpid() 
00115                                   << " GOT an EOL event for ls 0!!!" 
00116                                   << std::endl;
00117       return fillRawData(eID, tstamp, data);
00118     }
00119     return (-1)*ls;
00120   }
00121   // getting an 'empty' event here is a pathological condition !!!
00122   else if(state==evt::EMPTY){
00123     edm::LogError("EmptyRawCell")
00124       <<"Received empty event, this should not happen !!!" <<endl;
00125     std::cout << getpid() << "Received EPTY event!!! ERROR." << std::endl;
00126     return fillRawData(eID, tstamp, data);
00127   }
00128   else assert(state==evt::RAWREADING);
00129   
00130   // read the event data into the fwk raw data format
00131   evtNumber_    =newCell->evtNumber();
00132   lastCellIndex_=newCell->index();
00133   event_        =new FEDRawDataCollection();
00134   for (unsigned int i=0;i<newCell->nFed();i++) {
00135     unsigned int fedSize=newCell->fedSize(i);
00136     if (fedSize>0) {
00137       FEDRawData& fedData=event_->FEDData(i);
00138       fedData.resize(fedSize);
00139       newCell->readFed(i,fedData.data());
00140     }
00141   }
00142   
00143   // reading the cell is finished (new state will be 'isProcessing')
00144   shmBuffer_->finishReadingRawCell(newCell);
00145   eID=EventID(runNumber_,1U,evtNumber_);
00146   data=event_;
00147   if(evtNumber_==0) 
00148     std::cout << getpid() << " ShmReader got event number zero !!! " 
00149               << std::endl;
00150   return 1;
00151 }
00152 
00153 
00155 // CMSSW framwork macros
00157 
00158 
00159 DEFINE_EDM_PLUGIN(DaqReaderPluginFactoryU,FUShmReader,"FUShmReader");