CMS 3D CMS Logo

ProducerKFout.cc
Go to the documentation of this file.
12 
16 
17 #include <string>
18 #include <numeric>
19 
20 using namespace std;
21 using namespace edm;
22 using namespace trackerTFP;
23 using namespace tt;
24 
25 namespace trklet {
26 
36  class ProducerKFout : public stream::EDProducer<> {
37  public:
38  explicit ProducerKFout(const ParameterSet&);
39  ~ProducerKFout() override {}
40 
41  private:
42  void beginRun(const Run&, const EventSetup&) override;
43  void produce(Event&, const EventSetup&) override;
44  void endJob() {}
45 
46  // ED input token of kf stubs
48  // ED input token of kf tracks
50  // ED input token of kf input to kf output TTTrack map
52  // ED output token for accepted kfout tracks
54  // ED output token for truncated kfout tracks
56  // Setup token
58  // DataFormats token
60  // configuration
62  // helper class to store configurations
63  const Setup* setup_;
64  // helper class to extract structured data from tt::Frames
66  // Bins for dPhi/dZ use to create weight LUT
67  vector<double> dPhiBins_;
68  vector<double> dZBins_;
69 
70  std::unique_ptr<L1TrackQuality> trackQualityModel_;
71  vector<int> tqBins_;
72  double tqTanlScale_;
73  double tqZ0Scale_;
74  static constexpr double ap_fixed_rescale = 32.0;
75 
76  // For convenience and keeping readable code, accessed many times
78 
80 
81  // Helper function to convert floating value to bin
82  template <typename T>
83  unsigned int digitise(const T& bins, double value, double factor) {
84  unsigned int bin = 0;
85  for (unsigned int i = 0; i < bins.size() - 1; i++) {
86  if (value * factor > bins[i] && value * factor <= bins[i + 1])
87  break;
88  bin++;
89  }
90  return bin;
91  }
92  };
93 
94  ProducerKFout::ProducerKFout(const ParameterSet& iConfig) : iConfig_(iConfig) {
95  const string& labelKF = iConfig.getParameter<string>("LabelKF");
96  const string& labelAS = iConfig.getParameter<string>("LabelAS");
97  const string& branchStubs = iConfig.getParameter<string>("BranchAcceptedStubs");
98  const string& branchTracks = iConfig.getParameter<string>("BranchAcceptedTracks");
99  const string& branchLost = iConfig.getParameter<string>("BranchLostTracks");
100  // book in- and output ED products
101  edGetTokenStubs_ = consumes<StreamsStub>(InputTag(labelKF, branchStubs));
102  edGetTokenTracks_ = consumes<StreamsTrack>(InputTag(labelKF, branchTracks));
103  edGetTokenTTTrackRefMap_ = consumes<TTTrackRefMap>(InputTag(labelAS, branchTracks));
104  edPutTokenAccepted_ = produces<StreamsTrack>(branchTracks);
105  edPutTokenLost_ = produces<StreamsTrack>(branchLost);
106  // book ES products
107  esGetTokenSetup_ = esConsumes<Setup, SetupRcd, Transition::BeginRun>();
108  esGetTokenDataFormats_ = esConsumes<DataFormats, DataFormatsRcd, Transition::BeginRun>();
109  // initial ES products
110  setup_ = nullptr;
111  dataFormats_ = nullptr;
112 
113  trackQualityModel_ = std::make_unique<L1TrackQuality>(iConfig.getParameter<edm::ParameterSet>("TrackQualityPSet"));
114  edm::ParameterSet trackQualityPSset = iConfig.getParameter<edm::ParameterSet>("TrackQualityPSet");
115  tqBins_ = trackQualityPSset.getParameter<vector<int>>("tqemu_bins");
116  tqTanlScale_ = trackQualityPSset.getParameter<double>("tqemu_TanlScale");
117  tqZ0Scale_ = trackQualityPSset.getParameter<double>("tqemu_Z0Scale");
118  }
119 
120  void ProducerKFout::beginRun(const Run& iRun, const EventSetup& iSetup) {
121  // helper class to store configurations
122  setup_ = &iSetup.getData(esGetTokenSetup_);
124  return;
125  // check process history if desired
126  if (iConfig_.getParameter<bool>("CheckHistory"))
128  // helper class to extract structured data from tt::Frames
130 
131  // Calculate 1/dz**2 and 1/dphi**2 bins for v0 and v1 weightings
132 
133  float temp_dphi = 0.0;
134  float temp_dz = 0.0;
135  for (int i = 0;
136  i < pow(2, dataFormats_->width(Variable::dPhi, Process::kfin)) / pow(2, setup_->weightBinFraction());
137  i++) {
138  temp_dphi =
139  pow(dataFormats_->base(Variable::dPhi, Process::kfin) * (i + 1) * pow(2, setup_->weightBinFraction()), -2);
140  temp_dphi = temp_dphi / setup_->dphiTruncation();
141  temp_dphi = std::floor(temp_dphi);
142  dPhiBins_.push_back(temp_dphi * setup_->dphiTruncation());
143  }
144  for (int i = 0; i < pow(2, dataFormats_->width(Variable::dZ, Process::kfin)) / pow(2, setup_->weightBinFraction());
145  i++) {
146  temp_dz =
147  pow(dataFormats_->base(Variable::dZ, Process::kfin) * (i + 1) * pow(2, setup_->weightBinFraction()), -2);
148  temp_dz = temp_dz * setup_->dzTruncation();
149  temp_dz = std::ceil(temp_dz);
150  dZBins_.push_back(temp_dz / setup_->dzTruncation());
151  }
154  }
155 
157  // empty KFout product
160  // read in KF Product and produce KFout product
162  Handle<StreamsStub> handleStubs;
163  iEvent.getByToken<StreamsStub>(edGetTokenStubs_, handleStubs);
164  const StreamsStub& streamsStubs = *handleStubs.product();
165  Handle<StreamsTrack> handleTracks;
166  iEvent.getByToken<StreamsTrack>(edGetTokenTracks_, handleTracks);
167  const StreamsTrack& streamsTracks = *handleTracks.product();
168  Handle<TTTrackRefMap> handleTTTrackRefMap;
169  iEvent.getByToken<TTTrackRefMap>(edGetTokenTTTrackRefMap_, handleTTTrackRefMap);
170  const TTTrackRefMap& ttTrackRefMap = *handleTTTrackRefMap.product();
171  // 18 Output Links (First Vector) each has a vector of tracks per event (second vector) each track is 3 32 bit TTBV partial tracks
172  vector<vector<TTBV>> sortedPartialTracks(setup_->numRegions() * setup_->tfpNumChannel(), vector<TTBV>(0));
173 
174  TrackKFOutSAPtrCollectionss inTrackStreams;
175  TrackKFOutSAPtrCollectionss outTrackStreams;
176 
177  // Setup empty collections for input tracks to be routed
178  for (int iRegion = 0; iRegion < setup_->numRegions(); iRegion++) {
179  TrackKFOutSAPtrCollections temp_collection;
180  for (int iLink = 0; iLink < setup_->tfpNumChannel(); iLink++) {
182  for (int iTrack = 0; iTrack < setup_->numFramesIO(); iTrack++)
183  temp.emplace_back(std::make_shared<TrackKFOut>());
184  temp_collection.push_back(temp);
185  }
186  outTrackStreams.push_back(temp_collection);
187  }
188 
189  // Setup empty collections for output tracks from routing
190  for (int iRegion = 0; iRegion < setup_->numRegions(); iRegion++) {
191  TrackKFOutSAPtrCollections temp_collection;
192  for (int iLink = 0; iLink < numWorkers_; iLink++) {
194  for (int iTrack = 0; iTrack < setup_->numFramesIO(); iTrack++)
195  temp.emplace_back(std::make_shared<TrackKFOut>());
196  temp_collection.push_back(temp);
197  }
198  inTrackStreams.push_back(temp_collection);
199  }
200 
201  StreamsTrack outputStreamsTracks(setup_->numRegions() * setup_->tfpNumChannel());
202 
203  // Setup containers for track quality
204  float tempTQMVAPreSig = 0.0;
205  // Due to ap_fixed implementation in CMSSW this 10,5 must be specified at compile time, TODO make this a changeable parameter
206  std::vector<ap_fixed<10, 5>> trackQuality_inputs = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
207 
208  for (int iLink = 0; iLink < (int)streamsTracks.size(); iLink++) {
209  for (int iTrack = 0; iTrack < (int)streamsTracks[iLink].size(); iTrack++) {
210  const auto& track = streamsTracks[iLink].at(iTrack);
211  TrackKF inTrack(track, dataFormats_);
212 
213  double temp_z0 = inTrack.zT() - ((inTrack.cot() * setup_->chosenRofZ()));
214 
215  // Correction to Phi calcuation depending if +ve/-ve phi sector
216  const double baseSectorCorr = inTrack.sectorPhi() ? -setup_->baseSector() : setup_->baseSector();
217 
218  double temp_phi0 = inTrack.phiT() - ((inTrack.inv2R()) * setup_->hybridChosenRofPhi()) + baseSectorCorr;
219 
220  double temp_tanL = inTrack.cotGlobal();
221 
223 
224  double tempchi2rphi = 0;
225  double tempchi2rz = 0;
226 
227  int temp_nstub = 0;
228  int temp_ninterior = 0;
229  bool counter = false;
230 
231  for (int iStub = 0; iStub < setup_->numLayers() - 1; iStub++) {
232  const auto& stub = streamsStubs[setup_->numLayers() * iLink + iStub].at(iTrack);
233  StubKF inStub(stub, dataFormats_, iStub);
234 
235  if (!stub.first.isNonnull()) {
236  if (counter)
237  temp_ninterior += 1;
238  continue;
239  }
240 
241  counter = true;
242 
243  hitPattern.set(iStub);
244  temp_nstub += 1;
245  double phiSquared = pow(inStub.phi(), 2);
246  double zSquared = pow(inStub.z(), 2);
247 
248  double tempv0 = dPhiBins_[(inStub.dPhi() / (dataFormats_->base(Variable::dPhi, Process::kfin) *
249  pow(2, setup_->weightBinFraction())))];
250  double tempv1 = dZBins_[(
251  inStub.dZ() / (dataFormats_->base(Variable::dZ, Process::kfin) * pow(2, setup_->weightBinFraction())))];
252 
253  double tempRphi = phiSquared * tempv0;
254  double tempRz = zSquared * tempv1;
255 
256  tempchi2rphi += tempRphi;
257  tempchi2rz += tempRz;
258  } // Iterate over track stubs
259 
260  // Create bit vectors for eacch output, including digitisation of chi2
261  // TODO implement extraMVA, bendChi2, d0
263  TTBV extraMVA(0, TTTrack_TrackWord::TrackBitWidths::kMVAOtherSize, false);
264  TTBV bendChi2(0, TTTrack_TrackWord::TrackBitWidths::kBendChi2Size, false);
265  TTBV chi2rphi(digitise(TTTrack_TrackWord::chi2RPhiBins, tempchi2rphi, (double)setup_->kfoutchi2rphiConv()),
266  TTTrack_TrackWord::TrackBitWidths::kChi2RPhiSize,
267  false);
268  TTBV chi2rz(digitise(TTTrack_TrackWord::chi2RZBins, tempchi2rz, (double)setup_->kfoutchi2rzConv()),
269  TTTrack_TrackWord::TrackBitWidths::kChi2RZSize,
270  false);
271  TTBV d0(0, TTTrack_TrackWord::TrackBitWidths::kD0Size, false);
272  TTBV z0(
273  temp_z0, dataFormats_->base(Variable::zT, Process::kf), TTTrack_TrackWord::TrackBitWidths::kZ0Size, true);
274  TTBV tanL(temp_tanL,
275  dataFormats_->base(Variable::cot, Process::kf),
276  TTTrack_TrackWord::TrackBitWidths::kTanlSize,
277  true);
278  TTBV phi0(temp_phi0,
279  dataFormats_->base(Variable::phiT, Process::kf),
280  TTTrack_TrackWord::TrackBitWidths::kPhiSize,
281  true);
282  TTBV invR(-inTrack.inv2R(),
283  dataFormats_->base(Variable::inv2R, Process::kf),
284  TTTrack_TrackWord::TrackBitWidths::kRinvSize + 1,
285  true);
286  invR.resize(TTTrack_TrackWord::TrackBitWidths::kRinvSize);
287 
288  // Create input vector for BDT
289  trackQuality_inputs = {
290  (std::trunc(tanL.val() / tqTanlScale_)) / ap_fixed_rescale,
291  (std::trunc(z0.val() / tqZ0Scale_)) / ap_fixed_rescale,
292  0,
293  temp_nstub,
294  temp_ninterior,
297 
298  // Run BDT emulation and package output into 3 bits
299  // output needs sigmoid transformation applied
300  tempTQMVAPreSig = trackQualityModel_->runEmulatedTQ(trackQuality_inputs);
301  TTBV tqMVA(digitise(L1TrackQuality::getTqMVAPreSigBins(), tempTQMVAPreSig, 1.0),
302  TTTrack_TrackWord::TrackBitWidths::kMVAQualitySize,
303  false);
304 
305  // Build 32 bit partial tracks for outputting in 64 bit packets
306  // 12 + 3 + 7 + 3 + 6
307  TTBV partialTrack3((d0 + bendChi2 + hitPattern + tqMVA + extraMVA), partialTrackWordBits_, false);
308  // 16 + 12 + 4
309  TTBV partialTrack2((tanL + z0 + chi2rz), partialTrackWordBits_, false);
310  // 1 + 15 + 12 + 4
311  TTBV partialTrack1((trackValid + invR + phi0 + chi2rphi), partialTrackWordBits_, false);
312 
313  int sortKey = (inTrack.sectorEta() < (int)(setup_->numSectorsEta() / 2)) ? 0 : 1;
314  // Set correct bit to valid for track valid
315  TrackKFOut temp_track(partialTrack1.set((partialTrackWordBits_ - 1)),
316  partialTrack2,
317  partialTrack3,
318  sortKey,
319  track,
320  iTrack,
321  iLink,
322  true);
323 
324  inTrackStreams[iLink / setup_->kfNumWorker()][iLink % setup_->kfNumWorker()][iTrack] =
325  (std::make_shared<TrackKFOut>(temp_track));
326  } // Iterate over Tracks
327  } // Iterate over Links
328  // Route Tracks in eta based on their sort key
329  for (int iRegion = 0; iRegion < setup_->numRegions(); iRegion++) {
330  int buffered_tracks[] = {0, 0};
331  for (int iTrack = 0; iTrack < setup_->numFramesIO() * ((double)TTBV::S_ / TTTrack_TrackWord::kTrackWordSize);
332  iTrack++) {
333  for (int iWorker = 0; iWorker < setup_->kfNumWorker(); iWorker++) {
334  for (int iLink = 0; iLink < setup_->tfpNumChannel(); iLink++) {
335  if ((inTrackStreams[iRegion][iWorker][iTrack]->sortKey() == iLink) &&
336  (inTrackStreams[iRegion][iWorker][iTrack]->dataValid() == true)) {
337  outTrackStreams[iRegion][iLink][buffered_tracks[iLink]] = inTrackStreams[iRegion][iWorker][iTrack];
338  buffered_tracks[iLink] = buffered_tracks[iLink] + 1;
339  }
340  }
341  }
342  }
343  }
344 
345  // Pack output of router onto each link, with correct partial tracks in correct places
346  for (int iRegion = 0; iRegion < setup_->numRegions(); iRegion++) {
347  for (int iLink = 0; iLink < setup_->tfpNumChannel(); iLink++) {
348  for (int iTrack = 0; iTrack < (int)outTrackStreams[iRegion][iLink].size(); iTrack++) {
349  sortedPartialTracks[2 * iRegion + iLink].push_back(
350  outTrackStreams[iRegion][iLink][iTrack]->PartialTrack1());
351  sortedPartialTracks[2 * iRegion + iLink].push_back(
352  outTrackStreams[iRegion][iLink][iTrack]->PartialTrack2());
353  sortedPartialTracks[2 * iRegion + iLink].push_back(
354  outTrackStreams[iRegion][iLink][iTrack]->PartialTrack3());
355  outputStreamsTracks[2 * iRegion + iLink].emplace_back(outTrackStreams[iRegion][iLink][iTrack]->track());
356  }
357  }
358  }
359  // Fill products and match up tracks
360  // store products
361  const TTBV nullBitTrack(0, partialTrackWordBits_, false);
362  for (int iLink = 0; iLink < (int)outputStreamsTracks.size(); iLink++) {
363  // Iterate through partial tracks
364  int numLinkTracks = (int)outputStreamsTracks[iLink].size();
365  if (numLinkTracks == 0)
366  continue; // Don't fill links if no tracks
367  if ((numLinkTracks % 2 != 0)) {
368  sortedPartialTracks[iLink].push_back(nullBitTrack); //Pad out final set of bits
369  outputStreamsTracks[iLink].emplace_back(
370  outputStreamsTracks[iLink][numLinkTracks++]); //Pad out with final repeated track
371  } //If there is an odd number of tracks
372  for (int iTrack = 0; iTrack < (int)(sortedPartialTracks[iLink].size()); iTrack++) {
373  if (iTrack % 2 != 1) // Write to links every other partial track, 3 partial tracks per full TTTrack
374  continue;
375  TTTrackRef trackRef;
376  for (auto& it : ttTrackRefMap) { //Iterate through ttTrackRefMap to find TTTrackRef Key by a TTTrack Value
377  if (it.second == outputStreamsTracks[iLink][(int)(iTrack - 1) / 3].first)
378  trackRef = it.first;
379  }
380  if ((int)iTrack / 3 <= setup_->numFramesIO() * ((double)TTBV::S_ / TTTrack_TrackWord::kTrackWordSize))
381  accepted[iLink].emplace_back(
382  std::make_pair(trackRef,
383  (sortedPartialTracks[iLink][iTrack - 1].slice(partialTrackWordBits_) +
384  sortedPartialTracks[iLink][iTrack].slice(partialTrackWordBits_))
385  .bs()));
386  else
387  lost[iLink].emplace_back(
388  std::make_pair(trackRef,
389  (sortedPartialTracks[iLink][iTrack - 1].slice(partialTrackWordBits_) +
390  sortedPartialTracks[iLink][iTrack].slice(partialTrackWordBits_))
391  .bs()));
392  } //Iterate through sorted partial tracks
393  } // Iterate through links
394  } // Config Supported
395  // store products
396  iEvent.emplace(edPutTokenAccepted_, std::move(accepted));
397  iEvent.emplace(edPutTokenLost_, std::move(lost));
398  }
399 } // namespace trklet
400 
double cot() const
Definition: DataFormats.h:968
EDPutTokenT< StreamsTrack > edPutTokenAccepted_
std::vector< std::vector< std::vector< std::shared_ptr< TrackKFOut > > > > TrackKFOutSAPtrCollectionss
Definition: DataFormats.h:1031
double zT() const
Definition: DataFormats.h:970
constexpr int32_t ceil(float num)
double base(Variable v, Process p) const
Definition: DataFormats.h:492
static constexpr std::array< double, 1<< TTTrack_TrackWord::TrackBitWidths::kMVAQualitySize > getTqMVAPreSigBins()
static constexpr std::array< double, 1<< TrackBitWidths::kChi2RPhiSize > chi2RPhiBins
double phiT() const
Definition: DataFormats.h:964
T getParameter(std::string const &) const
Definition: ParameterSet.h:307
ESGetToken< Setup, SetupRcd > esGetTokenSetup_
double phi() const
Definition: DataFormats.h:830
std::vector< StreamTrack > StreamsTrack
Definition: TTTypes.h:67
T const & getData(const ESGetToken< T, R > &iToken) const noexcept(false)
Definition: EventSetup.h:119
std::map< TTTrackRef, TTTrackRef > TTTrackRefMap
Definition: TTTypes.h:69
int kfoutchi2rzConv() const
Definition: Setup.h:511
double chosenRofZ() const
Definition: Setup.h:417
Bit vector used by Track Trigger emulators. Mainly used to convert integers into arbitrary (within ma...
Definition: TTBV.h:20
vector< int > tqBins_
vector< double > dPhiBins_
Class to process and provide run-time constants used by Track Trigger emulators.
Definition: Setup.h:44
std::vector< StreamStub > StreamsStub
Definition: TTTypes.h:66
int tfpNumChannel() const
Definition: Setup.h:408
T const * product() const
Definition: Handle.h:70
double dPhi() const
Definition: DataFormats.h:834
std::vector< std::vector< std::shared_ptr< TrackKFOut > > > TrackKFOutSAPtrCollections
Definition: DataFormats.h:1030
ESGetToken< DataFormats, DataFormatsRcd > esGetTokenDataFormats_
double cotGlobal() const
Definition: DataFormats.h:972
TTBV & resize(int size)
Definition: TTBV.h:237
double dZ() const
Definition: DataFormats.h:836
std::unique_ptr< L1TrackQuality > trackQualityModel_
int width(Variable v, Process p) const
Definition: DataFormats.h:490
int sectorPhi() const
Definition: DataFormats.h:960
int numSectorsEta() const
Definition: Setup.h:415
int iEvent
Definition: GenABIO.cc:224
Definition: TTTypes.h:54
double z() const
Definition: DataFormats.h:832
static constexpr double ap_fixed_rescale
int kfoutchi2rphiConv() const
Definition: Setup.h:509
double inv2R() const
Definition: DataFormats.h:966
int kfNumWorker() const
Definition: Setup.h:491
EDGetTokenT< StreamsStub > edGetTokenStubs_
double baseSector() const
Definition: Setup.h:424
const unsigned int kValidSize
int sectorEta() const
Definition: DataFormats.h:962
EDPutTokenT< StreamsTrack > edPutTokenLost_
bool configurationSupported() const
Definition: Setup.h:60
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
ProcessHistory const & processHistory() const
Definition: Run.cc:115
Definition: value.py:1
void beginRun(const Run &, const EventSetup &) override
TTBV & set()
Definition: TTBV.h:187
int weightBinFraction() const
Definition: Setup.h:513
int numLayers() const
Definition: Setup.h:215
double hybridChosenRofPhi() const
Definition: Setup.h:266
std::vector< TrackKFOutSAPtr > TrackKFOutSAPtrCollection
Definition: DataFormats.h:1029
void checkHistory(const edm::ProcessHistory &processHistory) const
Definition: Setup.cc:242
static constexpr float d0
void produce(Event &, const EventSetup &) override
static constexpr int kTrackWordSize
int numRegions() const
Definition: Setup.h:349
int dzTruncation() const
Definition: Setup.h:515
HLT enums.
vector< double > dZBins_
static constexpr std::array< double, 1<< TrackBitWidths::kChi2RZSize > chi2RZBins
EDGetTokenT< TTTrackRefMap > edGetTokenTTTrackRefMap_
Class to calculate and provide dataformats used by Track Trigger emulator.
Definition: DataFormats.h:216
Converts KF output into TFP output A bit accurate emulation of the track transformation, the eta routing and splitting of the 96-bit track words into 64-bit packets. Also run is a bit accurate emulation of the track quality BDT, whose output is also added to the track word.
static constexpr int S_
Definition: TTBV.h:22
const DataFormats * dataFormats_
long double T
EDGetTokenT< StreamsTrack > edGetTokenTracks_
int numFramesIO() const
Definition: Setup.h:157
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:29
def move(src, dest)
Definition: eostools.py:511
const Setup * setup_
Definition: Run.h:45
int dphiTruncation() const
Definition: Setup.h:517
unsigned int digitise(const T &bins, double value, double factor)