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 
29 // system include files
30 #include <memory>
31 #include <iostream>
32 
33 // user include files
42 
44 
55 
57 
63 
64 #include "CLHEP/Vector/RotationInterfaces.h"
65 
66 //
67 // class declaration
68 //
69 
71 public:
73  ~CreateIdealTkAlRecords() override;
74 
75  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
78  static std::vector<GeomDetEnumerators::SubDetector>
79  toSubDetectors(const std::vector<std::string>& subs);
80 
81 private:
82  void analyze(const edm::Event&, const edm::EventSetup&) override;
83  void clearAlignmentInfos();
84  std::unique_ptr<TrackerGeometry> retrieveGeometry(const edm::EventSetup&);
85  void addAlignmentInfo(const GeomDet&);
86  void alignToGT(const edm::EventSetup&);
87  void writeToDB();
88 
89  // ----------member data ---------------------------
90  const std::vector<GeomDetEnumerators::SubDetector> skipSubDetectors_;
91  const bool alignToGlobalTag_;
92  const bool createReferenceRcd_;
97  std::vector<uint32_t> rawIDs_;
98  std::vector<GeomDetEnumerators::SubDetector> subDets_;
99 };
100 
101 
102 //
103 // constructors and destructor
104 //
106  skipSubDetectors_(toSubDetectors(iConfig.getUntrackedParameter<std::vector<std::string> >("skipSubDetectors"))),
107  alignToGlobalTag_(iConfig.getUntrackedParameter<bool>("alignToGlobalTag")),
108  createReferenceRcd_(iConfig.getUntrackedParameter<bool>("createReferenceRcd")),
110 {
111 }
112 
113 
115 {
116 }
117 
118 
119 //
120 // member functions
121 //
122 
123 // ------------ method called for each event ------------
124 void
126 {
127  if (firstEvent_) {
129  const auto tracker = retrieveGeometry(iSetup);
130 
131  auto dets = tracker->dets();
132  std::sort(dets.begin(), dets.end(),
133  [](const auto& a, const auto& b) {
134  return a->geographicalId().rawId() < b->geographicalId().rawId();});
135 
136  for (const auto& det: dets) addAlignmentInfo(*det);
138  writeToDB();
139  firstEvent_ = false;
140  }
141 }
142 
143 
146 {
147  switch (sub) {
148  case GeomDetEnumerators::PixelBarrel: return "PixelBarrel";
149  case GeomDetEnumerators::PixelEndcap: return "PixelEndcap";
150  case GeomDetEnumerators::TIB: return "TIB";
151  case GeomDetEnumerators::TOB: return "TOB";
152  case GeomDetEnumerators::TID: return "TID";
153  case GeomDetEnumerators::TEC: return "TEC";
154  case GeomDetEnumerators::CSC: return "CSC";
155  case GeomDetEnumerators::DT: return "DT";
156  case GeomDetEnumerators::RPCBarrel: return "RPCBarrel";
157  case GeomDetEnumerators::RPCEndcap: return "RPCEndcap";
158  case GeomDetEnumerators::GEM: return "GEM";
159  case GeomDetEnumerators::ME0: return "ME0";
160  case GeomDetEnumerators::P2OTB: return "P2OTB";
161  case GeomDetEnumerators::P2OTEC: return "P2OTEC";
162  case GeomDetEnumerators::P1PXB: return "P1PXB";
163  case GeomDetEnumerators::P1PXEC: return "P1PXEC";
164  case GeomDetEnumerators::P2PXB: return "P2PXB";
165  case GeomDetEnumerators::P2PXEC: return "P2PXEC";
166  case GeomDetEnumerators::invalidDet: return "invalidDet";
167  default:
168  throw cms::Exception("UnknownSubdetector");
169  }
170 }
171 
172 
175 {
176  if (sub == "PixelBarrel") return GeomDetEnumerators::PixelBarrel;
177  else if (sub == "PixelEndcap") return GeomDetEnumerators::PixelEndcap;
178  else if (sub == "TIB") return GeomDetEnumerators::TIB;
179  else if (sub == "TOB") return GeomDetEnumerators::TOB;
180  else if (sub == "TID") return GeomDetEnumerators::TID;
181  else if (sub == "TEC") return GeomDetEnumerators::TEC;
182  else if (sub == "CSC") return GeomDetEnumerators::CSC;
183  else if (sub == "DT") return GeomDetEnumerators::DT;
184  else if (sub == "RPCBarrel") return GeomDetEnumerators::RPCBarrel;
185  else if (sub == "RPCEndcap") return GeomDetEnumerators::RPCEndcap;
186  else if (sub == "GEM") return GeomDetEnumerators::GEM;
187  else if (sub == "ME0") return GeomDetEnumerators::ME0;
188  else if (sub == "P2OTB") return GeomDetEnumerators::P2OTB;
189  else if (sub == "P2OTEC") return GeomDetEnumerators::P2OTEC;
190  else if (sub == "P1PXB") return GeomDetEnumerators::P1PXB;
191  else if (sub == "P1PXEC") return GeomDetEnumerators::P1PXEC;
192  else if (sub == "P2PXB") return GeomDetEnumerators::P2PXB;
193  else if (sub == "P2PXEC") return GeomDetEnumerators::P2PXEC;
194  else if (sub == "invalidDet") return GeomDetEnumerators::invalidDet;
195  else throw cms::Exception("UnknownSubdetector") << sub;
196 }
197 
198 
199 std::vector<GeomDetEnumerators::SubDetector>
200 CreateIdealTkAlRecords::toSubDetectors(const std::vector<std::string>& subs)
201 {
202  std::vector<GeomDetEnumerators::SubDetector> result;
203  for (const auto& sub: subs) result.emplace_back(toSubDetector(sub));
204  return result;
205 }
206 
207 
208 void
210 {
211  alignments_.clear();
214  rawIDs_.clear();
215 }
216 
217 
218 std::unique_ptr<TrackerGeometry>
220 {
221  edm::ESHandle<GeometricDet> geometricDet;
222  iSetup.get<IdealGeometryRecord>().get(geometricDet);
223 
225  iSetup.get<PTrackerParametersRcd>().get(ptp);
226 
227  edm::ESHandle<TrackerTopology> tTopoHandle;
228  iSetup.get<TrackerTopologyRcd>().get(tTopoHandle);
229  const auto* const tTopo = tTopoHandle.product();
230 
231  TrackerGeomBuilderFromGeometricDet trackerBuilder;
232 
233  return std::unique_ptr<TrackerGeometry> {
234  trackerBuilder.build(&(*geometricDet), *ptp, tTopo )};
235 }
236 
237 
238 void
240 {
241  const auto subDetector = toString(det.subDetector());
242  const auto& detId = det.geographicalId().rawId();
243  const auto& pos = det.position();
244  const auto& rot = det.rotation();
245  rawIDs_.push_back(detId);
246  subDets_.push_back(det.subDetector());
247 
248  // TrackerAlignmentRcd entry
249  if (createReferenceRcd_) {
252  detId));
253  } else {
254  const AlignTransform::Translation translation(pos.x(), pos.y(), pos.z());
256  CLHEP::HepRep3x3(rot.xx(),rot.xy(),rot.xz(),
257  rot.yx(),rot.yy(),rot.yz(),
258  rot.zx(),rot.zy(),rot.zz()));
259  const auto& eulerAngles = rotation.eulerAngles();
260  LogDebug("Alignment")
261  << "============================================================\n"
262  << "subdetector: " << subDetector << "\n"
263  << "detId: " << detId << "\n"
264  << "------------------------------------------------------------\n"
265  << " x: " << pos.x() << "\n"
266  << " y: " << pos.y() << "\n"
267  << " z: " << pos.z() << "\n"
268  << " phi: " << eulerAngles.phi() << "\n"
269  << " theta: " << eulerAngles.theta() << "\n"
270  << " psi: " << eulerAngles.psi() << "\n"
271  << "============================================================\n";
272  alignments_.m_align.emplace_back(AlignTransform(translation, rotation, detId));
273  }
274 
275  // TrackerAlignmentErrorExtendedRcd entry
276  const AlignTransformError::SymMatrix zeroAPEs(6, 0);
277  alignmentErrors_.m_alignError.emplace_back(AlignTransformErrorExtended(zeroAPEs, detId));
278 }
279 
280 
281 void
283 {
284  LogDebug("Alignment") << "Aligning to global tag\n";
285 
286  edm::ESHandle<Alignments> alignments;
287  iSetup.get<TrackerAlignmentRcd>().get(alignments);
289  iSetup.get<TrackerAlignmentErrorExtendedRcd>().get(alignmentErrors);
291  iSetup.get<TrackerSurfaceDeformationRcd>().get(surfaceDeformations);
292 
293  if (alignments->m_align.size() != alignmentErrors->m_alignError.size())
294  throw cms::Exception("GeometryMismatch")
295  << "Size mismatch between alignments (size=" << alignments->m_align.size()
296  << ") and alignment errors (size=" << alignmentErrors->m_alignError.size()
297  << ")";
298 
299  std::vector<uint32_t> commonIDs;
300  auto itAlignErr = alignmentErrors->m_alignError.cbegin();
301  for (auto itAlign = alignments->m_align.cbegin();
302  itAlign != alignments->m_align.cend();
303  ++itAlign, ++itAlignErr) {
304  const auto id = itAlign->rawId();
305  auto found = std::find(rawIDs_.cbegin(), rawIDs_.cend(), id);
306  if (found != rawIDs_.cend()) {
307  if (id != itAlignErr->rawId())
308  throw cms::Exception("GeometryMismatch")
309  << "DetId mismatch between alignments (rawId=" << id
310  << ") and alignment errors (rawId=" << itAlignErr->rawId() << ")";
311 
312  const auto index = std::distance(rawIDs_.cbegin(), found);
313  if (std::find(skipSubDetectors_.begin(),
314  skipSubDetectors_.end(),
315  subDets_[index]) != skipSubDetectors_.end()) continue;
316 
317  if (alignments_.m_align[index].rawId()
318  != alignmentErrors_.m_alignError[index].rawId())
319  throw cms::Exception("GeometryMismatch")
320  << "DetId mismatch between alignments (rawId="
321  << alignments_.m_align[index].rawId()
322  << ") and alignment errors (rawId="
323  << alignmentErrors_.m_alignError[index].rawId() << ")";
324 
325  LogDebug("Alignment")
326  << "============================================================\n"
327  << "\nGeometry content (" << toString(subDets_[index]) << ", "
328  << alignments_.m_align[index].rawId() << "):\n"
329  << "\tx: " << alignments_.m_align[index].translation().x()
330  << "\ty: " << alignments_.m_align[index].translation().y()
331  << "\tz: " << alignments_.m_align[index].translation().z()
332  << "\tphi: " << alignments_.m_align[index].rotation().phi()
333  << "\ttheta: " << alignments_.m_align[index].rotation().theta()
334  << "\tpsi: " << alignments_.m_align[index].rotation().psi()
335  << "============================================================\n";
336  alignments_.m_align[index] = *itAlign;
337  alignmentErrors_.m_alignError[index] = *itAlignErr;
338  commonIDs.push_back(id);
339  LogDebug("Alignment")
340  << "============================================================\n"
341  << "Global tag content (" << toString(subDets_[index]) << ", "
342  << alignments_.m_align[index].rawId() << "):\n"
343  << "\tx: " << alignments_.m_align[index].translation().x()
344  << "\ty: " << alignments_.m_align[index].translation().y()
345  << "\tz: " << alignments_.m_align[index].translation().z()
346  << "\tphi: " << alignments_.m_align[index].rotation().phi()
347  << "\ttheta: " << alignments_.m_align[index].rotation().theta()
348  << "\tpsi: " << alignments_.m_align[index].rotation().psi()
349  << "============================================================\n";
350  }
351  }
352 
353  // - surface deformations are stored differently
354  // -> different treatment
355  // - the above payloads contain also entries for ideal modules
356  // - no entry is created for ideal surfaces
357  // -> size of surface deformation payload does not necessarily match the
358  // size of the other tracker alignment payload
359  for (const auto& id: commonIDs) {
360  // search for common raw ID in surface deformation items
361  auto item = std::find_if(surfaceDeformations->items().cbegin(),
362  surfaceDeformations->items().cend(),
363  [&id](const auto& i) { return i.m_rawId == id; });
364  if (item == surfaceDeformations->items().cend()) continue; // not found
365 
366  // copy surface deformation item
367  const auto index = std::distance(surfaceDeformations->items().cbegin(), item);
368  const auto beginEndPair = surfaceDeformations->parameters(index);
369  std::vector<align::Scalar> params(beginEndPair.first, beginEndPair.second);
370  alignmentSurfaceDeformations_.add(item->m_rawId,
371  item->m_parametrizationType,
372  params);
373  }
374 }
375 
376 
377 void
379 {
381 
383  if (!poolDb.isAvailable()) {
384  throw cms::Exception("NotAvailable") << "PoolDBOutputService not available";
385  }
386 
387  edm::LogInfo("Alignment")
388  << "Writing ideal tracker-alignment records.";
389  poolDb->writeOne(&alignments_, since, "TrackerAlignmentRcd");
390  poolDb->writeOne(&alignmentErrors_, since, "TrackerAlignmentErrorExtendedRcd");
391  poolDb->writeOne(&alignmentSurfaceDeformations_, since, "TrackerSurfaceDeformationRcd");
392 }
393 
394 
395 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
396 void
398 {
400  desc.setComment("Creates ideal TrackerAlignmentRcd and TrackerAlignmentErrorExtendedRcd "
401  "from the loaded tracker geometry. "
402  "PoolDBOutputService must be set up for these records.");
403  desc.addUntracked<bool>("alignToGlobalTag", false);
404  desc.addUntracked<std::vector<std::string> >("skipSubDetectors",
405  std::vector<std::string>{});
406  desc.addUntracked<bool>("createReferenceRcd", false);
407  descriptions.add("createIdealTkAlRecords", desc);
408 }
409 
410 
411 //define this as a plug-in
#define LogDebug(id)
std::unique_ptr< TrackerGeometry > retrieveGeometry(const edm::EventSetup &)
const TimeTypeSpecs timeTypeSpecs[]
Definition: Time.cc:22
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:45
const Surface::RotationType & rotation() const
The rotation defining the local R.F.
Definition: GeomDet.h:51
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
CLHEP::HepSymMatrix SymMatrix
CLHEP::Hep3Vector Translation
constexpr uint32_t rawId() const
get the raw id
Definition: DetId.h:50
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:20
std::vector< AlignTransform > m_align
Definition: Alignments.h:19
const std::vector< GeomDetEnumerators::SubDetector > skipSubDetectors_
void setComment(std::string const &value)
const Surface::PositionType & position() const
The position (origin of the R.F.)
Definition: GeomDet.h:48
ParametersConstIteratorPair parameters(size_t index) const
bool isAvailable() const
Definition: Service.h:46
DetId geographicalId() const
The label of this GeomDet.
Definition: GeomDet.h:79
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:120
void add(std::string const &label, ParameterSetDescription const &psetDescription)
double a
Definition: hdecay.h:121
T get() const
Definition: EventSetup.h:68
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
virtual SubDetector subDetector() const
Which subdetector.
Definition: GeomDet.cc:44
T const * product() const
Definition: ESHandle.h:84
void clear()
Clear vector without having to look into internals:
CLHEP::HepRotation Rotation