CMS 3D CMS Logo

KalmanAlignmentAlgorithm.cc

Go to the documentation of this file.
00001 
00002 //#include "Alignment/KalmanAlignmentAlgorithm/plugins/KalmanAlignmentAlgorithm.h"
00003 #include "KalmanAlignmentAlgorithm.h"
00004 #include "Alignment/CommonAlignmentAlgorithm/interface/AlignmentAlgorithmPluginFactory.h"
00005 
00006 // includes for alignment
00007 #include "Alignment/CommonAlignment/interface/AlignableNavigator.h"
00008 #include "Alignment/CommonAlignment/interface/Utilities.h"
00009 #include "Alignment/CommonAlignmentAlgorithm/interface/AlignmentIORoot.h"
00010 #include "Alignment/CommonAlignmentAlgorithm/interface/AlignmentParameterSelector.h"
00011 
00012 //#include "Alignment/KalmanAlignmentAlgorithm/interface/KalmanAlignmentUpdator.h"
00013 //#include "Alignment/KalmanAlignmentAlgorithm/interface/KalmanAlignmentMetricsUpdator.h"
00014 #include "Alignment/KalmanAlignmentAlgorithm/interface/KalmanAlignmentUpdatorPlugin.h"
00015 #include "Alignment/KalmanAlignmentAlgorithm/interface/KalmanAlignmentMetricsUpdatorPlugin.h"
00016 #include "Alignment/KalmanAlignmentAlgorithm/interface/KalmanAlignmentUserVariables.h"
00017 #include "Alignment/KalmanAlignmentAlgorithm/interface/KalmanAlignmentDataCollector.h"
00018 #include "Alignment/KalmanAlignmentAlgorithm/interface/CurrentAlignmentKFUpdator.h"
00019 #include "Alignment/CommonAlignmentParametrization/interface/RigidBodyAlignmentParameters.h"
00020 
00021 #include "Alignment/ReferenceTrajectories/interface/TrajectoryFactoryPlugin.h"
00022 
00023 #include "Alignment/TrackerAlignment/interface/TrackerAlignableId.h"
00024 #include "Alignment/TrackerAlignment/interface/AlignableTracker.h"
00025 
00026 #include "TrackingTools/TrackFitters/interface/KFFittingSmoother.h"
00027 #include "TrackingTools/TrackFitters/interface/KFTrajectoryFitter.h"
00028 #include "TrackingTools/TrackFitters/interface/KFTrajectorySmoother.h"
00029 #include "TrackingTools/KalmanUpdators/interface/Chi2MeasurementEstimator.h"
00030 #include "TrackingTools/GeomPropagators/interface/AnalyticalPropagator.h"
00031 #include "TrackingTools/MaterialEffects/interface/PropagatorWithMaterial.h"
00032 #include "TrackingTools/Records/interface/TrackingComponentsRecord.h"
00033 
00034 #include "DataFormats/TrackingRecHit/interface/AlignmentPositionError.h"
00035 
00036 // miscellaneous includes
00037 #include "FWCore/Utilities/interface/Exception.h"
00038 #include "Utilities/Timing/interface/TimingReport.h"
00039 #include "CLHEP/Random/RandGauss.h"
00040 #include <fstream>
00041 
00042 using namespace std;
00043 
00044 
00045 KalmanAlignmentAlgorithm::KalmanAlignmentAlgorithm( const edm::ParameterSet& config ) :
00046   AlignmentAlgorithmBase( config ),
00047   theConfiguration( config )
00048 {}
00049 
00050 
00051 KalmanAlignmentAlgorithm::~KalmanAlignmentAlgorithm( void ) {}
00052 
00053 
00054 void KalmanAlignmentAlgorithm::initialize( const edm::EventSetup& setup, 
00055                                            AlignableTracker* tracker, 
00056                                            AlignableMuon* muon,
00057                                            AlignmentParameterStore* store )
00058 {
00059   theParameterStore = store;
00060   theNavigator = new AlignableNavigator( tracker->components() );
00061   theSelector = new AlignmentParameterSelector( tracker );
00062 
00063   theRefitter = new KalmanAlignmentTrackRefitter( theConfiguration.getParameter< edm::ParameterSet >( "TrackRefitter" ), theNavigator );
00064 
00065   initializeAlignmentParameters( setup );
00066   initializeAlignmentSetups( setup );
00067 
00068   KalmanAlignmentDataCollector::configure( theConfiguration.getParameter< edm::ParameterSet >( "DataCollector" ) );
00069 }
00070 
00071 
00072 void KalmanAlignmentAlgorithm::terminate( void )
00073 {
00074   cout << "[KalmanAlignmentAlgorithm::terminate] start ..." << endl;
00075 
00076   set< Alignable* > allAlignables;
00077   vector< Alignable* > alignablesToWrite;
00078 
00079   AlignmentSetupCollection::const_iterator itSetup;
00080   for ( itSetup = theAlignmentSetups.begin(); itSetup != theAlignmentSetups.end(); ++itSetup )
00081   {
00082     delete (*itSetup)->alignmentUpdator();
00083 
00084     const vector< Alignable* >& alignablesFromMetrics  = (*itSetup)->metricsUpdator()->alignables();
00085     cout << "[KalmanAlignmentAlgorithm::terminate] The metrics updator for setup \'" << (*itSetup)->id()
00086          << "\' holds " << alignablesFromMetrics.size() << " alignables" << endl;
00087     allAlignables.insert( alignablesFromMetrics.begin(), alignablesFromMetrics.end() );
00088   }
00089 
00090   for ( set< Alignable* >::iterator it = allAlignables.begin(); it != allAlignables.end(); ++it )
00091   {
00092     AlignmentParameters* alignmentParameters = ( *it )->alignmentParameters();
00093 
00094     if ( alignmentParameters != 0 )
00095     {
00096       KalmanAlignmentUserVariables* userVariables =
00097         dynamic_cast< KalmanAlignmentUserVariables* >( alignmentParameters->userVariables() );
00098 
00099       if ( userVariables != 0 && userVariables->numberOfUpdates() > 0 )
00100       {
00101         userVariables->update( true );
00102         userVariables->histogramParameters( "KalmanAlignmentAlgorithm" );
00103         alignablesToWrite.push_back( *it );
00104       }
00105     }
00106   }
00107 
00108   if ( theConfiguration.getUntrackedParameter< bool >( "WriteAlignmentParameters", false ) )
00109   {
00110     AlignmentIORoot alignmentIO;
00111     int ierr = 0;
00112     string output = theConfiguration.getParameter< string >( "OutputFile" );
00113 
00114     cout << "Write data for " << alignablesToWrite.size() << " alignables ..." << endl;
00115 
00116     // Write output to "iteration 1", ...
00117     alignmentIO.writeAlignmentParameters( alignablesToWrite, output.c_str(), 1, false, ierr );
00118     // ... or, if "iteration 1" already exists, write it to "highest iteration + 1"
00119     if ( ierr == -1 ) alignmentIO.writeAlignmentParameters( alignablesToWrite, output.c_str(), -1, false, ierr );
00120   }
00121 
00122   KalmanAlignmentDataCollector::write();
00123 
00124   TimingReport* timing = TimingReport::current();
00125   timing->dump( cout );  
00126 
00127   string timingLogFile = theConfiguration.getUntrackedParameter< string >( "TimingLogFile", "timing.log" );
00128 
00129   ofstream* output = new ofstream( timingLogFile.c_str() );
00130   timing->dump( *output );
00131   output->close();
00132   delete output;
00133 
00134   delete theNavigator;
00135 
00136   cout << "[KalmanAlignmentAlgorithm::terminate] ... done." << endl;
00137 }
00138 
00139 
00140 void KalmanAlignmentAlgorithm::run( const edm::EventSetup & setup,
00141                                     const ConstTrajTrackPairCollection & tracks )
00142 {
00143   static int iEvent = 1;
00144   if ( iEvent % 500 == 0 )  cout << "[KalmanAlignmentAlgorithm::run] Event Nr. " << iEvent << endl;
00145   iEvent++;
00146 
00147   try
00148   {
00149     // Run the refitter algorithm
00150     TrackletCollection refittedTracklets = theRefitter->refitTracks( setup, theAlignmentSetups, tracks );
00151 
00152     // Associate tracklets to alignment setups
00153     map< AlignmentSetup*, TrackletCollection > setupToTrackletMap;
00154     TrackletCollection::iterator itTracklet;
00155     for ( itTracklet = refittedTracklets.begin(); itTracklet != refittedTracklets.end(); ++itTracklet )
00156       setupToTrackletMap[(*itTracklet)->alignmentSetup()].push_back( *itTracklet );
00157 
00158     // Iterate on alignment setups
00159     map< AlignmentSetup*, TrackletCollection >::iterator itMap;
00160     for ( itMap = setupToTrackletMap.begin(); itMap != setupToTrackletMap.end(); ++itMap )
00161     {
00162       ConstTrajTrackPairCollection tracklets;
00163       ExternalPredictionCollection external;
00164 
00165       TrackletCollection::iterator itTracklet;
00166       for ( itTracklet = itMap->second.begin(); itTracklet != itMap->second.end(); ++itTracklet )
00167       {
00168         tracklets.push_back( (*itTracklet)->trajTrackPair() );
00169         external.push_back( (*itTracklet)->externalPrediction() );
00170       }
00171 
00172       // Construct reference trajectories
00173       ReferenceTrajectoryCollection trajectories = itMap->first->trajectoryFactory()->trajectories( setup, tracklets, external );
00174       ReferenceTrajectoryCollection::iterator itTrajectories;
00175 
00176       // Run the alignment algorithm.
00177       for ( itTrajectories = trajectories.begin(); itTrajectories != trajectories.end(); ++itTrajectories )
00178       {
00179         itMap->first->alignmentUpdator()->process( *itTrajectories, theParameterStore, theNavigator, itMap->first->metricsUpdator() );
00180 
00181         KalmanAlignmentDataCollector::fillHistogram( "Trajectory_RecHits", (*itTrajectories)->recHits().size() );
00182       }
00183     }
00184   }
00185   catch( cms::Exception& exception )
00186   {
00187     cout << exception.what() << endl;
00188     terminate();
00189     throw exception;
00190   }
00191 
00192 }
00193 
00194 
00195 void KalmanAlignmentAlgorithm::initializeAlignmentParameters( const edm::EventSetup& setup )
00196 {
00197   TrackerAlignableId* alignableId = new TrackerAlignableId;
00198 
00199   const edm::ParameterSet initConfig = theConfiguration.getParameter< edm::ParameterSet >( "ParameterConfig" );
00200 
00201   int updateGraph = initConfig.getUntrackedParameter< int >( "UpdateGraphs", 100 );
00202 
00203   bool addPositionError = false;// = initConfig.getUntrackedParameter< bool >( "AddPositionError", true );
00204 
00205   int seed  = initConfig.getUntrackedParameter< int >( "RandomSeed", 1726354 );
00206   HepRandom::createInstance();
00207   HepRandom::setTheSeed( seed );
00208 
00209   bool applyXShifts =  initConfig.getUntrackedParameter< bool >( "ApplyXShifts", false );
00210   bool applyYShifts =  initConfig.getUntrackedParameter< bool >( "ApplyYShifts", false );
00211   bool applyZShifts =  initConfig.getUntrackedParameter< bool >( "ApplyZShifts", false );
00212   bool applyXRots =  initConfig.getUntrackedParameter< bool >( "ApplyXRotations", false );
00213   bool applyYRots =  initConfig.getUntrackedParameter< bool >( "ApplyYRotations", false );
00214   bool applyZRots =  initConfig.getUntrackedParameter< bool >( "ApplyZRotations", false );
00215 
00216   bool applyRandomStartValues = initConfig.getUntrackedParameter< bool >( "ApplyRandomStartValues", false );
00217   if ( applyRandomStartValues )
00218     cout << "[KalmanAlignmentAlgorithm::initializeAlignmentParameters] ADDING RANDOM START VALUES!!!" << endl;
00219 
00220   bool applyCurl =  initConfig.getUntrackedParameter< bool >( "ApplyCurl", false );
00221   double curlConst =  initConfig.getUntrackedParameter< double >( "CurlConstant", 1e-6 );
00222 
00223   bool applyShifts = applyXShifts || applyYShifts || applyZShifts;
00224   bool applyRots = applyXRots || applyYRots || applyZRots;
00225   //bool applyMisalignment = applyShifts || applyRots || applyCurl;
00226 
00227   AlgebraicVector displacement( 3 );
00228   AlgebraicVector eulerAngles( 3 );
00229 
00230   AlgebraicVector startParameters( 6, 0 );
00231   AlgebraicSymMatrix startError( 6, 0 );
00232 
00233   AlgebraicVector randSig( 6, 0 );
00234 
00235   vector< string > initSelection = initConfig.getParameter< vector<string> >( "InitializationSelector" );
00236 
00237   vector< string >::iterator itInitSel;
00238   for ( itInitSel = initSelection.begin(); itInitSel != initSelection.end(); ++itInitSel )
00239   {
00240     const edm::ParameterSet config = initConfig.getParameter< edm::ParameterSet >( *itInitSel );
00241 
00242     addPositionError = initConfig.getUntrackedParameter< bool >( "AddPositionError", false );
00243 
00244     double sigmaXShift = config.getUntrackedParameter< double >( "SigmaXShifts", 4e-2 );
00245     double sigmaYShift = config.getUntrackedParameter< double >( "SigmaYShifts", 4e-2 );
00246     double sigmaZShift = config.getUntrackedParameter< double >( "SigmaZShifts", 4e-2 );
00247     double sigmaXRot = config.getUntrackedParameter< double >( "SigmaXRotations", 5e-4 );
00248     double sigmaYRot = config.getUntrackedParameter< double >( "SigmaYRotations", 5e-4 );
00249     double sigmaZRot = config.getUntrackedParameter< double >( "SigmaZRotations", 5e-4 );
00250 
00251     randSig[0] = sigmaXShift; randSig[1] = sigmaYShift; randSig[2] = sigmaZShift;
00252     randSig[3] = sigmaXRot; randSig[4] = sigmaYRot; randSig[5] = sigmaZRot;
00253 
00254     startError[0][0] = config.getUntrackedParameter< double >( "XShiftsStartError", 4e-4 );
00255     startError[1][1] = config.getUntrackedParameter< double >( "YShiftsStartError", 4e-4 );
00256     startError[2][2] = config.getUntrackedParameter< double >( "ZShiftsStartError", 4e-4 );
00257     startError[3][3] = config.getUntrackedParameter< double >( "XRotationsStartError", 3e-5 );
00258     startError[4][4] = config.getUntrackedParameter< double >( "YRotationsStartError", 3e-5 );
00259     startError[5][5] = config.getUntrackedParameter< double >( "ZRotationsStartError", 3e-5 );
00260 
00261     const vector< char > dummyParamSelector( 6, '0' );
00262     const vector< string > alignableSelector = config.getParameter< vector<string> >( "AlignableSelection" );
00263 
00264     vector< string >::const_iterator itAliSel;
00265     for ( itAliSel = alignableSelector.begin(); itAliSel != alignableSelector.end(); ++itAliSel )
00266     {
00267       theSelector->addSelection( *itAliSel, dummyParamSelector );
00268       cout << "[" << *itInitSel << "] add selection: " << *itAliSel << endl;
00269     }
00270 
00271     vector< Alignable* >::iterator itAlignable;
00272     vector< Alignable* > alignables;
00273     vector< Alignable* > alignablesFromSelector = theSelector->selectedAlignables();
00274     for ( itAlignable = alignablesFromSelector.begin(); itAlignable != alignablesFromSelector.end(); ++itAlignable )
00275       //if ( (*itAlignable)->alignmentParameters() )
00276       alignables.push_back( *itAlignable );
00277 
00278     sort( alignables.begin(), alignables.end(), *this );
00279 
00280     // Apply existing alignment parameters.
00281     map< Alignable*, vector< AlignmentParameters* > > alignmentParametersMap;
00282 
00283     int iApply = 0;
00284     bool readParam = config.getUntrackedParameter< bool >( "ReadParametersFromFile", false );
00285     bool readCovar = config.getUntrackedParameter< bool >( "ReadCovarianceFromFile", false );
00286     bool applyParam = config.getUntrackedParameter< bool >( "ApplyParametersFromFile", false );
00287     bool applyCovar = config.getUntrackedParameter< bool >( "ApplyErrorFromFile", false );
00288     if ( readParam || readCovar || applyParam || applyCovar )
00289     {
00290       string file = config.getUntrackedParameter< string >( "FileName", "Input.root" );
00291       int ierr = 0;
00292       int iter = 1;
00293 
00294       AlignmentIORoot alignmentIO;
00295 
00296       while ( !ierr )
00297       {
00298         cout << "[" << *itInitSel << "] read alignment parameters. file / iteration = " << file << " / " << iter << endl;
00299         vector< AlignmentParameters* > alignmentParameters = alignmentIO.readAlignmentParameters( alignables, file.c_str(), iter, ierr );
00300         cout << "[" << *itInitSel << "] #param / ierr = " << alignmentParameters.size() << " / " << ierr << endl;
00301 
00302         vector< AlignmentParameters* >::iterator itParam;
00303         for ( itParam = alignmentParameters.begin(); itParam != alignmentParameters.end(); ++itParam )
00304           alignmentParametersMap[(*itParam)->alignable()].push_back( *itParam );
00305 
00306         ++iter;
00307       }
00308     }
00309 
00310     int iAlign = 0;
00311 
00312     for ( itAlignable = alignables.begin(); itAlignable != alignables.end(); itAlignable++ )
00313     {
00314       displacement[0] = applyXShifts ? sigmaXShift*RandGauss::shoot() : 0.;
00315       displacement[1] = applyYShifts ? sigmaZShift*RandGauss::shoot() : 0.;
00316       displacement[2] = applyZShifts ? sigmaYShift*RandGauss::shoot() : 0.;
00317 
00318       if ( applyShifts ) 
00319       {
00320         align::LocalVector localShift( displacement[0], displacement[1], displacement[2] );
00321         align::GlobalVector globalShift = ( *itAlignable )->surface().toGlobal( localShift );
00322         ( *itAlignable )->move( globalShift );
00323       }
00324 
00325       align::EulerAngles eulerAngles( 3 );
00326 
00327       eulerAngles[0] = applyXRots ? sigmaXRot*RandGauss::shoot() : 0.;
00328       eulerAngles[1] = applyYRots ? sigmaYRot*RandGauss::shoot() : 0.;
00329       eulerAngles[2] = applyZRots ? sigmaZRot*RandGauss::shoot() : 0.;
00330 
00331       if ( applyRots )
00332       {
00333         align::RotationType localRotation = align::toMatrix( eulerAngles );
00334         ( *itAlignable )->rotateInLocalFrame( localRotation );
00335       }
00336 
00337       if ( applyCurl )
00338       {
00339         double radius = ( *itAlignable )->globalPosition().perp();
00340         ( *itAlignable )->rotateAroundGlobalZ( curlConst*radius );
00341       }
00342 
00343       if ( addPositionError )
00344       {
00345         LocalVector localError( sqrt(startError[0][0]), sqrt(startError[1][1]), sqrt(startError[2][2]) );
00346         GlobalVector globalError = (*itAlignable)->surface().toGlobal( localError );
00347         AlignmentPositionError ape( globalError.x(), globalError.y(), globalError.z() );
00348         ( *itAlignable )->setAlignmentPositionError( ape );
00349       }
00350 
00351       //AlgebraicVector trueParameters( 6 );
00352       //trueParameters[0] = -displacement[0];
00353       //trueParameters[1] = -displacement[1];
00354       //trueParameters[2] = -displacement[2];
00355       //trueParameters[3] = -eulerAngles[0];
00356       //trueParameters[4] = -eulerAngles[1];
00357       //trueParameters[5] = -eulerAngles[2];
00358 
00359       if ( (*itAlignable)->alignmentParameters() != 0 )
00360       {
00361         AlignmentParameters* alignmentParameters;
00362         if ( readParam && readCovar )
00363         {
00364           if ( alignmentParametersMap.find( *itAlignable ) == alignmentParametersMap.end() )
00365           {
00366             //cout << "apply param and cov from FILE -> none stored, apply DEFAULT " << endl;
00367             alignmentParameters = (*itAlignable)->alignmentParameters()->clone( startParameters, startError );
00368             alignmentParameters->setUserVariables( new KalmanAlignmentUserVariables( *itAlignable, alignableId, updateGraph ) );
00369           }
00370           else
00371           {
00372             //cout << "apply param and cov from FILE" << endl;
00373             alignmentParameters = alignmentParametersMap[*itAlignable].back();
00374             KalmanAlignmentUserVariables* userVariables = new KalmanAlignmentUserVariables( *itAlignable, alignableId, updateGraph );
00375             userVariables->update( alignmentParameters );
00376             alignmentParameters->setUserVariables( userVariables );
00377           }
00378         }
00379         else if ( readParam )
00380         {
00381           if ( alignmentParametersMap.find( *itAlignable ) == alignmentParametersMap.end() )
00382           {
00383             alignmentParameters = (*itAlignable)->alignmentParameters()->clone( startParameters, startError );
00384             alignmentParameters->setUserVariables( new KalmanAlignmentUserVariables( *itAlignable, alignableId, updateGraph ) );
00385           }
00386           else
00387           {
00388             AlgebraicVector parameters = alignmentParametersMap[*itAlignable].back()->parameters();
00389             alignmentParameters = (*itAlignable)->alignmentParameters()->clone( parameters, startError );
00390             KalmanAlignmentUserVariables* userVariables = new KalmanAlignmentUserVariables( *itAlignable, alignableId, updateGraph );
00391             userVariables->update( alignmentParameters );
00392             alignmentParameters->setUserVariables( userVariables );
00393           }
00394         }
00395         else
00396         {
00397           //cout << "apply DEFAULT param and cov" << endl;
00398           alignmentParameters = (*itAlignable)->alignmentParameters()->clone( startParameters, startError );
00399           //alignmentParameters = (*itAlignable)->alignmentParameters()->clone( trueParameters, startError );
00400           alignmentParameters->setUserVariables( new KalmanAlignmentUserVariables( *itAlignable, alignableId, updateGraph ) );
00401         }
00402 
00403         (*itAlignable)->setAlignmentParameters( alignmentParameters );
00404         //if ( applyParam ) theParameterStore->applyParameters( *itAlignable );
00405 
00406         if ( applyRandomStartValues )
00407         {
00408           cout << "applying random start values" << endl;
00409 
00410           AlgebraicVector randomStartParameters = alignmentParameters->parameters();
00411           AlgebraicSymMatrix randomStartErrors = alignmentParameters->covariance();
00412 
00413           for ( int iParam = 0; iParam < randomStartParameters.num_row(); ++iParam )
00414           {
00415             randomStartParameters[iParam] += sqrt(randSig[iParam])*RandGauss::shoot();
00416             //randomStartErrors[iParam][iParam] += randSig[iParam]*randSig[iParam];
00417           }
00418 
00419           cout << randomStartParameters << endl;
00420 
00421           alignmentParameters = (*itAlignable)->alignmentParameters()->clone( randomStartParameters, randomStartErrors );
00422           (*itAlignable)->setAlignmentParameters( alignmentParameters );
00423         }
00424 
00425       }
00426 
00427       if ( ( applyParam || applyCovar ) && alignmentParametersMap.find( *itAlignable ) != alignmentParametersMap.end() )
00428       {
00429         ++iApply;
00430 
00431         vector< AlignmentParameters* > allAlignmentParameters = alignmentParametersMap[*itAlignable];
00432         vector< AlignmentParameters* >::iterator itParam;
00433 
00434         for ( itParam = allAlignmentParameters.begin(); itParam != allAlignmentParameters.end(); ++itParam )
00435         {
00436           RigidBodyAlignmentParameters* alignmentParameters = dynamic_cast<RigidBodyAlignmentParameters*>( *itParam );
00437 
00438           if ( !alignmentParameters )
00439             throw cms::Exception( "BadConfig" ) << "applyParameters: provided alignable does not have rigid body alignment parameters";
00440 
00441           if ( applyParam )
00442           {
00443             AlgebraicVector shift = alignmentParameters->translation();
00444             const AlignableSurface& alignableSurface = ( *itAlignable )->surface();
00445             ( *itAlignable )->move( alignableSurface.toGlobal( align::LocalVector( shift[0], shift[1], shift[2] ) ) );
00446 
00447             align::EulerAngles angles = alignmentParameters->rotation();
00448             if ( angles.normsq() > 1e-10 ) ( *itAlignable )->rotateInLocalFrame( align::toMatrix( angles ) );
00449           }
00450 
00451           if ( applyCovar )
00452           {
00453             const AlgebraicSymMatrix& aliCov = alignmentParameters->covariance();
00454             LocalVector localError( sqrt(aliCov[0][0]), sqrt(aliCov[1][1]), sqrt(aliCov[2][2]) );
00455             GlobalVector globalError = (*itAlignable)->surface().toGlobal( localError );
00456             AlignmentPositionError ape( globalError.x(), globalError.y(), globalError.z() );
00457             ( *itAlignable )->setAlignmentPositionError( ape );
00458           }
00459         }
00460 
00461         if ( ( *itAlignable )->alignmentParameters() )
00462         {
00463           KalmanAlignmentUserVariables* userVariables =
00464             dynamic_cast< KalmanAlignmentUserVariables* >( ( *itAlignable )->alignmentParameters()->userVariables() );
00465           if ( userVariables ) { ++iAlign; userVariables->setAlignmentFlag( true ); }
00466         }
00467       }
00468     }
00469 
00470     cout << "[" << *itInitSel << "] Set the alignment flag for " << iAlign << " alignables." << endl;
00471     cout << "[" << *itInitSel << "] number of applied parameters: " << iApply << endl; 
00472     theSelector->clear();
00473   }
00474 
00475 }
00476 
00477 
00478 void KalmanAlignmentAlgorithm::initializeAlignmentSetups( const edm::EventSetup& setup )
00479 {
00480   // Read the setups from the config-file. They define which rec-hits are refitted to tracklets and whether they are used
00481   // as an external measurement or not.
00482 
00483   const edm::ParameterSet initConfig = theConfiguration.getParameter< edm::ParameterSet >( "AlgorithmConfig" );
00484   const vector<string> selSetup = initConfig.getParameter< vector<string> >( "Setups" );
00485 
00486   for ( vector<string>::const_iterator itSel = selSetup.begin(); itSel != selSetup.end(); ++itSel )
00487   {
00488     cout << "[KalmanAlignmentAlgorithm::initializeAlignmentSetups] Add AlignmentSetup: " << *itSel << endl;
00489 
00490     const edm::ParameterSet confSetup = initConfig.getParameter< edm::ParameterSet >( *itSel );
00491 
00492     string strPropDir = confSetup.getUntrackedParameter< string >( "PropagationDirection", "alongMomentum" );
00493     string strSortingDir = confSetup.getUntrackedParameter< string >( "SortingDirection", "SortInsideOut" );
00494     vector<int> trackingIDs = confSetup.getParameter< vector<int> >( "Tracking" );
00495     unsigned int minTrackingHits = confSetup.getUntrackedParameter< unsigned int >( "MinTrackingHits", 0 );
00496 
00497     string strExternalPropDir = confSetup.getUntrackedParameter< string >( "ExternalPropagationDirection", "alongMomentum" );
00498     string strExternalSortingDir = confSetup.getUntrackedParameter< string >( "ExternalSortingDirection", "SortInsideOut" );
00499     vector<int> externalIDs = confSetup.getParameter< vector<int> >( "External" );
00500     unsigned int minExternalHits = confSetup.getUntrackedParameter< unsigned int >( "MinExternalHits", 0 );
00501 
00502     edm::ESHandle< TrajectoryFitter > aTrajectoryFitter;
00503     string fitterName = confSetup.getUntrackedParameter< string >( "Fitter", "KFFittingSmoother" );
00504     setup.get< TrackingComponentsRecord >().get( fitterName, aTrajectoryFitter );
00505 
00506     double outlierEstimateCut = 5.;
00507 
00508     const KFFittingSmoother* aFittingSmoother = dynamic_cast< const KFFittingSmoother* >( aTrajectoryFitter.product() );
00509     if ( aFittingSmoother )
00510     {
00511       KFTrajectoryFitter* fitter = 0;
00512       KFTrajectorySmoother* smoother = 0;
00513       CurrentAlignmentKFUpdator* updator = new CurrentAlignmentKFUpdator( theNavigator );
00514 
00515       KFTrajectoryFitter* externalFitter = 0;
00516       KFTrajectorySmoother* externalSmoother = 0;
00517 
00518       PropagationDirection fitterDir = getDirection( strPropDir );
00519       PropagationDirection externalFitterDir = getDirection( strExternalPropDir );
00520 
00521       PropagationDirection smootherDir = oppositeDirection( fitterDir );
00522       PropagationDirection externalSmootherDir = oppositeDirection( externalFitterDir );
00523 
00524       const KFTrajectoryFitter* aKFFitter = dynamic_cast< const KFTrajectoryFitter* >( aFittingSmoother->fitter() );
00525       if ( aKFFitter )
00526       {
00527         PropagatorWithMaterial propagator( fitterDir, 0.106, aKFFitter->propagator()->magneticField() );
00528         Chi2MeasurementEstimator estimator( 30. );
00529         fitter = new KFTrajectoryFitter( &propagator, updator, &estimator );
00530 
00531         AnalyticalPropagator externalPropagator( aKFFitter->propagator()->magneticField(), externalFitterDir );
00532         Chi2MeasurementEstimator externalEstimator( 1000. );
00533         externalFitter = new KFTrajectoryFitter( &externalPropagator, aKFFitter->updator(), &externalEstimator );
00534       }
00535 
00536       const KFTrajectorySmoother* aKFSmoother = dynamic_cast< const KFTrajectorySmoother* >( aFittingSmoother->smoother() );
00537       if ( aKFSmoother )
00538       {
00539 
00540         PropagatorWithMaterial propagator( smootherDir, 0.106, aKFSmoother->propagator()->magneticField() );
00541         Chi2MeasurementEstimator estimator( 30. );
00542         smoother = new KFTrajectorySmoother( &propagator, updator, &estimator );
00543 
00544         AnalyticalPropagator externalPropagator( aKFSmoother->propagator()->magneticField(), externalSmootherDir );
00545         Chi2MeasurementEstimator externalEstimator( 1000. );
00546         externalSmoother = new KFTrajectorySmoother( &externalPropagator, aKFSmoother->updator(), &externalEstimator );
00547       }
00548 
00549       if ( fitter && smoother )
00550       {
00551         KFFittingSmoother* fittingSmoother = new KFFittingSmoother( *fitter, *smoother, outlierEstimateCut );
00552         KFFittingSmoother* externalFittingSmoother = new KFFittingSmoother( *externalFitter, *externalSmoother );
00555 
00556         string identifier;
00557         edm::ParameterSet config;
00558 
00559         config = confSetup.getParameter< edm::ParameterSet >( "TrajectoryFactory" );
00560         identifier = config.getParameter< string >( "TrajectoryFactoryName" );
00561         cout << "TrajectoryFactoryPlugin::get() ... " << identifier << endl;
00562         TrajectoryFactoryBase* trajectoryFactory = TrajectoryFactoryPlugin::get()->create( identifier, config );
00563 
00564         config = confSetup.getParameter< edm::ParameterSet >( "AlignmentUpdator" );
00565         identifier = config.getParameter< string >( "AlignmentUpdatorName" );
00566         KalmanAlignmentUpdator* alignmentUpdator = KalmanAlignmentUpdatorPlugin::get()->create( identifier, config );
00567 
00568         config = confSetup.getParameter< edm::ParameterSet >( "MetricsUpdator" );
00569         identifier = config.getParameter< string >( "MetricsUpdatorName" );
00570         KalmanAlignmentMetricsUpdator* metricsUpdator = KalmanAlignmentMetricsUpdatorPlugin::get()->create( identifier, config );
00571 
00572         KalmanAlignmentSetup::SortingDirection sortingDir = getSortingDirection( strSortingDir );
00573         KalmanAlignmentSetup::SortingDirection externalSortingDir = getSortingDirection( strExternalSortingDir );
00574 
00575         AlignmentSetup* anAlignmentSetup
00576           = new AlignmentSetup( *itSel,
00577                                 fittingSmoother, fitter->propagator(), trackingIDs, minTrackingHits, sortingDir,
00578                                 externalFittingSmoother, externalFitter->propagator(), externalIDs, minExternalHits, externalSortingDir,
00579                                 trajectoryFactory, alignmentUpdator, metricsUpdator );
00580 
00581         theAlignmentSetups.push_back( anAlignmentSetup );
00582 
00583         delete fittingSmoother;
00584         delete fitter;
00585         delete smoother;
00586 
00587         delete externalFittingSmoother;
00588         delete externalFitter;
00589         delete externalSmoother;
00590       }
00591       else
00592       {
00593         throw cms::Exception( "BadConfig" ) << "[KalmanAlignmentAlgorithm::initializeAlignmentSetups] "
00594                                             << "Instance of class KFFittingSmoother has no KFTrajectoryFitter/KFTrajectorySmoother.";
00595       }
00596 
00597       delete updator;
00598     }
00599     else
00600     {
00601       throw cms::Exception( "BadConfig" ) << "[KalmanAlignmentAlgorithm::initializeAlignmentSetups] "
00602                                           << "No instance of class KFFittingSmoother registered to the TrackingComponentsRecord.";
00603     }
00604   }
00605 
00606   cout << "[KalmanAlignmentAlgorithm::initializeAlignmentSetups] I'm using " << theAlignmentSetups.size() << " AlignmentSetup(s)." << endl;
00607 
00608 }
00609 
00610 
00611 const KalmanAlignmentSetup::SortingDirection
00612 KalmanAlignmentAlgorithm::getSortingDirection( const std::string& sortDir ) const
00613 {
00614   if ( sortDir == "SortInsideOut" ) { return KalmanAlignmentSetup::sortInsideOut; }
00615   if ( sortDir == "SortOutsideIn" ) { return KalmanAlignmentSetup::sortOutsideIn; }
00616   if ( sortDir == "SortUpsideDown" ) { return KalmanAlignmentSetup::sortUpsideDown; }
00617   if ( sortDir == "SortDownsideUp" ) { return KalmanAlignmentSetup::sortDownsideUp; }
00618 
00619   throw cms::Exception( "BadConfig" ) << "[KalmanAlignmentAlgorithm::getSortingDirection] "
00620                                       << "Unknown sorting direction: " << sortDir << std::endl;
00621 }
00622 
00623 
00624 DEFINE_EDM_PLUGIN( AlignmentAlgorithmPluginFactory, KalmanAlignmentAlgorithm, "KalmanAlignmentAlgorithm");

Generated on Tue Jun 9 17:24:03 2009 for CMSSW by  doxygen 1.5.4