Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:08:58

0001 // -*- C++ -*-
0002 //
0003 // Package:    SiStripTools
0004 // Class:      APVCyclePhaseProducerFromL1TS
0005 //
0006 /**\class APVCyclePhaseProducerFromL1TS APVCyclePhaseProducerFromL1TS.cc DPGAnalysis/SiStripTools/plugins/APVCyclePhaseProducerFromL1TS.cc
0007 
0008  Description: EDproducer for APVCyclePhaseCollection which uses the configuration file to assign a phase to the run
0009 
0010  Implementation:
0011      <Notes on implementation>
0012 */
0013 //
0014 // Original Author:  Andrea Venturi
0015 //         Created:  Mon Jan 12 09:05:45 CET 2009
0016 //
0017 //
0018 
0019 // system include files
0020 #include <memory>
0021 
0022 // user include files
0023 #include "FWCore/Framework/interface/Frameworkfwd.h"
0024 #include "FWCore/Framework/interface/stream/EDProducer.h"
0025 
0026 #include "FWCore/Framework/interface/Event.h"
0027 #include "FWCore/Framework/interface/Run.h"
0028 #include "FWCore/Framework/interface/MakerMacros.h"
0029 
0030 #include "FWCore/Framework/interface/ESHandle.h"
0031 #include "FWCore/Framework/interface/ESWatcher.h"
0032 #include "FWCore/Framework/interface/EventSetup.h"
0033 
0034 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0035 
0036 #include "FWCore/Utilities/interface/InputTag.h"
0037 
0038 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0039 #include "FWCore/Utilities/interface/Exception.h"
0040 
0041 #include <map>
0042 #include <vector>
0043 #include <utility>
0044 #include <string>
0045 #include <iostream>
0046 
0047 #include "CondFormats/SiStripObjects/interface/SiStripConfObject.h"
0048 #include "CondFormats/DataRecord/interface/SiStripCondDataRecords.h"
0049 
0050 #include "DataFormats/Scalers/interface/Level1TriggerScalers.h"
0051 #include "DataFormats/TCDS/interface/TCDSRecord.h"
0052 #include "DPGAnalysis/SiStripTools/interface/APVCyclePhaseCollection.h"
0053 
0054 //
0055 // class declaration
0056 //
0057 
0058 class APVCyclePhaseProducerFromL1TS : public edm::stream::EDProducer<> {
0059 public:
0060   explicit APVCyclePhaseProducerFromL1TS(const edm::ParameterSet&);
0061   ~APVCyclePhaseProducerFromL1TS() override;
0062 
0063 private:
0064   void beginRun(const edm::Run&, const edm::EventSetup&) override;
0065   void produce(edm::Event&, const edm::EventSetup&) override;
0066 
0067   bool isBadRun(const unsigned int) const;
0068   void printConfiguration(std::stringstream& ss) const;
0069 
0070   // ----------member data ---------------------------
0071 
0072   const bool m_ignoreDB;
0073   edm::ESWatcher<SiStripConfObjectRcd> m_eswatcher;
0074   edm::EDGetTokenT<Level1TriggerScalersCollection> _l1tscollectionToken;
0075   edm::EDGetTokenT<TCDSRecord> _tcdsRecordToken;
0076   edm::ESGetToken<SiStripConfObject, SiStripConfObjectRcd> _confObjectToken;
0077   edm::ESGetToken<TrackerTopology, TrackerTopologyRcd> _tTopoToken;
0078   std::vector<std::string> _defpartnames;
0079   std::vector<int> _defphases;
0080   bool _useEC0;
0081   int _magicOffset;
0082   bool m_badRun;
0083   const bool _forceSCAL;
0084 
0085   std::vector<std::pair<unsigned int, unsigned int> > m_badruns;
0086 
0087   long long _lastResync;
0088   long long _lastHardReset;
0089   long long _lastStart;
0090   long long _lastEventCounter0;
0091   long long _lastOrbitCounter0;
0092   long long _lastTestEnable;
0093 };
0094 
0095 //
0096 // constants, enums and typedefs
0097 //
0098 
0099 //
0100 // static data member definitions
0101 //
0102 
0103 //
0104 // constructors and destructor
0105 //
0106 APVCyclePhaseProducerFromL1TS::APVCyclePhaseProducerFromL1TS(const edm::ParameterSet& iConfig)
0107     : m_ignoreDB(iConfig.getUntrackedParameter<bool>("ignoreDB", false)),
0108       m_eswatcher(),
0109       _l1tscollectionToken(
0110           consumes<Level1TriggerScalersCollection>(iConfig.getParameter<edm::InputTag>("l1TSCollection"))),
0111       _tcdsRecordToken(consumes<TCDSRecord>(iConfig.getParameter<edm::InputTag>("tcdsRecordLabel"))),
0112       _confObjectToken((!m_ignoreDB)
0113                            ? decltype(_confObjectToken){esConsumes<edm::Transition::BeginRun>(edm::ESInputTag{
0114                                  "", iConfig.getUntrackedParameter<std::string>("recordLabel", "apvphaseoffsets")})}
0115                            : decltype(_confObjectToken){}),
0116       _tTopoToken((!m_ignoreDB) ? decltype(_tTopoToken){esConsumes<edm::Transition::BeginRun>()}
0117                                 : decltype(_tTopoToken){}),
0118       _defpartnames(iConfig.getParameter<std::vector<std::string> >("defaultPartitionNames")),
0119       _defphases(iConfig.getParameter<std::vector<int> >("defaultPhases")),
0120       _useEC0(iConfig.getUntrackedParameter<bool>("useEC0", false)),
0121       _magicOffset(iConfig.getUntrackedParameter<int>("magicOffset", 8)),
0122       m_badRun(false),
0123       _forceSCAL(iConfig.getParameter<bool>("forceSCAL")),
0124       m_badruns(),
0125       _lastResync(-1),
0126       _lastHardReset(-1),
0127       _lastStart(-1),
0128       _lastEventCounter0(-1),
0129       _lastOrbitCounter0(-1),
0130       _lastTestEnable(-1) {
0131   std::stringstream ss;
0132   printConfiguration(ss);
0133   edm::LogInfo("ConfigurationAtConstruction") << ss.str();
0134 
0135   produces<APVCyclePhaseCollection, edm::InEvent>();
0136 
0137   m_badruns.push_back(std::pair<unsigned int, unsigned int>(0, 131767));
0138   m_badruns.push_back(std::pair<unsigned int, unsigned int>(193150, 193733));
0139 
0140   //now do what ever other initialization is needed
0141 }
0142 
0143 APVCyclePhaseProducerFromL1TS::~APVCyclePhaseProducerFromL1TS() {
0144   // do anything here that needs to be done at desctruction time
0145   // (e.g. close files, deallocate resources etc.)
0146 }
0147 
0148 //
0149 // member functions
0150 //
0151 
0152 // ------------ method called to produce the data  ------------
0153 void APVCyclePhaseProducerFromL1TS::beginRun(const edm::Run& iRun, const edm::EventSetup& iSetup)
0154 
0155 {
0156   // update the parameters from DB
0157 
0158   if (!m_ignoreDB && m_eswatcher.check(iSetup)) {
0159     const auto& confObj = iSetup.getData(_confObjectToken);
0160 
0161     std::stringstream summary;
0162     confObj.printDebug(summary, &iSetup.getData(_tTopoToken));
0163     LogDebug("SiStripConfObjectSummary") << summary.str();
0164 
0165     _defpartnames = confObj.get<std::vector<std::string> >("defaultPartitionNames");
0166     _defphases = confObj.get<std::vector<int> >("defaultPhases");
0167     _useEC0 = confObj.get<bool>("useEC0");
0168     m_badRun = confObj.get<bool>("badRun");
0169     _magicOffset = confObj.get<int>("magicOffset");
0170 
0171     std::stringstream ss;
0172     printConfiguration(ss);
0173     edm::LogInfo("UpdatedConfiguration") << ss.str();
0174   }
0175 
0176   if (isBadRun(iRun.run())) {
0177     LogDebug("UnreliableMissingL1TriggerScalers") << "In this run L1TriggerScalers is missing or unreliable for phase "
0178                                                      "determination: invlid phase will be returned";
0179   }
0180 }
0181 
0182 void APVCyclePhaseProducerFromL1TS::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
0183   using namespace edm;
0184 
0185   std::unique_ptr<APVCyclePhaseCollection> apvphases(new APVCyclePhaseCollection());
0186 
0187   std::vector<int> phases(_defphases.size(), APVCyclePhaseCollection::invalid);
0188 
0189   const std::vector<std::string>& partnames = _defpartnames;
0190 
0191   int phasechange = 0;
0192 
0193   Handle<Level1TriggerScalersCollection> l1ts;
0194   iEvent.getByToken(_l1tscollectionToken, l1ts);
0195   Handle<TCDSRecord> tcds_pIn;
0196   iEvent.getByToken(_tcdsRecordToken, tcds_pIn);
0197   bool useTCDS(tcds_pIn.isValid() && !_forceSCAL);
0198   const auto* tcdsRecord = useTCDS ? tcds_pIn.product() : nullptr;
0199   // offset computation
0200 
0201   long long orbitoffset = 0;
0202 
0203   if (useTCDS && iEvent.eventAuxiliary().isRealData()) {
0204     // l1ts->empty() always retuns false (last commit as of today: https://github.com/cms-sw/cmssw/commit/f4694d795d4b268d541c633dfb68283d889264b0 ), so the check is likely not necessary---and TCDSRecord hasn't anything similar
0205     if (tcdsRecord->getLastResync() != 0) {
0206       orbitoffset =
0207           _useEC0 ? tcdsRecord->getLastEventCounter0() + _magicOffset : tcdsRecord->getLastResync() + _magicOffset;
0208     }
0209 
0210     if (_lastResync != tcdsRecord->getLastResync()) {
0211       _lastResync = tcdsRecord->getLastResync();
0212       LogDebug("TTCSignalReceived") << "New Resync at orbit " << _lastResync;
0213     }
0214     if (_lastHardReset != tcdsRecord->getLastHardReset()) {
0215       _lastHardReset = tcdsRecord->getLastHardReset();
0216       LogDebug("TTCSignalReceived") << "New HardReset at orbit " << _lastHardReset;
0217     }
0218     if (_lastTestEnable != tcdsRecord->getLastTestEnable()) {
0219       _lastTestEnable = tcdsRecord->getLastTestEnable();
0220       //      LogDebug("TTCSignalReceived") << "New TestEnable at orbit " << _lastTestEnable ;
0221     }
0222     if (_lastOrbitCounter0 != tcdsRecord->getLastOrbitCounter0()) {
0223       _lastOrbitCounter0 = tcdsRecord->getLastOrbitCounter0();
0224       LogDebug("TTCSignalReceived") << "New OrbitCounter0 at orbit " << _lastOrbitCounter0;
0225     }
0226     if (_lastEventCounter0 != tcdsRecord->getLastEventCounter0()) {
0227       _lastEventCounter0 = tcdsRecord->getLastEventCounter0();
0228       LogDebug("TTCSignalReceived") << "New EventCounter0 at orbit " << _lastEventCounter0;
0229     }
0230     if (_lastStart != tcdsRecord->getLastStart()) {
0231       _lastStart = tcdsRecord->getLastStart();
0232       LogDebug("TTCSignalReceived") << "New Start at orbit " << _lastStart;
0233     }
0234 
0235     if (!isBadRun(iEvent.run())) {
0236       phasechange = ((long long)(orbitoffset * 3564)) % 70;
0237 
0238       for (unsigned int ipart = 0; ipart < phases.size(); ++ipart) {
0239         phases[ipart] = (_defphases[ipart] + phasechange) % 70;
0240       }
0241     }
0242 
0243   } else {
0244     if (!l1ts->empty()) {
0245       if ((*l1ts)[0].lastResync() != 0) {
0246         orbitoffset = _useEC0 ? (*l1ts)[0].lastEventCounter0() + _magicOffset : (*l1ts)[0].lastResync() + _magicOffset;
0247       }
0248 
0249       if (_lastResync != (*l1ts)[0].lastResync()) {
0250         _lastResync = (*l1ts)[0].lastResync();
0251         LogDebug("TTCSignalReceived") << "New Resync at orbit " << _lastResync;
0252       }
0253       if (_lastHardReset != (*l1ts)[0].lastHardReset()) {
0254         _lastHardReset = (*l1ts)[0].lastHardReset();
0255         LogDebug("TTCSignalReceived") << "New HardReset at orbit " << _lastHardReset;
0256       }
0257       if (_lastTestEnable != (*l1ts)[0].lastTestEnable()) {
0258         _lastTestEnable = (*l1ts)[0].lastTestEnable();
0259         //      LogDebug("TTCSignalReceived") << "New TestEnable at orbit " << _lastTestEnable ;
0260       }
0261       if (_lastOrbitCounter0 != (*l1ts)[0].lastOrbitCounter0()) {
0262         _lastOrbitCounter0 = (*l1ts)[0].lastOrbitCounter0();
0263         LogDebug("TTCSignalReceived") << "New OrbitCounter0 at orbit " << _lastOrbitCounter0;
0264       }
0265       if (_lastEventCounter0 != (*l1ts)[0].lastEventCounter0()) {
0266         _lastEventCounter0 = (*l1ts)[0].lastEventCounter0();
0267         LogDebug("TTCSignalReceived") << "New EventCounter0 at orbit " << _lastEventCounter0;
0268       }
0269       if (_lastStart != (*l1ts)[0].lastStart()) {
0270         _lastStart = (*l1ts)[0].lastStart();
0271         LogDebug("TTCSignalReceived") << "New Start at orbit " << _lastStart;
0272       }
0273 
0274       if (!isBadRun(iEvent.run())) {
0275         phasechange = ((long long)(orbitoffset * 3564)) % 70;
0276 
0277         for (unsigned int ipart = 0; ipart < phases.size(); ++ipart) {
0278           phases[ipart] = (_defphases[ipart] + phasechange) % 70;
0279         }
0280       }
0281     }
0282   }
0283 
0284   if (phases.size() < partnames.size()) {
0285     // throw exception
0286     throw cms::Exception("InvalidAPVCyclePhases")
0287         << " Inconsistent phases/partitions vector sizes: " << phases.size() << " " << partnames.size();
0288   }
0289 
0290   for (unsigned int ipart = 0; ipart < partnames.size(); ++ipart) {
0291     //    if(phases[ipart]>=0) {
0292     //      apvphases->get()[partnames[ipart]] = (phases[ipart]+phasechange)%70;
0293     apvphases->get()[partnames[ipart]] = phases[ipart];
0294 
0295     //    }
0296   }
0297 
0298   iEvent.put(std::move(apvphases));
0299 }
0300 
0301 bool APVCyclePhaseProducerFromL1TS::isBadRun(const unsigned int run) const {
0302   for (std::vector<std::pair<unsigned int, unsigned int> >::const_iterator runpair = m_badruns.begin();
0303        runpair != m_badruns.end();
0304        ++runpair) {
0305     if (run >= runpair->first && run <= runpair->second)
0306       return true;
0307   }
0308 
0309   return m_badRun;
0310 }
0311 
0312 void APVCyclePhaseProducerFromL1TS::printConfiguration(std::stringstream& ss) const {
0313   ss << _defpartnames.size() << " default partition names: ";
0314   for (std::vector<std::string>::const_iterator part = _defpartnames.begin(); part != _defpartnames.end(); ++part) {
0315     ss << *part << " ";
0316   }
0317   ss << std::endl;
0318   ss << _defphases.size() << " default phases: ";
0319   for (std::vector<int>::const_iterator phase = _defphases.begin(); phase != _defphases.end(); ++phase) {
0320     ss << *phase << " ";
0321   }
0322   ss << std::endl;
0323   ss << " Magic offset: " << _magicOffset << std::endl;
0324   ss << " use ECO: " << _useEC0 << std::endl;
0325   ss << " bad run: " << m_badRun << std::endl;
0326 }
0327 //define this as a plug-in
0328 DEFINE_FWK_MODULE(APVCyclePhaseProducerFromL1TS);