CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_2_9_HLT1_bphpatch4/src/Alignment/CommonAlignmentAlgorithm/src/AlignmentParameterBuilder.cc

Go to the documentation of this file.
00001 
00008 // This class's header should be first:
00009 #include "Alignment/CommonAlignmentAlgorithm/interface/AlignmentParameterBuilder.h"
00010 
00011 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00012 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00013 #include "FWCore/Utilities/interface/Exception.h"
00014 
00015 #include "Alignment/CommonAlignment/interface/Alignable.h"
00016 #include "Alignment/CommonAlignment/interface/AlignmentParameters.h"
00017 
00018 #include "Alignment/CommonAlignmentParametrization/interface/AlignmentParametersFactory.h"
00019 
00020 #include "Alignment/CommonAlignmentAlgorithm/interface/AlignmentParameterSelector.h"
00021 #include "Alignment/CommonAlignmentAlgorithm/interface/SelectionUserVariables.h"
00022 
00023 #include <algorithm>
00024 
00025 using namespace AlignmentParametersFactory;
00026 
00027 //__________________________________________________________________________________________________
00028 AlignmentParameterBuilder::AlignmentParameterBuilder(AlignableTracker* alignableTracker,
00029                                                      AlignableExtras* alignableExtras) :
00030   theAlignables(),
00031   theAlignableTracker(alignableTracker),
00032   theAlignableMuon(0),
00033   theAlignableExtras(alignableExtras)
00034 {
00035 }
00036 
00037 //__________________________________________________________________________________________________
00038 AlignmentParameterBuilder::AlignmentParameterBuilder(AlignableTracker* alignableTracker, 
00039                                                      AlignableMuon* alignableMuon,
00040                                                      AlignableExtras* alignableExtras) :
00041   theAlignables(), 
00042   theAlignableTracker(alignableTracker),
00043   theAlignableMuon(alignableMuon),
00044   theAlignableExtras(alignableExtras)
00045 {
00046 }
00047 
00048 
00049 //__________________________________________________________________________________________________
00050 AlignmentParameterBuilder::AlignmentParameterBuilder(AlignableTracker* alignableTracker,
00051                                                      AlignableExtras* alignableExtras,
00052                                                      const edm::ParameterSet &pSet) :
00053   theAlignables(), 
00054   theAlignableTracker(alignableTracker),
00055   theAlignableMuon(0),
00056   theAlignableExtras(alignableExtras)
00057 {
00058   this->addAllSelections(pSet);
00059 }
00060 
00061 //__________________________________________________________________________________________________
00062 AlignmentParameterBuilder::AlignmentParameterBuilder(AlignableTracker* alignableTracker,
00063                                                      AlignableMuon* alignableMuon,
00064                                                      AlignableExtras* alignableExtras,
00065                                                      const edm::ParameterSet &pSet) :
00066   theAlignables(), 
00067   theAlignableTracker(alignableTracker),
00068   theAlignableMuon(alignableMuon),
00069   theAlignableExtras(alignableExtras)
00070 {
00071   this->addAllSelections(pSet);
00072 }
00073 
00074 //__________________________________________________________________________________________________
00075 void AlignmentParameterBuilder::addAllSelections(const edm::ParameterSet &pSet)
00076 {
00077   AlignmentParameterSelector selector(0);
00078   std::vector<std::string> selsTypes(pSet.getParameter<std::vector<std::string> >("parameterTypes"));
00079   
00080   for (unsigned int i = 0; i < selsTypes.size(); ++i) {
00081     std::vector<std::string> selSetType(selector.decompose(selsTypes[i], ','));
00082     if (selSetType.size() != 2) {
00083       throw cms::Exception("BadConfig") << "AlignmentParameterBuilder"
00084                                         << "parameterTypes should contain 2 comma separated strings"
00085                                         << ", but found '" << selsTypes[i] << "'.";
00086     }
00087     this->addSelections(pSet.getParameter<edm::ParameterSet>(selSetType[0]),
00088                         AlignmentParametersFactory::parametersType(selSetType[1]));
00089   }
00090 }
00091 
00092 //__________________________________________________________________________________________________
00093 unsigned int AlignmentParameterBuilder::addSelections(const edm::ParameterSet &pSet,
00094                                                       ParametersType parType)
00095 {
00096 
00097   const unsigned int oldAliSize = theAlignables.size();
00098 
00099   AlignmentParameterSelector selector( theAlignableTracker, theAlignableMuon, theAlignableExtras );
00100   const unsigned int addedSets = selector.addSelections(pSet);
00101 
00102   const align::Alignables &alignables = selector.selectedAlignables();
00103   const std::vector<std::vector<char> > &paramSels = selector.selectedParameters();
00104 
00105   align::Alignables::const_iterator iAli = alignables.begin();
00106   std::vector<std::vector<char> >::const_iterator iParamSel = paramSels.begin();
00107   unsigned int nHigherLevel = 0;
00108 
00109   while (iAli != alignables.end() && iParamSel != paramSels.end()) {
00110     std::vector<bool> boolParSel;
00111     std::vector<char> parSel(*iParamSel); // copy, since decodeParamSel may manipulate
00112     bool charSelIsGeneral = this->decodeParamSel(parSel, boolParSel);
00113     if (this->add(*iAli, parType, boolParSel)) ++nHigherLevel;
00114     if (charSelIsGeneral) this->addFullParamSel((*iAli)->alignmentParameters(), parSel);
00115 
00116     ++iAli;
00117     ++iParamSel;
00118   }
00119 
00120   edm::LogInfo("Alignment") << "@SUB=AlignmentParameterBuilder::addSelections"
00121                             << " Added " << addedSets << " set(s) of alignables with "
00122                             << theAlignables.size() - oldAliSize << " alignables in total,"
00123                             << " of which " << nHigherLevel << " are higher level "
00124                             << "(using " << parametersTypeName(parType) << "AlignmentParameters).";
00125    
00126   return addedSets;
00127 }
00128 
00129 //__________________________________________________________________________________________________
00130 bool AlignmentParameterBuilder::add(Alignable *alignable,
00131                                     ParametersType parType,
00132                                     const std::vector<bool> &sel)
00133 { 
00134   AlignmentParameters *paras = AlignmentParametersFactory::createParameters(alignable, parType, sel);
00135   alignable->setAlignmentParameters(paras);
00136   theAlignables.push_back(alignable);
00137 
00138   const int aliTypeId = alignable->alignableObjectId();
00139   const bool isHigherLevel = (aliTypeId != align::AlignableDet
00140                               && aliTypeId != align::AlignableDetUnit);
00141   return isHigherLevel;
00142 }
00143 
00144 
00145 //__________________________________________________________________________________________________
00146 unsigned int AlignmentParameterBuilder::add(const align::Alignables &alignables,
00147                                             ParametersType parType, const std::vector<bool> &sel)
00148 {
00149 
00150   unsigned int nHigherLevel = 0;
00151 
00152   for (align::Alignables::const_iterator iAli = alignables.begin();
00153        iAli != alignables.end(); ++iAli) {
00154     if (this->add(*iAli, parType, sel)) ++nHigherLevel;
00155   }
00156 
00157   return nHigherLevel;
00158 }
00159 
00160 
00161 //__________________________________________________________________________________________________
00162 void AlignmentParameterBuilder::fixAlignables(int n)
00163 {
00164 
00165   if (n<1 || n>3) {
00166     edm::LogError("BadArgument") << " n = " << n << " is not in [1,3]";
00167     return;
00168   }
00169 
00170   align::Alignables theNewAlignables;
00171   int i=0;
00172   int imax = theAlignables.size();
00173   for ( align::Alignables::const_iterator ia=theAlignables.begin();
00174         ia!=theAlignables.end();  ia++ ) 
00175         {
00176           i++;
00177           if ( n==1 && i>1 ) 
00178                 theNewAlignables.push_back(*ia);
00179           else if ( n==2 && i>1 && i<imax ) 
00180                 theNewAlignables.push_back(*ia);
00181           else if ( n==3 && i>2 && i<imax) 
00182                 theNewAlignables.push_back(*ia);
00183         }
00184 
00185   theAlignables = theNewAlignables;
00186 
00187   edm::LogInfo("Alignment") << "@SUB=AlignmentParameterBuilder::fixAlignables"
00188                             << "removing " << n << " alignables, so that " 
00189                             << theAlignables.size() << " alignables left";
00190 }
00191 
00192 //__________________________________________________________________________________________________
00193 bool AlignmentParameterBuilder::decodeParamSel(std::vector<char> &paramSelChar,
00194                                                std::vector<bool> &result) const
00195 {
00196   result.clear();
00197   // remove all spaces from paramSelChar - useful to group the parameters if they are many
00198   paramSelChar.erase(std::remove(paramSelChar.begin(), paramSelChar.end(), ' '),
00199                      paramSelChar.end());
00200 
00201   bool anyNon01 = false;
00202 
00203   for (unsigned int pos = 0; pos < paramSelChar.size(); ++pos) {
00204 
00205     switch (paramSelChar[pos]) {
00206     default:
00207       anyNon01 = true;
00208       // no break;
00209     case '1':
00210       result.push_back(true);
00211       break;
00212     case '0':
00213       result.push_back(false);
00214       break;
00215     }
00216   }
00217 
00218   return anyNon01;
00219 }
00220 
00221 //__________________________________________________________________________________________________
00222 bool AlignmentParameterBuilder::addFullParamSel(AlignmentParameters *aliParams,
00223                                                 const std::vector<char> &fullSel) const
00224 {
00225   if (!aliParams) return false;
00226 
00227   aliParams->setUserVariables(new SelectionUserVariables(fullSel));
00228 
00229   return true;
00230 }