CMS 3D CMS Logo

Public Types | Public Member Functions | Private Member Functions | Private Attributes

KalmanAlignmentAlgorithm Class Reference

#include <KalmanAlignmentAlgorithm.h>

Inheritance diagram for KalmanAlignmentAlgorithm:
AlignmentAlgorithmBase

List of all members.

Public Types

typedef KalmanAlignmentSetup AlignmentSetup
typedef std::vector
< AlignmentSetup * > 
AlignmentSetupCollection
typedef
TrajectoryFactoryBase::ExternalPredictionCollection 
ExternalPredictionCollection
typedef
TrajectoryFactoryBase::ReferenceTrajectoryCollection 
ReferenceTrajectoryCollection
typedef
TrajectoryFactoryBase::ReferenceTrajectoryPtr 
ReferenceTrajectoryPtr
typedef std::vector< TrackletPtrTrackletCollection
typedef
KalmanAlignmentTracklet::TrackletPtr 
TrackletPtr

Public Member Functions

virtual void initialize (const edm::EventSetup &setup, AlignableTracker *tracker, AlignableMuon *muon, AlignableExtras *extras, AlignmentParameterStore *store)
 Call at beginning of job (must be implemented in derived class)
 KalmanAlignmentAlgorithm (const edm::ParameterSet &config)
bool operator() (const Alignable *a1, const Alignable *a2) const
virtual void produce (edm::Event &, const edm::EventSetup &)
virtual void run (const edm::EventSetup &setup, const EventInfo &eventInfo)
 Run the algorithm (must be implemented in derived class)
virtual void terminate (void)
 Call at end of job (must be implemented in derived class)
virtual ~KalmanAlignmentAlgorithm (void)

Private Member Functions

void applyAlignmentParameters (Alignable *ali, AlignmentParameters *par, bool applyPar, bool applyCov) const
void getComponents (Alignable *ali, std::vector< Alignable * > &comps) const
const PropagationDirection getDirection (const std::string &dir) const
const
KalmanAlignmentSetup::SortingDirection 
getSortingDirection (const std::string &sortDir) const
void initializeAlignmentParameters (const edm::EventSetup &setup)
void initializeAlignmentSetups (const edm::EventSetup &setup)
void mergeResults (void) const
const PropagationDirection oppositeDirection (const PropagationDirection dir) const
void setAPEToZero (void)

Private Attributes

AlignmentSetupCollection theAlignmentSetups
edm::ParameterSet theConfiguration
bool theMergerFlag
AlignableNavigatortheNavigator
AlignmentParameterStoretheParameterStore
KalmanAlignmentTrackRefittertheRefitter
AlignmentParameterSelectortheSelector
AlignableTrackertheTracker

Detailed Description

Definition at line 24 of file KalmanAlignmentAlgorithm.h.


Member Typedef Documentation

Definition at line 36 of file KalmanAlignmentAlgorithm.h.

Definition at line 37 of file KalmanAlignmentAlgorithm.h.

Definition at line 31 of file KalmanAlignmentAlgorithm.h.

Definition at line 30 of file KalmanAlignmentAlgorithm.h.

Definition at line 29 of file KalmanAlignmentAlgorithm.h.

Definition at line 34 of file KalmanAlignmentAlgorithm.h.

Definition at line 33 of file KalmanAlignmentAlgorithm.h.


Constructor & Destructor Documentation

KalmanAlignmentAlgorithm::KalmanAlignmentAlgorithm ( const edm::ParameterSet config)

Definition at line 46 of file KalmanAlignmentAlgorithm.cc.

                                                                                  :
  AlignmentAlgorithmBase( config ),
  theConfiguration( config )
{}
KalmanAlignmentAlgorithm::~KalmanAlignmentAlgorithm ( void  ) [virtual]

Definition at line 52 of file KalmanAlignmentAlgorithm.cc.

{}

Member Function Documentation

void KalmanAlignmentAlgorithm::applyAlignmentParameters ( Alignable ali,
AlignmentParameters par,
bool  applyPar,
bool  applyCov 
) const [private]

Definition at line 643 of file KalmanAlignmentAlgorithm.cc.

References AlignmentParameters::covariance(), ExpressReco_HICollisions_FallBack::e, Exception, Alignable::move(), Gflash::par, Alignable::rotateInLocalFrame(), RigidBodyAlignmentParameters::rotation(), Alignable::setAlignmentPositionError(), edm::shift, mathSSE::sqrt(), Alignable::surface(), AlignableSurface::toGlobal(), align::toMatrix(), RigidBodyAlignmentParameters::translation(), PV3DBase< T, PVType, FrameType >::x(), PV3DBase< T, PVType, FrameType >::y(), and PV3DBase< T, PVType, FrameType >::z().

Referenced by initializeAlignmentParameters(), and mergeResults().

{
  RigidBodyAlignmentParameters* rbap = dynamic_cast<RigidBodyAlignmentParameters*>( par );

  if ( !rbap )
    throw cms::Exception( "BadConfig" ) << "applyParameters: provided alignable does not have rigid body alignment parameters";

  if ( applyPar )
  {
    AlgebraicVector shift = rbap->translation();
    const AlignableSurface& alignableSurface = ali->surface();
    ali->move( alignableSurface.toGlobal( align::LocalVector( shift[0], shift[1], shift[2] ) ) );

    align::EulerAngles angles = rbap->rotation();
    if ( angles.normsq() > 1e-10 ) ali->rotateInLocalFrame( align::toMatrix( angles ) );
  }

  if ( applyCov )
  {
    const AlgebraicSymMatrix& aliCov = rbap->covariance();
    LocalVector localError( sqrt(aliCov[0][0]), sqrt(aliCov[1][1]), sqrt(aliCov[2][2]) );
    GlobalVector globalError = ali->surface().toGlobal( localError );
    AlignmentPositionError ape( globalError.x(), globalError.y(), globalError.z() );
    // FIXME (GF): The above does not seem to be a correct error transformation!
    ali->setAlignmentPositionError( ape, true );  // true: propagate down
  }
}
void KalmanAlignmentAlgorithm::getComponents ( Alignable ali,
std::vector< Alignable * > &  comps 
) const [private]
const PropagationDirection KalmanAlignmentAlgorithm::getDirection ( const std::string &  dir) const [inline, private]

Definition at line 73 of file KalmanAlignmentAlgorithm.h.

References alongMomentum, and oppositeToMomentum.

Referenced by initializeAlignmentSetups().

    { return ( dir == "alongMomentum" ) ? alongMomentum : oppositeToMomentum; }
const KalmanAlignmentSetup::SortingDirection KalmanAlignmentAlgorithm::getSortingDirection ( const std::string &  sortDir) const [private]

Definition at line 630 of file KalmanAlignmentAlgorithm.cc.

References Exception, KalmanAlignmentSetup::sortDownsideUp, KalmanAlignmentSetup::sortInsideOut, KalmanAlignmentSetup::sortOutsideIn, and KalmanAlignmentSetup::sortUpsideDown.

Referenced by initializeAlignmentSetups().

{
  if ( sortDir == "SortInsideOut" ) { return KalmanAlignmentSetup::sortInsideOut; }
  if ( sortDir == "SortOutsideIn" ) { return KalmanAlignmentSetup::sortOutsideIn; }
  if ( sortDir == "SortUpsideDown" ) { return KalmanAlignmentSetup::sortUpsideDown; }
  if ( sortDir == "SortDownsideUp" ) { return KalmanAlignmentSetup::sortDownsideUp; }

  throw cms::Exception( "BadConfig" ) << "[KalmanAlignmentAlgorithm::getSortingDirection] "
                                      << "Unknown sorting direction: " << sortDir << std::endl;
}
void KalmanAlignmentAlgorithm::initialize ( const edm::EventSetup setup,
AlignableTracker tracker,
AlignableMuon muon,
AlignableExtras extras,
AlignmentParameterStore store 
) [virtual]
void KalmanAlignmentAlgorithm::initializeAlignmentParameters ( const edm::EventSetup setup) [private]

Definition at line 212 of file KalmanAlignmentAlgorithm.cc.

References AlignmentParameterSelector::addSelection(), applyAlignmentParameters(), AlignmentParameterSelector::clear(), AlignmentParameters::clone(), cmsDriver::config, gather_cfg::cout, AlignmentParameters::covariance(), ExpressReco_HICollisions_FallBack::e, funct::false, dbtoweb::file, edm::ParameterSet::getParameter(), edm::ParameterSet::getUntrackedParameter(), AlignmentParametersFactory::kRigidBody, AlignmentParameters::parameters(), ExpressReco_HICollisions_FallBack::parameters, CosmicsPD_Skims::radius, AlignmentIORoot::readAlignmentParameters(), AlignmentParameterSelector::selectedAlignables(), KalmanAlignmentUserVariables::setAlignmentFlag(), setAPEToZero(), AlignmentParameters::setUserVariables(), AlignmentParameters::setValid(), python::multivaluedict::sort(), mathSSE::sqrt(), theConfiguration, theSelector, align::toMatrix(), KalmanAlignmentUserVariables::update(), PV3DBase< T, PVType, FrameType >::x(), PV3DBase< T, PVType, FrameType >::y(), and PV3DBase< T, PVType, FrameType >::z().

Referenced by initialize().

{
  // Just to be sure, set all APEs to zero ...
  setAPEToZero();

  const edm::ParameterSet initConfig = theConfiguration.getParameter< edm::ParameterSet >( "ParameterConfig" );

  int updateGraph = initConfig.getUntrackedParameter< int >( "UpdateGraphs", 100 );

  bool addPositionError = false;// = initConfig.getUntrackedParameter< bool >( "AddPositionError", true );

  int seed  = initConfig.getUntrackedParameter< int >( "RandomSeed", 1726354 );
  CLHEP::HepRandom::createInstance();
  CLHEP::HepRandom::setTheSeed( seed );

  bool applyXShifts =  initConfig.getUntrackedParameter< bool >( "ApplyXShifts", false );
  bool applyYShifts =  initConfig.getUntrackedParameter< bool >( "ApplyYShifts", false );
  bool applyZShifts =  initConfig.getUntrackedParameter< bool >( "ApplyZShifts", false );
  bool applyXRots =  initConfig.getUntrackedParameter< bool >( "ApplyXRotations", false );
  bool applyYRots =  initConfig.getUntrackedParameter< bool >( "ApplyYRotations", false );
  bool applyZRots =  initConfig.getUntrackedParameter< bool >( "ApplyZRotations", false );

  bool applyRandomStartValues = initConfig.getUntrackedParameter< bool >( "ApplyRandomStartValues", false );
  if ( applyRandomStartValues )
    cout << "[KalmanAlignmentAlgorithm::initializeAlignmentParameters] ADDING RANDOM START VALUES!!!" << endl;

  bool applyCurl =  initConfig.getUntrackedParameter< bool >( "ApplyCurl", false );
  double curlConst =  initConfig.getUntrackedParameter< double >( "CurlConstant", 1e-6 );

  bool applyShifts = applyXShifts || applyYShifts || applyZShifts;
  bool applyRots = applyXRots || applyYRots || applyZRots;
  //bool applyMisalignment = applyShifts || applyRots || applyCurl;

  AlgebraicVector displacement( 3 );
  AlgebraicVector eulerAngles( 3 );

  AlgebraicVector startParameters( 6, 0 );
  AlgebraicSymMatrix startError( 6, 0 );

  AlgebraicVector randSig( 6, 0 );

  vector< string > initSelection = initConfig.getParameter< vector<string> >( "InitializationSelector" );

  vector< string >::iterator itInitSel;
  for ( itInitSel = initSelection.begin(); itInitSel != initSelection.end(); ++itInitSel )
  {
    const edm::ParameterSet config = initConfig.getParameter< edm::ParameterSet >( *itInitSel );

    addPositionError = initConfig.getUntrackedParameter< bool >( "AddPositionError", false );

    double xAPEError = initConfig.getUntrackedParameter< double >( "SigmaXPositionError", 2e-2 );
    double yAPEError = initConfig.getUntrackedParameter< double >( "SigmaYPositionError", 2e-2  );
    double zAPEError = initConfig.getUntrackedParameter< double >( "SigmaZPositionError", 2e-2  );

    double sigmaXShift = config.getUntrackedParameter< double >( "SigmaXShifts", 4e-2 );
    double sigmaYShift = config.getUntrackedParameter< double >( "SigmaYShifts", 4e-2 );
    double sigmaZShift = config.getUntrackedParameter< double >( "SigmaZShifts", 4e-2 );
    double sigmaXRot = config.getUntrackedParameter< double >( "SigmaXRotations", 5e-4 );
    double sigmaYRot = config.getUntrackedParameter< double >( "SigmaYRotations", 5e-4 );
    double sigmaZRot = config.getUntrackedParameter< double >( "SigmaZRotations", 5e-4 );

    randSig[0] = sigmaXShift; randSig[1] = sigmaYShift; randSig[2] = sigmaZShift;
    randSig[3] = sigmaXRot; randSig[4] = sigmaYRot; randSig[5] = sigmaZRot;

    startError[0][0] = config.getUntrackedParameter< double >( "XShiftsStartError", 4e-4 );
    startError[1][1] = config.getUntrackedParameter< double >( "YShiftsStartError", 4e-4 );
    startError[2][2] = config.getUntrackedParameter< double >( "ZShiftsStartError", 4e-4 );
    startError[3][3] = config.getUntrackedParameter< double >( "XRotationsStartError", 3e-5 );
    startError[4][4] = config.getUntrackedParameter< double >( "YRotationsStartError", 3e-5 );
    startError[5][5] = config.getUntrackedParameter< double >( "ZRotationsStartError", 3e-5 );

    const vector< char > dummyParamSelector( 6, '0' );
    const vector< string > alignableSelector = config.getParameter< vector<string> >( "AlignableSelection" );

    vector< string >::const_iterator itAliSel;
    for ( itAliSel = alignableSelector.begin(); itAliSel != alignableSelector.end(); ++itAliSel )
    {
      theSelector->addSelection( *itAliSel, dummyParamSelector );
      cout << "[" << *itInitSel << "] add selection: " << *itAliSel << endl;
    }

    vector< Alignable* >::iterator itAlignable;
    vector< Alignable* > alignables;
    vector< Alignable* > alignablesFromSelector = theSelector->selectedAlignables();
    for ( itAlignable = alignablesFromSelector.begin(); itAlignable != alignablesFromSelector.end(); ++itAlignable )
      //if ( (*itAlignable)->alignmentParameters() )
      alignables.push_back( *itAlignable );

    cout << "[" << *itInitSel << "] total number of selected alignables = " << alignables.size() << endl;

    sort( alignables.begin(), alignables.end(), *this );

    // Apply existing alignment parameters.
    map< Alignable*, vector< AlignmentParameters* > > alignmentParametersMap;

    int iApply = 0;
    bool readParam = config.getUntrackedParameter< bool >( "ReadParametersFromFile", false );
    bool readCovar = config.getUntrackedParameter< bool >( "ReadCovarianceFromFile", false );
    bool applyParam = config.getUntrackedParameter< bool >( "ApplyParametersFromFile", false );
    bool applyCovar = config.getUntrackedParameter< bool >( "ApplyErrorFromFile", false );
    if ( readParam || readCovar || applyParam || applyCovar )
    {
      string file = config.getUntrackedParameter< string >( "FileName", "Input.root" );
      int ierr = 0;
      int iter = 1;

      AlignmentIORoot alignmentIO;
      while ( !ierr )
      {
        cout << "[" << *itInitSel << "] read alignment parameters. file / iteration = " << file << " / " << iter << endl;
        vector< AlignmentParameters* > alignmentParameters = alignmentIO.readAlignmentParameters( alignables, file.c_str(), iter, ierr );
        cout << "[" << *itInitSel << "] #param / ierr = " << alignmentParameters.size() << " / " << ierr << endl;

        vector< AlignmentParameters* >::iterator itParam;
        for ( itParam = alignmentParameters.begin(); itParam != alignmentParameters.end(); ++itParam )
          alignmentParametersMap[(*itParam)->alignable()].push_back( *itParam );

        ++iter;
      }
    }

    int iAlign = 0;

    for ( itAlignable = alignables.begin(); itAlignable != alignables.end(); itAlignable++ )
    {
      if ( (*itAlignable)->alignmentParameters() == 0 )
      {
        cout << "[KalmanAlignmentAlgorithm::initializeAlignmentParameters] "
             << "alignable is not associated with alignment parameters --> skip" << endl;
        continue;
      }

      if ( (*itAlignable)->alignmentParameters()->type() != AlignmentParametersFactory::kRigidBody )
      {
        cout << "[KalmanAlignmentAlgorithm::initializeAlignmentParameters] "
             << "alignable is not associated with rigid body alignment parameters --> skip" << endl;
        continue;
      }

      displacement[0] = applyXShifts ? sigmaXShift*CLHEP::RandGauss::shoot() : 0.;
      displacement[1] = applyYShifts ? sigmaZShift*CLHEP::RandGauss::shoot() : 0.;
      displacement[2] = applyZShifts ? sigmaYShift*CLHEP::RandGauss::shoot() : 0.;

      if ( applyShifts ) 
      {
        align::LocalVector localShift( displacement[0], displacement[1], displacement[2] );
        align::GlobalVector globalShift = ( *itAlignable )->surface().toGlobal( localShift );
        ( *itAlignable )->move( globalShift );
      }

      align::EulerAngles eulerAngles( 3 );

      eulerAngles[0] = applyXRots ? sigmaXRot*CLHEP::RandGauss::shoot() : 0.;
      eulerAngles[1] = applyYRots ? sigmaYRot*CLHEP::RandGauss::shoot() : 0.;
      eulerAngles[2] = applyZRots ? sigmaZRot*CLHEP::RandGauss::shoot() : 0.;

      if ( applyRots )
      {
        align::RotationType localRotation = align::toMatrix( eulerAngles );
        ( *itAlignable )->rotateInLocalFrame( localRotation );
      }

      if ( applyCurl )
      {
        double radius = ( *itAlignable )->globalPosition().perp();
        ( *itAlignable )->rotateAroundGlobalZ( curlConst*radius );
      }

      if ( addPositionError )
      {
        LocalVector localError( sqrt(xAPEError), sqrt(yAPEError), sqrt(zAPEError) );
        GlobalVector globalError = (*itAlignable)->surface().toGlobal( localError );
        AlignmentPositionError ape( globalError.x(), globalError.y(), globalError.z() );
        // FIXME (GF): The above does not seem to be a correct error transformation!
        ( *itAlignable )->setAlignmentPositionError( ape, true ); // true: propagate down
      }

      //AlgebraicVector trueParameters( 6 );
      //trueParameters[0] = -displacement[0];
      //trueParameters[1] = -displacement[1];
      //trueParameters[2] = -displacement[2];
      //trueParameters[3] = -eulerAngles[0];
      //trueParameters[4] = -eulerAngles[1];
      //trueParameters[5] = -eulerAngles[2];

      if ( (*itAlignable)->alignmentParameters() != 0 )
      {
        AlignmentParameters* alignmentParameters;
        if ( readParam && readCovar )
        {
          if ( alignmentParametersMap.find( *itAlignable ) == alignmentParametersMap.end() )
          {
            //cout << "apply param and cov from FILE -> none stored, apply DEFAULT " << endl;
            alignmentParameters = (*itAlignable)->alignmentParameters()->clone( startParameters, startError );
            alignmentParameters->setUserVariables( new KalmanAlignmentUserVariables( *itAlignable, updateGraph ) );
          }
          else
          {
            //cout << "apply param and cov from FILE" << endl;
            alignmentParameters = alignmentParametersMap[*itAlignable].back();
            KalmanAlignmentUserVariables* userVariables = new KalmanAlignmentUserVariables( *itAlignable, updateGraph );
            userVariables->update( alignmentParameters );
            alignmentParameters->setUserVariables( userVariables );
          }
        }
        else if ( readParam )
        {
          if ( alignmentParametersMap.find( *itAlignable ) == alignmentParametersMap.end() )
          {
            alignmentParameters = (*itAlignable)->alignmentParameters()->clone( startParameters, startError );
            alignmentParameters->setUserVariables( new KalmanAlignmentUserVariables( *itAlignable, updateGraph ) );
          }
          else
          {
            AlgebraicVector parameters = alignmentParametersMap[*itAlignable].back()->parameters();
            alignmentParameters = (*itAlignable)->alignmentParameters()->clone( parameters, startError );
            KalmanAlignmentUserVariables* userVariables = new KalmanAlignmentUserVariables( *itAlignable, updateGraph );
            userVariables->update( alignmentParameters );
            alignmentParameters->setUserVariables( userVariables );
          }
        }
        else
        {
          //cout << "apply DEFAULT param and cov" << endl;
          alignmentParameters = (*itAlignable)->alignmentParameters()->clone( startParameters, startError );
          //alignmentParameters = (*itAlignable)->alignmentParameters()->clone( trueParameters, startError );
          alignmentParameters->setUserVariables( new KalmanAlignmentUserVariables( *itAlignable, updateGraph ) );
        }

        (*itAlignable)->setAlignmentParameters( alignmentParameters );
        //if ( applyParam ) theParameterStore->applyParameters( *itAlignable );

        if ( applyRandomStartValues )
        {
          cout << "applying random start values" << endl;

          AlgebraicVector randomStartParameters = alignmentParameters->parameters();
          AlgebraicSymMatrix randomStartErrors = alignmentParameters->covariance();

          for ( int iParam = 0; iParam < randomStartParameters.num_row(); ++iParam )
          {
            randomStartParameters[iParam] += sqrt(randSig[iParam])*CLHEP::RandGauss::shoot();
            //randomStartErrors[iParam][iParam] += randSig[iParam]*randSig[iParam];
          }

          //cout << randomStartParameters << endl;

          alignmentParameters = (*itAlignable)->alignmentParameters()->clone( randomStartParameters, randomStartErrors );
          (*itAlignable)->setAlignmentParameters( alignmentParameters );
        }

        (*itAlignable)->alignmentParameters()->setValid( true );

      }

      if ( ( applyParam || applyCovar ) && alignmentParametersMap.find( *itAlignable ) != alignmentParametersMap.end() )
      {
        ++iApply;

        vector< AlignmentParameters* > allAlignmentParameters = alignmentParametersMap[*itAlignable];
        vector< AlignmentParameters* >::iterator itParam;

        for ( itParam = allAlignmentParameters.begin(); itParam != allAlignmentParameters.end(); ++itParam )
          applyAlignmentParameters( *itAlignable, *itParam, applyParam, applyCovar );

        if ( ( *itAlignable )->alignmentParameters() )
        {
          KalmanAlignmentUserVariables* userVariables =
            dynamic_cast< KalmanAlignmentUserVariables* >( ( *itAlignable )->alignmentParameters()->userVariables() );
          if ( userVariables ) { ++iAlign; userVariables->setAlignmentFlag( true ); }
        }
      }
    }

    cout << "[" << *itInitSel << "] Set the alignment flag for " << iAlign << " alignables." << endl;
    cout << "[" << *itInitSel << "] number of applied parameters: " << iApply << endl; 
    theSelector->clear();
  }

}
void KalmanAlignmentAlgorithm::initializeAlignmentSetups ( const edm::EventSetup setup) [private]

Definition at line 494 of file KalmanAlignmentAlgorithm.cc.

References cmsDriver::config, gather_cfg::cout, ExpressReco_HICollisions_FallBack::estimator, Exception, KFFittingSmoother::fitter(), edm::EventSetup::get(), reco::get(), getDirection(), edm::ParameterSet::getParameter(), getSortingDirection(), edm::ParameterSet::getUntrackedParameter(), KFFittingSmootherESProducer_cfi::KFFittingSmoother, KFTrajectoryFitterESProducer_cfi::KFTrajectoryFitter, KFTrajectorySmootherESProducer_cfi::KFTrajectorySmoother, Propagator::magneticField(), oppositeDirection(), KFTrajectoryFitter::propagator(), LargeD0_PixelPairStep_cff::propagator, KFTrajectorySmoother::propagator(), KFFittingSmoother::smoother(), theAlignmentSetups, theConfiguration, theNavigator, KFTrajectorySmoother::updator(), ExpressReco_HICollisions_FallBack::updator, and KFTrajectoryFitter::updator().

Referenced by initialize().

{
  // Read the setups from the config-file. They define which rec-hits are refitted to tracklets and whether they are used
  // as an external measurement or not.

  const edm::ParameterSet initConfig = theConfiguration.getParameter< edm::ParameterSet >( "AlgorithmConfig" );
  const vector<string> selSetup = initConfig.getParameter< vector<string> >( "Setups" );

  for ( vector<string>::const_iterator itSel = selSetup.begin(); itSel != selSetup.end(); ++itSel )
  {
    cout << "[KalmanAlignmentAlgorithm::initializeAlignmentSetups] Add AlignmentSetup: " << *itSel << endl;

    const edm::ParameterSet confSetup = initConfig.getParameter< edm::ParameterSet >( *itSel );

    string strPropDir = confSetup.getUntrackedParameter< string >( "PropagationDirection", "alongMomentum" );
    string strSortingDir = confSetup.getUntrackedParameter< string >( "SortingDirection", "SortInsideOut" );
    vector<int> trackingIDs = confSetup.getParameter< vector<int> >( "Tracking" );
    unsigned int minTrackingHits = confSetup.getUntrackedParameter< unsigned int >( "MinTrackingHits", 0 );
    
    string strExternalPropDir = confSetup.getUntrackedParameter< string >( "ExternalPropagationDirection", "alongMomentum" );
    string strExternalSortingDir = confSetup.getUntrackedParameter< string >( "ExternalSortingDirection", "SortInsideOut" );
    vector<int> externalIDs = confSetup.getParameter< vector<int> >( "External" );
    unsigned int minExternalHits = confSetup.getUntrackedParameter< unsigned int >( "MinExternalHits", 0 );

    edm::ESHandle< TrajectoryFitter > aTrajectoryFitter;
    string fitterName = confSetup.getUntrackedParameter< string >( "Fitter", "KFFittingSmoother" );
    setup.get<TrajectoryFitter::Record>().get( fitterName, aTrajectoryFitter );

    double outlierEstimateCut = 5.;

    const KFFittingSmoother* aFittingSmoother = dynamic_cast< const KFFittingSmoother* >( aTrajectoryFitter.product() );
    if ( aFittingSmoother )
    {
      KFTrajectoryFitter* fitter = 0;
      KFTrajectorySmoother* smoother = 0;
      CurrentAlignmentKFUpdator* updator = new CurrentAlignmentKFUpdator( theNavigator );

      KFTrajectoryFitter* externalFitter = 0;
      KFTrajectorySmoother* externalSmoother = 0;

      PropagationDirection fitterDir = getDirection( strPropDir );
      PropagationDirection externalFitterDir = getDirection( strExternalPropDir );

      PropagationDirection smootherDir = oppositeDirection( fitterDir );
      PropagationDirection externalSmootherDir = oppositeDirection( externalFitterDir );

      const KFTrajectoryFitter* aKFFitter = dynamic_cast< const KFTrajectoryFitter* >( aFittingSmoother->fitter() );
      if ( aKFFitter )
      {
        PropagatorWithMaterial propagator( fitterDir, 0.106, aKFFitter->propagator()->magneticField() );
        Chi2MeasurementEstimator estimator( 30. );
        fitter = new KFTrajectoryFitter( &propagator, updator, &estimator );
//      fitter = new KFTrajectoryFitter( &propagator, aKFFitter->updator(), &estimator );

        AnalyticalPropagator externalPropagator( aKFFitter->propagator()->magneticField(), externalFitterDir );
        Chi2MeasurementEstimator externalEstimator( 1000. );
        externalFitter = new KFTrajectoryFitter( &externalPropagator, aKFFitter->updator(), &externalEstimator );
      }

      const KFTrajectorySmoother* aKFSmoother = dynamic_cast< const KFTrajectorySmoother* >( aFittingSmoother->smoother() );
      if ( aKFSmoother )
      {

        PropagatorWithMaterial propagator( smootherDir, 0.106, aKFSmoother->propagator()->magneticField() );
        Chi2MeasurementEstimator estimator( 30. );
        smoother = new KFTrajectorySmoother( &propagator, updator, &estimator );
//      smoother = new KFTrajectorySmoother( &propagator, aKFFitter->updator(), &estimator );

        AnalyticalPropagator externalPropagator( aKFSmoother->propagator()->magneticField(), externalSmootherDir );
        Chi2MeasurementEstimator externalEstimator( 1000. );
        externalSmoother = new KFTrajectorySmoother( &externalPropagator, aKFSmoother->updator(), &externalEstimator );
      }

      if ( fitter && smoother )
      {
        KFFittingSmoother* fittingSmoother = new KFFittingSmoother( *fitter, *smoother, outlierEstimateCut );
        KFFittingSmoother* externalFittingSmoother = new KFFittingSmoother( *externalFitter, *externalSmoother );
//      KFFittingSmoother* fittingSmoother = aFittingSmoother->clone();
//      KFFittingSmoother* externalFittingSmoother = aFittingSmoother->clone();

        string identifier;
        edm::ParameterSet config;

        config = confSetup.getParameter< edm::ParameterSet >( "TrajectoryFactory" );
        identifier = config.getParameter< string >( "TrajectoryFactoryName" );
        cout << "TrajectoryFactoryPlugin::get() ... " << identifier << endl;
        TrajectoryFactoryBase* trajectoryFactory = TrajectoryFactoryPlugin::get()->create( identifier, config );

        config = confSetup.getParameter< edm::ParameterSet >( "AlignmentUpdator" );
        identifier = config.getParameter< string >( "AlignmentUpdatorName" );
        KalmanAlignmentUpdator* alignmentUpdator = KalmanAlignmentUpdatorPlugin::get()->create( identifier, config );

        config = confSetup.getParameter< edm::ParameterSet >( "MetricsUpdator" );
        identifier = config.getParameter< string >( "MetricsUpdatorName" );
        KalmanAlignmentMetricsUpdator* metricsUpdator = KalmanAlignmentMetricsUpdatorPlugin::get()->create( identifier, config );

        KalmanAlignmentSetup::SortingDirection sortingDir = getSortingDirection( strSortingDir );
        KalmanAlignmentSetup::SortingDirection externalSortingDir = getSortingDirection( strExternalSortingDir );

        AlignmentSetup* anAlignmentSetup
          = new AlignmentSetup( *itSel,
                                fittingSmoother, fitter->propagator(), trackingIDs, minTrackingHits, sortingDir,
                                externalFittingSmoother, externalFitter->propagator(), externalIDs, minExternalHits, externalSortingDir,
                                trajectoryFactory, alignmentUpdator, metricsUpdator );

        theAlignmentSetups.push_back( anAlignmentSetup );

        delete fittingSmoother;
        delete fitter;
        delete smoother;

        delete externalFittingSmoother;
        delete externalFitter;
        delete externalSmoother;
      }
      else
      {
        throw cms::Exception( "BadConfig" ) << "[KalmanAlignmentAlgorithm::initializeAlignmentSetups] "
                                            << "Instance of class KFFittingSmoother has no KFTrajectoryFitter/KFTrajectorySmoother.";
      }

      delete updator;
    }
    else
    {
      throw cms::Exception( "BadConfig" ) << "[KalmanAlignmentAlgorithm::initializeAlignmentSetups] "
                                          << "No instance of class KFFittingSmoother registered to the TrackingComponentsRecord.";
    }
  }

  cout << "[KalmanAlignmentAlgorithm::initializeAlignmentSetups] I'm using " << theAlignmentSetups.size() << " AlignmentSetup(s)." << endl;

}
void KalmanAlignmentAlgorithm::mergeResults ( void  ) const [private]

Definition at line 683 of file KalmanAlignmentAlgorithm.cc.

References applyAlignmentParameters(), AlignmentParameters::clone(), gather_cfg::cout, getComponents(), edm::ParameterSet::getParameter(), AlignmentIORoot::readAlignmentParameters(), theConfiguration, theTracker, and AlignmentIORoot::writeAlignmentParameters().

Referenced by initialize().

{
  std::cout << "[KalmanAlignmentAlgorithm::mergeResults] START MERGING RESULTS" << std::endl;

  edm::ParameterSet mergeConf = theConfiguration.getParameter<edm::ParameterSet>( "Merger" );

  vector<string> inFileNames = mergeConf.getParameter< vector<string> >( "InputMergeFileNames" );
  string outFileName = mergeConf.getParameter<string>( "OutputMergeFileName" );

  bool applyPar = mergeConf.getParameter<bool>( "ApplyParameters" );
  bool applyCov = mergeConf.getParameter<bool>( "ApplyErrors" );

  map< Alignable*, vector< AlignmentParameters* > > alignmentParametersMap;

  vector< Alignable* > allAlignables;
  getComponents( theTracker, allAlignables );

  cout << "allAlignables.size() = " << allAlignables.size() << endl;

  AlignmentIORoot alignmentIO;

  for ( vector<string>::iterator itFile = inFileNames.begin(); itFile != inFileNames.end(); ++itFile )
  {
    int iter = 1;
    int ierr = 0;

    while ( !ierr )
    {
      cout << "Read alignment parameters. file / iteration = " << *itFile << " / " << iter << endl;

      vector< AlignmentParameters* > alignmentParameters =
        alignmentIO.readAlignmentParameters( allAlignables, (*itFile).c_str(), iter, ierr );

      cout << "#param / ierr = " << alignmentParameters.size() << " / " << ierr << endl;
      
      vector< AlignmentParameters* >::iterator itParam;
      for ( itParam = alignmentParameters.begin(); itParam != alignmentParameters.end(); ++itParam )
          alignmentParametersMap[(*itParam)->alignable()].push_back( *itParam );

      ++iter;
    }
  }

  vector< Alignable* > alignablesToWrite;
  alignablesToWrite.reserve( alignmentParametersMap.size() );

  map< Alignable*, vector< AlignmentParameters* > >::iterator itMap;
  for ( itMap = alignmentParametersMap.begin(); itMap != alignmentParametersMap.end(); ++itMap )
  {
    //cout << "merge param for alignable" << itMap->first << endl;

    AlgebraicVector mergedParam( 6, 0 );
    AlgebraicSymMatrix mergedCov( 6, 0 );
    int nMerge = 0;

    vector< AlignmentParameters* >& vecParam = itMap->second;
    vector< AlignmentParameters* >::iterator itParam;
    for ( itParam = vecParam.begin(); itParam != vecParam.end(); ++itParam, ++nMerge )
      mergedParam += (*itParam)->parameters();

    mergedParam /= nMerge;

    // no merging of errors up to now
    AlignmentParameters* mergedAliParam = vecParam.front()->clone( mergedParam, mergedCov );
    itMap->first->setAlignmentParameters( mergedAliParam );

    alignablesToWrite.push_back( itMap->first );

    if ( applyPar || applyCov ) applyAlignmentParameters( itMap->first, mergedAliParam, applyPar, applyCov );
  }

  cout << "alignablesToWrite.size() = " << alignablesToWrite.size() << endl;

  int ierr = 0;
  // Write output to "iteration 1", ...
  alignmentIO.writeAlignmentParameters( alignablesToWrite, outFileName.c_str(), 1, false, ierr );
  // ... or, if "iteration 1" already exists, write it to "highest iteration + 1"
  if ( ierr == -1 ) alignmentIO.writeAlignmentParameters( alignablesToWrite, outFileName.c_str(), -1, false, ierr );

  std::cout << "[KalmanAlignmentAlgorithm::mergeResults] DONE" << std::endl;
}
bool KalmanAlignmentAlgorithm::operator() ( const Alignable a1,
const Alignable a2 
) const [inline]

Definition at line 57 of file KalmanAlignmentAlgorithm.h.

References Alignable::id().

{ return ( a1->id() < a2->id() ); }
const PropagationDirection KalmanAlignmentAlgorithm::oppositeDirection ( const PropagationDirection  dir) const [inline, private]

Definition at line 76 of file KalmanAlignmentAlgorithm.h.

References alongMomentum, and oppositeToMomentum.

Referenced by initializeAlignmentSetups().

virtual void KalmanAlignmentAlgorithm::produce ( edm::Event ,
const edm::EventSetup  
) [inline, virtual]

Dummy implementation. Needed for inheritance of TrackProducerBase, but we don't produce anything.

Definition at line 45 of file KalmanAlignmentAlgorithm.h.

{}
void KalmanAlignmentAlgorithm::run ( const edm::EventSetup setup,
const EventInfo eventInfo 
) [virtual]

Run the algorithm (must be implemented in derived class)

Implements AlignmentAlgorithmBase.

Definition at line 149 of file KalmanAlignmentAlgorithm.cc.

References ExpressReco_HICollisions_FallBack::beamSpot, AlignmentAlgorithmBase::EventInfo::beamSpot_, gather_cfg::cout, exception, KalmanAlignmentDataCollector::fillHistogram(), edm::EventSetup::get(), iEvent, edm::ESHandle< T >::product(), KalmanAlignmentTrackRefitter::refitTracks(), terminate(), theAlignmentSetups, theMergerFlag, theNavigator, theParameterStore, theRefitter, testEve_cfg::tracks, AlignmentAlgorithmBase::EventInfo::trajTrackPairs_, and cms::Exception::what().

{
  if ( theMergerFlag ) return; // only merging mode. nothing to do here.

  static int iEvent = 1;
  if ( iEvent % 100 == 0 ) cout << "[KalmanAlignmentAlgorithm::run] Event Nr. " << iEvent << endl;
  iEvent++;

  edm::ESHandle< MagneticField > aMagneticField;
  setup.get< IdealMagneticFieldRecord >().get( aMagneticField );  

  try
  {
    // Run the refitter algorithm
    const ConstTrajTrackPairCollection &tracks = eventInfo.trajTrackPairs_;
    const reco::BeamSpot &beamSpot = eventInfo.beamSpot_;
    TrackletCollection refittedTracklets = theRefitter->refitTracks( setup, theAlignmentSetups, tracks, &beamSpot );

    // Associate tracklets to alignment setups
    map< AlignmentSetup*, TrackletCollection > setupToTrackletMap;
    TrackletCollection::iterator itTracklet;
    for ( itTracklet = refittedTracklets.begin(); itTracklet != refittedTracklets.end(); ++itTracklet )
      setupToTrackletMap[(*itTracklet)->alignmentSetup()].push_back( *itTracklet );

    // Iterate on alignment setups
    map< AlignmentSetup*, TrackletCollection >::iterator itMap;
    for ( itMap = setupToTrackletMap.begin(); itMap != setupToTrackletMap.end(); ++itMap )
    {
      ConstTrajTrackPairCollection tracklets;
      ExternalPredictionCollection external;

      TrackletCollection::iterator itTracklet;
      for ( itTracklet = itMap->second.begin(); itTracklet != itMap->second.end(); ++itTracklet )
      {
        tracklets.push_back( (*itTracklet)->trajTrackPair() );
        external.push_back( (*itTracklet)->externalPrediction() );
      }

      // Construct reference trajectories
      ReferenceTrajectoryCollection trajectories =
        itMap->first->trajectoryFactory()->trajectories( setup, tracklets, external, eventInfo.beamSpot_ );

      ReferenceTrajectoryCollection::iterator itTrajectories;

      // Run the alignment algorithm.
      for ( itTrajectories = trajectories.begin(); itTrajectories != trajectories.end(); ++itTrajectories )
      {
        itMap->first->alignmentUpdator()->process( *itTrajectories, theParameterStore, theNavigator,
                                                   itMap->first->metricsUpdator(), aMagneticField.product() );

        KalmanAlignmentDataCollector::fillHistogram( "Trajectory_RecHits", (*itTrajectories)->recHits().size() );
      }
    }
  }
  catch( cms::Exception& exception )
  {
    cout << exception.what() << endl;
    terminate();
    throw exception;
  }
}
void KalmanAlignmentAlgorithm::setAPEToZero ( void  ) [private]
void KalmanAlignmentAlgorithm::terminate ( void  ) [virtual]

Call at end of job (must be implemented in derived class)

Implements AlignmentAlgorithmBase.

Definition at line 83 of file KalmanAlignmentAlgorithm.cc.

References gather_cfg::cout, edm::ParameterSet::getParameter(), edm::ParameterSet::getUntrackedParameter(), KalmanAlignmentUserVariables::histogramParameters(), KalmanAlignmentUserVariables::numberOfUpdates(), convertSQLitetoXML_cfg::output, theAlignmentSetups, theConfiguration, theMergerFlag, theNavigator, KalmanAlignmentUserVariables::update(), AlignmentParameters::userVariables(), KalmanAlignmentDataCollector::write(), and AlignmentIORoot::writeAlignmentParameters().

Referenced by run().

{
  if ( theMergerFlag )
  {
    return; // only merging mode. nothing to do here.
  }

  cout << "[KalmanAlignmentAlgorithm::terminate] start ..." << endl;

  set< Alignable* > allAlignables;
  vector< Alignable* > alignablesToWrite;

  AlignmentSetupCollection::const_iterator itSetup;
  for ( itSetup = theAlignmentSetups.begin(); itSetup != theAlignmentSetups.end(); ++itSetup )
  {
    delete (*itSetup)->alignmentUpdator();

    const vector< Alignable* >& alignablesFromMetrics  = (*itSetup)->metricsUpdator()->alignables();
    cout << "[KalmanAlignmentAlgorithm::terminate] The metrics updator for setup \'" << (*itSetup)->id()
         << "\' holds " << alignablesFromMetrics.size() << " alignables" << endl;
    allAlignables.insert( alignablesFromMetrics.begin(), alignablesFromMetrics.end() );
  }

  for ( set< Alignable* >::iterator it = allAlignables.begin(); it != allAlignables.end(); ++it )
  {
    AlignmentParameters* alignmentParameters = ( *it )->alignmentParameters();

    if ( alignmentParameters != 0 )
    {
      KalmanAlignmentUserVariables* userVariables =
        dynamic_cast< KalmanAlignmentUserVariables* >( alignmentParameters->userVariables() );

      if ( userVariables != 0 && userVariables->numberOfUpdates() > 0 )
      {
        userVariables->update( true );
        userVariables->histogramParameters( "KalmanAlignmentAlgorithm" );
        alignablesToWrite.push_back( *it );
      }
    }
  }

  if ( theConfiguration.getUntrackedParameter< bool >( "WriteAlignmentParameters", false ) )
  {
    AlignmentIORoot alignmentIO;
    int ierr = 0;
    string output = theConfiguration.getParameter< string >( "OutputFile" );

    cout << "Write data for " << alignablesToWrite.size() << " alignables ..." << endl;

    // Write output to "iteration 1", ...
    alignmentIO.writeAlignmentParameters( alignablesToWrite, output.c_str(), 1, false, ierr );
    // ... or, if "iteration 1" already exists, write it to "highest iteration + 1"
    if ( ierr == -1 ) alignmentIO.writeAlignmentParameters( alignablesToWrite, output.c_str(), -1, false, ierr );
  }

  KalmanAlignmentDataCollector::write();

  string timingLogFile = theConfiguration.getUntrackedParameter< string >( "TimingLogFile", "timing.log" );
  cout << "The usage of TimingReport from Utilities/Timing! Timing not written to log file" << endl;
  
  delete theNavigator;

  cout << "[KalmanAlignmentAlgorithm::terminate] ... done." << endl;
}

Member Data Documentation

Definition at line 83 of file KalmanAlignmentAlgorithm.h.

Referenced by initializeAlignmentSetups(), run(), and terminate().

Definition at line 93 of file KalmanAlignmentAlgorithm.h.

Referenced by initialize(), run(), and terminate().

Definition at line 88 of file KalmanAlignmentAlgorithm.h.

Referenced by initialize(), initializeAlignmentSetups(), run(), and terminate().

Definition at line 87 of file KalmanAlignmentAlgorithm.h.

Referenced by initialize(), and run().

Definition at line 85 of file KalmanAlignmentAlgorithm.h.

Referenced by initialize(), and run().

Definition at line 89 of file KalmanAlignmentAlgorithm.h.

Referenced by initialize(), and initializeAlignmentParameters().

Definition at line 91 of file KalmanAlignmentAlgorithm.h.

Referenced by initialize(), mergeResults(), and setAPEToZero().