CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
MuonResidualsFitter.h
Go to the documentation of this file.
1 #ifndef Alignment_MuonAlignmentAlgorithms_MuonResidualsFitter_H
2 #define Alignment_MuonAlignmentAlgorithms_MuonResidualsFitter_H
3 
12 #ifndef STANDALONE_FITTER
16 #endif
17 
18 #include "TMinuit.h"
19 #include "TH1F.h"
20 #include "TProfile.h"
21 #include "TF1.h"
22 #include "TMath.h"
23 #include "TRandom3.h"
24 #include "TMatrixDSym.h"
25 
26 #include <cstdio>
27 #include <iostream>
28 #include <string>
29 #include <sstream>
30 #include <map>
31 
32 // some mock classes for the case if we want to compile fitters with pure root outside of CMSSW
33 #ifdef STANDALONE_FITTER
34 
35 #include <cassert>
36 
37 class Alignable {
38 public:
39  struct Surface {
40  double width() { return 300.; }
41  double length() { return 300.; }
42  };
43  Surface s;
44  Surface &surface() { return s; }
45 };
46 
47 class TFileDirectory {
48 public:
49  template <typename T, typename A1, typename A2, typename A3, typename A4, typename A5>
50  T *make(const A1 &a1, const A2 &a2, const A3 &a3, const A4 &a4, const A5 &a5) const {
51  T *t = new T(a1, a2, a3, a4, a5);
52  return t;
53  }
54  template <typename T, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7>
55  T *make(const A1 &a1, const A2 &a2, const A3 &a3, const A4 &a4, const A5 &a5, const A6 &a6, const A7 &a7) const {
56  T *t = new T(a1, a2, a3, a4, a5, a6, a7);
57  return t;
58  }
59  template <typename T, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7, typename A8>
60  T *make(const A1 &a1, const A2 &a2, const A3 &a3, const A4 &a4, const A5 &a5, const A6 &a6, const A7 &a7, const A8 &a8)
61  const {
62  T *t = new T(a1, a2, a3, a4, a5, a6, a7, a8);
63  return t;
64  }
65 };
66 
67 #include <exception>
68 namespace cms {
69  class Exception : public std::exception {
70  public:
71  Exception(const std::string &s) : name(s) {}
72  ~Exception() throw() {}
74  template <class T>
75  Exception &operator<<(const T &what) {
76  std::cout << name << " exception: " << what << std::endl;
77  return *this;
78  }
79  };
80 } // namespace cms
81 
82 #endif // STANDALONE_FITTER
83 
85 public:
87 
89 
91 
93  Bool_t is_plus; // for +- endcap
94  Bool_t is_dt; // DT or CSC
95  UChar_t station; // 8bit uint
96  Char_t ring_wheel;
97  UChar_t sector;
98  Float_t res_x;
99  Float_t res_y;
100  Float_t res_slope_x;
101  Float_t res_slope_y;
102  Float_t pos_x;
103  Float_t pos_y;
104  Float_t angle_x;
105  Float_t angle_y;
106  Float_t pz;
107  Float_t pt;
108  Char_t q;
109  Bool_t select;
110  };
111 
113  virtual ~MuonResidualsFitter();
114 
115  virtual int type() const = 0;
116  virtual int npar() = 0;
117  virtual int ndata() = 0;
118 
119  int useRes(int pattern = -1) {
120  if (pattern >= 0)
121  m_useResiduals = pattern;
122  return m_useResiduals;
123  }
124  int residualsModel() const { return m_residualsModel; }
125  long numResiduals() const { return m_residuals.size(); }
126 
127  void fix(int parNum, bool dofix = true);
128  bool fixed(int parNum);
129 
130  int nfixed() { return std::count(m_fixed.begin(), m_fixed.end(), true); }
131 
132  void setPrintLevel(int printLevel) { m_printLevel = printLevel; }
133  void setStrategy(int strategy) { m_strategy = strategy; }
134 
135  void setInitialValue(int parNum, double value) { m_parNum2InitValue[parNum] = value; }
136 
137  // an array of the actual residual and associated baggage (qoverpt, trackangle, trackposition)
138  // arrays passed to fill() are "owned" by MuonResidualsFitter: MuonResidualsFitter will delete them, don't do it yourself!
139  void fill(double *residual);
140 
141  // this block of results is only valid if fit() returns true
142  // also gamma is only valid if the model is kPowerLawTails or kROOTVoigt
143  virtual bool fit(Alignable *ali) = 0;
144 
145  double value(int parNum) {
146  assert(0 <= parNum && parNum < npar());
147  return m_value[parNum];
148  }
149  double errorerror(int parNum) {
150  assert(0 <= parNum && parNum < npar());
151  return m_error[parNum];
152  }
153 
154  // parNum corresponds to the parameter number defined by enums in specific fitters
155  // parIdx is a continuous index in a set of parameters
156  int parNum2parIdx(int parNum) { return m_parNum2parIdx[parNum]; }
157 
158  TMatrixDSym covarianceMatrix() { return m_cov; }
159  double covarianceElement(int parNum1, int parNum2);
160  TMatrixDSym correlationMatrix();
161 
162  double loglikelihood() { return m_loglikelihood; }
163 
164  long numsegments();
165 
166  virtual double sumofweights() = 0;
167 
168  // demonstration plots; return reduced chi**2
169  virtual double plot(std::string name, TFileDirectory *dir, Alignable *ali) = 0;
170 
171  void plotsimple(std::string name, TFileDirectory *dir, int which, double multiplier);
172  void plotweighted(std::string name, TFileDirectory *dir, int which, int whichredchi2, double multiplier);
173 
174 #ifdef STANDALONE_FITTER
175  Alignable m_ali;
176  TFileDirectory m_dir;
177  bool fit() { return fit(&m_ali); }
178  virtual double plot(std::string &name) { return plot(name, &m_dir, &m_ali); }
179  void plotsimple(std::string &name, int which, double multiplier) { plotsimple(name, &m_dir, which, multiplier); }
180  void plotweighted(std::string &name, int which, int whichredchi2, double multiplier) {
181  plotweighted(name, &m_dir, which, whichredchi2, multiplier);
182  }
183 #endif
184 
185  // I/O of temporary files for collect mode
186  void write(FILE *file, int which = 0);
187  void read(FILE *file, int which = 0);
188 
189  // these are for the FCN to access what it needs to
190  std::vector<double *>::const_iterator residuals_begin() const { return m_residuals.begin(); }
191  std::vector<double *>::const_iterator residuals_end() const { return m_residuals.end(); }
192 
193  void computeHistogramRangeAndBinning(int which, int &nbins, double &a, double &b);
194  void histogramChi2GaussianFit(int which, double &fit_mean, double &fit_sigma);
195  void selectPeakResiduals_simple(double nsigma, int nvar, int *vars);
196  void selectPeakResiduals(double nsigma, int nvar, int *vars);
197 
198  // void fiducialCuts(double xMin = -1000, double xMax = 1000, double yMin = -1000, double yMax = 1000, bool fidcut1=true); // "No fiducial cut"
199  // void fiducialCuts(double xMin = -80.0, double xMax = 80.0, double yMin = -80.0, double yMax = 80.0, bool fidcut1=true); // "old" fiducial cut
200  void fiducialCuts(double xMin = -80.0,
201  double xMax = 80.0,
202  double yMin = -80.0,
203  double yMax = 80.0,
204  bool fidcut1 = false); // "new" fiducial cut
205 
206  virtual void correctBField() = 0;
207  virtual void correctBField(int idx_momentum, int idx_q);
208 
209  std::vector<bool> &selectedResidualsFlags() { return m_residuals_ok; }
210 
212 
213 protected:
214  void initialize_table();
215  bool dofit(void (*fcn)(int &, double *, double &, double *, int),
216  std::vector<int> &parNum,
217  std::vector<std::string> &parName,
218  std::vector<double> &start,
219  std::vector<double> &step,
220  std::vector<double> &low,
221  std::vector<double> &high);
222  virtual void inform(TMinuit *tMinuit) = 0;
223 
228  std::vector<bool> m_fixed;
229  std::map<int, double> m_parNum2InitValue;
231 
232  std::vector<double *> m_residuals;
233  std::vector<bool> m_residuals_ok;
234 
235  std::vector<double> m_value;
236  std::vector<double> m_error;
237  TMatrixDSym m_cov;
239 
240  std::map<int, int> m_parNum2parIdx;
241 
242  // center and radii of ellipsoid for peak selection
243  // NOTE: 10 is a hardcoded maximum of ellipsoid variables!!!
244  // but I can't imagine it ever becoming larger
245  double m_center[20];
246  double m_radii[20];
247 };
248 
249 // A ROOT-sponsored hack to get information into the fit function
250 class MuonResidualsFitterFitInfo : public TObject {
251 public:
254 
255 private:
257 #ifdef STANDALONE_FITTER
258  ClassDef(MuonResidualsFitterFitInfo, 1);
259 #endif
260 };
261 #ifdef STANDALONE_FITTER
263 #endif
264 
265 // fit functions (these can't be put in the class; "MuonResidualsFitter_" prefix avoids namespace clashes)
266 double MuonResidualsFitter_integrate_pureGaussian(double low, double high, double center, double sigma);
267 double MuonResidualsFitter_logPureGaussian(double residual, double center, double sigma);
268 Double_t MuonResidualsFitter_pureGaussian_TF1(Double_t *xvec, Double_t *par);
269 double MuonResidualsFitter_logPureGaussian2D(double x, double y, double x0, double y0, double sx, double sy, double r);
271  double toversigma, double gammaoversigma, double max = 1000., double step = 0.001, double power = 4.);
272 double MuonResidualsFitter_logPowerLawTails(double residual, double center, double sigma, double gamma);
273 Double_t MuonResidualsFitter_powerLawTails_TF1(Double_t *xvec, Double_t *par);
274 double MuonResidualsFitter_logROOTVoigt(double residual, double center, double sigma, double gamma);
275 Double_t MuonResidualsFitter_ROOTVoigt_TF1(Double_t *xvec, Double_t *par);
276 double MuonResidualsFitter_logGaussPowerTails(double residual, double center, double sigma);
277 Double_t MuonResidualsFitter_GaussPowerTails_TF1(Double_t *xvec, Double_t *par);
278 void MuonResidualsPositionFitter_FCN(int &npar, double *gin, double &fval, double *par, int iflag);
279 void MuonResidualsAngleFitter_FCN(int &npar, double *gin, double &fval, double *par, int iflag);
280 
281 #endif // Alignment_MuonAlignmentAlgorithms_MuonResidualsFitter_H
void histogramChi2GaussianFit(int which, double &fit_mean, double &fit_sigma)
tuple weightAlignment
Definition: align_cfg.py:30
double MuonResidualsFitter_logGaussPowerTails(double residual, double center, double sigma)
double MuonResidualsFitter_integrate_pureGaussian(double low, double high, double center, double sigma)
def which
Definition: eostools.py:336
std::vector< double * >::const_iterator residuals_begin() const
Double_t MuonResidualsFitter_powerLawTails_TF1(Double_t *xvec, Double_t *par)
virtual void inform(TMinuit *tMinuit)=0
std::vector< double * > m_residuals
void computeHistogramRangeAndBinning(int which, int &nbins, double &a, double &b)
void selectPeakResiduals(double nsigma, int nvar, int *vars)
double MuonResidualsFitter_logPureGaussian2D(double x, double y, double x0, double y0, double sx, double sy, double r)
void write(FILE *file, int which=0)
double covarianceElement(int parNum1, int parNum2)
int parNum2parIdx(int parNum)
virtual int npar()=0
std::vector< double > m_error
void read(FILE *file, int which=0)
void MuonResidualsPositionFitter_FCN(int &npar, double *gin, double &fval, double *par, int iflag)
virtual bool fit(Alignable *ali)=0
void fill(double *residual)
MuonResidualsFitter * fitter()
double value(int parNum)
assert(be >=bs)
TMatrixDSym covarianceMatrix()
bool dofit(void(*fcn)(int &, double *, double &, double *, int), std::vector< int > &parNum, std::vector< std::string > &parName, std::vector< double > &start, std::vector< double > &step, std::vector< double > &low, std::vector< double > &high)
double MuonResidualsFitter_logROOTVoigt(double residual, double center, double sigma, double gamma)
double MuonResidualsFitter_compute_log_convolution(double toversigma, double gammaoversigma, double max=1000., double step=0.001, double power=4.)
TMatrixDSym correlationMatrix()
std::vector< double > m_value
std::map< int, int > m_parNum2parIdx
double MuonResidualsFitter_logPowerLawTails(double residual, double center, double sigma, double gamma)
ClassImp(AliDaqEventHeader)
MuonResidualsFitter(int residualsModel, int minHits, int useResiduals, bool weightAlignment=true)
char const * what() const noexceptoverride
Definition: Exception.cc:103
uint16_t const *__restrict__ x
Definition: gpuClustering.h:39
std::vector< bool > m_residuals_ok
Double_t MuonResidualsFitter_GaussPowerTails_TF1(Double_t *xvec, Double_t *par)
void fix(int parNum, bool dofix=true)
T * make(const Args &...args) const
make new ROOT object
virtual void correctBField()=0
friend detail::Desired< E, detail::is_derived_or_same< Exception, std::remove_reference_t< E > >::value >::type & operator<<(E &&e, T const &stuff)
Definition: Exception.h:202
virtual double plot(std::string name, TFileDirectory *dir, Alignable *ali)=0
const AlignableSurface & surface() const
Return the Surface (global position and orientation) of the object.
Definition: Alignable.h:132
Double_t MuonResidualsFitter_pureGaussian_TF1(Double_t *xvec, Double_t *par)
void plotsimple(std::string name, TFileDirectory *dir, int which, double multiplier)
virtual int ndata()=0
void setStrategy(int strategy)
virtual double sumofweights()=0
list useResiduals
Definition: align_cfg.py:36
MuonResidualsFitter * m_fitter
double errorerror(int parNum)
std::map< int, double > m_parNum2InitValue
void fcn(int &, double *, double &, double *, int)
void fiducialCuts(double xMin=-80.0, double xMax=80.0, double yMin=-80.0, double yMax=80.0, bool fidcut1=false)
double b
Definition: hdecay.h:118
void setInitialValue(int parNum, double value)
int useRes(int pattern=-1)
std::vector< bool > & selectedResidualsFlags()
Exception(std::string const &aCategory)
Definition: Exception.cc:6
void plotweighted(std::string name, TFileDirectory *dir, int which, int whichredchi2, double multiplier)
double a
Definition: hdecay.h:119
void MuonResidualsAngleFitter_FCN(int &npar, double *gin, double &fval, double *par, int iflag)
std::vector< double * >::const_iterator residuals_end() const
tuple cout
Definition: gather_cfg.py:144
void selectPeakResiduals_simple(double nsigma, int nvar, int *vars)
step
Definition: StallMonitor.cc:94
std::vector< bool > m_fixed
vars
Definition: DeepTauId.cc:164
void setPrintLevel(int printLevel)
Double_t MuonResidualsFitter_ROOTVoigt_TF1(Double_t *xvec, Double_t *par)
long double T
virtual int type() const =0
MuonResidualsFitterFitInfo(MuonResidualsFitter *fitter)
double MuonResidualsFitter_logPureGaussian(double residual, double center, double sigma)
~Exception() noexceptoverride
Definition: Exception.cc:86