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