CMS 3D CMS Logo

DuplicateListMerger.cc
Go to the documentation of this file.
1 
9 
14 
18 
21 
23 
26 
27 #include <vector>
28 #include <algorithm>
29 #include <string>
30 #include <iostream>
31 #include <memory>
32 
33 // #include "TMVA/Reader.h"
34 
35 using namespace reco;
36 
37 namespace {
38  class DuplicateListMerger final : public edm::global::EDProducer<> {
39  public:
41  explicit DuplicateListMerger(const edm::ParameterSet& iPara);
43  ~DuplicateListMerger() override;
44 
46  using CandidateToDuplicate = std::vector<std::pair<int, int>>;
47 
48  using RecHitContainer = edm::OwnVector<TrackingRecHit>;
49 
50  using MVACollection = std::vector<float>;
51  using QualityMaskCollection = std::vector<unsigned char>;
52 
53  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
54 
55  private:
57  void produce(edm::StreamID, edm::Event&, const edm::EventSetup&) const override;
58 
59  private:
60  TrackCollectionCloner collectionCloner;
61  TrackCollectionCloner::Tokens mergedTrackSource_;
62  TrackCollectionCloner::Tokens originalTrackSource_;
63 
64  edm::EDGetTokenT<CandidateToDuplicate> candidateComponents_;
66 
67  edm::EDGetTokenT<MVACollection> originalMVAValsToken_;
68  edm::EDGetTokenT<MVACollection> mergedMVAValsToken_;
69 
70  std::string priorityName_;
71 
72  int diffHitsCut_;
73  };
74 } // namespace
75 
80 
83 
84 #include <tuple>
85 #include <array>
87 
88 namespace {
91  desc.add<edm::InputTag>("mergedSource", edm::InputTag());
92  desc.add<edm::InputTag>("originalSource", edm::InputTag());
93  desc.add<edm::InputTag>("mergedMVAVals", edm::InputTag());
94  desc.add<edm::InputTag>("originalMVAVals", edm::InputTag());
95  desc.add<edm::InputTag>("candidateSource", edm::InputTag());
96  desc.add<edm::InputTag>("candidateComponents", edm::InputTag());
97  desc.add<std::string>("trackAlgoPriorityOrder", "trackAlgoPriorityOrder");
98  desc.add<int>("diffHitsCut", 5);
100  descriptions.add("DuplicateListMerger", desc);
101  }
102 
103  DuplicateListMerger::DuplicateListMerger(const edm::ParameterSet& iPara)
104  : collectionCloner(producesCollector(), iPara, true),
105  mergedTrackSource_(iPara.getParameter<edm::InputTag>("mergedSource"), consumesCollector()),
106  originalTrackSource_(iPara.getParameter<edm::InputTag>("originalSource"), consumesCollector()),
107  priorityName_(iPara.getParameter<std::string>("trackAlgoPriorityOrder")) {
108  diffHitsCut_ = iPara.getParameter<int>("diffHitsCut");
109  candidateSource_ = consumes<std::vector<TrackCandidate>>(iPara.getParameter<edm::InputTag>("candidateSource"));
110  candidateComponents_ = consumes<CandidateToDuplicate>(iPara.getParameter<edm::InputTag>("candidateComponents"));
111 
112  mergedMVAValsToken_ = consumes<MVACollection>(iPara.getParameter<edm::InputTag>("mergedMVAVals"));
113  originalMVAValsToken_ = consumes<MVACollection>(iPara.getParameter<edm::InputTag>("originalMVAVals"));
114 
115  produces<MVACollection>("MVAValues");
116  produces<QualityMaskCollection>("QualityMasks");
117  }
118 
119  DuplicateListMerger::~DuplicateListMerger() { /* no op */
120  }
121 
122  void DuplicateListMerger::produce(edm::StreamID, edm::Event& iEvent, const edm::EventSetup& iSetup) const {
123  TrackCollectionCloner::Producer producer(iEvent, collectionCloner);
124 
125  auto const& originals = originalTrackSource_.tracks(iEvent);
126  auto const& merged = mergedTrackSource_.tracks(iEvent);
127  auto const& candIndices = mergedTrackSource_.indicesInput(iEvent);
128 
130  iEvent.getByToken(candidateSource_, candidateH);
131  auto const& candidates = *candidateH;
132 
133  edm::Handle<CandidateToDuplicate> candidateComponentsH;
134  iEvent.getByToken(candidateComponents_, candidateComponentsH);
135  auto const& candidateComponents = *candidateComponentsH;
136 
137  edm::Handle<MVACollection> originalMVAStore;
138  edm::Handle<MVACollection> mergedMVAStore;
139 
140  iEvent.getByToken(originalMVAValsToken_, originalMVAStore);
141  iEvent.getByToken(mergedMVAValsToken_, mergedMVAStore);
142 
144  iSetup.get<CkfComponentsRecord>().get(priorityName_, priorityH);
145  auto const& trackAlgoPriorityOrder = *priorityH;
146 
147  MVACollection mvaVec;
148 
149  auto mergedMVA = *mergedMVAStore;
150 
151  //match new tracks to their candidates
152  std::vector<std::array<int, 3>> matches;
153  for (int i = 0; i < (int)merged.size(); ++i) {
154  auto cInd = candIndices[i];
155  auto const& cand = candidates[cInd];
156  const reco::Track& matchedTrack = merged[i];
157 
158  if (mergedMVA[i] < -0.7f)
159  continue; // at least "loose" ( FIXME: take cut value from CutSelector)
160 
161  // if( ChiSquaredProbability(matchedTrack.chi2(),matchedTrack.ndof()) < minTrkProbCut_)continue;
162  int dHits = (cand.recHits().second - cand.recHits().first) - matchedTrack.recHitsSize();
163  if (dHits > diffHitsCut_)
164  continue;
165  matches.push_back(std::array<int, 3>{{i, candidateComponents[cInd].first, candidateComponents[cInd].second}});
166  }
167 
168  //check for candidates/tracks that share merged tracks, select minimum chi2, remove the rest
169  if (matches.size() > 1)
170  for (auto matchIter0 = matches.begin(); matchIter0 != matches.end() - 1; ++matchIter0) {
171  if ((*matchIter0)[0] < 0)
172  continue;
173  auto nchi2 = merged[(*matchIter0)[0]].normalizedChi2();
174  for (auto matchIter1 = matchIter0 + 1; matchIter1 != matches.end(); ++matchIter1) {
175  if ((*matchIter1)[0] < 0)
176  continue;
177  if ((*matchIter0)[1] == (*matchIter1)[1] || (*matchIter0)[1] == (*matchIter1)[2] ||
178  (*matchIter0)[2] == (*matchIter1)[1] || (*matchIter0)[2] == (*matchIter1)[2]) {
179  auto nchi2_1 = merged[(*matchIter1)[0]].normalizedChi2();
180  if (nchi2_1 < nchi2) {
181  (*matchIter0)[0] = -1;
182  break;
183  } else {
184  (*matchIter1)[0] = -1;
185  }
186  }
187  }
188  }
189 
190  // products
191  auto pmvas = std::make_unique<MVACollection>();
192  auto pquals = std::make_unique<QualityMaskCollection>();
193 
194  //add the good merged tracks to the output list, remove input tracks
195  std::vector<int> inputTracks;
196 
197  std::vector<unsigned int> selId;
198  auto ntotTk = matches.size();
199  // declareDynArray(reco::TrackBase::TrackAlgorithm, ntotTk, algo);
201  declareDynArray(reco::TrackBase::AlgoMask, ntotTk, algoMask);
202 
203  auto nsel = 0U;
204  for (auto matchIter0 = matches.begin(); matchIter0 != matches.end(); matchIter0++) {
205  if ((*matchIter0)[0] < 0)
206  continue;
207  selId.push_back((*matchIter0)[0]);
208 
209  pmvas->push_back(mergedMVA[(*matchIter0)[0]]);
210 
211  const reco::Track& inTrk1 = originals[(*matchIter0)[1]];
212  const reco::Track& inTrk2 = originals[(*matchIter0)[2]];
213  oriAlgo[nsel] = std::min(
215  return trackAlgoPriorityOrder.priority(a) < trackAlgoPriorityOrder.priority(b);
216  });
217 
218  algoMask[nsel] = inTrk1.algoMask() | inTrk2.algoMask();
219 
220  pquals->push_back((inTrk1.qualityMask() | inTrk2.qualityMask()));
221  pquals->back() |= (1 << reco::TrackBase::confirmed);
222 
223  inputTracks.push_back((*matchIter0)[1]);
224  inputTracks.push_back((*matchIter0)[2]);
225 
226  ++nsel;
227  }
228 
229  producer(mergedTrackSource_, selId);
230  assert(producer.selTracks_->size() == pquals->size());
231 
232  for (auto isel = 0U; isel < nsel; ++isel) {
233  algoMask[isel].set(reco::TrackBase::duplicateMerge);
234  auto& otk = (*producer.selTracks_)[isel];
235  otk.setQualityMask((*pquals)[isel]);
236  otk.setAlgorithm(reco::TrackBase::duplicateMerge);
237  otk.setOriginalAlgorithm(oriAlgo[isel]);
238  otk.setAlgoMask(algoMask[isel]);
239  }
240 
241  selId.clear();
242  for (int i = 0; i < (int)originals.size(); i++) {
243  const reco::Track& origTrack = originals[i];
244  if (std::find(inputTracks.begin(), inputTracks.end(), i) != inputTracks.end())
245  continue;
246  selId.push_back(i);
247  pmvas->push_back((*originalMVAStore)[i]);
248  pquals->push_back(origTrack.qualityMask());
249  }
250 
251  producer(originalTrackSource_, selId);
252  assert(producer.selTracks_->size() == pquals->size());
253 
254  iEvent.put(std::move(pmvas), "MVAValues");
255  iEvent.put(std::move(pquals), "QualityMasks");
256  }
257 
258 } // namespace
259 
262 
#define declareDynArray(T, n, x)
Definition: DynArray.h:91
T getParameter(std::string const &) const
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:131
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:525
size_t recHitsSize() const
Get number of RecHits. (Warning, this includes invalid hits, which are not physical hits)...
Definition: Track.h:97
static void fill(edm::ParameterSetDescription &desc)
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:19
TrackAlgorithm
track algorithm
Definition: TrackBase.h:89
TrackAlgorithm algo() const
Definition: TrackBase.h:526
int iEvent
Definition: GenABIO.cc:224
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
int qualityMask() const
Definition: TrackBase.h:773
double f[11][100]
T min(T a, T b)
Definition: MathUtil.h:58
ParameterDescriptionBase * add(U const &iLabel, T const &value)
std::bitset< algoSize > AlgoMask
algo mask
Definition: TrackBase.h:145
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
AlgoMask algoMask() const
Definition: TrackBase.h:394
double b
Definition: hdecay.h:118
void add(std::string const &label, ParameterSetDescription const &psetDescription)
fixed size matrix
HLT enums.
double a
Definition: hdecay.h:119
T get() const
Definition: EventSetup.h:73
std::array< unsigned int, reco::TrackBase::algoSize > trackAlgoPriorityOrder
def move(src, dest)
Definition: eostools.py:511