CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
CSCFitAFEBThr.cc
Go to the documentation of this file.
1 #include "Minuit2/VariableMetricMinimizer.h"
2 #include "Minuit2/FunctionMinimum.h"
3 
6 
7 #include <cmath>
8 #include <vector>
9 #include <iostream>
10 
11 using namespace ROOT::Minuit2;
12 using namespace std;
13 
15  theOBJfun = new CSCThrTurnOnFcn();
16  theFitter = new VariableMetricMinimizer();
17 }
18 
20  delete theFitter;
21  delete theOBJfun;
22 }
23 
24 bool CSCFitAFEBThr::ThresholdNoise(const std::vector<float>& inputx,
25  const std::vector<float>& inputy,
26  const int& npulses,
27  std::vector<int>& dacoccup,
28  std::vector<float>& mypar,
29  std::vector<float>& ermypar,
30  float& ercorr,
31  float& chisq,
32  int& ndf,
33  int& niter,
34  float& edm) const {
35  bool status = false;
36 
37  std::vector<double> parinit(2, 0);
38  std::vector<double> erparinit(2, 0);
39 
41  parinit[0] = 30.0;
42  parinit[1] = 2.0;
43 
44  erparinit[0] = 20;
45  erparinit[1] = 0.5;
46 
48  std::vector<float> x;
49  std::vector<float> y;
50  std::vector<float> ynorm;
51  std::vector<float> ery;
52  x.clear();
53  y.clear();
54  ynorm.clear();
55  ery.clear();
56 
64 
65  int sum = 0;
66  float r;
67  for (size_t i = 0; i < inputx.size(); i++) {
68  if (inputy[i] > 0.0)
69  sum++;
70  r = inputy[i] / (float)dacoccup[i];
71  ynorm.push_back(r);
72  // std::cout<<" "<<ynorm[i];
73  }
74  // std::cout<<std::endl;
75  if (sum == 0) {
76  ndf = -4;
77  return status;
78  }
79 
80  int nbeg = inputx.size();
81  // for(size_t i=inputx.size()-1;i>=0;i--) // Wrong.
82  // Because i is unsigned, i>=0 is always true,
83  // and the loop termination condition is never reached.
84  // We offset by 1.
85  for (size_t i = inputx.size(); i > 0; i--) {
86  if (ynorm[i - 1] < 1.0)
87  nbeg--;
88  if (ynorm[i - 1] == 1.0)
89  break;
90  }
91 
92  for (size_t i = nbeg; i < inputx.size(); i++) {
93  if (ynorm[i] > 0.0) {
94  x.push_back(inputx[i]);
95  y.push_back(ynorm[i]);
96 
97  float p = inputy[i] / (float)dacoccup[i];
98  float s = (float)dacoccup[i] * p * (1.0 - p);
99  s = sqrt(s) / (float)dacoccup[i];
100  ery.push_back(s);
101  }
102  }
103 
105  ndf = x.size() - 2;
106  if (ndf <= 0)
107  return status;
108 
110  float half = 0.5;
111  float dmin = 999999.0;
112  float diff;
113  for (size_t i = 0; i < x.size(); i++) {
114  diff = y[i] - half;
115  if (diff < 0.0)
116  diff = -diff;
117  if (diff < dmin) {
118  dmin = diff;
119  parinit[0] = x[i];
120  } // par[0] from data
121  //std::cout<<i+1<<" "<<x[i]<<" "<<y[i]<<" "<<ery[i]<<std::endl;
122  }
123 
125  theOBJfun->setData(x, y);
126  theOBJfun->setErrors(ery);
127  theOBJfun->setNorm(1.0);
128 
129  // for(size_t int i=0;i<x.size();i++) std::cout<<" "<<x[i]<<" "<<y[i]
130  // <<" "<<ery[i]<<std::endl;
131 
133  FunctionMinimum fmin = theFitter->Minimize(*theOBJfun, parinit, erparinit, 1, 500, 0.1);
134 
135  status = fmin.IsValid();
136 
137  if (status) {
138  mypar[0] = (float)fmin.Parameters().Vec()(0);
139  mypar[1] = (float)fmin.Parameters().Vec()(1);
140  ermypar[0] = (float)sqrt(fmin.Error().Matrix()(0, 0));
141  ermypar[1] = (float)sqrt(fmin.Error().Matrix()(1, 1));
142  ercorr = 0;
143  if (ermypar[0] != 0.0 && ermypar[1] != 0.0)
144  ercorr = (float)fmin.Error().Matrix()(0, 1) / (ermypar[0] * ermypar[1]);
145 
146  chisq = fmin.Fval();
147  ndf = y.size() - mypar.size();
148  niter = fmin.NFcn();
149  edm = fmin.Edm();
150  } else
151  ndf = -3;
152  return status;
153 }
void setNorm(float n)
Set the norm (if needed)
ModularFunctionMinimizer * theFitter
Definition: CSCFitAFEBThr.h:36
list status
Definition: mps_update.py:107
virtual bool ThresholdNoise(const std::vector< float > &inputx, const std::vector< float > &inputy, const int &npulses, std::vector< int > &dacoccup, std::vector< float > &mypar, std::vector< float > &ermypar, float &ercorr, float &chisq, int &ndf, int &niter, float &edm) const
void setData(const std::vector< float > &x, const std::vector< float > &y)
Cache the current data, x and y.
T sqrt(T t)
Definition: SSEVec.h:19
CSCThrTurnOnFcn * theOBJfun
Definition: CSCFitAFEBThr.h:37
virtual ~CSCFitAFEBThr()
void setErrors(const std::vector< float > &er)
Set the errors.