CMS 3D CMS Logo

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.

Public Types

enum  { kPureGaussian, kPowerLawTails, kROOTVoigt, kGaussPowerTails }
enum  {
  k1DOF, k5DOF, k6DOF, k6DOFrphi,
  kPositionFitter, kAngleFitter, kAngleBfieldFitter
}

Public Member Functions

double errorerror (int parNum)
void fill (double *residual)
virtual bool fit (Alignable *ali)=0
void fix (int parNum, bool value=true)
bool fixed (int parNum)
double loglikelihood ()
 MuonResidualsFitter (int residualsModel, int minHits, bool weightAlignment=true)
virtual int ndata ()=0
virtual int npar ()=0
long numResiduals () const
long numsegments ()
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
void setPrintLevel (int printLevel)
void setStrategy (int strategy)
virtual double sumofweights ()=0
virtual int type () const =0
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

std::vector< double > m_error
std::vector< bool > m_fixed
double m_loglikelihood
int m_minHits
int m_printLevel
std::vector< double * > m_residuals
int m_residualsModel
int m_strategy
std::vector< double > m_value
bool m_weightAlignment

Detailed Description

Date:
2010/03/12 22:25:05
Revision:
1.15
Author:
J. Pivarski - Texas A&M University <pivarski@physics.tamu.edu>

Definition at line 27 of file MuonResidualsFitter.h.


Member Enumeration Documentation

anonymous enum
Enumerator:
kPureGaussian 
kPowerLawTails 
kROOTVoigt 
kGaussPowerTails 

Definition at line 29 of file MuonResidualsFitter.h.

anonymous enum
Enumerator:
k1DOF 
k5DOF 
k6DOF 
k6DOFrphi 
kPositionFitter 
kAngleFitter 
kAngleBfieldFitter 

Definition at line 36 of file MuonResidualsFitter.h.


Constructor & Destructor Documentation

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

Definition at line 51 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

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 192 of file MuonResidualsFitter.cc.

References fitWZ::arglist, ExpressReco_HICollisions_FallBack::e, fcn(), fixed(), i, inform(), 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();

  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) {
    // minimize
    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);
  }

  delete MuonResidualsFitter_TMinuit;
  delete fitinfo;
  if (smierflg != 0) return false;
  return true;
}
double MuonResidualsFitter::errorerror ( int  parNum) [inline]

Definition at line 93 of file MuonResidualsFitter.h.

References m_error, and npar().

Referenced by MuonResidualsTwoBin::errorerror().

{ assert(0 <= parNum  &&  parNum < npar());  return m_error[parNum]; };
void MuonResidualsFitter::fill ( double *  residual) [inline]

Definition at line 85 of file MuonResidualsFitter.h.

References m_residuals.

Referenced by MuonResidualsTwoBin::fill(), and read().

                              {
    m_residuals.push_back(residual);
  };
virtual bool MuonResidualsFitter::fit ( Alignable ali) [pure virtual]
void MuonResidualsFitter::fix ( int  parNum,
bool  value = true 
) [inline]

Definition at line 64 of file MuonResidualsFitter.h.

References i, m_fixed, npar(), and value().

Referenced by MuonResidualsTwoBin::fix().

                                        {
    assert(0 <= parNum  &&  parNum < npar());
    if (m_fixed.size() == 0) {
      for (int i = 0;  i < npar();  i++) {
        m_fixed.push_back(false);
      }
    }
    m_fixed[parNum] = value;
  };
bool MuonResidualsFitter::fixed ( int  parNum) [inline]

Definition at line 74 of file MuonResidualsFitter.h.

References m_fixed, and npar().

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

                         {
    assert(0 <= parNum  &&  parNum < npar());
    if (m_fixed.size() == 0) return false;
    else return m_fixed[parNum];
  };
virtual void MuonResidualsFitter::inform ( TMinuit *  tMinuit) [protected, pure virtual]
void MuonResidualsFitter::initialize_table ( ) [protected]

Definition at line 120 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, residualsModel(), and value().

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." << std::endl;
    }

    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 value = 0.;

        convolution_table >> read_gsbin >> read_tsbin >> value;
        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." << std::endl;
        }

        MuonResidualsFitter_lookup_table[gsbin][tsbin] = value;
      }
    }

    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" << std::endl;
    }

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

    edm::LogWarning("MuonResidualsFitter") << "Initializing convolution look-up table (takes a few minutes)..." << 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();

    edm::LogWarning("MuonResidualsFitter") << "Initialization done!" << std::endl;
    std::cout << "Initialization done!" << std::endl;
  }
}
double MuonResidualsFitter::loglikelihood ( ) [inline]

Definition at line 94 of file MuonResidualsFitter.h.

References m_loglikelihood.

Referenced by MuonResidualsTwoBin::loglikelihood().

{ return m_loglikelihood; };
virtual int MuonResidualsFitter::ndata ( ) [pure virtual]
virtual int MuonResidualsFitter::npar ( ) [pure virtual]
long MuonResidualsFitter::numResiduals ( ) const [inline]
long MuonResidualsFitter::numsegments ( ) [inline]

Definition at line 95 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;
  };
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 355 of file MuonResidualsFitter.cc.

References estimatePileup::hist, TFileDirectory::make(), csvReporter::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 369 of file MuonResidualsFitter.cc.

References estimatePileup::hist, TFileDirectory::make(), csvReporter::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 311 of file MuonResidualsFitter.cc.

References ExpressReco_HICollisions_FallBack::e, Exception, fill(), i, ndata(), and tablePrinter::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 << ")" << std::endl;

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

  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] << ")" << std::endl;
    }
  }

  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]
void MuonResidualsFitter::setPrintLevel ( int  printLevel) [inline]

Definition at line 80 of file MuonResidualsFitter.h.

References m_printLevel.

Referenced by MuonResidualsTwoBin::setPrintLevel().

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

Definition at line 81 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]
double MuonResidualsFitter::value ( int  parNum) [inline]
void MuonResidualsFitter::write ( FILE *  file,
int  which = 0 
)

Definition at line 277 of file MuonResidualsFitter.cc.

References i, ndata(), numResiduals(), residuals_begin(), residuals_end(), and tablePrinter::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

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

Definition at line 132 of file MuonResidualsFitter.h.

Referenced by dofit(), and errorerror().

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

Definition at line 126 of file MuonResidualsFitter.h.

Referenced by fix(), and fixed().

Definition at line 133 of file MuonResidualsFitter.h.

Referenced by dofit(), and loglikelihood().

Definition at line 127 of file MuonResidualsFitter.h.

Referenced by dofit(), and setPrintLevel().

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

Definition at line 129 of file MuonResidualsFitter.h.

Referenced by fill(), numResiduals(), residuals_begin(), and residuals_end().

Definition at line 123 of file MuonResidualsFitter.h.

Referenced by MuonResidualsFitter(), and residualsModel().

Definition at line 127 of file MuonResidualsFitter.h.

Referenced by dofit(), and setStrategy().

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

Definition at line 131 of file MuonResidualsFitter.h.

Referenced by dofit(), and value().