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