-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathStFgtAlignmentMaker.cxx
More file actions
2683 lines (2579 loc) · 107 KB
/
StFgtAlignmentMaker.cxx
File metadata and controls
2683 lines (2579 loc) · 107 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*!
* \class StFgtAlignmentMaker
* \brief A typical Analysis Class
* \author Torre Wenaus, BNL, Thomas Ullrich
* \date Nov 1999
*
* $Id: StFgtAlignmentMaker.cxx,v 1.11 2015/05/21 19:46:15 akio Exp $
*
*/
#include <stdio.h>
#include <math.h>
#include "StFgtAlignmentMaker.h"
#include "StEventTypes.h"
#include "StMessMgr.h"
#include "StDcaGeometry.h"
#include "TNtuple.h"
#include "TMinuit.h"
#include "TRandom.h"
#include "TCanvas.h"
#include "TText.h"
#include "TGraph.h"
#include "TH1F.h"
#include "TH2F.h"
#include "TArrayL64.h"
#include "StThreeVectorF.hh"
#include "StPhysicalHelix.hh"
#include "SystemOfUnits.h"
#include "THelixTrack.h"
#include "StDetectorName.h"
#include "fgtAlignment.h"
#include "StRoot/StFgtUtil/geometry/StFgtGeom.h"
#include "StTpcDb/StTpcDb.h"
#include "StDetectorDbMaker/St_vertexSeedC.h"
#include "StRoot/StFgtDbMaker/StFgtDbMaker.h"
#include "StRoot/StFgtDbMaker/StFgtDb.h"
#include "StMuDSTMaker/COMMON/StMuDst.h"
#include "StMuDSTMaker/COMMON/StMuPrimaryVertex.h"
#include "StMuDSTMaker/COMMON/StMuDst.h"
#include "StMuDSTMaker/COMMON/StMuEvent.h"
#include "StarClassLibrary/StThreeVectorF.hh"
#include "StRoot/StFgtPool/StFgtClusterTools/StFgtGeneralBase.h"
#include "StRoot/StFgtPool/StFgtClusterTools/StFgtStraightTrackMaker.h"
#include "StRoot/StEEmcPool/StEEmcIUPi0/StEEmcIUPointMaker.h"
#include "StRoot/StEmcUtil/geometry/StEmcGeom.h"
static const int mDebug=0; // debug mesasge level
static const int mEventDisp=0; // event display
static const int MAXTRK=500000; //max number of tracks
static const int MAXHIT=8; //max number of hits
static const int NPAR=24*6; //number of parameters=(6 discs)*(4 quads)*(3 angles + 3 offset)
static const int NAXIS=4; // number of axis for hist
static TH1F *hist1[kFgtNumDiscs+6][kFgtNumQuads][NAXIS]; // 1d residual histos
static TH2F *hist2[kFgtNumDiscs+6][kFgtNumQuads][NAXIS*2]; // 2d residual histos
static TH1F *histdz;
static const double PI = 4.0*atan(1);
const float ISOCUT=0.5;
const float maxdfgt=0.15, maxdvtx=0.5, maxdtpc=10.0, maxdemc=10.0;
const float maxpfgt=0.005, maxpvtx=0.5, maxptpc= 0.3, maxpemc=0.3;
struct TRKHIT{
int run,evt,nhit,nhitUse,nhitFgt,nhitVtx,nhitTpc,nhitPrompt,nhitEemc,used;
float dca, chi2, trkz, dz, eta, phi, opt; //track parameters (last one is 1/pT)
int det[MAXHIT]; //0-23=FGT(disc*4+quad), 24-27=vertex, 28-31=TPC, 32-35=Prompt, 36-39=EEMC separated by quad
float x[MAXHIT],y[MAXHIT],z[MAXHIT]; //xyz
float ex[MAXHIT],ey[MAXHIT],ez[MAXHIT]; //xyz error
float lr[MAXHIT],lp[MAXHIT],r[MAXHIT],p[MAXHIT]; //local and globak r and phi
float dx[MAXHIT],dy[MAXHIT],dr[MAXHIT],dp[MAXHIT]; //residuals
float tw[MAXHIT],p1[MAXHIT],p2[MAXHIT],po[MAXHIT]; //EEMC energies (FGT: tw=ChgPhi, p1=ChgR, p2=PhiRAsy, po=EvenOddAsy)
float su[MAXHIT],sv[MAXHIT]; //ESMD energies
int nrl[MAXHIT],nsu[MAXHIT],nsv[MAXHIT]; //eemc number of relatives, strips in smd u & v
float ele[4],trk[4],bemc[4],eemc[4],tot[4],rec[4],iso[4]; //4 momentums of ele/tracks/bemc/eemc/total(trk+bemc+eemc)/recoil(=tot-ele)/isolation cone
float ptbal,riso; //pt balance & energy ratio ele/total in isolation cone
bool use[MAXHIT]; //false=do not use in fit true=use in fit
};
static int mNtrk[kFgtNumQuads]; // number of tracks per quad
static int mNtrkUse[kFgtNumQuads]; // number of usable tracks per quad after hitmask
static TRKHIT mHit[kFgtNumQuads][MAXTRK]; // storage to keep hit for track
static TRKHIT mHitAlg; // current working hits to be modified with aliggnment parameters
static int mQuad; // quad currently working on
static int mStep; // step number
static int mHitMaskDisc; // disc mask to define hits to be used hits in track fits. bit0-5=fgt, bit6=vtx, bit7=tpc, bit8=prompt, bit9=eemc
static int mResidMaskDisc; // disc mask to include residual sum of all tracks for alignments
static int mFgtInputRPhi; // 0=x/y/z or 1=r/phi/z for fgt hits in mHit
static int mFillHist; // 0=not filling histo 1=filling histo before alignments 2=filling histo after alignment
static int mReqHit=0; // required # of hits
static int mReqFgtHit=0; // required # of FGT hits
static int mReqVtx=0; // required vertex
static int mReqTpcHit=0; // required # of TPC hits
static int mReqPromptHit=0; // required # of TPC prompt hits
static int mReqEemcHit=0; // required # of EEMC hits
static float mErrFGT=0;
static float mErrVTX=0;
static float mErrVTXZ=0;
static float mErrTPCI=0;
static float mErrTPCO=0;
static float mErrTPCZ=0;
static float mErrPPT=0;
static float mErrEMC=0;
static int mBeamLine=1;
static StFgtDb* mDb;
static fgtAlignment_st* orig_algpar;
static StThreeVectorF ele,tracks,bemcs,eemcs,tot,rec,iso;
static float ptbal,riso;
#define SMALL_NUM 1e-4
class Vector{
public:
Vector(double vx=0, double vy=0, double vz=0):x(vx),y(vy),z(vz){}
Vector(const Vector& v):x(v.x),y(v.y),z(v.z){}
Vector(const Vector& v1, const Vector& v2):x(v1.x-v2.x),y(v1.y-v2.y),z(v1.z-v2.z){}
// Vector operator=(const Vector& v) {return Vector(v.x,v.y,v.z);}
Vector operator=(const Vector& v) {x=v.x; y=v.y; z=v.z;}
Vector operator-() {return Vector(-x,-y,-z);}
friend Vector operator+(const Vector& v1, const Vector& v2) {return Vector(v1.x+v2.x,v1.y+v2.y,v1.z+v2.z);}
friend Vector operator-(const Vector& v1, const Vector& v2) {return Vector(v1.x-v2.x,v1.y-v2.y,v1.z-v2.z);}
friend Vector operator*(const double d, const Vector& v) {return Vector(d*v.x,d*v.y,d*v.z);}
friend Vector operator*(const Vector& v, const double d) {return Vector(d*v.x,d*v.y,d*v.z);}
friend double operator*(const Vector& v1, const Vector& v2) {return v1.x*v2.x+v1.y*v2.y+v1.z*v2.z;}
double length() {return sqrt((*this)*(*this));}
void print(){printf("x=%8.3f y=%8.3f z=%8.3f\n",x,y,z);}
double x,y,z;
};
class Line{
public:
Line(Vector& u0, Vector& u1):v0(u0),v1(u1) {};
Vector v0,v1;
};
double distance(Line l1, Line l2, Vector& p1, Vector& p2){
Vector u(l1.v1,l1.v0);
Vector v(l2.v1,l2.v0);
Vector w(l1.v0,l2.v0);
double a=u*u;
double b=u*v;
double c=v*v;
double d=u*w;
double e=v*w;
double D=a*c - b*b;
double sc,tc;
if(D<SMALL_NUM) { // the lines are almost parallel
sc = 0.0;
tc = (b>c ? d/b : e/c); // use the largest denominator
printf("SMALL NUM!!!\n");
}else{
sc = (b*e - c*d) / D;
tc = (a*e - b*d) / D;
}
// 2 closest point
p1=l1.v0 + (sc * u);
p2=l2.v0 + (tc * v);
// get the difference of the two closest points
Vector dP = p1 - p2;
//Vector dP = w + (sc * u) - (tc * v); // = L1(sc) - L2(tc)
return dP.length(); // return the closest distance
}
void getZvtxAndDca(double* p, double& dca, double& z, double zmin=-900, double zmax=900){
double x0=0,y0=0,dxdz=0,dydz=0;
if(mFgtInputRPhi>0){
St_vertexSeedC* vSeed = St_vertexSeedC::instance();
x0=vSeed->x0(); dxdz=vSeed->dxdz();
y0=vSeed->y0(); dydz=vSeed->dydz();
}
Vector p1(p[0]+p[1]*zmin, p[2]+p[3]*zmin, zmin);
Vector p2(p[0]+p[1]*zmax, p[2]+p[3]*zmax, zmax);
Vector p3(x0+dxdz*zmin, y0+dydz*zmin, zmin);
Vector p4(x0+dxdz*zmax, y0+dydz*zmax, zmax);
Vector p5,p6;
Line l1(p1,p2), l2(p3,p4);
dca=distance(l1,l2,p5,p6); //3d DCA length
z=p6.z; //z of DCA point on beamline constrain
//printf("BEAM=%8.2f %8.2f %8.2f FGT=%8.2f %8.2f %8.2f\n",p6.x,p6.y,p6.z,p5.x,p5.y,p5.z);
}
inline StPhysicalHelix getStPhysicalHelix(double x0, double y0, double curve, double dip, double slp){
//printf("x0=%8.4f y0=%8.4f curv=%8.4f dip=%8.4f slp=%8.4f -> ",x0,y0,curve,dip,slp);
StThreeVectorD o(x0,y0,0);
int sign=int(curve/fabs(curve));
double phase=slp - sign*PI/2.0;
StPhysicalHelix h(fabs(curve),dip,phase,o,sign);
//printf("GetHelix c=%10.7f sign=%2d h=%2d\n",curve,sign,h.h());
//StThreeVectorD t1=h.at(0.0);
//printf(" x1=%8.4f y1=%8.4f z1=%8.4f \n",t1.x(),t1.y(),t1.z());
//StThreeVectorD t2=h.at(100.0);
//printf(" x2=%8.4f y2=%8.4f z2=%8.4f \n",t2.x(),t2.y(),t2.z());
//StThreeVectorD t3=h.at(200.0);
//printf(" x3=%8.4f y3=%8.4f z3=%8.4f \n",t3.x(),t3.y(),t3.z());
return h;
};
inline StPhysicalHelix getStPhysicalHelix(double *p) {return getStPhysicalHelix(p[0],p[1],p[2],p[3],p[4]);}
inline StPhysicalHelix getStPhysicalHelix(double x0, double y0, double dxdz, double dydz, double curve, int sign) {
double dr=sqrt(dxdz*dxdz+dydz*dydz);
double dip = atan2(1.0,dr);
double slp = atan2(dydz,dxdz);
return getStPhysicalHelix(x0,y0,curve*sign,dip,slp);
}
void getZvtxAndDcaFromHelix(double* par, double& dca, double& z, double &eta, double &phi, double& opt){
St_vertexSeedC* vSeed = St_vertexSeedC::instance();
//beamline
//double xyz[3]={vSeed->x0(),vSeed->y0(), 0.0};
//double dir[3]={vSeed->dxdz(),vSeed->dydz(),1.0};
//if(mFgtInputRPhi==0){ xyz[0]=0.0; xyz[1]=0.0; xyz[2]=0.0; dir[0]=0.00001; dir[1]=0.00001; dir[2]=1;}
//THelixTrack bb(xyz,dir,0.0000000001);
//bb.Move(0.0); printf(" BLT x1=%8.4f y1=%8.4f z1=%8.4f \n",bb.Pos()[0],bb.Pos()[1],bb.Pos()[2]);
//bb.Move(100.0); printf(" BLT x2=%8.4f y2=%8.4f z2=%8.4f \n",bb.Pos()[0],bb.Pos()[1],bb.Pos()[2]); bb.Move(-100);
//bb.Move(z); printf(" BLT x3=%8.4f y3=%8.4f z3=%8.4f \n",bb.Pos()[0],bb.Pos()[1],bb.Pos()[2]); bb.Move(-z);
//track
double xyz2[3]={par[0], par[1], 0.0};
double dir2[3]={cos(par[4])/tan(par[3]),sin(par[4])/tan(par[3]),1.0};
THelixTrack hh(xyz2,dir2,par[2]);
//hh.Move(0.0); printf(" TRT x1=%8.4f y1=%8.4f z1=%8.4f \n",hh.Pos()[0],hh.Pos()[1],hh.Pos()[2]);
//hh.Move(100.0); printf(" TRT x2=%8.4f y2=%8.4f z2=%8.4f \n",hh.Pos()[0],hh.Pos()[1],hh.Pos()[2]); hh.Move(-100);
//hh.Move(z); printf(" TRT x3=%8.4f y3=%8.4f z3=%8.4f \n",hh.Pos()[0],hh.Pos()[1],hh.Pos()[2]); hh.Move(-z);
//get DCA
double x0=0, y0=0, z0=0;
if(mFgtInputRPhi>0){ x0=vSeed->x0(); y0=vSeed->y0();}
double ss3=0;
for(int i=0; i<5; i++){
ss3=hh.Path(x0,y0); //closest point to x0,y0 line
hh.Move(ss3);
z0=hh.Pos()[2];
if(mFgtInputRPhi>0){
x0=vSeed->x0()+vSeed->dxdz()*z0;
y0=vSeed->y0()+vSeed->dydz()*z0;
}
double x=hh.Pos()[0], y=hh.Pos()[1], z=hh.Pos()[2];
double dx=x-x0, dy=y-y0, dz=z-z0;
dca=sqrt(dx*dx+dy*dy+dz*dz);
//printf("length=%8.4f x0=%8.4f y0=%8.4f z0=%8.4f : x=%8.4f y=%8.4f z=%8.4f dca=%8.4f\n",ss3,x0,y0,z0,hh.Pos()[0],hh.Pos()[1],hh.Pos()[2],dca);
hh.Move(-ss3);
}
z=z0; //z of DCA point on beamline constrain
double bfield=0.5*tesla;
StPhysicalHelix h = getStPhysicalHelix(par);
StThreeVectorD p = h.momentumAt(ss3,bfield);
//StThreeVectorD x = h.at(ss3);
//printf("ST x=%8.4f y=%8.4f z=%8.4f\n",x.x(),x.y(),x.z());
int sign=h.charge(bfield);
eta=p.pseudoRapidity();
phi=p.phi();
opt=sign/p.mag();
/*doesn't seems to work
//make StPhysicalHelix from fitted parameters
StPhysicalHelix h = getStPhysicalHelix(par);
//make StPhysicalHelix from beamLine with very small curvature
StPhysicalHelix b;
if(mFgtInputRPhi==0){
b = getStPhysicalHelix(0,0,0.000001,0.000001,0.0000000001,1);
}else{
b = getStPhysicalHelix(vSeed->x0(),vSeed->y0(),vSeed->dxdz(),vSeed->dydz(),0.0000000001,1);
}
StThreeVectorD t1=b.at(0.0);
printf(" BL x1=%8.4f y1=%8.4f z1=%8.4f \n",t1.x(),t1.y(),t1.z());
StThreeVectorD t2=b.at(100.0);
printf(" BL x2=%8.4f y2=%8.4f z2=%8.4f \n",t2.x(),t2.y(),t2.z());
StThreeVectorD t3=b.at(z);
printf(" BL x3=%8.4f y3=%8.4f z3=%8.4f \n",t3.x(),t3.y(),t3.z());
StThreeVectorD s1=h.at(0.0);
printf(" TR x1=%8.4f y1=%8.4f z1=%8.4f \n",s1.x(),s1.y(),s1.z());
StThreeVectorD s2=h.at(100.0);
printf(" TR x2=%8.4f y2=%8.4f z2=%8.4f \n",s2.x(),s2.y(),s2.z());
StThreeVectorD s3=h.at(z);
printf(" TR x3=%8.4f y3=%8.4f z3=%8.4f d=%8.4f\n",s3.x(),s3.y(),s3.z(),abs(s3-t3));
//get DCA
pair<double, double> d=b.pathLengths(h);
printf("path %8.4f %8.4f\n",d.first,d.second);
StThreeVectorD d1 = b.at(d.first);
StThreeVectorD d2 = h.at(d.second);
StThreeVectorD d3 = d1-d2;
dca=d3.mag(); //3d dca
z=d2.z(); //z of DCA point on beamline constrain
double bfield=0.5*tesla;
StThreeVectorD p = h.momentumAt(d.second,bfield);
int sign=h.charge(bfield);
eta=p.pseudoRapidity();
phi=p.phi();
opt=sign/p.mag();
//printf("getZvtxAndDcaFromHelix c=%10.6f sign=%2d h=%2dcurv=%10.6f mag=%10.6f opt=%10.6f\n",par[2],sign,h.h(),h.curvature(),p.mag(), opt);
*/
}
//Move fgt hits with alignment parameter
void getAlign(int itrk, fgtAlignment_st* algpar){
memcpy(&mHitAlg,&mHit[mQuad][itrk],sizeof(mHitAlg));
if(mFgtInputRPhi==0) return; //skip alignment for fake data
if(mDebug>3) printf("getAlign quad=%1d trk=%d mHitAlg.nhit=%d %d\n",mQuad,itrk,mHitAlg.nhit,mHit[mQuad][itrk].nhit);
for(int i=0; i<mHitAlg.nhit; i++){
if(mHitAlg.det[i]<24){
double r=mHitAlg.lr[i];
double phi=mHitAlg.lp[i];
int disc=mHitAlg.det[i]/4;
int quad=mHitAlg.det[i]%4;
TVector3 xyz,gxyz;
mDb->getStarXYZ(disc,quad,r,phi,xyz,2,algpar); //fgt internal alignment
//printf("y local=%8.2f\n",xyz.Y());
if(gStTpcDb){
double local[3]={0,0,0}, global[3]={0,0,0};
xyz.GetXYZ(local);
TGeoHMatrix globalMatrix = gStTpcDb->Tpc2GlobalMatrix(); //TPC global offsets
globalMatrix.LocalToMaster(local,global);
gxyz.SetXYZ(global[0],global[1],global[2]);
//printf("FGT local %9.6f %9.6f %9.6f\n",local[0],local[1],local[2]);
//printf("FGT globl %9.6f %9.6f %9.6f\n",global[0],global[1],global[2]);
//globalMatrix.Print();
}else{
static int nmess=0;
if(nmess<100){
printf("StFgtAlignmentMaker::Make could not get gStTpcDb... global xyz is same as fgt local xyz\n");
nmess++;
}
gxyz=xyz;
}
mHitAlg.x[i]=gxyz.X();
mHitAlg.y[i]=gxyz.Y();
mHitAlg.z[i]=gxyz.Z();
mHitAlg.r[i]=sqrt(mHitAlg.x[i]*mHitAlg.x[i]+mHitAlg.y[i]*mHitAlg.y[i]);
mHitAlg.p[i]=atan2(mHitAlg.y[i],mHitAlg.x[i]);
//printf("after y=%8.2f\n",mHitAlg.y[i]);
}
if(mDebug>3) printf("mHit[%3d] x=%8.3f y=%8.3f z=%8.3f det=%2d\n",i,
mHitAlg.x[i],mHitAlg.y[i],mHitAlg.z[i],mHitAlg.det[i]);
}
}
//fill residual histos
void fillHist(int disc, int quad, float dx, float dy, float dr, float dp, float x, float y, float r, float p){
hist1[disc][quad][0]->Fill(dx);
hist1[disc][quad][1]->Fill(dy);
hist1[disc][quad][2]->Fill(dr);
hist1[disc][quad][3]->Fill(dp);
hist2[disc][quad][0]->Fill(x,dx);
hist2[disc][quad][1]->Fill(x,dy);
hist2[disc][quad][2]->Fill(r,dr);
hist2[disc][quad][3]->Fill(r,dp);
hist2[disc][quad][4]->Fill(y,dx);
hist2[disc][quad][5]->Fill(y,dy);
hist2[disc][quad][6]->Fill(p,dr);
hist2[disc][quad][7]->Fill(p,dp);
if(mDebug>3) cout << Form("FillHist d=%1d q=%1d dx=%8.3f dy=%8.3f dr=%8.3f dp=%9.5f",
disc,quad,dx,dy,dr,dp) <<endl;
//if(disc>6) cout << Form("FillHist d=%1d q=%1d dx=%8.3f dy=%8.3f dr=%8.3f dp=%9.5f",
// disc,quad,dx,dy,dr,dp) <<endl;
}
//event display
void eventDisplay(double *par){
//setup helix track
StPhysicalHelix h = getStPhysicalHelix(par);
static StThreeVectorD n(0,0,1);
//set up TGraphs
TGraph *hitxz=new TGraph(mHitAlg.nhit); hitxz->SetMarkerStyle(21); hitxz->SetMarkerSize(0.4); hitxz->SetMarkerColor(kBlue);
TGraph *hityz=new TGraph(mHitAlg.nhit); hityz->SetMarkerStyle(21); hityz->SetMarkerSize(0.4); hityz->SetMarkerColor(kBlue);
TGraph *hitpr=new TGraph(mHitAlg.nhit); hitpr->SetMarkerStyle(21); hitpr->SetMarkerSize(0.4); hitpr->SetMarkerColor(kBlue);
//Plot hits
for(int i=0; i<mHitAlg.nhit; i++){
StThreeVectorD z(0,0,mHitAlg.z[i]);
double s=h.pathLength(z,n);
double x=h.x(s);
double y=h.y(s);
double dx = x - mHitAlg.x[i];
double dy = y - mHitAlg.y[i];
//double dr = sqrt(x*x+y*y) - sqrt(mHitAlg.x[i]*mHitAlg.x[i]+mHitAlg.y[i]*mHitAlg.y[i]);
double dp = atan2(y,x) - atan2(mHitAlg.y[i],mHitAlg.x[i]);
if(dp>PI) dp-=2*PI;
if(dp<-PI) dp+=2*PI;
double hx=mHitAlg.x[i];
double hy=mHitAlg.y[i];
double hz=mHitAlg.z[i];
double hr=sqrt(hx*hx+hy*hy);
//double hp=atan2(hy,hx);
hitxz->SetPoint(i,hz,dx);
hityz->SetPoint(i,hz,dy);
if(fabs(dp)<0.1) hitpr->SetPoint(i,hr,dp);
//if(mDebug>3) printf("residHelix %3d dx=%12.8f f=%12.8\n",i,dx,dy,f);
}
TCanvas *c1=new TCanvas("EventDisp","Event Disp",0,0,800,800);
c1->Divide(2,2);
c1->cd(1); hitxz->Draw("AP");
c1->cd(2); hityz->Draw("AP");
c1->cd(3); hitpr->Draw("AP");
c1->Update();
cin.get();
}
//Helix fit, called from TMinuit
void fitHelix(Int_t &npar, Double_t* gin, Double_t &f, Double_t *par, Int_t iflag){
if(mDebug>4) printf("fitHelix nhit=%d\n",mHitAlg.nhit);
f=0;
StPhysicalHelix h = getStPhysicalHelix(par);
static StThreeVectorD n(0,0,1);
int ifvtx=0;
int ndf=0;
for(int i=0; i<mHitAlg.nhit; i++){
if(mHitAlg.use[i]==false) continue;
if(mHitAlg.det[i]/4==6) ifvtx=1;
StThreeVectorD z(0,0,mHitAlg.z[i]);
double s=h.pathLength(z,n);
double dx = h.x(s) - mHitAlg.x[i];
double dy = h.y(s) - mHitAlg.y[i];
f += (dx*dx+dy*dy)/(mHitAlg.ex[i]*mHitAlg.ex[i]+mHitAlg.ey[i]*mHitAlg.ey[i]);
ndf+=2;
if(mDebug>5) printf("Helix1 Curve=%12.9f x=%8.3f y=%8.3f dx=%8.4f dy=%8.4f f=%8.4fe\n",par[2],h.x(s),h.y(s),dx,dy,f);
}
if(ifvtx==0 && mBeamLine==1){
double z,dca,eta,phi,opt;
getZvtxAndDcaFromHelix(par,dca,z,eta,phi,opt);
f += dca*dca/mErrVTX/mErrVTX;
ndf++;
}
ndf-=5;
if(ndf<=0) printf("ERROR fitHelix ndf=%d\n",ndf);
f/=double(ndf);
if(mDebug>4) printf("fitHelix x0=%12.8f y0=%12.8f c=%12.8f dip=%12.8f phase=%12.8f f=%12.8f\n",
par[0],par[1],par[2],par[3],par[4],f);
}
//Get residuals with helix fit
double residHelix(double *par){
double f=0, chi2=0;
StPhysicalHelix h = getStPhysicalHelix(par);
static StThreeVectorD n(0,0,1);
int ifvtx=0, ifvtx2=0;
int ndf=0,ndf2=0;
for(int i=0; i<mHitAlg.nhit; i++){
StThreeVectorD z(0,0,mHitAlg.z[i]);
double s=h.pathLength(z,n);
double x=h.x(s);
double y=h.y(s);
double dx = mHitAlg.x[i] - x;
double dy = mHitAlg.y[i] - y;
int disc=mHitAlg.det[i]/4;
double ff=(dx*dx+dy*dy)/(mHitAlg.ex[i]*mHitAlg.ex[i]+mHitAlg.ey[i]*mHitAlg.ey[i]);
if(mResidMaskDisc & (1<<disc)) {
if(disc==6) ifvtx=1;
f += ff;
ndf+=2;
}
if(mHitMaskDisc & (1<<disc)) {
if(disc==6) ifvtx2=1;
chi2 += ff;
ndf2+=2;
}
if(mDebug>3) printf("residHelix %3d dx=%12.8f dy=%12.8f f=%12.8f\n",i,dx,dy,f);
if(mFillHist>0) {
double r=sqrt(x*x+y*y);
double phi=atan2(y,x);
double hx=mHitAlg.x[i];
double hy=mHitAlg.y[i];
double hr=mHitAlg.r[i];
double hp=mHitAlg.p[i];
double dr = hr-r;
double dp = hp-phi;
//if(disc>8) printf("EEMCDR n=%2d used=%d %8.4f ->%8.4f\n",mHitAlg.nhitEemc,mHitAlg.used,mHitAlg.dr[i],dr);
while(dp>PI) dp-=2*PI;
while(dp<-PI) dp+=2*PI;
mHitAlg.dx[i]=dx;
mHitAlg.dy[i]=dy;
mHitAlg.dr[i]=dr;
mHitAlg.dp[i]=dp;
int quad=mHitAlg.det[i]%4;
if(disc<kFgtNumDiscs){
while(phi>StFgtGeom::phiQuadXaxis(2)) phi-=2*PI;
while(phi<StFgtGeom::phiQuadXaxis(1)) phi+=2*PI;
}
fillHist(disc,quad,dx,dy,dr,dp,x,y,r,phi);
}
}
if(ifvtx+ifvtx2<2 && mBeamLine==1){
double z,dca,eta,phi,opt;
getZvtxAndDcaFromHelix(par,dca,z,eta,phi,opt);
if(ifvtx==0) {f += dca*dca/mErrVTX/mErrVTX; ndf++; }
if(ifvtx2==0) {chi2 += dca*dca/mErrVTX/mErrVTX; ndf2++;}
}
ndf-=5;
if(ndf<=0) printf("ERROR resudHelix ndf=%d\n",ndf);
f/=double(ndf);
if(mFillHist>0) {
ndf2-=5;
if(ndf2<=0) {printf("ERROR residHelix ndf2=%d\n",ndf2);}
else {chi2/=double(ndf2);}
double z,dca,eta,phi,opt;
getZvtxAndDcaFromHelix(par,dca,z,eta,phi,opt);
if(mDebug>0) printf("UPDATE Dca=%8.4f->%8.4f trkz=%8.4f->%8.4f chi2=%8.4f->%8.4f ndf=%d\n",mHitAlg.dca,dca,mHitAlg.trkz,z,mHitAlg.chi2,chi2,ndf2);
mHitAlg.dca=dca;
mHitAlg.dz=mHitAlg.dz-mHitAlg.trkz+z;
mHitAlg.trkz=z;
mHitAlg.chi2=chi2;
mHitAlg.eta=eta;
mHitAlg.phi=phi;
mHitAlg.opt=opt;
}
if(mFillHist>1 && mEventDisp) eventDisplay(par);
if(mDebug>2)
printf("residHelix x0=%12.8f y0=%12.8f c=%12.8f dip=%12.8f phase=%12.8f f=%12.8f\n",
par[0],par[1],par[2],par[3],par[4],f);
return f;
}
//a track fit to helix
void fitTrackHelix(fgtAlignment_st* algpar, int itrk, double *par){
int flag;
static double arg[10];
static TMinuit *m = 0;
if(m==0){
m=new TMinuit(5);
m->SetFCN(fitHelix);
arg[0] =-1; m->mnexcm("SET PRI", arg, 1,flag);
arg[0] =-1; m->mnexcm("SET NOWarning", arg, 0,flag);
arg[0] = 1; m->mnexcm("SET ERR", arg, 1,flag);
arg[0] = 500; arg[1] = 1.;
}
//make "aligned" hits in mHitAlg
getAlign(itrk,algpar);
//first guess of helix fit parameters and set up
double dx = mHitAlg.x[1]-mHitAlg.x[0];
double dy = mHitAlg.y[1]-mHitAlg.y[0];
double dz = mHitAlg.z[1]-mHitAlg.z[0];
double dr = sqrt(dx*dx+dy*dy);
double dip = atan2(dz,dr);
double slp = atan2(dy,dx);
double x0= mHitAlg.x[0]-dx/dz*mHitAlg.z[0];
double y0= mHitAlg.y[0]-dy/dz*mHitAlg.z[0];
m->mnparm(0,"x0" ,x0 ,0.1, 0, 0,flag);
m->mnparm(1,"y0" ,y0 ,0.1, 0, 0,flag);
m->mnparm(2,"curv",0.0001 ,1.0, 0, 0,flag);
m->mnparm(3,"dip" ,dip ,0.1, 0, 0,flag);
m->mnparm(4,"slp" ,slp ,0.1, 0, 0,flag);
//do helix fit
m->mnexcm("MIGRAD", arg ,2, flag);
double r,e;
for(int j=0; j<5; j++){ m->GetParameter(j,r,e); par[j]=r;}
}
//Minimize residuals with helix fit, called from TMinuit
void funcHelix(Int_t &npar, Double_t* gin, Double_t &f, Double_t *par, Int_t iflag){
if(mDebug>3) printf("funcHelix mQuad=%d mNtrk=%d\n",mQuad,mNtrk[mQuad]);
f=0;
fgtAlignment_st* algpar= (fgtAlignment_st*)par;
double p[5];
for(int itrk=0; itrk<mNtrk[mQuad]; itrk++){
if(mHit[mQuad][itrk].used==0) continue;
fitTrackHelix(algpar,itrk,p);
f+=residHelix(p); //add up residuals
if(mFillHist>0) {
memcpy(&mHit[mQuad][itrk],&mHitAlg,sizeof(mHitAlg));
//printf("mHitAlg.z[0]=%8.2f\n",mHitAlg.z[0]);
//printf("mHit[mQuad][itrk].z[0]=%8.2f\n",mHit[mQuad][itrk].z[0]);
}
}
if(mDebug>3) printf("funcHelix f=%12.6f xoff=%12.8f yoff=%12.8f\n",f,algpar->xoff[8],algpar->xoff[8]);
static int ncall[4]={0,0,0,0};
ncall[mQuad]++;
if(ncall[mQuad]%100==0) printf("funcHelix quad=%d ncall=%d f=%12.6f\n",mQuad,ncall[mQuad],f);
}
//Straight line fit, called from TMinuit
void fitLine(Int_t &npar, Double_t* gin, Double_t &f, Double_t *par, Int_t iflag){
if(mDebug>4) printf("fitLine nhit=%d\n",mHitAlg.nhit);
f=0;
int ifvtx=0;
int ndf=0;
for(int i=0; i<mHitAlg.nhit; i++){
if(mHitAlg.use[i]==false) continue;
if(mHitAlg.det[i]/4==6) ifvtx=1;
double dx = par[0] + par[1] * mHitAlg.z[i] - mHitAlg.x[i];
double dy = par[2] + par[3] * mHitAlg.z[i] - mHitAlg.y[i];
f += (dx*dx+dy*dy)/(mHitAlg.ex[i]*mHitAlg.ex[i]+mHitAlg.ey[i]*mHitAlg.ey[i]);
ndf+=2;
if(mDebug>5) printf("dx=%8.4f dy=%8.4f dr=%8.4f\n",dx,dy,f);
}
if(ifvtx==0 && mBeamLine==1){
double z,dca;
getZvtxAndDca(par,dca,z);
f += dca*dca/mErrVTX/mErrVTX;
ndf++;
}
ndf-=4;
if(ndf<=0) {printf("ERROR fitLine ndf=%d\n",ndf);}
else {f/=double(ndf);}
if(mDebug>4) printf("fitLine x0=%12.8f x1=%12.8f y0=%12.8f y1=%12.8f f=%12.8f\n",par[0],par[1],par[2],par[3],f);
}
//Get residuals with straight line fit
double residLine(double *par){
//printf("residLine mQuad=%d mNtrk=%d mFillHist=%d\n",mQuad,mNtrk[mQuad],mFillHist);
double f=0, chi2=0;
int ifvtx=0, ifvtx2=0;
int ndf=0, ndf2=0;
for(int i=0; i<mHitAlg.nhit; i++){
double x=par[0] + par[1] * mHitAlg.z[i];
double y=par[2] + par[3] * mHitAlg.z[i];
double dx = mHitAlg.x[i]-x;
double dy = mHitAlg.y[i]-y;
int disc=mHitAlg.det[i]/4;
if(mResidMaskDisc & (1<<disc)) {
f += (dx*dx+dy*dy)/(mHitAlg.ex[i]*mHitAlg.ex[i]+mHitAlg.ey[i]*mHitAlg.ey[i]);
ndf+=2;
}
if(mHitMaskDisc & (1<<disc)) {
chi2 += (dx*dx+dy*dy)/(mHitAlg.ex[i]*mHitAlg.ex[i]+mHitAlg.ey[i]*mHitAlg.ey[i]);
ndf2+=2;
}
if(mDebug>5) printf("residLine %3d dx=%12.8f dy=%12.8f dr=%8.2f\n",i,dx,dy,dx*dx+dy*dy);
if(mFillHist>0) {
double r=sqrt(x*x+y*y);
double phi=atan2(y,x);
double hx=mHitAlg.x[i];
double hy=mHitAlg.y[i];
double hr=mHitAlg.r[i];
double hp=mHitAlg.p[i];
double dr = hr-r;
double dp = hp-phi;
while(dp>PI) dp-=2*PI;
while(dp<-PI) dp+=2*PI;
mHitAlg.dx[i]=dx;
mHitAlg.dy[i]=dy;
mHitAlg.dr[i]=dr;
mHitAlg.dp[i]=dp;
int quad=mHitAlg.det[i]%4;
if(disc<kFgtNumDiscs){
while(phi>StFgtGeom::phiQuadXaxis(2)) phi-=2*PI;
while(phi<StFgtGeom::phiQuadXaxis(1)) phi+=2*PI;
}
fillHist(disc,quad,dx,dy,dr,dp,x,y,r,phi);
//if(disc==6 && fabs(dx)<0.01 && fabs(dy)<0.01){
// printf("VTX %3d q=%1d d=%1d trk=%6.4f %6.4f %6.4f %6.4f fgt=%6.4f %6.4f %6.4f %6.4f xyrp=%6.4f %6.4f %6.4f %6.4f dxyrp=%6.4f %6.4f %6.4f %6.4f\n",
// i,quad,disc,par[0],par[1],par[2],par[3],x,y,r,phi,hx,hy,hr,hp,dx,dy,dr,dp);
//}
//if(disc>6)
//printf("EEMC2 %3d q=%1d d=%1d trk=%6.2f %6.2f %6.2f %6.2f fgt=%6.1f %6.1f %6.1f %6.2f xyrp=%6.1f %6.1f %6.1f %6.2f dxyrp=%6.1f %6.1f %6.1f %6.2f\n",
// i,quad,disc,par[0],par[1],par[2],par[3],x,y,r,phi,hx,hy,hr,hp,dx,dy,dr,dp);
}
}
if(ifvtx+ifvtx2<2 && mBeamLine==1){
double z,dca;
getZvtxAndDca(par,dca,z);
if(ifvtx==0) {f += dca*dca/mErrVTX/mErrVTX; ndf++; }
if(ifvtx2==0) {chi2 += dca*dca/mErrVTX/mErrVTX; ndf2++;}
}
ndf-=4;
if(ndf<=0) printf("ERROR residLine ndf=%d\n",ndf);
f/=double(ndf);
if(mFillHist>0) {
ndf2-=4;
if(ndf2<=0) printf("ERROR residLine ndf2=%d\n",ndf2);
chi2/=double(ndf2);
//update DCA and Trkz
double dca,z;
getZvtxAndDca(par,dca,z);
if(mDebug>0) printf("UPDATE Dca=%8.4f->%8.4f trkz=%8.4f->%8.4f chi2=%8.4f->%8.4f ndf=%d\n",mHitAlg.dca,dca,mHitAlg.trkz,z,mHitAlg.chi2,chi2,ndf2);
mHitAlg.dca=dca;
mHitAlg.dz=mHitAlg.dz-mHitAlg.trkz+z;
mHitAlg.trkz=z;
mHitAlg.chi2=chi2;
double theta=atan2(sqrt(par[1]*par[1]+par[3]*par[3]),1.0);
mHitAlg.eta=-log(tan(theta/2.0)) ;
mHitAlg.phi=atan2(par[3],par[1]);
mHitAlg.opt=0.0;
}
if(mFillHist>1 && mEventDisp) eventDisplay(par);
if(mDebug>4) printf("residLine x0=%12.8f x1=%12.8f y0=%12.8f y1=%12.8f f=%12.8f\n",par[0],par[1],par[2],par[3],f);
return f;
}
//a track fit to line
void fitTrackLine(fgtAlignment_st* algpar, int itrk, double *par){
int flag;
static double arg[10];
static TMinuit *m = 0;
if(m==0){
m=new TMinuit(4);
m->SetFCN(fitLine);
arg[0] =-1; m->mnexcm("SET PRI", arg, 1,flag);
arg[0] =-1; m->mnexcm("SET NOWarning", arg, 0,flag);
arg[0] = 1; m->mnexcm("SET ERR", arg, 1,flag);
arg[0] = 500; arg[1] = 1.;
}
//make "aligned" hits in mHitAlg
getAlign(itrk,algpar);
//first guess of helix fit parameters and set up
double x1 = (mHitAlg.x[1]-mHitAlg.x[0])/(mHitAlg.z[1]-mHitAlg.z[0]);
double y1 = (mHitAlg.y[1]-mHitAlg.y[0])/(mHitAlg.z[1]-mHitAlg.z[0]);
double x0 = mHitAlg.x[0] - x1*mHitAlg.z[0];
double y0 = mHitAlg.y[0] - y1*mHitAlg.z[0];
m->mnparm(0,"x0",x0,0.1,0,0,flag);
m->mnparm(1,"x1",x1,0.1,0,0,flag);
m->mnparm(2,"y0",y0,0.1,0,0,flag);
m->mnparm(3,"y1",y1,0.1,0,0,flag);
//do Line fit
m->mnexcm("MIGRAD", arg ,2, flag);
double r,e;
for(int j=0; j<4; j++){ m->GetParameter(j,r,e); par[j]=r;}
}
//Minimize residuals with line fit, called from TMinuit
void funcLine(Int_t &npar, Double_t* gin, Double_t &f, Double_t *par, Int_t iflag){
//printf("funcLine mQuad=%d mNtrk=%d mFillHist=%d\n",mQuad,mNtrk[mQuad],mFillHist);
if(mDebug>3) printf("funcLine mQuad=%d mNtrk=%d\n",mQuad,mNtrk[mQuad]);
f=0;
fgtAlignment_st* algpar= (fgtAlignment_st*)par;
double p[4];
for(int itrk=0; itrk<mNtrk[mQuad]; itrk++){
if(mHit[mQuad][itrk].used==0) continue;
fitTrackLine(algpar,itrk,p); //track fit
f+=residLine(p); //add up residuals
if(mFillHist>0) {
memcpy(&mHit[mQuad][itrk],&mHitAlg,sizeof(mHitAlg));
//printf("mHitAlg.z[0]=%8.2f\n",mHitAlg.z[0]);
//printf("mHit[mQuad][itrk].z[0]=%8.2f\n",mHit[mQuad][itrk].z[0]);
}
}
static int ncall[4]={0,0,0,0};
ncall[mQuad]++;
if(ncall[mQuad]%100==0) printf("funcLine quad=%d ncall=%d f=%12.6f\n",mQuad,ncall[mQuad],f);
}
ClassImp(StFgtAlignmentMaker);
StFgtAlignmentMaker::StFgtAlignmentMaker(const Char_t *name) : StMaker(name),mEventCounter(0),
mErrFgt(0.02), mErrVtx(0.02), mErrVtxZ(1.0),
mErrTpcI(0.6), mErrTpcO(0.12),mErrTpcZ(0.12),mErrPpt(0.1),mErrEmc(0.3),
mFakeNtrk(2000),mFakeEmin(40),mFakeEmax(40),mFakeEtamin(1.6),mFakeEtamax(1.6),
mFakePhimin(0),mFakePhimax(0),mFakeVtxSig(0),
mDataSource(0),
mOutTreeFile(0),mInTreeFile(0),mReadParFile(0),
mRunNumber(0),mSeqNumber(0),mDay(0),mNStep(0){
memset(mDzCut ,0,sizeof(mDzCut ));
memset(mDcaCut ,0,sizeof(mDcaCut ));
memset(mFgtRCut,0,sizeof(mFgtRCut));
memset(mFgtPCut,0,sizeof(mFgtPCut));
memset(mTpcRCut,0,sizeof(mTpcRCut));
memset(mTpcPCut,0,sizeof(mTpcPCut));
memset(mEmcRCut,0,sizeof(mEmcRCut));
memset(mEmcPCut,0,sizeof(mEmcPCut));
}
Int_t StFgtAlignmentMaker::Init(){
memset(mNtrk,0,sizeof(mNtrk));
memset(mHit,0,sizeof(mHit));
bookHist();
return kStOK;
}
Int_t StFgtAlignmentMaker::InitRun(Int_t runnum){
LOG_INFO << "StFgtAlignmentMaker::InitRun for " << runnum << endm;
StFgtDbMaker *fgtDbMkr = static_cast< StFgtDbMaker* >( GetMakerInheritsFrom( "StFgtDbMaker" ) );
//StFgtDbMaker *fgtDbMkr = static_cast<StFgtDbMaker * >( GetMaker("fgtDb"));
if( !fgtDbMkr ){
LOG_FATAL << "StFgtDb not provided and error finding StFgtDbMaker" << endm;
return kStFatal;
} else {
mDb = fgtDbMkr->getDbTables();
if( !mDb ){
LOG_FATAL << "StFgtDb not provided and error retrieving pointer from StFgtDbMaker '"
<< fgtDbMkr->GetName() << endm;
return kStFatal;
}
}
if(mReadParFile==0){
cout << "mDb="<<mDb<<endl;
orig_algpar=mDb->getAlignment();
}else{
readPar(orig_algpar);
}
return kStOK;
}
static const int mMaxStep=100;
void StFgtAlignmentMaker::setStep(int discmask,int quadmask, int parmask, int hitmask_disc, int residmask,
int trackType, int minHit, int minFgtHit, int minVtx, int minTpcHit, int minPromptHit, int minEemcHit,
float dzcut, float dcacut, float fgtrcut, float fgtpcut, float tpcrcut, float tpcpcut, float emcrcut, float emcpcut){
if(mNStep>=mMaxStep) {printf("Reached MaxStep\n"); return; }
if(mNStep==0) { printf("Step0 is making before histo, and masks are set to 0\n"); discmask=0; quadmask=0; parmask=0; }
mDiscMask[mNStep]=discmask;
mQuadMask[mNStep]=quadmask;
mParMask[mNStep]=parmask;
mHitMask[mNStep]=hitmask_disc;
mResidMask[mNStep]=residmask;
mTrackType[mNStep]=trackType;
mMinHit[mNStep]=minHit;
mMinFgtHit[mNStep]=minFgtHit;
mMinVtx[mNStep]=minVtx;
mMinTpcHit[mNStep]=minTpcHit;
mMinPromptHit[mNStep]=minPromptHit;
mMinEemcHit[mNStep]=minEemcHit;
mDzCut[mNStep]=dzcut;
mDcaCut[mNStep]=dcacut;
mFgtRCut[mNStep]=fgtrcut;
mFgtPCut[mNStep]=fgtpcut;
mTpcRCut[mNStep]=tpcrcut;
mTpcPCut[mNStep]=tpcpcut;
mEmcRCut[mNStep]=emcrcut;
mEmcPCut[mNStep]=emcpcut;
printf("Adding step=%d with discMask=%x quadMask=%x parMask=%x hitMask=%x trkType=%d minHit=%d minFgt=%d minTpc=%d minPrompt=%d minEemc=%d\n",
mNStep,discmask,quadmask,parmask,hitmask_disc,trackType,minHit,minFgtHit,minTpcHit,minPromptHit,minEemcHit);
printf(" Cuts dz=%8.3f dca=%8.3f fgtdr=%8.3f fgtdp=%8.3f tpcdr=%8.3f tpcdp=%8.3f emcdr=%8.3f emcdp=%8.3f\n",
mDzCut[mNStep],mDcaCut[mNStep],mFgtRCut[mNStep],mFgtPCut[mNStep],mTpcRCut[mNStep],mTpcPCut[mNStep],mEmcRCut[mNStep],mEmcPCut[mNStep]);
mNStep++;
}
Int_t StFgtAlignmentMaker::Make() {
orig_algpar=mDb->getAlignment();
mErrFGT=mErrFgt;
mErrVTX=mErrVtx;
mErrVTXZ=mErrVtxZ;
mErrTPCI=mErrTpcI;
mErrTPCO=mErrTpcO;
mErrTPCZ=mErrTpcZ;
mErrPPT=mErrPpt;
mErrEMC=mErrEmc;
if(mDataSource==0){
readFromStEvent();
}else if(mDataSource==1){
readFromStEventGlobal();
}else if(mDataSource==2){
readFromStraightTrackMaker();
DispFromStraightTrackMaker();
}else if(mDataSource==5){
calcETBalanceFromStEvent();
readFromStraightTrackAndStEvent();
}else if(mDataSource==6){
readFromStraightTrackAndMudst();
}
return kStOK;
}
Int_t StFgtAlignmentMaker::Finish() {
gMessMgr->Info() << "StFgtAlignmentMaker::Finish()" << endm;
if(mDataSource==4) {fakeData();}
else if(mDataSource==3) {readFromTree();}
overWriteError();
fgtAlignment_st result;
memcpy(&result,orig_algpar,sizeof(fgtAlignment_st));
cout << "Doing Alignment with Number of steps = "<<mNStep<<endl;
for(int s=0; s<mNStep; s++){
mStep=s;
if(mDiscMask[s]+mParMask[s]>0) {mFillHist=0;}
else {mFillHist=1; resetHist(); printf("Saving hist for step=%d\n",mStep);}
for(int quad=0; quad<kFgtNumQuads; quad++){
mQuad=quad;
int quadmask= 1<<quad;
if( (mFillHist>0) || (quadmask & mQuadMask[s]) ){
cout << Form("Doing alignment for quad=%1d with Ntrk=%4d",quad,mNtrk[quad])<<endl;
if(mNtrk[mQuad]>0){
printf("Quad=%d Step=%d with discMask=%x quadMask=%x parMask=%x hitMask=%x residMask=%x trkType=%d minHit=%d minFgt=%d vtx=%d minTpc=%d minPrompt=%d minEEmc=%d\n",
quad,s,mDiscMask[s],quadmask,mParMask[s],mHitMask[s],mResidMask[s],
mTrackType[s],mMinHit[s],mMinFgtHit[s],mMinVtx[s],mMinTpcHit[s],mMinPromptHit[s],mMinEemcHit[s]);
printf(" Cuts dz=%8.3f dca=%8.3f fgtdr=%8.3f fgtdp=%8.3f tpcdr=%8.3f tpcdp=%8.3f emcdr=%8.3f emcdp=%8.3f\n",
mDzCut[s],mDcaCut[s],mFgtRCut[s],mFgtPCut[s],mTpcRCut[s],mTpcPCut[s],mEmcRCut[s],mEmcPCut[s]);
doAlignment(&result,mDiscMask[s],quadmask,mParMask[s],mHitMask[s],mResidMask[s],
mTrackType[s],mMinHit[s],mMinFgtHit[s],mMinVtx[s],mMinTpcHit[s],mMinPromptHit[s],mMinEemcHit[s],&result);
}
}
}
if(mFillHist>0){
saveHist();
if(mOutTreeFile) writeTree();
}
}
writePar(&result);
return kStOK;
}
void StFgtAlignmentMaker::fakeData() {
mFgtInputRPhi=0;
orig_algpar=new fgtAlignment_st;
memset(orig_algpar,0,sizeof(fgtAlignment_st));
int quad=0;
mNtrk[quad]=0;
memset(mHit,0,sizeof(mHit));
int opt=1;
if(opt==0){
for(int itrk=0; itrk<1; itrk++){
mHit[quad][itrk].nhit=0;
for(int d=0; d<10; d++){
double z;
mHit[quad][itrk].nhit++;
if (d==0) {mHit[quad][itrk].det[d]=24+quad; z=0;}
else if(d>6) {mHit[quad][itrk].det[d]=28+quad; z=200.0+d;}
else{
mHit[quad][itrk].det[d]=(d-1)*4+quad;
z=StFgtGeom::getDiscZ(d-1);
}
mHit[quad][itrk].x[d]=20.0/67.399*(z-10); mHit[quad][itrk].ex[d]=0.1;
mHit[quad][itrk].y[d]=0.0; mHit[quad][itrk].ey[d]=0.1;
mHit[quad][itrk].z[d]=z; mHit[quad][itrk].ez[d]=0.1;
if(d==3) mHit[quad][itrk].x[d]+=0.2; // added mis-alignment
if(mDebug>0){
cout<<Form("Trk=%3d Hit=%3d Quad=%1d Det=%2d XYZ=%8.4f %8.4f %8.4f err=%8.4f %8.4f %8.4f",
itrk,d,quad,mHit[quad][itrk].det[d],
mHit[quad][itrk].x[d],mHit[quad][itrk].y[d],mHit[quad][itrk].z[d],
mHit[quad][itrk].ex[d],mHit[quad][itrk].ey[d],mHit[quad][itrk].ez[d])
<<endl;
}
}
mNtrk[quad]++;
}
}else{
//fake track inputs
mQuad=0;
TRandom rand;
int mFgt=1,mVtx=1,mTpc=0,mPpt=1,mEmc=1;
const double B = 0.5*tesla;
StThreeVectorD zvec(0,0,1);
//TPC and EEMC z positions
static const int NTPC=45;
double tpcr[NTPC];
for(int ipad= 0;ipad< 8; ipad++){tpcr[ipad]=60.0 + 4.8*ipad;}
for(int ipad= 8;ipad<13; ipad++){tpcr[ipad]=60.0 + 4.8*7 + 5.2*(ipad-8);}
for(int ipad=13;ipad<45; ipad++){tpcr[ipad]=127.950 + 2.0*(ipad-13);}
double zppt=209;
double zemc=280;
for(int itrk=0; itrk<mFakeNtrk; itrk++){
double ene=mFakeEmin;
double eta=mFakeEtamin;
double phi=mFakePhimin;
StThreeVectorD v(0,0,0);
if(mFakeEmax>mFakeEmin) ene=rand.Uniform(mFakeEmin,mFakeEmax);
if(mFakeEtamax>mFakeEtamin) eta=rand.Uniform(mFakeEtamin,mFakeEtamax);
if(mFakePhimax>mFakePhimin) phi=rand.Uniform(mFakePhimin,mFakePhimax);
if(mFakeVtxSig>0) {
double zz=-999;
while (zz>60 || zz<-120) {zz=rand.Gaus(0,mFakeVtxSig);}
v.setZ(zz);
}
//Making helix
double theta=2*atan(exp(-eta));
double pt=ene*sin(theta);
double pz=ene*cos(theta);
double px=ene*sin(theta)*cos(phi);
double py=ene*sin(theta)*sin(phi);
StThreeVectorD p(px, py, pz);
StPhysicalHelixD pos(p,v,B,1);
StPhysicalHelixD neg(p,v,B,-1);
if(itrk<3){
printf("FAKE pt=%8.3f eta=%8.3f phi=%8.3f ene=%8.3f\n",pt,eta,phi,ene);
printf("FAKE pxyz=%8.3f %8.3f %8.3f\n",px,py,pz);
printf("FAKE vxyz=%8.3f %8.3f %8.3f\n",v.x(),v.y(),v.z());
//print rough trajectory
for (double z=0; z<=300; z+=20) {
StThreeVectorD zplane(0,0,z);
double sp=pos.pathLength(zplane,zvec);
double sn=pos.pathLength(zplane,zvec);
StThreeVectorD pxyz = pos.at(sp);
StThreeVectorD nxyz = neg.at(sn);
printf("FAKE z=%8.3f pos=%8.3f %8.3f neg=%8.3f %8.3f\n",z,pxyz.x(),pxyz.y(),nxyz.x(),nxyz.y());
}
}
//make hits
StPhysicalHelixD trk;
if(itrk%2==0) {trk=pos;}
else {trk=neg;}
int nhit=0;
if(mVtx){
StThreeVectorD zplane(0,0,v.z());
double s=trk.pathLength(zplane,zvec);
StThreeVectorD xyz = trk.at(s);
mHit[mQuad][itrk].x[nhit]=xyz.x(); mHit[mQuad][itrk].y[nhit]=xyz.y(); mHit[mQuad][itrk].z[nhit]=xyz.z();
mHit[mQuad][itrk].ex[nhit]=mErrVtx; mHit[mQuad][itrk].ey[nhit]=mErrVtx; mHit[mQuad][itrk].ez[nhit]=mErrVtxZ;
mHit[mQuad][itrk].det[nhit]=4*6+mQuad;
nhit++;
if(itrk<5) printf("VTX %8.3f %8.3f %8.3f %8.4f %8.4f\n",xyz.x(),xyz.y(),xyz.z(),mErrVtx,mErrVtxZ);
}
mHit[mQuad][itrk].dz=0;
mHit[mQuad][itrk].trkz=v.z();
if(mFgt){
int nfgt=0;
for(int idisc=0; idisc<6; idisc++){
if(idisc==3) continue;
StThreeVectorD zplane(0,0,StFgtGeom::getDiscZ(idisc));