CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch9/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 <iostream>
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 
00056 void RPCUnpackingModule::produce(Event & ev, const EventSetup& es)
00057 {
00058   static bool debug = edm::MessageDrop::instance()->debugEnabled;
00059   eventCounter_++; 
00060   if (debug) LogDebug ("RPCUnpacker::produce") <<"Beginning To Unpack Event: "<<eventCounter_;
00061  
00062   Handle<FEDRawDataCollection> allFEDRawData; 
00063   ev.getByLabel(dataLabel_,allFEDRawData); 
00064 
00065   if (theRecordWatcher.check(es)) {  
00066     if (debug) LogTrace("") << "record has CHANGED!!, (re)initialise readout map!";
00067     delete theCabling; 
00068     ESTransientHandle<RPCEMap> readoutMapping;
00069     es.get<RPCEMapRcd>().get(readoutMapping);
00070     theCabling = readoutMapping->convert();
00071     theReadoutMappingSearch.init(theCabling);
00072     if (debug) LogTrace("") <<" READOUT MAP VERSION: " << theCabling->version() << endl;
00073   }
00074 
00075   std::auto_ptr<RPCDigiCollection> producedRPCDigis(new RPCDigiCollection);
00076   std::auto_ptr<RPCRawDataCounts> producedRawDataCounts(new RPCRawDataCounts);
00077   std::auto_ptr<RPCRawSynchro::ProdItem> producedRawSynchoCounts;
00078   if (doSynchro_) producedRawSynchoCounts.reset(new RPCRawSynchro::ProdItem);
00079 
00080   int status = 0;
00081   for (int fedId= FEDNumbering::MINRPCFEDID; fedId<=FEDNumbering::MAXRPCFEDID; ++fedId){  
00082 
00083     const FEDRawData & rawData = allFEDRawData->FEDData(fedId);
00084     RPCRecordFormatter interpreter = 
00085         theCabling ? RPCRecordFormatter(fedId,&theReadoutMappingSearch) : RPCRecordFormatter(fedId,0);
00086     int triggerBX =0;
00087     int nWords = rawData.size()/sizeof(Word64);
00088     if (nWords==0) continue;
00089 
00090     //
00091     // check headers
00092     //
00093     const Word64* header = reinterpret_cast<const Word64* >(rawData.data()); header--;
00094     bool moreHeaders = true;
00095     while (moreHeaders) {
00096       header++;
00097       FEDHeader fedHeader( reinterpret_cast<const unsigned char*>(header));
00098       if (!fedHeader.check()) {
00099         producedRawDataCounts->addReadoutError(fedId, ReadoutError(ReadoutError::HeaderCheckFail)); 
00100         if (debug) LogTrace("") <<" ** PROBLEM **, header.check() failed, break"; 
00101         break; 
00102       }
00103       if ( fedHeader.sourceID() != fedId) {
00104         producedRawDataCounts->addReadoutError(fedId, ReadoutError(ReadoutError::InconsitentFedId)); 
00105         if (debug) LogTrace ("") <<" ** PROBLEM **, fedHeader.sourceID() != fedId"
00106             << "fedId = " << fedId<<" sourceID="<<fedHeader.sourceID(); 
00107       }
00108       triggerBX = fedHeader.bxID();
00109       moreHeaders = fedHeader.moreHeaders();
00110       if (debug) {
00111         ostringstream str;
00112         str <<"  header: "<< *reinterpret_cast<const bitset<64>*> (header) << endl;
00113         str <<"  header triggerType: " << fedHeader.triggerType()<<endl;
00114         str <<"  header lvl1ID:      " << fedHeader.lvl1ID() << endl;
00115         str <<"  header bxID:        " << fedHeader.bxID() << endl;
00116         str <<"  header sourceID:    " << fedHeader.sourceID() << endl;
00117         str <<"  header version:     " << fedHeader.version() << endl;
00118         LogTrace("") << str.str();
00119       }
00120     }
00121 
00122     //
00123     // check trailers
00124     //
00125     const Word64* trailer=reinterpret_cast<const Word64* >(rawData.data())+(nWords-1); trailer++;
00126     bool moreTrailers = true;
00127     while (moreTrailers) {
00128       trailer--;
00129       FEDTrailer fedTrailer(reinterpret_cast<const unsigned char*>(trailer));
00130       if ( !fedTrailer.check()) {
00131         producedRawDataCounts->addReadoutError(fedId, ReadoutError(ReadoutError::TrailerCheckFail));
00132         if (debug) LogTrace("") <<" ** PROBLEM **, trailer.check() failed, break";
00133         break;
00134       }
00135       if ( fedTrailer.lenght()!= nWords) {
00136         producedRawDataCounts->addReadoutError(fedId, ReadoutError(ReadoutError::InconsistentDataSize)); 
00137         if (debug) LogTrace("")<<" ** PROBLEM **, fedTrailer.lenght()!= nWords, break";
00138         break;
00139       }
00140       moreTrailers = fedTrailer.moreTrailers();
00141       if (debug) {
00142         ostringstream str;
00143         str <<" trailer: "<<  *reinterpret_cast<const bitset<64>*> (trailer) << endl; 
00144         str <<"  trailer lenght:    "<<fedTrailer.lenght()<<endl;
00145         str <<"  trailer crc:       "<<fedTrailer.crc()<<endl;
00146         str <<"  trailer evtStatus: "<<fedTrailer.evtStatus()<<endl;
00147         str <<"  trailer ttsBits:   "<<fedTrailer.ttsBits()<<endl;
00148         LogTrace("") << str.str();
00149       }
00150     }
00151 
00152     //
00153     // data records
00154     //
00155     if (debug) {
00156       ostringstream str;
00157       for (const Word64* word = header+1; word != trailer; word++) {
00158         str<<"    data: "<<*reinterpret_cast<const bitset<64>*>(word) << endl; 
00159       }
00160       LogTrace("") << str.str();
00161     }
00162 //    if (triggerBX != 51) continue;
00163 //    if (triggerBX != 2316) continue;
00164     EventRecords event(triggerBX);
00165     for (const Word64* word = header+1; word != trailer; word++) {
00166       for( int iRecord=1; iRecord<=4; iRecord++){
00167         const DataRecord::Data* pRecord = reinterpret_cast<const DataRecord::Data* >(word+1)-iRecord;
00168         DataRecord record(*pRecord);
00169         event.add(record);
00170         if (debug) {
00171           std::ostringstream str;
00172           str <<"record: "<<record.print()<<" hex: "<<hex<<*pRecord<<dec;
00173           str <<" type:"<<record.type()<<DataRecord::print(record);
00174           if (event.complete()) {
00175             str<< " --> dccId: "<<fedId
00176                << " rmb: " <<event.recordSLD().rmb()
00177                << " lnk: "<<event.recordSLD().tbLinkInputNumber()
00178                << " lb: "<<event.recordCD().lbInLink()
00179                << " part: "<<event.recordCD().partitionNumber()
00180                << " data: "<<event.recordCD().partitionData()
00181                << " eod: "<<event.recordCD().eod();
00182           }
00183           LogTrace("") << str.str();
00184         }
00185         producedRawDataCounts->addDccRecord(fedId, record);
00186         int statusTMP = 0;
00187         if (event.complete() ) statusTMP= 
00188             interpreter.recordUnpack( event, 
00189             producedRPCDigis.get(), producedRawDataCounts.get(), producedRawSynchoCounts.get()); 
00190         if (statusTMP != 0) status = statusTMP;
00191       }
00192     }
00193   }
00194   if (status && debug) LogTrace("")<<" RPCUnpackingModule - There was unpacking PROBLEM in this event"<<endl;
00195   if (debug) LogTrace("") << DebugDigisPrintout()(producedRPCDigis.get()) << endl;
00196   ev.put(producedRPCDigis);  
00197   ev.put(producedRawDataCounts);
00198   if (doSynchro_) ev.put(producedRawSynchoCounts);
00199 
00200 }