Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 23:29:33

0001 // -*- C++ -*-
0002 //
0003 // Package:    CalibrationTrackSelectorFromDetIdList
0004 // Class:      CalibrationTrackSelectorFromDetIdList
0005 //
0006 /**\class CalibrationTrackSelectorFromDetIdList
0007 CalibrationTrackSelectorFromDetIdList.cc
0008 Calibration/TkAlCaRecoProducers/plugins/CalibrationTrackSelectorFromDetIdList.cc
0009 Description: Selects tracks that have at leaast one valid hit on a given set of
0010 Tracker DetIds
0011 */
0012 //
0013 // Original Author:  Marco Musich
0014 //         Created:  Wed Aug  22 09:17:01 CEST 2018
0015 //
0016 //
0017 
0018 // system include files
0019 #include <memory>
0020 
0021 // user include files
0022 #include "CommonTools/UtilAlgos/interface/DetIdSelector.h"
0023 #include "DataFormats/TrackCandidate/interface/TrackCandidateCollection.h"
0024 #include "DataFormats/TrackReco/interface/Track.h"
0025 #include "DataFormats/TrackReco/interface/TrackFwd.h"
0026 #include "FWCore/Framework/interface/ESHandle.h"
0027 #include "FWCore/Framework/interface/Event.h"
0028 #include "FWCore/Framework/interface/Frameworkfwd.h"
0029 #include "FWCore/Framework/interface/MakerMacros.h"
0030 #include "FWCore/Framework/interface/stream/EDProducer.h"
0031 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0032 #include "FWCore/Utilities/interface/InputTag.h"
0033 #include "Geometry/TrackerGeometryBuilder/interface/TrackerGeometry.h"
0034 #include "MagneticField/Records/interface/IdealMagneticFieldRecord.h"
0035 #include "TrackingTools/Records/interface/TransientRecHitRecord.h"
0036 #include "TrackingTools/TrajectoryState/interface/TrajectoryStateOnSurface.h"
0037 #include "TrackingTools/TrajectoryState/interface/TrajectoryStateTransform.h"
0038 #include "TrackingTools/TransientTrackingRecHit/interface/TransientTrackingRecHitBuilder.h"
0039 
0040 //
0041 // class decleration
0042 //
0043 
0044 class dso_hidden CalibrationTrackSelectorFromDetIdList final : public edm::stream::EDProducer<> {
0045 public:
0046   explicit CalibrationTrackSelectorFromDetIdList(const edm::ParameterSet &);
0047   ~CalibrationTrackSelectorFromDetIdList() override;
0048 
0049 private:
0050   void beginRun(edm::Run const &run, const edm::EventSetup &) override;
0051   void produce(edm::Event &, const edm::EventSetup &) override;
0052   edm::EDGetTokenT<reco::TrackCollection> m_label;
0053   TrackCandidate makeCandidate(const reco::Track &tk,
0054                                std::vector<const TrackingRecHit *>::iterator hitsBegin,
0055                                std::vector<const TrackingRecHit *>::iterator hitsEnd);
0056 
0057   const edm::ESGetToken<TrackerGeometry, TrackerDigiGeometryRecord> geometryToken_;
0058   const edm::ESGetToken<MagneticField, IdealMagneticFieldRecord> magFieldToken_;
0059 
0060   std::vector<DetIdSelector> detidsels_;
0061   bool m_verbose;
0062 
0063   edm::ESHandle<TrackerGeometry> theGeometry;
0064   edm::ESHandle<MagneticField> theMagField;
0065 };
0066 
0067 CalibrationTrackSelectorFromDetIdList::CalibrationTrackSelectorFromDetIdList(const edm::ParameterSet &iConfig)
0068     : geometryToken_(esConsumes<TrackerGeometry, TrackerDigiGeometryRecord, edm::Transition::BeginRun>()),
0069       magFieldToken_(esConsumes<MagneticField, IdealMagneticFieldRecord, edm::Transition::BeginRun>()),
0070       detidsels_() {
0071   std::vector<edm::ParameterSet> selconfigs = iConfig.getParameter<std::vector<edm::ParameterSet>>("selections");
0072 
0073   for (std::vector<edm::ParameterSet>::const_iterator selconfig = selconfigs.begin(); selconfig != selconfigs.end();
0074        ++selconfig) {
0075     DetIdSelector selection(*selconfig);
0076     detidsels_.push_back(selection);
0077   }
0078 
0079   m_verbose = iConfig.getUntrackedParameter<bool>("verbose");
0080   m_label = consumes<reco::TrackCollection>(iConfig.getParameter<edm::InputTag>("Input"));
0081   produces<TrackCandidateCollection>();
0082 }
0083 
0084 CalibrationTrackSelectorFromDetIdList::~CalibrationTrackSelectorFromDetIdList() {}
0085 
0086 //
0087 // member functions
0088 //
0089 
0090 // ------------ method called to produce the data  ------------
0091 void CalibrationTrackSelectorFromDetIdList::produce(edm::Event &iEvent, const edm::EventSetup &iSetup) {
0092   using namespace edm;
0093   using namespace std;
0094 
0095   edm::Handle<std::vector<reco::Track>> trackCollectionHandle;
0096   iEvent.getByToken(m_label, trackCollectionHandle);
0097   auto const &tracks = *trackCollectionHandle;
0098 
0099   auto output = std::make_unique<TrackCandidateCollection>();
0100 
0101   // loop on tracks
0102   for (auto &trk : tracks) {
0103     std::vector<const TrackingRecHit *> hits;
0104 
0105     bool saveTrack(false);
0106 
0107     for (auto const &hit : trk.recHits()) {
0108       DetId detid = hit->geographicalId();
0109 
0110       for (const auto &detidsel : detidsels_) {
0111         if (detidsel.isSelected(detid)) {
0112           LogDebug("CalibrationTrackSelectorFromDetIdList") << "Selected by selection " << detid.rawId();
0113           saveTrack = true;
0114           break;
0115         }
0116       }
0117 
0118       // here there will be the selection
0119       hits.emplace_back(hit);
0120     }
0121 
0122     if (saveTrack) {
0123       output->push_back(makeCandidate(trk, hits.begin(), hits.end()));
0124     }
0125   }
0126   iEvent.put(std::move(output));
0127 }
0128 
0129 TrackCandidate CalibrationTrackSelectorFromDetIdList::makeCandidate(
0130     const reco::Track &tk,
0131     std::vector<const TrackingRecHit *>::iterator hitsBegin,
0132     std::vector<const TrackingRecHit *>::iterator hitsEnd) {
0133   PropagationDirection pdir = tk.seedDirection();
0134   PTrajectoryStateOnDet state;
0135   if (pdir == anyDirection)
0136     throw cms::Exception("UnimplementedFeature") << "Cannot work with tracks that have 'anyDirecton' \n";
0137 
0138   if ((pdir == alongMomentum) == ((tk.outerPosition() - tk.innerPosition()).Dot(tk.momentum()) >= 0)) {
0139     // use inner state
0140     TrajectoryStateOnSurface originalTsosIn(
0141         trajectoryStateTransform::innerStateOnSurface(tk, *theGeometry, &*theMagField));
0142     state = trajectoryStateTransform::persistentState(originalTsosIn, DetId(tk.innerDetId()));
0143   } else {
0144     // use outer state
0145     TrajectoryStateOnSurface originalTsosOut(
0146         trajectoryStateTransform::outerStateOnSurface(tk, *theGeometry, &*theMagField));
0147     state = trajectoryStateTransform::persistentState(originalTsosOut, DetId(tk.outerDetId()));
0148   }
0149   TrajectorySeed seed(state, TrackCandidate::RecHitContainer(), pdir);
0150   TrackCandidate::RecHitContainer ownHits;
0151   ownHits.reserve(hitsEnd - hitsBegin);
0152   for (; hitsBegin != hitsEnd; ++hitsBegin) {
0153     ownHits.push_back((*hitsBegin)->clone());
0154   }
0155 
0156   TrackCandidate cand(ownHits, seed, state, tk.seedRef());
0157 
0158   return cand;
0159 }
0160 
0161 void CalibrationTrackSelectorFromDetIdList::beginRun(edm::Run const &run, const edm::EventSetup &iSetup) {
0162   theGeometry = iSetup.getHandle(geometryToken_);
0163   theMagField = iSetup.getHandle(magFieldToken_);
0164 
0165   if (m_verbose) {
0166     for (const auto &detidsel : detidsels_) {
0167       auto theDetIds = theGeometry.product()->detIds();
0168       for (const auto &theDet : theDetIds) {
0169         if (detidsel.isSelected(theDet)) {
0170           LogDebug("CalibrationTrackSelectorFromDetIdList") << "detid: " << theDet.rawId() << " is taken" << std::endl;
0171         }
0172       }
0173     }
0174   }
0175 }
0176 
0177 // define this as a plug-in
0178 DEFINE_FWK_MODULE(CalibrationTrackSelectorFromDetIdList);