00001
00002
00003
00004
00005
00006
00007
00008
00009 #include <string>
00010 #include <iostream>
00011 #include <sstream>
00012 #include <stdlib.h>
00013
00014
00015 #include "FWCore/Utilities/interface/Exception.h"
00016 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00017
00018
00019 #include "Alignment/CommonAlignment/interface/MisalignmentScenarioBuilder.h"
00020 #include "Alignment/CommonAlignment/interface/Alignable.h"
00021
00022
00023
00024 void MisalignmentScenarioBuilder::decodeMovements_(const edm::ParameterSet &pSet,
00025 const std::vector<Alignable*> &alignables)
00026 {
00027
00028
00029 typedef std::map<std::string, std::vector<Alignable*> > AlignablesMap;
00030 AlignablesMap alisMap;
00031 for (std::vector<Alignable*>::const_iterator iA = alignables.begin(); iA != alignables.end(); ++iA) {
00032 const std::string &levelName = AlignableObjectId::idToString((*iA)->alignableObjectId());
00033 alisMap[levelName].push_back(*iA);
00034 }
00035
00036
00037
00038
00039
00040
00041
00042
00043 const AlignablesMap::iterator itTec = alisMap.find("TECEndcap");
00044 for (AlignablesMap::iterator it = alisMap.begin(); it != alisMap.end(); ++it) {
00045 if (it == itTec) continue;
00046 this->decodeMovements_(pSet, it->second, it->first);
00047 }
00048 if (itTec != alisMap.end()) this->decodeMovements_(pSet, itTec->second, itTec->first);
00049 }
00050
00051
00052
00053
00054 void MisalignmentScenarioBuilder::decodeMovements_(const edm::ParameterSet &pSet,
00055 const std::vector<Alignable*> &alignables,
00056 const std::string &levelName)
00057 {
00058
00059 indent_ += " ";
00060
00061
00062 std::ostringstream name;
00063 name << levelName << "s";
00064 edm::ParameterSet globalParameters = this->getParameterSet_( name.str(), pSet );
00065 if ( !globalParameters.empty() ) {
00066 LogDebug("PrintParameters") << indent_ << " *** " << levelName << ": found "
00067 << globalParameters.getParameterNames().size()
00068 << " global parameters" << std::endl;
00069 }
00070
00071
00072 this->propagateParameters_( pSet, name.str(), globalParameters );
00073 LogDebug("PrintParameters") << indent_ << " global parameter is now:" << std::endl;
00074 this->printParameters_( globalParameters, true );
00075
00076
00077 int iComponent = 0;
00078 for (std::vector<Alignable*>::const_iterator iter = alignables.begin();
00079 iter != alignables.end(); ++iter) {
00080 iComponent++;
00081
00082
00083 name.str("");
00084 name << levelName << iComponent;
00085
00086 edm::ParameterSet localParameters = this->getParameterSet_( levelName, iComponent, pSet );
00087 LogDebug("PrintParameters") << indent_ << " ** " << name.str() << ": found "
00088 << localParameters.getParameterNames().size()
00089 << " local parameters" << std::endl;
00090 this->mergeParameters_( localParameters, globalParameters );
00091
00092
00093 LogDebug("PrintParameters") << indent_ << " parameters to apply:" << std::endl;
00094 this->printParameters_( localParameters, true );
00095 if ( theModifier.modify( (*iter), localParameters ) ) {
00096 theModifierCounter++;
00097 LogDebug("PrintParameters") << indent_ << "Movements applied to " << name.str();
00098 }
00099
00100
00101 std::vector<std::string> parameterSetNames;
00102 localParameters.getParameterSetNames( parameterSetNames, true );
00103 if ( (*iter)->size() > 0 && parameterSetNames.size() > 0 )
00104
00105 this->decodeMovements_( localParameters, (*iter)->components() );
00106 }
00107
00108 indent_ = indent_.substr( 0, indent_.length()-1 );
00109
00110 }
00111
00112
00113
00114
00115
00116
00117 void MisalignmentScenarioBuilder::mergeParameters_( edm::ParameterSet& localSet,
00118 const edm::ParameterSet& globalSet ) const
00119 {
00120
00121 indent_ += " ";
00122
00123
00124 std::vector<std::string> globalParameterNames = globalSet.getParameterNames();
00125 for ( std::vector<std::string>::iterator iter = globalParameterNames.begin();
00126 iter != globalParameterNames.end(); iter ++ ) {
00127
00128 if (globalSet.existsAs<edm::ParameterSet>(*iter)) {
00129
00130 edm::ParameterSet subLocalSet = this->getParameterSet_( (*iter), localSet );
00131 if ( subLocalSet.empty() ) {
00132
00133 localSet.copyFrom(globalSet, (*iter));
00134 } else {
00135
00136 this->mergeParameters_( subLocalSet, globalSet.getParameter<edm::ParameterSet>(*iter) );
00137 localSet.addParameter<edm::ParameterSet>( (*iter), subLocalSet );
00138 }
00139 } else {
00140
00141 localSet.copyFrom(globalSet, (*iter));
00142 }
00143 }
00144
00145 indent_ = indent_.substr( 0, indent_.length()-1 );
00146
00147 }
00148
00149
00150
00151
00152
00153 void MisalignmentScenarioBuilder::propagateParameters_( const edm::ParameterSet& pSet,
00154 const std::string& globalName,
00155 edm::ParameterSet& subSet ) const
00156 {
00157 indent_ += " ";
00158
00159
00160 std::vector<std::string> parameterNames = pSet.getParameterNames();
00161 for ( std::vector<std::string>::iterator iter = parameterNames.begin();
00162 iter != parameterNames.end(); iter++ ) {
00163 if ( theModifier.isPropagated( *iter ) ) {
00164 LogDebug("PropagateParameters") << indent_ << " - adding parameter " << (*iter) << std::endl;
00165 subSet.copyFrom(pSet, (*iter));
00166 }
00167 }
00168
00169
00170 std::vector<std::string> pSetNames;
00171 if ( pSet.getParameterSetNames( pSetNames, true ) > 0 ) {
00172 for ( std::vector<std::string>::const_iterator it = pSetNames.begin();
00173 it != pSetNames.end(); it++ ) {
00174 const std::string rootName = this->rootName_(*it);
00175 const std::string globalRoot(this->rootName_(globalName));
00176 if (rootName.compare(0, rootName.length(), globalRoot) == 0) {
00177
00178 LogDebug("PropagateParameters") << indent_ << " - skipping PSet " << (*it) << " from global "
00179 << globalName << std::endl;
00180 } else if ( this->isTopLevel_(*it) ) {
00181
00182 LogDebug("PropagateParameters") << indent_
00183 << " - skipping top-level PSet " << (*it)
00184 << " global " << globalName << std::endl;
00185
00186 } else if (!this->possiblyPartOf(*it, globalRoot)) {
00187
00188 LogDebug("PropagateParameters") << indent_
00189 << " - skipping PSet " << (*it)
00190 << " not fitting into global " << globalName << std::endl;
00191
00192 } else if ( AlignableObjectId::stringToId( rootName ) == align::invalid ) {
00193
00194 throw cms::Exception("BadConfig") << "Unknown parameter set name " << rootName;
00195 } else {
00196
00197
00198 LogDebug("PropagateParameters") << indent_ << " - adding PSet " << (*it)
00199 << " global " << globalName << std::endl;
00200 edm::ParameterSet m_subSet;
00201 m_subSet.addParameter<edm::ParameterSet>( (*it),
00202 pSet.getParameter<edm::ParameterSet>(*it) );
00203 this->mergeParameters_( subSet, m_subSet );
00204 }
00205 }
00206 }
00207
00208 indent_ = indent_.substr( 0, indent_.length()-1 );
00209 }
00210
00211
00212
00213
00214
00215 edm::ParameterSet MisalignmentScenarioBuilder::getParameterSet_( const std::string& name,
00216 const edm::ParameterSet& pSet ) const
00217 {
00218
00219 edm::ParameterSet result;
00220
00221
00222 std::vector<std::string> parameterSetNames;
00223 if ( this->hasParameter_( name, pSet ) ) {
00224 result = pSet.getParameter<edm::ParameterSet>( name );
00225 }
00226
00227 return result;
00228 }
00229
00230
00231
00232
00233 edm::ParameterSet MisalignmentScenarioBuilder::getParameterSet_( const std::string& levelName, int iComponent,
00234 const edm::ParameterSet& pSet ) const
00235 {
00236 edm::ParameterSet result;
00237 unsigned int nFittingPsets = 0;
00238
00239
00240 std::vector<std::string> pNames = pSet.getParameterNames();
00241 for (std::vector<std::string>::iterator iter = pNames.begin(); iter != pNames.end(); ++iter) {
00242 if (iter->find(levelName) != 0) continue;
00243
00244 const std::string numberString(*iter, levelName.size());
00245
00246 if (numberString.empty()) {
00247 continue;
00248 }
00249
00250 size_t lastPos = 0;
00251 size_t pos = numberString.find_first_of('_', lastPos);
00252 while (std::string::npos != pos || std::string::npos != lastPos) {
00253 const std::string digit(numberString.substr(lastPos, pos - lastPos));
00254
00255 bool isDigit = !digit.empty();
00256 for (std::string::const_iterator dIt = digit.begin(); dIt != digit.end(); ++dIt) {
00257 if (!isdigit(*dIt)) isDigit = false;
00258 }
00259 if (!isDigit) {
00260 if (lastPos != 0) {
00261 throw cms::Exception("BadConfig") << "[MisalignmentScenarioBuilder::getParameterSet_] "
00262 << "Expect only numbers, separated by '_' after "
00263 << levelName << " in " << *iter << std::endl;
00264 }
00265 break;
00266 }
00267
00268 if (atoi(digit.c_str()) == iComponent) {
00269 ++nFittingPsets;
00270 LogDebug("getParameterSet_") << indent_ << "found " << *iter << " matching "
00271 << levelName << iComponent;
00272 result = pSet.getParameter<edm::ParameterSet>(*iter);
00273 break;
00274 }
00275 lastPos = numberString.find_first_not_of('_', pos);
00276 pos = numberString.find_first_of('_', lastPos);
00277 }
00278 }
00279
00280 if (nFittingPsets > 1) {
00281 throw cms::Exception("BadConfig") << "[MisalignmentScenarioBuilder::getParameterSet_] "
00282 << "Found " << nFittingPsets << " PSet for "
00283 << levelName << " " << iComponent << "." << std::endl;
00284 }
00285
00286 return result;
00287 }
00288
00289
00290 bool MisalignmentScenarioBuilder::hasParameter_( const std::string& name,
00291 const edm::ParameterSet& pSet ) const
00292 {
00293
00294
00295 std::vector<std::string> names = pSet.getParameterNames();
00296
00297 return ( std::find( names.begin(), names.end(), name ) != names.end() );
00298
00299 }
00300
00301
00302
00303
00304 void MisalignmentScenarioBuilder::printParameters_( const edm::ParameterSet& pSet,
00305 const bool showPsets ) const
00306 {
00307
00308 std::vector<std::string> parameterNames = pSet.getParameterNames();
00309 for ( std::vector<std::string>::iterator iter = parameterNames.begin();
00310 iter != parameterNames.end(); iter++ ) {
00311 if (showPsets || !pSet.existsAs<edm::ParameterSet>(*iter)) {
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337 }
00338 }
00339 }
00340
00341
00342
00343 bool MisalignmentScenarioBuilder::isTopLevel_( const std::string& parameterSetName ) const
00344 {
00345
00346 std::string root = this->rootName_( parameterSetName );
00347
00348
00349 if ( root == "DTSector" ) return true;
00350 else if ( root == "CSCSector" ) return true;
00351 else if ( root == "Muon" ) return true;
00352
00353 return false;
00354
00355 }
00356
00357
00358 bool MisalignmentScenarioBuilder::possiblyPartOf(const std::string & , const std::string &) const
00359 {
00360 return true;
00361 }
00362
00363
00364
00365
00366 const std::string
00367 MisalignmentScenarioBuilder::rootName_( const std::string& parameterSetName ) const
00368 {
00369
00370 std::string result = parameterSetName;
00371
00372
00373 const int lastChar = parameterSetName.length()-1;
00374 if ( parameterSetName[lastChar] == 's' ) {
00375 result = parameterSetName.substr( 0, lastChar );
00376 } else {
00377
00378 for ( unsigned int ichar = 0; ichar<parameterSetName.length(); ichar++ ) {
00379 if ( isdigit(parameterSetName[ichar]) ) {
00380 result = parameterSetName.substr( 0, ichar );
00381 break;
00382 }
00383 }
00384 }
00385
00386 LogDebug("PrintParameters") << "Name was " << parameterSetName << ", root is " << result;
00387
00388 return result;
00389
00390 }