Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:44:39

0001 /** \file AlignableParameterBuilder.cc
0002  *
0003  *  $Date: 2010/10/26 20:01:52 $
0004  *  $Revision: 1.19 $
0005 
0006 */
0007 
0008 // This class's header should be first:
0009 #include "Alignment/CommonAlignmentAlgorithm/interface/AlignmentParameterBuilder.h"
0010 
0011 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0012 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0013 #include "FWCore/Utilities/interface/Exception.h"
0014 
0015 #include "Alignment/CommonAlignment/interface/Alignable.h"
0016 #include "Alignment/CommonAlignment/interface/AlignmentParameters.h"
0017 
0018 #include "Alignment/CommonAlignmentParametrization/interface/AlignmentParametersFactory.h"
0019 
0020 #include "Alignment/CommonAlignmentAlgorithm/interface/AlignmentParameterSelector.h"
0021 #include "Alignment/CommonAlignmentAlgorithm/interface/SelectionUserVariables.h"
0022 
0023 #include <algorithm>
0024 
0025 using namespace AlignmentParametersFactory;
0026 
0027 //__________________________________________________________________________________________________
0028 AlignmentParameterBuilder::AlignmentParameterBuilder(AlignableTracker *alignableTracker,
0029                                                      AlignableExtras *alignableExtras)
0030     : theAlignables(),
0031       theAlignableTracker(alignableTracker),
0032       theAlignableMuon(nullptr),
0033       theAlignableExtras(alignableExtras) {}
0034 
0035 //__________________________________________________________________________________________________
0036 AlignmentParameterBuilder::AlignmentParameterBuilder(AlignableTracker *alignableTracker,
0037                                                      AlignableMuon *alignableMuon,
0038                                                      AlignableExtras *alignableExtras)
0039     : theAlignables(),
0040       theAlignableTracker(alignableTracker),
0041       theAlignableMuon(alignableMuon),
0042       theAlignableExtras(alignableExtras) {}
0043 
0044 //__________________________________________________________________________________________________
0045 AlignmentParameterBuilder::AlignmentParameterBuilder(AlignableTracker *alignableTracker,
0046                                                      AlignableExtras *alignableExtras,
0047                                                      const edm::ParameterSet &pSet)
0048     : theAlignables(),
0049       theAlignableTracker(alignableTracker),
0050       theAlignableMuon(nullptr),
0051       theAlignableExtras(alignableExtras) {
0052   this->addAllSelections(pSet);
0053 }
0054 
0055 //__________________________________________________________________________________________________
0056 AlignmentParameterBuilder::AlignmentParameterBuilder(AlignableTracker *alignableTracker,
0057                                                      AlignableMuon *alignableMuon,
0058                                                      AlignableExtras *alignableExtras,
0059                                                      const edm::ParameterSet &pSet)
0060     : theAlignables(),
0061       theAlignableTracker(alignableTracker),
0062       theAlignableMuon(alignableMuon),
0063       theAlignableExtras(alignableExtras) {
0064   this->addAllSelections(pSet);
0065 }
0066 
0067 const AlignableTracker *AlignmentParameterBuilder::alignableTracker() const { return theAlignableTracker; }
0068 
0069 //__________________________________________________________________________________________________
0070 void AlignmentParameterBuilder::addAllSelections(const edm::ParameterSet &pSet) {
0071   AlignmentParameterSelector selector(nullptr);
0072   std::vector<std::string> selsTypes(pSet.getParameter<std::vector<std::string> >("parameterTypes"));
0073 
0074   for (unsigned int i = 0; i < selsTypes.size(); ++i) {
0075     std::vector<std::string> selSetType(selector.decompose(selsTypes[i], ','));
0076     if (selSetType.size() != 2) {
0077       throw cms::Exception("BadConfig") << "AlignmentParameterBuilder"
0078                                         << "parameterTypes should contain 2 comma separated strings"
0079                                         << ", but found '" << selsTypes[i] << "'.";
0080     }
0081     this->addSelections(pSet.getParameter<edm::ParameterSet>(selSetType[0]),
0082                         AlignmentParametersFactory::parametersType(selSetType[1]));
0083   }
0084 }
0085 
0086 //__________________________________________________________________________________________________
0087 unsigned int AlignmentParameterBuilder::addSelections(const edm::ParameterSet &pSet, ParametersType parType) {
0088   const unsigned int oldAliSize = theAlignables.size();
0089 
0090   AlignmentParameterSelector selector(theAlignableTracker, theAlignableMuon, theAlignableExtras);
0091   const unsigned int addedSets = selector.addSelections(pSet);
0092 
0093   const align::Alignables &alignables = selector.selectedAlignables();
0094   const std::vector<std::vector<char> > &paramSels = selector.selectedParameters();
0095 
0096   align::Alignables::const_iterator iAli = alignables.begin();
0097   std::vector<std::vector<char> >::const_iterator iParamSel = paramSels.begin();
0098   unsigned int nHigherLevel = 0;
0099 
0100   while (iAli != alignables.end() && iParamSel != paramSels.end()) {
0101     std::vector<bool> boolParSel;
0102     std::vector<char> parSel(*iParamSel);  // copy, since decodeParamSel may manipulate
0103     bool charSelIsGeneral = this->decodeParamSel(parSel, boolParSel);
0104     if (this->add(*iAli, parType, boolParSel))
0105       ++nHigherLevel;
0106     if (charSelIsGeneral)
0107       this->addFullParamSel((*iAli)->alignmentParameters(), parSel);
0108 
0109     ++iAli;
0110     ++iParamSel;
0111   }
0112 
0113   edm::LogInfo("Alignment") << "@SUB=AlignmentParameterBuilder::addSelections"
0114                             << " Added " << addedSets << " set(s) of alignables with "
0115                             << theAlignables.size() - oldAliSize << " alignables in total,"
0116                             << " of which " << nHigherLevel << " are higher level "
0117                             << "(using " << parametersTypeName(parType) << "AlignmentParameters).";
0118 
0119   return addedSets;
0120 }
0121 
0122 //__________________________________________________________________________________________________
0123 bool AlignmentParameterBuilder::add(Alignable *alignable, ParametersType parType, const std::vector<bool> &sel) {
0124   AlignmentParameters *paras = AlignmentParametersFactory::createParameters(alignable, parType, sel);
0125   alignable->setAlignmentParameters(paras);
0126   theAlignables.push_back(alignable);
0127 
0128   const int aliTypeId = alignable->alignableObjectId();
0129   const bool isHigherLevel = (aliTypeId != align::AlignableDet && aliTypeId != align::AlignableDetUnit);
0130   return isHigherLevel;
0131 }
0132 
0133 //__________________________________________________________________________________________________
0134 unsigned int AlignmentParameterBuilder::add(const align::Alignables &alignables,
0135                                             ParametersType parType,
0136                                             const std::vector<bool> &sel) {
0137   unsigned int nHigherLevel = 0;
0138 
0139   for (align::Alignables::const_iterator iAli = alignables.begin(); iAli != alignables.end(); ++iAli) {
0140     if (this->add(*iAli, parType, sel))
0141       ++nHigherLevel;
0142   }
0143 
0144   return nHigherLevel;
0145 }
0146 
0147 //__________________________________________________________________________________________________
0148 void AlignmentParameterBuilder::fixAlignables(int n) {
0149   if (n < 1 || n > 3) {
0150     edm::LogError("BadArgument") << " n = " << n << " is not in [1,3]";
0151     return;
0152   }
0153 
0154   align::Alignables theNewAlignables;
0155   int i = 0;
0156   int imax = theAlignables.size();
0157   for (align::Alignables::const_iterator ia = theAlignables.begin(); ia != theAlignables.end(); ++ia) {
0158     i++;
0159     if (n == 1 && i > 1)
0160       theNewAlignables.push_back(*ia);
0161     else if (n == 2 && i > 1 && i < imax)
0162       theNewAlignables.push_back(*ia);
0163     else if (n == 3 && i > 2 && i < imax)
0164       theNewAlignables.push_back(*ia);
0165   }
0166 
0167   theAlignables = theNewAlignables;
0168 
0169   edm::LogInfo("Alignment") << "@SUB=AlignmentParameterBuilder::fixAlignables"
0170                             << "removing " << n << " alignables, so that " << theAlignables.size()
0171                             << " alignables left";
0172 }
0173 
0174 //__________________________________________________________________________________________________
0175 bool AlignmentParameterBuilder::decodeParamSel(std::vector<char> &paramSelChar, std::vector<bool> &result) const {
0176   result.clear();
0177   // remove all spaces from paramSelChar - useful to group the parameters if they are many
0178   paramSelChar.erase(std::remove(paramSelChar.begin(), paramSelChar.end(), ' '), paramSelChar.end());
0179 
0180   bool anyNon01 = false;
0181 
0182   for (unsigned int pos = 0; pos < paramSelChar.size(); ++pos) {
0183     switch (paramSelChar[pos]) {
0184       default:
0185         anyNon01 = true;
0186         [[fallthrough]];
0187       case '1':
0188         result.push_back(true);
0189         break;
0190       case '0':
0191         result.push_back(false);
0192         break;
0193     }
0194   }
0195 
0196   return anyNon01;
0197 }
0198 
0199 //__________________________________________________________________________________________________
0200 bool AlignmentParameterBuilder::addFullParamSel(AlignmentParameters *aliParams,
0201                                                 const std::vector<char> &fullSel) const {
0202   if (!aliParams)
0203     return false;
0204 
0205   aliParams->setUserVariables(new SelectionUserVariables(fullSel));
0206 
0207   return true;
0208 }