CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros 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 {
39 public:
40  struct Surface
41  {
42  double width() {return 300.;}
43  double length() {return 300.;}
44  };
45  Surface s;
46  Surface & surface() {return s;}
47 };
48 
49 class TFileDirectory
50 {
51 public:
52  template<typename T, typename A1, typename A2, typename A3, typename A4, typename A5>
53  T * make(const A1 &a1, const A2 &a2, const A3 &a3, const A4 &a4, const A5 &a5 ) const { T * t = new T(a1, a2, a3, a4, a5); return t; }
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 { T * t = new T(a1, a2, a3, a4, a5, a6, a7); return t; }
56  template<typename T, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7, typename A8>
57  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) const { T * t = new T(a1, a2, a3, a4, a5, a6, a7, a8); return t; }
58 };
59 
60 #include <exception>
61 namespace cms{
62 class Exception : public std::exception
63 {
64 public:
65  Exception(const std::string & s): name(s) {}
66  ~Exception () throw () {}
67  std::string name;
68  template <class T> Exception& operator<<(const T& what)
69  {
70  std::cout<<name<<" exception: "<<what<<std::endl;
71  return *this;
72  }
73 };
74 }// namespace cms
75 
76 #endif // STANDALONE_FITTER
77 
78 
79 
81 {
82 public:
83  enum {
89  };
90 
91  enum {
99  };
100 
101  enum {
107  };
108 
110  {
111  Bool_t is_plus; // for +- endcap
112  Bool_t is_dt; // DT or CSC
113  UChar_t station; // 8bit uint
114  Char_t ring_wheel;
116  Float_t res_x;
117  Float_t res_y;
118  Float_t res_slope_x;
119  Float_t res_slope_y;
120  Float_t pos_x;
121  Float_t pos_y;
122  Float_t angle_x;
123  Float_t angle_y;
124  Float_t pz;
125  Float_t pt;
126  Char_t q;
127  Bool_t select;
128  };
129 
131  : m_residualsModel(residualsModel), m_minHits(minHits), m_useResiduals(useResiduals), m_weightAlignment(weightAlignment), m_printLevel(0), m_strategy(1), m_cov(1), m_loglikelihood(0.)
132  {
135  throw cms::Exception("MuonResidualsFitter") << "unrecognized residualsModel";
136  };
137 
139  {
140  for (std::vector<double*>::const_iterator residual = residuals_begin(); residual != residuals_end(); ++residual) {
141  delete [] (*residual);
142  }
143  }
144 
145  virtual int type() const = 0;
146  virtual int npar() = 0;
147  virtual int ndata() = 0;
148 
149  int useRes() const { return m_useResiduals; }
150  int residualsModel() const { return m_residualsModel; }
151  long numResiduals() const { return m_residuals.size(); }
152 
153  void fix(int parNum, bool val=true)
154  {
155  assert(0 <= parNum && parNum < npar());
156  if (m_fixed.size() == 0) m_fixed.resize(npar(), false);
157  m_fixed[parNum] = val;
158  }
159 
160  bool fixed(int parNum)
161  {
162  assert(0 <= parNum && parNum < npar());
163  if (m_fixed.size() == 0) return false;
164  else return m_fixed[parNum];
165  }
166  int nfixed() { return std::count(m_fixed.begin(), m_fixed.end(), true); }
167 
168  void setPrintLevel(int printLevel) { m_printLevel = printLevel; }
169  void setStrategy(int strategy) { m_strategy = strategy; }
170 
171  // an array of the actual residual and associated baggage (qoverpt, trackangle, trackposition)
172  // arrays passed to fill() are "owned" by MuonResidualsFitter: MuonResidualsFitter will delete them, don't do it yourself!
173  void fill(double *residual)
174  {
175  m_residuals.push_back(residual);
176  m_residuals_ok.push_back(true);
177  }
178 
179  // this block of results is only valid if fit() returns true
180  // also gamma is only valid if the model is kPowerLawTails or kROOTVoigt
181  virtual bool fit(Alignable *ali) = 0;
182 
183  double value(int parNum) { assert(0 <= parNum && parNum < npar()); return m_value[parNum]; }
184  double errorerror(int parNum) { assert(0 <= parNum && parNum < npar()); return m_error[parNum]; }
185 
186  // parNum corresponds to the parameter number defined by enums in specific fitters
187  // parIdx is a continuous index in a set of parameters
188  int parNum2parIdx(int parNum) { return m_parNum2parIdx[parNum];}
189 
190  TMatrixDSym covarianceMatrix() {return m_cov;}
191  double covarianceElement(int parNum1, int parNum2)
192  {
193  assert(0 <= parNum1 && parNum1 < npar());
194  assert(0 <= parNum2 && parNum2 < npar());
195  assert(m_cov.GetNcols() == npar()); // m_cov might have not yet been resized to account for proper #parameters
196  return m_cov(parNum2parIdx(parNum1), parNum2parIdx(parNum2));
197  }
198  TMatrixDSym correlationMatrix();
199 
200  double loglikelihood() { return m_loglikelihood; }
201 
202  long numsegments()
203  {
204  long num = 0;
205  for (std::vector<double*>::const_iterator resiter = residuals_begin(); resiter != residuals_end(); ++resiter) num++;
206  return num;
207  }
208 
209  virtual double sumofweights() = 0;
210 
211  // demonstration plots; return reduced chi**2
212  virtual double plot(std::string name, TFileDirectory *dir, Alignable *ali) = 0;
213 
214  void plotsimple(std::string name, TFileDirectory *dir, int which, double multiplier);
215  void plotweighted(std::string name, TFileDirectory *dir, int which, int whichredchi2, double multiplier);
216 
217 #ifdef STANDALONE_FITTER
218  Alignable m_ali;
219  TFileDirectory m_dir;
220  bool fit() { return fit(&m_ali); }
221  virtual double plot(std::string &name) { return plot(name, &m_dir, &m_ali); }
222  void plotsimple(std::string &name, int which, double multiplier) { plotsimple(name, &m_dir, which, multiplier); }
223  void plotweighted(std::string &name, int which, int whichredchi2, double multiplier) { plotweighted(name, &m_dir, which, whichredchi2, multiplier); }
224 #endif
225 
226  // I/O of temporary files for collect mode
227  void write(FILE *file, int which=0);
228  void read(FILE *file, int which=0);
229 
230  // these are for the FCN to access what it needs to
231  std::vector<double*>::const_iterator residuals_begin() const { return m_residuals.begin(); }
232  std::vector<double*>::const_iterator residuals_end() const { return m_residuals.end(); }
233 
234  void computeHistogramRangeAndBinning(int which, int &nbins, double &a, double &b);
235  void histogramChi2GaussianFit(int which, double &fit_mean, double &fit_sigma);
236  void selectPeakResiduals_simple(double nsigma, int nvar, int *vars);
237  void selectPeakResiduals(double nsigma, int nvar, int *vars);
238 
239  virtual void correctBField() = 0;
240  virtual void correctBField(int idx_momentum, int idx_q);
241 
242  std::vector<bool> & selectedResidualsFlags() {return m_residuals_ok;}
243 
245 
246 protected:
247  void initialize_table();
248  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);
249  virtual void inform(TMinuit *tMinuit) = 0;
250 
255  std::vector<bool> m_fixed;
257 
258  std::vector<double*> m_residuals;
259  std::vector<bool> m_residuals_ok;
260 
261  std::vector<double> m_value;
262  std::vector<double> m_error;
263  TMatrixDSym m_cov;
265 
266  std::map<int,int> m_parNum2parIdx;
267 
268  // center and radii of ellipsoid for peak selection
269  // NOTE: 10 is a hardcoded maximum of ellipsoid variables!!!
270  // but I can't imagine it ever becoming larger
271  double m_center[20];
272  double m_radii[20];
273 };
274 
275 // A ROOT-sponsored hack to get information into the fit function
276 class MuonResidualsFitterFitInfo: public TObject
277 {
278 public:
281 private:
283 #ifdef STANDALONE_FITTER
284  ClassDef(MuonResidualsFitterFitInfo,1);
285 #endif
286 };
287 #ifdef STANDALONE_FITTER
289 #endif
290 
291 // fit functions (these can't be put in the class; "MuonResidualsFitter_" prefix avoids namespace clashes)
292 double MuonResidualsFitter_integrate_pureGaussian(double low, double high, double center, double sigma);
293 double MuonResidualsFitter_logPureGaussian(double residual, double center, double sigma);
294 Double_t MuonResidualsFitter_pureGaussian_TF1(Double_t *xvec, Double_t *par);
295 double MuonResidualsFitter_logPureGaussian2D(double x, double y, double x0, double y0, double sx, double sy, double r);
296 double MuonResidualsFitter_compute_log_convolution(double toversigma, double gammaoversigma, double max=1000., double step=0.001, double power=4.);
297 double MuonResidualsFitter_logPowerLawTails(double residual, double center, double sigma, double gamma);
298 Double_t MuonResidualsFitter_powerLawTails_TF1(Double_t *xvec, Double_t *par);
299 double MuonResidualsFitter_logROOTVoigt(double residual, double center, double sigma, double gamma);
300 Double_t MuonResidualsFitter_ROOTVoigt_TF1(Double_t *xvec, Double_t *par);
301 double MuonResidualsFitter_logGaussPowerTails(double residual, double center, double sigma);
302 Double_t MuonResidualsFitter_GaussPowerTails_TF1(Double_t *xvec, Double_t *par);
303 void MuonResidualsPositionFitter_FCN(int &npar, double *gin, double &fval, double *par, int iflag);
304 void MuonResidualsAngleFitter_FCN(int &npar, double *gin, double &fval, double *par, int iflag);
305 
306 #endif // Alignment_MuonAlignmentAlgorithms_MuonResidualsFitter_H
virtual ~Exception()
Definition: Exception.cc:121
virtual char const * what() const
Definition: Exception.cc:141
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)
Double_t MuonResidualsFitter_powerLawTails_TF1(Double_t *xvec, Double_t *par)
virtual void inform(TMinuit *tMinuit)=0
void computeHistogramRangeAndBinning(int which, int &nbins, double &a, double &b)
void fix(int parNum, bool val=true)
void selectPeakResiduals(double nsigma, int nvar, int *vars)
list step
Definition: launcher.py:15
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)
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::vector< double * > m_residuals
const T & max(const T &a, const T &b)
bool fixed(int parNum)
unsigned char UChar_t
Definition: FUTypes.h:14
double MuonResidualsFitter_logPowerLawTails(double residual, double center, double sigma, double gamma)
MuonResidualsFitter(int residualsModel, int minHits, int useResiduals, bool weightAlignment=true)
std::vector< bool > m_residuals_ok
Double_t MuonResidualsFitter_GaussPowerTails_TF1(Double_t *xvec, Double_t *par)
std::map< int, int > m_parNum2parIdx
friend detail::Desired< E, detail::is_derived_or_same< Exception, E >::value >::type & operator<<(E &e, T const &stuff)
Definition: Exception.h:227
virtual void correctBField()=0
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:126
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)
void fcn(int &, double *, double &, double *, int)
long long int num
Definition: procUtils.cc:71
double b
Definition: hdecay.h:120
std::vector< double * >::const_iterator residuals_end() const
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:121
void MuonResidualsAngleFitter_FCN(int &npar, double *gin, double &fval, double *par, int iflag)
T * make() const
make new ROOT object
tuple cout
Definition: gather_cfg.py:121
void selectPeakResiduals_simple(double nsigma, int nvar, int *vars)
std::vector< bool > m_fixed
dbl *** dir
Definition: mlp_gen.cc:35
void setPrintLevel(int printLevel)
x
Definition: VDTMath.h:216
Double_t MuonResidualsFitter_ROOTVoigt_TF1(Double_t *xvec, Double_t *par)
std::vector< double * >::const_iterator residuals_begin() const
long double T
virtual int type() const =0
MuonResidualsFitterFitInfo(MuonResidualsFitter *fitter)
double MuonResidualsFitter_logPureGaussian(double residual, double center, double sigma)