CMS 3D CMS Logo

BDHadronTrackMonitoringAnalyzer.cc
Go to the documentation of this file.
2 
4 {
5  const reco::PFCandidate * pfcand = dynamic_cast<const reco::PFCandidate *>(cnd.get());
6 
7  if ( (std::abs(pfcand->pdgId()) == 11 || pfcand->pdgId() == 22) && pfcand->gsfTrackRef().isNonnull() && 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 
16 // ---------- Static member declaration -----------
17 const std::vector<std::string> BDHadronTrackMonitoringAnalyzer::TrkHistCat = {"BCWeakDecay", "BWeakDecay", "CWeakDecay", "PU", "Other", "Fake"};
18 
19 
20 // ---------- Constructor -----------
22  distJetAxis_ ( pSet.getParameter<double>("distJetAxisCut") ),
23  decayLength_ ( pSet.getParameter<double>("decayLengthCut") ),
24  minJetPt_ ( pSet.getParameter<double>("minJetPt") ),
25  maxJetEta_ ( pSet.getParameter<double>("maxJetEta") ),
26  ipTagInfos_ ( pSet.getParameter<std::string>("ipTagInfos") ),
27  PatJetSrc_ ( pSet.getParameter<InputTag>("PatJetSource") ),
28  TrackSrc_ ( pSet.getParameter<InputTag>("TrackSource") ),
29  PVSrc_ ( pSet.getParameter<InputTag>("PrimaryVertexSource") ),
30  ClusterTPMapSrc_ ( pSet.getParameter<InputTag>("clusterTPMap") ),
31  classifier_(pSet, consumesCollector())
32 
33 {
34  PatJetCollectionTag_ = consumes<pat::JetCollection>(PatJetSrc_);
35  TrackCollectionTag_ = consumes<reco::TrackCollection>(TrackSrc_);
36  PrimaryVertexColl_ = consumes<reco::VertexCollection>(PVSrc_);
37  clusterTPMapToken_ = consumes<ClusterTPAssociation>(ClusterTPMapSrc_);
38  //TrkHistCat = {"BCWeakDecay", "BWeakDecay", "CWeakDecay", "PU", "Other", "Fake"};
39 }
40 
41 
42 
43 
44 // ---------- BookHistograms -----------
45 
47 {
48  //
49  // Book all histograms.
50  //
52 
53 
54  nTrkAll_bjet = ibook.book1D("nTrkAll_bjet","Number of selected tracks in b jets;number of selected tracks;jets",16,-0.5,15.5);
55 
56  nTrkAll_cjet = ibook.book1D("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("nTrkAll_dusgjet","Number of selected tracks in dusg jets;number of selected tracks;jets",16,-0.5,15.5);
59 
60  // Loop over different Track History Categories
61  for (unsigned int i = 0; i < TrkHistCat.size(); i++){
62  // b jets
63  nTrk_bjet[i] = ibook.book1D("nTrk_bjet_"+TrkHistCat[i],"Number of selected tracks in b jets ("+TrkHistCat[i]+");number of selected tracks ("+TrkHistCat[i]+");jets",16,-0.5,15.5);
64 
65  // c jets
66  nTrk_cjet[i] = ibook.book1D("nTrk_cjet_"+TrkHistCat[i],"Number of selected tracks in c jets ("+TrkHistCat[i]+");number of selected tracks ("+TrkHistCat[i]+");jets",16,-0.5,15.5);
67 
68  // dusg jets
69  nTrk_dusgjet[i] = ibook.book1D("nTrk_dusgjet_"+TrkHistCat[i],"Number of selected tracks in dusg jets ("+TrkHistCat[i]+");number of selected tracks ("+TrkHistCat[i]+");jets",16,-0.5,15.5);
70 
71 
72  // track properties for all flavours combined
73  TrkPt_alljets[i] = ibook.book1D("TrkPt_"+TrkHistCat[i],"Track pT ("+TrkHistCat[i]+");track p_{T} ("+TrkHistCat[i]+");tracks",30,0,100);
74  TrkEta_alljets[i] = ibook.book1D("TrkEta_"+TrkHistCat[i],"Track #eta ("+TrkHistCat[i]+");track #eta ("+TrkHistCat[i]+");tracks",30,-2.5,2.5);
75  TrkPhi_alljets[i] = ibook.book1D("TrkPhi_"+TrkHistCat[i],"Track #phi ("+TrkHistCat[i]+");track #phi ("+TrkHistCat[i]+");tracks",30,-3.15,3.15);
76  TrkDxy_alljets[i] = ibook.book1D("TrkDxy_"+TrkHistCat[i],"Track dxy ("+TrkHistCat[i]+");track dxy ("+TrkHistCat[i]+");tracks",30,-0.1,0.1);
77  TrkDz_alljets[i] = ibook.book1D("TrkDz_"+TrkHistCat[i],"Track dz ("+TrkHistCat[i]+");track dz ("+TrkHistCat[i]+");tracks",30,-0.1,0.1);
78  TrkHitAll_alljets[i] = ibook.book1D("TrkHitAll_"+TrkHistCat[i],"Number of tracker hits ("+TrkHistCat[i]+");track number of all hits ("+TrkHistCat[i]+");tracks",31,-0.5,30.5);
79  TrkHitStrip_alljets[i] = ibook.book1D("TrkHitStrip_"+TrkHistCat[i],"Number of strip hits ("+TrkHistCat[i]+");track number of strip hits ("+TrkHistCat[i]+");tracks",31,-0.5,30.5);
80  TrkHitPixel_alljets[i] = ibook.book1D("TrkHitPixel_"+TrkHistCat[i],"Number of pixel hits ("+TrkHistCat[i]+");track number of pixel hits ("+TrkHistCat[i]+");tracks",9,-0.5,8.5);
81  if (i < 5){ // Fakes (i == 5) have no truth by definition!
82  TrkTruthPt_alljets[i] = ibook.book1D("TrkTruthPt_"+TrkHistCat[i],"Track pT ("+TrkHistCat[i]+" Truth);track p_{T} ("+TrkHistCat[i]+" Truth);tracks",30,0,100);
83  TrkTruthEta_alljets[i] = ibook.book1D("TrkTruthEta_"+TrkHistCat[i],"Track #eta ("+TrkHistCat[i]+" Truth);track #eta ("+TrkHistCat[i]+" Truth);tracks",30,-2.5,2.5);
84  TrkTruthPhi_alljets[i] = ibook.book1D("TrkTruthPhi_"+TrkHistCat[i],"Track #phi ("+TrkHistCat[i]+" Truth);track #phi ("+TrkHistCat[i]+" Truth);tracks",30,-3.15,3.15);
85  TrkTruthDxy_alljets[i] = ibook.book1D("TrkTruthDxy_"+TrkHistCat[i],"Track dxy ("+TrkHistCat[i]+" Truth);track dxy ("+TrkHistCat[i]+" Truth);tracks",30,-0.1,0.1);
86  TrkTruthDz_alljets[i] = ibook.book1D("TrkTruthDz_"+TrkHistCat[i],"Track dz ("+TrkHistCat[i]+" Truth);track dz ("+TrkHistCat[i]+" Truth);tracks",30,-0.1,0.1);
87  TrkTruthHitAll_alljets[i] = ibook.book1D("TrkTruthHitAll_"+TrkHistCat[i],"Number of tracker hits ("+TrkHistCat[i]+" Truth);track number of all hits ("+TrkHistCat[i]+" Truth);tracks",31,-0.5,30.5);
88  TrkTruthHitStrip_alljets[i] = ibook.book1D("TrkTruthHitStrip_"+TrkHistCat[i],"Number of strip hits ("+TrkHistCat[i]+" Truth);track number of strip hits ("+TrkHistCat[i]+" Truth);tracks",31,-0.5,30.5);
89  TrkTruthHitPixel_alljets[i] = ibook.book1D("TrkTruthHitPixel_"+TrkHistCat[i],"Number of pixel hits ("+TrkHistCat[i]+" Truth);track number of pixel hits ("+TrkHistCat[i]+" Truth);tracks",9,-0.5,8.5);
90  }
91  }
92 }
93 
94 
95 // ---------- Destructor -----------
96 
98 {
99 }
100 
101 
102 // ---------- Analyze -----------
103 // This is needed to get a TrackingParticle --> Cluster match (instead of Cluster-->TP)
104 using P = std::pair<OmniClusterRef, TrackingParticleRef>;
105 bool compare(const P& i, const P& j) {
106  return i.second.index() > j.second.index();
107 }
108 
110 {
111 
113  iEvent.getByToken(PatJetCollectionTag_, patJetsColl);
114 
116  iEvent.getByToken(TrackCollectionTag_,tracksHandle);
117 
118  edm::Handle<ClusterTPAssociation> pCluster2TPListH;
119  iEvent.getByToken(clusterTPMapToken_, pCluster2TPListH);
120  const ClusterTPAssociation& clusterToTPMap = *pCluster2TPListH;
121 
123  iSetup.get<TransientTrackRecord>().get("TransientTrackBuilder", trackBuilder);
124 
125  classifier_.newEvent(iEvent, iSetup);
126 
127  // -----Primary Vertex-----
128  const reco::Vertex *pv;
129 
131  iEvent.getByToken(PrimaryVertexColl_,primaryVertex);
132 
133  bool pvFound = (primaryVertex->size() != 0);
134  if ( pvFound ) {
135  pv = &(*primaryVertex->begin());
136  }
137  else {
139  e(0,0)=0.0015*0.0015;
140  e(1,1)=0.0015*0.0015;
141  e(2,2)=15.*15.;
142  reco::Vertex::Point p(0,0,0);
143  pv= new reco::Vertex(p,e,1,1,1);
144  }
145  // -----------------------
146 
147  // -------- Loop Over Jets ----------
148  for ( pat::JetCollection::const_iterator jet = patJetsColl->begin(); jet != patJetsColl->end(); ++jet ) {
149  if ( jet->pt() < minJetPt_ || std::fabs(jet->eta()) > maxJetEta_ ) continue;
150 
151  unsigned int flav = abs(jet->hadronFlavour());
152 
153 
154  const CandIPTagInfo *trackIpTagInfo = jet->tagInfoCandIP(ipTagInfos_.c_str());
155  const std::vector<edm::Ptr<reco::Candidate> > & selectedTracks( trackIpTagInfo->selectedTracks() );
156 
157 
158  unsigned int nseltracks = 0;
159  std::vector<int> nseltracksCat(TrkHistCat.size(),0); // following the order of TrkHistCat
160 
161  unsigned int nTrackSize = selectedTracks.size(); // number of tracks from IPInfos to loop over
162  // -------- Loop Over (selected) Tracks ----------
163  for (unsigned int itt=0; itt < nTrackSize; ++itt)
164  {
165  const TrackBaseRef ptrackRef = toTrackRef(selectedTracks[itt]);
166  const reco::Track * ptrackPtr = reco::btag::toTrack(ptrackRef);
167  const reco::Track & ptrack = *ptrackPtr;
168 
169  reco::TransientTrack transientTrack = trackBuilder->build(ptrackPtr);
170  GlobalVector direction(jet->px(), jet->py(), jet->pz());
171 
172  Double_t distJetAxis = IPTools::jetTrackDistance(transientTrack, direction, *pv).second.value();
173 
174  Double_t decayLength=999;
175  TrajectoryStateOnSurface closest = IPTools::closestApproachToJet(transientTrack.impactPointState(), *pv, direction, transientTrack.field());
176  if (closest.isValid())
177  decayLength = (closest.globalPosition() - RecoVertex::convertPos(pv->position())).mag();
178  else
179  decayLength = 999;
180 
181  // extra cut ons the tracks
182  if (std::fabs(distJetAxis) > distJetAxis_ || decayLength > decayLength_){
183  continue;
184  }
185  nseltracks+=1; // if it passed these cuts, nselectedtracks +1
186 
187 
189 
190  double TrkPt = ptrack.pt();
191  double TrkEta = ptrack.eta();
192  double TrkPhi = ptrack.phi();
193  double TrkDxy = ptrack.dxy(pv->position());
194  double TrkDz = ptrack.dz(pv->position());
195  int TrknHitAll = ptrack.numberOfValidHits();
196  int TrknHitPixel = ptrack.hitPattern().numberOfValidPixelHits();
197  int TrknHitStrip = ptrack.hitPattern().numberOfValidStripHits();
198 
199 
200  double TrkTruthPt=-99;
201  double TrkTruthEta=-99;
202  double TrkTruthPhi=-99;
203  double TrkTruthDxy=-1;
204  double TrkTruthDz=-1;
205  int TrkTruthnHitAll=-1;
206  int TrkTruthnHitPixel=-1;
207  int TrkTruthnHitStrip=-1;
208 
209  // Get corresponding Trackingparticle
210  std::pair<TrackingParticleRef, double> res = classifier_.history().getMatchedTrackingParticle();
211  TrackingParticleRef tpr = res.first;
212  double quality_tpr = res.second;
213 
214  // Match TP to hit-cluster (re-ordering according to TP rather than clusters and look for equal_range of a given tpr)
215  auto clusterTPmap = clusterToTPMap.map();
216  std::sort(clusterTPmap.begin(), clusterTPmap.end(), compare);
217  auto clusterRange = std::equal_range(clusterTPmap.begin(), clusterTPmap.end(),std::make_pair(OmniClusterRef(), tpr), compare);
218  if (quality_tpr != 0) {
219 
220  TrkTruthPt = tpr->pt();
221  TrkTruthEta = tpr->eta();
222  TrkTruthPhi = tpr->phi();
223 
224  TrackingParticle::Point vertex_pv = pv->position();
225  TrackingParticle::Point vertex_tpr = tpr->vertex();
226  TrackingParticle::Vector momentum_tpr = tpr->momentum();
227  TrkTruthDxy = (-(vertex_tpr.x()-vertex_pv.x())*momentum_tpr.y()+(vertex_tpr.y()-vertex_pv.y())*momentum_tpr.x())/tpr->pt();
228  TrkTruthDz = (vertex_tpr.z()-vertex_pv.z()) - ((vertex_tpr.x()-vertex_pv.x())*momentum_tpr.x()+(vertex_tpr.y()-vertex_pv.y())*momentum_tpr.y())/sqrt(momentum_tpr.perp2()) * momentum_tpr.z()/sqrt(momentum_tpr.perp2());
229 
230  TrkTruthnHitAll = 0;
231  TrkTruthnHitPixel = 0;
232  TrkTruthnHitStrip = 0;
233  if( clusterRange.first != clusterRange.second ) {
234  for( auto ip=clusterRange.first; ip != clusterRange.second; ++ip ) {
235  const OmniClusterRef& cluster = ip->first;
236  if (cluster.isPixel() && cluster.isValid()){ TrkTruthnHitPixel+=1;}
237  if (cluster.isStrip() && cluster.isValid()){ TrkTruthnHitStrip+=1;}
238  }
239  }
240  TrkTruthnHitAll = TrkTruthnHitPixel + TrkTruthnHitStrip;
241 
242  }
243 
244 
245 
246  // ----------- Filling the correct histograms based on jet flavour and Track history Category --------
247 
248  //BCWeakDecay
259  if (quality_tpr != 0) {
268  }
269  }
270  //BWeakDecay
271  else if ( theFlag[TrackCategories::SignalEvent] && theFlag[TrackCategories::BWeakDecay] && !theFlag[TrackCategories::CWeakDecay] ) {
281  if (quality_tpr != 0) {
290  }
291  }
292  //CWeakDecay
293  else if ( theFlag[TrackCategories::SignalEvent] && !theFlag[TrackCategories::BWeakDecay] && theFlag[TrackCategories::CWeakDecay] ) {
303  if (quality_tpr != 0) {
312  }
313  }
314  //PU
315  else if ( !theFlag[TrackCategories::SignalEvent] && !theFlag[TrackCategories::Fake] ) {
316  nseltracksCat[BDHadronTrackMonitoringAnalyzer::PU] += 1;
325  if (quality_tpr != 0) {
334  }
335  }
336  //Other
337  else if ( theFlag[TrackCategories::SignalEvent] && !theFlag[TrackCategories::BWeakDecay] && !theFlag[TrackCategories::CWeakDecay] ){
338  nseltracksCat[BDHadronTrackMonitoringAnalyzer::Other] += 1;
347  if (quality_tpr != 0) {
356  }
357  }
358  //Fake
359  else if ( !theFlag[TrackCategories::SignalEvent] && theFlag[TrackCategories::Fake] ) {
360  nseltracksCat[BDHadronTrackMonitoringAnalyzer::Fake] += 1;
369  // NO TRUTH FOR FAKES!!!
370  }
371 
372 
373 
374  }
375  // -------- END Loop Over (selected) Tracks ----------
376  // Still have to fill some jet-flavour specific variables
377  if (flav == 5){
378  nTrkAll_bjet->Fill(nseltracks);
379  for (unsigned int i = 0; i < TrkHistCat.size(); i++){
380  nTrk_bjet[i]->Fill(nseltracksCat[i]);
381  }
382  }
383  else if (flav == 4){
384  nTrkAll_cjet->Fill(nseltracks);
385  for (unsigned int i = 0; i < TrkHistCat.size(); i++){
386  nTrk_cjet[i]->Fill(nseltracksCat[i]);
387  }
388  }
389  else {
390  nTrkAll_dusgjet->Fill(nseltracks);
391  for (unsigned int i = 0; i < TrkHistCat.size(); i++){
392  nTrk_dusgjet[i]->Fill(nseltracksCat[i]);
393  }
394  }
395 
396  }
397  // -------- END Loop Over Jets ----------
398 
399  if (!pvFound){delete pv;}
400 
401 
402 }
403 
404 
405 
406 
407 
408 //define this as a plug-in
bool isAvailable() const
Definition: Ref.h:576
reco::Vertex::Point convertPos(const GlobalPoint &p)
bool isStrip() const
bool compare(const P &i, const P &j)
int i
Definition: DBlmapReader.cc:9
bool isNonnull() const
Checks for non-null.
Definition: Ref.h:252
edm::EDGetTokenT< ClusterTPAssociation > clusterTPMapToken_
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:457
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
T const * get() const
Returns C++ pointer to the item.
Definition: Ptr.h:160
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:853
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:640
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:4
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:438
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:230
double eta() const
pseudorapidity of momentum vector
Definition: TrackBase.h:646
edm::RefToBase< reco::Track > TrackBaseRef
persistent reference to a Track, using views
Definition: TrackFwd.h:32
T sqrt(T t)
Definition: SSEVec.h:18
virtual void bookHistograms(DQMStore::IBooker &, edm::Run const &, edm::EventSetup const &) override
double pt() const
track transverse momentum
Definition: TrackBase.h:616
virtual int pdgId() const final
PDG identifier.
MonitorElement * book1D(Args &&...args)
Definition: DQMStore.h:115
def pv(vc)
Definition: MetAnalyzer.py:6
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
int j
Definition: DBlmapReader.cc:9
math::XYZPoint Point
point in the space
Definition: Vertex.h:39
unsigned short numberOfValidHits() const
number of valid hits found
Definition: TrackBase.h:815
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:604
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:445
const T & get() const
Definition: EventSetup.h:56
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:39
reco::GsfTrackRef gsfTrackRef() const
Definition: PFCandidate.cc:476
const map_type & map() const
int numberOfValidPixelHits() const
Definition: HitPattern.h:838
std::vector< bool > Flags
Main types associated to the class.
TrajectoryStateOnSurface impactPointState() const
const std::pair< TrackingParticleRef, double > getMatchedTrackingParticle() const
Definition: TrackHistory.h:68
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:586
Definition: Run.h:42
virtual void analyze(const edm::Event &iEvent, const edm::EventSetup &iSetup) override