Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-03-14 23:57:21

0001 /** \class TKStatus
0002  * *
0003  *  \author  Geng-yuan Jeng/UC Riverside
0004  *           Francisco Yumiceva/FNAL
0005  *
0006  */
0007 
0008 // C++
0009 #include <array>
0010 #include <fstream>
0011 #include <string>
0012 
0013 // CMS
0014 #include "DataFormats/OnlineMetaData/interface/DCSRecord.h"
0015 #include "DataFormats/OnlineMetaData/interface/OnlineLuminosityRecord.h"
0016 #include "DataFormats/Scalers/interface/DcsStatus.h"
0017 #include "FWCore/Framework/interface/Event.h"
0018 #include "FWCore/Framework/interface/Frameworkfwd.h"
0019 #include "FWCore/Framework/interface/LuminosityBlock.h"
0020 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0021 #include "FWCore/Framework/interface/MakerMacros.h"
0022 #include "FWCore/Framework/interface/Run.h"
0023 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0024 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0025 #include "FWCore/ServiceRegistry/interface/Service.h"
0026 #include "FWCore/Utilities/interface/EDGetToken.h"
0027 
0028 //
0029 // class declaration
0030 //
0031 class TKStatus : public edm::one::EDAnalyzer<> {
0032 public:
0033   TKStatus(const edm::ParameterSet&);
0034 
0035 protected:
0036   void analyze(const edm::Event& e, const edm::EventSetup& c) override;
0037 
0038 private:
0039   void dumpTkDcsStatus(std::string const&, edm::RunNumber_t, std::array<bool, 6> const&);
0040 
0041   std::string dcsTkFileName_;
0042   const edm::EDGetTokenT<DcsStatusCollection> dcsStatusToken_;
0043   const edm::EDGetTokenT<DCSRecord> dcsRecordToken_;
0044 
0045   int lastlumi_ = -1;
0046   // ----------member data ---------------------------
0047 };
0048 
0049 // ----------------------------------------------------------
0050 TKStatus::TKStatus(const edm::ParameterSet& ps)
0051     : dcsStatusToken_(consumes<DcsStatusCollection>(edm::InputTag("scalersRawToDigi"))),
0052       dcsRecordToken_(consumes<DCSRecord>(edm::InputTag("onlineMetaDataDigis"))) {
0053   dcsTkFileName_ = ps.getParameter<edm::ParameterSet>("BeamFitter").getUntrackedParameter<std::string>("DIPFileName");
0054   {
0055     std::string tmpname = dcsTkFileName_;
0056     tmpname.insert(dcsTkFileName_.length() - 4, "_TkStatus");
0057     dcsTkFileName_ = std::move(tmpname);
0058   }
0059 }
0060 
0061 // ----------------------------------------------------------
0062 void TKStatus::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
0063   int nthlumi = iEvent.luminosityBlock();
0064   if (nthlumi > lastlumi_) {  // check every LS
0065     lastlumi_ = nthlumi;
0066 
0067     // Checking TK status
0068     const auto& dcsStatus = iEvent.getHandle(dcsStatusToken_);
0069     const auto& dcsRecord = iEvent.getHandle(dcsRecordToken_);
0070 
0071     std::array<bool, 6> dcsTk;
0072     for (auto& e : dcsTk) {
0073       e = true;
0074     }
0075 
0076     // Check that the DCS information is available in some form
0077     if (!dcsStatus.isValid() && !dcsRecord.isValid()) {
0078       edm::LogWarning("TkStatus") << "DcsStatusCollection product with InputTag \"scalersRawToDigi\" not in event \n"
0079                                   << "DCSRecord product with InputTag \"onlineMetaDataDigis\" not in event \n";
0080       dumpTkDcsStatus(dcsTkFileName_, iEvent.run(), dcsTk);
0081       return;
0082     }
0083 
0084     if (dcsStatus.isValid() && (*dcsStatus).empty()) {
0085       if (iEvent.eventAuxiliary().isRealData()) {
0086         // This is the Data case for >= Run3, DCSStatus is available (unpacked), but empty
0087         // because SCAL is not in data-taking. In this case we fall back to s/w FED 1022
0088         if (dcsRecord.isValid()) {
0089           edm::LogPrint("TkStatus") << "Using dcsRecord because dcsStatus is empty";
0090           dcsTk[0] = (*dcsRecord).highVoltageReady(DCSRecord::Partition::BPIX);
0091           dcsTk[1] = (*dcsRecord).highVoltageReady(DCSRecord::Partition::FPIX);
0092           dcsTk[2] = (*dcsRecord).highVoltageReady(DCSRecord::Partition::TIBTID);
0093           dcsTk[3] = (*dcsRecord).highVoltageReady(DCSRecord::Partition::TOB);
0094           dcsTk[4] = (*dcsRecord).highVoltageReady(DCSRecord::Partition::TECp);
0095           dcsTk[5] = (*dcsRecord).highVoltageReady(DCSRecord::Partition::TECm);
0096         } else {
0097           // DCS status is empty, and the DCS record is not available
0098           edm::LogWarning("TkStatus") << "DCSRecord product with InputTag \"onlineMetaDataDigis\" empty \n";
0099           dumpTkDcsStatus(dcsTkFileName_, iEvent.run(), dcsTk);
0100           return;
0101         }
0102       } else {
0103         // DCS status is empty, but it's not real data
0104         edm::LogPrint("TkStatus") << "Running on MC";
0105         dumpTkDcsStatus(dcsTkFileName_, iEvent.run(), dcsTk);
0106         return;
0107       }
0108     } else {
0109       // This is the case for <= Run2
0110       edm::LogPrint("TkStatus") << "Using dcsStatus (Run 1 and Run 2 SCAL)";
0111       for (auto const& status : *dcsStatus) {
0112         if (!status.ready(DcsStatus::BPIX))
0113           dcsTk[0] = false;
0114         if (!status.ready(DcsStatus::FPIX))
0115           dcsTk[1] = false;
0116         if (!status.ready(DcsStatus::TIBTID))
0117           dcsTk[2] = false;
0118         if (!status.ready(DcsStatus::TOB))
0119           dcsTk[3] = false;
0120         if (!status.ready(DcsStatus::TECp))
0121           dcsTk[4] = false;
0122         if (!status.ready(DcsStatus::TECm))
0123           dcsTk[5] = false;
0124       }
0125     }
0126 
0127     dumpTkDcsStatus(dcsTkFileName_, iEvent.run(), dcsTk);
0128   }
0129 }
0130 
0131 //--------------------------------------------------------
0132 void TKStatus::dumpTkDcsStatus(std::string const& fileName, edm::RunNumber_t runnum, std::array<bool, 6> const& dcsTk) {
0133   std::ofstream outFile;
0134 
0135   outFile.open(fileName.c_str());
0136   outFile << "BPIX " << (dcsTk[0] ? "On" : "Off") << std::endl;
0137   outFile << "FPIX " << (dcsTk[1] ? "On" : "Off") << std::endl;
0138   outFile << "TIBTID " << (dcsTk[2] ? "On" : "Off") << std::endl;
0139   outFile << "TOB " << (dcsTk[3] ? "On" : "Off") << std::endl;
0140   outFile << "TECp " << (dcsTk[4] ? "On" : "Off") << std::endl;
0141   outFile << "TECm " << (dcsTk[5] ? "On" : "Off") << std::endl;
0142   bool AllTkOn = true;
0143   for (auto status : dcsTk) {
0144     if (!status) {
0145       AllTkOn = false;
0146       break;
0147     }
0148   }
0149   outFile << "WholeTrackerOn " << (AllTkOn ? "Yes" : "No") << std::endl;
0150   outFile << "Runnumber " << runnum << std::endl;
0151 
0152   outFile.close();
0153 }
0154 
0155 DEFINE_FWK_MODULE(TKStatus);