CMS 3D CMS Logo

Public Types | Public Member Functions | Private Attributes | Static Private Attributes

PedeReader Class Reference

#include <PedeReader.h>

List of all members.

Public Types

typedef PedeLabelerBase::RunNumber RunNumber
typedef PedeLabelerBase::RunRange RunRange

Public Member Functions

AlignmentParameterscheckAliParams (Alignable *alignable, bool createUserVars) const
 PedeReader (const edm::ParameterSet &config, const PedeSteerer &steerer, const PedeLabelerBase &labels, const RunRange &runrange)
bool read (std::vector< Alignable * > &alignables, bool setUserVars)
template<class T >
bool readIfSameLine (std::ifstream &aStream, T &outValue) const
bool setCalibrationParameter (IntegratedCalibrationBase *calib, unsigned int paramNum, unsigned int bufLength, const float *buf) const
 Set pede results stored in 'buf' to parameter 'paramNum' of IntegratedCalibrationBase.
AlignablesetParameter (unsigned int paramLabel, unsigned int bufLength, const float *buf, bool setUserVars) const
 ~PedeReader ()
 non virtual destructor: do not inherit from this class

Private Attributes

const PedeLabelerBasemyLabels
std::ifstream myPedeResult
const RunRange myRunRange
const PedeSteerermySteerer

Static Private Attributes

static const unsigned int myMaxNumValPerParam = 5

Detailed Description

read in result from pede text file

Author:
: Gero Flucke date : November 2006
Revision:
1.5
Date:
2012/08/10 09:01:11

(last update by

Author:
flucke

)

Definition at line 33 of file PedeReader.h.


Member Typedef Documentation

Definition at line 37 of file PedeReader.h.

Definition at line 38 of file PedeReader.h.


Constructor & Destructor Documentation

PedeReader::PedeReader ( const edm::ParameterSet config,
const PedeSteerer steerer,
const PedeLabelerBase labels,
const RunRange runrange 
)

Definition at line 33 of file PedeReader.cc.

References PedeSteerer::directory(), edm::ParameterSet::getParameter(), edm::ParameterSet::getUntrackedParameter(), recoMuon::in, myPedeResult, and AlCaHLTBitMon_QueryRunRegistry::string.

  : mySteerer(steerer), myLabels(labels), myRunRange(runrange)
{
  std::string pedeResultFile(config.getUntrackedParameter<std::string>("fileDir"));
  if (pedeResultFile.empty()) pedeResultFile = steerer.directory(); // includes final '/'
  else if (pedeResultFile.find_last_of('/') != pedeResultFile.size() - 1) {
    pedeResultFile += '/'; // directory may need '/'
  }

  pedeResultFile += config.getParameter<std::string>("readFile");
  myPedeResult.open(pedeResultFile.c_str(), std::ios::in);
  if (!myPedeResult.is_open()) {
    edm::LogError("Alignment") << "@SUB=PedeReader"
                               << "Problem opening pede output file " << pedeResultFile;
  }
}
PedeReader::~PedeReader ( ) [inline]

non virtual destructor: do not inherit from this class

Definition at line 43 of file PedeReader.h.

{}

Member Function Documentation

AlignmentParameters * PedeReader::checkAliParams ( Alignable alignable,
bool  createUserVars 
) const

returns parameters of alignable (creates if not yet existing, but MillePedeVariables are only created if createUserVars == true)

Definition at line 258 of file PedeReader.cc.

References PedeLabelerBase::alignableLabel(), Alignable::alignmentParameters(), Exception, myLabels, AlignmentParameters::setUserVariables(), AlignmentParameters::size(), and AlignmentParameters::userVariables().

Referenced by setParameter().

{
  // first check that we have parameters
  AlignmentParameters *params = alignable->alignmentParameters();
  if (!params) {
    throw cms::Exception("BadConfig") << "PedeReader::checkAliParams: "
                                      << "Alignable without parameters.";

  }
  
  // now check that we have user parameters of correct type if requested:
  if (createUserVars && !dynamic_cast<MillePedeVariables*>(params->userVariables())) {
    edm::LogInfo("Alignment") << "@SUB=PedeReader::checkAliParams"
                              << "Add user variables for alignable with label " 
                              << myLabels.alignableLabel(alignable);
    params->setUserVariables(new MillePedeVariables(params->size(), myLabels.alignableLabel(alignable)));
  }
  
  return params;
}
bool PedeReader::read ( std::vector< Alignable * > &  alignables,
bool  setUserVars 
)

Read pede output into AlignmentParameters attached to 'alignables' (if they fit to the run range). If (setUserVars == true) also care about MillePedeVariables. Also treats parameters belonging to a IntegratedCalibrationBase.

Definition at line 52 of file PedeReader.cc.

References PedeLabelerBase::calibrationParamFromLabel(), myLabels, myMaxNumValPerParam, myPedeResult, myRunRange, PedeLabelerBase::runRangeFromLabel(), setCalibrationParameter(), and setParameter().

Referenced by MillePedeAlignmentAlgorithm::readFromPede().

{
  alignables.clear();
  myPedeResult.seekg(0, std::ios::beg); // back to start
  bool isAllOk = true;

  std::map<Alignable*,Alignable*> uniqueList; // Probably should use a std::set here...
  
  edm::LogInfo("Alignment") << "@SUB=PedeReader::read"
                            << "will read parameters for run range "
                            << myRunRange.first << " - " << myRunRange.second;
  
  // loop on lines of text file
  unsigned int nParam = 0, nParamCalib = 0;
  while (myPedeResult.good() && !myPedeResult.eof()) {
    // read label
    unsigned int paramLabel = 0;
    if (!this->readIfSameLine<unsigned int>(myPedeResult, paramLabel)) continue; // empty line?

    // read up to maximal number of pede result per parameter
    float buffer[myMaxNumValPerParam] = {0.};
    unsigned int bufferPos = 0;
    for ( ; bufferPos < myMaxNumValPerParam; ++bufferPos) {
      if (!this->readIfSameLine<float>(myPedeResult, buffer[bufferPos])) break;
    }
    
    // First check whether parameter is from any calibration (to be done before RunRange check:
    // run dependence for them is not handled here, but probably inside the calibration).
    // Double setting by calling read(..) twice for different RunRanges shouldn't harm.
    std::pair<IntegratedCalibrationBase*, unsigned int> calibParam
      = myLabels.calibrationParamFromLabel(paramLabel);
    if (calibParam.first) { // label belongs to a calibration
      if (this->setCalibrationParameter(calibParam.first, calibParam.second, bufferPos, buffer)) {
        ++nParamCalib;
      } else {
        edm::LogError("Alignment") << "@SUB=PedeReader::read" << "Problems setting results of "
                                   << "parameter " << calibParam.second << " to calibration '"
                                   << calibParam.first->name() << "' ("<< calibParam.first << ").";
        isAllOk = false;
      }
      continue; // does not belong to any Alignable, so go to next line of file 
    }
    // Now treat Alignables if paramLabel fits to run range, otherwise skip line:
    const RunRange & runRange = myLabels.runRangeFromLabel(paramLabel);
    if (!(runRange.first<=myRunRange.first && myRunRange.second<=runRange.second)) continue;
    
    Alignable *alignable = this->setParameter(paramLabel, bufferPos, buffer, setUserVars);
    if (!alignable) {
      isAllOk = false;  // or error?
      continue;
    }
    uniqueList[alignable] = alignable;
    ++nParam;
  }

  // add Alignables to output
  for ( std::map<Alignable*,Alignable*>::const_iterator iAli = uniqueList.begin();
        iAli != uniqueList.end(); ++iAli) {
    alignables.push_back((*iAli).first);
  }

  edm::LogInfo("Alignment") << "@SUB=PedeReader::read" << nParam << " parameters for "
                            << alignables.size() << " alignables and " << nParamCalib
                            << " for calibrations.";

  return isAllOk && (nParam + nParamCalib); // nParam+nParamCalib == 0: empty or bad file
}
template<class T >
bool PedeReader::readIfSameLine ( std::ifstream &  aStream,
T outValue 
) const

true if 'outValue' could be read via operator >> from the current line (!) of aStream, false otherwise

Definition at line 123 of file PedeReader.cc.

{

  while (true) {
    const int aChar = aStream.get();
    if (!aStream.good()) return false;

    switch(aChar) {
    case ' ':
    case '\t':
      continue; // to next character
    case '\n':
      return false; // end of line
    default:
      aStream.unget();
      aStream >> outValue;
      if (aStream.fail()) {// not correct type 'T' (!aStream.good() is true also in case of EOF)
        aStream.clear();
        while (aStream.good() && aStream.get() != '\n'); // forward to end of line
        return false; 
      } else {
        return true;
      }
    } // switch
  } // while

  edm::LogError("Alignment") << "@SUB=PedeReader::readIfSameLine" << "Should never come here!";
  return false;
}
bool PedeReader::setCalibrationParameter ( IntegratedCalibrationBase calib,
unsigned int  paramNum,
unsigned int  bufLength,
const float *  buf 
) const

Set pede results stored in 'buf' to parameter 'paramNum' of IntegratedCalibrationBase.

Definition at line 228 of file PedeReader.cc.

References mySteerer, IntegratedCalibrationBase::name(), PedeSteerer::parameterSign(), IntegratedCalibrationBase::setParameter(), and IntegratedCalibrationBase::setParameterError().

Referenced by read().

{
  if (!calib || !buf) return false;

  // FIXME: Should we attach MillePedeVariables to IntegratedCalibrationBase to store
  //        'other' results beyond value and error?
  switch (bufLength) {
  case 5: // buf[4]: global correlation - not treated yet FIXME // no break;
  case 4: // uncertainty
    calib->setParameterError(paramNum, buf[3]); // no break;
  case 3: // buf[2]: difference to start value - not treated yet // no break;
  case 2:
    if (bufLength == 2 && buf[1] >= 0.) { // buf[1]: pre-sigma, < 0 means fixed
      edm::LogWarning("Alignment") << "@SUB=PedeReader::setCalibrationParameter"
                                   << "Param " << paramNum << " of calibration '"
                                   << calib->name() << "' without result!";
    }
    return calib->setParameter(paramNum, buf[0] * mySteerer.parameterSign());
  case 0:
  case 1:
  default:
    edm::LogError("Alignment") << "@SUB=PedeReader::setCalibrationParameter"
                               << "Expect 2 to 5 values, got " << bufLength << "."; 
    return false;
  }

}
Alignable * PedeReader::setParameter ( unsigned int  paramLabel,
unsigned int  bufLength,
const float *  buf,
bool  setUserVars 
) const

Set pede results stored in 'buf' to AlignmentParameters and (if setUserVars == true) to MillePedeVariables, return corresponding Alignable.

Definition at line 154 of file PedeReader.cc.

References PedeLabelerBase::alignableFromLabel(), PedeLabelerBase::alignableLabel(), checkAliParams(), AlignmentParameters::clone(), PedeSteerer::cmsToPedeFactor(), AlignmentParameters::covariance(), MillePedeVariables::diffBefore(), MillePedeVariables::globalCor(), MillePedeVariables::isFixed(), MillePedeVariables::isValid(), MillePedeVariables::label(), PedeLabelerBase::lasBeamIdFromLabel(), myLabels, mySteerer, MillePedeVariables::parameter(), AlignmentParameters::parameters(), PedeSteerer::parameterSign(), PedeLabelerBase::paramNumFromLabel(), MillePedeVariables::preSigma(), Alignable::setAlignmentParameters(), MillePedeVariables::setAllDefault(), AlignmentParameters::setValid(), MillePedeVariables::sigma(), and AlignmentParameters::userVariables().

Referenced by read().

{
  Alignable *alignable = myLabels.alignableFromLabel(paramLabel);
  const unsigned int paramNum = myLabels.paramNumFromLabel(paramLabel);
  const double cmsToPede = mySteerer.cmsToPedeFactor(paramNum);
  if (alignable) {
    AlignmentParameters *params = this->checkAliParams(alignable, setUserVars);
    MillePedeVariables *userParams = // static cast ensured by previous checkAliParams
      (setUserVars ? static_cast<MillePedeVariables*>(params->userVariables()) : 0);
    // if (userParams && userParams->label() != myLabels.alignableLabelFromLabel(paramLabel)) {
    if (userParams && userParams->label() != myLabels.alignableLabel(alignable)) {
      edm::LogError("Alignment") << "@SUB=PedeReader::setParameter" 
                                 << "Label mismatch: paramLabel " << paramLabel 
                                 << " for alignableLabel " << userParams->label();
    }

    AlgebraicVector parVec(params->parameters());
    AlgebraicSymMatrix covMat(params->covariance());

    if (userParams) userParams->setAllDefault(paramNum);

    switch (bufLength) {
    case 5: // global correlation
      if (userParams) userParams->globalCor()[paramNum] = buf[4]; // no break
    case 4: // uncertainty
      if (userParams) userParams->sigma()[paramNum] = buf[3] / cmsToPede;
      covMat[paramNum][paramNum] = buf[3]*buf[3] / (cmsToPede*cmsToPede);
      // no break;
    case 3: // difference to start value
      if (userParams) userParams->diffBefore()[paramNum] = buf[2] / cmsToPede;
      // no break
    case 2: 
      params->setValid(true);
      parVec[paramNum] = buf[0] / cmsToPede * mySteerer.parameterSign(); // parameter
      if (userParams) {
        userParams->parameter()[paramNum] = parVec[paramNum]; // duplicate in millepede parameters
        userParams->preSigma()[paramNum] = buf[1];  // presigma given, probably means fixed
        if (!userParams->isFixed(paramNum)) {
          userParams->preSigma()[paramNum] /= cmsToPede;
          if (bufLength == 2) {
            edm::LogWarning("Alignment") << "@SUB=PedeReader::setParameter"
                                         << "Param " << paramLabel << " (from "
                                         << typeid(*alignable).name() << ") without result!";
            userParams->isValid()[paramNum] = false;
            params->setValid(false);
          }
        }
      }
      break;
    case 0:
    case 1:
    default:
      edm::LogError("Alignment") << "@SUB=PedeReader::setParameter"
                                 << "Expect 2 to 5 values, got " << bufLength 
                                 << " for label " << paramLabel;
      break;
    }
    alignable->setAlignmentParameters(params->clone(parVec, covMat));//transferred mem. responsib.
  } else {
    unsigned int lasBeamId = myLabels.lasBeamIdFromLabel(paramLabel);
    edm::LogError("Alignment") << "@SUB=PedeReader::setParameter"
                               << "No alignable for paramLabel " << paramLabel
                               << ", probably LasBeam with Id " << lasBeamId
                               << ",\nparam " << paramNum << ": " 
                               << buf[0] / cmsToPede * mySteerer.parameterSign()
                               << " += " << (bufLength >= 4 ? buf[3] / cmsToPede : -99.);
  }

  return alignable;
}

Member Data Documentation

Definition at line 69 of file PedeReader.h.

Referenced by checkAliParams(), read(), and setParameter().

const unsigned int PedeReader::myMaxNumValPerParam = 5 [static, private]

Definition at line 72 of file PedeReader.h.

Referenced by read().

std::ifstream PedeReader::myPedeResult [private]

Definition at line 67 of file PedeReader.h.

Referenced by PedeReader(), and read().

Definition at line 70 of file PedeReader.h.

Referenced by read().

Definition at line 68 of file PedeReader.h.

Referenced by setCalibrationParameter(), and setParameter().