CMS 3D CMS Logo

ConvertedPhotonProducer.cc
Go to the documentation of this file.
1 #include <iostream>
2 #include <vector>
3 #include <memory>
4 
5 // Framework
12 //
16 
17 //
18 
27 //
29 //
31 //
36 //
39 //
45 
47  : conf_(config), theTrackPairFinder_(nullptr), theVertexFinder_(nullptr), theLikelihoodCalc_(nullptr) {
48  //cout<< " ConvertedPhotonProducer CTOR " << "\n";
49 
50  // use onfiguration file to setup input collection names
52  consumes<edm::View<reco::CaloCluster> >(conf_.getParameter<edm::InputTag>("bcBarrelCollection"));
54  consumes<edm::View<reco::CaloCluster> >(conf_.getParameter<edm::InputTag>("bcEndcapCollection"));
55 
57  consumes<edm::View<reco::CaloCluster> >(conf_.getParameter<edm::InputTag>("scHybridBarrelProducer"));
59  consumes<edm::View<reco::CaloCluster> >(conf_.getParameter<edm::InputTag>("scIslandEndcapProducer"));
60 
61  std::string oitrackprod = conf_.getParameter<std::string>("conversionOITrackProducer");
62  std::string iotrackprod = conf_.getParameter<std::string>("conversionIOTrackProducer");
63 
64  std::string oitrackassoc = conf_.getParameter<std::string>("outInTrackSCAssociation");
65  std::string iotrackassoc = conf_.getParameter<std::string>("inOutTrackSCAssociation");
66 
67  edm::InputTag oitracks(oitrackprod), oitracksassoc(oitrackprod, oitrackassoc), iotracks(iotrackprod),
68  iotracksassoc(iotrackprod, iotrackassoc);
69 
70  conversionOITrackProducer_ = consumes<reco::TrackCollection>(oitracks);
71  outInTrackSCAssociationCollection_ = consumes<reco::TrackCaloClusterPtrAssociation>(oitracksassoc);
72  conversionIOTrackProducer_ = consumes<reco::TrackCollection>(iotracks);
73  inOutTrackSCAssociationCollection_ = consumes<reco::TrackCaloClusterPtrAssociation>(iotracksassoc);
74 
75  generalTrackProducer_ = consumes<reco::TrackCollection>(conf_.getParameter<edm::InputTag>("generalTracksSrc"));
76 
77  algoName_ = conf_.getParameter<std::string>("AlgorithmName");
78 
79  hcalTowers_ = consumes<CaloTowerCollection>(conf_.getParameter<edm::InputTag>("hcalTowers"));
80  hOverEConeSize_ = conf_.getParameter<double>("hOverEConeSize");
81  maxHOverE_ = conf_.getParameter<double>("maxHOverE");
82  minSCEt_ = conf_.getParameter<double>("minSCEt");
83  recoverOneTrackCase_ = conf_.getParameter<bool>("recoverOneTrackCase");
84  dRForConversionRecovery_ = conf_.getParameter<double>("dRForConversionRecovery");
85  deltaCotCut_ = conf_.getParameter<double>("deltaCotCut");
86  minApproachDisCut_ = conf_.getParameter<double>("minApproachDisCut");
87 
88  maxNumOfCandidates_ = conf_.getParameter<int>("maxNumOfCandidates");
89  risolveAmbiguity_ = conf_.getParameter<bool>("risolveConversionAmbiguity");
90  likelihoodWeights_ = conf_.getParameter<std::string>("MVA_weights_location");
91 
92  // use configuration file to setup output collection names
93  ConvertedPhotonCollection_ = conf_.getParameter<std::string>("convertedPhotonCollection");
94  CleanedConvertedPhotonCollection_ = conf_.getParameter<std::string>("cleanedConvertedPhotonCollection");
95 
96  // Register the product
97  produces<reco::ConversionCollection>(ConvertedPhotonCollection_);
98  produces<reco::ConversionCollection>(CleanedConvertedPhotonCollection_);
99 
100  // instantiate the Track Pair Finder algorithm
102  edm::FileInPath path_mvaWeightFile(likelihoodWeights_.c_str());
104  theLikelihoodCalc_->setWeightsFile(path_mvaWeightFile.fullPath().c_str());
105  // instantiate the Vertex Finder algorithm
107 
108  // Inizilize my global event counter
109  nEvt_ = 0;
110 }
111 
113  delete theTrackPairFinder_;
114  delete theLikelihoodCalc_;
115  delete theVertexFinder_;
116 }
117 
118 void ConvertedPhotonProducer::beginRun(edm::Run const& r, edm::EventSetup const& theEventSetup) {
119  //get magnetic field
120  //edm::LogInfo("ConvertedPhotonProducer") << " get magnetic field" << "\n";
121  theEventSetup.get<IdealMagneticFieldRecord>().get(theMF_);
122 
123  // Transform Track into TransientTrack (needed by the Vertex fitter)
124  theEventSetup.get<TransientTrackRecord>().get("TransientTrackBuilder", theTransientTrackBuilder_);
125 }
126 
127 void ConvertedPhotonProducer::produce(edm::Event& theEvent, const edm::EventSetup& theEventSetup) {
128  using namespace edm;
129  nEvt_++;
130 
131  // LogDebug("ConvertedPhotonProducer") << "ConvertedPhotonProduce::produce event number " << theEvent.id() << " Global counter " << nEvt_ << "\n";
132  // std::cout << "ConvertedPhotonProduce::produce event number " << theEvent.id() << " Global counter " << nEvt_ << "\n";
133 
134  //
135  // create empty output collections
136  //
137  // Converted photon candidates
138  reco::ConversionCollection outputConvPhotonCollection;
139  auto outputConvPhotonCollection_p = std::make_unique<reco::ConversionCollection>();
140  // Converted photon candidates
141  reco::ConversionCollection cleanedConversionCollection;
142  auto cleanedConversionCollection_p = std::make_unique<reco::ConversionCollection>();
143 
144  // Get the Super Cluster collection in the Barrel
145  bool validBarrelSCHandle = true;
147  theEvent.getByToken(scHybridBarrelProducer_, scBarrelHandle);
148  if (!scBarrelHandle.isValid()) {
149  edm::LogError("ConvertedPhotonProducer") << "Error! Can't get the scHybridBarrelProducer";
150  validBarrelSCHandle = false;
151  }
152 
153  // Get the Super Cluster collection in the Endcap
154  bool validEndcapSCHandle = true;
156  theEvent.getByToken(scIslandEndcapProducer_, scEndcapHandle);
157  if (!scEndcapHandle.isValid()) {
158  edm::LogError("ConvertedPhotonProducer") << "Error! Can't get the scIslandEndcapProducer";
159  validEndcapSCHandle = false;
160  }
161 
163  bool validTrackInputs = true;
164  Handle<reco::TrackCollection> outInTrkHandle;
165  theEvent.getByToken(conversionOITrackProducer_, outInTrkHandle);
166  if (!outInTrkHandle.isValid()) {
167  //std::cout << "Error! Can't get the conversionOITrack " << "\n";
168  edm::LogError("ConvertedPhotonProducer") << "Error! Can't get the conversionOITrack "
169  << "\n";
170  validTrackInputs = false;
171  }
172  // LogDebug("ConvertedPhotonProducer")<< "ConvertedPhotonProducer outInTrack collection size " << (*outInTrkHandle).size() << "\n";
173 
175  Handle<reco::TrackCaloClusterPtrAssociation> outInTrkSCAssocHandle;
176  theEvent.getByToken(outInTrackSCAssociationCollection_, outInTrkSCAssocHandle);
177  if (!outInTrkSCAssocHandle.isValid()) {
178  // std::cout << "Error! Can't get the product " << outInTrackSCAssociationCollection_.c_str() <<"\n";
179  edm::LogError("ConvertedPhotonProducer") << "Error! Can't get the outInTrackSCAssociationCollection)";
180  validTrackInputs = false;
181  }
182 
184  Handle<reco::TrackCollection> inOutTrkHandle;
185  theEvent.getByToken(conversionIOTrackProducer_, inOutTrkHandle);
186  if (!inOutTrkHandle.isValid()) {
187  // std::cout << "Error! Can't get the conversionIOTrack " << "\n";
188  edm::LogError("ConvertedPhotonProducer") << "Error! Can't get the conversionIOTrack "
189  << "\n";
190  validTrackInputs = false;
191  }
192  // LogDebug("ConvertedPhotonProducer") << " ConvertedPhotonProducer inOutTrack collection size " << (*inOutTrkHandle).size() << "\n";
193 
195 
196  Handle<reco::TrackCollection> generalTrkHandle;
197  if (recoverOneTrackCase_) {
198  theEvent.getByToken(generalTrackProducer_, generalTrkHandle);
199  if (!generalTrkHandle.isValid()) {
200  //std::cout << "Error! Can't get the genralTracks " << "\n";
201  edm::LogError("ConvertedPhotonProducer") << "Error! Can't get the genralTracks "
202  << "\n";
203  }
204  }
205 
207  Handle<reco::TrackCaloClusterPtrAssociation> inOutTrkSCAssocHandle;
208  theEvent.getByToken(inOutTrackSCAssociationCollection_, inOutTrkSCAssocHandle);
209  if (!inOutTrkSCAssocHandle.isValid()) {
210  //std::cout << "Error! Can't get the product " << inOutTrackSCAssociationCollection_.c_str() <<"\n";
211  edm::LogError("ConvertedPhotonProducer") << "Error! Can't get the inOutTrackSCAssociationCollection_.c_str()";
212  validTrackInputs = false;
213  }
214 
215  // Get the basic cluster collection in the Barrel
217  theEvent.getByToken(bcBarrelCollection_, bcBarrelHandle);
218  if (!bcBarrelHandle.isValid()) {
219  edm::LogError("ConvertedPhotonProducer") << "Error! Can't get the bcBarrelCollection";
220  }
221 
222  // Get the basic cluster collection in the Endcap
224  theEvent.getByToken(bcEndcapCollection_, bcEndcapHandle);
225  if (!bcEndcapHandle.isValid()) {
226  edm::LogError("ConvertedPhotonProducer") << "Error! Can't get the bcEndcapCollection";
227  }
228 
229  // get Hcal towers collection
230  Handle<CaloTowerCollection> hcalTowersHandle;
231  theEvent.getByToken(hcalTowers_, hcalTowersHandle);
232 
233  // get the geometry from the event setup:
234  theEventSetup.get<CaloGeometryRecord>().get(theCaloGeom_);
235 
236  if (validTrackInputs) {
237  //do the conversion:
238  std::vector<reco::TransientTrack> t_outInTrk = (*theTransientTrackBuilder_).build(outInTrkHandle);
239  std::vector<reco::TransientTrack> t_inOutTrk = (*theTransientTrackBuilder_).build(inOutTrkHandle);
240 
242  std::map<std::vector<reco::TransientTrack>, reco::CaloClusterPtr, CompareTwoTracksVectors> allPairs;
243  allPairs = theTrackPairFinder_->run(
244  t_outInTrk, outInTrkHandle, outInTrkSCAssocHandle, t_inOutTrk, inOutTrkHandle, inOutTrkSCAssocHandle);
245  //LogDebug("ConvertedPhotonProducer") << "ConvertedPhotonProducer allPairs.size " << allPairs.size() << "\n";
246 
247  buildCollections(theEventSetup,
248  scBarrelHandle,
249  bcBarrelHandle,
250  hcalTowersHandle,
251  generalTrkHandle,
252  allPairs,
253  outputConvPhotonCollection);
254  buildCollections(theEventSetup,
255  scEndcapHandle,
256  bcEndcapHandle,
257  hcalTowersHandle,
258  generalTrkHandle,
259  allPairs,
260  outputConvPhotonCollection);
261  }
262 
263  // put the product in the event
264  outputConvPhotonCollection_p->assign(outputConvPhotonCollection.begin(), outputConvPhotonCollection.end());
265  //LogDebug("ConvertedPhotonProducer") << " ConvertedPhotonProducer Putting in the event converted photon candidates " << (*outputConvPhotonCollection_p).size() << "\n";
267  theEvent.put(std::move(outputConvPhotonCollection_p), ConvertedPhotonCollection_);
268 
269  // Loop over barrel and endcap SC collections and fill the photon collection
270  if (validBarrelSCHandle)
271  cleanCollections(scBarrelHandle, conversionHandle, cleanedConversionCollection);
272  if (validEndcapSCHandle)
273  cleanCollections(scEndcapHandle, conversionHandle, cleanedConversionCollection);
274 
275  cleanedConversionCollection_p->assign(cleanedConversionCollection.begin(), cleanedConversionCollection.end());
276  theEvent.put(std::move(cleanedConversionCollection_p), CleanedConvertedPhotonCollection_);
277 }
278 
280  edm::EventSetup const& es,
281  const edm::Handle<edm::View<reco::CaloCluster> >& scHandle,
282  const edm::Handle<edm::View<reco::CaloCluster> >& bcHandle,
283  const edm::Handle<CaloTowerCollection>& hcalTowersHandle,
284  const edm::Handle<reco::TrackCollection>& generalTrkHandle,
285  std::map<std::vector<reco::TransientTrack>, reco::CaloClusterPtr, CompareTwoTracksVectors>& allPairs,
286  reco::ConversionCollection& outputConvPhotonCollection)
287 
288 {
289  // instantiate the algorithm for finding the position of the track extrapolation at the Ecal front face
290  ConversionTrackEcalImpactPoint theEcalImpactPositionFinder(&(*theMF_));
291 
293 
294  std::vector<reco::TransientTrack> t_generalTrk;
296  t_generalTrk = (*theTransientTrackBuilder_).build(generalTrkHandle);
297  //const CaloGeometry* geometry = theCaloGeom_.product();
298 
299  // Loop over SC in the barrel and reconstruct converted photons
300  int myCands = 0;
302  for (auto const& aClus : scHandle->ptrs()) {
303  // preselection based in Et and H/E cut
304  if (aClus->energy() / cosh(aClus->eta()) <= minSCEt_)
305  continue;
306  const reco::CaloCluster* pClus = &(*aClus);
307  const reco::SuperCluster* sc = dynamic_cast<const reco::SuperCluster*>(pClus);
308  const CaloTowerCollection* hcalTowersColl = hcalTowersHandle.product();
309  EgammaTowerIsolation towerIso(hOverEConeSize_, 0., 0., -1, hcalTowersColl);
310  double HoE = towerIso.getTowerESum(sc) / sc->energy();
311  if (HoE >= maxHOverE_)
312  continue;
314 
315  std::vector<edm::Ref<reco::TrackCollection> > trackPairRef;
316  std::vector<math::XYZPointF> trackInnPos;
317  std::vector<math::XYZVectorF> trackPin;
318  std::vector<math::XYZVectorF> trackPout;
319  float minAppDist = -99;
320 
321  //LogDebug("ConvertedPhotonProducer") << "ConvertedPhotonProducer SC energy " << aClus->energy() << " eta " << aClus->eta() << " phi " << aClus->phi() << "\n";
322 
324  const reco::Particle::Point vtx(0, 0, 0);
325 
326  math::XYZVector direction = aClus->position() - vtx;
327  math::XYZVector momentum = direction.unit() * aClus->energy();
328  const reco::Particle::LorentzVector p4(momentum.x(), momentum.y(), momentum.z(), aClus->energy());
329 
330  int nFound = 0;
331  if (!allPairs.empty()) {
332  nFound = 0;
333 
334  for (std::map<std::vector<reco::TransientTrack>, reco::CaloClusterPtr>::const_iterator iPair = allPairs.begin();
335  iPair != allPairs.end();
336  ++iPair) {
337  scPtrVec.clear();
338 
339  reco::Vertex theConversionVertex;
340  reco::CaloClusterPtr caloPtr = iPair->second;
341  if (!(aClus == caloPtr))
342  continue;
343 
344  scPtrVec.push_back(aClus);
345  nFound++;
346 
347  std::vector<math::XYZPointF> trkPositionAtEcal = theEcalImpactPositionFinder.find(iPair->first, bcHandle);
348  std::vector<reco::CaloClusterPtr> matchingBC = theEcalImpactPositionFinder.matchingBC();
349 
350  minAppDist = -99;
351  const std::string metname = "ConvertedPhotons|ConvertedPhotonProducer";
352  if ((iPair->first).size() > 1) {
353  try {
354  theVertexFinder_->run(iPair->first, theConversionVertex);
355 
356  } catch (cms::Exception& e) {
357  //std::cout << " cms::Exception caught in ConvertedPhotonProducer::produce" << "\n" ;
358  edm::LogWarning(metname) << "cms::Exception caught in ConvertedPhotonProducer::produce\n"
359  << e.explainSelf();
360  }
361 
362  // Old TwoTrackMinimumDistance md;
363  // Old md.calculate ( (iPair->first)[0].initialFreeState(), (iPair->first)[1].initialFreeState() );
364  // Old minAppDist = md.distance();
365 
366  /*
367  for ( unsigned int i=0; i< matchingBC.size(); ++i) {
368  if ( matchingBC[i].isNull() ) std::cout << " This ref to BC is null: skipping " << "\n";
369  else
370  std::cout << " BC energy " << matchingBC[i]->energy() << "\n";
371  }
372  */
373 
375  trackPairRef.clear();
376  trackInnPos.clear();
377  trackPin.clear();
378  trackPout.clear();
379 
380  for (std::vector<reco::TransientTrack>::const_iterator iTk = (iPair->first).begin();
381  iTk != (iPair->first).end();
382  ++iTk) {
383  //LogDebug("ConvertedPhotonProducer") << " ConvertedPhotonProducer Transient Tracks in the pair charge " << iTk->charge() << " Num of RecHits " << iTk->recHitsSize() << " inner momentum " << iTk->track().innerMomentum() << "\n";
384 
385  const reco::TrackTransientTrack* ttt =
386  dynamic_cast<const reco::TrackTransientTrack*>(iTk->basicTransientTrack());
387  reco::TrackRef myTkRef = ttt->persistentTrackRef();
388 
389  //LogDebug("ConvertedPhotonProducer") << " ConvertedPhotonProducer Ref to Rec Tracks in the pair charge " << myTkRef->charge() << " Num of RecHits " << myTkRef->recHitsSize() << " inner momentum " << myTkRef->innerMomentum() << "\n";
390  if (myTkRef->extra().isNonnull()) {
391  trackInnPos.push_back(toFConverterP(myTkRef->innerPosition()));
392  trackPin.push_back(toFConverterV(myTkRef->innerMomentum()));
393  trackPout.push_back(toFConverterV(myTkRef->outerMomentum()));
394  }
395  trackPairRef.push_back(myTkRef);
396  }
397 
398  // std::cout << " ConvertedPhotonProducer trackPin size " << trackPin.size() << std::endl;
399  //LogDebug("ConvertedPhotonProducer") << " ConvertedPhotonProducer SC energy " << aClus->energy() << "\n";
400  //LogDebug("ConvertedPhotonProducer") << " ConvertedPhotonProducer photon p4 " << p4 << "\n";
401  //LogDebug("ConvertedPhotonProducer") << " ConvertedPhotonProducer vtx " << vtx.x() << " " << vtx.y() << " " << vtx.z() << "\n";
402  if (theConversionVertex.isValid()) {
403  //LogDebug("ConvertedPhotonProducer") << " ConvertedPhotonProducer theConversionVertex " << theConversionVertex.position().x() << " " << theConversionVertex.position().y() << " " << theConversionVertex.position().z() << "\n";
404  }
405  //LogDebug("ConvertedPhotonProducer") << " ConvertedPhotonProducer trackPairRef " << trackPairRef.size() << "\n";
406 
407  minAppDist = calculateMinApproachDistance(trackPairRef[0], trackPairRef[1]);
408 
409  double like = -999.;
410  reco::Conversion newCandidate(scPtrVec,
411  trackPairRef,
412  trkPositionAtEcal,
413  theConversionVertex,
414  matchingBC,
415  minAppDist,
416  trackInnPos,
417  trackPin,
418  trackPout,
419  like,
420  algo);
421  // like = theLikelihoodCalc_->calculateLikelihood(newCandidate, es );
422  like = theLikelihoodCalc_->calculateLikelihood(newCandidate);
423  // std::cout << "like = " << like << std::endl;
424  newCandidate.setMVAout(like);
425  outputConvPhotonCollection.push_back(newCandidate);
426 
427  myCands++;
428  //LogDebug("ConvertedPhotonProducer") << " ConvertedPhotonProducer Put the ConvertedPhotonCollection a candidate in the Barrel " << "\n";
429 
430  } else {
431  // std::cout << " ConvertedPhotonProducer case with only one track found " << "\n";
432 
433  //std::cout << " ConvertedPhotonProducer recovering one track " << "\n";
434  trackPairRef.clear();
435  trackInnPos.clear();
436  trackPin.clear();
437  trackPout.clear();
438  std::vector<reco::TransientTrack>::const_iterator iTk = (iPair->first).begin();
439  //std::cout << " ConvertedPhotonProducer Transient Tracks in the pair charge " << iTk->charge() << " Num of RecHits " << iTk->recHitsSize() << " inner momentum " << iTk->track().innerMomentum() << " pt " << sqrt(iTk->track().innerMomentum().perp2()) << "\n";
440  const reco::TrackTransientTrack* ttt =
441  dynamic_cast<const reco::TrackTransientTrack*>(iTk->basicTransientTrack());
442  reco::TrackRef myTk = ttt->persistentTrackRef();
443  if (myTk->extra().isNonnull()) {
444  trackInnPos.push_back(toFConverterP(myTk->innerPosition()));
445  trackPin.push_back(toFConverterV(myTk->innerMomentum()));
446  trackPout.push_back(toFConverterV(myTk->outerMomentum()));
447  }
448  trackPairRef.push_back(myTk);
449  //std::cout << " Provenance " << myTk->algoName() << std::endl;
450 
451  if (recoverOneTrackCase_) {
452  float theta1 = myTk->innerMomentum().Theta();
453  float dCot = 999.;
454  float dCotTheta = -999.;
455  reco::TrackRef goodRef;
456  std::vector<reco::TransientTrack>::const_iterator iGoodGenTran;
457  for (std::vector<reco::TransientTrack>::const_iterator iTran = t_generalTrk.begin();
458  iTran != t_generalTrk.end();
459  ++iTran) {
460  const reco::TrackTransientTrack* ttt =
461  dynamic_cast<const reco::TrackTransientTrack*>(iTran->basicTransientTrack());
462  reco::TrackRef trRef = ttt->persistentTrackRef();
463  if (trRef->charge() * myTk->charge() > 0)
464  continue;
465  float dEta = trRef->eta() - myTk->eta();
466  float dPhi = trRef->phi() - myTk->phi();
467  if (sqrt(dEta * dEta + dPhi * dPhi) > dRForConversionRecovery_)
468  continue;
469  float theta2 = trRef->innerMomentum().Theta();
470  dCotTheta = 1. / tan(theta1) - 1. / tan(theta2);
471  // std::cout << " ConvertedPhotonProducer recovering general transient track charge " << trRef->charge() << " momentum " << trRef->innerMomentum() << " dcotTheta " << fabs(dCotTheta) << std::endl;
472  if (fabs(dCotTheta) < dCot) {
473  dCot = fabs(dCotTheta);
474  goodRef = trRef;
475  iGoodGenTran = iTran;
476  }
477  }
478 
479  if (goodRef.isNonnull()) {
480  minAppDist = calculateMinApproachDistance(myTk, goodRef);
481 
482  // std::cout << " ConvertedPhotonProducer chosen dCotTheta " << fabs(dCotTheta) << std::endl;
483  if (fabs(dCotTheta) < deltaCotCut_ && minAppDist > minApproachDisCut_) {
484  trackInnPos.push_back(toFConverterP(goodRef->innerPosition()));
485  trackPin.push_back(toFConverterV(goodRef->innerMomentum()));
486  trackPout.push_back(toFConverterV(goodRef->outerMomentum()));
487  trackPairRef.push_back(goodRef);
488  // std::cout << " ConvertedPhotonProducer adding opposite charge track from generalTrackCollection charge " << goodRef ->charge() << " pt " << sqrt(goodRef->innerMomentum().perp2()) << " trackPairRef size " << trackPairRef.size() << std::endl;
489  //std::cout << " Track Provenenance " << goodRef->algoName() << std::endl;
490  std::vector<reco::TransientTrack> mypair;
491  mypair.push_back(*iTk);
492  mypair.push_back(*iGoodGenTran);
493 
494  try {
495  theVertexFinder_->run(iPair->first, theConversionVertex);
496 
497  } catch (cms::Exception& e) {
498  //std::cout << " cms::Exception caught in ConvertedPhotonProducer::produce" << "\n" ;
499  edm::LogWarning(metname) << "cms::Exception caught in ConvertedPhotonProducer::produce\n"
500  << e.explainSelf();
501  }
502  }
503  }
504 
505  } // bool On/Off one track case recovery using generalTracks
506  double like = -999.;
507  reco::Conversion newCandidate(scPtrVec,
508  trackPairRef,
509  trkPositionAtEcal,
510  theConversionVertex,
511  matchingBC,
512  minAppDist,
513  trackInnPos,
514  trackPin,
515  trackPout,
516  like,
517  algo);
518  like = theLikelihoodCalc_->calculateLikelihood(newCandidate);
519  newCandidate.setMVAout(like);
520  outputConvPhotonCollection.push_back(newCandidate);
521 
522  } // case with only on track: looking in general tracks
523  }
524  }
525  }
526 }
527 
530  reco::ConversionCollection& outputConversionCollection) {
531  reco::Conversion* newCandidate = nullptr;
532  for (auto const& aClus : scHandle->ptrs()) {
533  // SC energy preselection
534  if (aClus->energy() / cosh(aClus->eta()) <= minSCEt_)
535  continue;
536 
537  if (conversionHandle.isValid()) {
538  if (risolveAmbiguity_) {
539  std::vector<reco::ConversionRef> bestRef = solveAmbiguity(conversionHandle, aClus);
540 
541  for (std::vector<reco::ConversionRef>::iterator iRef = bestRef.begin(); iRef != bestRef.end(); iRef++) {
542  if (iRef->isNonnull()) {
543  newCandidate = (*iRef)->clone();
544  outputConversionCollection.push_back(*newCandidate);
545  delete newCandidate;
546  }
547  }
548 
549  } else {
550  for (unsigned int icp = 0; icp < conversionHandle->size(); icp++) {
551  reco::ConversionRef cpRef(reco::ConversionRef(conversionHandle, icp));
552  if (!(aClus.id() == cpRef->caloCluster()[0].id() && aClus.key() == cpRef->caloCluster()[0].key()))
553  continue;
554  if (!cpRef->isConverted())
555  continue;
556  if (cpRef->nTracks() < 2)
557  continue;
558  newCandidate = (&(*cpRef))->clone();
559  outputConversionCollection.push_back(*newCandidate);
560  delete newCandidate;
561  }
562 
563  } // solve or not the ambiguity of many conversion candidates
564  }
565  }
566 }
567 
568 std::vector<reco::ConversionRef> ConvertedPhotonProducer::solveAmbiguity(
570  std::multimap<double, reco::ConversionRef, std::greater<double> > convMap;
571 
572  for (unsigned int icp = 0; icp < conversionHandle->size(); icp++) {
573  reco::ConversionRef cpRef(reco::ConversionRef(conversionHandle, icp));
574 
575  //std::cout << " cpRef " << cpRef->nTracks() << " " << cpRef ->caloCluster()[0]->energy() << std::endl;
576  if (!(scRef.id() == cpRef->caloCluster()[0].id() && scRef.key() == cpRef->caloCluster()[0].key()))
577  continue;
578  if (!cpRef->isConverted())
579  continue;
580  double like = cpRef->MVAout();
581  if (cpRef->nTracks() < 2)
582  continue;
583  // std::cout << " Like " << like << std::endl;
584  convMap.insert(std::make_pair(like, cpRef));
585  }
586 
587  // std::cout << " convMap size " << convMap.size() << std::endl;
588 
589  std::multimap<double, reco::ConversionRef>::iterator iMap;
590  std::vector<reco::ConversionRef> bestRefs;
591  for (iMap = convMap.begin(); iMap != convMap.end(); iMap++) {
592  // std::cout << " Like list in the map " << iMap->first << " " << (iMap->second)->EoverP() << std::endl;
593  bestRefs.push_back(iMap->second);
594  if (int(bestRefs.size()) == maxNumOfCandidates_)
595  break;
596  }
597 
598  return bestRefs;
599 }
600 
602  const reco::TrackRef& track2) {
603  float dist = 9999.;
604 
605  double x1, x2, y1, y2;
606  double xx_1 = track1->innerPosition().x(), yy_1 = track1->innerPosition().y(), zz_1 = track1->innerPosition().z();
607  double xx_2 = track2->innerPosition().x(), yy_2 = track2->innerPosition().y(), zz_2 = track2->innerPosition().z();
608  double radius1 = track1->innerMomentum().Rho() / (.3 * (theMF_->inTesla(GlobalPoint(xx_1, yy_1, zz_1)).z())) * 100;
609  double radius2 = track2->innerMomentum().Rho() / (.3 * (theMF_->inTesla(GlobalPoint(xx_2, yy_2, zz_2)).z())) * 100;
610  getCircleCenter(track1, radius1, x1, y1);
611  getCircleCenter(track2, radius2, x2, y2);
612  dist = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)) - radius1 - radius2;
613 
614  return dist;
615 }
616 
617 void ConvertedPhotonProducer::getCircleCenter(const reco::TrackRef& tk, double r, double& x0, double& y0) {
618  double x1, y1, phi;
619  x1 = tk->innerPosition().x(); //inner position and inner momentum need track Extra!
620  y1 = tk->innerPosition().y();
621  phi = tk->innerMomentum().phi();
622  const int charge = tk->charge();
623  x0 = x1 + r * sin(phi) * charge;
624  y0 = y1 - r * cos(phi) * charge;
625 }
T getParameter(std::string const &) const
edm::EDGetTokenT< reco::TrackCaloClusterPtrAssociation > outInTrackSCAssociationCollection_
edm::ESHandle< CaloGeometry > theCaloGeom_
ConversionTrackPairFinder * theTrackPairFinder_
edm::EDGetTokenT< edm::View< reco::CaloCluster > > bcEndcapCollection_
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:131
bool isNonnull() const
Checks for non-null.
Definition: Ref.h:238
TransientVertex run(const std::vector< reco::TransientTrack > &pair)
key_type key() const
Definition: Ptr.h:163
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:525
const std::string metname
virtual std::string explainSelf() const
Definition: Exception.cc:108
void push_back(Ptr< T > const &iPtr)
Definition: PtrVector.h:149
edm::EDGetTokenT< reco::TrackCollection > conversionIOTrackProducer_
edm::ESHandle< TransientTrackBuilder > theTransientTrackBuilder_
bool isValid() const
Tells whether the vertex is valid.
Definition: Vertex.h:71
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
#define nullptr
ConversionVertexFinder * theVertexFinder_
Global3DPoint GlobalPoint
Definition: GlobalPoint.h:10
edm::EDGetTokenT< edm::View< reco::CaloCluster > > scIslandEndcapProducer_
static ConversionAlgorithm algoByName(const std::string &name)
Definition: Conversion.cc:139
void buildCollections(edm::EventSetup const &es, const edm::Handle< edm::View< reco::CaloCluster > > &scHandle, const edm::Handle< edm::View< reco::CaloCluster > > &bcHandle, const edm::Handle< CaloTowerCollection > &hcalTowersHandle, const edm::Handle< reco::TrackCollection > &trkHandle, std::map< std::vector< reco::TransientTrack >, reco::CaloClusterPtr, CompareTwoTracksVectors > &allPairs, reco::ConversionCollection &outputConvPhotonCollection)
Definition: config.py:1
edm::Ptr< CaloCluster > CaloClusterPtr
key_type key() const
Accessor for product key.
Definition: Ref.h:250
std::vector< Conversion > ConversionCollection
collectin of Conversion objects
Definition: ConversionFwd.h:9
ProductID id() const
Accessor for product ID.
Definition: Ref.h:244
void beginRun(edm::Run const &, const edm::EventSetup &es) final
edm::EDGetTokenT< reco::TrackCollection > generalTrackProducer_
math::XYZVectorF toFConverterV(const math::XYZVector &val)
TrackRef persistentTrackRef() const
void cleanCollections(const edm::Handle< edm::View< reco::CaloCluster > > &scHandle, const edm::OrphanHandle< reco::ConversionCollection > &conversionHandle, reco::ConversionCollection &outputCollection)
T sqrt(T t)
Definition: SSEVec.h:19
double p4[4]
Definition: TauolaWrapper.h:92
T z() const
Definition: PV3DBase.h:61
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
edm::EDGetTokenT< CaloTowerCollection > hcalTowers_
Tan< T >::type tan(const T &t)
Definition: Tan.h:22
math::XYZPoint Point
point in the space
Definition: Particle.h:25
std::vector< reco::ConversionRef > solveAmbiguity(const edm::OrphanHandle< reco::ConversionCollection > &conversionHandle, reco::CaloClusterPtr const &sc)
float calculateMinApproachDistance(const reco::TrackRef &track1, const reco::TrackRef &track2)
double energy() const
cluster energy
Definition: CaloCluster.h:148
edm::EDGetTokenT< reco::TrackCollection > conversionOITrackProducer_
edm::EDGetTokenT< reco::TrackCaloClusterPtrAssociation > inOutTrackSCAssociationCollection_
std::vector< math::XYZPointF > find(const std::vector< reco::TransientTrack > &tracks, const edm::Handle< edm::View< reco::CaloCluster > > &bcHandle)
#define end
Definition: vmac.h:39
bool isValid() const
Definition: HandleBase.h:70
double getTowerESum(const reco::Candidate *cand, const std::vector< CaloTowerDetId > *detIdToExclude=0) const
virtual GlobalVector inTesla(const GlobalPoint &gp) const =0
Field value ad specified global point, in Tesla.
edm::ESHandle< MagneticField > theMF_
Conversion * clone() const
returns a clone of the candidate
Definition: Conversion.cc:148
double calculateLikelihood(reco::ConversionRef conversion)
edm::EDGetTokenT< edm::View< reco::CaloCluster > > bcBarrelCollection_
edm::EDGetTokenT< edm::View< reco::CaloCluster > > scHybridBarrelProducer_
T const * product() const
Definition: Handle.h:69
XYZVectorD XYZVector
spatial vector with cartesian internal representation
Definition: Vector3D.h:31
void produce(edm::Event &evt, const edm::EventSetup &es) override
TEveGeoShape * clone(const TEveElement *element, TEveElement *parent)
Definition: eve_macros.cc:135
void setMVAout(const float &mva)
set the value of the TMVA output
Definition: Conversion.h:161
ProductID id() const
Accessor for product ID.
Definition: Ptr.h:158
ConvertedPhotonProducer(const edm::ParameterSet &ps)
void clear()
Clear the PtrVector.
Definition: PtrVectorBase.h:79
#define begin
Definition: vmac.h:32
HLT enums.
T get() const
Definition: EventSetup.h:73
math::XYZPointF toFConverterP(const math::XYZPoint &val)
std::vector< reco::CaloClusterPtr > matchingBC() const
std::map< std::vector< reco::TransientTrack >, reco::CaloClusterPtr, CompareTwoTracksVectors > run(const std::vector< reco::TransientTrack > &outIn, const edm::Handle< reco::TrackCollection > &outInTrkHandle, const edm::Handle< reco::TrackCaloClusterPtrAssociation > &outInTrackSCAssH, const std::vector< reco::TransientTrack > &inOut, const edm::Handle< reco::TrackCollection > &inOutTrkHandle, const edm::Handle< reco::TrackCaloClusterPtrAssociation > &inOutTrackSCAssH)
ConversionLikelihoodCalculator * theLikelihoodCalc_
math::XYZTLorentzVector LorentzVector
Lorentz vector.
Definition: Particle.h:21
def move(src, dest)
Definition: eostools.py:511
Definition: Run.h:45
void getCircleCenter(const reco::TrackRef &tk, double r, double &x0, double &y0)