CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ConversionTrackMerger.cc
Go to the documentation of this file.
1 //
2 // Package: RecoTracker/FinalTrackSelectors
3 // Class: ConversionTrackMerger
4 //
5 // Description: Merger for ConversionTracks, adapted from SimpleTrackListMerger
6 //
7 // Original Author: J.Bendavid
8 //
9 // $Author: bendavid $
10 // $Date: 2010/11/22 02:02:08 $
11 // $Revision: 1.2 $
12 //
13 
14 #include <memory>
15 #include <string>
16 #include <iostream>
17 #include <cmath>
18 #include <vector>
19 
21 
27 
29 
31 
36 
37 //#include "DataFormats/TrackReco/src/classes.h"
38 
41 
42 
44  conf_(conf)
45  {
46  produces<reco::ConversionTrackCollection>();
47 
48  }
49 
50 
51  // Virtual destructor needed.
53 
54  // Functions that gets called by framework every event
56  {
57  // retrieve producer name of input TrackCollection(s)
58  std::string trackProducer1 = conf_.getParameter<std::string>("TrackProducer1");
59  std::string trackProducer2 = conf_.getParameter<std::string>("TrackProducer2");
60 
61  double shareFrac = conf_.getParameter<double>("ShareFrac");
62  bool allowFirstHitShare = conf_.getParameter<bool>("allowFirstHitShare");
63  bool checkCharge = conf_.getParameter<bool>("checkCharge");
64  double minProb = conf_.getParameter<double>("minProb");
65 
66  int outputPreferCollection = conf_.getParameter<int>("outputPreferCollection");
67  int trackerOnlyPreferCollection = conf_.getParameter<int>("trackerOnlyPreferCollection");
68  int arbitratedEcalSeededPreferCollection = conf_.getParameter<int>("arbitratedEcalSeededPreferCollection");
69  int arbitratedMergedPreferCollection = conf_.getParameter<int>("arbitratedMergedPreferCollection");
70  int arbitratedMergedEcalGeneralPreferCollection = conf_.getParameter<int>("arbitratedMergedEcalGeneralPreferCollection");
71 
72  // get Inputs
73  // if 1 input list doesn't exist, make an empty list, issue a warning, and continue
74  // this allows ConversionTrackMerger to be used as a cleaner only if handed just one list
75  // if both input lists don't exist, will issue 2 warnings and generate an empty output collection
76  //
77  const reco::ConversionTrackCollection *TC1 = 0;
78  static const reco::ConversionTrackCollection s_empty1, s_empty2;
80  e.getByLabel(trackProducer1, trackCollection1);
81  if (trackCollection1.isValid()) {
82  TC1 = trackCollection1.product();
83  //std::cout << "1st collection " << trackProducer1 << " has "<< TC1->size() << " tracks" << std::endl ;
84  } else {
85  TC1 = &s_empty1;
86  edm::LogWarning("ConversionTrackMerger") << "1st TrackCollection " << trackProducer1 << " not found; will only clean 2nd TrackCollection " << trackProducer2 ;
87  }
89 
90  const reco::ConversionTrackCollection *TC2 = 0;
92  e.getByLabel(trackProducer2, trackCollection2);
93  if (trackCollection2.isValid()) {
94  TC2 = trackCollection2.product();
95  //std::cout << "2nd collection " << trackProducer2 << " has "<< TC2->size() << " tracks" << std::endl ;
96  } else {
97  TC2 = &s_empty2;
98  edm::LogWarning("ConversionTrackMerger") << "2nd TrackCollection " << trackProducer2 << " not found; will only clean 1st TrackCollection " << trackProducer1 ;
99  }
101 
102  // Step B: create empty output collection
103  outputTrks = std::auto_ptr<reco::ConversionTrackCollection>(new reco::ConversionTrackCollection);
104  int i;
105 
106  std::vector<int> selected1; for (unsigned int i=0; i<tC1.size(); ++i){selected1.push_back(1);}
107  std::vector<int> selected2; for (unsigned int i=0; i<tC2.size(); ++i){selected2.push_back(1);}
108 
109 
110  std::map<reco::ConversionTrackCollection::const_iterator, std::vector<const TrackingRecHit*> > rh1;
111  std::map<reco::ConversionTrackCollection::const_iterator, std::vector<const TrackingRecHit*> > rh2;
112  for (reco::ConversionTrackCollection::const_iterator track=tC1.begin(); track!=tC1.end(); ++track){
113  trackingRecHit_iterator itB = track->track()->recHitsBegin();
114  trackingRecHit_iterator itE = track->track()->recHitsEnd();
115  for (trackingRecHit_iterator it = itB; it != itE; ++it) {
116  const TrackingRecHit* hit = &(**it);
117  rh1[track].push_back(hit);
118  }
119  }
120  for (reco::ConversionTrackCollection::const_iterator track=tC2.begin(); track!=tC2.end(); ++track){
121  trackingRecHit_iterator jtB = track->track()->recHitsBegin();
122  trackingRecHit_iterator jtE = track->track()->recHitsEnd();
123  for (trackingRecHit_iterator jt = jtB; jt != jtE; ++jt) {
124  const TrackingRecHit* hit = &(**jt);
125  rh2[track].push_back(hit);
126  }
127  }
128 
129  if ( (0<tC1.size())&&(0<tC2.size()) ){
130  i=-1;
131  for (reco::ConversionTrackCollection::iterator track=tC1.begin(); track!=tC1.end(); ++track){
132  i++;
133 
134  //clear flags if preferCollection was set to 0
135  selected1[i] = selected1[i] && outputPreferCollection!=0;
136  track->setIsTrackerOnly ( track->isTrackerOnly() && trackerOnlyPreferCollection!=0 );
137  track->setIsArbitratedEcalSeeded( track->isArbitratedEcalSeeded() && arbitratedEcalSeededPreferCollection!=0 );
138  track->setIsArbitratedMerged( track->isArbitratedMerged() && arbitratedMergedPreferCollection!=0 );
139  track->setIsArbitratedMergedEcalGeneral( track->isArbitratedMergedEcalGeneral() && arbitratedMergedEcalGeneralPreferCollection!=0 );
140 
141 
142  std::vector<const TrackingRecHit*>& iHits = rh1[track];
143  unsigned nh1 = iHits.size();
144  int j=-1;
145  for (reco::ConversionTrackCollection::iterator track2=tC2.begin(); track2!=tC2.end(); ++track2){
146  j++;
147 
148  //clear flags if preferCollection was set to 0
149  selected2[j] = selected2[j] && outputPreferCollection!=0;
150  track2->setIsTrackerOnly ( track2->isTrackerOnly() && trackerOnlyPreferCollection!=0 );
151  track2->setIsArbitratedEcalSeeded( track2->isArbitratedEcalSeeded() && arbitratedEcalSeededPreferCollection!=0 );
152  track2->setIsArbitratedMerged( track2->isArbitratedMerged() && arbitratedMergedPreferCollection!=0 );
153  track2->setIsArbitratedMergedEcalGeneral( track2->isArbitratedMergedEcalGeneral() && arbitratedMergedEcalGeneralPreferCollection!=0 );
154 
155  std::vector<const TrackingRecHit*>& jHits = rh2[track2];
156  unsigned nh2 = jHits.size();
157  int noverlap=0;
158  int firstoverlap=0;
159  for ( unsigned ih=0; ih<nh1; ++ih ) {
160  const TrackingRecHit* it = iHits[ih];
161  if (it->isValid()){
162  int jj=-1;
163  for ( unsigned jh=0; jh<nh2; ++jh ) {
164  const TrackingRecHit* jt = jHits[jh];
165  jj++;
166  if (jt->isValid()){
167  if ( it->sharesInput(jt,TrackingRecHit::some) ) {
168  noverlap++;
169  if ( allowFirstHitShare && ( ih == 0 ) && ( jh == 0 ) ) firstoverlap=1;
170  }
171  }
172  }
173  }
174  }
175  int nhit1 = track->track()->numberOfValidHits();
176  int nhit2 = track2->track()->numberOfValidHits();
177  //if (noverlap>0) printf("noverlap = %i, firstoverlap = %i, nhit1 = %i, nhit2 = %i, algo1 = %i, algo2 = %i, q1 = %i, q2 = %i\n",noverlap,firstoverlap,nhit1,nhit2,track->track()->algo(),track2->track()->algo(),track->track()->charge(),track2->track()->charge());
178  //std::cout << " trk1 trk2 nhits1 nhits2 nover " << i << " " << j << " " << track->numberOfValidHits() << " " << track2->numberOfValidHits() << " " << noverlap << " " << fi << " " << fj <<std::endl;
179  if ( (noverlap-firstoverlap) > (std::min(nhit1,nhit2)-firstoverlap)*shareFrac && (!checkCharge || track->track()->charge()*track2->track()->charge()>0) ) {
180  //printf("overlapping tracks\n");
181  //printf ("ndof1 = %5f, chisq1 = %5f, ndof2 = %5f, chisq2 = %5f\n",track->track()->ndof(),track->track()->chi2(),track2->track()->ndof(),track2->track()->chi2());
182 
183  double probFirst = ChiSquaredProbability(track->track()->chi2(), track->track()->ndof());
184  double probSecond = ChiSquaredProbability(track2->track()->chi2(), track2->track()->ndof());
185 
186  //arbitrate by number of hits and reduced chisq
187  bool keepFirst = ( nhit1>nhit2 || (nhit1==nhit2 && track->track()->normalizedChi2()<track2->track()->normalizedChi2()) );
188 
189  //override decision in case one track is radically worse quality than the other
190  keepFirst |= (probFirst>minProb && probSecond<=minProb);
191  keepFirst &= !(probFirst<=minProb && probSecond>minProb);
192 
193  bool keepSecond = !keepFirst;
194 
195  //set flags based on arbitration decision and precedence settings
196 
197  selected1[i] = selected1[i] && ( (keepFirst && outputPreferCollection==3) || outputPreferCollection==-1 || outputPreferCollection==1 );
198  track->setIsTrackerOnly ( track->isTrackerOnly() && ( (keepFirst && trackerOnlyPreferCollection==3) || trackerOnlyPreferCollection==-1 || trackerOnlyPreferCollection==1 ) );
199  track->setIsArbitratedEcalSeeded( track->isArbitratedEcalSeeded() && ( (keepFirst && arbitratedEcalSeededPreferCollection==3) || arbitratedEcalSeededPreferCollection==-1 || arbitratedEcalSeededPreferCollection==1 ) );
200  track->setIsArbitratedMerged( track->isArbitratedMerged() && ( (keepFirst && arbitratedMergedPreferCollection==3) || arbitratedMergedPreferCollection==-1 || arbitratedMergedPreferCollection==1 ) );
201  track->setIsArbitratedMergedEcalGeneral( track->isArbitratedMergedEcalGeneral() && ( (keepFirst && arbitratedMergedEcalGeneralPreferCollection==3) || arbitratedMergedEcalGeneralPreferCollection==-1 || arbitratedMergedEcalGeneralPreferCollection==1 ) );
202 
203  selected2[j] = selected2[j] && ( (keepSecond && outputPreferCollection==3) || outputPreferCollection==-1 || outputPreferCollection==2 );
204  track2->setIsTrackerOnly ( track2->isTrackerOnly() && ( (keepSecond && trackerOnlyPreferCollection==3) || trackerOnlyPreferCollection==-1 || trackerOnlyPreferCollection==2 ) );
205  track2->setIsArbitratedEcalSeeded( track2->isArbitratedEcalSeeded() && ( (keepSecond && arbitratedEcalSeededPreferCollection==3) || arbitratedEcalSeededPreferCollection==-1 || arbitratedEcalSeededPreferCollection==2 ) );
206  track2->setIsArbitratedMerged( track2->isArbitratedMerged() && ( (keepSecond && arbitratedMergedPreferCollection==3) || arbitratedMergedPreferCollection==-1 || arbitratedMergedPreferCollection==2 ) );
207  track2->setIsArbitratedMergedEcalGeneral( track2->isArbitratedMergedEcalGeneral() && ( (keepSecond && arbitratedMergedEcalGeneralPreferCollection==3) || arbitratedMergedEcalGeneralPreferCollection==-1 || arbitratedMergedEcalGeneralPreferCollection==2 ) );
208 
209  }//end got a duplicate
210  }//end track2 loop
211  }//end track loop
212  }//end more than 1 track
213 
214  //
215  // output selected tracks - if any
216  //
217 
218  if ( 0<tC1.size() ){
219  i=0;
220  for (reco::ConversionTrackCollection::const_iterator track=tC1.begin(); track!=tC1.end();
221  ++track, ++i){
222  //don't store tracks rejected as duplicates
223  if (!selected1[i]){
224  continue;
225  }
226  //fill the TrackCollection
227  outputTrks->push_back(*track);
228  }//end faux loop over tracks
229  }//end more than 0 track
230 
231  //Fill the trajectories, etc. for 1st collection
232 
233 
234  if ( 0<tC2.size() ){
235  i=0;
236  for (reco::ConversionTrackCollection::const_iterator track=tC2.begin(); track!=tC2.end();
237  ++track, ++i){
238  //don't store tracks rejected as duplicates
239  if (!selected2[i]){
240  continue;
241  }
242  //fill the TrackCollection
243  outputTrks->push_back(*track);
244  }//end faux loop over tracks
245  }//end more than 0 track
246 
247  e.put(outputTrks);
248  return;
249 
250  }//end produce
T getParameter(std::string const &) const
int i
Definition: DBlmapReader.cc:9
std::auto_ptr< reco::ConversionTrackCollection > outputTrks
virtual bool sharesInput(const TrackingRecHit *other, SharedInputType what) const
#define min(a, b)
Definition: mlp_lapack.h:161
std::vector< ConversionTrack > ConversionTrackCollection
collection of ConversionTracks
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:84
int j
Definition: DBlmapReader.cc:9
float ChiSquaredProbability(double chiSquared, double nrDOF)
ConversionTrackMerger(const edm::ParameterSet &conf)
bool isValid() const
Definition: HandleBase.h:76
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:355
tuple conf
Definition: dbtoconf.py:185
bool isValid() const
T const * product() const
Definition: Handle.h:74
virtual void produce(edm::Event &e, const edm::EventSetup &c)