Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 14:25:33

0001 // -*- C++ -*-

0002 //

0003 // Package:    HcalLaserEventFilter

0004 // Class:      HcalLaserEventFilter

0005 //

0006 /**\class HcalLaserEventFilter HcalLaserEventFilter.cc UserCode/HcalLaserEventFilter/src/HcalLaserEventFilter.cc

0007 

0008  Description: Filter for removing Hcal laser events

0009 

0010  Implementation:

0011  Filter allows users to remove specific (run,event) pairs that have been identified as noise

0012 It also allows users to remove events in which the number of HBHE rechits exceeds a given threshold (5000 by default).

0013 

0014 */
0015 //

0016 // Original Author:  Jeff Temple (temple@cern.ch)

0017 //         Created:  Thu Nov 17 12:44:22 EST 2011

0018 //

0019 //

0020 
0021 // system include files

0022 #include <memory>
0023 #include <sstream>
0024 #include <iostream>
0025 #include <string>
0026 
0027 // user include files

0028 #include "FWCore/Framework/interface/Frameworkfwd.h"
0029 #include "FWCore/Framework/interface/global/EDFilter.h"
0030 #include "FWCore/Version/interface/GetReleaseVersion.h"
0031 
0032 #include "FWCore/Framework/interface/Event.h"
0033 #include "FWCore/Framework/interface/MakerMacros.h"
0034 
0035 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0036 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0037 
0038 // Use for HBHERecHitCollection

0039 #include "DataFormats/HcalRecHit/interface/HcalRecHitCollections.h"
0040 
0041 #include "DataFormats/METReco/interface/HcalNoiseSummary.h"
0042 
0043 #include "DataFormats/Provenance/interface/RunLumiEventNumber.h"
0044 //

0045 // class declaration

0046 //

0047 
0048 class HcalLaserEventFilter : public edm::global::EDFilter<> {
0049 public:
0050   explicit HcalLaserEventFilter(const edm::ParameterSet&);
0051   ~HcalLaserEventFilter() override;
0052 
0053   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0054 
0055 private:
0056   bool filter(edm::StreamID, edm::Event&, const edm::EventSetup&) const override;
0057 
0058   std::vector<int> GetCMSSWVersion(std::string const&) const;
0059   bool IsGreaterThanMinCMSSWVersion(std::vector<int> const&) const;
0060 
0061   // ----------member data ---------------------------

0062 
0063   // Filter option 1:  veto events by run, event number

0064   const bool vetoByRunEventNumber_;
0065   std::vector<std::pair<edm::RunNumber_t, edm::EventNumber_t> > RunEventData_;
0066 
0067   // Filter option 2:  veto events by HBHE occupancy

0068   const bool vetoByHBHEOccupancy_;
0069   const unsigned int minOccupiedHBHE_;
0070 
0071   const bool vetoByLaserMonitor_;
0072   const double minLaserMonitorCharge_;
0073 
0074   // Allow for debugging information to be printed

0075   const bool debug_;
0076   // Reverse the filter decision (so instead of selecting only good events, it

0077   // will select only events that fail the filter conditions -- useful for studying

0078   // bad events.)

0079   const bool reverseFilter_;
0080 
0081   // InputTag for HBHE rechits

0082   const edm::InputTag hbheInputLabel_;
0083   edm::EDGetTokenT<HBHERecHitCollection> hbheToken_;
0084   const edm::InputTag hcalNoiseSummaryLabel_;
0085   edm::EDGetTokenT<HcalNoiseSummary> hcalNoiseSummaryToken_;
0086 
0087   const bool taggingMode_;
0088 
0089   // Decide whether to use the HcalNoiseSummary to get the RecHit info, or to use the RecHit Collection itself

0090   bool useHcalNoiseSummary_;
0091   bool forceUseRecHitCollection_;
0092   bool forceUseHcalNoiseSummary_;
0093   std::vector<int> minVersion_;
0094 };
0095 
0096 //

0097 // constants, enums and typedefs

0098 //

0099 
0100 //

0101 // static data member definitions

0102 //

0103 
0104 //

0105 // constructors and destructor

0106 //

0107 HcalLaserEventFilter::HcalLaserEventFilter(const edm::ParameterSet& iConfig)
0108 
0109     // Get values from python cfg file

0110     : vetoByRunEventNumber_(iConfig.getParameter<bool>("vetoByRunEventNumber")),
0111       vetoByHBHEOccupancy_(iConfig.getParameter<bool>("vetoByHBHEOccupancy")),
0112       minOccupiedHBHE_(iConfig.getParameter<unsigned int>("minOccupiedHBHE")),
0113       vetoByLaserMonitor_(iConfig.getParameter<bool>("vetoByLaserMonitor")),
0114       minLaserMonitorCharge_(iConfig.getParameter<double>("minLaserMonitorCharge")),
0115       debug_(iConfig.getParameter<bool>("debug")),
0116       reverseFilter_(iConfig.getParameter<bool>("reverseFilter")),
0117       hbheInputLabel_(iConfig.getUntrackedParameter<edm::InputTag>("hbheInputLabel", edm::InputTag("hbhereco"))),
0118       hbheToken_(mayConsume<HBHERecHitCollection>(hbheInputLabel_)),
0119       hcalNoiseSummaryLabel_(
0120           iConfig.getUntrackedParameter<edm::InputTag>("hcalNoiseSummaryLabel", edm::InputTag("hcalnoise"))),
0121       hcalNoiseSummaryToken_(mayConsume<HcalNoiseSummary>(hcalNoiseSummaryLabel_)),
0122       taggingMode_(iConfig.getParameter<bool>("taggingMode")),
0123       forceUseRecHitCollection_(iConfig.getParameter<bool>("forceUseRecHitCollection")),
0124       forceUseHcalNoiseSummary_(iConfig.getParameter<bool>("forceUseHcalNoiseSummary"))
0125 
0126 {
0127   std::vector<unsigned int> temprunevt = iConfig.getParameter<std::vector<unsigned int> >("BadRunEventNumbers");
0128 
0129   // Make (run,evt) pairs for storing bad events

0130   // Make this a map for better search performance?

0131   for (unsigned int i = 0; i + 1 < temprunevt.size(); i += 2) {
0132     edm::RunNumber_t run = temprunevt[i];
0133     edm::EventNumber_t evt = temprunevt[i + 1];
0134     RunEventData_.push_back(std::make_pair(run, evt));
0135   }
0136   produces<bool>();
0137 
0138   // Specify the minimum release that has the rechit counts in the HcalNoiseSummary object.

0139   // If current release >= that release, then HcalNoiseSummary will be used.  Otherwise, Rechit collection will be used.

0140   std::string minRelease = "\"CMSSW_5_2_0\"";
0141 
0142   minVersion_ = GetCMSSWVersion(minRelease);
0143   std::vector<int> currentVersion = GetCMSSWVersion(edm::getReleaseVersion());
0144 
0145   if (IsGreaterThanMinCMSSWVersion(currentVersion))  // current Version is >= minVersion_

0146     useHcalNoiseSummary_ = true;
0147   else
0148     useHcalNoiseSummary_ = false;
0149 }
0150 
0151 HcalLaserEventFilter::~HcalLaserEventFilter() {
0152   // do anything here that needs to be done at desctruction time

0153   // (e.g. close files, deallocate resources etc.)

0154 }
0155 
0156 //

0157 // member functions

0158 //

0159 
0160 // ------------ method called on each new Event  ------------

0161 bool HcalLaserEventFilter::filter(edm::StreamID, edm::Event& iEvent, const edm::EventSetup& iSetup) const {
0162   using namespace edm;
0163 
0164   bool filterDecision = true;
0165 
0166   if (debug_)
0167     edm::LogInfo("HcalLaserEventFilter") << "<HcalLaserEventFilter> Run = " << iEvent.id().run()
0168                                          << " Event = " << iEvent.id().event();
0169 
0170   // Veto events by run/event numbers

0171   if (vetoByRunEventNumber_) {
0172     for (unsigned int i = 0; i < RunEventData_.size(); ++i) {
0173       if (iEvent.id().run() == RunEventData_[i].first && iEvent.id().event() == RunEventData_[i].second) {
0174         if (debug_)
0175           edm::LogInfo("HcalLaserEventFilter") << "\t<HcalLaserEventFilter> Filtering bad event;  Run "
0176                                                << iEvent.id().run() << " Event = " << iEvent.id().event();
0177         filterDecision = false;
0178         break;
0179       }
0180     }
0181   }  // if (vetoByRunEventNumber_)

0182 
0183   //Veto events by HBHE rechit collection size

0184   if (vetoByHBHEOccupancy_) {
0185     // The decision on whether or not to use the noise summary is made based on the CMSSW version.

0186     // As of CMSSW_5_2_0, the HcalNoiseSummary contains the total number of HBHE hits, as well as number of hits > 1.5 GeV and some other info.

0187     // The boolean 'forceUseRecHitCollection_' can be used to override this automatic behavior, and to use the RecHit collection itself, regardless of CMSSW version.

0188 
0189     //////////////////////////////////////////////////////////

0190     //

0191     //  Apply Filtering based on RecHit information in HBHERecHitcollection

0192     //

0193     ////////////////////////////////////////////////////////////

0194 
0195     if (useHcalNoiseSummary_ == false || forceUseRecHitCollection_ == true) {
0196       edm::Handle<HBHERecHitCollection> hbheRecHits;
0197       if (iEvent.getByToken(hbheToken_, hbheRecHits)) {
0198         if (debug_)
0199           edm::LogInfo("HcalLaserEventFilter")
0200               << "Rechit size = " << hbheRecHits->size() << "  threshold = " << minOccupiedHBHE_;
0201         if (hbheRecHits->size() >= minOccupiedHBHE_) {
0202           if (debug_)
0203             edm::LogInfo("HcalLaserEventFilter")
0204                 << "<HcalLaserEventFilter>  Filtering because of large HBHE rechit size; " << hbheRecHits->size()
0205                 << " rechits is greater than or equal to the allowed maximum of " << minOccupiedHBHE_;
0206           filterDecision = false;
0207         }
0208       } else {
0209         if (debug_)
0210           edm::LogInfo("HcalLaserEventFilter")
0211               << "<HcalLaserEventFilter::Error> No valid HBHERecHitCollection with label '" << hbheInputLabel_
0212               << "' found";
0213       }
0214     }
0215 
0216     //////////////////////////////////////////////////////////

0217     //

0218     //  Apply Filtering based on RecHit information in HcalNoiseSummary object

0219     //

0220     ////////////////////////////////////////////////////////////

0221     else if (useHcalNoiseSummary_ == true || forceUseHcalNoiseSummary_ == true) {
0222       Handle<HcalNoiseSummary> hSummary;
0223       if (iEvent.getByToken(hcalNoiseSummaryToken_, hSummary))  // get by label, usually with label 'hcalnoise'

0224       {
0225         if (debug_)
0226           edm::LogInfo("HcalLaserEventFilter")
0227               << " RECHIT SIZE (from HcalNoiseSummary) = " << hSummary->GetRecHitCount()
0228               << "  threshold = " << minOccupiedHBHE_;
0229         if (hSummary->GetRecHitCount() >= (int)minOccupiedHBHE_) {
0230           if (debug_)
0231             edm::LogInfo("HcalLaserEventFilter")
0232                 << "<HcalLaserEventFilter>  Filtering because of large HBHE rechit size in HcalNoiseSummary; "
0233                 << hSummary->GetRecHitCount() << " rechits is greater than or equal to the allowed maximum of "
0234                 << minOccupiedHBHE_;
0235           filterDecision = false;
0236         }
0237       } else {
0238         if (debug_)
0239           edm::LogInfo("HcalLaserEventFilter") << "<HcalLaserEventFilter::Error> No valid HcalNoiseSummary with label '"
0240                                                << hcalNoiseSummaryLabel_ << "' found";
0241       }
0242     }
0243   }  // if (vetoByHBHEOccupancy_)

0244   if (vetoByLaserMonitor_) {
0245     //////////////////////////////////////////////////////////

0246     //

0247     //  Apply Filtering based on laser monitor information in HcalNoiseSummary object

0248     //

0249     ////////////////////////////////////////////////////////////

0250     Handle<HcalNoiseSummary> hSummary;
0251     if (iEvent.getByToken(hcalNoiseSummaryToken_, hSummary))  // get by label, usually with label 'hcalnoise'

0252     {
0253       if (debug_)
0254         edm::LogInfo("HcalLaserEventFilter")
0255             << " LASERMON CHARGE (from HcalNoiseSummary) = " << hSummary->GetLaserMonitorCharge()
0256             << "  threshold = " << minLaserMonitorCharge_;
0257       if (hSummary->GetLaserMonitorCharge() > minLaserMonitorCharge_) {
0258         if (debug_)
0259           edm::LogInfo("HcalLaserEventFilter")
0260               << "<HcalLaserEventFilter>  Filtering because of large Laser monitor charge in HcalNoiseSummary; "
0261               << hSummary->GetLaserMonitorCharge() << " charge is greater than or equal to the allowed maximum of "
0262               << minLaserMonitorCharge_;
0263         filterDecision = false;
0264       }
0265     } else {
0266       if (debug_)
0267         edm::LogInfo("HcalLaserEventFilter") << "<HcalLaserEventFilter::Error> No valid HcalNoiseSummary with label '"
0268                                              << hcalNoiseSummaryLabel_ << "' found";
0269     }
0270   }
0271 
0272   // Reverse decision, if specified by user

0273   if (reverseFilter_)
0274     filterDecision = !filterDecision;
0275 
0276   iEvent.put(std::make_unique<bool>(filterDecision));
0277 
0278   return taggingMode_ || filterDecision;
0279 }
0280 
0281 // ------------ method fills 'descriptions' with the allowed parameters for the module  ------------

0282 void HcalLaserEventFilter::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0283   //The following says we do not know what parameters are allowed so do no validation

0284   // Please change this to state exactly what you do use, even if it is no parameters

0285   edm::ParameterSetDescription desc;
0286 
0287   desc.add<bool>("vetoByRunEventNumber", false)->setComment("Enable filtering by run number");
0288   desc.add<bool>("vetoByHBHEOccupancy", true)->setComment("Enable occupancy filtering");
0289   desc.add<unsigned int>("minOccupiedHBHE", 4000)->setComment("Minimum occupancy to filter events");
0290   desc.add<bool>("vetoByLaserMonitor", true)->setComment("Enable Laser monitoring filtering");
0291   desc.add<double>("minLaserMonitorCharge", 5000.)->setComment("Set minimum laser monitor charge to filter events");
0292   desc.add<bool>("debug", false)->setComment("Enable debugging messages");
0293   desc.add<bool>("reverseFilter", false)->setComment("Invert filter decision");
0294   desc.add<bool>("taggingMode", false)->setComment("do not filter, just tag the event");
0295   desc.add<bool>("forceUseRecHitCollection", false)->setComment("force the evaluation using RecHit collection");
0296   desc.add<bool>("forceUseHcalNoiseSummary", false)->setComment("force the evaluation using Noise Summary");
0297   desc.add<std::vector<unsigned int> >("BadRunEventNumbers", {})->setComment("vector of bad events to filter");
0298 
0299   descriptions.add("hcallaserevent", desc);
0300 }
0301 
0302 std::vector<int> HcalLaserEventFilter::GetCMSSWVersion(std::string const& instring) const {
0303   std::vector<int> temp;
0304 
0305   std::string prefix;
0306   std::string v1, v2, v3;
0307 
0308   std::istringstream oss(instring);
0309   getline(oss, prefix, '_');
0310   getline(oss, v1, '_');
0311   getline(oss, v2, '_');
0312   getline(oss, v3, '_');
0313 
0314   std::stringstream buffer(v1);
0315   int t;
0316   buffer >> t;
0317   temp.push_back(t);
0318   buffer.str();
0319   buffer << v2;
0320   buffer >> t;
0321   temp.push_back(t);
0322   buffer.str();
0323   buffer << v3;
0324   buffer >> t;
0325   temp.push_back(t);
0326 
0327   //std::cout <<"PREFIX = "<<prefix<<" "<<temp[0]<<" "<<temp[1]<<" "<<temp[2]<<std::endl;

0328   //( ex:  PREFIX = "CMSSW 5 5 5  )

0329   return temp;
0330 }
0331 
0332 bool HcalLaserEventFilter::IsGreaterThanMinCMSSWVersion(std::vector<int> const& currentVersion) const {
0333   // Returns false if current version is less than min version

0334   // Otherwise, returns true

0335   // Assumes CMSSW versioning X_Y_Z

0336 
0337   // Compare X

0338   if (currentVersion[0] < minVersion_[0])
0339     return false;
0340   if (currentVersion[0] > minVersion_[0])
0341     return true;
0342   // If neither is true, first value of CMSSW versions are the same

0343 
0344   // Compare Y

0345   if (currentVersion[1] < minVersion_[1])
0346     return false;
0347   if (currentVersion[1] > minVersion_[1])
0348     return true;
0349 
0350   // Compare Z

0351   if (currentVersion[2] < minVersion_[2])
0352     return false;
0353   if (currentVersion[2] > minVersion_[2])
0354     return true;
0355 
0356   return true;  // versions are identical

0357 }
0358 
0359 //define this as a plug-in

0360 DEFINE_FWK_MODULE(HcalLaserEventFilter);