Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:20:31

0001 ///
0002 /// \class l1t::L1TExtCondProducer
0003 ///
0004 /// Description: Fill uGT external condition to allow testing stage 2 algos, e.g. Bptx
0005 ///
0006 ///
0007 /// \author: D. Puigh OSU
0008 ///
0009 
0010 // System include files
0011 // User include files
0012 #include "FWCore/Framework/interface/EventSetup.h"
0013 #include "FWCore/Framework/interface/Frameworkfwd.h"
0014 #include "FWCore/Framework/interface/stream/EDProducer.h"
0015 #include "FWCore/Framework/interface/ESHandle.h"
0016 #include "FWCore/Framework/interface/Event.h"
0017 #include "FWCore/Framework/interface/MakerMacros.h"
0018 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0019 #include "FWCore/Utilities/interface/EDGetToken.h"
0020 #include "FWCore/Utilities/interface/InputTag.h"
0021 #include "FWCore/Utilities/interface/ESGetToken.h"
0022 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0023 
0024 #include "CondFormats/L1TObjects/interface/L1TUtmTriggerMenu.h"
0025 #include "CondFormats/DataRecord/interface/L1TUtmTriggerMenuRcd.h"
0026 #include "L1Trigger/L1TGlobal/plugins/TriggerMenuParser.h"
0027 
0028 #include <FWCore/ParameterSet/interface/ConfigurationDescriptions.h>
0029 #include <FWCore/ParameterSet/interface/ParameterSetDescription.h>
0030 
0031 #include "DataFormats/L1Trigger/interface/BXVector.h"
0032 #include "DataFormats/L1TGlobal/interface/GlobalExtBlk.h"
0033 #include "DataFormats/TCDS/interface/TCDSRecord.h"
0034 
0035 #include <vector>
0036 
0037 using namespace std;
0038 using namespace edm;
0039 using namespace l1t;
0040 
0041 //
0042 // Class declaration
0043 //
0044 
0045 class L1TExtCondProducer : public stream::EDProducer<> {
0046 public:
0047   explicit L1TExtCondProducer(const ParameterSet&);
0048   ~L1TExtCondProducer() override;
0049 
0050   static void fillDescriptions(ConfigurationDescriptions& descriptions);
0051 
0052 private:
0053   void produce(edm::Event&, const edm::EventSetup&) override;
0054 
0055   // ---------- Member data ---------------------------
0056   // unsigned long long m_paramsCacheId; // Cache-ID from current parameters, to check if needs to be updated.
0057   //std::shared_ptr<const CaloParams> m_dbpars; // Database parameters for the trigger, to be updated as needed.
0058   //std::shared_ptr<const FirmwareVersion> m_fwv;
0059   //std::shared_ptr<FirmwareVersion> m_fwv; //not const during testing.
0060 
0061   // BX parameters
0062   int bxFirst_;
0063   int bxLast_;
0064 
0065   bool setBptxAND_;
0066   bool setBptxPlus_;
0067   bool setBptxMinus_;
0068   bool setBptxOR_;
0069 
0070   unsigned long long m_l1GtMenuCacheID;
0071   std::map<std::string, unsigned int> m_extBitMap;
0072 
0073   unsigned int m_triggerRulePrefireVetoBit;
0074 
0075   bool makeTriggerRulePrefireVetoBit_;
0076   edm::EDGetTokenT<TCDSRecord> tcdsRecordToken_;
0077   edm::InputTag tcdsInputTag_;
0078   edm::ESGetToken<L1TUtmTriggerMenu, L1TUtmTriggerMenuRcd> l1GtMenuToken_;
0079 };
0080 
0081 //
0082 // Constructors and destructor
0083 //
0084 L1TExtCondProducer::L1TExtCondProducer(const ParameterSet& iConfig)
0085     : bxFirst_(iConfig.getParameter<int>("bxFirst")),
0086       bxLast_(iConfig.getParameter<int>("bxLast")),
0087       setBptxAND_(iConfig.getParameter<bool>("setBptxAND")),
0088       setBptxPlus_(iConfig.getParameter<bool>("setBptxPlus")),
0089       setBptxMinus_(iConfig.getParameter<bool>("setBptxMinus")),
0090       setBptxOR_(iConfig.getParameter<bool>("setBptxOR")),
0091       tcdsInputTag_(iConfig.getParameter<edm::InputTag>("tcdsRecordLabel")),
0092       l1GtMenuToken_(esConsumes<L1TUtmTriggerMenu, L1TUtmTriggerMenuRcd>()) {
0093   makeTriggerRulePrefireVetoBit_ = false;
0094 
0095   m_triggerRulePrefireVetoBit = GlobalExtBlk::maxExternalConditions - 1;
0096 
0097   tcdsRecordToken_ = consumes<TCDSRecord>(tcdsInputTag_);
0098   // Note that the tcdsRecord input tag should be used as InputTag("unpackTcds","tcdsRecord") only for data
0099   if (!(tcdsInputTag_ == edm::InputTag(""))) {
0100     makeTriggerRulePrefireVetoBit_ = true;
0101   }
0102 
0103   // Register what you produce
0104   produces<GlobalExtBlkBxCollection>();
0105 
0106   // Initialize parameters
0107   m_l1GtMenuCacheID = 0ULL;
0108 }
0109 
0110 L1TExtCondProducer::~L1TExtCondProducer() {}
0111 
0112 //
0113 // Member functions
0114 //
0115 
0116 // ------------ method called to produce the data ------------
0117 void L1TExtCondProducer::produce(Event& iEvent, const EventSetup& iSetup) {
0118   LogDebug("L1TExtCondProducer") << "L1TExtCondProducer::produce function called...\n";
0119 
0120   // Get / update the trigger menu from the EventSetup
0121   // local cache & check on cacheIdentifier
0122   unsigned long long l1GtMenuCacheID = iSetup.get<L1TUtmTriggerMenuRcd>().cacheIdentifier();
0123 
0124   if (m_l1GtMenuCacheID != l1GtMenuCacheID) {
0125     edm::ESHandle<L1TUtmTriggerMenu> l1GtMenu = iSetup.getHandle(l1GtMenuToken_);
0126     const L1TUtmTriggerMenu* utml1GtMenu = l1GtMenu.product();
0127 
0128     // Instantiate Parser
0129     TriggerMenuParser gtParser = TriggerMenuParser();
0130 
0131     std::map<std::string, unsigned int> extBitMap = gtParser.getExternalSignals(utml1GtMenu);
0132 
0133     m_l1GtMenuCacheID = l1GtMenuCacheID;
0134     m_extBitMap = extBitMap;
0135   }
0136 
0137   edm::Handle<TCDSRecord> tcdsRecordH;
0138   iEvent.getByToken(tcdsRecordToken_, tcdsRecordH);
0139   makeTriggerRulePrefireVetoBit_ = makeTriggerRulePrefireVetoBit_ && tcdsRecordH.isValid();
0140 
0141   bool TriggerRulePrefireVetoBit(false);
0142   // The following list of checks on the tcdsRecord is relevant only for data;
0143   // code taken from Nick Smith's EventFilter/L1TRawToDigi/plugins/TriggerRulePrefireVetoFilter.cc
0144   if (iEvent.isRealData() && makeTriggerRulePrefireVetoBit_) {
0145     const auto& tcdsRecord = *tcdsRecordH.product();
0146 
0147     uint64_t thisEvent = (tcdsRecord.getBXID() - 1) + tcdsRecord.getOrbitNr() * 3564ull;
0148 
0149     std::vector<uint64_t> eventHistory;
0150     for (auto&& l1a : tcdsRecord.getFullL1aHistory()) {
0151       eventHistory.push_back(thisEvent - ((l1a.getBXID() - 1) + l1a.getOrbitNr() * 3564ull));
0152     }
0153 
0154     // It should be 16 according to TCDSRecord.h, we only care about the last 4
0155     if (eventHistory.size() >= 4) {
0156       // No more than 1 L1A in 3 BX
0157       if (eventHistory[0] < 3ull) {
0158         edm::LogError("L1TExtCondProducer") << "Found an L1A in an impossible location?! (1 in 3)";
0159       }
0160 
0161       if (eventHistory[0] == 3ull)
0162         TriggerRulePrefireVetoBit = true;
0163 
0164       // No more than 2 L1As in 25 BX
0165       if (eventHistory[0] < 25ull and eventHistory[1] < 25ull) {
0166         edm::LogError("L1TExtCondProducer") << "Found an L1A in an impossible location?! (2 in 25)";
0167       }
0168       if (eventHistory[0] < 25ull and eventHistory[1] == 25ull)
0169         TriggerRulePrefireVetoBit = true;
0170 
0171       // No more than 3 L1As in 100 BX
0172       if (eventHistory[0] < 100ull and eventHistory[1] < 100ull and eventHistory[2] <= 100ull) {
0173         if (eventHistory[2] == 100ull)
0174           TriggerRulePrefireVetoBit = true;
0175         else
0176           edm::LogError("L1TExtCondProducer") << "Found an L1A in an impossible location?! (3 in 100)";
0177       }
0178 
0179       // No more than 4 L1As in 240 BX
0180       if (eventHistory[0] < 240ull and eventHistory[1] < 240ull and eventHistory[2] < 240ull and
0181           eventHistory[3] <= 240ull) {
0182         if (eventHistory[3] == 240ull)
0183           TriggerRulePrefireVetoBit = true;
0184         else
0185           edm::LogError("L1TExtCondProducer") << "Found an L1A in an impossible location?! (4 in 240)";
0186       }
0187     }
0188   }
0189   // Setup vectors
0190   GlobalExtBlk extCond_bx;
0191 
0192   // Outputs
0193   std::unique_ptr<GlobalExtBlkBxCollection> extCond(new GlobalExtBlkBxCollection(0, bxFirst_, bxLast_));
0194 
0195   bool foundBptxAND = (m_extBitMap.find("BPTX_plus_AND_minus.v0") != m_extBitMap.end());
0196   bool foundBptxPlus = (m_extBitMap.find("BPTX_plus.v0") != m_extBitMap.end());
0197   bool foundBptxMinus = (m_extBitMap.find("BPTX_minus.v0") != m_extBitMap.end());
0198   bool foundBptxOR = (m_extBitMap.find("BPTX_plus_OR_minus.v0") != m_extBitMap.end());
0199 
0200   // Fill in some external conditions for testing
0201   if (setBptxAND_ && foundBptxAND)
0202     extCond_bx.setExternalDecision(m_extBitMap["BPTX_plus_AND_minus.v0"], true);
0203   if (setBptxPlus_ && foundBptxPlus)
0204     extCond_bx.setExternalDecision(m_extBitMap["BPTX_plus.v0"], true);
0205   if (setBptxMinus_ && foundBptxMinus)
0206     extCond_bx.setExternalDecision(m_extBitMap["BPTX_minus.v0"], true);
0207   if (setBptxOR_ && foundBptxOR)
0208     extCond_bx.setExternalDecision(m_extBitMap["BPTX_plus_OR_minus.v0"], true);
0209 
0210   // Check for updated Bptx names as well
0211   foundBptxAND = (m_extBitMap.find("ZeroBias_BPTX_AND_VME") != m_extBitMap.end());
0212   foundBptxPlus = (m_extBitMap.find("BPTX_B1_VME") != m_extBitMap.end());
0213   foundBptxMinus = (m_extBitMap.find("BPTX_B2_VME") != m_extBitMap.end());
0214   foundBptxOR = (m_extBitMap.find("BPTX_OR_VME") != m_extBitMap.end());
0215 
0216   // Fill in some external conditions for testing
0217   if (setBptxAND_ && foundBptxAND)
0218     extCond_bx.setExternalDecision(m_extBitMap["ZeroBias_BPTX_AND_VME"], true);
0219   if (setBptxPlus_ && foundBptxPlus)
0220     extCond_bx.setExternalDecision(m_extBitMap["BPTX_B1_VME"], true);
0221   if (setBptxMinus_ && foundBptxMinus)
0222     extCond_bx.setExternalDecision(m_extBitMap["BPTX_B2_VME"], true);
0223   if (setBptxOR_ && foundBptxOR)
0224     extCond_bx.setExternalDecision(m_extBitMap["BPTX_OR_VME"], true);
0225 
0226   // Set the bit for the TriggerRulePrefireVeto if true
0227   if (TriggerRulePrefireVetoBit)
0228     extCond_bx.setExternalDecision(m_triggerRulePrefireVetoBit, true);
0229 
0230   // Fill Externals
0231   for (int iBx = bxFirst_; iBx <= bxLast_; iBx++) {
0232     extCond->push_back(iBx, extCond_bx);
0233   }
0234 
0235   iEvent.put(std::move(extCond));
0236 }
0237 
0238 // ------------ Method fills 'descriptions' with the allowed parameters for the module ------------
0239 void L1TExtCondProducer::fillDescriptions(ConfigurationDescriptions& descriptions) {
0240   // simGtExtFakeProd
0241   edm::ParameterSetDescription desc;
0242   desc.add<bool>("setBptxMinus", true);
0243   desc.add<bool>("setBptxAND", true);
0244   desc.add<int>("bxFirst", -2);
0245   desc.add<bool>("setBptxOR", true);
0246   desc.add<int>("bxLast", 2);
0247   desc.add<bool>("setBptxPlus", true);
0248   desc.add<edm::InputTag>("tcdsRecordLabel", edm::InputTag(""));
0249   descriptions.add("simGtExtFakeProd", desc);
0250 }
0251 
0252 // Define this as a plug-in
0253 DEFINE_FWK_MODULE(L1TExtCondProducer);