Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-01-21 00:19:03

0001 #ifndef Alignment_CommonAlignmentProducer_AlignmentProducerBase_h
0002 #define Alignment_CommonAlignmentProducer_AlignmentProducerBase_h
0003 
0004 /**
0005  * @package   Alignment/CommonAlignmentProducer
0006  * @file      AlignmentProducerBase.h
0007  *
0008  * @author    Gregor Mittag
0009  * @date      2017/03/16
0010  *
0011  * @brief     Alignment producer base class
0012  *
0013  * Abstract base class providing the functionality to produce alignments.
0014  * Derived classes can use its methods to implement the methods of the
0015  * respective Framework module.
0016  *
0017  * At the time of writing, this class can only be used within edm::one or legacy
0018  * modules because it uses the TFileService within the alignment monitors,
0019  * i.e. edm::one modules need to declare this resource in the constructor:
0020  *
0021  * 'usesResource(TFileService::kSharedResource);'.
0022  *
0023  */
0024 
0025 #include <memory>
0026 
0027 #include "Alignment/CommonAlignment/interface/Utilities.h"
0028 #include "Alignment/CommonAlignmentAlgorithm/interface/AlignmentAlgorithmBase.h"
0029 #include "Alignment/CommonAlignmentMonitor/interface/AlignmentMonitorBase.h"
0030 
0031 #include "CondFormats/Alignment/interface/Alignments.h"
0032 #include "CondFormats/AlignmentRecord/interface/CSCAlignmentRcd.h"
0033 #include "CondFormats/AlignmentRecord/interface/CSCAlignmentErrorExtendedRcd.h"
0034 #include "CondFormats/AlignmentRecord/interface/CSCSurveyRcd.h"
0035 #include "CondFormats/AlignmentRecord/interface/CSCSurveyErrorExtendedRcd.h"
0036 #include "CondFormats/AlignmentRecord/interface/DTAlignmentRcd.h"
0037 #include "CondFormats/AlignmentRecord/interface/DTAlignmentErrorExtendedRcd.h"
0038 #include "CondFormats/AlignmentRecord/interface/DTSurveyRcd.h"
0039 #include "CondFormats/AlignmentRecord/interface/DTSurveyErrorExtendedRcd.h"
0040 #include "CondFormats/AlignmentRecord/interface/GlobalPositionRcd.h"
0041 #include "CondFormats/AlignmentRecord/interface/TrackerAlignmentRcd.h"
0042 #include "CondFormats/AlignmentRecord/interface/TrackerAlignmentErrorExtendedRcd.h"
0043 #include "CondFormats/AlignmentRecord/interface/TrackerSurfaceDeformationRcd.h"
0044 #include "CondFormats/AlignmentRecord/interface/TrackerSurveyRcd.h"
0045 #include "CondFormats/AlignmentRecord/interface/TrackerSurveyErrorExtendedRcd.h"
0046 #include "CondFormats/GeometryObjects/interface/PTrackerParameters.h"
0047 #include "CondFormats/Common/interface/Time.h"
0048 
0049 #include "FWCore/Framework/interface/ESHandle.h"
0050 #include "FWCore/Framework/interface/ESWatcher.h"
0051 #include "FWCore/Framework/interface/Frameworkfwd.h"
0052 #include "FWCore/Framework/interface/ConsumesCollector.h"
0053 
0054 #include "Geometry/DTGeometry/interface/DTGeometry.h"
0055 #include "Geometry/CSCGeometry/interface/CSCGeometry.h"
0056 #include "Geometry/GEMGeometry/interface/GEMGeometry.h"
0057 #include "Geometry/CommonTopologies/interface/GeometryAligner.h"
0058 #include "Geometry/TrackerGeometryBuilder/interface/TrackerGeometry.h"
0059 #include "Geometry/Records/interface/IdealGeometryRecord.h"
0060 #include "Geometry/Records/interface/MuonGeometryRecord.h"
0061 #include "Geometry/Records/interface/TrackerTopologyRcd.h"
0062 
0063 #include "TrackingTools/PatternTools/interface/TrajTrackAssociation.h"
0064 
0065 class AlignTransform;
0066 class Alignments;
0067 class AlignmentErrorsExtended;
0068 class AlignmentSurfaceDeformations;
0069 struct SurveyErrors;
0070 class TrackerTopology;
0071 class TrackerDigiGeometryRecord;
0072 
0073 class AlignmentProducerBase {
0074 protected:
0075   AlignmentProducerBase(const edm::ParameterSet&, edm::ConsumesCollector);
0076 
0077   // 'noexcept(false)' is needed currently for multiple inheritance with Framework modules
0078   virtual ~AlignmentProducerBase() noexcept(false);
0079 
0080   /*** Methods used in implementation of derived classes ***/
0081   /// Start processing of events
0082   void startProcessing();
0083 
0084   /// Terminate processing of events
0085   void terminateProcessing(const edm::EventSetup* = nullptr);
0086 
0087   /// Process event
0088   bool processEvent(const edm::Event&, const edm::EventSetup&);
0089 
0090   /// begin run
0091   void beginRunImpl(const edm::Run&, const edm::EventSetup&);
0092 
0093   /// end run
0094   void endRunImpl(const edm::Run&, const edm::EventSetup&);
0095 
0096   /// begin lumi block
0097   void beginLuminosityBlockImpl(const edm::LuminosityBlock&, const edm::EventSetup&);
0098 
0099   /// end lumi block
0100   void endLuminosityBlockImpl(const edm::LuminosityBlock&, const edm::EventSetup&);
0101 
0102   int nEvent() const { return nevent_; }
0103 
0104   /// Creates Geometry and Alignables of the Tracker and initializes the
0105   /// AlignmentAlgorithm @alignmentAlgo_
0106   void initAlignmentAlgorithm(const edm::EventSetup&, bool update = false);
0107 
0108   /// Steers activities after end of job, terminates the AlignmentAlgorithm
0109   /// @alignmentAlgo_
0110   bool finish();
0111 
0112   virtual bool getTrajTrackAssociationCollection(const edm::Event&, edm::Handle<TrajTrackAssociationCollection>&) = 0;
0113   virtual bool getBeamSpot(const edm::Event&, edm::Handle<reco::BeamSpot>&) = 0;
0114   virtual bool getTkFittedLasBeamCollection(const edm::Run&, edm::Handle<TkFittedLasBeamCollection>&) = 0;
0115   virtual bool getTsosVectorCollection(const edm::Run&, edm::Handle<TsosVectorCollection>&) = 0;
0116   virtual bool getAliClusterValueMap(const edm::Event&, edm::Handle<AliClusterValueMap>&) = 0;
0117 
0118   std::shared_ptr<TrackerGeometry> trackerGeometry_;
0119   edm::ESHandle<DTGeometry> muonDTGeometry_;
0120   edm::ESHandle<CSCGeometry> muonCSCGeometry_;
0121   edm::ESHandle<GEMGeometry> muonGEMGeometry_;
0122   const bool doTracker_, doMuon_, useExtras_;
0123 
0124   /// Map with tracks/trajectories
0125   const edm::InputTag tjTkAssociationMapTag_;
0126 
0127   /// BeamSpot
0128   const edm::InputTag beamSpotTag_;
0129 
0130   /// LAS beams in edm::Run (ignore if empty)
0131   const edm::InputTag tkLasBeamTag_;
0132 
0133   /// ValueMap containing associtaion cluster-flag
0134   const edm::InputTag clusterValueMapTag_;
0135 
0136 private:
0137   /// Creates the choosen alignment algorithm
0138   void createAlignmentAlgorithm(edm::ConsumesCollector&);
0139 
0140   /// Creates the monitors
0141   void createMonitors(edm::ConsumesCollector&);
0142 
0143   /// Creates the calibrations
0144   void createCalibrations(edm::ConsumesCollector&);
0145 
0146   /// Checks if one of the EventSetup-Records has changed
0147   bool setupChanged(const edm::EventSetup&);
0148 
0149   /// Initializes Beamspot @beamSpot_ of Alignables @alignableExtras_
0150   void initBeamSpot(const edm::Event&);
0151 
0152   /// Creates ideal geometry @trackerGeometry_ from IdealGeometryRecord
0153   void createGeometries(const edm::EventSetup&, const TrackerTopology*);
0154 
0155   /// Applies Alignments from Database (GlobalPositionRcd) to Geometry
0156   /// @trackerGeometry_
0157   void applyAlignmentsToDB(const edm::EventSetup&);
0158 
0159   /// Creates Alignables @alignableTracker_ from the previously loaded
0160   /// Geometry @trackerGeometry_
0161   void createAlignables(const TrackerTopology*, bool update = false);
0162 
0163   /// Creates the @alignmentParameterStore_, which manages all Alignables
0164   void buildParameterStore();
0165 
0166   /// Applies misalignment scenario to @alignableTracker_
0167   void applyMisalignment();
0168 
0169   /// Applies misalignment scenario to @alignableTracker_
0170   void simpleMisalignment(const align::Alignables&, const std::string&, float, float, bool);
0171 
0172   /// Applies Alignments, AlignmentErrors and SurfaceDeformations to
0173   /// @trackerGeometry_
0174   void applyAlignmentsToGeometry();
0175 
0176   /// Applies DB constants belonging to (Err)Rcd to Geometry, taking into
0177   /// account 'globalPosition' correction.
0178   template <class G, class Rcd, class ErrRcd>
0179   void applyDB(const G*,
0180                const edm::EventSetup&,
0181                const edm::ESGetToken<Alignments, Rcd>&,
0182                const edm::ESGetToken<AlignmentErrorsExtended, ErrRcd>&,
0183                const AlignTransform&) const;
0184 
0185   /// Applies DB constants for SurfaceDeformations
0186   template <class G, class DeformationRcd>
0187   void applyDB(const G*,
0188                const edm::EventSetup&,
0189                const edm::ESGetToken<AlignmentSurfaceDeformations, DeformationRcd>&) const;
0190 
0191   /// Reads in survey records
0192   void readInSurveyRcds(const edm::EventSetup&);
0193 
0194   /// Adds survey info to an Alignable
0195   void addSurveyInfo(Alignable*);
0196 
0197   /// Writes Alignments (i.e. Records) to database-file
0198   void storeAlignmentsToDB();
0199 
0200   /// Writes Alignments and AlignmentErrors for all sub detectors and the
0201   /// given run number
0202   void writeForRunRange(cond::Time_t);
0203 
0204   /// Writes Alignments and/or AlignmentErrors to DB for record names
0205   /// (removes *globalCoordinates before writing if non-null...).
0206   /// Takes over ownership of Alignments and AlignmentErrors.
0207   void writeDB(Alignments*,
0208                const std::string&,
0209                AlignmentErrorsExtended*,
0210                const std::string&,
0211                const AlignTransform*,
0212                cond::Time_t) const;
0213 
0214   /// Writes SurfaceDeformations (bows & kinks) to DB for given record name
0215   /// Takes over ownership of AlignmentSurfaceDeformations.
0216   void writeDB(const AlignmentSurfaceDeformations&, const std::string&, cond::Time_t) const;
0217 
0218   //========================== PRIVATE DATA ====================================
0219   //============================================================================
0220 
0221   /*** Alignment data ***/
0222 
0223   std::unique_ptr<AlignmentAlgorithmBase> alignmentAlgo_;
0224   CalibrationsOwner calibrations_;
0225   AlignmentMonitors monitors_;
0226 
0227   std::unique_ptr<AlignmentParameterStore> alignmentParameterStore_;
0228   std::unique_ptr<AlignableTracker> alignableTracker_;
0229   std::unique_ptr<AlignableMuon> alignableMuon_;
0230   std::unique_ptr<AlignableExtras> alignableExtras_;
0231 
0232   edm::Handle<reco::BeamSpot> beamSpot_;
0233   /// GlobalPositions that might be read from DB, nullptr otherwise
0234   std::unique_ptr<const Alignments> globalPositions_;
0235 
0236   const align::RunRanges uniqueRunRanges_;
0237   int nevent_{0};
0238   bool runAtPCL_{false};
0239 
0240   /*** Parameters from config-file ***/
0241 
0242   edm::ParameterSet config_;
0243 
0244   const int stNFixAlignables_;
0245   const double stRandomShift_, stRandomRotation_;
0246   const bool applyDbAlignment_, checkDbAlignmentValidity_;
0247   const bool doMisalignmentScenario_;
0248   const bool saveToDB_, saveApeToDB_, saveDeformationsToDB_;
0249   const bool useSurvey_;
0250   const bool enableAlignableUpdates_;
0251   const std::string tkAliRcdName_;
0252 
0253   /*** ESTokens ***/
0254   const edm::ESGetToken<TrackerTopology, TrackerTopologyRcd> ttopoToken_;
0255   const edm::ESGetToken<GeometricDet, IdealGeometryRecord> geomDetToken_;
0256   const edm::ESGetToken<PTrackerParameters, PTrackerParametersRcd> ptpToken_;
0257   const edm::ESGetToken<DTGeometry, MuonGeometryRecord> dtGeomToken_;
0258   const edm::ESGetToken<CSCGeometry, MuonGeometryRecord> cscGeomToken_;
0259   const edm::ESGetToken<GEMGeometry, MuonGeometryRecord> gemGeomToken_;
0260 
0261   const edm::ESGetToken<Alignments, TrackerAlignmentRcd> tkAliToken_;
0262   const edm::ESGetToken<Alignments, DTAlignmentRcd> dtAliToken_;
0263   const edm::ESGetToken<Alignments, CSCAlignmentRcd> cscAliToken_;
0264   const edm::ESGetToken<Alignments, GEMAlignmentRcd> gemAliToken_;
0265   const edm::ESGetToken<AlignmentErrorsExtended, TrackerAlignmentErrorExtendedRcd> tkAliErrToken_;
0266   const edm::ESGetToken<AlignmentErrorsExtended, DTAlignmentErrorExtendedRcd> dtAliErrToken_;
0267   const edm::ESGetToken<AlignmentErrorsExtended, CSCAlignmentErrorExtendedRcd> cscAliErrToken_;
0268   const edm::ESGetToken<AlignmentErrorsExtended, GEMAlignmentErrorExtendedRcd> gemAliErrToken_;
0269   const edm::ESGetToken<AlignmentSurfaceDeformations, TrackerSurfaceDeformationRcd> tkSurfDefToken_;
0270 
0271   const edm::ESGetToken<Alignments, GlobalPositionRcd> gprToken_;
0272   const edm::ESGetToken<Alignments, TrackerSurveyRcd> tkSurveyToken_;
0273   const edm::ESGetToken<SurveyErrors, TrackerSurveyErrorExtendedRcd> tkSurvErrorToken_;
0274   const edm::ESGetToken<Alignments, DTSurveyRcd> dtSurveyToken_;
0275   const edm::ESGetToken<SurveyErrors, DTSurveyErrorExtendedRcd> dtSurvErrorToken_;
0276   const edm::ESGetToken<Alignments, CSCSurveyRcd> cscSurveyToken_;
0277   const edm::ESGetToken<SurveyErrors, CSCSurveyErrorExtendedRcd> cscSurvErrorToken_;
0278 
0279   /*** ESWatcher ***/
0280 
0281   edm::ESWatcher<IdealGeometryRecord> watchIdealGeometryRcd_;
0282   edm::ESWatcher<GlobalPositionRcd> watchGlobalPositionRcd_;
0283 
0284   edm::ESWatcher<TrackerAlignmentRcd> watchTrackerAlRcd_;
0285   edm::ESWatcher<TrackerAlignmentErrorExtendedRcd> watchTrackerAlErrorExtRcd_;
0286   edm::ESWatcher<TrackerSurfaceDeformationRcd> watchTrackerSurDeRcd_;
0287 
0288   edm::ESWatcher<DTAlignmentRcd> watchDTAlRcd_;
0289   edm::ESWatcher<DTAlignmentErrorExtendedRcd> watchDTAlErrExtRcd_;
0290   edm::ESWatcher<CSCAlignmentRcd> watchCSCAlRcd_;
0291   edm::ESWatcher<CSCAlignmentErrorExtendedRcd> watchCSCAlErrExtRcd_;
0292 
0293   edm::ESWatcher<TrackerSurveyRcd> watchTkSurveyRcd_;
0294   edm::ESWatcher<TrackerSurveyErrorExtendedRcd> watchTkSurveyErrExtRcd_;
0295   edm::ESWatcher<DTSurveyRcd> watchDTSurveyRcd_;
0296   edm::ESWatcher<DTSurveyErrorExtendedRcd> watchDTSurveyErrExtRcd_;
0297   edm::ESWatcher<CSCSurveyRcd> watchCSCSurveyRcd_;
0298   edm::ESWatcher<CSCSurveyErrorExtendedRcd> watchCSCSurveyErrExtRcd_;
0299 
0300   /*** Survey stuff ***/
0301 
0302   size_t surveyIndex_{0};
0303   const Alignments* surveyValues_{nullptr};
0304   const SurveyErrors* surveyErrors_{nullptr};
0305 
0306   /*** Status flags ***/
0307   bool isAlgoInitialized_{false};
0308   bool isDuringLoop_{false};  // -> needed to ensure correct behaviour in
0309                               //    both, EDLooper and standard framework
0310                               //    modules
0311   cond::Time_t firstRun_{cond::timeTypeSpecs[cond::runnumber].endValue};
0312 };
0313 
0314 template <class G, class Rcd, class ErrRcd>
0315 void AlignmentProducerBase::applyDB(const G* geometry,
0316                                     const edm::EventSetup& iSetup,
0317                                     const edm::ESGetToken<Alignments, Rcd>& aliToken,
0318                                     const edm::ESGetToken<AlignmentErrorsExtended, ErrRcd>& errToken,
0319                                     const AlignTransform& globalCoordinates) const {
0320   // 'G' is the geometry class for that DB should be applied,
0321   // 'Rcd' is the record class for its Alignments
0322   // 'ErrRcd' is the record class for its AlignmentErrorsExtended
0323   // 'globalCoordinates' are global transformation for this geometry
0324 
0325   const Rcd& record = iSetup.get<Rcd>();
0326   if (checkDbAlignmentValidity_) {
0327     const edm::ValidityInterval& validity = record.validityInterval();
0328     const edm::IOVSyncValue first = validity.first();
0329     const edm::IOVSyncValue last = validity.last();
0330     if (first != edm::IOVSyncValue::beginOfTime() || last != edm::IOVSyncValue::endOfTime()) {
0331       throw cms::Exception("DatabaseError")
0332           << "@SUB=AlignmentProducerBase::applyDB"
0333           << "\nTrying to apply " << record.key().name() << " with multiple IOVs in tag.\n"
0334           << "Validity range is " << first.eventID().run() << " - " << last.eventID().run();
0335     }
0336   }
0337 
0338   const Alignments* alignments = &record.get(aliToken);
0339   const AlignmentErrorsExtended* alignmentErrors = &iSetup.getData(errToken);
0340 
0341   GeometryAligner aligner;
0342   aligner.applyAlignments<G>(geometry, alignments, alignmentErrors, globalCoordinates);
0343 }
0344 
0345 template <class G, class DeformationRcd>
0346 void AlignmentProducerBase::applyDB(
0347     const G* geometry,
0348     const edm::EventSetup& iSetup,
0349     const edm::ESGetToken<AlignmentSurfaceDeformations, DeformationRcd>& surfDefToken) const {
0350   // 'G' is the geometry class for that DB should be applied,
0351   // 'DeformationRcd' is the record class for its surface deformations
0352 
0353   const DeformationRcd& record = iSetup.get<DeformationRcd>();
0354   if (checkDbAlignmentValidity_) {
0355     const edm::ValidityInterval& validity = record.validityInterval();
0356     const edm::IOVSyncValue first = validity.first();
0357     const edm::IOVSyncValue last = validity.last();
0358     if (first != edm::IOVSyncValue::beginOfTime() || last != edm::IOVSyncValue::endOfTime()) {
0359       throw cms::Exception("DatabaseError")
0360           << "@SUB=AlignmentProducerBase::applyDB"
0361           << "\nTrying to apply " << record.key().name() << " with multiple IOVs in tag.\n"
0362           << "Validity range is " << first.eventID().run() << " - " << last.eventID().run();
0363     }
0364   }
0365   const AlignmentSurfaceDeformations* surfaceDeformations = &record.get(surfDefToken);
0366 
0367   GeometryAligner aligner;
0368   aligner.attachSurfaceDeformations<G>(geometry, surfaceDeformations);
0369 }
0370 
0371 #endif /* Alignment_CommonAlignmentProducer_AlignmentProducerBase_h */