ATTPCROOT  0.3.0-alpha
A ROOT-based framework for analyzing data from active target detectors
TInverseMap.cxx
Go to the documentation of this file.
1 
2 #include <TInverseMap.h>
3 #include <TSpline.h>
4 
5 #include <unistd.h>
6 
7 #include <cmath>
8 #include <cstdio>
9 #include <cstdlib>
10 #include <cstring>
11 #include <fstream> // IWYU pragma: keep
12 #include <iostream>
13 #include <memory>
14 #include <sstream> // IWYU pragma: keep
15 #include <utility>
16 
17 std::unique_ptr<TInverseMap> TInverseMap::fInverseMap = nullptr;
18 
19 TInverseMap::TInverseMap(const char *filename) : TNamed("InverseMap", filename)
20 {
21  ReadMapFile(filename);
22 }
23 
24 TInverseMap::TInverseMap() : TNamed("InverseMap", "multiInvMap") {}
25 
26 TInverseMap::~TInverseMap() = default;
27 
28 TInverseMap *TInverseMap::Get(const char *filename)
29 {
30  if (fInverseMap)
31  return fInverseMap.get();
32  if (strlen(filename) == 0 || access(filename, F_OK) == -1) {
33  printf("no inverse map loaded and file \"%s\" not found.\n", filename);
34  return nullptr;
35  }
36  fInverseMap = std::make_unique<TInverseMap>(filename);
37  return fInverseMap.get();
38 }
39 
40 bool TInverseMap::ReadMapFile(const char *filename)
41 {
42  std::string mapfile = filename;
43  /*if(mapfile.length()==0)
44  mapfile = TGRUTOptions::Get()->S800InverseMapFile();*/
45  if (mapfile.length() == 0 || access(mapfile.c_str(), F_OK) == -1) {
46  printf("no inverse map loaded and file \"%s\" not found.\n", mapfile.c_str());
47  return false;
48  }
49  // static std::mutex inv_map_mutex;
50 
51  std::ifstream infile;
52  infile.open(mapfile.c_str());
53  std::string line;
54  getline(infile, info);
55  sscanf(info.c_str(), "S800 inverse map - Brho=%g - M=%d - Q=%d", &fBrho, &fMass, &fCharge);
56 
57  int par = 0;
58  fsize = 0;
59  while (getline(infile, line)) {
60  if (line.find("----------") != std::string::npos)
61  continue;
62  if (line.find("COEFFICIENT") != std::string::npos) {
63  par++;
64  continue;
65  }
66  unsigned int index;
67  InvMapRow invrow{};
68  std::stringstream ss(line);
69  ss >> index;
70  /*if((index-1) != fMap[par-1].size()) {
71  //problems.
72  }*/
73  {
74  std::string temp;
75  ss >> temp;
76  invrow.coefficient = std::atof(temp.c_str());
77  fsize++;
78  }
79  // ss >> invrow.coefficient;
80  ss >> invrow.order;
81  ss >> invrow.exp[0];
82  ss >> invrow.exp[1];
83  ss >> invrow.exp[2];
84  ss >> invrow.exp[3];
85  ss >> invrow.exp[4];
86  ss >> invrow.exp[5];
87 
88  fMap[par - 1].push_back(invrow);
89 
90  // printf("%i\t%s\n",index,line.c_str());
91  }
92  return true;
93 }
94 
95 bool TInverseMap::ReadMultiMapFile(std::vector<std::string> &mapfile_v)
96 {
97 
98  std::cout << "mapfile size : " << mapfile_v.size() << std::endl;
99  fMap_v.clear();
100  for (auto &i : mapfile_v) {
101  bool isRead = false;
102  fMap.clear();
103  isRead = ReadMapFile(i.c_str());
104  fMap_v.push_back(fMap);
105  // fMapDist_v.push_back(0.1*(1+i));//change that, find a way to know the distance pivot-target for each map.
106  std::cout << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! " << i << std::endl;
107  if (!isRead)
108  std::cout << "! Inv map file not read : " << i << std::endl;
109  }
110 
111  std::vector<InvMapRow> invrow_v;
112  std::vector<std::vector<double>> coeff_v;
113  Int_t icoeff = 0;
114  TSpline3 *spline[fsize]; // NOLINT TODO:This plus code below looks like a memory leak...
115  // TGraph *graph[fsize];
116 
117  if (fMap_v.size() > 0)
118  for (int j = 0; j < fMap_v.at(0).size();
119  j++) { // loop on par//assuming the first map has the same format as the following maps
120  for (int k = 0; k < fMap_v.at(0).at(j).size(); k++) { // loop on coeff in par
121  std::vector<double> buff_v;
122  std::vector<InvMapRowS> blabla;
123  InvMapRowS invrow_s{};
124  for (auto &i : fMap_v) { // loop on maps
125  // std::cout<<"mapcoeff : "<<i<<" "<<j<<" "<<k<<" "<<fMap_v.at(i).at(j).at(k).coefficient<<std::endl;
126  buff_v.push_back((Double_t)i.at(j).at(k).coefficient);
127  // std::cout<<"invrow : "<<j<<" "<<invrow_v.at(j).coefficient<<std::endl;
128  }
129  coeff_v.push_back(buff_v);
130  char fname[20];
131  sprintf(fname, "f_%d", icoeff);
132  spline[icoeff] = new TSpline3(fname, &fMapDist_v[0], &buff_v[0], fMap_v.size()); // NOLINT
133  // graph[icoeff] = new TGraph(fMap_v.size(),&mapdist_v[0], &buff_v[0]);
134  // std::cout<<"debug : "<<j<<" "<<k<<" "<<fMap_v.size()<<" "<<fMapDist_v.back()<<"
135  // "<<spline[icoeff]->Eval(0.35)<<std::endl;
136  invrow_s.coefficient = spline[icoeff];
137  invrow_s.order = fMap_v.at(0).at(j).at(k).order;
138  invrow_s.exp[0] = fMap_v.at(0).at(j).at(k).exp[0];
139  invrow_s.exp[1] = fMap_v.at(0).at(j).at(k).exp[1];
140  invrow_s.exp[2] = fMap_v.at(0).at(j).at(k).exp[2];
141  invrow_s.exp[3] = fMap_v.at(0).at(j).at(k).exp[3];
142  invrow_s.exp[4] = fMap_v.at(0).at(j).at(k).exp[4];
143  invrow_s.exp[5] = fMap_v.at(0).at(j).at(k).exp[5];
144  fMap_s[j].push_back(invrow_s);
145  icoeff++;
146  }
147  // fMap_s[j].push_back(invrow_s);
148  }
149  // std::cout << "eval func " << fMap_s[0].at(0).coefficient->Eval(0.5) << " " << spline[0]->Eval(0.5) << std::endl;
150  return true;
151 }
152 
153 void TInverseMap::Print(Option_t *opt) const
154 {
155  printf("%s\n", info.c_str());
156  printf("\tBrho = %.04f\t", fBrho);
157  printf("Mass = %i\t", fMass);
158  printf("Q = %i\n", fCharge);
159  for (auto it1 : fMap) {
160  printf("----------- par: %i ---------------\n", it1.first);
161  int counter = 1;
162  for (auto &i : it1.second) {
163  printf("\t%i\t%.04f\t\t%i\t%i %i %i %i %i %i\n", counter++, i.coefficient, i.order, i.exp[0], i.exp[1],
164  i.exp[2], i.exp[3], i.exp[4], i.exp[5]);
165  }
166  }
167 }
168 
169 // Parameter def. |
170 // 0 == AtA |
171 // 1 == YTA |
172 // 2 == BTA |
173 // 3 == DTA |
174 
175 // Input def.
176 // 0 == Crdc 0 -x in meters. |
177 // 1 == - Afp in radians. |
178 // 2 == Crdc 0 +y in meters. |
179 // 3 == + Bfp in radians. |
180 // 4 == 0; |
181 // 5 == 0; |
182 
183 // S800 angles
184 // S800 system (looking dwnstream)
185 // positive ata: dispersive (aka: from x-axis) angle pointing down
186 // positive bta: none-dispersive (aka: from y-axis) angle pointing right
187 // yta: y position in [m], along none-dispersive angle, positive right
188 //
189 // GRETINA:
190 // z: beamaxis
191 // x: down (=S800 neg. dispersive direction)
192 // Y: left
193 // Opening for gate valve points to the floor (x positive)
194 //
195 // We transform S800 angle into GRETINA.
196 
197 float TInverseMap::Ata(int order, double xfp, double afp, double yfp, double bfp) const
198 {
199  float input[6];
200  input[0] = -xfp / 1000.0;
201  input[1] = -afp;
202  input[2] = yfp / 1000.0;
203  input[3] = bfp;
204  input[4] = 0.0;
205  input[5] = 0.0;
206  return MapCalc(order, 0, input);
207 }
208 
209 float TInverseMap::Bta(int order, double xfp, double afp, double yfp, double bfp) const
210 {
211  float input[6];
212  input[0] = -xfp / 1000.0;
213  input[1] = -afp;
214  input[2] = yfp / 1000.0;
215  input[3] = bfp;
216  input[4] = 0.0;
217  input[5] = 0.0;
218  return MapCalc(order, 2, input);
219 }
220 
221 float TInverseMap::Yta(int order, double xfp, double afp, double yfp, double bfp) const
222 {
223  float input[6];
224  input[0] = -xfp / 1000.0;
225  input[1] = -afp;
226  input[2] = yfp / 1000.0;
227  input[3] = bfp;
228  input[4] = 0.0;
229  input[5] = 0.0;
230  return MapCalc(order, 1, input);
231 }
232 
233 float TInverseMap::Dta(int order, double xfp, double afp, double yfp, double bfp) const
234 {
235  float input[6];
236  input[0] = -xfp / 1000.0;
237  input[1] = -afp;
238  input[2] = yfp / 1000.0;
239  input[3] = bfp;
240  input[4] = 0.0;
241  input[5] = 0.0;
242  return MapCalc(order, 3, input);
243 }
244 
245 float TInverseMap::Ata(int order, double xfp, double afp, double yfp, double bfp, double z)
246 {
247  float input[6];
248  input[0] = -xfp / 1000.0;
249  input[1] = -afp;
250  input[2] = yfp / 1000.0;
251  input[3] = bfp;
252  input[4] = 0.0;
253  input[5] = 0.0;
254  return MapCalc_s(order, 0, input, z);
255 }
256 
257 float TInverseMap::Bta(int order, double xfp, double afp, double yfp, double bfp, double z)
258 {
259  float input[6];
260  input[0] = -xfp / 1000.0;
261  input[1] = -afp;
262  input[2] = yfp / 1000.0;
263  input[3] = bfp;
264  input[4] = 0.0;
265  input[5] = 0.0;
266  return MapCalc_s(order, 2, input, z);
267 }
268 
269 float TInverseMap::Yta(int order, double xfp, double afp, double yfp, double bfp, double z)
270 {
271  float input[6];
272  input[0] = -xfp / 1000.0;
273  input[1] = -afp;
274  input[2] = yfp / 1000.0;
275  input[3] = bfp;
276  input[4] = 0.0;
277  input[5] = 0.0;
278  return MapCalc_s(order, 1, input, z);
279 }
280 
281 float TInverseMap::Dta(int order, double xfp, double afp, double yfp, double bfp, double z)
282 {
283  float input[6];
284  input[0] = -xfp / 1000.0;
285  input[1] = -afp;
286  input[2] = yfp / 1000.0;
287  input[3] = bfp;
288  input[4] = 0.0;
289  input[5] = 0.0;
290  return MapCalc_s(order, 3, input, z);
291 }
292 
293 /*
294 float TInverseMap::Ata(int order, const TS800 *s800) {
295  float input[6];
296  input[0] = - s800->GetXFP() / 1000.0;
297  input[1] = - s800->GetAFP();
298  input[2] = s800->GetYFP() / 1000.0;
299  input[3] = s800->GetBFP();
300  input[4] = 0.0;
301  input[5] = 0.0;
302  int par = 0; // AtA
303  return MapCalc(order,par,input);
304 }
305 
306 float TInverseMap::Bta(int order, const TS800 *s800) {
307  float input[6];
308  input[0] = - s800->GetXFP() / 1000.0;
309  input[1] = - s800->GetAFP();
310  input[2] = s800->GetYFP() / 1000.0;
311  input[3] = s800->GetBFP();
312  input[4] = 0.0;
313  input[5] = 0.0;
314  int par = 2; // BTA
315  return MapCalc(order,par,input);
316 }
317 
318 float TInverseMap::Yta(int order, const TS800 *s800) {
319  float input[6];
320  input[0] = - s800->GetXFP() / 1000.0;
321  input[1] = - s800->GetAFP();
322  input[2] = s800->GetYFP() / 1000.0;
323  input[3] = s800->GetBFP();
324  input[4] = 0.0;
325  input[5] = 0.0;
326  int par = 1; // YTA
327  // *1000, because the map returns the value in meters.
328  return MapCalc(order,par,input)*1000;
329 }
330 
331 float TInverseMap::Dta(int order, const TS800 *s800) {
332  float input[6];
333  input[0] = - s800->GetXFP() / 1000.0;
334  input[1] = - s800->GetAFP();
335  input[2] = s800->GetYFP() / 1000.0;
336  input[3] = s800->GetBFP();
337  input[4] = 0.0;
338  input[5] = 0.0;
339  int par = 3; // DTA
340  return MapCalc(order,par,input);
341 }*/
342 
343 float TInverseMap::MapCalc(int order, int par, float *input) const
344 {
345  float cumul = 0.0;
346  float multiplicator = 0.0;
347  std::vector<InvMapRow> vec = fMap.at(par);
348  for (auto &x : vec) {
349  if (order < x.order)
350  break;
351  multiplicator = 1.0;
352  for (int y = 0; y < 6; y++) {
353  if (x.exp[y] != 0)
354  multiplicator *= pow(input[y], x.exp[y]);
355  }
356  cumul += multiplicator * x.coefficient;
357  }
358  return cumul;
359 }
360 
361 float TInverseMap::MapCalc_s(int order, int par, float *input, double z)
362 {
363  float cumul = 0.0;
364  float multiplicator = 0.0;
365  std::vector<InvMapRowS> vec = fMap_s.at(par);
366  for (auto &x : vec) {
367  if (order < x.order)
368  break;
369  multiplicator = 1.0;
370  for (int y = 0; y < 6; y++) {
371  if (x.exp[y] != 0)
372  multiplicator *= pow(input[y], x.exp[y]);
373  }
374  cumul += multiplicator * x.coefficient->Eval(z);
375  }
376 
377  return cumul;
378 }
TInverseMap::MapCalc_s
float MapCalc_s(int order, int par, float *input, double z)
Definition: TInverseMap.cxx:361
TInverseMap::Print
virtual void Print(Option_t *opt="") const
Definition: TInverseMap.cxx:153
TInverseMap::Bta
float Bta(int degree, double xfp, double afp, double yfp, double bfp) const
Definition: TInverseMap.cxx:209
TInverseMap::~TInverseMap
virtual ~TInverseMap()
TInverseMap::Get
static TInverseMap * Get(const char *filename="")
Definition: TInverseMap.cxx:28
TInverseMap
Definition: TInverseMap.h:19
TInverseMap::Yta
float Yta(int degree, double xfp, double afp, double yfp, double bfp) const
Definition: TInverseMap.cxx:221
y
const double * y
Definition: lmcurve.cxx:20
TInverseMap::Ata
float Ata(int degree, double xfp, double afp, double yfp, double bfp) const
Definition: TInverseMap.cxx:197
TInverseMap::ReadMultiMapFile
bool ReadMultiMapFile(std::vector< std::string > &str)
Definition: TInverseMap.cxx:95
TInverseMap::Dta
float Dta(int degree, double xfp, double afp, double yfp, double bfp) const
Definition: TInverseMap.cxx:233
TInverseMap::TInverseMap
TInverseMap()
Definition: TInverseMap.cxx:24
TInverseMap::MapCalc
float MapCalc(int, int, float *) const
Definition: TInverseMap.cxx:343
TInverseMap.h