-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmultipleAsymSteps.cc
192 lines (159 loc) · 5.07 KB
/
multipleAsymSteps.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include <string>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <math.h>
#include <algorithm>
#include "G4ThreeVector.hh"
#include "G4PolarizationHelper.hh"
#include "G4StokesVector.hh"
#include "CLHEP/Units/PhysicalConstants.h"
#include "TFile.h"
#include "TTree.h"
#include "TRandom3.h"
using namespace std;
G4ThreeVector scatter(G4ThreeVector org, G4ThreeVector pol,G4ThreeVector &finPol);
void printVector(G4ThreeVector vec);
void rotateSpinToLocal(G4ThreeVector ki, G4ThreeVector kf, G4ThreeVector pi, G4ThreeVector &pf);
G4ThreeVector inverseRotateUz(G4ThreeVector v,G4ThreeVector dir);
int debugPrint(1);
int main(int argc, char** argv)
{
if( argc < 4 ) {
cout<<" usage: build/moreAsym [# events] [#of asymmetry steps] [1 for pos pol: -1 for neg pol]"<<endl;
cout<<"\t e.g. : build/moreAsym 100000 3 1"<<endl;
return 1;
}
int nEvt=atoi(argv[1]);
int nStp=atoi(argv[2]);
int pol=atoi(argv[3]);
string fOutNm=Form("o_moreAsym_%dstps_",nStp);
if(pol==1) fOutNm+="V.root";
else if(pol==-1) fOutNm+="mV.root";
else{
cout<<"polarization should be either 1 or -1"<<endl;
return 2;
}
TFile *fout=new TFile(fOutNm.c_str(),"RECREATE");
TTree *t=new TTree("t","Asym toy MC output");
double phi,th;
int evNr,stpNr;
t->Branch("evNr",&evNr,"evNr/I");
t->Branch("stpNr",&stpNr,"stpNr/I");
t->Branch("phi",&phi,"phi/D");
t->Branch("th",&th,"th/D");
// t->Branch("px",&px,"px/D");
// t->Branch("py",&px,"py/D");
// t->Branch("pz",&px,"pz/D");
vector<G4ThreeVector> prevStp,currStp;
vector<G4ThreeVector> prevStpPol,currStpPol;
for(int iEv=0;iEv<nEvt;iEv++){
currStp.push_back(G4ThreeVector(0,0,1));
currStpPol.push_back(G4ThreeVector(0,pol,0));
}
for(int iStp=0;iStp<nStp;iStp++){
cout<<"at step "<<iStp<<endl;
prevStp=currStp;
prevStpPol=currStpPol;
for(int iEv=0;iEv<nEvt;iEv++){
currStp[iEv]=scatter(prevStp[iEv],prevStpPol[iEv],currStpPol[iEv]);
evNr = iEv;
stpNr = iStp;
phi = currStp[iEv].getPhi();
th = currStp[iEv].getTheta();
t->Fill();
}
}
t->Write();
fout->Close();
return 0;
}
G4ThreeVector scatter(G4ThreeVector org, G4ThreeVector pol, G4ThreeVector &finPol){
//cos(20/180*pi)
double cth = 9.39692620785908428e-01;
double sth = 3.42020143325668657e-01;
TRandom3 a(0);
double phi = a.Uniform() * CLHEP::twopi;
G4double amplitude = 0.2;
G4double _prob = a.Uniform();
G4double tdirx1 = sth*cos(phi);
G4double tdiry1 = sth*sin(phi);
G4ThreeVector finLocal(tdirx1,tdiry1,cth);
G4ThreeVector orgLocal(0,0,1);
G4ThreeVector normalInteraction =
G4PolarizationHelper::GetFrame(orgLocal,finLocal);
G4double cosPhi = pol * normalInteraction;
if( _prob < amplitude * cosPhi ){
phi-=CLHEP::pi;
}
G4double tdirx = sth*cos(phi);
G4double tdiry = sth*sin(phi);
G4ThreeVector tnewDirection(tdirx,tdiry,cth);
tnewDirection.rotateUz(org);
rotateSpinToLocal(orgLocal,finLocal,pol,finPol);
return tnewDirection;
}
void printVector(G4ThreeVector vec){
cout<<"\tX: "
<<vec.getX()<<"\tY: "
<<vec.getY()<<"\tZ: "
<<vec.getZ()<<"\tR: "
<<vec.getR()<<"\tTh: "
<<vec.getTheta()/CLHEP::pi*180<<"\tPh: "
<<vec.getPhi()/CLHEP::pi*180<<endl;
}
void rotateSpinToLocal(G4ThreeVector ki, G4ThreeVector kf, G4ThreeVector pi, G4ThreeVector &pf){
G4StokesVector beamPol = pi;
if(debugPrint){
cout<<"l: initial beamPol "<<endl;
printVector(beamPol);
}
//G4ThreeVector nInteractionFrame = G4PolarizationHelper::GetFrame(ki,kf);
G4ThreeVector nInteractionFrame = (ki.cross(kf)).unit();
if(debugPrint){
cout<<"l: normal to interaction frame"<<endl;
printVector(nInteractionFrame);
cout<<"l: P*n "<<nInteractionFrame*beamPol<<endl;
}
// transform polarization from ki-local into Stokes
beamPol.RotateAz(nInteractionFrame,ki);
if(debugPrint){
cout<<"l: in stokes beamPol "<<endl;
printVector(beamPol);
}
//polarization transfer
G4double interactionTheta = ki*kf;
if(debugPrint)
cout<<"l: rotation by "<<acos(interactionTheta)*180/CLHEP::pi<<endl;
//beamPol.rotateY( - acos(interactionTheta) + alpha); //where alpha=atan(U/T)
if(debugPrint){
cout<<"l: after transport beamPol "<<endl;
printVector(beamPol);
}
//rotate from Stokes frame into kf-local
beamPol.InvRotateAz(nInteractionFrame,kf);
if(debugPrint){
cout<<"l: back to local beamPol "<<endl;
printVector(beamPol);
}
pf = (G4ThreeVector)beamPol;
}
G4ThreeVector inverseRotateUz(G4ThreeVector v,G4ThreeVector dir){
G4double nx(0),ny(0),nz(0);
G4double ux= dir.getX();
G4double uy= dir.getY();
G4double uz= dir.getZ();
G4double up= ux*ux + uy*uy;
if (up > 0){
up = sqrt(up);
nx = v.getX() * ux * uz / up - v.getY() * uy * uz / up - v.getZ() * up;
ny = -v.getX() * uy / up + v.getY() * ux / up ;
nx = v.getX() * uz + v.getY() * uy + v.getZ() * uz;
}else if ( uz <0 ){
nx = - v.getX();
ny = v.getY();
nz = - v.getZ();
}
G4ThreeVector finalV(nx,ny,nz);
return finalV;
}