Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:11:23

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<std::vector<std::unique_ptr<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(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   auto const& regions = e.get(trackingRegionToken);
0158   // and make sure there is at least one region
0159   if (regions.empty()) {
0160     e.put(std::move(output));
0161     return;
0162   }
0163 
0164   // instantiate the seed finder
0165   SeedFinder seedFinder(_seedingTree, trackerTopology);
0166   if (seedFinderSelector) {
0167     seedFinderSelector->initEvent(e, es);
0168     seedFinder.addHitSelector(seedFinderSelector.get(), nHitsPerSeed_);
0169   }
0170 
0171   // loop over the combinations
0172   for (unsigned icomb = 0; icomb < recHitCombinations->size(); ++icomb) {
0173     FastTrackerRecHitCombination recHitCombination = (*recHitCombinations)[icomb];
0174 
0175     // create a list of hits cleaned from masked hits
0176     std::vector<const FastTrackerRecHit*> seedHitCandidates;
0177     for (const auto& _hit : recHitCombination) {
0178       if (hitMasks && fastTrackingUtilities::hitIsMasked(_hit.get(), *hitMasks)) {
0179         continue;
0180       }
0181       seedHitCandidates.push_back(_hit.get());
0182     }
0183 
0184     // loop over the regions
0185     for (const auto& region : regions) {
0186       // set the region used in the selector
0187       if (seedFinderSelector) {
0188         seedFinderSelector->setTrackingRegion(region.get());
0189       }
0190 
0191       // find hits compatible with the seed requirements
0192       std::vector<unsigned int> seedHitNumbers = seedFinder.getSeed(seedHitCandidates);
0193 
0194       // create a seed from those hits
0195       if (seedHitNumbers.size() > 1) {
0196         // copy the hits
0197         edm::OwnVector<FastTrackerRecHit> seedHits;
0198         for (unsigned iIndex = 0; iIndex < seedHitNumbers.size(); ++iIndex) {
0199           seedHits.push_back(seedHitCandidates[seedHitNumbers[iIndex]]->clone());
0200         }
0201         // make them aware of the combination they originate from
0202         fastTrackingUtilities::setRecHitCombinationIndex(seedHits, icomb);
0203 
0204         // create the seed
0205         seedCreator->init(*region, es, nullptr);
0206         seedCreator->makeSeed(*output,
0207                               SeedingHitSet(&seedHits[0],
0208                                             &seedHits[1],
0209                                             seedHits.size() >= 3 ? &seedHits[2] : nullptr,
0210                                             seedHits.size() >= 4 ? &seedHits[3] : nullptr));
0211         break;  // break the loop over the regions
0212       }
0213     }
0214   }
0215   e.put(std::move(output));
0216 }
0217 
0218 #include "FWCore/Framework/interface/MakerMacros.h"
0219 DEFINE_FWK_MODULE(TrajectorySeedProducer);