CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/src/Alignment/MuonAlignmentAlgorithms/interface/MuonResidualsTwoBin.h

Go to the documentation of this file.
00001 #ifndef Alignment_MuonAlignmentAlgorithms_MuonResidualsTwoBin_H
00002 #define Alignment_MuonAlignmentAlgorithms_MuonResidualsTwoBin_H
00003 
00010 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00011 #include "Alignment/MuonAlignmentAlgorithms/interface/MuonResidualsFitter.h"
00012 #include "Alignment/CommonAlignment/interface/Alignable.h"
00013 #include "TMath.h"
00014 
00015 class MuonResidualsTwoBin {
00016 public:
00017   MuonResidualsTwoBin(bool twoBin, MuonResidualsFitter *pos, MuonResidualsFitter *neg): m_twoBin(twoBin), m_pos(pos), m_neg(neg) {};
00018   ~MuonResidualsTwoBin() {
00019     if (m_pos != NULL) delete m_pos;
00020     if (m_neg != NULL) delete m_neg;
00021   };
00022 
00023   int residualsModel() const { assert(m_pos->residualsModel() == m_neg->residualsModel());  return m_pos->residualsModel(); };
00024   long numResidualsPos() const { return m_pos->numResiduals(); };
00025   long numResidualsNeg() const { return m_neg->numResiduals(); };
00026   int npar() { assert(m_pos->npar() == m_neg->npar());  return m_pos->npar(); };
00027   int ndata() { assert(m_pos->ndata() == m_neg->ndata());  return m_pos->ndata(); };
00028   int type() const { assert(m_pos->type() == m_neg->type());  return m_pos->type(); };
00029 
00030   void fix(int parNum, bool value=true) {
00031     m_pos->fix(parNum, value);
00032     m_neg->fix(parNum, value);
00033   };
00034 
00035   bool fixed(int parNum) {
00036     return m_pos->fixed(parNum)  &&  m_neg->fixed(parNum);
00037   };
00038 
00039   void setPrintLevel(int printLevel) const {
00040     m_pos->setPrintLevel(printLevel);
00041     m_neg->setPrintLevel(printLevel);
00042   }
00043 
00044   void setStrategy(int strategy) const {
00045     m_pos->setStrategy(strategy);
00046     m_neg->setStrategy(strategy);
00047   }
00048 
00049   void fill(char charge, double *residual) {
00050     if (!m_twoBin  ||  charge > 0) m_pos->fill(residual);
00051     else m_neg->fill(residual);
00052   };
00053 
00054   bool fit(Alignable *ali) {
00055     return (m_twoBin ? (m_pos->fit(ali)  &&  m_neg->fit(ali)) : m_pos->fit(ali));
00056   };
00057   double value(int parNum) {
00058     return (m_twoBin ? ((m_pos->value(parNum) + m_neg->value(parNum)) / 2.) : m_pos->value(parNum));
00059   };
00060   double errorerror(int parNum) {
00061     return (m_twoBin ? (sqrt(pow(m_pos->errorerror(parNum), 2.) + pow(m_neg->errorerror(parNum), 2.)) / 2.) : m_pos->errorerror(parNum));
00062   };
00063   double antisym(int parNum) {
00064     return (m_twoBin ? ((m_pos->value(parNum) - m_neg->value(parNum)) / 2.) : 0.);
00065   };
00066   double loglikelihood() {
00067     return (m_twoBin ? (m_pos->loglikelihood() + m_neg->loglikelihood()) : m_pos->loglikelihood());
00068   };
00069   double numsegments() {
00070     return (m_twoBin ? (m_pos->numsegments() + m_neg->numsegments()) : m_pos->numsegments());
00071   };
00072   double sumofweights() {
00073     return (m_twoBin ? (m_pos->sumofweights() + m_neg->sumofweights()) : m_pos->sumofweights());
00074   };
00075 
00076   // demonstration plots
00077   double plot(std::string name, TFileDirectory *dir, Alignable *ali) {
00078     if (m_twoBin) {
00079       std::string namePos = name + std::string("Pos");
00080       std::string nameNeg = name + std::string("Neg");
00081       double output = 0.;
00082       output += m_pos->plot(namePos, dir, ali);
00083       output += m_neg->plot(nameNeg, dir, ali);
00084       return output;
00085     }
00086     else {
00087       return m_pos->plot(name, dir, ali);
00088     }
00089   };
00090 
00091   // I/O of temporary files for collect mode
00092   void write(FILE *file, int which=0) {
00093     if (m_twoBin) {
00094       m_pos->write(file, 2*which);
00095       m_neg->write(file, 2*which + 1);
00096     }
00097     else {
00098       m_pos->write(file, which);
00099     }
00100   };
00101   void read(FILE *file, int which=0) {
00102     if (m_twoBin) {
00103       m_pos->read(file, 2*which);
00104       m_neg->read(file, 2*which + 1);
00105     }
00106     else {
00107       m_pos->read(file, which);
00108     }
00109   };
00110 
00111   double median(int which) {
00112     std::vector<double> residuals;
00113     for (std::vector<double*>::const_iterator r = residualsPos_begin();  r != residualsPos_end();  ++r) {
00114       residuals.push_back((*r)[which]);
00115     }
00116     if (m_twoBin) {
00117       for (std::vector<double*>::const_iterator r = residualsNeg_begin();  r != residualsNeg_end();  ++r) {
00118         residuals.push_back((*r)[which]);
00119       }
00120     }
00121     std::sort(residuals.begin(), residuals.end());
00122     int length = residuals.size();
00123     return residuals[length/2];
00124   };
00125 
00126   double mean(int which, double truncate) {
00127     double sum = 0.;
00128     double n = 0.;
00129     for (std::vector<double*>::const_iterator r = residualsPos_begin();  r != residualsPos_end();  ++r) {
00130       double value = (*r)[which];
00131       if (fabs(value) < truncate) {
00132         sum += value;
00133         n += 1.;
00134       }
00135     }
00136     if (m_twoBin) {
00137       for (std::vector<double*>::const_iterator r = residualsNeg_begin();  r != residualsNeg_end();  ++r) {
00138         double value = (*r)[which];
00139         if (fabs(value) < truncate) {
00140           sum += value;
00141           n += 1.;
00142         }
00143       }
00144     }
00145     return sum/n;
00146   };
00147 
00148   double wmean(int which, int whichredchi2, double truncate) {
00149     double sum = 0.;
00150     double n = 0.;
00151     for (std::vector<double*>::const_iterator r = residualsPos_begin();  r != residualsPos_end();  ++r) {
00152       double value = (*r)[which];
00153       if (fabs(value) < truncate) {
00154         double weight = 1./(*r)[whichredchi2];
00155         if (TMath::Prob(1./weight*12, 12) < 0.99) {
00156           sum += weight*value;
00157           n += weight;
00158         }
00159       }
00160     }
00161     if (m_twoBin) {
00162       for (std::vector<double*>::const_iterator r = residualsNeg_begin();  r != residualsNeg_end();  ++r) {
00163         double value = (*r)[which];
00164         if (fabs(value) < truncate) {
00165           double weight = 1./(*r)[whichredchi2];
00166           if (TMath::Prob(1./weight*12, 12) < 0.99) {
00167             sum += weight*value;
00168             n += weight;
00169           }
00170         }
00171       }
00172     }
00173     return sum/n;
00174   };
00175 
00176   double stdev(int which, double truncate) {
00177     double sum2 = 0.;
00178     double sum = 0.;
00179     double n = 0.;
00180     for (std::vector<double*>::const_iterator r = residualsPos_begin();  r != residualsPos_end();  ++r) {
00181       double value = (*r)[which];
00182       if (fabs(value) < truncate) {
00183         sum2 += value*value;
00184         sum += value;
00185         n += 1.;
00186       }
00187     }
00188     if (m_twoBin) {
00189       for (std::vector<double*>::const_iterator r = residualsNeg_begin();  r != residualsNeg_end();  ++r) {
00190         double value = (*r)[which];
00191         if (fabs(value) < truncate) {
00192           sum2 += value*value;
00193           sum += value;
00194           n += 1.;
00195         }
00196       }
00197     }
00198     return sqrt(sum2/n - pow(sum/n, 2));
00199   };
00200 
00201   void plotsimple(std::string name, TFileDirectory *dir, int which, double multiplier) {
00202     if (m_twoBin) {
00203       std::string namePos = name + std::string("Pos");
00204       std::string nameNeg = name + std::string("Neg");
00205       m_pos->plotsimple(namePos, dir, which, multiplier);
00206       m_neg->plotsimple(nameNeg, dir, which, multiplier);
00207     }
00208     else {
00209       m_pos->plotsimple(name, dir, which, multiplier);
00210     }
00211   };
00212 
00213   void plotweighted(std::string name, TFileDirectory *dir, int which, int whichredchi2, double multiplier) {
00214     if (m_twoBin) {
00215       std::string namePos = name + std::string("Pos");
00216       std::string nameNeg = name + std::string("Neg");
00217       m_pos->plotweighted(namePos, dir, which, whichredchi2, multiplier);
00218       m_neg->plotweighted(nameNeg, dir, which, whichredchi2, multiplier);
00219     }
00220     else {
00221       m_pos->plotweighted(name, dir, which, whichredchi2, multiplier);
00222     }
00223   };
00224 
00225   std::vector<double*>::const_iterator residualsPos_begin() const { return m_pos->residuals_begin(); };
00226   std::vector<double*>::const_iterator residualsPos_end() const { return m_pos->residuals_end(); };
00227   std::vector<double*>::const_iterator residualsNeg_begin() const { return m_neg->residuals_begin(); };
00228   std::vector<double*>::const_iterator residualsNeg_end() const { return m_neg->residuals_end(); };
00229 
00230 protected:
00231   bool m_twoBin;
00232   MuonResidualsFitter *m_pos, *m_neg;
00233 };
00234 
00235 #endif // Alignment_MuonAlignmentAlgorithms_MuonResidualsTwoBin_H