Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-09-11 16:22:58

0001 /*!
0002 
0003   NOTE: what is called here 'FastTrackPreCandidate' is currently still known as 'FastTrackerRecHitCombination'
0004   
0005   TrajectorySeedProducer emulates the reconstruction of TrajectorySeeds in FastSim.
0006 
0007   The input data is a list of FastTrackPreCandidates.
0008   (input data are configured through the parameter 'src')
0009   In each given FastTrackPreCandidate,
0010   TrajectorySeedProducer searches for one combination of hits that matches all given seed requirements,
0011   (see parameters 'layerList','regionFactoryPSet')
0012   In that process it respects the order of the hits in the FastTrackPreCandidate.
0013   When such combination is found, a TrajectorySeed is reconstructed.
0014   Optionally, one can specify a list of hits to be ignored by TrajectorySeedProducer.
0015   (see parameter 'hitMasks')
0016   
0017   The output data is the list of reconstructed TrajectorySeeds.
0018 
0019 */
0020 
0021 // system
0022 #include <memory>
0023 #include <vector>
0024 #include <string>
0025 
0026 // framework
0027 #include "FWCore/Framework/interface/stream/EDProducer.h"
0028 #include "FWCore/Framework/interface/Event.h"
0029 #include "FWCore/Framework/interface/EventSetup.h"
0030 #include "FWCore/Framework/interface/ConsumesCollector.h"
0031 #include "FWCore/Utilities/interface/InputTag.h"
0032 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0033 
0034 // data formats
0035 #include "DataFormats/Common/interface/Handle.h"
0036 #include "DataFormats/Common/interface/OwnVector.h"
0037 #include "DataFormats/TrackerRecHit2D/interface/FastTrackerRecHitCollection.h"
0038 #include "DataFormats/TrajectorySeed/interface/TrajectorySeedCollection.h"
0039 
0040 // reco track classes
0041 #include "RecoTracker/TkTrackingRegions/interface/TrackingRegion.h"
0042 #include "RecoTracker/TkSeedingLayers/interface/SeedingHitSet.h"
0043 #include "RecoTracker/TkSeedGenerator/interface/SeedCreator.h"
0044 #include "RecoTracker/TkSeedGenerator/interface/SeedCreatorFactory.h"
0045 
0046 // geometry
0047 #include "Geometry/Records/interface/TrackerTopologyRcd.h"
0048 
0049 // fastsim
0050 #include "FastSimulation/Tracking/interface/SeedingTree.h"
0051 #include "FastSimulation/Tracking/interface/TrackingLayer.h"
0052 #include "FastSimulation/Tracking/interface/FastTrackingUtilities.h"
0053 #include "FastSimulation/Tracking/interface/SeedFinder.h"
0054 #include "FastSimulation/Tracking/interface/SeedFinderSelector.h"
0055 
0056 class MeasurementTracker;
0057 
0058 class TrajectorySeedProducer : public edm::stream::EDProducer<> {
0059 private:
0060   // tokens
0061 
0062   edm::EDGetTokenT<FastTrackerRecHitCombinationCollection> recHitCombinationsToken;
0063   edm::EDGetTokenT<std::vector<bool>> hitMasksToken;
0064   edm::EDGetTokenT<edm::OwnVector<TrackingRegion>> trackingRegionToken;
0065   const edm::ESGetToken<TrackerTopology, TrackerTopologyRcd> trackerTopologyESToken_;
0066 
0067   // other data members
0068   unsigned int nHitsPerSeed_;
0069 
0070   std::vector<std::vector<TrackingLayer>> seedingLayers;
0071   SeedingTree<TrackingLayer> _seedingTree;
0072 
0073   std::unique_ptr<SeedCreator> seedCreator;
0074   std::string measurementTrackerLabel;
0075 
0076   std::unique_ptr<SeedFinderSelector> seedFinderSelector;
0077 
0078 public:
0079   TrajectorySeedProducer(const edm::ParameterSet& conf);
0080 
0081   void produce(edm::Event&, const edm::EventSetup&) override;
0082 };
0083 
0084 template class SeedingTree<TrackingLayer>;
0085 template class SeedingNode<TrackingLayer>;
0086 
0087 TrajectorySeedProducer::TrajectorySeedProducer(const edm::ParameterSet& conf) : trackerTopologyESToken_(esConsumes()) {
0088   // products
0089   produces<TrajectorySeedCollection>();
0090 
0091   // consumes
0092   recHitCombinationsToken =
0093       consumes<FastTrackerRecHitCombinationCollection>(conf.getParameter<edm::InputTag>("recHitCombinations"));
0094   if (conf.exists("hitMasks")) {
0095     hitMasksToken = consumes<std::vector<bool>>(conf.getParameter<edm::InputTag>("hitMasks"));
0096   }
0097 
0098   // read Layers
0099   std::vector<std::string> layerStringList =
0100       conf.getParameter<edm::ParameterSet>("seedFinderSelector").getParameter<std::vector<std::string>>("layerList");
0101   std::string layerBegin = *(layerStringList.cbegin());
0102   nHitsPerSeed_ = 0;
0103   for (auto it = layerStringList.cbegin(); it < layerStringList.cend(); ++it) {
0104     std::vector<TrackingLayer> trackingLayerList;
0105     std::string line = *it;
0106     std::string::size_type pos = 0;
0107     unsigned int nHitsPerSeed = 0;
0108     while (pos != std::string::npos) {
0109       pos = line.find('+');
0110       std::string layer = line.substr(0, pos);
0111       TrackingLayer layerSpec = TrackingLayer::createFromString(layer);
0112       trackingLayerList.push_back(layerSpec);
0113       line = line.substr(pos + 1, std::string::npos);
0114       nHitsPerSeed++;
0115     }
0116     if (it == layerStringList.cbegin()) {
0117       nHitsPerSeed_ = nHitsPerSeed;
0118     } else if (nHitsPerSeed_ != nHitsPerSeed) {
0119       throw cms::Exception("FastSimTracking") << "All allowed seed layer definitions must have same elements";
0120     }
0121     _seedingTree.insert(trackingLayerList);
0122   }
0123 
0124   // seed finder selector
0125   if (conf.exists("seedFinderSelector")) {
0126     seedFinderSelector = std::make_unique<SeedFinderSelector>(
0127         conf.getParameter<edm::ParameterSet>("seedFinderSelector"), consumesCollector());
0128   }
0129 
0130   /// regions
0131   trackingRegionToken = consumes<edm::OwnVector<TrackingRegion>>(conf.getParameter<edm::InputTag>("trackingRegions"));
0132 
0133   // seed creator
0134   const edm::ParameterSet& seedCreatorPSet = conf.getParameter<edm::ParameterSet>("SeedCreatorPSet");
0135   std::string seedCreatorName = seedCreatorPSet.getParameter<std::string>("ComponentName");
0136   seedCreator = SeedCreatorFactory::get()->create(seedCreatorName, seedCreatorPSet, consumesCollector());
0137 }
0138 
0139 void TrajectorySeedProducer::produce(edm::Event& e, const edm::EventSetup& es) {
0140   // services
0141   auto const& trackerTopology = es.getData(trackerTopologyESToken_);
0142 
0143   // input data
0144   edm::Handle<FastTrackerRecHitCombinationCollection> recHitCombinations;
0145   e.getByToken(recHitCombinationsToken, recHitCombinations);
0146   const std::vector<bool>* hitMasks = nullptr;
0147   if (!hitMasksToken.isUninitialized()) {
0148     edm::Handle<std::vector<bool>> hitMasksHandle;
0149     e.getByToken(hitMasksToken, hitMasksHandle);
0150     hitMasks = &(*hitMasksHandle);
0151   }
0152 
0153   // output data
0154   std::unique_ptr<TrajectorySeedCollection> output(new TrajectorySeedCollection());
0155 
0156   // read the regions;
0157   edm::Handle<edm::OwnVector<TrackingRegion>> hregions;
0158   e.getByToken(trackingRegionToken, hregions);
0159   const auto& regions = *hregions;
0160   // and make sure there is at least one region
0161   if (regions.empty()) {
0162     e.put(std::move(output));
0163     return;
0164   }
0165 
0166   // instantiate the seed finder
0167   SeedFinder seedFinder(_seedingTree, trackerTopology);
0168   if (seedFinderSelector) {
0169     seedFinderSelector->initEvent(e, es);
0170     seedFinder.addHitSelector(seedFinderSelector.get(), nHitsPerSeed_);
0171   }
0172 
0173   // loop over the combinations
0174   for (unsigned icomb = 0; icomb < recHitCombinations->size(); ++icomb) {
0175     FastTrackerRecHitCombination recHitCombination = (*recHitCombinations)[icomb];
0176 
0177     // create a list of hits cleaned from masked hits
0178     std::vector<const FastTrackerRecHit*> seedHitCandidates;
0179     for (const auto& _hit : recHitCombination) {
0180       if (hitMasks && fastTrackingUtilities::hitIsMasked(_hit.get(), *hitMasks)) {
0181         continue;
0182       }
0183       seedHitCandidates.push_back(_hit.get());
0184     }
0185 
0186     // loop over the regions
0187     for (const auto& region : regions) {
0188       // set the region used in the selector
0189       if (seedFinderSelector) {
0190         seedFinderSelector->setTrackingRegion(&region);
0191       }
0192 
0193       // find hits compatible with the seed requirements
0194       std::vector<unsigned int> seedHitNumbers = seedFinder.getSeed(seedHitCandidates);
0195 
0196       // create a seed from those hits
0197       if (seedHitNumbers.size() > 1) {
0198         // copy the hits
0199         edm::OwnVector<FastTrackerRecHit> seedHits;
0200         for (unsigned iIndex = 0; iIndex < seedHitNumbers.size(); ++iIndex) {
0201           seedHits.push_back(seedHitCandidates[seedHitNumbers[iIndex]]->clone());
0202         }
0203         // make them aware of the combination they originate from
0204         fastTrackingUtilities::setRecHitCombinationIndex(seedHits, icomb);
0205 
0206         // create the seed
0207         seedCreator->init(region, es, nullptr);
0208         seedCreator->makeSeed(*output,
0209                               SeedingHitSet(&seedHits[0],
0210                                             &seedHits[1],
0211                                             seedHits.size() >= 3 ? &seedHits[2] : nullptr,
0212                                             seedHits.size() >= 4 ? &seedHits[3] : nullptr));
0213         break;  // break the loop over the regions
0214       }
0215     }
0216   }
0217   e.put(std::move(output));
0218 }
0219 
0220 #include "FWCore/Framework/interface/MakerMacros.h"
0221 DEFINE_FWK_MODULE(TrajectorySeedProducer);