Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
sChargeMap.cxx
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file sChargeMap.cxx
1 #include <iostream>
2 #include <fstream>
3 #include "sChargeMap.h"
4 #include "TMath.h"
5 #include "TH2D.h"
6 #include "TH3D.h"
7 #include "TAxis.h"
8 #include "TCanvas.h"
9 
10 //=================
11 sChargeMap::sChargeMap(int nr, float rmin, float rmax, int np, float pmin, float pmax, int nz, float hz, float ev, float iv) :
12  fI(NULL),
13  fE(NULL),
14  fEcmperus(ev),
15  fIcmperms(iv)
16 {
17  fI = new TH3D("fI","fI;r;phi;z",nr,rmin,rmax,np,pmin,pmax,nz,-hz,hz);
18  fE = new TH3D("fE","fE;r;phi;z",nr,rmin,rmax,np,pmin,pmax,nz,-hz,hz);
19 }
20 //=================
21 void sChargeMap::ScreenShot(char *name, char *ext, int n) {
22  TCanvas *main = new TCanvas();
23  main->Divide(2,2);
24  TH2D *tmp = (TH2D*) fI->Project3D("xz");
25  tmp->SetTitle(Form("IonMap (ev==%d)",n));
26  main->cd(1)->SetLogz(1); tmp->Draw("colz");
27  TH2D *tmp2 = (TH2D*) fE->Project3D("xz");
28  tmp2->SetTitle(Form("ElectronMap (ev==%d)",n));
29  main->cd(2)->SetLogz(1); tmp2->Draw("colz");
30  TH1D *tmp1 = fI->ProjectionZ("IonMapZ");
31  tmp1->SetTitle(Form("IonMapZ (ev==%d)",n));
32  tmp1->Rebin(10);
33  main->cd(3); tmp1->Draw("hist");
34  TH1D *tmp12 = fE->ProjectionZ("ElectronMapZ");
35  tmp12->SetTitle(Form("ElectronMapZ (ev==%d)",n));
36  tmp12->Rebin(10);
37  main->cd(4); tmp12->Draw("hist");
38  main->SaveAs( Form("%s.%s",name,ext),ext);
39  delete tmp;
40  delete tmp1;
41  delete tmp2;
42  delete tmp12;
43  delete main;
44 }
45 //=================
47  fI->SaveAs( name );
48 }
49 //=================
50 void sChargeMap::SaveRho(char *name,int nrad,int nphi,int nhz) {
51  int nr = fI->GetXaxis()->GetNbins();
52  int np = fI->GetYaxis()->GetNbins();
53  int nz = fI->GetZaxis()->GetNbins();
54  nrad = TMath::Max( nr, nrad );
55  nphi = TMath::Max( np, nphi );
56  nhz = TMath::Max( nz, nhz );
57  TH3F *rho = new TH3F("rho","ChargeDensity [fC/cm^3];Radial [cm];Azimuthal [rad];Longitudinal [cm]",
58  nrad, fI->GetXaxis()->GetBinLowEdge(1),fI->GetXaxis()->GetBinLowEdge( nr+1 ),
59  nphi, 0,TMath::TwoPi(),
60  nz, fI->GetZaxis()->GetBinLowEdge(1),fI->GetZaxis()->GetBinLowEdge( nz+1 ));
61  for(int i=0; i!=nrad; ++i) {
62  float rmin = rho->GetXaxis()->GetBinLowEdge(i+1);
63  float rmax = rho->GetXaxis()->GetBinLowEdge(i+2);
64  int brmin = fI->GetXaxis()->FindBin( rmin+1e-6 );
65  int brmax = fI->GetXaxis()->FindBin( rmax-1e-6 );
66  for(int j=0; j!=nphi; ++j) {
67  for(int k=0; k!=nhz; ++k) {
68  float zmin = rho->GetZaxis()->GetBinLowEdge(k+1);
69  float zmax = rho->GetZaxis()->GetBinLowEdge(k+2);
70  int bzmin = fI->GetZaxis()->FindBin( zmin+1e-6 );
71  int bzmax = fI->GetZaxis()->FindBin( zmax-1e-6 );
72  float n = 0;
73  for(int ii=brmin; ii!=brmax+1; ++ii)
74  for(int jj=1; jj!=np+1; ++jj)
75  for(int kk=bzmin; kk!=bzmax+1; ++kk)
76  n += fI->GetBinContent( ii, jj, kk );
77  float dv = TMath::Power(0.5*(rmax+rmin),2)*(rmax-rmin)*(zmax-zmin)*TMath::TwoPi()*(1./nphi);
78  float drho = n / (dv*10000) * 1.602; // [fC/cm^3]
79  //std::cout << " n " << n << " || dv " << dv << std::endl;
80  rho->SetBinContent( i+1, j+1, k+1, drho );
81  }
82  }
83  }
84  rho->SaveAs( name );
85 }
86 //=================
87 void sChargeMap::Fill(float r, float p, float z, float w) {
88  int vd1 = fI->GetZaxis()->FindBin(double(0));
89  int vd2 = fI->GetZaxis()->FindBin(z);
90  if(vd1==vd2) return;
91  if(w<0) fE->Fill(r,p,z,TMath::Abs(w));
92  else fI->Fill(r,p,z,w);
93 }
94 //=================
96  int NZ = fI->GetZaxis()->GetNbins();
97  //std::cout << " Ion Deltacm " << fIcmperms*ms << std::endl;
98  //std::cout << " Ion bin width " << fI->GetZaxis()->GetBinWidth(1) << std::endl;
99  int fIDis = fIcmperms*ms / fI->GetZaxis()->GetBinWidth(1);
100  //std::cout << " DeltaBin " << fIDis << std::endl;
101  if(fIDis<1) {
102  printf("Improve grid!!!!\n");
103  fIDis=1;
104  }
105  // IONS: the easy part. Ions propagate to Anode plate (z==0) and disappear.
106  // what i will do is to displace the bincontents according to the ion velocity
107  // whenever the ions pass the Anode plate they will go to either under or overflow.
108  int vd = fI->GetZaxis()->FindBin(double(0)); // anode
109  // left-side
110  for(int nz=vd-1; nz!=0; --nz) {
111  int newz = nz+fIDis;
112  if(newz>=vd) newz = 0; // to underflow
113  for(int nr=0; nr!=fI->GetXaxis()->GetNbins(); ++nr)
114  for(int np=0; np!=fI->GetYaxis()->GetNbins(); ++np) {
115  float nc = fI->GetBinContent(nr+1,np+1,newz) + fI->GetBinContent(nr+1,np+1,nz);
116  fI->SetBinContent(nr+1,np+1,nz, 0.0);
117  fI->SetBinContent(nr+1,np+1,newz, nc );
118  }
119  }
120  // right-side
121  for(int nz=vd+1; nz!=NZ+1; ++nz) {
122  int newz = nz-fIDis;
123  if(newz<=vd) newz = NZ + 1; // to overflow
124  for(int nr=0; nr!=fI->GetXaxis()->GetNbins(); ++nr)
125  for(int np=0; np!=fI->GetYaxis()->GetNbins(); ++np) {
126  float nc = fI->GetBinContent(nr+1,np+1,newz) + fI->GetBinContent(nr+1,np+1,nz);
127  fI->SetBinContent(nr+1,np+1,nz,0.0);
128  fI->SetBinContent(nr+1,np+1,newz, nc );
129  }
130  }
131  //float fBF = 0.01; // percentage
132  float fBF = 1; // percentage
133  // ELECTRONS: though. Electrons will propagate according to the End plates (|z|==hz) and produce ions (backflow).
134  // what i will do is to compute if the time passed is enough to reach the endplates in one go
135  // if so, then the remaining is used to compute the position of ions backflowing and inject ions accordingly
136  // if not, then the electrons are displaced normally
137 
138  // left-side
139  for(int nz=1; nz!=vd; ++nz) {
140  float dz = fE->GetZaxis()->GetBinCenter(nz) - fE->GetZaxis()->GetBinLowEdge(1);
141  float t2ep = dz / fEcmperus;
142  float msres = ms - t2ep/1000;
143  //std::cout << " Electron msres " << msres << std::endl;
144  if(msres>0) {
145  // make backflow ions appear
146  int newz = 1+fIcmperms*msres / fI->GetZaxis()->GetBinWidth(1);
147  //std::cout << " Ion where in bin " << newz << std::endl;
148  if(newz>=vd) newz = 0; // to overflow
149  for(int nr=0; nr!=fI->GetXaxis()->GetNbins(); ++nr)
150  for(int np=0; np!=fI->GetYaxis()->GetNbins(); ++np) {
151  float nc = fE->GetBinContent(nr+1,np+1,nz);
152  if(nc>0) {
153  nc = fI->GetBinContent(nr+1,np+1,newz) + fBF*fE->GetBinContent(nr+1,np+1,nz);
154  //std::cout << " BACKFLOW: from " << nz << " to " << newz << " w=" << fE->GetBinContent(nr+1,np+1,nz) << " ==> " << nc << std::endl;
155  fE->SetBinContent(nr+1,np+1,nz,0);
156  fI->SetBinContent(nr+1,np+1,newz, nc );
157  }
158  }
159  } else {
160  // move electrons
161  std::cout << " Electron Deltacm " << fEcmperus*ms*1000 << std::endl;
162  std::cout << " Electron bin width " << fE->GetZaxis()->GetBinWidth(1) << std::endl;
163  float fEDis = fEcmperus*ms*1000 / fE->GetZaxis()->GetBinWidth(1);
164  std::cout << " DeltaBin " << fEDis << std::endl;
165  int newz = nz-fEDis;
166  if(newz<1) newz = 0; // to underflow
167  for(int nr=0; nr!=fI->GetXaxis()->GetNbins(); ++nr)
168  for(int np=0; np!=fI->GetYaxis()->GetNbins(); ++np) {
169  float nc = fE->GetBinContent(nr+1,np+1,newz) + fE->GetBinContent(nr+1,np+1,nz);
170  fE->SetBinContent(nr+1,np+1,nz,0);
171  fE->SetBinContent(nr+1,np+1,newz, nc );
172  }
173  }
174  }
175  // right-side
176  for(int nz=vd+1; nz!=NZ+1; ++nz) {
177  float dz = fE->GetZaxis()->GetBinLowEdge(NZ+1) - fE->GetZaxis()->GetBinCenter(nz);
178  float t2ep = dz / fEcmperus;
179  float msres = ms - t2ep/1000;
180  //std::cout << " Electron msres " << msres << std::endl;
181  if(msres>0) {
182  // make backflow ions appear
183  int newz = NZ - fIcmperms*msres / fI->GetZaxis()->GetBinWidth(1);
184  //std::cout << " Ion where in bin " << newz << std::endl;
185  if(newz<=vd) newz = NZ+1; // to overflow
186  for(int nr=0; nr!=fI->GetXaxis()->GetNbins(); ++nr)
187  for(int np=0; np!=fI->GetYaxis()->GetNbins(); ++np) {
188  float nc = fE->GetBinContent(nr+1,np+1,nz);
189  if(nc>0) {
190  nc = fI->GetBinContent(nr+1,np+1,newz) + fBF*fE->GetBinContent(nr+1,np+1,nz);
191  //std::cout << " BACKFLOW: from " << nz << " to " << newz << " w=" << fE->GetBinContent(nr+1,np+1,nz) << " ==> " << nc << std::endl;
192  fE->SetBinContent(nr+1,np+1,nz,0);
193  //fI->SetBinContent(nr+1,np+1,newz, nc );
194  }
195  }
196  } else {
197  // move electrons
198  std::cout << " Electron Deltacm " << fEcmperus*ms*1000 << std::endl;
199  std::cout << " Electron bin width " << fE->GetZaxis()->GetBinWidth(1) << std::endl;
200  float fEDis = fEcmperus*ms*1000 / fE->GetZaxis()->GetBinWidth(1);
201  std::cout << " DeltaBin " << fEDis << std::endl;
202  int newz = nz+fEDis;
203  if(newz>NZ) newz = NZ+1; // to underflow
204  for(int nr=0; nr!=fI->GetXaxis()->GetNbins(); ++nr)
205  for(int np=0; np!=fI->GetYaxis()->GetNbins(); ++np) {
206  float nc = fE->GetBinContent(nr+1,np+1,newz) + fE->GetBinContent(nr+1,np+1,nz);
207  fE->SetBinContent(nr+1,np+1,nz,0);
208  fE->SetBinContent(nr+1,np+1,newz, nc );
209  }
210  }
211  }
212 }