CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ValidationMisalignedTracker.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: ValidationMisalignedTracker
4 // Class: ValidationMisalignedTracker
5 //
13 //
14 // Original Author: Nicola De Filippis
15 // Created: Thu Dec 14 13:13:32 CET 2006
16 // $Id: ValidationMisalignedTracker.cc,v 1.8 2013/01/07 20:46:23 wmtan Exp $
17 //
18 //
19 
20 
22 
23 
24 // user include files
25 
30 
39 
40 //
41 // constructors and destructor
42 //
44 {
45 
46  //now do what ever initialization is needed
48  recenezmu=0., enezmu=0., pLzmu=0., recpLzmu=0.,yzmu=0.,recyzmu=0.,mxptmu=0.,recmxptmu=0., minptmu=0.,recminptmu=0.;
49  // mzele=0.,recmzele=0.
50 
51  flag=0,flagrec=0,count=0,countrec=0;
52  nAssoc=0;
53 
54  for (int i=0;i<2;i++){
55  countpart[i]=0;
56  countpartrec[i]=0;
57  for (int j=0;j<2;j++){
58  ene[i][j]=0.;
59  p[i][j]=0.;
60  px[i][j]=0.;
61  py[i][j]=0.;
62  pz[i][j]=0.;
63  ptmu[i][j]=0.;
64  recene[i][j]=0.;
65  recp[i][j]=0.;
66  recpx[i][j]=0.;
67  recpy[i][j]=0.;
68  recpz[i][j]=0.;
69  recptmu[i][j]=0.;
70  }
71  }
72 
73 
74  eventCount_ = 0;
75 
76  selection_eff = iConfig.getUntrackedParameter<bool>("selection_eff", false);
77  selection_fake = iConfig.getUntrackedParameter<bool>("selection_fake", true);
78  ZmassSelection_ = iConfig.getUntrackedParameter<bool>("ZmassSelection", false);
79  simobject = iConfig.getUntrackedParameter<std::string>("simobject","g4SimHits");
80  trackassociator = iConfig.getUntrackedParameter<std::string>("TrackAssociator","ByHits");
81  associators = iConfig.getParameter< std::vector<std::string> >("associators");
82  label = iConfig.getParameter< std::vector<edm::InputTag> >("label");
83  label_tp_effic = iConfig.getParameter< edm::InputTag >("label_tp_effic");
84  label_tp_fake = iConfig.getParameter< edm::InputTag >("label_tp_fake");
85 
86  rootfile_ = iConfig.getUntrackedParameter<std::string>("rootfile","myroot.root");
87  file_ = new TFile(rootfile_.c_str(),"RECREATE");
88 
89  // initialize the tree
90  tree_eff = new TTree("EffTracks","Efficiency Tracks Tree");
91 
92  tree_eff->Branch("Run",&irun,"irun/i");
93  tree_eff->Branch("Event",&ievt,"ievt/i");
94 
95  // SimTrack
96  tree_eff->Branch("TrackID",&trackType,"trackType/i");
97  tree_eff->Branch("pt",&pt,"pt/F");
98  tree_eff->Branch("eta",&eta,"eta/F");
99  tree_eff->Branch("CotTheta",&cottheta,"cottheta/F");
100  tree_eff->Branch("phi",&phi,"phi/F");
101  tree_eff->Branch("d0",&d0,"d0/F");
102  tree_eff->Branch("z0",&z0,"z0/F");
103  tree_eff->Branch("nhit",&nhit,"nhit/i");
104 
105  // RecTrack
106  tree_eff->Branch("recpt",&recpt,"recpt/F");
107  tree_eff->Branch("receta",&receta,"receta/F");
108  tree_eff->Branch("CotRecTheta",&reccottheta,"reccottheta/F");
109  tree_eff->Branch("recphi",&recphi,"recphi/F");
110  tree_eff->Branch("recd0",& recd0,"recd0/F");
111  tree_eff->Branch("recz0",& recz0,"recz0/F");
112  tree_eff->Branch("nAssoc",&nAssoc,"nAssoc/i");
113  tree_eff->Branch("recnhit",&recnhit,"recnhit/i");
114  tree_eff->Branch("CHISQ",&recchiq,"recchiq/F");
115 
116  tree_eff->Branch("reseta",&reseta,"reseta/F");
117  tree_eff->Branch("respt",&respt,"respt/F");
118  tree_eff->Branch("resd0",&resd0,"resd0/F");
119  tree_eff->Branch("resz0",&resz0,"resz0/F");
120  tree_eff->Branch("resphi",&resphi,"resphi/F");
121  tree_eff->Branch("rescottheta",&rescottheta,"rescottheta/F");
122  tree_eff->Branch("eff",&eff,"eff/F");
123 
124  // Invariant masses, pt of Z
125  tree_eff->Branch("mzmu",&mzmu,"mzmu/F");
126  tree_eff->Branch("ptzmu",&ptzmu,"ptzmu/F");
127  tree_eff->Branch("pLzmu",&pLzmu,"pLzmu/F");
128  tree_eff->Branch("enezmu",&enezmu,"enezmu/F");
129  tree_eff->Branch("etazmu",&etazmu,"etazmu/F");
130  tree_eff->Branch("thetazmu",&thetazmu,"thetazmu/F");
131  tree_eff->Branch("phizmu",&phizmu,"phizmu/F");
132  tree_eff->Branch("yzmu",&yzmu,"yzmu/F");
133  tree_eff->Branch("mxptmu",&mxptmu,"mxptmu/F");
134  tree_eff->Branch("minptmu",&minptmu,"minptmu/F");
135 
136  tree_eff->Branch("recmzmu",&recmzmu,"recmzmu/F");
137  tree_eff->Branch("recptzmu",&recptzmu,"recptzmu/F");
138  tree_eff->Branch("recpLzmu",&recpLzmu,"recpLzmu/F");
139  tree_eff->Branch("recenezmu",&recenezmu,"recenezmu/F");
140  tree_eff->Branch("recetazmu",&recetazmu,"recetazmu/F");
141  tree_eff->Branch("recthetazmu",&recthetazmu,"recthetazmu/F");
142  tree_eff->Branch("recphizmu",&recphizmu,"recphizmu/F");
143  tree_eff->Branch("recyzmu",&recyzmu,"recyzmu/F");
144  tree_eff->Branch("recmxptmu",&recmxptmu,"recmxptmu/F");
145  tree_eff->Branch("recminptmu",&recminptmu,"recminptmu/F");
146 
147 
148  //tree->Branch("mzele",&ntmzele,"ntmzele/F");
149  //tree->Branch("recmzele",&ntmzeleRec,"ntmzeleRec/F");
150  tree_eff->Branch("chi2Associator",&recchiq,"recchiq/F");
151 
152  // Fake
153 
154  tree_fake = new TTree("FakeTracks","Fake Rate Tracks Tree");
155 
156  tree_fake->Branch("Run",&irun,"irun/i");
157  tree_fake->Branch("Event",&ievt,"ievt/i");
158 
159  // SimTrack
160  tree_fake->Branch("fakeTrackID",&faketrackType,"faketrackType/i");
161  tree_fake->Branch("fakept",&fakept,"fakept/F");
162  tree_fake->Branch("fakeeta",&fakeeta,"fakeeta/F");
163  tree_fake->Branch("fakeCotTheta",&fakecottheta,"fakecottheta/F");
164  tree_fake->Branch("fakephi",&fakephi,"fakephi/F");
165  tree_fake->Branch("faked0",&faked0,"faked0/F");
166  tree_fake->Branch("fakez0",&fakez0,"fakez0/F");
167  tree_fake->Branch("fakenhit",&fakenhit,"fakenhit/i");
168 
169  // RecTrack
170  tree_fake->Branch("fakerecpt",&fakerecpt,"fakerecpt/F");
171  tree_fake->Branch("fakereceta",&fakereceta,"fakereceta/F");
172  tree_fake->Branch("fakeCotRecTheta",&fakereccottheta,"fakereccottheta/F");
173  tree_fake->Branch("fakerecphi",&fakerecphi,"fakerecphi/F");
174  tree_fake->Branch("fakerecd0",& fakerecd0,"fakerecd0/F");
175  tree_fake->Branch("fakerecz0",& fakerecz0,"fakerecz0/F");
176  tree_fake->Branch("fakenAssoc",&fakenAssoc,"fakenAssoc/i");
177  tree_fake->Branch("fakerecnhit",&fakerecnhit,"fakerecnhit/i");
178  tree_fake->Branch("fakeCHISQ",&fakerecchiq,"fakerecchiq/F");
179 
180  tree_fake->Branch("fakereseta",&fakereseta,"fakereseta/F");
181  tree_fake->Branch("fakerespt",&fakerespt,"fakerespt/F");
182  tree_fake->Branch("fakeresd0",&fakeresd0,"fakeresd0/F");
183  tree_fake->Branch("fakeresz0",&fakeresz0,"fakeresz0/F");
184  tree_fake->Branch("fakeresphi",&fakeresphi,"fakeresphi/F");
185  tree_fake->Branch("fakerescottheta",&fakerescottheta,"fakerescottheta/F");
186  tree_fake->Branch("fake",&fake,"fake/F");
187 
188  // Invariant masses, pt of Z
189  tree_fake->Branch("fakemzmu",&fakemzmu,"fakemzmu/F");
190  tree_fake->Branch("fakeptzmu",&fakeptzmu,"fakeptzmu/F");
191  tree_fake->Branch("fakepLzmu",&fakepLzmu,"fakepLzmu/F");
192  tree_fake->Branch("fakeenezmu",&fakeenezmu,"fakeenezmu/F");
193  tree_fake->Branch("fakeetazmu",&fakeetazmu,"fakeetazmu/F");
194  tree_fake->Branch("fakethetazmu",&fakethetazmu,"fakethetazmu/F");
195  tree_fake->Branch("fakephizmu",&fakephizmu,"fakephizmu/F");
196  tree_fake->Branch("fakeyzmu",&fakeyzmu,"fakeyzmu/F");
197  tree_fake->Branch("fakemxptmu",&fakemxptmu,"fakemxptmu/F");
198  tree_fake->Branch("fakeminptmu",&fakeminptmu,"fakeminptmu/F");
199 
200  tree_fake->Branch("fakerecmzmu",&fakerecmzmu,"fakerecmzmu/F");
201  tree_fake->Branch("fakerecptzmu",&fakerecptzmu,"fakerecptzmu/F");
202  tree_fake->Branch("fakerecpLzmu",&fakerecpLzmu,"fakerecpLzmu/F");
203  tree_fake->Branch("fakerecenezmu",&fakerecenezmu,"fakerecenezmu/F");
204  tree_fake->Branch("fakerecetazmu",&fakerecetazmu,"fakerecetazmu/F");
205  tree_fake->Branch("fakerecthetazmu",&fakerecthetazmu,"fakerecthetazmu/F");
206  tree_fake->Branch("fakerecphizmu",&fakerecphizmu,"fakerecphizmu/F");
207  tree_fake->Branch("fakerecyzmu",&fakerecyzmu,"fakerecyzmu/F");
208  tree_fake->Branch("fakerecmxptmu",&fakerecmxptmu,"fakerecmxptmu/F");
209  tree_fake->Branch("fakerecminptmu",&fakerecminptmu,"fakerecminptmu/F");
210 
211  tree_fake->Branch("fakechi2Associator",&fakerecchiq,"fakerecchiq/F");
212 
213 }
214 
215 
217 {
218 
219 
220  std::cout << "ValidationMisalignedTracker::endJob Processed " << eventCount_
221  << " events" << std::endl;
222 
223  // store the tree in the output file
224  file_->Write();
225 
226 
227  // Closing the file deletes the tree.
228  file_->Close();
229  tree_eff=0;
230  tree_fake=0;
231 }
232 
233 
234 //
235 // member functions
236 //
237 
238 // ------------ method called to for each event ------------
239 void
241 {
242  std::vector<const reco::TrackToTrackingParticleAssociator*> associatore;
243 
244  {
246  for (unsigned int w=0;w<associators.size();w++) {
247  iEvent.getByLabel(associators[w], theAssociator);
248  associatore.push_back( theAssociator.product() );
249  }
250  }
251 
252  edm::LogInfo("Tracker Misalignment Validation") << "\n Starting!";
253 
254  // Monte Carlo Z selection
255  skip=false;
256  std::vector<int> indmu;
257 
258  if ( selection_eff && ZmassSelection_){
260  iEvent.getByLabel("source", evt);
261  bool accepted = false;
262  bool foundmuons=false;
263  HepMC::GenEvent * myGenEvent = new HepMC::GenEvent(*(evt->GetEvent()));
264 
265  for ( HepMC::GenEvent::particle_iterator p = myGenEvent->particles_begin(); p != myGenEvent->particles_end(); ++p ) {
266  if ( !accepted && ( (*p)->pdg_id() == 23 ) && (*p)->status() == 3 ) {
267  accepted=true;
268  for( HepMC::GenVertex::particle_iterator aDaughter=(*p)->end_vertex()->particles_begin(HepMC::descendants); aDaughter !=(*p)->end_vertex()->particles_end(HepMC::descendants);aDaughter++){
269  if ( abs((*aDaughter)->pdg_id())==13) {
270  foundmuons=true;
271  if ((*aDaughter)->status()!=1 ) {
272  for( HepMC::GenVertex::particle_iterator byaDaughter=(*aDaughter)->end_vertex()->particles_begin(HepMC::descendants); byaDaughter !=(*aDaughter)->end_vertex()->particles_end(HepMC::descendants);byaDaughter++){
273  if ((*byaDaughter)->status()==1 && abs((*byaDaughter)->pdg_id())==13) {
274  indmu.push_back((*byaDaughter)->barcode());
275  std::cout << "Stable muon from Z with charge " << (*byaDaughter)->pdg_id() << " and index "<< (*byaDaughter)->barcode() << std::endl;
276  }
277  }
278  }
279  else {
280  indmu.push_back((*aDaughter)->barcode());
281  std::cout << "Stable muon from Z with charge " << (*aDaughter)->pdg_id() << " and index "<< (*aDaughter)->barcode() << std::endl;
282  }
283  }
284  }
285  if (!foundmuons){
286  std::cout << "No muons from Z ...skip event" << std::endl;
287  skip=true;
288  }
289  }
290  }
291  if ( !accepted) {
292  std::cout << "No Z particles in the event ...skip event" << std::endl;
293  skip=true;
294  }
295  }
296  else {
297  skip=false;
298  }
299 
300  //
301  // Retrieve tracker geometry from event setup
302  //
303  edm::ESHandle<TrackerGeometry> trackerGeometry;
304  iSetup.get<TrackerDigiGeometryRecord>().get( trackerGeometry );
305  auto testGeomDet = trackerGeometry->detsTOB().front();
306  std::cout << testGeomDet->position() << std::endl;
307 
308 
309  //Dump Run and Event
310  irun=iEvent.id().run();
311  ievt=iEvent.id().event();
312 
313  // Reset tree variables
314  int countpart[2]={0,0},countpartrec[2]={0,0},flag=0,flagrec=0,count=0,countrec=0;
315  //int countsim=0;
316  float ene[2][2],px[2][2],py[2][2],pz[2][2],ptmu[2][2];
317  float recene[2][2],recp[2][2],recpx[2][2],recpy[2][2],recpz[2][2],recptmu[2][2];
318 
319  for (int i=0;i<2;i++){
320  for (int j=0;j<2;j++){
321  ene[i][j]=0.;
322  px[i][j]=0.;
323  py[i][j]=0.;
324  pz[i][j]=0.;
325  ptmu[i][j]=0.;
326  recene[i][j]=0.;
327  recp[i][j]=0.;
328  recpx[i][j]=0.;
329  recpy[i][j]=0.;
330  recpz[i][j]=0.;
331  recptmu[i][j]=0.;
332  }
333  }
334 
335 
336  edm::Handle<TrackingParticleCollection> TPCollectionHeff ;
337  iEvent.getByLabel(label_tp_effic,TPCollectionHeff);
338  const TrackingParticleCollection tPCeff = *(TPCollectionHeff.product());
339 
340  edm::Handle<TrackingParticleCollection> TPCollectionHfake ;
341  iEvent.getByLabel(label_tp_fake,TPCollectionHfake);
342  const TrackingParticleCollection tPCfake = *(TPCollectionHfake.product());
343 
344 
345  int w=0;
346  for (unsigned int ww=0;ww<associators.size();ww++){
347  //
348  //get collections from the event
349  //
350 
351  edm::InputTag algo = label[0];
352 
354  iEvent.getByLabel(algo, trackCollection);
355  const edm::View<reco::Track> tC = *(trackCollection.product());
356 
357 
358  //associate tracks
359  LogTrace("TrackValidator") << "Calling associateRecoToSim method" << "\n";
360  reco::RecoToSimCollection recSimColl=associatore[ww]->associateRecoToSim(trackCollection,
361  TPCollectionHfake);
362 
363  LogTrace("TrackValidator") << "Calling associateSimToReco method" << "\n";
364  reco::SimToRecoCollection simRecColl=associatore[ww]->associateSimToReco(trackCollection,
365  TPCollectionHeff);
366 
367 
368 
369 
370  //
371  //compute number of tracks per eta interval
372  //
373 
374  if (selection_eff && !skip ) {
375  std::cout << "Computing Efficiency" << std::endl;
376 
377  edm::LogVerbatim("TrackValidator") << "\n# of TrackingParticles (before cuts): " << tPCeff.size() << "\n";
378  int ats = 0;
379  int st=0;
380  for (TrackingParticleCollection::size_type i=0; i<tPCeff.size(); i++){
381 
382  // Initialize variables
383  eta = 0.,theta=0.,phi=0.,pt=0.,cottheta=0.,costheta=0.;
384  d0=0.,z0=0.;
385  nhit=0;
386  receta = 0.,rectheta = 0.,recphi = 0.,recpt = 0.,reccottheta=0.,recd0=0.,recz0=0.;
387  respt = 0.,resd0 = 0.,resz0 = 0.,reseta = 0.,resphi=0.,rescottheta=0.;
388  recchiq = 0.;
389  recnhit = 0;
390  trackType = 0;
391  eff=0;
392 
393  // typedef edm::Ref<TrackingParticleCollection> TrackingParticleRef;
394  TrackingParticleRef tp(TPCollectionHeff, i);
395  if (tp->charge()==0) continue;
396  st++;
397  //pt=sqrt(tp->momentum().perp2());
398  //eta=tp->momentum().eta();
399  //vpos=tp->vertex().perp2()));
400 
401  const SimTrack * simulatedTrack = &(*tp->g4Track_begin());
402 
404  iSetup.get<IdealMagneticFieldRecord>().get(theMF);
406  ftsAtProduction(GlobalPoint(tp->vertex().x(),tp->vertex().y(),tp->vertex().z()),
407  GlobalVector(simulatedTrack->momentum().x(),simulatedTrack->momentum().y(),simulatedTrack->momentum().z()),
408  TrackCharge(tp->charge()),
409  theMF.product());
410  TSCPBuilderNoMaterial tscpBuilder;
411  TrajectoryStateClosestToPoint tsAtClosestApproach
412  = tscpBuilder(ftsAtProduction,GlobalPoint(0,0,0));//as in TrackProducerAlgorithm
413  GlobalPoint v = tsAtClosestApproach.theState().position();
414  GlobalVector p = tsAtClosestApproach.theState().momentum();
415 
416  // double qoverpSim = tsAtClosestApproach.charge()/p.mag();
417  // double lambdaSim = M_PI/2-p.theta();
418  // double phiSim = p.phi();
419  double dxySim = (-v.x()*sin(p.phi())+v.y()*cos(p.phi()));
420  double dszSim = v.z()*p.perp()/p.mag() - (v.x()*p.x()+v.y()*p.y())/p.perp() * p.z()/p.mag();
421  d0 = float(-dxySim);
422  z0 = float(dszSim*p.mag()/p.perp());
423 
424 
425  if (abs(simulatedTrack->type())==13 && simulatedTrack->genpartIndex() != -1 ) {
426  std::cout << " TRACCIA SIM DI MUONI " << std::endl;
427  std::cout << "Gen part " << simulatedTrack->genpartIndex()<< std::endl;
428  trackType=simulatedTrack->type();
429  theta=simulatedTrack->momentum().theta();
430  costheta=cos(theta);
431  cottheta=1./tan(theta);
432 
433  eta=simulatedTrack->momentum().eta();
434  phi=simulatedTrack->momentum().phi();
435  pt=simulatedTrack->momentum().pt();
436  nhit=tp->matchedHit();
437 
438 
439  std::cout << "3) Before assoc: SimTrack of type = " << simulatedTrack->type()
440  << " ,at eta = " << eta
441  << " ,with pt at vertex = " << simulatedTrack->momentum().pt() << " GeV/c"
442  << " ,d0 =" << d0
443  << " ,z0 =" << z0
444  << " ,nhit=" << nhit
445  << std::endl;
446 
447  if ( ZmassSelection_ ){
448  if (abs(trackType)==13 && (simulatedTrack->genpartIndex()==indmu[0] || simulatedTrack->genpartIndex()==indmu[1] )) {
449  std::cout << " TRACK sim of muons from Z " << std::endl;
450  flag=0;
451  count=countpart[0];
452  countpart[0]++;
453  }
454  else if (abs(trackType)==11) {
455  //std::cout << " TRACCIA SIM DI ELETTRONI " << std::endl;
456  flag=1;
457  count=countpart[1];
458  countpart[1]++;
459  }
460 
461 
462  px[flag][count]=simulatedTrack->momentum().x();
463  py[flag][count]=simulatedTrack->momentum().y();
464  pz[flag][count]=simulatedTrack->momentum().z();
465  ptmu[flag][count]=simulatedTrack->momentum().pt();
466  ene[flag][count]=simulatedTrack->momentum().e();
467  }
468 
469 
470  std::vector<std::pair<edm::RefToBase<reco::Track>, double> > rt;
471  if(simRecColl.find(tp) != simRecColl.end()){
472 
473  rt = simRecColl[tp];
474  if (rt.size()!=0) {
475 
476  edm::RefToBase<reco::Track> t = rt.begin()->first;
477  ats++;
478 
479  // bool flagptused=false;
480  // for (unsigned int j=0;j<ptused.size();j++){
481  // if (fabs(t->pt()-ptused[j])<0.001) {
482  // flagptused=true;
483  // }
484  // }
485 
486  edm::LogVerbatim("TrackValidator") << "TrackingParticle #" << st << " with pt=" << t->pt()
487  << " associated with quality:" << rt.begin()->second <<"\n";
488  std::cout << "Reconstructed Track:" << t->pt()<< std::endl;
489  std::cout << "\tpT: " << t->pt()<< std::endl;
490  std::cout << "\timpact parameter:d0: " << t->d0()<< std::endl;
491  std::cout << "\timpact parameter:z0: " << t->dz()<< std::endl;
492  std::cout << "\tAzimuthal angle of point of closest approach:" << t->phi()<< std::endl;
493  std::cout << "\tcharge: " << t->charge()<< std::endl;
494  std::cout << "\teta: " << t->eta()<< std::endl;
495  std::cout << "\tnormalizedChi2: " << t->normalizedChi2()<< std::endl;
496 
498  recchiq=t->normalizedChi2();
499  rectheta=t->theta();
501  //receta=-log(tan(rectheta/2.));
502  receta=t->momentum().eta();
503  // reccostheta=cos(matchedrectrack->momentum().theta());
504  recphi=t->phi();
505  recpt=t->pt();
506  ptused.push_back(recpt);
507  recd0=t->d0();
508  recz0=t->dz();
509 
510  std::cout << "5) After call to associator: the best match has "
511  << recnhit << " hits, Chi2 = "
512  << recchiq << ", pt at vertex = "
513  << recpt << " GeV/c, "
514  << ", recd0 = " << recd0
515  << ", recz0= " << recz0
516  << std::endl;
517 
518 
519  respt=recpt - pt;
520  resd0=recd0-d0;
521  resz0=recz0-z0;
522  reseta=receta-eta;
523  resphi=recphi-phi;
525  eff=1;
526 
527  std::cout << "6) Transverse momentum residual=" << respt
528  << " ,d0 residual=" << resd0
529  << " ,z0 residual=" << resz0
530  << " with eff=" << eff << std::endl;
531 
532  if ( ZmassSelection_ ){
533 
534  if (abs(trackType)==13) {
535  std::cout << " TRACCIA RECO DI MUONI " << std::endl;
536  flagrec=0;
538  countpartrec[0]++;
539  }
540  else if (abs(trackType)==11) {
541  std::cout << " TRACCIA RECO DI ELETTRONI " << std::endl;
542  flagrec=1;
544  countpartrec[1]++;
545  }
546 
547  recp[flagrec][countrec]=sqrt(t->momentum().mag2());
548  recpx[flagrec][countrec]=t->momentum().x();
549  recpy[flagrec][countrec]=t->momentum().y();
550  recpz[flagrec][countrec]=t->momentum().z();
551  recptmu[flagrec][countrec]=sqrt( (t->momentum().x()*t->momentum().x()) + (t->momentum().y()*t->momentum().y()) );
552  if (abs(trackType)==13) recene[flagrec][countrec]=sqrt(recp[flagrec][countrec]*recp[flagrec][countrec]+0.105*0.105);
553  if (abs(trackType)==11) recene[flagrec][countrec]=sqrt(recp[flagrec][countrec]*recp[flagrec][countrec]+0.0005*0.0005);
554  }
555 
556  std::cout << "7) Transverse momentum reconstructed =" << recpt
557  << " at eta= " << receta
558  << " and phi= " << recphi
559  << std::endl;
560 
561  }
562  }
563  else{
564  edm::LogVerbatim("TrackValidator") << "TrackingParticle #" << st
565  << " with pt=" << sqrt(tp->momentum().perp2())
566  << " NOT associated to any reco::Track" << "\n";
567  receta =-100.;
568  recphi =-100.;
569  recpt =-100.;
570  recd0 =-100.;
571  recz0 =-100;
572  respt =-100.;
573  resd0 =-100.;
574  resz0 =-100.;
575  resphi =-100.;
576  reseta =-100.;
577  rescottheta=-100.;
578  recnhit=100;
579  recchiq=-100;
580  eff=0;
581  flagrec=100;
582  }
583 
584  std::cout << "Eff=" << eff << std::endl;
585 
586  // simulated muons
587 
588  std::cout <<"Flag is" << flag << std::endl;
589  std::cout <<"RecFlag is" << flagrec << std::endl;
590 
591  if (countpart[0]==2 && flag==0) {
592  mzmu=sqrt(
593  (ene[0][0]+ene[0][1])*(ene[0][0]+ene[0][1])-
594  (px[0][0]+px[0][1])*(px[0][0]+px[0][1])-
595  (py[0][0]+py[0][1])*(py[0][0]+py[0][1])-
596  (pz[0][0]+pz[0][1])*(pz[0][0]+pz[0][1])
597  );
598  std::cout << "Mzmu " << mzmu << std::endl;
599  ptzmu=sqrt(
600  (px[0][0]+px[0][1])*(px[0][0]+px[0][1])+
601  (py[0][0]+py[0][1])*(py[0][0]+py[0][1])
602  );
603 
604  pLzmu=pz[0][0]+pz[0][1];
605  enezmu=ene[0][0]+ene[0][1];
606  phizmu=atan2((py[0][0]+py[0][1]),(px[0][0]+px[0][1]));
607  thetazmu=atan2(ptzmu,(pz[0][0]+pz[0][1]));
608  etazmu=-log(tan(thetazmu*3.14/360.));
609  yzmu=0.5*log((enezmu+pLzmu)/(enezmu-pLzmu));
610  mxptmu=std::max( ptmu[0][0], ptmu[0][1]);
611  minptmu=std::min( ptmu[0][0], ptmu[0][1]);
612  }
613  else {
614  mzmu=-100.;
615  ptzmu=-100.;
616  pLzmu=-100.;
617  enezmu=-100.;
618  etazmu=-100.;
619  phizmu=-100.;
620  thetazmu=-100.;
621  yzmu=-100.;
622  mxptmu=-100.;
623  minptmu=-100.;
624  }
625 
626  // reconstructed muons
627  if (countpartrec[0]==2 && flagrec==0 ){
628  recmzmu=sqrt(
629  (recene[0][0]+recene[0][1])*(recene[0][0]+recene[0][1])-
630  (recpx[0][0]+recpx[0][1])*(recpx[0][0]+recpx[0][1])-
631  (recpy[0][0]+recpy[0][1])*(recpy[0][0]+recpy[0][1])-
632  (recpz[0][0]+recpz[0][1])*(recpz[0][0]+recpz[0][1])
633  );
634  std::cout << "RecMzmu " << recmzmu << std::endl;
635  recptzmu=sqrt(
636  (recpx[0][0]+recpx[0][1])*(recpx[0][0]+recpx[0][1])+
637  (recpy[0][0]+recpy[0][1])*(recpy[0][0]+recpy[0][1])
638  );
639 
640  recpLzmu=recpz[0][0]+recpz[0][1];
641  recenezmu=recene[0][0]+recene[0][1];
642  recphizmu=atan2((recpy[0][0]+recpy[0][1]),(recpx[0][0]+recpx[0][1]));
643  recthetazmu=atan2(recptzmu,(recpz[0][0]+recpz[0][1]));
644  recetazmu=-log(tan(recthetazmu*3.14/360.));
646  recmxptmu=std::max(recptmu[0][0], recptmu[0][1]);
647  recminptmu=std::min( recptmu[0][0], recptmu[0][1]);
648  }
649  else {
650  recmzmu=-100.;
651  recptzmu=-100.;
652  recpLzmu=-100.;
653  recenezmu=-100.;
654  recetazmu=-100.;
655  recphizmu=-100.;
656  recthetazmu=-100.;
657  recyzmu=-100.;
658  recmxptmu=-100;
659  recminptmu=-100.;
660  }
661 
662  tree_eff->Fill();
663 
664  } // end of loop on muons
665  } // end of loop for tracking particle
666  } // end of loop for efficiency
667 
668  //
669  // Fake Rate
670  //
671  if (selection_fake ) {
672  std::cout << "Computing Fake Rate" << std::endl;
673 
675  faked0=0.,fakez0=0.;
676  fakenhit=0;
679  fakerecchiq = 0.;
680  fakerecnhit = 0;
681  faketrackType = 0;
682  fake=0;
683 
684 
685  // int at=0;
686  int rT=0;
687  for(reco::TrackCollection::size_type i=0; i<tC.size(); ++i){
688  edm::RefToBase<reco::Track> track(trackCollection, i);
689  rT++;
690 
692  faked0=0.,fakez0=0.;
693  fakenhit=0;
696  fakerecchiq = 0.;
697  fakerecnhit = 0;
698  faketrackType = 0;
699  fake=0;
700 
702  fakerecchiq=track->normalizedChi2();
703  fakerectheta=track->theta();
705  //fakereceta=-log(tan(rectheta/2.));
706  fakereceta=track->momentum().eta();
707  // fakereccostheta=cos(track->momentum().theta());
708  fakerecphi=track->phi();
709  fakerecpt=track->pt();
710  fakerecd0=track->d0();
711  fakerecz0=track->dz();
712 
713  std::cout << "1) Before assoc: TkRecTrack at eta = " << fakereceta << std::endl;
714  std::cout << "Track number "<< i << std::endl ;
715  std::cout << "\tPT: " << track->pt()<< std::endl;
716  std::cout << "\timpact parameter:d0: " << track->d0()<< std::endl;
717  std::cout << "\timpact parameter:z0: " << track->dz()<< std::endl;
718  std::cout << "\tAzimuthal angle of point of closest approach:" << track->phi()<< std::endl;
719  std::cout << "\tcharge: " << track->charge()<< std::endl;
720  std::cout << "\teta: " << track->eta()<< std::endl;
721  std::cout << "\tnormalizedChi2: " << track->normalizedChi2()<< std::endl;
722 
723 
724  std::vector<std::pair<TrackingParticleRef, double> > tp;
725 
726  //Compute fake rate vs eta
727  if(recSimColl.find(track) != recSimColl.end()){
728  tp = recSimColl[track];
729  if (tp.size()!=0) {
730  edm::LogVerbatim("TrackValidator") << "reco::Track #" << rT << " with pt=" << track->pt()
731  << " associated with quality:" << tp.begin()->second <<"\n";
732 
733 
734  TrackingParticleRef tpr = tp.begin()->first;
735  const SimTrack * fakeassocTrack = &(*tpr->g4Track_begin());
736 
738  iSetup.get<IdealMagneticFieldRecord>().get(theMF);
740  ftsAtProduction(GlobalPoint(tpr->vertex().x(),tpr->vertex().y(),tpr->vertex().z()),
741  GlobalVector(fakeassocTrack->momentum().x(),fakeassocTrack->momentum().y(),fakeassocTrack->momentum().z()),
742  TrackCharge(tpr->charge()),
743  theMF.product());
744  TSCPBuilderNoMaterial tscpBuilder;
745  TrajectoryStateClosestToPoint tsAtClosestApproach
746  = tscpBuilder(ftsAtProduction,GlobalPoint(0,0,0));//as in TrackProducerAlgorithm
747  GlobalPoint v = tsAtClosestApproach.theState().position();
748  GlobalVector p = tsAtClosestApproach.theState().momentum();
749 
750  // double qoverpSim = tsAtClosestApproach.charge()/p.mag();
751  // double lambdaSim = M_PI/2-p.theta();
752  // double phiSim = p.phi();
753  double dxySim = (-v.x()*sin(p.phi())+v.y()*cos(p.phi()));
754  double dszSim = v.z()*p.perp()/p.mag() - (v.x()*p.x()+v.y()*p.y())/p.perp() * p.z()/p.mag();
755  faked0 = float(-dxySim);
756  fakez0 = float(dszSim*p.mag()/p.perp());
757 
758 
759  faketrackType=fakeassocTrack->type();
760  faketheta=fakeassocTrack->momentum().theta();
762  fakeeta=fakeassocTrack->momentum().eta();
763  fakephi=fakeassocTrack->momentum().phi();
764  fakept=fakeassocTrack->momentum().pt();
765  fakenhit=tpr->matchedHit();
766 
767  std::cout << "4) After call to associator: the best SimTrack match is of type" << fakeassocTrack->type()
768  << " ,at eta = " << fakeeta
769  << " and phi = " << fakephi
770  << " ,with pt at vertex = " << fakept << " GeV/c"
771  << " ,d0 global = " << faked0
772  << " ,z0 = " << fakez0
773  << std::endl;
774  fake=1;
775 
782 
783  }
784  }
785  else{
786  edm::LogVerbatim("TrackValidator") << "reco::Track #" << rT << " with pt=" << track->pt()
787  << " NOT associated to any TrackingParticle" << "\n";
788 
789  fakeeta =-100.;
790  faketheta=-100;
791  fakephi =-100.;
792  fakept =-100.;
793  faked0 =-100.;
794  fakez0 =-100;
795  fakerespt =-100.;
796  fakeresd0 =-100.;
797  fakeresz0 =-100.;
798  fakeresphi =-100.;
799  fakereseta =-100.;
800  fakerescottheta=-100.;
801  fakenhit=100;
802  fake=0;
803  }
804 
805  tree_fake->Fill();
806  }
807 
808  } // End of loop on fakerate
809 
810  w++;
811 
812  } // End of loop on associators
813 }
814 
815 // ------------ method called once each job just after ending the event loop ------------
817 
818  std::cout << "\t Misalignment analysis completed \n" << std::endl;
819 
820 }
821 
RunNumber_t run() const
Definition: EventID.h:39
T getParameter(std::string const &) const
EventNumber_t event() const
Definition: EventID.h:41
T getUntrackedParameter(std::string const &, T const &) const
tuple t
Definition: tree.py:139
int i
Definition: DBlmapReader.cc:9
T perp() const
Definition: PV3DBase.h:72
double d0() const
dxy parameter in perigee convention (d0 = -dxy)
Definition: TrackBase.h:560
const double w
Definition: UKUtility.cc:23
std::vector< TrackingParticle > TrackingParticleCollection
const_iterator end() const
last iterator over the map (read only)
double normalizedChi2() const
chi-squared divided by n.d.o.f. (or chi-squared * 1e6 if n.d.o.f. is zero)
Definition: TrackBase.h:524
const FreeTrajectoryState & theState() const
double theta() const
polar angle
Definition: TrackBase.h:542
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
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
Geom::Phi< T > phi() const
Definition: PV3DBase.h:69
Global3DPoint GlobalPoint
Definition: GlobalPoint.h:10
T y() const
Definition: PV3DBase.h:63
size_type size() const
double phi() const
azimuthal angle of momentum vector
Definition: TrackBase.h:608
const Vector & momentum() const
track momentum vector
Definition: TrackBase.h:638
virtual void analyze(const edm::Event &, const edm::EventSetup &) override
uint16_t size_type
ValidationMisalignedTracker(const edm::ParameterSet &)
int TrackCharge
Definition: TrackCharge.h:4
int iEvent
Definition: GenABIO.cc:230
T mag() const
Definition: PV3DBase.h:67
double eta() const
pseudorapidity of momentum vector
Definition: TrackBase.h:614
T sqrt(T t)
Definition: SSEVec.h:48
double pt() const
track transverse momentum
Definition: TrackBase.h:584
T z() const
Definition: PV3DBase.h:64
int genpartIndex() const
index of the corresponding Generator particle in the Event container (-1 if no Genpart) ...
Definition: SimTrack.h:33
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
Tan< T >::type tan(const T &t)
Definition: Tan.h:22
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
int j
Definition: DBlmapReader.cc:9
unsigned short numberOfValidHits() const
number of valid hits found
Definition: TrackBase.h:783
T min(T a, T b)
Definition: MathUtil.h:58
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:413
GlobalVector momentum() const
#define LogTrace(id)
std::vector< std::string > associators
double dz() const
dz parameter (= dsz/cos(lambda)). This is the track z0 w.r.t (0,0,0) only if the refPoint is close to...
Definition: TrackBase.h:572
std::vector< edm::InputTag > label
GlobalPoint position() const
T const * product() const
Definition: Handle.h:81
const T & get() const
Definition: EventSetup.h:55
T const * product() const
Definition: ESHandle.h:86
edm::EventID id() const
Definition: EventBase.h:60
int type() const
particle type (HEP PDT convension)
Definition: CoreSimTrack.h:25
const math::XYZTLorentzVectorD & momentum() const
Definition: CoreSimTrack.h:22
tuple cout
Definition: gather_cfg.py:121
int charge() const
track electric charge
Definition: TrackBase.h:530
T x() const
Definition: PV3DBase.h:62
edm::ESHandle< MagneticField > theMF
Global3DVector GlobalVector
Definition: GlobalVector.h:10