CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch9/src/Alignment/MuonAlignmentAlgorithms/src/MuonResiduals6DOFrphiFitter.cc

Go to the documentation of this file.
00001 #include "Alignment/MuonAlignmentAlgorithms/interface/MuonResiduals6DOFrphiFitter.h"
00002 #include "DataFormats/MuonDetId/interface/CSCDetId.h"
00003 #include "TH2F.h"
00004 #include "TMath.h"
00005 
00006 static TMinuit *MuonResiduals6DOFrphiFitter_TMinuit;
00007 static double MuonResiduals6DOFrphiFitter_sum_of_weights;
00008 static double MuonResiduals6DOFrphiFitter_number_of_hits;
00009 static bool MuonResiduals6DOFrphiFitter_weightAlignment;
00010 
00011 const CSCGeometry *MuonResiduals6DOFrphiFitter_cscGeometry;
00012 static CSCDetId MuonResiduals6DOFrphiFitter_cscDetId;
00013 static double MuonResiduals6DOFrphiFitter_R;
00014 
00015 void MuonResiduals6DOFrphiFitter::inform(TMinuit *tMinuit) {
00016   MuonResiduals6DOFrphiFitter_TMinuit = tMinuit;
00017 }
00018 
00019 double MuonResiduals6DOFrphiFitter_residual(double delta_x, double delta_y, double delta_z, double delta_phix, double delta_phiy, double delta_phiz, double track_x, double track_y, double track_dxdz, double track_dydz, double R, double alpha, double resslope) {
00020   return delta_x - (track_x/R - 3.*pow(track_x/R, 3)) * delta_y - track_dxdz * delta_z - track_y * track_dxdz * delta_phix + track_x * track_dxdz * delta_phiy - track_y * delta_phiz + resslope * alpha;
00021 }
00022 
00023 double MuonResiduals6DOFrphiFitter_resslope(double delta_x, double delta_y, double delta_z, double delta_phix, double delta_phiy, double delta_phiz, double track_x, double track_y, double track_dxdz, double track_dydz, double R) {
00024   return -track_dxdz/2./R * delta_y + (track_x/R - track_dxdz * track_dydz) * delta_phix + (1. + track_dxdz * track_dxdz) * delta_phiy - track_dydz * delta_phiz;
00025 }
00026 
00027 double MuonResiduals6DOFrphiFitter_effectiveR(double x, double y) {
00028 //   CSCDetId id = MuonResiduals6DOFrphiFitter_cscDetId;
00029 //   CSCDetId layerId(id.endcap(), id.station(), id.ring(), id.chamber(), 3);
00030 
00031 //   int strip = MuonResiduals6DOFrphiFitter_cscGeometry->layer(layerId)->geometry()->nearestStrip(LocalPoint(x, y, 0.));
00032 //   double angle = MuonResiduals6DOFrphiFitter_cscGeometry->layer(layerId)->geometry()->stripAngle(strip) - M_PI/2.;
00033 
00034 //   if (fabs(angle) < 1e-10) return MuonResiduals6DOFrphiFitter_R;
00035 //   else return x/tan(angle) - y;
00036 
00037   return MuonResiduals6DOFrphiFitter_R;
00038 }
00039 
00040 Double_t MuonResiduals6DOFrphiFitter_residual_trackx_TF1(Double_t *xvec, Double_t *par) { return 10.*MuonResiduals6DOFrphiFitter_residual(par[0], par[1], par[2], par[3], par[4], par[5], xvec[0], par[7], par[8], par[9], MuonResiduals6DOFrphiFitter_effectiveR(xvec[0], par[7]), par[10], par[11]); }
00041 Double_t MuonResiduals6DOFrphiFitter_residual_tracky_TF1(Double_t *xvec, Double_t *par) { return 10.*MuonResiduals6DOFrphiFitter_residual(par[0], par[1], par[2], par[3], par[4], par[5], par[6], xvec[0], par[8], par[9], MuonResiduals6DOFrphiFitter_effectiveR(par[6], xvec[0]), par[10], par[11]); }
00042 Double_t MuonResiduals6DOFrphiFitter_residual_trackdxdz_TF1(Double_t *xvec, Double_t *par) { return 10.*MuonResiduals6DOFrphiFitter_residual(par[0], par[1], par[2], par[3], par[4], par[5], par[6], par[7], xvec[0], par[9], MuonResiduals6DOFrphiFitter_effectiveR(par[6], par[7]), par[10], par[11]); }
00043 Double_t MuonResiduals6DOFrphiFitter_residual_trackdydz_TF1(Double_t *xvec, Double_t *par) { return 10.*MuonResiduals6DOFrphiFitter_residual(par[0], par[1], par[2], par[3], par[4], par[5], par[6], par[7], par[8], xvec[0], MuonResiduals6DOFrphiFitter_effectiveR(par[6], par[7]), par[10], par[11]); }
00044 
00045 Double_t MuonResiduals6DOFrphiFitter_resslope_trackx_TF1(Double_t *xvec, Double_t *par) { return 1000.*MuonResiduals6DOFrphiFitter_resslope(par[0], par[1], par[2], par[3], par[4], par[5], xvec[0], par[7], par[8], par[9], MuonResiduals6DOFrphiFitter_effectiveR(xvec[0], par[7])); }
00046 Double_t MuonResiduals6DOFrphiFitter_resslope_tracky_TF1(Double_t *xvec, Double_t *par) { return 1000.*MuonResiduals6DOFrphiFitter_resslope(par[0], par[1], par[2], par[3], par[4], par[5], par[6], xvec[0], par[8], par[9], MuonResiduals6DOFrphiFitter_effectiveR(par[6], xvec[0])); }
00047 Double_t MuonResiduals6DOFrphiFitter_resslope_trackdxdz_TF1(Double_t *xvec, Double_t *par) { return 1000.*MuonResiduals6DOFrphiFitter_resslope(par[0], par[1], par[2], par[3], par[4], par[5], par[6], par[7], xvec[0], par[9], MuonResiduals6DOFrphiFitter_effectiveR(par[6], par[7])); }
00048 Double_t MuonResiduals6DOFrphiFitter_resslope_trackdydz_TF1(Double_t *xvec, Double_t *par) { return 1000.*MuonResiduals6DOFrphiFitter_resslope(par[0], par[1], par[2], par[3], par[4], par[5], par[6], par[7], par[8], xvec[0], MuonResiduals6DOFrphiFitter_effectiveR(par[6], par[7])); }
00049 
00050 void MuonResiduals6DOFrphiFitter_FCN(int &npar, double *gin, double &fval, double *par, int iflag) {
00051   MuonResidualsFitterFitInfo *fitinfo = (MuonResidualsFitterFitInfo*)(MuonResiduals6DOFrphiFitter_TMinuit->GetObjectFit());
00052   MuonResidualsFitter *fitter = fitinfo->fitter();
00053 
00054   fval = 0.;
00055   for (std::vector<double*>::const_iterator resiter = fitter->residuals_begin();  resiter != fitter->residuals_end();  ++resiter) {
00056     const double residual = (*resiter)[MuonResiduals6DOFrphiFitter::kResid];
00057     const double resslope = (*resiter)[MuonResiduals6DOFrphiFitter::kResSlope];
00058     const double positionX = (*resiter)[MuonResiduals6DOFrphiFitter::kPositionX];
00059     const double positionY = (*resiter)[MuonResiduals6DOFrphiFitter::kPositionY];
00060     const double angleX = (*resiter)[MuonResiduals6DOFrphiFitter::kAngleX];
00061     const double angleY = (*resiter)[MuonResiduals6DOFrphiFitter::kAngleY];
00062     const double redchi2 = (*resiter)[MuonResiduals6DOFrphiFitter::kRedChi2];
00063 
00064     const double alignx = par[MuonResiduals6DOFrphiFitter::kAlignX];
00065     const double aligny = par[MuonResiduals6DOFrphiFitter::kAlignY];
00066     const double alignz = par[MuonResiduals6DOFrphiFitter::kAlignZ];
00067     const double alignphix = par[MuonResiduals6DOFrphiFitter::kAlignPhiX];
00068     const double alignphiy = par[MuonResiduals6DOFrphiFitter::kAlignPhiY];
00069     const double alignphiz = par[MuonResiduals6DOFrphiFitter::kAlignPhiZ];
00070     const double residsigma = par[MuonResiduals6DOFrphiFitter::kResidSigma];
00071     const double resslopesigma = par[MuonResiduals6DOFrphiFitter::kResSlopeSigma];
00072     const double alpha = par[MuonResiduals6DOFrphiFitter::kAlpha];
00073     const double residgamma = par[MuonResiduals6DOFrphiFitter::kResidGamma];
00074     const double resslopegamma = par[MuonResiduals6DOFrphiFitter::kResSlopeGamma];
00075 
00076     double residpeak = MuonResiduals6DOFrphiFitter_residual(alignx, aligny, alignz, alignphix, alignphiy, alignphiz, positionX, positionY, angleX, angleY, MuonResiduals6DOFrphiFitter_effectiveR(positionX, positionY), alpha, resslope);
00077     double resslopepeak = MuonResiduals6DOFrphiFitter_resslope(alignx, aligny, alignz, alignphix, alignphiy, alignphiz, positionX, positionY, angleX, angleY, MuonResiduals6DOFrphiFitter_effectiveR(positionX, positionY));
00078 
00079     double weight = (1./redchi2) * MuonResiduals6DOFrphiFitter_number_of_hits / MuonResiduals6DOFrphiFitter_sum_of_weights;
00080     if (!MuonResiduals6DOFrphiFitter_weightAlignment) weight = 1.;
00081 
00082     if (!MuonResiduals6DOFrphiFitter_weightAlignment  ||  TMath::Prob(redchi2*6, 6) < 0.99) {  // no spikes allowed
00083 
00084       if (fitter->residualsModel() == MuonResidualsFitter::kPureGaussian) {
00085         fval += -weight * MuonResidualsFitter_logPureGaussian(residual, residpeak, residsigma);
00086         fval += -weight * MuonResidualsFitter_logPureGaussian(resslope, resslopepeak, resslopesigma);
00087       }
00088       else if (fitter->residualsModel() == MuonResidualsFitter::kPowerLawTails) {
00089         fval += -weight * MuonResidualsFitter_logPowerLawTails(residual, residpeak, residsigma, residgamma);
00090         fval += -weight * MuonResidualsFitter_logPowerLawTails(resslope, resslopepeak, resslopesigma, resslopegamma);
00091       }
00092       else if (fitter->residualsModel() == MuonResidualsFitter::kROOTVoigt) {
00093         fval += -weight * MuonResidualsFitter_logROOTVoigt(residual, residpeak, residsigma, residgamma);
00094         fval += -weight * MuonResidualsFitter_logROOTVoigt(resslope, resslopepeak, resslopesigma, resslopegamma);
00095       }
00096       else if (fitter->residualsModel() == MuonResidualsFitter::kGaussPowerTails) {
00097         fval += -weight * MuonResidualsFitter_logGaussPowerTails(residual, residpeak, residsigma);
00098         fval += -weight * MuonResidualsFitter_logGaussPowerTails(resslope, resslopepeak, resslopesigma);
00099       }
00100       else { assert(false); }
00101 
00102     }
00103   }
00104 }
00105 
00106 double MuonResiduals6DOFrphiFitter::sumofweights() {
00107   MuonResiduals6DOFrphiFitter_sum_of_weights = 0.;
00108   MuonResiduals6DOFrphiFitter_number_of_hits = 0.;
00109   MuonResiduals6DOFrphiFitter_weightAlignment = m_weightAlignment;
00110   for (std::vector<double*>::const_iterator resiter = residuals_begin();  resiter != residuals_end();  ++resiter) {
00111     if (m_weightAlignment) {
00112        double redchi2 = (*resiter)[MuonResiduals6DOFrphiFitter::kRedChi2];
00113        if (TMath::Prob(redchi2*6, 6) < 0.99) {  // no spikes allowed
00114           MuonResiduals6DOFrphiFitter_sum_of_weights += 1./redchi2;
00115           MuonResiduals6DOFrphiFitter_number_of_hits += 1.;
00116        }
00117     }
00118     else {
00119        MuonResiduals6DOFrphiFitter_sum_of_weights += 1.;
00120        MuonResiduals6DOFrphiFitter_number_of_hits += 1.;
00121     }
00122   }
00123   return MuonResiduals6DOFrphiFitter_sum_of_weights;
00124 }
00125 
00126 bool MuonResiduals6DOFrphiFitter::fit(Alignable *ali) {
00127   initialize_table();  // if not already initialized
00128   sumofweights();
00129 
00130   double resid_mean = 0;
00131   double resslope_mean = 0;
00132   double resid_stdev = 0.5;
00133   double resslope_stdev = 0.005;
00134   double alpha_estimate = 0;
00135 
00136   std::vector<int> num;
00137   std::vector<std::string> name;
00138   std::vector<double> start;
00139   std::vector<double> step;
00140   std::vector<double> low;
00141   std::vector<double> high;
00142 
00143   if (fixed(kAlignX)) {
00144   num.push_back(kAlignX);         name.push_back(std::string("AlignX"));         start.push_back(0.);              step.push_back(0.1);                      low.push_back(0.);   high.push_back(0.);
00145   } else {
00146   num.push_back(kAlignX);         name.push_back(std::string("AlignX"));         start.push_back(resid_mean);      step.push_back(0.1);                      low.push_back(0.);   high.push_back(0.);
00147   }
00148   num.push_back(kAlignY);         name.push_back(std::string("AlignY"));         start.push_back(0.);              step.push_back(0.1);                      low.push_back(0.);   high.push_back(0.);
00149   num.push_back(kAlignZ);         name.push_back(std::string("AlignZ"));         start.push_back(0.);              step.push_back(0.1);                      low.push_back(0.);   high.push_back(0.);
00150   num.push_back(kAlignPhiX);      name.push_back(std::string("AlignPhiX"));      start.push_back(0.);              step.push_back(0.001);                    low.push_back(0.);   high.push_back(0.);
00151   if (fixed(kAlignPhiY)) {
00152   num.push_back(kAlignPhiY);      name.push_back(std::string("AlignPhiY"));      start.push_back(0.);              step.push_back(0.001);                    low.push_back(0.);   high.push_back(0.);
00153   } else {
00154   num.push_back(kAlignPhiY);      name.push_back(std::string("AlignPhiY"));      start.push_back(resslope_mean);   step.push_back(0.001);                    low.push_back(0.);   high.push_back(0.);
00155   }
00156   num.push_back(kAlignPhiZ);      name.push_back(std::string("AlignPhiZ"));      start.push_back(0.);              step.push_back(0.001);                    low.push_back(0.);   high.push_back(0.);
00157   num.push_back(kResidSigma);     name.push_back(std::string("ResidSigma"));     start.push_back(resid_stdev);     step.push_back(0.01*resid_stdev);         low.push_back(0.);   high.push_back(0.);
00158   num.push_back(kResSlopeSigma);  name.push_back(std::string("ResSlopeSigma"));  start.push_back(resslope_stdev);  step.push_back(0.01*resslope_stdev);      low.push_back(0.);   high.push_back(0.);
00159   num.push_back(kAlpha);          name.push_back(std::string("Alpha"));          start.push_back(alpha_estimate);  step.push_back(0.01*resslope_stdev);      low.push_back(0.);   high.push_back(0.);
00160   if (residualsModel() != kPureGaussian && residualsModel() != kGaussPowerTails) {
00161   num.push_back(kResidGamma);     name.push_back(std::string("ResidGamma"));     start.push_back(0.1*resid_stdev);     step.push_back(0.01*resid_stdev);     low.push_back(0.);   high.push_back(0.);
00162   num.push_back(kResSlopeGamma);  name.push_back(std::string("ResSlopeGamma"));  start.push_back(0.1*resslope_stdev);  step.push_back(0.01*resslope_stdev);  low.push_back(0.);   high.push_back(0.);
00163   }
00164 
00165   return dofit(&MuonResiduals6DOFrphiFitter_FCN, num, name, start, step, low, high);
00166 }
00167 
00168 double MuonResiduals6DOFrphiFitter::plot(std::string name, TFileDirectory *dir, Alignable *ali) {
00169   sumofweights();
00170 
00171   MuonResiduals6DOFrphiFitter_R = sqrt(pow(ali->globalPosition().x(), 2) + pow(ali->globalPosition().y(), 2));
00172   MuonResiduals6DOFrphiFitter_cscGeometry = m_cscGeometry;
00173   MuonResiduals6DOFrphiFitter_cscDetId = CSCDetId(ali->geomDetId().rawId());
00174 
00175   std::stringstream name_residual, name_resslope, name_residual_raw, name_resslope_raw, name_residual_cut, name_alpha;
00176   std::stringstream name_residual_trackx, name_resslope_trackx;
00177   std::stringstream name_residual_tracky, name_resslope_tracky;
00178   std::stringstream name_residual_trackdxdz, name_resslope_trackdxdz;
00179   std::stringstream name_residual_trackdydz, name_resslope_trackdydz;
00180 
00181   name_residual << name << "_residual";
00182   name_resslope << name << "_resslope";
00183   name_residual_raw << name << "_residual_raw";
00184   name_resslope_raw << name << "_resslope_raw";
00185   name_residual_cut << name << "_residual_cut";
00186   name_alpha << name << "_alpha";
00187   name_residual_trackx << name << "_residual_trackx";
00188   name_resslope_trackx << name << "_resslope_trackx";
00189   name_residual_tracky << name << "_residual_tracky";
00190   name_resslope_tracky << name << "_resslope_tracky";
00191   name_residual_trackdxdz << name << "_residual_trackdxdz";
00192   name_resslope_trackdxdz << name << "_resslope_trackdxdz";
00193   name_residual_trackdydz << name << "_residual_trackdydz";
00194   name_resslope_trackdydz << name << "_resslope_trackdydz";
00195 
00196   double width = ali->surface().width();
00197   double length = ali->surface().length();
00198   double min_residual = -100.;     double max_residual = 100.;
00199   double min_resslope = -100.;     double max_resslope = 100.;
00200   double min_trackx = -width/2.;   double max_trackx = width/2.;
00201   double min_tracky = -length/2.;  double max_tracky = length/2.;
00202   double min_trackdxdz = -1.5;     double max_trackdxdz = 1.5;
00203   double min_trackdydz = -1.5;     double max_trackdydz = 1.5;
00204 
00205   TH1F *hist_residual = dir->make<TH1F>(name_residual.str().c_str(), "", 100, min_residual, max_residual);
00206   TH1F *hist_resslope = dir->make<TH1F>(name_resslope.str().c_str(), "", 100, min_resslope, max_resslope);
00207   TH1F *hist_residual_raw = dir->make<TH1F>(name_residual_raw.str().c_str(), "", 100, min_residual, max_residual);
00208   TH1F *hist_resslope_raw = dir->make<TH1F>(name_resslope_raw.str().c_str(), "", 100, min_resslope, max_resslope);
00209   TH1F *hist_residual_cut = dir->make<TH1F>(name_residual_cut.str().c_str(), "", 100, min_residual, max_residual);
00210   TH2F *hist_alpha = dir->make<TH2F>(name_alpha.str().c_str(),"", 40, min_resslope, max_resslope, 40, -20., 20.);
00211   TProfile *hist_residual_trackx = dir->make<TProfile>(name_residual_trackx.str().c_str(), "", 100, min_trackx, max_trackx, min_residual, max_residual);
00212   TProfile *hist_resslope_trackx = dir->make<TProfile>(name_resslope_trackx.str().c_str(), "", 100, min_trackx, max_trackx, min_resslope, max_resslope);
00213   TProfile *hist_residual_tracky = dir->make<TProfile>(name_residual_tracky.str().c_str(), "", 100, min_tracky, max_tracky, min_residual, max_residual);
00214   TProfile *hist_resslope_tracky = dir->make<TProfile>(name_resslope_tracky.str().c_str(), "", 100, min_tracky, max_tracky, min_resslope, max_resslope);
00215   TProfile *hist_residual_trackdxdz = dir->make<TProfile>(name_residual_trackdxdz.str().c_str(), "", 500, min_trackdxdz, max_trackdxdz, min_residual, max_residual);
00216   TProfile *hist_resslope_trackdxdz = dir->make<TProfile>(name_resslope_trackdxdz.str().c_str(), "", 500, min_trackdxdz, max_trackdxdz, min_resslope, max_resslope);
00217   TProfile *hist_residual_trackdydz = dir->make<TProfile>(name_residual_trackdydz.str().c_str(), "", 500, min_trackdydz, max_trackdydz, min_residual, max_residual);
00218   TProfile *hist_resslope_trackdydz = dir->make<TProfile>(name_resslope_trackdydz.str().c_str(), "", 500, min_trackdydz, max_trackdydz, min_resslope, max_resslope);
00219 
00220   hist_residual_trackx->SetAxisRange(-10., 10., "Y");
00221   hist_resslope_trackx->SetAxisRange(-10., 10., "Y");
00222   hist_residual_tracky->SetAxisRange(-10., 10., "Y");
00223   hist_resslope_tracky->SetAxisRange(-10., 10., "Y");
00224   hist_residual_trackdxdz->SetAxisRange(-10., 10., "Y");
00225   hist_resslope_trackdxdz->SetAxisRange(-10., 10., "Y");
00226   hist_residual_trackdydz->SetAxisRange(-10., 10., "Y");
00227   hist_resslope_trackdydz->SetAxisRange(-10., 10., "Y");
00228 
00229   name_residual << "_fit";
00230   name_resslope << "_fit";
00231   name_alpha << "_fit";
00232   name_residual_trackx << "_fit";
00233   name_resslope_trackx << "_fit";
00234   name_residual_tracky << "_fit";
00235   name_resslope_tracky << "_fit";
00236   name_residual_trackdxdz << "_fit";
00237   name_resslope_trackdxdz << "_fit";
00238   name_residual_trackdydz << "_fit";
00239   name_resslope_trackdydz << "_fit";
00240 
00241   TF1 *fit_residual = NULL;
00242   TF1 *fit_resslope = NULL;
00243   if (residualsModel() == kPureGaussian) {
00244     fit_residual = new TF1(name_residual.str().c_str(), MuonResidualsFitter_pureGaussian_TF1, min_residual, max_residual, 3);
00245     fit_residual->SetParameters(MuonResiduals6DOFrphiFitter_sum_of_weights * (max_residual - min_residual)/100., 10.*value(kAlignX), 10.*value(kResidSigma));
00246     fit_resslope = new TF1(name_resslope.str().c_str(), MuonResidualsFitter_pureGaussian_TF1, min_resslope, max_resslope, 3);
00247     fit_resslope->SetParameters(MuonResiduals6DOFrphiFitter_sum_of_weights * (max_resslope - min_resslope)/100., 1000.*value(kAlignPhiY), 1000.*value(kResSlopeSigma));
00248   }
00249   else if (residualsModel() == kPowerLawTails) {
00250     fit_residual = new TF1(name_residual.str().c_str(), MuonResidualsFitter_powerLawTails_TF1, min_residual, max_residual, 4);
00251     fit_residual->SetParameters(MuonResiduals6DOFrphiFitter_sum_of_weights * (max_residual - min_residual)/100., 10.*value(kAlignX), 10.*value(kResidSigma), 10.*value(kResidGamma));
00252     fit_resslope = new TF1(name_resslope.str().c_str(), MuonResidualsFitter_powerLawTails_TF1, min_resslope, max_resslope, 4);
00253     fit_resslope->SetParameters(MuonResiduals6DOFrphiFitter_sum_of_weights * (max_resslope - min_resslope)/100., 1000.*value(kAlignPhiY), 1000.*value(kResSlopeSigma), 1000.*value(kResSlopeGamma));
00254   }
00255   else if (residualsModel() == kROOTVoigt) {
00256     fit_residual = new TF1(name_residual.str().c_str(), MuonResidualsFitter_ROOTVoigt_TF1, min_residual, max_residual, 4);
00257     fit_residual->SetParameters(MuonResiduals6DOFrphiFitter_sum_of_weights * (max_residual - min_residual)/100., 10.*value(kAlignX), 10.*value(kResidSigma), 10.*value(kResidGamma));
00258     fit_resslope = new TF1(name_resslope.str().c_str(), MuonResidualsFitter_ROOTVoigt_TF1, min_resslope, max_resslope, 4);
00259     fit_resslope->SetParameters(MuonResiduals6DOFrphiFitter_sum_of_weights * (max_resslope - min_resslope)/100., 1000.*value(kAlignPhiY), 1000.*value(kResSlopeSigma), 1000.*value(kResSlopeGamma));
00260   }
00261   else if (residualsModel() == kGaussPowerTails) {
00262     fit_residual = new TF1(name_residual.str().c_str(), MuonResidualsFitter_GaussPowerTails_TF1, min_residual, max_residual, 3);
00263     fit_residual->SetParameters(MuonResiduals6DOFrphiFitter_sum_of_weights * (max_residual - min_residual)/100., 10.*value(kAlignX), 10.*value(kResidSigma));
00264     fit_resslope = new TF1(name_resslope.str().c_str(), MuonResidualsFitter_GaussPowerTails_TF1, min_resslope, max_resslope, 3);
00265     fit_resslope->SetParameters(MuonResiduals6DOFrphiFitter_sum_of_weights * (max_resslope - min_resslope)/100., 1000.*value(kAlignPhiY), 1000.*value(kResSlopeSigma));
00266   }
00267   else { assert(false); }
00268 
00269   fit_residual->SetLineColor(2);  fit_residual->SetLineWidth(2);
00270   fit_resslope->SetLineColor(2);  fit_resslope->SetLineWidth(2);
00271   fit_residual->Write();
00272   fit_resslope->Write();
00273 
00274   TF1 *fit_alpha = new TF1(name_alpha.str().c_str(), "[0] + x*[1]", min_resslope, max_resslope);
00275   fit_alpha->SetParameters(10.*value(kAlignX), 10.*value(kAlpha)/1000.);
00276   fit_alpha->SetLineColor(2);  fit_alpha->SetLineWidth(2);
00277   fit_alpha->Write();
00278 
00279   TProfile *fit_residual_trackx = dir->make<TProfile>(name_residual_trackx.str().c_str(), "", 100, min_trackx, max_trackx);
00280   TProfile *fit_resslope_trackx = dir->make<TProfile>(name_resslope_trackx.str().c_str(), "", 100, min_trackx, max_trackx);
00281   TProfile *fit_residual_tracky = dir->make<TProfile>(name_residual_tracky.str().c_str(), "", 100, min_tracky, max_tracky);
00282   TProfile *fit_resslope_tracky = dir->make<TProfile>(name_resslope_tracky.str().c_str(), "", 100, min_tracky, max_tracky);
00283   TProfile *fit_residual_trackdxdz = dir->make<TProfile>(name_residual_trackdxdz.str().c_str(), "", 500, min_trackdxdz, max_trackdxdz);
00284   TProfile *fit_resslope_trackdxdz = dir->make<TProfile>(name_resslope_trackdxdz.str().c_str(), "", 500, min_trackdxdz, max_trackdxdz);
00285   TProfile *fit_residual_trackdydz = dir->make<TProfile>(name_residual_trackdydz.str().c_str(), "", 500, min_trackdydz, max_trackdydz);
00286   TProfile *fit_resslope_trackdydz = dir->make<TProfile>(name_resslope_trackdydz.str().c_str(), "", 500, min_trackdydz, max_trackdydz);
00287 
00288   fit_residual_trackx->SetLineColor(2);     fit_residual_trackx->SetLineWidth(2);
00289   fit_resslope_trackx->SetLineColor(2);     fit_resslope_trackx->SetLineWidth(2);
00290   fit_residual_tracky->SetLineColor(2);     fit_residual_tracky->SetLineWidth(2);
00291   fit_resslope_tracky->SetLineColor(2);     fit_resslope_tracky->SetLineWidth(2);
00292   fit_residual_trackdxdz->SetLineColor(2);  fit_residual_trackdxdz->SetLineWidth(2);
00293   fit_resslope_trackdxdz->SetLineColor(2);  fit_resslope_trackdxdz->SetLineWidth(2);
00294   fit_residual_trackdydz->SetLineColor(2);  fit_residual_trackdydz->SetLineWidth(2);
00295   fit_resslope_trackdydz->SetLineColor(2);  fit_resslope_trackdydz->SetLineWidth(2);
00296 
00297   name_residual_trackx << "line";
00298   name_resslope_trackx << "line";
00299   name_residual_tracky << "line";
00300   name_resslope_tracky << "line";
00301   name_residual_trackdxdz << "line";
00302   name_resslope_trackdxdz << "line";
00303   name_residual_trackdydz << "line";
00304   name_resslope_trackdydz << "line";
00305 
00306   TF1 *fitline_residual_trackx = new TF1(name_residual_trackx.str().c_str(), MuonResiduals6DOFrphiFitter_residual_trackx_TF1, min_trackx, max_trackx, 12);
00307   TF1 *fitline_resslope_trackx = new TF1(name_resslope_trackx.str().c_str(), MuonResiduals6DOFrphiFitter_resslope_trackx_TF1, min_trackx, max_trackx, 12);
00308   TF1 *fitline_residual_tracky = new TF1(name_residual_tracky.str().c_str(), MuonResiduals6DOFrphiFitter_residual_tracky_TF1, min_tracky, max_tracky, 12);
00309   TF1 *fitline_resslope_tracky = new TF1(name_resslope_tracky.str().c_str(), MuonResiduals6DOFrphiFitter_resslope_tracky_TF1, min_tracky, max_tracky, 12);
00310   TF1 *fitline_residual_trackdxdz = new TF1(name_residual_trackdxdz.str().c_str(), MuonResiduals6DOFrphiFitter_residual_trackdxdz_TF1, min_trackdxdz, max_trackdxdz, 12);
00311   TF1 *fitline_resslope_trackdxdz = new TF1(name_resslope_trackdxdz.str().c_str(), MuonResiduals6DOFrphiFitter_resslope_trackdxdz_TF1, min_trackdxdz, max_trackdxdz, 12);
00312   TF1 *fitline_residual_trackdydz = new TF1(name_residual_trackdydz.str().c_str(), MuonResiduals6DOFrphiFitter_residual_trackdydz_TF1, min_trackdydz, max_trackdydz, 12);
00313   TF1 *fitline_resslope_trackdydz = new TF1(name_resslope_trackdydz.str().c_str(), MuonResiduals6DOFrphiFitter_resslope_trackdydz_TF1, min_trackdydz, max_trackdydz, 12);
00314 
00315   double sum_resslope = 0.;
00316   double sum_trackx = 0.;
00317   double sum_tracky = 0.;
00318   double sum_trackdxdz = 0.;
00319   double sum_trackdydz = 0.;
00320   for (std::vector<double*>::const_iterator resiter = residuals_begin();  resiter != residuals_end();  ++resiter) {
00321     const double resslope = (*resiter)[MuonResiduals6DOFrphiFitter::kResSlope];
00322     const double positionX = (*resiter)[MuonResiduals6DOFrphiFitter::kPositionX];
00323     const double positionY = (*resiter)[MuonResiduals6DOFrphiFitter::kPositionY];
00324     const double angleX = (*resiter)[MuonResiduals6DOFrphiFitter::kAngleX];
00325     const double angleY = (*resiter)[MuonResiduals6DOFrphiFitter::kAngleY];
00326     const double redchi2 = (*resiter)[MuonResiduals6DOFrphiFitter::kRedChi2];
00327     double weight = 1./redchi2;
00328     if (!m_weightAlignment) weight = 1.;
00329 
00330     if (!m_weightAlignment  ||  TMath::Prob(redchi2*6, 6) < 0.99) {  // no spikes allowed
00331       sum_resslope += weight * resslope;
00332       sum_trackx += weight * positionX;
00333       sum_tracky += weight * positionY;
00334       sum_trackdxdz += weight * angleX;
00335       sum_trackdydz += weight * angleY;
00336     }
00337   }
00338   double mean_resslope = sum_resslope / MuonResiduals6DOFrphiFitter_sum_of_weights;
00339   double mean_trackx = sum_trackx / MuonResiduals6DOFrphiFitter_sum_of_weights;
00340   double mean_tracky = sum_tracky / MuonResiduals6DOFrphiFitter_sum_of_weights;
00341   double mean_trackdxdz = sum_trackdxdz / MuonResiduals6DOFrphiFitter_sum_of_weights;
00342   double mean_trackdydz = sum_trackdydz / MuonResiduals6DOFrphiFitter_sum_of_weights;
00343 
00344   double fitparameters[12];
00345   fitparameters[0] = value(kAlignX);
00346   fitparameters[1] = value(kAlignY);
00347   fitparameters[2] = value(kAlignZ);
00348   fitparameters[3] = value(kAlignPhiX);
00349   fitparameters[4] = value(kAlignPhiY);
00350   fitparameters[5] = value(kAlignPhiZ);
00351   fitparameters[6] = mean_trackx;
00352   fitparameters[7] = mean_tracky;
00353   fitparameters[8] = mean_trackdxdz;
00354   fitparameters[9] = mean_trackdydz;
00355   fitparameters[10] = value(kAlpha);
00356   fitparameters[11] = mean_resslope;
00357 
00358   fitline_residual_trackx->SetParameters(fitparameters);
00359   fitline_resslope_trackx->SetParameters(fitparameters);
00360   fitline_residual_tracky->SetParameters(fitparameters);
00361   fitline_resslope_tracky->SetParameters(fitparameters);
00362   fitline_residual_trackdxdz->SetParameters(fitparameters);
00363   fitline_resslope_trackdxdz->SetParameters(fitparameters);
00364   fitline_residual_trackdydz->SetParameters(fitparameters);
00365   fitline_resslope_trackdydz->SetParameters(fitparameters);
00366 
00367   fitline_residual_trackx->SetLineColor(2);        fitline_residual_trackx->SetLineWidth(2);
00368   fitline_resslope_trackx->SetLineColor(2);        fitline_resslope_trackx->SetLineWidth(2);
00369   fitline_residual_tracky->SetLineColor(2);        fitline_residual_tracky->SetLineWidth(2);
00370   fitline_resslope_tracky->SetLineColor(2);        fitline_resslope_tracky->SetLineWidth(2);
00371   fitline_residual_trackdxdz->SetLineColor(2);     fitline_residual_trackdxdz->SetLineWidth(2);
00372   fitline_resslope_trackdxdz->SetLineColor(2);     fitline_resslope_trackdxdz->SetLineWidth(2);
00373   fitline_residual_trackdydz->SetLineColor(2);     fitline_residual_trackdydz->SetLineWidth(2);
00374   fitline_resslope_trackdydz->SetLineColor(2);     fitline_resslope_trackdydz->SetLineWidth(2);
00375 
00376   fitline_residual_trackx->Write();
00377   fitline_resslope_trackx->Write();
00378   fitline_residual_tracky->Write();
00379   fitline_resslope_tracky->Write();
00380   fitline_residual_trackdxdz->Write();
00381   fitline_resslope_trackdxdz->Write();
00382   fitline_residual_trackdydz->Write();
00383   fitline_resslope_trackdydz->Write();
00384 
00385   for (std::vector<double*>::const_iterator resiter = residuals_begin();  resiter != residuals_end();  ++resiter) {
00386     const double resid = (*resiter)[MuonResiduals6DOFrphiFitter::kResid];
00387     const double resslope = (*resiter)[MuonResiduals6DOFrphiFitter::kResSlope];
00388     const double positionX = (*resiter)[MuonResiduals6DOFrphiFitter::kPositionX];
00389     const double positionY = (*resiter)[MuonResiduals6DOFrphiFitter::kPositionY];
00390     const double angleX = (*resiter)[MuonResiduals6DOFrphiFitter::kAngleX];
00391     const double angleY = (*resiter)[MuonResiduals6DOFrphiFitter::kAngleY];
00392     const double redchi2 = (*resiter)[MuonResiduals6DOFrphiFitter::kRedChi2];
00393     double weight = 1./redchi2;
00394     if (!m_weightAlignment) weight = 1.;
00395 
00396     if (!m_weightAlignment  ||  TMath::Prob(redchi2*6, 6) < 0.99) {  // no spikes allowed
00397       hist_alpha->Fill(1000.*resslope, 10.*resid);
00398 
00399       double geom_resid = MuonResiduals6DOFrphiFitter_residual(value(kAlignX), value(kAlignY), value(kAlignZ), value(kAlignPhiX), value(kAlignPhiY), value(kAlignPhiZ), positionX, positionY, angleX, angleY, MuonResiduals6DOFrphiFitter_effectiveR(positionX, positionY), value(kAlpha), resslope);
00400       hist_residual->Fill(10.*(resid - geom_resid + value(kAlignX)), weight);
00401       hist_residual_trackx->Fill(positionX, 10.*resid, weight);
00402       hist_residual_tracky->Fill(positionY, 10.*resid, weight);
00403       hist_residual_trackdxdz->Fill(angleX, 10.*resid, weight);
00404       hist_residual_trackdydz->Fill(angleY, 10.*resid, weight);
00405       fit_residual_trackx->Fill(positionX, 10.*geom_resid, weight);
00406       fit_residual_tracky->Fill(positionY, 10.*geom_resid, weight);
00407       fit_residual_trackdxdz->Fill(angleX, 10.*geom_resid, weight);
00408       fit_residual_trackdydz->Fill(angleY, 10.*geom_resid, weight);
00409 
00410       double geom_resslope = MuonResiduals6DOFrphiFitter_resslope(value(kAlignX), value(kAlignY), value(kAlignZ), value(kAlignPhiX), value(kAlignPhiY), value(kAlignPhiZ), positionX, positionY, angleX, angleY, MuonResiduals6DOFrphiFitter_effectiveR(positionX, positionY));
00411       hist_resslope->Fill(1000.*(resslope - geom_resslope + value(kAlignPhiY)), weight);
00412       hist_resslope_trackx->Fill(positionX, 1000.*resslope, weight);
00413       hist_resslope_tracky->Fill(positionY, 1000.*resslope, weight);
00414       hist_resslope_trackdxdz->Fill(angleX, 1000.*resslope, weight);
00415       hist_resslope_trackdydz->Fill(angleY, 1000.*resslope, weight);
00416       fit_resslope_trackx->Fill(positionX, 1000.*geom_resslope, weight);
00417       fit_resslope_tracky->Fill(positionY, 1000.*geom_resslope, weight);
00418       fit_resslope_trackdxdz->Fill(angleX, 1000.*geom_resslope, weight);
00419       fit_resslope_trackdydz->Fill(angleY, 1000.*geom_resslope, weight);
00420     }
00421 
00422     hist_residual_raw->Fill(10.*resid);
00423     hist_resslope_raw->Fill(1000.*resslope);
00424     if (fabs(resslope) < 0.005) hist_residual_cut->Fill(10.*resid);
00425   }
00426 
00427   double chi2 = 0.;
00428   double ndof = 0.;
00429   for (int i = 1;  i <= hist_residual->GetNbinsX();  i++) {
00430     double xi = hist_residual->GetBinCenter(i);
00431     double yi = hist_residual->GetBinContent(i);
00432     double yerri = hist_residual->GetBinError(i);
00433     double yth = fit_residual->Eval(xi);
00434     if (yerri > 0.) {
00435       chi2 += pow((yth - yi)/yerri, 2);
00436       ndof += 1.;
00437     }
00438   }
00439   for (int i = 1;  i <= hist_resslope->GetNbinsX();  i++) {
00440     double xi = hist_resslope->GetBinCenter(i);
00441     double yi = hist_resslope->GetBinContent(i);
00442     double yerri = hist_resslope->GetBinError(i);
00443     double yth = fit_resslope->Eval(xi);
00444     if (yerri > 0.) {
00445       chi2 += pow((yth - yi)/yerri, 2);
00446       ndof += 1.;
00447     }
00448   }
00449   ndof -= npar();
00450 
00451   return (ndof > 0. ? chi2 / ndof : -1.);
00452 }