CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
PedeReader.cc
Go to the documentation of this file.
1 
11 #include "PedeReader.h"
12 #include "PedeSteerer.h"
13 
16 
19 
21 
23 
24 #include <map>
25 #include <string>
26 
27 
28 const unsigned int PedeReader::myMaxNumValPerParam = 5;
29 
30 //__________________________________________________________________________________________________
32  const PedeLabelerBase &labels, const RunRange &runrange)
33  : mySteerer(steerer), myLabels(labels), myRunRange(runrange)
34 {
35  std::string pedeResultFile(config.getUntrackedParameter<std::string>("fileDir"));
36  if (pedeResultFile.empty()) pedeResultFile = steerer.directory(); // includes final '/'
37  else if (pedeResultFile.find_last_of('/') != pedeResultFile.size() - 1) {
38  pedeResultFile += '/'; // directory may need '/'
39  }
40 
41  pedeResultFile += config.getParameter<std::string>("readFile");
42  myPedeResult.open(pedeResultFile.c_str(), std::ios::in);
43  if (!myPedeResult.is_open()) {
44  edm::LogError("Alignment") << "@SUB=PedeReader"
45  << "Problem opening pede output file " << pedeResultFile;
46  }
47 }
48 
49 //__________________________________________________________________________________________________
50 bool PedeReader::read(std::vector<Alignable*> &alignables, bool setUserVars)
51 {
52  alignables.clear();
53  myPedeResult.seekg(0, std::ios::beg); // back to start
54  bool isAllOk = true;
55 
56  std::map<Alignable*,Alignable*> uniqueList; // Probably should use a std::set here...
57 
58  edm::LogInfo("Alignment") << "@SUB=PedeReader::read"
59  << "will read parameters for run range "
60  << myRunRange.first << " - " << myRunRange.second;
61 
62  // loop on lines of text file
63  unsigned int nParam = 0;
64  while (myPedeResult.good() && !myPedeResult.eof()) {
65  // read label
66  unsigned int paramLabel = 0;
67  if (!this->readIfSameLine<unsigned int>(myPedeResult, paramLabel)) continue; // empty line?
68 
69  // read up to maximal number of pede result per parameter
70  float buffer[myMaxNumValPerParam] = {0.};
71  unsigned int bufferPos = 0;
72  for ( ; bufferPos < myMaxNumValPerParam; ++bufferPos) {
73  if (!this->readIfSameLine<float>(myPedeResult, buffer[bufferPos])) break;
74  }
75 
76  const RunRange & runRange = myLabels.runRangeFromLabel(paramLabel);
77  if (!(runRange.first<=myRunRange.first && myRunRange.second<=runRange.second)) continue;
78 
79  Alignable *alignable = this->setParameter(paramLabel, bufferPos, buffer, setUserVars);
80  if (!alignable) {
81  isAllOk = false; // or error?
82  continue;
83  }
84  uniqueList[alignable] = alignable;
85  ++nParam;
86  }
87 
88  // add Alignables to output
89  for ( std::map<Alignable*,Alignable*>::const_iterator iAli = uniqueList.begin();
90  iAli != uniqueList.end(); ++iAli) {
91  alignables.push_back((*iAli).first);
92  }
93 
94  edm::LogInfo("Alignment") << "@SUB=PedeReader::read" << nParam << " parameters for "
95  << alignables.size() << " alignables";
96 
97  return isAllOk && nParam; // nParam == 0: empty or bad file
98 }
99 
100 
101 //__________________________________________________________________________________________________
102 template<class T>
103 bool PedeReader::readIfSameLine(std::ifstream &aStream, T &outValue) const
104 {
105 
106  while (true) {
107  const int aChar = aStream.get();
108  if (!aStream.good()) return false;
109 
110  switch(aChar) {
111  case ' ':
112  case '\t':
113  continue; // to next character
114  case '\n':
115  return false; // end of line
116  default:
117  aStream.unget();
118  aStream >> outValue;
119  if (aStream.fail()) {// not correct type 'T' (!aStream.good() is true also in case of EOF)
120  aStream.clear();
121  while (aStream.good() && aStream.get() != '\n'); // forward to end of line
122  return false;
123  } else {
124  return true;
125  }
126  } // switch
127  } // while
128 
129  edm::LogError("Alignment") << "@SUB=PedeReader::readIfSameLine" << "Should never come here!";
130  return false;
131 }
132 
133 //__________________________________________________________________________________________________
134 Alignable* PedeReader::setParameter(unsigned int paramLabel,
135  unsigned int bufLength, float *buf, bool setUserVars) const
136 {
137  Alignable *alignable = myLabels.alignableFromLabel(paramLabel);
138  const unsigned int paramNum = myLabels.paramNumFromLabel(paramLabel);
139  const double cmsToPede = mySteerer.cmsToPedeFactor(paramNum);
140  if (alignable) {
141  AlignmentParameters *params = this->checkAliParams(alignable, setUserVars);
142  MillePedeVariables *userParams = // static cast ensured by previous checkAliParams
143  (setUserVars ? static_cast<MillePedeVariables*>(params->userVariables()) : 0);
144  // if (userParams && userParams->label() != myLabels.alignableLabelFromLabel(paramLabel)) {
145  if (userParams && userParams->label() != myLabels.alignableLabel(alignable)) {
146  edm::LogError("Alignment") << "@SUB=PedeReader::setParameter"
147  << "Label mismatch: paramLabel " << paramLabel
148  << " for alignableLabel " << userParams->label();
149  }
150 
151  AlgebraicVector parVec(params->parameters());
152  AlgebraicSymMatrix covMat(params->covariance());
153 
154  if (userParams) userParams->setAllDefault(paramNum);
155 
156  switch (bufLength) {
157  case 5: // global correlation
158  if (userParams) userParams->globalCor()[paramNum] = buf[4]; // no break
159  case 4: // uncertainty
160  if (userParams) userParams->sigma()[paramNum] = buf[3] / cmsToPede;
161  covMat[paramNum][paramNum] = buf[3]*buf[3] / (cmsToPede*cmsToPede);
162  // no break;
163  case 3: // difference to start value
164  if (userParams) userParams->diffBefore()[paramNum] = buf[2] / cmsToPede;
165  // no break
166  case 2:
167  params->setValid(true);
168  parVec[paramNum] = buf[0] / cmsToPede * mySteerer.parameterSign(); // parameter
169  if (userParams) {
170  userParams->parameter()[paramNum] = parVec[paramNum]; // duplicate in millepede parameters
171  userParams->preSigma()[paramNum] = buf[1]; // presigma given, probably means fixed
172  if (!userParams->isFixed(paramNum)) {
173  userParams->preSigma()[paramNum] /= cmsToPede;
174  if (bufLength == 2) {
175  edm::LogWarning("Alignment") << "@SUB=PedeReader::setParameter"
176  << "Param " << paramLabel << " (from "
177  << typeid(*alignable).name() << ") without result!";
178  userParams->isValid()[paramNum] = false;
179  params->setValid(false);
180  }
181  }
182  }
183  break;
184  case 0:
185  case 1:
186  default:
187  edm::LogError("Alignment") << "@SUB=PedeReader::setParameter"
188  << "Expect 2 to 5 values, got " << bufLength
189  << " for label " << paramLabel;
190  break;
191  }
192  alignable->setAlignmentParameters(params->clone(parVec, covMat));//transferred mem. responsib.
193  } else {
194  unsigned int lasBeamId = myLabels.lasBeamIdFromLabel(paramLabel);
195  edm::LogError("Alignment") << "@SUB=PedeReader::setParameter"
196  << "No alignable for paramLabel " << paramLabel
197  << ", probably LasBeam with Id " << lasBeamId
198  << ",\nparam " << paramNum << ": "
199  << buf[0] / cmsToPede * mySteerer.parameterSign()
200  << " += " << (bufLength >= 4 ? buf[3] / cmsToPede : -99.);
201  }
202 
203  return alignable;
204 }
205 
206 //__________________________________________________________________________________________________
207 AlignmentParameters* PedeReader::checkAliParams(Alignable *alignable, bool createUserVars) const
208 {
209  // first check that we have parameters
210  AlignmentParameters *params = alignable->alignmentParameters();
211  if (!params) {
212  throw cms::Exception("BadConfig") << "PedeReader::checkAliParams"
213  << "Alignable without parameters.";
214 
215  }
216 
217  // now check that we have user parameters of correct type if requested:
218  if (createUserVars && !dynamic_cast<MillePedeVariables*>(params->userVariables())) {
219  edm::LogInfo("Alignment") << "@SUB=PedeReader::checkAliParams"
220  << "Add user variables for alignable with label "
221  << myLabels.alignableLabel(alignable);
222  params->setUserVariables(new MillePedeVariables(params->size(), myLabels.alignableLabel(alignable)));
223  }
224 
225  return params;
226 }
unsigned int label() const
get alignable label as used by pede
const std::vector< float > & globalCor() const
get global correlation array
T getParameter(std::string const &) const
T getUntrackedParameter(std::string const &, T const &) const
virtual const RunRange & runRangeFromLabel(unsigned int label) const
virtual Alignable * alignableFromLabel(unsigned int label) const =0
PedeReader(const edm::ParameterSet &config, const PedeSteerer &steerer, const PedeLabelerBase &labels, const RunRange &runrange)
Definition: PedeReader.cc:31
AlignmentParameters * checkAliParams(Alignable *alignable, bool createUserVars) const
Definition: PedeReader.cc:207
bool read(std::vector< Alignable * > &alignables, bool setUserVars)
Definition: PedeReader.cc:50
bool isFixed(unsigned int nParam) const
true if parameter is fixed
const PedeSteerer & mySteerer
Definition: PedeReader.h:61
virtual unsigned int alignableLabel(Alignable *alignable) const =0
std::ifstream myPedeResult
Definition: PedeReader.h:60
const std::vector< float > & parameter() const
get array of parameters
virtual unsigned int paramNumFromLabel(unsigned int paramLabel) const =0
parameter number, 0 &lt;= .. &lt; theMaxNumParam, belonging to unique parameter label
AlignmentParameters * alignmentParameters() const
Get the AlignmentParameters.
Definition: Alignable.h:57
const AlgebraicVector & parameters(void) const
Get alignment parameters.
AlignmentUserVariables * userVariables(void) const
Get pointer to user variables.
void setAlignmentParameters(AlignmentParameters *dap)
Set the AlignmentParameters.
Definition: Alignable.cc:81
void setValid(bool v)
Set validity flag.
const std::vector< float > & preSigma() const
get array of presigmas (&lt;= 0: means fixed)
const std::vector< float > & sigma() const
get array of sigmas
bool readIfSameLine(std::ifstream &aStream, T &outValue) const
Definition: PedeReader.cc:103
const std::vector< float > & diffBefore() const
get array of differences to start value
CLHEP::HepVector AlgebraicVector
void setUserVariables(AlignmentUserVariables *auv)
Set pointer to user variables.
int size(void) const
Get number of parameters.
virtual AlignmentParameters * clone(const AlgebraicVector &par, const AlgebraicSymMatrix &cov) const =0
Enforce clone methods in derived classes.
double cmsToPedeFactor(unsigned int parNum) const
Definition: PedeSteerer.cc:135
PedeLabelerBase::RunRange RunRange
Definition: PedeReader.h:37
const std::vector< bool > & isValid() const
get valid flag array
CLHEP::HepSymMatrix AlgebraicSymMatrix
static const unsigned int myMaxNumValPerParam
Definition: PedeReader.h:65
const std::string & directory() const
directory from constructor input, &#39;/&#39; is attached if needed
Definition: PedeSteerer.h:65
const AlgebraicSymMatrix & covariance(void) const
Get parameter covariance matrix.
long double T
Alignable * setParameter(unsigned int paramLabel, unsigned int bufLength, float *buf, bool setUserVars) const
Definition: PedeReader.cc:134
const PedeLabelerBase & myLabels
Definition: PedeReader.h:62
const RunRange myRunRange
Definition: PedeReader.h:63
bool setAllDefault(unsigned int nParam)
set default values for all data concerning nParam (false if nParam out of range)
int parameterSign() const
results from pede (and start values for pede) might need a sign flip
Definition: PedeSteerer.h:63
virtual unsigned int lasBeamIdFromLabel(unsigned int label) const =0