Index of /~dlr/g-2/g2/documentation/log_files

[ICO]NameLast modifiedSizeDescription

[PARENTDIR]Parent Directory  -  


December 8, 2022
energy_vs_turn_vs_time/config_034/ 20221207_140837/
Track 100k of Emerey's distribution into ring. 2 turns. to get x-phi, x'-phi correlation at
inflector exit. Use phase_space_to_histogram.f90 with 6,7,8 to read MARK_INFLECTOR_DS_phase_space.dat and
write  bin_average_rms_6.dat,  bin_average_rms_7.dat, bin_average_rms_8.dat (phi vs delta, x, x').
(Recall that an input file is required - <phase_space_to_hist.dat>.
and then use phase_space_to_hist.gnu to plot and fit all 3 and write
MARK_INFLECTOR_DS_phase_space.pdf. Recall (phase_space_to_hist.gnu reads the 3 bin_average_rms files.)?
    Looks reasonable but statistics are not great.

Rerun with 400k events and go 100 turns so collect info for x-delta and x'-delta
    for the stored beam. (Turn on scattering in inflector) and run g2_tracking ->config_034/20221208_100307
    Use <phase_space_to_hist.dat> to create bin_average_rms_6,7,8.dat and phase_space_to_hist.gnu to plot.

21:10 Run again with 400k starting at 400k

-----------------------------------------------------------------------------------
energy_vs_turn_vs_time/config_032/20221204_090912/
Track 369500 Diktys muons into ring and 100 turns. (no scattering in inflector). Use phase_space_to histogram to write
bin_average_rms_6,7,8 for MARK_INFLECTOR_DS_phase_space.dat and plot with phase_space_to_hist.gnu

December 9, 2022
 run g2_tracking ->config_034/20221208_100307 - completed 20221209_0748

-  Rerun events 800k ->1000k  > config_034/20221209_074900 - complete at 20221209_1249
-  And with events 1000k -> 1200k > config_034/20221209_075109 - complete at 20221209_1249
Combine using energy_vs_time.f90
Then energy_time_to_2D_hist.f90 (requires some repair to energy_vs_time.f90)
Plot with 2D_hist_test.gnu - 2D_histogram_4,5,6.pdf - uses 1.2e6 Emerey events 

To determine effect of shifting kicker time, set kStart to 180ns in input.dat in config_033.
Submit to grid. -> config_033/20221209_115910.
-> pulse_kicker.pdf (copied to 20221209_115910. Does indeed shift kick earlier so by eye more centered.

Create config_035 - run the same as in config_034 with kick 30ns earlier
  20221209_104253 - events 0-200000, Eremey, kStart=180ns (30ns earlier)
  20221209_194702 - events 200000-400000 "   qsub 7093778
  20221209_194903  -events 400000-600000 "  qsub 7093779
  20221209_195035  - events 600000-800000 " qsub 7093780
  20221209_195206 - events 800000-1E6        " qsub 7093781
  20221209_195435 - events 1E6-1.2E6          "  qsub 7093782   20221209_1955

December 10,2022
  config_035 jobs completed. This is to measure x,x' vs delta for spin-coordinate correlation with shifted kicker pulse.
  $pb/energy_vs_time
  $pb/energy_time_to_2D_hist and create plots. SLopes are not much different thatn config_034

Create config_036 - same as config_035 but kick 224G
20221210_084842 = events 0-200000 kstart=180ns, kick=224G  running on 6186
 20221210_085451 - events 200000-400000    " 7093789
 20221210_090036 - events 400000 - 600000  "    7093791
20221210_090220  - events 600000-800000   " 7093792
20221210_090406 - events 800000-1e6         " 7093793
20221210_090721 - events 1.e6 -1.2e6      "   7093795
(To submit use qsub line from submit_25.sh)
 Jobs all complete. Create 2D_histogram_4,5,6.pdf - > slopes are very similar to config_034

Next thing to try is adjusting inflector field. create config_037 for that test.
Copy input.dat from config_034/
Before any adjustment we find  (with inflctor scattering off)
initial_offsets = -5.8643E-02  0. 0. 2.5068E-02 0. 0.0 ! -2mm,-3.4mrad,Inf=0.976, 4mrad, -2.3mm, -3.6mrad, crosses zero 
=> 4.4000E+00  7.5001E-02 -3.3995E-03  0.0000E+00  0.0000E+00 -8.2633E-04  0.0000E+00 END
Increase inflector to 0.986
  4.4000E+00  7.7166E-02 -8.0400E-04  0.0000E+00  0.0000E+00 -8.2226E-04  0.0000E+00 END       
Looks reasonable. Particle is stored and survives 100 turns. Let's try.Turn inflector scattering back on. 

Start jobs config_037
20221210_212616 - events 0-200k kstart 210, kick 264, inf=0.986 7093803
20221210_212806 - events 200k-400k "                                           7093804
20221210_213509 - events 400k-600k "                                           7093805
20221210_213639 - events 600k-800k "                                           7093806
20221210_213736 - events 800k-1000k "                                         7093807
20221210_213928 - events  1e6 - 1.2e6                         6186

Create file energy_vs_time_vs_turn/slopes.dat with fitted slopes,etc.
energy_vs_time_vs_turn/slopes.gnu to plot and combine
writes to slopes_domega.dat

In config_037 run energy_vs_time and then energy_time_to_2D_hist and then gnup500 2D_hist_test.gnu to create 2D_histogram_4,5,6

In config_037/20221210_213928.  cp ../../config_032/20221204_090912/phase_space_to_hist.dat .
run $pb/phase_space_to_histogram - for number=6,7,8.
Statistics are not great so

copy to config_037 and combine 20221210_213736/MARK_INFLECTOR_DS_phase_space.dat and 20221210_213736/MARK_INFLECTOR_DS_phase_space.dat
into config_037/MARK_INFLECTOR_DS_phase_space.dat.
copy config_037?20221210_213928/phase_space_to_hist.dat
run phase_space_to_histogram with number 6,7,8 and gnup500 phase_space_to_hist.gnu to create MARK_INFLECTOR_DS_phase_space.pdf
__________________________________________________________________

December 13, 2022
config_033/20221213_134055
     400k events
     EndofM5_Valetov_withInit.dat
     time_vin_width = 20ns
     pbin_width=0.0002
     time_binning= momentum_slice
        To see if time binning does what it is supposed to

config_031 - lets go back here to redo kicker timing scan with run3b-4 kicker pulse
     20221213_134716

Just discover my quad_plate_misalign file includes real misalignment. - and true for most config_03* studies
____________________________________________________________________------
December 14, 2922
   config_031, running with time bin in 'position' mode, and finding bugs.
   Fix bugs ? start again 
  config_031/20221214_140352/VparamDependence.dat -> peak between 200ns and 220ns. Consistent with the 210ns had determined  before. 

   config_033 g2_tracking dies with 'killed' message. Is this a memory issue?
   Meanwhile it does write output. 

To better understand how dependence of x,x' on momentum of stored beam and phi on x,x' at inflector exit,
vary inflector to -1% (already did +1% in config_037)
For this create config_038 - copy from config_037 and change inflector
 config_038/20221214_121954  0-200k
                   20221214_122356  200-400k
		   20221214_122538 400-600k
		   20221214_122713  600-800k
		   20221214_122854 800-1M
		   20221214_123023 1-1.2M
config_038 - paste MARK_INFLECTOR_DS_phase_space.dat from  20221214_122356,  20221214_122538, 20221214_122713 together and write MARK_INFLECTOR_DS_phase_space.dat to config_038/ 



In g-2/files/muons plot phi vs x,x',delta for EndofM5_Valetov_withInit.dat

December 15, 2022
config_038/ - jobs completed with reduced inflector. Add slopes to slopes.dat and plot slopes.pdf. point 38 is higher than the others.

December 16, 2022

Back to phase vs time in config_033. Try in debug mode with fewer particles. - With 10000 particles, runs fine to completion. 20221216_053911 
Run again (not in production mode) with 200k particles,starting at 400k 20221216_073316 - runs to completion.
Next step is figure out how to combine output. Edit subroutine bin_spin_angle_by_time (compute_moments.f90) so it writes relevant output to directory.
Start two jobs, first 200k and second 200k events with plan to figure out how to combine output.
One job interactive and one batch

December 17, 2022
Both config_033 jobs complete. Submit a few more
     400k - 600k 


December 18, 2022
 The 6 batch jobs I had submitted in config_033 did not complete. Lost /nfs/gm2/data2/  ? Is that related to my jobs.
 Remov subdirectories with unfinished jobs
 Try running a single job. 20221218_133014  0-200k

December 19, 2022
 Single hob 20221218_133014 completes in 400 minutes
 Start another config_033/20221219_073409 200k-400k


December 21, 2022
On the mini
Set err_flag to false in track1_preprocess and set mat6_calc_method = bmad_standard for markers in the injection line, and then able to run in config_034 (on mac).
Start a couple of jobs starting at 0 > out.dat  0-100k
                                  100k > out1.dat 100k - 200k
                                  200k > out2.dat 200k - 300k
                                  300k > out3.dat 300k - 400k


January 4, 2023
On the mini
Recall that config_034 contained 2D_hist_fit_coef_err_7.dat, 2D_hist_fit_coef_err_8.dat, 2D_hist_fit_coef_err_5.dat, 2D_hist_fit_coef_err_4.dat 
for parameters close to run 3b. But we know that 2D_hist_fit_coef_err_7.dat, 2D_hist_fit_coef_err_8.dat (the correlation at the end of the inflector) are
not right. Instead we will use the theoretical values. For clarity, collect those values for plots etc. in 
config_theory. Copy 2D_hist_fit_coef_err_7.dat, 2D_hist_fit_coef_err_8.dat from config_034 and alter coef1 to -alpha/beta and 1. respectively.
Also copy 2D_hist_fit_coef_err_5.dat, 2D_hist_fit_coef_err_4.dat from config_034/ to config_theory
Copy 2D_hist_4.dat from config_034 to config_theory for average and stdev energy.
Then use $ps/spin_debug.gnu to plot.

config_040 - set up to 'use_as_is' the eremey distribution to see what the correlation is at the inflector exit. Start run with 200k particles.
              Check phase space MARK_INFLECTOR_DS_phase_space and correlations look wrong.
              Can it be the scattering in the inflector entrance and exit?
              Turn off scattering and try again.
              $pb/g2_tracking > out2.dat & - 20230104_133435

January 10, 2023
    config_040 - with scattering off
              Worse than with scattering on. Although the difference could be statistical.
              Slopes are bigger than expected at inflector exit. Especially phi vs x'
              
              Generate more statistics.
               $pb/g2_tracking > out3.dat & -20230110_182221  200k-> 400k
               $pb/g2_tracking > out4.dat & - 20230110_182221 400k-600k
                $pb/g2_tracking > out5.dat & - 20230110_182729 600k-800k 

January 17, 2023
            The program 'spincoordinate.f90' reads the fit coeficients for dspin_dx, dspin_dxp, dx_dp, dxp_dp and outputs delta_omega/dt. Plot with $ps/spin_debug.gnu
            The programs 'phase_space_to_histogram.f90' and 'energy_time_to_2D_hist.f90' both compute coeficients and write the files.
                  The input to energy_time_to_2D_hist.f90 is the output of 'energy_vs_time.f90'
                  The input to phase_space_to_histogram.f90 is a phase space file
            Put Renees data into config_renee_run2, renee_run3a, renee_run3b, etc.  and then for tuned and raw input phase space respectively.
         /renee_run2/
            Copy coefficient files from config_034 and edit with her numbers.
            Run spincoordinate and then spin_debug.gnu
            This works out with reasonable delta omega (except it is opposite the sign I got) for Run2

         /renee_run3a
            likewise reasonable but wrong sign

         /renee_run3b
            and again

      For the above I used the momentum average and width from config_034. Should use the real numbers.

      In config_040 - combine MARK_INFLECTOR_DS_phase_space.dat from all subdirectories with no scattering in inflector and use 'phase_space_to_histogram.f90' 
         to create 2D_hist_fit_coef_7.dat and 2D_hist_fit_coef_8.dat
         Use 'energy_vs_time.f90' and then 'energy_time_to_2D_hist.f90' to create 2D_fit_coef_4.dat and 5. (2D_hist_4.dat is generated by 'energy_time_to_2D_hist.dat')

January 19, 2023
      Renee provides energy average and width for her runs 2-3b. Save to renee_run3b. Then edit 2D_hist_4.dat in each of directories 2-3b with her numbers and rerun 
         energy_time_to_2D_hist

         /renee_run2, renee_run3a, renee_run3b, and renee_eremey- 2D_hist_4.dat updated with values in renee's email of 20230117 
            
      Copy contents (partial) of config_032, config_035, config_036, config_037, config_038 from lnx6186. Also slopes.dat
      This will allow reconstruction of all of those different configurations.    

      Renee's slopes tended to be opposite. See that she used only captured muons to get dspin/dx and dspin/dxp.
      Request corrected plots that inlcude all muons (for example dspin/dx and dspin/dxp at inflector exit)        

January 24, 2023
      Update          
        /renee_run2, renee_run3a, renee_run3b, and renee_eremey- 2D_hist_fit_coef_7.dat and 2D_hist_ it_coef_8.dat files updated with values in renee's email of 20230123
        Note that Renee's plots are copied to each of the directories respectively. Original plots labelled with ... _A_... Newest plotes labelled with ... _B_...
        
        After updating 2D_hist_fit_coef_7,8.dat in run2, run3a, run3b, eremey
        run spincoordinate.f90 in each directory        (first delete those data from ../spin_summary.dat)

January 25, 2023
       20230125_164339 - turn on save element by element info to get phase space at IBMS3 


February 14, 2023
       create subdirectorys of energy_vs_time_vs_turn and copy as follows
        run2_2007:      cp ../renee_run2/2D_hist_fit_coef_err_*.dat .           cp ../renee_run2/2D_hist_4.dat .       (To get average energy and width)
        run3_A-G_3000:  cp ../renee_run3a/2D_hist_fit_coef_err_*.dat .          cp ../renee_run3a/2D_hist_4.dat .
	run3_I-L_3001:  cp ../renee_run3b/2D_hist_fit_coef_err_*.dat .		cp ../renee_run3b/2D_hist_4.dat .
	run3_M_3002:    cp ../renee_run3b/2D_hist_fit_coef_err_*.dat .		cp ../renee_run3b/2D_hist_4.dat .
	run3_N-O_3100:  cp ../renee_run3b/2D_hist_fit_coef_err_*.dat .		cp ../renee_run3b/2D_hist_4.dat .

     Renne's results of slopes at gun, IBMS1, plane0 and IBMS3 are attached to email of 2/13/2023
     Copy the plots for each set of runs to appropriate directory.

     Edit 2D_hist_fit_coef_err_7,8.dat updating values from renee's plots

     run $pb/spincoordinate in each directory, and plot with $ps/spin_summary.gnu

     create slope_summary.dat in each of the 5 directories and update with latest numbers and plot with $ps/slope_summary.gnu
     
     Renee's results for dx/dp and dx'/dp for run2_2007 - run3_N-O-3100 attached to email of 2/14/2023
     Update 2D_hist_fit_coef_err,4,5.dat in each of 5 directories.
     
     run $pb/spincoordinate in each directory, and plot with $ps/spin_summary.gnu

March 15, 2023
   create subdirectory config_041 to test 'time_bins' that sorts particles into time bins with momentum phase information.
   start with input.dat from lnx6186 config_033
  
March 24, 2023
   config_041 - find that dphi/d(delta) is much bigger than expected. (see subdir 20230322_184855 - 200k particles, dphi/d(delta) \sim 20.rad/- )
   and also opposite sign of dphi/d(delta) at end of inflector. 
   run again with more statistics (264G 20230324_095327 out1.dat) 
   run again with more statistics and weaker kick (224 G 20230324_095503 out2.dat) 

March 26, 2023
    In above runs config_041/ 20230324_095327 and 20230324_095593 not much different. Effect of weaker kick is poorer capture and statistics. Any difference in
    dphi/d(delta) is not statistaically significant.
    Try with random distribution with all particles initially polarized the same. That is with no correlation.
    Change start_time in time_bins.f90 to 0.5e-6 from 4.e-6 to see if something is evolving while scraping. 20230326_161247 out1.dat
    
March 27, 2023
    Turns out I didn't change start time since it is also referenced in calling conditional in tracking_master.
    in config_041/20230326_161247 - dphi/d(delta) ~ 4 rad (as opposed to 20 rad when using Valetov dist)
    Corresponds to about 60 ppb on omega_a. 
    Once again fix up code with new parameter 'time_binning_start' and set it to 0.5 microsec
    Try same as 20230326_161247 this time 20230327_095637 out1.dat -> <> = 44.8 [ppb], 3.5 rad
     but I already know that its mostly about the initial distribution

    Another thing to try is a short bunch. That will preclude the kicker contribution
      config_041/20230327_124622 out2.dat
      The problem here is that the muon pulse is at the leading edge of the kicker pulse which is pretty weak.
      Try a time scan. 20230327_174031 out3.dat  -> from start_time = 80-280ns. Max at 280ns. 

March 28, 2023
      Run again from 260-380ns 20230328_101413 out3.dat -> peak is at tStart=280ns
      Start run with rand gaussian distribuion, rectangular width 1ns, 20230328_153331 > out1.dat

      Start run with eremey distribution but narrow rectangular 1ns pulse 20230328_162424 out2.dat

      Create plotting_scripts/dphi_ddelta_vs_time.gnu - first run energy_vs_time '2023----' to get average and mean momenta for that run. Copy all_Energy_vs_time.dat to 2023----'
       In 2023---- gnuplot $ps/dphi_ddelta_vs_time.gnu  reads domega_ddelta_vs_time.dat 

March 29, 2023
       Add another line to times_bins.f90 for another chance to cross twopi divide
       with these lines
         if(j>1)then
             if(avg_phase(j)-avg_phase(1) > twopi/2.)avg_phase(j)=avg_phase(j)-twopi
             if(avg_phase(j)-avg_phase(1) < -twopi/2.)avg_phase(j)=avg_phase(j)+twopi
             if(avg_phase(j)-avg_phase(1) > twopi/2.)avg_phase(j)=avg_phase(j)-twopi
             if(avg_phase(j)-avg_phase(1) < -twopi/2.)avg_phase(j)=avg_phase(j)+twopi
          endif    
        Next create config_042/  and run with 1ns width, Eremey distribution, 200k in 4 jobs at a time with plan to combine
        particles 0-200k 20230329_185121 out1.dat
                  200k-400k 20230329_185243 out2.dat
                  400k-600k 20230329_185337 out3.dat
                  600k - 800k 20230329_185431 out4.dat

        Create 'compile_domega_vs_time.f90' to combine domega_ddelta_vs_time.dat from multiple directories. Writes 'combined_domega_ddelta_vs_time.dat'

March 30, 2023
        There is structure in dphi/ddelta when using eremey data but not with unpolarized random.
        Fix up code (create_phase_space using 'set_polarization') so that eremey distribution is all polarized in z-direction (as oposed to all polarized with equal s dot p)
        Try this out in config_042/20230330_110124. Set to use eremey distribution but with no correlation and we get dphi/ddelta=0
        (Also, time_bins.f90 modified to write phase vs momentum AFTER fixing wrap around. Just before call to fit.)

        Try with full width muon pulse.
           20230330_172401 - out2.dat - <> = 37.2 ppb  (contribution from kicker) 2.8 rad

March 31, 2023
        config_042/20230331_164749 out4.dat Diktys distribution, set_polarization=F, full length pulse 

April 2, 2023
        Add subroutine manage_polarization to create_phase_space.f90 that can set polarizations to 'antiparallel', or with effect of DR 'spintune'
        Try it out with a random distribution, and 1ns t0 pulse config_042/20230402_105739 out1.dat 

April 3, 2023
        config_042/20230402_105739 - error in manage polarization (forgot the gamma)
        Now repaired (see latest create_phase_space.f90) and tested on linux with debug
        Try again with fewer particles (20k)
        config_042/20230403_114046 out1.dat

        in subroutine 'write_phase_space' in file create_phase_space.f90, add acos(p.s) to output file

April 4, 2023
        config_042/20230404_100534  out1.dat - try out update of write_phase_space
        looks OK     


April 12, 2023
        config_043 - compute closed orbits with quad misalignment for Brynn
          Turn off scraping
          20230413_145308 - quad_plate_misalign.bmad all set to zero.
          use $ps/closed_orbit_pdf.gnu to plot orbits
          20230413_144938 - shift horizontal plates horizontally and vertical plates vertically 
          20230413_182836 - displace vertical plates 1mm vertically. No change to horizontal. - vertical orbit shifts up .52mm
          20230413_200631 - shift all plates vertically and horizontally (quad is displaced intact)
      
April 19, 2023
         Add markers to lattice (config_044) to split quads to allow 10ns time bins
         In order to make this work switch to curvilinear multipoles. Turn off scraping. Add quad field index to input.dat. Comment out 'call quad_plate_misalign.' , tlength=1ns
           call calorimeter lattice (markers at each calorimeter)
           run with 20k muons from Eremey data.
              20230419_113126 - out1.dat - time_bin=20ns (as before for reference since now using different quad model)
              20230419_113225 - out2.dat - time_bin=10ns
              both 10 and 20ns binned look the same. Maybe not as noisy as with other version of quads.

              For reference try setting polarizations=T to 'antiparallel'. 10ns bins, T0=1ns
              20230419_143915 out3.dat - this looks fine. Namely all zero
              write bug in time_bins. extra loop is wrong. Commented out as follows
              !          do p_bin=-npbins/2,npbins/2
            write(345,'(es12.4,i12,i10,4es12.4)')(i+start_time_bin)*bin_width,p(m)/bin_width,m,p(m),avg_phase(m),rms(m), err_avg(m)
              !          end do

              Use real T0 pulse. And use polarizations in eremey distribution. This should give the whole deal
              20230420_034010 out1.dat

              Fix up domega_ddelta_vs_time.dat so that gnuplot can read data and fit parameters easily.
               this is accomplished by including evaluation of fitted function along with data points.
 
             Run with 50k points, real T0 pulse, Eremey, 20230420_111343 out1.dat

             write plotting script to plot 'spin_vs_momentum_at_time.dat' with carriage return going from one index to next
                $ps/spin_vs_momentu_at_time.gnu  works OK. No obvious disconnect at wrap around for data from 20230420_111343

             Try more events.
                1.3 million,   tdistr    =  "e989-P8" ,muon_file =  'EndofM5_Valetov_withInit.dat',time_bin_width = 10.e-9, pbin_width = 0.0002, time_binning = 'momentum_slice', set_polarization = F    
                20230420_191549, out1.dat  This could take awhile.

April 21, 2023
               650k muons, tdistr    =  "flat", tlength=2ns, muon_file =  'EndofM5_Valetov_withInit.dat',time_bin_width = 10.e-9, pbin_width = 0.0002, time_binning = 'momentum_slice', set_polarization = F    
               20230421_094257 out2.dat
                

April 22, 2023
              Puzzling result for  20230420_191549. Should be the whole deal but average comes out near zero? Sanity check.
              Run same input.dat again but with only 20k events.
              20230422_064604 out3.dat              
 
              Work out more definitive way to get phase using cross product (subroutine RightOrLeft in time_bins)
              After some debugging on lns6186, I think it is correct.
              On lnx6186 compile without debug and run 20k test
               lnx6186 / / config_044/20230422_183238

April 24, 2023
             After some fussing with totalview on lnx6186 finally manage to write phi(t) as monotonically increasing.
             Again on lnx6186 config_044/20230424_092620 out4.dat. 20k events. write phi(t) for each muon to fort.301
             Works much better. No oscillation of dphi/ddelta. There is a step at 2.33 us. and smaller steps at 2.22us and 4.48us
                Insufficient memory. Try again with 100k muons
                lnx6186 config_044/20230424_190644  out1.dat

             Start again with 200k events 20230424_105947 out1dat
             ci time_bins.90 (and funcs_and_fit_2D_array.f90 although this is not related to recent debug effort)
             Update on mac
             config_044/20230424_111028 out1.dat full eremey and T0

             config_044/20230424_111244 out2.dat full eremey and 2ns T0
             config_044/20230424_111408 out3.dat eremey, antiparallel and 2ns T0
          
             Now go back to map style quads in config_042 - to see how this compares to multipole style. Also this has scraping.
                 20ns bins, Full Eremey, full T0   config_042/20230424_191150 out1.dat. Doesn't know what to do with IBMS3 so comment out call IBMS_lattice. in bmad_esquad_grid.

April 25, 2023
              config_044/20230424_111244 out2.dat full eremey and 2ns T0
                average dphi/ddelta < 0. Constant vs time until 5.65 us. Then every turn (149ns) 3 time points have more negative dphi/ddelta. Very sharp transition.
                Why does it begin after 5.65 us?
                time_bins.f90 moidified so that phi vs time are written to spin_phase_vs_time.dat
                Run with same input.dat and see what we learn from phase advance. Note that T0 is 2ns.
                config_044/20230425_084314 out3.dat

                20230424_111244 and 20230425_084314 are not the same. Very different. Maybe the dphi/ddelta fit is systematic. Try set coefficients to zero after each fit. 
                And run same input.dat again. config_044/20230425_200519 out4.dat

                Try again with quad map version of lattice in config_042. Comment out !call IBMS_lattice. in bmad_esquad_grid. With scraping.
                  20ns bins, Full Eremey, full T0   config_042/20230424_191150 out1.dat. Doesn't know what to do with IBMS3 so comment out call IBMS_lattice. in bmad_esquad_grid.
                  config_042/20230425_200405

April 26, 2023
              config_044/ 20230424_111244, 20230425_083414 and 20230425_200519 all have same input file but very different outcome. In most recent had reset parameters for fitting routine on
              each call, but still get consistently  same fit values and oscillation at revolution frequency.
              This used the full T0. Maybe it's all about the T0.
                            
               config_042/20230425_200405 - random input file. quad maps with scraping 20ns bins no oscillation. looks reasonable but very different from full T0 eremey in config_044
               
               config_042/20230426_080440 out2.dat - quad maps, scraping, full Eremey and T0
               config_042/20230426_080713 out3.dat - precisely the same
                  Results not exactly the same. Some variation time bin to time bin. Average is about -25 rad                
 

               Try config_044 (quad multipoles) with 20ns bins
                  20230426_075827  out1.dat
                    This is the same as config_042/ with quad field maps. Full Eremey and T0. All with 20ns time bins. Converges to about -10 rad. One difference is quad scraping.

              Try config_042 with quad scraping turned off config_042/20230426_164546 out5.dat

              What we know.
                 Multipoles with 20ns bins gives same result as maps with 20ns bins as long as scraping is turned off. Scraping off dphi/ddelta=-12
                 Scraping turned on and dphi/ddelta = -26

                 There is an oscillation with map not present in multipoles at revolution frequency.


            With no scraping
              Try set polarization to antiparallel. config_042/20230426_205159 out1.dat                     kicker correlation    +7rad
              Try set polarization to antiparallel and 2ns T0 pulse config_042/20230426_205251 out2.dat     no correlations        0
              Try full Eremeny. T0=2ns.   config_042/20230426_205342 out3.dat                               beam line correlation -10 rad

            With scraping
              Try set polarization to antiparallel. config_042/20230427_071037 out1.dat                     kicker correlation      4
              Try set polarization to antiparallel and 2ns T0 pulse config_042/20230427_070939 out2.dat     no correlation          0
              Try full Eremeny. T0=2ns.   config_042/20230427_070843 out3.dat                               beam line correlation  -5 rad

            CODE MODIFICATION
             modify tracking_master, time_bins, muon_interface so that if there are CALO names in the lattice, only data at CALO locations will be used to bin times
             This is for use with lattice with multipole quads that have call, calo_lattice.

             Test this latest feature in config_044/20230427_100445 out1.dat 20ns bins, full Eremey and T0  -15 rad with some big oscillations at rev freq  -15rad
             Test also with 10ns bin config_044/20230427_202148 out1.dat  -10 rad
                            30ns bin config_044/20230427_202238 out2.dat  -32 rad

April 28. 2023

                dphi/ddelta seems to increase with width of time bin. 
                The smallest bin with the 24 calos is 149/24.=6.22.
                6.5ns time bin  config_044/20230428_081936 out3.dat

                Call, extra_calorimeter_lattice. in bmad_esquad_curvilinear_calo_extra. Doubles number of calorimeters to 48. 3.11ns
                Using this lattice with extra calorimeters run 6.5ns time bin same as 20230428_081936 to test
                config_044/20230428_105415 out4.dat

                No clue as to differences. Statistics.
                Try the same but with antiparallel. See if same anomaly appears.

                config_044/20230428_194515 out1.dat - extra calorimeters, spin antiparallel 6.5ns
                config_044/20230428_194703 out2.dat - 24 calorimeters, spin antiparallel 6.5ns   (1121 particles at end)
                 These agree pretty well. 7.1 rad. 24 cal is perhaps 0.1rad higher than 48 cal.
                 The above use kicker timing optimized for 2ns T0

April 29, 2023
                Now try with 2ns T0 - flat, tlength=2ns, setpolarization = F
                 config_044/20230429_062914  48calo   6.5ns
                 config_044/20230429_062955  24calo   6.5ns (5059 particles at end)

                And compare those to extra. with 3.5ns bin
                  config_044/ 20230429_080349 out5.dat  3.5ns

                All pretty consistent with 2ns T0, setpolarization = F
                Try a few more, with 10ns and 15ns bins in 24 cal lattice. (quicker)

                config_044/20230429_195145 out1.dat  10ns bin
                config_044 /20230429_195240 out2.dat 15ns bin

April 30, 2023
               All are the same! that is with 2ns T0- flat, setpolarization =F all come out to -12 rad.
               Note that the above with 2ns T0, flat use     kicker_tStart = 280.0e-9   280.0e-9 280.0e-9  !  best for tdistr  = "flat", muon distribution width                

               Let's try a couple more with setpolarization=T, antiparallel.
               Restore     kicker_tStart = 210.0e-9   210.0e-9 210.0e-9 ! best for tdistr = "e989-P1", scan with 224G      
                   config_044/ 20230430_084339 out1.dat antiparallel, full eremey and T0, 24calos, 15ns
                   config_044/ 20230430_084452 out2.dat antiparallel, full eremeny and T0, 24cals, 10ns
                   config_044/ 20230430_084539 out3.dat antiparallel, full eremeny and T0, 24cals, 6.5ns
                   config_044/ 20230430_084707 out4.dat antiparallel, full eremeny and T0m 48 cals, 3.5 ns   (3272 particles at end)
                  3.5 and 6.5 ns zero rad, 10 and 15ns ~1.

               But compared to the same but with 280ns tStart which gets 7rad

               Huh? Just to be sure go back to 24 calos and tstart 280ns 20230430_194501 out6.dat  ratty, 7rad, only (1076 particles at end)

May 1, 2023
               Well, let's take that latest at face value. Namely we see that if the kicker timing is set for good capture with full T0, that kicker contribution to dphi/ddelta is very small
               We also find that with narrow T0, and kicker timed for best capture (280ns) that we get -12 rad
               So next try full T0, eremey, setpolarization false and kicker for best capture with full kick (210ns)

               kickstart=210ns, full eremey, full t0
               time_bin 3.5ns 20230501_092315 out1.dat  (48cal)          -1 rad     
                        6.5ns 20230501_092403 out2.dat  (24cal)          +2 rad
                        10ns  20230501_092457 out3.dat (24cal)   (3328 particles at end)  -18 rad

                        15ns 20230501_195949 out4.dat                         -8 rad
                        10ns 20230501_200740 out3.dat sanity check            -7 rad

May 2, 2023
                        10ns 20230502_090721 out5.dat insanity check -same input.dat as  20230501_200740 and 20230501_092457
                        10ns 20230502_090721 -23 rad

                         Just tried 3 runs with 10ns, full eremey and full t0, identical input.dat and get -18 rad, -7 rad, -23rad
                         Is it jsut statistics?

                         Try running 4 identical at 10ns with 600k events.

                         20230502_165625/ out1.dat  -4rad
                         20230502_165635/ out2.dat  -8.5rad
                         20230502_165649/ out3.dat  -7.2rad
May 3, 2023
                           They are easily all the same within the error bars

                         Create new directory config_045
                           do 7 2008 runs to get all muons in eremey distribution of 200 each same input.dat as for config_044/20230502_165649 (except 200k vs 600k events, and starting muon from 0 to 1400k
                            20230503_193823 out1.dat 0-200k         -24rad
                            20230503_194005 out2.dat 200k-400k      -4rad
                            20230503_194125 out3.dat 400k-600k      +4rad
                            20230503_194207 out4.dat 600k-800k      -14rad

May 4, 2023                 Set save_element_by_element_info = F so that not writing so much
                            Otherwise only change to input.dat is to starting muon number
                            20230504_035918 out1.dat 800k-1000k     -18rad
                            20230504_040030 out2.dat 1000k-1200k    0 rad
                            20230504_040135 out3.dat 1200k-1400k    0 rad

                               That's all there is. Now start over
                            20230504_040502  out4.dat 0-200k  -5 rad
                            Average of all 8 so far -6.5 rad

                            Another set of 4
                            20230504_103424 out1.dat 200k-400k  -9 rad 
                            20230504_103511 out2.dat 400k-600k  -13 rad
                            20230504_103600 out3.dat 600k-800k  -13 rad
                            20230504_103705 out4.dat 800k-1000k  -27 rad

                         Fix up compile_domega_vs_time.f90 so that it writes a file to load directories to gnuplot. Writes directory list to fort.401
                       
                            average of all so far -11 rad

                           Another set of 4
                            20230504_180432 out1.dat  1000k-1200k  -15rad
                            20230504_180551 out2.dat  1200k-1400k  0rad
                            20230504_180642 out3.dat  0-200k      -20rad
                            20230504_180946 out4.dat  200k-400k   -12rad

May 5, 2023
                             average of all 16 files -12 rad
                           Just for fun. Find the random call in setup of particle distribution and set it so always the same.
                           Maybe setting seed=1 in input.dat will do it.

                           20230505_071107/  out1.dat 20k particles, seed=1 
                           20230505_071116/  out2.dat 20k particles, seed=1 
                            Results are not identical. Look in create_phase_space.f90 - call ran_seed_put(1) before looping fnalw that generates times

                           20230505_150320  out1.dat
                           20230505_150327  out2.dat  the times are different in IBMS1_phase_space.dat


May 6, 2023

                           Try setting random seed directly in function fnalw
                           20230506_010806 out1.dat  just 2k muons
                           20230506_010906 out2.dat
                             That seemed to do the trick.

                           test with 20k muons
                            20230506_012033 out1.dat
                            20230506_012107 out2.dat
                               These two are the same. So it's all about assigning the time

                           
May 9, 2023
                         Submit 4 more jobs with 200k events using same input.dat as from 20230504_180946
                              config_045/20230509_083638 out1.dat  400k-600k   -4.5rad
                                         20230509_083747 out2.dat  600k-800k   -24 rad
                                         20230509_083830 out3.dat  800k-1000k  -19 rad
                                         20230509_083926 out4.dat  1000k-1200k  -12 rad

                              Create directory config_046
                              Move subdirectories that are not 200k runs in config_045 to config_046
                               This includes subdirectories from May 5   20230505_071107  20230505_071116 20230505_150320  20230505_150327
                                                             and May 6   20230506_010806  20230506_010906 20230506_012033 20230506_012107


                              Running average in config_045 is -12 +/- 4 rad

                              In create_phase_space.f90 put seed=0 back into function fnalw

                             Submit 4 more jobs 
                                         20230509_210148 out1.dat 1200k - 1400k  -0.5 rad
                                         20230509_210241 out2.dat 0-200k         -22 rad
                                         20230509_210339 out3.dat 200k-400k      -8.5 rad
                                         20230509_210419 out4.dat 400k-600k       2 rad

                                         average of all -11 +/- 3 rad      

May 10, 2023
                              Submit 4 more jobs
                                         20230510_102330/ out1.dat 600k-800k  -24 rad
                                         20230510_102859 out2.dat 800k-1000k  -8 rad
                                         20230510_102949 out3.dat 1000k-1200k  -4 rad
                                         20230510_103037 out4.dat 1200k-1400k  2 rad                                      
May 12, 2023
                                         20230512_152228 out1.dat 0-200k      -16.5 rad
                                         20230512_152319 out2.dat 200k-400k   -12 rad
                                         20230512_152544 out3.dat 400k-600k   -7.5 rad
                                         20230512_152622 out5.dat 600k-800k   -22 rad

May 13, 2023
                                     combined -11 +/- 3
                                       20230513_084756 out1.dat 800k-1000k   -17 rad
                                       20230513_084901/ out2.dat 1000k-1200k  -10 rad
                                       20230513_084942 out4.dat 1200-1400k    1.5 rad
                                       20230513_085033 out3.dat 0-200k        -5 rad

                                        -11 +/- 2.5

                                       20230513_192125 out1.dat 200k-400k   -2.5 rad
                                       20230513_192402/ out2.dat 400k-600k  -6 rad
                                       20230513_192435  out3.dat 600k-800k  -21.5 rad
                                       20230513_192520 out4.dat 800k-1000k  -15.5 rad
                                        =11.5 +/-2

May 14, 2023
                                       Historgram is beginning to take on shape with peak at -11.5 but still broad

                                       Let's move to tests with no coorelation in incoming beam (polarization antiparallel)
                                            in config_047                        
                                           20230514_064852/ out1.dat 0-200k    0. rad
                                           20230514_065017/ out2.dat 200k-400k   0.5 rad
                                           20230514_065216 out3.dat  400k-600k  -0.5 rad
                                           20230514_065306/ out4.dat 600k-800k   0.4 rad

                                         average is 0 +/- 8 rad

                                           20230514_150725/ out1.dat 800k-1000k   0.7 rad
                                           20230514_150815  out2.dat 1000k-1200k  1.6 rad
                                           20230514_150907  out3.dat 1200k-1400k  1.5 rad
                                           20230514_151002  out4.dat 0-200k       1.1 rad

                                          average 0.5 +/- 5 rad

                                          create config_048 - same as config_047 but shift kicker time 30ns     kicker_tStart =   240.0e-9  !  shift time 30ns later to see what happens to dphi/delta
                                            20230514_214846 out1.dat 0-200k         4.5 rad
                                            20230514_214940 out2.dat 200k-400k      4 rad
                                            20230514_215020 out3.dat 400k-600k      3.5 rad
                                            20230514_215103 out4.dat 600k-800k      3.5 rad

May 15, 2023
                                          average 3.9 +/- 9

                                       Create config_049 and try same again but with kicker shifted -30ns
                                          kicker_tStart = 180.0e-9 !  shift time 30ns earlier to see what happens to dphi/delta
                                          20230515_075214/ out1.dat 0-200k  -3.1 rad
                                          20230515_075407/ out2.dat 200k-400k  -2.6 rad
                                          20230515_075457 out3.dat 400k-600k   -4     rad
                                          20230515_075545 out4.dat 600k-800k  -4.3 rad

                                          average = -3.5 +/-8.5

                                           20230515_144034/ out1.dat  800k-1000k    -3 rad
                                           20230515_144051/ out2.dat  1000k-1200k   -2.9 rad
                                           20230515_144254  out3.dat 1200k-1400k    -2.2 rad
                                           20230515_144329 out4.dat 0-200k          -2.2 rad

                                        average = -3.0 +/- 6

                                    Create energy_vs_time_vs_turn/dphi_de_doc for ppt 

                                    Most of above is with 10ns bin. Try 6.5ns. Create config_050
                                    copy input.dat from config_045 but with 6.5ns bin
                           config_050/20230515_205927 out1.dat 0-200k   -1 rad
                                      20230515_210052 out2.dat 200k-400k  -1 rad
                                      20230515_210146 out3.dat 400k-600k  9 rad
                                      20230515_210228 out4.dat 600k-800k  -9 rad

May 16, 2023
                           config_050/20230516_073122 out1.dat 800k-1000k  -23 rad
                                      20230516_073214 out2.dat 1000k-1200k -15.5 rad
                                      20230516_073301 out3.dat 1200k-1400k  2.3 rad
                                      20230516_073358 out4.dat 0-200k   -8.5 rad
                                 average -6.2 rad

                           config_050/20230516_192922 out1.dat  200k-400k   -11 rad
                                      20230516_193001 out2.dat  400k-600k   4 rad
                                      20230516_193041 out3.dat  600k-800k   -10 rad
                                      20230516_193153 out4.dat  800k-1000k   -24 rad
                                  average -7.3 +/-5 rad

May 17, 2023       
                           config_050/20230517_101700/ out1.dat 1000k-1200k  -15 rad
                                      20230517_101748  out2.dat 1200k-1400k  -2 rad
                                      20230517_101835 out3.dat 0-200k        -13 rad
                                      20230517_101916/ out4.dat 200k-400k    -10 rad
                                   average -7.9 rad

                          config_050/20230517_183855/ out1.dat 400k-600k  8.5 rad
                                     20230517_183936  out2.dat 600k-800k  -12.5 rad
                                     20230517_184018  out3.dat 800k-1000k  -17 rad
                                     20230517_184056  out4.dat 1000k-1200k   -6 rad

May 18, 2023
                                  average -7.7 rad
                           config_050/20230518_082422 out1.dat  1200k-1400k  -2
                                      20230518_082506 out2.dat 0-200k  -16
                                      20230518_082551 out3.dat 200k-400k  -6.5
                                      20230518_082632 out4.dat 400k-600k  -7

                                  average -7.6 (but the peak of the histogram is at -12?

                           config_050/20230518_154250 out1.dat 600k-800k  -15
                                      20230518_154344 out2.dat 800k-1000k   -31
                                      20230518_154419  out3.dat 1000k-1200k -15
                                      20230518_154519 out4.dat 1200k-1400k  -3

                                      20230518_215114 out4.dat 0-200k     -9.5
                                      20230518_215230 out5.dat 200k-400k  -4.5
                                      20230518_215318 out6.dat 400k-600k   -3
                                      20230518_215407 out7.dat 600k-800k -18
May 19, 2023
                                 average = -8.8 rad
                                    
                           config_050/20230519_070903 out1.dat 800k-1000k  -20
                                      20230519_070954  out2.dat 1000k-1200k  -14
                                      20230519_071040 out3.dat 1200k-1400k   -4
                                      20230519_071136 out4.dat 0- 200k     -19

                           config_050/20230519_164053/ out1 200k-400k        0
                                      20230519_164105/ out2.dat 400k-600k    3
                                      20230519_164117/ out3.dat 600k-800k   -27
                                      20230519_164127/ out4.dat 800k-1000k  -22

                                  average -9.5 +/- 2.5

May 20, 2023
                       Go back to setting polarization in initial distribution but with 6.5 ns bin
                            Create config_051 for polarization = antiparallel (like config_047 but with 6.5ns
                                config_051/20230520_085559/ out1.dat 0-200k   0.1
                                           20230520_090740  out2.dat 200k-400k 1.7
                                           20230520_090753/ out3.dat 400k-600k 1.0
                                           20230520_090804  out4.dat 600k-800k 0.4

                                           20230520_155550/  out1.dat  800k-1000k 0.6
                                           20230520_155601/  out2.dat  1000k-1200k 1.1
                                           20230520_155612/  out3.dat  1200k-1400k  1.0     
                                           20230520_155621/  out4/dat  0 - 200k  0.25
                                           average   1 +/-6 rad

May 21, 2023
             Find that in create_phase_space the times in the initial distribution are overwriten with times drawn from the distribution (for example the fnalw distribution). 
             The times in the distribution generated by Eremeny are very close together and presumably neglecting them makes no difference but they do hold some information about correclation with
             momentum and therefore with spin. The times in the Eremey distribution are all within +/- 4 ns. In the momentum range +/- 0.01, times are in range +/-1 1.25 ns.
             In momentum range +/- 0.006 times are in range +/- 1ns. Not likely to make any difference if the times are all assumed to be zero.       

                 Next repeat retarded kick as in config_048 but this time with 6.5ns bin in config_052
                  config_052 - retarded kick, antiparallel polarization, 6.5ns bin
                      config_052/20230521_101734/ out1.dat 0-200k   2.2
                                 20230521_101854/  out2.dat 200-400k 5.5
                                 20230521_101905/  out3.dat 400-600k 4.2
                                 20230521_101915/  out4.dat 600-800k 5.1

                 Next repeat retarded kick as in config_049 but this time with 6.5ns bin in config_053
                  config_053 - advanced kick, antiparallel polarization, 6.5ns bin                 
                    config_053/20230521_190500/ out1.dat  0-200k       -3.5
                               20230521_190528/ out2.dat 200-400k      -3.5
                               20230521_190539/ out3.dat 400k-600k     -4
                               20230521_190550/ out4.dat 600k-800k     -2.6

                       average -3.45 +/- 8


May 23, 2023
            Prepare presentation for 5/24 BD meeting /energy_vs_turn_vs_time/dphi_de_doc/dphi_de.pptx

              D. Tarazona gets opposite sign dph/d(delta) using Diktys distribution. 
              Try that distribution in /config_054/  (copy input.dat from config_050)
                20230523_174104/  out1.dat  0-184.5k
                20230523_174319/  out2.dat  184.5k-369k
                20230523_174342/  out3.dat  0-184.5k
                20230523_174356/  out4.dat  184.5k-369k

May 26, 2023
            Revisit grid layout so that scraping can be turned on.
            The element 20-DFREE is the longest in the ring. So add an element called SPLIT_DFREE
            Use config_055 to investigate. 
            Copy config_042/bmad_esquad_grid. to config_055
               With addition of SPLIT_DFREE - maximum spacing of element ends is <13 ns. Try 13ns bins
               Lattice with SPLIT_DFREE is named bmad_esquad_grid_split_dfree.
               Input.dat - use bmad_esquad_grid_split_dfree., 13ns bin, Emerey dist. tstart=210ns, set polarization F  
	       20230526_094420/ out1.dat 0-200k
	       20230526_094431/ out2.dat 200k-400k
	       20230526_094443/ out3.dat 400k-600k
	       20230526_094454/ out4.dat 600k-800k
               
	       20230526_144512/ out1.dat 800k-1000k
	       20230526_144520/ out2.dat 1000k-1200k
	       20230526_144528/ out3.dat 1200k-1400k
	       20230526_144539/ out4.dat 0-200k

	       20230526_205336/  out1.dat 200k-400k
	       20230526_205350/  out2.dat 400k-600k
	       20230526_205358/  out3.dat 600k-800k
	       20230526_205407/  out4.dat 800k-1000k

May 27, 2023
                config_055
		20230527_070502/  out1.dat 1000k-1200k
		20230527_070512/  out2.dat 1200k-1400k
		20230527_070527/  out3.dat 0-200k
		20230527_070542/  out4.dat 200k-400k

               Average = -12+/- 5, scraping turned on and 13ns bins and grid field. call quad_plate_misalign.bmad which does have some quad plate misalignment

              create config_056 and set polarization antiparallel and pulse width 1ns. Set quad_plate_misalign to zero
                     kicker_tStart            =   280.0e-9   280.0e-9 280.0e-9   !   best for tdistr    = "flat", muon distribution width = 1 ns
              

             config_056/20230527_193154/  out1.dat 0-200k
	                20230527_193249/  out2.dat 200k-400k
			20230527_193303/  out3.dat 400k-600k	
			20230527_193312/  out4.dat 600k-800k
                      average = -0.1 rad
                        20230528_061932/ out1.dat 800-1000k
			20230528_061949/ out2.dat 1000k-1200k
			20230528_062005/ out3.dat 1200k-1400k
			20230528_062025/ out4.dat 0k-200k
                      average = -0.1 +/-5

May 28, 2023
              config_057 set polarization F, pulse width 1ns Set quad_plate_misalign to zero  (copy from config_056 and set polarization F
	      		 20230528_192751/  out1.dat 0-200k
  		   	 20230528_192951/  out2.dat 200k-400k
			 20230528_193000/  out3.dat 400k-600k
			 20230528_193016/  out4.dat 600k-800k

May 29 2023
              config_057 - narrow pulse
	      		 20230529_073323/ out1.dat 800k-1000k
			 20230529_073334/ out2.dat 1000k-1200k
			 20230529_073343/ out3.dat 1200-1400k
			 20230529_073356/ out4.dat 0-200k
                     average = -10+/-5

		     	 20230529_131435/ out1.dat 200k-400k
			 20230529_131443/ out2.dat 400k-600k
			 20230529_131452/ out3.dat 600k-800k
			 20230529_131502/ out4.dat 800k-1000k
                     average = -11.2+/-4

May 30, 2023
           Modify manage_polarization (subroutine in create_phase_space.f90).
             Setting polarization = 'spintune' computes phase shift phi going around DR 4 times. polarization is set at angle phi with respect to momentum + gaussian smear with width width=0.2 radians
             Try this out in config_058

                      with 2000 muons  - <100 survive end up with -50 rad
                      with 20,000     483 survive 2 rad
                 config_058 - polarization =  'spintune', width=0.2rad, 1ns T0 pulse, otherwise the same as config_057
		 	    20230530_111203/  out1.dat 0-200k
			    20230530_111215/  out2.dat 200k-400k
			    20230530_111234/  out3.dat 400k-600k
			    20230530_111245/  out4.dat 600k-800k
                        average = -1.3 +/- 6 rad

				20230530_165336/ out1.dat 800-1000k
				20230530_165345/ out2.dat 1000k-1200k
				20230530_165354/ out3.dat 1200k-1400k
				20230530_165406/ out4.dat 0-200k
                         average = -1.3 +/- 4

                 config_059 - polarization = 'spincoord' width=0.2rad, 1ns T0 pulse, phi = 0.49 * x'
                      initial test with 20k particles  10 rad.
		      config_059/20230530_212603/ out1.dat 0-200k
                                 20230530_212622/ out2.dat 200k-400k
				 20230530_212631/ out3.dat 400k-600k
				 20230530_212639/ out4.dat 600k-800k
                  
May 31, 2023
			config_059/20230531_120017/ out1.dat 800k-1000k
                                   20230531_120030/ out2.dat 1000k-1200k
				   20230531_120040/ out3.dat 1200k-1400k
				   20230531_120053/ out4.dat 0-200k
                            average = 2.4 +/- 4

                       config_059/20230531_185615/ out1.dat 200k-400k
		                  20230531_185624/ out2.dat 400k-600k
				  20230531_185632/ out3.dat 600k-800k
				  20230531_185640/ out4.dat 800k-1000k

                               average = 1.8 +/-3

            Modify manage polarization so that if polarization = spintune  phi = pi + 4(2*pi*gamma*amu) + noise
                                                  polarization = spincoord  phi = pi + (dphi/dx') x' + noise



June 5, 2023
           Modify phase_space_to_histogram to plot <phi> vs yp. Extract coefficients for fitted cubic and modify create phase space so that it will use this cubic to generate the correlation.
           Test this new effect in config_060/20230605_190223

June 8, 2023 - Find a bug in time_bins. subroutine leftright - returning sXp instead of sxp''
                 Fix bug - test in config_061 - copy files from config_057

                 Seems to check out
		 config_061/20230608_192807/ out1.dat 0-200k
		            20230608_192820/ out2.dat 200k-400k
			    20230608_192829/ out3.dat 400k-600k
			    20230608_192839/ out4.dat 600k-800k

June 9,2023
                            20230609_105130/ ou1.dat 800k-1000k
			    20230609_105146/ ou2.dat 1000-1200k
			    20230609_105206/ ou3.dat 1200k-1400k
			    20230609_105220/ ou4.dat 0-200k

              Implement 'new' definition of phase in time_bins. The new definition is phase in the p-x plane.

                !Find the part of the polarization in the x-p plane
		! The unit vector normal to the x-p plane is
		khat = (/zero,-pvec(3),pvec(2)/)
		khat = khat /sqrt(dot_product(khat,khat))
		!the polarization in the x-p plane is
		sdir = svec-dot_product(svec,khat)*khat
		svec = sdir /sqrt(dot_product(sdir,sdir))

               another bug. Must evaluate s dot p after call to RightOrLeft

               check it out in config_062 - copy from config_061
              
             Set up config_062 on lnx6186
              submit 
	        config_062/7184338/log.dat: Skip          0 muons 20230609_192559
		config_062/7184339/log.dat: Skip     200000 muons 20230609_192747
		config_062/7184340/log.dat: Skip     600000 muons 20230609_192815
		config_062/7184341/log.dat: Skip     800000 muons 20230609_192832
		config_062/7184342/log.dat: Skip     800000 muons 20230609_192845
		config_062/7184343/log.dat: Skip    1200000 muons 20230609_193357
		config_062/7184344/log.dat: Skip    1200000 muons 20230609_193441
		config_062/7184345/log.dat: Skip    1000000 muons 20230609_193512
		config_062/7184346/log.dat: Skip    1000000 muons 20230609_193541

        Another bug in time_bins
               if(m>=3 .and. all(err_avg(1:m) > 0) )then  !, otherwise numerical subroutine fit throws an assert_eq error
                 error is call from fit line 43. Replace call to gammq with type out. In fit.f90 in reciples

June 10, 2023
         Failed with assert2 in mumerical recipes. Modify nruitl.f90 which contains function assert2 so that it does not stop with error

               config_062 on lnx6186 - Ihad neglected to update g2_stracking. Start over but this time with 150k events/job
                config_062/7184349/log.dat: Skip          0 muons 20230610_074429	
		config_062/7184350/log.dat: Skip     150000 muons 20230610_074600 
		config_062/7184351/log.dat: Skip     300000 muons 20230610_074629 
		config_062/7184352/log.dat: Skip     450000 muons 20230610_074746 
		config_062/7184353/log.dat: Skip     600000 muons 20230610_074859 
		config_062/7184354/log.dat: Skip     750000 muons 20230610_075013 
		config_062/7184355/log.dat: Skip     900000 muons 20230610_075113	
		config_062/7184356/log.dat: Skip    1050000 muons 20230610_075258 
		config_062/7184357/log.dat: Skip    1200000 muons 20230610_075458 
		config_062/7184358/log.dat: Skip          0 muons 20230610_075628 
		config_062/7184359/log.dat: Skip     150000 muons 20230610_075713 


      create lnx6186 config_063 to find assert error - copy files from config_062
         found and fixed with a change in recipes routine fit
    !		if (ndata > 2) q=gammq(0.5_dp*(size(x)-2),0.5_dp*chi2)  (this is where it had crashed)
		if (ndata > 2)then
                   if(0.5_dp*(size(x)-2) <0.0_dp .or.0.5_dp*chi2 <=0)then 
                         print '(a,a,i10,a,es12.4)',' gammq_s call gives error ', 'size(x) = ',size(x),' chi2 = ', chi2
                   endif
                endif

      try a few runs in config_062 (mac) to compare with lnx6186
                20230610_202407/ out1.dat: Skip          0 muons
		20230610_202421/ out2.dat: Skip     200000 muons
		20230610_202430/ out3.dat: Skip     400000 muons
		20230610_202440/ out4.dat: Skip     600000 muons

June 11, 2023
             Still not right on mac. Look for bug in time_bins. Find that Rzp in RightOrLeft had not been initialized. Linux fortran didnot have a problem with this

           Submit more jobs on linux. Just to be sure we are using the right exe, rebuild after submitting 7192947, but before 7192948

              config_062/7192940/log.dat: Skip     300000 muons 20230611_113846
	      config_062/7192941/log.dat: Skip     450000 muons 20230611_113930
	      config_062/7192942/log.dat: Skip     600000 muons 20230611_114030
	      config_062/7192943/log.dat: Skip     750000 muons 20230611_114131
	      config_062/7192944/log.dat: Skip    1000000 muons 20230611_114430
	      config_062/7192945/log.dat: Skip     900000 muons 20230611_114714
	      config_062/7192946/log.dat: Skip    1150000 muons 20230611_114814
	      config_062/7192947/log.dat: Skip          0 muons 20230611_115014
	      config_062/7192948/log.dat: Skip     150000 muons 20230611_115415

            Start jobs on mac config_062
 
                  20230611_153100/ out1.dat: Skip          0 muons
		  20230611_153114/ out2.dat: Skip     200000 muons
		  20230611_153124/ out3.dat: Skip     400000 muons
		  20230611_153134/ out4.dat: Skip     600000 muons

		  20230611_211040/ out5.dat: Skip     800000 muons
		  20230611_211051/ out6.dat: Skip    1000000 muons

		  20230611_212152/  out7.dat: Skip    1200000 muons
		  20230611_212208/  out8.dat: Skip          0 muons
		  
                  
first type C-x SPC  to get in rectangle mode
Then set cursor at upper left corner
C - SPC
set cursor at lower right corner
C-x r M-w
Move cursor to new location for rectangle
C-x r y

June 12, 2023
  
                  20230612_074929/ out1.dat: Skip     200000 muons
		  20230612_074939/ out2.dat: Skip     400000 muons
		  20230612_075145/ out3.dat: Skip     550000 muons
		  20230612_075157/ out4.dat: Skip     750000 muons

               average = -9.1/ +/- 4

June 13, 2023
          lnx6186 config_064 jobs with true T0
            copy from mac config_055

	           config_064/7202091/log.dat: Skip          0 muons   20230613_061347 
		   config_064/7202092/log.dat: Skip     150000 muons   20230613_061447
		   config_064/7202093/log.dat: Skip     300000 muons   20230613_061632
		   config_064/7202094/log.dat: Skip     450000 muons   20230613_061747
		   config_064/7202095/log.dat: Skip     600000 muons   20230613_061847
		   config_064/7202096/log.dat: Skip     750000 muons   20230613_061946
		   config_064/7202097/log.dat: Skip     900000 muons   20230613_062117
		   config_064/7202098/log.dat: Skip    1050000 muons   20230613_062202
		   config_064/7202099/log.dat: Skip    1200000 muons   20230613_062301
		   config_064/7202100/log.dat: Skip      50000 muons   20230613_062431
		   config_064/7202101/log.dat: Skip     200000 muons   20230613_062531
		   config_064/7202102/log.dat: Skip     200000 muons   20230613_062616
		   config_064/7202103/log.dat: Skip     400000 muons   20230613_062731
		   config_064/7202104/log.dat: Skip     600000 muons   20230613_062816
		   config_064/7202105/log.dat: Skip     800000 muons   20230613_062915
		   config_064/7202106/log.dat: Skip    1000000 muons   20230613_062945
		   config_064/7202107/log.dat: Skip    1200000 muons   20230613_063016

                 average =-11.4 +/-5 rad

              create subroutine get_all_dated_directories. Gets the list of dated directories. Reads command line. single directory, min and max directories by date, CR - all directories
              call get_all_dated_directories from phase_space_to_histogram so can include all data. But so far fit of 2D histogram in program does not agree with gnuplot fit. Most egregiously
              the y_offset is inconsistent.

June 14, 2023
       On lnx6186 in config_064 find that domega_vs_time.dat from different runs are not always 1-1. Modify compile_domega_vs_time.f90 so that it checks by index in the file rather than index in do loop.
       It ran out of unit numbers to open.          



June 17, 2023
        On lnx6186 modify manage_polarization in create_phase_space.f90 so that the angle is in the x-p plane
        try out in config_065 (initialized by copying from config_066)
           polarization = "spincoordx"

                   config_065/7202252/log.dat: Skip          0 muons  20230617_122036
		   config_065/7202253/log.dat: Skip     150000 muons  20230617_122411
		   config_065/7202254/log.dat: Skip     300000 muons  20230617_122511
		   config_065/7202255/log.dat: Skip     450000 muons  20230617_122542
		   config_065/7202256/log.dat: Skip     600000 muons  20230617_122607
		   config_065/7202258/log.dat: Skip     750000 muons  20230617_122637
		   config_065/7202259/log.dat: Skip     900000 muons  20230617_122710
		   config_065/7202260/log.dat: Skip    1050000 muons  20230617_122737
		   config_065/7202261/log.dat: Skip    1200000 muons  20230617_122808
June 18, 2023
                 avg dphi/ddelta = 10. with initialized spincoordx set to phi=10*x. For some reason it appears as phi=-10*x in phase space output. Possible because at x=0, phi=0 rather than pi.
                 fix up create_phase_space so at x=0, phi=pi and try again config_066

                   config_066/7202305/log.dat: Skip          0 muons  20230618_170159
		   config_066/7202306/log.dat: Skip     300000 muons  20230618_170230
		   config_066/7202307/log.dat: Skip     450000 muons  20230618_170245
		   config_066/7202308/log.dat: Skip     600000 muons  20230618_170315
		   config_066/7202309/log.dat: Skip     750000 muons  20230618_170327
		   config_066/7202310/log.dat: Skip     750000 muons  20230618_170339
		   config_066/7202311/log.dat: Skip    1050000 muons  20230618_170409
		   config_066/7202312/log.dat: Skip    1050000 muons  20230618_170423
		   config_066/7202313/log.dat: Skip    1200000 muons  20230618_170454

                  avg = 6
                  analysis with differential_decay_dependencies shows phi(x) is cubic with coef1=90 (even though input is 10), phi(delta) = flat, 
                    in this case I had set manage_polarization so that x=0 => phi=pi

June 20, 2023	    
                  config_066 - some screwing around to compute phi in phase_space_to_histogram with sensible sign since average is pi.
                         great idea of signed_phi doesn't really pan out.


                  config_067 - try a run with 'use_as_is' so no confusion about manipulating beta and losing correlation with spin

                        config_067/7202414/log.dat: Skip          0 muons  20230620_192800
			config_067/7202415/log.dat: Skip     200000 muons  20230620_192855
			config_067/7202416/log.dat: Skip     400000 muons  20230620_192910
			config_067/7202417/log.dat: Skip     600000 muons  20230620_192924
			config_067/7202418/log.dat: Skip     800000 muons  20230620_192958
			config_067/7202419/log.dat: Skip    1000000 muons  20230620_193122
			config_067/7202420/log.dat: Skip    1200000 muons  20230620_193152
			config_067/7202421/log.dat: Skip          0 muons  20230620_193223
			config_067/7202425/log.dat: Skip     200000 muons  20230620_193237
			config_067/7202426/log.dat: Skip     400000 muons  20230620_194138
			config_067/7202427/log.dat: Skip     600000 muons  20230620_194223
			config_067/7202428/log.dat: Skip     800000 muons  20230620_194323
			config_067/7202429/log.dat: Skip    1000000 muons  20230620_194408

June 22, 2023
          Coding: Fix up energy_vs_time.f90. the program matches muons in one phase space file with those in another, for example, muons that are stored with muons before injection.
                   The new code writes a single file that has phase space at both places.
                   Also there is an option to select which ... phase_space.dat files to select with argument list.

                   time_bins.f90 - calls write_phase_space_at_time to write the phase space at a particular time.
                                   Tentatively at 7/8 max time and max time.

                   create_config_068 to test this new feature, copying from config_066

                         config_068/7202581/log.dat: Skip          0 muons 20230622_135130
			 config_068/7202583/log.dat: Skip     150000 muons 20230622_135219
			 config_068/7202584/log.dat: Skip     300000 muons 20230622_135244
			 config_068/7202585/log.dat: Skip     450000 muons 20230622_135300
			 config_068/7202586/log.dat: Skip     600000 muons 20230622_135331
			 config_068/7202587/log.dat: Skip     750000 muons 20230622_135400
			 config_068/7202588/log.dat: Skip     900000 muons 20230622_135457
			 config_068/7202589/log.dat: Skip    1050000 muons 20230622_135527
			 config_068/7202590/log.dat: Skip    1200000 muons 20230622_135558
 


                  create config_069 copy from config_064 - true T0 set_polarization=F scraping on

config_069/7202625/log.dat: create directory = 20230622_185735  config_069/7202625/log.dat: Skip          0 muons
config_069/7202626/log.dat: create directory = 20230622_190148	config_069/7202626/log.dat: Skip     150000 muons
config_069/7202627/log.dat: create directory = 20230622_190220	config_069/7202627/log.dat: Skip     300000 muons
config_069/7202628/log.dat: create directory = 20230622_190236	config_069/7202628/log.dat: Skip     450000 muons
config_069/7202629/log.dat: create directory = 20230622_190321	config_069/7202629/log.dat: Skip     600000 muons
config_069/7202631/log.dat: create directory = 20230622_190519  config_069/7202631/log.dat: Skip     900000 muons
config_069/7202632/log.dat: create directory = 20230622_190735	config_069/7202632/log.dat: Skip    1050000 muons
config_069/7202633/log.dat: create directory = 20230622_190835	config_069/7202633/log.dat: Skip    1200000 muons

June 23, 2023

Average phi = -7.8 rad

7202719/log.dat: create directory = 20230623_115641  7202719/log.dat: Skip          0 muons
7202720/log.dat: create directory = 20230623_115711  7202720/log.dat: Skip     150000 muons
7202723/log.dat: create directory = 20230623_120226  7202723/log.dat: Skip     450000 muons
7202725/log.dat: create directory = 20230623_121025  7202725/log.dat: Skip     300000 muons
7202726/log.dat: create directory = 20230623_121125  7202726/log.dat: Skip     600000 muons
7202727/log.dat: create directory = 20230623_121225  7202727/log.dat: Skip     750000 muons
7202728/log.dat: create directory = 20230623_121325  7202728/log.dat: Skip     900000 muons
7202729/log.dat: create directory = 20230623_121426  7202729/log.dat: Skip    1050000 muons
7202730/log.dat: create directory = 20230623_121524  7202730/log.dat: Skip    1200000 muons

         Maybe the easiest for James to analyze by matching muons at time t after injection with muon before injection is
          to use all muons at the same time at injection, namely with a 2 ns
          create config_070 - 1ns T0, kicker timing delayed to 280e-9

config_070/7202806/log.dat: Skip          0 muons  20230623_170426
config_070/7202807/log.dat: Skip     150000 muons  20230623_171141
config_070/7202808/log.dat: Skip     300000 muons  20230623_171255
config_070/7202809/log.dat: Skip     450000 muons  20230623_171425
config_070/7202810/log.dat: Skip     600000 muons  20230623_171555
config_070/7202811/log.dat: Skip     750000 muons  20230623_171741
config_070/7202812/log.dat: Skip     900000 muons  20230623_171956
config_070/7202813/log.dat: Skip    1050000 muons  20230623_172155
config_070/7202814/log.dat: Skip    1200000 muons  20230623_172255


June 27, 2023
                  To understand evolution of phase better need some more code.
                  First - something to rewrite files in standard phase space format to more abbreviated format
                     write(lun(ix),'(a,a10,1x,14a16)')'!', 'muon', 'jx','jy', 'x', 'xp','y','yp','vec(5)','pz','t','s','s_x','s_y','s_z','acos(s . p)'
                     write(lun(ix),'(i10,1x, 14es16.8)') muon_number,jx,jy,  co%vec, co%t, co%s, co%spin,  spin_phase  
                   Modify phase_space_to_histogram.f90 for this purpose - the new file is original file name with _trimmed appended

                  in g-2/files/muons/ create  EndofM5_Valetov_withInit.dat_trimmed

                  Next generalize energy_vs_time so that it can read multiple parallel files (in addition to all directories) and write a single file with all relevant parameters
                  The new program energy_spin_vs_time.f90 - should eventually replace energy_vs_time.f90

July 3, 2023
               energy_spin_vs_time.f90 does everything that energy_vs_time.f90 did and more. Can read ten phase files at different times and identify particles that 
               are stored so that phase space of particles in injected distribution that are eventually stored can be investigated.

               For unknown reason, dphi/d(delta) of distributions upstream of injectin channel and at inflector exit had opposite sign of the stored distributions.
               Maybe because I was using a different algorithm to compute phase. (For upstream particles phase is computed using phase_space_to_histogram.f90)
               phase_space_to_histogram is updated so it calculates phase the same way as time_bins.f90. Will have to see what happens

July 4, 2023
               To try to understand why slope chnanges sign, 1) set time_binning_start to 10ns. 2) write 16 files 3) track for 10 turns only. To see where it flips.
               Do this with trial run in config_071. Start withinput from config_070 and just change number of turns

July 5, 2023
               Write output to 20 files. 10 turns.
	             config_071/7203850/log.dat: Skip          0 muons  20230705_205735  7203850
		     config_071/7203851/log.dat: Skip     150000 muons  20230705_205821  7203851
		     config_071/7203852/log.dat: Skip     300000 muons  20230705_205952  7203852
		     config_071/7203853/log.dat: Skip     600000 muons  20230705_210022  7203853
		     config_071/7203854/log.dat: Skip     450000 muons  20230705_210051  7203854
		     config_071/7203855/log.dat: Skip     750000 muons  20230705_210130  7203855
		     config_071/7203856/log.dat: Skip     900000 muons  20230705_210200  7203856
		     config_071/7203857/log.dat: Skip    1050000 muons  20230705_210231  7203857

July 8, 2023
            After much screwing around. Considerable revamp of energy_spin_vs_time.f90 that combines all output into a couple of grand files (all on lnx6186)
              find that when plotting phi vs delta, that there are a cluster of events that are off by twopi. And it appears that these events are all
              from directory    20230705_210231 which includes the last 150k events in Eremey distribution.
              Combine all except 20230705_210231            

              Copy lnx6186 config_070/all*.dat  to mac. All looks sensible. Cutting out large sy reduces momentum phase correlation.

              Try a new run with full effects, true T0, kicker timing 210, polarization=F,  config_069?

              config_072 - 150 turns - Use first 1,050,000 events in distribution

                         config_072/7204045/log.dat: Skip          0 muons  20230708_171552 
			 config_072/7204046/log.dat: Skip     150000 muons  20230708_171905 
			 config_072/7204047/log.dat: Skip     300000 muons  20230708_172037 
			 config_072/7204048/log.dat: Skip     450000 muons  20230708_172207 
			 config_072/7204049/log.dat: Skip     600000 muons  20230708_172320 
			 config_072/7204050/log.dat: Skip     750000 muons  20230708_172517 
			 config_072/7204051/log.dat: Skip     900000 muons  20230708_172701 
			 config_072/7204052/log.dat: Skip          0 muons  20230708_172731 
			 config_072/7204053/log.dat: Skip     150000 muons  20230708_172816 
			 config_072/7204054/log.dat: Skip     300000 muons  20230708_172916 
			 config_072/7204055/log.dat: Skip     450000 muons  20230708_173031 
			 config_072/7204056/log.dat: Skip     600000 muons  20230708_173216 
			 config_072/7204057/log.dat: Skip     750000 muons  20230708_173446 
			 config_072/7204058/log.dat: Skip     900000 muons  20230708_173601 

               Edit energy_spin_vs_time.f90.  If phi is more than pi from average phi then add (or subract) two pi.
               The result is that fitted omega is almost always sensible.

July 19, 2023 - talk at BD meeting. Develop tools to combine files (spin_energy_vs_time.f90) to plot(spin_vs_energy_at_time.gnu) and to bin data(bin_data_from_two_columns.f90)

             How does dphi/d delta depend on trajectory through injection channel
             config_073. Input file changed from
              
 inflector_field =    0.976  ! multiplier of Bmagic. 1.012747 * Bmagic = B_wuzeng                                        
 initial_offsets_ref = -5.8643E-02  0. 0. 2.5068E-02 0. 0. ! -2mm,-3.4mrad,Inf=0.976, 4mrad, -2.3mm, -3.6mrad            
 initial_offsets = -5.8643E-02  0. 0. 2.5068E-02 0. 0.0 ! -2mm,-3.4mrad,Inf=0.976, 4mrad, -2.3mm, -3.6mrad, crosses zero 

to 

inflector_field=0.976                                                                                                   
initial_offsets_ref =  -7.8630E-02  0. 0. 3.1185E-02 0. 0. ! 0mm,0mrad,inf=0.976                                        
initial_offsets =  -7.8630E-02  0. 0. 3.1185E-02 0. 0. ! 0mm,0mrad,inf=0.976                                            

config_073/7205279/log.dat: Skip          0 muons  20230720_093711
config_073/7205281/log.dat: Skip     150000 muons  20230720_094031
config_073/7205283/log.dat: Skip     300000 muons  20230720_094257
config_073/7205284/log.dat: Skip     450000 muons  20230720_094518
config_073/7205285/log.dat: Skip     600000 muons  20230720_094905
config_073/7205286/log.dat: Skip     750000 muons  20230720_095517
config_073/7205287/log.dat: Skip     900000 muons  20230720_095723
config_073/7205288/log.dat: Skip    1050000 muons  20230720_095905
config_073/7205289/log.dat: Skip          0 muons  20230720_100532
config_073/7205290/log.dat: Skip     150000 muons  20230720_100801
config_073/7205291/log.dat: Skip     300000 muons  20230720_101102
config_073/7205292/log.dat: Skip     450000 muons  20230720_101520
config_073/7205293/log.dat: Skip     600000 muons  20230720_101754
config_073/7205294/log.dat: Skip     750000 muons  20230720_102333
config_073/7205295/log.dat: Skip     900000 muons  20230720_103001
config_073/7205296/log.dat: Skip    1050000 muons  20230720_103217

In preparation for simulating experiment where dipole is varied create file=../quad_curvilinear/dipole_bvert.bmad frome file=../quad_curvilinear/dipole.bmad  !dipole field grid
 Different strategy. There is already in tracking master 'set_dipole_params a way to change the dipole field with Delta_B. Change the code so that it does not automatically scale the 
p0c$ value. Make that an independent change using new parameter Delta_Energy. In set_dipole_params look for kickers and use set_attribute to set db_field$

Also modify this line in fields.f90 > field%B = [ B_radial, ele%value(B_field$) + ele%value(DB_field$), 0.0_rp ] so that field delta is included

Now to change the main dipole simply set Delta_B = ? in input.dat

Set up config_074 to test. Copy from config_072. With single muon so that first turn is calcuated, check B around ring.

After a lot of fussing with set_dipole_params, involving lords for the split element (DFREE), the elements with grids and the kickers without, I think it is doing the right thing.
In the input.dat file set DeltaB_onB to the fractional change in the dipole field. It has trouble finding a closed orbit. But the tracking looks OK. With DeltaB_onB=-0.007 the average
momentum of stored beam is about -0.0068

config_074/7205447/log.dat: Skip          0 muons 20230725_142133
config_074/7205448/log.dat: Skip     150000 muons 20230725_142329
config_074/7205449/log.dat: Skip     300000 muons 20230725_142816
config_074/7205452/log.dat: Skip     450000 muons 20230725_143750
config_074/7205453/log.dat: Skip     600000 muons 20230725_143905
config_074/7205454/log.dat: Skip     750000 muons 20230725_144133
config_074/7205455/log.dat: Skip     900000 muons 20230725_144232
config_074/7205456/log.dat: Skip    1050000 muons 20230725_144349





July 27, 2023
  Bug in track_all_int_int_efield.f90. Lost particle gets delta = -1 (makes no sense) Move, check for lost particle before loop over points in element. That way
   we exit from the routine before checking intermediate points. Not sure why I didn't do this in the first place.

  Here are the lines that got moved from after loop over intermediate points to right after track1

  if (err .or. .not. particle_is_moving_forward(orbit(n))) then
    if (present(track_state)) track_state = n
    call set_orbit_to_zero (orbit, n+1, branch%n_ele_track)
    if (orbit(n)%location == upstream_end$) orbit(n)%vec = 0 ! But do not reset orbit(n)%state
    if (present(err_flag)) err_flag = .true.
    exit
  endif

  if (debug) then
    call out_io (s_blank$, r_name, ele%name, '\6es16.6\ ', r_array = orbit(n)%vec)
  endif


Having made adjustments so that dipole can be shifted, momentum aperture is shifted. But in time_bins.f90, the aperture is set to +/-0.005. Which leaves out most of particles.
The easiest thing to do is to increase aperture. The downside is that there will be a lot of empty time bins. But there is plenty of memory so who cares?
Try increasing to +/- 0.015. OK as long is dipole range is limited to +/- 0.0075

This appears to have fixed the problem. Delete all the data from config_074 and start over.


config_074/7205567/log.dat: Skip          0 muons  20230727_111020
config_074/7205568/log.dat: Skip     150000 muons  20230727_111137
config_074/7205569/log.dat: Skip     300000 muons  20230727_111251
config_074/7205570/log.dat: Skip     450000 muons  20230727_111407
config_074/7205571/log.dat: Skip     600000 muons  20230727_111521
config_074/7205572/log.dat: Skip     750000 muons  20230727_111650
config_074/7205574/log.dat: Skip     900000 muons  20230727_111828
config_074/7205575/log.dat: Skip    1050000 muons  20230727_111928

config_074/7205579/log.dat: Skip          0 muons   20230727_121820
config_074/7205580/log.dat: Skip     150000 muons   20230727_121921
config_074/7205581/log.dat: Skip     300000 muons   20230727_122021
config_074/7205582/log.dat: Skip     450000 muons   20230727_122120
config_074/7205583/log.dat: Skip     600000 muons   20230727_122221
config_074/7205584/log.dat: Skip     750000 muons   20230727_122551
config_074/7205586/log.dat: Skip     900000 muons   20230727_122724
config_074/7205587/log.dat: Skip    1050000 muons   20230727_122909


Comparison of plot of phase vs time in config_074 with config_072 
Statistics are not great.


Just realized that previous config_074 runs were with only 15000 particles. Now do 150k runs in config_074

config_074/7205595/log.dat: Skip          0 muons 20230727_152553
config_074/7205596/log.dat: Skip     150000 muons 20230727_153238
config_074/7205597/log.dat: Skip     300000 muons 20230727_153338
config_074/7205598/log.dat: Skip     450000 muons 20230727_153438
config_074/7205599/log.dat: Skip     600000 muons 20230727_153622
config_074/7205601/log.dat: Skip     750000 muons 20230727_153822
config_074/7205602/log.dat: Skip     900000 muons 20230727_153952
config_074/7205603/log.dat: Skip    1050000 muons 20230727_154051

August , 2023

In this run config_075 increase dipole field +0.007

config_075/7205728/log.dat: Skip          0 muons 20230802_194202
config_075/7205729/log.dat: Skip     150000 muons 20230802_194432
config_075/7205730/log.dat: Skip     300000 muons 20230802_194605
config_075/7205731/log.dat: Skip     450000 muons 20230802_194702
config_075/7205732/log.dat: Skip     600000 muons 20230802_194817
config_075/7205733/log.dat: Skip     750000 muons 20230802_194917
config_075/7205734/log.dat: Skip     900000 muons 20230802_195032
config_075/7205735/log.dat: Skip    1050000 muons 20230802_195302

August 28, 2023

       In config_076 and config_077, take injection trajectory from config_073 and shift B field - 0.007 and + 0.007

       config_076/7209434/log.dat: Skip          0 muons 20230828_191904  7209434
       config_076/7209435/log.dat: Skip     150000 muons 20230828_192450  7209435
       config_076/7209436/log.dat: Skip     300000 muons 20230828_192620  7209436
       config_076/7209437/log.dat: Skip     450000 muons 20230828_192905  7209437
       config_076/7209438/log.dat: Skip     600000 muons 20230828_193133  7209438
       config_076/7209439/log.dat: Skip     750000 muons 20230828_193435  7209439
       config_076/7209440/log.dat: Skip     900000 muons 20230828_193555  7209440
       config_076/7209441/log.dat: Skip    1050000 muons 20230828_193635  7209441

August 30, 2023

       Shift B field + 0.007

       config_077/7209530/log.dat: Skip    1050000 muons 20230830_134332  7209530
       config_077/7209534/log.dat: Skip          0 muons 20230830_144128  7209534
       config_077/7209535/log.dat: Skip     150000 muons 20230830_144228  7209535
       config_077/7209536/log.dat: Skip     300000 muons 20230830_144344  7209536
       config_077/7209537/log.dat: Skip     450000 muons 20230830_144627  7209537
       config_077/7209538/log.dat: Skip     600000 muons 20230830_150159  7209538
       config_077/7209539/log.dat: Skip     750000 muons 20230830_150512  7209539
       config_077/7209540/log.dat: Skip     900000 muons 20230830_150858  7209540

August 31, 2023

       Another thing to try that will eliminate confusion about start time is to use narrow muon pulse
       The reference is config_070 

       Create config_079 and shift B -0.007 

       config_079/7209544/log.dat: Skip          0 muons 20230831_094124
       config_079/7209545/log.dat: Skip     150000 muons 20230831_094525
       config_079/7209546/log.dat: Skip     300000 muons 20230831_094856
       config_079/7209547/log.dat: Skip     450000 muons 20230831_095150
       config_079/7209549/log.dat: Skip     600000 muons 20230831_104811
       config_079/7209550/log.dat: Skip     750000 muons 20230831_105526
       config_079/7209551/log.dat: Skip     900000 muons 20230831_105741
       config_079/7209552/log.dat: Skip    1050000 muons 20230831_110256

     comparison of config_079(dB/B=-0.007) and config_070(dB/B=0) gives phase difference at zero 0.0147 and dphi/ddelta = 2.1 rad

September 1, 2023
A funny thing. 
    If I fit phase_vs_time in configs  72,74,75  (dB/B = 0, -0.007,0.007) the phase difference at zero time is very nearly the same for -0.007 and 0.007 (+/- 0.026)
    Then dphi/ddelta = 0.026/0.007 = 3.7

    On the other hand in configs 73,76,77 (dB/B = 0, -0.007,0.007) the phase differences at zero times are very different. +0.007 > 0.00028554, -0.007 > 0.00681
    so dphi/ddelta = 0.00028554/-0.007, 0.00681/0.007 = 0.0407926805185659, 0.97289264104721

    Why is that? The only thing I can see is the order of the runs in config_077 is different than config_073
    In config_077 - rerun entire initial distribution

config_077/7209530/log.dat: Skip    1050000 muons 20230830_134332  7209530
config_077/7209534/log.dat: Skip          0 muons 20230830_144128  7209534
config_077/7209535/log.dat: Skip     150000 muons 20230830_144228  7209535
config_077/7209536/log.dat: Skip     300000 muons 20230830_144344  7209536
config_077/7209537/log.dat: Skip     450000 muons 20230830_144627  7209537
config_077/7209538/log.dat: Skip     600000 muons 20230830_150159  7209538
config_077/7209539/log.dat: Skip     750000 muons 20230830_150512  7209539
config_077/7209540/log.dat: Skip     900000 muons 20230830_150858  7209540
config_077/7209579/log.dat: Skip          0 muons 20230901_115814  7209579
config_077/7209580/log.dat: Skip     150000 muons 20230901_115933  7209580
config_077/7209581/log.dat: Skip     300000 muons 20230901_120043  7209581
config_077/7209583/log.dat: Skip     450000 muons 20230901_120655  7209583
config_077/7209584/log.dat: Skip     600000 muons 20230901_122040  7209584
config_077/7209585/log.dat: Skip     750000 muons 20230901_123326  7209585
config_077/7209586/log.dat: Skip     900000 muons 20230901_132326  7209586
config_077/7209587/log.dat: Skip    1050000 muons 20230901_132627  7209587

    Peculiar bug - file config_079/phase_space_at_16750_ns.dat (but none of the other) last ten lines are different format - must be stealing the write unit number.
     To deal with it for the moment change list in energy_spin_vs_time.dat replace phase_space_at_16750_ns.dat with phase_space_at_15632_ns.dat

In config_080 complete study of narrow muon pulse started in config_070 and config_079
Copy config_079 stuff to config_080 and switch sign of dB/B

config_080/7209591/log.dat: Skip          0 muons 20230901_200752  7209591
config_080/7209592/log.dat: Skip     150000 muons 20230901_200940  7209592
config_080/7209593/log.dat: Skip     300000 muons 20230901_201106  7209593
config_080/7209594/log.dat: Skip     450000 muons 20230901_201738  7209594
config_080/7209595/log.dat: Skip     600000 muons 20230901_203321  7209595
config_080/7209596/log.dat: Skip     750000 muons 20230901_204722  7209596
config_080/7209598/log.dat: Skip     900000 muons 20230902_102238  7209598
config_080/7209599/log.dat: Skip    1050000 muons 20230902_102507  7209599


September 3, 2023
  When comparing phi(0) (namely the coefficient a) for config_070, 079 and 080 find that (a80-a79)/2/0.007 is consistent with 072,074,075 but (a80-a70) and (a79-a70) are not equal.
  The problem may be that there are very few time bins in the config_070 run. Since then some improvements to code. So rerun config_070 in config_078

config_078/7209600/log.dat: Skip          0 muons 20230901_200752  7209591
config_078/7209601/log.dat: Skip     150000 muons 20230901_200940  7209592
config_078/7209602/log.dat: Skip     300000 muons 20230901_201106  7209593
config_078/7209603/log.dat: Skip     450000 muons 20230901_201738  7209594
config_078/7209604/log.dat: Skip     600000 muons 20230901_203321  7209595
config_078/7209605/log.dat: Skip     750000 muons 20230901_204722  7209596
config_078/7209606/log.dat: Skip     900000 muons 20230902_102238  7209598
config_078/7209607/log.dat: Skip    1050000 muons 20230902_102507  7209599


config_073,config_076,config_077 are still not consistent, even after doubling statistics in config_077. a73-a76 /= a77-a73
    a73-a76 = 0.00028554876315745 a77-a73=0.00788353956944698   and (a77-a76)/0.014 = 0.583506309471745 rad 

Peculiar bug appears a second time. In config_078/20230903_093225/ phase_space_at_16750_ns.dat has 10 misplaced lines at the end
 Again replace phase_space_at_16750_ns.dat with phase_space_at_15632_ns.dat in energy_spin_vs_time_input.dat   

September 6, 2023
  Another peculiar bug in output files.  In config_078-080 phase_space_at_21222_ns.dat is odd. When plotting dp/p the first 800 or so entries are dp/p> 0.001 and the remaining 2200 are dp/p<.001.
  That limit is different for 79 and 80 since the average momentum is different. The other phase_space_at files look OK. I suspect this is related to the bug reported in the previous entry.
  Try running g2_tracking with typeout when files are opened and closed. Must be that one of th eunits is being overwritten. Test run in config_078 with 10,000 events 

September 7, 2023
  Find that writing phase_space_at... files uses units 40-59. The same flaws appear in phase_space_at_16750 and phase_space_at_21222.  Also that in tracking master there is a write to 54.

  if(nturns - i < vparam_cbo_turns)then
      call moment_range(moment)  !compute range of moments only for last 100 turns
      if(100-(nturns-i)==1)write(54,'(a12,a10,3a12)')'vparam','turn','avex','max_avex','min_avex'
      write(54,'(es12.4,1x,i10,1x,3es12.4)')vparam,i,moment%ave(1),moment%max_ave(1),moment%min_ave(1)
  endif

Change the write unit number from 54 to 154 and run g2_tracking again.
 That fixed the problem with phase_space_at_16750.
 But no change to the problem with 21222. I wonder if it's an effect of the binning. The particles with higher momentumjust make it into the time bin?

September 12, 2023
 Summary 
    72,74,75 - Full T0, 
    73,76,77 - Shifted injection trajectory, smaller dphi/de
    78,79,80 - narrow T0, (adjusted kicker) otherwiselike 72,74,75 

September 17, 2023
  Try trajectory halfway between standard and straight ahead. (See July 19)
  In directory config_081.

   Random thought. Should inflector scattering be on or off? Apparently scattering has been turned off. Leave it that way for now.

!config_081 halfway between normal and straight ahead
initial_offsets_ref = -6.8646E-02 0. 0. 2.8150E-02 0. 0.
initial_offsets = -6.8646E-02 0. 0. 2.8150E-02 0. 0.  !-1mm,-1.7mrad,Inf=0.976


config_081/7210156/log.dat: Skip          0 muons 20230917_120716  7210156
config_081/7210157/log.dat: Skip     150000 muons 20230917_120916  7210157
config_081/7210158/log.dat: Skip     300000 muons 20230917_121046  7210158
config_081/7210159/log.dat: Skip     450000 muons 20230917_121150  7210159
config_081/7210160/log.dat: Skip     600000 muons 20230917_121401  7210160
config_081/7210161/log.dat: Skip     750000 muons 20230917_121517  7210161
config_081/7210162/log.dat: Skip     900000 muons 20230917_121616  7210162
config_081/7210164/log.dat: Skip    1050000 muons 20230917_122029  7210164

September 20, 2023
  For this latest trajectory compute +/- Delta B in config_082 and config_083

config_082/7210231/log.dat: Skip          0 muons 20230920_115842  7210231
config_082/7210233/log.dat: Skip     150000 muons 20230920_120359  7210233
config_082/7210234/log.dat: Skip     300000 muons 20230920_120642  7210234
config_082/7210235/log.dat: Skip     450000 muons 20230920_121315  7210235
config_082/7210236/log.dat: Skip     600000 muons 20230920_121431  7210236
config_082/7210243/log.dat: Skip     750000 muons 20230920_140609  7210243
config_082/7210244/log.dat: Skip     900000 muons 20230920_141056  7210244
config_082/7210245/log.dat: Skip    1050000 muons 20230920_141208  7210245

config_083/7210318/log.dat: Skip          0 muons 20230920_190732  7210318
config_083/7210319/log.dat: Skip     150000 muons 20230920_191220  7210319
config_083/7210321/log.dat: Skip     300000 muons 20230920_191439  7210321
config_083/7210323/log.dat: Skip     450000 muons 20230920_191954  7210323
config_083/7210327/log.dat: Skip     600000 muons 20230920_211052  7210327
config_083/7210328/log.dat: Skip     750000 muons 20230920_211222  7210328
config_083/7210329/log.dat: Skip     900000 muons 20230920_211320  7210329
config_083/7210330/log.dat: Skip    1050000 muons 20230920_211452  7210330

September 23, 2023
Epiphany. Neglected to scale fringe field with dipole. Fix by adding routine 'set_fringe_parameters'. Called along with set inflector and dipole and quads from tracking_master.

In order to be sure the field change is working properly, set up new directory to study /test_bfield_shift
  in test_bfield_shift
  20230925_093720 - nominal. Single particle. Injection through channel, deltab=0
  20230925_094314 - start at inflector exit. vec=0, kicker=0, deltab=0, reading file twiss_file.dat u 1:20 Bfield perfectly constant around ring.
  20230925_094900 - start at inflector exit. vec=0, kicker=0, deltab=0.01, reading file twiss_file.dat u 1:20 Bfield perfectly const. Trajectory
                      is pushed to smaller radii since field is high.
  20230925_095704 - start at inflector exit. vec= 0. 0. 0. 0. 0. 0.01, reading file twiss_file.dat u 1:20 (This time particle survives)
                             Trajectories in 095704 and 094313 are identical reading from single_particle_by_element.dat
                             But twiss_file.dat tries to find a closed orbit but there is none unless dp/p is changed.
  20230925_101501 - start at backleg with usual kick and angle offset  but dp/p=0.01. particle does not survive 
  20230925_110130 -                 Try increasing inflector by 1%. This time particle survives
                  Compare 20230925_93720 and 20230925_110130.
                      Trajectories are close but not identical (single_particle_by_element.dat)
                      Magnetic fields are shifted by 1% in ring, except at kickers where shift varies over length of kickers

         Edit code so that I can start tracking at inflector exit and still call first_turn to get B-field

20230925_141055 - start tracking at inflector exit. DeltaB_onB=0, vec = 0. 0. 0. 0. 0. 0.
20230925_141711 - start tracking at inflector exit. DeltaB_onB=0.01, vec = 0. 0. 0. 0. 0. 0.01

Looks good. The evidence is /test_bfield_shift/Bfield_difference_injection_line.pdf  assembled by $ps/Bfield_difference_injection_line.gnu

Go back to config_072 and redo config_074 and config_075 in config_074a and config_075a
   
DeltaB_onB = -0.007
config_074a/7210546/log.dat: Skip          0 muons 20230925_145525  7210546
config_074a/7210548/log.dat: Skip     150000 muons 20230925_145902  7210548
config_074a/7210549/log.dat: Skip     300000 muons 20230925_150017  7210549
config_074a/7210550/log.dat: Skip     450000 muons 20230925_150119  7210550
config_074a/7210551/log.dat: Skip     600000 muons 20230925_150211  7210551
config_074a/7210552/log.dat: Skip     750000 muons 20230925_150344  7210552
config_074a/7210553/log.dat: Skip     900000 muons 20230925_150829  7210553
config_074a/7210554/log.dat: Skip    1050000 muons 20230925_151044  7210554

DeltaB_onB = +0.007
config_075a/7210555/log.dat: Skip          0 muons 20230925_151629  7210555
config_075a/7210556/log.dat: Skip     150000 muons 20230925_151847  7210556
config_075a/7210557/log.dat: Skip     300000 muons 20230925_151946  7210557
config_075a/7210558/log.dat: Skip     450000 muons 20230925_152102  7210558
config_075a/7210559/log.dat: Skip     600000 muons 20230925_152217  7210559
config_075a/7210560/log.dat: Skip     750000 muons 20230925_152256  7210560
config_075a/7210561/log.dat: Skip     900000 muons 20230925_152400  7210561
config_075a/7210562/log.dat: Skip    1050000 muons 20230925_152514  7210562

September 27,2023
After combining with energy_spin_vs_time.f90 and then plotting with phase_vs_energy.gnu and spin_vs_energy_at_time.gnu
find tentatively that slope of phase vs bfield is not as big as before fix to injection line. But statistics for high field
point are poor so run more events on config_075a.

config_075a/7210599/log.dat: Skip          0 muons 20230927_082742
config_075a/7210600/log.dat: Skip     150000 muons 20230927_082911
config_075a/7210601/log.dat: Skip     300000 muons 20230927_083227
config_075a/7210606/log.dat: Skip     450000 muons 20230927_091125
config_075a/7210607/log.dat: Skip     600000 muons 20230927_091654
config_075a/7210608/log.dat: Skip     750000 muons 20230927_092540
config_075a/7210609/log.dat: Skip     900000 muons 20230927_092839
config_075a/7210610/log.dat: Skip    1050000 muons 20230927_093056
config_075a/7210613/log.dat: Skip          0 muons 20230927_093809
config_075a/7210614/log.dat: Skip     150000 muons 20230927_093940
config_075a/7210615/log.dat: Skip     300000 muons 20230927_100055
config_075a/7210616/log.dat: Skip     450000 muons 20230927_100154
config_075a/7210617/log.dat: Skip     600000 muons 20230927_100524
config_075a/7210618/log.dat: Skip     750000 muons 20230927_100655
config_075a/7210619/log.dat: Skip     900000 muons 20230927_101012
config_075a/7210620/log.dat: Skip    1050000 muons 20230927_101254


October 2, 2023
 Reanalyze with greater statistics in config_075a. No difference. (See plots mac config_075a).
 Next repeat calculations at +/- 0.0035 in config_074b and config_075b 

config_075b/7210829/log.dat: Skip          0 muons 20231002_114903  7210829
config_075b/7210831/log.dat: Skip     150000 muons 20231002_115430  7210831
config_075b/7210833/log.dat: Skip     300000 muons 20231002_115533  7210833
config_075b/7210834/log.dat: Skip     450000 muons 20231002_115949  7210834
config_075b/7210835/log.dat: Skip     600000 muons 20231002_120420  7210835
config_075b/7210837/log.dat: Skip     750000 muons 20231002_120644  7210837
config_075b/7210839/log.dat: Skip     900000 muons 20231002_120749  7210839
config_075b/7210841/log.dat: Skip    1050000 muons 20231002_121019  7210841

config_074b/7210826/log.dat: Skip          0 muons 20231002_113949  7210826
config_074b/7210830/log.dat: Skip     150000 muons 20231002_115220  7210830
config_074b/7210832/log.dat: Skip     300000 muons 20231002_115519  7210832
config_074b/7210838/log.dat: Skip     450000 muons 20231002_120735  7210838
config_074b/7210840/log.dat: Skip     600000 muons 20231002_120949  7210840
config_074b/7210842/log.dat: Skip     750000 muons 20231002_121305  7210842
config_074b/7210843/log.dat: Skip     900000 muons 20231002_121704  7210843
config_074b/7210844/log.dat: Skip    1050000 muons 20231002_122019  7210844

update findings in /Users/david/development/bmad_dist/g-2/mytest/energy_vs_time_vs_turn/dphi_de_doc/SimulatedMeasurementPhiVsE_update.pptx

October 18, 2023
Consider config_081. Intermediate angle. Revisit 'measurement' now that fringe field scales properly with change in main field.

config_082a/ same as config_081 (intermediate angle) but Delta B/B=-0.007

config_082a/7223864/log.dat: Skip          0 muons 20231018_143043  7223864
config_082a/7223865/log.dat: Skip     150000 muons 20231018_143154  7223865
config_082a/7223866/log.dat: Skip     300000 muons 20231018_143654  7223866
config_082a/7223939/log.dat: Skip     450000 muons 20231018_144931  7223939
config_082a/7223940/log.dat: Skip     600000 muons 20231018_145132  7223940
config_082a/7223941/log.dat: Skip     750000 muons 20231018_150516  7223941
config_082a/7223942/log.dat: Skip     900000 muons 20231018_150716  7223942
config_082a/7224185/log.dat: Skip    1050000 muons 20231018_154131  7224185

config_083a/ same as 082a but Delta B/B =+0.007

config_083a/7223868/log.dat: Skip          0 muons 20231018_143725  7223868
config_083a/7224186/log.dat: Skip     150000 muons 20231018_154216  7224186
config_083a/7224187/log.dat: Skip     300000 muons 20231018_154520  7224187
config_083a/7224188/log.dat: Skip     450000 muons 20231018_154734  7224188
config_083a/7224189/log.dat: Skip     600000 muons 20231018_155200  7224189
config_083a/7224191/log.dat: Skip     750000 muons 20231018_160102  7224191
config_083a/7224192/log.dat: Skip     900000 muons 20231018_160231  7224192
config_083a/7224193/log.dat: Skip    1050000 muons 20231018_160847  7224193

October 20, 2023
update findings in /Users/david/development/bmad_dist/g-2/mytest/energy_vs_time_vs_turn/dphi_de_doc/SimulatedMeasurementPhiVsE_update.pptx

Next look for injection trajecdtory half way between intermediate and reference to see how dphi/ddelta depends on trajectory,
config_081/  adjust injection angle and offset so that exits inflector at -1.5 mm, -2.55 mrad

config_084/7224518/log.dat: Skip          0 muons 20231020_110832  7224518
config_084/7224521/log.dat: Skip     150000 muons 20231020_111027  7224521
config_084/7224526/log.dat: Skip     300000 muons 20231020_111147  7224526
config_084/7224531/log.dat: Skip     450000 muons 20231020_111302  7224531
config_084/7224534/log.dat: Skip     600000 muons 20231020_111459  7224534
config_084/7224536/log.dat: Skip     750000 muons 20231020_111559  7224536
config_084/7224543/log.dat: Skip     900000 muons 20231020_111757  7224543
config_084/7224545/log.dat: Skip    1050000 muons 20231020_111859  7224545

For result see on mac config_084/spin_vs_energy_at_time.pdf

dphi/ddelta at 19us = -9.76 +/- 2 rad

Summary of dphi/ddelta at 19 us vs angle of trajectory at inflector exit

angle[mrad]	offset[mm]	       dir   	   dphi/ddelta [rad] at 19 us
-2.55		-1.5	       	       config_084  -9.76 +/- 2
-3.4		-2.0		       config_072  -13.13 +/- 2.4
 0		0		       config_073  +0.79 +/- 2
-1.7		-1		       config_081  -1.72 +/- 2.83
 -4.25          -2.5		       config_087  -16.04 +/- 3.68

October 22, 2023
Next try increasing angle and offset config_087 (in same ratio at nominal)
   inflector_end_target = -0.0025 0. 0. -0.00425 0. 0.                                                                          
   initial_offsets_ref = -5.3626E-02  0. 0. 2.3509E-02 0. 0. !-2.5mm, -4.25mrad, inf=0.976
   initial_offsets = -5.3626E-02  0. 0. 2.3509E-02 0. 0. !-2.5mm, -4.25mrad, inf=0.976   

config_087/7224575/log.dat: Skip          0 muons 20231022_060526  7224575
config_087/7224576/log.dat: Skip     150000 muons 20231022_060741  7224576
config_087/7224577/log.dat: Skip     300000 muons 20231022_060911  7224577
config_087/7224578/log.dat: Skip     450000 muons 20231022_061056  7224578
config_087/7224579/log.dat: Skip     600000 muons 20231022_061256  7224579
config_087/7224580/log.dat: Skip     750000 muons 20231022_061457  7224580
config_087/7224581/log.dat: Skip     900000 muons 20231022_061642  7224581
config_087/7224582/log.dat: Skip    1050000 muons 20231022_061941  7224582

Dphi/ddelta see mac config_087/spin_vs_energy_at_time.pdf -16.05 mrad at 19 us


dphi/ddelta vs injection angle summarized in energy_vs_time_vs_turn/dphi_ddelta_vs_angle_off.dat

Next in config_090 angle=-0.85 offset=-0.5

config_090/7224613/log.dat: Skip          0 muons  20231022_193822  7224613
config_090/7224614/log.dat: Skip     150000 muons  20231022_193942  7224614
config_090/7224615/log.dat: Skip     300000 muons  20231022_194042  7224615
config_090/7224616/log.dat: Skip     450000 muons  20231022_194227  7224616
config_090/7224617/log.dat: Skip     600000 muons  20231022_194342  7224617
config_090/7224618/log.dat: Skip     750000 muons  20231022_194612  7224618
config_090/7224619/log.dat: Skip     900000 muons  20231022_195727  7224619
config_090/7224620/log.dat: Skip    1050000 muons  20231022_200212  7224620

October 24, 2023
Dphi/ddelta see mac config_087/spin_vs_energy_at_time.pdf -3.15 mrad at 19 us

October 28, 2023
look for correlation with kicker strength.
config_093 - reduced kicker from nominal - and nominal injection angle (config_072)
             Reduce kicker 10% from 264 gauss to 237 gauss

config_093/7228403/log.dat: Skip          0 muons 20231028_193058  7228403
config_093/7228404/log.dat: Skip     150000 muons 20231028_193329  7228404
config_093/7228406/log.dat: Skip     300000 muons 20231028_193502  7228406
config_093/7228407/log.dat: Skip     450000 muons 20231028_193636  7228407
config_093/7228408/log.dat: Skip     600000 muons 20231028_193803  7228408
config_093/7228409/log.dat: Skip     750000 muons 20231028_193903  7228409
config_093/7228410/log.dat: Skip     900000 muons 20231028_194218  7228410
config_093/7228411/log.dat: Skip    1050000 muons 20231028_194617  7228411

October 29, 2023
Not much change from config_072
config_096 - increase kick 10%

config_096/7228997/log.dat: Skip          0 muons 20231029_194450  7228997
config_096/7228998/log.dat: Skip     150000 muons 20231029_194836  7228998
config_096/7228999/log.dat: Skip     300000 muons 20231029_195035  7228999
config_096/7229000/log.dat: Skip     450000 muons 20231029_195335  7229000
config_096/7229001/log.dat: Skip     600000 muons 20231029_195634  7229001
config_096/7229002/log.dat: Skip     750000 muons 20231029_195749  7229002
config_096/7229003/log.dat: Skip     900000 muons 20231029_195922  7229003
config_096/7229004/log.dat: Skip    1050000 muons 20231029_200136  7229004

See mac config_096 spin_vs_energy_at_time.pdf

Summarize kicker dependence in energy_vs_time_vs_turn/dphi_ddelta_vs_kick.dat


November 17, 2023 -
 Explore kicker dependence at smaller injection angle.
Start from config_081 input.dat
 !config_081 halfway between normal and straight ahead
 initial_offsets_ref = -6.8646E-02 0. 0. 2.8150E-02 0. 0.
 initial_offsets = -6.8646E-02 0. 0. 2.8150E-02 0. 0.  !-1mm,-1.7mrad,Inf=0.976

And decrease kicker 10%
             Reduce kicker 10% from 264 gauss to 237 gauss

config_099/7233662/log.dat: Skip          0 muons  20231118_110347  7233662
config_099/7233663/log.dat: Skip     150000 muons  20231118_110546  7233663
config_099/7233664/log.dat: Skip     300000 muons  20231118_110746  7233664
config_099/7233665/log.dat: Skip     450000 muons  20231118_111059  7233665
config_099/7233666/log.dat: Skip     600000 muons  20231118_111215  7233666
config_099/7233667/log.dat: Skip     750000 muons  20231118_111313  7233667
config_099/7233668/log.dat: Skip     900000 muons  20231118_111531  7233668
config_099/7233669/log.dat: Skip    1050000 muons  20231118_111700  7233669


Decrease kicker 10%
     kicker_params%kicker_field      =   290.0e-4   290.0e-4  290.0e-4 !<xe>=1.3mm with tstart=210ns - increased from nominal by 10% config_100 (also config_096)

config_100/7233757/log.dat: Skip          0 muons 20231119_154545  7233757
config_100/7233767/log.dat: Skip     150000 muons 20231119_184355  7233767
config_100/7233772/log.dat: Skip     300000 muons 20231119_191657  7233772
config_100/7233773/log.dat: Skip     450000 muons 20231119_191754  7233773
config_100/7233776/log.dat: Skip     600000 muons 20231119_193407  7233776
config_100/7233778/log.dat: Skip     750000 muons 20231119_193555  7233778
config_100/7233783/log.dat: Skip     900000 muons 20231119_195711  7233783
config_100/7233784/log.dat: Skip    1050000 muons 20231119_195756  7233784


November 22, 2023

Find a bug in tracking_master. The symptom was that the phase space files like MARK_INFLECTOR_DS_phase_space.dat included all particles. coord%state for particles that had been lost were
being reset to  alive$. The problem was apparently due to a change in track1.  t appeared after rebuilding with the latetest bmad code. The problem was fixed by adding the following, (copied from track_all) in the loop over the injection branch
that appears at line 804 in tracking master


         do j=1,lat%branch(0)%n_ele_track
           ele_save%tracking_method = lat%branch(0)%ele(j)%tracking_method
           if(inj_matrix_tracking .and.lat%branch(0)%ele(j)%key /= marker$)lat%branch(0)%ele(j)%tracking_method = linear$
           call track1(from_orbit(j-1),lat%branch(0)%ele(j), lat%param, from_orbit(j), err_flag =err)


  the following 5 lines were added to the loop to deal with lost particles

           if (err .or. .not. particle_is_moving_forward(from_orbit(j))) then
              call set_orbit_to_zero (from_orbit, j+1, lat%branch(0)%n_ele_track)
              if (from_orbit(j)%location == upstream_end$) from_orbit(j)%vec = 0 ! But do not reset orbit(n)%state
              exit
           endif                    


           lat%branch(0)%ele(j)%tracking_method = ele_save%tracking_method
           muons_ele_inj(n,j)%coord = from_orbit(j)

        end do

November 24, 2023
   That wasn't the whole story. There is an apparent bug in bmad/code/track1

if (associated(track1_preprocess_ptr))then
  finished = .false.
  call track1_preprocess_ptr (start2_orb, ele, param, err, finished, radiation_included, track)
  if (err) return
  if (finished) then
!    if (present(err_flag)) err_flag = err
    if (present(err_flag)) err_flag = .true.  !dlr 2023 11 24
    return
  endif
endif

The change was made and the bmad library rebuilt
Rerun on mac config_099 - and looks just fine

November 25, 2023
Run 4 more jobs on mac so that we have in config_099

out1.dat: Skip          0 muons 20231124_194109
out2.dat: Skip     150000 muons	20231125_082532
out3.dat: Skip     300000 muons	20231125_082611
out4.dat: Skip     450000 muons	20231125_082632
out5.dat: Skip     600000 muons	20231125_082700

Result is reasonable. Re submit linux jobs config_099 and config_100


config_099/7234168/log.dat: Skip          0 muons 20231125_173704  7234168
config_099/7234169/log.dat: Skip     150000 muons 20231125_173917  7234169
config_099/7234170/log.dat: Skip     300000 muons 20231125_174046  7234170
config_099/7234172/log.dat: Skip     450000 muons 20231125_174230  7234172
config_099/7234174/log.dat: Skip     600000 muons 20231125_174332  7234174
config_099/7234176/log.dat: Skip     750000 muons 20231125_174445  7234176
config_099/7234178/log.dat: Skip     900000 muons 20231125_174546  7234178
config_099/7234179/log.dat: Skip    1050000 muons 20231125_174731  7234179

config_100/7234171/log.dat: Skip          0 muons 20231125_174156  7234171
config_100/7234173/log.dat: Skip     150000 muons 20231125_174317  7234173
config_100/7234175/log.dat: Skip     300000 muons 20231125_174432  7234175
config_100/7234177/log.dat: Skip     450000 muons 20231125_174531  7234177
config_100/7234180/log.dat: Skip     600000 muons 20231125_174746  7234180
config_100/7234181/log.dat: Skip     750000 muons 20231125_174846  7234181
config_100/7234182/log.dat: Skip     900000 muons 20231125_175000  7234182
config_100/7234184/log.dat: Skip    1050000 muons 20231125_175245  7234184

November 27, 2023 - The results of the above are in config_100 and config_099 on mac in spin_vs_energy_at_time.pdf
   In summary, there is little dependence of dphi/ddelta on kicker even when angle at inflector exit is 'small'

Included in dphi_ddelta_vs_kick.dat

# at reduced injection angle of -1.7 mrad
#
237			   -3.53	2.91				config_099
264			   -1.72	2.83				config_081
290			   -3.47	2.85				config_100

Begin scan of dphi/ddelta vs inflector field. Start with config_072 in config_101 and config_102

! for nominal lattice starting  3mm upstream IBMS1                                                                      
!Run 3 - lattice that starts 3mm upstream of IBMS1
!increase inflector field from nominal
inflector_field =    0.986  ! multiplier of Bmagic. 1.012747 * Bmagic = B_wuzeng
initial_offsets_ref = -5.8643E-02  0. 0. 2.5068E-02 0. 0. ! 0.16mm,-0.8mrad,Inf=0.986
initial_offsets = -5.8643E-02  0. 0. 2.5068E-02 0. 0.0 ! 0.16mm,-0.8mrad,Inf=0.986

config_101/7234238/log.dat: Skip          0 muons 20231127_113556 7234238
config_101/7234239/log.dat: Skip     150000 muons 20231127_113801 7234239
config_101/7234243/log.dat: Skip     300000 muons 20231127_122601 7234243
config_101/7234244/log.dat: Skip     450000 muons 20231127_124117 7234244
config_101/7234245/log.dat: Skip     600000 muons 20231127_124347 7234245
config_101/7234246/log.dat: Skip     750000 muons 20231127_124603 7234246
config_101/7234247/log.dat: Skip     900000 muons 20231127_124716 7234247
config_101/7234248/log.dat: Skip    1050000 muons 20231127_125002 7234248

For inflector field increased 1% dphi/ddelta = -6.36+/- 2.59 rad at 19us

November 29, 2023
Next try inflector half way, (0.5% increase from nominal)
! for nominal lattice starting  3mm upstream IBMS1                                                                      
!Run 3 - lattice that starts 3mm upstream of IBMS1
!set inflector 0.5% greater than nominal
inflector_field =    0.981  ! multiplier of Bmagic. 1.012747 * Bmagic = B_wuzeng
initial_offsets_ref = -5.8643E-02  0. 0. 2.5068E-02 0. 0. ! -1mm,-2.1mrad,Inf=0.981
initial_offsets = -5.8643E-02  0. 0. 2.5068E-02 0. 0.0 ! -1mm, -2.1mrad,Inf=0.981

config_102/7234443/log.dat: Skip          0 muons 20231129_191333  7234443
config_102/7234444/log.dat: Skip     150000 muons 20231129_191521  7234444
config_102/7234445/log.dat: Skip     300000 muons 20231129_191702  7234445
config_102/7234446/log.dat: Skip     450000 muons 20231129_191847  7234446
config_102/7234447/log.dat: Skip     600000 muons 20231129_191952  7234447
config_102/7234448/log.dat: Skip     750000 muons 20231129_193836  7234448
config_102/7234449/log.dat: Skip     900000 muons 20231129_194004  7234449
config_102/7234451/log.dat: Skip    1050000 muons 20231129_194207  7234450

For inflector field increased 0.5% to 0.981, dphi/ddelta = -11.45 +/- 2.8 at 19us

December 5, 2023
Just for fun repeat config_081 (as config_103) - as it does not quite line up with kicker +/- 10%   (config_099 and config_100)
 Recall that 99 and 100 are completed after bmad bug fix. ???

Summary plots of dphi/ddelta vs angle, kick and inflector in SimulatedMeasermentPhiVsE_update.pptx

config_103/7234655/log.dat: Skip          0 muons 20231205_121445  7234655
config_103/7234656/log.dat: Skip     150000 muons 20231205_122647  7234656
config_103/7234658/log.dat: Skip     300000 muons 20231205_132545  7234658
config_103/7234659/log.dat: Skip     450000 muons 20231205_134828  7234659
config_103/7234660/log.dat: Skip     600000 muons 20231205_142756  7234660
config_103/7234661/log.dat: Skip     750000 muons 20231205_143755  7234661
config_103/7234662/log.dat: Skip     900000 muons 20231205_143853  7234662
config_103/7234663/log.dat: Skip    1050000 muons 20231205_143953  7234663

Repeat of config_081 (config_103) dphi/ddelta = -3.94 +/- 2.84 vs -1.72 +/- 2.83 


December 6, 2023
Add another inflector value to scan of dphi/ddelta vs inflector
Copy config_102 to config_104 abd set inflector to 0.95 * nominal = 0.995*0.976 = 0.971

!set inflector 0.5% less than nominal
inflector_field =    0.971  ! multiplier of Bmagic. 1.012747 * Bmagic = B_wuzeng
initial_offsets_ref = -5.8643E-02  0. 0. 2.5068E-02 0. 0. ! -3.1,-4.7mrad,Inf=0.971
initial_offsets = -5.8643E-02  0. 0. 2.5068E-02 0. 0.0 ! -3.1mm, -4.7mrad,Inf=0.971

config_104/7234735/log.dat: Skip          0 muons 20231206_112453  7234735
config_104/7234737/log.dat: Skip     150000 muons 20231206_114050  7234737
config_104/7234738/log.dat: Skip     300000 muons 20231206_114208  7234738
config_104/7234739/log.dat: Skip     450000 muons 20231206_114735  7234739
config_104/7234740/log.dat: Skip     600000 muons 20231206_114833  7234740
config_104/7234741/log.dat: Skip     750000 muons 20231206_114936  7234741
config_104/7234742/log.dat: Skip     900000 muons 20231206_115336  7234742
config_104/7234745/log.dat: Skip    1050000 muons 20231206_121056  7234745

config_104 dphi/ddelta = -5.20 +/-3.99 at 19 us


December 15, 2023
Updating RF quads in track1_preprocess. Previous implementation had been to apply RF focusing. The actual implementation in the storage ring is RF dipole.
While debugging discover that 'horizontal_scraping' had been turned off. This needs to be set in the input.dat file.

January 10, 2024
  Fix scattering bug. Input.dat param quad_plate=F was not operational. Scattering in plate in wall_hit_handling_custom was always turned on.
  That is fixed by putting quad_plate parameter into materials_mod.f90 and these lines in wall_hit_handling_custom

 orb%state=alive$
  if(.not. quad_plate)return
  if(orb%species == positron$) return
  if(abs(orb%vec(3)) > height .and. abs(orb%vec(1)) >height)return   ! misses the plates

?---------------------------------------------------------------------------------------------

July 30, 2024
   I would like to reproduce the runs with the different trajectories through the injection line.
   The three plotted in 'G Minus 2 Experiment Document 29494-v1 Simulation of Measurement of g-2 phase vs energy' are from directories
           dir1 = 'config_072/20230918_132456/injection_line_trajectory.dat'
	   dir2 = 'config_073/20230918_132221/injection_line_trajectory.dat'
	   dir3 = 'config_081/20230918_124336/injection_line_trajectory.dat'
    and plotted with $ps/many_injection_trajectories.gnu
    Can I also plot the magnetic field along the trajectory?

  Create config_072r to include IBMS detectors

 config_072r    (nominal injection angle -3.4 mrad)

Create a new directory for each and run with IBMS detectors. 

Create plotting script 'magnetic_field_inj_trajectory.gnu' to plot magnetic field along trajectory

Addition of IBMS3, which is in the ring, with superimpose, causes error in odeint at boundary. No idea why this should happen.
Will attempt to put in IBMS3 as an element.
Modify 'bmad_esquad_grid_split_dfree.' to include IBMS3
     Split afree. Two new elements, afree_before_ibms3 and afree_after_ibms3.
     Also set apertures for these new elements.
     Also, walls are defined in walls_free.bmad. This is replaced with walls_free_less_afree.bmad which does not set walls for afree.
     Finally, in order to get output at IBMS detectors, use tracking_method =custom, mat6_calc_method = bmad_standard
     run > out1.dat and 20240730_192317

August 1, 2024
   in config_072/20240730_192317/IBMS_hits.dat - having started with 400k particles, 'nominal' injection angle 3.4mrad (from config_072)
  Use gnuplot to get average x, x' on IBMS
							IBMS coords					global coords(ref to tangent line at inflector exit)
config			     IBMS	      	 <x>[mm]	      <xp>[mrad]		<x>[mm]		<xp>	
 072r			        1	      		  -13 			24.9				-68				  ?
 072r			   	2			  -6.5			8.6				4.4				  ?
                            				  						           ref to ring coordinates
 072r			   	3											71.3				-4.5				



where
IBMS1:  marker, superimpose, ref = mark_inflector_ds, offset = -4.397,  ref_origin = beginning, x_offset=-0.045, tracking_method = custom, mat6_calc_method=bmad_standard
IBMS2:  marker, superimpose,  ref = mark_inflector_ds, offset = -1.9505,  ref_origin = beginning, x_offset=0.0109, tracking_method = custom, mat6_calc_method=bmad_standard
IBMS3:  marker , mat6_calc_method=bmad_standard, tracking_method=custom
                                   !,  offset = 8.175/360.*44.686=1.0147,  ref_origin = beginning, x_offset =7181.2-7112. = 69.2!, tracking_method = custom


Next create config_073r and again track to get distributions on IBMS  - injection angle straight ahead
     cd config_073r
     cp ../config_072r/bmad_esquad_grid_split_dfree_ibms3. .
     cp ../config_072r/quad_plate_misalign.bmad  .
     cp ../config_072r/quad_input.dat .
     cp ../config_073/input.dat .
     cp ../config_072r/ibms_lattice. .
     cp ../config_072r/walls_free_less_afree.bmad .

 plot injection trajectory and magnetic field using $ps/magnetic_field_inj_trajectory.gnu with data in config_073r/20240801_201845/inj_traj_bfield.dat
     config_073r/20240801_203028/   1000 particles - looks OK
     	                 20240801_204632/   150k, nmuon_first=0,  > out1.dat
			 20240801_205424/   150k, nmuon_first=150000 > out2.dat

  Use gnuplot to get average x, x' on IBMS from config_073r/20240801_204632/
							IBMS coords					global coords(ref to tangent line at inflector exit)
config			     IBMS	      	 <x>[mm]	      <xp>[mrad]		<x>[mm]		<xp>	
 072r			        1	      		  -13 			24.9				-68				  ?
 073r				1			-33.0			31.0				-88														
 072r			   	2			  -6.5			8.6				4.4				  ?
 073r				2			  -9.4			11.7				1.5
                            				  						           ref to ring coordinates
 072r			   	3											71.3				-4.5																		   
 073r			   	3			  								75.3				-2.7

August 2, 2024

Create 	   config_081r for   intermediate injection angle

    cp ../config_072r/bmad_esquad_grid_split_dfree_ibms3. .
    cp ../config_081/quad_plate_misalign.bmad .
    cp ../config_081/quad_input.dat .
    cp ../config_081/input.dat .
    cp ../config_072r/ibms_lattice. .
    cp ../config_072r/walls_free_less_afree.bmad .

 plot injection trajectory and magnetic field using $ps/magnetic_field_inj_trajectory.gnu with data in config_081r/20240802_192020/inj_traj_bfield.dat

 To track many particles set 'twiss_file = F'
  20240802_194557 - 1000 particles 

August 3, 2024
  config_081r/20240803_092636 - 150k particles > out1.dat

  Use gnuplot to get average x, x' on IBMS from config_081r/20240803_092636
							IBMS coords					global coords(ref to tangent line at inflector exit)
config			     IBMS	      	 <x>[mm]	      <xp>[mrad]		<x>[mm]		<xp>	
 072r			        1	      		  -13 			24.9				-58				  ?
 073r				1			-33.0			31.0				-78
 081r				1			-23.0			28.0				-68
 072r			   	2			  -6.5			8.6				4.4				  ?
 073r				2			  -9.4			11.7				1.5
 081r				2			  -8.2			10.4				2.7
                            				  						           ref to ring coordinates
  072r			   	3											71.3				-4.5
  073r				3											75.3				-2.7
  081r				3                        								73.6				-3.4