CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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(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=0;
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.size()!=0) {
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(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(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.size()!=0) {
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(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(w,track, v_dEdx);
357 
358 
359  //Fill other histos
360  //try{ //Is this really necessary ????
361 
362  if (tp.size()==0) continue;
363 
364  histoProducerAlgo_->fill_simAssociated_recoTrack_histos(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(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(w,at,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 }
std::vector< GenParticle > GenParticleCollection
collection of GenParticles
T getParameter(std::string const &) const
unsigned int size_type
Definition: View.h:85
edm::EDGetTokenT< edm::ValueMap< reco::DeDxData > > m_dEdx1Tag
edm::EDGetTokenT< reco::GenToRecoCollection > associatormapGtR
int i
Definition: DBlmapReader.cc:9
std::vector< edm::InputTag > associators
static const std::string kTrackAssociatorByChi2("trackAssociatorByChi2")
const double w
Definition: UKUtility.cc:23
edm::EDGetTokenT< std::vector< PileupSummaryInfo > > label_pileupinfo
const_iterator end() const
last iterator over the map (read only)
edm::EDGetTokenT< TrackingParticleCollection > label_tp_effic
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:457
virtual const Point & vertex() const
vertex position (overwritten by PF...)
void analyze(const edm::Event &, const edm::EventSetup &)
Method called once per event.
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
int collisionId() const
Definition: GenParticle.h:39
uint16_t size_type
MultiTrackValidatorGenPs(const edm::ParameterSet &pset)
Constructor.
edm::EDGetTokenT< reco::BeamSpot > bsSrc
math::XYZPointD Point
point in the space
edm::EDGetTokenT< reco::TrackToGenParticleAssociator > label_gen_associator
std::vector< edm::InputTag > label
GenParticleCustomSelector gpSelector
T sqrt(T t)
Definition: SSEVec.h:18
double pt() const
track transverse momentum
Definition: TrackBase.h:616
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
T const * get() const
Returns C++ pointer to the item.
Definition: Ref.h:244
How EventSelector::AcceptEvent() decides whether to accept an event for output otherwise it is excluding the probing of A single or multiple positive and the trigger will pass if any such matching triggers are PASS or EXCEPTION[A criterion thatmatches no triggers at all is detected and causes a throw.] A single negative with an expectation of appropriate bit checking in the decision and the trigger will pass if any such matching triggers are FAIL or EXCEPTION A wildcarded negative criterion that matches more than one trigger in the trigger but the state exists so we define the behavior If all triggers are the negative crieriion will lead to accepting the event(this again matches the behavior of"!*"before the partial wildcard feature was incorporated).The per-event"cost"of each negative criterion with multiple relevant triggers is about the same as!*was in the past
#define LogTrace(id)
std::unique_ptr< MTVHistoProducerAlgoForTracker > histoProducerAlgo_
tuple trackCollection
virtual Vector momentum() const final
spatial momentum vector
T const * product() const
Definition: Handle.h:81
const T & get() const
Definition: EventSetup.h:56
bool isUninitialized() const
Definition: EDGetToken.h:73
int charge() const
track electric charge
Definition: TrackBase.h:562
SingleObjectSelector< GenParticleCollection,::GenParticleCustomSelector > GenParticleCustomSelector
std::vector< edm::EDGetTokenT< edm::View< reco::Track > > > labelToken
math::XYZVectorD Vector
point in the space
edm::EDGetTokenT< TrackingParticleCollection > label_tp_fake
edm::EDGetTokenT< reco::RecoToGenCollection > associatormapRtG
tuple size
Write out results.
virtual ~MultiTrackValidatorGenPs()
Destructor.
edm::EDGetTokenT< edm::ValueMap< reco::DeDxData > > m_dEdx2Tag
const bool parametersDefinerIsCosmic_