ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PHG4TpcDistortion.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file PHG4TpcDistortion.cc
1 // $Id: $
2 
11 #include "PHG4TpcDistortion.h"
12 
13 #include <TFile.h>
14 #include <TH3.h>
15 #include <TTree.h>
16 
17 #include <cmath> // for sqrt, fabs, NAN
18 #include <cstdlib> // for exit
19 #include <iostream>
20 
21 namespace
22 {
23  template <class T>
24  inline constexpr T square(const T& x)
25  {
26  return x * x;
27  }
28 
29 } // namespace
30 
31 //__________________________________________________________________________________________________________
33 {
35  {
36  std::cout << "PHG4TpcDistortion::Init - m_static_distortion_filename: " << m_static_distortion_filename << std::endl;
37  m_static_tfile.reset(new TFile(m_static_distortion_filename.c_str()));
38  if (!m_static_tfile->IsOpen())
39  {
40  std::cout << "Static distortion file could not be opened!" << std::endl;
41  exit(1);
42  }
43 
44  //Open Static Space Charge Maps
45  hDRint[0] = dynamic_cast<TH3*>(m_static_tfile->Get("hIntDistortionR_negz"));
46  hDRint[1] = dynamic_cast<TH3*>(m_static_tfile->Get("hIntDistortionR_posz"));
47  hDPint[0] = dynamic_cast<TH3*>(m_static_tfile->Get("hIntDistortionP_negz"));
48  hDPint[1] = dynamic_cast<TH3*>(m_static_tfile->Get("hIntDistortionP_posz"));
49  hDZint[0] = dynamic_cast<TH3*>(m_static_tfile->Get("hIntDistortionZ_negz"));
50  hDZint[1] = dynamic_cast<TH3*>(m_static_tfile->Get("hIntDistortionZ_posz"));
51 
52  }
53 
55  {
56  std::cout << "PHG4TpcDistortion::Init - m_time_ordered_distortion_filename: " << m_time_ordered_distortion_filename << std::endl;
58  if (!m_time_ordered_tfile->IsOpen())
59  {
60  std::cout << "TimeOrdered distortion file could not be opened!" << std::endl;
61  exit(1);
62  }
63 
64  // create histograms
65  TimehDR[0] = new TH3F();
66  TimehDR[1] = new TH3F();
67  TimehDP[0] = new TH3F();
68  TimehDP[1] = new TH3F();
69  TimehDZ[0] = new TH3F();
70  TimehDZ[1] = new TH3F();
71 
72  TimeTree = static_cast<TTree*>(m_time_ordered_tfile->Get("TimeDists"));
73  TimeTree->SetBranchAddress("hIntDistortionR_negz", &(TimehDR[0]));
74  TimeTree->SetBranchAddress("hIntDistortionR_posz", &(TimehDR[1]));
75  TimeTree->SetBranchAddress("hIntDistortionP_negz", &(TimehDP[0]));
76  TimeTree->SetBranchAddress("hIntDistortionP_posz", &(TimehDP[1]));
77  TimeTree->SetBranchAddress("hIntDistortionZ_negz", &(TimehDZ[0]));
78  TimeTree->SetBranchAddress("hIntDistortionZ_posz", &(TimehDZ[1]));
79  }
80 }
81 
82 //__________________________________________________________________________________________________________
83 void PHG4TpcDistortion::load_event(int event_num)
84 {
85  if (TimeTree)
86  {
87  int nentries = TimeTree->GetEntries();
88  if (event_num > nentries) event_num = event_num % nentries;
89  if (event_num % nentries == 0 && event_num != 0)
90  {
91  std::cout << "Distortion map sequence repeating as of event number " << event_num << std::endl;
92  }
93  TimeTree->GetEntry(event_num);
94  }
95 
96  return;
97 }
98 
99 //__________________________________________________________________________________________________________
100 double PHG4TpcDistortion::get_x_distortion_cartesian(double x, double y, double z) const
101 {
102  double r=sqrt(x*x+y*y);
103  double phi=std::atan2(y,x);
104 
105  //get components
106  double dr=get_distortion('r', r, phi, z);
107  double dphi=get_distortion('p', r, phi, z);
108 
109  //rotate into cartesian based on local r phi:
110  double cosphi=cos(phi);
111  double sinphi=sin(phi);
112  double dx=dr*cosphi-dphi*sinphi;
113  return dx;
114 }
115 
116 //__________________________________________________________________________________________________________
117 double PHG4TpcDistortion::get_y_distortion_cartesian(double x, double y, double z) const
118 {
119  double r=sqrt(x*x+y*y);
120  double phi=std::atan2(y,x);
121 
122  //get components
123  double dr=get_distortion('r', r, phi, z);
124  double dphi=get_distortion('p', r, phi, z);
125 
126  //rotate into cartesian based on local r phi:
127  double cosphi=cos(phi);
128  double sinphi=sin(phi);
129  double dy=dphi*cosphi+dr*sinphi;
130  return dy;
131 }
132 
133 //__________________________________________________________________________________________________________
134 double PHG4TpcDistortion::get_z_distortion_cartesian(double x, double y, double z) const
135 {
136  double r=sqrt(x*x+y*y);
137  double phi=std::atan2(y,x);
138 
139  //get components
140  double dz=get_distortion('z', r, phi, z);
141 
142  return dz;
143 }
144 
145 
146 //__________________________________________________________________________________________________________
147 double PHG4TpcDistortion::get_r_distortion(double r, double phi, double z) const
148 {
149  return get_distortion('r',r,phi, z);
150 }
151 
152 //__________________________________________________________________________________________________________
153 double PHG4TpcDistortion::get_rphi_distortion(double r, double phi, double z) const
154 {
155  return get_distortion('p',r,phi, z);
156 }
157 
158 //__________________________________________________________________________________________________________
159 double PHG4TpcDistortion::get_z_distortion(double r, double phi, double z) const
160 {
161  return get_distortion('z',r,phi, z);
162 }
163 
164 double PHG4TpcDistortion::get_distortion(char axis, double r, double phi, double z) const
165 {
166  if (phi < 0) phi += 2 * M_PI;
167  const int zpart=(z>0?1:0); //z<0 corresponds to the negative side, which is element 0.
168 
169  TH3* hdistortion=nullptr;
170 
171  if (axis!='r' && axis!='p' && axis !='z'){
172  std::cout << "Distortion Requested along axis " << axis << " which is invalid. Exiting.\n" << std::endl;
173  exit(1);
174  }
175 
176  double _distortion=0.;
177 
178  //select the appropriate histogram:
180  {
181  if (axis=='r'){
182  hdistortion=hDRint[zpart];
183  } else if (axis=='p'){
184  hdistortion=hDPint[zpart];
185  } else if (axis=='z'){
186  hdistortion=hDZint[zpart];
187  }
188  if (hdistortion){
189  _distortion+=hdistortion->Interpolate(phi, r, z);
190  } else {
191  std::cout << "Static Distortion Requested along axis " << axis << ", but distortion map does not exist. Exiting.\n" << std::endl;
192  exit(1);
193  }
194  }
195 
197  {
198  if (axis=='r'){
199  hdistortion=TimehDR[zpart];
200  } else if (axis=='p'){
201  hdistortion=TimehDP[zpart];
202  } else if (axis=='z'){
203  hdistortion=TimehDZ[zpart];
204  }
205  if (hdistortion){
206  _distortion+=hdistortion->Interpolate(phi, r, z);
207  } else {
208  std::cout << "Time Series Distortion Requested along axis " << axis << ", but distortion map does not exist. Exiting.\n" << std::endl;
209  exit(1);
210  }
211  }
212 
213  return _distortion;
214 }