TrackSmearer.cxx

Go to the documentation of this file.
00001 #include "AtlfastAlgs/TrackSmearer.h"
00002 #include <cmath>
00003 
00004 #include "AtlfastAlgs/TrackMaker.h"
00005 #include "AtlfastAlgs/MuonMatrixManager.h"
00006 #include "AtlfastAlgs/PionMatrixManager.h"
00007 #include "AtlfastAlgs/ElectronMatrixManager.h"
00008 
00009 namespace Atlfast 
00010 {
00011   using std::abs;
00012   using std::sin;
00013   using std::cos;
00014   using std::tan;
00015   using std::atan;
00016   using std::sqrt;
00017   using std::pow;
00018   
00019   //----------------------------------------
00020   // Constructor
00021   //----------------------------------------
00022   
00023   TrackSmearer::TrackSmearer( int randSeed, 
00024                               MsgStream& log,
00025                               std::string muonparamfile, 
00026                               std::string pionparamfile, 
00027                               std::string electronparamfile,
00028                               std::string bremparamfile ) :
00029     m_log(log), 
00030     m_muonParamFile(muonparamfile), 
00031     m_pionParamFile(pionparamfile),
00032     m_electronParamFile(electronparamfile),
00033     m_bremParamFile(bremparamfile)
00034   {
00035 
00036     makeMatrixManagers(randSeed);
00037     makeChargeService();
00038     
00039   }
00040 
00041   void TrackSmearer::makeMatrixManagers(int randSeed)
00042   {
00043     m_muonMatrixManager     = new MuonMatrixManager( m_muonParamFile, randSeed, m_log );
00044     m_pionMatrixManager     = new PionMatrixManager( m_pionParamFile, randSeed, m_log );
00045     m_electronMatrixManager = new ElectronMatrixManager( m_bremParamFile, m_electronParamFile, randSeed, m_log );
00046     
00047     m_log << MSG::INFO << "TrackSmearer constructed" << endreq;
00048 
00049     return;
00050   }
00051   
00052   void TrackSmearer::makeChargeService()
00053   {
00054     try
00055     {
00056       m_chargeService = new ChargeService() ;
00057     } 
00058     catch( std::string errMsg )
00059     {
00060       std::cout << "TrackSmearer: Error making ChargeService " << errMsg << std::endl;
00061     }
00062     catch( ... )
00063     {
00064       std::cout << "TrackSmearer: Unknown Error making ChargeService " << std::endl;
00065     }
00066     
00067     return;
00068   }
00069 
00070   //------------------------------------------
00071   // Destructor
00072   //------------------------------------------
00073   
00074   TrackSmearer::~TrackSmearer()
00075   {
00076     if (m_muonMatrixManager)     delete m_muonMatrixManager;
00077     if (m_pionMatrixManager)     delete m_pionMatrixManager;
00078     if (m_electronMatrixManager) delete m_electronMatrixManager;
00079     delete m_chargeService;
00080   }
00081   
00082   //------------------------------------------------------------------
00083   // PUBLIC: smear : takes track trajectory and smears its parameters
00084   //------------------------------------------------------------------
00085   Track TrackSmearer::smear( const Track& track ) const
00086   {
00087 
00088     vector<double> smearVariables ;   // Vector of correlated gaussian variables
00089 
00090     int pdg = abs( track.pdg_id() );
00091     IMatrixManager* correctManager = getCorrectMatrixManager(pdg);
00092 
00093     TrackTrajectory originalTrajectory = track.trajectory();
00094     HepSymMatrix sigma; // smear matrix for track
00095     smearVariables = correctManager->getVariables( originalTrajectory, sigma );
00096     
00097     // smear parameters with gaussian variables
00098     // NOTE: smear variables are delta(d0, z0, phi0, cot(theta0), q/pT)
00099     TrackParameters parameters = originalTrajectory.parameters();
00100 
00101     double impactParameter = parameters.impactParameter() + smearVariables[0];
00102     double zPerigee        = parameters.zPerigee() + smearVariables[1];
00103     Phi phi                = parameters.phi() + smearVariables[2];
00104     double cotTheta        = parameters.cotTheta() + smearVariables[3];
00105     double invPtCharge     = parameters.invPtCharge() + smearVariables[4];
00106 
00107     double curvature = originalTrajectory.curvature(); 
00108     double pT = abs( 1. / invPtCharge );
00109     Hep3Vector vec;
00110     vec.setX( pT * cos(phi) );
00111     vec.setY( pT * sin(phi) );
00112     vec.setZ( pT * cotTheta );
00113     
00114     // Convert results to object
00115     TrackTrajectory smearedTrajectory( TrackParameters( vec.pseudoRapidity(), 
00116                                                         phi, pT, impactParameter,
00117                                                         zPerigee, cotTheta,
00118                                                         invPtCharge ), 
00119                                        originalTrajectory.startPoint(),
00120                                        curvature );
00121 
00122     // sigma has to be converted into the representation used 
00123     // by Common Tracking, i.e. (d0, z0, phi0, theta0, q/p), 
00124     // when it is written to CBNTs or AODs.  
00125     HepSymMatrix sigmaConverted = convertToComTrk( track, sigma );
00126     
00127     Track newTrack( originalTrajectory, smearedTrajectory, track.truth(), sigmaConverted );
00128 
00129     return newTrack ;
00130   }
00131 
00132 
00133   IMatrixManager* TrackSmearer::getCorrectMatrixManager( int &pdg ) const 
00134   {
00135     IMatrixManager* correctManager = NULL;
00136     
00137     if ( pdg == 11 )  correctManager = m_electronMatrixManager;
00138     if ( pdg == 13 )  correctManager = m_muonMatrixManager;
00139     if ( pdg == 211 ) correctManager = m_pionMatrixManager;
00140     if ( correctManager == NULL )  correctManager = m_pionMatrixManager;
00141 
00142     return correctManager;
00143   }
00144 
00145 
00146   HepSymMatrix TrackSmearer::convertToComTrk( const Track& track, const HepSymMatrix& sigma ) const 
00147   {
00148     // Method to convert covariance matrix into the representation used 
00149     // by Common Tracking, i.e. (d0, z0, phi0, theta0, q/p), instead of 
00150     // internally used (d0, z0, phi0, cot(theta0), q/pT). Needed when  
00151     // matrix is written to CBNTs or AODs. 
00152     // Uses track parameters of original (MC) track to determine the 
00153     // Jacobian matrix.
00154     
00155     TrackTrajectory originalTrajectory = track.trajectory();
00156     TrackParameters parameters = originalTrajectory.parameters();
00157      
00158     double cotTheta    = parameters.cotTheta();
00159     double sinTheta    = sqrt( 1.0 / ( 1.0 + cotTheta * cotTheta ) );
00160     double invPtCharge = parameters.invPtCharge();
00161      
00162     HepSymMatrix conv(5,0);
00163     
00164     // USE the fact that from the sigma off-diagonals, the only non-zero 
00165     // elements are sigma(1,3), sigma(1,5), sigma(3,5), sigma(2,4)
00166      
00167     // cov(d0,d0)
00168     conv(1,1) = sigma(1,1);
00169     // cov(d0,phi0)
00170     conv(1,3) = conv(3,1) = sigma(1,3);
00171     // cov(d0,q/p)
00172     conv(1,5) = conv(5,1) = sigma(1,5) * sinTheta;
00173     // cov(phi0,phi0)
00174     conv(3,3) = sigma(3,3);
00175     // cov(phi0,q/p)
00176     conv(3,5) = conv(5,3) = sigma(3,5) * sinTheta;
00177     
00178     // cov(z0,z0)
00179     conv(2,2) = sigma(2,2);
00180     // cov(z0,theta0)
00181     conv(2,4) = conv(4,2) = -sigma(2,4) * sinTheta * sinTheta;
00182     // cov(theta0,theta0)
00183     conv(4,4) = sigma(4,4) * pow( sinTheta, 4 );
00184        
00185     // cov(z0,q/p)
00186     conv(2,5) = conv(5,2) = conv(2,4) * invPtCharge * sinTheta * cotTheta;
00187     // cov(theta0,q/p)
00188     conv(4,5) = conv(5,4) = conv(4,4) * invPtCharge * sinTheta * cotTheta;
00189     
00190     // cov(q/p,q/p)
00191     conv(5,5)  = sigma(5,5) + 
00192                  sigma(4,4) * pow( invPtCharge * sinTheta * sinTheta * cotTheta, 2 );  
00193     conv(5,5) *= sinTheta * sinTheta;             
00194     
00195     return conv;
00196   }
00197 
00198   
00199   double TrackSmearer::getPtResolution( const HepMC::GenParticle& particle )
00200   {
00201     int pdg = abs( particle.pdg_id() );
00202     IMatrixManager* correctManager = getCorrectMatrixManager(pdg);
00203     
00204     // Look up vertex
00205     Hep3Vector vertex = (particle.production_vertex())->position().getV();
00206     // Get 4-momentum and explicitly convert to 3-momentum
00207     Hep3Vector threeMomentum = particle.momentum().getV() ;
00208     // Obtain charge of particle
00209     double charge = m_chargeService->operator()( &particle );    
00210     // Magnetic field strength
00211     MagField *bfield = MagField::Instance();
00212 
00213     TrackTrajectory trTraj( threeMomentum, vertex, charge, bfield->strength() );
00214     
00215     // Get smearing matrix
00216     HepSymMatrix sigma; // smear matrix for track
00217     std::vector<double> smearVariables = correctManager->getVariables( trTraj, sigma );
00218     
00219     // return pT element
00220     // At some stage, need to develop a system for returning
00221     // element independent of track parameterisation, eg.
00222     // AtlfastMatrix->getPtRes()?
00223 
00224     double pt = ( particle.momentum() ).perp();
00225 
00226     // sigma(5,5) is resolution^2 for q/pT, 
00227     // sigma(pT) = pT^2 * sqrt( sigma(5,5) ) 
00228     // sigma(pT)/pT gives proportional resolution
00229 
00230     double ptResProp = pt * sqrt( sigma(5,5) );
00231     
00232     return ptResProp;
00233   }
00234 
00235 } // end namespace

Generated on Mon Sep 24 14:19:12 2007 for AtlfastAlgs by  doxygen 1.5.1