CMS 3D CMS Logo

TwoBodyDecayMomConstraintProducer.cc
Go to the documentation of this file.
1 
10 #include <memory>
11 
18 
21 
24 
26 
32 
33 // // debug
34 // #include <map>
35 // #include "TH1F.h"
36 // #include "TFile.h"
37 // #include "TLorentzVector.h"
38 // #include "Alignment/TwoBodyDecay/interface/TwoBodyDecayModel.h"
39 
41 public:
43  ~TwoBodyDecayMomConstraintProducer() override = default;
44 
45 private:
46  void produce(edm::StreamID streamid, edm::Event&, const edm::EventSetup&) const override;
47 
48  std::pair<double, double> momentaAtVertex(const TwoBodyDecay& tbd) const;
49 
50  std::pair<double, double> momentaAtInnermostSurface(const TwoBodyDecay& tbd,
51  const std::vector<reco::TransientTrack>& ttracks,
52  const MagneticField* magField) const;
53 
54  std::pair<bool, TrajectoryStateOnSurface> innermostState(const reco::TransientTrack& ttrack) const;
55  bool match(const TrajectoryStateOnSurface& newTsos, const TrajectoryStateOnSurface& oldTsos) const;
56 
59 
61 
62  const double primaryMass_;
63  const double primaryWidth_;
64  const double secondaryMass_;
65 
66  const double sigmaPositionCutValue_;
67  const double chi2CutValue_;
68  const double fixedMomentumError_;
69 
72 
73  static MomentumForRefitting momentumForRefittingFromString(std::string momentumForRefittingString);
74 
77 
80  // // debug
81  // std::map<std::string, TH1F*> histos_;
82 };
83 
85  : srcTag_(iConfig.getParameter<edm::InputTag>("src")),
86  bsSrcTag_(iConfig.getParameter<edm::InputTag>("beamSpot")),
87  tbdFitter_(iConfig),
88  primaryMass_(iConfig.getParameter<double>("primaryMass")),
89  primaryWidth_(iConfig.getParameter<double>("primaryWidth")),
90  secondaryMass_(iConfig.getParameter<double>("secondaryMass")),
91  sigmaPositionCutValue_(iConfig.getParameter<double>("sigmaPositionCut")),
92  chi2CutValue_(iConfig.getParameter<double>("chi2Cut")),
93  fixedMomentumError_(iConfig.getParameter<double>("fixedMomentumError")),
94  momentumForRefitting_(momentumForRefittingFromString(iConfig.getParameter<std::string>("momentumForRefitting"))),
95  magFieldToken_(esConsumes()),
96  trackingGeometryToken_(esConsumes()) {
97  trackCollToken_ = consumes<reco::TrackCollection>(edm::InputTag(srcTag_));
98  bsToken_ = consumes<reco::BeamSpot>(edm::InputTag(bsSrcTag_));
99 
100  produces<std::vector<MomentumConstraint> >();
101  produces<TrackMomConstraintAssociationCollection>();
102 
103  // //debug
104  // histos_["deltaEta1"] = new TH1F( "deltaEta1", "deltaEta1", 200, -1., 1. );
105  // histos_["deltaP1"] = new TH1F( "deltaP1", "deltaP1", 200, -50., 50. );
106 
107  // histos_["deltaEta2"] = new TH1F( "deltaEta2", "deltaEta2", 200, -1., 1. );
108  // histos_["deltaP2"] = new TH1F( "deltaP2", "deltaP2", 200, -50., 50. );
109 }
110 
113  const edm::EventSetup& iSetup) const {
114  using namespace edm;
115 
117  iEvent.getByToken(trackCollToken_, trackColl);
118 
120  iEvent.getByToken(bsToken_, beamSpot);
121 
122  auto const* magField = &iSetup.getData(magFieldToken_);
123 
124  auto trackingGeometry = iSetup.getHandle(trackingGeometryToken_);
125 
126  edm::RefProd<std::vector<MomentumConstraint> > rPairs = iEvent.getRefBeforePut<std::vector<MomentumConstraint> >();
127  std::unique_ptr<std::vector<MomentumConstraint> > pairs(new std::vector<MomentumConstraint>);
128  std::unique_ptr<TrackMomConstraintAssociationCollection> output(
130 
131  if (trackColl->size() == 2) {
134 
136  std::vector<reco::TransientTrack> ttracks(2);
137  ttracks[0] = reco::TransientTrack(reco::TrackRef(trackColl, 0), magField);
138  ttracks[0].setTrackingGeometry(trackingGeometry);
139  ttracks[1] = reco::TransientTrack(reco::TrackRef(trackColl, 1), magField);
140  ttracks[1].setTrackingGeometry(trackingGeometry);
141 
143  TwoBodyDecay tbd = tbdFitter_.estimate(ttracks, vm);
144 
145  if (!tbd.isValid() or (tbd.chi2() > chi2CutValue_))
146  return;
147 
148  std::pair<double, double> fitMomenta;
150  fitMomenta = momentaAtVertex(tbd);
152  fitMomenta = momentaAtInnermostSurface(tbd, ttracks, magField);
153  } // no other possibility!!!
154 
155  if ((fitMomenta.first > 0.) and (fitMomenta.second > 0.)) {
156  // produce constraint for first track
157  MomentumConstraint constraint1(fitMomenta.first, fixedMomentumError_);
158  pairs->push_back(constraint1);
159  output->insert(reco::TrackRef(trackColl, 0), edm::Ref<std::vector<MomentumConstraint> >(rPairs, 0));
160 
161  // produce constraint for second track
162  MomentumConstraint constraint2(fitMomenta.second, fixedMomentumError_);
163  pairs->push_back(constraint2);
164  output->insert(reco::TrackRef(trackColl, 1), edm::Ref<std::vector<MomentumConstraint> >(rPairs, 1));
165  }
166 
167  // // debug
168  // if ( tbd.isValid() and ( fitMomenta.first > 0. ) and ( fitMomenta.second > 0. ) ) {
169  // TwoBodyDecayModel model;
170  // const std::pair< AlgebraicVector, AlgebraicVector > fitMomenta = model.cartesianSecondaryMomenta( tbd );
171 
172  // TLorentzVector recoMomentum1( ttracks[0].track().px(), ttracks[0].track().py(), ttracks[0].track().pz(),
173  // sqrt((ttracks[0].track().p()*ttracks[0].track().p())+0.105658*0.105658) );
174  // TLorentzVector fitMomentum1( fitMomenta.first[0], fitMomenta.first[1], fitMomenta.first[2],
175  // sqrt( fitMomenta.first.normsq()+0.105658*0.105658) );
176  // histos_["deltaP1"]->Fill( recoMomentum1.P() - fitMomentum1.P() );
177  // histos_["deltaEta1"]->Fill( recoMomentum1.Eta() - fitMomentum1.Eta() );
178 
179  // TLorentzVector recoMomentum2( ttracks[1].track().px(), ttracks[1].track().py(), ttracks[1].track().pz(),
180  // sqrt((ttracks[1].track().p()*ttracks[1].track().p())+0.105658*0.105658) );
181  // TLorentzVector fitMomentum2( fitMomenta.second[0], fitMomenta.second[1], fitMomenta.second[2],
182  // sqrt( fitMomenta.second.normsq()+0.105658*0.105658) );
183  // histos_["deltaP2"]->Fill( recoMomentum2.P() - fitMomentum2.P() );
184  // histos_["deltaEta2"]->Fill( recoMomentum2.Eta() - fitMomentum2.Eta() );
185  // }
186  }
187 
188  iEvent.put(std::move(pairs));
189  iEvent.put(std::move(output));
190 }
191 
192 std::pair<double, double> TwoBodyDecayMomConstraintProducer::momentaAtVertex(const TwoBodyDecay& tbd) const {
193  // construct global trajectory parameters at the starting point
195  std::pair<AlgebraicVector, AlgebraicVector> secondaryMomenta = tbdDecayModel.cartesianSecondaryMomenta(tbd);
196 
197  return std::make_pair(secondaryMomenta.first.norm(), secondaryMomenta.second.norm());
198 }
199 
201  const TwoBodyDecay& tbd, const std::vector<reco::TransientTrack>& ttracks, const MagneticField* magField) const {
202  std::pair<double, double> result = std::make_pair(-1., -1.);
203 
205  std::pair<bool, TrajectoryStateOnSurface> oldInnermostState1 = innermostState(ttracks[0]);
206  std::pair<bool, TrajectoryStateOnSurface> oldInnermostState2 = innermostState(ttracks[1]);
207  if (!oldInnermostState1.second.isValid() || !oldInnermostState2.second.isValid())
208  return result;
209 
211  TwoBodyDecayTrajectoryState::TsosContainer trackTsos(oldInnermostState1.second, oldInnermostState2.second);
212  TwoBodyDecayTrajectoryState tbdTrajState(trackTsos, tbd, secondaryMass_, magField, true);
213  if (!tbdTrajState.isValid())
214  return result;
215 
217  bool match1 = match(tbdTrajState.trajectoryStates(true).first, oldInnermostState1.second);
218  bool match2 = match(tbdTrajState.trajectoryStates(true).second, oldInnermostState2.second);
219  if (!match1 || !match2)
220  return result;
221 
222  result = std::make_pair(fabs(1. / tbdTrajState.trajectoryStates(true).first.localParameters().qbp()),
223  fabs(1. / tbdTrajState.trajectoryStates(true).second.localParameters().qbp()));
224  return result;
225 }
226 
227 std::pair<bool, TrajectoryStateOnSurface> TwoBodyDecayMomConstraintProducer::innermostState(
228  const reco::TransientTrack& ttrack) const {
229  double outerR = ttrack.outermostMeasurementState().globalPosition().perp();
230  double innerR = ttrack.innermostMeasurementState().globalPosition().perp();
231  bool insideOut = (outerR > innerR);
232  return std::make_pair(insideOut, insideOut ? ttrack.innermostMeasurementState() : ttrack.outermostMeasurementState());
233 }
234 
236  const TrajectoryStateOnSurface& oldTsos) const {
237  LocalPoint lp1 = newTsos.localPosition();
238  LocalPoint lp2 = oldTsos.localPosition();
239 
240  double deltaX = lp1.x() - lp2.x();
241  double deltaY = lp1.y() - lp2.y();
242 
243  return ((fabs(deltaX) < sigmaPositionCutValue_) && (fabs(deltaY) < sigmaPositionCutValue_));
244 }
245 
248  if (strMomentumForRefitting == "atVertex") {
249  return atVertex;
250  } else if (strMomentumForRefitting == "atInnermostSurface") {
251  return atInnermostSurface;
252  } else {
253  throw cms::Exception("TwoBodyDecayMomConstraintProducer") << "value of config variable 'momentumForRefitting': "
254  << "has to be 'atVertex' or 'atInnermostSurface'";
255  }
256 }
257 
258 //define this as a plug-in
ESGetTokenH3DDVariant esConsumes(std::string const &Record, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
T perp() const
Definition: PV3DBase.h:69
std::pair< double, double > momentaAtVertex(const TwoBodyDecay &tbd) const
~TwoBodyDecayMomConstraintProducer() override=default
std::pair< double, double > momentaAtInnermostSurface(const TwoBodyDecay &tbd, const std::vector< reco::TransientTrack > &ttracks, const MagneticField *magField) const
void produce(edm::StreamID streamid, edm::Event &, const edm::EventSetup &) const override
std::pair< bool, TrajectoryStateOnSurface > innermostState(const reco::TransientTrack &ttrack) const
T x() const
Definition: PV3DBase.h:59
T y() const
Definition: PV3DBase.h:60
int iEvent
Definition: GenABIO.cc:224
GlobalPoint globalPosition() const
edm::EDGetTokenT< reco::TrackCollection > trackCollToken_
TwoBodyDecayMomConstraintProducer(const edm::ParameterSet &)
TrajectoryStateOnSurface outermostMeasurementState() const
The Signals That Services Can Subscribe To This is based on ActivityRegistry and is current per Services can connect to the signals distributed by the ActivityRegistry in order to monitor the activity of the application Each possible callback has some defined which we here list in angle e< void, edm::EventID const &, edm::Timestamp const & > We also list in braces which AR_WATCH_USING_METHOD_ is used for those or
Definition: Activities.doc:12
const edm::ESGetToken< GlobalTrackingGeometry, GlobalTrackingGeometryRecord > trackingGeometryToken_
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
TrajectoryStateOnSurface innermostMeasurementState() const
const edm::ESGetToken< MagneticField, IdealMagneticFieldRecord > magFieldToken_
bool getData(T &iHolder) const
Definition: EventSetup.h:122
ESHandle< T > getHandle(const ESGetToken< T, R > &iToken) const
Definition: EventSetup.h:151
static MomentumForRefitting momentumForRefittingFromString(std::string momentumForRefittingString)
edm::AssociationMap< edm::OneToOne< reco::TrackCollection, std::vector< MomentumConstraint > > > TrackMomConstraintAssociationCollection
double chi2(void) const
Definition: TwoBodyDecay.h:44
bool match(const TrajectoryStateOnSurface &newTsos, const TrajectoryStateOnSurface &oldTsos) const
std::pair< TrajectoryStateOnSurface, TrajectoryStateOnSurface > TsosContainer
HLT enums.
bool isValid(void) const
Definition: TwoBodyDecay.h:46
const TsosContainer & trajectoryStates(bool useRefittedState=true) const
virtual const TwoBodyDecay estimate(const std::vector< reco::TransientTrack > &tracks, const TwoBodyDecayVirtualMeasurement &vm) const
edm::EDGetTokenT< reco::BeamSpot > bsToken_
def move(src, dest)
Definition: eostools.py:511
const std::pair< AlgebraicVector, AlgebraicVector > cartesianSecondaryMomenta(const AlgebraicVector &param)