CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_4_5_patch3/src/RecoParticleFlow/PFTracking/src/PFGsfHelper.cc

Go to the documentation of this file.
00001 //
00002 // -*- C++ -*-
00003 // Package:    PFTracking
00004 // Class:      PFGsfHelper
00005 // 
00006 // Original Author:  Daniele Benedetti 
00007 
00008 #include "RecoParticleFlow/PFTracking/interface/PFGsfHelper.h"
00009 
00010 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00011 #include "FWCore/Framework/interface/ESHandle.h"
00012 
00013 #include "DataFormats/ParticleFlowReco/interface/PFCluster.h"
00014 #include "DataFormats/ParticleFlowReco/interface/PFRecTrackFwd.h"
00015 #include "DataFormats/TrackReco/interface/Track.h"
00016 
00017 #include "TrackingTools/PatternTools/interface/Trajectory.h"
00018 //#include "FastSimulation/BaseParticlePropagator/interface/BaseParticlePropagator.h"
00019 
00020 #include "TrackingTools/TrajectoryState/interface/TrajectoryStateOnSurface.h"
00021 // Add by Daniele
00022 #include "TrackingTools/GsfTools/interface/MultiGaussianStateTransform.h"
00023 #include "TrackingTools/GsfTools/interface/MultiGaussianState1D.h"
00024 #include "TrackingTools/GsfTools/interface/GaussianSumUtilities1D.h"
00025 #include "RecoParticleFlow/PFTracking/interface/CollinearFitAtTM.h"
00026 using namespace std;
00027 using namespace reco;
00028 using namespace edm;
00029 
00030 
00031 PFGsfHelper::PFGsfHelper(const TrajectoryMeasurement& tm){
00032   
00033   /* LogInfo("PFGsfHelper")<<" PFGsfHelper  built"; */
00034 
00035   
00036   // TrajectoryStateOnSurface theUpdateState = tm.forwardPredictedState();
00037   theUpdateState = tm.updatedState();
00038   theForwardState = tm.forwardPredictedState();
00039   theBackwardState = tm.backwardPredictedState();
00040   
00041 
00042   Valid = true;
00043   if ( !theUpdateState.isValid() ||
00044        !theForwardState.isValid() ||
00045         !theBackwardState.isValid() )  Valid = false;
00046 
00047   if (Valid){
00048       
00049     mode_Px = 0.;
00050     mode_Py = 0.;
00051     mode_Pz = 0.;
00052     std::vector<TrajectoryStateOnSurface> components(theUpdateState.components());
00053     unsigned int numb = components.size();
00054 
00055     std::vector<SingleGaussianState1D> pxStates; pxStates.reserve(numb);
00056     std::vector<SingleGaussianState1D> pyStates; pyStates.reserve(numb);
00057     std::vector<SingleGaussianState1D> pzStates; pzStates.reserve(numb);
00058     for ( std::vector<TrajectoryStateOnSurface>::const_iterator ic=components.begin();
00059           ic!=components.end(); ++ic ) {
00060       GlobalVector momentum(ic->globalMomentum());
00061       AlgebraicSymMatrix66 cov(ic->cartesianError().matrix());
00062         pxStates.push_back(SingleGaussianState1D(momentum.x(),cov(3,3),ic->weight()));
00063         pyStates.push_back(SingleGaussianState1D(momentum.y(),cov(4,4),ic->weight()));
00064         pzStates.push_back(SingleGaussianState1D(momentum.z(),cov(5,5),ic->weight()));
00065         //      cout<<"COMP "<<momentum<<endl;
00066     }
00067     MultiGaussianState1D pxState(pxStates);
00068     MultiGaussianState1D pyState(pyStates);
00069     MultiGaussianState1D pzState(pzStates);
00070     GaussianSumUtilities1D pxUtils(pxState);
00071     GaussianSumUtilities1D pyUtils(pyState);
00072     GaussianSumUtilities1D pzUtils(pzState);
00073     mode_Px = pxUtils.mode().mean();
00074     mode_Py = pyUtils.mode().mean();
00075     mode_Pz = pzUtils.mode().mean();
00076 
00077   
00078     dp = 0.;
00079     sigmaDp = 0.;
00080     
00081     //
00082     // prepare input parameter vectors and covariance matrices
00083     //
00084  
00085     AlgebraicVector5 fwdPars = theForwardState.localParameters().vector();
00086     AlgebraicSymMatrix55 fwdCov = theForwardState.localError().matrix();
00087     computeQpMode(theForwardState,fwdPars,fwdCov);
00088     AlgebraicVector5 bwdPars = theBackwardState.localParameters().vector();
00089     AlgebraicSymMatrix55 bwdCov = theBackwardState.localError().matrix();
00090     computeQpMode(theBackwardState,bwdPars,bwdCov);
00091     LocalPoint hitPos(0.,0.,0.);
00092     LocalError hitErr(-1.,-1.,-1.);
00093     if ( tm.recHit()->isValid() ) {
00094       hitPos = tm.recHit()->localPosition();
00095       hitErr = tm.recHit()->localPositionError();
00096     }
00097     
00098     CollinearFitAtTM collinearFit;
00099     CollinearFitAtTM::ResultVector fitParameters;
00100     CollinearFitAtTM::ResultMatrix fitCovariance;
00101     double fitChi2;
00102     bool CollFit = true;
00103     if ( !collinearFit.fit(fwdPars,fwdCov,bwdPars,bwdCov,
00104                            hitPos,hitErr,
00105                            fitParameters,fitCovariance,fitChi2) )  CollFit = false;
00106 
00107     if (CollFit){
00108       double qpIn = fitParameters(0);
00109       double sig2In = fitCovariance(0,0);
00110       double qpOut = fitParameters(1);
00111       double sig2Out = fitCovariance(1,1);
00112       double corrInOut = fitCovariance(0,1);
00113       double pIn = 1./fabs(qpIn);
00114       double pOut = 1./fabs(qpOut);
00115       double sig2DeltaP = pIn/qpIn*pIn/qpIn*sig2In - 2*pIn/qpIn*pOut/qpOut*corrInOut + 
00116         pOut/qpOut*pOut/qpOut*sig2Out;
00117       //   std::cout << "fitted delta p = " << pOut-pIn << " sigma = " 
00118       //            << sqrt(sig2DeltaP) << std::endl;
00119       dp = pOut - pIn;      
00120       sigmaDp = sqrt(sig2DeltaP);
00121   
00122     }
00123     
00124     
00125   }
00126 }
00127 
00128 PFGsfHelper::~PFGsfHelper(){
00129 }
00130 GlobalVector PFGsfHelper::computeP(bool ComputeMode) const {
00131   GlobalVector gsfp;
00132   if (ComputeMode) gsfp = GlobalVector(mode_Px,mode_Py,mode_Pz);
00133   else gsfp = theUpdateState.globalMomentum();
00134   return gsfp;
00135 }
00136 double PFGsfHelper::fittedDP () const
00137 {  
00138   return dp;
00139 }
00140 double PFGsfHelper::sigmafittedDP () const
00141 {
00142   return sigmaDp;
00143 }
00144 bool PFGsfHelper::isValid () const
00145 {
00146   return Valid;
00147 } 
00148 
00149 void PFGsfHelper::computeQpMode (const TrajectoryStateOnSurface tsos,
00150                          AlgebraicVector5& parameters, AlgebraicSymMatrix55& covariance) const
00151 {
00152   //
00153   // parameters and errors from combined state
00154   //
00155   parameters = tsos.localParameters().vector();
00156   covariance = tsos.localError().matrix();
00157   //
00158   // mode for parameter 0 (q/p)
00159   //
00160   MultiGaussianState1D qpState(MultiGaussianStateTransform::multiState1D(tsos,0));
00161   GaussianSumUtilities1D qpGS(qpState);
00162   if ( !qpGS.modeIsValid() )  return;
00163   double qp = qpGS.mode().mean();
00164   double varQp = qpGS.mode().variance();
00165   //
00166   // replace q/p value and variance, rescale correlation terms
00167   //   (heuristic procedure - alternative would be mode in 5D ...)
00168   //
00169   double VarQpRatio = sqrt(varQp/covariance(0,0));
00170   parameters(0) = qp;
00171   covariance(0,0) = varQp;
00172   for ( int i=1; i<5; ++i )  covariance(i,0) *= VarQpRatio;
00173 //   std::cout << "covariance " << VarQpRatio << " "
00174 //          << covariance(1,0) << " " << covariance(0,1) << std::endl;
00175 }
00176 
00177