CMS 3D CMS Logo

PedeSteererWeakModeConstraints.cc
Go to the documentation of this file.
1 
10 
12 
14 
25 
27 // for 'type identification' as Alignable
31 // GF doubts the need of these includes from include checker campaign:
37 // end of doubt
38 
40 
41 #include <fstream>
42 #include <sstream>
43 #include <algorithm>
44 
45 // from ROOT
46 #include <TSystem.h>
47 #include <TMath.h>
48 
49 #include <iostream>
50 
52  const std::vector<double>& co,
53  const std::string& c,
54  const std::vector<std::pair<Alignable*, std::string> >& alisFile,
55  const int sd,
56  const align::Alignables& ex,
57  const int instance,
58  const bool downToLowestLevel)
59  : coefficients_(co),
60  constraintName_(c),
61  levelsFilenames_(alisFile),
62  excludedAlignables_(ex),
63  sysdeformation_(sd),
64  instance_(instance),
65  downToLowestLevel_(downToLowestLevel) {}
66 
67 //_________________________________________________________________________
69  const PedeLabelerBase* labels,
70  const std::vector<edm::ParameterSet>& config,
71  std::string sf)
72  : myLabels_(labels),
73  myConfig_(config),
74  steerFile_(sf),
75  alignableObjectId_{AlignableObjectId::commonObjectIdProvider(aliTracker, nullptr)} {
76  unsigned int psetnr = 0;
77  std::set<std::string> steerFilePrefixContainer;
78  for (const auto& pset : myConfig_) {
79  this->verifyParameterNames(pset, psetnr);
80  psetnr++;
81 
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>();
85  std::string name = pset.getParameter<std::string>("constraint");
86  std::transform(name.begin(), name.end(), name.begin(), ::tolower);
87  const auto ignoredInstances = pset.exists("ignoredInstances")
88  ? pset.getUntrackedParameter<std::vector<unsigned int> >("ignoredInstances")
89  : std::vector<unsigned int>();
90 
91  const auto downToLowestLevel =
92  pset.exists("downToLowestLevel") ? pset.getUntrackedParameter<bool>("downToLowestLevel") : false;
93 
94  AlignmentParameterSelector selector(aliTracker, nullptr, nullptr);
95  selector.clear();
96  selector.addSelections(pset.getParameter<edm::ParameterSet>("levels"));
97 
98  const auto& alis = selector.selectedAlignables();
99 
100  AlignmentParameterSelector selector_excludedalignables(aliTracker, nullptr, nullptr);
101  selector_excludedalignables.clear();
102  if (pset.exists("excludedAlignables")) {
103  selector_excludedalignables.addSelections(pset.getParameter<edm::ParameterSet>("excludedAlignables"));
104  }
105  const auto& excluded_alis = selector_excludedalignables.selectedAlignables();
106 
107  //check that the name of the deformation is known and that the number
108  //of provided parameter is right.
109  auto sysdeformation = this->verifyDeformationName(name, coefficients);
110 
111  if (deadmodules_.empty()) { //fill the list of dead modules only once
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);
116  }
117 
118  // loop over all IOVs/momentum ranges
119  for (unsigned int instance = 0; instance < myLabels_->maxNumberOfParameterInstances(); ++instance) {
120  // check if this IOV/momentum range is to be ignored:
121  if (std::find(ignoredInstances.begin(), ignoredInstances.end(), instance) != ignoredInstances.end()) {
122  continue;
123  }
124  std::stringstream defaultsteerfileprefix;
125  defaultsteerfileprefix << "autosteerFilePrefix_" << name << "_" << psetnr << "_" << instance;
126 
127  const auto steerFilePrefix = pset.exists("steerFilePrefix") ? pset.getParameter<std::string>("steerFilePrefix") +
128  "_" + std::to_string(instance)
129  : defaultsteerfileprefix.str();
130 
131  const auto levelsFilenames = this->makeLevelsFilenames(steerFilePrefixContainer, alis, steerFilePrefix);
132 
133  //Add the configuration data for this constraint to the container of config data
134  ConstraintsConfigContainer_.emplace_back(GeometryConstraintConfigData(
135  coefficients, name, levelsFilenames, sysdeformation, excluded_alis, instance, downToLowestLevel));
136  }
137  }
138 }
139 
140 //_________________________________________________________________________
141 std::pair<align::GlobalPoint, align::GlobalPoint> PedeSteererWeakModeConstraints::getDoubleSensorPosition(
142  const Alignable* ali) const {
143  const auto aliPar = dynamic_cast<TwoBowedSurfacesAlignmentParameters*>(ali->alignmentParameters());
144  if (aliPar) {
145  const auto ySplit = aliPar->ySplit();
146  const auto halfLength = 0.5 * ali->surface().length();
147  const auto yM1 = 0.5 * (ySplit - halfLength); // y_mean of surface 1
148  const auto yM2 = yM1 + halfLength; // y_mean of surface 2
149  const auto pos_sensor0(ali->surface().toGlobal(align::LocalPoint(0., yM1, 0.)));
150  const auto pos_sensor1(ali->surface().toGlobal(align::LocalPoint(0., yM2, 0.)));
151  return std::make_pair(pos_sensor0, pos_sensor1);
152  } else {
153  throw cms::Exception("Alignment") << "[PedeSteererWeakModeConstraints::getDoubleSensorPosition]"
154  << " Dynamic cast to double sensor parameters failed.";
155  return std::make_pair(align::GlobalPoint(0.0, 0.0, 0.0), align::GlobalPoint(0.0, 0.0, 0.0));
156  }
157 }
158 
159 //_________________________________________________________________________
161  unsigned int nConstraints = 0;
162  for (auto& iC : ConstraintsConfigContainer_) {
163  //loop over all HLS for which the constraint is to be determined
164  for (const auto& iHLS : iC.levelsFilenames_) {
165  //determine next active sub-alignables for iHLS
166  align::Alignables aliDaughts;
167  if (iC.downToLowestLevel_) {
168  if (!iHLS.first->lastCompsWithParams(aliDaughts)) {
169  edm::LogWarning("Alignment") << "@SUB=PedeSteererWeakModeConstraints::createAlignablesDataStructure"
170  << "Some but not all component branches "
171  << alignableObjectId_.idToString(iHLS.first->alignableObjectId())
172  << " with params!";
173  }
174  } else {
175  if (!iHLS.first->firstCompsWithParams(aliDaughts)) {
176  edm::LogWarning("Alignment") << "@SUB=PedeSteererWeakModeConstraints::createAlignablesDataStructure"
177  << "Some but not all daughters of "
178  << alignableObjectId_.idToString(iHLS.first->alignableObjectId())
179  << " with params!";
180  }
181  }
182  ++nConstraints;
183 
184  std::list<Alignable*> usedinconstraint;
185  for (const auto& iD : aliDaughts) {
186  bool isNOTdead = true;
187  for (const auto& iDeadmodules : deadmodules_) {
188  if ((iD->alignableObjectId() == align::AlignableDetUnit || iD->alignableObjectId() == align::AlignableDet) &&
189  iD->geomDetId().rawId() == iDeadmodules) {
190  isNOTdead = false;
191  break;
192  }
193  }
194  //check if the module is excluded
195  for (const auto& iEx : iC.excludedAlignables_) {
196  if (iD->id() == iEx->id() && iD->alignableObjectId() == iEx->alignableObjectId()) {
197  //if(iD->geomDetId().rawId() == (*iEx)->geomDetId().rawId()) {
198  isNOTdead = false;
199  break;
200  }
201  }
202  const bool issubcomponent = this->checkMother(iD, iHLS.first);
203  if (issubcomponent) {
204  if (isNOTdead) {
205  usedinconstraint.push_back(iD);
206  }
207  } else {
208  //sanity check
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 "
212  << alignableObjectId_.idToString(iHLS.first->alignableObjectId());
213  }
214  }
215 
216  if (!usedinconstraint.empty()) {
217  iC.HLSsubdets_.push_back(std::make_pair(iHLS.first, usedinconstraint));
218  } else {
219  edm::LogInfo("Alignment") << "@SUB=PedeSteererWeakModeConstraints"
220  << "No sub-components for "
221  << alignableObjectId_.idToString(iHLS.first->alignableObjectId()) << " at ("
222  << iHLS.first->globalPosition().x() << "," << iHLS.first->globalPosition().y() << ","
223  << iHLS.first->globalPosition().z() << ") selected. Skip constraint";
224  }
225  if (aliDaughts.empty()) {
226  edm::LogWarning("Alignment") << "@SUB=PedeSteererWeakModeConstraints::createAlignablesDataStructure"
227  << "No active sub-alignables found for "
228  << alignableObjectId_.idToString(iHLS.first->alignableObjectId()) << " at ("
229  << iHLS.first->globalPosition().x() << "," << iHLS.first->globalPosition().y()
230  << "," << iHLS.first->globalPosition().z() << ").";
231  }
232  }
233  }
234  return nConstraints;
235 }
236 
237 //_________________________________________________________________________
238 double PedeSteererWeakModeConstraints::getX(const int sysdeformation,
239  const align::GlobalPoint& pos,
240  const double phase) const {
241  double x = 0.0;
242 
243  const double r = TMath::Sqrt(pos.x() * pos.x() + pos.y() * pos.y());
244 
245  switch (sysdeformation) {
246  case SystematicDeformations::kTwist:
247  case SystematicDeformations::kZexpansion:
248  x = pos.z();
249  break;
250  case SystematicDeformations::kSagitta:
251  case SystematicDeformations::kRadial:
252  case SystematicDeformations::kTelescope:
253  case SystematicDeformations::kLayerRotation:
254  x = r;
255  break;
256  case SystematicDeformations::kBowing:
257  x = pos.z() * pos.z(); //TMath::Abs(pos.z());
258  break;
259  case SystematicDeformations::kElliptical:
260  x = r * TMath::Cos(2.0 * pos.phi() + phase);
261  break;
262  case SystematicDeformations::kSkew:
263  x = TMath::Cos(pos.phi() + phase);
264  break;
265  };
266 
267  return x;
268 }
269 
270 //_________________________________________________________________________
271 double PedeSteererWeakModeConstraints::getCoefficient(const int sysdeformation,
272  const align::GlobalPoint& pos,
273  const GlobalPoint gUDirection,
274  const GlobalPoint gVDirection,
275  const GlobalPoint gWDirection,
276  const int iParameter,
277  const double& x0,
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 << ".";
283  }
284 
285  //global vectors pointing in u,v,w direction
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()};
289 
290  //FIXME: how to make inner vectors const?
291  const std::vector<std::vector<double> > global_vecs = {vec_u, vec_v, vec_w};
292 
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());
297 
298  const double phase = this->getPhase(constraintparameters);
299  //const double radial_direction[3] = {TMath::Sin(phase), TMath::Cos(phase), 0.0};
300  const std::vector<double> radial_direction = {TMath::Sin(phase), TMath::Cos(phase), 0.0};
301  //is equal to unity by construction ...
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));
305 
306  //const double phi_direction[3] = { -1.0 * pos.y(), pos.x(), 0.0};
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));
311 
312  //const double z_direction[3] = {0.0, 0.0, 1.0};
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));
317 
318  //unit vector pointing from the origin to the module position in the transverse plane
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));
322 
323  double coeff = 0.0;
324  double dot_product = 0.0;
325  double normalisation_factor = 1.0;
326 
327  //see https://indico.cern.ch/getFile.py/access?contribId=15&sessionId=1&resId=0&materialId=slides&confId=127126
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;
335  break;
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);
343  break;
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);
351  break;
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);
357  break;
358  default:
359  break;
360  }
361 
362  if (TMath::Abs(normalisation_factor) > 0.0) {
363  coeff = dot_product * (this->getX(sysdeformation, pos, phase) - x0) / normalisation_factor;
364  } else {
365  throw cms::Exception("Alignment") << "[PedeSteererWeakModeConstraints::getCoefficient]"
366  << " Normalisation factor"
367  << "for coefficient calculation equal to zero! Misconfiguration?";
368  }
369  return coeff;
370 }
371 
372 //_________________________________________________________________________
373 bool PedeSteererWeakModeConstraints::checkSelectionShiftParameter(const Alignable* ali, unsigned int iParameter) const {
374  bool isselected = false;
375  const std::vector<bool>& aliSel = ali->alignmentParameters()->selector();
376  //exclude non-shift parameters
377  if ((iParameter <= 2) || (iParameter >= 9 && iParameter <= 11)) {
378  if (!aliSel.at(iParameter)) {
379  isselected = false;
380  } else {
381  auto params = ali->alignmentParameters();
382  auto selVar = dynamic_cast<SelectionUserVariables*>(params->userVariables());
383  if (selVar) {
384  if (selVar->fullSelection().size() <= (iParameter + 1)) {
385  throw cms::Exception("Alignment")
386  << "[PedeSteererWeakModeConstraints::checkSelectionShiftParameter]"
387  << " Can not access selected alignment variables of alignable "
388  << alignableObjectId_.idToString(ali->alignableObjectId()) << "at (" << ali->globalPosition().x() << ","
389  << ali->globalPosition().y() << "," << ali->globalPosition().z() << ") "
390  << "for parameter number " << (iParameter + 1) << ".";
391  }
392  }
393  const char selChar = (selVar ? selVar->fullSelection().at(iParameter) : '1');
394  // if(selChar == '1') { //FIXME??? what about 'r'?
395  if (selChar == '1' || selChar == 'r') {
396  isselected = true;
397  } else {
398  isselected = false;
399  }
400  }
401  }
402  return isselected;
403 }
404 
405 //_________________________________________________________________________
407  //'delete' output files which means: close them
408  for (auto& it : ConstraintsConfigContainer_) {
409  for (auto& iFile : it.mapFileName_) {
410  if (iFile.second) {
411  delete iFile.second;
412  iFile.second = nullptr;
413  } else {
414  throw cms::Exception("FileCloseProblem") << "[PedeSteererWeakModeConstraints]"
415  << " can not close file " << iFile.first << ".";
416  }
417  }
418  }
419 }
420 
421 //_________________________________________________________________________
422 void PedeSteererWeakModeConstraints::writeOutput(const std::list<std::pair<unsigned int, double> >& output,
424  const Alignable* iHLS,
425  double sum_xi_x0) {
426  std::ofstream* ofile = getFile(it, iHLS);
427 
428  if (ofile == nullptr) {
429  throw cms::Exception("FileFindError") << "[PedeSteererWeakModeConstraints] Cannot find output file.";
430  } else {
431  if (!output.empty()) {
432  const double constr = sum_xi_x0 * it.coefficients_.front();
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;
436  }
437  }
438  }
439 }
440 
441 //_________________________________________________________________________
443  const Alignable* iHLS) const {
444  std::ofstream* file = nullptr;
445 
446  for (const auto& ilevelsFilename : it.levelsFilenames_) {
447  if (ilevelsFilename.first->id() == iHLS->id() &&
448  ilevelsFilename.first->alignableObjectId() == iHLS->alignableObjectId()) {
449  const auto iFile = it.mapFileName_.find(ilevelsFilename.second);
450  if (iFile != it.mapFileName_.end()) {
451  file = iFile->second;
452  }
453  }
454  }
455 
456  return file;
457 }
458 
459 //_________________________________________________________________________
460 double PedeSteererWeakModeConstraints::getX0(const std::pair<Alignable*, std::list<Alignable*> >& iHLS,
461  const GeometryConstraintConfigData& it) const {
462  double nmodules = 0.0;
463  double x0 = 0.0;
464 
465  for (const auto& ali : iHLS.second) {
466  align::PositionType pos = ali->globalPosition();
467  bool alignableIsFloating = false; //means: true=alignable is able to move in at least one direction
468 
469  //test whether at least one variable has been selected in the configuration
470  for (unsigned int iParameter = 0; static_cast<int>(iParameter) < ali->alignmentParameters()->size(); iParameter++) {
471  if (this->checkSelectionShiftParameter(ali, iParameter)) {
472  alignableIsFloating = true;
473  // //verify that alignable has just one label -- meaning no IOV-dependence etc
474  // const unsigned int nInstances = myLabels_->numberOfParameterInstances(ali, iParameter);
475  // if(nInstances > 1) {
476  // throw cms::Exception("PedeSteererWeakModeConstraints")
477  // << "@SUB=PedeSteererWeakModeConstraints::ConstructConstraints"
478  // << " Weak mode constraints are only supported for alignables which have"
479  // << " just one label. However, e.g. alignable"
480  // << " " << alignableObjectId_.idToString(ali->alignableObjectId())
481  // << "at (" << ali->globalPosition().x() << ","<< ali->globalPosition().y() << "," << ali->globalPosition().z()<< "), "
482  // << " was configured to have >1 label. Remove e.g. IOV-dependence for this (and other) alignables which are used in the constraint.";
483  // }
484  break;
485  }
486  }
487  //at least one parameter of the alignable can be changed in the alignment
488  if (alignableIsFloating) {
489  const auto phase = this->getPhase(it.coefficients_);
490  if (ali->alignmentParameters()->type() != AlignmentParametersFactory::kTwoBowedSurfaces) {
491  x0 += this->getX(it.sysdeformation_, pos, phase);
492  nmodules++;
493  } else {
494  std::pair<align::GlobalPoint, align::GlobalPoint> sensorpositions = this->getDoubleSensorPosition(ali);
495  x0 += this->getX(it.sysdeformation_, sensorpositions.first, phase) +
496  this->getX(it.sysdeformation_, sensorpositions.second, phase);
497  nmodules++;
498  nmodules++;
499  }
500  }
501  }
502  if (nmodules > 0) {
503  x0 = x0 / nmodules;
504  } else {
505  throw cms::Exception("Alignment") << "@SUB=PedeSteererWeakModeConstraints::ConstructConstraints"
506  << " Number of selected modules equal to zero. Check configuration!";
507  x0 = 1.0;
508  }
509  return x0;
510 }
511 
512 //_________________________________________________________________________
514  //FIXME: split the code of the method into smaller pieces/submethods
515 
516  //create the data structures that store the alignables
517  //for which the constraints need to be calculated and
518  //their association to high-level structures
519  const auto nConstraints = this->createAlignablesDataStructure();
520 
521  std::vector<std::list<std::pair<unsigned int, double> > > createdConstraints;
522 
523  //calculate constraints
524  //loop over all constraints
525  for (const auto& it : ConstraintsConfigContainer_) {
526  //loop over all subdets for which constraints are determined
527  for (const auto& iHLS : it.HLSsubdets_) {
528  double sum_xi_x0 = 0.0;
529  std::list<std::pair<unsigned int, double> > output;
530 
531  const double x0 = this->getX0(iHLS, it);
532 
533  for (std::list<Alignable*>::const_iterator iAlignables = iHLS.second.begin(); iAlignables != iHLS.second.end();
534  iAlignables++) {
535  const Alignable* ali = (*iAlignables);
536  const auto aliLabel = myLabels_->alignableLabelFromParamAndInstance(ali, 0, it.instance_);
537  const AlignableSurface& surface = ali->surface();
538 
539  const LocalPoint lUDirection(1., 0., 0.), lVDirection(0., 1., 0.), lWDirection(0., 0., 1.);
540 
541  GlobalPoint gUDirection = surface.toGlobal(lUDirection), gVDirection = surface.toGlobal(lVDirection),
542  gWDirection = surface.toGlobal(lWDirection);
543 
544  const bool isDoubleSensor = ali->alignmentParameters()->type() == AlignmentParametersFactory::kTwoBowedSurfaces;
545 
546  const auto sensorpositions = isDoubleSensor ? this->getDoubleSensorPosition(ali)
547  : std::make_pair(ali->globalPosition(), align::PositionType());
548 
549  const auto& pos_sensor0 = sensorpositions.first;
550  const auto& pos_sensor1 = sensorpositions.second;
551  const auto phase = this->getPhase(it.coefficients_);
552  const auto x_sensor0 = this->getX(it.sysdeformation_, pos_sensor0, phase);
553  const auto x_sensor1 = isDoubleSensor ? this->getX(it.sysdeformation_, pos_sensor1, phase) : 0.0;
554 
555  sum_xi_x0 += (x_sensor0 - x0) * (x_sensor0 - x0);
556  if (isDoubleSensor) {
557  sum_xi_x0 += (x_sensor1 - x0) * (x_sensor1 - x0);
558  }
559  const int numparameterlimit = ali->alignmentParameters()->size(); //isDoubleSensor ? 18 : 3;
560 
561  for (int iParameter = 0; iParameter < numparameterlimit; iParameter++) {
562  int localindex = 0;
563  if (iParameter == 0 || iParameter == 9)
564  localindex = 0;
565  if (iParameter == 1 || iParameter == 10)
566  localindex = 1;
567  if (iParameter == 2 || iParameter == 11)
568  localindex = 2;
569 
570  if ((iParameter >= 0 && iParameter <= 2) || (iParameter >= 9 && iParameter <= 11)) {
571  } else {
572  continue;
573  }
574  if (!this->checkSelectionShiftParameter(ali, iParameter)) {
575  continue;
576  }
577  //do it for each 'instance' separately? -> IOV-dependence, no
578  const auto paramLabel = myLabels_->parameterLabel(aliLabel, iParameter);
579 
580  const auto& pos = (iParameter <= 2) ? pos_sensor0 : pos_sensor1;
581  //select only u,v,w
582  if (iParameter == 0 || iParameter == 1 || iParameter == 2 || iParameter == 9 || iParameter == 10 ||
583  iParameter == 11) {
584  const double coeff = this->getCoefficient(
585  it.sysdeformation_, pos, gUDirection, gVDirection, gWDirection, localindex, x0, it.coefficients_);
586  if (TMath::Abs(coeff) > 0.0) {
587  //nothing
588  } else {
589  edm::LogWarning("PedeSteererWeakModeConstraints")
590  << "@SUB=PedeSteererWeakModeConstraints::getCoefficient"
591  << "Coefficient of alignable " << alignableObjectId_.idToString(ali->alignableObjectId()) << " at ("
592  << ali->globalPosition().x() << "," << ali->globalPosition().y() << "," << ali->globalPosition().z()
593  << ") "
594  << " in subdet " << alignableObjectId_.idToString(iHLS.first->alignableObjectId())
595  << " for parameter " << localindex << " equal to zero. This alignable is used in the constraint"
596  << " '" << it.constraintName_
597  << "'. The id is: alignable->geomDetId().rawId() = " << ali->geomDetId().rawId() << ".";
598  }
599  output.push_back(std::make_pair(paramLabel, coeff));
600  }
601  }
602  }
603 
604  if (std::find(createdConstraints.begin(), createdConstraints.end(), output) != createdConstraints.end()) {
605  // check if linearly dependent constraint exists already:
606  auto outFile = getFile(it, iHLS.first);
607  if (outFile == nullptr) {
608  throw cms::Exception("FileFindError") << "[PedeSteererWeakModeConstraints] Cannot find output file.";
609  } else {
610  *outFile << "! The constraint for this IOV/momentum range" << std::endl
611  << "! has been removed because the used parameters" << std::endl
612  << "! are not IOV or momentum-range dependent." << std::endl;
613  }
614  continue;
615  }
616  this->writeOutput(output, it, iHLS.first, sum_xi_x0);
617  createdConstraints.push_back(output);
618  }
619  }
620  this->closeOutputfiles();
621 
622  return nConstraints;
623 }
624 
625 //_________________________________________________________________________
626 bool PedeSteererWeakModeConstraints::checkMother(const Alignable* const lowleveldet, const Alignable* const HLS) const {
627  if (lowleveldet->id() == HLS->id() && lowleveldet->alignableObjectId() == HLS->alignableObjectId()) {
628  return true;
629  } else {
630  if (lowleveldet->mother() == nullptr)
631  return false;
632  else
633  return this->checkMother(lowleveldet->mother(), HLS);
634  }
635 }
636 
637 //_________________________________________________________________________
639  const auto parameterNames = pset.getParameterNames();
640  for (const auto& name : parameterNames) {
641  if (name != "coefficients" && name != "deadmodules" && name != "constraint" && name != "steerFilePrefix" &&
642  name != "levels" && name != "excludedAlignables" && name != "ignoredInstances" && name != "downToLowestLevel") {
643  throw cms::Exception("BadConfig") << "@SUB=PedeSteererWeakModeConstraints::verifyParameterNames:"
644  << " Unknown parameter name '" << name << "' in PSet number " << psetnr
645  << ". Maybe a typo?";
646  }
647  }
648 }
649 
650 //_________________________________________________________________________
651 const std::vector<std::pair<Alignable*, std::string> > PedeSteererWeakModeConstraints::makeLevelsFilenames(
652  std::set<std::string>& steerFilePrefixContainer,
653  const align::Alignables& alis,
654  const std::string& steerFilePrefix) const {
655  //check whether the prefix is unique
656  if (steerFilePrefixContainer.find(steerFilePrefix) != steerFilePrefixContainer.end()) {
657  throw cms::Exception("BadConfig") << "[PedeSteererWeakModeConstraints] Steering file"
658  << " prefix '" << steerFilePrefix << "' already exists. Specify unique names!";
659  } else {
660  steerFilePrefixContainer.insert(steerFilePrefix);
661  }
662 
663  std::vector<std::pair<Alignable*, std::string> > levelsFilenames;
664  for (const auto& ali : alis) {
665  std::stringstream n;
666  n << steerFile_ << "_" << steerFilePrefix //<< "_" << name
667  << "_" << alignableObjectId_.idToString(ali->alignableObjectId()) << "_" << ali->id() << "_"
668  << ali->alignableObjectId() << ".txt";
669 
670  levelsFilenames.push_back(std::make_pair(ali, n.str()));
671  }
672  return levelsFilenames;
673 }
674 
675 //_________________________________________________________________________
677  const std::vector<double>& coefficients) const {
678  int sysdeformation = SystematicDeformations::kUnknown;
679 
680  if (name == "twist") {
681  sysdeformation = SystematicDeformations::kTwist;
682  } else if (name == "zexpansion") {
683  sysdeformation = SystematicDeformations::kZexpansion;
684  } else if (name == "sagitta") {
685  sysdeformation = SystematicDeformations::kSagitta;
686  } else if (name == "radial") {
687  sysdeformation = SystematicDeformations::kRadial;
688  } else if (name == "telescope") {
689  sysdeformation = SystematicDeformations::kTelescope;
690  } else if (name == "layerrotation") {
691  sysdeformation = SystematicDeformations::kLayerRotation;
692  } else if (name == "bowing") {
693  sysdeformation = SystematicDeformations::kBowing;
694  } else if (name == "skew") {
695  sysdeformation = SystematicDeformations::kSkew;
696  } else if (name == "elliptical") {
697  sysdeformation = SystematicDeformations::kElliptical;
698  }
699 
700  if (sysdeformation == SystematicDeformations::kUnknown) {
701  throw cms::Exception("BadConfig") << "[PedeSteererWeakModeConstraints]"
702  << " specified configuration option '" << name << "' not known.";
703  }
704  if ((sysdeformation == SystematicDeformations::kSagitta || sysdeformation == SystematicDeformations::kElliptical ||
705  sysdeformation == SystematicDeformations::kSkew) &&
706  coefficients.size() != 2) {
707  throw cms::Exception("BadConfig") << "[PedeSteererWeakModeConstraints]"
708  << " Excactly two parameters using the coefficient"
709  << " variable have to be provided for the " << name << " constraint.";
710  }
711  if ((sysdeformation == SystematicDeformations::kTwist || sysdeformation == SystematicDeformations::kZexpansion ||
712  sysdeformation == SystematicDeformations::kTelescope ||
713  sysdeformation == SystematicDeformations::kLayerRotation || sysdeformation == SystematicDeformations::kRadial ||
714  sysdeformation == SystematicDeformations::kBowing) &&
715  coefficients.size() != 1) {
716  throw cms::Exception("BadConfig") << "[PedeSteererWeakModeConstraints]"
717  << " Excactly ONE parameter using the coefficient"
718  << " variable have to be provided for the " << name << " constraint.";
719  }
720 
721  if (coefficients.empty()) {
722  throw cms::Exception("BadConfig") << "[PedeSteererWeakModeConstraints]"
723  << " At least one coefficient has to be specified.";
724  }
725  return sysdeformation;
726 }
727 
728 //_________________________________________________________________________
729 double PedeSteererWeakModeConstraints::getPhase(const std::vector<double>& coefficients) const {
730  return coefficients.size() == 2 ? coefficients.at(1) : 0.0; //treat second parameter as phase otherwise return 0
731 }
732 
733 //_________________________________________________________________________
align::GlobalPoints toGlobal(const align::LocalPoints &) const
Return in global coord given a set of local points.
unsigned int constructConstraints(const align::Alignables &)
AlignmentParameters * alignmentParameters() const
Get the AlignmentParameters.
Definition: Alignable.h:58
Alignable * mother() const
Return pointer to container alignable (if any)
Definition: Alignable.h:91
const AlignableSurface & surface() const
Return the Surface (global position and orientation) of the object.
Definition: Alignable.h:132
static AlignableObjectId commonObjectIdProvider(const AlignableObjectId &, const AlignableObjectId &)
static PFTauRenderPlugin instance
T z() const
Definition: PV3DBase.h:61
std::ofstream * getFile(const GeometryConstraintConfigData &it, const Alignable *iHLS) const
std::pair< align::GlobalPoint, align::GlobalPoint > getDoubleSensorPosition(const Alignable *ali) const
Definition: config.py:1
void writeOutput(const std::list< std::pair< unsigned int, double > > &output, const GeometryConstraintConfigData &it, const Alignable *iHLS, double sum_xi_x0)
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:19
__host__ __device__ VT * co
Definition: prefixScan.h:47
static std::string to_string(const XMLCh *ch)
bool checkSelectionShiftParameter(const Alignable *ali, unsigned int iParameter) const
virtual unsigned int parameterLabel(unsigned int aliLabel, unsigned int parNum) const =0
returns the label for a given alignable parameter number combination
Point3DBase< Scalar, GlobalTag > PositionType
Definition: Definitions.h:28
GeometryConstraintConfigData(const std::vector< double > &co, const std::string &c, const std::vector< std::pair< Alignable *, std::string > > &alisFile, const int sd, const align::Alignables &ex, const int instance, const bool downToLowestLevel)
T x() const
Definition: PV3DBase.h:59
T y() const
Definition: PV3DBase.h:60
const PositionType & globalPosition() const
Return the global position of the object.
Definition: Alignable.h:135
int verifyDeformationName(const std::string &name, const std::vector< double > &coefficients) const
double getCoefficient(const int sysdeformation, const align::GlobalPoint &pos, const GlobalPoint gUDirection, const GlobalPoint gVDirection, const GlobalPoint gWDirection, const int iParameter, const double &x0, const std::vector< double > &constraintparameters) const
bool checkMother(const Alignable *const lowleveldet, const Alignable *const HLS) const
virtual StructureType alignableObjectId() const =0
Return the alignable type identifier.
int size(void) const
Get number of parameters.
const std::vector< double > coefficients_
PedeSteererWeakModeConstraints(AlignableTracker *aliTracker, const PedeLabelerBase *labels, const std::vector< edm::ParameterSet > &config, std::string sf)
Log< level::Info, false > LogInfo
double getPhase(const std::vector< double > &coefficients) const
align::ID id() const
Return the ID of Alignable, i.e. DetId of &#39;first&#39; component GeomDet(Unit).
Definition: Alignable.h:180
virtual int type() const =0
tell type (AlignmentParametersFactory::ParametersType - but no circular dependency) ...
const char * idToString(align::StructureType type) const
const DetId & geomDetId() const
Definition: Alignable.h:177
virtual unsigned int alignableLabelFromParamAndInstance(const Alignable *alignable, unsigned int param, unsigned int instance) const =0
constexpr uint32_t rawId() const
get the raw id
Definition: DetId.h:57
double getX(const int sysdeformation, const align::GlobalPoint &pos, const double phase) const
align::Scalar length() const
const std::vector< bool > & selector(void) const
Get alignment parameter selector vector.
const std::vector< std::pair< Alignable *, std::string > > makeLevelsFilenames(std::set< std::string > &steerFilePrefixContainer, const align::Alignables &alis, const std::string &steerFilePrefix) const
std::vector< Alignable * > Alignables
Definition: Utilities.h:31
void verifyParameterNames(const edm::ParameterSet &pset, unsigned int psetnr) const
Definition: output.py:1
Log< level::Warning, false > LogWarning
#define str(s)
std::list< GeometryConstraintConfigData > ConstraintsConfigContainer_
double getX0(const std::pair< Alignable *, std::list< Alignable *> > &iHLS, const GeometryConstraintConfigData &it) const
unsigned transform(const HcalDetId &id, unsigned transformCode)