BOSS 7.0.2
BESIII Offline Software System
Loading...
Searching...
No Matches
MdcxFittedHel.cxx
Go to the documentation of this file.
1// -------------------------------------------------------------------------
2// File and Version Information:
3// $Id: MdcxFittedHel.cxx,v 1.9 2010/09/26 00:31:13 zhangy Exp $
4//
5// Description:
6// Class Implementation for |MdcxFittedHel|
7//
8// Environment:
9// Software developed for the BaBar Detector at the SLAC B-Factory.
10//
11// Author List:
12// S. Wagner
13// Zhang Yao([email protected]) Migrate to BESIII
14//
15// Copyright Information:
16// Copyright (C) 1995 BEPCII
17//
18// History:
19// Migration for BESIII MDC
20//
21//------------------------------------------------------------------------
22#include <math.h>
23#include "MdcxReco/Mdcxmatinv.h"
24#include "MdcxReco/MdcxFittedHel.h"
25#include "MdcxReco/MdcxHit.h"
26#include "MdcxReco/Mdcxprobab.h"
27
28#include "AIDA/IHistogram1D.h"
29#include "AIDA/IHistogram2D.h"
30using std::cout;
31using std::endl;
32using std::ostream;
33
34//extern AIDA::IHistogram1D* g_fitOmega;
36
37void MdcxFittedHel::basics()
38{nhits=0; itofit=0; fittime=0.0;
39 prob=0.0; chisq=1000000.0; fail=1300; quality=0; origin=-1; usedonhel=0;
40 bailout=1; chidofbail=1200.0; niter=10;
41} // endof basics
42
43void MdcxFittedHel::basics(const HepAList<MdcxHit> &e) {
44 basics();
45 nhits=e.length();
46 xHitList=e;
47 origin=OriginIncluded();
48} // endof basics
49
50//constructors
52 basics();
53}
54
55//points+guess
57(HepAList<MdcxHit> &XHitList, MdcxHel& hel, double Sfac)
58{
59 basics(XHitList);
60 sfac=Sfac;
61 *this=hel;
63}//endof MdcxFittedHel
64
65//destructor
66MdcxFittedHel::~MdcxFittedHel( ){ }//endof ~MdcxFittedHel
67
68//operators
70 copy(rhs);
71 return *this;
72} //endof MdcxFittedHel::operator=
73
75 // FIXME
76 copy(rhs);
77 fail=rhs.Fail();
78 chisq=rhs.Chisq();
79 rcs=rhs.Rcs();
80 prob=rhs.Prob();
81 fittime=rhs.Fittime();
82 nhits=rhs.Nhits();
83 itofit=rhs.Itofit();
84 quality=rhs.Quality();
85 origin=rhs.Origin();
86 xHitList=rhs.XHitList();
87 sfac=rhs.Sfac();
89 bailout=1; chidofbail=1200.0; niter=10;
90 return *this;
91}//endof MdcxFittedHel::operator=
92
94 const HepAList<MdcxHit> &ListOAdds) {
95 copy(rhs);
96 fail=rhs.Fail();
97 chisq=rhs.Chisq();
98 //rcs=rhs.Rcs();
99 //prob=rhs.Prob();
100 fittime=0.0;
101 nhits=rhs.Nhits();
102 itofit=0;
103 quality=rhs.Quality();
104 origin=rhs.Origin();
105 xHitList=rhs.XHitList();
106 sfac=rhs.Sfac();
108 bailout=1; chidofbail=1200.0; niter=10;
109 int kkk=0; while (ListOAdds[kkk]){ListOAdds[kkk]->SetUsedOnHel(0); kkk++;}
110 kkk=0; while (xHitList[kkk]){xHitList[kkk]->SetUsedOnHel(1); kkk++;}
111 double spull;
112 MdcxHel* temp = (MdcxHel*)(&rhs);
113 kkk=0; while (ListOAdds[kkk]){
114 if (ListOAdds[kkk]->GetUsedOnHel() == 0) {
115 spull = ListOAdds[kkk]->pull(*temp)/sfac;
116 chisq += spull*spull;
117 xHitList.append(ListOAdds[kkk]);
118 nhits++;
119 }
120 kkk++;
121 }
122
123 int ndof = nhits - nfree;
124 prob = Mdcxprobab(ndof, chisq);
125 rcs = chisq/ndof;
126 return *this;
127}//endof MdcxFittedHel::Grow
128
129//accessors
131 //float pull=xHitList[i]->pull(*this);
132 //float E=xHitList[i]->e();
133 //return pull*E;
134 return xHitList[i]->residual(*this);
135}//endof Residual
136
138 return xHitList[i]->pull(*this);
139}//endof Pulls
140
141int MdcxFittedHel::Fail(float Probmin)const {
142 if(fail) return fail;
143 if(prob < Probmin) return 1303;
144 // now done in DoFit if(fabs(omega)>omegmax) {return 1306;}
145 return 0;
146} // endof Fail
147
148//utilities&workers
149
151 int kbl = 0;
152 while(xHitList[kbl]) xHitList[kbl++]->SetConstErr(0);
153}
154
156 fail = IterateFit();
157 return fail;
158}//endof ReFit
159
161 int ftemp = 1301; // not enough hits
162 if (nfree > nhits) return ftemp;
163 ftemp = 0;
164
165 if(6 == debug) std::cout<<"IterateFit niter="<<niter<< std::endl;
166 if (niter >= 1) {
167 float prevchisq = 0.0;
168 for (int i = 0; i < niter; i++) {
169 itofit = i + 1;
170 ftemp = DoFit();
171 if (6 == debug) {
172 if (nfree == 5) {
173 cout << " iteration number= " << i << " chisq= " << chisq;
174 cout << " nhits= " << nhits << " " << " fail= " << ftemp << endl;
175 }
176 print();
177 }
178 if (ftemp != 0) break;
179 if(6 == debug)std::cout<<"in MdcxFittedHel::IterateFit() chisq="<<chisq<<" prechi2="<<prevchisq<<std::endl;//yzhang debug
180 if ((fabs(chisq-prevchisq) < 0.01*chisq) || (chisq < 0.001)) break; /// FIXME
181 prevchisq = chisq;
182 }//endof iter loop
183 } else {
184 float prevchisq = 0.0;
185 chisq = 1000000.0;
186 int iter = 0;
187 while ((fabs(chisq-prevchisq) > 0.01) && (iter++ < 1000)) {
188 prevchisq = chisq;
189 ftemp = DoFit();
190 if (ftemp != 0) break;
191 }//endof (fabs(chisq-oldchisq).gt.0.01)
192 }//endof (niter>=1)
193 int ndof = nhits - nfree;
194 prob = Mdcxprobab(ndof, chisq);
195 rcs = chisq/ndof;
196 return ftemp;
197}//endof IterateFit
198
200 int ftemp = 1301;
201 // if(nfree>nhits) {return Fail;}
202 if (nfree > nhits) return ftemp;
203 double m_2pi = 2.0 * M_PI;
204 ftemp = 0;
205 //pointloop
206 if (6 == debug) {
207 std::cout << "in MdcxFittedHel::DoFit() nfree = " << nfree
208 << " nhits = " << nhits << std::endl;
209 }
210
211 int norder = nfree;
212 double A[10][10] = {{0.}}, B[10] = {0.}, D[10] = {0.}, det;
213 chisq = 0.0;
214
215 if (6 == debug) {
216 std::cout << "xHitList.length " << xHitList.length() << " ";
217 for (int ii = 0; ii < xHitList.length(); ii++) {
218 xHitList[ii]->print(std::cout, ii);
219 }
220 std::cout << std::endl << "sfac = " << sfac << std::endl;
221 }
222
223 for (int i = 0; i < nhits; i++) {
224 std::vector<float> derivs = xHitList[i]->derivatives(*this);
225 if (6 == debug) {
226 cout << "derivs " << i<<" ";
227 for (unsigned int ii = 0; ii < derivs.size(); ii++) {
228 cout << setw(15)<< derivs[ii];
229 }
230 std::cout << std::endl;
231 }
232 if (sfac != 1.0) {
233 for(unsigned int ipar = 0; ipar < derivs.size(); ipar++) {
234 derivs[ipar] /= sfac;
235 if(6 == debug) cout << " derivs[" << ipar << "] = " << derivs[ipar];
236 }
237 if(6 == debug) std::cout << std::endl;
238 }
239 chisq += derivs[0] * derivs[0];
240 //outer parameter loop
241 for (int ipar = 0; ipar < norder; ipar++) {
242 D[ipar] += derivs[0] * derivs[ipar+1];
243 //inner parameter loop
244 for(int jpar = 0; jpar < norder; jpar++) {
245 A[ipar][jpar] += derivs[ipar+1] * derivs[jpar+1];
246 }//endof inner parameter loop
247 }//endof outer parameter loop
248 }//pointloop
249 if (6 == debug) cout << "chisq = " << chisq << endl;
250 if (chisq == 0 && nhits != 3) { //zoujh: chisq is invalid??? FIXME
251 ftemp = 1310;
252 return ftemp;
253 }
254 if (6 == debug) {
255 for (int ii = 0; ii < norder; ii++) {
256 cout << "D["<< ii << "]: " << D[ii] << " A:";
257 for (int jj = 0; jj < norder; jj++) cout << " " << A[ii][jj];
258 cout << endl;
259 }
260 }
261 //invert A
262 int ierr;
263 if (bailout) {
264 ftemp = 1308; // bailout
265 int ndof = nhits - nfree;
266 if (ndof > 0) {
267 if (6 == debug){
268 cout << "chisq " << chisq << " ndof " << ndof
269 << " chiperdof " << chisq/ndof
270 << " >?chidofbail " << chidofbail << endl;
271 }
272 float chiperdof = chisq/ndof;
273 if(chiperdof > chidofbail) return ftemp;
274 } else {
275 if (6 == debug){
276 cout << " ndof <=0 : chisq " << chisq
277 << " >? chidofbail/2.5 " << chidofbail/2.5 << endl;
278 }
279 if (chisq > chidofbail/2.5) return ftemp; //FIXME
280 }
281 } // (bailout)
282 ftemp = 0;
283 ierr = Mdcxmatinv(&A[0][0], &norder, &det);
284 if (6 == debug) cout << "ierr = " << ierr << endl;
285 if (ierr == 0) {
286 for(int ii = 0; ii < norder; ii++)
287 for(int jj = 0; jj < norder; jj++)
288 B[ii] += A[ii][jj] * D[jj];
289 if (6 == debug) {
290 for (int ii = 0; ii < norder; ii++) {
291 cout << "B[" << ii << "]: " << B[ii] << " A:";
292 for (int jj = 0; jj < norder; jj++) cout << " " << A[ii][jj];
293 cout << endl;
294 }
295 }
296 int bump = -1;
297 if (qd0) d0 -= B[++bump];
298 if (qphi0) {
299 phi0 -= B[++bump];
300 if (phi0 > M_PI) phi0 -= m_2pi;
301 if (phi0 < -M_PI) phi0 += m_2pi;
302 cphi0 = cos(phi0); sphi0 = sin(phi0);
303 }
304 if (qomega) {
305 omega -= B[++bump];
306 ominfl = (fabs(omega) < omin) ? 0 : 1;
307 }
308 if (qz0) z0 -= B[++bump];
309 if (qtanl) tanl -= B[++bump];
310 if (qt0) t0 -= B[++bump];
311
312 x0 = X0(); y0 = Y0(); xc = Xc(); yc = Yc();
313 if ( fabs(d0) > MdcxParameters::maxMdcRadius ) ftemp = 1305;
314 //if(g_fitOmega)g_fitOmega->fill(omega);
315 if ( fabs(omega) > 10.0 ) ftemp = 1306; // Too tight (r < 1 cm)//yzhang FIXME 2009-11-03
316 } else {
317 ftemp = ierr;
318 }
319 return ftemp;
320}//endof DoFit
321
322//is origin included in fit ?
323int MdcxFittedHel::OriginIncluded() {
324 for(int i=0; xHitList[i]; i++) {
325 int type=xHitList[i]->type();
326 if(2==type) { // origin "hit" ?
327 //move to end, move fit point, return hit number
328 xHitList.swap(i,nhits-1);
329 return nhits-1;
330 }
331 }
332 return -1;
333}
334
336 cout << " d0 " << d0;
337 cout << " phi0 " << phi0;
338 cout << " omega " << omega;
339 cout << " z0 " << z0;
340 cout << " tanl " << tanl << endl;
341 cout << " fail " << fail;
342 cout << " chisq " << chisq;
343 cout << " iter to fit " << itofit;
344 cout << " sfac " << sfac;
345 cout << " rcs " << rcs;
346 cout << " prob " << prob;
347 cout << " fittime " << fittime << endl;
348 cout << " nhits= " << nhits << " xHitList.length " << xHitList.length() << endl;
349 for (int ii = 0; ii < xHitList.length(); ii++) {
350 xHitList[ii]->print(cout, ii);
351 }
352 cout<<endl;
353
354 return 0;
355}//endof FitPrint
356
357int MdcxFittedHel::FitPrint(MdcxHel &hel, ostream &o) {
358 double m_2pi=2.0*M_PI;
359 double difphi0=phi0-hel.Phi0();
360 if (difphi0>M_PI)difphi0-=m_2pi; if (difphi0<-M_PI)difphi0+=m_2pi;
361 cout << " difphi0= " << difphi0 << endl;
362 cout << " difomega= " << omega-hel.Omega() << endl;
363 cout << " difz0= " << z0-hel.Z0() << endl;
364 cout << " diftanl= " << tanl-hel.Tanl() << endl;
365 o << "FitPrint ";
366 o << "nhits "<< nhits << " fail " << fail << " chi2 " << chisq ;
367 o << "rcs " << rcs << " prob " << prob <<endl;
368 return 0;
369}//endof FitPrint
370
371//Find layer number of |hitno|
372int MdcxFittedHel::Layer(int hitno)const {
373 if(hitno >= nhits) return 0;
374 return xHitList[hitno]->Layer();
375} // endof Layer
376
377//Find superlayer numbber of |hitno|
378int MdcxFittedHel::SuperLayer(int hitno) const {
379 if(hitno >= nhits) { return 0; }
380 if(hitno < 0) { return 0; }
381 return xHitList[hitno]->SuperLayer();
382} // endof SuperLayer
383
EvtStreamInputIterator< typename Generator::result_type > iter(Generator gen, int N=0)
double sin(const BesAngle a)
double cos(const BesAngle a)
int Mdcxmatinv(double *array, int *norder, double *det)
Definition: Mdcxmatinv.cxx:7
float Mdcxprobab(int &ndof, float &chisq)
Definition: Mdcxprobab.cxx:4
#define M_PI
Definition: TConstant.h:4
int Layer(int hitno=0) const
float Residual(int i)
const HepAList< MdcxHit > & XHitList() const
int SuperLayer(int hitno=0) const
MdcxFittedHel & operator=(const MdcxHel &)
int Fail(float Probmin=0.0) const
float Pull(int i)
MdcxFittedHel & Grow(const MdcxFittedHel &, const HepAList< MdcxHit > &)
virtual ~MdcxFittedHel()
double X0() const
Definition: MdcxHel.cxx:77
double Yc() const
Definition: MdcxHel.cxx:68
double Xc() const
Definition: MdcxHel.cxx:59
void print() const
Definition: MdcxHel.cxx:357
double Y0() const
Definition: MdcxHel.cxx:81
void copy(const MdcxHel &hel)
Definition: MdcxHel.cxx:219
static const double maxMdcRadius
MDC Geometry.