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 chi2 to chi2 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  bin = i;
88  }
89  return bin;
90  }
91  };
92 
93  ProducerKFout::ProducerKFout(const ParameterSet& iConfig) : iConfig_(iConfig) {
94  const string& labelKF = iConfig.getParameter<string>("LabelKF");
95  const string& labelAS = iConfig.getParameter<string>("LabelAS");
96  const string& branchStubs = iConfig.getParameter<string>("BranchAcceptedStubs");
97  const string& branchTracks = iConfig.getParameter<string>("BranchAcceptedTracks");
98  const string& branchLost = iConfig.getParameter<string>("BranchLostTracks");
99  // book in- and output ED products
100  edGetTokenStubs_ = consumes<StreamsStub>(InputTag(labelKF, branchStubs));
101  edGetTokenTracks_ = consumes<StreamsTrack>(InputTag(labelKF, branchTracks));
102  edGetTokenTTTrackRefMap_ = consumes<TTTrackRefMap>(InputTag(labelAS, branchTracks));
103  edPutTokenAccepted_ = produces<StreamsTrack>(branchTracks);
104  edPutTokenLost_ = produces<StreamsTrack>(branchLost);
105  // book ES products
106  esGetTokenSetup_ = esConsumes<Setup, SetupRcd, Transition::BeginRun>();
107  esGetTokenDataFormats_ = esConsumes<DataFormats, DataFormatsRcd, Transition::BeginRun>();
108  // initial ES products
109  setup_ = nullptr;
110  dataFormats_ = nullptr;
111 
112  trackQualityModel_ = std::make_unique<L1TrackQuality>(iConfig.getParameter<edm::ParameterSet>("TrackQualityPSet"));
113  edm::ParameterSet trackQualityPSset = iConfig.getParameter<edm::ParameterSet>("TrackQualityPSet");
114  tqBins_ = trackQualityPSset.getParameter<vector<int>>("tqemu_bins");
115  tqTanlScale_ = trackQualityPSset.getParameter<double>("tqemu_TanlScale");
116  tqZ0Scale_ = trackQualityPSset.getParameter<double>("tqemu_Z0Scale");
117  }
118 
119  void ProducerKFout::beginRun(const Run& iRun, const EventSetup& iSetup) {
120  // helper class to store configurations
121  setup_ = &iSetup.getData(esGetTokenSetup_);
123  return;
124  // check process history if desired
125  if (iConfig_.getParameter<bool>("CheckHistory"))
127  // helper class to extract structured data from tt::Frames
129 
130  // Calculate 1/dz**2 and 1/dphi**2 bins for v0 and v1 weightings
131 
132  float temp_dphi = 0.0;
133  float temp_dz = 0.0;
134  for (int i = 0;
135  i < pow(2, dataFormats_->width(Variable::dPhi, Process::kfin)) / pow(2, setup_->weightBinFraction());
136  i++) {
137  temp_dphi =
138  pow(dataFormats_->base(Variable::dPhi, Process::kfin) * (i + 1) * pow(2, setup_->weightBinFraction()), -2);
139  temp_dphi = temp_dphi / setup_->dphiTruncation();
140  temp_dphi = std::floor(temp_dphi);
141  dPhiBins_.push_back(temp_dphi * setup_->dphiTruncation());
142  }
143  for (int i = 0; i < pow(2, dataFormats_->width(Variable::dZ, Process::kfin)) / pow(2, setup_->weightBinFraction());
144  i++) {
145  temp_dz =
146  pow(dataFormats_->base(Variable::dZ, Process::kfin) * (i + 1) * pow(2, setup_->weightBinFraction()), -2);
147  temp_dz = temp_dz * setup_->dzTruncation();
148  temp_dz = std::ceil(temp_dz);
149  dZBins_.push_back(temp_dz / setup_->dzTruncation());
150  }
153  }
154 
156  // empty KFout product
159  // read in KF Product and produce KFout product
161  Handle<StreamsStub> handleStubs;
162  iEvent.getByToken<StreamsStub>(edGetTokenStubs_, handleStubs);
163  const StreamsStub& streamsStubs = *handleStubs.product();
164  Handle<StreamsTrack> handleTracks;
165  iEvent.getByToken<StreamsTrack>(edGetTokenTracks_, handleTracks);
166  const StreamsTrack& streamsTracks = *handleTracks.product();
167  Handle<TTTrackRefMap> handleTTTrackRefMap;
168  iEvent.getByToken<TTTrackRefMap>(edGetTokenTTTrackRefMap_, handleTTTrackRefMap);
169  const TTTrackRefMap& ttTrackRefMap = *handleTTTrackRefMap.product();
170  // 18 Output Links (First Vector) each has a vector of tracks per event (second vector) each track is 3 32 bit TTBV partial tracks
171  vector<vector<TTBV>> sortedPartialTracks(setup_->numRegions() * setup_->tfpNumChannel(), vector<TTBV>(0));
172 
173  TrackKFOutSAPtrCollectionss inTrackStreams;
174  TrackKFOutSAPtrCollectionss outTrackStreams;
175 
176  // Setup empty collections for input tracks to be routed
177  for (int iRegion = 0; iRegion < setup_->numRegions(); iRegion++) {
178  TrackKFOutSAPtrCollections temp_collection;
179  for (int iLink = 0; iLink < setup_->tfpNumChannel(); iLink++) {
181  for (int iTrack = 0; iTrack < setup_->numFramesIO(); iTrack++)
182  temp.emplace_back(std::make_shared<TrackKFOut>());
183  temp_collection.push_back(temp);
184  }
185  outTrackStreams.push_back(temp_collection);
186  }
187 
188  // Setup empty collections for output tracks from routing
189  for (int iRegion = 0; iRegion < setup_->numRegions(); iRegion++) {
190  TrackKFOutSAPtrCollections temp_collection;
191  for (int iLink = 0; iLink < numWorkers_; iLink++) {
193  for (int iTrack = 0; iTrack < setup_->numFramesIO(); iTrack++)
194  temp.emplace_back(std::make_shared<TrackKFOut>());
195  temp_collection.push_back(temp);
196  }
197  inTrackStreams.push_back(temp_collection);
198  }
199 
200  StreamsTrack outputStreamsTracks(setup_->numRegions() * setup_->tfpNumChannel());
201 
202  // Setup containers for track quality
203  float tempTQMVA = 0.0;
204  // Due to ap_fixed implementation in CMSSW this 10,5 must be specified at compile time, TODO make this a changeable parameter
205  std::vector<ap_fixed<10, 5>> trackQuality_inputs = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
206 
207  for (int iLink = 0; iLink < (int)streamsTracks.size(); iLink++) {
208  for (int iTrack = 0; iTrack < (int)streamsTracks[iLink].size(); iTrack++) {
209  const auto& track = streamsTracks[iLink].at(iTrack);
210  TrackKF inTrack(track, dataFormats_);
211 
212  double temp_z0 = inTrack.zT() - ((inTrack.cot() * setup_->chosenRofZ()));
213 
214  // Correction to Phi calcuation depending if +ve/-ve phi sector
215  const double baseSectorCorr = inTrack.sectorPhi() ? -setup_->baseSector() : setup_->baseSector();
216 
217  double temp_phi0 = inTrack.phiT() - ((inTrack.inv2R()) * setup_->hybridChosenRofPhi()) + baseSectorCorr;
218 
219  double temp_tanL = inTrack.cotGlobal();
220 
222 
223  double tempchi2rphi = 0;
224  double tempchi2rz = 0;
225 
226  int temp_nstub = 0;
227  int temp_ninterior = 0;
228  bool counter = false;
229 
230  for (int iStub = 0; iStub < setup_->numLayers() - 1; iStub++) {
231  const auto& stub = streamsStubs[setup_->numLayers() * iLink + iStub].at(iTrack);
232  StubKF inStub(stub, dataFormats_, iStub);
233 
234  if (!stub.first.isNonnull()) {
235  if (counter)
236  temp_ninterior += 1;
237  continue;
238  }
239 
240  counter = true;
241 
242  hitPattern.set(iStub);
243  temp_nstub += 1;
244  double phiSquared = pow(inStub.phi(), 2);
245  double zSquared = pow(inStub.z(), 2);
246 
247  double tempv0 = dPhiBins_[(inStub.dPhi() / (dataFormats_->base(Variable::dPhi, Process::kfin) *
248  pow(2, setup_->weightBinFraction())))];
249  double tempv1 = dZBins_[(
250  inStub.dZ() / (dataFormats_->base(Variable::dZ, Process::kfin) * pow(2, setup_->weightBinFraction())))];
251 
252  double tempRphi = phiSquared * tempv0;
253  double tempRz = zSquared * tempv1;
254 
255  tempchi2rphi += tempRphi;
256  tempchi2rz += tempRz;
257  } // Iterate over track stubs
258 
259  // Create bit vectors for eacch output, including digitisation of chi2
260  // TODO implement extraMVA, bendChi2, d0
262  TTBV extraMVA(0, TTTrack_TrackWord::TrackBitWidths::kMVAOtherSize, false);
263  TTBV bendChi2(0, TTTrack_TrackWord::TrackBitWidths::kBendChi2Size, false);
264  TTBV chi2rphi(digitise(TTTrack_TrackWord::chi2RPhiBins, tempchi2rphi, (double)setup_->kfoutchi2rphiConv()),
265  TTTrack_TrackWord::TrackBitWidths::kChi2RPhiSize,
266  false);
267  TTBV chi2rz(digitise(TTTrack_TrackWord::chi2RZBins, tempchi2rz, (double)setup_->kfoutchi2rzConv()),
268  TTTrack_TrackWord::TrackBitWidths::kChi2RZSize,
269  false);
270  TTBV d0(0, TTTrack_TrackWord::TrackBitWidths::kD0Size, false);
271  TTBV z0(
272  temp_z0, dataFormats_->base(Variable::zT, Process::kf), TTTrack_TrackWord::TrackBitWidths::kZ0Size, true);
273  TTBV tanL(temp_tanL,
274  dataFormats_->base(Variable::cot, Process::kf),
275  TTTrack_TrackWord::TrackBitWidths::kTanlSize,
276  true);
277  TTBV phi0(temp_phi0,
278  dataFormats_->base(Variable::phiT, Process::kf),
279  TTTrack_TrackWord::TrackBitWidths::kPhiSize,
280  true);
281  TTBV invR(-inTrack.inv2R(),
282  dataFormats_->base(Variable::inv2R, Process::kf),
283  TTTrack_TrackWord::TrackBitWidths::kRinvSize + 1,
284  true);
285  invR.resize(TTTrack_TrackWord::TrackBitWidths::kRinvSize);
286 
287  // Create input vector for BDT
288  trackQuality_inputs = {
289  (std::trunc(tanL.val() / tqTanlScale_)) / ap_fixed_rescale,
290  (std::trunc(z0.val() / tqZ0Scale_)) / ap_fixed_rescale,
291  0,
292  temp_nstub,
293  temp_ninterior,
296 
297  // Run BDT emulation and package output into 3 bits
298 
299  tempTQMVA = trackQualityModel_->runEmulatedTQ(trackQuality_inputs);
300  tempTQMVA = std::trunc(tempTQMVA * ap_fixed_rescale);
301  TTBV tqMVA(digitise(tqBins_, tempTQMVA, 1.0), TTTrack_TrackWord::TrackBitWidths::kMVAQualitySize, false);
302 
303  // Build 32 bit partial tracks for outputting in 64 bit packets
304  // 12 + 3 + 7 + 3 + 6
305  TTBV partialTrack3((d0 + bendChi2 + hitPattern + tqMVA + extraMVA), partialTrackWordBits_, false);
306  // 16 + 12 + 4
307  TTBV partialTrack2((tanL + z0 + chi2rz), partialTrackWordBits_, false);
308  // 1 + 15 + 12 + 4
309  TTBV partialTrack1((trackValid + invR + phi0 + chi2rphi), partialTrackWordBits_, false);
310 
311  int sortKey = (inTrack.sectorEta() < (int)(setup_->numSectorsEta() / 2)) ? 0 : 1;
312  // Set correct bit to valid for track valid
313  TrackKFOut temp_track(partialTrack1.set((partialTrackWordBits_ - 1)),
314  partialTrack2,
315  partialTrack3,
316  sortKey,
317  track,
318  iTrack,
319  iLink,
320  true);
321 
322  inTrackStreams[iLink / setup_->kfNumWorker()][iLink % setup_->kfNumWorker()][iTrack] =
323  (std::make_shared<TrackKFOut>(temp_track));
324  } // Iterate over Tracks
325  } // Iterate over Links
326  // Route Tracks in eta based on their sort key
327  for (int iRegion = 0; iRegion < setup_->numRegions(); iRegion++) {
328  int buffered_tracks[] = {0, 0};
329  for (int iTrack = 0;
330  iTrack < setup_->numFramesIO() * ((double)TTBV::S_ / TTTrack_TrackWord::TrackBitWidths::kTrackWordSize);
331  iTrack++) {
332  for (int iWorker = 0; iWorker < setup_->kfNumWorker(); iWorker++) {
333  for (int iLink = 0; iLink < setup_->tfpNumChannel(); iLink++) {
334  if ((inTrackStreams[iRegion][iWorker][iTrack]->sortKey() == iLink) &&
335  (inTrackStreams[iRegion][iWorker][iTrack]->dataValid() == true)) {
336  outTrackStreams[iRegion][iLink][buffered_tracks[iLink]] = inTrackStreams[iRegion][iWorker][iTrack];
337  buffered_tracks[iLink] = buffered_tracks[iLink] + 1;
338  }
339  }
340  }
341  }
342  }
343 
344  // Pack output of router onto each link, with correct partial tracks in correct places
345  for (int iRegion = 0; iRegion < setup_->numRegions(); iRegion++) {
346  for (int iLink = 0; iLink < setup_->tfpNumChannel(); iLink++) {
347  for (int iTrack = 0; iTrack < (int)outTrackStreams[iRegion][iLink].size(); iTrack++) {
348  sortedPartialTracks[2 * iRegion + iLink].push_back(
349  outTrackStreams[iRegion][iLink][iTrack]->PartialTrack1());
350  sortedPartialTracks[2 * iRegion + iLink].push_back(
351  outTrackStreams[iRegion][iLink][iTrack]->PartialTrack2());
352  sortedPartialTracks[2 * iRegion + iLink].push_back(
353  outTrackStreams[iRegion][iLink][iTrack]->PartialTrack3());
354  outputStreamsTracks[2 * iRegion + iLink].emplace_back(outTrackStreams[iRegion][iLink][iTrack]->track());
355  }
356  }
357  }
358  // Fill products and match up tracks
359  // store products
360  const TTBV nullBitTrack(0, partialTrackWordBits_, false);
361  for (int iLink = 0; iLink < (int)outputStreamsTracks.size(); iLink++) {
362  // Iterate through partial tracks
363  int numLinkTracks = (int)outputStreamsTracks[iLink].size();
364  if (numLinkTracks == 0)
365  continue; // Don't fill links if no tracks
366  if ((numLinkTracks % 2 != 0)) {
367  sortedPartialTracks[iLink].push_back(nullBitTrack); //Pad out final set of bits
368  outputStreamsTracks[iLink].emplace_back(
369  outputStreamsTracks[iLink][numLinkTracks++]); //Pad out with final repeated track
370  } //If there is an odd number of tracks
371  for (int iTrack = 0; iTrack < (int)(sortedPartialTracks[iLink].size()); iTrack++) {
372  if (iTrack % 2 != 1) // Write to links every other partial track, 3 partial tracks per full TTTrack
373  continue;
374  TTTrackRef trackRef;
375  for (auto& it : ttTrackRefMap) { //Iterate through ttTrackRefMap to find TTTrackRef Key by a TTTrack Value
376  if (it.second == outputStreamsTracks[iLink][(int)(iTrack - 1) / 3].first)
377  trackRef = it.first;
378  }
379  if ((int)iTrack / 3 <=
380  setup_->numFramesIO() * ((double)TTBV::S_ / TTTrack_TrackWord::TrackBitWidths::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<< 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
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)