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  ibook.setCurrentFolder("BDHadronTracks/JetContent");
49  //
50  // Book all histograms.
51  //
53 
54 
55  nTrkAll_bjet = ibook.book1D("nTrkAll_bjet","Number of selected tracks in b jets;number of selected tracks;jets",16,-0.5,15.5);
56 
57  nTrkAll_cjet = ibook.book1D("nTrkAll_cjet","Number of selected tracks in c jets;number of selected tracks;jets",16,-0.5,15.5);
58 
59  nTrkAll_dusgjet = ibook.book1D("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],"Number of selected tracks in b jets ("+TrkHistCat[i]+");number of selected tracks ("+TrkHistCat[i]+");jets",16,-0.5,15.5);
66 
67  // c jets
68  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);
69 
70  // dusg jets
71  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);
72 
73  ibook.setCurrentFolder("BDHadronTracks/TrackInfo");
74  // track properties for all flavours combined
75  TrkPt_alljets[i] = ibook.book1D("TrkPt_"+TrkHistCat[i],"Track pT ("+TrkHistCat[i]+");track p_{T} ("+TrkHistCat[i]+");tracks",30,0,100);
76  TrkEta_alljets[i] = ibook.book1D("TrkEta_"+TrkHistCat[i],"Track #eta ("+TrkHistCat[i]+");track #eta ("+TrkHistCat[i]+");tracks",30,-2.5,2.5);
77  TrkPhi_alljets[i] = ibook.book1D("TrkPhi_"+TrkHistCat[i],"Track #phi ("+TrkHistCat[i]+");track #phi ("+TrkHistCat[i]+");tracks",30,-3.15,3.15);
78  TrkDxy_alljets[i] = ibook.book1D("TrkDxy_"+TrkHistCat[i],"Track dxy ("+TrkHistCat[i]+");track dxy ("+TrkHistCat[i]+");tracks",30,-0.1,0.1);
79  TrkDz_alljets[i] = ibook.book1D("TrkDz_"+TrkHistCat[i],"Track dz ("+TrkHistCat[i]+");track dz ("+TrkHistCat[i]+");tracks",30,-0.1,0.1);
80  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);
81  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);
82  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);
83 
84  ibook.setCurrentFolder("BDHadronTracks/TrackTruthInfo");
85  if (i < 5){ // Fakes (i == 5) have no truth by definition!
86  TrkTruthPt_alljets[i] = ibook.book1D("TrkTruthPt_"+TrkHistCat[i],"Track pT ("+TrkHistCat[i]+" Truth);track p_{T} ("+TrkHistCat[i]+" Truth);tracks",30,0,100);
87  TrkTruthEta_alljets[i] = ibook.book1D("TrkTruthEta_"+TrkHistCat[i],"Track #eta ("+TrkHistCat[i]+" Truth);track #eta ("+TrkHistCat[i]+" Truth);tracks",30,-2.5,2.5);
88  TrkTruthPhi_alljets[i] = ibook.book1D("TrkTruthPhi_"+TrkHistCat[i],"Track #phi ("+TrkHistCat[i]+" Truth);track #phi ("+TrkHistCat[i]+" Truth);tracks",30,-3.15,3.15);
89  TrkTruthDxy_alljets[i] = ibook.book1D("TrkTruthDxy_"+TrkHistCat[i],"Track dxy ("+TrkHistCat[i]+" Truth);track dxy ("+TrkHistCat[i]+" Truth);tracks",30,-0.1,0.1);
90  TrkTruthDz_alljets[i] = ibook.book1D("TrkTruthDz_"+TrkHistCat[i],"Track dz ("+TrkHistCat[i]+" Truth);track dz ("+TrkHistCat[i]+" Truth);tracks",30,-0.1,0.1);
91  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);
92  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);
93  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);
94  }
95  }
96 }
97 
98 
99 // ---------- Destructor -----------
100 
102 {
103 }
104 
105 
106 // ---------- Analyze -----------
107 // This is needed to get a TrackingParticle --> Cluster match (instead of Cluster-->TP)
108 using P = std::pair<OmniClusterRef, TrackingParticleRef>;
109 bool compare(const P& i, const P& j) {
110  return i.second.index() > j.second.index();
111 }
112 
114 {
115 
117  iEvent.getByToken(PatJetCollectionTag_, patJetsColl);
118 
120  iEvent.getByToken(TrackCollectionTag_,tracksHandle);
121 
122  edm::Handle<ClusterTPAssociation> pCluster2TPListH;
123  iEvent.getByToken(clusterTPMapToken_, pCluster2TPListH);
124  const ClusterTPAssociation& clusterToTPMap = *pCluster2TPListH;
125 
127  iSetup.get<TransientTrackRecord>().get("TransientTrackBuilder", trackBuilder);
128 
129  classifier_.newEvent(iEvent, iSetup);
130 
131  // -----Primary Vertex-----
132  const reco::Vertex *pv;
133 
135  iEvent.getByToken(PrimaryVertexColl_,primaryVertex);
136 
137  bool pvFound = (!primaryVertex->empty());
138  if ( pvFound ) {
139  pv = &(*primaryVertex->begin());
140  }
141  else {
143  e(0,0)=0.0015*0.0015;
144  e(1,1)=0.0015*0.0015;
145  e(2,2)=15.*15.;
146  reco::Vertex::Point p(0,0,0);
147  pv= new reco::Vertex(p,e,1,1,1);
148  }
149  // -----------------------
150 
151  // -------- Loop Over Jets ----------
152  for ( pat::JetCollection::const_iterator jet = patJetsColl->begin(); jet != patJetsColl->end(); ++jet ) {
153  if ( jet->pt() < minJetPt_ || std::fabs(jet->eta()) > maxJetEta_ ) continue;
154 
155  unsigned int flav = abs(jet->hadronFlavour());
156 
157  //std::cout << "patJet collection has pfImpactParameterTagInfo?: " << jet->hasTagInfo("pfImpactParameter") << std::endl;
158  const CandIPTagInfo *trackIpTagInfo = jet->tagInfoCandIP(ipTagInfos_);
159  const std::vector<edm::Ptr<reco::Candidate> > & selectedTracks( trackIpTagInfo->selectedTracks() );
160 
161 
162  unsigned int nseltracks = 0;
163  std::vector<int> nseltracksCat(TrkHistCat.size(),0); // following the order of TrkHistCat
164 
165  unsigned int nTrackSize = selectedTracks.size(); // number of tracks from IPInfos to loop over
166  // -------- Loop Over (selected) Tracks ----------
167  for (unsigned int itt=0; itt < nTrackSize; ++itt)
168  {
169  const TrackBaseRef ptrackRef = toTrackRef(selectedTracks[itt]);
170  const reco::Track * ptrackPtr = reco::btag::toTrack(ptrackRef);
171  const reco::Track & ptrack = *ptrackPtr;
172 
173  reco::TransientTrack transientTrack = trackBuilder->build(ptrackPtr);
174  GlobalVector direction(jet->px(), jet->py(), jet->pz());
175 
176  Double_t distJetAxis = IPTools::jetTrackDistance(transientTrack, direction, *pv).second.value();
177 
178  Double_t decayLength=999;
179  TrajectoryStateOnSurface closest = IPTools::closestApproachToJet(transientTrack.impactPointState(), *pv, direction, transientTrack.field());
180  if (closest.isValid())
181  decayLength = (closest.globalPosition() - RecoVertex::convertPos(pv->position())).mag();
182  else
183  decayLength = 999;
184 
185  // extra cut ons the tracks
186  if (std::fabs(distJetAxis) > distJetAxis_ || decayLength > decayLength_){
187  continue;
188  }
189  nseltracks+=1; // if it passed these cuts, nselectedtracks +1
190 
191 
193 
194  double TrkPt = ptrack.pt();
195  double TrkEta = ptrack.eta();
196  double TrkPhi = ptrack.phi();
197  double TrkDxy = ptrack.dxy(pv->position());
198  double TrkDz = ptrack.dz(pv->position());
199  int TrknHitAll = ptrack.numberOfValidHits();
200  int TrknHitPixel = ptrack.hitPattern().numberOfValidPixelHits();
201  int TrknHitStrip = ptrack.hitPattern().numberOfValidStripHits();
202 
203 
204  double TrkTruthPt=-99;
205  double TrkTruthEta=-99;
206  double TrkTruthPhi=-99;
207  double TrkTruthDxy=-1;
208  double TrkTruthDz=-1;
209  int TrkTruthnHitAll=-1;
210  int TrkTruthnHitPixel=-1;
211  int TrkTruthnHitStrip=-1;
212 
213  // Get corresponding Trackingparticle
214  std::pair<TrackingParticleRef, double> res = classifier_.history().getMatchedTrackingParticle();
215  TrackingParticleRef tpr = res.first;
216  double quality_tpr = res.second;
217 
218  // Match TP to hit-cluster (re-ordering according to TP rather than clusters and look for equal_range of a given tpr)
219  auto clusterTPmap = clusterToTPMap.map();
220  std::sort(clusterTPmap.begin(), clusterTPmap.end(), compare);
221  auto clusterRange = std::equal_range(clusterTPmap.begin(), clusterTPmap.end(),std::make_pair(OmniClusterRef(), tpr), compare);
222  if (quality_tpr != 0) {
223 
224  TrkTruthPt = tpr->pt();
225  TrkTruthEta = tpr->eta();
226  TrkTruthPhi = tpr->phi();
227 
228  const TrackingParticle::Point& vertex_pv = pv->position();
229  TrackingParticle::Point vertex_tpr = tpr->vertex();
230  TrackingParticle::Vector momentum_tpr = tpr->momentum();
231  TrkTruthDxy = (-(vertex_tpr.x()-vertex_pv.x())*momentum_tpr.y()+(vertex_tpr.y()-vertex_pv.y())*momentum_tpr.x())/tpr->pt();
232  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());
233 
234  TrkTruthnHitAll = 0;
235  TrkTruthnHitPixel = 0;
236  TrkTruthnHitStrip = 0;
237  if( clusterRange.first != clusterRange.second ) {
238  for( auto ip=clusterRange.first; ip != clusterRange.second; ++ip ) {
239  const OmniClusterRef& cluster = ip->first;
240  if (cluster.isPixel() && cluster.isValid()){ TrkTruthnHitPixel+=1;}
241  if (cluster.isStrip() && cluster.isValid()){ TrkTruthnHitStrip+=1;}
242  }
243  }
244  TrkTruthnHitAll = TrkTruthnHitPixel + TrkTruthnHitStrip;
245 
246  }
247 
248 
249 
250  // ----------- Filling the correct histograms based on jet flavour and Track history Category --------
251 
252  //BCWeakDecay
263  if (quality_tpr != 0) {
272  }
273  }
274  //BWeakDecay
275  else if ( theFlag[TrackCategories::SignalEvent] && theFlag[TrackCategories::BWeakDecay] && !theFlag[TrackCategories::CWeakDecay] ) {
285  if (quality_tpr != 0) {
294  }
295  }
296  //CWeakDecay
297  else if ( theFlag[TrackCategories::SignalEvent] && !theFlag[TrackCategories::BWeakDecay] && theFlag[TrackCategories::CWeakDecay] ) {
307  if (quality_tpr != 0) {
316  }
317  }
318  //PU
319  else if ( !theFlag[TrackCategories::SignalEvent] && !theFlag[TrackCategories::Fake] ) {
320  nseltracksCat[BDHadronTrackMonitoringAnalyzer::PU] += 1;
329  if (quality_tpr != 0) {
338  }
339  }
340  //Other
341  else if ( theFlag[TrackCategories::SignalEvent] && !theFlag[TrackCategories::BWeakDecay] && !theFlag[TrackCategories::CWeakDecay] ){
342  nseltracksCat[BDHadronTrackMonitoringAnalyzer::Other] += 1;
351  if (quality_tpr != 0) {
360  }
361  }
362  //Fake
363  else if ( !theFlag[TrackCategories::SignalEvent] && theFlag[TrackCategories::Fake] ) {
364  nseltracksCat[BDHadronTrackMonitoringAnalyzer::Fake] += 1;
373  // NO TRUTH FOR FAKES!!!
374  }
375 
376 
377 
378  }
379  // -------- END Loop Over (selected) Tracks ----------
380  // Still have to fill some jet-flavour specific variables
381  if (flav == 5){
382  nTrkAll_bjet->Fill(nseltracks);
383  for (unsigned int i = 0; i < TrkHistCat.size(); i++){
384  nTrk_bjet[i]->Fill(nseltracksCat[i]);
385  }
386  }
387  else if (flav == 4){
388  nTrkAll_cjet->Fill(nseltracks);
389  for (unsigned int i = 0; i < TrkHistCat.size(); i++){
390  nTrk_cjet[i]->Fill(nseltracksCat[i]);
391  }
392  }
393  else {
394  nTrkAll_dusgjet->Fill(nseltracks);
395  for (unsigned int i = 0; i < TrkHistCat.size(); i++){
396  nTrk_dusgjet[i]->Fill(nseltracksCat[i]);
397  }
398  }
399 
400  }
401  // -------- END Loop Over Jets ----------
402 
403  if (!pvFound){delete pv;}
404 
405 
406 }
407 
408 
409 
410 
411 
412 //define this as a plug-in
bool isAvailable() const
Definition: Ref.h:577
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:253
edm::EDGetTokenT< ClusterTPAssociation > clusterTPMapToken_
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:579
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
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:923
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:230
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:32
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:68
reco::GsfTrackRef gsfTrackRef() const
Definition: PFCandidate.cc:480
const map_type & map() const
int numberOfValidPixelHits() const
Definition: HitPattern.h:908
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:624
Definition: Run.h:44
void analyze(const edm::Event &iEvent, const edm::EventSetup &iSetup) override