CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
DcxFittedHel.cc
Go to the documentation of this file.
1 // -------------------------------------------------------------------------
2 // File and Version Information:
3 // $Id: DcxFittedHel.cc,v 1.5 2011/04/07 21:47:06 stevew Exp $
4 //
5 // Description:
6 // Class Implementation for |DcxFittedHel|
7 //
8 // Environment:
9 // Software developed for the BaBar Detector at the SLAC B-Factory.
10 //
11 // Author List:
12 // S. Wagner
13 //
14 //------------------------------------------------------------------------
15 //babar #include "BaBar/BaBar.hh"
16 //babar #include <math.h>
17 
18 //babar #include "DcxReco/Dcxmatinv.hh"
19 //babar #include "DcxReco/DcxFittedHel.hh"
20 //babar #include "DcxReco/DcxHit.hh"
21 //babar #include "DcxReco/Dcxprobab.hh"
22 #include <cmath>
23 #include <sstream>
24 #include "RecoTracker/RoadSearchHelixMaker/interface/Dcxmatinv.hh"
25 #include "RecoTracker/RoadSearchHelixMaker/interface/DcxFittedHel.hh"
26 #include "RecoTracker/RoadSearchHelixMaker/interface/DcxHit.hh"
27 #include "RecoTracker/RoadSearchHelixMaker/interface/Dcxprobab.hh"
28 
30 
31 using std::cout;
32 using std::endl;
33 using std::ostream;
34 
35 void DcxFittedHel::basics()
36 {nhits=0; itofit=0; fittime=0.0;
37  prob=0.0; chisq=1000000.0; fail=1300; quality=0; origin=-1; usedonhel=0;
38  bailout=1; chidofbail=1000.0; niter=10;
39 } // endof basics
40 
41 //babar void DcxFittedHel::basics(const HepAList<DcxHit> &e) {
42 void DcxFittedHel::basics(const std::vector<DcxHit*> &e) {
43  basics();
44  nhits=e.size();
45  listohits=e;
46  origin=OriginIncluded();
47 } // endof basics
48 
49 //constructors
50 DcxFittedHel::DcxFittedHel(){basics();}
51 
52 //points+guess
53 //babar DcxFittedHel::DcxFittedHel(HepAList<DcxHit> &ListOHits, DcxHel& hel, double Sfac)
54 DcxFittedHel::DcxFittedHel(std::vector<DcxHit*> &ListOHits, DcxHel& hel, double Sfac)
55 {
56  // float tstart=clock();
57  basics(ListOHits);
58  sfac=Sfac;
59  *this=hel;
60  fail=IterateFit();
61  // float tstop=clock();
62  // fittime=tstop-tstart;
63 }//endof DcxFittedHel
64 
65 //destructor
66 DcxFittedHel::~DcxFittedHel( ){ }//endof ~DcxFittedHel
67 
68 //operators
69 DcxFittedHel& DcxFittedHel::operator=(const DcxHel& rhs){
70  copy(rhs);
71  return *this;
72 } //endof DcxFittedHel::operator=
73 
74 DcxFittedHel& DcxFittedHel::operator=(const DcxFittedHel& rhs){
75  copy(rhs);
76  fail=rhs.Fail();
77  chisq=rhs.Chisq();
78  rcs=rhs.Rcs();
79  prob=rhs.Prob();
80  fittime=rhs.Fittime();
81  nhits=rhs.Nhits();
82  itofit=rhs.Itofit();
83  quality=rhs.Quality();
84  origin=rhs.Origin();
85  listohits=rhs.ListOHits();
86  sfac=rhs.Sfac();
87  usedonhel=rhs.GetUsedOnHel();
88  bailout=1; chidofbail=1000.0; niter=10;
89  return *this;
90 }//endof DcxFittedHel::operator=
91 
92 //babar DcxFittedHel& DcxFittedHel::Grow(const DcxFittedHel& rhs, HepAList<DcxHit> &ListOAdds){
93 DcxFittedHel& DcxFittedHel::Grow(const DcxFittedHel& rhs, std::vector<DcxHit*> &ListOAdds){
94  copy(rhs);
95  fail=rhs.Fail();
96  chisq=rhs.Chisq();
97  // rcs=rhs.Rcs();
98  // prob=rhs.Prob();
99  fittime=0.0;
100  nhits=rhs.Nhits();
101  itofit=0;
102  quality=rhs.Quality();
103  origin=rhs.Origin();
104  listohits=rhs.ListOHits();
105  sfac=rhs.Sfac();
106  usedonhel=rhs.GetUsedOnHel();
107  bailout=1; chidofbail=1000.0; niter=10;
108  int kkk=0; while (ListOAdds[kkk]){ListOAdds[kkk]->SetUsedOnHel(0); kkk++;}
109  kkk=0; while (listohits[kkk]){listohits[kkk]->SetUsedOnHel(1); kkk++;}
110  double spull; DcxHel temp=rhs;
111  kkk=0; while (ListOAdds[kkk]){
112  if (ListOAdds[kkk]->GetUsedOnHel() == 0){
113  spull=ListOAdds[kkk]->pull(temp)/sfac; chisq+=spull*spull;
114  //babar listohits.append(ListOAdds[kkk]); nhits++;
115  listohits.push_back(ListOAdds[kkk]); nhits++;
116  }
117  kkk++;
118  }
119  int ndof=nhits-nfree;
120  prob=Dcxprobab(ndof,chisq);
121  rcs=chisq/ndof;
122  return *this;
123 }//endof DcxFittedHel::Grow
124 
125 //accessors
126 float DcxFittedHel::Residual(int i){
127  float pull=listohits[i]->pull(*this);
128  float E=listohits[i]->e();
129  return pull*E;
130 }//endof Residual
131 
132 float DcxFittedHel::Pull(int i){
133  float pull=listohits[i]->pull(*this);
134  return pull;
135 }//endof Pulls
136 
137 int DcxFittedHel::Fail(float Probmin)const {
138  if(fail) {return fail;}
139  if(prob<Probmin) {return 1303;}
140  // now done in DoFit if(fabs(omega)>omegmax) {return 1306;}
141  return 0;
142 } // endof Fail
143 
144 //utilities&workers
145 
146 void DcxFittedHel::VaryRes() {
147  int kbl=0; while (listohits[kbl]){listohits[kbl]->SetConstErr(0); kbl++;}
148 }
149 
150 int DcxFittedHel::ReFit(){
151  fail=IterateFit();
152  return fail;
153 }//endof ReFit
154 
155 int DcxFittedHel::IterateFit(){
156  int ftemp=1301; // not enough hits
157  if(nfree>=nhits) {return ftemp;}
158  ftemp=0;
159  if(niter>=1) {
160  float prevchisq=0.0;
161  for (int i=0; i< niter; i++) {
162  itofit=i+1;
163  ftemp=DoFit();
164 // if (nfree == 5){
165 // LogInfo("RoadSearch") << " iteration number= " << i << " chisq= " << chisq;
166 // LogInfo("RoadSearch") << " nhits= " << nhits << " " << " fail= " << ftemp ;
167 // }
168 // print();
169  if(ftemp!=0) {break;}
170  if(fabs(chisq-prevchisq)<0.01*chisq) {break;}
171  prevchisq=chisq;
172  }//endof iter loop
173  }else{
174  float prevchisq=0.0;
175  chisq=1000000.0;
176  int iter=0;
177  while(fabs(chisq-prevchisq)>0.01) {
178  iter++;
179  prevchisq=chisq;
180  ftemp=DoFit();
181  if(ftemp!=0) break;
182  if(iter>=1000) break;
183  }//endof (fabs(chisq-oldchisq).gt.0.01)
184  }//endof (niter>=1)
185  int ndof=nhits-nfree;
186  prob=Dcxprobab(ndof,chisq);
187  rcs=chisq/ndof;
188  return ftemp;
189 }//endof IterateFit
190 
191 int DcxFittedHel::DoFit(){
192  int ftemp=1301;
193  // if(nfree>nhits) {return Fail;}
194  if(nfree>=nhits) {return ftemp;}
195  double m_2pi=2.0*M_PI;
196  ftemp=0;
197  //pointloop
198  int norder=nfree;
199  double A[10][10], B[10], D[10], det; int ii, jj;
200  for (ii=0; ii<norder; ii++){
201  D[ii]=0.0; B[ii]=0.0; for (jj=0; jj<norder; jj++){A[ii][jj]=0.0;}
202  }
203  chisq=0.0;
204  for (int i=0; i< nhits; i++){
205  std::vector<float> derivs=listohits[i]->derivatives(*this);
206  std::ostringstream output;
207  output << "derivs ";
208  if (sfac != 1.0){
209  for(unsigned int ipar=0; ipar<derivs.size(); ipar++) {derivs[ipar]/=sfac;
210  output << " " << derivs[ipar];
211  }
212 // edm::LogInfo("RoadSearch") << output;
213  }
214  chisq+=derivs[0]*derivs[0];
215  //outer parameter loop
216  for(int ipar=0; ipar<norder; ipar++){
217  D[ipar]+=derivs[0]*derivs[ipar+1];
218  //inner parameter loop
219  for(int jpar=0; jpar<norder; jpar++){
220  A[ipar][jpar]+=derivs[ipar+1]*derivs[jpar+1];
221  }//endof inner parameter loop
222  }//endof outer parameter loop
223  }//pointloop
224 // edm::LogInfo("RoadSearch") << " D A " ;
225  for (ii=0; ii<norder; ii++){
226  std::ostringstream output;
227  output << D[ii] << " "; for (jj=0;jj<norder;jj++){output << A[ii][jj] << " ";}
228 // edm::LogInfo("RoadSearch") << output;
229  }
230  //invert A
231  int ierr;
232  if(bailout) {
233  ftemp=1308; // bailout
234  int ndof=nhits-nfree;
235  if(ndof>0) {
236  float chiperdof=chisq/ndof;
237  if(chiperdof>chidofbail) {return ftemp;}
238 // edm::LogInfo("RoadSearch") << " got here; chiperdof = " << chiperdof ;
239  } // (ndof>0)
240  } // (bailout)
241  ftemp=0;
242  ierr = Dcxmatinv(&A[0][0],&norder,&det);
243 // edm::LogInfo("RoadSearch") << " ierr = " << ierr ;
244  if(ierr==0) {
245  for(ii=0;ii<norder;ii++){for(jj=0;jj<norder;jj++){B[ii]+=A[ii][jj]*D[jj];}}
246  for (ii=0; ii<norder; ii++){
247  std::ostringstream output;
248  output << B[ii] << " "; for(jj=0;jj<norder;jj++){output << A[ii][jj] << " ";}
249 // edm::LogInfo("RoadSearch") << output;
250  }
251  int bump=-1;
252  if(qd0) {bump++; d0-=B[bump];}
253  if(qphi0) {bump++; phi0-=B[bump];
254  if (phi0 > M_PI){phi0-=m_2pi;}
255  if (phi0 < -M_PI){phi0+=m_2pi;}
256  cphi0=cos(phi0); sphi0=sin(phi0);
257  }
258  if(qomega) {bump++; omega-=B[bump];
259  ominfl=1; if(fabs(omega)<omin){ominfl=0;}
260  }
261  if(qz0) {bump++; z0-=B[bump];}
262  if(qtanl) {bump++; tanl-=B[bump];}
263  if(qt0) {bump++; t0-=B[bump];}
264  x0=X0(); y0=Y0(); xc=Xc(); yc=Yc();
265  if ( fabs(d0) > 80.0 )ftemp=1305; // No longer in Dch
266  if ( fabs(omega) > 1.0 )ftemp=1306; // Too tight (r < 1 cm)
267  }else{
268  // Fail=Fail+ierr;
269  ftemp=ierr;
270  }
271  return ftemp;
272 }//endof DoFit
273 
274 //is origin included in fit ?
275 int DcxFittedHel::OriginIncluded() {
276  for(int i=0; listohits[i]; i++) {
277  int type=listohits[i]->type();
278  if(2==type) { // origin "hit" ?
279  //move to end, move fit point, return hit number
280  //cms?? listohits.swap(i,nhits-1);
281  return nhits-1;
282  } // (2==type)
283  } // (int i=0; listohits[i]; i++)
284  return -1;
285 }//endof OriginIncluded
286 
287 int DcxFittedHel::FitPrint(){
288  edm::LogInfo("RoadSearch") << " fail= " << fail
289  << " iterations to fit= " << itofit
290  << " nhits= " << nhits
291  << " sfac= " << sfac
292  << " chisq= " << chisq
293  << " rcs= " << rcs
294  << " prob= " << prob
295  << " fittime= " << fittime ;
296  return 0;
297 }//endof FitPrint
298 
299 int DcxFittedHel::FitPrint(DcxHel &hel){
300  FitPrint();
301  double m_2pi=2.0*M_PI;
302  double difphi0=phi0-hel.Phi0();
303  if (difphi0>M_PI)difphi0-=m_2pi; if (difphi0<-M_PI)difphi0+=m_2pi;
304  edm::LogInfo("RoadSearch") << " difd0= " << d0-hel.D0()
305  << " difphi0= " << difphi0
306  << " difomega= " << omega-hel.Omega()
307  << " difz0= " << z0-hel.Z0()
308  << " diftanl= " << tanl-hel.Tanl() ;
309  return 0;
310 }//endof FitPrint
311 
312 //Find layer number of |hitno|
313 int DcxFittedHel::Layer(int hitno)const {
314  if(hitno>=nhits) {return 0;}
315  //babar const HepAList<DcxHit> &temp=(HepAList<DcxHit>&)listohits;
316  const std::vector<DcxHit*> &temp=(std::vector<DcxHit*>&)listohits;
317  int layer=temp[hitno]->Layer();
318  return layer;
319 } // endof Layer
320 
321 //Find superlayer numbber of |hitno|
322 int DcxFittedHel::SuperLayer(int hitno)const {
323  if(hitno>=nhits) {return 0;}
324  if(hitno<0) {return 0;}
325  //babar const HepAList<DcxHit> &temp=(HepAList<DcxHit>&)listohits;
326  const std::vector<DcxHit*> &temp=(std::vector<DcxHit*>&)listohits;
327  return temp[hitno]->SuperLayer();
328 } // endof SuperLayer
329 
nocap nocap const skelname & operator=(const skelname &)
type
Definition: HCALResponse.h:22
int i
Definition: DBlmapReader.cc:9
float Dcxprobab(int &ndof, float &chisq)
Definition: Dcxprobab.cc:6
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
reject
Definition: HLTenums.h:23
int Dcxmatinv(double *array, int *norder, double *det)
Definition: Dcxmatinv.cc:13
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
#define M_PI
Definition: BFit3D.cc:3
static const double Y0
tuple cout
Definition: gather_cfg.py:121
static const double X0
DecomposeProduct< arg, typename Div::arg > D
Definition: Factorize.h:150