Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Shifter.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file Shifter.cc
1 #include "Shifter.h"
2 
3 #include <TFile.h>
4 #include <TH3.h>
5 #include <TString.h>
6 #include <TVector3.h>
7 
8 #include <cmath>
9 #include <cstdlib> // for getenv
10 
11 Shifter::Shifter(const std::string &truthfilename, const std::string &correctionfilename)
12 {
13  //load a 'truth' distortion map and, optionally, a map of a measured correction to those distortions
14  //this code is currently set up to load a particular correction map that doesn't have distortions
15  // in X,Y, and Z components, but rather only in R, R*Phi, and Z components.
16 
17  //single event distortion file
18  if (!truthfilename.empty())
19  {
20  forward = TFile::Open(truthfilename.c_str(), "READ");
21  if (forward)
22  {
23  forward->GetObject("hIntDistortionX", hX);
24  forward->GetObject("hIntDistortionY", hY);
25  forward->GetObject("hIntDistortionZ", hZ);
26 
27  //not strictly needed, but handy:
28  forward->GetObject("hIntDistortionR", hR);
29  forward->GetObject("hIntDistortionP", hPhi);
30  }
31  }
32  if (hX && hY && hZ)
33  {
34  hasTruth = true;
35  }
36 
37  //single event distortion file
38  if (!correctionfilename.empty())
39  {
40  //average=TFile::Open(correctionfilename,"READ");
41  // hardcoded????????
42  std::string correction_filename = std::string(getenv("CALIBRATIONROOT")) + "/distortion_maps/Distortions_full_realistic_micromegas_all-coarse.root";
43  average = TFile::Open(correction_filename.c_str(), "READ");
44  if (average)
45  {
46  average->GetObject("hIntDistortionX", hXave);
47  average->GetObject("hIntDistortionY", hYave);
48  average->GetObject("hIntDistortionZ", hZave);
49 
50  average->GetObject("hIntDistortionR", hRave);
51  average->GetObject("hIntDistortionP", hPhiave);
52  }
53  }
54  if (hXave && hYave && hZave)
55  {
56  hasCorrection = true;
57  }
58 }
59 
60 TVector3 Shifter::ShiftForward(const TVector3 &position)
61 {
62  double x, y, z, xshift, yshift, zshift;
63  //const double mm = 1.0;
64  //const double cm = 10.0;
65  TVector3 shiftposition;
66 
67  x = position.X();
68  y = position.Y();
69  z = position.Z();
70 
71  double r = position.Perp();
72  double phi = position.Phi();
73  if (position.Phi() < 0.0)
74  {
75  phi = position.Phi() + 2.0 * M_PI;
76  }
77 
78  //distort coordinate of stripe
79  xshift = 0;
80  yshift = 0;
81  zshift = 0;
82  if (hasTruth)
83  {
84  xshift = hX->Interpolate(phi, r, z);
85  yshift = hY->Interpolate(phi, r, z);
86  zshift = hZ->Interpolate(phi, r, z);
87  }
88 
89  //remove average distortion
90  if (hasCorrection)
91  {
92  double raveshift = hRave->Interpolate(phi, r, z);
93  double paveshift = hPhiave->Interpolate(phi, r, z); //hugo confirms the units are cm
94  double cosphi = cos(phi);
95  double sinphi = sin(phi);
96  xshift -= raveshift * cosphi - paveshift * sinphi;
97  yshift -= raveshift * sinphi + paveshift * cosphi;
98 
99  zshift -= hZave->Interpolate(phi, r, z);
100  }
101 
102  TVector3 forwardshift(x + xshift, y + yshift, z + zshift);
103 
104  return forwardshift;
105 }
106 
107 TVector3 Shifter::ShiftBack(const TVector3 &forwardshift)
108 {
109  double x, y, z;
110  // const double mm = 1.0;
111  //const double cm = 10.0;
112  TVector3 shiftposition;
113 
114  x = forwardshift.X();
115  y = forwardshift.Y();
116  z = forwardshift.Z();
117 
118  double rforward = forwardshift.Perp();
119  double phiforward = forwardshift.Phi();
120  if (forwardshift.Phi() < 0.0)
121  {
122  phiforward += 2.0 * M_PI;
123  }
124 
125  double xshiftback = -1 * hXBack->Interpolate(phiforward, rforward, z);
126  double yshiftback = -1 * hYBack->Interpolate(phiforward, rforward, z);
127  double zshiftback = -1 * hZBack->Interpolate(phiforward, rforward, z);
128 
129  shiftposition.SetXYZ(x + xshiftback, y + yshiftback, z + zshiftback);
130 
131  return shiftposition;
132 }
133 
134 TVector3 Shifter::Shift(const TVector3 &position)
135 {
136  return ShiftBack(ShiftForward(position));
137 }