CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
DuplicateTrackMerger.cc
Go to the documentation of this file.
1 
22 #include "TrackMerger.h"
23 #include <vector>
24 #include <algorithm>
25 #include <string>
26 #include <iostream>
27 #include <map>
28 
30 
31 using namespace reco;
32 
34  public:
36  explicit DuplicateTrackMerger(const edm::ParameterSet& iPara);
38  virtual ~DuplicateTrackMerger();
39 
40  typedef std::vector<std::pair<TrackCandidate,std::pair<reco::TrackRef,reco::TrackRef> > > CandidateToDuplicate;
41 
42  protected:
44  void produce( edm::Event &, const edm::EventSetup &) override;
45 
46  private:
49 
51  float tmva_ddsz_;
52  float tmva_ddxy_;
53  float tmva_dphi_;
56  float tmva_d3dr_;
57  float tmva_d3dz_;
60 
61  float* gbrVals_;
62 
70  double minDeltaR3d_;
72  double minBDTG_;
74  double minpT_;
76  double minP_;
78  double maxDCA_;
80  double maxDPhi_;
82  double maxDLambda_;
84  double maxDdxy_;
86  double maxDdsz_;
88  double maxDQoP_;
89 
91 
94  };
95 
105 #include "TFile.h"
106 
108 {
109  forestLabel_ = "MVADuplicate";
110  useForestFromDB_ = true;
111 
112  minDeltaR3d_ = -4.0;
113  minBDTG_ = -0.96;
114  minpT_ = 0.2;
115  minP_ = 0.4;
116  maxDCA_ = 30.0;
117  maxDPhi_ = 0.30;
118  maxDLambda_ = 0.30;
119  maxDdsz_ = 10.0;
120  maxDdxy_ = 10.0;
121  maxDQoP_ = 0.25;
122  if(iPara.exists("minpT"))minpT_ = iPara.getParameter<double>("minpT");
123  if(iPara.exists("minP"))minP_ = iPara.getParameter<double>("minP");
124  if(iPara.exists("maxDCA"))maxDCA_ = iPara.getParameter<double>("maxDCA");
125  if(iPara.exists("maxDPhi"))maxDPhi_ = iPara.getParameter<double>("maxDPhi");
126  if(iPara.exists("maxDLambda"))maxDLambda_ = iPara.getParameter<double>("maxDLambda");
127  if(iPara.exists("maxDdsz"))maxDdsz_ = iPara.getParameter<double>("maxDdsz");
128  if(iPara.exists("maxDdxy"))maxDdxy_ = iPara.getParameter<double>("maxDdxy");
129  if(iPara.exists("maxDQoP"))maxDQoP_ = iPara.getParameter<double>("maxDQoP");
130  if(iPara.exists("source"))trackSource_ = consumes<reco::TrackCollection>(iPara.getParameter<edm::InputTag>("source"));
131  if(iPara.exists("minDeltaR3d"))minDeltaR3d_ = iPara.getParameter<double>("minDeltaR3d");
132  if(iPara.exists("minBDTG"))minBDTG_ = iPara.getParameter<double>("minBDTG");
133 
134  produces<std::vector<TrackCandidate> >("candidates");
135  produces<CandidateToDuplicate>("candidateMap");
136 
137  forest_ = 0;
138  gbrVals_ = new float[9];
139  dbFileName_ = "";
140  if(iPara.exists("GBRForestLabel"))forestLabel_ = iPara.getParameter<std::string>("GBRForestLabel");
141  if(iPara.exists("GBRForestFileName")){
142  dbFileName_ = iPara.getParameter<std::string>("GBRForestFileName");
143  useForestFromDB_ = false;
144  }
145 
146  /*
147  tmvaReader_ = new TMVA::Reader("!Color:Silent");
148  tmvaReader_->AddVariable("ddsz",&tmva_ddsz_);
149  tmvaReader_->AddVariable("ddxy",&tmva_ddxy_);
150  tmvaReader_->AddVariable("dphi",&tmva_dphi_);
151  tmvaReader_->AddVariable("dlambda",&tmva_dlambda_);
152  tmvaReader_->AddVariable("dqoverp",&tmva_dqoverp_);
153  tmvaReader_->AddVariable("delta3d_r",&tmva_d3dr_);
154  tmvaReader_->AddVariable("delta3d_z",&tmva_d3dz_);
155  tmvaReader_->AddVariable("outer_nMissingInner",&tmva_outer_nMissingInner_);
156  tmvaReader_->AddVariable("inner_nMissingOuter",&tmva_inner_nMissingOuter_);
157  tmvaReader_->BookMVA("BDTG",mvaFilePath);
158  */
159 
160 }
161 
163 {
164 
165  if(gbrVals_)delete [] gbrVals_;
166  if(!useForestFromDB_ && forest_) delete forest_;
167  /* no op */
168 
169 }
170 
172 {
173 
174  merger_.init(iSetup);
175 
176  if(!forest_){
177  if(useForestFromDB_){
178  edm::ESHandle<GBRForest> forestHandle;
179  iSetup.get<GBRWrapperRcd>().get(forestLabel_,forestHandle);
180  forest_ = (GBRForest*)forestHandle.product();
181  }else{
182  TFile gbrfile(dbFileName_.c_str());
183  forest_ = (GBRForest*)gbrfile.Get(forestLabel_.c_str());
184  }
185  }
186 
187  //edm::Handle<edm::View<reco::Track> >handle;
189  iEvent.getByToken(trackSource_,handle);
190  reco::TrackRefProd refTrks(handle);
191 
192  iSetup.get<IdealMagneticFieldRecord>().get(magfield_);
194  TSCPBuilderNoMaterial tscpBuilder;
195  std::auto_ptr<std::vector<TrackCandidate> > out_duplicateCandidates(new std::vector<TrackCandidate>());
196 
197  std::auto_ptr<CandidateToDuplicate> out_candidateMap(new CandidateToDuplicate());
198 
199  for(int i = 0; i < (int)handle->size(); i++){
200  const reco::Track *rt1 = &(handle->at(i));
201  if(rt1->innerMomentum().Rho() < minpT_)continue;
202  if(rt1->innerMomentum().R() < minP_)continue;
203  for(int j = i+1; j < (int)handle->size();j++){
204  const reco::Track *rt2 = &(handle->at(j));
205  if(rt2->innerMomentum().Rho() < minpT_)continue;
206  if(rt2->innerMomentum().R() < minP_)continue;
207  if(rt1->charge() != rt2->charge())continue;
208  const reco::Track* t1,*t2;
209  if(rt1->outerPosition().Rho() < rt2->outerPosition().Rho()){
210  t1 = rt1;
211  t2 = rt2;
212  }else{
213  t1 = rt2;
214  t2 = rt1;
215  }
216  double deltaR3d = sqrt(pow(t1->outerPosition().x()-t2->innerPosition().x(),2) + pow(t1->outerPosition().y()-t2->innerPosition().y(),2) + pow(t1->outerPosition().z()-t2->innerPosition().z(),2));
217 
218  if(t1->outerPosition().Rho() > t2->innerPosition().Rho())deltaR3d *= -1.0;
219  if(deltaR3d < minDeltaR3d_)continue;
220 
223  GlobalPoint avgPoint((t1->outerPosition().x()+t2->innerPosition().x())*0.5,(t1->outerPosition().y()+t2->innerPosition().y())*0.5,(t1->outerPosition().z()+t2->innerPosition().z())*0.5);
224  TrajectoryStateClosestToPoint TSCP1 = tscpBuilder(fts1, avgPoint);
225  TrajectoryStateClosestToPoint TSCP2 = tscpBuilder(fts2, avgPoint);
226  if(!TSCP1.isValid())continue;
227  if(!TSCP2.isValid())continue;
228 
229  const FreeTrajectoryState ftsn1 = TSCP1.theState();
230  const FreeTrajectoryState ftsn2 = TSCP2.theState();
231 
232  if ( (ftsn2.position()-ftsn1.position()).mag() > maxDCA_ ) continue;
233 
234  double qoverp1 = ftsn1.signedInverseMomentum();
235  double qoverp2 = ftsn2.signedInverseMomentum();
236  tmva_dqoverp_ = qoverp1-qoverp2;
237  if ( fabs(tmva_dqoverp_) > maxDQoP_ ) continue;
238 
239  double lambda1 = M_PI/2 - ftsn1.momentum().theta();
240  double lambda2 = M_PI/2 - ftsn2.momentum().theta();
241  tmva_dlambda_ = lambda1-lambda2;
242  if ( fabs(tmva_dlambda_) > maxDLambda_ ) continue;
243 
244  double phi1 = ftsn1.momentum().phi();
245  double phi2 = ftsn2.momentum().phi();
246  tmva_dphi_ = phi1-phi2;
247  if(fabs(tmva_dphi_) > M_PI) tmva_dphi_ = 2*M_PI - fabs(tmva_dphi_);
248  if ( fabs(tmva_dphi_) > maxDPhi_ ) continue;
249 
250  double dxy1 = (-ftsn1.position().x() * ftsn1.momentum().y() + ftsn1.position().y() * ftsn1.momentum().x())/TSCP1.pt();
251  double dxy2 = (-ftsn2.position().x() * ftsn2.momentum().y() + ftsn2.position().y() * ftsn2.momentum().x())/TSCP2.pt();
252  tmva_ddxy_ = dxy1-dxy2;
253  if ( fabs(tmva_ddxy_) > maxDdxy_ ) continue;
254 
255  double dsz1 = ftsn1.position().z() * TSCP1.pt() / TSCP1.momentum().mag() - (ftsn1.position().x() * ftsn1.momentum().y() + ftsn1.position().y() * ftsn1.momentum().x())/TSCP1.pt() * ftsn1.momentum().z()/ftsn1.momentum().mag();
256  double dsz2 = ftsn2.position().z() * TSCP2.pt() / TSCP2.momentum().mag() - (ftsn2.position().x() * ftsn2.momentum().y() + ftsn2.position().y() * ftsn2.momentum().x())/TSCP2.pt() * ftsn2.momentum().z()/ftsn2.momentum().mag();
257  tmva_ddsz_ = dsz1-dsz2;
258  if ( fabs(tmva_ddsz_) > maxDdsz_ ) continue;
259 
260  tmva_d3dr_ = avgPoint.perp();
261  tmva_d3dz_ = avgPoint.z();
264 
265 
266  gbrVals_[0] = tmva_ddsz_;
267  gbrVals_[1] = tmva_ddxy_;
268  gbrVals_[2] = tmva_dphi_;
269  gbrVals_[3] = tmva_dlambda_;
270  gbrVals_[4] = tmva_dqoverp_;
271  gbrVals_[5] = tmva_d3dr_;
272  gbrVals_[6] = tmva_d3dz_;
275 
276 
277  double mvaBDTG = forest_->GetClassifier(gbrVals_);
278  if(mvaBDTG < minBDTG_)continue;
279 
280 
281  TrackCandidate mergedTrack = merger_.merge(*t1,*t2);
282  out_duplicateCandidates->push_back(mergedTrack);
283  std::pair<TrackRef,TrackRef> trackPair(TrackRef(refTrks,i),TrackRef(refTrks,j));
284  std::pair<TrackCandidate, std::pair<TrackRef,TrackRef> > cp(mergedTrack,trackPair);
285  out_candidateMap->push_back(cp);
286  }
287  }
288  iEvent.put(out_duplicateCandidates,"candidates");
289  iEvent.put(out_candidateMap,"candidateMap");
290 
291 }
292 
295 
T getParameter(std::string const &) const
#define dso_hidden
int i
Definition: DBlmapReader.cc:9
TrackMerger merger_
Merger.
std::string dbFileName_
MVA weights file.
DuplicateTrackMerger(const edm::ParameterSet &iPara)
constructor
T perp() const
Definition: PV3DBase.h:72
double maxDLambda_
max difference in Lambda between two tracks
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:446
const FreeTrajectoryState & theState() const
T mag() const
The vector magnitude. Equivalent to sqrt(vec.mag2())
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
std::vector< std::pair< TrackCandidate, std::pair< reco::TrackRef, reco::TrackRef > > > CandidateToDuplicate
Geom::Phi< T > phi() const
Definition: PV3DBase.h:69
T y() const
Definition: PV3DBase.h:63
bool exists(std::string const &parameterName) const
checks if a parameter exists
double maxDQoP_
max difference in q/p between two tracks
double maxDPhi_
max difference in phi between two tracks
const math::XYZPoint & outerPosition() const
position of the outermost hit
Definition: Track.h:65
void init(const edm::EventSetup &iSetup)
Definition: TrackMerger.cc:30
TrackCandidate merge(const reco::Track &inner, const reco::Track &outer) const
Definition: TrackMerger.cc:38
double maxDCA_
max distance between two tracks at closest approach
Geom::Theta< T > theta() const
Definition: PV3DBase.h:75
const math::XYZPoint & innerPosition() const
position of the innermost hit
Definition: Track.h:55
int iEvent
Definition: GenABIO.cc:230
double minP_
min p cut value
GBRForest * forest_
MVA discriminator.
T mag() const
Definition: PV3DBase.h:67
double minDeltaR3d_
minDeltaR3d cut value
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:113
T sqrt(T t)
Definition: SSEVec.h:48
double maxDdsz_
max difference in longitudinal impact parameter between two tracks
T z() const
Definition: PV3DBase.h:64
tuple handle
Definition: patZpeak.py:22
int j
Definition: DBlmapReader.cc:9
GlobalVector momentum() const
#define M_PI
edm::ESHandle< MagneticField > magfield_
GlobalPoint position() const
const HitPattern & hitPattern() const
Access the hit pattern, indicating in which Tracker layers the track has hits.
Definition: TrackBase.h:384
const T & get() const
Definition: EventSetup.h:55
edm::Ref< TrackCollection > TrackRef
persistent reference to a Track
Definition: TrackFwd.h:19
T const * product() const
Definition: ESHandle.h:86
virtual ~DuplicateTrackMerger()
destructor
int numberOfLostHits(HitCategory category) const
Definition: HitPattern.h:804
double minpT_
min pT cut value
const math::XYZVector & innerMomentum() const
momentum vector at the innermost hit position
Definition: Track.h:60
void produce(edm::Event &, const edm::EventSetup &) override
produce one event
int charge() const
track electric charge
Definition: TrackBase.h:615
edm::EDGetTokenT< reco::TrackCollection > trackSource_
track input collection
double minBDTG_
minBDTG cut value
float tmva_ddsz_
MVA input variables.
FreeTrajectoryState innerFreeState(const reco::Track &tk, const MagneticField *field, bool withErr=true)
double GetClassifier(const float *vector) const
Definition: GBRForest.h:64
T x() const
Definition: PV3DBase.h:62
FreeTrajectoryState outerFreeState(const reco::Track &tk, const MagneticField *field, bool withErr=true)
double signedInverseMomentum() const
double maxDdxy_
max difference in transverse impact parameter between two tracks
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:40