1- // user include files
21#include < unordered_map>
32
43#include " FWCore/Framework/interface/Frameworkfwd.h"
109#include " FWCore/Framework/interface/MakerMacros.h"
1110#include " FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
1211#include " FWCore/ParameterSet/interface/ParameterSetDescription.h"
12+ #include " FWCore/ParameterSet/interface/ParameterSet.h"
1313
1414#include " DataFormats/HGCRecHit/interface/HGCRecHitCollections.h"
1515#include " DataFormats/ParticleFlowReco/interface/PFRecHit.h"
@@ -19,81 +19,88 @@ class RecHitMapProducer : public edm::global::EDProducer<> {
1919public:
2020 RecHitMapProducer (const edm::ParameterSet&);
2121 static void fillDescriptions (edm::ConfigurationDescriptions& descriptions);
22-
2322 void produce (edm::StreamID, edm::Event&, const edm::EventSetup&) const override ;
2423
2524private:
26- std::vector<edm::EDGetTokenT<HGCRecHitCollection>> hgcal_hits_token_;
27- std::vector<edm::EDGetTokenT<reco::PFRecHitCollection>> barrel_hits_token_;
25+ using DetIdRecHitMap = std::unordered_map<DetId, const unsigned int >;
26+
27+ std::unordered_map<std::string, edm::EDGetTokenT<HGCRecHitCollection>> hgcal_hits_tokens_;
28+ std::unordered_map<std::string, edm::EDGetTokenT<reco::PFRecHitCollection>> barrel_hits_tokens_;
2829
2930 bool hgcalOnly_;
3031};
3132
3233DEFINE_FWK_MODULE (RecHitMapProducer);
3334
34- using DetIdRecHitMap = std::unordered_map<DetId, const unsigned int >;
35-
3635RecHitMapProducer::RecHitMapProducer (const edm::ParameterSet& ps) : hgcalOnly_(ps.getParameter<bool >(" hgcalOnly" )) {
37- std::vector<edm::InputTag> tags = ps.getParameter <std::vector<edm::InputTag>>(" hits" );
38- for (auto & tag : tags) {
36+ const edm::ParameterSet& hitPSet = ps.getParameter <edm::ParameterSet>(" hits" );
37+ for (const auto & entry : hitPSet.getParameterNamesForType <edm::InputTag>()) {
38+ edm::InputTag tag = hitPSet.getParameter <edm::InputTag>(entry);
3939 if (tag.label ().find (" HGCalRecHit" ) != std::string::npos) {
40- hgcal_hits_token_. push_back ( consumes<HGCRecHitCollection>(tag));
40+ hgcal_hits_tokens_. emplace (entry, consumes<HGCRecHitCollection>(tag));
4141 } else {
42- barrel_hits_token_. push_back ( consumes<reco::PFRecHitCollection>(tag));
42+ barrel_hits_tokens_. emplace (entry, consumes<reco::PFRecHitCollection>(tag));
4343 }
4444 }
4545
4646 produces<DetIdRecHitMap>(" hgcalRecHitMap" );
47- if (!hgcalOnly_)
47+ if (!hgcalOnly_) {
4848 produces<DetIdRecHitMap>(" barrelRecHitMap" );
49+ }
4950}
5051
5152void RecHitMapProducer::fillDescriptions (edm::ConfigurationDescriptions& descriptions) {
53+ edm::ParameterSetDescription hitDesc;
54+ hitDesc.add <edm::InputTag>(" HGCEE" , edm::InputTag (" HGCalRecHit" , " HGCEERecHits" ));
55+ hitDesc.add <edm::InputTag>(" HGCHEF" , edm::InputTag (" HGCalRecHit" , " HGCHEFRecHits" ));
56+ hitDesc.add <edm::InputTag>(" HGCHEB" , edm::InputTag (" HGCalRecHit" , " HGCHEBRecHits" ));
57+ // Optionally allow barrel collections to be defined
58+ hitDesc.addOptional <edm::InputTag>(" ECAL" , edm::InputTag (" particleFlowRecHitECAL" ));
59+ hitDesc.addOptional <edm::InputTag>(" HCAL" , edm::InputTag (" particleFlowRecHitHBHE" ));
60+
5261 edm::ParameterSetDescription desc;
53- desc.add <std::vector<edm::InputTag>>(" hits" ,
54- {edm::InputTag (" HGCalRecHit" , " HGCEERecHits" ),
55- edm::InputTag (" HGCalRecHit" , " HGCHEFRecHits" ),
56- edm::InputTag (" HGCalRecHit" , " HGCHEBRecHits" )});
62+ desc.add <edm::ParameterSetDescription>(" hits" , hitDesc);
5763 desc.add <bool >(" hgcalOnly" , true );
5864 descriptions.add (" recHitMapProducer" , desc);
5965}
6066
61- void RecHitMapProducer::produce (edm::StreamID, edm::Event& evt, const edm::EventSetup& es ) const {
67+ void RecHitMapProducer::produce (edm::StreamID, edm::Event& evt, const edm::EventSetup&) const {
6268 auto hitMapHGCal = std::make_unique<DetIdRecHitMap>();
69+ MultiVectorManager<HGCRecHit> rechitManager;
6370
64- // Retrieve collections
65- const auto & ee_hits = evt.getHandle (hgcal_hits_token_[0 ]);
66- const auto & fh_hits = evt.getHandle (hgcal_hits_token_[1 ]);
67- const auto & bh_hits = evt.getHandle (hgcal_hits_token_[2 ]);
68-
69- // Check validity of all handles
70- if (!ee_hits.isValid () || !fh_hits.isValid () || !bh_hits.isValid ()) {
71- edm::LogWarning (" HGCalRecHitMapProducer" ) << " One or more hit collections are unavailable. Returning an empty map." ;
72- evt.put (std::move (hitMapHGCal), " hgcalRecHitMap" );
73- return ;
71+ for (const auto & [name, token] : hgcal_hits_tokens_) {
72+ const auto & handle = evt.getHandle (token);
73+ if (handle.isValid ()) {
74+ rechitManager.addVector (*handle);
75+ } else {
76+ edm::LogWarning (" RecHitMapProducer" ) << " HGCal collection \" " << name << " \" is invalid." ;
77+ }
7478 }
7579
76- MultiVectorManager<HGCRecHit> rechitManager;
77- rechitManager.addVector (*ee_hits);
78- rechitManager.addVector (*fh_hits);
79- rechitManager.addVector (*bh_hits);
80-
8180 for (unsigned int i = 0 ; i < rechitManager.size (); ++i) {
82- const auto recHitDetId = rechitManager[i].detid ();
83- hitMapHGCal->emplace (recHitDetId, i);
81+ hitMapHGCal->emplace (rechitManager[i].detid (), i);
8482 }
8583
8684 evt.put (std::move (hitMapHGCal), " hgcalRecHitMap" );
8785
88- if (!hgcalOnly_) {
89- auto hitMapBarrel = std::make_unique<DetIdRecHitMap>();
90- MultiVectorManager<reco::PFRecHit> barrelRechitManager;
91- barrelRechitManager.addVector (evt.get (barrel_hits_token_[0 ]));
92- barrelRechitManager.addVector (evt.get (barrel_hits_token_[1 ]));
93- for (unsigned int i = 0 ; i < barrelRechitManager.size (); ++i) {
94- const auto recHitDetId = barrelRechitManager[i].detId ();
95- hitMapBarrel->emplace (recHitDetId, i);
86+ if (hgcalOnly_)
87+ return ;
88+
89+ auto hitMapBarrel = std::make_unique<DetIdRecHitMap>();
90+ MultiVectorManager<reco::PFRecHit> barrelManager;
91+
92+ for (const auto & [name, token] : barrel_hits_tokens_) {
93+ const auto & handle = evt.getHandle (token);
94+ if (handle.isValid ()) {
95+ barrelManager.addVector (*handle);
96+ } else {
97+ edm::LogWarning (" RecHitMapProducer" ) << " Barrel collection \" " << name << " \" is invalid." ;
9698 }
97- evt.put (std::move (hitMapBarrel), " barrelRecHitMap" );
9899 }
100+
101+ for (unsigned int i = 0 ; i < barrelManager.size (); ++i) {
102+ hitMapBarrel->emplace (barrelManager[i].detId (), i);
103+ }
104+
105+ evt.put (std::move (hitMapBarrel), " barrelRecHitMap" );
99106}
0 commit comments