source: ZHANGProjects/ICOSIM/CPP/trunk/source/CrystalCollimator.cc @ 2

Last change on this file since 2 was 2, checked in by zhangj, 10 years ago

Initial import

File size: 4.7 KB
Line 
1#include <iostream>
2#include <fstream>
3#include <iomanip>
4#include <vector>
5#include <string>
6#include <cmath>
7#include "CrystalCollimator.h"
8using namespace std;
9
10int CrystalCollimator::crystaloutflag = 0;
11
12
13CrystalCollimator::CrystalCollimator(const double& ALFX, const double& ALFY, const double& APER_1, const double& APER_2, const double& APER_3, const double& APER_4, const string& APERTYPE, const double& BETX, const double& BETY, const double& DPX, const double& DPY, const double& DX, const double& DY, const string& KEYWORD, const double& L, const double& MUX, const double& MUY, const string& NAME, const double& PTC, const double& PXC, const double& PYC, const double& S, const double& TC, const double& XC, const double& YC, const double& K0L, const double& K0SL, const double& K1L, const double& K1SL, const double& K2L, const double& K2SL, const string& PARENT, const string& meth, const double& hgap, const double& hgap2, const double& collang, const double& pdepth, const double& pdepth2, const double& tcang, const double& nsig)
14    : Collimator(ALFX, ALFY, APER_1, APER_2, APER_3, APER_4, APERTYPE, BETX, BETY, DPX, DPY, DX, DY, KEYWORD, L, MUX, MUY, NAME, PTC, PXC, PYC, S, TC, XC, YC, K0L, K0SL, K1L, K1SL, K2L, K2SL, PARENT, meth, hgap, hgap2, collang, pdepth, pdepth2, tcang, nsig)
15{};
16
17CrystalCollimator::CrystalCollimator(Element elt, const double& tcang, const double& nsig, const string& meth)
18    : Collimator(elt, tcang, nsig, meth)
19{};
20
21CrystalCollimator::CrystalCollimator(const CrystalCollimator& obj)
22    : Collimator(obj)
23{};
24
25void CrystalCollimator::affiche()
26{
27
28    cout << "Crystal collimator: " << endl;
29
30    this->Collimator::affiche();
31};
32
33
34void CrystalCollimator::collipassCrystal(vector <Particle>& bunch, const double& betgam, const int& pas, string outputpath)
35{
36
37    //Calculate proton energy
38    double Mproton(0.93827231); //rest mass [GeV]
39
40    vector <double> betgamProton;
41    vector <double> betaProton;
42    vector <double> Eproton;
43
44    for (int j(0); j < bunch.size(); ++j) {
45        betgamProton.push_back(betgam * (1 + bunch[j].coordonnees[0][4])); //relativistic beta * relativistic gamma
46        betaProton.push_back(sqrt((betgamProton[j]*betgamProton[j]) / ((betgamProton[j]*betgamProton[j]) + 1))); //relativistic beta
47        Eproton.push_back(Mproton * betgamProton[j] / betaProton[j]); //total energy [GeV]
48    }
49
50
51    ofstream sortie;
52    string filename;
53    filename = outputpath + "/ascii_before.csv";
54
55    sortie.open(filename.c_str());
56
57    if (sortie.fail()) {
58        cerr << "Warning: problem with the file " << filename << "!" << endl;
59    } else {
60
61        int col(15);
62
63        for (int k(0); k < bunch.size(); ++k) {
64
65            sortie  << bunch[k].coordonnees[0][0] << "," << bunch[k].coordonnees[0][2] << "," << bunch[k].coordonnees[0][1] << "," << bunch[k].coordonnees[0][3] << "," << Eproton[k] << endl;
66        }
67
68        sortie.close();
69
70    }
71
72    ofstream out;
73    string file;
74    file = outputpath + "/crystal_output.out";
75
76    if (crystaloutflag == 0) {
77        out.open(file.c_str());
78        crystaloutflag = 1;
79    } else {
80        out.open(file.c_str(), ios::out | ios::app);
81    }
82
83    if (out.fail()) {
84        cerr << "Warning: problem writing in the file: " << file << "!" << endl;
85    } else {
86        out << this->NAME << endl;
87        out.close();
88    }
89
90
91    SimCrys sim(Crystal (C_orient,  IS, C_xmax, C_ymax, Cry_length, Rcurv), Partcrys (0)); //simulation of the passage through the crystal
92
93    sim.general(sim, pas, outputpath, C_rotation, C_aperture, C_offset, C_tilt, Cry_tilt);
94
95    ifstream enter;
96    string file_in;
97    file_in = outputpath + "/ascii_after.csv";
98
99    enter.open(file_in.c_str());
100
101    if (enter.fail()) {
102        cerr << "Warning: problem reading the file " << file_in << "!" << endl;
103    } else {
104
105        for (int k(0); k < bunch.size(); ++k) {
106
107            string rest;
108
109            getline(enter, rest, ',');
110            bunch[k].coordonnees[1][0] = atof(rest.c_str());
111
112            getline(enter, rest, ',');
113            bunch[k].coordonnees[1][2] = atof(rest.c_str());
114
115            getline(enter, rest, ',');
116            bunch[k].coordonnees[1][1] = atof(rest.c_str());
117
118            getline(enter, rest, ',');
119            bunch[k].coordonnees[1][3] = atof(rest.c_str());
120
121            getline(enter, rest);
122            Eproton[k] = atof(rest.c_str());
123        }
124
125        enter.close();
126    }
127
128    vector <double> gamProton;
129    vector <double> betProton;
130
131    //we return to the initial referential
132    for (int j(0); j < Eproton.size(); ++j) {
133        gamProton.push_back(Eproton[j] / Mproton);
134        betProton.push_back(sqrt(1 - (1 / (gamProton[j]*gamProton[j]))));
135        bunch[j].coordonnees[1][4] = gamProton[j] * betProton[j] / betgam - 1;
136    }
137
138};
Note: See TracBrowser for help on using the repository browser.