CMS 3D CMS Logo

MuonResidualsTwoBin.h
Go to the documentation of this file.
1 #ifndef Alignment_MuonAlignmentAlgorithms_MuonResidualsTwoBin_H
2 #define Alignment_MuonAlignmentAlgorithms_MuonResidualsTwoBin_H
3 
13 #include "TMath.h"
14 
16 public:
18  : m_twoBin(twoBin), m_pos(pos), m_neg(neg){};
20  if (m_pos != nullptr)
21  delete m_pos;
22  if (m_neg != nullptr)
23  delete m_neg;
24  };
25 
26  int residualsModel() const {
28  return m_pos->residualsModel();
29  };
30  long numResidualsPos() const { return m_pos->numResiduals(); };
31  long numResidualsNeg() const { return m_neg->numResiduals(); };
32  int npar() {
33  assert(m_pos->npar() == m_neg->npar());
34  return m_pos->npar();
35  };
36  int ndata() {
37  assert(m_pos->ndata() == m_neg->ndata());
38  return m_pos->ndata();
39  };
40  int type() const {
41  assert(m_pos->type() == m_neg->type());
42  return m_pos->type();
43  };
44  int useRes() const { return m_pos->useRes(); };
45 
46  void fix(int parNum, bool value = true) {
47  m_pos->fix(parNum, value);
48  m_neg->fix(parNum, value);
49  };
50 
51  bool fixed(int parNum) { return m_pos->fixed(parNum) && m_neg->fixed(parNum); };
52 
53  void setPrintLevel(int printLevel) const {
54  m_pos->setPrintLevel(printLevel);
55  m_neg->setPrintLevel(printLevel);
56  }
57 
58  void setStrategy(int strategy) const {
59  m_pos->setStrategy(strategy);
60  m_neg->setStrategy(strategy);
61  }
62 
63  void fill(char charge, double *residual) {
64  if (!m_twoBin || charge > 0)
65  m_pos->fill(residual);
66  else
67  m_neg->fill(residual);
68  };
69 
70  bool fit(Alignable *ali) { return (m_twoBin ? (m_pos->fit(ali) && m_neg->fit(ali)) : m_pos->fit(ali)); };
71  double value(int parNum) {
72  return (m_twoBin ? ((m_pos->value(parNum) + m_neg->value(parNum)) / 2.) : m_pos->value(parNum));
73  };
74  double errorerror(int parNum) {
75  return (m_twoBin ? (sqrt(pow(m_pos->errorerror(parNum), 2.) + pow(m_neg->errorerror(parNum), 2.)) / 2.)
76  : m_pos->errorerror(parNum));
77  };
78  double antisym(int parNum) { return (m_twoBin ? ((m_pos->value(parNum) - m_neg->value(parNum)) / 2.) : 0.); };
79  double loglikelihood() {
81  };
82  double numsegments() { return (m_twoBin ? (m_pos->numsegments() + m_neg->numsegments()) : m_pos->numsegments()); };
83  double sumofweights() {
85  };
86 
87  // demonstration plots
89  if (m_twoBin) {
90  std::string namePos = name + std::string("Pos");
91  std::string nameNeg = name + std::string("Neg");
92  double output = 0.;
93  output += m_pos->plot(namePos, dir, ali);
94  output += m_neg->plot(nameNeg, dir, ali);
95  return output;
96  } else {
97  return m_pos->plot(name, dir, ali);
98  }
99  };
100 
101  // I/O of temporary files for collect mode
102  void write(FILE *file, int which = 0) {
103  if (m_twoBin) {
104  m_pos->write(file, 2 * which);
105  m_neg->write(file, 2 * which + 1);
106  } else {
107  m_pos->write(file, which);
108  }
109  };
110  void read(FILE *file, int which = 0) {
111  if (m_twoBin) {
112  m_pos->read(file, 2 * which);
113  m_neg->read(file, 2 * which + 1);
114  } else {
115  m_pos->read(file, which);
116  }
117  };
118 
119  double median(int which) {
120  std::vector<double> residuals;
121  for (std::vector<double *>::const_iterator r = residualsPos_begin(); r != residualsPos_end(); ++r) {
122  residuals.push_back((*r)[which]);
123  }
124  if (m_twoBin) {
125  for (std::vector<double *>::const_iterator r = residualsNeg_begin(); r != residualsNeg_end(); ++r) {
126  residuals.push_back((*r)[which]);
127  }
128  }
129  std::sort(residuals.begin(), residuals.end());
130  int length = residuals.size();
131  return residuals[length / 2];
132  };
133 
134  double mean(int which, double truncate) {
135  double sum = 0.;
136  double n = 0.;
137  for (std::vector<double *>::const_iterator r = residualsPos_begin(); r != residualsPos_end(); ++r) {
138  double value = (*r)[which];
139  if (fabs(value) < truncate) {
140  sum += value;
141  n += 1.;
142  }
143  }
144  if (m_twoBin) {
145  for (std::vector<double *>::const_iterator r = residualsNeg_begin(); r != residualsNeg_end(); ++r) {
146  double value = (*r)[which];
147  if (fabs(value) < truncate) {
148  sum += value;
149  n += 1.;
150  }
151  }
152  }
153  return sum / n;
154  };
155 
156  double wmean(int which, int whichredchi2, double truncate) {
157  double sum = 0.;
158  double n = 0.;
159  for (std::vector<double *>::const_iterator r = residualsPos_begin(); r != residualsPos_end(); ++r) {
160  double value = (*r)[which];
161  if (fabs(value) < truncate) {
162  double weight = 1. / (*r)[whichredchi2];
163  if (TMath::Prob(1. / weight * 12, 12) < 0.99) {
164  sum += weight * value;
165  n += weight;
166  }
167  }
168  }
169  if (m_twoBin) {
170  for (std::vector<double *>::const_iterator r = residualsNeg_begin(); r != residualsNeg_end(); ++r) {
171  double value = (*r)[which];
172  if (fabs(value) < truncate) {
173  double weight = 1. / (*r)[whichredchi2];
174  if (TMath::Prob(1. / weight * 12, 12) < 0.99) {
175  sum += weight * value;
176  n += weight;
177  }
178  }
179  }
180  }
181  return sum / n;
182  };
183 
184  double stdev(int which, double truncate) {
185  double sum2 = 0.;
186  double sum = 0.;
187  double n = 0.;
188  for (std::vector<double *>::const_iterator r = residualsPos_begin(); r != residualsPos_end(); ++r) {
189  double value = (*r)[which];
190  if (fabs(value) < truncate) {
191  sum2 += value * value;
192  sum += value;
193  n += 1.;
194  }
195  }
196  if (m_twoBin) {
197  for (std::vector<double *>::const_iterator r = residualsNeg_begin(); r != residualsNeg_end(); ++r) {
198  double value = (*r)[which];
199  if (fabs(value) < truncate) {
200  sum2 += value * value;
201  sum += value;
202  n += 1.;
203  }
204  }
205  }
206  return sqrt(sum2 / n - pow(sum / n, 2));
207  };
208 
209  void plotsimple(std::string name, TFileDirectory *dir, int which, double multiplier) {
210  if (m_twoBin) {
211  std::string namePos = name + std::string("Pos");
212  std::string nameNeg = name + std::string("Neg");
213  m_pos->plotsimple(namePos, dir, which, multiplier);
214  m_neg->plotsimple(nameNeg, dir, which, multiplier);
215  } else {
216  m_pos->plotsimple(name, dir, which, multiplier);
217  }
218  };
219 
220  void plotweighted(std::string name, TFileDirectory *dir, int which, int whichredchi2, double multiplier) {
221  if (m_twoBin) {
222  std::string namePos = name + std::string("Pos");
223  std::string nameNeg = name + std::string("Neg");
224  m_pos->plotweighted(namePos, dir, which, whichredchi2, multiplier);
225  m_neg->plotweighted(nameNeg, dir, which, whichredchi2, multiplier);
226  } else {
227  m_pos->plotweighted(name, dir, which, whichredchi2, multiplier);
228  }
229  };
230 
231  void selectPeakResiduals(double nsigma, int nvar, int *vars) {
232  if (m_twoBin) {
233  m_pos->selectPeakResiduals(nsigma, nvar, vars);
234  m_neg->selectPeakResiduals(nsigma, nvar, vars);
235  } else {
236  m_pos->selectPeakResiduals(nsigma, nvar, vars);
237  }
238  }
239 
240  void correctBField() {
241  m_pos->correctBField();
242  //if (m_twoBin) m_neg->correctBField();
243  };
244 
246 
248  if (m_twoBin) {
251  } else {
253  }
254  }
255 
256  std::vector<double *>::const_iterator residualsPos_begin() const { return m_pos->residuals_begin(); };
257  std::vector<double *>::const_iterator residualsPos_end() const { return m_pos->residuals_end(); };
258  std::vector<double *>::const_iterator residualsNeg_begin() const { return m_neg->residuals_begin(); };
259  std::vector<double *>::const_iterator residualsNeg_end() const { return m_neg->residuals_end(); };
260 
261  std::vector<bool>::const_iterator residualsPos_ok_begin() const { return m_pos->selectedResidualsFlags().begin(); };
262  std::vector<bool>::const_iterator residualsPos_ok_end() const { return m_pos->selectedResidualsFlags().end(); };
263  std::vector<bool>::const_iterator residualsNeg_ok_begin() const { return m_neg->selectedResidualsFlags().begin(); };
264  std::vector<bool>::const_iterator residualsNeg_ok_end() const { return m_neg->selectedResidualsFlags().end(); };
265 
266 protected:
267  bool m_twoBin;
269 };
270 
271 #endif // Alignment_MuonAlignmentAlgorithms_MuonResidualsTwoBin_H
double value(int parNum)
double wmean(int which, int whichredchi2, double truncate)
MuonResidualsFitter * m_neg
MuonResidualsFitter * m_pos
void selectPeakResiduals(double nsigma, int nvar, int *vars)
void fill(char charge, double *residual)
long numResidualsNeg() const
void write(FILE *file, int which=0)
virtual int npar()=0
void read(FILE *file, int which=0)
double stdev(int which, double truncate)
std::vector< double * >::const_iterator residuals_begin() const
virtual bool fit(Alignable *ali)=0
void fill(double *residual)
Definition: weight.py:1
constexpr int pow(int x)
Definition: conifer.h:24
double value(int parNum)
tuple twoBin
Definition: align_cfg.py:29
std::vector< bool >::const_iterator residualsPos_ok_begin() const
std::vector< bool >::const_iterator residualsNeg_ok_end() const
std::vector< double * >::const_iterator residuals_end() const
assert(be >=bs)
std::vector< double * >::const_iterator residualsNeg_begin() const
void setStrategy(int strategy) const
T sqrt(T t)
Definition: SSEVec.h:19
void plotweighted(std::string name, TFileDirectory *dir, int which, int whichredchi2, double multiplier)
void fix(int parNum, bool dofix=true)
void write(FILE *file, int which=0)
double mean(int which, double truncate)
Definition: value.py:1
virtual void correctBField()=0
bool fit(Alignable *ali)
virtual double plot(std::string name, TFileDirectory *dir, Alignable *ali)=0
std::vector< bool >::const_iterator residualsPos_ok_end() const
double antisym(int parNum)
MuonResidualsTwoBin(bool twoBin, MuonResidualsFitter *pos, MuonResidualsFitter *neg)
void plotsimple(std::string name, TFileDirectory *dir, int which, double multiplier)
virtual int ndata()=0
void setStrategy(int strategy)
virtual double sumofweights()=0
std::vector< double * >::const_iterator residualsNeg_end() const
std::vector< double * >::const_iterator residualsPos_begin() const
double errorerror(int parNum)
void read(FILE *file, int which=0)
void fiducialCuts(double xMin=-80.0, double xMax=80.0, double yMin=-80.0, double yMax=80.0, bool fidcut1=false)
int useRes(int pattern=-1)
std::vector< bool > & selectedResidualsFlags()
void setPrintLevel(int printLevel) const
long numResidualsPos() const
std::vector< double * >::const_iterator residualsPos_end() const
void plotweighted(std::string name, TFileDirectory *dir, int which, int whichredchi2, double multiplier)
double errorerror(int parNum)
std::vector< bool >::const_iterator residualsNeg_ok_begin() const
void plotsimple(std::string name, TFileDirectory *dir, int which, double multiplier)
def which(cmd)
Definition: eostools.py:336
vars
Definition: DeepTauId.cc:166
void setPrintLevel(int printLevel)
void selectPeakResiduals(double nsigma, int nvar, int *vars)
virtual int type() const =0
double plot(std::string name, TFileDirectory *dir, Alignable *ali)
void fix(int parNum, bool value=true)
bool fixed(int parNum)
double median(int which)