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