52 const std::vector<double>&
co,
54 const std::vector<std::pair<Alignable*, std::string> >& alisFile,
58 const bool downToLowestLevel)
61 levelsFilenames_(alisFile),
62 excludedAlignables_(ex),
65 downToLowestLevel_(downToLowestLevel) {}
70 const std::vector<edm::ParameterSet>&
config,
76 unsigned int psetnr = 0;
77 std::set<std::string> steerFilePrefixContainer;
78 for (
const auto&
pset : myConfig_) {
79 this->verifyParameterNames(
pset, psetnr);
82 const auto coefficients =
pset.getParameter<std::vector<double> >(
"coefficients");
83 const auto dm =
pset.exists(
"deadmodules") ?
pset.getParameter<std::vector<unsigned int> >(
"deadmodules")
84 : std::vector<unsigned int>();
87 const auto ignoredInstances =
pset.exists(
"ignoredInstances")
88 ?
pset.getUntrackedParameter<std::vector<unsigned int> >(
"ignoredInstances")
89 : std::vector<unsigned int>();
91 const auto downToLowestLevel =
92 pset.exists(
"downToLowestLevel") ?
pset.getUntrackedParameter<
bool>(
"downToLowestLevel") :
false;
98 const auto& alis = selector.selectedAlignables();
101 selector_excludedalignables.clear();
102 if (
pset.exists(
"excludedAlignables")) {
103 selector_excludedalignables.addSelections(
pset.getParameter<
edm::ParameterSet>(
"excludedAlignables"));
105 const auto& excluded_alis = selector_excludedalignables.selectedAlignables();
109 auto sysdeformation = this->verifyDeformationName(
name, coefficients);
111 if (deadmodules_.empty()) {
112 edm::LogInfo(
"Alignment") <<
"@SUB=PedeSteererWeakModeConstraints"
113 <<
"Load list of dead modules (size = " <<
dm.size() <<
").";
114 for (
const auto& it :
dm)
115 deadmodules_.push_back(it);
121 if (
std::find(ignoredInstances.begin(), ignoredInstances.end(),
instance) != ignoredInstances.end()) {
124 std::stringstream defaultsteerfileprefix;
125 defaultsteerfileprefix <<
"autosteerFilePrefix_" <<
name <<
"_" << psetnr <<
"_" <<
instance;
127 const auto steerFilePrefix =
pset.exists(
"steerFilePrefix") ?
pset.getParameter<
std::string>(
"steerFilePrefix") +
129 : defaultsteerfileprefix.
str();
131 const auto levelsFilenames = this->makeLevelsFilenames(steerFilePrefixContainer, alis, steerFilePrefix);
135 coefficients,
name, levelsFilenames, sysdeformation, excluded_alis,
instance, downToLowestLevel));
143 const auto aliPar = dynamic_cast<TwoBowedSurfacesAlignmentParameters*>(ali->
alignmentParameters());
145 const auto ySplit = aliPar->ySplit();
147 const auto yM1 = 0.5 * (ySplit - halfLength);
148 const auto yM2 = yM1 + halfLength;
151 return std::make_pair(pos_sensor0, pos_sensor1);
153 throw cms::Exception(
"Alignment") <<
"[PedeSteererWeakModeConstraints::getDoubleSensorPosition]"
154 <<
" Dynamic cast to double sensor parameters failed.";
161 unsigned int nConstraints = 0;
164 for (
const auto& iHLS : iC.levelsFilenames_) {
167 if (iC.downToLowestLevel_) {
168 if (!iHLS.first->lastCompsWithParams(aliDaughts)) {
169 edm::LogWarning(
"Alignment") <<
"@SUB=PedeSteererWeakModeConstraints::createAlignablesDataStructure"
170 <<
"Some but not all component branches "
175 if (!iHLS.first->firstCompsWithParams(aliDaughts)) {
176 edm::LogWarning(
"Alignment") <<
"@SUB=PedeSteererWeakModeConstraints::createAlignablesDataStructure"
177 <<
"Some but not all daughters of "
184 std::list<Alignable*> usedinconstraint;
185 for (
const auto& iD : aliDaughts) {
186 bool isNOTdead =
true;
189 iD->geomDetId().rawId() == iDeadmodules) {
195 for (
const auto& iEx : iC.excludedAlignables_) {
196 if (iD->id() == iEx->id() && iD->alignableObjectId() == iEx->alignableObjectId()) {
202 const bool issubcomponent = this->
checkMother(iD, iHLS.first);
203 if (issubcomponent) {
205 usedinconstraint.push_back(iD);
209 throw cms::Exception(
"Alignment") <<
"[PedeSteererWeakModeConstraints::createAlignablesDataStructure]"
210 <<
" Sanity check failed. Alignable defined as active sub-component, "
211 <<
" but in fact its not a daugther of "
216 if (!usedinconstraint.empty()) {
217 iC.HLSsubdets_.push_back(std::make_pair(iHLS.first, usedinconstraint));
219 edm::LogInfo(
"Alignment") <<
"@SUB=PedeSteererWeakModeConstraints"
220 <<
"No sub-components for "
222 << iHLS.first->globalPosition().x() <<
"," << iHLS.first->globalPosition().y() <<
","
223 << iHLS.first->globalPosition().z() <<
") selected. Skip constraint";
225 if (aliDaughts.empty()) {
226 edm::LogWarning(
"Alignment") <<
"@SUB=PedeSteererWeakModeConstraints::createAlignablesDataStructure"
227 <<
"No active sub-alignables found for "
229 << iHLS.first->globalPosition().x() <<
"," << iHLS.first->globalPosition().y()
230 <<
"," << iHLS.first->globalPosition().z() <<
").";
240 const double phase)
const {
243 const double r = TMath::Sqrt(
pos.x() *
pos.x() +
pos.y() *
pos.y());
245 switch (sysdeformation) {
246 case SystematicDeformations::kTwist:
247 case SystematicDeformations::kZexpansion:
250 case SystematicDeformations::kSagitta:
251 case SystematicDeformations::kRadial:
252 case SystematicDeformations::kTelescope:
253 case SystematicDeformations::kLayerRotation:
256 case SystematicDeformations::kBowing:
259 case SystematicDeformations::kElliptical:
262 case SystematicDeformations::kSkew:
276 const int iParameter,
278 const std::vector<double>& constraintparameters)
const {
279 if (iParameter < 0 || iParameter > 2) {
280 throw cms::Exception(
"Alignment") <<
"[PedeSteererWeakModeConstraints::getCoefficient]"
281 <<
" iParameter has to be in the range [0,2] but"
282 <<
" it is equal to " << iParameter <<
".";
286 const std::vector<double> vec_u = {
pos.x() - gUDirection.
x(),
pos.y() - gUDirection.
y(),
pos.z() - gUDirection.
z()};
287 const std::vector<double> vec_v = {
pos.x() - gVDirection.
x(),
pos.y() - gVDirection.
y(),
pos.z() - gVDirection.
z()};
288 const std::vector<double> vec_w = {
pos.x() - gWDirection.
x(),
pos.y() - gWDirection.
y(),
pos.z() - gWDirection.
z()};
291 const std::vector<std::vector<double> > global_vecs = {vec_u, vec_v, vec_w};
293 const double n = TMath::Sqrt(global_vecs.at(iParameter).at(0) * global_vecs.at(iParameter).at(0) +
294 global_vecs.at(iParameter).at(1) * global_vecs.at(iParameter).at(1) +
295 global_vecs.at(iParameter).at(2) * global_vecs.at(iParameter).at(2));
296 const double r = TMath::Sqrt(
pos.x() *
pos.x() +
pos.y() *
pos.y());
300 const std::vector<double> radial_direction = {TMath::Sin(
phase), TMath::Cos(
phase), 0.0};
302 const double norm_radial_direction =
303 TMath::Sqrt(radial_direction.at(0) * radial_direction.at(0) + radial_direction.at(1) * radial_direction.at(1) +
304 radial_direction.at(2) * radial_direction.at(2));
307 const std::vector<double> phi_direction = {-1.0 *
pos.y(),
pos.x(), 0.0};
308 const double norm_phi_direction =
309 TMath::Sqrt(phi_direction.at(0) * phi_direction.at(0) + phi_direction.at(1) * phi_direction.at(1) +
310 phi_direction.at(2) * phi_direction.at(2));
313 static const std::vector<double> z_direction = {0.0, 0.0, 1.0};
314 const double norm_z_direction =
315 TMath::Sqrt(z_direction.at(0) * z_direction.at(0) + z_direction.at(1) * z_direction.at(1) +
316 z_direction.at(2) * z_direction.at(2));
319 const std::vector<double> rDirection = {
pos.x(),
pos.y(), 0.0};
320 const double norm_rDirection = TMath::Sqrt(rDirection.at(0) * rDirection.at(0) + rDirection.at(1) * rDirection.at(1) +
321 rDirection.at(2) * rDirection.at(2));
324 double dot_product = 0.0;
325 double normalisation_factor = 1.0;
328 switch (sysdeformation) {
329 case SystematicDeformations::kTwist:
330 case SystematicDeformations::kLayerRotation:
331 dot_product = phi_direction.at(0) * global_vecs.at(iParameter).at(0) +
332 phi_direction.at(1) * global_vecs.at(iParameter).at(1) +
333 phi_direction.at(2) * global_vecs.at(iParameter).at(2);
334 normalisation_factor =
r *
n * norm_phi_direction;
336 case SystematicDeformations::kZexpansion:
337 case SystematicDeformations::kTelescope:
338 case SystematicDeformations::kSkew:
339 dot_product = global_vecs.at(iParameter).at(0) * z_direction.at(0) +
340 global_vecs.at(iParameter).at(1) * z_direction.at(1) +
341 global_vecs.at(iParameter).at(2) * z_direction.at(2);
342 normalisation_factor = (
n * norm_z_direction);
344 case SystematicDeformations::kRadial:
345 case SystematicDeformations::kBowing:
346 case SystematicDeformations::kElliptical:
347 dot_product = global_vecs.at(iParameter).at(0) * rDirection.at(0) +
348 global_vecs.at(iParameter).at(1) * rDirection.at(1) +
349 global_vecs.at(iParameter).at(2) * rDirection.at(2);
350 normalisation_factor = (
n * norm_rDirection);
352 case SystematicDeformations::kSagitta:
353 dot_product = global_vecs.at(iParameter).at(0) * radial_direction.at(0) +
354 global_vecs.at(iParameter).at(1) * radial_direction.at(1) +
355 global_vecs.at(iParameter).at(2) * radial_direction.at(2);
356 normalisation_factor = (
n * norm_radial_direction);
363 coeff = dot_product * (this->
getX(sysdeformation,
pos,
phase) - x0) / normalisation_factor;
365 throw cms::Exception(
"Alignment") <<
"[PedeSteererWeakModeConstraints::getCoefficient]"
366 <<
" Normalisation factor"
367 <<
"for coefficient calculation equal to zero! Misconfiguration?";
374 bool isselected =
false;
377 if ((iParameter <= 2) || (iParameter >= 9 && iParameter <= 11)) {
378 if (!aliSel.at(iParameter)) {
382 auto selVar = dynamic_cast<SelectionUserVariables*>(
params->userVariables());
384 if (selVar->fullSelection().size() <= (iParameter + 1)) {
386 <<
"[PedeSteererWeakModeConstraints::checkSelectionShiftParameter]"
387 <<
" Can not access selected alignment variables of alignable "
390 <<
"for parameter number " << (iParameter + 1) <<
".";
393 const char selChar = (selVar ? selVar->fullSelection().at(iParameter) :
'1');
395 if (selChar ==
'1' || selChar ==
'r') {
409 for (
auto& iFile : it.mapFileName_) {
412 iFile.second =
nullptr;
414 throw cms::Exception(
"FileCloseProblem") <<
"[PedeSteererWeakModeConstraints]"
415 <<
" can not close file " << iFile.first <<
".";
428 if (
ofile ==
nullptr) {
429 throw cms::Exception(
"FileFindError") <<
"[PedeSteererWeakModeConstraints] Cannot find output file.";
433 (*ofile) <<
"Constraint " << std::scientific << constr << std::endl;
434 for (
const auto& ioutput :
output) {
435 (*ofile) <<
std::fixed << ioutput.first <<
" " << std::scientific << ioutput.second << std::endl;
444 std::ofstream*
file =
nullptr;
447 if (ilevelsFilename.first->id() == iHLS->
id() &&
449 const auto iFile = it.
mapFileName_.find(ilevelsFilename.second);
451 file = iFile->second;
462 double nmodules = 0.0;
465 for (
const auto& ali : iHLS.second) {
467 bool alignableIsFloating =
false;
470 for (
unsigned int iParameter = 0; static_cast<int>(iParameter) < ali->alignmentParameters()->size(); iParameter++) {
472 alignableIsFloating =
true;
488 if (alignableIsFloating) {
505 throw cms::Exception(
"Alignment") <<
"@SUB=PedeSteererWeakModeConstraints::ConstructConstraints"
506 <<
" Number of selected modules equal to zero. Check configuration!";
521 std::vector<std::list<std::pair<unsigned int, double> > > createdConstraints;
527 for (
const auto& iHLS : it.HLSsubdets_) {
528 double sum_xi_x0 = 0.0;
529 std::list<std::pair<unsigned int, double> >
output;
531 const double x0 = this->
getX0(iHLS, it);
533 for (std::list<Alignable*>::const_iterator iAlignables = iHLS.second.begin(); iAlignables != iHLS.second.end();
536 const auto aliLabel =
540 const LocalPoint lUDirection(1., 0., 0.), lVDirection(0., 1., 0.), lWDirection(0., 0., 1.);
543 gWDirection = surface.
toGlobal(lWDirection);
550 const auto& pos_sensor0 = sensorpositions.first;
551 const auto& pos_sensor1 = sensorpositions.second;
553 const auto x_sensor0 = this->
getX(it.sysdeformation_, pos_sensor0,
phase);
554 const auto x_sensor1 = isDoubleSensor ? this->
getX(it.sysdeformation_, pos_sensor1,
phase) : 0.0;
556 sum_xi_x0 += (x_sensor0 - x0) * (x_sensor0 - x0);
557 if (isDoubleSensor) {
558 sum_xi_x0 += (x_sensor1 - x0) * (x_sensor1 - x0);
562 for (
int iParameter = 0; iParameter < numparameterlimit; iParameter++) {
564 if (iParameter == 0 || iParameter == 9)
566 if (iParameter == 1 || iParameter == 10)
568 if (iParameter == 2 || iParameter == 11)
571 if ((iParameter >= 0 && iParameter <= 2) || (iParameter >= 9 && iParameter <= 11)) {
581 const auto&
pos = (iParameter <= 2) ? pos_sensor0 : pos_sensor1;
583 if (iParameter == 0 || iParameter == 1 || iParameter == 2 || iParameter == 9 || iParameter == 10 ||
586 it.sysdeformation_,
pos, gUDirection, gVDirection, gWDirection, localindex, x0, it.coefficients_);
591 <<
"@SUB=PedeSteererWeakModeConstraints::getCoefficient"
596 <<
" for parameter " << localindex <<
" equal to zero. This alignable is used in the constraint"
597 <<
" '" << it.constraintName_
598 <<
"'. The id is: alignable->geomDetId().rawId() = " << ali->
geomDetId().
rawId() <<
".";
605 if (
std::find(createdConstraints.begin(), createdConstraints.end(),
output) != createdConstraints.end()) {
609 throw cms::Exception(
"FileFindError") <<
"[PedeSteererWeakModeConstraints] Cannot find output file.";
611 *
outFile <<
"! The constraint for this IOV/momentum range" << std::endl
612 <<
"! has been removed because the used parameters" << std::endl
613 <<
"! are not IOV or momentum-range dependent." << std::endl;
617 this->
writeOutput(output, it, iHLS.first, sum_xi_x0);
618 createdConstraints.push_back(
output);
631 if (lowleveldet->
mother() ==
nullptr)
640 const auto parameterNames =
pset.getParameterNames();
641 for (
const auto&
name : parameterNames) {
642 if (
name !=
"coefficients" &&
name !=
"deadmodules" &&
name !=
"constraint" &&
name !=
"steerFilePrefix" &&
643 name !=
"levels" &&
name !=
"excludedAlignables" &&
name !=
"ignoredInstances" &&
name !=
"downToLowestLevel") {
644 throw cms::Exception(
"BadConfig") <<
"@SUB=PedeSteererWeakModeConstraints::verifyParameterNames:"
645 <<
" Unknown parameter name '" <<
name <<
"' in PSet number " << psetnr
646 <<
". Maybe a typo?";
653 std::set<std::string>& steerFilePrefixContainer,
657 if (steerFilePrefixContainer.find(steerFilePrefix) != steerFilePrefixContainer.end()) {
658 throw cms::Exception(
"BadConfig") <<
"[PedeSteererWeakModeConstraints] Steering file"
659 <<
" prefix '" << steerFilePrefix <<
"' already exists. Specify unique names!";
661 steerFilePrefixContainer.insert(steerFilePrefix);
664 std::vector<std::pair<Alignable*, std::string> > levelsFilenames;
665 for (
const auto& ali : alis) {
669 << ali->alignableObjectId() <<
".txt";
671 levelsFilenames.push_back(std::make_pair(ali,
n.str()));
673 return levelsFilenames;
678 const std::vector<double>& coefficients)
const {
681 if (
name ==
"twist") {
682 sysdeformation = SystematicDeformations::kTwist;
683 }
else if (
name ==
"zexpansion") {
684 sysdeformation = SystematicDeformations::kZexpansion;
685 }
else if (
name ==
"sagitta") {
686 sysdeformation = SystematicDeformations::kSagitta;
687 }
else if (
name ==
"radial") {
688 sysdeformation = SystematicDeformations::kRadial;
689 }
else if (
name ==
"telescope") {
690 sysdeformation = SystematicDeformations::kTelescope;
691 }
else if (
name ==
"layerrotation") {
692 sysdeformation = SystematicDeformations::kLayerRotation;
693 }
else if (
name ==
"bowing") {
694 sysdeformation = SystematicDeformations::kBowing;
695 }
else if (
name ==
"skew") {
696 sysdeformation = SystematicDeformations::kSkew;
697 }
else if (
name ==
"elliptical") {
698 sysdeformation = SystematicDeformations::kElliptical;
702 throw cms::Exception(
"BadConfig") <<
"[PedeSteererWeakModeConstraints]"
703 <<
" specified configuration option '" <<
name <<
"' not known.";
705 if ((sysdeformation == SystematicDeformations::kSagitta || sysdeformation == SystematicDeformations::kElliptical ||
706 sysdeformation == SystematicDeformations::kSkew) &&
707 coefficients.size() != 2) {
708 throw cms::Exception(
"BadConfig") <<
"[PedeSteererWeakModeConstraints]"
709 <<
" Excactly two parameters using the coefficient"
710 <<
" variable have to be provided for the " <<
name <<
" constraint.";
712 if ((sysdeformation == SystematicDeformations::kTwist || sysdeformation == SystematicDeformations::kZexpansion ||
713 sysdeformation == SystematicDeformations::kTelescope ||
714 sysdeformation == SystematicDeformations::kLayerRotation || sysdeformation == SystematicDeformations::kRadial ||
715 sysdeformation == SystematicDeformations::kBowing) &&
716 coefficients.size() != 1) {
717 throw cms::Exception(
"BadConfig") <<
"[PedeSteererWeakModeConstraints]"
718 <<
" Excactly ONE parameter using the coefficient"
719 <<
" variable have to be provided for the " <<
name <<
" constraint.";
722 if (coefficients.empty()) {
723 throw cms::Exception(
"BadConfig") <<
"[PedeSteererWeakModeConstraints]"
724 <<
" At least one coefficient has to be specified.";
726 return sysdeformation;
731 return coefficients.size() == 2 ? coefficients.at(1) : 0.0;