Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
mkMaterialMap.cc
Go to the documentation of this file.
1 // Author: David Lawrence June 19, 2009
2 //
3 //
4 // mkMaterialMap.cc
5 //
6 #include <fstream>
7 #include <sstream>
8 
9 #include "DANA/DApplication.h"
10 using namespace std;
11 
12 #include <DVector3.h>
13 #include <HDGEOMETRY/DRootGeom.h>
14 
15 #include <TROOT.h>
16 #include <TFile.h>
17 #include <TH2D.h>
18 
19 void ParseCommandLineArguments(int narg, char *argv[], JApplication &app);
20 void Usage(JApplication &app);
21 
22 
23 class Material{
24 public:
25  double A;
26  double Z;
27  double Density;
28  double RadLen;
29  double rhoZ_overA; // density*Z/A
30  double rhoZ_overA_logI; // density*Z/A * log(mean excitation energy)
31  double chi2c_factor;
32  double chi2a_factor;
33  double chi2a_corr;
34 };
35 
36 
37 // Table dimensions
38 int Nr = 500;
39 int Nz = 1500;
40 double rmin = 0.0;
41 double rmax = 9.75;
42 double zmin = 15.0;
43 double zmax = 100.0;
44 int n_r = 3;
45 int n_z = 3;
46 int n_phi = 60;
47 
48 //-----------
49 // mkstr
50 //-----------
51 template<class T>
52 string mkstr(const T &val, unsigned int width)
53 {
54  stringstream ssval;
55  ssval<<val;
56  string s = ssval.str();
57  stringstream ss;
58  int Nspaces = (int)width - (int)s.size();
59  if(Nspaces>0)ss<<string(Nspaces, ' ');
60  ss<<s;
61 
62  return ss.str();
63 }
64 
65 
66 //-----------
67 // main
68 //-----------
69 int main(int narg, char *argv[])
70 {
71  DApplication *dapp = new DApplication(narg, argv);
72 
73  ParseCommandLineArguments(narg, argv, *dapp);
74 
75 
76  DRootGeom *g = new DRootGeom(dapp);
77 
78  // Fill average materials table
79  cout<<"Filling material table ..."; cout.flush();
80 
81  double r0 = rmin;
82  double z0 = zmin;
83  double dr = (rmax-rmin)/(double)(Nr);
84  double dz = (zmax-zmin)/(double)(Nz);
85  Material **MatTable = new Material*[Nr];
86  Material *buff = new Material[Nr*Nz];
87  for(int ir=0; ir<Nr; ir++){
88  double r = r0 + dr/2.0 + (double)ir*dr;
89  MatTable[ir]=&buff[ir*Nz];
90  for(int iz=0; iz<Nz; iz++){
91  double z = z0 + dz/2.0 + (double)iz*dz;
92 
93  // Loop over points in phi, r, and z and add up material
94  double d_r = dr/(double)n_r;
95  double d_z = dz/(double)n_z;
96  double d_phi = 2.0*M_PI/(double)n_phi;
97  Material avg_mat={0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
98  for(int i_r=0; i_r<n_r; i_r++){
99  double my_r = r - dr/2.0 + (double)i_r*d_r;
100  for(int i_z=0; i_z<n_z; i_z++){
101  double my_z = z - dz/2.0 + (double)i_z*d_z;
102  for(int i_phi=0; i_phi<n_phi; i_phi++){
103  double my_phi = (double)i_phi*d_phi;
104 
105  DVector3 pos(my_r*cos(my_phi), my_r*sin(my_phi), my_z);
106  double A, Z, density, radlen, rhoZ_overA, rhoZ_overA_logI;
107  jerror_t err = g->FindMatLL(pos, density, A, Z, radlen);
108  if(err==NOERROR){
109  rhoZ_overA = density*Z/A;
110  double I = (Z*12.0 + 7.0)*1.0E-9; // From Leo 2nd ed. pg 25.
111  rhoZ_overA_logI = rhoZ_overA*log(I);
112  }else{
113  A = Z = density = radlen = rhoZ_overA = rhoZ_overA_logI = 0.0;
114  }
115 
116  avg_mat.A += A;
117  avg_mat.Z += Z;
118  avg_mat.Density += density;
119  avg_mat.RadLen += 1.0/radlen; // use this to keep proper sum (will be flipped and normalized below)
120  avg_mat.rhoZ_overA += rhoZ_overA;
121  avg_mat.rhoZ_overA_logI += rhoZ_overA_logI;
122  avg_mat.chi2c_factor += 0.157*rhoZ_overA*(Z+1);
123  avg_mat.chi2a_factor += 2.007e-5*cbrt(Z*Z);
124  avg_mat.chi2a_corr += 3.34*Z*Z*5.32513e-05;
125  }
126  }
127  }
128 
129  // Divide by number of points to get averages
130  double N = (double)(n_r*n_z*n_phi);
131  avg_mat.A /= N;
132  avg_mat.Z /= N;
133  avg_mat.RadLen = N/avg_mat.RadLen; // see eq. 27.23 pg 273 of 2008 PDG (note: we implicitly converted to g cm^-2 and back)
134  avg_mat.Density /= N;
135  avg_mat.rhoZ_overA /= N;
136  avg_mat.rhoZ_overA_logI /= N;
137  avg_mat.chi2c_factor/=N;
138  avg_mat.chi2a_factor/=N;
139  avg_mat.chi2a_corr/=N;
140 
141  if(!finite(avg_mat.RadLen))avg_mat.RadLen = 0.0;
142 
143  MatTable[ir][iz] = avg_mat;
144  }
145  cout<<"\r Filling Material table ... "<<100.0*(double)ir/(double)Nr<<"% ";cout.flush();
146  }
147  cout <<"Done"<<endl;
148 
149  // Column names
150  vector<string> cols;
151  cols.push_back("r");
152  cols.push_back("z");
153  cols.push_back("A");
154  cols.push_back("Z");
155  cols.push_back("density");
156  cols.push_back("radlen");
157  cols.push_back("rhoZ_overA");
158  cols.push_back("rhoZ_overA_logI");
159  cols.push_back("chi2c_factor");
160  cols.push_back("chi2a_factor");
161  cols.push_back("chi2a_corr");
162 
163  // Find max width of each column so we can print these in file with aligned columns
164  vector<unsigned int> max_width;
165  for(unsigned int i=0; i<cols.size(); i++)max_width.push_back(cols[i].size()+2);
166  for(int ir=0; ir<Nr; ir++){
167  double r = r0 + dr/2.0 + (double)ir*dr;
168  for(int iz=0; iz<Nz; iz++){
169  double z = z0 + dz/2.0 + (double)iz*dz;
170 
171  Material &mat = MatTable[ir][iz];
172  vector<string> strs;
173  stringstream ss;
174  ss.str(""); ss<<r; strs.push_back(ss.str());
175  ss.str(""); ss<<z; strs.push_back(ss.str());
176  ss.str(""); ss<<mat.A; strs.push_back(ss.str());
177  ss.str(""); ss<<mat.Z; strs.push_back(ss.str());
178  ss.str(""); ss<<mat.Density; strs.push_back(ss.str());
179  ss.str(""); ss<<mat.RadLen; strs.push_back(ss.str());
180  ss.str(""); ss<<mat.rhoZ_overA; strs.push_back(ss.str());
181  ss.str(""); ss<<mat.rhoZ_overA_logI; strs.push_back(ss.str());
182  ss.str(""); ss<<mat.chi2c_factor; strs.push_back(ss.str());
183  ss.str(""); ss<<mat.chi2a_factor; strs.push_back(ss.str());
184  ss.str(""); ss<<mat.chi2a_corr; strs.push_back(ss.str());
185 
186  for(unsigned int i=0; i<strs.size(); i++){
187  if(strs[i].size()+2 > max_width[i])max_width[i] = strs[i].size()+2;
188  }
189  }
190  }
191 
192  // Make colheader string as a comment that can be placed at the top and then periodically
193  // in the file to make it easier to read
194  stringstream colheader;
195  colheader<<"#";
196  for(unsigned int i=0; i<cols.size(); i++)colheader<<mkstr(cols[i], max_width[i] + (i==0 ? -1:0));
197 
198 
199  // Write table to file
200  cout<<"Writing material table to file ..."<<endl;
201  ofstream of("material_map");
202  time_t t = time(NULL);
203  of<<"#"<<endl;
204  of<<"# Material map generated with src/programs/Utilities/mkMaterialMap"<<endl;
205  of<<"# generated: "<<ctime(&t);
206  of<<"#"<<endl;
207  of<<"# Generated with the following parameters:"<<endl;
208  of<<"# Nr = "<<Nr<<endl;
209  of<<"# Nz = "<<Nz<<endl;
210  of<<"# rmin = "<<rmin<<endl;
211  of<<"# rmax = "<<rmax<<endl;
212  of<<"# zmin = "<<zmin<<endl;
213  of<<"# zmax = "<<zmax<<endl;
214  of<<"#"<<endl;
215  of<<"# sampling points per cell:"<<endl;
216  of<<"# n_r = "<<n_r<<endl;
217  of<<"# n_z = "<<n_z<<endl;
218  of<<"# n_phi = "<<n_phi<<endl;
219  of<<"#"<<endl;
220  //of<<colheader.str()<<endl;
221 
222  // Loop over all entries in table
223  unsigned int entries_written = 0;
224  for(int ir=0; ir<Nr; ir++){
225  double r = r0 + dr/2.0 + (double)ir*dr;
226  for(int iz=0; iz<Nz; iz++){
227  double z = z0 + dz/2.0 + (double)iz*dz;
228 
229  Material &mat = MatTable[ir][iz];
230 
231  if(entries_written%50 == 0)of<<colheader.str()<<endl;
232  if(entries_written==0) of <<"#% 00 01 02 03 04 05 06 07 08 09 10" <<endl;
233 
234  of<<mkstr(r, max_width[0]);
235  of<<mkstr(z, max_width[1]);
236  of<<mkstr(mat.A, max_width[2]);
237  of<<mkstr(mat.Z, max_width[3]);
238  of<<mkstr(mat.Density, max_width[4]);
239  of<<mkstr(mat.RadLen, max_width[5]);
240  of<<mkstr(mat.rhoZ_overA, max_width[6]);
241  of<<mkstr(mat.rhoZ_overA_logI, max_width[7]);
242  of<<mkstr(mat.chi2c_factor, max_width[8]);
243  of<<mkstr(mat.chi2a_factor, max_width[9]);
244  of<<mkstr(mat.chi2a_corr, max_width[10]);
245 
246  of<<endl;
247 
248  entries_written++;
249  //of<<r<<"\t"<<z<<"\t"<<mat.A<<"\t"<<mat.Z<<"\t"<<mat.Density<<"\t"<<mat.RadLen<<"\t"<<mat.rhoZ_overA<<"\t"<<mat.rhoZ_overA_logI<<endl;
250  }
251  }
252  of.close();
253 
254  return 0;
255 }
256 
257 
258 //-----------
259 // ParseCommandLineArguments
260 //-----------
261 void ParseCommandLineArguments(int narg, char *argv[], JApplication &app)
262 {
263 
264  for(int i=1; i<narg; i++){
265  string arg = argv[i];
266  string arg2 = ((i+1)<narg) ? argv[i+1]:"";
267  if(arg=="-h"){
268  Usage(app);
269  exit(0);
270  }
271  else if(arg=="-Nr" ){ Nr = atoi(arg2.c_str()); i++;}
272  else if(arg=="-Nz" ){ Nz = atoi(arg2.c_str()); i++;}
273  else if(arg=="-rmin" ){ rmin = atof(arg2.c_str()); i++;}
274  else if(arg=="-rmax" ){ rmax = atof(arg2.c_str()); i++;}
275  else if(arg=="-zmin" ){ zmin = atof(arg2.c_str()); i++;}
276  else if(arg=="-zmax" ){ zmax = atof(arg2.c_str()); i++;}
277  else if(arg=="-n_r" ){ n_r = atoi(arg2.c_str()); i++;}
278  else if(arg=="-n_z" ){ n_z = atoi(arg2.c_str()); i++;}
279  else if(arg=="-n_phi"){n_phi = atoi(arg2.c_str()); i++;}
280  else{
281  cout<<"Unknown argument \""<<arg<<"\""<<endl;
282  exit(-1);
283  }
284  if(i>=narg){
285  cout<<"option \""<<arg<<"\" requires a value!"<<endl;
286  exit(-2);
287  }
288  }
289 }
290 
291 //-----------
292 // Usage
293 //-----------
294 void Usage(JApplication &app)
295 {
296  cout<<endl;
297  cout<<"Usage:"<<endl;
298  cout<<" mkMaterialMap [options] source1 source2 source3 ..."<<endl;
299  cout<<endl;
300  cout<<" -Nr # Set number of grid points in r (def:"<<Nr<<")"<<endl;
301  cout<<" -Nz # Set number of grid points in z (def:"<<Nz<<")"<<endl;
302  cout<<" -rmin # Set lower boundary of map in r (def:"<<rmin<<")"<<endl;
303  cout<<" -rmax # Set upper boundary of map in r (def:"<<rmax<<")"<<endl;
304  cout<<" -zmin # Set lower boundary of map in z (def:"<<zmin<<")"<<endl;
305  cout<<" -zmax # Set upper boundary of map in z (def:"<<zmax<<")"<<endl;
306  cout<<" -n_r # Set number of sampling points in r (def:"<<n_r<<")"<<endl;
307  cout<<" -n_z # Set number of sampling points in z (def:"<<n_r<<")"<<endl;
308  cout<<" -n_phi # Set number of sampling points in phi (def:"<<n_phi<<")"<<endl;
309  cout<<endl;
310  app.Usage();
311 
312  exit(0);
313 }
314 
double chi2c_factor
DApplication * dapp
double rmax
double Density
string mkstr(const T &val, unsigned int width)
TVector3 DVector3
Definition: DVector3.h:14
int n_z
char string[256]
double rhoZ_overA_logI
double RadLen
double A
double zmax
int Nr
Definition: bfield2root.cc:25
double chi2a_corr
double rmin
jerror_t FindMatLL(DVector3 pos, double &rhoZ_overA, double &rhoZ_overA_logI, double &RadLen) const
Definition: DRootGeom.cc:402
int Nz
Definition: bfield2root.cc:27
double rhoZ_overA
int n_r
void ParseCommandLineArguments(int &narg, char *argv[])
Definition: hd_dump.cc:124
double Z
double sin(double)
int n_phi
double chi2a_factor
double zmin
void Usage(JApplication &app)
Definition: hd_ana.cc:33
#define I(x, y, z)
int main(int argc, char *argv[])
Definition: gendoc.cc:6