CMS 3D CMS Logo

MultiTrackValidatorGenPs.cc
Go to the documentation of this file.
2 
5 
20 
24 
25 #include "TMath.h"
26 #include <TF1.h>
27 
28 //#include <iostream>
29 
30 using namespace std;
31 using namespace edm;
32 
33 static const std::string kTrackAssociatorByChi2("trackAssociatorByChi2");
34 
36  gpSelector = GenParticleCustomSelector(pset.getParameter<double>("ptMinGP"),
37  pset.getParameter<double>("minRapidityGP"),
38  pset.getParameter<double>("maxRapidityGP"),
39  pset.getParameter<double>("tipGP"),
40  pset.getParameter<double>("lipGP"),
41  pset.getParameter<bool>("chargedOnlyGP"),
42  pset.getParameter<int>("statusGP"),
43  pset.getParameter<std::vector<int> >("pdgIdGP"));
44 
45  if (useAssociators_) {
46  for (auto const& src : associators) {
47  if (src.label() == kTrackAssociatorByChi2) {
48  label_gen_associator = consumes<reco::TrackToGenParticleAssociator>(src);
49  break;
50  }
51  }
52  } else {
53  for (auto const& src : associators) {
54  associatormapGtR = consumes<reco::GenToRecoCollection>(src);
55  associatormapRtG = consumes<reco::RecoToGenCollection>(src);
56  break;
57  }
58  }
59 }
60 
62 
64  const edm::EventSetup& setup,
65  const Histograms& histograms) const {
66  using namespace reco;
67 
68  edm::LogInfo("TrackValidator") << "\n===================================================="
69  << "\n"
70  << "Analyzing new event"
71  << "\n"
72  << "====================================================\n"
73  << "\n";
74 
75  const TrackerTopology& ttopo = setup.getData(tTopoEsToken);
76 
77  edm::Handle<GenParticleCollection> TPCollectionHeff;
78  event.getByToken(label_tp_effic, TPCollectionHeff);
79  const GenParticleCollection tPCeff = *(TPCollectionHeff.product());
80 
81  edm::Handle<GenParticleCollection> TPCollectionHfake;
82  event.getByToken(label_tp_fake, TPCollectionHfake);
83  const GenParticleCollection tPCfake = *(TPCollectionHfake.product());
84 
85  //if (tPCeff.size()==0) {edm::LogInfo("TrackValidator")
86  //<< "TP Collection for efficiency studies has size = 0! Skipping Event." ; return;}
87  //if (tPCfake.size()==0) {edm::LogInfo("TrackValidator")
88  //<< "TP Collection for fake rate studies has size = 0! Skipping Event." ; return;}
89 
90  edm::Handle<reco::BeamSpot> recoBeamSpotHandle;
91  event.getByToken(bsSrc, recoBeamSpotHandle);
92  reco::BeamSpot bs = *recoBeamSpotHandle;
93 
95  event.getByToken(label_pileupinfo, puinfoH);
96  PileupSummaryInfo puinfo;
97 
98  for (unsigned int puinfo_ite = 0; puinfo_ite < (*puinfoH).size(); ++puinfo_ite) {
99  if ((*puinfoH)[puinfo_ite].getBunchCrossing() == 0) {
100  puinfo = (*puinfoH)[puinfo_ite];
101  break;
102  }
103  }
104 
105  const reco::TrackToGenParticleAssociator* trackGenAssociator = nullptr;
106  if (useAssociators_) {
108  return;
109  } else {
111  event.getByToken(label_gen_associator, trackGenAssociatorH);
112  trackGenAssociator = trackGenAssociatorH.product();
113  }
114  } else if (associatormapGtR.isUninitialized()) {
115  return;
116  }
117 
118  // dE/dx
119  // at some point this could be generalized, with a vector of tags and a corresponding vector of Handles
120  // I'm writing the interface such to take vectors of ValueMaps
121  std::vector<const edm::ValueMap<reco::DeDxData>*> v_dEdx;
122  if (dodEdxPlots_) {
125  event.getByToken(m_dEdx1Tag, dEdx1Handle);
126  event.getByToken(m_dEdx2Tag, dEdx2Handle);
127  v_dEdx.push_back(dEdx1Handle.product());
128  v_dEdx.push_back(dEdx2Handle.product());
129  }
130 
131  std::vector<float> mvaDummy;
132 
133  int w = 0; //counter counting the number of sets of histograms
134  for (unsigned int www = 0; www < label.size(); www++) {
135  //
136  //get collections from the event
137  //
140  continue;
141  //if (trackCollection->size()==0)
142  //edm::LogInfo("TrackValidator") << "TrackCollection size = 0!" ;
143  //continue;
144  //}
145  reco::RecoToGenCollection recGenColl;
146  reco::GenToRecoCollection genRecColl;
147  //associate tracks
148  if (useAssociators_) {
149  edm::LogVerbatim("TrackValidator") << "Analyzing " << label[www].process() << ":" << label[www].label() << ":"
150  << label[www].instance() << " with " << kTrackAssociatorByChi2 << "\n";
151 
152  LogTrace("TrackValidator") << "Calling associateRecoToGen method"
153  << "\n";
154  recGenColl = trackGenAssociator->associateRecoToGen(trackCollection, TPCollectionHfake);
155  LogTrace("TrackValidator") << "Calling associateGenToReco method"
156  << "\n";
157  genRecColl = trackGenAssociator->associateGenToReco(trackCollection, TPCollectionHeff);
158  } else {
159  edm::LogVerbatim("TrackValidator") << "Analyzing " << label[www].process() << ":" << label[www].label() << ":"
160  << label[www].instance() << " with " << associators[0] << "\n";
161 
162  Handle<reco::GenToRecoCollection> gentorecoCollectionH;
163  event.getByToken(associatormapGtR, gentorecoCollectionH);
164  genRecColl = *(gentorecoCollectionH.product());
165 
166  Handle<reco::RecoToGenCollection> recotogenCollectionH;
167  event.getByToken(associatormapRtG, recotogenCollectionH);
168  recGenColl = *(recotogenCollectionH.product());
169  }
170 
171  // ########################################################
172  // fill simulation histograms (LOOP OVER TRACKINGPARTICLES)
173  // ########################################################
174 
175  //compute number of tracks per eta interval
176  //
177  edm::LogVerbatim("TrackValidator") << "\n# of GenParticles: " << tPCeff.size() << "\n";
178  int ats(0); //This counter counts the number of simTracks that are "associated" to recoTracks
179  int st(0); //This counter counts the number of simulated tracks passing the MTV selection (i.e. tpSelector(tp) )
180  for (GenParticleCollection::size_type i = 0; i < tPCeff.size();
181  i++) { //loop over TPs collection for tracking efficiency
182  GenParticleRef tpr(TPCollectionHeff, i);
183  GenParticle* tp = const_cast<GenParticle*>(tpr.get());
184  TrackingParticle::Vector momentumTP;
185  TrackingParticle::Point vertexTP;
186  double dxyGen(0);
187  double dzGen(0);
188 
189  //---------- THIS PART HAS TO BE CLEANED UP. THE PARAMETER DEFINER WAS NOT MEANT TO BE USED IN THIS WAY ----------
190  //If the GenParticle is collison like, get the momentum and vertex at production state
192  //fixme this one shold be implemented
193  if (!gpSelector(*tp))
194  continue;
195  momentumTP = tp->momentum();
196  vertexTP = tp->vertex();
197  //Calcualte the impact parameters w.r.t. PCA
200  dxyGen = (-vertex.x() * sin(momentum.phi()) + vertex.y() * cos(momentum.phi()));
201  dzGen = vertex.z() - (vertex.x() * momentum.x() + vertex.y() * momentum.y()) / sqrt(momentum.perp2()) *
202  momentum.z() / sqrt(momentum.perp2());
203  }
204  //If the GenParticle is comics, get the momentum and vertex at PCA
205  else {
206  //if(! cosmictpSelector(*tp,&bs,event,setup)) continue;
207  momentumTP = parametersDefinerTP_->momentum(event, setup, *tp);
208  vertexTP = parametersDefinerTP_->vertex(event, setup, *tp);
209  dxyGen = (-vertexTP.x() * sin(momentumTP.phi()) + vertexTP.y() * cos(momentumTP.phi()));
210  dzGen = vertexTP.z() - (vertexTP.x() * momentumTP.x() + vertexTP.y() * momentumTP.y()) /
211  sqrt(momentumTP.perp2()) * momentumTP.z() / sqrt(momentumTP.perp2());
212  }
213  //---------- THE PART ABOVE HAS TO BE CLEANED UP. THE PARAMETER DEFINER WAS NOT MEANT TO BE USED IN THIS WAY ----------
214 
215  st++; //This counter counts the number of simulated tracks passing the MTV selection (i.e. tpSelector(tp) )
216 
217  // in the coming lines, histos are filled using as input
218  // - momentumTP
219  // - vertexTP
220  // - dxyGen
221  // - dzGen
222 
223  if (doSimPlots_ && w == 0) {
224  histoProducerAlgo_->fill_generic_simTrack_histos(histograms.histoProducerAlgo,
225  momentumTP,
226  vertexTP,
227  tp->collisionId()); //fixme: check meaning of collisionId
228  }
229  if (!doSimTrackPlots_)
230  continue;
231 
232  // ##############################################
233  // fill RecoAssociated GenTracks' histograms
234  // ##############################################
235  // bool isRecoMatched(false); // UNUSED
236  const reco::Track* matchedTrackPointer = nullptr;
237  std::vector<std::pair<RefToBase<Track>, double> > rt;
238  if (genRecColl.find(tpr) != genRecColl.end()) {
239  rt = (std::vector<std::pair<RefToBase<Track>, double> >)genRecColl[tpr];
240  if (!rt.empty()) {
241  ats++; //This counter counts the number of simTracks that have a recoTrack associated
242  // isRecoMatched = true; // UNUSED
243  matchedTrackPointer = rt.begin()->first.get();
244  edm::LogVerbatim("TrackValidator") << "GenParticle #" << st << " with pt=" << sqrt(momentumTP.perp2())
245  << " associated with quality:" << rt.begin()->second << "\n";
246  }
247  } else {
248  edm::LogVerbatim("TrackValidator") << "GenParticle #" << st << " with pt,eta,phi: " << sqrt(momentumTP.perp2())
249  << " , " << momentumTP.eta() << " , " << momentumTP.phi() << " , "
250  << " NOT associated to any reco::Track"
251  << "\n";
252  }
253 
254  int nSimHits = 0;
255  histoProducerAlgo_->fill_recoAssociated_simTrack_histos(histograms.histoProducerAlgo,
256  w,
257  *tp,
258  momentumTP,
259  vertexTP,
260  dxyGen,
261  dzGen,
262  nSimHits,
263  matchedTrackPointer,
264  puinfo.getPU_NumInteractions());
265 
266  } // End for (GenParticleCollection::size_type i=0; i<tPCeff.size(); i++){
267 
268  if (doSimPlots_ && w == 0) {
269  histoProducerAlgo_->fill_simTrackBased_histos(histograms.histoProducerAlgo, st);
270  }
271 
272  // ##############################################
273  // fill recoTracks histograms (LOOP OVER TRACKS)
274  // ##############################################
275  if (!doRecoTrackPlots_)
276  continue;
277  edm::LogVerbatim("TrackValidator") << "\n# of reco::Tracks with " << label[www].process() << ":"
278  << label[www].label() << ":" << label[www].instance() << ": "
279  << trackCollection->size() << "\n";
280 
281  //int sat(0); //This counter counts the number of recoTracks that are associated to GenTracks from Signal only
282  int at(0); //This counter counts the number of recoTracks that are associated to GenTracks
283  int rT(0); //This counter counts the number of recoTracks in general
284 
285  for (View<Track>::size_type i = 0; i < trackCollection->size(); ++i) {
287  rT++;
288 
289  bool isSigGenMatched(false);
290  bool isGenMatched(false);
291  bool isChargeMatched(true);
292  int numAssocRecoTracks = 0;
293  int nSimHits = 0;
294  double sharedFraction = 0.;
295  std::vector<std::pair<GenParticleRef, double> > tp;
296  if (recGenColl.find(track) != recGenColl.end()) {
297  tp = recGenColl[track];
298  if (!tp.empty()) {
299  /*
300  std::vector<PSimHit> simhits=tp[0].first->trackPSimHit(DetId::Tracker);
301  nSimHits = simhits.end()-simhits.begin();
302  */
303  sharedFraction = tp[0].second;
304  isGenMatched = true;
305  if (tp[0].first->charge() != track->charge())
306  isChargeMatched = false;
307  if (genRecColl.find(tp[0].first) != genRecColl.end())
308  numAssocRecoTracks = genRecColl[tp[0].first].size();
309  //std::cout << numAssocRecoTracks << std::endl;
310  at++;
311  for (unsigned int tp_ite = 0; tp_ite < tp.size(); ++tp_ite) {
312  GenParticle trackpart = *(tp[tp_ite].first);
313  /*
314  if ((trackpart.eventId().event() == 0) && (trackpart.eventId().bunchCrossing() == 0)){
315  isSigGenMatched = true;
316  sat++;
317  break;
318  }
319  */
320  }
321  edm::LogVerbatim("TrackValidator") << "reco::Track #" << rT << " with pt=" << track->pt()
322  << " associated with quality:" << tp.begin()->second << "\n";
323  }
324  } else {
325  edm::LogVerbatim("TrackValidator")
326  << "reco::Track #" << rT << " with pt=" << track->pt() << " NOT associated to any GenParticle"
327  << "\n";
328  }
329 
330  double dR = 0; //fixme: plots vs dR and vs dRjet not implemented for now
331  histoProducerAlgo_->fill_generic_recoTrack_histos(histograms.histoProducerAlgo,
332  w,
333  *track,
334  ttopo,
335  bs.position(),
336  nullptr,
337  nullptr,
338  isGenMatched,
339  isSigGenMatched,
340  isChargeMatched,
341  numAssocRecoTracks,
342  puinfo.getPU_NumInteractions(),
343  nSimHits,
344  sharedFraction,
345  dR,
346  dR,
347  mvaDummy,
348  0,
349  0);
350 
351  // dE/dx
352  if (dodEdxPlots_)
353  histoProducerAlgo_->fill_dedx_recoTrack_histos(histograms.histoProducerAlgo, w, track, v_dEdx);
354 
355  //Fill other histos
356  //try{ //Is this really necessary ????
357 
358  if (tp.empty())
359  continue;
360 
361  histoProducerAlgo_->fill_simAssociated_recoTrack_histos(histograms.histoProducerAlgo, w, *track);
362 
363  GenParticleRef tpr = tp.begin()->first;
364 
365  /* TO BE FIXED LATER
366  if (associators[ww]=="TrackAssociatorByChi2"){
367  //association chi2
368  double assocChi2 = -tp.begin()->second;//in association map is stored -chi2
369  h_assochi2[www]->Fill(assocChi2);
370  h_assochi2_prob[www]->Fill(TMath::Prob((assocChi2)*5,5));
371  }
372  else if (associators[ww]=="quickTrackAssociatorByHits"){
373  double fraction = tp.begin()->second;
374  h_assocFraction[www]->Fill(fraction);
375  h_assocSharedHit[www]->Fill(fraction*track->numberOfValidHits());
376  }
377  */
378 
379  //Get tracking particle parameters at point of closest approach to the beamline
380  TrackingParticle::Vector momentumTP = parametersDefinerTP_->momentum(event, setup, *(tpr.get()));
381  TrackingParticle::Point vertexTP = parametersDefinerTP_->vertex(event, setup, *(tpr.get()));
382  int chargeTP = tpr->charge();
383 
384  histoProducerAlgo_->fill_ResoAndPull_recoTrack_histos(
385  histograms.histoProducerAlgo, w, momentumTP, vertexTP, chargeTP, *track, bs.position());
386 
387  //TO BE FIXED
388  //std::vector<PSimHit> simhits=tpr.get()->trackPSimHit(DetId::Tracker);
389  //nrecHit_vs_nsimHit_rec2sim[w]->Fill(track->numberOfValidHits(), (int)(simhits.end()-simhits.begin() ));
390 
391  /*
392  } // End of try{
393  catch (cms::Exception e){
394  LogTrace("TrackValidator") << "exception found: " << e.what() << "\n";
395  }
396  */
397 
398  } // End of for(View<Track>::size_type i=0; i<trackCollection->size(); ++i){
399 
400  histoProducerAlgo_->fill_trackBased_histos(histograms.histoProducerAlgo, w, at, rT, rT, st);
401 
402  edm::LogVerbatim("TrackValidator") << "Total Simulated: " << st << "\n"
403  << "Total Associated (genToReco): " << ats << "\n"
404  << "Total Reconstructed: " << rT << "\n"
405  << "Total Associated (recoToGen): " << at << "\n"
406  << "Total Fakes: " << rT - at << "\n";
407 
408  w++;
409  } // End of for (unsigned int www=0;www<label.size();www++){
410 }
size
Write out results.
const edm::ESGetToken< TrackerTopology, TrackerTopologyRcd > tTopoEsToken
Log< level::Info, true > LogVerbatim
std::vector< GenParticle > GenParticleCollection
collection of GenParticles
unsigned int size_type
Definition: View.h:92
edm::EDGetTokenT< reco::GenToRecoCollection > associatormapGtR
edm::EDGetTokenT< edm::ValueMap< reco::DeDxData > > m_dEdx2Tag
static const std::string kTrackAssociatorByChi2("trackAssociatorByChi2")
reco::RecoToGenCollection associateRecoToGen(const edm::RefToBaseVector< reco::Track > &tracks, const edm::RefVector< reco::GenParticleCollection > &gens) const
Association Sim To Reco with Collections (Gen Particle version)
T w() const
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
T const * product() const
Definition: Handle.h:70
constexpr bool isUninitialized() const noexcept
Definition: EDGetToken.h:104
void dqmAnalyze(const edm::Event &, const edm::EventSetup &, const Histograms &) const override
Method called once per event.
uint16_t size_type
edm::EDGetTokenT< reco::BeamSpot > bsSrc
const_iterator find(const key_type &k) const
find element with specified reference key
MultiTrackValidatorGenPs(const edm::ParameterSet &pset)
Constructor.
#define LogTrace(id)
const_iterator end() const
last iterator over the map (read only)
math::XYZPointD Point
point in the space
std::vector< edm::InputTag > associators
edm::EDGetTokenT< reco::TrackToGenParticleAssociator > label_gen_associator
std::vector< edm::EDGetTokenT< edm::View< reco::Track > > > labelToken
edm::EDGetTokenT< edm::ValueMap< reco::DeDxData > > m_dEdx1Tag
GenParticleCustomSelector gpSelector
T sqrt(T t)
Definition: SSEVec.h:19
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
edm::EDGetTokenT< std::vector< PileupSummaryInfo > > label_pileupinfo
std::vector< edm::InputTag > label
SingleObjectSelector< GenParticleCollection, ::GenParticleCustomSelector > GenParticleCustomSelector
std::unique_ptr< ParametersDefinerForTP > parametersDefinerTP_
trackCollection
Definition: JetHT_cfg.py:51
std::unique_ptr< MTVHistoProducerAlgoForTracker > histoProducerAlgo_
Log< level::Info, false > LogInfo
~MultiTrackValidatorGenPs() override
Destructor.
const bool ignoremissingtkcollection_
fixed size matrix
HLT enums.
T const * get() const
Returns C++ pointer to the item.
Definition: Ref.h:232
edm::EDGetTokenT< TrackingParticleCollection > label_tp_fake
math::XYZVectorD Vector
point in the space
edm::EDGetTokenT< reco::RecoToGenCollection > associatormapRtG
edm::EDGetTokenT< TrackingParticleCollection > label_tp_effic
const int getPU_NumInteractions() const
reco::GenToRecoCollection associateGenToReco(const edm::RefToBaseVector< reco::Track > &tracks, const edm::RefVector< reco::GenParticleCollection > &gens) const
Association Sim To Reco with Collections (Gen Particle version)
Definition: event.py:1
const bool parametersDefinerIsCosmic_