EModeBit

MeasurementDirection

MillePedeAlignmentAlgorithm

Macros

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
#ifndef Alignment_MillePedeAlignmentAlgorithm_MillePedeAlignmentAlgorithm_h
#define Alignment_MillePedeAlignmentAlgorithm_MillePedeAlignmentAlgorithm_h

/// \class MillePedeAlignmentAlgorithm
///
///  CMSSW interface to pede: produces pede's binary input and steering file(s)
///
///  \author    : Gero Flucke
///  date       : October 2006
///  $Revision: 1.36 $
///  $Date: 2012/08/10 09:01:11 $
///  (last update by $Author: flucke $)

#include "Alignment/CommonAlignmentAlgorithm/interface/AlignmentAlgorithmPluginFactory.h"
#include "Alignment/CommonAlignmentAlgorithm/interface/AlignmentAlgorithmBase.h"

#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/Framework/interface/ConsumesCollector.h"

#include "TrackingTools/TrajectoryState/interface/TrajectoryStateOnSurface.h"
#include "TrackingTools/TransientTrackingRecHit/interface/TransientTrackingRecHit.h"

#include "Alignment/ReferenceTrajectories/interface/ReferenceTrajectoryBase.h"

#include "DataFormats/TrackerCommon/interface/TrackerTopology.h"
#include "Geometry/Records/interface/TrackerTopologyRcd.h"

#include "Geometry/TrackerGeometryBuilder/interface/PixelTopologyMap.h"
#include "Geometry/TrackerGeometryBuilder/interface/TrackerGeometry.h"
#include "Geometry/Records/interface/TrackerDigiGeometryRecord.h"

#include "CondFormats/PCLConfig/interface/AlignPCLThresholdsHG.h"
#include "CondFormats/DataRecord/interface/AlignPCLThresholdsHGRcd.h"

#include "CondFormats/SiPixelObjects/interface/SiPixelQuality.h"
#include "CondFormats/DataRecord/interface/SiPixelQualityFromDbRcd.h"

#include <vector>
#include <string>
#include <memory>

class Alignable;
class AlignableTracker;
class AlignableMuon;
class AlignableExtras;

class AlignmentParameters;
class AlignableNavigator;
class AlignableDetOrUnitPtr;
class AlignmentUserVariables;

class AlignmentParameterStore;

class IntegratedCalibrationBase;

class MillePedeMonitor;
class PedeSteerer;
class PedeLabelerBase;
class Mille;
class TrajectoryFactoryBase;

// already from base class - and forward declaration does not work since typedef!
/* class TkFittedLasBeamCollection; */
/* class TsosVectorCollection; */

class MillePedeAlignmentAlgorithm : public AlignmentAlgorithmBase {
public:
  /// Constructor
  MillePedeAlignmentAlgorithm(const edm::ParameterSet &cfg, edm::ConsumesCollector &iC);

  /// Destructor
  ~MillePedeAlignmentAlgorithm() override;

  /// Called at beginning of job
  void initialize(const edm::EventSetup &setup,
                  AlignableTracker *tracker,
                  AlignableMuon *muon,
                  AlignableExtras *extras,
                  AlignmentParameterStore *store) override;

  /// Returns whether MP supports calibrations
  bool supportsCalibrations() override;
  /// Pass integrated calibrations to Millepede (they are not owned by Millepede!)
  bool addCalibrations(const std::vector<IntegratedCalibrationBase *> &iCals) override;

  virtual bool storeThresholds(const int &nRecords,
                               const AlignPCLThresholdsHG::threshold_map &thresholdMap,
                               const AlignPCLThresholdsHG::param_map &floatMap);

  /// Called at end of job
  void terminate(const edm::EventSetup &iSetup) override;
  /// Called at end of job
  void terminate() override;

  /// Returns whether MP should process events in the current configuration
  bool processesEvents() override;

  /// Returns whether MP produced results to be stored
  bool storeAlignments() override;

  /// Run the algorithm on trajectories and tracks
  void run(const edm::EventSetup &setup, const EventInfo &eventInfo) override;

  /// called at begin of run
  void beginRun(const edm::Run &run, const edm::EventSetup &setup, bool changed) override;

  // TODO: This method does NOT match endRun() in base class! Nobody is
  //       calling this?
  /// Run on run products, e.g. TkLAS
  virtual void endRun(const EventInfo &, const EndRunInfo &,
                      const edm::EventSetup &);  //override;

  // This one will be called since it matches the interface of the base class
  void endRun(const EndRunInfo &runInfo, const edm::EventSetup &setup) override;

  /// called at begin of luminosity block (resets Mille binary in mille mode)
  void beginLuminosityBlock(const edm::EventSetup &) override;

  /// called at end of luminosity block
  void endLuminosityBlock(const edm::EventSetup &) override;

  /// Called in order to pass parameters to alignables for a specific run
  /// range in case the algorithm supports run range dependent alignment.
  bool setParametersForRunRange(const RunRange &runrange) override;

private:
  enum MeasurementDirection { kLocalX = 0, kLocalY };

  /// fill mille for a trajectory, returning number of x/y hits ([0,0] if 'bad' trajectory)
  std::pair<unsigned int, unsigned int> addReferenceTrajectory(
      const edm::EventSetup &setup,
      const EventInfo &eventInfo,
      const ReferenceTrajectoryBase::ReferenceTrajectoryPtr &refTrajPtr);

  /// If hit is usable: callMille for x and (probably) y direction.
  /// If globalDerivatives fine: returns 2 if 2D-hit, 1 if 1D-hit, 0 if no Alignable for hit.
  /// Returns -1 if any problem (for params cf. globalDerivativesHierarchy)
  int addMeasurementData(const edm::EventSetup &setup,
                         const EventInfo &eventInfo,
                         const ReferenceTrajectoryBase::ReferenceTrajectoryPtr &refTrajPtr,
                         unsigned int iHit,
                         AlignmentParameters *&params);

  /// Add global data (labels, derivatives) to GBL trajectory
  /// Returns -1 if any problem (for params cf. globalDerivativesHierarchy)
  int addGlobalData(const edm::EventSetup &setup,
                    const EventInfo &eventInfo,
                    const ReferenceTrajectoryBase::ReferenceTrajectoryPtr &refTrajPtr,
                    unsigned int iHit,
                    gbl::GblPoint &gblPoint);

  /// Increase hit counting of MillePedeVariables behind each parVec[i]
  /// (and also for parameters higher in hierarchy),
  /// assuming 'parVec' and 'validHitVecY' to be parallel.
  /// Returns number of valid y-hits.
  unsigned int addHitCount(const std::vector<AlignmentParameters *> &parVec,
                           const std::vector<bool> &validHitVecY) const;

  /// adds data from reference trajectory from a specific Hit
  template <typename CovarianceMatrix, typename ResidualMatrix, typename LocalDerivativeMatrix>
  void addRefTrackData2D(const ReferenceTrajectoryBase::ReferenceTrajectoryPtr &refTrajPtr,
                         unsigned int iTrajHit,
                         Eigen::MatrixBase<CovarianceMatrix> &aHitCovarianceM,
                         Eigen::MatrixBase<ResidualMatrix> &aHitResidualsM,
                         Eigen::MatrixBase<LocalDerivativeMatrix> &aLocalDerivativesM);

  /// adds data for virtual measurements from reference trajectory
  void addVirtualMeas(const ReferenceTrajectoryBase::ReferenceTrajectoryPtr &refTrajPtr, unsigned int iVirtualMeas);

  /// adds data for a specific virtual measurement from reference trajectory
  template <typename CovarianceMatrix, typename ResidualMatrix, typename LocalDerivativeMatrix>
  void addRefTrackVirtualMeas1D(const ReferenceTrajectoryBase::ReferenceTrajectoryPtr &refTrajPtr,
                                unsigned int iVirtualMeas,
                                Eigen::MatrixBase<CovarianceMatrix> &aHitCovarianceM,
                                Eigen::MatrixBase<ResidualMatrix> &aHitResidualsM,
                                Eigen::MatrixBase<LocalDerivativeMatrix> &aLocalDerivativesM);

  /// recursively adding derivatives and labels, false if problems
  bool globalDerivativesHierarchy(const EventInfo &eventInfo,
                                  const TrajectoryStateOnSurface &tsos,
                                  Alignable *ali,
                                  const AlignableDetOrUnitPtr &alidet,
                                  std::vector<float> &globalDerivativesX,
                                  std::vector<float> &globalDerivativesY,
                                  std::vector<int> &globalLabels,
                                  AlignmentParameters *&lowestParams) const;

  /// recursively adding derivatives (double) and labels, false if problems
  bool globalDerivativesHierarchy(const EventInfo &eventInfo,
                                  const TrajectoryStateOnSurface &tsos,
                                  Alignable *ali,
                                  const AlignableDetOrUnitPtr &alidet,
                                  std::vector<double> &globalDerivativesX,
                                  std::vector<double> &globalDerivativesY,
                                  std::vector<int> &globalLabels,
                                  AlignmentParameters *&lowestParams) const;

  /// adding derivatives from integrated calibrations
  void globalDerivativesCalibration(const TransientTrackingRecHit::ConstRecHitPointer &recHit,
                                    const TrajectoryStateOnSurface &tsos,
                                    const edm::EventSetup &setup,
                                    const EventInfo &eventInfo,
                                    std::vector<float> &globalDerivativesX,
                                    std::vector<float> &globalDerivativesY,
                                    std::vector<int> &globalLabels) const;

  /// calls callMille1D or callMille2D
  int callMille(const ReferenceTrajectoryBase::ReferenceTrajectoryPtr &refTrajPtr,
                unsigned int iTrajHit,
                const std::vector<int> &globalLabels,
                const std::vector<float> &globalDerivativesX,
                const std::vector<float> &globalDerivativesY);

  /// calls Mille for 1D hits
  int callMille1D(const ReferenceTrajectoryBase::ReferenceTrajectoryPtr &refTrajPtr,
                  unsigned int iTrajHit,
                  const std::vector<int> &globalLabels,
                  const std::vector<float> &globalDerivativesX);

  /// calls Mille for x and possibly y component of hit,
  /// y is skipped for non-real 2D (e.g. SiStripRecHit2D),
  /// for TID/TEC first diagonalises if correlation is larger than configurable
  int callMille2D(const ReferenceTrajectoryBase::ReferenceTrajectoryPtr &refTrajPtr,
                  unsigned int iTrajHit,
                  const std::vector<int> &globalLabels,
                  const std::vector<float> &globalDerivativesx,
                  const std::vector<float> &globalDerivativesy);

  template <typename CovarianceMatrix,
            typename LocalDerivativeMatrix,
            typename ResidualMatrix,
            typename GlobalDerivativeMatrix>
  void diagonalize(Eigen::MatrixBase<CovarianceMatrix> &aHitCovarianceM,
                   Eigen::MatrixBase<LocalDerivativeMatrix> &aLocalDerivativesM,
                   Eigen::MatrixBase<ResidualMatrix> &aHitResidualsM,
                   Eigen::MatrixBase<GlobalDerivativeMatrix> &aGlobalDerivativesM) const;

  // deals with the non matrix format of theFloatBufferX ...
  template <typename GlobalDerivativeMatrix>
  void makeGlobDerivMatrix(const std::vector<float> &globalDerivativesx,
                           const std::vector<float> &globalDerivativesy,
                           Eigen::MatrixBase<GlobalDerivativeMatrix> &aGlobalDerivativesM);

  //   void callMille(const ReferenceTrajectoryBase::ReferenceTrajectoryPtr &refTrajPtr,
  //               unsigned int iTrajHit, MeasurementDirection xOrY,
  //               const std::vector<float> &globalDerivatives, const std::vector<int> &globalLabels);
  /// true if hit belongs to 2D detector (currently tracker specific)
  bool is2D(const TransientTrackingRecHit::ConstRecHitPointer &recHit) const;

  /// read pede input defined by 'psetName', flag to create/not create MillePedeVariables
  bool readFromPede(const edm::ParameterSet &mprespset, bool setUserVars, const RunRange &runrange);
  bool areEmptyParams(const align::Alignables &alignables) const;
  unsigned int doIO(int loop) const;
  /// add MillePedeVariables for each AlignmentParameters (exception if no parameters...)
  void buildUserVariables(const align::Alignables &alignables) const;

  /// Generates list of files to read, given the list and dir from the configuration.
  /// This will automatically expand formatting directives, if they appear.
  std::vector<std::string> getExistingFormattedFiles(const std::vector<std::string> &plainFiles,
                                                     const std::string &theDir);

  void addLaserData(const EventInfo &eventInfo,
                    const TkFittedLasBeamCollection &tkLasBeams,
                    const TsosVectorCollection &tkLasBeamTsoses);
  void addLasBeam(const EventInfo &eventInfo,
                  const TkFittedLasBeam &lasBeam,
                  const std::vector<TrajectoryStateOnSurface> &tsoses);

  /// add measurement data from PXB survey
  void addPxbSurvey(const edm::ParameterSet &pxbSurveyCfg);

  //
  bool areIOVsSpecified() const;

  //--------------------------------------------------------
  // Data members
  //--------------------------------------------------------

  const edm::ESGetToken<TrackerTopology, TrackerTopologyRcd> topoToken_;
  const edm::ESGetToken<AlignPCLThresholdsHG, AlignPCLThresholdsHGRcd> aliThrToken_;
  const edm::ESGetToken<SiPixelQuality, SiPixelQualityFromDbRcd> siPixelQualityToken_;
  const edm::ESGetToken<TrackerGeometry, TrackerDigiGeometryRecord> geomToken_;

  enum EModeBit { myMilleBit = 1 << 0, myPedeRunBit = 1 << 1, myPedeSteerBit = 1 << 2, myPedeReadBit = 1 << 3 };
  unsigned int decodeMode(const std::string &mode) const;
  bool isMode(unsigned int testMode) const { return (theMode & testMode); }
  bool addHitStatistics(int fromLoop, const std::string &outFile, const std::vector<std::string> &inFiles) const;
  bool addHits(const align::Alignables &alis, const std::vector<AlignmentUserVariables *> &mpVars) const;

  edm::ParameterSet theConfig;
  unsigned int theMode;
  std::string theDir;  /// directory for all kind of files
  AlignmentParameterStore *theAlignmentParameterStore;
  align::Alignables theAlignables;
  std::unique_ptr<AlignableNavigator> theAlignableNavigator;
  std::unique_ptr<MillePedeMonitor> theMonitor;
  std::unique_ptr<Mille> theMille;
  std::shared_ptr<PedeLabelerBase> thePedeLabels;
  std::unique_ptr<PedeSteerer> thePedeSteer;
  std::unique_ptr<TrajectoryFactoryBase> theTrajectoryFactory;
  std::vector<IntegratedCalibrationBase *> theCalibrations;
  std::shared_ptr<AlignPCLThresholdsHG> theThresholds;
  std::shared_ptr<PixelTopologyMap> pixelTopologyMap;
  std::shared_ptr<SiPixelQuality> pixelQuality;
  unsigned int theMinNumHits;
  double theMaximalCor2D;  /// maximal correlation allowed for 2D hit in TID/TEC.
                           /// If larger, the 2D measurement gets diagonalized!!!
  const align::RunNumber firstIOV_;
  const bool ignoreFirstIOVCheck_;
  const bool enableAlignableUpdates_;
  int theLastWrittenIov;  // keeping track for output trees...
  std::vector<float> theFloatBufferX;
  std::vector<float> theFloatBufferY;
  std::vector<int> theIntBuffer;
  bool theDoSurveyPixelBarrel;
  // CHK for GBL
  std::unique_ptr<gbl::MilleBinary> theBinary;
  bool theGblDoubleBinary;

  const bool runAtPCL_;
  const bool ignoreHitsWithoutGlobalDerivatives_;
  const bool skipGlobalPositionRcdCheck_;

  const align::RunRanges uniqueRunRanges_;
  const bool enforceSingleIOVInput_;
  std::vector<align::RunNumber> cachedRuns_;
  align::RunNumber lastProcessedRun_;
};

DEFINE_EDM_PLUGIN(AlignmentAlgorithmPluginFactory, MillePedeAlignmentAlgorithm, "MillePedeAlignmentAlgorithm");
#endif