CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_2_SLHC4_patch1/src/EventFilter/RPCRawToDigi/src/RPCPackingModule.cc

Go to the documentation of this file.
00001 #include "EventFilter/RPCRawToDigi/interface/RPCPackingModule.h"
00002 
00003 #include "FWCore/Framework/interface/Event.h"
00004 #include "FWCore/Framework/interface/EventSetup.h"
00005 
00006 #include "DataFormats/FEDRawData/interface/FEDRawDataCollection.h"
00007 #include "DataFormats/FEDRawData/interface/FEDHeader.h"
00008 #include "DataFormats/FEDRawData/interface/FEDTrailer.h"
00009 
00010 #include "DataFormats/Common/interface/Handle.h"
00011 #include "FWCore/Framework/interface/ESHandle.h"
00012 #include "FWCore/Framework/interface/ESWatcher.h"
00013 
00014 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00015 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00016 
00017 #include "CondFormats/RPCObjects/interface/RPCEMap.h"
00018 #include "CondFormats/DataRecord/interface/RPCEMapRcd.h"
00019 #include "CondFormats/RPCObjects/interface/RPCReadOutMapping.h"
00020 
00021 #include "EventFilter/RPCRawToDigi/interface/RPCRecordFormatter.h"
00022 #include "EventFilter/RPCRawToDigi/interface/DebugDigisPrintout.h"
00023 #include "DataFormats/RPCDigi/interface/EmptyWord.h"
00024 
00025 #include <string>
00026 #include <sstream>
00027 
00028 
00029 using namespace std;
00030 using namespace edm;
00031 using namespace rpcrawtodigi;
00032 
00033 typedef uint64_t Word64;
00034 
00035 RPCPackingModule::RPCPackingModule( const ParameterSet& pset ) 
00036   : dataLabel_(pset.getParameter<edm::InputTag>("InputLabel")),
00037     eventCounter_(0)
00038 {
00039   
00040   theCabling = new RPCReadOutMapping("");
00041   produces<FEDRawDataCollection>();
00042 
00043 }
00044 
00045 RPCPackingModule::~RPCPackingModule() 
00046 {
00047   delete theCabling;
00048 }
00049 
00050 
00051 void RPCPackingModule::produce( edm::Event& ev,
00052                               const edm::EventSetup& es)
00053 {
00054   eventCounter_++;
00055   LogInfo("RPCPackingModule") << "[RPCPackingModule::produce] " 
00056                               << "event counter: " << eventCounter_;
00057 
00058   Handle< RPCDigiCollection > digiCollection;
00059   ev.getByLabel(dataLabel_,digiCollection);
00060   LogDebug("") << DebugDigisPrintout()(digiCollection.product());
00061 
00062   static edm::ESWatcher<RPCEMapRcd> recordWatcher; 
00063   if(recordWatcher.check(es)) {
00064     delete theCabling;
00065     LogTrace("") << "record has CHANGED!!, initialise readout map!";
00066     ESHandle<RPCEMap> readoutMapping;
00067     es.get<RPCEMapRcd>().get(readoutMapping);
00068     theCabling = readoutMapping->convert();
00069     LogTrace("") <<" READOUT MAP VERSION: " << theCabling->version() << endl; 
00070   }
00071 
00072   auto_ptr<FEDRawDataCollection> buffers( new FEDRawDataCollection );
00073 
00074 //  pair<int,int> rpcFEDS=FEDNumbering::getRPCFEDIds();
00075   pair<int,int> rpcFEDS(790,792);
00076   for (int id= rpcFEDS.first; id<=rpcFEDS.second; ++id){
00077 
00078     RPCRecordFormatter formatter(id, theCabling) ;
00079     unsigned int lvl1_ID = ev.id().event();
00080     FEDRawData* rawData =  RPCPackingModule::rawData(id, lvl1_ID, digiCollection.product(), formatter);
00081     FEDRawData& fedRawData = buffers->FEDData(id);
00082 
00083     fedRawData = *rawData;
00084     delete rawData;
00085   }
00086   ev.put( buffers );  
00087 }
00088 
00089 
00090 FEDRawData * RPCPackingModule::rawData( int fedId, unsigned int lvl1_ID, const RPCDigiCollection * digis, const RPCRecordFormatter & formatter)
00091 {
00092   //
00093   // get merged records
00094   //
00095   int trigger_BX = 200;   // FIXME - set event by event but correct bx assigment in digi
00096   vector<EventRecords> merged = RPCPackingModule::eventRecords(fedId,trigger_BX,digis,formatter);
00097 
00098   //
00099   // create data words
00100   //
00101   vector<Word64> dataWords;
00102   EmptyWord empty;
00103   typedef vector<EventRecords>::const_iterator IR;
00104   for (IR ir = merged.begin(), irEnd =  merged.end() ; ir != irEnd; ++ir) {
00105     Word64 w = ( ( (Word64(ir->recordBX().data()) << 16) | ir->recordSLD().data() ) << 16
00106                     | ir->recordCD().data() ) << 16 | empty.data();
00107     dataWords.push_back(w);
00108   }
00109 
00110   //
00111   // create raw data
00112   //
00113   int nHeaders = 1;
00114   int nTrailers = 1;
00115   int dataSize = (nHeaders+nTrailers+dataWords.size()) * sizeof(Word64);
00116   FEDRawData * raw = new FEDRawData(dataSize);
00117 
00118   //
00119   // add header
00120   //
00121   unsigned char *pHeader  = raw->data();
00122   int evt_ty = 3;
00123   int source_ID = fedId;
00124   FEDHeader::set(pHeader, evt_ty, lvl1_ID, trigger_BX, source_ID);
00125 
00126   //
00127   // add datawords
00128   //
00129   for (unsigned int idata = 0; idata < dataWords.size(); idata ++) {
00130     Word64 * word = reinterpret_cast<Word64* >(pHeader+(idata+1)*sizeof(Word64));
00131     *word = dataWords[idata];
00132   }
00133 
00134   //
00135   // add trailer
00136   //
00137   unsigned char *pTrailer = pHeader + raw->size()-sizeof(Word64);
00138   int crc = 0;
00139   int evt_stat = 15;
00140   int tts = 0;
00141   int datasize =  raw->size()/sizeof(Word64);
00142   FEDTrailer::set(pTrailer, datasize, crc, evt_stat, tts);
00143 
00144   return raw;
00145 }
00146 
00147 vector<EventRecords> RPCPackingModule::eventRecords(
00148     int fedId, 
00149     int trigger_BX, 
00150     const RPCDigiCollection* digis , 
00151     const RPCRecordFormatter& formatter)
00152 {
00153   typedef  DigiContainerIterator<RPCDetId, RPCDigi> DigiRangeIterator;
00154   vector<EventRecords> dataRecords;
00155 
00156 
00157   LogDebug("RPCRawDataPacker")<<"Packing Fed id="<<fedId;
00158   for (DigiRangeIterator it=digis->begin(); it != digis->end(); it++) {
00159     RPCDetId rpcDetId = (*it).first;
00160     uint32_t rawDetId = rpcDetId.rawId();
00161     RPCDigiCollection::Range range = digis->get(rpcDetId);
00162     for (vector<RPCDigi>::const_iterator  id = range.first; id != range.second; id++) {
00163       const RPCDigi & digi = (*id);
00164       vector<EventRecords>  rawFromDigi =  formatter.recordPack(rawDetId, digi, trigger_BX);
00165       dataRecords.insert(dataRecords.end(), rawFromDigi.begin(), rawFromDigi.end());
00166     }
00167   }
00168 
00169   //
00170   // merge data words
00171   //
00172   LogTrace("RPCRawDataPacker") <<" size of   data: " << dataRecords.size();
00173   vector<EventRecords> merged = EventRecords::mergeRecords(dataRecords);
00174   LogTrace("") <<" size of megred: " << merged.size();
00175 
00176   return merged;
00177 }