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 
17 //#include "MagneticField/Engine/interface/MagneticField.h"
18 //#include "MagneticField/Records/interface/IdealMagneticField.h"
19 
23 // #include "RecoVertex/KinematicFitPrimitives/interface/"
29 
30 
31 #include <iostream>
32 
33 using namespace reco;
34 using namespace edm;
35 using namespace std;
36 
38  : theConfig(iConfig)
39 {
40  token_tracks = consumes<TrackCollection>(iConfig.getParameter<string>("TrackLabel"));
41  outputFile_ = iConfig.getUntrackedParameter<std::string>("outputFile");
42  kvfPSet = iConfig.getParameter<edm::ParameterSet>("KVFParameters");
43 // rootFile_ = TFile::Open(outputFile_.c_str(),"RECREATE");
44  edm::LogInfo("RecoVertex/KineExample")
45  << "Initializing KVF TEST analyser - Output file: " << outputFile_ <<"\n";
46 // token_TrackTruth = consumes<TrackingParticleCollection>(edm::InputTag("trackingtruth", "TrackTruth"));
47  token_VertexTruth = consumes<TrackingVertexCollection>(edm::InputTag("trackingtruth", "VertexTruth"));
48 }
49 
50 
52 // delete rootFile_;
53 }
54 
56 
57 // edm::ESHandle<MagneticField> magField;
58 // setup.get<IdealMagneticFieldRecord>().get(magField);
59 // tree.reset(new SimpleVertexTree("VertexFitter", magField.product()));
60 }
61 
62 
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 // &iSetup);
219 //
220 // tree->fill(tv, &sv, &recSimColl);
221 // }
222 
223  }
224  }
225 
226  }
227  catch (std::exception & err) {
228  cout << "Exception during event number: " << iEvent.id()
229  << "\n" << err.what() << "\n";
230  }
231 
232 }
233 
235 {
236  if (myVertex->vertexIsValid()) {
237  cout << "Decay vertex: " << myVertex->position() <<myVertex->chiSquared()<< " "<<myVertex->degreesOfFreedom()<<endl;
238  } else cout << "Decay vertex Not valid\n";
239 }
240 
242 {
243  cout << "Particle: \n";
244 //accessing the reconstructed Bs meson parameters:
245 //SK: uncomment if needed AlgebraicVector7 bs_par = myParticle->currentState().kinematicParameters().vector();
246 
247 //and their joint covariance matrix:
248 //SK:uncomment if needed AlgebraicSymMatrix77 bs_er = myParticle->currentState().kinematicParametersError().matrix();
249  cout << "Momentum at vertex: " << myParticle->currentState().globalMomentum ()<<endl;
250  cout << "Parameters at vertex: " << myParticle->currentState().kinematicParameters().vector()<<endl;
251 }
252 
254 {
255  if (!myTree->isValid()) {
256  cout <<"Tree is invalid. Fit failed.\n";
257  return;
258  }
259 
260 //accessing the tree components, move pointer to top
261  myTree->movePointerToTheTop();
262 
263 //We are now at the top of the decay tree getting the B_s reconstructed KinematicPartlcle
264  RefCountedKinematicParticle b_s = myTree->currentParticle();
265  printout(b_s);
266 
267 // The B_s decay vertex
268  RefCountedKinematicVertex b_dec_vertex = myTree->currentDecayVertex();
269  printout(b_dec_vertex);
270 
271  // Get all the children of Bs:
272  //In this way, the pointer is not moved
273  vector< RefCountedKinematicParticle > bs_children = myTree->finalStateParticles();
274 
275  for (unsigned int i=0;i< bs_children.size();++i) {
276  printout(bs_children[i]);
277  }
278 
279 //Now navigating down the tree , pointer is moved:
280  bool child = myTree->movePointerToTheFirstChild();
281 
282  if(child) while (myTree->movePointerToTheNextChild()) {
283  RefCountedKinematicParticle aChild = myTree->currentParticle();
284  printout(aChild);
285  }
286 }
287 
288 //Returns the first vertex in the list.
289 
291 {
292  // get the simulated vertices
294  iEvent.getByToken(token_VertexTruth,TVCollectionH);
295  const TrackingVertexCollection tPC = *(TVCollectionH.product());
296 
297 // Handle<edm::SimVertexContainer> simVtcs;
298 // iEvent.getByLabel("g4SimHits", simVtcs);
299 // std::cout << "SimVertex " << simVtcs->size() << std::endl;
300 // for(edm::SimVertexContainer::const_iterator v=simVtcs->begin();
301 // v!=simVtcs->end(); ++v){
302 // std::cout << "simvtx "
303 // << v->position().x() << " "
304 // << v->position().y() << " "
305 // << v->position().z() << " "
306 // << v->parentIndex() << " "
307 // << v->noParent() << " "
308 // << std::endl;
309 // }
310  return *(tPC.begin());
311 }
T getParameter(std::string const &) const
edm::EDGetTokenT< reco::TrackCollection > token_tracks
Definition: KineExample.h:63
T getUntrackedParameter(std::string const &, T const &) const
int i
Definition: DBlmapReader.cc:9
RefCountedKinematicTree fit(const std::vector< RefCountedKinematicParticle > &part)
void printout(const RefCountedKinematicVertex &myVertex) const
Definition: KineExample.cc:234
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:462
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
edm::ParameterSet kvfPSet
Definition: KineExample.h:58
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:63
std::pair< double, double > Point
Definition: CaloEllipse.h:18
KineExample(const edm::ParameterSet &)
Definition: KineExample.cc:37
std::vector< RefCountedKinematicTree > fit(KinematicConstraint *cs, const std::vector< RefCountedKinematicTree > &trees) const
virtual void beginRun(edm::Run const &, edm::EventSetup const &)
Definition: KineExample.cc:55
int iEvent
Definition: GenABIO.cc:230
edm::EDGetTokenT< TrackingVertexCollection > token_VertexTruth
Definition: KineExample.h:65
GlobalPoint position() const
bool isValid() const
Definition: HandleBase.h:75
RefCountedKinematicTree fit(const std::vector< RefCountedKinematicParticle > &particles) const
TrackingVertex getSimVertex(const edm::Event &iEvent) const
Definition: KineExample.cc:290
std::string outputFile_
Definition: KineExample.h:62
T const * product() const
Definition: Handle.h:81
std::vector< TrackingVertex > TrackingVertexCollection
const T & get() const
Definition: EventSetup.h:56
#define jpsi
edm::EventID id() const
Definition: EventBase.h:59
RefCountedKinematicParticle particle(const reco::TransientTrack &initialTrack, const ParticleMass &massGuess, float chiSquared, float degreesOfFr, float &m_sigma) const
tuple cout
Definition: gather_cfg.py:145
bool isValid() const
Definition: Run.h:43