CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_2_9_HLT1_bphpatch4/src/RecoEgamma/EgammaPhotonAlgos/src/ConversionVertexFinder.cc

Go to the documentation of this file.
00001 #include <iostream>
00002 #include <vector>
00003 #include <memory>
00004 #include "RecoEgamma/EgammaPhotonAlgos/interface/ConversionVertexFinder.h"
00005 // Framework
00006 #include "FWCore/Framework/interface/Event.h"
00007 #include "FWCore/Framework/interface/EventSetup.h"
00008 #include "DataFormats/Common/interface/Handle.h"
00009 #include "FWCore/Framework/interface/ESHandle.h"
00010 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00011 #include "FWCore/Utilities/interface/Exception.h"
00012 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00013 //
00014 //Kinematic constraint vertex fitter
00015 #include "RecoVertex/KinematicFitPrimitives/interface/ParticleMass.h"
00016 #include "RecoVertex/KinematicFitPrimitives/interface/MultiTrackKinematicConstraint.h"
00017 #include <RecoVertex/KinematicFitPrimitives/interface/KinematicParticleFactoryFromTransientTrack.h>
00018 
00019 #include "RecoVertex/KinematicFit/interface/TwoTrackMassKinematicConstraint.h"
00020 #include "RecoVertex/KinematicFit/interface/KinematicParticleVertexFitter.h"
00021 #include "RecoVertex/KinematicFit/interface/KinematicParticleFitter.h"
00022 #include "RecoVertex/KinematicFit/interface/MassKinematicConstraint.h"
00023 #include "RecoVertex/KinematicFit/interface/ColinearityKinematicConstraint.h"
00024 #include "CommonTools/Statistics/interface/ChiSquaredProbability.h"
00025 
00026 // new templated one
00027 #include "RecoVertex/KinematicFit/interface/KinematicConstrainedVertexFitterT.h"
00028 #include "RecoVertex/KinematicFit/interface/ColinearityKinematicConstraintT.h"
00029 
00030 
00031 
00032 //
00033 #include "CLHEP/Units/GlobalPhysicalConstants.h"
00034 #include <TMath.h>
00035 
00036 
00037 
00038 
00039 
00040 ConversionVertexFinder::ConversionVertexFinder(const edm::ParameterSet& config ):
00041   conf_(config)
00042 { 
00043   LogDebug("ConversionVertexFinder") << "ConversionVertexFinder CTOR  " <<  "\n";  
00044   maxDelta_ = conf_.getParameter<double>("maxDelta");
00045   maxReducedChiSq_ = conf_.getParameter<double>("maxReducedChiSq");
00046   minChiSqImprovement_  = conf_.getParameter<double>("minChiSqImprovement");
00047   maxNbrOfIterations_ = conf_.getParameter<int>("maxNbrOfIterations");
00048   kcvFitter_ = new KinematicConstrainedVertexFitter();
00049   kcvFitter_->setParameters(conf_);
00050 
00051 }
00052 
00053 ConversionVertexFinder::~ConversionVertexFinder() {
00054 
00055   LogDebug("ConversionVertexFinder") << "ConversionVertexFinder DTOR " <<  "\n";  
00056   delete   kcvFitter_;
00057  
00058 }
00059 
00060 bool  ConversionVertexFinder::run(std::vector<reco::TransientTrack>  pair, reco::Vertex& the_vertex) {
00061   bool found= false;
00062 
00063   if ( pair.size() < 2) return found;
00064   
00065   float sigma = 0.00000001;
00066   float chi = 0.;
00067   float ndf = 0.;
00068   float mass = 0.000511;
00069   
00070 
00071   KinematicParticleFactoryFromTransientTrack pFactory;
00072   
00073   std::vector<RefCountedKinematicParticle> particles;
00074   
00075   particles.push_back(pFactory.particle (pair[0],mass,chi,ndf,sigma,*pair[0].innermostMeasurementState().freeState()));
00076   particles.push_back(pFactory.particle (pair[1],mass,chi,ndf,sigma,*pair[1].innermostMeasurementState().freeState()));
00077   
00078 
00079 #ifdef OldKineFit
00080   ColinearityKinematicConstraint constr(ColinearityKinematicConstraint::PhiTheta);
00081   
00082   RefCountedKinematicTree myTree = kcvFitter_->fit(particles, &constr);
00083 #else
00084 
00085   // bizzare way to the get the field...
00086   const MagneticField* mf = pair[0].field();
00087 
00088   ColinearityKinematicConstraintT<colinearityKinematic::PhiTheta> constr;
00089   KinematicConstrainedVertexFitterT<2,2> kcvFitter(mf);
00090   kcvFitter.setParameters(conf_);
00091   RefCountedKinematicTree myTree =  kcvFitter.fit(particles, &constr);
00092 
00093 #ifdef KineFitDebug
00094 
00095   ColinearityKinematicConstraint oldconstr(ColinearityKinematicConstraint::PhiTheta);
00096   
00097   RefCountedKinematicTree oldTree = kcvFitter_->fit(particles, &oldconstr);
00098 
00099 
00100   if( oldTree->isValid() ) {
00101     std::cout << "old " << kcvFitter_->getNit() << std::endl;
00102     RefCountedKinematicVertex gamma_dec_vertex = oldTree->currentDecayVertex();                                                          
00103     if( gamma_dec_vertex->vertexIsValid())
00104       std::cout << gamma_dec_vertex->chiSquared() <<  " " << gamma_dec_vertex->degreesOfFreedom() << std::endl;
00105     std::cout <<  oldTree->currentParticle()->currentState().globalMomentum() <<
00106       oldTree->currentParticle()->currentState().globalPosition()<< std::endl;
00107     std::vector<RefCountedKinematicParticle> fStates=oldTree->finalStateParticles();
00108     for (unsigned int kk=0; kk<fStates.size(); kk++) {
00109       std::cout <<  fStates[kk]->currentState().globalMomentum() << 
00110         fStates[kk]->currentState().globalPosition() << std::endl;
00111       std::cout <<  fStates[kk]->currentState().kinematicParametersError().matrix()<<std::endl;
00112     }
00113   } else       std::cout << "old invalid " << kcvFitter_->getNit() << std::endl;
00114   
00115   if( myTree->isValid() ) {
00116     std::cout << "new " << kcvFitter.getNit() << std::endl;
00117     RefCountedKinematicVertex gamma_dec_vertex = myTree->currentDecayVertex();                                                          
00118     if( gamma_dec_vertex->vertexIsValid())
00119       std::cout << gamma_dec_vertex->chiSquared() <<  " " << gamma_dec_vertex->degreesOfFreedom() << std::endl;
00120     std::cout <<  myTree->currentParticle()->currentState().globalMomentum() <<
00121       myTree->currentParticle()->currentState().globalPosition()<< std::endl;
00122     std::vector<RefCountedKinematicParticle> fStates=myTree->finalStateParticles();
00123     for (unsigned int kk=0; kk<fStates.size(); kk++) {
00124       std::cout <<  fStates[kk]->currentState().globalMomentum() << 
00125         fStates[kk]->currentState().globalPosition() << std::endl;
00126       std::cout <<  fStates[kk]->currentState().kinematicParametersError().matrix()<<std::endl;
00127     }
00128   } else       std::cout << "new invalid " << kcvFitter.getNit() << std::endl;
00129 
00130 #endif // TemplateKineFitDebug
00131 
00132 #endif
00133 
00134   if( myTree->isValid() ) {
00135     myTree->movePointerToTheTop();                                                                                
00136     RefCountedKinematicParticle the_photon = myTree->currentParticle();                                           
00137     if (the_photon->currentState().isValid()){                                                                    
00138       //const ParticleMass photon_mass = the_photon->currentState().mass();                                       
00139       RefCountedKinematicVertex gamma_dec_vertex;                                                               
00140       gamma_dec_vertex = myTree->currentDecayVertex();                                                          
00141       if( gamma_dec_vertex->vertexIsValid() ){                                                                  
00142         const float chi2Prob = ChiSquaredProbability(gamma_dec_vertex->chiSquared(), gamma_dec_vertex->degreesOfFreedom());
00143         if (chi2Prob>0.){// no longer cut here, only ask positive probability here 
00144           //const math::XYZPoint vtxPos(gamma_dec_vertex->position());                                           
00145           the_vertex = *gamma_dec_vertex;
00146           found = true;
00147         }
00148       }
00149     }
00150   }
00151  
00152   return found;
00153 }
00154 
00155 
00156 TransientVertex  ConversionVertexFinder::run(std::vector<reco::TransientTrack>  pair) {
00157   LogDebug("ConversionVertexFinder") << "ConversionVertexFinder run pair size " << pair.size() <<  "\n";  
00158   
00159   //for ( std::vector<reco::TransientTrack>::const_iterator iTk=pair.begin(); iTk!=pair.end(); ++iTk) {
00160   // LogDebug("ConversionVertexFinder") << "  ConversionVertexFinder  Tracks in the pair  charge " << iTk->charge() << " Num of RecHits " << iTk->recHitsSize() << " inner momentum " << iTk->track().innerMomentum() << "\n";  
00161   //}
00162 
00163 
00164   reco::Vertex theVertex;  
00165   KalmanVertexFitter fitter(true);
00166   TransientVertex transientVtx;
00167 
00168   const std::string metname =  "ConversionVertexFinder| ConversionVertexFinder";
00169   try{
00170 
00171     transientVtx = fitter.vertex(pair); 
00172 
00173   }  catch ( cms::Exception& e ) {
00174 
00175 
00176     edm::LogWarning(metname) << "cms::Exception caught in ConversionVertexFinder::run\n"
00177                              << e.explainSelf();
00178     
00179   }
00180   
00181 
00182   return transientVtx;
00183 
00184     
00185     
00186 }
00187