CMS 3D CMS Logo

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