Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-10-25 04:55:06

0001 #include "DataFormats/Common/interface/Handle.h"
0002 #include "DataFormats/FEDRawData/interface/FEDNumbering.h"
0003 #include "DataFormats/FEDRawData/interface/FEDRawData.h"
0004 #include "DataFormats/FEDRawData/interface/FEDRawDataCollection.h"
0005 #include "DataFormats/Scalers/interface/DcsStatus.h"
0006 #include "DataFormats/OnlineMetaData/interface/DCSRecord.h"
0007 #include "FWCore/Framework/interface/Event.h"
0008 #include "FWCore/Framework/interface/stream/EDFilter.h"
0009 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0010 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0011 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0012 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
0013 
0014 #include <type_traits>  // for std::is_same
0015 
0016 class DetectorStateFilter : public edm::stream::EDFilter<> {
0017 public:
0018   DetectorStateFilter(const edm::ParameterSet&);
0019   ~DetectorStateFilter() override;
0020 
0021   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0022 
0023 private:
0024   bool filter(edm::Event&, edm::EventSetup const&) override;
0025 
0026   const bool verbose_;
0027   uint64_t nEvents_, nSelectedEvents_;
0028   bool detectorOn_;
0029   const std::string detectorType_;
0030   const edm::EDGetTokenT<DcsStatusCollection> dcsStatusLabel_;
0031   const edm::EDGetTokenT<DCSRecord> dcsRecordToken_;
0032 
0033   template <typename T>
0034   bool checkSubdet(const T& DCS, const int index);
0035   template <typename T>
0036   bool checkDCS(const T& DCS);
0037 
0038   bool checkDCSStatus(const DcsStatusCollection& dcsStatus);
0039   bool checkDCSRecord(const DCSRecord& dcsRecord);
0040 };
0041 
0042 //
0043 // auxilliary enum
0044 //
0045 namespace DetStateFilter {
0046   enum parts { BPix = 0, FPix = 1, TIBTID = 2, TOB = 3, TECp = 4, TECm = 5, Invalid };
0047 }
0048 
0049 //
0050 // -- Constructor
0051 //
0052 DetectorStateFilter::DetectorStateFilter(const edm::ParameterSet& pset)
0053     : verbose_(pset.getUntrackedParameter<bool>("DebugOn", false)),
0054       detectorType_(pset.getUntrackedParameter<std::string>("DetectorType", "sistrip")),
0055       dcsStatusLabel_(consumes<DcsStatusCollection>(
0056           pset.getUntrackedParameter<edm::InputTag>("DcsStatusLabel", edm::InputTag("scalersRawToDigi")))),
0057       dcsRecordToken_(consumes<DCSRecord>(
0058           pset.getUntrackedParameter<edm::InputTag>("DCSRecordLabel", edm::InputTag("onlineMetaDataDigis")))) {
0059   nEvents_ = 0;
0060   nSelectedEvents_ = 0;
0061   detectorOn_ = false;
0062 }
0063 
0064 //
0065 // -- Destructor
0066 //
0067 DetectorStateFilter::~DetectorStateFilter() = default;
0068 
0069 template <typename T>
0070 //*********************************************************************//
0071 bool DetectorStateFilter::checkSubdet(const T& DCS, const int index)
0072 //*********************************************************************//
0073 {
0074   std::vector<int> dcsStatusParts = {
0075       DcsStatus::BPIX, DcsStatus::FPIX, DcsStatus::TIBTID, DcsStatus::TOB, DcsStatus::TECp, DcsStatus::TECm};
0076 
0077   std::vector<DCSRecord::Partition> dcsRecordParts = {DCSRecord::Partition::BPIX,
0078                                                       DCSRecord::Partition::FPIX,
0079                                                       DCSRecord::Partition::TIBTID,
0080                                                       DCSRecord::Partition::TOB,
0081                                                       DCSRecord::Partition::TECp,
0082                                                       DCSRecord::Partition::TECm};
0083 
0084   if constexpr (std::is_same_v<T, DcsStatusCollection>) {
0085     return (DCS)[0].ready(dcsStatusParts[index]);
0086   } else if constexpr (std::is_same_v<T, DCSRecord>) {
0087     return DCS.highVoltageReady(dcsRecordParts[index]);
0088   } else {
0089     edm::LogError("DetectorStatusFilter")
0090         << __FILE__ << " " << __LINE__ << " passed a wrong object type, cannot deduce DCS information.\n"
0091         << " returning true" << std::endl;
0092     return true;
0093   }
0094 }
0095 
0096 template <typename T>
0097 bool
0098 //*********************************************************************//
0099 DetectorStateFilter::checkDCS(const T& DCS)
0100 //*********************************************************************//
0101 {
0102   bool accepted = false;
0103   if (detectorType_ == "pixel") {
0104     if (checkSubdet(DCS, DetStateFilter::BPix) && checkSubdet(DCS, DetStateFilter::FPix)) {
0105       accepted = true;
0106       nSelectedEvents_++;
0107     } else {
0108       accepted = false;
0109     }
0110     if (verbose_) {
0111       edm::LogInfo("DetectorStatusFilter")
0112           << " Total Events " << nEvents_ << " Selected Events " << nSelectedEvents_ << " DCS States : "
0113           << " BPix " << checkSubdet(DCS, DetStateFilter::BPix) << " FPix " << checkSubdet(DCS, DetStateFilter::FPix)
0114           << " Detector State " << accepted << std::endl;
0115     }
0116   } else if (detectorType_ == "sistrip") {
0117     if (checkSubdet(DCS, DetStateFilter::TIBTID) && checkSubdet(DCS, DetStateFilter::TOB) &&
0118         checkSubdet(DCS, DetStateFilter::TECp) && checkSubdet(DCS, DetStateFilter::TECm)) {
0119       accepted = true;
0120       nSelectedEvents_++;
0121     } else {
0122       accepted = false;
0123     }
0124     if (verbose_) {
0125       edm::LogInfo("DetectorStatusFilter")
0126           << " Total Events " << nEvents_ << " Selected Events " << nSelectedEvents_ << " DCS States : "
0127           << " TEC- " << checkSubdet(DCS, DetStateFilter::TECm) << " TEC+ " << checkSubdet(DCS, DetStateFilter::TECp)
0128           << " TIB/TID " << checkSubdet(DCS, DetStateFilter::TIBTID) << " TOB " << checkSubdet(DCS, DetStateFilter::TOB)
0129           << " Detector States " << accepted << std::endl;
0130     }
0131   } else {
0132     throw cms::Exception("Wrong Configuration")
0133         << "Stated DetectorType '" << detectorType_
0134         << "' is neither 'pixel' or 'sistrip', please check your configuration!";
0135   }
0136   return accepted;
0137 }
0138 
0139 //*********************************************************************//
0140 bool DetectorStateFilter::filter(edm::Event& evt, edm::EventSetup const& es)
0141 //*********************************************************************//
0142 {
0143   nEvents_++;
0144   // Check Detector state Only for Real Data and return true for MC
0145   if (evt.isRealData()) {
0146     edm::Handle<DcsStatusCollection> dcsStatus;
0147     evt.getByToken(dcsStatusLabel_, dcsStatus);
0148     edm::Handle<DCSRecord> dcsRecord;
0149     evt.getByToken(dcsRecordToken_, dcsRecord);
0150 
0151     if (dcsStatus.isValid() && !dcsStatus->empty()) {
0152       // if the old style DCS status is valid (Run1 + Run2)
0153       detectorOn_ = checkDCS(*dcsStatus);
0154     } else if (dcsRecord.isValid()) {
0155       // in case of real data check for DCSRecord content (Run >=3)
0156       detectorOn_ = checkDCS(*dcsRecord);
0157     } else {
0158       edm::LogError("DetectorStatusFilter")
0159           << "Error! can't get the products, neither DCSRecord, nor scalersRawToDigi: accept in any case!";
0160       detectorOn_ = true;
0161     }
0162   } else {
0163     detectorOn_ = true;
0164     nSelectedEvents_++;
0165     if (verbose_) {
0166       edm::LogInfo("DetectorStatusFilter") << "Total MC Events " << nEvents_ << " Selected Events " << nSelectedEvents_
0167                                            << " Detector States " << detectorOn_ << std::endl;
0168     }
0169   }
0170   return detectorOn_;
0171 }
0172 
0173 // ------------ method fills 'descriptions' with the allowed parameters for the module  ------------
0174 void DetectorStateFilter::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0175   edm::ParameterSetDescription desc;
0176   desc.setComment("filters on the HV status of the Tracker (either pixels or strips)");
0177   desc.addUntracked<bool>("DebugOn", false)->setComment("activates debugging");
0178   desc.addUntracked<std::string>("DetectorType", "sistrip")->setComment("either strips or pixels");
0179   desc.addUntracked<edm::InputTag>("DcsStatusLabel", edm::InputTag("scalersRawToDigi"))
0180       ->setComment("event data for DCS (Run2)");
0181   desc.addUntracked<edm::InputTag>("DCSRecordLabel", edm::InputTag("onlineMetaDataDigis"))
0182       ->setComment("event data for DCS (Run3)");
0183   descriptions.add("_detectorStateFilter", desc);
0184 }
0185 
0186 #include "FWCore/Framework/interface/MakerMacros.h"
0187 DEFINE_FWK_MODULE(DetectorStateFilter);