Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-06-24 22:51:33

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