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

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

Added comments...

File size: 5.2 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, long int& nhit, string outputpath)
35{
36//pas: number of turns
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            //simulate the particles that are not lost on the previous aperture
65            if(bunch[k].inabs !=0){   
66            sortie  << bunch[k].coordonnees[0][0] << "," << bunch[k].coordonnees[0][2] << "," << bunch[k].coordonnees[0][1] << "," << bunch[k].coordonnees[0][3] << "," << Eproton[k] << endl;
67            }
68        }
69
70        sortie.close();
71
72    }
73
74    ofstream out;
75    string file;
76    file = outputpath + "/crystal_output.out";
77
78    if (crystaloutflag == 0) {
79        out.open(file.c_str());
80        crystaloutflag = 1;
81    } else {
82        out.open(file.c_str(), ios::out | ios::app);
83    }
84
85    if (out.fail()) {
86        cerr << "Warning: problem writing in the file: " << file << "!" << endl;
87    } else {
88        out << this->NAME << endl;
89        out.close();
90    }
91
92
93    SimCrys sim(Crystal (C_orient,  IS, C_xmax, C_ymax, Cry_length, Rcurv), Partcrys (0)); //simulation of the passage through the crystal
94
95   // sim.general(sim, pas, outputpath, NAME, emitx0, emity0,enum, C_rotation, C_aperture, C_offset, C_tilt, Cry_tilt);
96   
97    sim.general(sim, pas, nhit, outputpath, Mirror, C_rotation, C_aperture, C_offset, C_tilt, Cry_tilt);
98
99   
100    //assign the new bunch coordinates after the crystal
101    for (int k(0); k < bunch.size(); ++k) {
102      bunch[k].inabs =0 ;
103    }
104   
105   
106    ifstream enter;
107    string file_in;
108    file_in = outputpath + "/ascii_after.csv";
109
110    enter.open(file_in.c_str());
111
112    if (enter.fail()) {
113        cerr << "Warning: problem reading the file " << file_in << "!" << endl;
114    } else {
115
116        for (int k(0); k < bunch.size()-nhit; ++k) {
117
118            string rest;
119
120            getline(enter, rest, ',');
121            bunch[k].coordonnees[1][0] = atof(rest.c_str());
122
123            getline(enter, rest, ',');
124            bunch[k].coordonnees[1][2] = atof(rest.c_str());
125
126            getline(enter, rest, ',');
127            bunch[k].coordonnees[1][1] = atof(rest.c_str());
128
129            getline(enter, rest, ',');
130            bunch[k].coordonnees[1][3] = atof(rest.c_str());
131           
132            //particle is not lost (interact) with the crystal
133            bunch[k].inabs = 1; 
134           
135            getline(enter, rest);
136            Eproton[k] = atof(rest.c_str());
137         
138        }
139
140        enter.close();
141    }
142
143   
144    vector <double> gamProton;
145    vector <double> betProton;
146
147    //we return to the initial referential
148    for (int j(0); j < Eproton.size(); ++j) {
149        gamProton.push_back(Eproton[j] / Mproton);
150        betProton.push_back(sqrt(1 - (1 / (gamProton[j]*gamProton[j]))));
151        bunch[j].coordonnees[1][4] = gamProton[j] * betProton[j] / betgam - 1;
152    }
153
154};
Note: See TracBrowser for help on using the repository browser.