@@ -56,6 +56,12 @@ class ScoutingMuonPropertiesAnalyzer : public DQMEDAnalyzer {
5656 void bookHistograms (DQMStore::IBooker&, edm::Run const &, edm::EventSetup const &) override ;
5757
5858private:
59+ template <typename T>
60+ bool getValidHandle (const edm::Event& iEvent,
61+ const edm::EDGetTokenT<T>& token,
62+ edm::Handle<T>& handle,
63+ const std::string& label);
64+
5965 // Output Folder
6066 const std::string outputInternalPath_;
6167 // do to some histogram duplicates with the ScoutingCollectionMonitor.cc module, we added the option to just fill the unique plots w.r.t the aforementioned module, if this bool is set to false
@@ -228,6 +234,19 @@ ScoutingMuonPropertiesAnalyzer::ScoutingMuonPropertiesAnalyzer(const edm::Parame
228234 };
229235}
230236
237+ template <typename T>
238+ bool ScoutingMuonPropertiesAnalyzer::getValidHandle (const edm::Event& iEvent,
239+ const edm::EDGetTokenT<T>& token,
240+ edm::Handle<T>& handle,
241+ const std::string& label) {
242+ iEvent.getByToken (token, handle);
243+ if (!handle.isValid ()) {
244+ edm::LogWarning (" ScoutingMuonPropertiesAnalyzer" ) << " Invalid handle for " << label << " ! Skipping event." ;
245+ return false ;
246+ }
247+ return true ;
248+ }
249+
231250void ScoutingMuonPropertiesAnalyzer::bookHistograms (DQMStore::IBooker& ibooker,
232251 edm::Run const &,
233252 edm::EventSetup const &) {
@@ -696,23 +715,22 @@ void ScoutingMuonPropertiesAnalyzer::analyze(const edm::Event& iEvent, const edm
696715 using namespace edm ;
697716
698717 edm::Handle<edm::TriggerResults> triggerResults;
699- iEvent.getByToken (triggerResultsToken_, triggerResults);
700-
701718 edm::Handle<std::vector<Run3ScoutingMuon>> muonsNoVtx;
702- iEvent.getByToken (muonsNoVtxToken_, muonsNoVtx);
703-
704719 edm::Handle<std::vector<Run3ScoutingMuon>> muonsVtx;
705- iEvent.getByToken (muonsVtxToken_, muonsVtx);
720+ edm::Handle<std::vector<Run3ScoutingVertex>> SVNoVtx;
721+ edm::Handle<std::vector<Run3ScoutingVertex>> SVVtx;
722+
723+ if (!getValidHandle (iEvent, triggerResultsToken_, triggerResults, " TriggerResults" ) ||
724+ !getValidHandle (iEvent, muonsNoVtxToken_, muonsNoVtx, " muonsNoVtx" ) ||
725+ !getValidHandle (iEvent, muonsVtxToken_, muonsVtx, " muonsVtx" ) ||
726+ !getValidHandle (iEvent, SVNoVtxToken_, SVNoVtx, " SVNoVtx" ) ||
727+ !getValidHandle (iEvent, SVVtxToken_, SVVtx, " SVVtx" )) {
728+ return ;
729+ }
706730
707731 edm::Handle<std::vector<Run3ScoutingVertex>> PV;
708732 iEvent.getByToken (PVToken_, PV);
709733
710- edm::Handle<std::vector<Run3ScoutingVertex>> SVNoVtx;
711- iEvent.getByToken (SVNoVtxToken_, SVNoVtx);
712-
713- edm::Handle<std::vector<Run3ScoutingVertex>> SVVtx;
714- iEvent.getByToken (SVVtxToken_, SVVtx);
715-
716734 const TransientTrackBuilder* theB = &iSetup.getData (ttbESToken_);
717735
718736 h_run_->Fill (iEvent.eventAuxiliary ().run ());
0 commit comments