CMS 3D CMS Logo

Classes | Public Types | Public Member Functions | Protected Member Functions | Protected Attributes

MuonResidualsFitter Class Reference

#include <MuonResidualsFitter.h>

Inheritance diagram for MuonResidualsFitter:
MuonResiduals1DOFFitter MuonResiduals5DOFFitter MuonResiduals6DOFFitter MuonResiduals6DOFrphiFitter MuonResidualsAngleFitter MuonResidualsBfieldAngleFitter MuonResidualsPositionFitter

List of all members.

Classes

struct  MuonAlignmentTreeRow

Public Types

enum  {
  kPureGaussian, kPowerLawTails, kROOTVoigt, kGaussPowerTails,
  kPureGaussian2D
}
enum  {
  k1DOF, k5DOF, k6DOF, k6DOFrphi,
  kPositionFitter, kAngleFitter, kAngleBfieldFitter
}
enum  {
  k1111, k1110, k1100, k1010,
  k0010
}

Public Member Functions

void computeHistogramRangeAndBinning (int which, int &nbins, double &a, double &b)
virtual void correctBField ()=0
virtual void correctBField (int idx_momentum, int idx_q)
TMatrixDSym correlationMatrix ()
double covarianceElement (int parNum1, int parNum2)
TMatrixDSym covarianceMatrix ()
void eraseNotSelectedResiduals ()
double errorerror (int parNum)
void fill (double *residual)
virtual bool fit (Alignable *ali)=0
void fix (int parNum, bool val=true)
bool fixed (int parNum)
void histogramChi2GaussianFit (int which, double &fit_mean, double &fit_sigma)
double loglikelihood ()
 MuonResidualsFitter (int residualsModel, int minHits, int useResiduals, bool weightAlignment=true)
virtual int ndata ()=0
int nfixed ()
virtual int npar ()=0
long numResiduals () const
long numsegments ()
int parNum2parIdx (int parNum)
virtual double plot (std::string name, TFileDirectory *dir, Alignable *ali)=0
void plotsimple (std::string name, TFileDirectory *dir, int which, double multiplier)
void plotweighted (std::string name, TFileDirectory *dir, int which, int whichredchi2, double multiplier)
void read (FILE *file, int which=0)
std::vector< double * >
::const_iterator 
residuals_begin () const
std::vector< double * >
::const_iterator 
residuals_end () const
int residualsModel () const
std::vector< bool > & selectedResidualsFlags ()
void selectPeakResiduals (double nsigma, int nvar, int *vars)
void selectPeakResiduals_simple (double nsigma, int nvar, int *vars)
void setPrintLevel (int printLevel)
void setStrategy (int strategy)
virtual double sumofweights ()=0
virtual int type () const =0
int useRes () const
double value (int parNum)
void write (FILE *file, int which=0)
virtual ~MuonResidualsFitter ()

Protected Member Functions

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)
virtual void inform (TMinuit *tMinuit)=0
void initialize_table ()

Protected Attributes

double m_center [20]
TMatrixDSym m_cov
std::vector< double > m_error
std::vector< bool > m_fixed
double m_loglikelihood
int m_minHits
std::map< int, int > m_parNum2parIdx
int m_printLevel
double m_radii [20]
std::vector< double * > m_residuals
std::vector< bool > m_residuals_ok
int m_residualsModel
int m_strategy
int m_useResiduals
std::vector< double > m_value
bool m_weightAlignment

Detailed Description

Date:
2011/10/12 23:44:10
Revision:
1.17
Author:
J. Pivarski - Texas A&M University <pivarski@physics.tamu.edu>
Id:
MuonResidualsFitter.h,v 1.17 2011/10/12 23:44:10 khotilov Exp

Definition at line 80 of file MuonResidualsFitter.h.


Member Enumeration Documentation

anonymous enum
Enumerator:
kPureGaussian 
kPowerLawTails 
kROOTVoigt 
kGaussPowerTails 
kPureGaussian2D 

Definition at line 83 of file MuonResidualsFitter.h.

anonymous enum
Enumerator:
k1DOF 
k5DOF 
k6DOF 
k6DOFrphi 
kPositionFitter 
kAngleFitter 
kAngleBfieldFitter 

Definition at line 91 of file MuonResidualsFitter.h.

anonymous enum
Enumerator:
k1111 
k1110 
k1100 
k1010 
k0010 

Definition at line 101 of file MuonResidualsFitter.h.


Constructor & Destructor Documentation

MuonResidualsFitter::MuonResidualsFitter ( int  residualsModel,
int  minHits,
int  useResiduals,
bool  weightAlignment = true 
) [inline]
virtual MuonResidualsFitter::~MuonResidualsFitter ( ) [inline, virtual]

Definition at line 138 of file MuonResidualsFitter.h.

References residuals_begin(), and residuals_end().

  {
    for (std::vector<double*>::const_iterator residual = residuals_begin();  residual != residuals_end();  ++residual) {
      delete [] (*residual);
    }
  }

Member Function Documentation

void MuonResidualsFitter::computeHistogramRangeAndBinning ( int  which,
int &  nbins,
double &  a,
double &  b 
)

Definition at line 455 of file MuonResidualsFitter.cc.

References gather_cfg::cout, AlCaHLTBitMon_QueryRunRegistry::data, i, m_residuals, n, NULL, numResiduals(), funct::pow(), alignCSCRings::r, and python::multivaluedict::sort().

Referenced by histogramChi2GaussianFit().

{
  // first, make a numeric array while discarding some crazy outliers
  double *data = new double[numResiduals()];
  int n = 0;
  for (std::vector<double*>::const_iterator r = m_residuals.begin();  r != m_residuals.end();  r++)  
    if (fabs((*r)[which])<50.) 
    {
      data[n] = (*r)[which];
      n++;
    }
  
  // compute "3 normal sigma" and regular interquantile ranges
  const int n_quantiles = 7;
  double probabilities[n_quantiles] = {0.00135, 0.02275, 0.25, 0.5, 0.75, 0.97725, 0.99865}; // "3 normal sigma"
  //double probabilities[n_quantiles] = {0.02275, 0.25, 0.75, 0.97725}; // "2 normal sigma"
  double quantiles[n_quantiles];
  std::sort(data, data + n);
  TMath::Quantiles(n, n_quantiles, data, quantiles, probabilities, true, NULL, 7);
  delete [] data;
  double iqr = quantiles[4] - quantiles[2];
  
  // estimate optimal bin size according to Freedman-Diaconis rule
  double hbin = 2 * iqr / pow( n, 1./3);

  a = quantiles[1];
  b = quantiles[5];
  nbins = (int) ( (b - a) / hbin + 3. ); // add extra safety margin of 3

  std::cout<<"   quantiles: ";  for (int i=0;i<n_quantiles;i++) std::cout<<quantiles[i]<<" "; std::cout<<std::endl;
  //cout<<"n="<<select_count<<" quantiles ["<<quantiles[1]<<", "<<quantiles[2]<<"]  IQR="<<iqr
  //  <<"  full range=["<<minx<<","<<maxx<<"]"<<"  2 normal sigma quantile range = ["<<quantiles[0]<<", "<<quantiles[3]<<"]"<<endl;
  std::cout<<"   optimal h="<<hbin<<" nbins="<<nbins<<std::endl;
}
virtual void MuonResidualsFitter::correctBField ( ) [pure virtual]
void MuonResidualsFitter::correctBField ( int  idx_momentum,
int  idx_q 
) [virtual]

Definition at line 646 of file MuonResidualsFitter.cc.

References newFWLiteAna::bin, prof2calltree::count, gather_cfg::cout, alignCSCRings::e, i, j, m_residuals, m_residuals_ok, neg, pos, alignCSCRings::r, residuals_begin(), residuals_end(), findQualityFiles::size, and python::multivaluedict::sort().

{
  const int Nbin = 17;
  // find max 1/pt and bin width
  double min_pt = 9999.;
  for (std::vector<double*>::const_iterator r = residuals_begin();  r != residuals_end();  ++r)
  {
    double pt = fabs((*r)[idx_momentum]);
    if (pt < min_pt) min_pt = pt;
  }
  min_pt -= 0.01; // to prevent bin # overflow
  const double bin_width = 1./min_pt/Nbin;

  // fill indices of positive and negative charge residuals in each bin
  std::vector<size_t> pos[Nbin], neg[Nbin], to_erase;
  for (size_t i = 0; i < m_residuals.size(); i++)
  {
    if (!m_residuals_ok[i]) continue;
    int bin = (int)floor(1./fabs(m_residuals[i][idx_momentum])/bin_width);
    if (m_residuals[i][idx_q] > 0) pos[bin].push_back(i);
    else                           neg[bin].push_back(i);
  }

  // equalize pos and neg in each bin
  for (int j = 0; j < Nbin; j++)
  {
    size_t psize = pos[j].size();
    size_t nsize = neg[j].size();
    if (psize == nsize) continue;

    std::set<int> idx_set; // use a set to collect certain number of unique random indices to erase
    if (psize > nsize)
    {
      while (idx_set.size() < psize - nsize)  idx_set.insert( gRandom->Integer(psize) );
      for (std::set<int>::iterator it = idx_set.begin() ; it != idx_set.end(); it++ )  to_erase.push_back(pos[j][*it]);
    }
    else
    {
      while (idx_set.size() < nsize - psize)  idx_set.insert( gRandom->Integer(nsize) );
      for (std::set<int>::iterator it = idx_set.begin() ; it != idx_set.end(); it++ )  to_erase.push_back(neg[j][*it]);
    }
  }
  // sort in descending order, so we safely go from higher to lower indices:
  std::sort(to_erase.begin(), to_erase.end(), std::greater<int>() );
  for (std::vector<size_t>::const_iterator e = to_erase.begin();  e != to_erase.end();  ++e)
  {
    m_residuals_ok[*e] = false;
    //delete[] *(m_residuals.begin() + *e);
    //m_residuals.erase(m_residuals.begin() + *e);
  }

  std::vector<size_t> apos[Nbin], aneg[Nbin];
  for (size_t i = 0; i < m_residuals.size(); i++)
  {
    if (!m_residuals_ok[i]) continue;
    int bin = (int)floor(1./fabs(m_residuals[i][idx_momentum])/bin_width);
    if (m_residuals[i][idx_q] > 0) apos[bin].push_back(i);
    else                           aneg[bin].push_back(i);
  }
  for (int j = 0; j < Nbin; j++) std::cout << "bin " << j << ": [pos,neg] sizes before & after: ["
    << pos[j].size() <<","<< neg[j].size()<<"] -> [" << apos[j].size() <<","<< aneg[j].size() << "]" << std::endl;
  std::cout<<" N residuals "<<m_residuals.size()<<" -> "<<(size_t) std::count(m_residuals_ok.begin(), m_residuals_ok.end(), true)<<std::endl;
}
TMatrixDSym MuonResidualsFitter::correlationMatrix ( )
double MuonResidualsFitter::covarianceElement ( int  parNum1,
int  parNum2 
) [inline]

Definition at line 191 of file MuonResidualsFitter.h.

References m_cov, npar(), and parNum2parIdx().

  {
    assert(0 <= parNum1  &&  parNum1 < npar());
    assert(0 <= parNum2  &&  parNum2 < npar());
    assert(m_cov.GetNcols() == npar()); // m_cov might have not yet been resized to account for proper #parameters
    return m_cov(parNum2parIdx(parNum1),  parNum2parIdx(parNum2));
  }
TMatrixDSym MuonResidualsFitter::covarianceMatrix ( ) [inline]

Definition at line 190 of file MuonResidualsFitter.h.

References m_cov.

{return m_cov;}
bool MuonResidualsFitter::dofit ( void(*)(int &, double *, double &, double *, int)  fcn,
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 
) [protected]

Definition at line 237 of file MuonResidualsFitter.cc.

References fitWZ::arglist, alignCSCRings::e, fcn(), fixed(), i, inform(), m_cov, m_error, m_loglikelihood, m_printLevel, m_strategy, m_value, MuonResidualsFitter_TMinuit, npar(), and v.

Referenced by MuonResiduals6DOFFitter::fit(), MuonResiduals6DOFrphiFitter::fit(), MuonResiduals5DOFFitter::fit(), MuonResidualsPositionFitter::fit(), MuonResidualsBfieldAngleFitter::fit(), MuonResiduals1DOFFitter::fit(), and MuonResidualsAngleFitter::fit().

{
  MuonResidualsFitterFitInfo *fitinfo = new MuonResidualsFitterFitInfo(this);

  MuonResidualsFitter_TMinuit = new TMinuit(npar());
  MuonResidualsFitter_TMinuit->SetPrintLevel(m_printLevel);
  MuonResidualsFitter_TMinuit->SetObjectFit(fitinfo);
  MuonResidualsFitter_TMinuit->SetFCN(fcn);
  inform(MuonResidualsFitter_TMinuit);

  std::vector<int>::const_iterator iNum = parNum.begin();
  std::vector<std::string>::const_iterator iName = parName.begin();
  std::vector<double>::const_iterator istart = start.begin();
  std::vector<double>::const_iterator istep = step.begin();
  std::vector<double>::const_iterator ilow = low.begin();
  std::vector<double>::const_iterator ihigh = high.begin();
  
  //MuonResidualsFitter_TMinuit->SetPrintLevel(-1);
  
  for (; iNum != parNum.end();  ++iNum, ++iName, ++istart, ++istep, ++ilow, ++ihigh)
  {
    MuonResidualsFitter_TMinuit->DefineParameter(*iNum, iName->c_str(), *istart, *istep, *ilow, *ihigh);
    if (fixed(*iNum)) MuonResidualsFitter_TMinuit->FixParameter(*iNum);
  }

  double arglist[10];
  int ierflg;
  int smierflg; //second MIGRAD ierflg

  // chi^2 errors should be 1.0, log-likelihood should be 0.5
  for (int i = 0;  i < 10;  i++) arglist[i] = 0.;
  arglist[0] = 0.5;
  ierflg = 0;
  smierflg = 0;
  MuonResidualsFitter_TMinuit->mnexcm("SET ERR", arglist, 1, ierflg);
  if (ierflg != 0) { delete MuonResidualsFitter_TMinuit; delete fitinfo; return false; }

  // set strategy = 2 (more refined fits)
  for (int i = 0;  i < 10;  i++) arglist[i] = 0.;
  arglist[0] = m_strategy;
  ierflg = 0;
  MuonResidualsFitter_TMinuit->mnexcm("SET STR", arglist, 1, ierflg);
  if (ierflg != 0) { delete MuonResidualsFitter_TMinuit; delete fitinfo; return false; }

  bool try_again = false;

  // minimize
  for (int i = 0;  i < 10;  i++) arglist[i] = 0.;
  arglist[0] = 50000;
  ierflg = 0;
  MuonResidualsFitter_TMinuit->mnexcm("MIGRAD", arglist, 1, ierflg);
  if (ierflg != 0) try_again = true;

  // just once more, if needed (using the final Minuit parameters from the failed fit; often works)
  if (try_again)
  {
    for (int i = 0;  i < 10;  i++) arglist[i] = 0.;
    arglist[0] = 50000;
    MuonResidualsFitter_TMinuit->mnexcm("MIGRAD", arglist, 1, smierflg);
  }

  Double_t fmin, fedm, errdef;
  Int_t npari, nparx, istat;
  MuonResidualsFitter_TMinuit->mnstat(fmin, fedm, errdef, npari, nparx, istat);

  if (istat != 3)
  {
    for (int i = 0;  i < 10;  i++) arglist[i] = 0.;
    ierflg = 0;
    MuonResidualsFitter_TMinuit->mnexcm("HESSE", arglist, 0, ierflg);
  }

  // read-out the results
  m_loglikelihood = -fmin;

  m_value.clear();
  m_error.clear();
  for (int i = 0;  i < npar();  i++)
  {
    double v, e;
    MuonResidualsFitter_TMinuit->GetParameter(i, v, e);
    m_value.push_back(v);
    m_error.push_back(e);
  }
  m_cov.ResizeTo(npar(),npar());
  MuonResidualsFitter_TMinuit->mnemat( m_cov.GetMatrixArray(), npar());

  delete MuonResidualsFitter_TMinuit;
  delete fitinfo;
  if (smierflg != 0) return false;
  return true;
}
void MuonResidualsFitter::eraseNotSelectedResiduals ( )

Definition at line 711 of file MuonResidualsFitter.cc.

References prof2calltree::count, gather_cfg::cout, i, m_residuals, m_residuals_ok, and tmp.

Referenced by MuonResidualsTwoBin::eraseNotSelectedResiduals().

{
  // it should probably be faster then doing erase
  size_t n_ok = (size_t) std::count(m_residuals_ok.begin(), m_residuals_ok.end(), true);
  std::vector<double*> tmp(n_ok, 0);
  std::cout << "residuals sizes: all=" << m_residuals.size()<<" good="<<n_ok<<std::endl;
  int iok=0;
  for (size_t i = 0; i < m_residuals.size(); i++)
  {
    if (!m_residuals_ok[i]) continue;
    tmp[iok++] = m_residuals[i];
  }
  m_residuals.swap(tmp);

  std::vector<bool> tmp_ok(n_ok, true);
  m_residuals_ok.swap(tmp_ok);

  std::cout << "residuals size after eraseNotSelectedResiduals =" << m_residuals.size()<<"  ok size="<<m_residuals_ok.size()<<std::endl;
}
double MuonResidualsFitter::errorerror ( int  parNum) [inline]

Definition at line 184 of file MuonResidualsFitter.h.

References m_error, and npar().

Referenced by MuonResidualsTwoBin::errorerror(), MuonResiduals6DOFrphiFitter::plot(), MuonResiduals6DOFFitter::plot(), and MuonResiduals5DOFFitter::plot().

{ assert(0 <= parNum  &&  parNum < npar());  return m_error[parNum]; }
void MuonResidualsFitter::fill ( double *  residual) [inline]
virtual bool MuonResidualsFitter::fit ( Alignable ali) [pure virtual]
void MuonResidualsFitter::fix ( int  parNum,
bool  val = true 
) [inline]

Definition at line 153 of file MuonResidualsFitter.h.

References m_fixed, and npar().

Referenced by MuonResiduals6DOFFitter::fit(), MuonResiduals5DOFFitter::fit(), MuonResiduals6DOFrphiFitter::fit(), and MuonResidualsTwoBin::fix().

  {
    assert(0 <= parNum  &&  parNum < npar());
    if (m_fixed.size() == 0) m_fixed.resize(npar(), false);
    m_fixed[parNum] = val;
  }
bool MuonResidualsFitter::fixed ( int  parNum) [inline]

Definition at line 160 of file MuonResidualsFitter.h.

References m_fixed, and npar().

Referenced by dofit(), MuonResiduals1DOFFitter::fit(), and MuonResidualsTwoBin::fixed().

  {
    assert(0 <= parNum  &&  parNum < npar());
    if (m_fixed.size() == 0) return false;
    else return m_fixed[parNum];
  }
void MuonResidualsFitter::histogramChi2GaussianFit ( int  which,
double &  fit_mean,
double &  fit_sigma 
)

Definition at line 491 of file MuonResidualsFitter.cc.

References a, b, computeHistogramRangeAndBinning(), gather_cfg::cout, python::connectstrParser::f1, estimatePileup::hist, m_residuals, pileupCalc::nbins, and alignCSCRings::r.

Referenced by selectPeakResiduals_simple().

{
  int nbins;
  double a, b;
  computeHistogramRangeAndBinning(which, nbins, a, b); 
  if (a==b || a > b) { fit_mean = a; fit_sigma = 0; return; }

  TH1D *hist = new TH1D("htmp", "", nbins, a, b);
  for (std::vector<double*>::const_iterator r = m_residuals.begin();  r != m_residuals.end();  ++r)   hist->Fill( (*r)[which] );
  
  // do simple chi2 gaussian fit
  TF1 *f1= new TF1("f1","gaus", a, b);
  f1->SetParameter(0, hist->GetEntries());
  f1->SetParameter(1, 0);
  f1->SetParameter(2, hist->GetRMS());
  hist->Fit("f1","RQ");
  
  fit_mean  = f1->GetParameter(1);
  fit_sigma = f1->GetParameter(2);
  std::cout<<" h("<<nbins<<","<<a<<","<<b<<") mu="<<fit_mean<<" sig="<<fit_sigma<<std::endl;
  
  delete f1;
  delete hist;
}
virtual void MuonResidualsFitter::inform ( TMinuit *  tMinuit) [protected, pure virtual]
void MuonResidualsFitter::initialize_table ( ) [protected]

Definition at line 166 of file MuonResidualsFitter.cc.

References gather_cfg::cout, Exception, kPowerLawTails, MuonResidualsFitter_compute_log_convolution(), MuonResidualsFitter_gsbinsize, MuonResidualsFitter_lookup_table, MuonResidualsFitter_numgsbins, MuonResidualsFitter_numtsbins, MuonResidualsFitter_table_initialized, MuonResidualsFitter_tsbinsize, and residualsModel().

Referenced by MuonResiduals6DOFFitter::fit(), MuonResiduals5DOFFitter::fit(), MuonResiduals6DOFrphiFitter::fit(), MuonResidualsPositionFitter::fit(), MuonResidualsBfieldAngleFitter::fit(), MuonResiduals1DOFFitter::fit(), and MuonResidualsAngleFitter::fit().

{
  if (MuonResidualsFitter_table_initialized  ||  residualsModel() != kPowerLawTails) return;
  MuonResidualsFitter_table_initialized = true;

  std::ifstream convolution_table("convolution_table.txt");
  if (convolution_table.is_open())
  {
    int numgsbins = 0;
    int numtsbins = 0;
    double tsbinsize = 0.;
    double gsbinsize = 0.;

    convolution_table >> numgsbins >> numtsbins >> tsbinsize >> gsbinsize;
    if (numgsbins != MuonResidualsFitter_numgsbins  ||  numtsbins != MuonResidualsFitter_numtsbins  ||  
        tsbinsize != MuonResidualsFitter_tsbinsize  ||  gsbinsize != MuonResidualsFitter_gsbinsize)
    {
      throw cms::Exception("MuonResidualsFitter") << "convolution_table.txt has the wrong bin width/bin size.  Throw it away and let the fitter re-create the file.\n";
    }

    for (int gsbin = 0;  gsbin < MuonResidualsFitter_numgsbins;  gsbin++)
    {
      for (int tsbin = 0;  tsbin < MuonResidualsFitter_numtsbins;  tsbin++)
      {
        int read_gsbin = 0;
        int read_tsbin = 0;
        double val = 0.;

        convolution_table >> read_gsbin >> read_tsbin >> val;
        if (read_gsbin != gsbin  ||  read_tsbin != tsbin)
        {
          throw cms::Exception("MuonResidualsFitter") << "convolution_table.txt is out of order.  Throw it away and let the fitter re-create the file.\n";
        }
        MuonResidualsFitter_lookup_table[gsbin][tsbin] = val;
      }
    }
    convolution_table.close();
  }
  else
  {
    std::ofstream convolution_table2("convolution_table.txt");

    if (!convolution_table2.is_open()) throw cms::Exception("MuonResidualsFitter") << "Couldn't write to file convolution_table.txt\n";

    convolution_table2 << MuonResidualsFitter_numgsbins << " " << MuonResidualsFitter_numtsbins << " " << MuonResidualsFitter_tsbinsize << " " << MuonResidualsFitter_gsbinsize << std::endl;

    std::cout << "Initializing convolution look-up table (takes a few minutes)..." << std::endl;

    for (int gsbin = 0;  gsbin < MuonResidualsFitter_numgsbins;  gsbin++)
    {
      double gammaoversigma = double(gsbin) * MuonResidualsFitter_gsbinsize;
      std::cout << "    gsbin " << gsbin << "/" << MuonResidualsFitter_numgsbins << std::endl;
      for (int tsbin = 0;  tsbin < MuonResidualsFitter_numtsbins;  tsbin++)
      {
        double toversigma = double(tsbin) * MuonResidualsFitter_tsbinsize;

        // 1e-6 errors (out of a value of ~0.01) with max=100, step=0.001, power=4 (max=1000 does a little better with the tails)
        MuonResidualsFitter_lookup_table[gsbin][tsbin] = MuonResidualsFitter_compute_log_convolution(toversigma, gammaoversigma);

        // <10% errors with max=20, step=0.005, power=4 (faster computation for testing)
        // MuonResidualsFitter_lookup_table[gsbin][tsbin] = MuonResidualsFitter_compute_log_convolution(toversigma, gammaoversigma, 100., 0.005, 4.);

        convolution_table2 << gsbin << " " << tsbin << " " << MuonResidualsFitter_lookup_table[gsbin][tsbin] << std::endl;
      }
    }
    convolution_table2.close();
    std::cout << "Initialization done!" << std::endl;
  }
}
double MuonResidualsFitter::loglikelihood ( ) [inline]

Definition at line 200 of file MuonResidualsFitter.h.

References m_loglikelihood.

Referenced by MuonResidualsTwoBin::loglikelihood().

{ return m_loglikelihood; }
virtual int MuonResidualsFitter::ndata ( ) [pure virtual]
int MuonResidualsFitter::nfixed ( ) [inline]

Definition at line 166 of file MuonResidualsFitter.h.

References prof2calltree::count, and m_fixed.

{ return std::count(m_fixed.begin(), m_fixed.end(), true); }
virtual int MuonResidualsFitter::npar ( ) [pure virtual]
long MuonResidualsFitter::numResiduals ( ) const [inline]
long MuonResidualsFitter::numsegments ( ) [inline]

Definition at line 202 of file MuonResidualsFitter.h.

References residuals_begin(), and residuals_end().

Referenced by MuonResidualsTwoBin::numsegments().

  {
    long num = 0;
    for (std::vector<double*>::const_iterator resiter = residuals_begin();  resiter != residuals_end();  ++resiter) num++;
    return num;
  }
int MuonResidualsFitter::parNum2parIdx ( int  parNum) [inline]

Definition at line 188 of file MuonResidualsFitter.h.

References m_parNum2parIdx.

Referenced by covarianceElement().

{ return m_parNum2parIdx[parNum];}
virtual double MuonResidualsFitter::plot ( std::string  name,
TFileDirectory dir,
Alignable ali 
) [pure virtual]
void MuonResidualsFitter::plotsimple ( std::string  name,
TFileDirectory dir,
int  which,
double  multiplier 
)

Definition at line 425 of file MuonResidualsFitter.cc.

References estimatePileup::hist, TFileDirectory::make(), alignCSCRings::r, residuals_begin(), residuals_end(), and svgfig::window().

Referenced by MuonResidualsTwoBin::plotsimple().

{
  double window = 100.;
  if (which == 0) window = 2.*30.;
  else if (which == 1) window = 2.*30.;
  else if (which == 2) window = 2.*20.;
  else if (which == 3) window = 2.*50.;

  TH1F *hist = dir->make<TH1F>(name.c_str(), "", 200, -window, window);
  for (std::vector<double*>::const_iterator r = residuals_begin();  r != residuals_end();  ++r) hist->Fill(multiplier * (*r)[which]);
}
void MuonResidualsFitter::plotweighted ( std::string  name,
TFileDirectory dir,
int  which,
int  whichredchi2,
double  multiplier 
)

Definition at line 438 of file MuonResidualsFitter.cc.

References estimatePileup::hist, TFileDirectory::make(), alignCSCRings::r, residuals_begin(), residuals_end(), CommonMethods::weight(), and svgfig::window().

Referenced by MuonResidualsTwoBin::plotweighted().

{
  double window = 100.;
  if (which == 0) window = 2.*30.;
  else if (which == 1) window = 2.*30.;
  else if (which == 2) window = 2.*20.;
  else if (which == 3) window = 2.*50.;

  TH1F *hist = dir->make<TH1F>(name.c_str(), "", 200, -window, window);
  for (std::vector<double*>::const_iterator r = residuals_begin();  r != residuals_end();  ++r)
  {
    double weight = 1./(*r)[whichredchi2];
    if (TMath::Prob(1./weight*12, 12) < 0.99) hist->Fill(multiplier * (*r)[which], weight);
  }
}
void MuonResidualsFitter::read ( FILE *  file,
int  which = 0 
)

Definition at line 369 of file MuonResidualsFitter.cc.

References alignCSCRings::e, Exception, fill(), i, m_residuals, m_residuals_ok, ndata(), numResiduals(), and UserOptions_cff::rows.

Referenced by MuonResidualsTwoBin::read().

{
  long rows = -100;
  int cols = -100;
  int readwhich = -100;

  fread(&rows, sizeof(long), 1, file);
  fread(&cols, sizeof(int), 1, file);
  fread(&readwhich, sizeof(int), 1, file);

  if (cols != ndata()  ||  rows < 0  ||  readwhich != which)
  {
    throw cms::Exception("MuonResidualsFitter") << "temporary file is corrupted (which = " << which << " readwhich = " << readwhich << " rows = " << rows << " cols = " << cols << ")\n";
  }

  double *likeAChecksum = new double[cols];
  double *likeAChecksum2 = new double[cols];
  for (int i = 0;  i < cols;  i++)
  {
    likeAChecksum[i] = 0.;
    likeAChecksum2[i] = 0.;
  }

  m_residuals.reserve(rows);
  for (long row = 0;  row < rows;  row++)
  {
    double *residual = new double[cols];
    fread(residual, sizeof(double), cols, file);
    fill(residual);
    for (int i = 0;  i < cols;  i++)
    {
      if (fabs(residual[i]) > likeAChecksum[i]) likeAChecksum[i] = fabs(residual[i]);
      if (fabs(residual[i]) < likeAChecksum2[i]) likeAChecksum2[i] = fabs(residual[i]);
    }
  } // end loop over records in file

  double *readChecksum = new double[cols];
  double *readChecksum2 = new double[cols];
  fread(readChecksum, sizeof(double), cols, file);
  fread(readChecksum2, sizeof(double), cols, file);

  for (int i = 0;  i < cols;  i++)
  {
    if (fabs(likeAChecksum[i] - readChecksum[i]) > 1e-10  ||  fabs(1./likeAChecksum2[i] - 1./readChecksum2[i]) > 1e10)
    {
      throw cms::Exception("MuonResidualsFitter") << "temporary file is corrupted (which = " << which << " rows = " << rows << " likeAChecksum " << likeAChecksum[i] << " != readChecksum " << readChecksum[i] << " " << " likeAChecksum2 " << likeAChecksum2[i] << " != readChecksum2 " << readChecksum2[i] << ")\n";
    }
  }

  m_residuals_ok.resize(numResiduals(), true);

  delete [] likeAChecksum;
  delete [] likeAChecksum2;
}
std::vector<double*>::const_iterator MuonResidualsFitter::residuals_begin ( ) const [inline]
std::vector<double*>::const_iterator MuonResidualsFitter::residuals_end ( ) const [inline]
int MuonResidualsFitter::residualsModel ( ) const [inline]
std::vector<bool>& MuonResidualsFitter::selectedResidualsFlags ( ) [inline]
void MuonResidualsFitter::selectPeakResiduals ( double  nsigma,
int  nvar,
int *  vars 
)

Definition at line 559 of file MuonResidualsFitter.cc.

References prof2calltree::count, gather_cfg::cout, AlCaHLTBitMon_QueryRunRegistry::data, i, m_residuals, m_residuals_ok, numResiduals(), alignCSCRings::r, mathSSE::sqrt(), and v.

Referenced by MuonResidualsTwoBin::selectPeakResiduals().

{
  //std::cout<<"doing selectpeakresiduals: nsig="<<nsigma<<" nvar="<<nvar<<" vars=";
  for (int i=0; i<nvar; ++i) std::cout<<vars[i]<<" ";
  std::cout<<std::endl;

  // does not make sense for small statistics
  if (numResiduals()<50) return;
  
  size_t nbefore = numResiduals();
  std::cout<<" N residuals "<<nbefore<<" ~ "<<(size_t) std::count(m_residuals_ok.begin(), m_residuals_ok.end(), true)<<std::endl;
  //just to be sure (can't see why it might ever be more then 10)
  assert(nvar<=10 && nvar>0);

  std::vector<double*>::iterator r = m_residuals.begin();

  // it's awkward, but the 1D case has to be handled separately
  if (nvar==1)
  {
    // get robust estimates for the peak and sigma
    double *data = new double[nbefore];
    for (size_t i = 0; i < nbefore; i++) data[i] = m_residuals[i][ vars[0] ];
    double peak, sigma;
    TRobustEstimator re;
    re.EvaluateUni(nbefore, data, peak, sigma);

    // filter out residuals that are more then nsigma away from the peak
    while (r != m_residuals.end())
    {
      double distance = fabs( ((*r)[ vars[0] ] - peak)/sigma );
      if (distance <= nsigma)  ++r;
      else
      {
        m_residuals_ok[r - m_residuals.begin()] = false;
        ++r;
        //delete [] (*r);
        //r = m_residuals.erase(r);
      }
    }
    std::cout<<" N residuals "<<nbefore<<" -> "<<numResiduals()<<std::endl;
    return;
  } // end 1D case
  
  // initialize and run the robust estimator for D>1
  TRobustEstimator re(nbefore, nvar);
  r = m_residuals.begin();
  while (r != m_residuals.end())
  {
    double *row = new double[nvar];
    for (int v = 0; v<nvar; v++)  row[v] = (*r)[ vars[v] ];
    re.AddRow(row);
    delete[] row;
    ++r;
  }
  re.Evaluate();
  
  // get nvar-dimensional ellipsoid center & covariance
  TVectorD M(nvar);
  re.GetMean(M);
  TMatrixDSym Cov(nvar);
  re.GetCovariance(Cov);
  Cov.Invert();

  // calculate the normalized radius for this nvar-dimensional ellipsoid from a 1D-Gaussian nsigma equivalent distance
  double conf_1d = TMath::Erf(nsigma/sqrt(2));
  double surf_radius = sqrt(TMath::ChisquareQuantile(conf_1d, nvar));

  // filter out residuals that are outside of the covariance ellipsoid with the above normalized radius
  r = m_residuals.begin();
  while (r != m_residuals.end())
  {
    TVectorD res(nvar);
    for (int v = 0; v<nvar; v++)  res[v] = (*r)[ vars[v] ];
    double distance = sqrt( Cov.Similarity(res - M) );
    if (distance <= surf_radius)  ++r;
    else
    {
      m_residuals_ok[r - m_residuals.begin()] = false;
      ++r;
      //delete [] (*r);
      //r = m_residuals.erase(r);
    }
  }
  std::cout<<" N residuals "<<nbefore<<" -> "<<(size_t) std::count(m_residuals_ok.begin(), m_residuals_ok.end(), true)<<std::endl;
}
void MuonResidualsFitter::selectPeakResiduals_simple ( double  nsigma,
int  nvar,
int *  vars 
)

Definition at line 518 of file MuonResidualsFitter.cc.

References gather_cfg::cout, histogramChi2GaussianFit(), m_center, m_radii, m_residuals, numResiduals(), funct::pow(), alignCSCRings::r, and v.

{
  // does not make sense for small statistics
  if (numResiduals()<25) return;

  int nbefore = numResiduals();

  //just to be sure (can't see why it might ever be more then 10)
  assert(nvar<=10);

  // estimate nvar-D ellipsoid center & axes
  for (int v = 0; v<nvar; v++)
  {
    int which = vars[v];
    histogramChi2GaussianFit(which, m_center[which], m_radii[which]);
    m_radii[which] = nsigma * m_radii[which];
  }

  // filter out residuals that don't fit into the ellipsoid
  std::vector<double*>::iterator r = m_residuals.begin();
  while (r != m_residuals.end())
  {
    double ellipsoid_sum = 0;
    for (int v = 0; v<nvar; v++)
    {
      int which = vars[v];
      if (m_radii[which] == 0.) continue;
      ellipsoid_sum += pow( ( (*r)[which] - m_center[which]) / m_radii[which] , 2);
    }
    if (ellipsoid_sum <= 1.)  ++r;
    else
    {
      delete [] (*r);
      r = m_residuals.erase(r);
    }
  }
  std::cout<<" N residuals "<<nbefore<<" -> "<<numResiduals()<<std::endl;
}
void MuonResidualsFitter::setPrintLevel ( int  printLevel) [inline]

Definition at line 168 of file MuonResidualsFitter.h.

References m_printLevel.

Referenced by MuonResidualsTwoBin::setPrintLevel().

{ m_printLevel = printLevel; }
void MuonResidualsFitter::setStrategy ( int  strategy) [inline]

Definition at line 169 of file MuonResidualsFitter.h.

References m_strategy.

Referenced by MuonResidualsTwoBin::setStrategy().

{ m_strategy = strategy; }
virtual double MuonResidualsFitter::sumofweights ( ) [pure virtual]
virtual int MuonResidualsFitter::type ( ) const [pure virtual]
int MuonResidualsFitter::useRes ( ) const [inline]
double MuonResidualsFitter::value ( int  parNum) [inline]
void MuonResidualsFitter::write ( FILE *  file,
int  which = 0 
)

Definition at line 332 of file MuonResidualsFitter.cc.

References i, ndata(), numResiduals(), residuals_begin(), residuals_end(), and UserOptions_cff::rows.

Referenced by MuonResidualsTwoBin::write().

{
  long rows = numResiduals();
  int cols = ndata();
  int whichcopy = which;

  fwrite(&rows, sizeof(long), 1, file);
  fwrite(&cols, sizeof(int), 1, file);
  fwrite(&whichcopy, sizeof(int), 1, file);

  double *likeAChecksum = new double[cols];
  double *likeAChecksum2 = new double[cols];
  for (int i = 0;  i < cols;  i++)
  {
    likeAChecksum[i] = 0.;
    likeAChecksum2[i] = 0.;
  }

  for (std::vector<double*>::const_iterator residual = residuals_begin();  residual != residuals_end();  ++residual)
  {
    fwrite((*residual), sizeof(double), cols, file);
    for (int i = 0;  i < cols;  i++)
    {
      if (fabs((*residual)[i]) > likeAChecksum[i]) likeAChecksum[i] = fabs((*residual)[i]);
      if (fabs((*residual)[i]) < likeAChecksum2[i]) likeAChecksum2[i] = fabs((*residual)[i]);
    }
  } // end loop over residuals

  // the idea is that mal-formed doubles are likely to be huge values (or tiny values)
  // because the exponent gets screwed up; we want to check for that
  fwrite(likeAChecksum, sizeof(double), cols, file);
  fwrite(likeAChecksum2, sizeof(double), cols, file);

  delete [] likeAChecksum;
  delete [] likeAChecksum2;
}

Member Data Documentation

double MuonResidualsFitter::m_center[20] [protected]

Definition at line 271 of file MuonResidualsFitter.h.

Referenced by selectPeakResiduals_simple().

TMatrixDSym MuonResidualsFitter::m_cov [protected]

Definition at line 263 of file MuonResidualsFitter.h.

Referenced by covarianceElement(), covarianceMatrix(), and dofit().

std::vector<double> MuonResidualsFitter::m_error [protected]

Definition at line 262 of file MuonResidualsFitter.h.

Referenced by dofit(), and errorerror().

std::vector<bool> MuonResidualsFitter::m_fixed [protected]

Definition at line 255 of file MuonResidualsFitter.h.

Referenced by fix(), fixed(), and nfixed().

Definition at line 264 of file MuonResidualsFitter.h.

Referenced by dofit(), and loglikelihood().

std::map<int,int> MuonResidualsFitter::m_parNum2parIdx [protected]

Definition at line 266 of file MuonResidualsFitter.h.

Referenced by parNum2parIdx().

Definition at line 256 of file MuonResidualsFitter.h.

Referenced by dofit(), and setPrintLevel().

double MuonResidualsFitter::m_radii[20] [protected]

Definition at line 272 of file MuonResidualsFitter.h.

Referenced by selectPeakResiduals_simple().

std::vector<double*> MuonResidualsFitter::m_residuals [protected]
std::vector<bool> MuonResidualsFitter::m_residuals_ok [protected]

Definition at line 251 of file MuonResidualsFitter.h.

Referenced by MuonResidualsFitter(), and residualsModel().

Definition at line 256 of file MuonResidualsFitter.h.

Referenced by dofit(), and setStrategy().

Definition at line 253 of file MuonResidualsFitter.h.

Referenced by useRes().

std::vector<double> MuonResidualsFitter::m_value [protected]

Definition at line 261 of file MuonResidualsFitter.h.

Referenced by dofit(), and value().