-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathfitQunDisplay.C
1354 lines (1240 loc) · 41.9 KB
/
fitQunDisplay.C
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
#define fitQunDisplay_cxx
// The class definition in fitQunDisplay.h has been generated automatically
// by the ROOT utility TTree::MakeSelector(). This class is derived
// from the ROOT class TSelector. For more information on the TSelector
// framework see $ROOTSYS/README/README.SELECTOR or the ROOT User Manual.
// The following methods are defined in this file:
// Begin(): called every time a loop on the tree starts,
// a convenient place to create your histograms.
// SlaveBegin(): called after Begin(), when on PROOF called only on the
// slave servers.
// Process(): called for each event, in this function you decide what
// to read and fill your histograms.
// SlaveTerminate: called at the end of the loop on the tree, when on PROOF
// called only on the slave servers.
// Terminate(): called at the end of the loop on the tree,
// a convenient place to draw/fit your histograms.
//
// To use this file, try the following session on your Tree T:
//
// Root > T->Process("fitQunDisplay.C")
// Root > T->Process("fitQunDisplay.C","some options")
// Root > T->Process("fitQunDisplay.C+")
//
#include <algorithm>
#include "fitQunDisplay.h"
#include <TH2.h>
#include <TStyle.h>
#include "THKGamma.h"
#include <iostream>
#include "TEveVector.h"
#include <TGLViewer.h>
using std::cout;
using std::endl;
void fitQunDisplay::Begin(TTree * /*tree*/)
{
// The Begin() function is called at the start of the query.
// When running with PROOF Begin() is only called on the client.
// The tree argument is deprecated (on PROOF 0 is passed).
TString option = GetOption();
}
void fitQunDisplay::SlaveBegin(TTree * /*tree*/)
{
// The SlaveBegin() function is called after the Begin() function.
// When running with PROOF SlaveBegin() is called on each slave server.
// The tree argument is deprecated (on PROOF 0 is passed).
TString option = GetOption();
}
void fitQunDisplay::describe_event(int entry)
{
cout<<endl<<endl<<" ================================================== "<<endl<<" Entry "<<entry<<endl<<" ================================================== "<<endl;
cout<<endl<<"Time-window information"<<endl
<<"-------------------------"<<endl;
cout<<fqntwnd<<" Number of time windows (good clusters) in this event"<<endl;
for(int ifqntwnd =0;ifqntwnd<fqntwnd;ifqntwnd++){
cout<<fqtwnd_iclstr[ifqntwnd]<<" Cluster index of the time window(corresponds to cluster_ncand)"<<endl;
cout<<fqtwnd_npeak[ifqntwnd]<<" Number of peaks(sub-events) in the time window"<<endl;
cout<<fqtwnd_prftt0[ifqntwnd]<<" Pre-fitter vertex time"<<endl;
cout<<fqtwnd_prftpos[ifqntwnd][0]<<" "<<fqtwnd_prftpos[ifqntwnd][1]<<" "<<fqtwnd_prftpos[ifqntwnd][2]<<" Pre-fitter vertex position"<<endl;
cout<<fqtwnd[ifqntwnd][0]<<fqtwnd[ifqntwnd][1]<<" Time window start/end time"<<endl;
for(int i = 0;i<fqntwnd;i++){
cout<<fqtwnd_peakt0[ifqntwnd][i]<<" Time of each sub-event in the time window"<<endl;
cout<<fqtwnd_peakiness[ifqntwnd][i]<<" Vertex goodness parameter evaluated at the peak position"<<endl;
}
}
cout<<endl<<"Sub-event information"<<endl
<<"-------------------------"<<endl;
cout<<fqnse<<" Total number of subevents in the event"<<endl;
for(int ifqnse = 0;ifqnse<fqnse;ifqnse++){
cout<<fqitwnd[ifqnse]<<" Index of the time window to which the subevent belongs"<<endl;
cout<<fqipeak[ifqnse]<<" Peak index within the time window"<<endl;
cout<<fqnhitpmt[ifqnse]<<" Number of hits in each subevent"<<endl;
cout<<fqtotq[ifqnse]<<" Total charge in each subevent"<<endl;
cout<<fq0rtotmu[ifqnse]<<" Total predicted charge for the 0-ring hypothesis in each subevent - these variables are the result of evaluating the likelihood function with a particle that is below Cherenkov threshold."<<endl;
cout<<fq0rnll[ifqnse]<<" -log(L) value for the 0-ring hypothesis in each subevent"<<endl;
cout<<fqn50[ifqnse]<<" n50 - In the TOF-corrected hit time distribution, number of hits within the 50ns time window centred at vertex time(1R electron fit vertex is used)"<<endl;
cout<<fqq50[ifqnse]<<" q50 - Total charge of hits included in n50 above"<<endl;
}
cout<<endl<<" Number of candidate clusters (subevents) "<<cluster_ncand<<endl;
for(int i=0;i<cluster_ncand;i++){
cout<<" Cluster start time (ns) "<<cluster_tstart[cluster_ncand]<<endl;
cout<<" Cluster end time (ns) "<<cluster_tend[cluster_ncand]<<endl;
cout<<" Number of hits included in cluster"<<cluster_nhits[cluster_ncand]<<endl;
cout<<" Total charge included in cluster "<<cluster_totq[cluster_ncand]<<endl;
}
cout<<endl<<" There are "<<fqnse<<" sub events "<<endl;
cout<<endl<<"1-ring fits"<<endl
<<"-------------"<<endl
<<"- These variables are the result of the 1-ring fits. The first index "<<endl
<<"is the subevent (1-ring fits are run on all subevents). The second"<<endl
<<"index is the particle-hypothesis index (same as apfit):"<<endl
<<" 0 = GAMMA, 1 = ELECTRON, 2 = MUON, 3 = PION, 4 = KAON, 5 = PROTON, "<<endl
<<"6 = CONE GENERATOR"<<endl
<<" Currently, only the electron, muon, and pion (the upstream pion "<<endl
<<"segement) hypotheses are implemented."<<endl;
for(int ifqnse=0;ifqnse<fqnse;ifqnse++)
{
for(int PID=1;PID<4;PID++)
{
cout<<" subevent "<<ifqnse<<" hypothesis "<<HYPO[PID]<<endl;
cout<<fq1rpcflg[ifqnse][PID]<<" Flag to indicate whether fiTQun believes the particle is exiting the ID(<0 if MINUIT did not converge)"<<endl;
cout<<fq1rmom[ifqnse][PID]<<" Fit momentum"<<endl;
cout<<fq1rt0[ifqnse][PID]<<" Fit particle creation time"<<endl;
cout<<fq1rtotmu[ifqnse][PID]<<" Best-fit total predicted charge"<<endl;
cout<<fq1rnll[ifqnse][PID]<<" Best-fit -lnL"<<endl;
cout<<fq1rpos[ifqnse][PID][0]<< " ";
cout<<fq1rpos[ifqnse][PID][1] + yOffset<< " ";
cout<<fq1rpos[ifqnse][PID][2]<< " ";
cout<<" Fit vertex (0=X, 1=Y, 2=Z)"<<endl;
cout<<fq1rdir[ifqnse][PID][0]<<" ";
cout<<fq1rdir[ifqnse][PID][1]<<" ";
cout<<fq1rdir[ifqnse][PID][2]<<" ";
cout<<" Fit direction (0=X, 1=Y, 2=Z)"<<endl;
cout<<fq1rdconv[ifqnse][PID]<<"Fit conversion length (always 0 for 1R fits)"<<endl;
cout<<fq1reloss[ifqnse][PID]<<" Energy lost in the upstream track segment before the hadronic interaction(for upstream tracks only)"<<endl;
float x=fq1rdir[ifqnse][PID][0];
float y=fq1rdir[ifqnse][PID][1];
float z=fq1rdir[ifqnse][PID][2];
float dl=sqrt(x*x+y*y+z*z);
float theta =acos(z/dl);
float phi =atan2(y,x);
cout<<" x "<<x<<" y "<<y<<" z "<<z<<" dl "<<dl<<" theta "<<theta<<" phi "<<phi<<endl;
}
}
cout<<endl<<" pi0 fit "<<endl
<<"-----------"<<endl;
cout<<"- Pi0 fits are only run on the first subevent. Index 0 gives the "<<endl
<<"standard, unconstrained pi0 fit. (Index 1 is not filled currently)"<<endl;
cout<<fqpi0pcflg[0]<<" (PCflg for photon 1) + 2*(PCflg for photon 2)"<<endl;
cout<<fqpi0mom1[0]<<"Fit momentum of first photon"<<endl;
cout<<fqpi0mom2[0]<<" Fit momentum of second photon"<<endl;
cout<<fqpi0momtot[0]<<" Fit momentum of the pi0"<<endl;
cout<<fqpi0dconv1[0]<<" Fit conversion length for the first photon"<<endl;
cout<<fqpi0dconv2[0]<<" Fit conversion length for the second photon"<<endl;
cout<<fqpi0t0[0]<<" Fit pi0 creation time"<<endl;
cout<<fqpi0totmu[0]<<" Best fit total predicted charge"<<endl;
cout<<fqpi0nll[0]<<" Best fit -log(Likelihood)"<<endl;
cout<<fqpi0mass[0]<<" Fit pi0 mass (always 134.9766 for constrained mass fit)"<<endl;
cout<<fqpi0photangle[0]<<" Fit opening angle between the photons"<<endl;
cout<<fqpi0pos[0][0]<<" ";
cout<<fqpi0pos[0][1] + yOffset<<" ";
cout<<fqpi0pos[0][2]<<" ";
cout<<" Fit vertex position"<<endl;
cout<<fqpi0dir1[0][0]<<" ";
cout<<fqpi0dir1[0][1]<<" ";
cout<<fqpi0dir1[0][2]<<" ";
cout<<" Fit direction of the first photon"<<endl;
cout<<fqpi0dir2[0][0]<<" ";
cout<<fqpi0dir2[0][1]<<" ";
cout<<fqpi0dir2[0][2]<<" ";
cout<<" Fit direction of the second photon"<<endl;
cout<<fqpi0dirtot[0][0]<<" ";
cout<<fqpi0dirtot[0][1]<<" ";
cout<<fqpi0dirtot[0][2]<<" ";
cout<<" Fit direction of the pi0"<<endl;
/*
Multi ring events
*/
cout<<endl<<"Multi-Ring Fits"<<endl
<<"----------------"<<endl
<<"- These are the results of the Multi-Ring(MR) fits. The number of "<<endl
<<"executed multi-ring fits depends on the event topology,and the first "<<endl
<<"index specifies different fit results.(Index 0 is the best-fit result.)"<<endl
<<"Each fit result is assigned a unique fit ID which tells the type of the"<<endl
<<"fit(see fiTQun.cc for more details):"<<endl;
cout<<endl<<" There are "<<fqnmrfit<<" multi ring fit restults "<<endl;
for(int ifqnmrfit=0;ifqnmrfit <fqnmrfit ; ifqnmrfit++)
{
cout<<fqmrifit[fqnmrfit]<<" Fit ID of MR Fit result "<<endl;
cout<<fqmrnring[fqnmrfit]<<" Number of rings for this fit [1-6]"<<endl;
cout<<fqmrpcflg[ifqnmrfit]<<" <0 if MINUIT did not converge during the fit"<<endl;
cout<<fqmrnll[ifqnmrfit]<<" Best-fit -lnL"<<endl;
cout<<fqmrtotmu[ifqnmrfit]<<" Best-fit total predicted charge"<<endl;
for(int nRing=0 ; nRing<fqmrnring[fqnmrfit]; nRing++){
cout<<fqmrpid[ifqnmrfit][nRing]<<" Particle type index for each ring in this fit (Same convention as in 1R fit)"<<endl;
cout<<fqmrmom[ifqnmrfit][nRing]<<" Fit momentum of each ring"<<endl;
cout<<fqmrdconv[ifqnmrfit][nRing]<<" Fit conversion length of each ring(always \"0\" in default mode)"<<endl;
cout<<fqmreloss[ifqnmrfit][nRing]<<" Energy lost in the upstream track segment(for upstream tracks only)"<<endl;
cout<<fqmrt0[ifqnmrfit][nRing]<<" Fit creation time of each ring"<<endl;
cout<<fqmrpos[ifqnmrfit][nRing][0]<<" "<<fqmrpos[ifqnmrfit][nRing][1] + yOffset<<" "<<fqmrpos[ifqnmrfit][nRing][2]<<" Fit vertex position of each ring"<<endl;
cout<<fqmrdir[ifqnmrfit][nRing][0]<<" "<<fqmrdir[ifqnmrfit][nRing][1]<<" "<<fqmrdir[ifqnmrfit][nRing][2]<<" Fit direction of each ring"<<endl;
}
}
cout<<" Event Listing Ends "<<endl;
}
Bool_t fitQunDisplay::Process(Long64_t entry)
{
std::cout << "HELLO" << std::endl;
fitQunDisplay::GetEntry(entry);
// useful for debug...describe_event(entry);
std::cout << "HELLO2" << std::endl;
fitQunResults= new TEveElementList("fitQun Results");
fitQunResults2D= new TEveElementList("fitQun Results (unrolled)");
load_event();
gEve->AddElement(fitQunResults);
TEveScene* UnrolledScene=NULL;
TEveSceneList* scenes = gEve->GetScenes();
for (TEveElement::List_i i=scenes->BeginChildren(); i!=scenes->EndChildren(); ++i)
{
TEveScene* scene=(TEveScene*)(*i);
if(!strcmp(scene->GetName(),"Unrolled Event")){
UnrolledScene = scene;
break;
}
}
UnrolledScene->AddElement(fitQunResults2D);
return kTRUE;
}
void fitQunDisplay::load_event()
{
/*
Add fitqun results to the Eve event display
*/
//float electronMass=0.510998;
//float muonMass=113.43;
//float pionMass=139.57;
//float kaonMass=493.677;
//float protonMass=938.272;
gEve->GetDefaultGLViewer()->UpdateScene();
for(int se=0;se< fqnse;se++)
{
TEveElementList* EveSubEvent = new TEveElementList(Form("Sub event %i ",se));
TEveElementList* EveSubEvent2D = new TEveElementList(Form("Sub event %i ",se));
fitQunResults->AddElement(EveSubEvent);
fitQunResults2D->AddElement(EveSubEvent2D);
for(int PID=1;PID<4;PID++)
{
if(fq1rpcflg[se][PID]>-1)
{
float x=fq1rdir[se][PID][0];
float y=fq1rdir[se][PID][1];
float z=fq1rdir[se][PID][2];
float dl=sqrt(x*x+y*y+z*z);
float theta =acos(z/dl);
float phi =atan2(y,x);
TString description=Form("Fitqun %s, sub event %i",HYPO[PID].Data(),se);
double pos[3];
pos[0]=fq1rpos[se][PID][0];
pos[1]=fq1rpos[se][PID][1] + yOffset;
pos[2]=fq1rpos[se][PID][2];
addTrack(pos,theta,phi,fq1rmom[se][PID],description,HYPERCOLOUR[PID],MASS[PID],EveSubEvent,EveSubEvent2D);
}
}
if(se == 0)
{
float x=fqpi0dir1[0][0];
float y=fqpi0dir1[0][1];
float z=fqpi0dir1[0][2];
float dl=sqrt(x*x+y*y+z*z);
float theta1 =acos(z/dl);
float phi1 =atan2(y,x);
x=fqpi0dir2[0][0];
y=fqpi0dir2[0][1];
z=fqpi0dir2[0][2];
dl=sqrt(x*x+y*y+z*z);
float theta2=acos(z/dl);
float phi2=atan2(y,x);
double pos[3];
pos[0]=fqpi0pos[0][0];
pos[1]=fqpi0pos[0][1] + yOffset;
pos[2]=fqpi0pos[0][2];
addPi0(pos,theta1,phi1,fqpi0mom1[0],theta2,phi2,fqpi0mom2[0],EveSubEvent,EveSubEvent2D);
}
}
}
void fitQunDisplay::addPi0(double pos[3],double theta1,double phi1,double mom1,double theta2,double phi2,double mom2,TEveElementList* se3d,TEveElementList* se2d )
{
THKLine* pi0Track=new THKLine("Fitqun pi0");
pi0Track->SetElementTitle("Fitqun pi0");
pi0Track->SetMainColor(kWhite);
pi0Track->SetNextPoint(pos[0],pos[1],pos[2]);
pi0Track->SetNextPoint(pos[0]+0.001,pos[1]+0.001,pos[2]+0.001);
se3d->AddElement(pi0Track);
TString Name("Fitqun Pi0->gamma #1");
THKGamma* gammaTrack=new THKGamma(Name);
gammaTrack->Create(Name,kWhite,pos,pScale*mom1,mom1,theta1,phi1);
pi0Track->AddElement(gammaTrack);
THKCerenkov* cone = new THKCerenkov(Name+" Cerenkov Cone");
THKCerenkov2D* cone2D = new THKCerenkov2D(Name+"Cerenkov cone");
createCerenkov(cone,cone2D,Name,kWhite,mom1,theta1,phi1,0.0,pos);
gammaTrack->AddElement(cone);
se2d->AddElement(cone2D);
Name=TString("Fitqun Pi0->gamma #2");
gammaTrack=new THKGamma(Name);
gammaTrack->Create(Name,kWhite,pos,pScale*mom2,mom2,theta2,phi2);
cone = new THKCerenkov(Name+"Cerenkov Cone");
cone2D = new THKCerenkov2D(Name+"Cerenkov cone");
createCerenkov(cone,cone2D,Name,kWhite,mom2,theta2,phi2,0.0,pos);
gammaTrack->AddElement(cone);
se2d->AddElement(cone2D);
pi0Track->AddElement(gammaTrack);
}
void fitQunDisplay::addTrack(double pos[3],double theta,double phi,double Momentum,TString Name,int colour,float Mass,TEveElementList* se3d, TEveElementList* se2d)
{
if(Momentum<=0)return;
THKLine* track=new THKLine(Name);
track->Create(Name,colour,pos,pScale*Momentum,Momentum,theta,phi);
THKCerenkov* cone = new THKCerenkov(Name+"Cerenkov Cone");
THKCerenkov2D* cone2D = new THKCerenkov2D(Name+"Cerenkov cone");
createCerenkov(cone,cone2D,Name,colour,Momentum,theta,phi,Mass,pos);
track->AddElement(cone);
se3d->AddElement(track);
se2d->AddElement(cone2D);
}
void fitQunDisplay::createCerenkov(THKCerenkov* cone, THKCerenkov2D* cone2D,
TString Name,Color_t colour,double Momentum,double theta,double phi,float Mass,double pos[3])
{
cone2D->SetValues(pos,Momentum,theta,phi);
//? double length=pScale*Momentum;
float length=1.0;
double px = Momentum*cos(phi)*sin(theta);
double py = Momentum*sin(phi)*sin(theta);
double pz = Momentum*cos(theta);
double E = sqrt(Momentum*Momentum+Mass*Mass);
double Beta = Momentum/E;
//cout<<" beta "<<beta;
double n=1.34;
double cosAlpha=1/(n*Beta);
/*
Construct a vector of points showing the cherenkov circle where it
impacts with the detector. An alternative is to draw the cones themselves
- great for publicity but not very practical
*/
cone->SetElementTitle(Name);
cone->SetMainColor(colour);
cone->SetLineWidth(2.0);
cone2D->SetElementTitle(Name);
cone2D->SetMainColor(colour);
cone2D->SetLineWidth(2.0);
// double ct = cos(theta);
// double st = sin(theta);
// double sf = sin(phi);
// double cf = cos(phi);
double azim=0;
int nPoints=1000;
double delta = 6.283/nPoints;
double alpha=acos(cosAlpha);
// double cScale=5.0;
float firstX=0.0;
float firstY=0.0;
float firstZ=0.0;
float firstX2D=0.0;
float firstY2D=0.0;
float firstZ2D=0.0;
float oldX2D=0.0;
float oldY2D=0.0;
float oldZ2D=0.0;
int oldlocation=-1;
maxTube=-1;
nextMaxTube=-1;
nextNextMaxTube=-1;
double endPoint[3];
bool first=true;
do
{
/*
Point x1,y1,z1 is a point on the Cerenkov cone, in the frame which
has Z along the track direction.
The cone is at an angle alpha w.r.t. the direction of the track.
*/
double x1=length*sin(alpha)*cos(azim);
double y1=length*sin(alpha)*sin(azim);
double z1=length*cos(alpha);
/*
Rotate into the detector frame
*/
Double_t u1 = px/Momentum;
Double_t u2 = py/Momentum;
Double_t u3 = pz/Momentum;
Double_t up = u1*u1 + u2*u2;
double x2=0.0;
double y2=0.0;
double z2=0.0;
if (up) {
up = TMath::Sqrt(up);
Double_t PX = x1, PY = y1, PZ = z1;
x2= (u1*u3*PX - u2*PY + u1*up*PZ)/up;
y2 = (u2*u3*PX + u1*PY + u2*up*PZ)/up;
z2 = (u3*u3*PX - PX + u3*up*PZ)/up;
}
else if (u3 < 0.)
{ x2 = -x1; z2 = -y1; } // phi=0 teta=pi
else {};
/*
Cerenkov photon starts at pos[...], in direction cerenkov and ends at endPoint
*/
double cerenkov[3];
cerenkov[0]=x2;
cerenkov[1]=y2;
cerenkov[2]=z2;
float step=delta;
int location;
if(FindConeEnd(pos,cerenkov,endPoint,location))
{
float rTest=sqrt(endPoint[0]*endPoint[0]+endPoint[2]*endPoint[2]);
if(rTest>maxR)
{
maxTube=-1;
}
else{
cone->SetNextPoint(endPoint[0],endPoint[1],endPoint[2]);
if(first==true){
firstX=endPoint[0];
firstY=endPoint[1];
firstZ=endPoint[2];
}
UnrollView(endPoint,location);
//cone2D->SetNextPoint(endPoint[0],endPoint[1],endPoint[2]);
if(!first){
length=sqrt(
(oldX2D-endPoint[0])*(oldX2D-endPoint[0])+
(oldY2D-endPoint[1])*(oldY2D-endPoint[1])+
(oldZ2D-endPoint[2])*(oldZ2D-endPoint[2]));
if(oldlocation==location && length < 1000)
cone2D->AddLine(oldX2D,oldY2D,oldZ2D,endPoint[0],endPoint[1],endPoint[2]);
}
oldX2D=endPoint[0];
oldY2D=endPoint[1];
oldZ2D=endPoint[2];
oldlocation=location;
if(first==true){
first=false;
firstX2D=endPoint[0];
firstY2D=endPoint[1];
firstZ2D=endPoint[2];
}
}
float r=sqrt(endPoint[0]*endPoint[0]+endPoint[2]*endPoint[2]);
float testy=endPoint[1];
if((maxY-abs(testy)<500)&&(maxR-r)<500)
{
step=delta/10.0;
}
}
azim+=step;
}
while(azim<6.283);
if(!first){
cone->SetNextPoint(firstX,firstY,firstZ);
cone2D->AddLine(endPoint[0],endPoint[1],endPoint[2],firstX2D,firstY2D,firstZ2D);
}
}
void fitQunDisplay::UnrollView(double position[3],int location)
{
double pmtX=position[0];
double pmtY=position[1];
double pmtZ=position[2];
if(location==0)
{
// cout<<" add 2* "<<maxY<<" to "<<*pmtY<<endl;
pmtY = pmtX + 1.05*maxX + maxY;
pmtX = pmtZ;
//cout<<" result is "<<*pmtY<<endl;
}
if(location==2)
{// cout<<" subtract 2* "<<maxY<<" from "<<*pmtY<<endl;
pmtY = pmtX - 1.05*maxX - maxY;
pmtX = pmtZ;
}
if(location==1)
{
float angle=atan2(pmtX,pmtZ);//+(3.1415927/2.0);
float rho=maxZ*angle;
pmtX=rho;
}
pmtZ=0.0;
//cout<<" x,y,z of Unrolled phototube "<<*pmtX<<" "<<*pmtY<<" "<<*pmtZ<<endl;
float xshift=maxZ*(3.1415927);
float x=pmtX;
if(x>xshift)x=x-(xshift*2);
pmtX=x;
//cout<<" x,y,z of Unrolled phototube "<<*pmtX<<" "<<*pmtY<<" "<<*pmtZ<<endl;
position[0]=pmtX;
position[1]=pmtY;
position[2]=pmtZ;
return;
}
int fitQunDisplay::xyIndex(float xOry)
{
int nXYvalues=200;
return nXYvalues*(maxR+xOry)/(2*maxR);
}
int fitQunDisplay::zIndex(float z)
{
int indexMin= ((nYvalues)*(minY/(maxY-minY)));
int index = ((nYvalues)*(z/(maxY-minY)))-indexMin;
return index;
}
int fitQunDisplay::phiIndex(float x,float y)
{
float phi = atan2(x,y);//?x,y reversed??!!
float phiDegrees=phi*360.0/6.283185307;
int iPhiDegrees = (int) phiDegrees;
return 180+iPhiDegrees;
}
void fitQunDisplay::PreProcessGeometry()
{
for(int tubeId=0;tubeId<wcsimrootgeom->GetWCNumPMT();tubeId++)
{
WCSimRootPMT pmt = wcsimrootgeom -> GetPMT ( tubeId );
int location=pmt.GetCylLoc();
double pmtX = pmt.GetPosition (0);
double pmtY = pmt.GetPosition (1);
double pmtZ = pmt.GetPosition (2);
if(pmtX>maxX)maxX=pmtX;
if(pmtX<minX)minX=pmtX;
if(pmtY>maxY)maxY=pmtY;
if(pmtY<minY)minY=pmtY;
if(pmtZ>maxZ)maxZ=pmtZ;
if(pmtZ<minZ)minZ=pmtZ;
}
float height = (maxY - minY)/2.0;
yOffset = height - maxY;
maxY = height;
minY = -height;
nYvalues=(maxY-minY)/100;
maxYIndex=0;
maxPhiIndex=0;
for(int tubeId=0;tubeId<wcsimrootgeom->GetWCNumPMT();tubeId++)
{
WCSimRootPMT pmt = wcsimrootgeom -> GetPMT ( tubeId );
double pmtX = pmt.GetPosition (0);
double pmtY = pmt.GetPosition (1) + yOffset;
double pmtZ = pmt.GetPosition (2) ;
int location= pmt.GetCylLoc();
if(pmtY > maxY - 15) location = 0;
else if(pmtY < minY + 15) location = 2;
else location = 1;
if(location ==1 )
{
int yI = zIndex(pmtY);
int phiI = phiIndex(pmtX,pmtZ);
if(yI>maxYIndex)maxYIndex=yI;
if(phiI>maxPhiIndex)maxPhiIndex=phiI;
}
else
{
int xI = xyIndex(pmtX);
int zI = xyIndex(pmtZ);
if(xI>maxXIndex)maxXIndex=xI;
if(zI>maxZIndex)maxZIndex=zI;
}
}
CylinderTubeList = new int[(maxYIndex+1)*maxPhiIndex];
for(int i = 0;i<(maxYIndex+1)*maxPhiIndex;i++)CylinderTubeList[i]=-1;
NegativeCapTubeList = new int[(maxXIndex+1)*maxZIndex];
for(int i = 0;i<(maxXIndex+1)*maxZIndex;i++)NegativeCapTubeList[i]=-1;
PositiveCapTubeList = new int[(maxXIndex+1)*maxZIndex];
for(int i = 0;i<(maxXIndex+1)*maxZIndex;i++)PositiveCapTubeList[i]=-1;
int count=0;
for(int tubeId=0;tubeId<wcsimrootgeom->GetWCNumPMT();tubeId++)
{
WCSimRootPMT pmt = wcsimrootgeom -> GetPMT ( tubeId );
double pmtX = pmt.GetPosition (0);
double pmtY = pmt.GetPosition (1);
pmtY = pmtY + yOffset;
double pmtZ = pmt.GetPosition (2);
int location= pmt.GetCylLoc();
if(pmtY > maxY - 15) location = 0;
else if(pmtY < minY + 15) location = 2;
else location = 1;
if(location == 1 )
{
int yI = zIndex(pmtY);
int phiI = phiIndex(pmtX,pmtZ);
if(yI*maxPhiIndex+phiI>(maxYIndex+1)*maxPhiIndex)
{
cout<<" ERROR "<<endl;
cout<<pmtX<<" "<<pmtZ<<" "<<pmtY<<" "<<yI<<" "<<phiI<<endl;
cout<<" fill item "<<yI<<" "<<phiI<<" with "<<tubeId<<endl;
cout<<" index is "<<yI*maxPhiIndex+phiI<<endl;
cout<<" array dimensions are "<<(maxYIndex+1)*maxPhiIndex<<endl;
}
else
CylinderTubeList[yI*maxPhiIndex+phiI]=tubeId;
}
else
{
int xI = xyIndex(pmtX);
int zI = xyIndex(pmtZ);
if(xI*maxZIndex+zI>(maxXIndex+1)*maxZIndex)
{
cout<<" ERROR "<<endl;
cout<<pmtX<<" "<<pmtZ<<" "<<pmtY<<" "<<xI<<" "<<zI<<endl;
cout<<" fill item "<<xI<<" "<<zI<<" with "<<tubeId<<endl;
cout<<" index is "<<xI*maxZIndex+zI<<endl;
cout<<" array dimensions are "<<(maxXIndex+1)*maxZIndex<<endl;
}
else
{
if(location==0)
{
PositiveCapTubeList[xI*maxZIndex+zI]=tubeId;
count+=20;
//if(count>100){cout<<endl;count=0;}
}
if(location==2)
NegativeCapTubeList[xI*maxZIndex+zI]=tubeId;
}
}
}
}
void fitQunDisplay::lineTubeIntersect(double cerenkov[3],double pos[3],float cylinderR,float &xc,float &yc)
{
double lCxy=sqrt(cerenkov[0]*cerenkov[0]+cerenkov[1]*cerenkov[1]);
double cx = cerenkov[0]/lCxy;
double cy = cerenkov[1]/lCxy;
double px=pos[0];
double py=pos[1];
float m=cy/cx;
float Const=py-m*px;
float a = m*m+1;
float b = 2*m*Const;
float c = Const*Const-cylinderR*cylinderR;
xc = (-b + sqrt(b*b-4*a*c))/(2*a);
yc=m*xc+Const;
}
void fitQunDisplay::searchForTube(int *List,int xCentre,int yCentre,
int maxYSearchIndex, double pos[3],double cerenkov[3],
bool debug,int tubes[3])
{
//bool foundIt=kFALSE;
tubes[0]=-1;
tubes[1]=-1;
tubes[2]=-1;
if(debug)cout<<"fitQunDisplay::searchForTube"<<endl;
/*
Start searching at centre and look at close neighbours
- keep looking untill we don't find a different tube to the one at
the centre of the search.
The criterion is that the cosine of the angle between the cerenkov light and
the tube must be maximized.
*/
int xI,yI;
int nX,nY;
int newXCentre=xCentre;
int newYCentre=yCentre;
int nMax=4;
double cosMax=-1;
do
{
xCentre=newXCentre;
yCentre=newYCentre;
if(debug)cout<<" searching around "<<xCentre<<" "<<yCentre<<endl;
for(int n=0;n<nMax;n++) // n is distance from centre
{
nX=n;
for(nY=-n;nY<=n;nY++)
{
xI=xCentre+nX;
yI=yCentre+nY;
if(xI>=0&&yI>=0&&xI<=maxXIndex&&yI<=maxYSearchIndex){
int iTube=List[xI*maxYSearchIndex+yI];
if(iTube >0 ){
double cosAng=cosAngleToTube(pos,cerenkov,iTube);
if(cosAng>cosMax)
{
cosMax=cosAng;
newXCentre=xI;
newYCentre=yI;
}
//if(debug)cout<<xI<<" "<<yI<<" "<<iTube<<endl;
}
}
}
nY=n;
for(nX=1-n;nX<=(n-1);nX++)
{
xI=xCentre+nX;
yI=yCentre+nY;
if(xI>=0&&yI>=0&&xI<=maxXIndex&&yI<=maxYSearchIndex){
int iTube=List[xI*maxYSearchIndex+yI];
if(iTube >0){
double cosAng=cosAngleToTube(pos,cerenkov,iTube);
if(cosAng>cosMax)
{
cosMax=cosAng;
newXCentre=xI;
newYCentre=yI;
}
//if(debug)cout<<xI<<" "<<yI<<" "<<iTube<<endl;
}
}
}
nX=-n;
for(nY=-n;nY<=n;nY++)
{
xI=xCentre+nX;
yI=yCentre+nY;
if(xI>=0&&yI>=0&&xI<=maxXIndex&&yI<=maxYSearchIndex){
int iTube=List[xI*maxYSearchIndex+yI];
if(iTube >0){
double cosAng=cosAngleToTube(pos,cerenkov,iTube);
if(cosAng>cosMax)
{
cosMax=cosAng;
newXCentre=xI;
newYCentre=yI;
}
}
}
}
nY=-n;
for(nX=1-n;nX<=(n-1);nX++)
{
xI=xCentre+nX;
yI=yCentre+nY;
if(xI>=0&&yI>=0&&xI<=maxXIndex&&yI<=maxYSearchIndex){
int iTube=List[xI*maxYSearchIndex+yI];
if(iTube >0){
double cosAng=cosAngleToTube(pos,cerenkov,iTube);
if(cosAng>cosMax)
{
cosMax=cosAng;
newXCentre=xI;
newYCentre=yI;
}
}
}
}
}
}while(newXCentre!=xCentre||newYCentre!=yCentre);
/*
If the cos(angle) is too small return an invalid result
*/
if(debug)cout<<" cosMax = "<<cosMax<<endl;
if(cosMax<0.999)
{
if(debug)cout<<" which is not close enough "<<endl;
return;
}
/*
found centre, now find two neighbours
*/
if(newXCentre==xCentre&&newYCentre==yCentre)
{
tubes[0]=List[xCentre*maxYSearchIndex+yCentre];
cosMax=-1;
int iXneighour=-1;
int iYneighour=-1;
int n=nMax;
nY=0;
for(nX=1-n;nX<=(n-1);nX++) //scan x directin
{
xI=xCentre+nX;
yI=yCentre+nY;
if(xI>=0&&yI>=0&&xI<=maxXIndex&&yI<=maxYSearchIndex&&xI!=xCentre){
int iTube=List[xI*maxYSearchIndex+yI];
if(iTube >0){
double cosAng=cosAngleToTube(pos,cerenkov,iTube);
if(cosAng>cosMax){
iXneighour=iTube;
cosMax=cosAng;
}
}
}
}
tubes[1]=iXneighour;
nX=0;
cosMax=-1;
for(nY=1-n;nY<=(n-1);nY++)// now scan in Y
{
xI=xCentre+nX;
yI=yCentre+nY;
if(xI>=0&&yI>=0&&xI<=maxXIndex&&yI<=maxYSearchIndex&&yI!=yCentre){
int iTube=List[xI*maxYSearchIndex+yI];
if(iTube >0){
double cosAng=cosAngleToTube(pos,cerenkov,iTube);
if(cosAng>cosMax){
iYneighour=iTube;
cosMax=cosAng;
}
}
}
}
tubes[2]=iYneighour;
}
if(debug)cout<<" normal return "<<tubes[0]<<" "<<tubes[1]<<" "<<tubes[2]<<endl;
}
double fitQunDisplay::cosAngleToTube(double pos[3],double cerenkov[3],int tubeId)
{
WCSimRootPMT pmt = wcsimrootgeom -> GetPMT ( tubeId );
int location= pmt.GetCylLoc();
double pmtX = pmt.GetPosition (0);
double pmtY = pmt.GetPosition (1) + yOffset;
double pmtZ = pmt.GetPosition (2);
if(pmtY > maxY - 15) location = 0;
else if(pmtY < minY + 15) location = 2;
else location = 1;
if(location !=0 && location !=1 && location !=2)return -999;
pmtX = pmt.GetPosition (0) - pos[0];
pmtY = pmt.GetPosition (1) - pos[1];
pmtZ = pmt.GetPosition (2) - pos[2];
double dot=pmtX*cerenkov[0]+pmtY*cerenkov[1]+pmtZ*cerenkov[2];
double lTube=sqrt(pmtX*pmtX+pmtY*pmtY+pmtZ*pmtZ);
double lCerenkov=sqrt(cerenkov[0]*cerenkov[0]+cerenkov[1]*cerenkov[1]+cerenkov[2]*cerenkov[2]);
double cosAng=dot/lTube/lCerenkov;
return cosAng;
}
bool fitQunDisplay::FindConeEnd(double pos[3],double cerenkov[3],double endPoint[3],int &location)
{
if(wcsimrootgeom==NULL)return false;
/*
Loop over all tubes and find 3 nearest ones from which
we can form a plane
*/
bool debug=kFALSE;
if(debug)cout<<endl<<" fitQunDisplay::FindConeEnd starts"<<endl;
float maxCosine=-1.0;
float nextMaxCosine=-1.0;
float nextNextMaxCosine=-1.0;
int tubes[3];
double lCerenkov=sqrt(cerenkov[0]*cerenkov[0]+cerenkov[1]*cerenkov[1]+cerenkov[2]*cerenkov[2]);
double l1 = cerenkov[0]/lCerenkov;
double l2 = cerenkov[1]/lCerenkov;
double l3 = cerenkov[2]/lCerenkov;
double px=pos[0];
double py=pos[1];
double pz=pos[2];
/*
First look close to the last tube we found (maxTube), if we have one.
*/
if(maxTube>0)
{
if(debug)cout<<" start searching close to previous candidate "<<maxTube;
WCSimRootPMT pmt = wcsimrootgeom -> GetPMT ( maxTube );
float pmtx=pmt.GetPosition (0);
float pmty=pmt.GetPosition (1) + yOffset;
float pmtz=pmt.GetPosition (2);
if(debug)cout<<" located at "<<pmtx<<" , "<<pmty<<" , "<<pmtz<<endl;
if(debug)cout<<" min/max z "<<minZ<<" "<<maxZ<<endl;
if(debug)cout<<" maxR "<<maxR<<endl;
location=pmt.GetCylLoc();
if(pmty > maxY - 15) location = 0;
else if(pmty < minY + 15) location = 2;
else location = 1;
/*
Check for situations where we are about to jump from cylinder to end wall or vice versa.
In these cases just looking around the previous tube does not work, so don't try
*/
if(location==1){
if(pmty<(minY+50) || pmty>(maxY-50)){
//cout<<" cylinder tube is close to min or max Z"<<endl;
maxTube=-1;
goto Global;
}
}
if(location==0||location==2){
float r=sqrt(pmtx*pmtx+pmtz*pmtz);
//cout<<" end cap tube r"<<r<<endl;
if(r>(maxR-500)){
//cout<<" is close to maxR"<<maxR<<endl;
maxTube=-1;
goto Extrapolate;
}
//else
// cout<<" is ok "<<endl;
}
int xI,yI,zI;
if(location==0)
{
pmtx=pmt.GetPosition (0);
xI=xyIndex(pmtx);
pmtz=pmt.GetPosition (2);
zI=xyIndex(pmtz);
searchForTube(PositiveCapTubeList,xI,zI,maxZIndex,pos,cerenkov,debug,tubes);
}
if(location==1)
{
yI=zIndex(pmty);
int phiI=phiIndex(pmtx,pmtz);
//int zStep=20;
//int phiStep=20;
searchForTube(CylinderTubeList,yI,phiI,360,pos,cerenkov,debug,tubes);
}
if(location==2)
{
// cout<<" search in -ve cap "<<endl;
xI=xyIndex(pmtx);
zI=xyIndex(pmtz);
searchForTube(NegativeCapTubeList,xI,zI,maxZIndex,pos,cerenkov,debug,tubes);
}
maxTube=tubes[0];
}
/*
Calculate phi, z index to start looking in the array of tube ids.
Assume detector is a cylinder of radius maxR, end plates as minZ, maxZ
*/
Extrapolate:
if(maxTube<0)
{
if(debug)cout<<" start searching close to extrapolated cerenkov light "<<endl;
float xc,zc;
lineTubeIntersect(cerenkov,pos,maxR,xc,zc);
//cout<<" xc,yc from lineTubeIntersect"<<xc<<" "<<yc<<endl;
//cout<<" point of intersect is "<<xc<<" "<<yc<<endl;
float rp=sqrt(px*px+pz*pz);
float rc = maxR-rp;
float theta = acos(l3);
float cyIntersect = py+ rc/tan(theta);
//cout<<" czIntersect= "<<czIntersect<<" compare to "<<minZ<<" "<<maxZ<<endl;
if(cyIntersect<minY||cyIntersect>maxY)
{
//cout<<" probably in end plates. czIntersect= "<<czIntersect<<" compare to "<<minZ<<" "<<maxZ<<endl;
/* calculate x,y if we have hit an end plate */
if(l3>0)rc=(maxY-py)*tan(theta);
else rc=(minY-py)*tan(theta);
float rCap=rc+rp;
// cout<<" r at end cap is "<<rCap<<endl;
lineTubeIntersect(cerenkov,pos,rCap,xc,zc);
// cout<<" xc,yc from lineTubeIntersect: "<<xc<<" "<<yc;
int xI = xyIndex(xc);
int zI = xyIndex(zc);
// cout<<" x/y indices: "<<xI<<" "<<yI<<endl;
// int xStep=50;
// int yStep=50;
//temp!
if(cyIntersect>maxY)
{