CMS 3D CMS Logo

MuonResidualsAngleFitter.cc
Go to the documentation of this file.
2 
4 
5 void MuonResidualsAngleFitter::inform(TMinuit *tMinuit) {
7 }
8 
9 void MuonResidualsAngleFitter_FCN(int &npar, double *gin, double &fval, double *par, int iflag) {
11  MuonResidualsFitter *fitter = fitinfo->fitter();
12 
13  fval = 0.;
14  for (std::vector<double*>::const_iterator resiter = fitter->residuals_begin(); resiter != fitter->residuals_end(); ++resiter) {
15  const double residual = (*resiter)[MuonResidualsAngleFitter::kResidual];
16  const double xangle = (*resiter)[MuonResidualsAngleFitter::kXAngle];
17  const double yangle = (*resiter)[MuonResidualsAngleFitter::kYAngle];
18 
19  double center = 0.;
20  center += par[MuonResidualsAngleFitter::kAngle];
21  center += par[MuonResidualsAngleFitter::kXControl] * xangle;
22  center += par[MuonResidualsAngleFitter::kYControl] * yangle;
23 
26  }
29  }
30  else if (fitter->residualsModel() == MuonResidualsFitter::kROOTVoigt) {
32  }
35  }
36  else { assert(false); }
37  }
38 }
39 
41  initialize_table(); // if not already initialized
42 
43  double sum_x = 0.;
44  double sum_xx = 0.;
45  int N = 0;
46 
47  for (std::vector<double*>::const_iterator resiter = residuals_begin(); resiter != residuals_end(); ++resiter) {
48  const double residual = (*resiter)[kResidual];
49 // const double xangle = (*resiter)[kXAngle];
50 // const double yangle = (*resiter)[kYAngle];
51 
52  if (fabs(residual) < 0.1) { // truncate at 100 mrad
53  sum_x += residual;
54  sum_xx += residual*residual;
55  N++;
56  }
57  }
58 
59  if (N < m_minHits) return false;
60 
61  // truncated mean and stdev to seed the fit
62  double mean = sum_x/double(N);
63  double stdev = sqrt(sum_xx/double(N) - pow(sum_x/double(N), 2));
64 
65  // refine the standard deviation calculation
66  sum_x = 0.;
67  sum_xx = 0.;
68  N = 0;
69  for (std::vector<double*>::const_iterator resiter = residuals_begin(); resiter != residuals_end(); ++resiter) {
70  const double residual = (*resiter)[kResidual];
71  if (mean - 1.5*stdev < residual && residual < mean + 1.5*stdev) {
72  sum_x += residual;
73  sum_xx += residual*residual;
74  N++;
75  }
76  }
77  mean = sum_x/double(N);
78  stdev = sqrt(sum_xx/double(N) - pow(sum_x/double(N), 2));
79 
80  sum_x = 0.;
81  sum_xx = 0.;
82  N = 0;
83  for (std::vector<double*>::const_iterator resiter = residuals_begin(); resiter != residuals_end(); ++resiter) {
84  const double residual = (*resiter)[kResidual];
85  if (mean - 1.5*stdev < residual && residual < mean + 1.5*stdev) {
86  sum_x += residual;
87  sum_xx += residual*residual;
88  N++;
89  }
90  }
91  mean = sum_x/double(N);
92  stdev = sqrt(sum_xx/double(N) - pow(sum_x/double(N), 2));
93 
94  std::vector<int> parNum;
95  std::vector<std::string> parName;
96  std::vector<double> start;
97  std::vector<double> step;
98  std::vector<double> low;
99  std::vector<double> high;
100 
101  parNum.push_back(kAngle); parName.push_back(std::string("angle")); start.push_back(mean); step.push_back(0.1); low.push_back(0.); high.push_back(0.);
102  parNum.push_back(kXControl); parName.push_back(std::string("xcontrol")); start.push_back(0.); step.push_back(0.1); low.push_back(0.); high.push_back(0.);
103  parNum.push_back(kYControl); parName.push_back(std::string("ycontrol")); start.push_back(0.); step.push_back(0.1); low.push_back(0.); high.push_back(0.);
104  parNum.push_back(kSigma); parName.push_back(std::string("sigma")); start.push_back(stdev); step.push_back(0.1*stdev); low.push_back(0.); high.push_back(0.);
106  parNum.push_back(kGamma); parName.push_back(std::string("gamma")); start.push_back(stdev); step.push_back(0.1*stdev); low.push_back(0.); high.push_back(0.);
107  }
108 
109  return dofit(&MuonResidualsAngleFitter_FCN, parNum, parName, start, step, low, high);
110 }
111 
113  std::stringstream raw_name, narrowed_name, xcontrol_name, ycontrol_name;
114  raw_name << name << "_raw";
115  narrowed_name << name << "_narrowed";
116  xcontrol_name << name << "_xcontrol";
117  ycontrol_name << name << "_ycontrol";
118 
119  TH1F *raw_hist = dir->make<TH1F>(raw_name.str().c_str(), (raw_name.str() + std::string(" (mrad)")).c_str(), 100, -100., 100.);
120  TH1F *narrowed_hist = dir->make<TH1F>(narrowed_name.str().c_str(), (narrowed_name.str() + std::string(" (mrad)")).c_str(), 100, -100., 100.);
121  TProfile *xcontrol_hist = dir->make<TProfile>(xcontrol_name.str().c_str(), (xcontrol_name.str() + std::string(" (mrad)")).c_str(), 100, -1., 1.);
122  TProfile *ycontrol_hist = dir->make<TProfile>(ycontrol_name.str().c_str(), (ycontrol_name.str() + std::string(" (mrad)")).c_str(), 100, -1., 1.);
123 
124  narrowed_name << "fit";
125  xcontrol_name << "fit";
126  ycontrol_name << "fit";
127 
128  double scale_factor = double(numResiduals()) * (100. - -100.)/100; // (max - min)/nbins
129 
130  TF1 *narrowed_fit = nullptr;
131  if (residualsModel() == kPureGaussian) {
132  narrowed_fit = new TF1(narrowed_name.str().c_str(), MuonResidualsFitter_pureGaussian_TF1, -100., 100., 3);
133  narrowed_fit->SetParameters(scale_factor, value(kAngle) * 1000., value(kSigma) * 1000.);
134  narrowed_fit->Write();
135  }
136  else if (residualsModel() == kPowerLawTails) {
137  narrowed_fit = new TF1(narrowed_name.str().c_str(), MuonResidualsFitter_powerLawTails_TF1, -100., 100., 4);
138  narrowed_fit->SetParameters(scale_factor, value(kAngle) * 1000., value(kSigma) * 1000., value(kGamma) * 1000.);
139  narrowed_fit->Write();
140  }
141  else if (residualsModel() == kROOTVoigt) {
142  narrowed_fit = new TF1(narrowed_name.str().c_str(), MuonResidualsFitter_ROOTVoigt_TF1, -100., 100., 4);
143  narrowed_fit->SetParameters(scale_factor, value(kAngle) * 1000., value(kSigma) * 1000., value(kGamma) * 1000.);
144  narrowed_fit->Write();
145  }
146  else if (residualsModel() == kGaussPowerTails) {
147  narrowed_fit = new TF1(narrowed_name.str().c_str(), MuonResidualsFitter_GaussPowerTails_TF1, -100., 100., 3);
148  narrowed_fit->SetParameters(scale_factor, value(kAngle) * 1000., value(kSigma) * 1000.);
149  narrowed_fit->Write();
150  }
151 
152  TF1 *xcontrol_fit = new TF1(xcontrol_name.str().c_str(), "[0]+x*[1]", -1., 1.);
153  xcontrol_fit->SetParameters(value(kAngle) * 1000., value(kXControl) * 1000.);
154  xcontrol_fit->Write();
155 
156  TF1 *ycontrol_fit = new TF1(ycontrol_name.str().c_str(), "[0]+x*[1]", -1., 1.);
157  ycontrol_fit->SetParameters(value(kAngle) * 1000., value(kYControl) * 1000.);
158  ycontrol_fit->Write();
159 
160  for (std::vector<double*>::const_iterator resiter = residuals_begin(); resiter != residuals_end(); ++resiter) {
161  const double raw_residual = (*resiter)[kResidual];
162  const double xangle = (*resiter)[kXAngle];
163  const double yangle = (*resiter)[kYAngle];
164 
165  double xangle_correction = value(kXControl) * xangle;
166  double yangle_correction = value(kYControl) * yangle;
167  double corrected_residual = raw_residual - xangle_correction - yangle_correction;
168 
169  raw_hist->Fill(raw_residual * 1000.);
170  narrowed_hist->Fill(corrected_residual * 1000.);
171 
172  xcontrol_hist->Fill(xangle, (raw_residual - yangle_correction) * 1000.);
173  ycontrol_hist->Fill(yangle, (raw_residual - xangle_correction) * 1000.);
174  }
175 
176  return 0.;
177 }
double MuonResidualsFitter_logGaussPowerTails(double residual, double center, double sigma)
Double_t MuonResidualsFitter_powerLawTails_TF1(Double_t *xvec, Double_t *par)
MuonResidualsFitter * fitter()
double value(int parNum)
double plot(std::string name, TFileDirectory *dir, Alignable *ali) override
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)
bool fit(Alignable *ali) override
T sqrt(T t)
Definition: SSEVec.h:18
double MuonResidualsFitter_logPowerLawTails(double residual, double center, double sigma, double gamma)
Double_t MuonResidualsFitter_GaussPowerTails_TF1(Double_t *xvec, Double_t *par)
T * make(const Args &...args) const
make new ROOT object
Double_t MuonResidualsFitter_pureGaussian_TF1(Double_t *xvec, Double_t *par)
#define N
Definition: blowfish.cc:9
std::vector< double * >::const_iterator residuals_end() const
void MuonResidualsAngleFitter_FCN(int &npar, double *gin, double &fval, double *par, int iflag)
void inform(TMinuit *tMinuit) override
def stdev(xlist)
Definition: plotscripts.py:67
step
dbl *** dir
Definition: mlp_gen.cc:35
Double_t MuonResidualsFitter_ROOTVoigt_TF1(Double_t *xvec, Double_t *par)
std::vector< double * >::const_iterator residuals_begin() const
static TMinuit * MuonResidualsAngleFitter_TMinuit
double MuonResidualsFitter_logPureGaussian(double residual, double center, double sigma)
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:40