1+ std ::string getEnergyPath ()
2+ {
3+ auto env = std ::getenv ("VMCWORKDIR" );
4+ if (env == nullptr ) {
5+ return "../../resources/energy_loss/HinH.txt" ; // Default path assuming cwd is build/AtTools
6+ }
7+ return std ::string (env ) + "/resources/energy_loss/HinH.txt" ; // Use environment variable
8+ }
9+
10+ const double mass_p = 938.272 ; // Mass of proton in MeV/c^2
11+ const double charge_p = 1.602176634e-19 ; // Charge of proton
12+
13+ // This test should plot the trajectory of a particle in a magnetic field using
14+ // the output from GEANT and the AtPropagator class.
15+ void AtPropagator ()
16+ {
17+ using namespace AtTools ;
18+
19+ std ::vector < double > x , y , z ;
20+ std ::vector < double > x2 , y2 , z2 ;
21+
22+ std ::ifstream infile ("hits.txt" );
23+ double xi , yi , zi , Ei ;
24+ while (infile >> xi >> yi >> zi >> Ei ) {
25+ x .push_back (xi * 10 );
26+ y .push_back (yi * 10 );
27+ z .push_back (zi * 10 );
28+ }
29+
30+ // Our propagator setup
31+ double charge = charge_p ; // Charge in Coulombs
32+ double mass = mass_p ; // Mass in MeV/c^2
33+ auto elossModel = std ::make_unique < AtTools ::AtELossTable > (0 );
34+ elossModel -> LoadSrimTable (getEnergyPath ()); // Use the function to get the path
35+ elossModel -> SetDensity (3.553e-5 ); // Set density in g/cm^3 for 300 torr H2
36+ AtTools ::AtPropagator propagator (charge , mass , std ::move (elossModel ));
37+ propagator .SetEField ({0 , 0 , 0 }); // No electric field
38+ propagator .SetBField ({0 , 0 , 2.85 }); // Magnetic field
39+ AtTools ::AtRK4Stepper stepper ;
40+
41+ XYZPoint startPos (-3.40046e-05 , -1.49863e-05 , 0.10018 ); // Start position in cm
42+ startPos *= 10 ; // Convert to mm
43+ XYZVector startMom (0.00935463 , -0.0454279 , 0.00826042 ); // Start momentum in GeV/c
44+ startMom *= 1e3 ; // Convert to MeV/c
45+
46+ propagator .SetState (startPos , startMom );
47+
48+ // Loop through until the particle is stopped
49+ while (Kinematics ::KE (propagator .GetState ().fMom , propagator .GetState ().fMass ) > 0.1 ) {
50+ // Propagate to the next point
51+ propagator .PropagateOneStep (stepper );
52+
53+ // Get the current position and momentum
54+ auto pos = propagator .GetPosition ();
55+
56+ // Store the position for plotting
57+ x2 .push_back (pos .X ());
58+ y2 .push_back (pos .Y ());
59+ z2 .push_back (pos .Z ());
60+ }
61+
62+ TGraph2D * track = new TGraph2D (x .size (), x .data (), y .data (), z .data ());
63+ track -> SetTitle ("Particle Track;X [mm];Y [mm];Z [mm]" );
64+ track -> SetMarkerStyle (20 );
65+ track -> SetMarkerSize (0.8 );
66+
67+ TGraph2D * track2 = new TGraph2D (x2 .size (), x2 .data (), y2 .data (), z2 .data ());
68+ track2 -> SetTitle ("Propagated Particle Track;X [mm];Y [mm];Z [mm]" );
69+ track2 -> SetMarkerStyle (21 );
70+ track2 -> SetMarkerSize (0.8 );
71+ track2 -> SetMarkerColor (kRed );
72+
73+ TCanvas * c1 = new TCanvas ("c1" , "Particle Track" , 800 , 600 );
74+ track -> Draw ("P" );
75+ track2 -> Draw ("PSAME" );
76+ }
0 commit comments