CMS 3D CMS Logo

TrackParameterAnalyzer.cc
Go to the documentation of this file.
2 
3 //system includes
4 #include <memory>
5 #include <vector>
6 
7 // core framework
12 
13 // Hep MC stuff from CLHEP
14 #include "CLHEP/Vector/LorentzVector.h"
15 
16 // track
18 
19 // Root
20 #include <TH1.h>
21 #include <TH2.h>
22 #include <TFile.h>
23 
24 //
25 //
26 // constants, enums and typedefs
27 //
28 
29 //
30 // static data member definitions
31 //
32 
33 //
34 // constructors and destructor
35 //
37  : edmSimVertexContainerToken_( consumes<edm::SimVertexContainer>( iConfig.getParameter<edm::InputTag>( "simG4" ) ) )
38  , edmSimTrackContainerToken_( consumes<edm::SimTrackContainer>( iConfig.getParameter<edm::InputTag>( "simG4" ) ) )
39  , recoTrackCollectionToken_( consumes<reco::TrackCollection>( edm::InputTag( iConfig.getUntrackedParameter<std::string>( "recoTrackProducer" ) ) ) )
40  , outputFile_( iConfig.getUntrackedParameter<std::string>( "outputFile" ) )
41  , simUnit_( 1.0 ) // starting from CMSSW_1_2_x, I think
42  , verbose_( iConfig.getUntrackedParameter<bool>( "verbose", false ) ) {
43  //now do whatever initialization is needed
44  // open output file to store histograms}
45  auto tversion = edm::getReleaseVersion();
46  tversion = tversion.erase(tversion.size() - 1, 1).erase(0, 1);
47  outputFile_ = tversion + "_" + outputFile_;
48  rootFile_ = TFile::Open(outputFile_.c_str(),"RECREATE");
49  if ( (edm::getReleaseVersion()).find("CMSSW_1_1_",0)!=std::string::npos){
50  simUnit_=0.1; // for use in CMSSW_1_1_1 tutorial
51  }
52 }
53 
54 
56 {
57  // do anything here that needs to be done at destruction time
58  // (e.g. close files, deallocate resources etc.)
59  delete rootFile_;
60 }
61 
62 
63 
64 //
65 // member functions
66 //
68  std::cout << " TrackParameterAnalyzer::beginJob conversion from sim units to rec units is " << simUnit_ << std::endl;
69 
70  rootFile_->cd();
71  h1_pull0_ = new TH1F("pull0","pull q/p",100,-25.,25.);
72  h1_pull1_ = new TH1F("pull1","pull lambda",100,-25.,25.);
73  h1_pull2_ = new TH1F("pull2","pull phi ",100,-25.,25.);
74  h1_pull3_ = new TH1F("pull3","pull dca ",100,-25.,25.);
75  h1_pull4_ = new TH1F("pull4","pull zdca ",100,-25.,25.);
76 
77  h1_res0_ = new TH1F("res0","res q/p",100,-0.1,0.1);
78  h1_res1_ = new TH1F("res1","res lambda",100,-0.1,0.1);
79  h1_res2_ = new TH1F("res2","res phi ",100,-0.1,0.1);
80  h1_res3_ = new TH1F("res3","res dca ",100,-0.1,0.1);
81  h1_res4_ = new TH1F("res4","res zdca ",100,-0.1,0.1);
82 
83  h1_Beff_ = new TH1F("Beff", "Beff",2000,-10.,10.);
84  h2_dvsphi_ = new TH2F("dvsphi","dvsphi",360,-M_PI,M_PI,100,-0.1,0.1);
85  h1_par0_ = new TH1F("par0","q/p",100,-0.1,0.1);
86  h1_par1_ = new TH1F("par1","lambda",100, -M_PI/2.,M_PI/2.);
87  h1_par2_ = new TH1F("par2","phi ",100,-M_PI,M_PI);
88  h1_par3_ = new TH1F("par3","dca ",100,-0.1,0.1);
89  h1_par4_ = new TH1F("par4","zdca ",1000,-10.,10.);
90 }
91 
92 
94  rootFile_->cd();
95  h1_pull0_->Write();
96  h1_pull1_->Write();
97  h1_pull2_->Write();
98  h1_pull3_->Write();
99  h1_pull4_->Write();
100 
101  h1_res0_->Write();
102  h1_res1_->Write();
103  h1_res2_->Write();
104  h1_res3_->Write();
105  h1_res4_->Write();
106 
107  h1_Beff_->Write();
108  h2_dvsphi_->Write();
109  h1_par0_->Write();
110  h1_par1_->Write();
111  h1_par2_->Write();
112  h1_par3_->Write();
113  h1_par4_->Write();
114 }
115 
116 // helper function
118  const ParameterVector &b){
119  double dtheta=a(1)-b(1);
120  double dphi =a(2)-b(2);
121  if (dphi>M_PI){ dphi-=M_2_PI; }else if(dphi<-M_PI){dphi+=M_2_PI;}
122  return ( (fabs(dtheta)<0.02) && (fabs(dphi)<0.04) );
123 }
124 
125 // ------------ method called to produce the data ------------
126 void
128 {
129  using CLHEP::HepLorentzVector;
130 
131  const double fBfield=3.8;
132 
134  iEvent.getByToken( edmSimVertexContainerToken_, simVtcs );
135  if(verbose_){
136  std::cout << "SimVertex " << simVtcs->size() << std::endl;
137  for(edm::SimVertexContainer::const_iterator v=simVtcs->begin();
138  v!=simVtcs->end(); ++v){
139  std::cout << "simvtx "
140  << std::setw(10) << std::setprecision(4)
141  << v->position().x() << " "
142  << v->position().y() << " "
143  << v->position().z() << " "
144  << v->parentIndex() << " "
145  << v->noParent() << " "
146  << std::endl;
147  }
148  }
149 
150  // get the simulated tracks, extract perigee parameters
152  iEvent.getByToken( edmSimTrackContainerToken_, simTrks );
153 
154  if(verbose_){std::cout << "simtrks " << simTrks->size() << std::endl;}
155  std::vector<ParameterVector > tsim;
156  for(edm::SimTrackContainer::const_iterator t=simTrks->begin();
157  t!=simTrks->end(); ++t){
158  if (t->noVertex()){
159  std::cout << "simtrk has no vertex" << std::endl;
160  return;
161  }else{
162  // get the vertex position
163  HepLorentzVector v((*simVtcs)[t->vertIndex()].position().x(),
164  (*simVtcs)[t->vertIndex()].position().y(),
165  (*simVtcs)[t->vertIndex()].position().z(),
166  (*simVtcs)[t->vertIndex()].position().e());
167  int pdgCode=t->type();
168 
169  if( pdgCode==-99 ){
170  // such entries cause crashes, no idea what they are
171  std::cout << "funny particle skipped , code=" << pdgCode << std::endl;
172  }else{
173  double Q=0;
174  if ((pdgCode==11)||(pdgCode==13)||(pdgCode==15)||(pdgCode==-211)||(pdgCode==-2212)||(pdgCode==321)){Q=-1;}
175  else if((pdgCode==-11)||(pdgCode==-13)||(pdgCode==-15)||(pdgCode==211)||(pdgCode==2212)||(pdgCode==321)){Q=1;}
176  else {
177  std::cout << pdgCode << " " <<std::endl;
178  }
179  HepLorentzVector p(t->momentum().x(),t->momentum().y(),t->momentum().z(),t->momentum().e());
180  if(verbose_){
181  std::cout << "simtrk "
182  << " gen=" << std::setw( 4) << t->genpartIndex()
183  << " vtx=" << std::setw( 4) << t->vertIndex()
184  << " pdg=" << std::setw( 5) << t->type()
185  << " Q=" << std::setw( 3) << Q
186  << " pt=" << std::setw(6) << p.perp()
187  << " vx=" << std::setw(6) << v.x()
188  << " vy=" << std::setw(6) << v.y()
189  << " vz=" << std::setw(6) << v.z()
190  << std::endl;
191  }
192  if ( (Q != 0) && (p.perp()>0.1) ){
193  double x0=v.x()*simUnit_;
194  double y0=v.y()*simUnit_;
195  double z0=v.z()*simUnit_;
196  double kappa=-Q*0.002998*fBfield/p.perp();
197  double D0=x0*sin(p.phi())-y0*cos(p.phi())-0.5*kappa*(x0*x0+y0*y0);
198  double q=sqrt(1.-2.*kappa*D0);
199  double s0=(x0*cos(p.phi())+y0*sin(p.phi()))/q;
200  double s1;
201  if (fabs(kappa*s0)>0.001){
202  s1=asin(kappa*s0)/kappa;
203  }else{
204  double ks02=(kappa*s0)*(kappa*s0);
205  s1=s0*(1.+ks02/6.+3./40.*ks02*ks02+5./112.*pow(ks02,3));
206  }
207  ParameterVector par;
208  par[reco::TrackBase::i_qoverp] = Q/sqrt(p.perp2()+p.pz()*p.pz());
209  par[reco::TrackBase::i_lambda] = M_PI/2.-p.theta();
210  par[reco::TrackBase::i_phi] = p.phi()-asin(kappa*s0);
211  par[reco::TrackBase::i_dxy] = 2.*D0/(1.+q);
212  par[reco::TrackBase::i_dsz] = z0*sin(p.theta())-s1*cos(p.theta());
213  tsim.push_back(par);
214  }
215  }
216  }// has vertex
217  }//for loop
218 
219  // simtrack parameters are in now tsim
220  // loop over tracks and try to match them to simulated tracks
221 
222 
224  iEvent.getByToken( recoTrackCollectionToken_, recTracks );
225 
226  for(reco::TrackCollection::const_iterator t=recTracks->begin();
227  t!=recTracks->end(); ++t){
228  reco::TrackBase::ParameterVector p = t->parameters();
229  reco::TrackBase::CovarianceMatrix c = t->covariance();
230  if(verbose_){
231  std::cout << "reco pars= " << p << std::endl;
232  }
233  for(std::vector<ParameterVector>::const_iterator s=tsim.begin();
234  s!=tsim.end(); ++s){
235  if (match(*s,p)){
236  h1_pull0_->Fill((p(0)-(*s)(0))/sqrt(c(0,0)));
237  h1_pull1_->Fill((p(1)-(*s)(1))/sqrt(c(1,1)));
238  h1_pull2_->Fill((p(2)-(*s)(2))/sqrt(c(2,2)));
239  h1_pull3_->Fill((p(3)-(*s)(3))/sqrt(c(3,3)));
240  h1_pull4_->Fill((p(4)-(*s)(4))/sqrt(c(4,4)));
241 
242  h1_res0_->Fill(p(0)-(*s)(0));
243  h1_res1_->Fill(p(1)-(*s)(1));
244  h1_res2_->Fill(p(2)-(*s)(2));
245  h1_res3_->Fill(p(3)-(*s)(3));
246  h1_res4_->Fill(p(4)-(*s)(4));
247 
248  h1_Beff_->Fill(p(0)/(*s)(0)*fBfield);
249  h2_dvsphi_->Fill(p(2),p(3));
250  h1_par0_->Fill(p(0));
251  h1_par1_->Fill(p(1));
252  h1_par2_->Fill(p(2));
253  h1_par3_->Fill(p(3));
254  h1_par4_->Fill(p(4));
255  }
256  }
257  }
258 
259 }
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:508
Divides< arg, void > D0
Definition: Factorize.h:144
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
std::vector< Track > TrackCollection
collection of Tracks
Definition: TrackFwd.h:14
TrackParameterAnalyzer(const edm::ParameterSet &)
math::Vector< dimension >::type ParameterVector
parameter vector
Definition: TrackBase.h:74
bool match(const ParameterVector &a, const ParameterVector &b)
int iEvent
Definition: GenABIO.cc:230
edm::EDGetTokenT< edm::SimTrackContainer > edmSimTrackContainerToken_
T sqrt(T t)
Definition: SSEVec.h:18
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
edm::EDGetTokenT< reco::TrackCollection > recoTrackCollectionToken_
virtual void analyze(const edm::Event &, const edm::EventSetup &)
#define M_PI
std::string getReleaseVersion()
edm::EDGetTokenT< edm::SimVertexContainer > edmSimVertexContainerToken_
std::vector< SimVertex > SimVertexContainer
double b
Definition: hdecay.h:120
reco::TrackBase::ParameterVector ParameterVector
fixed size matrix
HLT enums.
double a
Definition: hdecay.h:121
static const G4double kappa
std::vector< SimTrack > SimTrackContainer
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:40
math::Error< dimension >::type CovarianceMatrix
5 parameter covariance matrix
Definition: TrackBase.h:77