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() );
221 bool addPositionError =
false;
224 CLHEP::HepRandom::createInstance();
225 CLHEP::HepRandom::setTheSeed( seed );
235 if ( applyRandomStartValues )
236 cout <<
"[KalmanAlignmentAlgorithm::initializeAlignmentParameters] ADDING RANDOM START VALUES!!!" << endl;
241 bool applyShifts = applyXShifts || applyYShifts || applyZShifts;
242 bool applyRots = applyXRots || applyYRots || applyZRots;
253 vector< string > initSelection = initConfig.
getParameter< vector<string> >(
"InitializationSelector" );
255 vector< string >::iterator itInitSel;
256 for ( itInitSel = initSelection.begin(); itInitSel != initSelection.end(); ++itInitSel )
273 randSig[0] = sigmaXShift; randSig[1] = sigmaYShift; randSig[2] = sigmaZShift;
274 randSig[3] = sigmaXRot; randSig[4] = sigmaYRot; randSig[5] = sigmaZRot;
283 const vector< char > dummyParamSelector( 6,
'0' );
284 const vector< string > alignableSelector = config.
getParameter< vector<string> >(
"AlignableSelection" );
286 vector< string >::const_iterator itAliSel;
287 for ( itAliSel = alignableSelector.begin(); itAliSel != alignableSelector.end(); ++itAliSel )
290 cout <<
"[" << *itInitSel <<
"] add selection: " << *itAliSel << endl;
293 vector< Alignable* >::iterator itAlignable;
294 vector< Alignable* > alignables;
296 for ( itAlignable = alignablesFromSelector.begin(); itAlignable != alignablesFromSelector.end(); ++itAlignable )
298 alignables.push_back( *itAlignable );
300 cout <<
"[" << *itInitSel <<
"] total number of selected alignables = " << alignables.size() << endl;
302 sort( alignables.begin(), alignables.end(), *this );
305 map< Alignable*, vector< AlignmentParameters* > > alignmentParametersMap;
312 if ( readParam || readCovar || applyParam || applyCovar )
321 cout <<
"[" << *itInitSel <<
"] read alignment parameters. file / iteration = " << file <<
" / " << iter << endl;
322 vector< AlignmentParameters* > alignmentParameters = alignmentIO.
readAlignmentParameters( alignables, file.c_str(), iter, ierr );
323 cout <<
"[" << *itInitSel <<
"] #param / ierr = " << alignmentParameters.size() <<
" / " << ierr << endl;
325 vector< AlignmentParameters* >::iterator itParam;
326 for ( itParam = alignmentParameters.begin(); itParam != alignmentParameters.end(); ++itParam )
327 alignmentParametersMap[(*itParam)->alignable()].push_back( *itParam );
335 for ( itAlignable = alignables.begin(); itAlignable != alignables.end(); itAlignable++ )
337 if ( (*itAlignable)->alignmentParameters() == 0 )
339 cout <<
"[KalmanAlignmentAlgorithm::initializeAlignmentParameters] "
340 <<
"alignable is not associated with alignment parameters --> skip" << endl;
346 cout <<
"[KalmanAlignmentAlgorithm::initializeAlignmentParameters] "
347 <<
"alignable is not associated with rigid body alignment parameters --> skip" << endl;
351 displacement[0] = applyXShifts ? sigmaXShift*CLHEP::RandGauss::shoot() : 0.;
352 displacement[1] = applyYShifts ? sigmaZShift*CLHEP::RandGauss::shoot() : 0.;
353 displacement[2] = applyZShifts ? sigmaYShift*CLHEP::RandGauss::shoot() : 0.;
359 ( *itAlignable )->move( globalShift );
364 eulerAngles[0] = applyXRots ? sigmaXRot*CLHEP::RandGauss::shoot() : 0.;
365 eulerAngles[1] = applyYRots ? sigmaYRot*CLHEP::RandGauss::shoot() : 0.;
366 eulerAngles[2] = applyZRots ? sigmaZRot*CLHEP::RandGauss::shoot() : 0.;
371 ( *itAlignable )->rotateInLocalFrame( localRotation );
376 double radius = ( *itAlignable )->globalPosition().perp();
377 ( *itAlignable )->rotateAroundGlobalZ( curlConst*radius );
380 if ( addPositionError )
383 GlobalVector globalError = (*itAlignable)->surface().toGlobal( localError );
386 ( *itAlignable )->setAlignmentPositionError( ape,
true );
397 if ( (*itAlignable)->alignmentParameters() != 0 )
400 if ( readParam && readCovar )
402 if ( alignmentParametersMap.find( *itAlignable ) == alignmentParametersMap.end() )
405 alignmentParameters = (*itAlignable)->alignmentParameters()->
clone( startParameters, startError );
411 alignmentParameters = alignmentParametersMap[*itAlignable].back();
413 userVariables->
update( alignmentParameters );
417 else if ( readParam )
419 if ( alignmentParametersMap.find( *itAlignable ) == alignmentParametersMap.end() )
421 alignmentParameters = (*itAlignable)->alignmentParameters()->
clone( startParameters, startError );
427 alignmentParameters = (*itAlignable)->alignmentParameters()->
clone( parameters, startError );
429 userVariables->
update( alignmentParameters );
436 alignmentParameters = (*itAlignable)->alignmentParameters()->
clone( startParameters, startError );
441 (*itAlignable)->setAlignmentParameters( alignmentParameters );
444 if ( applyRandomStartValues )
446 cout <<
"applying random start values" << endl;
451 for (
int iParam = 0; iParam < randomStartParameters.num_row(); ++iParam )
453 randomStartParameters[iParam] +=
sqrt(randSig[iParam])*CLHEP::RandGauss::shoot();
459 alignmentParameters = (*itAlignable)->alignmentParameters()->
clone( randomStartParameters, randomStartErrors );
460 (*itAlignable)->setAlignmentParameters( alignmentParameters );
463 (*itAlignable)->alignmentParameters()->
setValid(
true );
467 if ( ( applyParam || applyCovar ) && alignmentParametersMap.find( *itAlignable ) != alignmentParametersMap.end() )
471 vector< AlignmentParameters* > allAlignmentParameters = alignmentParametersMap[*itAlignable];
472 vector< AlignmentParameters* >::iterator itParam;
474 for ( itParam = allAlignmentParameters.begin(); itParam != allAlignmentParameters.end(); ++itParam )
477 if ( ( *itAlignable )->alignmentParameters() )
486 cout <<
"[" << *itInitSel <<
"] Set the alignment flag for " << iAlign <<
" alignables." << endl;
487 cout <<
"[" << *itInitSel <<
"] number of applied parameters: " << iApply << endl;
500 const vector<string> selSetup = initConfig.
getParameter< vector<string> >(
"Setups" );
502 for ( vector<string>::const_iterator itSel = selSetup.begin(); itSel != selSetup.end(); ++itSel )
504 cout <<
"[KalmanAlignmentAlgorithm::initializeAlignmentSetups] Add AlignmentSetup: " << *itSel << endl;
508 string strPropDir = confSetup.
getUntrackedParameter<
string >(
"PropagationDirection",
"alongMomentum" );
509 string strSortingDir = confSetup.
getUntrackedParameter<
string >(
"SortingDirection",
"SortInsideOut" );
510 vector<int> trackingIDs = confSetup.
getParameter< vector<int> >(
"Tracking" );
511 unsigned int minTrackingHits = confSetup.
getUntrackedParameter<
unsigned int >(
"MinTrackingHits", 0 );
513 string strExternalPropDir = confSetup.
getUntrackedParameter<
string >(
"ExternalPropagationDirection",
"alongMomentum" );
514 string strExternalSortingDir = confSetup.
getUntrackedParameter<
string >(
"ExternalSortingDirection",
"SortInsideOut" );
515 vector<int> externalIDs = confSetup.
getParameter< vector<int> >(
"External" );
516 unsigned int minExternalHits = confSetup.
getUntrackedParameter<
unsigned int >(
"MinExternalHits", 0 );
522 double outlierEstimateCut = 5.;
525 if ( aFittingSmoother )
567 if ( fitter && smoother )
578 identifier = config.
getParameter<
string >(
"TrajectoryFactoryName" );
579 cout <<
"TrajectoryFactoryPlugin::get() ... " << identifier << endl;
583 identifier = config.
getParameter<
string >(
"AlignmentUpdatorName" );
587 identifier = config.
getParameter<
string >(
"MetricsUpdatorName" );
595 fittingSmoother, fitter->
propagator(), trackingIDs, minTrackingHits, sortingDir,
596 externalFittingSmoother, externalFitter->
propagator(), externalIDs, minExternalHits, externalSortingDir,
597 trajectoryFactory, alignmentUpdator, metricsUpdator );
601 delete fittingSmoother;
605 delete externalFittingSmoother;
606 delete externalFitter;
607 delete externalSmoother;
611 throw cms::Exception(
"BadConfig" ) <<
"[KalmanAlignmentAlgorithm::initializeAlignmentSetups] "
612 <<
"Instance of class KFFittingSmoother has no KFTrajectoryFitter/KFTrajectorySmoother.";
619 throw cms::Exception(
"BadConfig" ) <<
"[KalmanAlignmentAlgorithm::initializeAlignmentSetups] "
620 <<
"No instance of class KFFittingSmoother registered to the TrackingComponentsRecord.";
624 cout <<
"[KalmanAlignmentAlgorithm::initializeAlignmentSetups] I'm using " <<
theAlignmentSetups.size() <<
" AlignmentSetup(s)." << endl;
637 throw cms::Exception(
"BadConfig" ) <<
"[KalmanAlignmentAlgorithm::getSortingDirection] "
638 <<
"Unknown sorting direction: " << sortDir << std::endl;
644 bool applyPar,
bool applyCov )
const
649 throw cms::Exception(
"BadConfig" ) <<
"applyParameters: provided alignable does not have rigid body alignment parameters";
674 vector<Alignable*>& comps )
const
676 comps.push_back( ali );
677 vector<Alignable*> nextComps = ali->
components();
678 vector<Alignable*>::iterator it;
679 for ( it = nextComps.begin(); it != nextComps.end(); ++it )
getComponents( *it, comps );
685 std::cout <<
"[KalmanAlignmentAlgorithm::mergeResults] START MERGING RESULTS" << std::endl;
689 vector<string> inFileNames = mergeConf.
getParameter< vector<string> >(
"InputMergeFileNames" );
690 string outFileName = mergeConf.
getParameter<
string>(
"OutputMergeFileName" );
692 bool applyPar = mergeConf.
getParameter<
bool>(
"ApplyParameters" );
693 bool applyCov = mergeConf.
getParameter<
bool>(
"ApplyErrors" );
695 map< Alignable*, vector< AlignmentParameters* > > alignmentParametersMap;
697 vector< Alignable* > allAlignables;
700 cout <<
"allAlignables.size() = " << allAlignables.size() << endl;
704 for ( vector<string>::iterator itFile = inFileNames.begin(); itFile != inFileNames.end(); ++itFile )
711 cout <<
"Read alignment parameters. file / iteration = " << *itFile <<
" / " << iter << endl;
713 vector< AlignmentParameters* > alignmentParameters =
716 cout <<
"#param / ierr = " << alignmentParameters.size() <<
" / " << ierr << endl;
718 vector< AlignmentParameters* >::iterator itParam;
719 for ( itParam = alignmentParameters.begin(); itParam != alignmentParameters.end(); ++itParam )
720 alignmentParametersMap[(*itParam)->alignable()].push_back( *itParam );
726 vector< Alignable* > alignablesToWrite;
727 alignablesToWrite.reserve( alignmentParametersMap.size() );
729 map< Alignable*, vector< AlignmentParameters* > >::iterator itMap;
730 for ( itMap = alignmentParametersMap.begin(); itMap != alignmentParametersMap.end(); ++itMap )
738 vector< AlignmentParameters* >& vecParam = itMap->second;
739 vector< AlignmentParameters* >::iterator itParam;
740 for ( itParam = vecParam.begin(); itParam != vecParam.end(); ++itParam, ++nMerge )
741 mergedParam += (*itParam)->parameters();
743 mergedParam /= nMerge;
747 itMap->first->setAlignmentParameters( mergedAliParam );
749 alignablesToWrite.push_back( itMap->first );
754 cout <<
"alignablesToWrite.size() = " << alignablesToWrite.size() << endl;
762 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)
const Propagator * propagator() const
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)
const TrajectoryStateUpdator * updator() const
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.
const TrajectorySmoother * smoother() const
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.
const TrajectoryStateUpdator * updator() const
T const * product() const
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)
virtual const MagneticField * magneticField() const =0
const Propagator * propagator() const
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
const TrajectoryFitter * fitter() const
static void configure(const edm::ParameterSet &config)
#define DEFINE_EDM_PLUGIN(factory, type, name)
virtual Alignables components() const
Return vector of direct components.
virtual void terminate(void)
Call at end of job (must be implemented in derived class)
const PropagationDirection oppositeDirection(const PropagationDirection dir) const
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="")
T get(const Candidate &c)
std::vector< ConstTrajTrackPair > ConstTrajTrackPairCollection
define event information passed to algorithms