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 
95  const std::vector<GeomDetEnumerators::SubDetector> skipSubDetectors_;
96  const bool alignToGlobalTag_;
97  const bool createReferenceRcd_;
102  std::vector<uint32_t> rawIDs_;
103  std::vector<GeomDetEnumerators::SubDetector> subDets_;
104 };
105 
106 //
107 // constructors and destructor
108 //
110  : geomDetToken_(esConsumes()),
111  ptpToken_(esConsumes()),
112  topoToken_(esConsumes()),
113  aliToken_(esConsumes()),
114  aliErrorToken_(esConsumes()),
115  aliSurfaceToken_(esConsumes()),
116  skipSubDetectors_(toSubDetectors(iConfig.getUntrackedParameter<std::vector<std::string> >("skipSubDetectors"))),
117  alignToGlobalTag_(iConfig.getUntrackedParameter<bool>("alignToGlobalTag")),
118  createReferenceRcd_(iConfig.getUntrackedParameter<bool>("createReferenceRcd")),
119  firstEvent_(true) {}
120 
122 
123 //
124 // member functions
125 //
126 
127 // ------------ method called for each event ------------
129  if (firstEvent_) {
131  const auto tracker = retrieveGeometry(iSetup);
132 
133  auto dets = tracker->dets();
134  std::sort(dets.begin(), dets.end(), [](const auto& a, const auto& b) {
135  return a->geographicalId().rawId() < b->geographicalId().rawId();
136  });
137 
138  for (const auto& det : dets)
139  addAlignmentInfo(*det);
141  alignToGT(iSetup);
142  writeToDB();
143  firstEvent_ = false;
144  }
145 }
146 
148  switch (sub) {
150  return "PixelBarrel";
152  return "PixelEndcap";
154  return "TIB";
156  return "TOB";
158  return "TID";
160  return "TEC";
162  return "CSC";
164  return "DT";
166  return "RPCBarrel";
168  return "RPCEndcap";
170  return "GEM";
172  return "ME0";
174  return "P2OTB";
176  return "P2OTEC";
178  return "P1PXB";
180  return "P1PXEC";
182  return "P2PXB";
184  return "P2PXEC";
186  return "invalidDet";
187  default:
188  throw cms::Exception("UnknownSubdetector");
189  }
190 }
191 
193  if (sub == "PixelBarrel")
195  else if (sub == "PixelEndcap")
197  else if (sub == "TIB")
199  else if (sub == "TOB")
201  else if (sub == "TID")
203  else if (sub == "TEC")
205  else if (sub == "CSC")
207  else if (sub == "DT")
208  return GeomDetEnumerators::DT;
209  else if (sub == "RPCBarrel")
211  else if (sub == "RPCEndcap")
213  else if (sub == "GEM")
215  else if (sub == "ME0")
217  else if (sub == "P2OTB")
219  else if (sub == "P2OTEC")
221  else if (sub == "P1PXB")
223  else if (sub == "P1PXEC")
225  else if (sub == "P2PXB")
227  else if (sub == "P2PXEC")
229  else if (sub == "invalidDet")
231  else
232  throw cms::Exception("UnknownSubdetector") << sub;
233 }
234 
235 std::vector<GeomDetEnumerators::SubDetector> CreateIdealTkAlRecords::toSubDetectors(
236  const std::vector<std::string>& subs) {
237  std::vector<GeomDetEnumerators::SubDetector> result;
238  result.reserve(subs.size());
239  for (const auto& sub : subs)
240  result.emplace_back(toSubDetector(sub));
241  return result;
242 }
243 
245  alignments_.clear();
248  rawIDs_.clear();
249 }
250 
251 std::unique_ptr<TrackerGeometry> CreateIdealTkAlRecords::retrieveGeometry(const edm::EventSetup& iSetup) {
252  const GeometricDet* geometricDet = &iSetup.getData(geomDetToken_);
253  const PTrackerParameters& ptp = iSetup.getData(ptpToken_);
254  const TrackerTopology* tTopo = &iSetup.getData(topoToken_);
255 
256  TrackerGeomBuilderFromGeometricDet trackerBuilder;
257 
258  return std::unique_ptr<TrackerGeometry>{trackerBuilder.build(geometricDet, ptp, tTopo)};
259 }
260 
262  const auto subDetector = toString(det.subDetector());
263  const auto& detId = det.geographicalId().rawId();
264  const auto& pos = det.position();
265  const auto& rot = det.rotation();
266  rawIDs_.push_back(detId);
267  subDets_.push_back(det.subDetector());
268 
269  // TrackerAlignmentRcd entry
270  if (createReferenceRcd_) {
272  } else {
273  const AlignTransform::Translation translation(pos.x(), pos.y(), pos.z());
275  CLHEP::HepRep3x3(rot.xx(), rot.xy(), rot.xz(), rot.yx(), rot.yy(), rot.yz(), rot.zx(), rot.zy(), rot.zz()));
276  const auto& eulerAngles = rotation.eulerAngles();
277  LogDebug("Alignment") << "============================================================\n"
278  << "subdetector: " << subDetector << "\n"
279  << "detId: " << detId << "\n"
280  << "------------------------------------------------------------\n"
281  << " x: " << pos.x() << "\n"
282  << " y: " << pos.y() << "\n"
283  << " z: " << pos.z() << "\n"
284  << " phi: " << eulerAngles.phi() << "\n"
285  << " theta: " << eulerAngles.theta() << "\n"
286  << " psi: " << eulerAngles.psi() << "\n"
287  << "============================================================\n";
288  alignments_.m_align.emplace_back(AlignTransform(translation, rotation, detId));
289  }
290 
291  // TrackerAlignmentErrorExtendedRcd entry
292  const AlignTransformError::SymMatrix zeroAPEs(6, 0);
293  alignmentErrors_.m_alignError.emplace_back(AlignTransformErrorExtended(zeroAPEs, detId));
294 }
295 
297  LogDebug("Alignment") << "Aligning to global tag\n";
298 
299  const Alignments* alignments = &iSetup.getData(aliToken_);
300  const AlignmentErrorsExtended* alignmentErrors = &iSetup.getData(aliErrorToken_);
301  const AlignmentSurfaceDeformations* surfaceDeformations = &iSetup.getData(aliSurfaceToken_);
302 
303  if (alignments->m_align.size() != alignmentErrors->m_alignError.size())
304  throw cms::Exception("GeometryMismatch")
305  << "Size mismatch between alignments (size=" << alignments->m_align.size()
306  << ") and alignment errors (size=" << alignmentErrors->m_alignError.size() << ")";
307 
308  std::vector<uint32_t> commonIDs;
309  auto itAlignErr = alignmentErrors->m_alignError.cbegin();
310  for (auto itAlign = alignments->m_align.cbegin(); itAlign != alignments->m_align.cend(); ++itAlign, ++itAlignErr) {
311  const auto id = itAlign->rawId();
312  auto found = std::find(rawIDs_.cbegin(), rawIDs_.cend(), id);
313  if (found != rawIDs_.cend()) {
314  if (id != itAlignErr->rawId())
315  throw cms::Exception("GeometryMismatch") << "DetId mismatch between alignments (rawId=" << id
316  << ") and alignment errors (rawId=" << itAlignErr->rawId() << ")";
317 
318  const auto index = std::distance(rawIDs_.cbegin(), found);
320  continue;
321 
322  if (alignments_.m_align[index].rawId() != alignmentErrors_.m_alignError[index].rawId())
323  throw cms::Exception("GeometryMismatch")
324  << "DetId mismatch between alignments (rawId=" << alignments_.m_align[index].rawId()
325  << ") and alignment errors (rawId=" << alignmentErrors_.m_alignError[index].rawId() << ")";
326 
327  LogDebug("Alignment") << "============================================================\n"
328  << "\nGeometry content (" << toString(subDets_[index]) << ", "
329  << alignments_.m_align[index].rawId() << "):\n"
330  << "\tx: " << alignments_.m_align[index].translation().x()
331  << "\ty: " << alignments_.m_align[index].translation().y()
332  << "\tz: " << alignments_.m_align[index].translation().z()
333  << "\tphi: " << alignments_.m_align[index].rotation().phi()
334  << "\ttheta: " << alignments_.m_align[index].rotation().theta()
335  << "\tpsi: " << alignments_.m_align[index].rotation().psi()
336  << "============================================================\n";
337  alignments_.m_align[index] = *itAlign;
338  alignmentErrors_.m_alignError[index] = *itAlignErr;
339  commonIDs.push_back(id);
340  LogDebug("Alignment") << "============================================================\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())
365  continue; // not found
366 
367  // copy surface deformation item
368  const auto index = std::distance(surfaceDeformations->items().cbegin(), item);
369  const auto beginEndPair = surfaceDeformations->parameters(index);
370  std::vector<align::Scalar> params(beginEndPair.first, beginEndPair.second);
371  alignmentSurfaceDeformations_.add(item->m_rawId, item->m_parametrizationType, params);
372  }
373 }
374 
377 
379  if (!poolDb.isAvailable()) {
380  throw cms::Exception("NotAvailable") << "PoolDBOutputService not available";
381  }
382 
383  edm::LogInfo("Alignment") << "Writing ideal tracker-alignment records.";
384  poolDb->writeOneIOV(alignments_, since, "TrackerAlignmentRcd");
385  poolDb->writeOneIOV(alignmentErrors_, since, "TrackerAlignmentErrorExtendedRcd");
386  poolDb->writeOneIOV(alignmentSurfaceDeformations_, since, "TrackerSurfaceDeformationRcd");
387 }
388 
389 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
392  desc.setComment(
393  "Creates ideal TrackerAlignmentRcd and TrackerAlignmentErrorExtendedRcd "
394  "from the loaded tracker geometry. "
395  "PoolDBOutputService must be set up for these records.");
396  desc.addUntracked<bool>("alignToGlobalTag", false);
397  desc.addUntracked<std::vector<std::string> >("skipSubDetectors", std::vector<std::string>{});
398  desc.addUntracked<bool>("createReferenceRcd", false);
399  descriptions.add("createIdealTkAlRecords", desc);
400 }
401 
402 //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_
ESGetTokenH3DDVariant esConsumes(std::string const &Record, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
void addAlignmentInfo(const GeomDet &)
static std::string toString(const GeomDetEnumerators::SubDetector &)
T const & getData(const ESGetToken< T, R > &iToken) const noexcept(false)
Definition: EventSetup.h:119
const edm::ESGetToken< GeometricDet, IdealGeometryRecord > geomDetToken_
AlignmentErrorsExtended alignmentErrors_
AlignmentSurfaceDeformations alignmentSurfaceDeformations_
std::vector< GeomDetEnumerators::SubDetector > subDets_
Time_t beginValue
Definition: Time.h:41
CLHEP::HepSymMatrix SymMatrix
CLHEP::Hep3Vector Translation
ParametersConstIteratorPair parameters(size_t index) const
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_
const edm::ESGetToken< TrackerTopology, TrackerTopologyRcd > topoToken_
const Surface::RotationType & rotation() const
The rotation defining the local R.F.
Definition: GeomDet.h:46
const edm::ESGetToken< Alignments, TrackerAlignmentRcd > aliToken_
Hash writeOneIOV(const T &payload, Time_t time, const std::string &recordName)
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
DetId geographicalId() const
The label of this GeomDet.
Definition: GeomDet.h:64
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
constexpr uint32_t rawId() const
get the raw id
Definition: DetId.h:57
const edm::ESGetToken< AlignmentErrorsExtended, TrackerAlignmentErrorExtendedRcd > aliErrorToken_
double b
Definition: hdecay.h:118
void add(std::string const &label, ParameterSetDescription const &psetDescription)
virtual SubDetector subDetector() const
Which subdetector.
Definition: GeomDet.cc:38
const Surface::PositionType & position() const
The position (origin of the R.F.)
Definition: GeomDet.h:43
double a
Definition: hdecay.h:119
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
const edm::ESGetToken< AlignmentSurfaceDeformations, TrackerSurfaceDeformationRcd > aliSurfaceToken_
bool isAvailable() const
Definition: Service.h:40
const edm::ESGetToken< PTrackerParameters, PTrackerParametersRcd > ptpToken_
const ItemVector & items() const
Get vector of all items.
#define LogDebug(id)
void clear()
Clear vector without having to look into internals:
CLHEP::HepRotation Rotation