CGEM BOSS 6.6.5.f
BESIII Offline Software System
Loading...
Searching...
No Matches
SamplingGar.cxx
Go to the documentation of this file.
1#include "CgemDigitizerSvc/SamplingGar.h"
2
3#include "GaudiKernel/ISvcLocator.h"
4#include "GaudiKernel/Bootstrap.h"
5#include "GaudiKernel/IDataProviderSvc.h"
6#include "GaudiKernel/SmartDataPtr.h"
7#include "GaudiKernel/DataSvc.h"
8
9#include "CLHEP/Units/PhysicalConstants.h"
10
11#include "G4DigiManager.hh"
12#include "Randomize.hh"
13#include "G4ios.hh"
14
15#include "CLHEP/Units/PhysicalConstants.h"
16
17#include <iomanip>
18#include <iostream>
19#include <fstream>
20#include <cmath>
21
22#include "TRandom.h"
23#include "TMath.h"
24
25using namespace std;
26
28}
29
31}
32
33void SamplingGar::init(ICgemGeomSvc* geomSvc, double magConfig){
34 m_geomSvc = geomSvc;
35 m_magConfig = magConfig;
36
37 readSmearPar();
38
39 // initialize polya functions
40 char funname[3];
41 for(int i=0; i<3; i++){
42 sprintf(funname, "funPolya%d", i);
43 m_funPolya[i] = new TF1(funname, "[0]*pow(1+[2],1+[2])*pow(x/[1],[2])*exp(-(1+[2])*x/[1])/TMath::Gamma(1+[2])", 1, 500);
44 m_funPolya[i]->SetParameter(0, m_gain_gem[i][0]);
45 m_funPolya[i]->SetParameter(1, m_gain_gem[i][1]);
46 m_funPolya[i]->SetParameter(2, m_gain_gem[i][2]);
47 }
48}
49
50void SamplingGar::setIonElectrons(int layer, int nElectrons, std::vector<double> x, std::vector<double> y, std::vector<double> z, std::vector<double> t){
51 clear();
52 m_nIonE = nElectrons;
53 // cout << "nIonE = " << m_nIonE << endl;
54 for(int i=0; i<nElectrons; i++){
55 double r = sqrt(x[i]*x[i] + y[i]*y[i]);
56 double phi;
57 if(y[i] >= 0) phi = acos(x[i] / r);
58 else phi = CLHEP::twopi - acos(x[i] / r);
59 m_vecIonR.push_back(r);
60 m_vecIonPhi.push_back(phi);
61 m_vecIonZ.push_back(z[i]);
62 m_vecIonT.push_back(t[i]);
63
64 // cout << "check geom layer" << setw(15) << layer << setw(15) << m_geomSvc->getCgemLayer(layer)->getInnerROfGapD()
65 // << setw(15) << m_geomSvc->getCgemLayer(layer)->getOuterROfGapD()
66 // << setw(15) << m_geomSvc->getCgemLayer(layer)->getCgemFoil(0)->getInnerROfCgemFoil() << endl;
67
68 double radii_gem1 = m_geomSvc->getCgemLayer(layer)->getCgemFoil(0)->getInnerROfCgemFoil();
69 double driftD = radii_gem1 - r;
70 // cout << "driftD = " << setw(15) << driftD << setw(15) << radii_gem1 << setw(15) << r << endl;
71 samplingDrift(driftD, i);
72 }
73 // cout << "nMultiGem1 = " << m_nEgem1 << endl;
74 for(int i=0; i<m_nEgem1; i++) samplingTransfer(1, i);
75 // cout << "nMultiGem2 = " << m_nEgem2 << endl;
76 for(int i=0; i<m_nEgem2; i++) samplingTransfer(2, i);
77 // cout << "nMultiGem3 = " << m_nEgem3 << endl;
78 calMultiE(layer);
79}
80
81void SamplingGar::clear(){
82 m_nIonE = 0;
83 m_vecIonR.clear();
84 m_vecIonPhi.clear();
85 m_vecIonZ.clear();
86 m_vecIonT.clear();
87
88 m_nEgem1 = 0;
89 m_idIon.clear();
90 m_vecGem1dX.clear();
91 m_vecGem1dZ.clear();
92 m_vecGem1dT.clear();
93
94 m_nEgem2 = 0;
95 m_idGem1.clear();
96 m_vecGem2dX.clear();
97 m_vecGem2dZ.clear();
98 m_vecGem2dT.clear();
99
100 m_nEgem3 = 0;
101 m_idGem2.clear();
102 m_vecGem3dX.clear();
103 m_vecGem3dZ.clear();
104 m_vecGem3dT.clear();
105
106 m_nMulElec = 0;
107 m_eX.clear();
108 m_eY.clear();
109 m_eZ.clear();
110 m_eT.clear();
111}
112
113bool SamplingGar::readSmearPar(){
114 string filePath = getenv("CGEMDIGITIZERSVCROOT");
115 string fileName;
116 if(m_magConfig<0.05) fileName = filePath + "/dat/par_SamplingGar_0T.txt";
117 else fileName = filePath + "/dat/par_SamplingGar_1T.txt";
118 ifstream fin(fileName.c_str(), ios::in);
119 cout << "fileName: " << fileName << endl;
120
121 string strline;
122 string strpar;
123 if( ! fin.is_open() ){
124 cout << "ERROR: can not open file " << fileName << endl;
125 return false;
126 }
127 while(fin >> strpar){
128 if('#' == strpar[0]){
129 getline(fin, strline);
130 } else if("Mean_X_function" == strpar){
131 fin >> m_meanXpar[0] >> m_meanXpar[1];
132 } else if("Sigma_X_function" == strpar){
133 fin >> m_sigmaXpar[0] >> m_sigmaXpar[1] >> m_sigmaXpar[2] >> m_sigmaXpar[3];
134 } else if("Mean_Z_function" == strpar){
135 fin >> m_meanZpar;
136 } else if("Sigma_Z_function" == strpar){
137 fin >> m_sigmaZpar[0] >> m_sigmaZpar[1] >> m_sigmaZpar[2] >> m_sigmaZpar[3];
138 } else if("Mean_T_function" == strpar){
139 fin >> m_meanTpar[0] >> m_meanTpar[1];
140 } else if("Sigma_T_function" == strpar){
141 fin >> m_sigmaTpar[0] >> m_sigmaTpar[1] >> m_sigmaTpar[2] >> m_sigmaTpar[3];
142 } else if("Transparency_Gem1" == strpar){
143 fin >> m_transparency_gem1;
144 } else if("Gain_Gem1" == strpar){
145 fin >> m_gain_gem[0][0] >> m_gain_gem[0][1] >> m_gain_gem[0][2];
146
147 } else if("MeanX_Transf1" == strpar){
148 fin >> m_meanX_transf[0];
149 } else if("SigmaX_Transf1" == strpar){
150 fin >> m_sigmaX_transf[0];
151 } else if("MeanZ_Transf1" == strpar){
152 fin >> m_meanZ_transf[0];
153 } else if("SigmaZ_Transf1" == strpar){
154 fin >> m_sigmaZ_transf[0];
155 } else if("MeanT_Transf1" == strpar){
156 fin >> m_meanT_transf[0];
157 } else if("SigmaT_Transf1" == strpar){
158 fin >> m_sigmaT_transf[0];
159 } else if("Transparency_Gem2" == strpar){
160 fin >> m_transparency_gem2;
161 } else if("Gain_Gem2" == strpar){
162 fin >> m_gain_gem[1][0] >> m_gain_gem[1][1] >> m_gain_gem[1][2];
163
164 } else if("MeanX_Transf2" == strpar){
165 fin >> m_meanX_transf[1];
166 } else if("SigmaX_Transf2" == strpar){
167 fin >> m_sigmaX_transf[1];
168 } else if("MeanZ_Transf2" == strpar){
169 fin >> m_meanZ_transf[1];
170 } else if("SigmaZ_Transf2" == strpar){
171 fin >> m_sigmaZ_transf[1];
172 } else if("MeanT_Transf2" == strpar){
173 fin >> m_meanT_transf[1];
174 } else if("SigmaT_Transf2" == strpar){
175 fin >> m_sigmaT_transf[1];
176 } else if("Transparency_Gem3" == strpar){
177 fin >> m_transparency_gem3;
178 } else if("Gain_Gem3" == strpar){
179 fin >> m_gain_gem[2][0] >> m_gain_gem[2][1] >> m_gain_gem[2][2];
180
181 } else if("MeanX_Induc" == strpar){
182 fin >> m_meanX_induc;
183 } else if("SigmaX_Induc" == strpar){
184 fin >> m_sigmaX_induc;
185 } else if("MeanZ_Induc" == strpar){
186 fin >> m_meanZ_induc;
187 } else if("SigmaZ_Induc" == strpar){
188 fin >> m_sigmaZ_induc;
189 } else if("MeanT_Induc" == strpar){
190 fin >> m_meanT_induc;
191 } else if("SigmaT_Induc" == strpar){
192 fin >> m_sigmaT_induc;
193 }
194 }
195 fin.close();
196 return true;
197}
198
199bool SamplingGar::samplingDrift(double driftD, int idIon){
200 G4double frandom = G4UniformRand();
201 // cout << "frandom = " << frandom << ", trans = " << m_transparency_gem1 << endl;
202 if(frandom > m_transparency_gem1) return false;
203 int n = samplingGain(0);
204 double meanPhi = m_meanXpar[0] + m_meanXpar[1]*driftD;
205 double sigmaPhi = m_sigmaXpar[0] + m_sigmaXpar[1]*driftD + m_sigmaXpar[2]*driftD*driftD + m_sigmaXpar[3]*driftD*driftD*driftD;
206 double meanZ = 0.0;
207 double sigmaZ = m_sigmaZpar[0] + m_sigmaZpar[1]*driftD + m_sigmaZpar[2]*driftD*driftD + m_sigmaZpar[3]*driftD*driftD*driftD;
208 double meanT = m_meanTpar[0] + m_meanTpar[1]*driftD;
209 double sigmaT = m_sigmaTpar[0] + m_sigmaTpar[1]*driftD + m_sigmaTpar[2]*driftD*driftD + m_sigmaTpar[3]*driftD*driftD*driftD;
210 // cout << "debug in samplingDrift: " << setw(15) << driftD << setw(15) << meanPhi << setw(15) << sigmaPhi
211 // << setw(15) << sigmaZ << setw(15) << meanT << setw(15) << sigmaT << endl;
212 for(int i=0; i<n; i++){
213 double dphi = G4RandGauss::shoot(meanPhi, sigmaPhi);
214 double dz = G4RandGauss::shoot(meanZ, sigmaZ);
215 double dt = G4RandGauss::shoot(meanT, sigmaT);
216
217 m_idIon.push_back(idIon);
218 m_vecGem1dX.push_back(-1.0*dphi);
219 m_vecGem1dZ.push_back(dz);
220 m_vecGem1dT.push_back(dt);
221 }
222 m_nEgem1 += n;
223 return true;
224}
225
226bool SamplingGar::samplingTransfer(int region, int idLastStep){
227 double transp;
228 if(1==region) transp = m_transparency_gem2;
229 else transp = m_transparency_gem3;
230 G4double frandom = G4UniformRand();
231 if(frandom > transp) return false;
232 int n = samplingGain(region);
233
234 for(int i=0; i<n; i++){
235 double dphi = G4RandGauss::shoot(m_meanX_transf[region-1], m_sigmaX_transf[region-1]);
236 double dz = G4RandGauss::shoot(m_meanZ_transf[region-1], m_sigmaZ_transf[region-1]);
237 double dt = G4RandGauss::shoot(m_meanT_transf[region-1], m_sigmaT_transf[region-1]);
238
239 if(1==region){
240 m_idGem1.push_back(idLastStep);
241 m_vecGem2dX.push_back(-1.0*dphi);
242 m_vecGem2dZ.push_back(dz);
243 m_vecGem2dT.push_back(dt);
244 } else{
245 m_idGem2.push_back(idLastStep);
246 m_vecGem3dX.push_back(-1.0*dphi);
247 m_vecGem3dZ.push_back(dz);
248 m_vecGem3dT.push_back(dt);
249 }
250 }
251 if(1==region) m_nEgem2 += n;
252 else m_nEgem3 += n;
253 return true;
254}
255
256int SamplingGar::samplingGain(int region){
257 double xRand = m_funPolya[region]->GetRandom(1, 500);
258 int gain = (int)xRand;
259 return gain;
260}
261
262bool SamplingGar::calMultiE(int layer){
263 for(int i=0; i<m_nEgem3; i++){
264 double dphiInduc = -1.0 * G4RandGauss::shoot(m_meanX_induc, m_sigmaX_induc);
265 double dzInduc = G4RandGauss::shoot(m_meanZ_induc, m_sigmaZ_induc);
266 double dtInduc = G4RandGauss::shoot(m_meanT_induc, m_sigmaT_induc);
267
268 double dphiGem3 = m_vecGem3dX[i];
269 double dzGem3 = m_vecGem3dZ[i];
270 double dtGem3 = m_vecGem3dT[i];
271
272 int idGem2 = m_idGem2[i];
273 double dphiGem2 = m_vecGem2dX[idGem2];
274 double dzGem2 = m_vecGem2dZ[idGem2];
275 double dtGem2 = m_vecGem2dT[idGem2];
276
277 int idGem1 = m_idGem1[idGem2];
278 double dphiGem1 = m_vecGem1dX[idGem1];
279 double dzGem1 = m_vecGem1dZ[idGem1];
280 double dtGem1 = m_vecGem1dT[idGem1];
281
282 int idIon = m_idIon[idGem1];
283 double rini = m_vecIonR[idIon];
284 double phiini = m_vecIonPhi[idIon];
285 double zini = m_vecIonZ[idIon];
286 double tini = m_vecIonT[idIon];
287
288 double rNew = m_geomSvc->getCgemLayer(layer)->getInnerROfGapI();
289 double dphiTot = dphiGem1 + dphiGem2 + dphiGem3;
290 double xNew = rNew * phiini + dphiTot;
291 double phiOut; // phi angle
292 if(xNew < 0){
293 phiOut = xNew/rNew + CLHEP::twopi;
294 } else if(xNew > (rNew * CLHEP::twopi)){
295 phiOut = xNew/rNew - CLHEP::twopi;
296 } else{
297 phiOut = xNew / rNew;
298 }
299
300 m_eX.push_back(rNew * cos(phiOut));
301 m_eY.push_back(rNew * sin(phiOut));
302 m_eZ.push_back(zini + dzGem1 + dzGem2 + dzGem3);
303 m_eT.push_back(tini + dtGem1 + dtGem2 + dtGem3);
304 m_nMulElec++;
305
306 // double rNew = m_geomSvc->getCgemLayer(layer)->getInnerROfAnode();
307 // double dphiTot = dphiGem1 + dphiGem2 + dphiGem3 + dphiInduc;
308 // double xNew = rNew * phiini + dphiTot;
309 // double phiOut; // phi angle
310 // if(xNew < 0){
311 // phiOut = xNew/rNew + CLHEP::twopi;
312 // } else if(xNew > (rNew * CLHEP::twopi)){
313 // phiOut = xNew/rNew - CLHEP::twopi;
314 // } else{
315 // phiOut = xNew / rNew;
316 // }
317
318 // m_eX.push_back(rNew * cos(phiOut));
319 // m_eY.push_back(rNew * sin(phiOut));
320 // m_eZ.push_back(zini + dzGem1 + dzGem2 + dzGem3 + dzInduc);
321 // m_eT.push_back(tini + dtGem1 + dtGem2 + dtGem3 + dtInduc);
322 // m_nMulElec++;
323 }
324 return true;
325}
TGraphErrors * dt
Definition: AbsCor.cxx:72
const Int_t n
Double_t x[10]
double sin(const BesAngle a)
double cos(const BesAngle a)
virtual CgemGeoLayer * getCgemLayer(int i) const =0
void init(ICgemGeomSvc *geomSvc, double magConfig)
Definition: SamplingGar.cxx:33
void setIonElectrons(int layer, int nElectrons, std::vector< double > x, std::vector< double > y, std::vector< double > z, std::vector< double > t)
Definition: SamplingGar.cxx:50
int t()
Definition: t.c:1