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 
32  class ProducerKFout : public stream::EDProducer<> {
33  public:
34  explicit ProducerKFout(const ParameterSet&);
35  ~ProducerKFout() override {}
36  template <typename T>
37  int digitise(const vector<T> Bins, T Value, T factor = 1);
38 
39  private:
40  void beginRun(const Run&, const EventSetup&) override;
41  void produce(Event&, const EventSetup&) override;
42  void endJob() {}
43 
44  // ED input token of kf stubs
46  // ED input token of kf tracks
48  // ED input token of kf input to kf output TTTrack map
50  // ED output token for accepted kfout tracks
52  // ED output token for truncated kfout tracks
54  // Setup token
56  // DataFormats token
58  // configuration
60  // helper class to store configurations
61  const Setup* setup_ = nullptr;
62  // helper class to extract structured data from tt::Frames
63  const DataFormats* dataFormats_ = nullptr;
64  // Bins for dPhi/dZ use to create weight LUT
65  vector<double> dPhiBins_;
66  vector<double> dZBins_;
67 
68  // Constant used throughout for partial ttrack words
70 
71  // For convenience and keeping readable code, accessed many times
73  };
74 
75  ProducerKFout::ProducerKFout(const ParameterSet& iConfig) : iConfig_(iConfig) {
76  const string& labelKF = iConfig.getParameter<string>("LabelKF");
77  const string& labelAS = iConfig.getParameter<string>("LabelAS");
78  const string& branchStubs = iConfig.getParameter<string>("BranchAcceptedStubs");
79  const string& branchTracks = iConfig.getParameter<string>("BranchAcceptedTracks");
80  const string& branchLost = iConfig.getParameter<string>("BranchLostTracks");
81  // book in- and output ED products
82  edGetTokenStubs_ = consumes<StreamsStub>(InputTag(labelKF, branchStubs));
83  edGetTokenTracks_ = consumes<StreamsTrack>(InputTag(labelKF, branchTracks));
84  edGetTokenTTTrackRefMap_ = consumes<TTTrackRefMap>(InputTag(labelAS, branchTracks));
85  edPutTokenAccepted_ = produces<StreamsTrack>(branchTracks);
86  edPutTokenLost_ = produces<StreamsTrack>(branchLost);
87  // book ES products
88  esGetTokenSetup_ = esConsumes<Setup, SetupRcd, Transition::BeginRun>();
89  esGetTokenDataFormats_ = esConsumes<DataFormats, DataFormatsRcd, Transition::BeginRun>();
90  }
91 
92  void ProducerKFout::beginRun(const Run& iRun, const EventSetup& iSetup) {
93  // helper class to store configurations
94  setup_ = &iSetup.getData(esGetTokenSetup_);
96  return;
97  // check process history if desired
98  if (iConfig_.getParameter<bool>("CheckHistory"))
100  // helper class to extract structured data from tt::Frames
102 
103  // Calculate 1/dz**2 and 1/dphi**2 bins for v0 and v1 weightings
104  for (int i = 0;
105  i < pow(2, dataFormats_->width(Variable::dPhi, Process::kfin)) / pow(2, setup_->weightBinFraction());
106  i++)
107  dPhiBins_.push_back(
108  pow(dataFormats_->base(Variable::dPhi, Process::kfin) * (i + 1) * pow(2, setup_->weightBinFraction()), -2));
109 
110  for (int i = 0; i < pow(2, dataFormats_->width(Variable::dZ, Process::kfin)) / pow(2, setup_->weightBinFraction());
111  i++)
112  dZBins_.push_back(
113  pow(dataFormats_->base(Variable::dZ, Process::kfin) * (i + 1) * pow(2, setup_->weightBinFraction()), -2));
114 
117  }
118 
119  // Helper function to convert floating chi2 to chi2 bin
120  template <typename T>
121  int ProducerKFout::digitise(const vector<T> Bins, T Value, T factor) {
122  for (int i = 0; i < (int)Bins.size(); i++) {
123  if (Value * factor > Bins[i] && Value * factor <= Bins[i + 1]) {
124  return i;
125  }
126  }
127  return -1;
128  }
129 
131  // empty KFout product
134  // read in KF Product and produce KFout product
136  Handle<StreamsStub> handleStubs;
137  iEvent.getByToken<StreamsStub>(edGetTokenStubs_, handleStubs);
138  const StreamsStub& streamsStubs = *handleStubs.product();
139  Handle<StreamsTrack> handleTracks;
140  iEvent.getByToken<StreamsTrack>(edGetTokenTracks_, handleTracks);
141  const StreamsTrack& streamsTracks = *handleTracks.product();
142  Handle<TTTrackRefMap> handleTTTrackRefMap;
143  iEvent.getByToken<TTTrackRefMap>(edGetTokenTTTrackRefMap_, handleTTTrackRefMap);
144  const TTTrackRefMap& ttTrackRefMap = *handleTTTrackRefMap.product();
145  // 18 Output Links (First Vector) each has a vector of tracks per event (second vector) each track is 3 32 bit TTBV partial tracks
146  vector<vector<TTBV>> SortedPartialTracks(setup_->numRegions() * setup_->tfpNumChannel(), vector<TTBV>(0));
147 
148  TrackKFOutSAPtrCollectionss InTrackStreams;
149  TrackKFOutSAPtrCollectionss OutTrackStreams;
150 
151  // Setup empty collections for input tracks to distribution server
152  for (int iRegion = 0; iRegion < setup_->numRegions(); iRegion++) {
153  TrackKFOutSAPtrCollections temp_collection;
154  for (int iLink = 0; iLink < setup_->tfpNumChannel(); iLink++) {
156  for (int iTrack = 0; iTrack < setup_->numFramesIO(); iTrack++)
157  temp.emplace_back(std::make_shared<TrackKFOut>());
158  temp_collection.push_back(temp);
159  }
160  OutTrackStreams.push_back(temp_collection);
161  }
162 
163  // Setup empty collections for oiutpu tracks from distribution server
164  for (int iRegion = 0; iRegion < setup_->numRegions(); iRegion++) {
165  TrackKFOutSAPtrCollections temp_collection;
166  for (int iLink = 0; iLink < numWorkers_; iLink++) {
168  for (int iTrack = 0; iTrack < setup_->numFramesIO(); iTrack++)
169  temp.emplace_back(std::make_shared<TrackKFOut>());
170  temp_collection.push_back(temp);
171  }
172  InTrackStreams.push_back(temp_collection);
173  }
174 
175  StreamsTrack OutputStreamsTracks(setup_->numRegions() * setup_->tfpNumChannel());
176 
177  for (int iLink = 0; iLink < (int)streamsTracks.size(); iLink++) {
178  for (int iTrack = 0; iTrack < (int)streamsTracks[iLink].size(); iTrack++) {
179  const auto& track = streamsTracks[iLink].at(iTrack);
180  TrackKF InTrack(track, dataFormats_);
181 
182  double temp_z0 = InTrack.zT() - ((InTrack.cot() * setup_->chosenRofZ()));
183 
184  // Correction to Phi calcuation depending if +ve/-ve phi sector
185  const double baseSectorCorr = InTrack.sectorPhi() ? -setup_->baseSector() : setup_->baseSector();
186 
187  double temp_phi0 = InTrack.phiT() - ((InTrack.inv2R()) * setup_->hybridChosenRofPhi()) + baseSectorCorr;
188 
189  double temp_tanL = InTrack.cotGlobal();
190 
191  TTBV HitPattern(0, setup_->numLayers());
192 
193  double tempchi2rphi = 0;
194  double tempchi2rz = 0;
195 
196  for (int iStub = 0; iStub < setup_->numLayers() - 1; iStub++) {
197  const auto& stub = streamsStubs[setup_->numLayers() * iLink + iStub].at(iTrack);
198  StubKF InStub(stub, dataFormats_, iStub);
199 
200  if (!stub.first.isNonnull())
201  continue;
202 
203  HitPattern.set(iStub);
204  double phiSquared = pow(InStub.phi(), 2);
205  double zSquared = pow(InStub.z(), 2);
206 
207  double tempv0 = dPhiBins_[(InStub.dPhi() / (dataFormats_->base(Variable::dPhi, Process::kfin) *
208  pow(2, setup_->weightBinFraction())))];
209  double tempv1 = dZBins_[(
210  InStub.dZ() / (dataFormats_->base(Variable::dZ, Process::kfin) * pow(2, setup_->weightBinFraction())))];
211 
212  double tempRphi = phiSquared * tempv0;
213  double tempRz = zSquared * tempv1;
214 
215  tempchi2rphi += tempRphi;
216  tempchi2rz += tempRz;
217  } // Iterate over track stubs
218 
219  // TODO extract TTTrack bit widths from TTTrack word pending update to the TTTrack_word class
220  TTBV TrackValid(1, 1, false);
221  TTBV extraMVA(0, 6, false);
222  TTBV TQMVA(0, 3, false);
223  TTBV BendChi2(0, 3, false);
224  TTBV Chi2rphi(
225  digitise(setup_->kfoutchi2rphiBins(), tempchi2rphi, (double)setup_->kfoutchi2rphiConv()), 4, false);
226  TTBV Chi2rz(digitise(setup_->kfoutchi2rzBins(), tempchi2rz, (double)setup_->kfoutchi2rzConv()), 4, false);
227  TTBV D0(0, 13, false);
228  TTBV z0(temp_z0, dataFormats_->base(Variable::zT, Process::kf), 12, true);
229  TTBV TanL(temp_tanL, dataFormats_->base(Variable::cot, Process::kf), 16, true);
230  TTBV phi0(temp_phi0, dataFormats_->base(Variable::phiT, Process::kf), 12, true);
231  TTBV InvR(-InTrack.inv2R(), dataFormats_->base(Variable::inv2R, Process::kf), 16, true);
232  InvR.resize(15);
233  // 13 + 3 + 7 + 3 + 3
234  TTBV PartialTrack3((D0 + BendChi2 + HitPattern + TQMVA + extraMVA), partialTrackWordBits_, false);
235  // 16 + 12 + 4
236  TTBV PartialTrack2((TanL + z0 + Chi2rz), partialTrackWordBits_, false);
237  // 1 + 15 + 12 + 4
238  TTBV PartialTrack1((TrackValid + InvR + phi0 + Chi2rphi), partialTrackWordBits_, false);
239 
240  int sortKey = (InTrack.sectorEta() < (int)(setup_->numSectorsEta() / 2)) ? 0 : 1;
241  // Set correct bit to valid for track valid
242  TrackKFOut Temp_track(
243  PartialTrack1.set(31), PartialTrack2, PartialTrack3, sortKey, track, iTrack, iLink, true);
244 
245  InTrackStreams[iLink / numWorkers_][iLink % numWorkers_][iTrack] = (std::make_shared<TrackKFOut>(Temp_track));
246 
247  } // Iterate over Tracks
248 
249  //Pad out input streams to Dist server with extra null track if odd number of tracks on a stream
250  int iFinTrack = (int)streamsTracks[iLink].size();
251  if (iFinTrack % numWorkers_ != 0) {
252  TrackKFOut null_track(0, 0, 0, iLink % numWorkers_, tt::FrameTrack(), iFinTrack + 1, iLink, true);
253  InTrackStreams[iLink / numWorkers_][iLink % numWorkers_][iFinTrack] =
254  (std::make_shared<TrackKFOut>(null_track));
255  }
256  } // Iterate over Links
257  // Fill products and match up tracks
258 
259  // One distribution server for every region, num inputs = num KF workers, num outputs = num output links
260  vector<DistServer> distServers(
261  setup_->numRegions(),
262  DistServer(numWorkers_, setup_->tfpNumChannel(), 2)); //Magic number for interleaving in dist server
263 
264  for (int iRegion = 0; iRegion < setup_->numRegions(); iRegion++) {
265  for (int iTrack = 0; iTrack < setup_->numFramesIO() * ((double)TTBV::S_ / setup_->tttrackBits()); iTrack++) {
267  for (int iWorker = 0; iWorker < numWorkers_; iWorker++)
268  DistIn.push_back(InTrackStreams[iRegion][iWorker][iTrack]); // Reorganise input to distribution server
269  TrackKFOutSAPtrCollection DistOut = distServers[iRegion].clock(DistIn); // Run dist server
270  for (int iLink = 0; iLink < setup_->tfpNumChannel(); iLink++)
271  OutTrackStreams[iRegion][iLink][iTrack] =
272  DistOut[iLink]; // Reorganise output of distribution server in output streams
273  }
274  }
275 
276  // Pack output of distribution server onto each link, with correct partial tracks in correct places
277  for (int iRegion = 0; iRegion < setup_->numRegions(); iRegion++) {
278  for (int iLink = 0; iLink < setup_->tfpNumChannel(); iLink++) {
279  for (int iTrack = 0; iTrack < (int)OutTrackStreams[iRegion][iLink].size(); iTrack++) {
280  SortedPartialTracks[2 * iRegion + iLink].push_back(
281  OutTrackStreams[iRegion][iLink][iTrack]->PartialTrack1());
282  SortedPartialTracks[2 * iRegion + iLink].push_back(
283  OutTrackStreams[iRegion][iLink][iTrack]->PartialTrack2());
284  SortedPartialTracks[2 * iRegion + iLink].push_back(
285  OutTrackStreams[iRegion][iLink][iTrack]->PartialTrack3());
286  OutputStreamsTracks[2 * iRegion + iLink].emplace_back(OutTrackStreams[iRegion][iLink][iTrack]->track());
287  }
288  }
289  }
290 
291  const TTBV NullBitTrack(0, partialTrackWordBits_, false);
292  for (int iLink = 0; iLink < (int)OutputStreamsTracks.size(); iLink++) {
293  // Iterate through partial tracks
294  int numLinkTracks = (int)OutputStreamsTracks[iLink].size();
295  if (numLinkTracks == 0)
296  continue; // Don't fill links if no tracks
297  if ((numLinkTracks % 2 != 0)) {
298  SortedPartialTracks[iLink].push_back(NullBitTrack); //Pad out final set of bits
299  OutputStreamsTracks[iLink].emplace_back(
300  OutputStreamsTracks[iLink][numLinkTracks++]); //Pad out with final repeated track
301  } //If there is an odd number of tracks
302  for (int iTrack = 0; iTrack < (int)(SortedPartialTracks[iLink].size()); iTrack++) {
303  if (iTrack % 2 != 1) // Write to links every other partial track, 3 partial tracks per full TTTrack
304  continue;
306  for (auto& it : ttTrackRefMap) { //Iterate through ttTrackRefMap to find TTTrackRef Key by a TTTrack Value
307  if (it.second == OutputStreamsTracks[iLink][(int)(iTrack - 1) / 3].first)
308  TrackRef = it.first;
309  }
310  if ((int)iTrack / 3 <= setup_->numFramesIO() * ((double)TTBV::S_ / setup_->tttrackBits()))
311  accepted[iLink].emplace_back(
312  std::make_pair(TrackRef,
313  (SortedPartialTracks[iLink][iTrack - 1].slice(partialTrackWordBits_) +
314  SortedPartialTracks[iLink][iTrack].slice(partialTrackWordBits_))
315  .bs()));
316  else
317  lost[iLink].emplace_back(
318  std::make_pair(TrackRef,
319  (SortedPartialTracks[iLink][iTrack - 1].slice(partialTrackWordBits_) +
320  SortedPartialTracks[iLink][iTrack].slice(partialTrackWordBits_))
321  .bs()));
322  } //Iterate through sorted partial tracks
323  } // Iterate through links
324  } // Config Supported
325  // store products
326  iEvent.emplace(edPutTokenAccepted_, move(accepted));
327  iEvent.emplace(edPutTokenLost_, move(lost));
328  }
329 } // namespace trklet
330 
double cot() const
Definition: DataFormats.h:968
size
Write out results.
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
double base(Variable v, Process p) const
Definition: DataFormats.h:492
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:504
double chosenRofZ() const
Definition: Setup.h:413
Bit vector used by Track Trigger emulators. Mainly used to convert integers into arbitrary (within ma...
Definition: TTBV.h:20
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:404
Divides< arg, void > D0
Definition: Factorize.h:135
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
int tttrackBits() const
Definition: Setup.h:506
int width(Variable v, Process p) const
Definition: DataFormats.h:490
std::pair< TTTrackRef, Frame > FrameTrack
Definition: TTTypes.h:62
int sectorPhi() const
Definition: DataFormats.h:960
std::vector< double > kfoutchi2rzBins() const
Definition: Setup.h:500
int numSectorsEta() const
Definition: Setup.h:411
int iEvent
Definition: GenABIO.cc:224
Definition: TTTypes.h:54
double z() const
Definition: DataFormats.h:832
int kfoutchi2rphiConv() const
Definition: Setup.h:502
double inv2R() const
Definition: DataFormats.h:966
int kfNumWorker() const
Definition: Setup.h:487
EDGetTokenT< StreamsStub > edGetTokenStubs_
double baseSector() const
Definition: Setup.h:420
int sectorEta() const
Definition: DataFormats.h:962
EDPutTokenT< StreamsTrack > edPutTokenLost_
reco::JetExtendedAssociation::JetExtendedData Value
bool configurationSupported() const
Definition: Setup.h:60
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
ProcessHistory const & processHistory() const
Definition: Run.cc:115
void beginRun(const Run &, const EventSetup &) override
TTBV & set()
Definition: TTBV.h:187
int weightBinFraction() const
Definition: Setup.h:508
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:238
void produce(Event &, const EventSetup &) override
edm::Ref< TrackCollection > TrackRef
persistent reference to a Track
Definition: TrackFwd.h:20
int numRegions() const
Definition: Setup.h:345
HLT enums.
vector< double > dZBins_
std::vector< double > kfoutchi2rphiBins() const
Definition: Setup.h:498
EDGetTokenT< TTTrackRefMap > edGetTokenTTTrackRefMap_
int digitise(const vector< T > Bins, T Value, T factor=1)
Class to calculate and provide dataformats used by Track Trigger emulator.
Definition: DataFormats.h:216
Converts KF output into TFP output.
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