CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
55 
57 
64 
65 #include "CLHEP/Vector/RotationInterfaces.h"
66 
67 //
68 // class declaration
69 //
70 
72 public:
74  ~CreateIdealTkAlRecords() override;
75 
76  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
79  static std::vector<GeomDetEnumerators::SubDetector> 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 
98  const std::vector<GeomDetEnumerators::SubDetector> skipSubDetectors_;
99  const bool alignToGlobalTag_;
105  std::vector<uint32_t> rawIDs_;
106  std::vector<GeomDetEnumerators::SubDetector> subDets_;
107 };
108 
109 //
110 // constructors and destructor
111 //
113  : geomDetToken_(esConsumes()),
114  ptpToken_(esConsumes()),
115  ptitpToken_(esConsumes()),
116  topoToken_(esConsumes()),
117  aliToken_(esConsumes()),
118  aliErrorToken_(esConsumes()),
119  aliSurfaceToken_(esConsumes()),
120  skipSubDetectors_(toSubDetectors(iConfig.getUntrackedParameter<std::vector<std::string> >("skipSubDetectors"))),
121  alignToGlobalTag_(iConfig.getUntrackedParameter<bool>("alignToGlobalTag")),
122  createReferenceRcd_(iConfig.getUntrackedParameter<bool>("createReferenceRcd")),
123  firstEvent_(true) {}
124 
126 
127 //
128 // member functions
129 //
130 
131 // ------------ method called for each event ------------
133  if (firstEvent_) {
135  const auto tracker = retrieveGeometry(iSetup);
136 
137  auto dets = tracker->dets();
138  std::sort(dets.begin(), dets.end(), [](const auto& a, const auto& b) {
139  return a->geographicalId().rawId() < b->geographicalId().rawId();
140  });
141 
142  for (const auto& det : dets)
143  addAlignmentInfo(*det);
145  alignToGT(iSetup);
146  writeToDB();
147  firstEvent_ = false;
148  }
149 }
150 
152  switch (sub) {
154  return "PixelBarrel";
156  return "PixelEndcap";
158  return "TIB";
160  return "TOB";
162  return "TID";
164  return "TEC";
166  return "CSC";
168  return "DT";
170  return "RPCBarrel";
172  return "RPCEndcap";
174  return "GEM";
176  return "ME0";
178  return "P2OTB";
180  return "P2OTEC";
182  return "P1PXB";
184  return "P1PXEC";
186  return "P2PXB";
188  return "P2PXEC";
190  return "invalidDet";
191  default:
192  throw cms::Exception("UnknownSubdetector");
193  }
194 }
195 
197  if (sub == "PixelBarrel")
199  else if (sub == "PixelEndcap")
201  else if (sub == "TIB")
203  else if (sub == "TOB")
205  else if (sub == "TID")
207  else if (sub == "TEC")
209  else if (sub == "CSC")
211  else if (sub == "DT")
212  return GeomDetEnumerators::DT;
213  else if (sub == "RPCBarrel")
215  else if (sub == "RPCEndcap")
217  else if (sub == "GEM")
219  else if (sub == "ME0")
221  else if (sub == "P2OTB")
223  else if (sub == "P2OTEC")
225  else if (sub == "P1PXB")
227  else if (sub == "P1PXEC")
229  else if (sub == "P2PXB")
231  else if (sub == "P2PXEC")
233  else if (sub == "invalidDet")
235  else
236  throw cms::Exception("UnknownSubdetector") << sub;
237 }
238 
239 std::vector<GeomDetEnumerators::SubDetector> CreateIdealTkAlRecords::toSubDetectors(
240  const std::vector<std::string>& subs) {
241  std::vector<GeomDetEnumerators::SubDetector> result;
242  result.reserve(subs.size());
243  for (const auto& sub : subs)
244  result.emplace_back(toSubDetector(sub));
245  return result;
246 }
247 
249  alignments_.clear();
252  rawIDs_.clear();
253 }
254 
255 std::unique_ptr<TrackerGeometry> CreateIdealTkAlRecords::retrieveGeometry(const edm::EventSetup& iSetup) {
256  const GeometricDet* geometricDet = &iSetup.getData(geomDetToken_);
257  const PTrackerParameters& ptp = iSetup.getData(ptpToken_);
259  const TrackerTopology* tTopo = &iSetup.getData(topoToken_);
260 
261  TrackerGeomBuilderFromGeometricDet trackerBuilder;
262 
263  return std::unique_ptr<TrackerGeometry>{trackerBuilder.build(geometricDet, ptitp, ptp, tTopo)};
264 }
265 
267  const auto subDetector = toString(det.subDetector());
268  const auto& detId = det.geographicalId().rawId();
269  const auto& pos = det.position();
270  const auto& rot = det.rotation();
271  rawIDs_.push_back(detId);
272  subDets_.push_back(det.subDetector());
273 
274  // TrackerAlignmentRcd entry
275  if (createReferenceRcd_) {
277  } else {
278  const AlignTransform::Translation translation(pos.x(), pos.y(), pos.z());
280  CLHEP::HepRep3x3(rot.xx(), rot.xy(), rot.xz(), rot.yx(), rot.yy(), rot.yz(), rot.zx(), rot.zy(), rot.zz()));
281  const auto& eulerAngles = rotation.eulerAngles();
282  LogDebug("Alignment") << "============================================================\n"
283  << "subdetector: " << subDetector << "\n"
284  << "detId: " << detId << "\n"
285  << "------------------------------------------------------------\n"
286  << " x: " << pos.x() << "\n"
287  << " y: " << pos.y() << "\n"
288  << " z: " << pos.z() << "\n"
289  << " phi: " << eulerAngles.phi() << "\n"
290  << " theta: " << eulerAngles.theta() << "\n"
291  << " psi: " << eulerAngles.psi() << "\n"
292  << "============================================================\n";
293  alignments_.m_align.emplace_back(AlignTransform(translation, rotation, detId));
294  }
295 
296  // TrackerAlignmentErrorExtendedRcd entry
297  const AlignTransformError::SymMatrix zeroAPEs(6, 0);
298  alignmentErrors_.m_alignError.emplace_back(AlignTransformErrorExtended(zeroAPEs, detId));
299 }
300 
302  LogDebug("Alignment") << "Aligning to global tag\n";
303 
304  const Alignments* alignments = &iSetup.getData(aliToken_);
305  const AlignmentErrorsExtended* alignmentErrors = &iSetup.getData(aliErrorToken_);
306  const AlignmentSurfaceDeformations* surfaceDeformations = &iSetup.getData(aliSurfaceToken_);
307 
308  if (alignments->m_align.size() != alignmentErrors->m_alignError.size())
309  throw cms::Exception("GeometryMismatch")
310  << "Size mismatch between alignments (size=" << alignments->m_align.size()
311  << ") and alignment errors (size=" << alignmentErrors->m_alignError.size() << ")";
312 
313  std::vector<uint32_t> commonIDs;
314  auto itAlignErr = alignmentErrors->m_alignError.cbegin();
315  for (auto itAlign = alignments->m_align.cbegin(); itAlign != alignments->m_align.cend(); ++itAlign, ++itAlignErr) {
316  const auto id = itAlign->rawId();
317  auto found = std::find(rawIDs_.cbegin(), rawIDs_.cend(), id);
318  if (found != rawIDs_.cend()) {
319  if (id != itAlignErr->rawId())
320  throw cms::Exception("GeometryMismatch") << "DetId mismatch between alignments (rawId=" << id
321  << ") and alignment errors (rawId=" << itAlignErr->rawId() << ")";
322 
323  const auto index = std::distance(rawIDs_.cbegin(), found);
325  continue;
326 
327  if (alignments_.m_align[index].rawId() != alignmentErrors_.m_alignError[index].rawId())
328  throw cms::Exception("GeometryMismatch")
329  << "DetId mismatch between alignments (rawId=" << alignments_.m_align[index].rawId()
330  << ") and alignment errors (rawId=" << alignmentErrors_.m_alignError[index].rawId() << ")";
331 
332  LogDebug("Alignment") << "============================================================\n"
333  << "\nGeometry content (" << toString(subDets_[index]) << ", "
334  << alignments_.m_align[index].rawId() << "):\n"
335  << "\tx: " << alignments_.m_align[index].translation().x()
336  << "\ty: " << alignments_.m_align[index].translation().y()
337  << "\tz: " << alignments_.m_align[index].translation().z()
338  << "\tphi: " << alignments_.m_align[index].rotation().phi()
339  << "\ttheta: " << alignments_.m_align[index].rotation().theta()
340  << "\tpsi: " << alignments_.m_align[index].rotation().psi()
341  << "============================================================\n";
342  alignments_.m_align[index] = *itAlign;
343  alignmentErrors_.m_alignError[index] = *itAlignErr;
344  commonIDs.push_back(id);
345  LogDebug("Alignment") << "============================================================\n"
346  << "Global tag content (" << toString(subDets_[index]) << ", "
347  << alignments_.m_align[index].rawId() << "):\n"
348  << "\tx: " << alignments_.m_align[index].translation().x()
349  << "\ty: " << alignments_.m_align[index].translation().y()
350  << "\tz: " << alignments_.m_align[index].translation().z()
351  << "\tphi: " << alignments_.m_align[index].rotation().phi()
352  << "\ttheta: " << alignments_.m_align[index].rotation().theta()
353  << "\tpsi: " << alignments_.m_align[index].rotation().psi()
354  << "============================================================\n";
355  }
356  }
357 
358  // - surface deformations are stored differently
359  // -> different treatment
360  // - the above payloads contain also entries for ideal modules
361  // - no entry is created for ideal surfaces
362  // -> size of surface deformation payload does not necessarily match the
363  // size of the other tracker alignment payload
364  for (const auto& id : commonIDs) {
365  // search for common raw ID in surface deformation items
366  auto item = std::find_if(surfaceDeformations->items().cbegin(),
367  surfaceDeformations->items().cend(),
368  [&id](const auto& i) { return i.m_rawId == id; });
369  if (item == surfaceDeformations->items().cend())
370  continue; // not found
371 
372  // copy surface deformation item
373  const auto index = std::distance(surfaceDeformations->items().cbegin(), item);
374  const auto beginEndPair = surfaceDeformations->parameters(index);
375  std::vector<align::Scalar> params(beginEndPair.first, beginEndPair.second);
376  alignmentSurfaceDeformations_.add(item->m_rawId, item->m_parametrizationType, params);
377  }
378 }
379 
382 
384  if (!poolDb.isAvailable()) {
385  throw cms::Exception("NotAvailable") << "PoolDBOutputService not available";
386  }
387 
388  edm::LogInfo("Alignment") << "Writing ideal tracker-alignment records.";
389  poolDb->writeOneIOV(alignments_, since, "TrackerAlignmentRcd");
390  poolDb->writeOneIOV(alignmentErrors_, since, "TrackerAlignmentErrorExtendedRcd");
391  poolDb->writeOneIOV(alignmentSurfaceDeformations_, since, "TrackerSurfaceDeformationRcd");
392 }
393 
394 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
397  desc.setComment(
398  "Creates ideal TrackerAlignmentRcd and TrackerAlignmentErrorExtendedRcd "
399  "from the loaded tracker geometry. "
400  "PoolDBOutputService must be set up for these records.");
401  desc.addUntracked<bool>("alignToGlobalTag", false);
402  desc.addUntracked<std::vector<std::string> >("skipSubDetectors", std::vector<std::string>{});
403  desc.addUntracked<bool>("createReferenceRcd", false);
404  descriptions.add("createIdealTkAlRecords", desc);
405 }
406 
407 //define this as a plug-in
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 &)
const edm::ESGetToken< GeometricDet, IdealGeometryRecord > geomDetToken_
ParameterDescriptionBase * addUntracked(U const &iLabel, T const &value)
uint16_t *__restrict__ id
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
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
TrackerGeometry * build(const GeometricDet *gd, const PTrackerAdditionalParametersPerDet *ptitp, const PTrackerParameters &ptp, const TrackerTopology *tTopo)
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 &)
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_
const edm::ESGetToken< TrackerTopology, TrackerTopologyRcd > topoToken_
tuple result
Definition: mps_fire.py:311
bool getData(T &iHolder) const
Definition: EventSetup.h:122
void setComment(std::string const &value)
const edm::ESGetToken< Alignments, TrackerAlignmentRcd > aliToken_
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
Hash writeOneIOV(const T &payload, Time_t time, const std::string &recordName)
DetId geographicalId() const
The label of this GeomDet.
Definition: GeomDet.h:64
const edm::ESGetToken< PTrackerAdditionalParametersPerDet, PTrackerAdditionalParametersPerDetRcd > ptitpToken_
const ItemVector & items() const
Get vector of all items.
Log< level::Info, false > LogInfo
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
const edm::ESGetToken< AlignmentErrorsExtended, TrackerAlignmentErrorExtendedRcd > aliErrorToken_
double b
Definition: hdecay.h:118
void add(std::string const &label, ParameterSetDescription const &psetDescription)
double a
Definition: hdecay.h:119
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
const edm::ESGetToken< AlignmentSurfaceDeformations, TrackerSurfaceDeformationRcd > aliSurfaceToken_
virtual SubDetector subDetector() const
Which subdetector.
Definition: GeomDet.cc:38
const edm::ESGetToken< PTrackerParameters, PTrackerParametersRcd > ptpToken_
ESGetTokenH3DDVariant esConsumes(std::string const &Reccord, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
#define LogDebug(id)
void clear()
Clear vector without having to look into internals:
CLHEP::HepRotation Rotation