forked from jan-kaspar/analysis_ctpps_alignment_2018
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalignment_classes.h
289 lines (226 loc) · 5.72 KB
/
alignment_classes.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#ifndef _alignment_classes_h_
#define _alignment_classes_h_
#include <cstring>
#include <cstdio>
#include <cmath>
#include <map>
#include <vector>
#include <set>
#include <string>
#include <Math/Rotation3D.h>
#include <Math/RotationZYX.h>
#include <Math/Vector3D.h>
#include "DataFormats/CTPPSReco/interface/CTPPSLocalTrackLite.h"
#include "DataFormats/CTPPSDetId/interface/TotemRPDetId.h"
//----------------------------------------------------------------------------------------------------
struct AlignmentResult
{
double sh_x=0., sh_x_unc=0.; // mm
double sh_y=0., sh_y_unc=0.; // mm
double rot_x=0., rot_x_unc=0.; // rad
double rot_y=0., rot_y_unc=0.; // rad
double rot_z=0., rot_z_unc=0.; // rad
AlignmentResult(double _sh_x=0., double _sh_x_unc=0., double _sh_y=0., double _sh_y_unc=0., double _rot_z=0., double _rot_z_unc=0.) :
sh_x(_sh_x), sh_x_unc(_sh_x_unc), sh_y(_sh_y), sh_y_unc(_sh_y_unc), rot_z(_rot_z), rot_z_unc(_rot_z_unc)
{
}
void Write(FILE *f) const
{
fprintf(f, "sh_x=%+.3f,sh_x_unc=%+.3f,sh_y=%+.3f,sh_y_unc=%+.3f,", sh_x, sh_x_unc, sh_y, sh_y_unc);
fprintf(f, "rot_x=%+.4f,rot_x_unc=%+.4f,rot_y=%+.4f,rot_y_unc=%+.4f,rot_z=%+.4f,rot_z_unc=%+.4f\n", rot_x, rot_x_unc, rot_y, rot_y_unc, rot_z, rot_z_unc);
}
CTPPSLocalTrackLite Apply(const CTPPSLocalTrackLite &tr) const
{
ROOT::Math::DisplacementVector3D<ROOT::Math::Cartesian3D<double>> v(tr.getX(), tr.getY(), 0.);
ROOT::Math::DisplacementVector3D<ROOT::Math::Cartesian3D<double>> s(sh_x, -sh_y, 0.);
ROOT::Math::RotationZYX R(-rot_z, rot_y, rot_x);
v = R * v + s;
return CTPPSLocalTrackLite(tr.getRPId(), v.x(), 0., v.y(), 0.,
tr.getTx(), tr.getTxUnc(), tr.getTy(), tr.getTyUnc(),
tr.getChiSquaredOverNDF(), tr.getPixelTrackRecoInfo(), tr.getNumberOfPointsUsedForFit(),
tr.getTime(), tr.getTimeUnc()
);
}
};
//----------------------------------------------------------------------------------------------------
struct AlignmentResults : public std::map<unsigned int, AlignmentResult>
{
void Write(FILE *f) const
{
for (auto &p : *this)
{
fprintf(f, "id=%u,", p.first);
p.second.Write(f);
}
}
int Add(char *line)
{
// ignore anything after "#"
char *hash_pos = strstr(line, "#");
if (hash_pos)
*hash_pos = 0;
// defaults
bool idSet = false;
unsigned int id = 0;
AlignmentResult result;
// loop over entries separated by ","
char *p = strtok(line, ",");
while (p != NULL)
{
// isolate key and value strings
char *pe = strstr(p, "=");
if (pe == NULL)
{
printf("ERROR in AlignmentResults::Add > entry missing = sign: %s.\n", p);
return 2;
}
char *s_key = p;
p = strtok(NULL, ",");
*pe = 0;
char *s_val = pe+1;
// interprete keys
if (strcmp(s_key, "id") == 0)
{
idSet = true;
id = atoi(s_val);
continue;
}
if (strcmp(s_key, "sh_x") == 0)
{
result.sh_x = atof(s_val);
continue;
}
if (strcmp(s_key, "sh_x_unc") == 0)
{
result.sh_x_unc = atof(s_val);
continue;
}
if (strcmp(s_key, "sh_y") == 0)
{
result.sh_y = atof(s_val);
continue;
}
if (strcmp(s_key, "sh_y_unc") == 0)
{
result.sh_y_unc = atof(s_val);
continue;
}
if (strcmp(s_key, "rot_x") == 0)
{
result.rot_x = atof(s_val);
continue;
}
if (strcmp(s_key, "rot_x_unc") == 0)
{
result.rot_x_unc = atof(s_val);
continue;
}
if (strcmp(s_key, "rot_y") == 0)
{
result.rot_y = atof(s_val);
continue;
}
if (strcmp(s_key, "rot_y_unc") == 0)
{
result.rot_y_unc = atof(s_val);
continue;
}
if (strcmp(s_key, "rot_z") == 0)
{
result.rot_z = atof(s_val);
continue;
}
if (strcmp(s_key, "rot_z_unc") == 0)
{
result.rot_z_unc = atof(s_val);
continue;
}
printf("ERROR in AlignmentResults::Add > unknown key: %s.\n", s_key);
return 3;
}
if (!idSet)
{
printf("ERROR in AlignmentResults::Add > id not set on the following line:\n%s.\n", line);
return 4;
}
insert({id, result});
return 0;
}
std::vector<CTPPSLocalTrackLite> Apply(const std::vector<CTPPSLocalTrackLite> &input) const
{
std::vector<CTPPSLocalTrackLite> output;
for (auto &t : input)
{
auto ait = find(t.getRPId());
if (ait == end())
throw cms::Exception("alignment") << "No alignment data for RP " << t.getRPId();
output.emplace_back(ait->second.Apply(t));
}
return output;
}
};
//----------------------------------------------------------------------------------------------------
struct AlignmentResultsCollection : public std::map<std::string, AlignmentResults>
{
int Write(const std::string &fn) const
{
FILE *f = fopen(fn.c_str(), "w");
if (!f)
return 1;
Write(f);
fclose(f);
return 0;
}
void Write(FILE *f) const
{
for (auto &p : *this)
{
fprintf(f, "\n[%s]\n", p.first.c_str());
p.second.Write(f);
}
}
int Load(const std::string &fn)
{
FILE *f = fopen(fn.c_str(), "r");
if (!f)
return 1;
int r = Load(f);
fclose(f);
return r;
}
int Load(FILE *f)
{
std::string label = "unknown";
AlignmentResults block;
while (!feof(f))
{
char line[300];
char *success = fgets(line, 300, f);
if (success == NULL)
break;
if (line[0] == '\n')
continue;
if (line[0] == '[')
{
if (block.size() > 0)
insert({label, block});
block.clear();
char *end = strstr(line, "]");
if (end == NULL)
{
printf("ERROR in AlignmentResultsCollection::Load > missing closing ].\n");
return 1;
}
*end = 0;
label = line+1;
continue;
}
if (block.Add(line) != 0)
return 2;
}
if (block.size() > 0)
insert({label, block});
return 0;
}
};
#endif