CMS 3D CMS Logo

CreateTrackerAlignmentRcds.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: Alignment/TrackerAlignment
4 // Class: CreateIdealTkAlRecords
5 //
22 //
23 // Original Author: Gregor Mittag
24 // Created: Tue, 26 Apr 2016 09:45:13 GMT
25 //
26 //
27 
28 // system include files
29 #include <memory>
30 #include <iostream>
31 
32 // user include files
41 
43 
54 
56 
62 
63 #include "CLHEP/Vector/RotationInterfaces.h"
64 
65 //
66 // class declaration
67 //
68 
70 public:
72  ~CreateIdealTkAlRecords() override;
73 
74  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
77  static std::vector<GeomDetEnumerators::SubDetector> toSubDetectors(const std::vector<std::string>& subs);
78 
79 private:
80  void analyze(const edm::Event&, const edm::EventSetup&) override;
81  void clearAlignmentInfos();
82  std::unique_ptr<TrackerGeometry> retrieveGeometry(const edm::EventSetup&);
83  void addAlignmentInfo(const GeomDet&);
84  void alignToGT(const edm::EventSetup&);
85  void writeToDB();
86 
87  // ----------member data ---------------------------
88  const std::vector<GeomDetEnumerators::SubDetector> skipSubDetectors_;
89  const bool alignToGlobalTag_;
90  const bool createReferenceRcd_;
95  std::vector<uint32_t> rawIDs_;
96  std::vector<GeomDetEnumerators::SubDetector> subDets_;
97 };
98 
99 //
100 // constructors and destructor
101 //
103  : skipSubDetectors_(toSubDetectors(iConfig.getUntrackedParameter<std::vector<std::string> >("skipSubDetectors"))),
104  alignToGlobalTag_(iConfig.getUntrackedParameter<bool>("alignToGlobalTag")),
105  createReferenceRcd_(iConfig.getUntrackedParameter<bool>("createReferenceRcd")),
106  firstEvent_(true) {}
107 
109 
110 //
111 // member functions
112 //
113 
114 // ------------ method called for each event ------------
116  if (firstEvent_) {
118  const auto tracker = retrieveGeometry(iSetup);
119 
120  auto dets = tracker->dets();
121  std::sort(dets.begin(), dets.end(), [](const auto& a, const auto& b) {
122  return a->geographicalId().rawId() < b->geographicalId().rawId();
123  });
124 
125  for (const auto& det : dets)
126  addAlignmentInfo(*det);
128  alignToGT(iSetup);
129  writeToDB();
130  firstEvent_ = false;
131  }
132 }
133 
135  switch (sub) {
137  return "PixelBarrel";
139  return "PixelEndcap";
141  return "TIB";
143  return "TOB";
145  return "TID";
147  return "TEC";
149  return "CSC";
151  return "DT";
153  return "RPCBarrel";
155  return "RPCEndcap";
157  return "GEM";
159  return "ME0";
161  return "P2OTB";
163  return "P2OTEC";
165  return "P1PXB";
167  return "P1PXEC";
169  return "P2PXB";
171  return "P2PXEC";
173  return "invalidDet";
174  default:
175  throw cms::Exception("UnknownSubdetector");
176  }
177 }
178 
180  if (sub == "PixelBarrel")
182  else if (sub == "PixelEndcap")
184  else if (sub == "TIB")
186  else if (sub == "TOB")
188  else if (sub == "TID")
190  else if (sub == "TEC")
192  else if (sub == "CSC")
194  else if (sub == "DT")
195  return GeomDetEnumerators::DT;
196  else if (sub == "RPCBarrel")
198  else if (sub == "RPCEndcap")
200  else if (sub == "GEM")
202  else if (sub == "ME0")
204  else if (sub == "P2OTB")
206  else if (sub == "P2OTEC")
208  else if (sub == "P1PXB")
210  else if (sub == "P1PXEC")
212  else if (sub == "P2PXB")
214  else if (sub == "P2PXEC")
216  else if (sub == "invalidDet")
218  else
219  throw cms::Exception("UnknownSubdetector") << sub;
220 }
221 
222 std::vector<GeomDetEnumerators::SubDetector> CreateIdealTkAlRecords::toSubDetectors(
223  const std::vector<std::string>& subs) {
224  std::vector<GeomDetEnumerators::SubDetector> result;
225  for (const auto& sub : subs)
226  result.emplace_back(toSubDetector(sub));
227  return result;
228 }
229 
231  alignments_.clear();
234  rawIDs_.clear();
235 }
236 
237 std::unique_ptr<TrackerGeometry> CreateIdealTkAlRecords::retrieveGeometry(const edm::EventSetup& iSetup) {
238  edm::ESHandle<GeometricDet> geometricDet;
239  iSetup.get<IdealGeometryRecord>().get(geometricDet);
240 
242  iSetup.get<PTrackerParametersRcd>().get(ptp);
243 
244  edm::ESHandle<TrackerTopology> tTopoHandle;
245  iSetup.get<TrackerTopologyRcd>().get(tTopoHandle);
246  const auto* const tTopo = tTopoHandle.product();
247 
248  TrackerGeomBuilderFromGeometricDet trackerBuilder;
249 
250  return std::unique_ptr<TrackerGeometry>{trackerBuilder.build(&(*geometricDet), *ptp, tTopo)};
251 }
252 
254  const auto subDetector = toString(det.subDetector());
255  const auto& detId = det.geographicalId().rawId();
256  const auto& pos = det.position();
257  const auto& rot = det.rotation();
258  rawIDs_.push_back(detId);
259  subDets_.push_back(det.subDetector());
260 
261  // TrackerAlignmentRcd entry
262  if (createReferenceRcd_) {
264  } else {
265  const AlignTransform::Translation translation(pos.x(), pos.y(), pos.z());
267  CLHEP::HepRep3x3(rot.xx(), rot.xy(), rot.xz(), rot.yx(), rot.yy(), rot.yz(), rot.zx(), rot.zy(), rot.zz()));
268  const auto& eulerAngles = rotation.eulerAngles();
269  LogDebug("Alignment") << "============================================================\n"
270  << "subdetector: " << subDetector << "\n"
271  << "detId: " << detId << "\n"
272  << "------------------------------------------------------------\n"
273  << " x: " << pos.x() << "\n"
274  << " y: " << pos.y() << "\n"
275  << " z: " << pos.z() << "\n"
276  << " phi: " << eulerAngles.phi() << "\n"
277  << " theta: " << eulerAngles.theta() << "\n"
278  << " psi: " << eulerAngles.psi() << "\n"
279  << "============================================================\n";
280  alignments_.m_align.emplace_back(AlignTransform(translation, rotation, detId));
281  }
282 
283  // TrackerAlignmentErrorExtendedRcd entry
284  const AlignTransformError::SymMatrix zeroAPEs(6, 0);
285  alignmentErrors_.m_alignError.emplace_back(AlignTransformErrorExtended(zeroAPEs, detId));
286 }
287 
289  LogDebug("Alignment") << "Aligning to global tag\n";
290 
291  edm::ESHandle<Alignments> alignments;
292  iSetup.get<TrackerAlignmentRcd>().get(alignments);
294  iSetup.get<TrackerAlignmentErrorExtendedRcd>().get(alignmentErrors);
296  iSetup.get<TrackerSurfaceDeformationRcd>().get(surfaceDeformations);
297 
298  if (alignments->m_align.size() != alignmentErrors->m_alignError.size())
299  throw cms::Exception("GeometryMismatch")
300  << "Size mismatch between alignments (size=" << alignments->m_align.size()
301  << ") and alignment errors (size=" << alignmentErrors->m_alignError.size() << ")";
302 
303  std::vector<uint32_t> commonIDs;
304  auto itAlignErr = alignmentErrors->m_alignError.cbegin();
305  for (auto itAlign = alignments->m_align.cbegin(); itAlign != alignments->m_align.cend(); ++itAlign, ++itAlignErr) {
306  const auto id = itAlign->rawId();
307  auto found = std::find(rawIDs_.cbegin(), rawIDs_.cend(), id);
308  if (found != rawIDs_.cend()) {
309  if (id != itAlignErr->rawId())
310  throw cms::Exception("GeometryMismatch") << "DetId mismatch between alignments (rawId=" << id
311  << ") and alignment errors (rawId=" << itAlignErr->rawId() << ")";
312 
313  const auto index = std::distance(rawIDs_.cbegin(), found);
315  continue;
316 
317  if (alignments_.m_align[index].rawId() != alignmentErrors_.m_alignError[index].rawId())
318  throw cms::Exception("GeometryMismatch")
319  << "DetId mismatch between alignments (rawId=" << alignments_.m_align[index].rawId()
320  << ") and alignment errors (rawId=" << alignmentErrors_.m_alignError[index].rawId() << ")";
321 
322  LogDebug("Alignment") << "============================================================\n"
323  << "\nGeometry content (" << toString(subDets_[index]) << ", "
324  << alignments_.m_align[index].rawId() << "):\n"
325  << "\tx: " << alignments_.m_align[index].translation().x()
326  << "\ty: " << alignments_.m_align[index].translation().y()
327  << "\tz: " << alignments_.m_align[index].translation().z()
328  << "\tphi: " << alignments_.m_align[index].rotation().phi()
329  << "\ttheta: " << alignments_.m_align[index].rotation().theta()
330  << "\tpsi: " << alignments_.m_align[index].rotation().psi()
331  << "============================================================\n";
332  alignments_.m_align[index] = *itAlign;
333  alignmentErrors_.m_alignError[index] = *itAlignErr;
334  commonIDs.push_back(id);
335  LogDebug("Alignment") << "============================================================\n"
336  << "Global tag content (" << toString(subDets_[index]) << ", "
337  << alignments_.m_align[index].rawId() << "):\n"
338  << "\tx: " << alignments_.m_align[index].translation().x()
339  << "\ty: " << alignments_.m_align[index].translation().y()
340  << "\tz: " << alignments_.m_align[index].translation().z()
341  << "\tphi: " << alignments_.m_align[index].rotation().phi()
342  << "\ttheta: " << alignments_.m_align[index].rotation().theta()
343  << "\tpsi: " << alignments_.m_align[index].rotation().psi()
344  << "============================================================\n";
345  }
346  }
347 
348  // - surface deformations are stored differently
349  // -> different treatment
350  // - the above payloads contain also entries for ideal modules
351  // - no entry is created for ideal surfaces
352  // -> size of surface deformation payload does not necessarily match the
353  // size of the other tracker alignment payload
354  for (const auto& id : commonIDs) {
355  // search for common raw ID in surface deformation items
356  auto item = std::find_if(surfaceDeformations->items().cbegin(),
357  surfaceDeformations->items().cend(),
358  [&id](const auto& i) { return i.m_rawId == id; });
359  if (item == surfaceDeformations->items().cend())
360  continue; // not found
361 
362  // copy surface deformation item
363  const auto index = std::distance(surfaceDeformations->items().cbegin(), item);
364  const auto beginEndPair = surfaceDeformations->parameters(index);
365  std::vector<align::Scalar> params(beginEndPair.first, beginEndPair.second);
366  alignmentSurfaceDeformations_.add(item->m_rawId, item->m_parametrizationType, params);
367  }
368 }
369 
372 
374  if (!poolDb.isAvailable()) {
375  throw cms::Exception("NotAvailable") << "PoolDBOutputService not available";
376  }
377 
378  edm::LogInfo("Alignment") << "Writing ideal tracker-alignment records.";
379  poolDb->writeOne(&alignments_, since, "TrackerAlignmentRcd");
380  poolDb->writeOne(&alignmentErrors_, since, "TrackerAlignmentErrorExtendedRcd");
381  poolDb->writeOne(&alignmentSurfaceDeformations_, since, "TrackerSurfaceDeformationRcd");
382 }
383 
384 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
387  desc.setComment(
388  "Creates ideal TrackerAlignmentRcd and TrackerAlignmentErrorExtendedRcd "
389  "from the loaded tracker geometry. "
390  "PoolDBOutputService must be set up for these records.");
391  desc.addUntracked<bool>("alignToGlobalTag", false);
392  desc.addUntracked<std::vector<std::string> >("skipSubDetectors", std::vector<std::string>{});
393  desc.addUntracked<bool>("createReferenceRcd", false);
394  descriptions.add("createIdealTkAlRecords", desc);
395 }
396 
397 //define this as a plug-in
#define LogDebug(id)
std::unique_ptr< TrackerGeometry > retrieveGeometry(const edm::EventSetup &)
const TimeTypeSpecs timeTypeSpecs[]
Definition: Time.cc:16
std::vector< uint32_t > rawIDs_
void addAlignmentInfo(const GeomDet &)
static std::string toString(const GeomDetEnumerators::SubDetector &)
ParameterDescriptionBase * addUntracked(U const &iLabel, T const &value)
AlignmentErrorsExtended alignmentErrors_
AlignmentSurfaceDeformations alignmentSurfaceDeformations_
std::vector< GeomDetEnumerators::SubDetector > subDets_
Time_t beginValue
Definition: Time.h:41
const Surface::RotationType & rotation() const
The rotation defining the local R.F.
Definition: GeomDet.h:46
CLHEP::HepSymMatrix SymMatrix
CLHEP::Hep3Vector Translation
constexpr uint32_t rawId() const
get the raw id
Definition: DetId.h:57
void analyze(const edm::Event &, const edm::EventSetup &) override
void clear()
Clear vector without having to look into internals:
Definition: Alignments.h:17
CreateIdealTkAlRecords(const edm::ParameterSet &)
TrackerGeometry * build(const GeometricDet *gd, const PTrackerParameters &ptp, const TrackerTopology *tTopo)
static std::vector< GeomDetEnumerators::SubDetector > toSubDetectors(const std::vector< std::string > &subs)
void alignToGT(const edm::EventSetup &)
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:19
std::vector< AlignTransform > m_align
Definition: Alignments.h:19
const std::vector< GeomDetEnumerators::SubDetector > skipSubDetectors_
void setComment(std::string const &value)
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
const Surface::PositionType & position() const
The position (origin of the R.F.)
Definition: GeomDet.h:43
ParametersConstIteratorPair parameters(size_t index) const
bool isAvailable() const
Definition: Service.h:40
DetId geographicalId() const
The label of this GeomDet.
Definition: GeomDet.h:64
void writeOne(T *payload, Time_t time, const std::string &recordName, bool withlogging=false)
const ItemVector & items() const
Get vector of all items.
bool add(align::ID rawId, int type, const std::vector< align::Scalar > &parameters)
Add a new item.
static GeomDetEnumerators::SubDetector toSubDetector(const std::string &sub)
std::vector< AlignTransformErrorExtended > m_alignError
double b
Definition: hdecay.h:118
void add(std::string const &label, ParameterSetDescription const &psetDescription)
double a
Definition: hdecay.h:119
T get() const
Definition: EventSetup.h:73
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
virtual SubDetector subDetector() const
Which subdetector.
Definition: GeomDet.cc:38
T const * product() const
Definition: ESHandle.h:86
void clear()
Clear vector without having to look into internals:
CLHEP::HepRotation Rotation