61 : myParameterStore(store),
65 myDirectory(myConfig.getUntrackedParameter<
std::string>(
"fileDir")),
66 myNoSteerFiles(noSteerFiles),
67 myIsSteerFileDebug(myConfig.getUntrackedParameter<
bool>(
"steerFileDebug")),
68 myParameterSign(myConfig.getUntrackedParameter<
int>(
"parameterSign")),
69 theMinHieraConstrCoeff(myConfig.getParameter<
double>(
"minHieraConstrCoeff")),
70 theMinHieraParPerConstr(myConfig.getParameter<
unsigned int>(
"minHieraParPerConstr")),
71 theConstrPrecision(myConfig.getParameter<
unsigned int>(
"constrPrecision")),
72 theCoordMaster(
nullptr) {
73 if (myParameterSign != 1 && myParameterSign != -1) {
74 cms::Exception(
"BadConfig") <<
"Expect PedeSteerer.parameterSign = +/-1, "
75 <<
"found " << myParameterSign <<
".";
79 if (myDirectory.empty())
80 myDirectory = defaultDir;
81 if (!myDirectory.empty() && myDirectory.find_last_of(
'/') != myDirectory.size() - 1) {
85 const auto &alis = myParameterStore->alignables();
86 if (!this->checkParameterChoices(alis)) {
90 theCoordDefiners = this->selectCoordinateAlis(alis);
91 if (!theCoordDefiners.empty()) {
107 }
else if (aliMuon) {
111 <<
"Cannot define global coordinate system "
112 <<
"with neither tracker nor muon!";
115 theCoordMaster->addComponent(aliMuon);
118 for (
const auto &it : allExtras)
119 theCoordMaster->addComponent(it);
122 theCoordMaster->recenterSurface();
124 if (this->isCorrectToRefSystem(theCoordDefiners)) {
125 this->correctToReferenceSystem();
164 for (
const auto &iAli : alis) {
170 unsigned int numNoHieraPar = 0;
171 unsigned int numHieraPar = 0;
172 for (
unsigned int iParam = 0; static_cast<int>(iParam) <
params->size(); ++iParam) {
174 if (selector ==
'C' || selector ==
'F' || selector ==
'H') {
176 }
else if (selector ==
'c' || selector ==
'f' || selector ==
'1' || selector ==
'r' || selector ==
's') {
183 <<
"[PedeSteerer::buildNoHierarchyCollection] All active parameters of alignables to be "
184 <<
" taken out of the hierarchy must be marked with capital letters 'C', 'F' or 'H'!";
186 bool isInHiera =
false;
188 while ((mother = mother->mother())) {
189 if (mother->alignmentParameters())
196 edm::LogWarning(
"Alignment") <<
"@SUB=PedeSteerer::buildNoHierarchyCollection"
197 <<
"Alignable not in hierarchy, no need to remove it!";
206 for (
const auto &iAli : alis) {
211 for (
unsigned int iParam = 0; static_cast<int>(iParam) < paras->
size(); ++iParam) {
213 if (
sel !=
'f' &&
sel !=
'F' &&
sel !=
'c' &&
sel !=
'C' &&
sel !=
'0' &&
sel !=
'1' &&
sel !=
'H' &&
214 sel !=
'r' &&
sel !=
's') {
216 <<
"[PedeSteerer::unknownParameterChoices] "
217 <<
"Unexpected parameter selector '" <<
sel
218 <<
"', use \n'f/F' (fix),\n'c/C' (fix at correct pos.),\n'1/H' (free),\n"
219 <<
"'r/s' (free, but defining reference system, trying to correct misalignment if 's')"
220 <<
" or \n'0' (ignore).\n"
221 <<
"Capital letters mean that the Alignable is taken out of a possible hierarchy,\n"
222 <<
"but must be used consistently for all its parameters.";
235 std::pair<unsigned int, unsigned int> numFixNumFixCor(0, 0);
237 std::ofstream *filePtr =
nullptr;
239 for (
const auto &iAli : alis) {
245 for (
unsigned int iParam = 0; static_cast<int>(iParam) <
params->size(); ++iParam) {
247 for (
unsigned int iInstance = 0; iInstance < nInstances; ++iInstance) {
250 ++(numFixNumFixCor.first);
251 }
else if (whichFix == -1) {
252 ++(numFixNumFixCor.second);
260 return numFixNumFixCor;
265 unsigned int iInstance,
268 std::ofstream *&filePtr,
272 if (selector ==
'c' || selector ==
'C') {
275 <<
"PedeSteerer::fixParameter: correction (c/C) possible only for RigidBodyParameters";
279 }
else if (selector ==
'f' || selector ==
'F') {
286 (*filePtr) <<
"Parameter\n";
288 std::ofstream &
file = *filePtr;
307 for (
const auto &iAli : alis) {
312 unsigned int refParam = 0;
313 unsigned int nonRefParam = 0;
314 for (
unsigned int iParam = 0; static_cast<int>(iParam) <
params->size(); ++iParam) {
316 if (selector ==
'r' || selector ==
's') {
318 }
else if (selector !=
'0' && selector !=
'f') {
326 <<
"[PedeSteerer::selectCoordinateAlis] All active parameters of alignables defining "
327 <<
"the coordinate system must be marked with 'r/s' (or fixed, 'f')!";
330 while ((mother = mother->mother())) {
331 if (mother->alignmentParameters()) {
332 throw cms::Exception(
"BadConfig") <<
"[PedeSteerer::selectCoordinateAlis] "
333 <<
"Alignables defining the coordinate system must "
334 <<
"be highest level!";
337 coordAlis.push_back(iAli);
348 (*filePtr) <<
"* Constraints to define coordinate system:\n";
350 throw cms::Exception(
"BadConfig") <<
"[PedeSteerer::defineCoordinates] "
351 <<
"No master alignable or it has parameters!";
354 edm::LogError(
"Alignment") <<
"@SUB=PedeSteerer::defineCoordinates"
355 <<
"Ignore following LogicErrors from PedeLabeler.";
367 bool doCorrect =
false;
368 bool doNotCorrect =
false;
369 for (
const auto &it : coordDefiners) {
371 (it->alignmentParameters() ? dynamic_cast<SelectionUserVariables *>(it->alignmentParameters()->userVariables())
384 if (doCorrect && doNotCorrect) {
386 <<
"[PedeSteerer::doCorrectToRefSystem]: Parameter selection 's' and 'r' must not coexist!";
399 for (
const auto &it :
401 const auto &
comp = it->deepComponents();
402 definerDets.insert(definerDets.end(),
comp.begin(),
comp.end());
405 for (
unsigned int iLoop = 0;; ++iLoop) {
407 for (
const auto &it : definerDets) {
408 meanPars += RbPars(it,
true).globalParameters();
410 meanPars /= definerDets.size();
413 if (squareSum < 1.
e-20)
416 edm::LogInfo(
"Alignment") <<
"@SUB=PedeSteerer::correctToReferenceSystem"
417 <<
"Loop " << iLoop <<
" "
418 <<
"Mean misalignment of dets of defined coordinate system"
419 << (squareSum < 1.e-20 ?
":" :
" (will be iteratively corrected to < 1.e-10):")
423 edm::LogError(
"Alignment") <<
"@SUB=PedeSteerer::correctToReferenceSystem"
424 <<
"No convergence in " << iLoop <<
" iterations, "
425 <<
"remaining misalignment: " << meanPars;
432 globalAngles[0] = meanPars[RbPars::dalpha];
433 globalAngles[1] = meanPars[RbPars::dbeta];
434 globalAngles[2] = meanPars[RbPars::dgamma];
441 std::ofstream *filePtr =
nullptr;
443 unsigned int nConstraints = 0;
445 for (
const auto &iA : alis) {
447 if (!(iA->firstCompsWithParams(aliDaughts))) {
448 edm::LogWarning(
"Alignment") <<
"@SUB=PedeSteerer::hierarchyConstraints"
449 <<
"Some but not all daughters of "
454 if (aliDaughts.empty())
476 std::ofstream &
file)
const {
479 std::vector<std::vector<ParameterId> > paramIdsVec;
480 std::vector<std::vector<double> > factorsVec;
481 const bool allConstr =
false;
482 static std::atomic<bool> allConstrWarning{
false};
483 bool expected{
false};
484 if (allConstr && allConstrWarning.compare_exchange_strong(expected,
true)) {
485 edm::LogWarning(
"Alignment") <<
"@SUB=PedeSteerer::hierarchyConstraint"
486 <<
"changed to use all 6 constraints";
490 edm::LogWarning(
"Alignment") <<
"@SUB=PedeSteerer::hierarchyConstraint"
491 <<
"Problems from store.";
494 for (
unsigned int iConstr = 0; iConstr < paramIdsVec.size(); ++iConstr) {
495 std::ostringstream aConstr;
497 const std::vector<ParameterId> &parIds = paramIdsVec[iConstr];
498 const std::vector<double> &factors = factorsVec[iConstr];
499 unsigned int nParPerConstr = 0;
501 for (
unsigned int iParam = 0; iParam < parIds.size(); ++iParam) {
502 Alignable *aliSubComp = parIds[iParam].first;
503 const unsigned int compParNum = parIds[iParam].second;
506 aConstr <<
"* Taken out of hierarchy: ";
515 aConstr <<
paramLabel <<
" " << factors[iParam];
517 aConstr <<
" ! for param " << compParNum <<
" of a "
532 file <<
"\nConstraint 0.\n" << aConstr.str();
533 }
else if (nParPerConstr > 0) {
534 edm::LogWarning(
"Alignment") <<
"@SUB=PedeSteerer::hierarchyConstraint"
535 <<
"Skip constraint on " << nParPerConstr <<
" parameter(s):\n"
554 for (std::vector<edm::ParameterSet>::const_iterator iSet = cffPresi.begin(), iE = cffPresi.end(); iSet != iE;
560 const float presigma = (*iSet).getParameter<
double>(
"presigma");
561 if (presigma <= 0.) {
562 throw cms::Exception(
"BadConfig") <<
"[PedeSteerer::presigmas]: Pre-sigma must be > 0., but is " << presigma
566 for (
unsigned int iAli = 0; iAli < alis.size(); ++iAli) {
567 std::vector<float> &
presigmas = aliPresiMap[alis[iAli]];
570 for (
unsigned int iParam = 0; iParam <
sels[iAli].size(); ++iParam) {
571 if (
sels[iAli][iParam] !=
'0') {
574 <<
"[PedeSteerer::presigmas]: Try to set pre-sigma " << presigma <<
", but already "
575 <<
"set " <<
presigmas[iParam] <<
" (for a "
584 if (aliPresiMap.empty())
596 std::ofstream *filePtr =
nullptr;
598 unsigned int nPresiParam = 0;
599 for (
const auto &iAli : alis) {
601 AlignablePresigmasMap::const_iterator presigmasIt = aliPresiMap.find(iAli);
602 if (presigmasIt == aliPresiMap.end())
607 const std::vector<float> &
presigmas = presigmasIt->second;
608 for (
unsigned int iParam = 0; iParam <
presigmas.size(); ++iParam) {
613 if (!(iAli->alignmentParameters()->selector()[iParam]))
616 dynamic_cast<SelectionUserVariables *>(iAli->alignmentParameters()->userVariables());
617 const char selChar = (selVar ? selVar->
fullSelection()[iParam] :
'1');
618 if (selChar ==
'f' || selChar ==
'F' || selChar ==
'c' || selChar ==
'C')
623 (*filePtr) <<
"* Presigma values for active parameters: \nParameter\n";
648 throw cms::Exception(
"FileOpenProblem") <<
"[PedeSteerer::createSteerFile]"
649 <<
"Could not open " << realName <<
" as output file.";
650 }
else if (addToList) {
675 <<
" highest level objects define the "
676 <<
"coordinate system, steering file " << nameCoordFile <<
".";
680 const std::pair<unsigned int, unsigned int> nFixFixCor(this->
fixParameters(alis, nameFixFile));
681 if (nFixFixCor.first != 0 || nFixFixCor.second != 0) {
682 edm::LogInfo(
"Alignment") <<
"@SUB=PedeSteerer::buildSubSteer" << nFixFixCor.first <<
" parameters fixed at 0. and "
683 << nFixFixCor.second <<
" at 'original' position, "
684 <<
"steering file " << nameFixFile <<
".";
689 <<
" alignables taken out of hierarchy.";
695 edm::LogInfo(
"Alignment") <<
"@SUB=PedeSteerer::buildSubSteer"
696 <<
"Hierarchy constraints for " << nConstraint <<
" alignables, "
697 <<
"steering file " << nameHierarchyFile <<
".";
711 auto &ConstraintsConfigContainer = GeometryConstraints.
getConfigData();
714 for (
auto &it : ConstraintsConfigContainer) {
716 for (
const auto &ilevelsFilename : it.levelsFilenames_) {
717 it.mapFileName_.insert(
718 std::make_pair(ilevelsFilename.second, this->createSteerFile(ilevelsFilename.second,
true)));
723 if (nGeometryConstraint) {
724 edm::LogInfo(
"Alignment") <<
"@SUB=PedeSteerer::buildSubSteer"
725 <<
"Geometry constraints for " << nGeometryConstraint <<
" alignables.";
737 edm::LogInfo(
"Alignment") <<
"@SUB=PedeSteerer::buildSubSteer"
738 <<
"Presigma values set for " << nPresigma <<
" parameters, "
739 <<
"steering file " << namePresigmaFile <<
".";
748 std::ofstream *mainSteerPtr = this->
createSteerFile(nameMasterSteer,
false);
753 std::vector<std::string> addfiles =
myConfig.
getParameter<std::vector<std::string> >(
"additionalSteerFiles");
757 std::ofstream &mainSteerRef = *mainSteerPtr;
758 for (
unsigned int iFile = 0; iFile <
mySteeringFiles.size(); ++iFile) {
763 mainSteerRef <<
"\nCfiles\n";
764 for (
unsigned int iFile = 0; iFile < binaryFiles.size(); ++iFile) {
765 mainSteerRef << binaryFiles[iFile] <<
"\n";
773 mainSteerRef <<
"\n* Outlier treatment and other options \n";
774 for (
unsigned int i = 0;
i <
opt.size(); ++
i) {
775 mainSteerRef <<
opt[
i] <<
"\n";
780 return nameMasterSteer;
785 if (masterSteer.empty()) {
787 <<
"Empty master steer file, stop";
792 (
command +=
" ") += masterSteer;
799 edm::LogInfo(
"Alignment") <<
"@SUB=PedeSteerer::runPede"
800 <<
"Start running " <<
command;
802 int shellReturn = gSystem->Exec(
command.c_str());
803 edm::LogInfo(
"Alignment") <<
"@SUB=PedeSteerer::runPede"
804 <<
"Command returns " << shellReturn;