CMS 3D CMS Logo

MillePedeFileReader.cc
Go to the documentation of this file.
1 /*** Header file ***/
3 
4 /*** system includes ***/
5 #include <cmath> // include floating-point std::abs functions
6 #include <fstream>
7 
8 /*** core framework functionality ***/
10 
11 /*** Alignment ***/
13 
14 
15 //=============================================================================
16 //=== PUBLIC METHOD IMPLEMENTATION ===
17 //=============================================================================
18 
21  const std::shared_ptr<const PedeLabelerBase>& pedeLabeler,
22  const std::shared_ptr<const AlignPCLThresholds>& theThresholds) :
23  pedeLabeler_(pedeLabeler),
24  theThresholds_(theThresholds),
25  millePedeLogFile_(config.getParameter<std::string>("millePedeLogFile")),
26  millePedeResFile_(config.getParameter<std::string>("millePedeResFile"))
27 {
28 }
29 
31 ::read() {
32  readMillePedeLogFile();
33  readMillePedeResultFile();
34 }
35 
38  return updateDB_;
39 }
40 
41 
42 
43 //=============================================================================
44 //=== PRIVATE METHOD IMPLEMENTATION ===
45 //=============================================================================
46 
49 {
50  std::ifstream logFile;
51  logFile.open(millePedeLogFile_.c_str());
52 
53  if (logFile.is_open()) {
54  edm::LogInfo("MillePedeFileReader") << "Reading millepede log-file";
56 
57  while (getline(logFile, line)) {
58  std::string Nrec_string = "NREC =";
59 
60  if (line.find(Nrec_string) != std::string::npos) {
61  std::istringstream iss(line);
62  std::string trash;
63  iss >> trash >> trash >> Nrec_;
64 
65  if (Nrec_ < theThresholds_->getNrecords() ) {
66  edm::LogInfo("MillePedeFileReader")<<"Number of records used "<<theThresholds_->getNrecords()<<std::endl;
67  updateDB_ = false;
68  }
69  }
70  }
71 
72  } else {
73  edm::LogError("MillePedeFileReader") << "Could not read millepede log-file.";
74 
75  updateDB_ = false;
76  Nrec_ = 0;
77  }
78 }
79 
82 {
83 
84  // cutoffs by coordinate and by alignable
85  std::map<std::string,std::array<float, 6 > > cutoffs_;
86  std::map<std::string,std::array<float, 6 > > significances_;
87  std::map<std::string,std::array<float, 6 > > thresholds_;
88  std::map<std::string,std::array<float, 6 > > errors_;
89 
90  std::vector<std::string> alignables_ = theThresholds_->getAlignableList();
91  for (auto &ali : alignables_){
92  cutoffs_[ali] = theThresholds_->getCut(ali);
93  significances_[ali] = theThresholds_->getSigCut(ali);
94  thresholds_[ali] = theThresholds_->getMaxMoveCut(ali);
95  errors_[ali] = theThresholds_->getMaxErrorCut(ali);
96  }
97 
98  updateDB_ = false;
99  std::ifstream resFile;
100  resFile.open(millePedeResFile_.c_str());
101 
102  if (resFile.is_open()) {
103  edm::LogInfo("MillePedeFileReader") << "Reading millepede result-file";
104 
106  getline(resFile, line); // drop first line
107 
108  while (getline(resFile, line)) {
109  std::istringstream iss(line);
110 
111  std::vector<std::string> tokens;
112  std::string token;
113  while (iss >> token) {
114  tokens.push_back(token);
115  }
116 
117  if (tokens.size() > 4 /*3*/) {
118 
119  auto alignableLabel = std::stoul(tokens[0]);
120  auto alignableIndex = alignableLabel % 10 - 1;
121  const auto alignable = pedeLabeler_->alignableFromLabel(alignableLabel);
122 
123  double ObsMove = std::stof(tokens[3]) * multiplier_[alignableIndex];
124  double ObsErr = std::stof(tokens[4]) * multiplier_[alignableIndex];
125 
126  auto det = getHLS(alignable);
127  int detIndex = static_cast<int>(det);
128  auto coord = static_cast<AlignPCLThresholds::coordType>(alignableIndex);
129  std::string detLabel = getStringFromHLS(det);
130 
131  if (det != PclHLS::NotInPCL) {
132  switch (coord) {
134  Xobs_[detIndex] = ObsMove;
135  XobsErr_[detIndex] = ObsErr;
136  break;
138  Yobs_[detIndex] = ObsMove;
139  YobsErr_[detIndex] = ObsErr;
140  break;
142  Zobs_[detIndex] = ObsMove;
143  ZobsErr_[detIndex] = ObsErr;
144  break;
146  tXobs_[detIndex] = ObsMove;
147  tXobsErr_[detIndex] = ObsErr;
148  break;
150  tYobs_[detIndex] = ObsMove;
151  tYobsErr_[detIndex] = ObsErr;
152  break;
154  tZobs_[detIndex] = ObsMove;
155  tZobsErr_[detIndex] = ObsErr;
156  break;
157  default:
158  edm::LogError("MillePedeFileReader") << "Currently not able to handle DOF " << coord
159  << std::endl;
160  break;
161  }
162  } else {
163  continue;
164  }
165 
166  edm::LogVerbatim("MillePedeFileReader")<<" alignableLabel: "<< alignableLabel <<" with alignableIndex "<<alignableIndex <<" detIndex"<< detIndex <<"\n"
167  <<" i.e. detLabel: "<< detLabel <<" ("<< coord <<")\n"
168  <<" has movement: "<< ObsMove <<" +/- "<< ObsErr <<"\n"
169  <<" cutoff (cutoffs_["<< detLabel <<"]["<< coord <<"]): "<< cutoffs_[detLabel][alignableIndex] <<"\n"
170  <<" significance (significances_["<< detLabel <<"]["<< coord <<"]): "<< significances_[detLabel][alignableIndex] <<"\n"
171  <<" error thresolds (errors_["<< detLabel <<"]["<< coord <<"]): "<< errors_[detLabel][alignableIndex] <<"\n"
172  <<" max movement (thresholds_["<< detLabel <<"]["<< coord <<"]): "<< thresholds_[detLabel][alignableIndex] <<"\n"
173  <<"============="<< std::endl;
174 
175  if (std::abs(ObsMove) > thresholds_[detLabel][alignableIndex]) {
176  edm::LogWarning("MillePedeFileReader")<<"Aborting payload creation."
177  <<" Exceeding maximum thresholds for movement: "<<std::abs(ObsMove)<<" for"<< detLabel <<"("<<coord<<")" ;
178  updateDB_ = false;
179  break;
180 
181  } else if (std::abs(ObsMove) > cutoffs_[detLabel][alignableIndex]) {
182 
183  if (std::abs(ObsErr) > errors_[detLabel][alignableIndex]) {
184  edm::LogWarning("MillePedeFileReader")<<"Aborting payload creation."
185  <<" Exceeding maximum thresholds for error: "<<std::abs(ObsErr)<<" for"<< detLabel <<"("<<coord<<")" ;
186  updateDB_ = false;
187  break;
188  } else {
189  if (std::abs(ObsMove/ObsErr) < significances_[detLabel][alignableIndex]) {
190  continue;
191  }
192  }
193  updateDB_ = true;
194  edm::LogInfo("MillePedeFileReader")<<"This correction: "<<ObsMove<<"+/-" <<ObsErr<<" for "<< detLabel <<"("<<coord<<") will trigger a new Tracker Alignment payload!";
195  }
196  }
197  }
198  } else {
199  edm::LogError("MillePedeFileReader") << "Could not read millepede result-file.";
200 
201  updateDB_ = false;
202  Nrec_ = 0;
203  }
204 }
205 
206 
208 ::getHLS(const Alignable* alignable) {
209  if (!alignable) return PclHLS::NotInPCL;
210 
211  const auto& tns = pedeLabeler_->alignableTracker()->trackerNameSpace();
212 
213  switch (alignable->alignableObjectId()) {
215  switch (tns.tpb().halfBarrelNumber(alignable->id())) {
216  case 1: return PclHLS::TPBHalfBarrelXminus;
217  case 2: return PclHLS::TPBHalfBarrelXplus;
218  default:
219  throw cms::Exception("LogicError")
220  << "@SUB=MillePedeFileReader::getHLS\n"
221  << "Found a pixel half-barrel number that should not exist: "
222  << tns.tpb().halfBarrelNumber(alignable->id());
223  }
225  switch (tns.tpe().endcapNumber(alignable->id())) {
226  case 1:
227  switch (tns.tpe().halfCylinderNumber(alignable->id())) {
228  case 1: return PclHLS::TPEHalfCylinderXminusZminus;
229  case 2: return PclHLS::TPEHalfCylinderXplusZminus;
230  default:
231  throw cms::Exception("LogicError")
232  << "@SUB=MillePedeFileReader::getHLS\n"
233  << "Found a pixel half-cylinder number that should not exist: "
234  << tns.tpe().halfCylinderNumber(alignable->id());
235  }
236  case 2:
237  switch (tns.tpe().halfCylinderNumber(alignable->id())) {
238  case 1: return PclHLS::TPEHalfCylinderXminusZplus;
239  case 2: return PclHLS::TPEHalfCylinderXplusZplus;
240  default:
241  throw cms::Exception("LogicError")
242  << "@SUB=MillePedeFileReader::getHLS\n"
243  << "Found a pixel half-cylinder number that should not exist: "
244  << tns.tpe().halfCylinderNumber(alignable->id());
245  }
246  default:
247  throw cms::Exception("LogicError")
248  << "@SUB=MillePedeFileReader::getHLS\n"
249  << "Found a pixel endcap number that should not exist: "
250  << tns.tpe().endcapNumber(alignable->id());
251  }
252  default: return PclHLS::NotInPCL;
253  }
254 }
255 
258  switch (HLS)
259  {
260  case PclHLS::TPBHalfBarrelXminus : return "TPBHalfBarrelXminus";
261  case PclHLS::TPBHalfBarrelXplus : return "TPBHalfBarrelXplus";
262  case PclHLS::TPEHalfCylinderXminusZminus : return "TPEHalfCylinderXminusZminus";
263  case PclHLS::TPEHalfCylinderXplusZminus : return "TPEHalfCylinderXplusZminus";
264  case PclHLS::TPEHalfCylinderXminusZplus : return "TPEHalfCylinderXminusZplus";
265  case PclHLS::TPEHalfCylinderXplusZplus : return "TPEHalfCylinderXplusZplus";
266  default:
267  throw cms::Exception("LogicError")
268  << "@SUB=MillePedeFileReader::getStringFromHLS\n"
269  << "Found an alignable structure not possible to map in the default AlignPCLThresholds partitions";
270  }
271 }
272 
273 
274 //=============================================================================
275 //=== STATIC CONST MEMBER DEFINITION ===
276 //=============================================================================
277 constexpr std::array<double, 6> MillePedeFileReader::multiplier_;
278 
279 
280 
align::ID id() const
Return the ID of Alignable, i.e. DetId of &#39;first&#39; component GeomDet(Unit).
Definition: Alignable.h:189
Definition: config.py:1
PclHLS getHLS(const Alignable *)
#define constexpr
virtual StructureType alignableObjectId() const =0
Return the alignable type identifier.
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
MillePedeFileReader(const edm::ParameterSet &, const std::shared_ptr< const PedeLabelerBase > &, const std::shared_ptr< const AlignPCLThresholds > &)
std::string getStringFromHLS(PclHLS HLS)
static std::array< double, 6 > multiplier_