CMS 3D CMS Logo

BDHadronTrackMonitoringAnalyzer.cc
Go to the documentation of this file.
2 
4  const reco::PFCandidate *pfcand = dynamic_cast<const reco::PFCandidate *>(cnd.get());
5 
6  if ((std::abs(pfcand->pdgId()) == 11 || pfcand->pdgId() == 22) && pfcand->gsfTrackRef().isNonnull() &&
7  pfcand->gsfTrackRef().isAvailable())
8  return reco::TrackBaseRef(pfcand->gsfTrackRef());
9  else if (pfcand->trackRef().isNonnull() && pfcand->trackRef().isAvailable())
10  return reco::TrackBaseRef(pfcand->trackRef());
11  else
12  return reco::TrackBaseRef();
13 }
14 
15 // ---------- Static member declaration -----------
16 const std::vector<std::string> BDHadronTrackMonitoringAnalyzer::TrkHistCat = {
17  "BCWeakDecay", "BWeakDecay", "CWeakDecay", "PU", "Other", "Fake"};
18 
19 // ---------- Constructor -----------
21  : distJetAxis_(pSet.getParameter<double>("distJetAxisCut")),
22  decayLength_(pSet.getParameter<double>("decayLengthCut")),
23  minJetPt_(pSet.getParameter<double>("minJetPt")),
24  maxJetEta_(pSet.getParameter<double>("maxJetEta")),
25  ipTagInfos_(pSet.getParameter<std::string>("ipTagInfos")),
26  PatJetSrc_(pSet.getParameter<InputTag>("PatJetSource")),
27  TrackSrc_(pSet.getParameter<InputTag>("TrackSource")),
28  PVSrc_(pSet.getParameter<InputTag>("PrimaryVertexSource")),
29  ClusterTPMapSrc_(pSet.getParameter<InputTag>("clusterTPMap")),
30  classifier_(pSet, consumesCollector())
31 
32 {
33  PatJetCollectionTag_ = consumes<pat::JetCollection>(PatJetSrc_);
34  TrackCollectionTag_ = consumes<reco::TrackCollection>(TrackSrc_);
35  PrimaryVertexColl_ = consumes<reco::VertexCollection>(PVSrc_);
36  clusterTPMapToken_ = consumes<ClusterTPAssociation>(ClusterTPMapSrc_);
37  // TrkHistCat = {"BCWeakDecay", "BWeakDecay", "CWeakDecay", "PU", "Other",
38  // "Fake"};
39 }
40 
41 // ---------- BookHistograms -----------
42 
44  edm::Run const &run,
45  edm::EventSetup const &es) {
46  ibook.setCurrentFolder("BDHadronTracks/JetContent");
47  //
48  // Book all histograms.
49  //
51 
52  nTrkAll_bjet = ibook.book1D(
53  "nTrkAll_bjet", "Number of selected tracks in b jets;number of selected tracks;jets", 16, -0.5, 15.5);
54 
55  nTrkAll_cjet = ibook.book1D(
56  "nTrkAll_cjet", "Number of selected tracks in c jets;number of selected tracks;jets", 16, -0.5, 15.5);
57 
58  nTrkAll_dusgjet = ibook.book1D(
59  "nTrkAll_dusgjet", "Number of selected tracks in dusg jets;number of selected tracks;jets", 16, -0.5, 15.5);
60 
61  // Loop over different Track History Categories
62  for (unsigned int i = 0; i < TrkHistCat.size(); i++) {
63  ibook.setCurrentFolder("BDHadronTracks/JetContent");
64  // b jets
65  nTrk_bjet[i] = ibook.book1D("nTrk_bjet_" + TrkHistCat[i],
66  "Number of selected tracks in b jets (" + TrkHistCat[i] +
67  ");number of selected tracks (" + TrkHistCat[i] + ");jets",
68  16,
69  -0.5,
70  15.5);
71 
72  // c jets
73  nTrk_cjet[i] = ibook.book1D("nTrk_cjet_" + TrkHistCat[i],
74  "Number of selected tracks in c jets (" + TrkHistCat[i] +
75  ");number of selected tracks (" + TrkHistCat[i] + ");jets",
76  16,
77  -0.5,
78  15.5);
79 
80  // dusg jets
81  nTrk_dusgjet[i] = ibook.book1D("nTrk_dusgjet_" + TrkHistCat[i],
82  "Number of selected tracks in dusg jets (" + TrkHistCat[i] +
83  ");number of selected tracks (" + TrkHistCat[i] + ");jets",
84  16,
85  -0.5,
86  15.5);
87 
88  ibook.setCurrentFolder("BDHadronTracks/TrackInfo");
89  // track properties for all flavours combined
90  TrkPt_alljets[i] = ibook.book1D("TrkPt_" + TrkHistCat[i],
91  "Track pT (" + TrkHistCat[i] + ");track p_{T} (" + TrkHistCat[i] + ");tracks",
92  30,
93  0,
94  100);
95  TrkEta_alljets[i] = ibook.book1D("TrkEta_" + TrkHistCat[i],
96  "Track #eta (" + TrkHistCat[i] + ");track #eta (" + TrkHistCat[i] + ");tracks",
97  30,
98  -2.5,
99  2.5);
100  TrkPhi_alljets[i] = ibook.book1D("TrkPhi_" + TrkHistCat[i],
101  "Track #phi (" + TrkHistCat[i] + ");track #phi (" + TrkHistCat[i] + ");tracks",
102  30,
103  -3.15,
104  3.15);
105  TrkDxy_alljets[i] = ibook.book1D("TrkDxy_" + TrkHistCat[i],
106  "Track dxy (" + TrkHistCat[i] + ");track dxy (" + TrkHistCat[i] + ");tracks",
107  30,
108  -0.1,
109  0.1);
110  TrkDz_alljets[i] = ibook.book1D("TrkDz_" + TrkHistCat[i],
111  "Track dz (" + TrkHistCat[i] + ");track dz (" + TrkHistCat[i] + ");tracks",
112  30,
113  -0.1,
114  0.1);
115  TrkHitAll_alljets[i] = ibook.book1D(
116  "TrkHitAll_" + TrkHistCat[i],
117  "Number of tracker hits (" + TrkHistCat[i] + ");track number of all hits (" + TrkHistCat[i] + ");tracks",
118  31,
119  -0.5,
120  30.5);
121  TrkHitStrip_alljets[i] = ibook.book1D(
122  "TrkHitStrip_" + TrkHistCat[i],
123  "Number of strip hits (" + TrkHistCat[i] + ");track number of strip hits (" + TrkHistCat[i] + ");tracks",
124  31,
125  -0.5,
126  30.5);
127  TrkHitPixel_alljets[i] = ibook.book1D(
128  "TrkHitPixel_" + TrkHistCat[i],
129  "Number of pixel hits (" + TrkHistCat[i] + ");track number of pixel hits (" + TrkHistCat[i] + ");tracks",
130  9,
131  -0.5,
132  8.5);
133 
134  ibook.setCurrentFolder("BDHadronTracks/TrackTruthInfo");
135  if (i < 5) { // Fakes (i == 5) have no truth by definition!
137  ibook.book1D("TrkTruthPt_" + TrkHistCat[i],
138  "Track pT (" + TrkHistCat[i] + " Truth);track p_{T} (" + TrkHistCat[i] + " Truth);tracks",
139  30,
140  0,
141  100);
143  ibook.book1D("TrkTruthEta_" + TrkHistCat[i],
144  "Track #eta (" + TrkHistCat[i] + " Truth);track #eta (" + TrkHistCat[i] + " Truth);tracks",
145  30,
146  -2.5,
147  2.5);
149  ibook.book1D("TrkTruthPhi_" + TrkHistCat[i],
150  "Track #phi (" + TrkHistCat[i] + " Truth);track #phi (" + TrkHistCat[i] + " Truth);tracks",
151  30,
152  -3.15,
153  3.15);
155  ibook.book1D("TrkTruthDxy_" + TrkHistCat[i],
156  "Track dxy (" + TrkHistCat[i] + " Truth);track dxy (" + TrkHistCat[i] + " Truth);tracks",
157  30,
158  -0.1,
159  0.1);
161  ibook.book1D("TrkTruthDz_" + TrkHistCat[i],
162  "Track dz (" + TrkHistCat[i] + " Truth);track dz (" + TrkHistCat[i] + " Truth);tracks",
163  30,
164  -0.1,
165  0.1);
167  ibook.book1D("TrkTruthHitAll_" + TrkHistCat[i],
168  "Number of tracker hits (" + TrkHistCat[i] + " Truth);track number of all hits (" +
169  TrkHistCat[i] + " Truth);tracks",
170  31,
171  -0.5,
172  30.5);
174  ibook.book1D("TrkTruthHitStrip_" + TrkHistCat[i],
175  "Number of strip hits (" + TrkHistCat[i] + " Truth);track number of strip hits (" +
176  TrkHistCat[i] + " Truth);tracks",
177  31,
178  -0.5,
179  30.5);
181  ibook.book1D("TrkTruthHitPixel_" + TrkHistCat[i],
182  "Number of pixel hits (" + TrkHistCat[i] + " Truth);track number of pixel hits (" +
183  TrkHistCat[i] + " Truth);tracks",
184  9,
185  -0.5,
186  8.5);
187  }
188  }
189 }
190 
191 // ---------- Destructor -----------
192 
194 
195 // ---------- Analyze -----------
196 // This is needed to get a TrackingParticle --> Cluster match (instead of
197 // Cluster-->TP)
198 using P = std::pair<OmniClusterRef, TrackingParticleRef>;
199 bool compare(const P &i, const P &j) { return i.second.index() > j.second.index(); }
200 
203  iEvent.getByToken(PatJetCollectionTag_, patJetsColl);
204 
206  iEvent.getByToken(TrackCollectionTag_, tracksHandle);
207 
208  edm::Handle<ClusterTPAssociation> pCluster2TPListH;
209  iEvent.getByToken(clusterTPMapToken_, pCluster2TPListH);
210  const ClusterTPAssociation &clusterToTPMap = *pCluster2TPListH;
211 
213  iSetup.get<TransientTrackRecord>().get("TransientTrackBuilder", trackBuilder);
214 
215  classifier_.newEvent(iEvent, iSetup);
216 
217  // -----Primary Vertex-----
218  const reco::Vertex *pv;
219 
221  iEvent.getByToken(PrimaryVertexColl_, primaryVertex);
222 
223  bool pvFound = (!primaryVertex->empty());
224  if (pvFound) {
225  pv = &(*primaryVertex->begin());
226  } else {
228  e(0, 0) = 0.0015 * 0.0015;
229  e(1, 1) = 0.0015 * 0.0015;
230  e(2, 2) = 15. * 15.;
231  reco::Vertex::Point p(0, 0, 0);
232  pv = new reco::Vertex(p, e, 1, 1, 1);
233  }
234  // -----------------------
235 
236  // -------- Loop Over Jets ----------
237  for (pat::JetCollection::const_iterator jet = patJetsColl->begin(); jet != patJetsColl->end(); ++jet) {
238  if (jet->pt() < minJetPt_ || std::fabs(jet->eta()) > maxJetEta_)
239  continue;
240 
241  unsigned int flav = abs(jet->hadronFlavour());
242 
243  // std::cout << "patJet collection has pfImpactParameterTagInfo?: " <<
244  // jet->hasTagInfo("pfImpactParameter") << std::endl;
245  const CandIPTagInfo *trackIpTagInfo = jet->tagInfoCandIP(ipTagInfos_);
246  const std::vector<edm::Ptr<reco::Candidate>> &selectedTracks(trackIpTagInfo->selectedTracks());
247 
248  unsigned int nseltracks = 0;
249  std::vector<int> nseltracksCat(TrkHistCat.size(),
250  0); // following the order of TrkHistCat
251 
252  unsigned int nTrackSize = selectedTracks.size(); // number of tracks from IPInfos to loop over
253  // -------- Loop Over (selected) Tracks ----------
254  for (unsigned int itt = 0; itt < nTrackSize; ++itt) {
255  const TrackBaseRef ptrackRef = toTrackRef(selectedTracks[itt]);
256  const reco::Track *ptrackPtr = reco::btag::toTrack(ptrackRef);
257  const reco::Track &ptrack = *ptrackPtr;
258 
259  reco::TransientTrack transientTrack = trackBuilder->build(ptrackPtr);
260  GlobalVector direction(jet->px(), jet->py(), jet->pz());
261 
262  Double_t distJetAxis = IPTools::jetTrackDistance(transientTrack, direction, *pv).second.value();
263 
264  Double_t decayLength = 999;
265  TrajectoryStateOnSurface closest =
266  IPTools::closestApproachToJet(transientTrack.impactPointState(), *pv, direction, transientTrack.field());
267  if (closest.isValid())
268  decayLength = (closest.globalPosition() - RecoVertex::convertPos(pv->position())).mag();
269  else
270  decayLength = 999;
271 
272  // extra cut ons the tracks
273  if (std::fabs(distJetAxis) > distJetAxis_ || decayLength > decayLength_) {
274  continue;
275  }
276  nseltracks += 1; // if it passed these cuts, nselectedtracks +1
277 
279 
280  double TrkPt = ptrack.pt();
281  double TrkEta = ptrack.eta();
282  double TrkPhi = ptrack.phi();
283  double TrkDxy = ptrack.dxy(pv->position());
284  double TrkDz = ptrack.dz(pv->position());
285  int TrknHitAll = ptrack.numberOfValidHits();
286  int TrknHitPixel = ptrack.hitPattern().numberOfValidPixelHits();
287  int TrknHitStrip = ptrack.hitPattern().numberOfValidStripHits();
288 
289  double TrkTruthPt = -99;
290  double TrkTruthEta = -99;
291  double TrkTruthPhi = -99;
292  double TrkTruthDxy = -1;
293  double TrkTruthDz = -1;
294  int TrkTruthnHitAll = -1;
295  int TrkTruthnHitPixel = -1;
296  int TrkTruthnHitStrip = -1;
297 
298  // Get corresponding Trackingparticle
299  std::pair<TrackingParticleRef, double> res = classifier_.history().getMatchedTrackingParticle();
300  TrackingParticleRef tpr = res.first;
301  double quality_tpr = res.second;
302 
303  // Match TP to hit-cluster (re-ordering according to TP rather than
304  // clusters and look for equal_range of a given tpr)
305  auto clusterTPmap = clusterToTPMap.map();
306  std::sort(clusterTPmap.begin(), clusterTPmap.end(), compare);
307  auto clusterRange =
308  std::equal_range(clusterTPmap.begin(), clusterTPmap.end(), std::make_pair(OmniClusterRef(), tpr), compare);
309  if (quality_tpr != 0) {
310  TrkTruthPt = tpr->pt();
311  TrkTruthEta = tpr->eta();
312  TrkTruthPhi = tpr->phi();
313 
314  const TrackingParticle::Point &vertex_pv = pv->position();
315  TrackingParticle::Point vertex_tpr = tpr->vertex();
316  TrackingParticle::Vector momentum_tpr = tpr->momentum();
317  TrkTruthDxy = (-(vertex_tpr.x() - vertex_pv.x()) * momentum_tpr.y() +
318  (vertex_tpr.y() - vertex_pv.y()) * momentum_tpr.x()) /
319  tpr->pt();
320  TrkTruthDz = (vertex_tpr.z() - vertex_pv.z()) - ((vertex_tpr.x() - vertex_pv.x()) * momentum_tpr.x() +
321  (vertex_tpr.y() - vertex_pv.y()) * momentum_tpr.y()) /
322  sqrt(momentum_tpr.perp2()) * momentum_tpr.z() /
323  sqrt(momentum_tpr.perp2());
324 
325  TrkTruthnHitAll = 0;
326  TrkTruthnHitPixel = 0;
327  TrkTruthnHitStrip = 0;
328  if (clusterRange.first != clusterRange.second) {
329  for (auto ip = clusterRange.first; ip != clusterRange.second; ++ip) {
330  const OmniClusterRef &cluster = ip->first;
331  if (cluster.isPixel() && cluster.isValid()) {
332  TrkTruthnHitPixel += 1;
333  }
334  if (cluster.isStrip() && cluster.isValid()) {
335  TrkTruthnHitStrip += 1;
336  }
337  }
338  }
339  TrkTruthnHitAll = TrkTruthnHitPixel + TrkTruthnHitStrip;
340  }
341 
342  // ----------- Filling the correct histograms based on jet flavour and
343  // Track history Category --------
344 
345  // BCWeakDecay
347  theFlag[TrackCategories::CWeakDecay]) {
357  if (quality_tpr != 0) {
366  }
367  }
368  // BWeakDecay
369  else if (theFlag[TrackCategories::SignalEvent] && theFlag[TrackCategories::BWeakDecay] &&
370  !theFlag[TrackCategories::CWeakDecay]) {
380  if (quality_tpr != 0) {
389  }
390  }
391  // CWeakDecay
392  else if (theFlag[TrackCategories::SignalEvent] && !theFlag[TrackCategories::BWeakDecay] &&
393  theFlag[TrackCategories::CWeakDecay]) {
403  if (quality_tpr != 0) {
412  }
413  }
414  // PU
415  else if (!theFlag[TrackCategories::SignalEvent] && !theFlag[TrackCategories::Fake]) {
416  nseltracksCat[BDHadronTrackMonitoringAnalyzer::PU] += 1;
425  if (quality_tpr != 0) {
434  }
435  }
436  // Other
437  else if (theFlag[TrackCategories::SignalEvent] && !theFlag[TrackCategories::BWeakDecay] &&
438  !theFlag[TrackCategories::CWeakDecay]) {
439  nseltracksCat[BDHadronTrackMonitoringAnalyzer::Other] += 1;
448  if (quality_tpr != 0) {
457  }
458  }
459  // Fake
460  else if (!theFlag[TrackCategories::SignalEvent] && theFlag[TrackCategories::Fake]) {
461  nseltracksCat[BDHadronTrackMonitoringAnalyzer::Fake] += 1;
470  // NO TRUTH FOR FAKES!!!
471  }
472  }
473  // -------- END Loop Over (selected) Tracks ----------
474  // Still have to fill some jet-flavour specific variables
475  if (flav == 5) {
476  nTrkAll_bjet->Fill(nseltracks);
477  for (unsigned int i = 0; i < TrkHistCat.size(); i++) {
478  nTrk_bjet[i]->Fill(nseltracksCat[i]);
479  }
480  } else if (flav == 4) {
481  nTrkAll_cjet->Fill(nseltracks);
482  for (unsigned int i = 0; i < TrkHistCat.size(); i++) {
483  nTrk_cjet[i]->Fill(nseltracksCat[i]);
484  }
485  } else {
486  nTrkAll_dusgjet->Fill(nseltracks);
487  for (unsigned int i = 0; i < TrkHistCat.size(); i++) {
488  nTrk_dusgjet[i]->Fill(nseltracksCat[i]);
489  }
490  }
491  }
492  // -------- END Loop Over Jets ----------
493 
494  if (!pvFound) {
495  delete pv;
496  }
497 }
498 
499 // define this as a plug-in
bool isAvailable() const
Definition: Ref.h:575
reco::Vertex::Point convertPos(const GlobalPoint &p)
int pdgId() const final
PDG identifier.
bool isStrip() const
bool compare(const P &i, const P &j)
bool isNonnull() const
Checks for non-null.
Definition: Ref.h:251
edm::EDGetTokenT< ClusterTPAssociation > clusterTPMapToken_
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:517
T const * get() const
Returns C++ pointer to the item.
Definition: Ptr.h:159
TStyle * setTDRStyle()
Definition: Tools.cc:346
edm::EDGetTokenT< reco::TrackCollection > TrackCollectionTag_
edm::EDGetTokenT< reco::VertexCollection > PrimaryVertexColl_
reco::TransientTrack build(const reco::Track *p) const
int numberOfValidStripHits() const
Definition: HitPattern.h:931
math::Error< dimension >::type Error
covariance error matrix (3x3)
Definition: Vertex.h:43
GlobalPoint globalPosition() const
double phi() const
azimuthal angle of momentum vector
Definition: TrackBase.h:678
TrajectoryStateOnSurface closestApproachToJet(const TrajectoryStateOnSurface &state, const reco::Vertex &vertex, const GlobalVector &aJetDirection, const MagneticField *field)
Definition: IPTools.cc:177
const MagneticField * field() const
TrackHistory const & history() const
Returns a reference to the track history used in the classification.
BDHadronTrackMonitoringAnalyzer(const edm::ParameterSet &pSet)
const reco::Track * toTrack(const reco::TrackBaseRef &t)
Definition: IPTagInfo.h:24
const Point & position() const
position
Definition: Vertex.h:109
Definition: Electron.h:6
std::pair< double, Measurement1D > jetTrackDistance(const reco::TransientTrack &track, const GlobalVector &direction, const reco::Vertex &vertex)
Definition: IPTools.cc:200
void Fill(long long x)
reco::TrackRef trackRef() const
Definition: PFCandidate.cc:442
edm::EDGetTokenT< pat::JetCollection > PatJetCollectionTag_
math::XYZPointD Point
point in the space
bool isValid() const
TrackClassifier const & evaluate(reco::TrackBaseRef const &)
Classify the RecoTrack in categories.
int iEvent
Definition: GenABIO.cc:224
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
double eta() const
pseudorapidity of momentum vector
Definition: TrackBase.h:684
void setCurrentFolder(std::string const &fullpath)
Definition: DQMStore.cc:268
edm::RefToBase< reco::Track > TrackBaseRef
persistent reference to a Track, using views
Definition: TrackFwd.h:36
T sqrt(T t)
Definition: SSEVec.h:18
void bookHistograms(DQMStore::IBooker &, edm::Run const &, edm::EventSetup const &) override
double pt() const
track transverse momentum
Definition: TrackBase.h:654
MonitorElement * book1D(Args &&...args)
Definition: DQMStore.h:106
def pv(vc)
Definition: MetAnalyzer.py:7
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
math::XYZPoint Point
point in the space
Definition: Vertex.h:39
unsigned short numberOfValidHits() const
number of valid hits found
Definition: TrackBase.h:889
static const std::vector< std::string > TrkHistCat
void newEvent(edm::Event const &, edm::EventSetup const &)
Pre-process event information (for accessing reconstraction information)
bool isPixel() const
double dz() const
dz parameter (= dsz/cos(lambda)). This is the track z0 w.r.t (0,0,0) only if the refPoint is close to...
Definition: TrackBase.h:642
const Flags & flags() const
Returns flags with the category descriptions.
const HitPattern & hitPattern() const
Access the hit pattern, indicating in which Tracker layers the track has hits.
Definition: TrackBase.h:479
std::pair< OmniClusterRef, TrackingParticleRef > P
const reco::TrackBaseRef toTrackRef(const edm::Ptr< reco::Candidate > &cnd)
Particle reconstructed by the particle flow algorithm.
Definition: PFCandidate.h:40
T get() const
Definition: EventSetup.h:71
reco::GsfTrackRef gsfTrackRef() const
Definition: PFCandidate.cc:480
const map_type & map() const
int numberOfValidPixelHits() const
Definition: HitPattern.h:916
std::vector< bool > Flags
Main types associated to the class.
TrajectoryStateOnSurface impactPointState() const
const std::pair< TrackingParticleRef, double > getMatchedTrackingParticle() const
Definition: TrackHistory.h:58
math::XYZVectorD Vector
point in the space
double dxy() const
dxy parameter. (This is the transverse impact parameter w.r.t. to (0,0,0) ONLY if refPoint is close t...
Definition: TrackBase.h:624
Definition: Run.h:45
void analyze(const edm::Event &iEvent, const edm::EventSetup &iSetup) override