Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:47:49

0001 /****************************************************************************
0002 * Authors: 
0003 *  Jan Kašpar (jan.kaspar@gmail.com) 
0004 *  Cristian Baldenegro (crisx.baldenegro@gmail.com)
0005 ****************************************************************************/
0006 
0007 #ifndef CalibPPS_AlignmentRelative_StraightTrackAlignment_h
0008 #define CalibPPS_AlignmentRelative_StraightTrackAlignment_h
0009 
0010 #include <set>
0011 #include <vector>
0012 #include <string>
0013 
0014 #include <TMatrixD.h>
0015 #include <TVectorD.h>
0016 #include <TFile.h>
0017 
0018 #include "DataFormats/Common/interface/DetSetVector.h"
0019 #include "DataFormats/CTPPSReco/interface/TotemRPRecHit.h"
0020 #include "DataFormats/CTPPSReco/interface/TotemRPUVPattern.h"
0021 #include "DataFormats/CTPPSReco/interface/CTPPSDiamondRecHit.h"
0022 #include "DataFormats/CTPPSReco/interface/CTPPSPixelRecHit.h"
0023 #include "DataFormats/CTPPSReco/interface/CTPPSPixelLocalTrack.h"
0024 
0025 #include "CondFormats/PPSObjects/interface/CTPPSRPAlignmentCorrectionsData.h"
0026 
0027 #include "CalibPPS/AlignmentRelative/interface/AlignmentGeometry.h"
0028 #include "CalibPPS/AlignmentRelative/interface/HitCollection.h"
0029 #include "CalibPPS/AlignmentRelative/interface/AlignmentAlgorithm.h"
0030 #include "CalibPPS/AlignmentRelative/interface/AlignmentConstraint.h"
0031 #include "CalibPPS/AlignmentRelative/interface/AlignmentTask.h"
0032 #include "CalibPPS/AlignmentRelative/interface/LocalTrackFit.h"
0033 #include "CalibPPS/AlignmentRelative/interface/LocalTrackFitter.h"
0034 #include "FWCore/Framework/interface/ESHandle.h"
0035 
0036 namespace edm {
0037   class ParameterSet;
0038   class Event;
0039   class EventSetup;
0040 }  // namespace edm
0041 
0042 class TH1D;
0043 class TGraph;
0044 
0045 /**
0046  *\brief Track-based alignment using straight tracks.
0047  **/
0048 class StraightTrackAlignment {
0049 public:
0050   StraightTrackAlignment(const edm::ParameterSet &);
0051   virtual ~StraightTrackAlignment();
0052 
0053   virtual void begin(edm::ESHandle<CTPPSRPAlignmentCorrectionsData> hRealAlignment,
0054                      edm::ESHandle<CTPPSGeometry> hRealGeometry,
0055                      edm::ESHandle<CTPPSGeometry> hMisalignedGeometry);
0056 
0057   virtual void processEvent(const edm::EventID &eventId,
0058                             const edm::DetSetVector<TotemRPUVPattern> &uvPatternsStrip,
0059                             const edm::DetSetVector<CTPPSDiamondRecHit> &hitsDiamond,
0060                             const edm::DetSetVector<CTPPSPixelRecHit> &hitsPixel,
0061                             const edm::DetSetVector<CTPPSPixelLocalTrack> &tracksPixel);
0062 
0063   /// performs analyses and fill results variable
0064   virtual void finish();
0065 
0066 protected:
0067   // ---------- input parameters -----------
0068 
0069   /// verbosity level
0070   unsigned int verbosity;
0071 
0072   /// list of RPs for which the alignment parameters shall be optimized
0073   std::vector<unsigned int> rpIds;
0074 
0075   /// list of planes to be excluded from processing
0076   std::vector<unsigned int> excludePlanes;
0077 
0078   /// a characteristic z in mm
0079   /// to keep values of z small - this helps the numerical solution
0080   double z0;
0081 
0082   /// the collection of the alignment algorithms
0083   std::vector<AlignmentAlgorithm *> algorithms;
0084 
0085   /// constraint types
0086   enum ConstraintsType { ctFixedDetectors, ctStandard };
0087 
0088   /// the chosen type of constraints
0089   ConstraintsType constraintsType;
0090 
0091   /// stops after this event number has been reached
0092   signed int maxEvents;
0093 
0094   // ---------- hit/track selection parameters ----------
0095 
0096   /// remove events with impossible signatures (i.e. simultaneously top and bottom)
0097   bool removeImpossible;
0098 
0099   /// select only tracks with activity in minimal number of units
0100   unsigned int requireNumberOfUnits;
0101 
0102   /// if a track goes through overlap, select it only if it leaves signal in at least 3 pots
0103   bool requireAtLeast3PotsInOverlap;
0104 
0105   /// if true, only track through vertical-horizontal overlap are seleceted
0106   bool requireOverlap;
0107 
0108   /// whether to cut on chi^2/ndf
0109   bool cutOnChiSqPerNdf;
0110 
0111   /// the value of chi^2/ndf cut threshold
0112   double chiSqPerNdfCut;
0113 
0114   /// cuts on absolute values of the track angle
0115   double maxTrackAx;
0116   double maxTrackAy;
0117 
0118   /// list of RP sets accepted irrespective of the other "require" settings
0119   std::vector<std::set<unsigned int> > additionalAcceptedRPSets;
0120 
0121   // ---------- output parameters ----------
0122 
0123   /// file name prefix for result files
0124   std::string fileNamePrefix;
0125 
0126   /// file name prefix for cumulative result files
0127   std::string cumulativeFileNamePrefix;
0128 
0129   /// file name prefix for cumulative expanded result files
0130   std::string expandedFileNamePrefix;
0131 
0132   /// file name prefix for cumulative factored result files
0133   std::string factoredFileNamePrefix;
0134 
0135   /// whether to use long format (many decimal digits) when saving XML files
0136   bool preciseXMLFormat;
0137 
0138   /// whether to save uncertainties in the result XML files
0139   bool saveXMLUncertainties;
0140 
0141   /// whether itermediate results (S, CS matrices) of alignments shall be saved
0142   bool saveIntermediateResults;
0143 
0144   /// the name task data file
0145   std::string taskDataFileName;
0146 
0147   /// the file with task data
0148   TFile *taskDataFile;
0149 
0150   // ---------- internal data members ----------
0151 
0152   /// the alignment task to be solved
0153   AlignmentTask task;
0154 
0155   /// track fitter
0156   LocalTrackFitter fitter;
0157 
0158   /// (real geometry) alignments before this alignment iteration
0159   CTPPSRPAlignmentCorrectionsData initialAlignments;
0160 
0161   // ---------- diagnostics parameters and plots ----------
0162 
0163   /// whether to build and save diagnostic plots
0164   bool buildDiagnosticPlots;
0165 
0166   /// file name for some event selection statistics
0167   std::string diagnosticsFile;
0168 
0169   signed int eventsTotal;     ///< counter of events
0170   signed int eventsFitted;    ///< counter of processed tracks
0171   signed int eventsSelected;  ///< counter of processed tracks
0172 
0173   std::map<std::set<unsigned int>, unsigned long> fittedTracksPerRPSet;  ///< counter of fitted tracks in a certain RP set
0174   std::map<std::set<unsigned int>, unsigned long>
0175       selectedTracksPerRPSet;  ///< counter of selected tracks in a certain RP set
0176 
0177   std::map<unsigned int, unsigned int> selectedHitsPerPlane;  ///< counter of selected hits per plane
0178 
0179   TH1D *fitNdfHist_fitted, *fitNdfHist_selected;  ///< fit num. of degrees of freedom histograms for all/selected tracks
0180   TH1D *fitPHist_fitted, *fitPHist_selected;      ///< fit p-value histograms for all/selected tracks
0181   TH1D *fitAxHist_fitted, *fitAxHist_selected;    ///< fit ax histograms for all/selected tracks
0182   TH1D *fitAyHist_fitted, *fitAyHist_selected;    ///< fit ay histograms for all/selected tracks
0183   TH1D *fitBxHist_fitted, *fitBxHist_selected;    ///< fit bx histograms for all/selected tracks
0184   TH1D *fitByHist_fitted, *fitByHist_selected;    ///< fit by histograms for all/selected tracks
0185 
0186   struct RPSetPlots {
0187     std::string name;
0188 
0189     /// normalised chi^2 histograms for all/selected tracks, in linear/logarithmic scale
0190     TH1D *chisqn_lin_fitted = nullptr, *chisqn_lin_selected = nullptr, *chisqn_log_fitted = nullptr,
0191          *chisqn_log_selected = nullptr;
0192 
0193     /// plots ax vs. ay
0194     TGraph *fitAxVsAyGraph_fitted = nullptr, *fitAxVsAyGraph_selected = nullptr;
0195 
0196     /// plots bx vs. by
0197     TGraph *fitBxVsByGraph_fitted = nullptr, *fitBxVsByGraph_selected = nullptr;
0198 
0199     RPSetPlots() {}
0200 
0201     RPSetPlots(const std::string &_name);
0202 
0203     void free();
0204 
0205     void write() const;
0206   };
0207 
0208   /// global (all RP sets) chi^2 histograms
0209   RPSetPlots globalPlots;
0210 
0211   /// chi^2 histograms per RP set
0212   std::map<std::set<unsigned int>, RPSetPlots> rpSetPlots;
0213 
0214   /// map: detector id --> residua histogram
0215   struct ResiduaHistogramSet {
0216     TH1D *total_fitted, *total_selected;
0217     TGraph *selected_vs_chiSq;
0218     std::map<std::set<unsigned int>, TH1D *> perRPSet_fitted, perRPSet_selected;
0219   };
0220 
0221   /// residua histograms
0222   std::map<unsigned int, ResiduaHistogramSet> residuaHistograms;
0223 
0224   // ----------- methods ------------
0225 
0226   /// creates a new residua histogram
0227   TH1D *newResiduaHist(const char *name);
0228 
0229   /// fits the collection of hits and removes hits with too high residual/sigma ratio
0230   /// \param failed whether the fit has failed
0231   /// \param selectionChanged whether some hits have been removed
0232   void fitLocalTrack(HitCollection &, LocalTrackFit &, bool &failed, bool &selectionChanged);
0233 
0234   /// removes the hits of pots with too few planes active
0235   void removeInsufficientPots(HitCollection &, bool &selectionChanged);
0236 
0237   /// builds a selected set of constraints
0238   void buildConstraints(std::vector<AlignmentConstraint> &);
0239 
0240   /// fills diagnostic (chi^2, residua, ...) histograms
0241   void updateDiagnosticHistograms(const HitCollection &selection,
0242                                   const std::set<unsigned int> &selectedRPs,
0243                                   const LocalTrackFit &trackFit,
0244                                   bool trackSelected);
0245 
0246   /// converts a set to string
0247   static std::string setToString(const std::set<unsigned int> &);
0248 
0249   /// result pretty printing routines
0250   void printN(const char *str, unsigned int N);
0251   void printLineSeparator(const std::vector<std::map<unsigned int, AlignmentResult> > &);
0252   void printQuantitiesLine(const std::vector<std::map<unsigned int, AlignmentResult> > &);
0253   void printAlgorithmsLine(const std::vector<std::map<unsigned int, AlignmentResult> > &);
0254 
0255   /// saves a ROOT file with diagnostic plots
0256   void saveDiagnostics() const;
0257 };
0258 
0259 #endif