CMS 3D CMS Logo

KineExample.cc

Go to the documentation of this file.
00001 #include "RecoVertex/KinematicFit/plugins/KineExample.h"
00002 
00003 #include "DataFormats/VertexReco/interface/Vertex.h"
00004 #include "DataFormats/VertexReco/interface/VertexFwd.h"
00005 #include "DataFormats/TrackReco/interface/Track.h"
00006 #include "DataFormats/TrackReco/interface/TrackFwd.h"
00007 #include "DataFormats/Common/interface/EDProduct.h"
00008 #include "DataFormats/Common/interface/Handle.h"
00009 #include "FWCore/Framework/interface/MakerMacros.h"
00010 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00011 #include "FWCore/Framework/interface/ESHandle.h"
00012 
00013 #include "TrackingTools/TransientTrack/interface/TransientTrackBuilder.h"
00014 #include "TrackingTools/Records/interface/TransientTrackRecord.h"
00015 #include "TrackingTools/TransientTrack/interface/TransientTrack.h"
00016 #include "RecoVertex/VertexPrimitives/interface/TransientVertex.h"
00017 #include "RecoVertex/KalmanVertexFit/interface/KalmanVertexFitter.h"
00018 #include "SimTracker/Records/interface/TrackAssociatorRecord.h"
00019 
00020 #include "RecoVertex/KinematicFitPrimitives/interface/ParticleMass.h"
00021 #include "RecoVertex/KinematicFitPrimitives/interface/MultiTrackKinematicConstraint.h"
00022 #include <RecoVertex/KinematicFitPrimitives/interface/KinematicParticleFactoryFromTransientTrack.h>
00023 // #include "RecoVertex/KinematicFitPrimitives/interface/"
00024 #include "RecoVertex/KinematicFit/interface/KinematicConstrainedVertexFitter.h"
00025 #include "RecoVertex/KinematicFit/interface/TwoTrackMassKinematicConstraint.h"
00026 #include "RecoVertex/KinematicFit/interface/KinematicParticleVertexFitter.h"
00027 #include "RecoVertex/KinematicFit/interface/KinematicParticleFitter.h"
00028 #include "RecoVertex/KinematicFit/interface/MassKinematicConstraint.h"
00029 
00030 #include <iostream>
00031 
00032 using namespace reco;
00033 using namespace edm;
00034 using namespace std;
00035 
00036 KineExample::KineExample(const edm::ParameterSet& iConfig)
00037   : theConfig(iConfig)
00038 {
00039   trackLabel_ = iConfig.getParameter<std::string>("TrackLabel");
00040   outputFile_ = iConfig.getUntrackedParameter<std::string>("outputFile");
00041   kvfPSet = iConfig.getParameter<edm::ParameterSet>("KVFParameters");
00042 //   rootFile_ = TFile::Open(outputFile_.c_str(),"RECREATE");
00043   edm::LogInfo("RecoVertex/KineExample")
00044     << "Initializing KVF TEST analyser  - Output file: " << outputFile_ <<"\n";
00045 }
00046 
00047 
00048 KineExample::~KineExample() {
00049 //   delete rootFile_;
00050 }
00051 
00052 void KineExample::beginJob(edm::EventSetup const& setup){
00053   edm::ESHandle<TrackAssociatorBase> theAssociatorForParamAtPca;
00054   setup.get<TrackAssociatorRecord>().get("TrackAssociatorByChi2",theAssociatorForParamAtPca);
00055   associatorForParamAtPca = (TrackAssociatorByChi2 *) theAssociatorForParamAtPca.product();
00056 
00057 //   tree = new SimpleVertexTree("VertexFitter", associatorForParamAtPca);
00058 }
00059 
00060 
00061 void KineExample::endJob() {
00062 //   delete tree;
00063 }
00064 
00065 //
00066 // member functions
00067 //
00068 
00069 void
00070 KineExample::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup)
00071 {
00072 
00073   edm::LogInfo("RecoVertex/KineExample")
00074     << "Reconstructing event number: " << iEvent.id() << "\n";
00075   
00076   // get RECO tracks from the event
00077   // `tks` can be used as a ptr to a reco::TrackCollection
00078   edm::Handle<reco::TrackCollection> tks;
00079   iEvent.getByLabel(trackLabel_, tks);
00080   if (!tks.isValid()) {
00081     cout
00082       << "Exception during event number: " << iEvent.id()
00083       << "\n";
00084   } else {
00085 
00086     edm::LogInfo("RecoVertex/KineExample")
00087       << "Found: " << (*tks).size() << " reconstructed tracks" << "\n";
00088     cout << "got " << (*tks).size() << " tracks " << endl;
00089     
00090     // Transform Track to TransientTrack
00091     
00092     //get the builder:
00093     edm::ESHandle<TransientTrackBuilder> theB;
00094     iSetup.get<TransientTrackRecord>().get("TransientTrackBuilder",theB);
00095     //do the conversion:
00096     vector<TransientTrack> t_tks = (*theB).build(tks);
00097     
00098     edm::LogInfo("RecoVertex/KineExample")
00099       << "Found: " << t_tks.size() << " reconstructed tracks" << "\n";
00100     
00101     // Do a KindFit, if >= 4 tracks.
00102     if (t_tks.size() > 3) {
00103       
00104       // For a first test, suppose that the first four tracks are the 2 muons,
00105       // then the 2 kaons. Since this will not be true, the result of the fit
00106       // will not be meaningfull, but at least you will get the idea of how to
00107       // do such a fit.
00108       
00109       //First, to get started, a simple vertex fit:
00110       
00111       vector<TransientTrack> ttv;  
00112       ttv.push_back(t_tks[0]); ttv.push_back(t_tks[1]); ttv.push_back(t_tks[2]);ttv.push_back(t_tks[3]);
00113       KalmanVertexFitter kvf(false);
00114       TransientVertex tv = kvf.vertex(ttv);
00115       
00116       std::cout << "Position: " << Vertex::Point(tv.position()) << "\n";
00117       
00118       
00119       TransientTrack ttMuPlus = t_tks[0];
00120       TransientTrack ttMuMinus = t_tks[1];
00121       TransientTrack ttKPlus = t_tks[2];
00122       TransientTrack ttKMinus = t_tks[3];
00123       
00124       //the final state muons and kaons from the Bs->J/PsiPhi->mumuKK decay
00125       //Creating a KinematicParticleFactory
00126       KinematicParticleFactoryFromTransientTrack pFactory;
00127       
00128       //The mass of a muon and the insignificant mass sigma to avoid singularities in the covariance matrix.
00129       ParticleMass muon_mass = 0.1056583;
00130       ParticleMass kaon_mass = 0.493677;
00131       float muon_sigma = 0.0000000001;
00132       float kaon_sigma = 0.000016;
00133       
00134       //initial chi2 and ndf before kinematic fits. The chi2 of the reconstruction is not considered
00135       float chi = 0.;
00136       float ndf = 0.;
00137       
00138       //making particles
00139       vector<RefCountedKinematicParticle> muonParticles;
00140       vector<RefCountedKinematicParticle> phiParticles;
00141       vector<RefCountedKinematicParticle> allParticles;
00142       muonParticles.push_back(pFactory.particle (ttMuPlus,muon_mass,chi,ndf,muon_sigma));
00143       muonParticles.push_back(pFactory.particle (ttMuMinus,muon_mass,chi,ndf,muon_sigma));
00144       allParticles.push_back(pFactory.particle (ttMuPlus,muon_mass,chi,ndf,muon_sigma));
00145       allParticles.push_back(pFactory.particle (ttMuMinus,muon_mass,chi,ndf,muon_sigma));
00146       
00147       phiParticles.push_back(pFactory.particle (ttKPlus,kaon_mass,chi,ndf,kaon_sigma));
00148       phiParticles.push_back(pFactory.particle (ttKMinus,kaon_mass,chi,ndf,kaon_sigma));
00149       allParticles.push_back(pFactory.particle (ttKPlus,kaon_mass,chi,ndf,kaon_sigma));
00150       allParticles.push_back(pFactory.particle (ttKMinus,kaon_mass,chi,ndf,kaon_sigma));
00151       
00152       /* Example of a simple vertex fit, without other constraints
00153        * The reconstructed decay tree is a result of the kinematic fit
00154        * The KinematicParticleVertexFitter fits the final state particles to their vertex and
00155        * reconstructs the decayed state
00156        */
00157       KinematicParticleVertexFitter fitter;
00158       RefCountedKinematicTree vertexFitTree = fitter.fit(allParticles);
00159       
00160       printout(vertexFitTree);
00161       
00163         
00164         //creating the constraint for the J/Psi mass
00165         ParticleMass jpsi = 3.09687;
00166         
00167         //creating the two track mass constraint
00168         MultiTrackKinematicConstraint *  j_psi_c = new  TwoTrackMassKinematicConstraint(jpsi);
00169         
00170         //creating the fitter
00171         KinematicConstrainedVertexFitter kcvFitter;
00172         
00173         //obtaining the resulting tree
00174         RefCountedKinematicTree myTree = kcvFitter.fit(allParticles, j_psi_c);
00175         
00176         cout << "Global fit done:\n";
00177         printout(myTree);
00178         
00179         //creating the vertex fitter
00180         KinematicParticleVertexFitter kpvFitter;
00181         
00182         //reconstructing a J/Psi decay
00183         RefCountedKinematicTree jpTree = kpvFitter.fit(muonParticles);
00184         
00185         //creating the particle fitter
00186         KinematicParticleFitter csFitter;
00187         
00188         // creating the constraint
00189         float jp_m_sigma = 0.00004;
00190         KinematicConstraint * jpsi_c2 = new MassKinematicConstraint(jpsi,jp_m_sigma);
00191         
00192         //the constrained fit:  
00193         jpTree = csFitter.fit(jpsi_c2,jpTree);
00194         
00195         //getting the J/Psi KinematicParticle and putting it together with the kaons.
00196         //The J/Psi KinematicParticle has a pointer to the tree it belongs to  
00197         jpTree->movePointerToTheTop();
00198         RefCountedKinematicParticle jpsi_part = jpTree->currentParticle();
00199         phiParticles.push_back(jpsi_part);
00200         
00201         //making a vertex fit and thus reconstructing the Bs parameters  
00202         // the resulting tree includes all the final state tracks, the J/Psi meson,
00203         // its decay vertex, the Bs meson and its decay vertex.
00204         RefCountedKinematicTree bsTree = kpvFitter.fit(phiParticles); 
00205         cout << "Sequential fit done:\n";
00206         printout(bsTree);
00207         
00208         
00209         
00210 //       // For the analysis: compare to your SimVertex
00211 //       TrackingVertex sv = getSimVertex(iEvent);
00212 //   edm::Handle<TrackingParticleCollection>  TPCollectionH ;
00213 //   iEvent.getByLabel("trackingtruth","TrackTruth",TPCollectionH);
00214 //   const TrackingParticleCollection tPC = *(TPCollectionH.product());
00215 //       reco::RecoToSimCollection recSimColl=associatorForParamAtPca->associateRecoToSim(tks,
00216 //                                                                            TPCollectionH,
00217 //                                                                            &iEvent);
00218 // 
00219 //       tree->fill(tv, &sv, &recSimColl);
00220 //     }
00221 
00222     }
00223   }  
00224 }
00225 
00226 void KineExample::printout(const RefCountedKinematicVertex& myVertex) const
00227 {
00228   cout << "Decay vertex: " << myVertex->position() <<endl;
00229 }
00230 
00231 void KineExample::printout(const RefCountedKinematicParticle& myParticle) const
00232 {
00233   cout << "Particle: \n";
00234 //accessing the reconstructed Bs meson parameters:
00235   AlgebraicVector7 bs_par = myParticle->currentState().kinematicParameters().vector();
00236 
00237 //and their joint covariance matrix:
00238   AlgebraicSymMatrix77 bs_er = myParticle->currentState().kinematicParametersError().matrix();
00239   cout << "Momentum at vertex: " << myParticle->currentState().globalMomentum ()<<endl;
00240   cout << "Parameters at vertex: " << myParticle->currentState().kinematicParameters().vector()<<endl;
00241 }
00242 
00243 void KineExample::printout(const RefCountedKinematicTree& myTree) const
00244 {
00245 
00246 //accessing the tree components, move pointer to top
00247   myTree->movePointerToTheTop();
00248 
00249 //We are now at the top of the decay tree getting the B_s reconstructed KinematicPartlcle
00250   RefCountedKinematicParticle b_s = myTree->currentParticle();
00251   printout(b_s);
00252 
00253 // The B_s decay vertex
00254   RefCountedKinematicVertex b_dec_vertex = myTree->currentDecayVertex();
00255   printout(b_dec_vertex);
00256 
00257   // Get all the children of Bs:
00258   //In this way, the pointer is not moved
00259   vector< RefCountedKinematicParticle > bs_children = myTree->finalStateParticles();
00260 
00261   for (unsigned int i=0;i< bs_children.size();++i) {
00262     printout(bs_children[i]);
00263   }
00264 
00265 //Now navigating down the tree , pointer is moved:
00266   bool child = myTree->movePointerToTheFirstChild();
00267 
00268   if(child) while (myTree->movePointerToTheNextChild()) {
00269     RefCountedKinematicParticle aChild = myTree->currentParticle();
00270     printout(aChild);
00271   }
00272 }
00273 
00274 //Returns the first vertex in the list.
00275 
00276 TrackingVertex KineExample::getSimVertex(const edm::Event& iEvent) const
00277 {
00278    // get the simulated vertices
00279   edm::Handle<TrackingVertexCollection>  TVCollectionH ;
00280   iEvent.getByLabel("trackingtruth","VertexTruth",TVCollectionH);
00281   const TrackingVertexCollection tPC = *(TVCollectionH.product());
00282 
00283 //    Handle<edm::SimVertexContainer> simVtcs;
00284 //    iEvent.getByLabel("g4SimHits", simVtcs);
00285 //    std::cout << "SimVertex " << simVtcs->size() << std::endl;
00286 //    for(edm::SimVertexContainer::const_iterator v=simVtcs->begin();
00287 //        v!=simVtcs->end(); ++v){
00288 //      std::cout << "simvtx "
00289 //             << v->position().x() << " "
00290 //             << v->position().y() << " "
00291 //             << v->position().z() << " "
00292 //             << v->parentIndex() << " "
00293 //             << v->noParent() << " "
00294 //               << std::endl;
00295 //    }
00296    return *(tPC.begin());
00297 }
00298 DEFINE_ANOTHER_FWK_MODULE(KineExample);

Generated on Tue Jun 9 17:46:09 2009 for CMSSW by  doxygen 1.5.4