CMS 3D CMS Logo

EgammaHLTPixelMatchElectronAlgo.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: EgammaHLTAlgos
4 // Class: EgammaHLTPixelMatchElectronAlgo.
5 //
11 //
12 // Original Author: Monica Vazquez Acosta (CERN)
13 //
14 //
17 
22 
27 
30 
33 
36 
38 
40 
41 using namespace edm;
42 using namespace std;
43 using namespace reco;
44 
47  : trackProducer_(iC.consumes(conf.getParameter<edm::InputTag>("TrackProducer"))),
48  gsfTrackProducer_(iC.consumes(conf.getParameter<edm::InputTag>("GsfTrackProducer"))),
49  useGsfTracks_(conf.getParameter<bool>("UseGsfTracks")),
50  bsProducer_(iC.consumes<reco::BeamSpot>(conf.getParameter<edm::InputTag>("BSProducer"))),
51  magneticFieldToken_(iC.esConsumes()),
52  trackerGeometryToken_(iC.esConsumes()) {}
53 
55  //services
56  bool updateField = magneticFieldWatcher_.check(eventSetup);
57  bool updateGeometry = trackerGeometryWatcher_.check(eventSetup);
58 
59  if (updateField) {
61  }
62 
63  if (useGsfTracks_) { //only need the geom and mtsTransform if we are doing gsf tracks
64  if (updateGeometry) {
66  }
67  if (updateField || updateGeometry || !mtsTransform_) {
68  mtsTransform_ = std::make_unique<MultiTrajectoryStateTransform>(trackerGeom_.product(), magField_.product());
69  }
70  }
71 }
72 
74  // get the input
76  if (!useGsfTracks_)
77  e.getByToken(trackProducer_, tracksH);
78 
79  // get the input
81  if (useGsfTracks_)
82  e.getByToken(gsfTrackProducer_, gsfTracksH);
83 
84  //Get the Beam Spot position
85  edm::Handle<reco::BeamSpot> recoBeamSpotHandle;
86  e.getByToken(bsProducer_, recoBeamSpotHandle);
87 
88  // gets its position
89  const BeamSpot::Point& bsPosition = recoBeamSpotHandle->position();
90  Global3DPoint bs(bsPosition.x(), bsPosition.y(), 0);
91  process(tracksH, gsfTracksH, outEle, bs);
92 
93  return;
94 }
95 
98  ElectronCollection& outEle,
99  Global3DPoint& bs) {
100  if (!useGsfTracks_) {
101  for (unsigned int i = 0; i < tracksH->size(); ++i) {
102  const TrackRef trackRef = edm::Ref<TrackCollection>(tracksH, i);
103  edm::RefToBase<TrajectorySeed> seed = trackRef->extra()->seedRef();
104  ElectronSeedRef elseed = seed.castTo<ElectronSeedRef>();
105 
106  edm::RefToBase<CaloCluster> caloCluster = elseed->caloCluster();
107  SuperClusterRef scRef = caloCluster.castTo<SuperClusterRef>();
108 
109  // Get the momentum at vertex (not at the innermost layer)
110  TSCPBuilderNoMaterial tscpBuilder;
111 
113  TrajectoryStateClosestToPoint tscp = tscpBuilder(fts, bs);
114 
115  float scale = scRef->energy() / tscp.momentum().mag();
116 
117  const math::XYZTLorentzVector momentum(
118  tscp.momentum().x() * scale, tscp.momentum().y() * scale, tscp.momentum().z() * scale, scRef->energy());
119 
120  Electron ele(trackRef->charge(), momentum, trackRef->vertex());
121  ele.setSuperCluster(scRef);
122  edm::Ref<TrackCollection> myRef(tracksH, i);
123  ele.setTrack(myRef);
124  outEle.push_back(ele);
125  } // loop over tracks
126  } else {
127  // clean gsf tracks
128  std::vector<unsigned int> flag(gsfTracksH->size(), 0);
129  if (gsfTracksH->empty())
130  return;
131 
132  for (unsigned int i = 0; i < gsfTracksH->size() - 1; ++i) {
133  const GsfTrackRef trackRef1 = edm::Ref<GsfTrackCollection>(gsfTracksH, i);
134  ElectronSeedRef elseed1 = trackRef1->extra()->seedRef().castTo<ElectronSeedRef>();
135  SuperClusterRef scRef1 = elseed1->caloCluster().castTo<SuperClusterRef>();
136 
137  TrajectoryStateOnSurface inTSOS = mtsTransform_->innerStateOnSurface((*trackRef1));
138  TrajectoryStateOnSurface fts = mtsTransform_->extrapolatedState(inTSOS, bs);
139  GlobalVector innMom;
140  float pin1 = trackRef1->pMode();
141  if (fts.isValid()) {
143  pin1 = innMom.mag();
144  }
145 
146  for (unsigned int j = i + 1; j < gsfTracksH->size(); ++j) {
147  const GsfTrackRef trackRef2 = edm::Ref<GsfTrackCollection>(gsfTracksH, j);
148  ElectronSeedRef elseed2 = trackRef2->extra()->seedRef().castTo<ElectronSeedRef>();
149  SuperClusterRef scRef2 = elseed2->caloCluster().castTo<SuperClusterRef>();
150 
151  TrajectoryStateOnSurface inTSOS = mtsTransform_->innerStateOnSurface((*trackRef2));
152  TrajectoryStateOnSurface fts = mtsTransform_->extrapolatedState(inTSOS, bs);
153  GlobalVector innMom;
154  float pin2 = trackRef2->pMode();
155  if (fts.isValid()) {
157  pin2 = innMom.mag();
158  }
159 
160  if (scRef1 == scRef2) {
161  bool isSameLayer = false;
162  bool iGsfInnermostWithLostHits = isInnerMostWithLostHits(trackRef2, trackRef1, isSameLayer);
163 
164  if (iGsfInnermostWithLostHits) {
165  flag[j] = 1;
166  } else if (isSameLayer) {
167  if (fabs((scRef1->energy() / pin1) - 1) < fabs((scRef2->energy() / pin2) - 1))
168  flag[j] = 1;
169  } else {
170  flag[i] = 1;
171  }
172  }
173  }
174  }
175 
176  for (unsigned int i = 0; i < gsfTracksH->size(); ++i) {
177  if (flag[i] == 1)
178  continue;
179 
180  const GsfTrackRef trackRef = edm::Ref<GsfTrackCollection>(gsfTracksH, i);
181  ElectronSeedRef elseed = trackRef->extra()->seedRef().castTo<ElectronSeedRef>();
182  SuperClusterRef scRef = elseed->caloCluster().castTo<SuperClusterRef>();
183 
184  // Get the momentum at vertex (not at the innermost layer)
185  TrajectoryStateOnSurface inTSOS = mtsTransform_->innerStateOnSurface((*trackRef));
186  TrajectoryStateOnSurface fts = mtsTransform_->extrapolatedState(inTSOS, bs);
187  GlobalVector innMom;
189  if (fts.isValid()) {
191  }
192 
193  float scale = scRef->energy() / innMom.mag();
194  const math::XYZTLorentzVector momentum(
195  innMom.x() * scale, innMom.y() * scale, innMom.z() * scale, scRef->energy());
196 
197  Electron ele(trackRef->charge(), momentum, trackRef->vertex());
198  ele.setSuperCluster(scRef);
199  edm::Ref<GsfTrackCollection> myRef(gsfTracksH, i);
200  ele.setGsfTrack(myRef);
201  outEle.push_back(ele);
202  }
203  }
204 }
205 
207  const reco::GsfTrackRef& iGsfTrack,
208  bool& sameLayer) {
209  // define closest using the lost hits on the expectedhitsineer
210  auto nLostHits = nGsfTrack->missingInnerHits();
211  auto iLostHits = iGsfTrack->missingInnerHits();
212 
213  if (nLostHits != iLostHits) {
214  return (nLostHits > iLostHits);
215  } else {
216  sameLayer = true;
217  return false;
218  }
219 }
ESGetTokenH3DDVariant esConsumes(std::string const &Record, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
const Point & position() const
position
Definition: BeamSpot.h:59
T z() const
Definition: PV3DBase.h:61
void process(edm::Handle< reco::TrackCollection > tracksH, edm::Handle< reco::GsfTrackCollection > gsfTracksH, reco::ElectronCollection &outEle, Global3DPoint &bs)
math::XYZPoint Point
point in the space
Definition: BeamSpot.h:27
XYZTLorentzVectorD XYZTLorentzVector
Lorentz vector with cylindrical internal representation using pseudorapidity.
Definition: LorentzVector.h:29
T x() const
Definition: PV3DBase.h:59
T y() const
Definition: PV3DBase.h:60
edm::ESHandle< MagneticField > magField_
T const * product() const
Definition: ESHandle.h:86
edm::EDGetTokenT< reco::GsfTrackCollection > gsfTrackProducer_
void run(edm::Event &, reco::ElectronCollection &)
edm::ESWatcher< TrackerDigiGeometryRecord > trackerGeometryWatcher_
EgammaHLTPixelMatchElectronAlgo(const edm::ParameterSet &conf, edm::ConsumesCollector &&iC)
T mag() const
Definition: PV3DBase.h:64
bool isInnerMostWithLostHits(const reco::GsfTrackRef &, const reco::GsfTrackRef &, bool &)
void setupES(const edm::EventSetup &setup)
edm::ESWatcher< IdealMagneticFieldRecord > magneticFieldWatcher_
std::vector< Electron > ElectronCollection
collectin of Electron objects
Definition: ElectronFwd.h:9
edm::EDGetTokenT< reco::BeamSpot > bsProducer_
edm::EDGetTokenT< reco::TrackCollection > trackProducer_
const edm::ESGetToken< TrackerGeometry, TrackerDigiGeometryRecord > trackerGeometryToken_
const edm::ESGetToken< MagneticField, IdealMagneticFieldRecord > magneticFieldToken_
bool check(const edm::EventSetup &iSetup)
Definition: ESWatcher.h:57
std::unique_ptr< MultiTrajectoryStateTransform > mtsTransform_
fixed size matrix
HLT enums.
edm::ESHandle< TrackerGeometry > trackerGeom_
FreeTrajectoryState innerFreeState(const reco::Track &tk, const MagneticField *field, bool withErr=true)
bool momentumFromModeCartesian(TrajectoryStateOnSurface const &tsos, GlobalVector &momentum)