Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ParameterizeBField_codegen.C
Go to the documentation of this file.
1 
2 //
3 //
4 // This macro is used to generate ASCII files containing a parameterized
5 // magnetic field map in a format suitable for use with the JANA
6 // JCalibrationFile class. It takes as input the root files
7 // BfieldParameters_Bz.root and BfieldParameters_Bx.root which themselves
8 // are produced by the ParameterizeBField.C macro using bfield.root
9 // file as input. (The bfield.root file is generated with the bfield2root
10 // utility from a full map already in the calibDB).
11 //
12 
13 //------------------------------
14 // ParameterizeBField_codegen
15 //------------------------------
16 void ParameterizeBField_codegen(const char *based_on="_1500_poisson_20090814_01")
17 {
18  gROOT->Reset();
19 
20  // Create master params file
21  stringstream fname;
22  fname<<"solenoid"<<based_on<<"_params";
23  ofstream fout_master(fname.str().c_str());
24 
25  // Write header info to file
26  time_t now = time(NULL);
27  fout_master<<"#"<<endl;
28  fout_master<<"# Solenoidal field parameterization"<<endl;
29  fout_master<<"#"<<endl;
30  fout_master<<"# This file auto-generated by ParameterizeBField_codegen.C macro."<<endl;
31  fout_master<<"# "<<ctime(&now);
32  fout_master<<"# Produced by "<<getenv("USER")<<" on "<<getenv("HOST")<<endl;
33  fout_master<<"#"<<endl;
34  fout_master<<"# input based on: "<<based_on<<endl;
35  fout_master<<"#"<<endl;
36  fout_master<<"# This is the master parameters list. The parameterization of the"<<endl;
37  fout_master<<"# field is broken up into several sections in z. Each section has"<<endl;
38  fout_master<<"# its own set of parameters kept in its own namepath."<<endl;
39  fout_master<<"#"<<endl;
40  fout_master<<"# Each line below represents a section. Sections are either representing"<<endl;
41  fout_master<<"# Bx or Bz. Given in the table are the limits over with the section"<<endl;
42  fout_master<<"# covers."<<endl;
43  fout_master<<"#"<<endl;
44  fout_master<<"#"<<endl;
45  fout_master<<"#% Bi sec zmin zmax zmid znorm rmin rmax rmid rnorm namepath"<<endl;
46 
47  // Write indivdual params files, adding entries to master file
48  WriteFile("BfieldParameters_Bz.root", "Bz", fout_master, fname);
49  WriteFile("BfieldParameters_Bx.root", "Bx", fout_master, fname);
50 
51  // Close master params file
52  fout_master.close();
53 }
54 
55 //------------------------------
56 // WriteFile
57 //------------------------------
58 void WriteFile(const char *input_fname, string element, ofstream &fout_master, stringstream &fname)
59 {
60 #define MAX_SECTIONS 100
61 
62  // Open input ROOT file
63  TFile *f = new TFile(input_fname);
64 
65  cout<<"------------------- Ignore errors in this section --------------------"<<endl;
66 
67  // Get zmin, zmax, rmin, and rmax for each section
70  TH1D *z_bounds_hist = (TH1D*)gROOT->FindObject("z_bounds_hist");
71  UInt_t Nsec = z_bounds_hist->GetNbinsX()-1;
72  for(UInt_t sec=1; sec<=Nsec; sec++){
73  zmin[sec-1] = z_bounds_hist->GetBinContent(z_bounds_hist->FindBin(sec));
74  zmax[sec-1] = z_bounds_hist->GetBinContent(z_bounds_hist->FindBin(sec+1));
75 
76  stringstream hname;
77  hname<<"sec"<<(sec)<<"_p0"; // sec1_p0, sec2_p0, ...
78  TH1D *sec_p0 = (TH1D*)gROOT->FindObject(hname.str().c_str());
79  if(!sec_p0)break;
80  rmin[sec-1] = sec_p0->GetXaxis()->GetXmin();
81  rmax[sec-1] = sec_p0->GetXaxis()->GetXmax();
82  }
83 
84  // Get order of Chebyshev polynomials used for 1st level of parameterization
85  UInt_t order1;
86  for(order1=0; order1<20; order1++){
87  stringstream hname;
88  hname<<"sec1"<<"_p"<<order1;
89  TH1D *sec1_p = (TH1D*)gROOT->FindObject(hname.str().c_str());
90  if(!sec1_p){order1--; break;}
91  }
92 
93  // Get order of Chebyshev polynomials used for 2nd level of parameterization
94  TH1D *sec1_pp0 = (TH1D*)gROOT->FindObject("sec1_pp0");
95  UInt_t order2 = sec1_pp0->GetNbinsX()-1;
96 
97  cout<<"----------------------------------------------------------------------"<<endl;
98 
99  // Information messages
100  time_t now = time(NULL);
101  cout<<endl;
102  cout<<"Writing parameters for: "<<element<<endl;
103  cout<<" sections = "<<Nsec<<endl;
104  cout<<"1st level order = "<<order1<<endl;
105  cout<<"2nd level order = "<<order2<<endl;
106  cout<<endl;
107 
108  // Parameters
109  for(UInt_t sec=1; sec<=Nsec; sec++){
110 
111  // Open output file
112  stringstream my_fname;
113  my_fname<<fname.str()<<"_"<<element<<"_sec"<<sec;
114  ofstream fout(my_fname.str().c_str());
115  cout<<"Writing parameters to "<<fname.str()<<endl;
116 
117  // Add line to master file
118  fout_master<<element;
119  fout_master<<"\t"<<sec;
120  fout_master<<"\t"<<zmin[sec-1];
121  fout_master<<"\t"<<zmax[sec-1];
122  fout_master<<"\t"<<(zmax[sec-1]+zmin[sec-1])/2.0;
123  fout_master<<"\t"<<(zmax[sec-1]-zmin[sec-1])/2.0;
124  fout_master<<"\t"<<rmin[sec-1];
125  fout_master<<"\t"<<rmax[sec-1];
126  fout_master<<"\t"<<(rmax[sec-1]+rmin[sec-1])/2.0;
127  fout_master<<"\t"<<(rmax[sec-1]-rmin[sec-1])/2.0;
128  fout_master<<"\t"<<"Magnets/Solenoid/"<<my_fname.str();
129  fout_master<<endl;
130 
131  // Write header info to file
132  fout<<"#"<<endl;
133  fout<<"# Solenoidal field parameterization"<<endl;
134  fout<<"#"<<endl;
135  fout<<"# This file auto-generated by ParameterizeBField_codegen.C macro."<<endl;
136  fout<<"# "<<ctime(&now);
137  fout<<"# Produced by "<<getenv("USER")<<" on "<<getenv("HOST")<<endl;
138  fout<<"#"<<endl;
139  fout<<"# input file: "<<f->GetName()<<endl;
140  fout<<"# section: "<<sec<<" (out of "<<Nsec<<")"<<endl;
141  fout<<"# 1st level order: "<<order1<<endl;
142  fout<<"# 2nd level order: "<<order2<<endl;
143  fout<<"#"<<endl;
144  fout<<"# In the following table, values are coefficients of Chebyshev"<<endl;
145  fout<<"# polynomials. There are 2 levels of parameterization corresponding"<<endl;
146  fout<<"# to the 2 dimensions in which the field map is parameterized (z and r)."<<endl;
147  fout<<"#"<<endl;
148  fout<<"# The first level fits the field as a function of z in lab coordinates."<<endl;
149  fout<<"# The second level parametrizes those coefficients as a function of r"<<endl;
150  fout<<"# also in lab coordinates."<<endl;
151  fout<<"#"<<endl;
152  fout<<"#"<<endl;
153  fout<<"#"<<endl;
154 
155  int chars_per_val = 14;
156  string header((order2+1)*chars_per_val, ' ');
157  for(UInt_t j=0; j<=order2; j++){
158  stringstream name;
159  name<<"p"<<j;
160  string &s = name.str();
161  header.replace((j+1)*chars_per_val-s.length()-2, s.length(), s);
162  }
163  fout<<"#"<<header<<endl;
164 
165  // Loop
166  for(UInt_t i=0; i<=order1; i++){
167  stringstream hname;
168  hname<<"sec"<<sec<<"_pp"<<i;
169  TH1D *h = (TH1D*)gROOT->FindObject(hname.str().c_str());
170 
171  string line((order2+1)*chars_per_val, ' ');
172  for(UInt_t j=0; j<=order2; j++){
173  stringstream val;
174  val<<h->GetBinContent(j);
175  string &s = val.str();
176  if(s.length()>=chars_per_val){
177  cerr<<"ERROR: too many characters in val! ("<<__FILE__<<":"<<__LINE__<<")"<<endl;
178  return -1;
179  }
180  line.replace((j+1)*chars_per_val-s.length(), s.length(), s);
181  }
182  fout<<line<<endl;
183  }
184 
185  fout.close();
186  }
187 }
188 
189 
double rmax
double zmax
double rmin
TF1 * f
Definition: FitGains.C:21
void WriteFile(const char *input_fname, string element, ofstream &fout_master, stringstream &fname)
void ParameterizeBField_codegen(const char *based_on="_1500_poisson_20090814_01")
double zmin
#define MAX_SECTIONS