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_&&!vetoUpdateDB_);
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  vetoUpdateDB_ = false;
100  std::ifstream resFile;
101  resFile.open(millePedeResFile_.c_str());
102 
103  if (resFile.is_open()) {
104  edm::LogInfo("MillePedeFileReader") << "Reading millepede result-file";
105 
107  getline(resFile, line); // drop first line
108 
109  while (getline(resFile, line)) {
110  std::istringstream iss(line);
111 
112  std::vector<std::string> tokens;
113  std::string token;
114  while (iss >> token) {
115  tokens.push_back(token);
116  }
117 
118  if (tokens.size() > 4 /*3*/) {
119 
120  auto alignableLabel = std::stoul(tokens[0]);
121  auto alignableIndex = alignableLabel % 10 - 1;
122  const auto alignable = pedeLabeler_->alignableFromLabel(alignableLabel);
123 
124  double ObsMove = std::stof(tokens[3]) * multiplier_[alignableIndex];
125  double ObsErr = std::stof(tokens[4]) * multiplier_[alignableIndex];
126 
127  auto det = getHLS(alignable);
128  int detIndex = static_cast<int>(det);
129  auto coord = static_cast<AlignPCLThresholds::coordType>(alignableIndex);
130  std::string detLabel = getStringFromHLS(det);
131 
132  if (det != PclHLS::NotInPCL) {
133  switch (coord) {
135  Xobs_[detIndex] = ObsMove;
136  XobsErr_[detIndex] = ObsErr;
137  break;
139  Yobs_[detIndex] = ObsMove;
140  YobsErr_[detIndex] = ObsErr;
141  break;
143  Zobs_[detIndex] = ObsMove;
144  ZobsErr_[detIndex] = ObsErr;
145  break;
147  tXobs_[detIndex] = ObsMove;
148  tXobsErr_[detIndex] = ObsErr;
149  break;
151  tYobs_[detIndex] = ObsMove;
152  tYobsErr_[detIndex] = ObsErr;
153  break;
155  tZobs_[detIndex] = ObsMove;
156  tZobsErr_[detIndex] = ObsErr;
157  break;
158  default:
159  edm::LogError("MillePedeFileReader") << "Currently not able to handle DOF " << coord
160  << std::endl;
161  break;
162  }
163  } else {
164  continue;
165  }
166 
167  edm::LogVerbatim("MillePedeFileReader")<<" alignableLabel: "<< alignableLabel <<" with alignableIndex "<<alignableIndex <<" detIndex"<< detIndex <<"\n"
168  <<" i.e. detLabel: "<< detLabel <<" ("<< coord <<")\n"
169  <<" has movement: "<< ObsMove <<" +/- "<< ObsErr <<"\n"
170  <<" cutoff (cutoffs_["<< detLabel <<"]["<< coord <<"]): "<< cutoffs_[detLabel][alignableIndex] <<"\n"
171  <<" significance (significances_["<< detLabel <<"]["<< coord <<"]): "<< significances_[detLabel][alignableIndex] <<"\n"
172  <<" error thresolds (errors_["<< detLabel <<"]["<< coord <<"]): "<< errors_[detLabel][alignableIndex] <<"\n"
173  <<" max movement (thresholds_["<< detLabel <<"]["<< coord <<"]): "<< thresholds_[detLabel][alignableIndex] <<"\n"
174  <<"============="<< std::endl;
175 
176  if (std::abs(ObsMove) > thresholds_[detLabel][alignableIndex]) {
177  edm::LogWarning("MillePedeFileReader")<<"Aborting payload creation."
178  <<" Exceeding maximum thresholds for movement: "<<std::abs(ObsMove)<<" for"<< detLabel <<"("<<coord<<")" ;
179  vetoUpdateDB_ = true;
180  continue;
181 
182  } else if (std::abs(ObsMove) > cutoffs_[detLabel][alignableIndex]) {
183 
184  if (std::abs(ObsErr) > errors_[detLabel][alignableIndex]) {
185  edm::LogWarning("MillePedeFileReader")<<"Aborting payload creation."
186  <<" Exceeding maximum thresholds for error: "<<std::abs(ObsErr)<<" for"<< detLabel <<"("<<coord<<")" ;
187  vetoUpdateDB_ = true;
188  continue;
189  } else {
190  if (std::abs(ObsMove/ObsErr) < significances_[detLabel][alignableIndex]) {
191  continue;
192  }
193  }
194  updateDB_ = true;
195  edm::LogInfo("MillePedeFileReader")<<"This correction: "<<ObsMove<<"+/-" <<ObsErr<<" for "<< detLabel <<"("<<coord<<") will trigger a new Tracker Alignment payload!";
196  }
197  }
198  }
199  } else {
200  edm::LogError("MillePedeFileReader") << "Could not read millepede result-file.";
201 
202  updateDB_ = false;
203  Nrec_ = 0;
204  }
205 }
206 
207 
209 ::getHLS(const Alignable* alignable) {
210  if (!alignable) return PclHLS::NotInPCL;
211 
212  const auto& tns = pedeLabeler_->alignableTracker()->trackerNameSpace();
213 
214  switch (alignable->alignableObjectId()) {
216  switch (tns.tpb().halfBarrelNumber(alignable->id())) {
217  case 1: return PclHLS::TPBHalfBarrelXminus;
218  case 2: return PclHLS::TPBHalfBarrelXplus;
219  default:
220  throw cms::Exception("LogicError")
221  << "@SUB=MillePedeFileReader::getHLS\n"
222  << "Found a pixel half-barrel number that should not exist: "
223  << tns.tpb().halfBarrelNumber(alignable->id());
224  }
226  switch (tns.tpe().endcapNumber(alignable->id())) {
227  case 1:
228  switch (tns.tpe().halfCylinderNumber(alignable->id())) {
229  case 1: return PclHLS::TPEHalfCylinderXminusZminus;
230  case 2: return PclHLS::TPEHalfCylinderXplusZminus;
231  default:
232  throw cms::Exception("LogicError")
233  << "@SUB=MillePedeFileReader::getHLS\n"
234  << "Found a pixel half-cylinder number that should not exist: "
235  << tns.tpe().halfCylinderNumber(alignable->id());
236  }
237  case 2:
238  switch (tns.tpe().halfCylinderNumber(alignable->id())) {
239  case 1: return PclHLS::TPEHalfCylinderXminusZplus;
240  case 2: return PclHLS::TPEHalfCylinderXplusZplus;
241  default:
242  throw cms::Exception("LogicError")
243  << "@SUB=MillePedeFileReader::getHLS\n"
244  << "Found a pixel half-cylinder number that should not exist: "
245  << tns.tpe().halfCylinderNumber(alignable->id());
246  }
247  default:
248  throw cms::Exception("LogicError")
249  << "@SUB=MillePedeFileReader::getHLS\n"
250  << "Found a pixel endcap number that should not exist: "
251  << tns.tpe().endcapNumber(alignable->id());
252  }
253  default: return PclHLS::NotInPCL;
254  }
255 }
256 
259  switch (HLS)
260  {
261  case PclHLS::TPBHalfBarrelXminus : return "TPBHalfBarrelXminus";
262  case PclHLS::TPBHalfBarrelXplus : return "TPBHalfBarrelXplus";
263  case PclHLS::TPEHalfCylinderXminusZminus : return "TPEHalfCylinderXminusZminus";
264  case PclHLS::TPEHalfCylinderXplusZminus : return "TPEHalfCylinderXplusZminus";
265  case PclHLS::TPEHalfCylinderXminusZplus : return "TPEHalfCylinderXminusZplus";
266  case PclHLS::TPEHalfCylinderXplusZplus : return "TPEHalfCylinderXplusZplus";
267  default:
268  throw cms::Exception("LogicError")
269  << "@SUB=MillePedeFileReader::getStringFromHLS\n"
270  << "Found an alignable structure not possible to map in the default AlignPCLThresholds partitions";
271  }
272 }
273 
274 
275 //=============================================================================
276 //=== STATIC CONST MEMBER DEFINITION ===
277 //=============================================================================
278 constexpr std::array<double, 6> MillePedeFileReader::multiplier_;
279 
280 
281 
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 *)
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)
#define constexpr
static std::array< double, 6 > multiplier_