00001 #include "EventFilter/GctRawToDigi/plugins/GctDigiToRaw.h"
00002
00003
00004 #include <vector>
00005 #include <sstream>
00006 #include <iostream>
00007 #include <iomanip>
00008
00009
00010 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00011 #include "FWCore/PluginManager/interface/ModuleDef.h"
00012 #include "FWCore/Framework/interface/MakerMacros.h"
00013
00014
00015 #include "DataFormats/FEDRawData/interface/FEDRawDataCollection.h"
00016 #include "DataFormats/FEDRawData/interface/FEDHeader.h"
00017 #include "DataFormats/FEDRawData/interface/FEDTrailer.h"
00018 #include "DataFormats/Provenance/interface/EventID.h"
00019
00020
00021 #include "FWCore/Utilities/interface/CRC16.h"
00022
00023
00024 #include "DataFormats/L1CaloTrigger/interface/L1CaloEmCand.h"
00025 #include "DataFormats/L1CaloTrigger/interface/L1CaloRegion.h"
00026 #include "DataFormats/L1CaloTrigger/interface/L1CaloRegionDetId.h"
00027 #include "DataFormats/L1CaloTrigger/interface/L1CaloCollections.h"
00028
00029
00030 #include "DataFormats/L1GlobalCaloTrigger/interface/L1GctCollections.h"
00031
00032
00033 #include "DataFormats/FEDRawData/interface/FEDRawDataCollection.h"
00034
00035 using std::cout;
00036 using std::endl;
00037 using std::vector;
00038
00039
00040 GctDigiToRaw::GctDigiToRaw(const edm::ParameterSet& iConfig) :
00041 rctInputLabel_(iConfig.getParameter<edm::InputTag>("rctInputLabel")),
00042 gctInputLabel_(iConfig.getParameter<edm::InputTag>("gctInputLabel")),
00043 packRctEm_(iConfig.getUntrackedParameter<bool>("packRctEm", true)),
00044 packRctCalo_(iConfig.getUntrackedParameter<bool>("packRctCalo", true)),
00045 fedId_(iConfig.getParameter<int>("gctFedId")),
00046 verbose_(iConfig.getUntrackedParameter<bool>("verbose",false)),
00047 counter_(0),
00048 formatTranslator_()
00049 {
00050 LogDebug("GCT") << "GctDigiToRaw will pack FED Id " << fedId_;
00051
00052
00053 produces<FEDRawDataCollection>();
00054 }
00055
00056
00057 GctDigiToRaw::~GctDigiToRaw()
00058 {
00059
00060
00061 }
00062
00063
00064
00065
00066
00067
00068
00069 void
00070 GctDigiToRaw::produce(edm::Event& iEvent, const edm::EventSetup& iSetup)
00071 {
00072 using namespace edm;
00073
00074 counter_++;
00075 unsigned int bx = counter_ % 3564;
00076 EventNumber_t eventNumber = iEvent.id().event();
00077
00078
00079 formatTranslator_.setPackingBxId(bx);
00080 formatTranslator_.setPackingEventId(eventNumber);
00081
00082
00083 const std::string gctInputLabelStr = gctInputLabel_.label();
00084 const std::string rctInputLabelStr = rctInputLabel_.label();
00085
00086
00087 edm::Handle<L1GctEmCandCollection> isoEm;
00088 iEvent.getByLabel(gctInputLabelStr, "isoEm", isoEm);
00089 edm::Handle<L1GctEmCandCollection> nonIsoEm;
00090 iEvent.getByLabel(gctInputLabelStr, "nonIsoEm", nonIsoEm);
00091 edm::Handle<L1GctJetCandCollection> cenJets;
00092 iEvent.getByLabel(gctInputLabelStr, "cenJets", cenJets);
00093 edm::Handle<L1GctJetCandCollection> forJets;
00094 iEvent.getByLabel(gctInputLabelStr, "forJets", forJets);
00095 edm::Handle<L1GctJetCandCollection> tauJets;
00096 iEvent.getByLabel(gctInputLabelStr, "tauJets", tauJets);
00097 edm::Handle<L1GctEtTotalCollection> etTotal;
00098 iEvent.getByLabel(gctInputLabelStr, "", etTotal);
00099 edm::Handle<L1GctEtHadCollection> etHad;
00100 iEvent.getByLabel(gctInputLabelStr, "", etHad);
00101 edm::Handle<L1GctEtMissCollection> etMiss;
00102 iEvent.getByLabel(gctInputLabelStr, "", etMiss);
00103 edm::Handle<L1GctHFRingEtSumsCollection> hfRingSums;
00104 iEvent.getByLabel(gctInputLabelStr, "", hfRingSums);
00105 edm::Handle<L1GctHFBitCountsCollection> hfBitCounts;
00106 iEvent.getByLabel(gctInputLabelStr, "", hfBitCounts);
00107 edm::Handle<L1GctHtMissCollection> htMiss;
00108 iEvent.getByLabel(gctInputLabelStr, "", htMiss);
00109 edm::Handle<L1GctJetCountsCollection> jetCounts;
00110 iEvent.getByLabel(gctInputLabelStr, "", jetCounts);
00111
00112
00113 bool packRctEmThisEvent = packRctEm_;
00114 edm::Handle<L1CaloEmCollection> rctEm;
00115 if(packRctEmThisEvent)
00116 {
00117 iEvent.getByLabel(rctInputLabelStr, rctEm);
00118 if(rctEm.failedToGet())
00119 {
00120 packRctEmThisEvent = false;
00121 LogDebug("GCT") << "RCT EM Candidate packing requested, but failed to get them from event!";
00122 }
00123 }
00124
00125
00126 bool packRctCaloThisEvent = packRctCalo_;
00127 edm::Handle<L1CaloRegionCollection> rctCalo;
00128 if(packRctCaloThisEvent)
00129 {
00130 iEvent.getByLabel(rctInputLabelStr, rctCalo);
00131 if(rctCalo.failedToGet())
00132 {
00133 packRctCaloThisEvent = false;
00134 LogDebug("GCT") << "RCT Calo Region packing requested, but failed to get them from event!";
00135 }
00136 }
00137
00138
00139 std::auto_ptr<FEDRawDataCollection> rawColl(new FEDRawDataCollection());
00140
00141
00142 FEDRawData& fedRawData=rawColl->FEDData(fedId_);
00143
00144
00145 unsigned int rawSize = 88;
00146 if(packRctEmThisEvent) { rawSize += 232; }
00147 if(packRctCaloThisEvent) { rawSize += 800; }
00148 fedRawData.resize(rawSize);
00149 unsigned char * pHeader = fedRawData.data();
00150 unsigned char * pPayload = pHeader + 16;
00151 unsigned char * pFooter = pHeader + rawSize - 8;
00152
00153
00154 FEDHeader fedHeader(pHeader);
00155 fedHeader.set(pHeader, 1, eventNumber, bx, fedId_);
00156
00157
00158 formatTranslator_.writeGctOutJetBlock(pPayload,
00159 cenJets.product(),
00160 forJets.product(),
00161 tauJets.product(),
00162 hfRingSums.product(),
00163 hfBitCounts.product(),
00164 htMiss.product());
00165
00166 pPayload += 36;
00167
00168
00169 formatTranslator_.writeGctOutEmAndEnergyBlock(pPayload,
00170 isoEm.product(),
00171 nonIsoEm.product(),
00172 etTotal.product(),
00173 etHad.product(),
00174 etMiss.product());
00175
00176 pPayload += 28;
00177
00178
00179 if(packRctEmThisEvent)
00180 {
00181 formatTranslator_.writeRctEmCandBlocks(pPayload, rctEm.product());
00182 pPayload+=232;
00183 }
00184
00185
00186 if(packRctCaloThisEvent)
00187 {
00188 formatTranslator_.writeAllRctCaloRegionBlock(pPayload, rctCalo.product());
00189 }
00190
00191
00192 FEDTrailer fedTrailer(pFooter);
00193 fedTrailer.set(pFooter, rawSize/8, evf::compute_crc(pHeader, rawSize), 0, 0);
00194
00195
00196 if (verbose_) { print(fedRawData); }
00197
00198
00199 iEvent.put(rawColl);
00200 }
00201
00202
00203 void GctDigiToRaw::print(FEDRawData& data) {
00204
00205 const unsigned char * d = data.data();
00206
00207 for (unsigned int i=0; i<data.size(); i=i+4) {
00208 uint32_t w = (uint32_t)d[i] + (uint32_t)(d[i+1]<<8) + (uint32_t)(d[i+2]<<16) + (uint32_t)(d[i+3]<<24);
00209 cout << std::hex << std::setw(4) << i/4 << " " << std::setw(8) << w << endl;
00210 }
00211
00212 }
00213
00214
00215
00216 void
00217 GctDigiToRaw::beginJob()
00218 {
00219 }
00220
00221
00222 void
00223 GctDigiToRaw::endJob() {
00224 }
00225
00227 DEFINE_FWK_MODULE(GctDigiToRaw);
00228