CMS 3D CMS Logo

MuonResidualsPositionFitter.cc
Go to the documentation of this file.
2 
4 
5 void MuonResidualsPositionFitter::inform(TMinuit *tMinuit) {
7 }
8 
9 void MuonResidualsPositionFitter_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)[MuonResidualsPositionFitter::kResidual];
16  const double angleerror = (*resiter)[MuonResidualsPositionFitter::kAngleError];
17  const double trackangle = (*resiter)[MuonResidualsPositionFitter::kTrackAngle];
18  const double trackposition = (*resiter)[MuonResidualsPositionFitter::kTrackPosition];
19 
20  double center = 0.;
22  center += par[MuonResidualsPositionFitter::kZpos] * trackangle;
23  center += par[MuonResidualsPositionFitter::kPhiz] * trackposition;
24  center += par[MuonResidualsPositionFitter::kScattering] * angleerror;
25 
28  }
31  }
32  else if (fitter->residualsModel() == MuonResidualsFitter::kROOTVoigt) {
34  }
37  }
38  else { assert(false); }
39  }
40 }
41 
43  initialize_table(); // if not already initialized
44 
45  double sum_x = 0.;
46  double sum_xx = 0.;
47  int N = 0;
48 
49  for (std::vector<double*>::const_iterator resiter = residuals_begin(); resiter != residuals_end(); ++resiter) {
50  const double residual = (*resiter)[kResidual];
51  // const double angleerror = (*resiter)[kAngleError];
52  // const double trackangle = (*resiter)[kTrackAngle];
53  // const double trackposition = (*resiter)[kTrackPosition];
54 
55  if (fabs(residual) < 10.) { // truncate at 100 mm
56  sum_x += residual;
57  sum_xx += residual*residual;
58  N++;
59  }
60  }
61 
62  if (N < m_minHits) return false;
63 
64  // truncated mean and stdev to seed the fit
65  double mean = sum_x/double(N);
66  double stdev = sqrt(sum_xx/double(N) - pow(sum_x/double(N), 2));
67 
68  // refine the standard deviation calculation
69  sum_x = 0.;
70  sum_xx = 0.;
71  N = 0;
72  for (std::vector<double*>::const_iterator resiter = residuals_begin(); resiter != residuals_end(); ++resiter) {
73  const double residual = (*resiter)[kResidual];
74  if (mean - 1.5*stdev < residual && residual < mean + 1.5*stdev) {
75  sum_x += residual;
76  sum_xx += residual*residual;
77  N++;
78  }
79  }
80  mean = sum_x/double(N);
81  stdev = sqrt(sum_xx/double(N) - pow(sum_x/double(N), 2));
82 
83  sum_x = 0.;
84  sum_xx = 0.;
85  N = 0;
86  for (std::vector<double*>::const_iterator resiter = residuals_begin(); resiter != residuals_end(); ++resiter) {
87  const double residual = (*resiter)[kResidual];
88  if (mean - 1.5*stdev < residual && residual < mean + 1.5*stdev) {
89  sum_x += residual;
90  sum_xx += residual*residual;
91  N++;
92  }
93  }
94  mean = sum_x/double(N);
95  stdev = sqrt(sum_xx/double(N) - pow(sum_x/double(N), 2));
96 
97  std::vector<int> parNum;
98  std::vector<std::string> parName;
99  std::vector<double> start;
100  std::vector<double> step;
101  std::vector<double> low;
102  std::vector<double> high;
103 
104  parNum.push_back(kPosition); parName.push_back(std::string("position")); start.push_back(mean); step.push_back(0.1); low.push_back(0.); high.push_back(0.);
105  parNum.push_back(kZpos); parName.push_back(std::string("zpos")); start.push_back(0.); step.push_back(0.1); low.push_back(0.); high.push_back(0.);
106  parNum.push_back(kPhiz); parName.push_back(std::string("phiz")); start.push_back(0.); step.push_back(0.1); low.push_back(0.); high.push_back(0.);
107  parNum.push_back(kScattering); parName.push_back(std::string("scattering")); start.push_back(0.); step.push_back(0.1*1000.); low.push_back(0.); high.push_back(0.);
108  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.);
110  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.);
111  }
112 
113  return dofit(&MuonResidualsPositionFitter_FCN, parNum, parName, start, step, low, high);
114 }
115 
117  std::stringstream raw_name, narrowed_name, angleerror_name, trackangle_name, trackposition_name;
118  raw_name << name << "_raw";
119  narrowed_name << name << "_narrowed";
120  angleerror_name << name << "_angleerror";
121  trackangle_name << name << "_trackangle";
122  trackposition_name << name << "_trackposition";
123 
124  TH1F *raw_hist = dir->make<TH1F>(raw_name.str().c_str(), (raw_name.str() + std::string(" (mm)")).c_str(), 100, -100., 100.);
125  TH1F *narrowed_hist = dir->make<TH1F>(narrowed_name.str().c_str(), (narrowed_name.str() + std::string(" (mm)")).c_str(), 100, -100., 100.);
126  TProfile *angleerror_hist = dir->make<TProfile>(angleerror_name.str().c_str(), (angleerror_name.str() + std::string(" (mm)")).c_str(), 100, -30., 30.);
127  TProfile *trackangle_hist = dir->make<TProfile>(trackangle_name.str().c_str(), (trackangle_name.str() + std::string(" (mm)")).c_str(), 100, -0.5, 0.5);
128  TProfile *trackposition_hist = dir->make<TProfile>(trackposition_name.str().c_str(), (trackposition_name.str() + std::string(" (mm)")).c_str(), 100, -300., 300.);
129 
130  angleerror_hist->SetAxisRange(-100., 100., "Y");
131  trackangle_hist->SetAxisRange(-10., 10., "Y");
132  trackposition_hist->SetAxisRange(-10., 10., "Y");
133 
134  narrowed_name << "fit";
135  angleerror_name << "fit";
136  trackangle_name << "fit";
137  trackposition_name << "fit";
138 
139  double scale_factor = double(numResiduals()) * (100. - -100.)/100; // (max - min)/nbins
140 
141  TF1 *narrowed_fit = nullptr;
142  if (residualsModel() == kPureGaussian) {
143  narrowed_fit = new TF1(narrowed_name.str().c_str(), MuonResidualsFitter_pureGaussian_TF1, -100., 100., 3);
144  narrowed_fit->SetParameters(scale_factor, value(kPosition) * 10., value(kSigma) * 10.);
145  narrowed_fit->Write();
146  }
147  else if (residualsModel() == kPowerLawTails) {
148  narrowed_fit = new TF1(narrowed_name.str().c_str(), MuonResidualsFitter_powerLawTails_TF1, -100., 100., 4);
149  narrowed_fit->SetParameters(scale_factor, value(kPosition) * 10., value(kSigma) * 10., value(kGamma) * 10.);
150  narrowed_fit->Write();
151  }
152  else if (residualsModel() == kROOTVoigt) {
153  narrowed_fit = new TF1(narrowed_name.str().c_str(), MuonResidualsFitter_ROOTVoigt_TF1, -100., 100., 4);
154  narrowed_fit->SetParameters(scale_factor, value(kPosition) * 10., value(kSigma) * 10., value(kGamma) * 10.);
155  narrowed_fit->Write();
156  }
157  else if (residualsModel() == kGaussPowerTails) {
158  narrowed_fit = new TF1(narrowed_name.str().c_str(), MuonResidualsFitter_GaussPowerTails_TF1, -100., 100., 3);
159  narrowed_fit->SetParameters(scale_factor, value(kPosition) * 10., value(kSigma) * 10.);
160  narrowed_fit->Write();
161  }
162 
163  TF1 *angleerror_fit = new TF1(angleerror_name.str().c_str(), "[0]+x*[1]", -30., 30.);
164  angleerror_fit->SetParameters(value(kPosition) * 10., value(kScattering) * 10./1000.);
165  angleerror_fit->Write();
166 
167  TF1 *trackangle_fit = new TF1(trackangle_name.str().c_str(), "[0]+x*[1]", -0.5, 0.5);
168  trackangle_fit->SetParameters(value(kPosition) * 10., value(kZpos) * 10.);
169  trackangle_fit->Write();
170 
171  TF1 *trackposition_fit = new TF1(trackposition_name.str().c_str(), "[0]+x*[1]", -300., 300.);
172  trackposition_fit->SetParameters(value(kPosition) * 10., value(kPhiz) * 10.);
173  trackposition_fit->Write();
174 
175  for (std::vector<double*>::const_iterator resiter = residuals_begin(); resiter != residuals_end(); ++resiter) {
176  const double raw_residual = (*resiter)[kResidual];
177  const double angleerror = (*resiter)[kAngleError];
178  const double trackangle = (*resiter)[kTrackAngle];
179  const double trackposition = (*resiter)[kTrackPosition];
180 
181  double angleerror_correction = value(kScattering) * angleerror;
182  double trackangle_correction = value(kZpos) * trackangle;
183  double trackposition_correction = value(kPhiz) * trackposition;
184 
185  double corrected_residual = raw_residual - angleerror_correction - trackangle_correction - trackposition_correction;
186 
187  raw_hist->Fill(raw_residual * 10.);
188  narrowed_hist->Fill(corrected_residual * 10.);
189 
190  angleerror_hist->Fill(angleerror*1000., (raw_residual - trackangle_correction - trackposition_correction) * 10.);
191  trackangle_hist->Fill(trackangle, (raw_residual - angleerror_correction - trackposition_correction) * 10.);
192  trackposition_hist->Fill(trackposition, (raw_residual - angleerror_correction - trackangle_correction) * 10.);
193  }
194 
195  return 0.;
196 }
double MuonResidualsFitter_logGaussPowerTails(double residual, double center, double sigma)
Double_t MuonResidualsFitter_powerLawTails_TF1(Double_t *xvec, Double_t *par)
void MuonResidualsPositionFitter_FCN(int &npar, double *gin, double &fval, double *par, int iflag)
MuonResidualsFitter * fitter()
double value(int parNum)
static TMinuit * MuonResidualsPositionFitter_TMinuit
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 plot(std::string name, TFileDirectory *dir, 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 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
bool fit(Alignable *ali) override
double MuonResidualsFitter_logPureGaussian(double residual, double center, double sigma)
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:40