CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
SymmetryFit.cc
Go to the documentation of this file.
2 #include <cmath>
3 #include <cassert>
4 #include "boost/foreach.hpp"
5 
6 TH1* SymmetryFit::symmetryChi2(std::string basename, const std::vector<TH1*>& candidates, const std::pair<unsigned,unsigned> range)
7 {
8  TH1* fake = (TH1*)(candidates[0]->Clone(basename.c_str()));
9  fake->Reset();
10  SymmetryFit combined(fake,range);
11  delete fake;
12 
13  BOOST_FOREACH(const TH1* candidate, candidates) {
14  SymmetryFit sf(candidate,range);
15  combined+=sf;
16  delete sf.chi2_;
17  }
18 
19  int status = combined.fit();
20  if(status) { delete combined.chi2_; combined.chi2_=0;}
21  return combined.chi2_;
22 }
23 
24 TH1* SymmetryFit::symmetryChi2(const TH1* candidate, const std::pair<unsigned,unsigned> range)
25 {
26  SymmetryFit sf(candidate, range);
27  int status = sf.fit();
28  if(status) { delete sf.chi2_; sf.chi2_=0; }
29  return sf.chi2_;
30 }
31 
32 SymmetryFit::SymmetryFit(const TH1* h, const std::pair<unsigned,unsigned> r)
33  : symm_candidate_(h),
34  minDF_(r.second-r.first),
35  range_(r),
36  minmaxUsable_(findUsableMinMax()),
37  ndf_( minmaxUsable_.first<minmaxUsable_.second ? minmaxUsable_.second-minmaxUsable_.first : 0),
38  chi2_(0)
39 {
41  fillchi2();
42 }
43 
45 {
46  std::string XXname = name(symm_candidate_->GetName());
47  unsigned Nbins = 2*( range_.second - range_.first ) + 1;
48  double binwidth = symm_candidate_->GetBinWidth(1);
49  double low = symm_candidate_->GetBinCenter(range_.first) - 3*binwidth/4;
50  double up = symm_candidate_->GetBinCenter(range_.second-1) + 3*binwidth/4;
51  chi2_ = new TH1F(XXname.c_str(),"", Nbins, low, up);
52 }
53 
54 std::pair<unsigned,unsigned> SymmetryFit::findUsableMinMax() const
55 {
56  unsigned NEAR(0), FAR(0);
57  const std::vector<std::pair<unsigned,unsigned> > cont = continuousRanges();
58  for(unsigned L=0; L<cont.size(); L++) { if( cont[L].first > range_.first) continue;
59  for(unsigned R=L; R<cont.size(); R++) { if( cont[R].second < range_.second) continue;
60 
61  const unsigned far = std::min( range_.first - cont[L].first,
62  cont[R].second - range_.second );
63  const unsigned near = std::max( range_.second<cont[L].second ? 0 : range_.second - cont[L].second,
64  cont[R].first<range_.first ? 0 : cont[R].first - range_.first );
65 
66  if ( (far>near) && (far-near)>(FAR-NEAR) ) { FAR = far; NEAR = near;}
67  }
68  }
69  return std::make_pair(NEAR,FAR);
70 }
71 
72 std::vector<std::pair<unsigned,unsigned> > SymmetryFit::continuousRanges() const
73 {
74  std::vector<std::pair<unsigned,unsigned> > ranges;
75  const unsigned Nbins = symm_candidate_->GetNbinsX();
76  unsigned i=0;
77  while(++i<=Nbins) {
78  if(symm_candidate_->GetBinError(i)) {
79  std::pair<unsigned,unsigned> range(i,i+1);
80  while(++i<=Nbins && symm_candidate_->GetBinError(i)) range.second++;
81  ranges.push_back(range);
82  }
83  }
84  return ranges;
85 }
86 
87 
89 {
90  if(ndf_<minDF_) return;
91 
92  for(int i=1; i<=chi2_->GetNbinsX() ; ++i) {
93  const unsigned L( range_.first-1+(i-1)/2 ), R( range_.first+i/2 );
94  chi2_->SetBinContent( i, chi2( std::make_pair(L,R)) );
95  }
96 }
97 
98 float SymmetryFit::chi2(std::pair<unsigned,unsigned> point)
99 {
100  point.first -= minmaxUsable_.first;
101  point.second += minmaxUsable_.first;
102  float XX=0;
103  unsigned i=ndf_;
104  while(i-->0) {
105  XX += chi2_element(point);
106  point.first--;
107  point.second++;
108  }
109  return XX;
110 }
111 
112 float SymmetryFit::chi2_element(std::pair<unsigned,unsigned> range)
113 {
114  float
115  y0(symm_candidate_->GetBinContent(range.first)),
116  y1(symm_candidate_->GetBinContent(range.second)),
117  e0(symm_candidate_->GetBinError(range.first)),
118  e1(symm_candidate_->GetBinError(range.second)); assert(e0&&e1);
119 
120  return pow(y0-y1, 2)/(e0*e0+e1*e1);
121 }
122 
124 
125  std::vector<double> p = pol2_from_pol3(chi2_);
126  if( !p.size() ||
127  p[0] < chi2_->GetBinCenter(1) ||
128  p[0] > chi2_->GetBinCenter(chi2_->GetNbinsX()))
129  return 7;
130 
131  TF1* f = fitfunction();
132  f->SetParameter(0, p[0]); f->SetParLimits(0, p[0], p[0]);
133  f->SetParameter(1, p[1]); f->SetParLimits(1, p[1], p[1]);
134  f->SetParameter(2, p[2]); f->SetParLimits(2, p[2], p[2]);
135  f->SetParameter(3, ndf_); f->SetParLimits(3, ndf_,ndf_); //Fixed
136  chi2_->Fit(f,"WQ");
137  return 0;
138 }
139 
141 {
142  TF1* f = new TF1("SymmetryFit","((x-[0])/[1])**2+[2]+0*[3]");
143  f->SetParName(0,"middle");
144  f->SetParName(1,"uncertainty");
145  f->SetParName(2,"chi2");
146  f->SetParName(3,"NDF");
147  return f;
148 }
149 
150 
151 std::vector<double> SymmetryFit::pol2_from_pol2(TH1* hist) {
152  std::vector<double> v;
153 
154  int status = hist->Fit("pol2","WQ");
155  if(!status) {
156  std::vector<double> p;
157  p.push_back(hist->GetFunction("pol2")->GetParameter(0));
158  p.push_back(hist->GetFunction("pol2")->GetParameter(1));
159  p.push_back(hist->GetFunction("pol2")->GetParameter(2));
160  if(p[2]>0) {
161  v.push_back( -0.5*p[1]/p[2] );
162  v.push_back( 1./sqrt(p[2]) );
163  v.push_back( p[0]-0.25*p[1]*p[1]/p[2] );
164  }
165  }
166  return v;
167 }
168 
169 std::vector<double> SymmetryFit::pol2_from_pol3(TH1* hist) {
170  std::vector<double> v;
171 
172  int status = hist->Fit("pol3","WQ");
173  if(!status) {
174  std::vector<double> p;
175  p.push_back(hist->GetFunction("pol3")->GetParameter(0));
176  p.push_back(hist->GetFunction("pol3")->GetParameter(1));
177  p.push_back(hist->GetFunction("pol3")->GetParameter(2));
178  p.push_back(hist->GetFunction("pol3")->GetParameter(3));
179  double radical = p[2]*p[2] - 3*p[1]*p[3] ;
180  if(radical>0) {
181  double x0 = ( -p[2] + sqrt(radical) ) / ( 3*p[3] ) ;
182  v.push_back( x0 );
183  v.push_back( pow( radical, -0.25) );
184  v.push_back( p[0] + p[1]*x0 + p[2]*x0*x0 + p[3]*x0*x0*x0 );
185  }
186  }
187  return v;
188 }
int i
Definition: DBlmapReader.cc:9
const std::pair< unsigned, unsigned > minmaxUsable_
Definition: SymmetryFit.h:33
SymmetryFit(const TH1 *, const std::pair< unsigned, unsigned >)
Definition: SymmetryFit.cc:32
static std::string name(std::string base)
Definition: SymmetryFit.h:14
std::pair< unsigned, unsigned > findUsableMinMax() const
Definition: SymmetryFit.cc:54
const std::pair< unsigned, unsigned > range_
Definition: SymmetryFit.h:33
double XX[2]
Definition: herwig.h:152
static TH1 * symmetryChi2(std::string, const std::vector< TH1 * > &, const std::pair< unsigned, unsigned >)
Definition: SymmetryFit.cc:6
static std::vector< double > pol2_from_pol2(TH1 *hist)
Definition: SymmetryFit.cc:151
float chi2_element(std::pair< unsigned, unsigned >)
Definition: SymmetryFit.cc:112
TH1 * chi2_
Definition: SymmetryFit.h:35
U second(std::pair< T, U > const &p)
unsigned ndf_
Definition: SymmetryFit.h:34
static std::vector< double > pol2_from_pol3(TH1 *hist)
Definition: SymmetryFit.cc:169
const T & max(const T &a, const T &b)
T sqrt(T t)
Definition: SSEVec.h:48
float chi2(std::pair< unsigned, unsigned >)
Definition: SymmetryFit.cc:98
static TF1 * fitfunction()
Definition: SymmetryFit.cc:140
double f[11][100]
The Signals That Services Can Subscribe To This is based on ActivityRegistry h
Helper function to determine trigger accepts.
Definition: Activities.doc:4
bool first
Definition: L1TdeRCT.cc:75
string ranges
Definition: diffTwoXMLs.py:78
Float e1
Definition: deltaR.h:22
const TH1 * symm_candidate_
Definition: SymmetryFit.h:31
int cont
void fillchi2()
Definition: SymmetryFit.cc:88
std::vector< std::pair< unsigned, unsigned > > continuousRanges() const
Definition: SymmetryFit.cc:72
tuple status
Definition: ntuplemaker.py:245
const unsigned minDF_
Definition: SymmetryFit.h:32
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:40
*vegas h *****************************************************used in the default bin number in original ***version of VEGAS is ***a higher bin number might help to derive a more precise ***grade subtle point
Definition: invegas.h:5
void makeChi2Histogram()
Definition: SymmetryFit.cc:44