CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_5_3_14/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       edm::LogWarning("FUShmReader") << "Class destructor called without received STOP event!"<<endl;
00049       if (lastCellIndex_<0xfffffffe)//not sending lumi cell to the error stream
00050         shmBuffer_->writeErrorEventData(runNumber_,getpid(),lastCellIndex_,true);
00051       bool success = shmBuffer_->removeClientPrcId(getpid());
00052       if (!success)
00053         edm::LogWarning("ShmMissingPID")<<"Destructor: did not find pid in shared memory index"<<endl;
00054     }
00055     shmdt(shmBuffer_);
00056   }
00057 }
00058 
00059 
00061 // implementation of member functions
00063 
00064 //______________________________________________________________________________
00065 int FUShmReader::fillRawData(EventID& eID,
00066                              Timestamp& tstamp, 
00067                              FEDRawDataCollection*& data)
00068 {
00069   // just in case the reader hasn't yet attached to the shm segment
00070   if (0==shmBuffer_) {
00071     shmBuffer_=FUShmBuffer::getShmBuffer();
00072     if(0==shmBuffer_) {
00073       edm::LogError("NoShmBuffer")<<"Failed to retrieve shm segment."<<endl;
00074       throw cms::Exception("NullPointer")<<"Failed to retrieve shm segment."<<endl;
00075     }
00076   }
00077   
00078   // discard old event
00079   if(0!=event_) {
00080     shmBuffer_->scheduleRawCellForDiscard(lastCellIndex_);
00081     event_ = 0;
00082   }
00083   
00084   // wait for an event to become available, retrieve it
00085   FUShmRawCell* newCell=shmBuffer_->rawCellToRead();
00086   
00087   // if the event is 'stop', the reader is being told to shut down!
00088   evt::State_t state=shmBuffer_->evtState(newCell->index());
00089   if (state==evt::STOP) {
00090     edm::LogInfo("ShutDown")<<"Received STOP event, shut down."<<endl;
00091     std::cout << getpid() << " Received STOP event, shut down." << std::endl;
00092     if(newCell->getEventType() != evt::STOPPER){
00093       edm::LogError("InconsistentStopCell") << getpid() 
00094                                           << " GOT what claims to be a STOPPER event but eventType is " 
00095                                           <<  newCell->getEventType()
00096                                           << std::endl;
00097     }
00098     bool pidstatus=true;
00099     bool success = shmBuffer_->scheduleRawEmptyCellForDiscard(newCell,pidstatus);
00100     if (!success) //should not happen for STOPPER cell
00101         edm::LogError("InconsistentStopCell")<<"fillRawData:STOP: failed to discard STOP event raw cell!"<<endl;
00102     else if (!pidstatus) 
00103         edm::LogWarning("ShmMissingPID")<<"fillRawData:STOP: did not find pid in shared memory pid index"<<endl;
00104     shmdt(shmBuffer_);
00105     shmBuffer_=0;
00106     event_=0;
00107     lastCellIndex_=0xffffffff;
00108     return 0;
00109   }
00110   else if(state==evt::LUMISECTION){
00111     unsigned int ls = newCell->getLumiSection();
00112     if(newCell->getEventType() != evt::EOL){
00113       edm::LogError("InconsistentLsCell") << getpid() 
00114                                           << " GOT what claims to be an EOL event but eventType is " 
00115                                           <<  newCell->getEventType()
00116                                           << " and ls is " << ls
00117                                           << std::endl;
00118       shmBuffer_->sem_print();
00119     }
00120     shmBuffer_->scheduleRawCellForDiscard(newCell->index());
00121     lastCellIndex_=0xfffffffe;
00122     if(ls==0){
00123       edm::LogError("ZeroLsCell") << getpid() 
00124                                   << " GOT an EOL event for ls 0!!!" 
00125                                   << std::endl;
00126       return fillRawData(eID, tstamp, data);
00127     }
00128     return (-1)*ls;
00129   }
00130   // getting an 'empty' event here is a pathological condition !!!
00131   else if(state==evt::EMPTY){
00132     edm::LogError("EmptyRawCell")
00133       <<"Received empty event, this should not happen !!!" <<endl;
00134     std::cout << getpid() << "Received EPTY event!!! ERROR." << std::endl;
00135     return fillRawData(eID, tstamp, data);
00136   }
00137   else assert(state==evt::RAWREADING);
00138   
00139   // read the event data into the fwk raw data format
00140   evtNumber_    =newCell->evtNumber();
00141   lastCellIndex_=newCell->index();
00142   event_        =new FEDRawDataCollection();
00143   for (unsigned int i=0;i<newCell->nFed();i++) {
00144     unsigned int fedSize=newCell->fedSize(i);
00145     if (fedSize>0) {
00146       FEDRawData& fedData=event_->FEDData(i);
00147       fedData.resize(fedSize);
00148       newCell->readFed(i,fedData.data());
00149     }
00150   }
00151   
00152   // reading the cell is finished (new state will be 'isProcessing')
00153   shmBuffer_->finishReadingRawCell(newCell);
00154   eID=EventID(runNumber_,1U,evtNumber_);
00155   data=event_;
00156   if(evtNumber_==0) 
00157     std::cout << getpid() << " ShmReader got event number zero !!! " 
00158               << std::endl;
00159   return 1;
00160 }
00161 
00162 
00164 // CMSSW framwork macros
00166 
00167 
00168 DEFINE_EDM_PLUGIN(DaqReaderPluginFactoryU,FUShmReader,"FUShmReader");