forked from R3BRootGroup/R3BRoot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtracker_routines.f95
6468 lines (6262 loc) · 263 KB
/
tracker_routines.f95
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
module vars
implicit none
logical :: grafical_output=.true. ! determines if global grafical output is wanted
logical :: track_out=.false. ! determines single-track data are written to file
logical :: reference_track ! determines if text output is set during tracking
logical :: debug_track=.true. ! if tracking is done in debugging mode
logical :: get_new_derivatives ! determines if the derivatives have to be freshly calculated
logical :: get_new_y_corrections=.true. ! determines if the y_corrections have to be freshly calculated
logical :: calibration_mode=.false. ! program is generally in calibration mode rather than production / tracking mode
logical :: acknowledge_event=.true.
logical,parameter :: derivative_ini_from_file=.false. ! determines if the derivatives ar freshly calculated with track data from file, special use
logical, allocatable :: det_hit(:) ! .true. if detector was hit during current track
logical, allocatable :: det_passed(:) ! .true. if xy-plane of detector was hit during current track
logical, allocatable :: det_consider(:,:) ! (detector,dimension) .true. dimension (x,y,z) of detector is considered during tracking. .false. if uncertainty of detector is <= 0.
logical, allocatable :: det_step_function(:,:) ! (detector,dimension) .true. dimension (x,y,z) of detector is considered as step function during tracking. uncertainty is paddle width .false. if uncertainty of detector is >= 0.
logical, allocatable :: trigger_matrix(:) ! true, if a certain pattern ID is allowed for a given track
logical, allocatable :: trigger_always_together(:,:) ! true, if 2 detectors have to be hit together for a given track
logical, allocatable :: trigger_never_together(:,:) ! true, if 2 detectors are never hit together for a given track
logical :: screen_output=.true. ! if true, standard output is screen, otherwise logfile
logical :: calibration_event_by_event ! true is calibration is done with events from experiment rather than average interception points
logical :: sweep_calibration ! true if calibration is done with magnetic sweep runs, .false. if done with scatter data
character(LEN=256) :: ini_file, output_file(3), geometry_file,magnetic_field_file, derivative_file, calibration_data_file
integer,parameter :: length_ch_trigger_logic=1000
character(LEN=length_ch_trigger_logic) :: ch_trigger_logic
integer, parameter :: output_unit_log = 75 ! output unit for log file.
integer output_unit
character(LEN=256) :: tracker_log_file
character(LEN=256) :: calib_file='calibration.ini'
character(LEN=256), parameter :: track_debug_file='single_track.dat'
character(LEN=256), parameter :: track_debug_file_d1='double_track1.dat'
character(LEN=256), parameter :: track_debug_file_d2='double_track2.dat'
character(LEN=256), parameter :: track_debug_gle_file='single_track.gle'
character(LEN=256), parameter :: double_track_debug_gle_file='double_track.gle'
integer, parameter :: unit_standard_track=9 ! output unit for reference track
integer, parameter :: unit_debug_track=22 ! output unit for track(s) in debug mode
double precision :: dt, t, t_total, dt_fine, dt_coarse
integer,parameter :: nbr_dimensions=6 ! (x,y,z,px,py,pz)
double precision :: x(nbr_dimensions), dx(nbr_dimensions), derivative_b_field(nbr_dimensions)
double precision :: x_start(nbr_dimensions), p_start(3), x_reference_step(nbr_dimensions)
double precision :: x_track1(nbr_dimensions), x_track2(nbr_dimensions), x_reference(nbr_dimensions)
double precision :: b_field(3), v(3) ! magnetic field, velocity
double precision :: b_field_position_in_lab(4) ! position of magnetic field coordinate system in lab-frame (x,y,z,rotation_around_y-axis)
double precision :: glad_current ! magnetic field map will be multiplied with this number divided by 3584...
double precision,parameter :: glad_current_nominal_current=3584.d0 ! current for which the field map is calculated
double precision :: a_over_q, m, m2 !A/Z in AMU/e
double precision :: q_e, q ! charge of particle in elementary unit + SI
double precision :: master_borders(3,2) ! borders of master volume ((xmin,xmax),(ymin,ymax),(zmin,zmax))
double precision :: b_field_borders(3,2) ! borders of magnetic field ((xmin,xmax),(ymin,ymax),(zmin,zmax))
double precision :: spatial_resolution ! spatial resolution of the entire geometry, given by best detector resolution...
double precision :: max_momentum_deviation(3) ! constrains the possible solutions when multiple tracks are possible
double precision, parameter :: crossing_resolution = 1.d-3 ! spatial resolution allowed for crossing tracks
double precision :: slope_parameter(4) ! parameters of the linear slope describing the particle track outside the magnetic field area
double precision,parameter,dimension(3) :: norm_x = (/1.d0,0.d0,0.d0/) ! norm-vector in x-direction ...
double precision,parameter,dimension(3) :: norm_y = (/0.d0,1.d0,0.d0/) ! norm-vector in y-direction ...
double precision,parameter,dimension(3) :: norm_z = (/0.d0,0.d0,1.d0/) ! norm-vector in z-direction ...
double precision, parameter :: c=299792458.d0 ! speed of light, SI units
double precision, parameter :: c2=c*c
double precision, parameter :: c4=c2*c2
double precision, parameter :: e=1.60217662d-19 ! elementary charge, SI units
double precision, parameter :: amu=1.660539040d-27 ! atomic mass, SI units
double precision, allocatable :: b_field_map(:,:,:,:) ! map of magnetic field... (x,y,z,1:3) , last entry for Bx,By,Bz
double precision, allocatable :: b_field_map_1amp(:,:,:,:) ! map of magnetic field for a current of 1 Amp ... (x,y,z,1:3) , last entry for Bx,By,Bz
integer , parameter :: b_field_map_resolution=5 ! resolution of the field in cm !! , hence the integer ...
integer , parameter :: b_field_resolution_factor=100/b_field_map_resolution ! this is to convert position in m into channels in the b-field map
integer :: nbr_steps(2), nbr_detectors, nbr_tracks=1000
double precision, allocatable :: detector_position_in_lab(:,:) ! position of detector coordinate system in lab-frame (detector_ID:x,y,z,rotation_around_y-axis)
double precision, allocatable :: detector_xy_plane(:,:) ! x-y-plane of detector in lab-frame (detector_ID:4) last 4 values correspond to parameters in: a1*x+a2*y+a3*z+a4=0)
double precision, allocatable :: detector_track_interactions_lab_frame(:,:) ! intersection of track point with z=0 plane of detector in lab coordinates (detector_ID,3) last 3: x,y,z
double precision, allocatable :: detector_track_interactions_lab_frame_ref(:,:) ! intersection of track point with z=0 plane of detector in lab coordinates (detector_ID,3) last 3: x,y,z) , reference track
double precision, allocatable :: detector_track_interactions_det_frame(:,:) ! intersection of track point with z=0 plane of detector in detector coordinates (detector_ID,3) last 3: x,y,z)
double precision, allocatable :: detector_track_interactions_time(:) ! time of intersection of track point with z=0 plane of detector
double precision, allocatable :: detector_track_interactions_path(:) ! flightpath between starting point and intersection of track point with z=0 plane of detector
double precision, allocatable :: detector_track_interactions_det_frame_ref(:,:) ! intersection of track point with z=0 plane of detector in detector coordinates (detector_ID,3) last 3: x,y,z), reference track
double precision, allocatable :: detector_track_interactions_det_frame_ref_step(:,:) ! intersection of track point with z=0 plane of detector in detector coordinates (detector_ID,3) last 3: x,y,z), reference during step-wise initialization
double precision, allocatable :: track_lab_frame(:,:) ! experimental track in lab coordinates (detector_ID,3) last 3: x,y,z
double precision, allocatable :: track_det_frame(:,:),track_det_frame1(:,:),track_det_frame2(:,:) ! experimental track in detector coordinates (detector_ID,3) last 3: x,y,z
double precision, allocatable :: track_det_frame_y_corrected(:,:) ! experimental track in detector coordinates (detector_ID,3) last 3: x,y,z
double precision, allocatable :: track_det_frame1_y_corrected(:,:),track_det_frame2_y_corrected(:,:) ! experimental track in detector coordinates (detector_ID,3) last 3: x,y,z
double precision, allocatable :: sigma_track(:,:) ! absolute unctertainties of experimental track
double precision, allocatable :: sigma_track2(:,:) ! square of sigma_track
double precision :: pos_target(3),sigma_target(3),sigma_target2(3), pos_target_original(3) ! target position, absolute uncertainty and it's square (x,y,z)
logical, allocatable :: track_hit_pattern(:),track_hit_pattern1(:),track_hit_pattern2(:) ! true, if detector provided data (detector_ID)
logical, allocatable :: track_hit_pattern_used(:),track1_hit_pattern_used(:),track2_hit_pattern_used(:) ! true, if detector provided data (detector_ID)
logical, allocatable :: track_hit_pattern_from_chi2(:),track1_hit_pattern_from_chi2(:),track2_hit_pattern_from_chi2(:) ! true, if detector current track SHOULD go through this detector (detector_ID)
logical, allocatable :: paddle_hit(:,:),paddle_hit_1(:,:),paddle_hit_2(:,:) ! true, if detector is set to "step function" and track goes through the corresponding paddle (detector_id,dimension)
double precision, allocatable :: detector_range(:,:,:) ! range of detector in detector-frame (detector_ID:x,y,z:min,max,delta)
double precision, allocatable :: derivative_theta(:),offset_theta(:) ! derivative and offset of x-position in detector-frame (detector_ID) when theta_yz is varied
double precision, allocatable :: derivative_x(:),offset_x(:) ! derivative and offset of x-position in detector-frame (detector_ID) when theta_yz is varied
double precision, allocatable :: derivative_q_over_a(:),offset_q_over_a(:) ! derivative and offset of x-position in detector-frame (detector_ID) when q/a of particle is varied
double precision, allocatable :: derivative_p(:),offset_p(:) ! derivative and offset of x-position in detector-frame (detector_ID) when momentum of particle is varied
integer , parameter :: nbr_variables=5 ! variable to be fitted during tracking routine
double precision, allocatable :: derivative_variable(:,:),offset_variable(:,:) ! derivative and offset of x-position in detector-frame (detector_ID) when any variable is varied
double precision, allocatable :: offset_ave_x(:) ! x-position in detector-frame (detector_ID) when x or theta_yz is varied (averaged), reference track
double precision, allocatable :: offset_ave_y(:) ! y-position in detector-frame (detector_ID) reference track
integer, parameter :: length_detector_name = 10
character (LEN=length_detector_name), allocatable :: detector_name(:) ! name of detector (detector_ID)
double precision, allocatable :: stopping_power(:) ! relative stopping power in detector material (detector_ID) (dE/dx / E) for kinetic energy
integer , parameter :: nbr_channels_position_spectra=10000
integer , parameter :: nbr_paramters_position_spectra=6 ! mean, 1-sigma variance, 2x first channel with content > 0, last channel with content > 0, (1..4 values already converted to in m!!!, 5,6 in channels)
integer :: max_dim_pos_spectra=3
integer, allocatable :: detector_position_spectra(:,:,:) ! spectra about xyz distribution of tracks going through detectors (detector_ID:Dimension(1..x,2..y,3..z):Channel_x,y,z)
double precision, allocatable :: detector_position_spectra_paramter(:,:,:) ! spectra about xyz distribution of tracks going through detectors (detector_ID:Dimension(1..x,2..y,3..z):Paramter_type)
double precision :: x_pos_target, theta_xz_target ! origin of track at target, theta in x-z plane at target
double precision :: det
double precision, allocatable :: sum_yi(:), sum_xiyi(:), sum_xi(:), sum_xixi(:), sum_tracks(:)
!
! this is for theta_yz, p, x
!
double precision :: a3_matrix(3,3), a3_matrix_inv(3,3), a5_matrix(5,5), a5_matrix_inv(5,5)
double precision, allocatable :: a3_matrix_inv_array(:,:,:) ! set of matrizes to retrieve single track parameters from detector positions
double precision, allocatable :: t3_matrix_inv_array(:,:,:) ! set of matrizes to retrieve single track parameters from detector positions, target position also constrained
double precision, allocatable :: a5_matrix_inv_array(:,:,:) ! set of matrizes to retrieve double track parameters from detector positions
double precision, allocatable :: t5_matrix_inv_array(:,:,:) ! set of matrizes to retrieve double track parameters from detector positions, target position also constrained
logical, allocatable :: t5_matrix_inv_ok(:,:) ! ok, if matrix-inversion was inverted (:1) and successfully inverted (:,2)
logical, allocatable :: a5_matrix_inv_ok(:,:) ! ok, if matrix-inversion was inverted (:1) and successfully inverted (:,2)
logical, allocatable :: t3_matrix_inv_ok(:) ! ok, if matrix-inversion was successful
logical, allocatable :: a3_matrix_inv_ok(:) ! ok, if matrix-inversion was successful
!
! this is for theta_xz, y
!
double precision :: a2_matrix(2,2), a2_matrix_inv(2,2)
double precision, allocatable :: y_a2_matrix_inv_array(:,:,:) ! set of matrizes to retrieve single track parameters from detector positions
double precision, allocatable :: y_t2_matrix_inv_array(:,:,:) ! set of matrizes to retrieve single track parameters from detector positions, target position also constrained
double precision, allocatable :: y_a3_matrix_inv_array(:,:,:) ! set of matrizes to retrieve single track parameters from detector positions
double precision, allocatable :: y_t3_matrix_inv_array(:,:,:) ! set of matrizes to retrieve single track parameters from detector positions, target position also constrained
logical, allocatable :: y_a2_matrix_inv_ok(:) ! ok, if matrix-inversion was successful
logical, allocatable :: y_t2_matrix_inv_ok(:) ! ok, if matrix-inversion was successful
logical, allocatable :: y_t3_matrix_inv_ok(:,:) ! ok, if matrix-inversion was inverted (:1) and successfully inverted (:,2)
logical, allocatable :: y_a3_matrix_inv_ok(:,:) ! ok, if matrix-inversion was inverted (:1) and successfully inverted (:,2)
!
double precision :: chi2_single(2), chi2_double(6)
double precision :: theta_yz, y_target, theta_yz1, theta_yz2
integer zero_crossing, b_field_map_limits(3,2)
character(LEN=1),parameter, dimension(3) :: ch_xyz=(/'x','y','z'/)
integer, parameter :: length_ch_tf=5
character(LEN=length_ch_tf), parameter :: ch_true='tTtTt',ch_false='fFfFf'
!
integer , parameter :: nbr_functions_y_correction=20 ! 3: only 3rd deg x, 6: 3rd ded. in x and 1st deg in y 8: 3rd ded. in x and 2nd deg in y 8: 3rd ded. in x and 3rd deg in y
double precision, allocatable :: coefficients_y_correction(:,:) ! coefficients to correct y, if theta_xz is varied (nbr_detector,basic_functions_y_correction)
double precision, allocatable :: y_correction(:),y_correction1(:),y_correction2(:)
!
double precision :: dx_b_calibration(3) ! used only for calibration purposes
!
double precision :: p_vector_y2(2), p_vector_y3(3), p_vector_x3(3), p_vector_x5(5) ! raw-results of linearisation
!
integer , parameter :: nbr_channels_2d_spectra=200
integer :: spectrum_dthetay_dpx(0:nbr_channels_2d_spectra,0:nbr_channels_2d_spectra)
integer :: spectrum_dthetay_dpy(0:nbr_channels_2d_spectra,0:nbr_channels_2d_spectra)
integer :: spectrum_dthetax_dpx(0:nbr_channels_2d_spectra,0:nbr_channels_2d_spectra)
integer :: spectrum_dthetax_dpy(0:nbr_channels_2d_spectra,0:nbr_channels_2d_spectra)
integer :: spectrum_dpy_dpx(0:nbr_channels_2d_spectra,0:nbr_channels_2d_spectra)
logical :: constrain_target_x, constrain_target_y ! true, if target position is constrained during fitting
!
integer (kind=8) counter(6) ! some global counting, different internal uses
character(LEN=256), parameter :: geometry_outpout_file='geometry_position_optimized.ini'
end module vars
module calib
integer :: max_nbr_events_run ! max. number of events (contains several hits) to be considered per run
integer :: max_nbr_hits_per_run ! max. number of detector hits to be stored per run
integer :: max_nbr_hits_per_event ! max. number of detector hits per event as found in data
integer :: nbr_runs ! number of B-field settings
integer :: max_nbr_good_events ! maximum number of valid events to be treated. Will be set autimatically to the minimum number of good events available between all runs
integer, allocatable :: counter_calib(:,:) ! some counting ...
integer, allocatable :: nbr_events_in_run(:) ! number of events in current run loaded into memory
integer, allocatable :: nbr_hits_in_event(:,:) ! number of hits in current event loaded into memory
integer, allocatable :: current_event(:), next_event(:) ! position of current and next event of run in event-arrays
integer, allocatable :: detector_id(:,:), event_id(:,:) ! detector-ID and event-ID of hit in event-array
double precision, allocatable :: x_pos(:,:), y_pos(:,:) ! x,y position of hit in detector coordinates in event-array
double precision, allocatable :: b_field_sweep(:) ! magnetic field (per run)
logical, allocatable :: optimize_detector(:) ! true if detector needs to be position-optimized
logical, allocatable :: locked_pair(:,:,:) ! true if distance between detector pair is locked during position-optimization (det,det,dimesion)
integer, parameter :: nbr_dimensions_optimize = 3 ! number of dimensions to optimize. for the moment 3 - x,y,z
logical :: write_to_calib_histos = .true. ! if true, data will be written into position calibration spectra
logical :: momentum_optimization ! if true, all optimizations will be done according to the initial momentum rather than chi2 deviations from a reference track
logical :: lab_coordinates = .false. ! data are provided in lab coordinates rather than detector coordinates
double precision :: p_incoming(3) ! incoming momentum assuming scattering
double precision :: m_b , m_t ! mass of moving particle, mass of target particle
double precision :: chi_treshold ! chi2-cut off to be counted as good event
double precision,allocatable :: track_ref_det_frame(:,:,:) ! track interactions for each sweep run (run,det_id,dimension)
double precision, allocatable :: masses(:,:) ! masses of beam particle & scatter target (per run)
double precision, allocatable :: derivative_variable_ref(:,:), d_derivative_variable(:,:) ! derivative x-position in detector-frame (detector_ID) when any variable is varied
double precision, allocatable :: detector_track_interactions_lab_frame_m(:,:) ! intersection of track point with z=-derivative_step plane of detector in lab coordinates (detector_ID,3) last 3: x,y,z
double precision, allocatable :: detector_track_interactions_lab_frame_p(:,:) ! intersection of track point with z=+derivative_step plane of detector in lab coordinates (detector_ID,3) last 3: x,y,z
double precision, allocatable :: detector_track_interactions_det_frame_m(:,:) ! intersection of track point with z=-derivative_step plane of detector in det coordinates (detector_ID,3) last 3: x,y,z
double precision, allocatable :: detector_track_interactions_det_frame_p(:,:) ! intersection of track point with z=+derivative_step plane of detector in det coordinates (detector_ID,3) last 3: x,y,z
character (LEN=3),parameter, dimension(nbr_dimensions_optimize) :: ch_direction=(/'.X.','.Y.','.Z.'/)
double precision :: derivative_step = 2.d-3
double precision, allocatable :: sum_xiyi_p(:), sum_xixi_p(:),sum_xiyi_m(:), sum_xixi_m(:)
double precision, allocatable :: detector_track_interactions_det_frame_ref_p(:,:) ! intersection of track point with z=+derivative_step plane of detector in detector coordinates (detector_ID,3) last 3: x,y,z), reference during step-wise initialization
double precision, allocatable :: detector_track_interactions_det_frame_ref_m(:,:) ! intersection of track point with z=-derivative_step plane of detector in detector coordinates (detector_ID,3) last 3: x,y,z), reference during step-wise initialization
double precision, allocatable :: d_coefficients_y_correction(:,:) ! coefficients to correct y, if theta_xz is varied (nbr_detector,basic_functions_y_correction)
double precision, allocatable :: coefficients_y_correction_ref(:,:) ! coefficients to correct y, if theta_xz is varied (nbr_detector,basic_functions_y_correction)
double precision, allocatable :: detector_position_in_lab_orig(:,:) ! position of detector coordinate system in lab-frame (detector_ID:x,y,z,rotation_around_y-axis)
!
end module calib
subroutine init_from_cpp
use vars
implicit none
!
call init
call initialize_trigger_logic
call b_field_init
if (calibration_mode) then
call make_calibration
else
call get_derivatives
call initialize_y_corrections
call initialize_matrizes
end if
end
subroutine multi_track_extended_output_from_cpp(array_size,n_points,det_coordinates,double_track, local_target_position, &
detector_id_in, charge_in, x_positions_in,y_positions_in,z_positions_in, &
track_parameter_out, chi_parameter_out,track1_hit_pattern_out,track2_hit_pattern_out)
!
use vars
implicit none
logical (kind=1) track1_hit_pattern_out(2*nbr_detectors),track2_hit_pattern_out(2*nbr_detectors) ! true, if detector was used for track (detector_ID)
integer n_points, array_size
logical (kind=1) det_coordinates, double_track
integer detector_id_in(array_size)
integer charge_in(array_size+2) ! last 2 are requests for track1 and track2
double precision x_positions_in(array_size)
double precision y_positions_in(array_size)
double precision z_positions_in(array_size)
double precision track_parameter_out(12), chi_parameter_out(6)
double precision local_target_position(3)
integer i
character(LEN=1) c1
!
! print*,'entered tracker'
call multi_track_from_cpp(array_size,n_points,det_coordinates,double_track, local_target_position, &
detector_id_in, charge_in, x_positions_in,y_positions_in,z_positions_in, &
track_parameter_out, chi_parameter_out)
!
do i=1,2*nbr_detectors
track1_hit_pattern_out(i) = track1_hit_pattern_used(i)
track2_hit_pattern_out(i) = track2_hit_pattern_used(i)
end do
!
if (debug_track .and. acknowledge_event) then
write(*,*) 'Acknowledge to start next event (g ....go on for ever) '
read(*,*) c1
if (c1 .eq. 'g' .or. c1 .eq. 'G') acknowledge_event = .false.
end if
! print*,'left tracker'
end
subroutine multi_track_from_cpp(array_size,n_points,det_coordinates,double_track, local_target_position, &
detector_id_in, charge_in, x_positions_in,y_positions_in,z_positions_in, &
track_parameter_out, chi_parameter_out)
use vars
implicit none
integer i,j, d, new_hit, det_pattern_id, pattern_id, d1, d2
integer n_det_track_old
integer n_det_track, n_det_track1, n_det_track2
integer n_points, array_size
logical (kind=1) det_coordinates, double_track, left, right, new_combination
logical all_done, all_done1, all_done2, skip_combination, skip
logical track_ok,track1_ok,track2_ok
integer detector_id_in(array_size)
integer charge_in(array_size+2) ! last 2 are requests for track1 and track2
double precision x_positions_in(array_size)
double precision y_positions_in(array_size)
double precision z_positions_in(array_size)
double precision track_parameter_out(12), chi_parameter_out(6)
double precision x_local(3), local_target_position(3), x_local_det(3), x_local_lab(3), delta
integer nbr_hits_per_detector(nbr_detectors)
integer track_hit_numbers(nbr_detectors),track_hit_numbers1(nbr_detectors),track_hit_numbers2(nbr_detectors)
integer track1_hit_numbers_return(nbr_detectors),track2_hit_numbers_return(nbr_detectors), track_hit_numbers_return(nbr_detectors)
double precision track_points_det_frame(nbr_detectors,n_points,3)
double precision track_points_lab_frame(nbr_detectors,n_points,3)
double precision chi2_local,chi2_local_old
integer track_points_charge(nbr_detectors,n_points), charge1, charge2, charge
!
if (debug_track) then
write(output_unit,*) 'multi: sorted events'
write(output_unit,*) 'array_size',array_size
write(output_unit,*) 'n_points',n_points
write(output_unit,*) 'Desired charge for track 1 (0 ..undecided)',charge_in(array_size+1)
write(output_unit,*) 'Desired charge for track 2 (0 ..undecided)',charge_in(array_size+2)
end if
!
if (constrain_target_x) then
pos_target(1) = local_target_position(1)
else
pos_target(1) = pos_target_original(1)
end if
!
if (constrain_target_y) then
pos_target(2) = local_target_position(2)
else
pos_target(2) = pos_target_original(2)
end if
!
nbr_hits_per_detector = 0
track_hit_numbers_return = 0
track1_hit_numbers_return = 0
track2_hit_numbers_return = 0
n_det_track_old = 0
track_points_det_frame = 0.d0
track_points_lab_frame = 0.d0
track_points_charge = 0
chi_parameter_out = 1.d35
chi2_local_old = 1.d35
do i=1,n_points
d = detector_id_in(i) + 1
nbr_hits_per_detector(d) = nbr_hits_per_detector(d) + 1
x_local(1) = x_positions_in(i)
x_local(2) = y_positions_in(i)
x_local(3) = z_positions_in(i)
if (det_coordinates) then
x_local_det = x_local
call get_lab_position(d,x_local,x_local_lab )
else
x_local_lab = x_local
call get_det_position(d,x_local,x_local_det )
end if
!
! do some sorting within a given detector: sort according to x in lab frame. x ascending!
!
new_hit = nbr_hits_per_detector(d)
do j=nbr_hits_per_detector(d)-1,1,-1
if (x_local_lab(1) < track_points_lab_frame(d,j,1) ) then ! will flip entries
track_points_det_frame(d,j+1,:) = track_points_det_frame(d,j,:)
track_points_lab_frame(d,j+1,:) = track_points_lab_frame(d,j,:)
track_points_charge(d,j+1) = track_points_charge(d,j)
new_hit = j
else
exit
end if
end do
track_points_det_frame(d,new_hit,:) = x_local_det
track_points_lab_frame(d,new_hit,:) = x_local_lab
track_points_charge(d,new_hit) = charge_in(i)
end do
if (debug_track) then
write(output_unit,*) 'multi: sorted events'
do d=1,nbr_detectors
write(output_unit,*)
write(output_unit,*) detector_name(d)
do i=1,nbr_hits_per_detector(d)
write(output_unit,*) track_points_charge(d,i), track_points_det_frame(d,i,:)
end do
end do
end if
!
counter = 0
!
! start looping & tracking
!
if (double_track) then
track_hit_numbers1 = 0
all_done1 = .false.
do
!
! get hit-pattern of current track1
!
do d=1,nbr_detectors
track_hit_pattern1(d) = (track_hit_numbers1(d) > 0)
end do
!
! do some checks on the track1
!
! request certain detector combinations
!
pattern_id = det_pattern_id(nbr_detectors,track_hit_pattern1)
track1_ok = trigger_matrix(pattern_id)
!
! check, if track CAN make sense (no crossings...)
!
n_det_track1 = 0
charge1 = charge_in(array_size+1)
if (track1_ok) then
left = .true.
right = .true.
do d=1,nbr_detectors
if (track_hit_pattern1(d)) then
if (det_consider(d,1)) n_det_track1 = n_det_track1 + 1
delta = track_points_lab_frame(d,track_hit_numbers1(d),1) - detector_track_interactions_lab_frame_ref(d,1)
left = left .and. (delta > -crossing_resolution) ! interception point is left of reference track
right = right .and. (delta < crossing_resolution) ! interception point is right of reference track
end if
track1_ok = track1_ok .and. (left .or. right) ! all interception points are either left or right
if (track_hit_pattern1(d) .and. track1_ok) then
track_det_frame1(d,:) = track_points_det_frame(d,track_hit_numbers1(d),:) ! store interception points for later tracking
if (track_points_charge(d,track_hit_numbers1(d)) /= 0) then
if (charge1>0 .and. charge1 /=track_points_charge(d,track_hit_numbers1(d))) then
track1_ok = .false.
if (debug_track) then
write(output_unit,*) 'Multi hit routine, doube track 1, charge missmatch!',&
charge1,track_points_charge(d,track_hit_numbers1(d))
end if
end if
charge1=track_points_charge(d,track_hit_numbers1(d))
end if
end if
if (.not.track1_ok) exit
end do
end if
track_hit_numbers2 = 0
if (track1_ok) then
counter(1) = counter(1) + 1 ! counter for all double track1 candidates
all_done2 = .false.
!
do
!
! get hit-pattern of current track2
!
do d=1,nbr_detectors
track_hit_pattern2(d) = (track_hit_numbers2(d) > 0)
end do
!
! do some checks on the track2
!
! request certain detector combinations
!
pattern_id = det_pattern_id(nbr_detectors,track_hit_pattern2)
track2_ok = trigger_matrix(pattern_id)
!
! check, if track CAN make sense (no crossings...)
!
n_det_track2 = 0
charge2 = charge_in(array_size+2)
if (track2_ok) then
counter(3) = counter(3) + 1
left = .true.
right = .true.
do d=1,nbr_detectors
if (track_hit_pattern2(d)) then
if (det_consider(d,1)) n_det_track2 = n_det_track2 + 1
delta = track_points_lab_frame(d,track_hit_numbers2(d),1) - detector_track_interactions_lab_frame_ref(d,1)
left = left .and. (delta > -crossing_resolution) ! interception point is left of reference track
right = right .and. (delta < crossing_resolution) ! interception point is right of reference track
end if
track2_ok = track2_ok .and. (left .or. right) ! all interception points are either left or right
if (track_hit_pattern2(d) .and. track2_ok) then
track_det_frame2(d,:) = track_points_det_frame(d,track_hit_numbers2(d),:) ! store interception points for later tracking
if (track_points_charge(d,track_hit_numbers2(d)) /= 0) then
if (charge2>0 .and. charge2 /=track_points_charge(d,track_hit_numbers2(d))) then
track2_ok = .false.
! write(output_unit,*) 'Multi hit routine, doube track 2, charge missmatch!',&
! charge2,track_points_charge(d,track_hit_numbers2(d))
end if
charge2=track_points_charge(d,track_hit_numbers2(d))
end if
end if
if (.not.track2_ok) exit
end do
end if
if (track2_ok) then
counter(4) = counter(4) + 1
right = .true.
do d=1,nbr_detectors
if (track_hit_pattern2(d) .and.track_hit_pattern1(d) ) then
right = right .and. (track_hit_numbers2(d) > track_hit_numbers1(d)) ! interception point is right of track1
end if
track2_ok = track2_ok .and. right ! all interception points are right
if (.not. track2_ok) then
exit
end if
end do
end if
!
if (track2_ok) then ! let's track...
counter(2) = counter(2)+1
!
call get_double_track_parameter_var8
!
n_det_track = n_det_track1 + n_det_track2
chi2_local = chi2_double(5) + chi2_double(6)
new_combination = (n_det_track > n_det_track_old)
new_combination = new_combination .or. ((n_det_track == n_det_track_old) .and. chi2_local < chi2_local_old)
if (any(.not.paddle_hit_1) .or. any(.not.paddle_hit_2) ) then
new_combination = .false.
if (debug_track) then
write(output_unit,*) ' new combination refused, because of step function in probability distribution ',&
' (negative uncertainty of detector position) '
do d=1,nbr_detectors
write(output_unit,*) ' track_hit_pattern (false, if detecotr was expected to hit) ',&
detector_name(d), paddle_hit_1(d,:), paddle_hit_2(d,:)
end do
end if
end if
do i=1,3
if (max_momentum_deviation(i) > 0.d0) then
if (dabs(x_track1(i+3) - x_reference(i+3)) > max_momentum_deviation(i) ) then
new_combination = .false.
write(output_unit,*) ' new combination refused, because of momentum constraints in track1'
write(output_unit,*) x_track1(i+3) , x_reference(i+3),max_momentum_deviation(i)
end if
if (dabs(x_track2(i+3) - x_reference(i+3)) > max_momentum_deviation(i) ) then
new_combination = .false.
write(output_unit,*) ' new combination refused, because of momentum constraints in track2'
write(output_unit,*) x_track2(i+3) , x_reference(i+3),max_momentum_deviation(i)
end if
end if
end do
if (new_combination) then
if (charge1 < charge2) then
track_parameter_out(1:3) = x_track1(1:3) ! starting position in lab frame (m)
track_parameter_out(4:6) = x_track1(4:6)/e*c/1.d6*dble(charge1) ! momentum (MeV/c)
track_parameter_out(7:9) = x_track2(1:3) ! starting position in lab frame (m)
track_parameter_out(10:12) = x_track2(4:6)/e*c/1.d6*dble(charge2) ! momentum (MeV/c)
chi_parameter_out(1:4) = chi2_double(1:4)
track1_hit_pattern_used(1:nbr_detectors) = track_hit_pattern1
track2_hit_pattern_used(1:nbr_detectors) = track_hit_pattern2
track1_hit_pattern_used(nbr_detectors+1:2*nbr_detectors) = track1_hit_pattern_from_chi2
track2_hit_pattern_used(nbr_detectors+1:2*nbr_detectors) = track2_hit_pattern_from_chi2
track1_hit_numbers_return = track_hit_numbers1
track2_hit_numbers_return = track_hit_numbers2
else
track_parameter_out(1:3) = x_track2(1:3) ! starting position in lab frame (m)
track_parameter_out(4:6) = x_track2(4:6)/e*c/1.d6*dble(charge2) ! momentum (MeV/c)
track_parameter_out(7:9) = x_track1(1:3) ! starting position in lab frame (m)
track_parameter_out(10:12) = x_track1(4:6)/e*c/1.d6*dble(charge1) ! momentum (MeV/c)
chi_parameter_out(1:2) = chi2_double(3:4)
chi_parameter_out(3:4) = chi2_double(1:2)
track1_hit_pattern_used(1:nbr_detectors) = track_hit_pattern2
track2_hit_pattern_used(1:nbr_detectors) = track_hit_pattern1
track1_hit_pattern_used(nbr_detectors+1:2*nbr_detectors) = track2_hit_pattern_from_chi2
track2_hit_pattern_used(nbr_detectors+1:2*nbr_detectors) = track1_hit_pattern_from_chi2
track1_hit_numbers_return = track_hit_numbers2
track2_hit_numbers_return = track_hit_numbers1
end if
!
chi_parameter_out(5:6) = chi2_double(5:6)
n_det_track_old = n_det_track
chi2_local_old = chi2_local
if (debug_track) then
write(output_unit,*) ' new combination accepted! '
write(output_unit,*) ' n_det_track_old, chi2 ',n_det_track_old, chi2_double
write(output_unit,*) ' track_parameter_out_double 1 ',track_parameter_out(1:6)
write(output_unit,*) ' track_parameter_out_double 2 ',track_parameter_out(7:12)
write(output_unit,*) 'x_track1',x_track1
write(output_unit,*) 'x_track2',x_track2
end if
end if
end if
!
! get next track2 candidate
!
do d1=1,nbr_detectors
skip_combination = .false.
if (track_hit_numbers2(d1) == 0) then
do d2=d1+1,nbr_detectors
skip = (trigger_always_together(d1,d2).and.track_hit_numbers2(d2)==0)
skip = (skip.or.(trigger_never_together(d1,d2).and.track_hit_numbers2(d2)>0))
skip_combination = skip_combination .or. skip
if (skip_combination) exit
end do
end if
if (skip_combination) then
all_done2 = (d1 == nbr_detectors)
else
track_hit_numbers2(d1) = track_hit_numbers2(d1) + 1
if (track_hit_numbers2(d1) == track_hit_numbers1(d1) ) then
track_hit_numbers2(d1) = track_hit_numbers2(d1) + 1
end if
if (track_hit_numbers2(d1) > nbr_hits_per_detector(d1) ) then
track_hit_numbers2(d1) = 0
all_done2 = (d1 == nbr_detectors)
else
exit
end if
end if
end do
counter(6) = counter(6)+1
if (all_done2) exit
end do
end if
!
! get next track1 candidate
!
do d1=1,nbr_detectors
skip_combination = .false.
if (track_hit_numbers1(d1) < nbr_hits_per_detector(d1)) then
if (track_hit_numbers1(d1) == 0) then
do d2=d1+1,nbr_detectors
skip = (trigger_always_together(d1,d2).and.track_hit_numbers1(d2)==0)
skip = (skip.or.(trigger_never_together(d1,d2).and.track_hit_numbers1(d2)>0))
skip_combination = skip_combination .or. skip
if (skip_combination) exit
end do
end if
if (skip_combination) then
all_done1 = (d1 == nbr_detectors)
else
track_hit_numbers1(d1) = track_hit_numbers1(d1) + 1
exit
end if
else
track_hit_numbers1(d1) = 0
all_done1 = (d1 == nbr_detectors)
end if
end do
counter(5) = counter(5)+1
if (all_done1) exit
! write(output_unit,*) 'multi: track_hit_numbers1', track_hit_numbers1,' *** ',nbr_hits_per_detector
! write(output_unit,*) 'multi-counter', counter
end do
!
! single-track
!
else
track_hit_numbers = 0
all_done = .false.
do
counter(1) = counter(1) + 1 ! counter for all track candidates
!
! get hit-pattern of current track
!
do d=1,nbr_detectors
track_hit_pattern(d) = (track_hit_numbers(d) > 0)
end do
!
! do some checks on the track
!
! request certain detector combinations
!
pattern_id = det_pattern_id(nbr_detectors,track_hit_pattern)
track_ok = trigger_matrix(pattern_id)
if (debug_track) then
do d=1,nbr_detectors
write(output_unit,*) ' track_hit_pattern ',detector_name(d), track_hit_pattern(d)
end do
write(output_unit,*) ' checked tigger logik will go on with tracking T/F ',track_ok
end if
!
! check, if track CAN make sense (no crossings...)
!
n_det_track = 0
charge = charge_in(array_size+1)
if (track_ok) then
left = .true.
right = .true.
do d=1,nbr_detectors
if (track_hit_pattern(d)) then
if (det_consider(d,1)) then
n_det_track = n_det_track + 1
delta = track_points_lab_frame(d,track_hit_numbers(d),1) - detector_track_interactions_lab_frame_ref(d,1)
left = left .and. (delta > -crossing_resolution) ! interception point is left of reference track
right = right .and. (delta < crossing_resolution) ! interception point is right of reference track
end if
if (det_step_function(d,1)) then
! n_det_track = n_det_track + 1
delta = track_points_lab_frame(d,track_hit_numbers(d),1) - detector_track_interactions_lab_frame_ref(d,1)
left = left .and. (delta > -sigma_track(d,i)-crossing_resolution) ! interception point is left of reference track
right = right .and. (delta < sigma_track(d,i)+crossing_resolution) ! interception point is right of reference track
end if
end if
! track_ok = track_ok .and. (left .or. right) ! all interception points are either left or right
if (.not. track_ok) then
if (debug_track) then
write(output_unit,*) ' left/right check failed ',track_ok
end if
exit
else
track_det_frame(d,:) = track_points_det_frame(d,track_hit_numbers(d),:) ! store interception points for later tracking
if (track_hit_pattern(d) .and. track_points_charge(d,track_hit_numbers(d)) /= 0) then
if (charge>0 .and. charge /=track_points_charge(d,track_hit_numbers(d))) then
track_ok = .false.
if (debug_track) then
write(output_unit,*) ' charge check failed ',track_ok,&
charge,track_points_charge(d,track_hit_numbers(d))
end if
end if
charge=track_points_charge(d,track_hit_numbers(d))
end if
end if
end do
end if
if (debug_track) then
write(output_unit,*) ' further checks of track, will go on with tracking T/F ',track_ok
end if
!
! do something with the track (e.g. call tracker)
!
if (track_ok) then
counter(2) = counter(2) + 1 ! counter for reasonable track candidates
!
call get_single_track_parameter_var5
!
new_combination = (n_det_track > n_det_track_old)
new_combination = new_combination .or. ((n_det_track == n_det_track_old) .and. chi2_single(1) < chi_parameter_out(1))
if (any(.not.paddle_hit)) then
new_combination = .false.
if (debug_track) then
write(output_unit,*) 'some detectors are not hit, but required to be hit'
do d = 1,nbr_detectors
do i=1,3
write(output_unit,*) detector_name(d), 'dimension',i, paddle_hit(d,i)
end do
end do
end if
end if
do i=1,3
if (max_momentum_deviation(i) > 0.d0) then
if (dabs(x_track1(i+3) - x_reference(i+3)) > max_momentum_deviation(i) ) new_combination = .false.
if (debug_track) then
write(output_unit,*) 'tracked momentum outside range', i, x_track1(i+3),max_momentum_deviation(i)
end if
end if
end do
if (new_combination) then
track_parameter_out(1:3) = x_track1(1:3) ! starting position in lab frame (m)
track_parameter_out(4:6) = x_track1(4:6)/e*c/1.d6*dble(charge) ! momentum (MeV/c)
!
chi_parameter_out(1:2) = chi2_single
n_det_track_old = n_det_track
track_hit_pattern_used(1:nbr_detectors) = track_hit_pattern
track_hit_pattern_used(nbr_detectors+1:2*nbr_detectors) = track_hit_pattern_from_chi2
track_hit_numbers_return = track_hit_numbers
if (debug_track) then
write(output_unit,*) ' n_det_track_old, chi2, track_parameter_out_single ',&
n_det_track_old, chi2_single,track_parameter_out(1:6)
write(output_unit,*) 'x_track1',x_track1
end if
end if
!
end if
!
! get next track candidate
!
do d1=1,nbr_detectors
skip_combination = .false.
if (track_hit_numbers(d1) < nbr_hits_per_detector(d1)) then
if (track_hit_numbers(d1) == 0) then
do d2=d1+1,nbr_detectors
skip = (trigger_always_together(d1,d2).and.track_hit_numbers(d2)==0)
skip = (skip.or.(trigger_never_together(d1,d2).and.track_hit_numbers(d2)>0))
skip_combination = skip_combination .or. skip
if (skip_combination) exit
end do
end if
if (skip_combination) then
all_done = (d1 == nbr_detectors)
else
if (debug_track) then
write(output_unit,*) ' track_hit_numbers(d1), nbr_hits_per_detector(d1), d1 ',&
track_hit_numbers(d1), nbr_hits_per_detector(d1), d1
write(output_unit,*) 'x_track1',x_track1
end if
track_hit_numbers(d1) = track_hit_numbers(d1) + 1
exit
end if
else
track_hit_numbers(d1) = 0
all_done = (d1 == nbr_detectors)
end if
end do
counter(6) = counter(6)+1
if (all_done) exit
end do
end if
if (debug_track) then
write(output_unit,*) 'Multi: all combinations done'
end if
!
n_points = 0
n_det_track1 = 0
n_det_track2 = 0
!
if (double_track) then
do d = 1, nbr_detectors
if (track1_hit_numbers_return(d) > 0) then
n_points = n_points + 1
n_det_track1 = n_det_track1 + 1
detector_id_in(n_points) = d - 1 ! fortran -> cpp
charge_in(n_points) = track_points_charge(d,track1_hit_numbers_return(d))
x_positions_in(n_points) = track_points_det_frame(d,track1_hit_numbers_return(d),1)
y_positions_in(n_points) = track_points_det_frame(d,track1_hit_numbers_return(d),2)
z_positions_in(n_points) = track_points_det_frame(d,track1_hit_numbers_return(d),3)
end if
end do
charge = maxval(charge_in(1:n_det_track1))
charge_in(1:n_det_track1) = charge
do d = 1, nbr_detectors
if (track2_hit_numbers_return(d) > 0) then
n_points = n_points + 1
n_det_track2 = n_det_track2 + 1
detector_id_in(n_points) = d - 1 ! fortran -> cpp
charge_in(n_points) = track_points_charge(d,track2_hit_numbers_return(d))
x_positions_in(n_points) = track_points_det_frame(d,track2_hit_numbers_return(d),1)
y_positions_in(n_points) = track_points_det_frame(d,track2_hit_numbers_return(d),2)
z_positions_in(n_points) = track_points_det_frame(d,track2_hit_numbers_return(d),3)
end if
end do
charge = maxval(charge_in(1+n_det_track1:n_det_track1+n_det_track2))
charge_in(1+n_det_track1:n_det_track1+n_det_track2) = charge
else
do d = 1, nbr_detectors
if (track_hit_numbers_return(d) > 0) then
n_points = n_points + 1
if (debug_track) then
write(output_unit,*) 'Multi: d',d, detector_name(d)
write(output_unit,*) 'Multi: n_points,track_hit_numbers_return(d)',n_points,track_hit_numbers_return(d)
write(output_unit,*) 'Multi: track_points_charge(d,track_hit_numbers_return(d))',&
track_points_charge(d,track_hit_numbers_return(d))
write(output_unit,*) 'Multi: track_points_det_frame(d,track_hit_numbers_return(d),:)',&
track_points_det_frame(d,track_hit_numbers_return(d),:)
end if
detector_id_in(n_points) = d - 1 ! fortran -> cpp
charge_in(n_points) = track_points_charge(d,track_hit_numbers_return(d))
x_positions_in(n_points) = track_points_det_frame(d,track_hit_numbers_return(d),1)
y_positions_in(n_points) = track_points_det_frame(d,track_hit_numbers_return(d),2)
z_positions_in(n_points) = track_points_det_frame(d,track_hit_numbers_return(d),3)
end if
end do
charge = maxval(charge_in(1:n_points))
charge_in(1:n_points) = charge
end if
!
if (debug_track) then
write(output_unit,*) 'Multi: number of data points:',n_points
write(output_unit,*) 'Multi: some counting of events:',counter
end if
! write(output_unit,*) 'Multi: some counting of events:',counter
end
subroutine single_track_from_cpp(n_det,det_coordinates,local_target_position, &
x_positions_in,y_positions_in,z_positions_in, hit_pattern_in, &
track_parameter_out, chi_parameter_out)
use vars
implicit none
integer n_det, i
logical (kind=1) hit_pattern_in(n_det), det_coordinates
double precision x_positions_in(n_det),track_parameter_out(6), chi_parameter_out(2)
double precision y_positions_in(n_det)
double precision z_positions_in(n_det)
double precision x_local_lab(3), x_local_det(3), local_target_position(3)
character(LEN=1) response
!
if (constrain_target_x) then
pos_target(1) = local_target_position(1)
else
pos_target(1) = pos_target_original(1)
end if
!
if (constrain_target_y) then
pos_target(2) = local_target_position(2)
else
pos_target(2) = pos_target_original(2)
end if
!
if (n_det == nbr_detectors) then
if (det_coordinates) then
track_det_frame(:,1) = x_positions_in (:)
track_det_frame(:,2) = y_positions_in (:)
track_det_frame(:,3) = z_positions_in (:)
else
do i=1,nbr_detectors
x_local_lab(1) = x_positions_in(i)
x_local_lab(2) = y_positions_in(i)
x_local_lab(3) = z_positions_in(i)
call get_det_position(i,x_local_lab,x_local_det )
track_det_frame(i,:) = x_local_det
end do
end if
if (debug_track) then
write(output_unit,*)
write(output_unit,*) 'Values from R3B-root'
write(output_unit,*)
write(output_unit,*) ' Nbr-detectors:', n_det
write(output_unit,*)
write(output_unit,*) ' det_coordinates:', det_coordinates
write(output_unit,*)
write(output_unit,*) ' x-positions (m):', x_positions_in
write(output_unit,*)
write(output_unit,*) ' y-positions (m):', y_positions_in
write(output_unit,*)
write(output_unit,*) ' z-positions (m):', z_positions_in
write(output_unit,*)
write(output_unit,*) ' hit-pattern:', hit_pattern_in
end if
track_hit_pattern = hit_pattern_in
!
call get_single_track_parameter_var5
!
track_parameter_out(1:3) = x_track1(1:3) ! starting position in lab frame (m)
track_parameter_out(4:6) = x_track1(4:6)/e*c/1.d6 ! momentum (MeV/c)
!
chi_parameter_out = chi2_single
!
if (debug_track) then
write(output_unit,*)
write(output_unit,*) 'Return values to r3broot'
write(output_unit,*)
write(output_unit,*) ' Position at Target (m):', track_parameter_out(1:3)
write(output_unit,*)
write(output_unit,*) ' Momentum at Target (MeV/c/q):', track_parameter_out(4:6)
write(output_unit,*)
write(output_unit,*) ' Momentum at Target (MeV/c/q):', chi_parameter_out
end if
else
print*,'Missmatch between number of detectors called from cpp and expected inside fortran', n_det, nbr_detectors
write(output_unit,*) 'Missmatch between number of detectors called from cpp and expected inside fortran',&
n_det, nbr_detectors
stop
end if
if (debug_track .and. grafical_output) then
print*
write(*,'(A24)', advance='no') ', continue (q = quit) : '
read(*,*) response
if ( (response == 'q') ) stop
end if
end
subroutine double_track_from_cpp(n_det, det_coordinates, local_target_position,x1_in,y1_in,z1_in, hit1_in,&
x2_in,y2_in,z2_in, hit2_in, &
track1_out,track2_out, chi_parameter_out)
use vars
implicit none
integer n_det, i
logical (kind=1) hit1_in(n_det),hit2_in(n_det), det_coordinates
double precision x1_in(n_det),y1_in(n_det),z1_in(n_det)
double precision x2_in(n_det),y2_in(n_det),z2_in(n_det)
double precision track1_out(6),track2_out(6),chi_parameter_out(6)
double precision x_local_lab(3), x_local_det(3) , local_target_position(3)
!
if (constrain_target_x) then
pos_target(1) = local_target_position(1)
else
pos_target(1) = pos_target_original(1)
end if
!
if (constrain_target_y) then
pos_target(2) = local_target_position(2)
else
pos_target(2) = pos_target_original(2)
end if
!
if (n_det == nbr_detectors) then
if (det_coordinates) then
track_det_frame1(:,1) = x1_in
track_det_frame1(:,2) = y1_in
track_det_frame1(:,3) = z1_in
track_det_frame2(:,1) = x2_in
track_det_frame2(:,2) = y2_in
track_det_frame2(:,3) = z2_in
else
do i=1,nbr_detectors
x_local_lab(1) = x1_in(i)
x_local_lab(2) = y1_in(i)
x_local_lab(3) = z1_in(i)
call get_det_position(i,x_local_lab,x_local_det )
track_det_frame1(i,:) = x_local_det
x_local_lab(1) = x2_in(i)
x_local_lab(2) = y2_in(i)
x_local_lab(3) = z2_in(i)
call get_det_position(i,x_local_lab,x_local_det )
track_det_frame2(i,:) = x_local_det
end do
end if
track_hit_pattern1 = hit1_in
track_hit_pattern2 = hit2_in
!
call get_double_track_parameter_var8
!
track1_out(1:3) = x_track1(1:3) ! starting position in lab frame (m)
track1_out(4:6) = x_track1(4:6)/e*c/1.d6 ! momentum (MeV/c)
track2_out(1:3) = x_track2(1:3) ! starting position in lab frame (m)
track2_out(4:6) = x_track2(4:6)/e*c/1.d6 ! momentum (MeV/c)
!
chi_parameter_out = chi2_double
!
if (debug_track) then
write(output_unit,*) 'FORTRAN, track1:',real(track1_out)
write(output_unit,*) 'FORTRAN, track2:',real(track2_out)
write(output_unit,*) 'FORTRAN, chi2 :',real(chi_parameter_out)
end if
!
else
print*,'Missmatch between number of detectors called from cpp and expected inside fortran', n_det, nbr_detectors
write(output_unit,*) 'Missmatch between number of detectors called from cpp and expected inside fortran',&
n_det, nbr_detectors
stop