forked from jan-kaspar/analysis_ctpps_alignment_2016_preTS2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalignment_classes.h
242 lines (188 loc) · 4.57 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
#ifndef _alignment_classes_h_
#define _alignment_classes_h_
#include <cstring>
#include <cstdio>
#include <cmath>
#include <map>
#include <vector>
#include <set>
#include <string>
#include "DataFormats/CTPPSReco/interface/CTPPSLocalTrackLite.h"
#include "DataFormats/CTPPSDetId/interface/TotemRPDetId.h"
//----------------------------------------------------------------------------------------------------
struct AlignmentResult
{
double sh_x, sh_x_unc; // mm
double sh_y, sh_y_unc; // mm
double rot_z, rot_z_unc; // 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,rot_z=%+.4f,rot_z_unc=%+.4f\n", sh_x, sh_x_unc, sh_y, sh_y_unc, rot_z, rot_z_unc);
}
CTPPSLocalTrackLite Apply(const CTPPSLocalTrackLite &tr) const
{
const double x = cos(rot_z) * tr.getX() + sin(rot_z) * tr.getY() + sh_x;
const double y = -sin(rot_z) * tr.getX() + cos(rot_z) * tr.getY() - sh_y;
return CTPPSLocalTrackLite(tr.getRPId(), x, 0., y, 0.);
}
};
//----------------------------------------------------------------------------------------------------
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)
{
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_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;
return Load(f);
fclose(f);
}
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