Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-09-07 04:36:13

0001 #include "EventFilter/L1ScoutingRawToDigi/plugins/ScGMTRawToDigi.h"
0002 
0003 ScGMTRawToDigi::ScGMTRawToDigi(const edm::ParameterSet& iConfig) {
0004   using namespace edm;
0005   srcInputTag = iConfig.getParameter<InputTag>("srcInputTag");
0006   skipInterm_ = iConfig.getParameter<bool>("skipInterm");
0007   debug_ = iConfig.getUntrackedParameter<bool>("debug", false);
0008 
0009   // initialize orbit buffer for BX 1->3564;
0010   orbitBuffer_ = std::vector<std::vector<l1ScoutingRun3::Muon>>(3565);
0011   for (auto& bxVec : orbitBuffer_) {
0012     bxVec.reserve(8);
0013   }
0014   nMuonsOrbit_ = 0;
0015 
0016   produces<l1ScoutingRun3::MuonOrbitCollection>("Muon").setBranchAlias("MuonOrbitCollection");
0017   rawToken = consumes<SDSRawDataCollection>(srcInputTag);
0018 }
0019 
0020 ScGMTRawToDigi::~ScGMTRawToDigi() {}
0021 
0022 void ScGMTRawToDigi::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
0023   using namespace edm;
0024 
0025   Handle<SDSRawDataCollection> ScoutingRawDataCollection;
0026   iEvent.getByToken(rawToken, ScoutingRawDataCollection);
0027 
0028   const FEDRawData& sourceRawData = ScoutingRawDataCollection->FEDData(SDSNumbering::GmtSDSID);
0029   size_t orbitSize = sourceRawData.size();
0030 
0031   std::unique_ptr<l1ScoutingRun3::MuonOrbitCollection> unpackedMuons(new l1ScoutingRun3::MuonOrbitCollection);
0032 
0033   if ((sourceRawData.size() == 0) && debug_) {
0034     std::cout << "No raw data for GMT FED\n";
0035   }
0036 
0037   // unpack current orbit and store data into the orbitBufferr
0038   unpackOrbit(sourceRawData.data(), orbitSize);
0039 
0040   // fill orbit collection and clear the Bx buffer vector
0041   unpackedMuons->fillAndClear(orbitBuffer_, nMuonsOrbit_);
0042 
0043   // store collection in the event
0044   iEvent.put(std::move(unpackedMuons), "Muon");
0045 }
0046 
0047 void ScGMTRawToDigi::unpackOrbit(const unsigned char* buf, size_t len) {
0048   using namespace l1ScoutingRun3;
0049 
0050   // reset counters
0051   nMuonsOrbit_ = 0;
0052 
0053   size_t pos = 0;
0054 
0055   while (pos < len) {
0056     assert(pos + 4 <= len);
0057 
0058     // get BX header
0059     uint32_t header = *((uint32_t*)(buf + pos));
0060     pos += 4;
0061     // count mA and mB
0062     uint32_t mAcount = (header & header_masks::mAcount) >> header_shifts::mAcount;
0063     uint32_t mBcount = (header & header_masks::mBcount) >> header_shifts::mBcount;
0064 
0065     // declare block to read
0066     ugmt::block* bl = (ugmt::block*)(buf + pos);
0067     pos += 4 + 4 + (mAcount + mBcount) * 12;
0068     assert(pos <= len);
0069 
0070     uint32_t orbit = bl->orbit & 0x7FFFFFFF;
0071     uint32_t bx = bl->bx;
0072 
0073     if (debug_) {
0074       std::cout << "GMT Orbit " << orbit << ", BX -> " << bx << ", nMuons -> " << mAcount + mBcount << std::endl;
0075     }
0076 
0077     // Unpack muons for this BX
0078     orbitBuffer_[bx].reserve(mAcount + mBcount);
0079 
0080     for (unsigned int i = 0; i < mAcount + mBcount; i++) {
0081       uint32_t interm = (bl->mu[i].extra >> ugmt::shiftsMuon::interm) & ugmt::masksMuon::interm;
0082       if ((interm == 1) && (skipInterm_)) {
0083         if (debug_) {
0084           std::cout << " -> Excluding intermediate muon\n";
0085         }
0086         continue;
0087       }
0088 
0089       uint32_t index = (bl->mu[i].s >> ugmt::shiftsMuon::index) & ugmt::masksMuon::index;
0090       uint32_t ietaextu = (bl->mu[i].f >> ugmt::shiftsMuon::etaext) & ugmt::masksMuon::etaextv;
0091       int32_t ietaext;
0092       if (((bl->mu[i].f >> ugmt::shiftsMuon::etaext) & ugmt::masksMuon::etaexts) != 0) {
0093         ietaext = ietaextu -= 256;
0094       } else {
0095         ietaext = ietaextu;
0096       }
0097 
0098       // extract pt and quality and apply cut if required
0099       int32_t iptuncon = (bl->mu[i].s >> ugmt::shiftsMuon::ptuncon) & ugmt::masksMuon::ptuncon;
0100       int32_t ipt = (bl->mu[i].f >> ugmt::shiftsMuon::pt) & ugmt::masksMuon::pt;
0101       if ((ipt - 1) < 0) {
0102         continue;
0103       }
0104       uint32_t qual = (bl->mu[i].f >> ugmt::shiftsMuon::qual) & ugmt::masksMuon::qual;
0105 
0106       // extract integer value for extrapolated phi
0107       int32_t iphiext = ((bl->mu[i].f >> ugmt::shiftsMuon::phiext) & ugmt::masksMuon::phiext);
0108 
0109       // extract integer value for extrapolated phi
0110       int32_t idxy = ((bl->mu[i].s >> ugmt::shiftsMuon::dxy) & ugmt::masksMuon::dxy);
0111 
0112       // extract iso bits and charge
0113       uint32_t iso = (bl->mu[i].s >> ugmt::shiftsMuon::iso) & ugmt::masksMuon::iso;
0114       int32_t chrg = 0;
0115       if (((bl->mu[i].s >> ugmt::shiftsMuon::chrgv) & ugmt::masksMuon::chrgv) == 1)
0116         chrg = ((bl->mu[i].s >> ugmt::shiftsMuon::chrg) & ugmt::masksMuon::chrg) == 1 ? -1 : 1;
0117 
0118       // extract eta and phi at muon station
0119       int32_t iphi = (bl->mu[i].s >> ugmt::shiftsMuon::phi) & ugmt::masksMuon::phi;
0120       uint32_t ieta1 = (bl->mu[i].extra >> ugmt::shiftsMuon::eta1) & ugmt::masksMuon::eta;
0121       uint32_t ieta2 = (bl->mu[i].extra >> ugmt::shiftsMuon::eta2) & ugmt::masksMuon::eta;
0122 
0123       uint32_t ieta_u;
0124       int32_t ieta;
0125       // checking if raw eta should be taken from muon 1 or muon 2
0126       if ((bl->mu[i].extra & 0x1) == 0) {
0127         ieta_u = ieta1;
0128       } else {
0129         ieta_u = ieta2;
0130       }
0131 
0132       // two's complement
0133       if (ieta_u > 256) {
0134         ieta = ieta_u - 512;
0135       } else {
0136         ieta = ieta_u;
0137       }
0138 
0139       // increment muon counter
0140       nMuonsOrbit_++;
0141 
0142       l1ScoutingRun3::Muon muon(ipt, ieta, iphi, qual, chrg, chrg != 0, iso, index, ietaext, iphiext, iptuncon, idxy);
0143 
0144       orbitBuffer_[bx].push_back(muon);
0145 
0146       if (debug_) {
0147         std::cout << "--- Muon " << i << " ---\n";
0148         std::cout << "  Raw f:     0x" << std::hex << bl->mu[i].f << std::dec << "\n";
0149         std::cout << "  Raw s:     0x" << std::hex << bl->mu[i].s << std::dec << "\n";
0150         std::cout << "  Raw extra: 0x" << std::hex << bl->mu[i].extra << std::dec << "\n";
0151         printMuon(muon);
0152       }
0153 
0154     }  // end of bx
0155 
0156   }  // end orbit while loop
0157 }
0158 
0159 void ScGMTRawToDigi::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0160   edm::ParameterSetDescription desc;
0161   desc.add<edm::InputTag>("srcInputTag", edm::InputTag("rawDataCollector"));
0162   desc.add<bool>("skipInterm", true);
0163   desc.addUntracked<bool>("debug", false);
0164   descriptions.add("ScGMTRawToDigi", desc);
0165 }
0166 
0167 DEFINE_FWK_MODULE(ScGMTRawToDigi);