File indexing completed on 2025-12-19 09:18:45
0001 #include "Shifter.h"
0002
0003 #include <TFile.h>
0004 #include <TH3.h>
0005 #include <TString.h>
0006 #include <TVector3.h>
0007
0008 #include <cmath>
0009 #include <cstdlib> // for getenv
0010
0011 Shifter::Shifter(const std::string &truthfilename, const std::string &correctionfilename)
0012 {
0013
0014
0015
0016
0017
0018 if (!truthfilename.empty())
0019 {
0020 forward = TFile::Open(truthfilename.c_str(), "READ");
0021 if (forward)
0022 {
0023 forward->GetObject("hIntDistortionX", hX);
0024 forward->GetObject("hIntDistortionY", hY);
0025 forward->GetObject("hIntDistortionZ", hZ);
0026
0027
0028 forward->GetObject("hIntDistortionR", hR);
0029 forward->GetObject("hIntDistortionP", hPhi);
0030 }
0031 }
0032 if (hX && hY && hZ)
0033 {
0034 hasTruth = true;
0035 }
0036
0037
0038 if (!correctionfilename.empty())
0039 {
0040
0041
0042 std::string correction_filename = std::string(getenv("CALIBRATIONROOT")) + "/distortion_maps/Distortions_full_realistic_micromegas_all-coarse.root";
0043 average = TFile::Open(correction_filename.c_str(), "READ");
0044 if (average)
0045 {
0046 average->GetObject("hIntDistortionX", hXave);
0047 average->GetObject("hIntDistortionY", hYave);
0048 average->GetObject("hIntDistortionZ", hZave);
0049
0050 average->GetObject("hIntDistortionR", hRave);
0051 average->GetObject("hIntDistortionP", hPhiave);
0052 }
0053 }
0054 if (hXave && hYave && hZave)
0055 {
0056 hasCorrection = true;
0057 }
0058 }
0059
0060 TVector3 Shifter::ShiftForward(const TVector3 &position) const
0061 {
0062 double x;
0063 double y;
0064 double z;
0065 double xshift;
0066 double yshift;
0067 double zshift;
0068
0069
0070 TVector3 shiftposition;
0071
0072 x = position.X();
0073 y = position.Y();
0074 z = position.Z();
0075
0076 double r = position.Perp();
0077 double phi = position.Phi();
0078 if (position.Phi() < 0.0)
0079 {
0080 phi = position.Phi() + 2.0 * M_PI;
0081 }
0082
0083
0084 xshift = 0;
0085 yshift = 0;
0086 zshift = 0;
0087 if (hasTruth)
0088 {
0089 xshift = hX->Interpolate(phi, r, z);
0090 yshift = hY->Interpolate(phi, r, z);
0091 zshift = hZ->Interpolate(phi, r, z);
0092 }
0093
0094
0095 if (hasCorrection)
0096 {
0097 double raveshift = hRave->Interpolate(phi, r, z);
0098 double paveshift = hPhiave->Interpolate(phi, r, z);
0099 double cosphi = cos(phi);
0100 double sinphi = sin(phi);
0101 xshift -= raveshift * cosphi - paveshift * sinphi;
0102 yshift -= raveshift * sinphi + paveshift * cosphi;
0103
0104 zshift -= hZave->Interpolate(phi, r, z);
0105 }
0106
0107 TVector3 forwardshift(x + xshift, y + yshift, z + zshift);
0108
0109 return forwardshift;
0110 }
0111
0112 TVector3 Shifter::ShiftBack(const TVector3 &forwardshift) const
0113 {
0114 double x;
0115 double y;
0116 double z;
0117
0118
0119 TVector3 shiftposition;
0120
0121 x = forwardshift.X();
0122 y = forwardshift.Y();
0123 z = forwardshift.Z();
0124
0125 double rforward = forwardshift.Perp();
0126 double phiforward = forwardshift.Phi();
0127 if (forwardshift.Phi() < 0.0)
0128 {
0129 phiforward += 2.0 * M_PI;
0130 }
0131
0132 double xshiftback = -1 * hXBack->Interpolate(phiforward, rforward, z);
0133 double yshiftback = -1 * hYBack->Interpolate(phiforward, rforward, z);
0134 double zshiftback = -1 * hZBack->Interpolate(phiforward, rforward, z);
0135
0136 shiftposition.SetXYZ(x + xshiftback, y + yshiftback, z + zshiftback);
0137
0138 return shiftposition;
0139 }
0140
0141 TVector3 Shifter::Shift(const TVector3 &position) const
0142 {
0143 return ShiftBack(ShiftForward(position));
0144 }