CMS 3D CMS Logo

PixelTrackProducerFromSoA.cc
Go to the documentation of this file.
1 #include <algorithm>
2 #include <cmath>
3 //#include <iostream>
4 #include <memory>
5 #include <numeric>
6 #include <string>
7 #include <utility>
8 #include <vector>
9 
42 
43 #include "storeTracks.h"
44 
49 template <typename TrackerTraits>
54  using IndToEdm = std::vector<uint32_t>;
55 
56 public:
57  explicit PixelTrackProducerFromSoAT(const edm::ParameterSet &iConfig);
58  ~PixelTrackProducerFromSoAT() override = default;
59 
60  static void fillDescriptions(edm::ConfigurationDescriptions &descriptions);
61 
62 private:
63  void produce(edm::StreamID streamID, edm::Event &iEvent, const edm::EventSetup &iSetup) const override;
64 
65  // Event Data tokens
70  // Event Setup tokens
73 
74  int32_t const minNumberOfHits_;
76 };
77 
78 template <typename TrackerTraits>
80  : tBeamSpot_(consumes<reco::BeamSpot>(iConfig.getParameter<edm::InputTag>("beamSpot"))),
81  tokenTrack_(consumes(iConfig.getParameter<edm::InputTag>("trackSrc"))),
82  cpuHits_(consumes<SiPixelRecHitCollectionNew>(iConfig.getParameter<edm::InputTag>("pixelRecHitLegacySrc"))),
83  hmsToken_(consumes<HMSstorage>(iConfig.getParameter<edm::InputTag>("pixelRecHitLegacySrc"))),
84  idealMagneticFieldToken_(esConsumes()),
85  ttTopoToken_(esConsumes()),
86  minNumberOfHits_(iConfig.getParameter<int>("minNumberOfHits")),
87  minQuality_(pixelTrack::qualityByName(iConfig.getParameter<std::string>("minQuality"))) {
89  throw cms::Exception("PixelTrackConfiguration")
90  << iConfig.getParameter<std::string>("minQuality") + " is not a pixelTrack::Quality";
91  }
93  throw cms::Exception("PixelTrackConfiguration")
94  << iConfig.getParameter<std::string>("minQuality") + " not supported";
95  }
96  produces<TrackingRecHitCollection>();
97  produces<reco::TrackExtraCollection>();
98  // TrackCollection refers to TrackingRechit and TrackExtra
99  // collections, need to declare its production after them to work
100  // around a rare race condition in framework scheduling
101  produces<reco::TrackCollection>();
102  produces<IndToEdm>();
103 }
104 
105 template <typename TrackerTraits>
108  desc.add<edm::InputTag>("beamSpot", edm::InputTag("offlineBeamSpot"));
109  desc.add<edm::InputTag>("trackSrc", edm::InputTag("pixelTracksSoA"));
110  desc.add<edm::InputTag>("pixelRecHitLegacySrc", edm::InputTag("siPixelRecHitsPreSplittingLegacy"));
111  desc.add<int>("minNumberOfHits", 0);
112  desc.add<std::string>("minQuality", "loose");
113  descriptions.addWithDefaultLabel(desc);
114 }
115 
116 template <typename TrackerTraits>
119  const edm::EventSetup &iSetup) const {
120  // enum class Quality : uint8_t { bad = 0, edup, dup, loose, strict, tight, highPurity };
129 
130  // std::cout << "Converting gpu helix in reco tracks" << std::endl;
131 
132  auto indToEdmP = std::make_unique<IndToEdm>();
133  auto &indToEdm = *indToEdmP;
134 
135  auto const &idealField = iSetup.getData(idealMagneticFieldToken_);
136 
138 
139  auto const &httopo = iSetup.getData(ttTopoToken_);
140 
141  const auto &bsh = iEvent.get(tBeamSpot_);
142  GlobalPoint bs(bsh.x0(), bsh.y0(), bsh.z0());
143 
144  auto const &rechits = iEvent.get(cpuHits_);
145  std::vector<TrackingRecHit const *> hitmap;
146  auto const &rcs = rechits.data();
147  auto const nhits = rcs.size();
148 
149  hitmap.resize(nhits, nullptr);
150 
151  auto const *hitsModuleStart = iEvent.get(hmsToken_).get();
152 
153  for (auto const &hit : rcs) {
154  auto const &thit = static_cast<BaseTrackerRecHit const &>(hit);
155  auto const detI = thit.det()->index();
156  auto const &clus = thit.firstClusterRef();
157  assert(clus.isPixel());
158  auto const idx = hitsModuleStart[detI] + clus.pixelCluster().originalId();
159  if (idx >= hitmap.size())
160  hitmap.resize(idx + 256, nullptr); // only in case of hit overflow in one module
161 
162  assert(nullptr == hitmap[idx]);
163  hitmap[idx] = &hit;
164  }
165 
166  std::vector<const TrackingRecHit *> hits;
167  hits.reserve(5);
168 
169  auto const &tsoa = iEvent.get(tokenTrack_);
170  auto const *quality = tsoa.view().quality();
171  auto const &hitIndices = tsoa.view().hitIndices();
172  auto nTracks = tsoa.view().nTracks();
173 
174  tracks.reserve(nTracks);
175 
176  int32_t nt = 0;
177 
178  // sort index by pt
179  std::vector<int32_t> sortIdxs(nTracks);
180  std::iota(sortIdxs.begin(), sortIdxs.end(), 0);
181  // sort good-quality tracks by pt, keep bad-quality tracks in the bottom
182  std::sort(sortIdxs.begin(), sortIdxs.end(), [&](int32_t const i1, int32_t const i2) {
183  if (quality[i1] >= minQuality_ && quality[i2] >= minQuality_)
184  return tsoa.view()[i1].pt() > tsoa.view()[i2].pt();
185  else
186  return quality[i1] > quality[i2];
187  });
188 
189  // store the index of the SoA: indToEdm[index_SoAtrack] -> index_edmTrack (if it exists)
190  indToEdm.resize(sortIdxs.size(), -1);
191  for (const auto &it : sortIdxs) {
192  auto nHits = TracksHelpers::nHits(tsoa.view(), it);
193  assert(nHits >= 3);
194  auto q = quality[it];
195 
196  if (q < minQuality_)
197  continue;
198  if (nHits < minNumberOfHits_) // move to nLayers?
199  continue;
200  indToEdm[it] = nt;
201  ++nt;
202 
203  hits.resize(nHits);
204  auto b = hitIndices.begin(it);
205  for (int iHit = 0; iHit < nHits; ++iHit)
206  hits[iHit] = hitmap[*(b + iHit)];
207 
208  // mind: this values are respect the beamspot!
209  float chi2 = tsoa.view()[it].chi2();
210  float phi = TracksHelpers::phi(tsoa.view(), it);
211 
212  riemannFit::Vector5d ipar, opar;
213  riemannFit::Matrix5d icov, ocov;
214  TracksHelpers::template copyToDense<riemannFit::Vector5d, riemannFit::Matrix5d>(tsoa.view(), ipar, icov, it);
215  riemannFit::transformToPerigeePlane(ipar, icov, opar, ocov);
216 
217  LocalTrajectoryParameters lpar(opar(0), opar(1), opar(2), opar(3), opar(4), 1.);
219  for (int i = 0; i < 5; ++i)
220  for (int j = i; j < 5; ++j)
221  m(i, j) = ocov(i, j);
222 
223  float sp = std::sin(phi);
224  float cp = std::cos(phi);
225  Surface::RotationType rot(sp, -cp, 0, 0, 0, -1.f, cp, sp, 0);
226 
227  Plane impPointPlane(bs, rot);
229  impPointPlane.toGlobal(lpar.position()), impPointPlane.toGlobal(lpar.momentum()), lpar.charge(), &idealField);
230  JacobianLocalToCurvilinear jl2c(impPointPlane, lpar, idealField);
231 
232  AlgebraicSymMatrix55 mo = ROOT::Math::Similarity(jl2c.jacobian(), m);
233 
234  int ndof = 2 * hits.size() - 5;
235  chi2 = chi2 * ndof;
236  GlobalPoint vv = gp.position();
237  math::XYZPoint pos(vv.x(), vv.y(), vv.z());
238  GlobalVector pp = gp.momentum();
239  math::XYZVector mom(pp.x(), pp.y(), pp.z());
240 
241  auto track = std::make_unique<reco::Track>(chi2, ndof, pos, mom, gp.charge(), CurvilinearTrajectoryError(mo));
242 
243  // bad and edup not supported as fit not present or not reliable
244  auto tkq = recoQuality[int(q)];
245  track->setQuality(tkq);
246  // loose,tight and HP are inclusive
247  if (reco::TrackBase::highPurity == tkq) {
248  track->setQuality(reco::TrackBase::tight);
249  track->setQuality(reco::TrackBase::loose);
250  } else if (reco::TrackBase::tight == tkq) {
251  track->setQuality(reco::TrackBase::loose);
252  }
253  track->setQuality(tkq);
254  // filter???
255  tracks.emplace_back(track.release(), hits);
256  }
257  // std::cout << "processed " << nt << " good tuples " << tracks.size() << "out of " << indToEdm.size() << std::endl;
258 
259  // store tracks
260  storeTracks(iEvent, tracks, httopo);
261  iEvent.put(std::move(indToEdmP));
262 }
263 
266 
269 
void addWithDefaultLabel(ParameterSetDescription const &psetDescription)
ESGetTokenH3DDVariant esConsumes(std::string const &Record, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
T getParameter(std::string const &) const
Definition: ParameterSet.h:307
Eigen::Matrix< double, 5, 5 > Matrix5d
Definition: FitResult.h:17
T const & getData(const ESGetToken< T, R > &iToken) const noexcept(false)
Definition: EventSetup.h:119
const edm::EDGetTokenT< reco::BeamSpot > tBeamSpot_
const edm::EDGetTokenT< TrackSoAHost > tokenTrack_
Quality qualityByName(std::string const &name)
void storeTracks(Ev &ev, const TWH &tracksWithHits, const TrackerTopology &ttopo)
Definition: storeTracks.h:19
T z() const
Definition: PV3DBase.h:61
const edm::EDGetTokenT< SiPixelRecHitCollectionNew > cpuHits_
TrackQuality
track quality
Definition: TrackBase.h:150
int index() const
Definition: GeomDet.h:83
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
~PixelTrackProducerFromSoAT() override=default
Eigen::Matrix< double, 5, 1 > Vector5d
Definition: FitResult.h:13
assert(be >=bs)
void transformToPerigeePlane(VI5 const &ip, MI5 const &icov, VO5 &op, MO5 &ocov)
Definition: FitUtils.h:60
Definition: Plane.h:16
const GeomDet * det() const
string quality
T x() const
Definition: PV3DBase.h:59
T y() const
Definition: PV3DBase.h:60
int iEvent
Definition: GenABIO.cc:224
const edm::ESGetToken< MagneticField, IdealMagneticFieldRecord > idealMagneticFieldToken_
LocalVector momentum() const
Momentum vector in the local frame.
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
double f[11][100]
def template(fileName, svg, replaceme="REPLACEME")
Definition: svgfig.py:521
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
int nt
Definition: AMPTWrapper.h:42
TrackCharge charge() const
Charge (-1, 0 or 1)
GlobalPoint toGlobal(const Point2DBase< Scalar, LocalTag > lp) const
Definition: Surface.h:79
const edm::EDGetTokenT< HMSstorage > hmsToken_
pixelTrack::Quality const minQuality_
XYZVectorD XYZVector
spatial vector with cartesian internal representation
Definition: Vector3D.h:31
void produce(edm::StreamID streamID, edm::Event &iEvent, const edm::EventSetup &iSetup) const override
XYZPointD XYZPoint
point in space with cartesian internal representation
Definition: Point3D.h:12
PixelTrackProducerFromSoAT(const edm::ParameterSet &iConfig)
ROOT::Math::SMatrix< double, 5, 5, ROOT::Math::MatRepSym< double, 5 > > AlgebraicSymMatrix55
double b
Definition: hdecay.h:120
fixed size matrix
HLT enums.
std::vector< TrackWithRecHits > TracksWithRecHits
const edm::ESGetToken< TrackerTopology, TrackerTopologyRcd > ttTopoToken_
TupleMultiplicity< TrackerTraits > const *__restrict__ uint32_t nHits
LocalPoint position() const
Local x and y position coordinates.
def move(src, dest)
Definition: eostools.py:511
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)