CMS 3D CMS Logo

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