EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
eTTreemaker.C
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file eTTreemaker.C
1 // this macro takes as input an Ascii eSTARlight output
2 // file, slight.out, and creates a standard TTREE
3 // S. Klein, June, 2018
4 
5 
6 #include <TFile.h>
7 #include <TNtuple.h>
8 #include <TMath.h>
9 #include <cmath>
10 #include <cassert>
11 #include <string>
12 #include <iostream>
13 #include <fstream>
14 #include <sstream>
15 
16 using namespace std;
17 
18 void eTTreemaker(TString slightname="slight.out")
19 {
20  double me=0.000511;
21  double mfinal;
22  // create output TTree here
23 
24  double fspx,fspy,fspz,fse,fsrap,fspt,fsmass,p1x,p1y,p1z,p1e,p1prap;
25  double p2x,p2y,p2z,p2e,p2prap,p1pt,p2pt;
26  double targx, targy, targz, targE;
27  double sx, sy, sz, sE;
28 
29  // new variables for eSTARlight
30  double kphoton,kperp,qsquared,xbj,ttransfer;
31  double eTheta; //Scattered electron polar angle
32 
33  // ttransfer will be imlemented later
34 
35  TNtuple *esNTuple = new TNtuple("esNT","eslightNtuple","fspt:fspz:fsrap:fsmass:p1pt:p1z:p1prap:p2pt:p2z:p2prap:kg:qsq:xbj");
36  // This is the electron, photon, final state (particle combination), particle 1 and particle 2
37  // These are pseudorapidities, except for the final state, where it is rapidity
38  // For the photon there is the photon energy, Q^2 the bjorken x of the target (calculated with x = Q^2/(2km_p) and t, the momentum transfer from the target
39 
40  cout <<" Opening slight.out"<< endl;
41 
42  ifstream inFile;
43  cout << "FileName=" << slightname << endl;
44  inFile.open( slightname.Data());
45  cout << "slight.out open"<<endl;
46  int countLines=0;
47 
48  // Define variables for event loop
49  int ntrk,nvtx;
50  int i1,i2,i3,i4; // junk variables for fscanf lines
51 
52  string line;
53  stringstream lineStream;
54 
55  // event card
56  string label;
57  int eventNmb,nmbTracks, pcode;
58  int nev,ntr,stopv,pdgpid1,pdgpid2;
59 
60  // go through input cards, which include the gammas of the electron and ion.
61  // We expect a CONFIG_OPT, BEAM_1 (electron), BEAM_2 (ion) and PHOTON
62  double gamma_e,gamma_ion,junk1;
63 
64  if (!getline(inFile,line)) {cout <<" Error reading CONFIG_OPT line"<<endl;}
65  countLines++;
66  lineStream=stringstream(line);
67 
68  lineStream>>label;// CONFIG_OPT
69 
70  if (!getline(inFile,line)) {cout <<" Error reading BEAM_1 line"<<endl;}
71  countLines++;
72  lineStream=stringstream(line);
73  lineStream>>label>>i1>>i2>>gamma_e; // BEAM_1 is the electron
74  cout<<"Electron Lorentz boost is "<<gamma_e<<endl;
75 
76  if (!getline(inFile,line)) {cout <<" Error reading BEAM_2 line"<<endl;}
77  countLines++;
78  lineStream=stringstream(line);
79  lineStream>>label>>i1>>i2>>gamma_ion; // BEAM_2 is the ion
80  cout << "Check... " << label << " " << i1 << " " << i2 << " " << gamma_ion << endl;
81 
82  cout<<"Ion Lorentz boost is "<<gamma_ion<<endl;
83 
84  if (!getline(inFile,line)) {cout <<" Error reading PHOTON line"<<endl;}
85  countLines++;
86  lineStream=stringstream(line); // PHOTON
87 
88 
89  // begin event loop here. eSTARlight events have the format:
90  // Event card, Vertex card, photon card, source card, track 1 card, track 2 card
91 
92  while (inFile.good())
93  {
94 
95  if (!getline(inFile,line)) {break;}
96  countLines++;
97  lineStream=stringstream(line);
98  lineStream>>label>>eventNmb>>nmbTracks;
99  if (!(label =="EVENT:")) continue;
100  if (eventNmb < 5) {cout <<"Reached Event "<<eventNmb<<endl;}
101 
102  // vertex card should be followed by gamma card, t card and source card.
103  if (!getline(inFile,line)) {break;}
104  countLines++;
105 
106  lineStream=stringstream(line);
107  lineStream>>label;
108 
109  if (!getline(inFile,line)) {break;}
110  lineStream=stringstream(line);
111  lineStream>>label>>kphoton>>qsquared;
112 
113  if (!getline(inFile,line)) {break;}
114  lineStream=stringstream(line);
115  lineStream>>label>>ttransfer;
116 
117  if (!getline(inFile,line)) {break;}
118  lineStream=stringstream(line);
119  lineStream>>label >> targx >> targy >> targz >> targE;
120 
121  if (!getline(inFile,line)) {break;}
122  lineStream=stringstream(line);
123  lineStream>>label >> sx >> sy >> sz >> sE;
124 
125  // two track cards
126  if (!getline(inFile,line)) {break;}
127  countLines++;
128 
129  lineStream=stringstream(line);
130  lineStream>>label>>pcode>>p1x>>p1y>>p1z>>i1>>i2>>i3>>pdgpid1;
131 
132  if (!getline(inFile,line)) {break;}
133  countLines++;
134  lineStream=stringstream(line);
135 
136  lineStream>>label>>pcode>>p2x>>p2y>>p2z>>i1>>i2>>i3>>pdgpid2;
137 
138  // get the final state masses should be particle anti-particle, so pdgcodes should be opposite
139  if (pdgpid1 != -pdgpid2)
140  { cout<<"Error pdgpid codes don't match"<<pdgpid1<<" "<<pdgpid2<<endl;
141  exit(-1);
142  }
143 
144  double pdgabs=abs(pdgpid1);
145  mfinal=0.;
146  if (pdgabs == 211) mfinal=0.139;
147  if (pdgabs == 11) mfinal=0.000511;
148  if (pdgabs == 13) mfinal=0.105;
149  if (pdgabs == 321) mfinal=0.494;
150  if (mfinal ==0)
151  {cout<<" Error final mass=0. pdgabs="<<pdgabs<<endl;
152  exit(-1);
153  }
154 
155  // Now do needed kinematics computations
156  double p1=sqrt(p1x*p1x+p1y*p1y+p1z*p1z);
157  double e1=sqrt(p1*p1+mfinal*mfinal);
158  p1prap=0.5*log((p1+p1z)/(p1-p1z));
159  p1pt=sqrt(p1x*p1x+p1y*p1y);
160 
161  double p2=sqrt(p2x*p2x+p2y*p2y+p2z*p2z);
162  double e2=sqrt(p2*p2+mfinal*mfinal);
163  p2prap=0.5*log((p2+p2z)/(p2-p2z));
164  p2pt=sqrt(p2x*p2x+p2y*p2y);
165 
166  eTheta = TMath::ATan(TMath::Sqrt(sx*sx+sy*sy)/sz);
167 
168  // final state
169  fspx=p1x+p2x;
170  fspy=p1y+p2y;
171  fspz=p1z+p2z;
172  double fse=e1+e2;
173 
174  // need to determine energy from pdgpid
175  fspt=sqrt(fspx*fspx+fspy*fspy);
176  fsmass=sqrt(fse*fse-(fspx*fspx+fspy*fspy+fspz*fspz));
177  fsrap= 0.5*log((fse+fspz)/(fse-fspz));
178  // done - now book NTuple
179 
180  // to calculate X_bjorken, need photon energy in lab frame
181  double kphotontargetframe=kphoton*gamma_ion;
182  double mass = sqrt(fsmass*fsmass+qsquared*qsquared);
183  double xbjorken=mass/(2.*gamma_ion*0.939)*exp(fsrap);
184 
185  //Fill ntuple
186  esNTuple->Fill(fspt,fspz,fsrap,fsmass,p1pt,p1z,p1prap,p2pt,p2z,p2prap,kphoton,qsquared,xbjorken);
187 
188  if (eventNmb < 5)
189  {
190  cout<<fspx<<fspy<<fspz<<fse<<fsrap<<fspt<<fsmass<<fsrap<<endl;
191  cout<<p1x<<p1y<<p1z<<p1prap<<" "<<p1<<" pt1="<<p1pt<<endl;
192  cout<<p2x<<p2y<<p2z<<p2prap<<" "<<p2<<" pt2="<<p2pt<<endl;
193  }
194 
195  } //Done with event loop.
196 
197  // write out NTuple
198  TString baseName = slightname.ReplaceAll(".out",".root");
199  TFile *NTfile = new TFile(TString::Format("ntuple_%s",baseName.Data()),"recreate");
200 
201  esNTuple->Write();
202  NTfile->Close();
203 
204 }
205 
206