File indexing completed on 2024-04-06 12:28:53
0001 #include "FWCore/Framework/interface/stream/EDProducer.h"
0002 #include "FWCore/Framework/interface/ConsumesCollector.h"
0003 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0004 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0005 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
0006 #include "FWCore/Utilities/interface/EDGetToken.h"
0007 #include "FWCore/Utilities/interface/EDPutToken.h"
0008 #include "FWCore/Framework/interface/Event.h"
0009 #include "DataFormats/Common/interface/Handle.h"
0010 #include "FWCore/Utilities/interface/RunningAverage.h"
0011
0012 #include "RecoTracker/TkHitPairs/interface/IntermediateHitDoublets.h"
0013 #include "RecoTracker/TkHitPairs/interface/RegionsSeedingHitSets.h"
0014 #include "RecoTracker/PixelSeeding/interface/LayerTriplets.h"
0015 #include "MultiHitGeneratorFromChi2.h"
0016
0017 #include <memory>
0018 #include <vector>
0019
0020 class MultiHitFromChi2EDProducer : public edm::stream::EDProducer<> {
0021 public:
0022 MultiHitFromChi2EDProducer(const edm::ParameterSet& iConfig);
0023 ~MultiHitFromChi2EDProducer() override = default;
0024
0025 static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0026
0027 void produce(edm::Event& iEvent, const edm::EventSetup& iSetup) override;
0028
0029 private:
0030 edm::EDGetTokenT<IntermediateHitDoublets> doubletToken_;
0031 edm::EDPutTokenT<std::vector<std::unique_ptr<BaseTrackerRecHit>>> hitsPutToken_;
0032
0033 edm::RunningAverage localRA_;
0034
0035 MultiHitGeneratorFromChi2 generator_;
0036 };
0037
0038 MultiHitFromChi2EDProducer::MultiHitFromChi2EDProducer(const edm::ParameterSet& iConfig)
0039 : doubletToken_(consumes<IntermediateHitDoublets>(iConfig.getParameter<edm::InputTag>("doublets"))),
0040 hitsPutToken_{produces()},
0041 generator_(iConfig, consumesCollector()) {
0042 produces<RegionsSeedingHitSets>();
0043 }
0044
0045 void MultiHitFromChi2EDProducer::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0046 edm::ParameterSetDescription desc;
0047
0048 desc.add<edm::InputTag>("doublets", edm::InputTag("hitPairEDProducer"));
0049
0050 MultiHitGeneratorFromChi2::fillDescriptions(desc);
0051
0052 auto label = MultiHitGeneratorFromChi2::fillDescriptionsLabel() + std::string("EDProducerDefault");
0053 descriptions.add(label, desc);
0054 }
0055
0056 void MultiHitFromChi2EDProducer::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
0057 edm::Handle<IntermediateHitDoublets> hdoublets;
0058 iEvent.getByToken(doubletToken_, hdoublets);
0059 const auto& regionDoublets = *hdoublets;
0060
0061 const SeedingLayerSetsHits& seedingLayerHits = regionDoublets.seedingLayerHits();
0062 if (seedingLayerHits.numberOfLayersInSet() < 3) {
0063 throw cms::Exception("LogicError")
0064 << "MultiHitFromChi2EDProducer expects SeedingLayerSetsHits::numberOfLayersInSet() to be >= 3, got "
0065 << seedingLayerHits.numberOfLayersInSet()
0066 << ". This is likely caused by a configuration error of this module, HitPairEDProducer, or "
0067 "SeedingLayersEDProducer.";
0068 }
0069
0070 auto seedingHitSets = std::make_unique<RegionsSeedingHitSets>();
0071 if (regionDoublets.empty()) {
0072 iEvent.put(std::move(seedingHitSets));
0073 return;
0074 }
0075 seedingHitSets->reserve(regionDoublets.regionSize(), localRA_.upper());
0076 generator_.initES(iSetup);
0077
0078
0079 std::vector<LayerTriplets::LayerSetAndLayers> trilayers = LayerTriplets::layers(seedingLayerHits);
0080
0081 OrderedMultiHits multihits;
0082 multihits.reserve(localRA_.upper());
0083 std::vector<std::unique_ptr<BaseTrackerRecHit>> refittedHitStorage;
0084 refittedHitStorage.reserve(localRA_.upper() * 2);
0085
0086 LogDebug("MultiHitFromChi2EDProducer") << "Creating multihits for " << regionDoublets.regionSize() << " regions, and "
0087 << trilayers.size() << " pair+3rd layers from "
0088 << regionDoublets.layerPairsSize() << " layer pairs";
0089
0090 LayerHitMapCache hitCache;
0091 for (const auto& regionLayerPairs : regionDoublets) {
0092 const TrackingRegion& region = regionLayerPairs.region();
0093
0094 auto seedingHitSetsFiller = RegionsSeedingHitSets::dummyFiller();
0095 seedingHitSetsFiller = seedingHitSets->beginRegion(®ion);
0096
0097 hitCache.clear();
0098 hitCache.extend(regionLayerPairs.layerHitMapCache());
0099
0100 LogTrace("MultiHitFromChi2EDProducer") << " starting region";
0101
0102 for (const auto& layerPair : regionLayerPairs) {
0103 LogTrace("MultiHitFromChi2EDProducer")
0104 << " starting layer pair " << layerPair.innerLayerIndex() << "," << layerPair.outerLayerIndex();
0105
0106 auto found = std::find_if(trilayers.begin(), trilayers.end(), [&](const LayerTriplets::LayerSetAndLayers& a) {
0107 return a.first[0].index() == layerPair.innerLayerIndex() && a.first[1].index() == layerPair.outerLayerIndex();
0108 });
0109 if (found == trilayers.end()) {
0110 auto exp = cms::Exception("LogicError") << "Did not find the layer pair from vector<pair+third layers>. This "
0111 "is a sign of some internal inconsistency\n";
0112 exp << "I was looking for layer pair " << layerPair.innerLayerIndex() << "," << layerPair.outerLayerIndex()
0113 << ". Triplets have the following pairs:\n";
0114 for (const auto& a : trilayers) {
0115 exp << " " << a.first[0].index() << "," << a.first[1].index() << ": 3rd layers";
0116 for (const auto& b : a.second) {
0117 exp << " " << b.index();
0118 }
0119 exp << "\n";
0120 }
0121 throw exp;
0122 }
0123 const auto& thirdLayers = found->second;
0124
0125 generator_.hitSets(region, multihits, layerPair.doublets(), thirdLayers, hitCache, refittedHitStorage);
0126
0127 #ifdef EDM_ML_DEBUG
0128 LogTrace("MultiHitFromChi2EDProducer")
0129 << " created " << multihits.size() << " multihits for layer pair " << layerPair.innerLayerIndex() << ","
0130 << layerPair.outerLayerIndex() << " and 3rd layers";
0131 for (const auto& l : thirdLayers) {
0132 LogTrace("MultiHitFromChi2EDProducer") << " " << l.index();
0133 }
0134 #endif
0135
0136 for (const SeedingHitSet& hitSet : multihits) {
0137 seedingHitSetsFiller.emplace_back(hitSet);
0138 }
0139 multihits.clear();
0140 }
0141 }
0142 localRA_.update(seedingHitSets->size());
0143
0144 seedingHitSets->shrink_to_fit();
0145 refittedHitStorage.shrink_to_fit();
0146 iEvent.put(std::move(seedingHitSets));
0147 iEvent.emplace(hitsPutToken_, std::move(refittedHitStorage));
0148 }
0149
0150 #include "FWCore/PluginManager/interface/ModuleDef.h"
0151 #include "FWCore/Framework/interface/MakerMacros.h"
0152 DEFINE_FWK_MODULE(MultiHitFromChi2EDProducer);