CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch9/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     shmBuffer_->scheduleRawEmptyCellForDiscard(newCell);
00089     shmdt(shmBuffer_);
00090     shmBuffer_=0;
00091     event_=0;
00092     lastCellIndex_=0xffffffff;
00093     return 0;
00094   }
00095   else if(state==evt::LUMISECTION){
00096     unsigned int ls = newCell->getLumiSection();
00097     //shmBuffer_->setEvtState(newCell->index(),evt::PROCESSING);
00098     shmBuffer_->scheduleRawCellForDiscard(newCell->index());
00099     if(ls==0){
00100       std::cout << getpid() << " GOT an EOL event for ls 0!!!" 
00101                 << std::endl;
00102       throw edm::Exception(errors::LogicError)
00103         << "FUShmReader received an EOL event with ls = 0 !!!";
00104     }
00105     return (-1)*ls;
00106   }
00107   // getting an 'empty' event here is a pathological condition !!!
00108   else if(state==evt::EMPTY){
00109     edm::LogError("EmptyRawCell")
00110       <<"Received empty event, this should not happen !!!" <<endl;
00111     std::cout << getpid() << "Received EPTY event!!! ERROR." << std::endl;
00112     return fillRawData(eID, tstamp, data);
00113   }
00114   else assert(state==evt::RAWREADING);
00115   
00116   // read the event data into the fwk raw data format
00117   evtNumber_    =newCell->evtNumber();
00118   lastCellIndex_=newCell->index();
00119   event_        =new FEDRawDataCollection();
00120   for (unsigned int i=0;i<newCell->nFed();i++) {
00121     unsigned int fedSize=newCell->fedSize(i);
00122     if (fedSize>0) {
00123       FEDRawData& fedData=event_->FEDData(i);
00124       fedData.resize(fedSize);
00125       newCell->readFed(i,fedData.data());
00126     }
00127   }
00128   
00129   // reading the cell is finished (new state will be 'isProcessing')
00130   shmBuffer_->finishReadingRawCell(newCell);
00131   eID=EventID(runNumber_,1U,evtNumber_);
00132   data=event_;
00133   if(evtNumber_==0) 
00134     std::cout << getpid() << " ShmReader got event number zero !!! " 
00135               << std::endl;
00136   return evtNumber_;
00137 }
00138 
00139 
00141 // CMSSW framwork macros
00143 
00144 
00145 DEFINE_EDM_PLUGIN(DaqReaderPluginFactoryU,FUShmReader,"FUShmReader");