test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros 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 
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:
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  virtual 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")),
109  firstEvent_(true)
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::P2PXEC: return "P2PXEC";
165  case GeomDetEnumerators::invalidDet: return "invalidDet";
166  default:
167  throw cms::Exception("UnknownSubdetector");
168  }
169 }
170 
171 
174 {
175  if (sub == "PixelBarrel") return GeomDetEnumerators::PixelBarrel;
176  else if (sub == "PixelEndcap") return GeomDetEnumerators::PixelEndcap;
177  else if (sub == "TIB") return GeomDetEnumerators::TIB;
178  else if (sub == "TOB") return GeomDetEnumerators::TOB;
179  else if (sub == "TID") return GeomDetEnumerators::TID;
180  else if (sub == "TEC") return GeomDetEnumerators::TEC;
181  else if (sub == "CSC") return GeomDetEnumerators::CSC;
182  else if (sub == "DT") return GeomDetEnumerators::DT;
183  else if (sub == "RPCBarrel") return GeomDetEnumerators::RPCBarrel;
184  else if (sub == "RPCEndcap") return GeomDetEnumerators::RPCEndcap;
185  else if (sub == "GEM") return GeomDetEnumerators::GEM;
186  else if (sub == "ME0") return GeomDetEnumerators::ME0;
187  else if (sub == "P2OTB") return GeomDetEnumerators::P2OTB;
188  else if (sub == "P2OTEC") return GeomDetEnumerators::P2OTEC;
189  else if (sub == "P1PXB") return GeomDetEnumerators::P1PXB;
190  else if (sub == "P1PXEC") return GeomDetEnumerators::P1PXEC;
191  else if (sub == "P2PXEC") return GeomDetEnumerators::P2PXEC;
192  else if (sub == "invalidDet") return GeomDetEnumerators::invalidDet;
193  else throw cms::Exception("UnknownSubdetector") << sub;
194 }
195 
196 
197 std::vector<GeomDetEnumerators::SubDetector>
198 CreateIdealTkAlRecords::toSubDetectors(const std::vector<std::string>& subs)
199 {
200  std::vector<GeomDetEnumerators::SubDetector> result;
201  for (const auto& sub: subs) result.emplace_back(toSubDetector(sub));
202  return result;
203 }
204 
205 
206 void
208 {
209  alignments_.clear();
212  rawIDs_.clear();
213 }
214 
215 
216 std::unique_ptr<TrackerGeometry>
218 {
219  edm::ESHandle<GeometricDet> geometricDet;
220  iSetup.get<IdealGeometryRecord>().get(geometricDet);
221 
223  iSetup.get<PTrackerParametersRcd>().get(ptp);
224 
225  edm::ESHandle<TrackerTopology> tTopoHandle;
226  iSetup.get<TrackerTopologyRcd>().get(tTopoHandle);
227  const auto* const tTopo = tTopoHandle.product();
228 
229  TrackerGeomBuilderFromGeometricDet trackerBuilder;
230 
231  return std::unique_ptr<TrackerGeometry> {
232  trackerBuilder.build(&(*geometricDet), *ptp, tTopo )};
233 }
234 
235 
236 void
238 {
239  const auto subDetector = toString(det.subDetector());
240  const auto& detId = det.geographicalId().rawId();
241  const auto& pos = det.position();
242  const auto& rot = det.rotation();
243  rawIDs_.push_back(detId);
244  subDets_.push_back(det.subDetector());
245 
246  // TrackerAlignmentRcd entry
247  if (createReferenceRcd_) {
250  detId));
251  } else {
252  const AlignTransform::Translation translation(pos.x(), pos.y(), pos.z());
254  CLHEP::HepRep3x3(rot.xx(),rot.xy(),rot.xz(),
255  rot.yx(),rot.yy(),rot.yz(),
256  rot.zx(),rot.zy(),rot.zz()));
257  const auto& eulerAngles = rotation.eulerAngles();
258  LogDebug("Alignment")
259  << "============================================================\n"
260  << "subdetector: " << subDetector << "\n"
261  << "detId: " << detId << "\n"
262  << "------------------------------------------------------------\n"
263  << " x: " << pos.x() << "\n"
264  << " y: " << pos.y() << "\n"
265  << " z: " << pos.z() << "\n"
266  << " phi: " << eulerAngles.phi() << "\n"
267  << " theta: " << eulerAngles.theta() << "\n"
268  << " psi: " << eulerAngles.psi() << "\n"
269  << "============================================================\n";
270  alignments_.m_align.emplace_back(AlignTransform(translation, rotation, detId));
271  }
272 
273  // TrackerAlignmentErrorExtendedRcd entry
274  const AlignTransformError::SymMatrix zeroAPEs(6, 0);
275  alignmentErrors_.m_alignError.emplace_back(AlignTransformErrorExtended(zeroAPEs, detId));
276 }
277 
278 
279 void
281 {
282  LogDebug("Alignment") << "Aligning to global tag\n";
283 
284  edm::ESHandle<Alignments> alignments;
285  iSetup.get<TrackerAlignmentRcd>().get(alignments);
287  iSetup.get<TrackerAlignmentErrorExtendedRcd>().get(alignmentErrors);
289  iSetup.get<TrackerSurfaceDeformationRcd>().get(surfaceDeformations);
290 
291  if (alignments->m_align.size() != alignmentErrors->m_alignError.size())
292  throw cms::Exception("GeometryMismatch")
293  << "Size mismatch between alignments (size=" << alignments->m_align.size()
294  << ") and alignment errors (size=" << alignmentErrors->m_alignError.size()
295  << ")";
296 
297  std::vector<uint32_t> commonIDs;
298  auto itAlignErr = alignmentErrors->m_alignError.cbegin();
299  for (auto itAlign = alignments->m_align.cbegin();
300  itAlign != alignments->m_align.cend();
301  ++itAlign, ++itAlignErr) {
302  const auto id = itAlign->rawId();
303  auto found = std::find(rawIDs_.cbegin(), rawIDs_.cend(), id);
304  if (found != rawIDs_.cend()) {
305  if (id != itAlignErr->rawId())
306  throw cms::Exception("GeometryMismatch")
307  << "DetId mismatch between alignments (rawId=" << id
308  << ") and alignment errors (rawId=" << itAlignErr->rawId() << ")";
309 
310  const auto index = std::distance(rawIDs_.cbegin(), found);
311  if (std::find(skipSubDetectors_.begin(),
312  skipSubDetectors_.end(),
313  subDets_[index]) != skipSubDetectors_.end()) continue;
314 
315  if (alignments_.m_align[index].rawId()
316  != alignmentErrors_.m_alignError[index].rawId())
317  throw cms::Exception("GeometryMismatch")
318  << "DetId mismatch between alignments (rawId="
319  << alignments_.m_align[index].rawId()
320  << ") and alignment errors (rawId="
321  << alignmentErrors_.m_alignError[index].rawId() << ")";
322 
323  LogDebug("Alignment")
324  << "============================================================\n"
325  << "\nGeometry content (" << toString(subDets_[index]) << ", "
326  << alignments_.m_align[index].rawId() << "):\n"
327  << "\tx: " << alignments_.m_align[index].translation().x()
328  << "\ty: " << alignments_.m_align[index].translation().y()
329  << "\tz: " << alignments_.m_align[index].translation().z()
330  << "\tphi: " << alignments_.m_align[index].rotation().phi()
331  << "\ttheta: " << alignments_.m_align[index].rotation().theta()
332  << "\tpsi: " << alignments_.m_align[index].rotation().psi()
333  << "============================================================\n";
334  alignments_.m_align[index] = *itAlign;
335  alignmentErrors_.m_alignError[index] = *itAlignErr;
336  commonIDs.push_back(id);
337  LogDebug("Alignment")
338  << "============================================================\n"
339  << "Global tag content (" << toString(subDets_[index]) << ", "
340  << alignments_.m_align[index].rawId() << "):\n"
341  << "\tx: " << alignments_.m_align[index].translation().x()
342  << "\ty: " << alignments_.m_align[index].translation().y()
343  << "\tz: " << alignments_.m_align[index].translation().z()
344  << "\tphi: " << alignments_.m_align[index].rotation().phi()
345  << "\ttheta: " << alignments_.m_align[index].rotation().theta()
346  << "\tpsi: " << alignments_.m_align[index].rotation().psi()
347  << "============================================================\n";
348  }
349  }
350 
351  // - surface deformations are stored differently
352  // -> different treatment
353  // - the above payloads contain also entries for ideal modules
354  // - no entry is created for ideal surfaces
355  // -> size of surface deformation payload does not necessarily match the
356  // size of the other tracker alignment payload
357  for (const auto& id: commonIDs) {
358  // search for common raw ID in surface deformation items
359  auto item = std::find_if(surfaceDeformations->items().cbegin(),
360  surfaceDeformations->items().cend(),
361  [&id](const auto& i) { return i.m_rawId == id; });
362  if (item == surfaceDeformations->items().cend()) continue; // not found
363 
364  // copy surface deformation item
365  const auto index = std::distance(surfaceDeformations->items().cbegin(), item);
366  const auto beginEndPair = surfaceDeformations->parameters(index);
367  std::vector<align::Scalar> params(beginEndPair.first, beginEndPair.second);
368  alignmentSurfaceDeformations_.add(item->m_rawId,
369  item->m_parametrizationType,
370  params);
371  }
372 }
373 
374 
375 void
377 {
378  const auto& since = cond::timeTypeSpecs[cond::runnumber].beginValue;
379 
381  if (!poolDb.isAvailable()) {
382  throw cms::Exception("NotAvailable") << "PoolDBOutputService not available";
383  }
384 
385  edm::LogInfo("Alignment")
386  << "Writing ideal tracker-alignment records.";
387  poolDb->writeOne(&alignments_, since, "TrackerAlignmentRcd");
388  poolDb->writeOne(&alignmentErrors_, since, "TrackerAlignmentErrorExtendedRcd");
389  poolDb->writeOne(&alignmentSurfaceDeformations_, since, "TrackerSurfaceDeformationRcd");
390 }
391 
392 
393 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
394 void
396 {
398  desc.setComment("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",
403  std::vector<std::string>{});
404  desc.addUntracked<bool>("createReferenceRcd", false);
405  descriptions.add("createIdealTkAlRecords", desc);
406 }
407 
408 
409 //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_
int i
Definition: DBlmapReader.cc:9
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
virtual 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:7
std::vector< AlignTransform > m_align
Definition: Alignments.h:19
const std::vector< GeomDetEnumerators::SubDetector > skipSubDetectors_
tuple result
Definition: mps_fire.py:84
uint32_t rawId() const
get the raw id
Definition: DetId.h:43
void setComment(std::string const &value)
const Surface::PositionType & position() const
The position (origin of the R.F.)
Definition: GeomDet.h:48
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)
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 T & get() const
Definition: EventSetup.h:56
T const * product() const
Definition: ESHandle.h:86
double b
Definition: hdecay.h:120
void add(std::string const &label, ParameterSetDescription const &psetDescription)
double a
Definition: hdecay.h:121
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
virtual SubDetector subDetector() const
Which subdetector.
Definition: GeomDet.cc:44
void clear()
Clear vector without having to look into internals:
CLHEP::HepRotation Rotation