CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
KineExample.cc
Go to the documentation of this file.
2 
11 
18 
22 // #include "RecoVertex/KinematicFitPrimitives/interface/"
28 
29 #include <iostream>
30 
31 using namespace reco;
32 using namespace edm;
33 using namespace std;
34 
36  : theConfig(iConfig)
37 {
38  token_tracks = consumes<TrackCollection>(iConfig.getParameter<string>("TrackLabel"));
39  outputFile_ = iConfig.getUntrackedParameter<std::string>("outputFile");
40  kvfPSet = iConfig.getParameter<edm::ParameterSet>("KVFParameters");
41 // rootFile_ = TFile::Open(outputFile_.c_str(),"RECREATE");
42  edm::LogInfo("RecoVertex/KineExample")
43  << "Initializing KVF TEST analyser - Output file: " << outputFile_ <<"\n";
44 // token_TrackTruth = consumes<TrackingParticleCollection>(edm::InputTag("trackingtruth", "TrackTruth"));
45  token_VertexTruth = consumes<TrackingVertexCollection>(edm::InputTag("trackingtruth", "VertexTruth"));
46 }
47 
48 
50 // delete rootFile_;
51 }
52 
54  edm::ESHandle<TrackAssociatorBase> theAssociatorForParamAtPca;
55  setup.get<TrackAssociatorRecord>().get("TrackAssociatorByChi2",theAssociatorForParamAtPca);
56  associatorForParamAtPca = (TrackAssociatorByChi2 *) theAssociatorForParamAtPca.product();
57 
58 // tree = new SimpleVertexTree("VertexFitter", associatorForParamAtPca);
59 }
60 
61 
63 // delete tree;
64 }
65 
66 //
67 // member functions
68 //
69 
70 void
72 {
73  try {
74 
75  cout << "Reconstructing event number: " << iEvent.id() << "\n";
76 
77  // get RECO tracks from the event
78  // `tks` can be used as a ptr to a reco::TrackCollection
80  iEvent.getByToken(token_tracks, tks);
81  if (!tks.isValid()) {
82  cout
83  << "Couln't find track collection: " << iEvent.id()
84  << "\n";
85  } else {
86 
87  edm::LogInfo("RecoVertex/KineExample")
88  << "Found: " << (*tks).size() << " reconstructed tracks" << "\n";
89  cout << "got " << (*tks).size() << " tracks " << endl;
90 
91  // Transform Track to TransientTrack
92  //get the builder:
94  iSetup.get<TransientTrackRecord>().get("TransientTrackBuilder",theB);
95  //do the conversion:
96  vector<TransientTrack> t_tks = (*theB).build(tks);
97 
98  cout << "Found: " << t_tks.size() << " reconstructed tracks" << "\n";
99 
100  // Do a KindFit, if >= 4 tracks.
101  if (t_tks.size() > 3) {
102 
103  // For a first test, suppose that the first four tracks are the 2 muons,
104  // then the 2 kaons. Since this will not be true, the result of the fit
105  // will not be meaningfull, but at least you will get the idea of how to
106  // do such a fit.
107 
108  //First, to get started, a simple vertex fit:
109 
110  vector<TransientTrack> ttv;
111  ttv.push_back(t_tks[0]); ttv.push_back(t_tks[1]); ttv.push_back(t_tks[2]);ttv.push_back(t_tks[3]);
112  KalmanVertexFitter kvf(false);
113  TransientVertex tv = kvf.vertex(ttv);
114  if (!tv.isValid()) cout << "KVF failed\n";
115  else std::cout << "KVF fit Position: " << Vertex::Point(tv.position()) << "\n";
116 
117 
118  TransientTrack ttMuPlus = t_tks[0];
119  TransientTrack ttMuMinus = t_tks[1];
120  TransientTrack ttKPlus = t_tks[2];
121  TransientTrack ttKMinus = t_tks[3];
122 
123  //the final state muons and kaons from the Bs->J/PsiPhi->mumuKK decay
124  //Creating a KinematicParticleFactory
126 
127  //The mass of a muon and the insignificant mass sigma to avoid singularities in the covariance matrix.
128  ParticleMass muon_mass = 0.1056583;
129  ParticleMass kaon_mass = 0.493677;
130  float muon_sigma = 0.0000001;
131  float kaon_sigma = 0.000016;
132 
133  //initial chi2 and ndf before kinematic fits. The chi2 of the reconstruction is not considered
134  float chi = 0.;
135  float ndf = 0.;
136 
137  //making particles
138  vector<RefCountedKinematicParticle> muonParticles;
139  vector<RefCountedKinematicParticle> phiParticles;
140  vector<RefCountedKinematicParticle> allParticles;
141  muonParticles.push_back(pFactory.particle (ttMuPlus,muon_mass,chi,ndf,muon_sigma));
142  muonParticles.push_back(pFactory.particle (ttMuMinus,muon_mass,chi,ndf,muon_sigma));
143  allParticles.push_back(pFactory.particle (ttMuPlus,muon_mass,chi,ndf,muon_sigma));
144  allParticles.push_back(pFactory.particle (ttMuMinus,muon_mass,chi,ndf,muon_sigma));
145 
146  phiParticles.push_back(pFactory.particle (ttKPlus,kaon_mass,chi,ndf,kaon_sigma));
147  phiParticles.push_back(pFactory.particle (ttKMinus,kaon_mass,chi,ndf,kaon_sigma));
148  allParticles.push_back(pFactory.particle (ttKPlus,kaon_mass,chi,ndf,kaon_sigma));
149  allParticles.push_back(pFactory.particle (ttKMinus,kaon_mass,chi,ndf,kaon_sigma));
150 
151  /* Example of a simple vertex fit, without other constraints
152  * The reconstructed decay tree is a result of the kinematic fit
153  * The KinematicParticleVertexFitter fits the final state particles to their vertex and
154  * reconstructs the decayed state
155  */
157  cout <<"Simple vertex fit with KinematicParticleVertexFitter:\n";
158  RefCountedKinematicTree vertexFitTree = fitter.fit(allParticles);
159 
160  printout(vertexFitTree);
161 
163 
164  //creating the constraint for the J/Psi mass
165  ParticleMass jpsi = 3.09687;
166 
167  //creating the two track mass constraint
169 
170  //creating the fitter
172 
173  //obtaining the resulting tree
174  RefCountedKinematicTree myTree = kcvFitter.fit(allParticles, j_psi_c);
175 
176  cout << "\nGlobal fit done:\n";
177  printout(myTree);
178 
179  //creating the vertex fitter
181 
182  //reconstructing a J/Psi decay
183  RefCountedKinematicTree jpTree = kpvFitter.fit(muonParticles);
184 
185  //creating the particle fitter
186  KinematicParticleFitter csFitter;
187 
188  // creating the constraint
189  float jp_m_sigma = 0.00004;
190  KinematicConstraint * jpsi_c2 = new MassKinematicConstraint(jpsi,jp_m_sigma);
191 
192  //the constrained fit:
193  jpTree = csFitter.fit(jpsi_c2,jpTree);
194 
195  //getting the J/Psi KinematicParticle and putting it together with the kaons.
196  //The J/Psi KinematicParticle has a pointer to the tree it belongs to
197  jpTree->movePointerToTheTop();
198  RefCountedKinematicParticle jpsi_part = jpTree->currentParticle();
199  phiParticles.push_back(jpsi_part);
200 
201  //making a vertex fit and thus reconstructing the Bs parameters
202  // the resulting tree includes all the final state tracks, the J/Psi meson,
203  // its decay vertex, the Bs meson and its decay vertex.
204  RefCountedKinematicTree bsTree = kpvFitter.fit(phiParticles);
205  cout << "Sequential fit done:\n";
206  printout(bsTree);
207 
208 
209 
210 // // For the analysis: compare to your SimVertex
211 // TrackingVertex sv = getSimVertex(iEvent);
212 // edm::Handle<TrackingParticleCollection> TPCollectionH ;
213 // iEvent.getByToken(token_TrackTruth, TPCollectionH);
214 // const TrackingParticleCollection tPC = *(TPCollectionH.product());
215 // reco::RecoToSimCollection recSimColl=associatorForParamAtPca->associateRecoToSim(tks,
216 // TPCollectionH,
217 // &iEvent);
218 //
219 // tree->fill(tv, &sv, &recSimColl);
220 // }
221 
222  }
223  }
224 
225  }
226  catch (std::exception & err) {
227  cout << "Exception during event number: " << iEvent.id()
228  << "\n" << err.what() << "\n";
229  }
230 
231 }
232 
234 {
235  if (myVertex->vertexIsValid()) {
236  cout << "Decay vertex: " << myVertex->position() <<myVertex->chiSquared()<< " "<<myVertex->degreesOfFreedom()<<endl;
237  } else cout << "Decay vertex Not valid\n";
238 }
239 
241 {
242  cout << "Particle: \n";
243 //accessing the reconstructed Bs meson parameters:
244 //SK: uncomment if needed AlgebraicVector7 bs_par = myParticle->currentState().kinematicParameters().vector();
245 
246 //and their joint covariance matrix:
247 //SK:uncomment if needed AlgebraicSymMatrix77 bs_er = myParticle->currentState().kinematicParametersError().matrix();
248  cout << "Momentum at vertex: " << myParticle->currentState().globalMomentum ()<<endl;
249  cout << "Parameters at vertex: " << myParticle->currentState().kinematicParameters().vector()<<endl;
250 }
251 
253 {
254  if (!myTree->isValid()) {
255  cout <<"Tree is invalid. Fit failed.\n";
256  return;
257  }
258 
259 //accessing the tree components, move pointer to top
260  myTree->movePointerToTheTop();
261 
262 //We are now at the top of the decay tree getting the B_s reconstructed KinematicPartlcle
263  RefCountedKinematicParticle b_s = myTree->currentParticle();
264  printout(b_s);
265 
266 // The B_s decay vertex
267  RefCountedKinematicVertex b_dec_vertex = myTree->currentDecayVertex();
268  printout(b_dec_vertex);
269 
270  // Get all the children of Bs:
271  //In this way, the pointer is not moved
272  vector< RefCountedKinematicParticle > bs_children = myTree->finalStateParticles();
273 
274  for (unsigned int i=0;i< bs_children.size();++i) {
275  printout(bs_children[i]);
276  }
277 
278 //Now navigating down the tree , pointer is moved:
279  bool child = myTree->movePointerToTheFirstChild();
280 
281  if(child) while (myTree->movePointerToTheNextChild()) {
282  RefCountedKinematicParticle aChild = myTree->currentParticle();
283  printout(aChild);
284  }
285 }
286 
287 //Returns the first vertex in the list.
288 
290 {
291  // get the simulated vertices
293  iEvent.getByToken(token_VertexTruth,TVCollectionH);
294  const TrackingVertexCollection tPC = *(TVCollectionH.product());
295 
296 // Handle<edm::SimVertexContainer> simVtcs;
297 // iEvent.getByLabel("g4SimHits", simVtcs);
298 // std::cout << "SimVertex " << simVtcs->size() << std::endl;
299 // for(edm::SimVertexContainer::const_iterator v=simVtcs->begin();
300 // v!=simVtcs->end(); ++v){
301 // std::cout << "simvtx "
302 // << v->position().x() << " "
303 // << v->position().y() << " "
304 // << v->position().z() << " "
305 // << v->parentIndex() << " "
306 // << v->noParent() << " "
307 // << std::endl;
308 // }
309  return *(tPC.begin());
310 }
T getParameter(std::string const &) const
edm::EDGetTokenT< reco::TrackCollection > token_tracks
Definition: KineExample.h:65
T getUntrackedParameter(std::string const &, T const &) const
int i
Definition: DBlmapReader.cc:9
RefCountedKinematicTree fit(const std::vector< RefCountedKinematicParticle > &part)
TrackAssociatorByChi2 * associatorForParamAtPca
Definition: KineExample.h:60
void printout(const RefCountedKinematicVertex &myVertex) const
Definition: KineExample.cc:233
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:434
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
edm::ParameterSet kvfPSet
Definition: KineExample.h:59
virtual CachingVertex< 5 > vertex(const std::vector< reco::TransientTrack > &tracks) const
virtual void analyze(const edm::Event &, const edm::EventSetup &)
Definition: KineExample.cc:71
double ParticleMass
Definition: ParticleMass.h:5
virtual void endJob()
Definition: KineExample.cc:62
std::pair< double, double > Point
Definition: CaloEllipse.h:18
KineExample(const edm::ParameterSet &)
Definition: KineExample.cc:35
std::vector< RefCountedKinematicTree > fit(KinematicConstraint *cs, const std::vector< RefCountedKinematicTree > &trees) const
virtual void beginRun(edm::Run const &, edm::EventSetup const &)
Definition: KineExample.cc:53
int iEvent
Definition: GenABIO.cc:230
edm::EDGetTokenT< TrackingVertexCollection > token_VertexTruth
Definition: KineExample.h:67
GlobalPoint position() const
bool isValid() const
Definition: HandleBase.h:76
RefCountedKinematicTree fit(const std::vector< RefCountedKinematicParticle > &particles) const
TrackingVertex getSimVertex(const edm::Event &iEvent) const
Definition: KineExample.cc:289
std::string outputFile_
Definition: KineExample.h:64
std::vector< TrackingVertex > TrackingVertexCollection
const T & get() const
Definition: EventSetup.h:55
T const * product() const
Definition: ESHandle.h:62
T const * product() const
Definition: Handle.h:81
#define jpsi
edm::EventID id() const
Definition: EventBase.h:56
RefCountedKinematicParticle particle(const reco::TransientTrack &initialTrack, const ParticleMass &massGuess, float chiSquared, float degreesOfFr, float &m_sigma) const
tuple cout
Definition: gather_cfg.py:121
bool isValid() const
void setup(std::vector< TH2F > &depth, std::string name, std::string units="")
Definition: Run.h:41