Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-02-02 23:43:35

0001 // -*- C++ -*-
0002 //
0003 // Package:    L1Trigger/L1Ntuples
0004 // Class:      L1UpgradeTfMuonTreeProducer
0005 //
0006 /**\class L1UpgradeTfMuonTreeProducer L1UpgradeTfMuonTreeProducer.cc L1Trigger/L1Ntuples/plugins/L1UpgradeTfMuonTreeProducer.cc
0007 
0008 Implementation:
0009      
0010 */
0011 //
0012 // Original Author:
0013 //         Created:
0014 // $Id: L1UpgradeTfMuonTreeProducer.cc,v 1.8 2012/08/29 12:44:03 jbrooke Exp $
0015 //
0016 //
0017 
0018 // system include files
0019 #include <memory>
0020 
0021 // framework
0022 #include "FWCore/Framework/interface/Frameworkfwd.h"
0023 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0024 #include "FWCore/Framework/interface/Event.h"
0025 #include "FWCore/Framework/interface/MakerMacros.h"
0026 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0027 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0028 
0029 // data formats
0030 #include "DataFormats/FEDRawData/interface/FEDHeader.h"
0031 #include "DataFormats/L1TMuon/interface/RegionalMuonCand.h"
0032 
0033 // Needed to get BMTF firmware version
0034 #include "DataFormats/FEDRawData/interface/FEDRawDataCollection.h"
0035 #include "EventFilter/L1TRawToDigi/interface/AMC13Spec.h"
0036 #include "EventFilter/L1TRawToDigi/interface/Block.h"
0037 
0038 // ROOT output stuff
0039 #include "FWCore/ServiceRegistry/interface/Service.h"
0040 #include "CommonTools/UtilAlgos/interface/TFileService.h"
0041 #include "TTree.h"
0042 
0043 #include "L1Trigger/L1TNtuples/interface/L1AnalysisL1UpgradeTfMuon.h"
0044 #include "L1Trigger/L1TNtuples/interface/L1AnalysisBMTFInputs.h"
0045 
0046 //
0047 // class declaration
0048 //
0049 
0050 class L1UpgradeTfMuonTreeProducer : public edm::one::EDAnalyzer<edm::one::SharedResources> {
0051 public:
0052   explicit L1UpgradeTfMuonTreeProducer(const edm::ParameterSet&);
0053   ~L1UpgradeTfMuonTreeProducer() override = default;
0054 
0055 private:
0056   void beginJob(void) override;
0057   void analyze(const edm::Event&, const edm::EventSetup&) override;
0058   void endJob() override;
0059 
0060 public:
0061   L1Analysis::L1AnalysisL1UpgradeTfMuon l1UpgradeKBmtf;
0062   L1Analysis::L1AnalysisL1UpgradeTfMuon l1UpgradeBmtf;
0063   L1Analysis::L1AnalysisL1UpgradeTfMuon l1UpgradeOmtf;
0064   L1Analysis::L1AnalysisL1UpgradeTfMuon l1UpgradeEmtf;
0065   L1Analysis::L1AnalysisBMTFInputs l1UpgradeBmtfInputs;
0066   L1Analysis::L1AnalysisL1UpgradeTfMuonDataFormat* l1UpgradeBmtfData;
0067   L1Analysis::L1AnalysisL1UpgradeTfMuonDataFormat* l1UpgradeKBmtfData;
0068   L1Analysis::L1AnalysisL1UpgradeTfMuonDataFormat* l1UpgradeOmtfData;
0069   L1Analysis::L1AnalysisL1UpgradeTfMuonDataFormat* l1UpgradeEmtfData;
0070   L1Analysis::L1AnalysisBMTFInputsDataFormat* l1UpgradeBmtfInputsData;
0071 
0072 private:
0073   unsigned maxL1UpgradeTfMuon_;
0074   bool isEMU_;
0075 
0076   // output file
0077   edm::Service<TFileService> fs_;
0078 
0079   // tree
0080   TTree* tree_;
0081 
0082   // EDM input tags
0083   const edm::EDGetTokenT<FEDRawDataCollection> fedToken_;
0084   const edm::EDGetTokenT<l1t::RegionalMuonCandBxCollection> bmtfMuonToken_;
0085   const edm::EDGetTokenT<l1t::RegionalMuonCandBxCollection> bmtf2MuonToken_;
0086   const edm::EDGetTokenT<l1t::RegionalMuonCandBxCollection> omtfMuonToken_;
0087   const edm::EDGetTokenT<l1t::RegionalMuonCandBxCollection> emtfMuonToken_;
0088 
0089   const edm::EDGetTokenT<L1MuDTChambPhContainer> bmtfPhInputToken_;
0090   const edm::EDGetTokenT<L1MuDTChambThContainer> bmtfThInputToken_;
0091 
0092   // EDM handles
0093   edm::Handle<FEDRawDataCollection> feds_;
0094 
0095   unsigned getAlgoFwVersion();
0096 };
0097 
0098 L1UpgradeTfMuonTreeProducer::L1UpgradeTfMuonTreeProducer(const edm::ParameterSet& iConfig)
0099     : fedToken_(consumes<FEDRawDataCollection>(iConfig.getParameter<edm::InputTag>("feds"))),
0100       bmtfMuonToken_(
0101           consumes<l1t::RegionalMuonCandBxCollection>(iConfig.getUntrackedParameter<edm::InputTag>("bmtfMuonToken"))),
0102       bmtf2MuonToken_(
0103           consumes<l1t::RegionalMuonCandBxCollection>(iConfig.getUntrackedParameter<edm::InputTag>("bmtf2MuonToken"))),
0104       omtfMuonToken_(
0105           consumes<l1t::RegionalMuonCandBxCollection>(iConfig.getUntrackedParameter<edm::InputTag>("omtfMuonToken"))),
0106       emtfMuonToken_(
0107           consumes<l1t::RegionalMuonCandBxCollection>(iConfig.getUntrackedParameter<edm::InputTag>("emtfMuonToken"))),
0108       bmtfPhInputToken_(
0109           consumes<L1MuDTChambPhContainer>(iConfig.getUntrackedParameter<edm::InputTag>("bmtfInputPhMuonToken"))),
0110       bmtfThInputToken_(
0111           consumes<L1MuDTChambThContainer>(iConfig.getUntrackedParameter<edm::InputTag>("bmtfInputThMuonToken"))) {
0112   isEMU_ = iConfig.getParameter<bool>("isEMU");
0113   maxL1UpgradeTfMuon_ = iConfig.getParameter<unsigned int>("maxL1UpgradeTfMuon");
0114 
0115   l1UpgradeBmtfData = l1UpgradeBmtf.getData();
0116   l1UpgradeKBmtfData = l1UpgradeKBmtf.getData();
0117   l1UpgradeOmtfData = l1UpgradeOmtf.getData();
0118   l1UpgradeEmtfData = l1UpgradeEmtf.getData();
0119   l1UpgradeBmtfInputsData = l1UpgradeBmtfInputs.getData();
0120 
0121   usesResource(TFileService::kSharedResource);
0122 
0123   // set up output
0124   tree_ = fs_->make<TTree>("L1UpgradeTfMuonTree", "L1UpgradeTfMuonTree");
0125   tree_->Branch("L1UpgradeBmtfMuon", "L1Analysis::L1AnalysisL1UpgradeTfMuonDataFormat", &l1UpgradeBmtfData, 32000, 3);
0126   tree_->Branch("L1UpgradeKBmtfMuon", "L1Analysis::L1AnalysisL1UpgradeTfMuonDataFormat", &l1UpgradeKBmtfData, 32000, 3);
0127   tree_->Branch("L1UpgradeOmtfMuon", "L1Analysis::L1AnalysisL1UpgradeTfMuonDataFormat", &l1UpgradeOmtfData, 32000, 3);
0128   tree_->Branch("L1UpgradeEmtfMuon", "L1Analysis::L1AnalysisL1UpgradeTfMuonDataFormat", &l1UpgradeEmtfData, 32000, 3);
0129 
0130   tree_->Branch(
0131       "L1UpgradeBmtfInputs", "L1Analysis::L1AnalysisBMTFInputsDataFormat", &l1UpgradeBmtfInputsData, 32000, 3);
0132 }
0133 
0134 //
0135 // member functions
0136 //
0137 
0138 // ------------ method called to for each event  ------------
0139 void L1UpgradeTfMuonTreeProducer::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
0140   edm::EDGetTokenT<l1t::RegionalMuonCandBxCollection> legacybmtfMuonToken;
0141   edm::EDGetTokenT<l1t::RegionalMuonCandBxCollection> kbmtfMuonToken;
0142   if (isEMU_) {
0143     legacybmtfMuonToken = bmtfMuonToken_;
0144     kbmtfMuonToken = bmtf2MuonToken_;
0145   } else {
0146     iEvent.getByToken(fedToken_, feds_);
0147     // Get fw version
0148     unsigned algoFwVersion{getAlgoFwVersion()};
0149     if (algoFwVersion < 2499805536) {  //95000160(hex)
0150       // Legacy was triggering (and therefore in the main collection)
0151       legacybmtfMuonToken = bmtfMuonToken_;
0152       kbmtfMuonToken = bmtf2MuonToken_;
0153     } else {
0154       // kBMTF was triggering
0155       legacybmtfMuonToken = bmtf2MuonToken_;
0156       kbmtfMuonToken = bmtfMuonToken_;
0157     }
0158   }
0159 
0160   l1UpgradeBmtf.Reset();
0161   l1UpgradeKBmtf.Reset();
0162   l1UpgradeKBmtf.SetRun3Muons();
0163   l1UpgradeOmtf.Reset();
0164   l1UpgradeEmtf.Reset();
0165   l1UpgradeEmtf.SetRun3Muons();
0166   l1UpgradeBmtfInputs.Reset();
0167 
0168   edm::Handle<l1t::RegionalMuonCandBxCollection> bmtfMuon;
0169   edm::Handle<l1t::RegionalMuonCandBxCollection> kbmtfMuon;
0170   edm::Handle<l1t::RegionalMuonCandBxCollection> omtfMuon;
0171   edm::Handle<l1t::RegionalMuonCandBxCollection> emtfMuon;
0172   edm::Handle<L1MuDTChambPhContainer> bmtfPhInputs;
0173   edm::Handle<L1MuDTChambThContainer> bmtfThInputs;
0174 
0175   iEvent.getByToken(legacybmtfMuonToken, bmtfMuon);
0176   iEvent.getByToken(kbmtfMuonToken, kbmtfMuon);
0177   iEvent.getByToken(omtfMuonToken_, omtfMuon);
0178   iEvent.getByToken(emtfMuonToken_, emtfMuon);
0179   iEvent.getByToken(bmtfPhInputToken_, bmtfPhInputs);
0180   iEvent.getByToken(bmtfThInputToken_, bmtfThInputs);
0181 
0182   if (bmtfMuon.isValid()) {
0183     l1UpgradeBmtf.SetTfMuon(*bmtfMuon, maxL1UpgradeTfMuon_);
0184   } else {
0185     edm::LogWarning("MissingProduct") << "L1Upgrade BMTF muons not found. Branch will not be filled" << std::endl;
0186   }
0187 
0188   if (kbmtfMuon.isValid()) {
0189     l1UpgradeKBmtf.SetTfMuon(*kbmtfMuon, maxL1UpgradeTfMuon_);
0190   } else {
0191     edm::LogWarning("MissingProduct") << "L1Upgrade kBMTF muons not found. Branch will not be filled" << std::endl;
0192   }
0193 
0194   if (omtfMuon.isValid()) {
0195     l1UpgradeOmtf.SetTfMuon(*omtfMuon, maxL1UpgradeTfMuon_);
0196   } else {
0197     edm::LogWarning("MissingProduct") << "L1Upgrade OMTF muons not found. Branch will not be filled" << std::endl;
0198   }
0199 
0200   if (emtfMuon.isValid()) {
0201     l1UpgradeEmtf.SetTfMuon(*emtfMuon, maxL1UpgradeTfMuon_);
0202   } else {
0203     edm::LogWarning("MissingProduct") << "L1Upgrade EMTF muons not found. Branch will not be filled" << std::endl;
0204   }
0205 
0206   int max_inputs = maxL1UpgradeTfMuon_ * 4;
0207 
0208   if (!bmtfPhInputs.isValid()) {
0209     edm::LogWarning("MissingProduct") << "L1Upgrade BMTF Ph Inputs not found. Branch will not be filled" << std::endl;
0210   } else
0211     l1UpgradeBmtfInputs.SetBMPH(bmtfPhInputs, max_inputs);
0212 
0213   if (!bmtfThInputs.isValid()) {
0214     edm::LogWarning("MissingProduct") << "L1Upgrade BMTF Th Inputs not found. Branch will not be filled" << std::endl;
0215   } else
0216     l1UpgradeBmtfInputs.SetBMTH(bmtfThInputs, max_inputs);
0217 
0218   tree_->Fill();
0219 }
0220 
0221 // ------------ method called once each job just before starting event loop  ------------
0222 void L1UpgradeTfMuonTreeProducer::beginJob(void) {}
0223 
0224 // ------------ method called once each job just after ending the event loop  ------------
0225 void L1UpgradeTfMuonTreeProducer::endJob() {}
0226 
0227 unsigned L1UpgradeTfMuonTreeProducer::getAlgoFwVersion() {
0228   int nonEmptyFed = 0;
0229   if (feds_->FEDData(1376).size() > 0)
0230     nonEmptyFed = 1376;
0231   else if (feds_->FEDData(1377).size() > 0)
0232     nonEmptyFed = 1377;
0233   else {
0234     edm::LogError("L1UpgradeTfMuonTreeProducer")
0235         << "Both BMTF feds (1376, 1377) seem empty, will lead to unexpected results in tree from data.";
0236     return 0;
0237   }
0238 
0239   const FEDRawData& l1tRcd = feds_->FEDData(nonEmptyFed);
0240   edm::LogInfo("L1UpgradeTfMuonTreeProducer") << "L1T Rcd taken from the FEDData.";
0241   edm::LogInfo("L1UpgradeTfMuonTreeProducer") << "l1tRcd.size=" << l1tRcd.size() << "   for fed:" << nonEmptyFed;
0242 
0243   const unsigned char* data = l1tRcd.data();
0244   FEDHeader header(data);
0245 
0246   amc13::Packet packet;
0247   if (!packet.parse((const uint64_t*)data,
0248                     (const uint64_t*)(data + 8),
0249                     (l1tRcd.size()) / 8,
0250                     header.lvl1ID(),
0251                     header.bxID(),
0252                     false,
0253                     false)) {
0254     edm::LogError("L1UpgradeTfMuonTreeProducer") << "Could not extract AMC13 Packet.";
0255     return 0;
0256   }
0257 
0258   if (!packet.payload().empty()) {
0259     auto payload64 = (packet.payload().at(0)).data();
0260     const uint32_t* start = (const uint32_t*)payload64.get();
0261     const uint32_t* end = start + (packet.payload().at(0).size() * 2);
0262 
0263     l1t::MP7Payload payload(start, end, false);
0264     return payload.getAlgorithmFWVersion();
0265 
0266   } else {
0267     edm::LogError("L1UpgradeTfMuonTreeProducer") << "AMC13 payload is empty, cannot extract AMC13 Packet.";
0268     return 0;
0269   }
0270 }
0271 
0272 //define this as a plug-in
0273 DEFINE_FWK_MODULE(L1UpgradeTfMuonTreeProducer);