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 
107 DuplicateTrackMerger::DuplicateTrackMerger(const edm::ParameterSet& iPara) : forest_(nullptr), gbrVals_(nullptr), merger_(iPara)
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  gbrVals_ = new float[9];
138  dbFileName_ = "";
139  if(iPara.exists("GBRForestLabel"))forestLabel_ = iPara.getParameter<std::string>("GBRForestLabel");
140  if(iPara.exists("GBRForestFileName")){
141  dbFileName_ = iPara.getParameter<std::string>("GBRForestFileName");
142  useForestFromDB_ = false;
143  }
144 
145  /*
146  tmvaReader_ = new TMVA::Reader("!Color:Silent");
147  tmvaReader_->AddVariable("ddsz",&tmva_ddsz_);
148  tmvaReader_->AddVariable("ddxy",&tmva_ddxy_);
149  tmvaReader_->AddVariable("dphi",&tmva_dphi_);
150  tmvaReader_->AddVariable("dlambda",&tmva_dlambda_);
151  tmvaReader_->AddVariable("dqoverp",&tmva_dqoverp_);
152  tmvaReader_->AddVariable("delta3d_r",&tmva_d3dr_);
153  tmvaReader_->AddVariable("delta3d_z",&tmva_d3dz_);
154  tmvaReader_->AddVariable("outer_nMissingInner",&tmva_outer_nMissingInner_);
155  tmvaReader_->AddVariable("inner_nMissingOuter",&tmva_inner_nMissingOuter_);
156  tmvaReader_->BookMVA("BDTG",mvaFilePath);
157  */
158 
159 }
160 
162 {
163 
164  if(gbrVals_)delete [] gbrVals_;
165  if(!useForestFromDB_ && forest_) delete forest_;
166  /* no op */
167 
168 }
169 
171 {
172 
173  merger_.init(iSetup);
174 
175  if(!forest_){
176  if(useForestFromDB_){
177  edm::ESHandle<GBRForest> forestHandle;
178  iSetup.get<GBRWrapperRcd>().get(forestLabel_,forestHandle);
179  forest_ = forestHandle.product();
180  }else{
181  TFile gbrfile(dbFileName_.c_str());
182  forest_ = dynamic_cast<const GBRForest*>(gbrfile.Get(forestLabel_.c_str()));
183  }
184  }
185 
186  //edm::Handle<edm::View<reco::Track> >handle;
188  iEvent.getByToken(trackSource_,handle);
189  reco::TrackRefProd refTrks(handle);
190 
191  iSetup.get<IdealMagneticFieldRecord>().get(magfield_);
193  TSCPBuilderNoMaterial tscpBuilder;
194  std::auto_ptr<std::vector<TrackCandidate> > out_duplicateCandidates(new std::vector<TrackCandidate>());
195 
196  std::auto_ptr<CandidateToDuplicate> out_candidateMap(new CandidateToDuplicate());
197 
198  for(int i = 0; i < (int)handle->size(); i++){
199  const reco::Track *rt1 = &(handle->at(i));
200  if(rt1->innerMomentum().Rho() < minpT_)continue;
201  if(rt1->innerMomentum().R() < minP_)continue;
202  for(int j = i+1; j < (int)handle->size();j++){
203  const reco::Track *rt2 = &(handle->at(j));
204  if(rt2->innerMomentum().Rho() < minpT_)continue;
205  if(rt2->innerMomentum().R() < minP_)continue;
206  if(rt1->charge() != rt2->charge())continue;
207  const reco::Track* t1,*t2;
208  if(rt1->outerPosition().Rho() < rt2->outerPosition().Rho()){
209  t1 = rt1;
210  t2 = rt2;
211  }else{
212  t1 = rt2;
213  t2 = rt1;
214  }
215  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));
216 
217  if(t1->outerPosition().Rho() > t2->innerPosition().Rho())deltaR3d *= -1.0;
218  if(deltaR3d < minDeltaR3d_)continue;
219 
222  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);
223  TrajectoryStateClosestToPoint TSCP1 = tscpBuilder(fts1, avgPoint);
224  TrajectoryStateClosestToPoint TSCP2 = tscpBuilder(fts2, avgPoint);
225  if(!TSCP1.isValid())continue;
226  if(!TSCP2.isValid())continue;
227 
228  const FreeTrajectoryState ftsn1 = TSCP1.theState();
229  const FreeTrajectoryState ftsn2 = TSCP2.theState();
230 
231  if ( (ftsn2.position()-ftsn1.position()).mag() > maxDCA_ ) continue;
232 
233  double qoverp1 = ftsn1.signedInverseMomentum();
234  double qoverp2 = ftsn2.signedInverseMomentum();
235  tmva_dqoverp_ = qoverp1-qoverp2;
236  if ( fabs(tmva_dqoverp_) > maxDQoP_ ) continue;
237 
238  double lambda1 = M_PI/2 - ftsn1.momentum().theta();
239  double lambda2 = M_PI/2 - ftsn2.momentum().theta();
240  tmva_dlambda_ = lambda1-lambda2;
241  if ( fabs(tmva_dlambda_) > maxDLambda_ ) continue;
242 
243  double phi1 = ftsn1.momentum().phi();
244  double phi2 = ftsn2.momentum().phi();
245  tmva_dphi_ = phi1-phi2;
246  if(fabs(tmva_dphi_) > M_PI) tmva_dphi_ = 2*M_PI - fabs(tmva_dphi_);
247  if ( fabs(tmva_dphi_) > maxDPhi_ ) continue;
248 
249  double dxy1 = (-ftsn1.position().x() * ftsn1.momentum().y() + ftsn1.position().y() * ftsn1.momentum().x())/TSCP1.pt();
250  double dxy2 = (-ftsn2.position().x() * ftsn2.momentum().y() + ftsn2.position().y() * ftsn2.momentum().x())/TSCP2.pt();
251  tmva_ddxy_ = dxy1-dxy2;
252  if ( fabs(tmva_ddxy_) > maxDdxy_ ) continue;
253 
254  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();
255  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();
256  tmva_ddsz_ = dsz1-dsz2;
257  if ( fabs(tmva_ddsz_) > maxDdsz_ ) continue;
258 
259  tmva_d3dr_ = avgPoint.perp();
260  tmva_d3dz_ = avgPoint.z();
263 
264 
265  gbrVals_[0] = tmva_ddsz_;
266  gbrVals_[1] = tmva_ddxy_;
267  gbrVals_[2] = tmva_dphi_;
268  gbrVals_[3] = tmva_dlambda_;
269  gbrVals_[4] = tmva_dqoverp_;
270  gbrVals_[5] = tmva_d3dr_;
271  gbrVals_[6] = tmva_d3dz_;
274 
275 
276  double mvaBDTG = forest_->GetClassifier(gbrVals_);
277  if(mvaBDTG < minBDTG_)continue;
278 
279 
280  TrackCandidate mergedTrack = merger_.merge(*t1,*t2);
281  out_duplicateCandidates->push_back(mergedTrack);
282  std::pair<TrackRef,TrackRef> trackPair(TrackRef(refTrks,i),TrackRef(refTrks,j));
283  std::pair<TrackCandidate, std::pair<TrackRef,TrackRef> > cp(mergedTrack,trackPair);
284  out_candidateMap->push_back(cp);
285  }
286  }
287  iEvent.put(out_duplicateCandidates,"candidates");
288  iEvent.put(out_candidateMap,"candidateMap");
289 
290 }
291 
294 
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:449
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
#define nullptr
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
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:411
const GBRForest * forest_
MVA discriminator.
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:520
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