CMS 3D CMS Logo

Public Member Functions | Protected Member Functions | Protected Attributes

MisalignmentScenarioBuilder Class Reference

Base class to build a scenario from configuration and apply to either tracker or muon. More...

#include <MisalignmentScenarioBuilder.h>

Inheritance diagram for MisalignmentScenarioBuilder:
MuonScenarioBuilder TrackerScenarioBuilder

List of all members.

Public Member Functions

virtual void applyScenario (const edm::ParameterSet &scenario)=0
 Apply misalignment scenario to the tracker (sub-system specific)
 MisalignmentScenarioBuilder ()
 Default constructor.
virtual ~MisalignmentScenarioBuilder ()
 Destructor.

Protected Member Functions

void applyMovements_ (Alignable *alignable, const edm::ParameterSet &pSet)
 Apply movements given by parameter set to given alignable.
void decodeMovements_ (const edm::ParameterSet &pSet, const std::vector< Alignable * > &alignables, const std::string &levelName)
 Decode movements defined in given parameter set for given set of alignables tagged by given name.
void decodeMovements_ (const edm::ParameterSet &pSet, const std::vector< Alignable * > &alignables)
 Decode movements defined in given parameter set for given set of alignables.
edm::ParameterSet getParameterSet_ (const std::string &name, const edm::ParameterSet &pSet) const
edm::ParameterSet getParameterSet_ (const std::string &levelName, int iComponent, const edm::ParameterSet &pSet) const
bool hasParameter_ (const std::string &name, const edm::ParameterSet &pSet) const
 Check if given parameter exists in parameter set.
virtual bool isTopLevel_ (const std::string &parameterSetName) const
 Check if given parameter is for a top-level structure.
void mergeParameters_ (edm::ParameterSet &localSet, const edm::ParameterSet &globalSet) const
 Merge two sets of parameters into one (the first argument)
virtual bool possiblyPartOf (const std::string &subStruct, const std::string &largeStruct) const
void printParameters_ (const edm::ParameterSet &pSet, const bool showPsets=false) const
 Print all parameters and values for given set.
void propagateParameters_ (const edm::ParameterSet &pSet, const std::string &globalName, edm::ParameterSet &subSet) const
 Propagate global parameters to sub-parameters.
const std::string rootName_ (const std::string &parameterSetName) const
 Get root name of a parameter set (e.g. 'Rod' in 'Rods' or 'Rod1')

Protected Attributes

std::string indent_
 Depth in hierarchy.
AlignableModifier theModifier
 Helper class for random movements.
int theModifierCounter
 Counter for applied modification.
edm::ParameterSet theScenario
 Misalignment scenario to apply (from config file)

Detailed Description

Base class to build a scenario from configuration and apply to either tracker or muon.

Date:
2012/06/30 09:36:23
Revision:
1.11
Author:
eulisse
Author:
Frederic Ronga - CERN-PH-CMG

Definition at line 21 of file MisalignmentScenarioBuilder.h.


Constructor & Destructor Documentation

MisalignmentScenarioBuilder::MisalignmentScenarioBuilder ( ) [inline]

Default constructor.

Definition at line 27 of file MisalignmentScenarioBuilder.h.

virtual MisalignmentScenarioBuilder::~MisalignmentScenarioBuilder ( ) [inline, virtual]

Destructor.

Definition at line 31 of file MisalignmentScenarioBuilder.h.

{};

Member Function Documentation

void MisalignmentScenarioBuilder::applyMovements_ ( Alignable alignable,
const edm::ParameterSet pSet 
) [protected]

Apply movements given by parameter set to given alignable.

virtual void MisalignmentScenarioBuilder::applyScenario ( const edm::ParameterSet scenario) [pure virtual]

Apply misalignment scenario to the tracker (sub-system specific)

Implemented in MuonScenarioBuilder, and TrackerScenarioBuilder.

void MisalignmentScenarioBuilder::decodeMovements_ ( const edm::ParameterSet pSet,
const std::vector< Alignable * > &  alignables 
) [protected]

Decode movements defined in given parameter set for given set of alignables.

Definition at line 24 of file MisalignmentScenarioBuilder.cc.

References AlignableObjectId::idToString(), fwlog::levelName(), and AlCaHLTBitMon_QueryRunRegistry::string.

Referenced by TrackerScenarioBuilder::applyScenario(), MuonScenarioBuilder::applyScenario(), and decodeMovements_().

{

  // first create a map with one std::vector<Alignable*> per type (=levelName)
  typedef std::map<std::string, std::vector<Alignable*> > AlignablesMap;
  AlignablesMap alisMap;
  for (std::vector<Alignable*>::const_iterator iA = alignables.begin(); iA != alignables.end(); ++iA) {
    const std::string &levelName = AlignableObjectId::idToString((*iA)->alignableObjectId());
    alisMap[levelName].push_back(*iA); // either first entry of new level or add to an old one
  }

  // Now call the more general version for each entry in the map.
  //
  // There is a hack to ensure that strip components are called in the same order
  // as in old version of TrackerScenarioBuilder (TIB,TID,TOB,TEC) while 
  // std::map seems to order alphabetically (TECEndcap,TIBHalfBarrel,TIDEndcap,TOBHalfBarrel).
  // Order matters due to random sequence. If scenarios are allowed to change
  // 'numerically', remove this comment and the lines marked with 'HACK'.
  const AlignablesMap::iterator itTec = alisMap.find("TECEndcap"); // HACK
  for (AlignablesMap::iterator it = alisMap.begin(); it != alisMap.end(); ++it) {
    if (it == itTec) continue; // HACK
    this->decodeMovements_(pSet, it->second, it->first);
  }
  if (itTec != alisMap.end()) this->decodeMovements_(pSet, itTec->second, itTec->first); // HACK
}
void MisalignmentScenarioBuilder::decodeMovements_ ( const edm::ParameterSet pSet,
const std::vector< Alignable * > &  alignables,
const std::string &  levelName 
) [protected]

Decode movements defined in given parameter set for given set of alignables tagged by given name.

Definition at line 54 of file MisalignmentScenarioBuilder.cc.

References decodeMovements_(), edm::ParameterSet::empty(), edm::ParameterSet::getParameterNames(), getParameterSet_(), edm::ParameterSet::getParameterSetNames(), indent_, LogDebug, mergeParameters_(), AlignableModifier::modify(), mergeVDriftHistosByStation::name, printParameters_(), propagateParameters_(), theModifier, and theModifierCounter.

{

  indent_ += " "; // For indented output!

  // Retrieve parameters for all components at this level
  std::ostringstream name;
  name << levelName << "s";
  edm::ParameterSet globalParameters = this->getParameterSet_( name.str(), pSet );
  if ( !globalParameters.empty() ) {
    LogDebug("PrintParameters") << indent_ << " *** " << levelName << ": found "
                                << globalParameters.getParameterNames().size() 
                                << " global parameters" << std::endl;
  }
  
  // Propagate down parameters from upper level
  this->propagateParameters_( pSet, name.str(), globalParameters );
  LogDebug("PrintParameters") << indent_ << " global parameter is now:" << std::endl;
  this->printParameters_( globalParameters, true );

  // Loop on alignables
  int iComponent = 0; // physical numbering starts at 1...
  for (std::vector<Alignable*>::const_iterator iter = alignables.begin();
       iter != alignables.end(); ++iter) {
    iComponent++;

    // Check for special parameters -> merge with global
    name.str("");
    name << levelName << iComponent;

    edm::ParameterSet localParameters = this->getParameterSet_( levelName, iComponent, pSet );
    LogDebug("PrintParameters") << indent_ << " ** " << name.str() << ": found "
                                << localParameters.getParameterNames().size() 
                                << " local parameters"  << std::endl;
    this->mergeParameters_( localParameters, globalParameters );
          
    // Retrieve and apply parameters
    LogDebug("PrintParameters")  << indent_ << " parameters to apply:" << std::endl;
    this->printParameters_( localParameters, true );
    if ( theModifier.modify( (*iter), localParameters ) ) {
      theModifierCounter++;
      LogDebug("PrintParameters") << indent_ << "Movements applied to " << name.str();
    }

    // Apply movements to components
    std::vector<std::string> parameterSetNames;
    localParameters.getParameterSetNames( parameterSetNames, true );
    if ( (*iter)->size() > 0 && parameterSetNames.size() > 0 )
      // Has components and remaining parameter sets
      this->decodeMovements_( localParameters, (*iter)->components() );
  }

  indent_ = indent_.substr( 0, indent_.length()-1 );

}
edm::ParameterSet MisalignmentScenarioBuilder::getParameterSet_ ( const std::string &  levelName,
int  iComponent,
const edm::ParameterSet pSet 
) const [protected]

Get parameter set corresponding to given levelName and iComponent from pSet, any parameter set name like <levelName><m>, <levelName><m>_<n>, <levelName><m>_<n>_<o> etc. is accepted for iComponent == m, n or o. (returns empty parameter set if does not exist).

Definition at line 233 of file MisalignmentScenarioBuilder.cc.

References Exception, edm::ParameterSet::getParameter(), edm::ParameterSet::getParameterNames(), indent_, LogDebug, pos, query::result, and AlCaHLTBitMon_QueryRunRegistry::string.

{
  edm::ParameterSet result;
  unsigned int nFittingPsets = 0;

  // Get list of parameter set names and look for requested one
  std::vector<std::string> pNames = pSet.getParameterNames();
  for (std::vector<std::string>::iterator iter = pNames.begin(); iter != pNames.end(); ++iter) {
    if (iter->find(levelName) != 0) continue; // parameter not starting with levelName

    const std::string numberString(*iter, levelName.size());
    //    if (numberString.empty() || numberString == "s") { // "s" only left means we have e.g. 'TOBs' 
    if (numberString.empty()) { // check on "s" not needed, see below
      continue;  // nothing left in levelName to be iComponent...
    }
    // now look for numbers (separated by '_', tolerating '__' or ending with '_')
    size_t lastPos = 0;
    size_t pos     = numberString.find_first_of('_', lastPos);
    while (std::string::npos != pos || std::string::npos != lastPos) {
      const std::string digit(numberString.substr(lastPos, pos - lastPos));

      bool isDigit = !digit.empty();
      for (std::string::const_iterator dIt = digit.begin(); dIt != digit.end(); ++dIt) {
        if (!isdigit(*dIt)) isDigit = false; // check all 'letters' to be a digit
      }
      if (!isDigit) {
        if (lastPos != 0) { // do not throw if e.g. after 'TOB' ('Det') you find only 's' (Unit<n>)
          throw cms::Exception("BadConfig") << "[MisalignmentScenarioBuilder::getParameterSet_] "
                                            << "Expect only numbers, separated by '_' after " 
                                            << levelName << " in " << *iter << std::endl;
        }
        break;
      }

      if (atoi(digit.c_str()) == iComponent) {
        ++nFittingPsets;
        LogDebug("getParameterSet_") << indent_ << "found " << *iter << " matching "
                                     << levelName << iComponent;
        result = pSet.getParameter<edm::ParameterSet>(*iter);
        break;
      }
      lastPos = numberString.find_first_not_of('_', pos);
      pos     = numberString.find_first_of('_', lastPos);
    }
  } // end loop on names of parameters in pSet
  
  if (nFittingPsets > 1) {
    throw cms::Exception("BadConfig") << "[MisalignmentScenarioBuilder::getParameterSet_] "
                                      << "Found " << nFittingPsets << " PSet for " 
                                      << levelName << " " << iComponent << "." << std::endl;
  }

  return result;
}
edm::ParameterSet MisalignmentScenarioBuilder::getParameterSet_ ( const std::string &  name,
const edm::ParameterSet pSet 
) const [protected]

Get parameter set corresponding to given name from pSet returns empty parameter set if does not exist)

Definition at line 215 of file MisalignmentScenarioBuilder.cc.

References edm::ParameterSet::getParameter(), hasParameter_(), mergeVDriftHistosByStation::name, and query::result.

Referenced by decodeMovements_(), MuonScenarioBuilder::extractParameters(), and mergeParameters_().

{

  edm::ParameterSet result;

  // Get list of parameter set names and retrieve requested one
  std::vector<std::string> parameterSetNames;
  if ( this->hasParameter_( name, pSet ) ) {
    result = pSet.getParameter<edm::ParameterSet>( name );
  }

  return result;
}
bool MisalignmentScenarioBuilder::hasParameter_ ( const std::string &  name,
const edm::ParameterSet pSet 
) const [protected]

Check if given parameter exists in parameter set.

Definition at line 290 of file MisalignmentScenarioBuilder.cc.

References spr::find(), edm::ParameterSet::getParameterNames(), mergeVDriftHistosByStation::name, and cscdqm::h::names.

Referenced by TrackerScenarioBuilder::applyScenario(), MuonScenarioBuilder::applyScenario(), and getParameterSet_().

{

  // Get list of parameter set names and look for requested one
  std::vector<std::string> names = pSet.getParameterNames();

  return ( std::find( names.begin(), names.end(), name ) != names.end() );

}
bool MisalignmentScenarioBuilder::isTopLevel_ ( const std::string &  parameterSetName) const [protected, virtual]

Check if given parameter is for a top-level structure.

Reimplemented in TrackerScenarioBuilder.

Definition at line 343 of file MisalignmentScenarioBuilder.cc.

References dbtoconf::root, rootName_(), and AlCaHLTBitMon_QueryRunRegistry::string.

Referenced by propagateParameters_().

{
  // Get root name (strip last character[s])
  std::string root = this->rootName_( parameterSetName );

  // tracker stuff treated in overwriting TrackerScenarioBuilder::isTopLevel_(..) 
  if ( root == "DTSector" ) return true;
  else if ( root == "CSCSector" ) return true;
  else if ( root == "Muon" ) return true;

  return false;

}
void MisalignmentScenarioBuilder::mergeParameters_ ( edm::ParameterSet localSet,
const edm::ParameterSet globalSet 
) const [protected]

Merge two sets of parameters into one (the first argument)

Definition at line 117 of file MisalignmentScenarioBuilder.cc.

References edm::ParameterSet::addParameter(), edm::ParameterSet::copyFrom(), edm::ParameterSet::empty(), edm::ParameterSet::existsAs(), edm::ParameterSet::getParameter(), edm::ParameterSet::getParameterNames(), getParameterSet_(), and indent_.

Referenced by decodeMovements_(), and propagateParameters_().

{

  indent_ += " ";

  // Loop on globalSet. Add to localSet all non-existing parameters
  std::vector<std::string> globalParameterNames = globalSet.getParameterNames();
  for ( std::vector<std::string>::iterator iter = globalParameterNames.begin();
        iter != globalParameterNames.end(); iter ++ ) {

    if (globalSet.existsAs<edm::ParameterSet>(*iter)) {
      // This is a parameter set: check it
      edm::ParameterSet subLocalSet = this->getParameterSet_( (*iter), localSet );
      if ( subLocalSet.empty() ) {
        // No local subset exists: just insert it
        localSet.copyFrom(globalSet, (*iter));
      } else {
        // Merge with local subset and replace
        this->mergeParameters_( subLocalSet, globalSet.getParameter<edm::ParameterSet>(*iter) );
        localSet.addParameter<edm::ParameterSet>( (*iter), subLocalSet );
      }
    } else {
      // If (*iter) exists, (silently...) not replaced:
      localSet.copyFrom(globalSet, (*iter));
    }
  }

  indent_ = indent_.substr( 0, indent_.length()-1 );

}
bool MisalignmentScenarioBuilder::possiblyPartOf ( const std::string &  subStruct,
const std::string &  largeStruct 
) const [protected, virtual]

Check whether structure 'subStruct' could be part of 'largeStruct', defaults to true, but can be overwritten in derived classes to speed up recursive merging.

Reimplemented in TrackerScenarioBuilder.

Definition at line 358 of file MisalignmentScenarioBuilder.cc.

Referenced by propagateParameters_().

{
  return true; // possibly overwrite in specific class
}
void MisalignmentScenarioBuilder::printParameters_ ( const edm::ParameterSet pSet,
const bool  showPsets = false 
) const [protected]

Print all parameters and values for given set.

Definition at line 304 of file MisalignmentScenarioBuilder.cc.

References edm::ParameterSet::existsAs(), and edm::ParameterSet::getParameterNames().

Referenced by decodeMovements_().

{

  std::vector<std::string> parameterNames = pSet.getParameterNames();
  for ( std::vector<std::string>::iterator iter = parameterNames.begin();
        iter != parameterNames.end(); iter++ ) {
    if (showPsets || !pSet.existsAs<edm::ParameterSet>(*iter)) {
//       LogTrace("PrintParameters") << indent_ << "   " << (*iter) << " = " 
//                                << pSet.retrieve( *iter ).toString() << std::endl;
// From Bill Tannenbaum:
// You can use
//   pset.getParameterAsString(aString).
// This function was added with the new tag.
//   However, there is a possible complication if the parameter in question is
//   itself a ParameterSet or a vector of ParameterSets.  In the new format, a
//   ParameterSet cannot be converted to a string until its ID is calculated,
//   which happens when it is registered.  So, if you get error messages about
//   not being able to convert an unregistered ParameterSet to a string, you can
//   do one of two things:
// A) You can use ParameterSet::dump() to print the parameter set, instead of
//    getParameterAsString().  This does not require registering.  I'm not sure of
//    the exact format of the dump output (Rick wrote this, I think).
// OR
// B) You can use ParameterSet::registerIt() to register the parameter set
//    before calling getParameterAsString().
//
// In either case, you can use existsAs to determine which parameters are
// themselves parameter sets or vectors of parameter sets.
//
// Note that in the new parameter set format, ParameterSet::toString() does not
// write out nested parameter sets by value.  It writes them out by
// "reference", i.e it writes the ID.
    }
  }
}
void MisalignmentScenarioBuilder::propagateParameters_ ( const edm::ParameterSet pSet,
const std::string &  globalName,
edm::ParameterSet subSet 
) const [protected]

Propagate global parameters to sub-parameters.

Definition at line 153 of file MisalignmentScenarioBuilder.cc.

References edm::ParameterSet::addParameter(), edm::ParameterSet::copyFrom(), Exception, edm::ParameterSet::getParameter(), edm::ParameterSet::getParameterNames(), edm::ParameterSet::getParameterSetNames(), indent_, align::invalid, AlignableModifier::isPropagated(), isTopLevel_(), LogDebug, mergeParameters_(), possiblyPartOf(), rootName_(), AlCaHLTBitMon_QueryRunRegistry::string, AlignableObjectId::stringToId(), and theModifier.

Referenced by decodeMovements_().

{
  indent_ += " "; // For indented output!

  // Propagate some given parameters
  std::vector<std::string> parameterNames = pSet.getParameterNames();
  for ( std::vector<std::string>::iterator iter = parameterNames.begin();
        iter != parameterNames.end(); iter++ ) {
    if ( theModifier.isPropagated( *iter ) ) { // like 'distribution', 'scale', etc.
      LogDebug("PropagateParameters") << indent_ << " - adding parameter " << (*iter) << std::endl;
      subSet.copyFrom(pSet, (*iter)); // If existing, is not replaced.
    }
  }

  // Propagate all tracked parameter sets
  std::vector<std::string> pSetNames;
  if ( pSet.getParameterSetNames( pSetNames, true ) > 0 ) {
    for ( std::vector<std::string>::const_iterator it = pSetNames.begin();
          it != pSetNames.end(); it++ ) {
      const std::string rootName = this->rootName_(*it);
      const std::string globalRoot(this->rootName_(globalName));
      if (rootName.compare(0, rootName.length(), globalRoot) == 0) {
        // Parameter for this level: skip
        LogDebug("PropagateParameters") << indent_ << " - skipping PSet " << (*it) << " from global "
                                        << globalName << std::endl;
      } else if ( this->isTopLevel_(*it) ) {
        // Top-level parameters should not be propagated
        LogDebug("PropagateParameters") << indent_  
                                        << " - skipping top-level PSet " << (*it) 
                                        << " global " << globalName << std::endl;

      } else if (!this->possiblyPartOf(*it, globalRoot)) {
        // (*it) is a part of the detector that does not fit to globalName
        LogDebug("PropagateParameters") << indent_ 
                                        << " - skipping PSet " << (*it) 
                                        << " not fitting into global " << globalName << std::endl;

      } else if ( AlignableObjectId::stringToId( rootName ) == align::invalid ) {
        // Parameter is not known!
        throw cms::Exception("BadConfig") << "Unknown parameter set name " << rootName;
      } else {
        // Pass down any other: in order to merge PSets, create dummy PSet
        // only containing this PSet and merge it recursively.
        LogDebug("PropagateParameters") << indent_ << " - adding PSet " << (*it) 
                                        << " global " << globalName << std::endl;
        edm::ParameterSet m_subSet;
        m_subSet.addParameter<edm::ParameterSet>( (*it), 
                                                  pSet.getParameter<edm::ParameterSet>(*it) );
        this->mergeParameters_( subSet, m_subSet );
      }  
    }
  }

  indent_ = indent_.substr( 0, indent_.length()-1 );
}
const std::string MisalignmentScenarioBuilder::rootName_ ( const std::string &  parameterSetName) const [protected]

Get root name of a parameter set (e.g. 'Rod' in 'Rods' or 'Rod1')

Definition at line 367 of file MisalignmentScenarioBuilder.cc.

References LogDebug, query::result, and AlCaHLTBitMon_QueryRunRegistry::string.

Referenced by isTopLevel_(), TrackerScenarioBuilder::isTopLevel_(), and propagateParameters_().

{

  std::string result = parameterSetName; // Initialise to full string
  
  // Check if string ends with 's'
  const int lastChar = parameterSetName.length()-1;
  if ( parameterSetName[lastChar] == 's' ) {
    result =  parameterSetName.substr( 0, lastChar );
  } else {
    // Otherwise, look for numbers (assumes names have no numbers inside...)
    for ( unsigned int ichar = 0; ichar<parameterSetName.length(); ichar++ ) {
      if ( isdigit(parameterSetName[ichar]) ) {
        result = parameterSetName.substr( 0, ichar );
        break; // Stop at first digit
      }
    }
  }

  LogDebug("PrintParameters") << "Name was " << parameterSetName << ", root is " << result;

  return result;

}

Member Data Documentation

std::string MisalignmentScenarioBuilder::indent_ [mutable, protected]

Depth in hierarchy.

Definition at line 90 of file MisalignmentScenarioBuilder.h.

Referenced by decodeMovements_(), getParameterSet_(), mergeParameters_(), and propagateParameters_().

Counter for applied modification.

Definition at line 88 of file MisalignmentScenarioBuilder.h.

Referenced by TrackerScenarioBuilder::applyScenario(), MuonScenarioBuilder::applyScenario(), and decodeMovements_().

Misalignment scenario to apply (from config file)

Definition at line 85 of file MisalignmentScenarioBuilder.h.

Referenced by MuonScenarioBuilder::applyScenario().