CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_0/src/EventFilter/RPCRawToDigi/plugins/RPCUnpackingModule.cc

Go to the documentation of this file.
00001 #include "RPCUnpackingModule.h"
00002 #include "CondFormats/RPCObjects/interface/RPCReadOutMapping.h"
00003 #include "EventFilter/RPCRawToDigi/interface/RPCRecordFormatter.h"
00004 #include "DataFormats/FEDRawData/interface/FEDRawData.h"
00005 #include "DataFormats/FEDRawData/interface/FEDNumbering.h"
00006 #include "DataFormats/FEDRawData/interface/FEDRawDataCollection.h"
00007 #include "DataFormats/FEDRawData/interface/FEDHeader.h"
00008 #include "DataFormats/FEDRawData/interface/FEDTrailer.h"
00009 #include "DataFormats/RPCDigi/interface/RPCDigiCollection.h"
00010 #include "DataFormats/RPCDigi/interface/RPCRawDataCounts.h"
00011 #include "DataFormats/Common/interface/Handle.h"
00012 #include "FWCore/Framework/interface/Event.h"
00013 //#include "FWCore/Framework/interface/ESHandle.h"
00014 #include "FWCore/Framework/interface/ESTransientHandle.h"
00015 #include "FWCore/Framework/interface/EventSetup.h"
00016 #include "FWCore/Framework/interface/ESWatcher.h"
00017 
00018 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00019 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00020 
00021 #include "CondFormats/RPCObjects/interface/RPCEMap.h"
00022 #include "CondFormats/DataRecord/interface/RPCEMapRcd.h"
00023 #include "DataFormats/RPCDigi/interface/DataRecord.h"
00024 #include "DataFormats/RPCDigi/interface/ReadoutError.h"
00025 #include "DataFormats/RPCDigi/interface/RPCRawSynchro.h"
00026 #include "EventFilter/RPCRawToDigi/interface/EventRecords.h"
00027 #include "EventFilter/RPCRawToDigi/interface/DebugDigisPrintout.h"
00028 
00029 #include <sstream>
00030 #include <bitset>
00031 
00032 using namespace edm;
00033 using namespace std;
00034 using namespace rpcrawtodigi;
00035 
00036 typedef uint64_t Word64;
00037 
00038 
00039 RPCUnpackingModule::RPCUnpackingModule(const edm::ParameterSet& pset) 
00040   : dataLabel_(pset.getParameter<edm::InputTag>("InputLabel")),
00041     doSynchro_(pset.getParameter<bool>("doSynchro")),
00042     eventCounter_(0),
00043     theCabling(0)
00044 {
00045   produces<RPCDigiCollection>();
00046   produces<RPCRawDataCounts>();
00047   if (doSynchro_) produces<RPCRawSynchro::ProdItem>();
00048 }
00049 
00050 RPCUnpackingModule::~RPCUnpackingModule()
00051 { 
00052   delete theCabling;
00053 }
00054 
00055 void RPCUnpackingModule::beginRun(edm::Run &run, const edm::EventSetup& es)
00056 {
00057   if (theRecordWatcher.check(es)) {  
00058     LogTrace("") << "record has CHANGED!!, (re)initialise readout map!";
00059     delete theCabling; 
00060     ESTransientHandle<RPCEMap> readoutMapping;
00061     es.get<RPCEMapRcd>().get(readoutMapping);
00062     theCabling = readoutMapping->convert();
00063     theReadoutMappingSearch.init(theCabling);
00064      LogTrace("") <<" READOUT MAP VERSION: " << theCabling->version() << endl;
00065   }
00066 }
00067 
00068 
00069 void RPCUnpackingModule::produce(Event & ev, const EventSetup& es)
00070 {
00071   static bool debug = edm::MessageDrop::instance()->debugEnabled;
00072   eventCounter_++; 
00073   if (debug) LogDebug ("RPCUnpacker::produce") <<"Beginning To Unpack Event: "<<eventCounter_;
00074  
00075   Handle<FEDRawDataCollection> allFEDRawData; 
00076   ev.getByLabel(dataLabel_,allFEDRawData); 
00077 
00078 
00079   std::auto_ptr<RPCDigiCollection> producedRPCDigis(new RPCDigiCollection);
00080   std::auto_ptr<RPCRawDataCounts> producedRawDataCounts(new RPCRawDataCounts);
00081   std::auto_ptr<RPCRawSynchro::ProdItem> producedRawSynchoCounts;
00082   if (doSynchro_) producedRawSynchoCounts.reset(new RPCRawSynchro::ProdItem);
00083 
00084   int status = 0;
00085   for (int fedId= FEDNumbering::MINRPCFEDID; fedId<=FEDNumbering::MAXRPCFEDID; ++fedId){  
00086 
00087     const FEDRawData & rawData = allFEDRawData->FEDData(fedId);
00088     RPCRecordFormatter interpreter = 
00089         theCabling ? RPCRecordFormatter(fedId,&theReadoutMappingSearch) : RPCRecordFormatter(fedId,0);
00090     int triggerBX =0;
00091     int nWords = rawData.size()/sizeof(Word64);
00092     if (nWords==0) continue;
00093 
00094     //
00095     // check headers
00096     //
00097     const Word64* header = reinterpret_cast<const Word64* >(rawData.data()); header--;
00098     bool moreHeaders = true;
00099     while (moreHeaders) {
00100       header++;
00101       FEDHeader fedHeader( reinterpret_cast<const unsigned char*>(header));
00102       if (!fedHeader.check()) {
00103         producedRawDataCounts->addReadoutError(fedId, ReadoutError(ReadoutError::HeaderCheckFail)); 
00104         if (debug) LogTrace("") <<" ** PROBLEM **, header.check() failed, break"; 
00105         break; 
00106       }
00107       if ( fedHeader.sourceID() != fedId) {
00108         producedRawDataCounts->addReadoutError(fedId, ReadoutError(ReadoutError::InconsitentFedId)); 
00109         if (debug) LogTrace ("") <<" ** PROBLEM **, fedHeader.sourceID() != fedId"
00110             << "fedId = " << fedId<<" sourceID="<<fedHeader.sourceID(); 
00111       }
00112       triggerBX = fedHeader.bxID();
00113       moreHeaders = fedHeader.moreHeaders();
00114       if (debug) {
00115         stringstream str;
00116         str <<"  header: "<< *reinterpret_cast<const bitset<64>*> (header) << endl;
00117         str <<"  header triggerType: " << fedHeader.triggerType()<<endl;
00118         str <<"  header lvl1ID:      " << fedHeader.lvl1ID() << endl;
00119         str <<"  header bxID:        " << fedHeader.bxID() << endl;
00120         str <<"  header sourceID:    " << fedHeader.sourceID() << endl;
00121         str <<"  header version:     " << fedHeader.version() << endl;
00122         LogTrace("") << str.str();
00123       }
00124     }
00125 
00126     //
00127     // check trailers
00128     //
00129     const Word64* trailer=reinterpret_cast<const Word64* >(rawData.data())+(nWords-1); trailer++;
00130     bool moreTrailers = true;
00131     while (moreTrailers) {
00132       trailer--;
00133       FEDTrailer fedTrailer(reinterpret_cast<const unsigned char*>(trailer));
00134       if ( !fedTrailer.check()) {
00135         producedRawDataCounts->addReadoutError(fedId, ReadoutError(ReadoutError::TrailerCheckFail));
00136         if (debug) LogTrace("") <<" ** PROBLEM **, trailer.check() failed, break";
00137         break;
00138       }
00139       if ( fedTrailer.lenght()!= nWords) {
00140         producedRawDataCounts->addReadoutError(fedId, ReadoutError(ReadoutError::InconsistentDataSize)); 
00141         if (debug) LogTrace("")<<" ** PROBLEM **, fedTrailer.lenght()!= nWords, break";
00142         break;
00143       }
00144       moreTrailers = fedTrailer.moreTrailers();
00145       if (debug) {
00146         ostringstream str;
00147         str <<" trailer: "<<  *reinterpret_cast<const bitset<64>*> (trailer) << endl; 
00148         str <<"  trailer lenght:    "<<fedTrailer.lenght()<<endl;
00149         str <<"  trailer crc:       "<<fedTrailer.crc()<<endl;
00150         str <<"  trailer evtStatus: "<<fedTrailer.evtStatus()<<endl;
00151         str <<"  trailer ttsBits:   "<<fedTrailer.ttsBits()<<endl;
00152         LogTrace("") << str.str();
00153       }
00154     }
00155 
00156     //
00157     // data records
00158     //
00159     if (debug) {
00160       ostringstream str;
00161       for (const Word64* word = header+1; word != trailer; word++) {
00162         str<<"    data: "<<*reinterpret_cast<const bitset<64>*>(word) << endl; 
00163       }
00164       LogTrace("") << str.str();
00165     }
00166 //    if (triggerBX != 51) continue;
00167 //    if (triggerBX != 2316) continue;
00168     EventRecords event(triggerBX);
00169     for (const Word64* word = header+1; word != trailer; word++) {
00170       for( int iRecord=1; iRecord<=4; iRecord++){
00171         const DataRecord::Data* pRecord = reinterpret_cast<const DataRecord::Data* >(word+1)-iRecord;
00172         DataRecord record(*pRecord);
00173         event.add(record);
00174         if (debug) {
00175           std::ostringstream str;
00176           str <<"record: "<<record.print()<<" hex: "<<hex<<*pRecord<<dec;
00177           str <<" type:"<<record.type()<<DataRecord::print(record);
00178           if (event.complete()) {
00179             str<< " --> dccId: "<<fedId
00180                << " rmb: " <<event.recordSLD().rmb()
00181                << " lnk: "<<event.recordSLD().tbLinkInputNumber()
00182                << " lb: "<<event.recordCD().lbInLink()
00183                << " part: "<<event.recordCD().partitionNumber()
00184                << " data: "<<event.recordCD().partitionData()
00185                << " eod: "<<event.recordCD().eod();
00186           }
00187           LogTrace("") << str.str();
00188         }
00189         producedRawDataCounts->addDccRecord(fedId, record);
00190         int statusTMP = 0;
00191         if (event.complete() ) statusTMP= 
00192             interpreter.recordUnpack( event, 
00193             producedRPCDigis.get(), producedRawDataCounts.get(), producedRawSynchoCounts.get()); 
00194         if (statusTMP != 0) status = statusTMP;
00195       }
00196     }
00197   }
00198   if (status && debug) LogTrace("")<<" RPCUnpackingModule - There was unpacking PROBLEM in this event"<<endl;
00199   if (debug) LogTrace("") << DebugDigisPrintout()(producedRPCDigis.get()) << endl;
00200   ev.put(producedRPCDigis);  
00201   ev.put(producedRawDataCounts);
00202   if (doSynchro_) ev.put(producedRawSynchoCounts);
00203 
00204 }