test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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);
105  descriptions.add("DuplicateListMerger", desc);
106 }
107 
108 DuplicateListMerger::DuplicateListMerger(const edm::ParameterSet& iPara) :
109  collectionCloner(*this, iPara, true),
110  mergedTrackSource_(iPara.getParameter<edm::InputTag>("mergedSource"),consumesCollector()),
111  originalTrackSource_(iPara.getParameter<edm::InputTag>("originalSource"),consumesCollector())
112 {
113 
114  diffHitsCut_ = iPara.getParameter<int>("diffHitsCut");
115  candidateSource_ = consumes<std::vector<TrackCandidate> >(iPara.getParameter<edm::InputTag>("candidateSource"));
116  candidateComponents_ = consumes<CandidateToDuplicate>(iPara.getParameter<edm::InputTag>("candidateComponents"));
117 
118  mergedMVAValsToken_ = consumes<MVACollection>(iPara.getParameter<edm::InputTag>("mergedMVAVals"));
119  originalMVAValsToken_ = consumes<MVACollection>(iPara.getParameter<edm::InputTag>("originalMVAVals"));
120 
121 
122 
123  produces<MVACollection>("MVAValues");
124  produces<QualityMaskCollection>("QualityMasks");
125 
126 
127 }
128 
129 DuplicateListMerger::~DuplicateListMerger()
130 {
131 
132  /* no op */
133 
134 }
135 
136 void DuplicateListMerger::produce(edm::StreamID, edm::Event& iEvent, const edm::EventSetup& iSetup) const
137 {
138 
139  TrackCollectionCloner::Producer producer(iEvent, collectionCloner);
140 
141 
142 
143  auto const & originals = originalTrackSource_.tracks(iEvent);
144  auto const & merged = mergedTrackSource_.tracks(iEvent);
145  auto const & candIndices = mergedTrackSource_.indicesInput(iEvent);
146 
148  iEvent.getByToken(candidateSource_,candidateH);
149  auto const & candidates = *candidateH;
150 
151  edm::Handle<CandidateToDuplicate> candidateComponentsH;
152  iEvent.getByToken(candidateComponents_,candidateComponentsH);
153  auto const & candidateComponents = *candidateComponentsH;
154 
155 
156 
157  edm::Handle<MVACollection> originalMVAStore;
158  edm::Handle<MVACollection> mergedMVAStore;
159 
160  iEvent.getByToken(originalMVAValsToken_,originalMVAStore);
161  iEvent.getByToken(mergedMVAValsToken_,mergedMVAStore);
162 
163  MVACollection mvaVec;
164 
165 
166  auto mergedMVA = *mergedMVAStore;
167 
168  //match new tracks to their candidates
169  std::vector<std::array<int,3>> matches;
170  for(int i = 0; i < (int)merged.size(); ++i) {
171  auto cInd = candIndices[i];
172  auto const & cand = candidates[cInd];
173  const reco::Track& matchedTrack = merged[i];
174 
175  if (mergedMVA[i]< -0.7f) continue; // at least "loose" ( FIXME: take cut value from CutSelector)
176 
177  // if( ChiSquaredProbability(matchedTrack.chi2(),matchedTrack.ndof()) < minTrkProbCut_)continue;
178  int dHits = (cand.recHits().second - cand.recHits().first) - matchedTrack.recHitsSize();
179  if(dHits > diffHitsCut_)continue;
180  matches.push_back(std::array<int,3>{{i,candidateComponents[cInd].first,candidateComponents[cInd].second}});
181  }
182 
183  //check for candidates/tracks that share merged tracks, select minimum chi2, remove the rest
184  if (matches.size()>1)
185  for ( auto matchIter0 = matches.begin(); matchIter0 != matches.end()-1; ++matchIter0) {
186  if ( (*matchIter0)[0]<0) continue;
187  auto nchi2 = merged[(*matchIter0)[0]].normalizedChi2();
188  for( auto matchIter1 = matchIter0+1; matchIter1 != matches.end(); ++matchIter1){
189  if ( (*matchIter1)[0]<0) continue;
190  if ( (*matchIter0)[1]==(*matchIter1)[1]
191  || (*matchIter0)[1]==(*matchIter1)[2]
192  || (*matchIter0)[2]==(*matchIter1)[1]
193  || (*matchIter0)[2]==(*matchIter1)[2]
194  ) {
195  auto nchi2_1 = merged[(*matchIter1)[0]].normalizedChi2();
196  if(nchi2_1 < nchi2){
197  (*matchIter0)[0] = -1;
198  break;
199  }else{
200  (*matchIter1)[0] = -1;
201  }
202  }
203  }
204  }
205 
206 
207  // products
208  auto pmvas = std::make_unique<MVACollection>();
209  auto pquals = std::make_unique<QualityMaskCollection>();
210 
211 
212  //add the good merged tracks to the output list, remove input tracks
213  std::vector<int> inputTracks;
214 
215 
216  std::vector<unsigned int> selId;
217  auto ntotTk = matches.size();
218  // declareDynArray(reco::TrackBase::TrackAlgorithm, ntotTk, algo);
220  declareDynArray(reco::TrackBase::AlgoMask, ntotTk, algoMask);
221 
222  auto nsel=0U;
223  for(auto matchIter0 = matches.begin(); matchIter0 != matches.end(); matchIter0++){
224  if ( (*matchIter0)[0]<0) continue;
225  selId.push_back((*matchIter0)[0]);
226 
227  pmvas->push_back(mergedMVA[(*matchIter0)[0]]);
228 
229  const reco::Track& inTrk1 = originals[(*matchIter0)[1]];
230  const reco::Track& inTrk2 = originals[(*matchIter0)[2]];
231  oriAlgo[nsel] = std::min(inTrk1.algo(),inTrk2.algo(),
234  });
235 
236 
237  algoMask[nsel] = inTrk1.algoMask() | inTrk2.algoMask();
238 
239  pquals->push_back((inTrk1.qualityMask() | inTrk2.qualityMask()));
240  pquals->back() |= (1<<reco::TrackBase::confirmed);
241 
242  inputTracks.push_back((*matchIter0)[1]);
243  inputTracks.push_back((*matchIter0)[2]);
244 
245 
246  ++nsel;
247 
248  }
249 
250 
251  producer(mergedTrackSource_,selId);
252  assert(producer.selTracks_->size()==pquals->size());
253 
254  for (auto isel=0U;isel<nsel;++isel) {
255  algoMask[isel].set(reco::TrackBase::duplicateMerge);
256  auto & otk = (*producer.selTracks_)[isel];
257  otk.setQualityMask((*pquals)[isel]);
258  otk.setAlgorithm(reco::TrackBase::duplicateMerge);
259  otk.setOriginalAlgorithm(oriAlgo[isel]);
260  otk.setAlgoMask(algoMask[isel]);
261  }
262 
263 
264  selId.clear();
265  for(int i = 0; i < (int)originals.size(); i++){
266  const reco::Track& origTrack = originals[i];
267  if (std::find(inputTracks.begin(),inputTracks.end(),i) != inputTracks.end()) continue;
268  selId.push_back(i);
269  pmvas->push_back((*originalMVAStore)[i]);
270  pquals->push_back(origTrack.qualityMask());
271  }
272 
273  producer(originalTrackSource_,selId);
274  assert(producer.selTracks_->size()==pquals->size());
275 
276  iEvent.put(std::move(pmvas),"MVAValues");
277  iEvent.put(std::move(pquals),"QualityMasks");
278 
279 
280 }
281 
282 }
283 
286 
T getParameter(std::string const &) const
int i
Definition: DBlmapReader.cc:9
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:462
#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
assert(m_qm.get())
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
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:121
def move
Definition: eostools.py:510
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)
double a
Definition: hdecay.h:121
#define declareDynArray(T, n, x)
Definition: DynArray.h:58