CMS 3D CMS Logo

MuonResidualsAngleFitter.cc
Go to the documentation of this file.
2 
4 
6 
7 void MuonResidualsAngleFitter_FCN(int &npar, double *gin, double &fval, double *par, int iflag) {
10  MuonResidualsFitter *fitter = fitinfo->fitter();
11 
12  fval = 0.;
13  for (std::vector<double *>::const_iterator resiter = fitter->residuals_begin(); resiter != fitter->residuals_end();
14  ++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];
22  center += par[MuonResidualsAngleFitter::kYControl] * yangle;
23 
26  } else if (fitter->residualsModel() == MuonResidualsFitter::kPowerLawTails) {
29  } else if (fitter->residualsModel() == MuonResidualsFitter::kROOTVoigt) {
32  } else if (fitter->residualsModel() == MuonResidualsFitter::kGaussPowerTails) {
34  } else {
35  assert(false);
36  }
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)
60  return false;
61 
62  // truncated mean and stdev to seed the fit
63  double mean = sum_x / double(N);
64  double stdev = sqrt(sum_xx / double(N) - pow(sum_x / double(N), 2));
65 
66  // refine the standard deviation calculation
67  sum_x = 0.;
68  sum_xx = 0.;
69  N = 0;
70  for (std::vector<double *>::const_iterator resiter = residuals_begin(); resiter != residuals_end(); ++resiter) {
71  const double residual = (*resiter)[kResidual];
72  if (mean - 1.5 * stdev < residual && residual < mean + 1.5 * stdev) {
73  sum_x += residual;
74  sum_xx += residual * residual;
75  N++;
76  }
77  }
78  mean = sum_x / double(N);
79  stdev = sqrt(sum_xx / double(N) - pow(sum_x / double(N), 2));
80 
81  sum_x = 0.;
82  sum_xx = 0.;
83  N = 0;
84  for (std::vector<double *>::const_iterator resiter = residuals_begin(); resiter != residuals_end(); ++resiter) {
85  const double residual = (*resiter)[kResidual];
86  if (mean - 1.5 * stdev < residual && residual < mean + 1.5 * stdev) {
87  sum_x += residual;
88  sum_xx += residual * residual;
89  N++;
90  }
91  }
92  mean = sum_x / double(N);
93  stdev = sqrt(sum_xx / double(N) - pow(sum_x / double(N), 2));
94 
95  std::vector<int> parNum;
96  std::vector<std::string> parName;
97  std::vector<double> start;
98  std::vector<double> step;
99  std::vector<double> low;
100  std::vector<double> high;
101 
102  parNum.push_back(kAngle);
103  parName.push_back(std::string("angle"));
104  start.push_back(mean);
105  step.push_back(0.1);
106  low.push_back(0.);
107  high.push_back(0.);
108  parNum.push_back(kXControl);
109  parName.push_back(std::string("xcontrol"));
110  start.push_back(0.);
111  step.push_back(0.1);
112  low.push_back(0.);
113  high.push_back(0.);
114  parNum.push_back(kYControl);
115  parName.push_back(std::string("ycontrol"));
116  start.push_back(0.);
117  step.push_back(0.1);
118  low.push_back(0.);
119  high.push_back(0.);
120  parNum.push_back(kSigma);
121  parName.push_back(std::string("sigma"));
122  start.push_back(stdev);
123  step.push_back(0.1 * stdev);
124  low.push_back(0.);
125  high.push_back(0.);
127  parNum.push_back(kGamma);
128  parName.push_back(std::string("gamma"));
129  start.push_back(stdev);
130  step.push_back(0.1 * stdev);
131  low.push_back(0.);
132  high.push_back(0.);
133  }
134 
135  return dofit(&MuonResidualsAngleFitter_FCN, parNum, parName, start, step, low, high);
136 }
137 
139  std::stringstream raw_name, narrowed_name, xcontrol_name, ycontrol_name;
140  raw_name << name << "_raw";
141  narrowed_name << name << "_narrowed";
142  xcontrol_name << name << "_xcontrol";
143  ycontrol_name << name << "_ycontrol";
144 
145  TH1F *raw_hist =
146  dir->make<TH1F>(raw_name.str().c_str(), (raw_name.str() + std::string(" (mrad)")).c_str(), 100, -100., 100.);
147  TH1F *narrowed_hist = dir->make<TH1F>(
148  narrowed_name.str().c_str(), (narrowed_name.str() + std::string(" (mrad)")).c_str(), 100, -100., 100.);
149  TProfile *xcontrol_hist = dir->make<TProfile>(
150  xcontrol_name.str().c_str(), (xcontrol_name.str() + std::string(" (mrad)")).c_str(), 100, -1., 1.);
151  TProfile *ycontrol_hist = dir->make<TProfile>(
152  ycontrol_name.str().c_str(), (ycontrol_name.str() + std::string(" (mrad)")).c_str(), 100, -1., 1.);
153 
154  narrowed_name << "fit";
155  xcontrol_name << "fit";
156  ycontrol_name << "fit";
157 
158  double scale_factor = double(numResiduals()) * (100. - -100.) / 100; // (max - min)/nbins
159 
160  TF1 *narrowed_fit = nullptr;
161  if (residualsModel() == kPureGaussian) {
162  narrowed_fit = new TF1(narrowed_name.str().c_str(), MuonResidualsFitter_pureGaussian_TF1, -100., 100., 3);
163  narrowed_fit->SetParameters(scale_factor, value(kAngle) * 1000., value(kSigma) * 1000.);
164  narrowed_fit->Write();
165  } else if (residualsModel() == kPowerLawTails) {
166  narrowed_fit = new TF1(narrowed_name.str().c_str(), MuonResidualsFitter_powerLawTails_TF1, -100., 100., 4);
167  narrowed_fit->SetParameters(scale_factor, value(kAngle) * 1000., value(kSigma) * 1000., value(kGamma) * 1000.);
168  narrowed_fit->Write();
169  } else if (residualsModel() == kROOTVoigt) {
170  narrowed_fit = new TF1(narrowed_name.str().c_str(), MuonResidualsFitter_ROOTVoigt_TF1, -100., 100., 4);
171  narrowed_fit->SetParameters(scale_factor, value(kAngle) * 1000., value(kSigma) * 1000., value(kGamma) * 1000.);
172  narrowed_fit->Write();
173  } else if (residualsModel() == kGaussPowerTails) {
174  narrowed_fit = new TF1(narrowed_name.str().c_str(), MuonResidualsFitter_GaussPowerTails_TF1, -100., 100., 3);
175  narrowed_fit->SetParameters(scale_factor, value(kAngle) * 1000., value(kSigma) * 1000.);
176  narrowed_fit->Write();
177  }
178 
179  TF1 *xcontrol_fit = new TF1(xcontrol_name.str().c_str(), "[0]+x*[1]", -1., 1.);
180  xcontrol_fit->SetParameters(value(kAngle) * 1000., value(kXControl) * 1000.);
181  xcontrol_fit->Write();
182 
183  TF1 *ycontrol_fit = new TF1(ycontrol_name.str().c_str(), "[0]+x*[1]", -1., 1.);
184  ycontrol_fit->SetParameters(value(kAngle) * 1000., value(kYControl) * 1000.);
185  ycontrol_fit->Write();
186 
187  for (std::vector<double *>::const_iterator resiter = residuals_begin(); resiter != residuals_end(); ++resiter) {
188  const double raw_residual = (*resiter)[kResidual];
189  const double xangle = (*resiter)[kXAngle];
190  const double yangle = (*resiter)[kYAngle];
191 
192  double xangle_correction = value(kXControl) * xangle;
193  double yangle_correction = value(kYControl) * yangle;
194  double corrected_residual = raw_residual - xangle_correction - yangle_correction;
195 
196  raw_hist->Fill(raw_residual * 1000.);
197  narrowed_hist->Fill(corrected_residual * 1000.);
198 
199  xcontrol_hist->Fill(xangle, (raw_residual - yangle_correction) * 1000.);
200  ycontrol_hist->Fill(yangle, (raw_residual - xangle_correction) * 1000.);
201  }
202 
203  return 0.;
204 }
MuonResidualsFitter
Definition: MuonResidualsFitter.h:84
MuonResidualsAngleFitter_FCN
void MuonResidualsAngleFitter_FCN(int &npar, double *gin, double &fval, double *par, int iflag)
Definition: MuonResidualsAngleFitter.cc:7
MuonResidualsFitter_pureGaussian_TF1
Double_t MuonResidualsFitter_pureGaussian_TF1(Double_t *xvec, Double_t *par)
Definition: MuonResidualsFitter.cc:38
start
Definition: start.py:1
SiStripPI::mean
Definition: SiStripPayloadInspectorHelper.h:169
MuonResidualsFitter::value
double value(int parNum)
Definition: MuonResidualsFitter.h:145
step
step
Definition: StallMonitor.cc:94
MuonResidualsAngleFitter.h
MuonResidualsAngleFitter::kGamma
Definition: MuonResidualsAngleFitter.h:14
MuonResidualsFitter_logROOTVoigt
double MuonResidualsFitter_logROOTVoigt(double residual, double center, double sigma, double gamma)
Definition: MuonResidualsFitter.cc:120
MuonResidualsFitter::dofit
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)
Definition: MuonResidualsFitter.cc:272
Alignable
Definition: Alignable.h:27
MuonResidualsFitter::numResiduals
long numResiduals() const
Definition: MuonResidualsFitter.h:125
cms::cuda::assert
assert(be >=bs)
MuonResidualsFitter::kGaussPowerTails
Definition: MuonResidualsFitter.h:86
TFileDirectory
Definition: TFileDirectory.h:24
tools.TF1
TF1
Definition: tools.py:23
MuonResidualsAngleFitter_TMinuit
static TMinuit * MuonResidualsAngleFitter_TMinuit
Definition: MuonResidualsAngleFitter.cc:3
MuonResidualsAngleFitter::kResidual
Definition: MuonResidualsAngleFitter.h:16
MuonResidualsFitterFitInfo
Definition: MuonResidualsFitter.h:250
mathSSE::sqrt
T sqrt(T t)
Definition: SSEVec.h:19
MuonResidualsFitter::kPowerLawTails
Definition: MuonResidualsFitter.h:86
MuonResidualsFitter_powerLawTails_TF1
Double_t MuonResidualsFitter_powerLawTails_TF1(Double_t *xvec, Double_t *par)
Definition: MuonResidualsFitter.cc:116
N
#define N
Definition: blowfish.cc:9
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
OpticalFunctionsConfig_cfi.xangle
xangle
Definition: OpticalFunctionsConfig_cfi.py:17
MuonResidualsAngleFitter::kXAngle
Definition: MuonResidualsAngleFitter.h:16
MuonResidualsFitter_ROOTVoigt_TF1
Double_t MuonResidualsFitter_ROOTVoigt_TF1(Double_t *xvec, Double_t *par)
Definition: MuonResidualsFitter.cc:124
MuonResidualsFitter::kPureGaussian
Definition: MuonResidualsFitter.h:86
MuonResidualsFitter::residuals_end
std::vector< double * >::const_iterator residuals_end() const
Definition: MuonResidualsFitter.h:191
MuonResidualsAngleFitter::kSigma
Definition: MuonResidualsAngleFitter.h:14
MuonResidualsFitter::m_minHits
int m_minHits
Definition: MuonResidualsFitter.h:225
MuonResidualsFitter_logPowerLawTails
double MuonResidualsFitter_logPowerLawTails(double residual, double center, double sigma, double gamma)
Definition: MuonResidualsFitter.cc:85
MuonResidualsFitter::kROOTVoigt
Definition: MuonResidualsFitter.h:86
MuonResidualsAngleFitter::plot
double plot(std::string name, TFileDirectory *dir, Alignable *ali) override
Definition: MuonResidualsAngleFitter.cc:138
plotscripts.stdev
def stdev(xlist)
Definition: plotscripts.py:69
LaserClient_cfi.high
high
Definition: LaserClient_cfi.py:50
MuonResidualsAngleFitter::fit
bool fit(Alignable *ali) override
Definition: MuonResidualsAngleFitter.cc:40
MuonResidualsFitterFitInfo::fitter
MuonResidualsFitter * fitter()
Definition: MuonResidualsFitter.h:253
Skims_PA_cff.name
name
Definition: Skims_PA_cff.py:17
MuonResidualsFitter_logGaussPowerTails
double MuonResidualsFitter_logGaussPowerTails(double residual, double center, double sigma)
Definition: MuonResidualsFitter.cc:128
MuonResidualsFitter::initialize_table
void initialize_table()
Definition: MuonResidualsFitter.cc:205
MuonResidualsFitter_GaussPowerTails_TF1
Double_t MuonResidualsFitter_GaussPowerTails_TF1(Double_t *xvec, Double_t *par)
Definition: MuonResidualsFitter.cc:142
MuonResidualsAngleFitter::kAngle
Definition: MuonResidualsAngleFitter.h:14
MuonResidualsAngleFitter::inform
void inform(TMinuit *tMinuit) override
Definition: MuonResidualsAngleFitter.cc:5
funct::pow
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:29
MuonResidualsAngleFitter::kXControl
Definition: MuonResidualsAngleFitter.h:14
command_line.start
start
Definition: command_line.py:167
makeMuonMisalignmentScenario.sum_x
int sum_x
More diagnostics.
Definition: makeMuonMisalignmentScenario.py:176
MuonResidualsAngleFitter::kYControl
Definition: MuonResidualsAngleFitter.h:14
MuonResidualsFitter::residualsModel
int residualsModel() const
Definition: MuonResidualsFitter.h:124
makeMuonMisalignmentScenario.sum_xx
int sum_xx
Definition: makeMuonMisalignmentScenario.py:183
MuonResidualsFitter::residuals_begin
std::vector< double * >::const_iterator residuals_begin() const
Definition: MuonResidualsFitter.h:190
MuonResidualsAngleFitter::kYAngle
Definition: MuonResidualsAngleFitter.h:16
LaserClient_cfi.low
low
Definition: LaserClient_cfi.py:52
DeadROC_duringRun.dir
dir
Definition: DeadROC_duringRun.py:23
MuonResidualsFitter_logPureGaussian
double MuonResidualsFitter_logPureGaussian(double residual, double center, double sigma)
Definition: MuonResidualsFitter.cc:31