40 #include "CLHEP/Random/RandGauss.h"
48 theConfiguration( config )
90 cout <<
"[KalmanAlignmentAlgorithm::terminate] start ..." << endl;
92 set< Alignable* > allAlignables;
93 vector< Alignable* > alignablesToWrite;
95 AlignmentSetupCollection::const_iterator itSetup;
98 delete (*itSetup)->alignmentUpdator();
100 const vector< Alignable* >& alignablesFromMetrics = (*itSetup)->metricsUpdator()->alignables();
101 cout <<
"[KalmanAlignmentAlgorithm::terminate] The metrics updator for setup \'" << (*itSetup)->id()
102 <<
"\' holds " << alignablesFromMetrics.size() <<
" alignables" << endl;
103 allAlignables.insert( alignablesFromMetrics.begin(), alignablesFromMetrics.end() );
106 for ( set< Alignable* >::iterator it = allAlignables.begin(); it != allAlignables.end(); ++it )
110 if ( alignmentParameters != 0 )
117 userVariables->
update(
true );
119 alignablesToWrite.push_back( *it );
130 cout <<
"Write data for " << alignablesToWrite.size() <<
" alignables ..." << endl;
141 cout <<
"The usage of TimingReport from Utilities/Timing! Timing not written to log file" << endl;
145 cout <<
"[KalmanAlignmentAlgorithm::terminate] ... done." << endl;
154 if ( iEvent % 100 == 0 )
cout <<
"[KalmanAlignmentAlgorithm::run] Event Nr. " << iEvent << endl;
168 map< AlignmentSetup*, TrackletCollection > setupToTrackletMap;
169 TrackletCollection::iterator itTracklet;
170 for ( itTracklet = refittedTracklets.begin(); itTracklet != refittedTracklets.end(); ++itTracklet )
171 setupToTrackletMap[(*itTracklet)->alignmentSetup()].push_back( *itTracklet );
174 map< AlignmentSetup*, TrackletCollection >::iterator itMap;
175 for ( itMap = setupToTrackletMap.begin(); itMap != setupToTrackletMap.end(); ++itMap )
180 TrackletCollection::iterator itTracklet;
181 for ( itTracklet = itMap->second.begin(); itTracklet != itMap->second.end(); ++itTracklet )
183 tracklets.push_back( (*itTracklet)->trajTrackPair() );
184 external.push_back( (*itTracklet)->externalPrediction() );
189 itMap->first->trajectoryFactory()->trajectories( setup, tracklets, external, eventInfo.
beamSpot_ );
191 ReferenceTrajectoryCollection::iterator itTrajectories;
194 for ( itTrajectories = trajectories.begin(); itTrajectories != trajectories.end(); ++itTrajectories )
197 itMap->first->metricsUpdator(), aMagneticField.
product() );
226 bool addPositionError =
false;
229 CLHEP::HepRandom::createInstance();
230 CLHEP::HepRandom::setTheSeed( seed );
240 if ( applyRandomStartValues )
241 cout <<
"[KalmanAlignmentAlgorithm::initializeAlignmentParameters] ADDING RANDOM START VALUES!!!" << endl;
246 bool applyShifts = applyXShifts || applyYShifts || applyZShifts;
247 bool applyRots = applyXRots || applyYRots || applyZRots;
258 vector< string > initSelection = initConfig.
getParameter< vector<string> >(
"InitializationSelector" );
260 vector< string >::iterator itInitSel;
261 for ( itInitSel = initSelection.begin(); itInitSel != initSelection.end(); ++itInitSel )
278 randSig[0] = sigmaXShift; randSig[1] = sigmaYShift; randSig[2] = sigmaZShift;
279 randSig[3] = sigmaXRot; randSig[4] = sigmaYRot; randSig[5] = sigmaZRot;
288 const vector< char > dummyParamSelector( 6,
'0' );
289 const vector< string > alignableSelector = config.
getParameter< vector<string> >(
"AlignableSelection" );
291 vector< string >::const_iterator itAliSel;
292 for ( itAliSel = alignableSelector.begin(); itAliSel != alignableSelector.end(); ++itAliSel )
295 cout <<
"[" << *itInitSel <<
"] add selection: " << *itAliSel << endl;
298 vector< Alignable* >::iterator itAlignable;
299 vector< Alignable* > alignables;
301 for ( itAlignable = alignablesFromSelector.begin(); itAlignable != alignablesFromSelector.end(); ++itAlignable )
303 alignables.push_back( *itAlignable );
305 cout <<
"[" << *itInitSel <<
"] total number of selected alignables = " << alignables.size() << endl;
307 sort( alignables.begin(), alignables.end(), *this );
310 map< Alignable*, vector< AlignmentParameters* > > alignmentParametersMap;
317 if ( readParam || readCovar || applyParam || applyCovar )
326 cout <<
"[" << *itInitSel <<
"] read alignment parameters. file / iteration = " << file <<
" / " << iter << endl;
327 vector< AlignmentParameters* > alignmentParameters = alignmentIO.
readAlignmentParameters( alignables, file.c_str(), iter, ierr );
328 cout <<
"[" << *itInitSel <<
"] #param / ierr = " << alignmentParameters.size() <<
" / " << ierr << endl;
330 vector< AlignmentParameters* >::iterator itParam;
331 for ( itParam = alignmentParameters.begin(); itParam != alignmentParameters.end(); ++itParam )
332 alignmentParametersMap[(*itParam)->alignable()].push_back( *itParam );
340 for ( itAlignable = alignables.begin(); itAlignable != alignables.end(); itAlignable++ )
342 if ( (*itAlignable)->alignmentParameters() == 0 )
344 cout <<
"[KalmanAlignmentAlgorithm::initializeAlignmentParameters] "
345 <<
"alignable is not associated with alignment parameters --> skip" << endl;
351 cout <<
"[KalmanAlignmentAlgorithm::initializeAlignmentParameters] "
352 <<
"alignable is not associated with rigid body alignment parameters --> skip" << endl;
356 displacement[0] = applyXShifts ? sigmaXShift*CLHEP::RandGauss::shoot() : 0.;
357 displacement[1] = applyYShifts ? sigmaZShift*CLHEP::RandGauss::shoot() : 0.;
358 displacement[2] = applyZShifts ? sigmaYShift*CLHEP::RandGauss::shoot() : 0.;
364 ( *itAlignable )->move( globalShift );
369 eulerAngles[0] = applyXRots ? sigmaXRot*CLHEP::RandGauss::shoot() : 0.;
370 eulerAngles[1] = applyYRots ? sigmaYRot*CLHEP::RandGauss::shoot() : 0.;
371 eulerAngles[2] = applyZRots ? sigmaZRot*CLHEP::RandGauss::shoot() : 0.;
376 ( *itAlignable )->rotateInLocalFrame( localRotation );
381 double radius = ( *itAlignable )->globalPosition().perp();
382 ( *itAlignable )->rotateAroundGlobalZ( curlConst*radius );
385 if ( addPositionError )
388 GlobalVector globalError = (*itAlignable)->surface().toGlobal( localError );
391 ( *itAlignable )->setAlignmentPositionError( ape,
true );
402 if ( (*itAlignable)->alignmentParameters() != 0 )
405 if ( readParam && readCovar )
407 if ( alignmentParametersMap.find( *itAlignable ) == alignmentParametersMap.end() )
410 alignmentParameters = (*itAlignable)->alignmentParameters()->
clone( startParameters, startError );
416 alignmentParameters = alignmentParametersMap[*itAlignable].back();
418 userVariables->
update( alignmentParameters );
422 else if ( readParam )
424 if ( alignmentParametersMap.find( *itAlignable ) == alignmentParametersMap.end() )
426 alignmentParameters = (*itAlignable)->alignmentParameters()->
clone( startParameters, startError );
432 alignmentParameters = (*itAlignable)->alignmentParameters()->
clone( parameters, startError );
434 userVariables->
update( alignmentParameters );
441 alignmentParameters = (*itAlignable)->alignmentParameters()->
clone( startParameters, startError );
446 (*itAlignable)->setAlignmentParameters( alignmentParameters );
449 if ( applyRandomStartValues )
451 cout <<
"applying random start values" << endl;
456 for (
int iParam = 0; iParam < randomStartParameters.num_row(); ++iParam )
458 randomStartParameters[iParam] +=
sqrt(randSig[iParam])*CLHEP::RandGauss::shoot();
464 alignmentParameters = (*itAlignable)->alignmentParameters()->
clone( randomStartParameters, randomStartErrors );
465 (*itAlignable)->setAlignmentParameters( alignmentParameters );
468 (*itAlignable)->alignmentParameters()->
setValid(
true );
472 if ( ( applyParam || applyCovar ) && alignmentParametersMap.find( *itAlignable ) != alignmentParametersMap.end() )
476 vector< AlignmentParameters* > allAlignmentParameters = alignmentParametersMap[*itAlignable];
477 vector< AlignmentParameters* >::iterator itParam;
479 for ( itParam = allAlignmentParameters.begin(); itParam != allAlignmentParameters.end(); ++itParam )
482 if ( ( *itAlignable )->alignmentParameters() )
491 cout <<
"[" << *itInitSel <<
"] Set the alignment flag for " << iAlign <<
" alignables." << endl;
492 cout <<
"[" << *itInitSel <<
"] number of applied parameters: " << iApply << endl;
505 const vector<string> selSetup = initConfig.
getParameter< vector<string> >(
"Setups" );
507 for ( vector<string>::const_iterator itSel = selSetup.begin(); itSel != selSetup.end(); ++itSel )
509 cout <<
"[KalmanAlignmentAlgorithm::initializeAlignmentSetups] Add AlignmentSetup: " << *itSel << endl;
513 string strPropDir = confSetup.
getUntrackedParameter<
string >(
"PropagationDirection",
"alongMomentum" );
514 string strSortingDir = confSetup.
getUntrackedParameter<
string >(
"SortingDirection",
"SortInsideOut" );
515 vector<int> trackingIDs = confSetup.
getParameter< vector<int> >(
"Tracking" );
516 unsigned int minTrackingHits = confSetup.
getUntrackedParameter<
unsigned int >(
"MinTrackingHits", 0 );
518 string strExternalPropDir = confSetup.
getUntrackedParameter<
string >(
"ExternalPropagationDirection",
"alongMomentum" );
519 string strExternalSortingDir = confSetup.
getUntrackedParameter<
string >(
"ExternalSortingDirection",
"SortInsideOut" );
520 vector<int> externalIDs = confSetup.
getParameter< vector<int> >(
"External" );
521 unsigned int minExternalHits = confSetup.
getUntrackedParameter<
unsigned int >(
"MinExternalHits", 0 );
527 double outlierEstimateCut = 5.;
530 if ( aFittingSmoother )
553 AnalyticalPropagator externalPropagator( aKFFitter->propagator()->magneticField(), externalFitterDir );
555 externalFitter =
new KFTrajectoryFitter( &externalPropagator, aKFFitter->updator(), &externalEstimator );
567 AnalyticalPropagator externalPropagator( aKFSmoother->alongPropagator()->magneticField(), externalSmootherDir );
569 externalSmoother =
new KFTrajectorySmoother( &externalPropagator, aKFSmoother->updator(), &externalEstimator );
572 if ( fitter && smoother )
583 identifier = config.
getParameter<
string >(
"TrajectoryFactoryName" );
584 cout <<
"TrajectoryFactoryPlugin::get() ... " << identifier << endl;
588 identifier = config.
getParameter<
string >(
"AlignmentUpdatorName" );
592 identifier = config.
getParameter<
string >(
"MetricsUpdatorName" );
600 fittingSmoother, fitter->propagator(), trackingIDs, minTrackingHits, sortingDir,
601 externalFittingSmoother, externalFitter->propagator(), externalIDs, minExternalHits, externalSortingDir,
602 trajectoryFactory, alignmentUpdator, metricsUpdator );
606 delete fittingSmoother;
610 delete externalFittingSmoother;
611 delete externalFitter;
612 delete externalSmoother;
616 throw cms::Exception(
"BadConfig" ) <<
"[KalmanAlignmentAlgorithm::initializeAlignmentSetups] "
617 <<
"Instance of class KFFittingSmoother has no KFTrajectoryFitter/KFTrajectorySmoother.";
624 throw cms::Exception(
"BadConfig" ) <<
"[KalmanAlignmentAlgorithm::initializeAlignmentSetups] "
625 <<
"No instance of class KFFittingSmoother registered to the TrackingComponentsRecord.";
629 cout <<
"[KalmanAlignmentAlgorithm::initializeAlignmentSetups] I'm using " <<
theAlignmentSetups.size() <<
" AlignmentSetup(s)." << endl;
642 throw cms::Exception(
"BadConfig" ) <<
"[KalmanAlignmentAlgorithm::getSortingDirection] "
643 <<
"Unknown sorting direction: " << sortDir << std::endl;
649 bool applyPar,
bool applyCov )
const
654 throw cms::Exception(
"BadConfig" ) <<
"applyParameters: provided alignable does not have rigid body alignment parameters";
679 vector<Alignable*>& comps )
const
681 comps.push_back( ali );
682 vector<Alignable*> nextComps = ali->
components();
683 vector<Alignable*>::iterator it;
684 for ( it = nextComps.begin(); it != nextComps.end(); ++it )
getComponents( *it, comps );
690 std::cout <<
"[KalmanAlignmentAlgorithm::mergeResults] START MERGING RESULTS" << std::endl;
694 vector<string> inFileNames = mergeConf.
getParameter< vector<string> >(
"InputMergeFileNames" );
695 string outFileName = mergeConf.
getParameter<
string>(
"OutputMergeFileName" );
697 bool applyPar = mergeConf.
getParameter<
bool>(
"ApplyParameters" );
698 bool applyCov = mergeConf.
getParameter<
bool>(
"ApplyErrors" );
700 map< Alignable*, vector< AlignmentParameters* > > alignmentParametersMap;
702 vector< Alignable* > allAlignables;
705 cout <<
"allAlignables.size() = " << allAlignables.size() << endl;
709 for ( vector<string>::iterator itFile = inFileNames.begin(); itFile != inFileNames.end(); ++itFile )
716 cout <<
"Read alignment parameters. file / iteration = " << *itFile <<
" / " << iter << endl;
718 vector< AlignmentParameters* > alignmentParameters =
721 cout <<
"#param / ierr = " << alignmentParameters.size() <<
" / " << ierr << endl;
723 vector< AlignmentParameters* >::iterator itParam;
724 for ( itParam = alignmentParameters.begin(); itParam != alignmentParameters.end(); ++itParam )
725 alignmentParametersMap[(*itParam)->alignable()].push_back( *itParam );
731 vector< Alignable* > alignablesToWrite;
732 alignablesToWrite.reserve( alignmentParametersMap.size() );
734 map< Alignable*, vector< AlignmentParameters* > >::iterator itMap;
735 for ( itMap = alignmentParametersMap.begin(); itMap != alignmentParametersMap.end(); ++itMap )
743 vector< AlignmentParameters* >& vecParam = itMap->second;
744 vector< AlignmentParameters* >::iterator itParam;
745 for ( itParam = vecParam.begin(); itParam != vecParam.end(); ++itParam, ++nMerge )
746 mergedParam += (*itParam)->parameters();
748 mergedParam /= nMerge;
752 itMap->first->setAlignmentParameters( mergedAliParam );
754 alignablesToWrite.push_back( itMap->first );
759 cout <<
"alignablesToWrite.size() = " << alignablesToWrite.size() << endl;
767 std::cout <<
"[KalmanAlignmentAlgorithm::mergeResults] DONE" << std::endl;
void initializeAlignmentSetups(const edm::EventSetup &setup)
virtual char const * what() const
T getParameter(std::string const &) const
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)
T getUntrackedParameter(std::string const &, T const &) const
TrackletCollection refitTracks(const edm::EventSetup &eventSetup, const AlignmentSetupCollection &algoSetups, const ConstTrajTrackPairCollection &tracks, const reco::BeamSpot *beamSpot)
void histogramParameters(std::string histoNamePrefix)
Histogram current estimate of the alignment parameters wrt. the true values.
void setAlignmentFlag(bool flag)
virtual void rotateInLocalFrame(const RotationType &rotation)
Rotation intepreted in the local reference frame.
void mergeResults(void) const
void getComponents(Alignable *ali, std::vector< Alignable * > &comps) const
static void fillHistogram(std::string histo_name, float data)
KalmanAlignmentAlgorithm(const edm::ParameterSet &config)
virtual ~KalmanAlignmentAlgorithm(void)
virtual void move(const GlobalVector &displacement)=0
Movement with respect to the global reference frame.
const ConstTrajTrackPairCollection & trajTrackPairs_
AlignableTracker * theTracker
virtual Alignables components() const =0
Return vector of all direct components.
virtual void setAlignmentPositionError(const AlignmentPositionError &ape, bool propagateDown)
void initializeAlignmentParameters(const edm::EventSetup &setup)
void update(bool enforceUpdate=false)
Call this function in case the associated Alignable was updated by the alignment algorithm.
AlignmentParameterStore * theParameterStore
const AlgebraicVector & parameters(void) const
Get alignment parameters.
AlignmentUserVariables * userVariables(void) const
Get pointer to user variables.
void clear()
remove all selected Alignables and geometrical restrictions
const PropagationDirection getDirection(const std::string &dir) const
void setValid(bool v)
Set validity flag.
void applyAlignmentParameters(Alignable *ali, AlignmentParameters *par, bool applyPar, bool applyCov) const
std::vector< TrackletPtr > TrackletCollection
virtual void run(const edm::EventSetup &setup, const EventInfo &eventInfo)
Run the algorithm (must be implemented in derived class)
virtual void setAlignmentPositionError(const AlignmentPositionError &ape, bool propagateDown)=0
Set the alignment position error - if (!propagateDown) do not affect daughters.
const AlignableSurface & surface() const
Return the Surface (global position and orientation) of the object.
const align::Alignables & selectedAlignables() const
vector of alignables selected so far
const reco::BeamSpot & beamSpot_
CLHEP::HepVector AlgebraicVector
AlignmentParameterSelector * theSelector
AlgebraicVector EulerAngles
void setUserVariables(AlignmentUserVariables *auv)
Set pointer to user variables.
KalmanAlignmentSetup AlignmentSetup
void writeAlignmentParameters(const align::Alignables &alivec, const char *filename, int iter, bool validCheck, int &ierr)
write AlignmentParameters
const KalmanAlignmentSetup::SortingDirection getSortingDirection(const std::string &sortDir) const
virtual AlignmentParameters * clone(const AlgebraicVector &par, const AlgebraicSymMatrix &cov) const =0
Enforce clone methods in derived classes.
T const * product() const
virtual void terminate(const edm::EventSetup &setup)
Call at end of each loop (must be implemented in derived class)
edm::ParameterSet theConfiguration
AlignmentSetupCollection theAlignmentSetups
KalmanAlignmentTrackRefitter * theRefitter
AlgebraicVector translation(void) const
Get translation parameters.
unsigned int addSelection(const std::string &name, const std::vector< char > ¶mSel)
int numberOfUpdates(void) const
Return the number of updates.
AlignableNavigator * theNavigator
align::GlobalPoints toGlobal(const align::LocalPoints &) const
Return in global coord given a set of local points.
RotationType toMatrix(const EulerAngles &)
Convert rotation angles about x-, y-, z-axes to matrix.
CLHEP::HepSymMatrix AlgebraicSymMatrix
static unsigned int const shift
tuple KFTrajectorySmoother
static void configure(const edm::ParameterSet &config)
#define DEFINE_EDM_PLUGIN(factory, type, name)
virtual Alignables components() const
Return vector of direct components.
const PropagationDirection oppositeDirection(const PropagationDirection dir) const
volatile std::atomic< bool > shutdown_flag false
align::Parameters readAlignmentParameters(const align::Alignables &alivec, const char *filename, int iter, int &ierr)
read AlignmentParameters
const AlgebraicSymMatrix & covariance(void) const
Get parameter covariance matrix.
AlgebraicVector rotation(void) const
Get rotation parameters.
Constructor of the full muon geometry.
TrajectoryFactoryBase::ReferenceTrajectoryCollection ReferenceTrajectoryCollection
TrajectoryFactoryBase::ExternalPredictionCollection ExternalPredictionCollection
void setup(std::vector< TH2F > &depth, std::string name, std::string units="")
tuple AnalyticalPropagator
T get(const Candidate &c)
std::vector< ConstTrajTrackPair > ConstTrajTrackPairCollection
define event information passed to algorithms