-
Notifications
You must be signed in to change notification settings - Fork 0
/
amplitudesCalculator.cpp
357 lines (256 loc) · 9.67 KB
/
amplitudesCalculator.cpp
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
// USAGE : compilation :g++ amplitudeCalculator.cpp -o amplitudeCalculator
// execution : ./amplitudeCalculator -w w file
// where w : Gap between detectors.
// GOAL :calculate the angles from four pieces of information in decimal format. It is different from the one applied on real data, because the reading is not in binary code this time.
#include <iostream>
#include <fstream>
#include <unistd.h>
#include <string>
#include <cstdlib>
#include <vector>
#include <cmath>
#include <iomanip>
#include <sstream>
#include <random>
using namespace std;
//pi Definition
const double PI = 4*atan(1);
//Shorthand notation to make the code more understadable and to allow for
//drop-in replacement by another C++11 random number generator.
typedef mt19937 Generator;
//A fonction that generate a random position within a certain rectangle
double getRandom(Generator& generator){
return generate_canonical<double, 32>(generator);
}
double getRandomInRange(Generator& generator, double x0, double length){
return length*getRandom(generator) + x0;
}
/**************************************************************************************/
vector <double> misalignment (double delta, vector <double> aligned, int detectorPlan){
vector <double> outputVector;
for(unsigned int i=0;i<aligned.size();i++){
aligned[i] = aligned[i] + delta;
outputVector.push_back(aligned[i]);
}
return outputVector;
}
/****************************************************************************************/
pair<vector <double>, vector<double> > generalCase(vector <double> x1, vector <double> y2, vector <double> x3, vector <double> y4, double w, double delta1, double delta2, double delta3, double delta4){
if(x1.size() == 0 || x1.size() != y2.size() || x1.size() != x3.size()
|| x1.size()!= y4.size()){
cerr << "Error with data." << endl;
cerr << "The files must contain the same number(>0) of values." << endl;
cerr << "Data01 : " << x1.size() << " read values" << endl;
cerr << "Data02 : " << y2.size() << " read values" << endl;
cerr << "Data03 : " << x3.size() << " read values" << endl;
cerr << "Data04 : " << y4.size() << " read values" << endl;
exit(EXIT_FAILURE);
}
double x, y,z,r;
vector<double> rho(x1.size()), theta(x1.size());
vector<double> rhoInDeg(x1.size()), thetaInDeg(x1.size());
vector<double> x1Mis = misalignment(delta1,x1,1);
vector<double> y2Mis = misalignment(delta2,y2,2);
vector<double> x3Mis = misalignment(delta3,x3,3);
vector<double> y4Mis = misalignment(delta4,y4,4);
;
for(unsigned int i=0; i< x1.size(); i++){
x = x3Mis[i] - x1Mis[i];
y = y4Mis[i] - y2Mis[i];
z = w + 7.4;
r = sqrt(x * x + y * y + z * z);
rho[i] = x == 0.0 ? 0.0 : atan(y / x);
theta[i] = acos(z / r);
rhoInDeg[i]= rho[i]*180/PI;
thetaInDeg[i]= theta[i]*180/PI;
}
return make_pair(rhoInDeg, thetaInDeg);
}
/*****************************************************************************************/
void writeFile(string filename, vector<double> v){
ofstream outFile(filename.c_str());
if(!outFile.is_open()){
std::cerr << "Error while writing the file \'" << filename << "\'" << endl;
return;
}
for(unsigned int i=0; i< v.size(); i++)
outFile << v[i] << endl;
outFile.close();
}
/******************************************************************************************/
vector<vector <double> > readFile(string filename){
vector<double> line;
vector<vector<double> > matrix;
double value;
ifstream myFile(filename.c_str());
if(!myFile.is_open()){
cerr << "Error while opening the file \'" << filename << "\'." << endl;
exit(EXIT_FAILURE);
}
if(!myFile.good()){
cerr << "Error with the file \'" << filename << "\'."<< endl;
cerr << "Reading cancelled." << endl;
exit(EXIT_FAILURE);
}
while(!myFile.eof()){
myFile >> value;
line.push_back(value);
if(myFile.peek() == '\n'){
matrix.push_back(line);
line.clear();
}
}
myFile.close();
return matrix;
}
/****************************************************************************************/
string getHelpPage(char* scriptName){
string name(scriptName);
string out = "Usage: " + name + " -w w file \n";
out += "\t-w w\t Gap between detectors\n";
out += "\tfile\t file containing data\n";
out += "The program applies the following formulas :\n";
out += "All rho will be printed in the outfile \'rho_X\' and all theta\n";
out += "in the outfile \'theta_X\' (where X is the initial outfile name).\n";
return out;
}
/*************************************************************************************/
/*vector<double> conversion(vector<double> stripNumber){
vector<double> position;
double stripWidth=0.9;
double interstrip = 0.15;
double firstStrip = 0.45;
double strip=stripNumber[0];
double value=0;
double lengthVector = stripNumber.size();
for(int i=0;i<lengthVector;i++){
value = firstStrip + (stripNumber[i]-1)*(stripWidth+interstrip);
position.push_back(value);
}
return position;
}*/
/*****************************************************************************************/
double conversion(double stripNumber){
double position;
double stripWidth=0.9;
double interstrip = 0.15;
double firstStrip = 0.45;
position = firstStrip + (stripNumber-1)*(stripWidth+interstrip);
return position;
}
/*************************************************************************************************************************************************************************************************************************************************************************************/
int main(int argc, char** argv){
// Start with random seed
random_device rd;
Generator generator(rd());
// Get the thing started
generator.discard(100);
int opt;
char* endptr;
string outfileName1 = "alignedAmplitudes.txt";
string outfileName2 = "misalignedAmplitudes.txt";
string outfileName3 = "misalignEffect.txt";
vector < vector <double> > dataIn;
double w = -1;
const double delta1 = getRandomInRange(generator,-1,2);
const double delta2 = getRandomInRange(generator,-1,2);
const double delta3 = getRandomInRange(generator,-1,2);
const double delta4 = getRandomInRange(generator,-1,2);
cout << "delta1 = " << delta1 << endl;
cout << "delta2 = " << delta2 << endl;
cout << "delta3 = " << delta3 << endl;
cout << "delta4 = " << delta4 << endl;
string filename;
vector<double> data01, data02, data03, data04, outputS;
double data1, data2, data3, data4;
pair<vector<double>, vector<double> > outputAligned;
pair<vector<double>, vector<double> > outputMisaligned;
pair<vector<double>, vector<double> > outputDifference;
while((opt = getopt(argc, argv, "w:h")) != -1){
switch(opt){
case 'w':
endptr = optarg;
w = strtof(optarg,&endptr);
if(optarg == endptr || w<0){
cerr << "invalid value for option -w" << endl;
return EXIT_FAILURE;
}
break;
case 'h':
cout << getHelpPage(argv[0]);
return EXIT_SUCCESS;
default:
cerr << getHelpPage(argv[0]);
return EXIT_FAILURE;
}
}
if(argc < 2){
cerr << "Some arguments (or options) are missing" << endl;
cerr << "\n" << getHelpPage(argv[0]);
return EXIT_FAILURE;
}
if( w == -1){
cerr << "The options -w must be set !" << endl;
cerr << "\n" << getHelpPage(argv[0]);
return EXIT_FAILURE;
}
filename = argv[optind];
cout << "Reading file..." << endl;
dataIn = readFile(filename);
// Aligned detectors configuration
for(int i=0; i<dataIn.size();i++){
if(dataIn[i].size()!=4){
cerr<<"Data n° "<< (i+1)<< " do not have 4 values. \nData ignored."<<endl;
}
else{
data1 = conversion(dataIn[i][0]);
data2 = conversion(dataIn[i][1]);
data3 = conversion(dataIn[i][2]);
data4 = conversion(dataIn[i][3]);
data01.push_back(data1);
data02.push_back(data2);
data03.push_back(data3);
data04.push_back(data4);
}
}
//cout << "Calculating the amplitudes in aligned case.." << endl;
outputAligned = generalCase(data01, data02, data03, data04,w,0,0,0,0);
//cout << "Writing the results (1/2)..." << endl;
//writeFile("rho_"+outfileName1, outputAligned.first);
//cout << "Writing the results (2/2)..." << endl;
//writeFile("theta_"+outfileName1, outputAligned.second);
// Misaligned detectors configuration with random delta in x and y
for(int i=0; i<dataIn.size();i++){
if(dataIn[i].size()!=4){
cerr<<"Data n° "<< (i+1)<< " do not have 4 values. \nData ignored."<<endl;
}
else{
data1 = conversion(dataIn[i][0]);
data2 = conversion(dataIn[i][1]);
data3 = conversion(dataIn[i][2]);
data4 = conversion(dataIn[i][3]);
data01[i]=data1;
data02[i]=data2;
data03[i]=data3;
data04[i]=data4;
}
}
//cout << "Calculating the amplitudes in misaligned case..." << endl;
outputMisaligned = generalCase(data01, data02, data03, data04,w,delta1,delta2,delta3,delta4);
cout << "length data01 : " << data01.size() << endl;
//cout << "Writing the results (1/2)..." << endl;
//writeFile("rho_"+outfileName2, outputMisaligned.first);
//cout << "Writing the results (2/2)..." << endl;
//writeFile("theta_"+outfileName2, outputMisaligned.second);
outputDifference.first = vector<double>(outputMisaligned.first.size());
outputDifference.second = vector<double>(outputMisaligned.first.size());
for(unsigned int k = 0; k < outputAligned.first.size();k++){
outputDifference.first[k] = outputMisaligned.first[k] - outputAligned.first[k];
outputDifference.second[k] = outputMisaligned.second[k] - outputAligned.second[k];
}
//cout << "Writing the results (1/2)..." << endl;
writeFile("rho_"+outfileName3, outputDifference.first);
//cout << "Writing the results (2/2)..." << endl;
writeFile("theta_"+outfileName3, outputDifference.second);
return EXIT_SUCCESS;
}