CMS 3D CMS Logo

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
35  ) const {
36  bool status = false;
37 
38  std::vector<double> parinit(2,0);
39  std::vector<double> erparinit(2,0);
40 
42  parinit[0] = 30.0;
43  parinit[1] = 2.0;
44 
45  erparinit[0] = 20;
46  erparinit[1] = 0.5;
47 
49  std::vector<float> x;
50  std::vector<float> y;
51  std::vector<float> ynorm;
52  std::vector<float> ery;
53  x.clear();
54  y.clear();
55  ynorm.clear();
56  ery.clear();
57 
65 
66  int sum=0;
67  float r;
68  for(size_t i=0;i<inputx.size();i++) {
69  if(inputy[i]>0.0) 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 
81  int nbeg=inputx.size();
82  // for(size_t i=inputx.size()-1;i>=0;i--) // Wrong.
83  // Because i is unsigned, i>=0 is always true,
84  // and the loop termination condition is never reached.
85  // We offset by 1.
86  for(size_t i=inputx.size();i>0;i--) {
87  if(ynorm[i-1]<1.0) nbeg--;
88  if(ynorm[i-1]==1.0) break;
89  }
90 
91  for(size_t i=nbeg;i<inputx.size();i++) {
92  if(ynorm[i]>0.0) {
93  x.push_back(inputx[i]);
94  y.push_back(ynorm[i]);
95 
96  float p=inputy[i]/(float)dacoccup[i];
97  float s=(float)dacoccup[i] * p * (1.0-p);
98  s=sqrt(s)/(float)dacoccup[i];
99  ery.push_back(s);
100  }
101  }
102 
104  ndf=x.size()-2;
105  if(ndf <=0) return status;
106 
108  float half=0.5;
109  float dmin=999999.0;
110  float diff;
111  for(size_t i=0;i<x.size();i++) {
112  diff=y[i]-half; if(diff<0.0) diff=-diff;
113  if(diff<dmin) {dmin=diff; parinit[0]=x[i];} // par[0] from data
114  //std::cout<<i+1<<" "<<x[i]<<" "<<y[i]<<" "<<ery[i]<<std::endl;
115  }
116 
118  theOBJfun->setData(x,y);
119  theOBJfun->setErrors(ery);
120  theOBJfun->setNorm(1.0);
121 
122  // for(size_t int i=0;i<x.size();i++) std::cout<<" "<<x[i]<<" "<<y[i]
123  // <<" "<<ery[i]<<std::endl;
124 
126  FunctionMinimum fmin=theFitter->Minimize(*theOBJfun,parinit,erparinit,1,500,0.1);
127 
128  status = fmin.IsValid();
129 
130  if(status) {
131  mypar[0]=(float)fmin.Parameters().Vec()(0);
132  mypar[1]=(float)fmin.Parameters().Vec()(1);
133  ermypar[0]=(float)sqrt( fmin.Error().Matrix()(0,0) );
134  ermypar[1]=(float)sqrt( fmin.Error().Matrix()(1,1) );
135  ercorr=0;
136  if(ermypar[0] !=0.0 && ermypar[1]!=0.0)
137  ercorr=(float)fmin.Error().Matrix()(0,1)/(ermypar[0]*ermypar[1]);
138 
139  chisq = fmin.Fval();
140  ndf=y.size()-mypar.size();
141  niter=fmin.NFcn();
142  edm=fmin.Edm();
143  }
144  else ndf=-3;
145  return status;
146 }
void setNorm(float n)
Set the norm (if needed)
ModularFunctionMinimizer * theFitter
Definition: CSCFitAFEBThr.h:37
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:18
CSCThrTurnOnFcn * theOBJfun
Definition: CSCFitAFEBThr.h:38
virtual ~CSCFitAFEBThr()
void setErrors(const std::vector< float > &er)
Set the errors.
HLT enums.