Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ParameterizeBField.C
Go to the documentation of this file.
1 
2 
3 #include "chebyshev_fit.C"
4 
5 //---------------------
6 // ParameterizeBField
7 //---------------------
9 {
10  gROOT->Reset();
11  TColor::CreateColorWheel();
12 
13  TFile *f = new TFile("bfield.root");
14  TTree *Bfield = (TTree*)gROOT->FindObject("Bfield");
15 
16  Parameterize(Bfield, "Bz");
17  Parameterize(Bfield, "Bx");
18 }
19 
20 //-------------------
21 // Parameterize
22 //-------------------
23 void Parameterize(TTree *Bfield, const char *Bi)
24 {
25  // Bounds and poly-order of fits in cm
26  //Double_t z_bounds[] = {-20.0, +25.0, +67.0, +100.0, +130.0, +180.0, +236.0, 0.0};
27  //Double_t z_bounds[] = {-20.0, -10.0, +5.0, +30.0, +50.0, +60.0, +80.0, +100.0, +120.0, +140.0, +160.0, +180.0, +200.0, +220.0, +240.0, 0.0};
28  //Double_t z_bounds[] = {-50.0, +5.0, +50.0, +80.0, +150.0, +200.0, +250.0, +300.0, +350.0, +400.0, +450.0, +500.0, +550.0, +610.0, 0.0};
29  //Double_t z_bounds[] = {-50.8, +63.5, +170.18, +254.0, +330.2, +457.2, +599.44, 0.0};
30  Double_t z_bounds[] = {-30.0, -10.0, +10.0, +30.0, +67.0, +100.0, +125.0, +148.0, +175.0, 205.0, +236.0, 0.0};
31  int order1 = 9; // max order of poly for Bi vs. z fit (fit to B-field)
32  int order2 = 9; // max order of poly for Pi vs. r fit (fit to parameters of above)
33  Double_t rmin_inches = 1.0;
34  Double_t rmax_inches = 26.0;
35  UInt_t Nr = (UInt_t)(26.0-1.0)+1;
36  UInt_t Nsec = 0;
37  while(z_bounds[Nsec+1]!=0.0){
38  // Convert z_bounds to inches (historic)
39  //z_bounds[Nsec]/=2.54;
40 
41  Nsec++;
42  }
43 
44 
45 
46  // Open file to write results to for later extraction
47  stringstream fname;
48  fname<<"BfieldParameters_"<<Bi<<".root";
49  TFile *fout = new TFile(fname.str().c_str(),"RECREATE");
50 
51  // Create a Postscript file that we can use to draw multiple plots to
52  // (doesn't seem to work for PDF)
53  TCanvas *c1 = new TCanvas("c1");
54  stringstream ss;
55  ss<<"BfieldParameters_"<<Bi<<".ps";
56  string gfname = ss.str();
57  c1->Print((gfname+"[").c_str());
58 
59  int colors[] = {kBlue, kRed, kGreen, kMagenta, kCyan};
60  int Ncolors = 5;
61 
62  // Create histograms to hold parameters from fits. The naming scheme is:
63  //
64  // secS_pN : holds coefficient of poly order "N" from Bi vs. z fit for section "S"
65  // as function of r. This will have one bin on the x-axis for each value
66  // of r fit.
67  //
68  // secS_ppN : holds coefficients of poly fit to the corresponding secS_pN histo.
69  // described above. This will have order2+1 bins.
70  //
71  TH1D *phist = new TH1D("phist","Parameters", 26, 0.5*2.54, 26.5*2.54);
72  phist->SetStats(0);
73  phist->SetXTitle("R (cm)");
74  phist->SetYTitle("Value of Chebyshev coefficient");
75 
76  // Create histogram to hold z boundaries
77  TH1D *z_bounds_hist = new TH1D("z_bounds_hist", "Z boundaries for sections", Nsec+1, 0.5, Nsec+1.5);
78  for(UInt_t sec=0; sec<=Nsec; sec++)z_bounds_hist->Fill(sec+1, z_bounds[sec]*2.54);
79 
80  // Create a histo to hold each parameter as a function of r for this section
81  TH1D *sec_pars[50][50]; // sec_pars[section][p_order] (not all elements will be used)
82  for(UInt_t sec=1; sec<=Nsec; sec++){
83  for(UInt_t i=0; i<=order1; i++){
84  char hname[256];
85  sprintf(hname, "sec%d_p%d", sec, i);
86  sec_pars[sec][i] = (TH1D*)phist->Clone(hname);
87 
88  stringstream title;
89  title<<"Parameter "<<i<<" for section "<<sec<<" "<<Bi;
90  sec_pars[sec][i]->SetTitle(title.str().c_str());
91  }
92  }
93 
94  // Loop over values of r, finding the parameters for fitting each
95  for(Double_t r_inches=rmin_inches; r_inches<=rmax_inches; r_inches+=1.0){
96 
97  // Define sections that will be fit and loop over them
98  TF1 *func[50]; // save function pointers for each section so we can plot later
99  for(UInt_t sec=1; sec<=Nsec; sec++){
100  if(z_bounds[sec]==0.0)break;
101 
102  Double_t r = 2.54*r_inches;
103  Double_t zmin = z_bounds[sec-1];
104  Double_t zmax = z_bounds[sec];
105 
106  // Fit Bi vs. z for this value of r
107  stringstream funcname;
108  funcname<<"sec"<<sec<<"_r"<<(UInt_t)r_inches;
109  func[sec] = FitSection(order1, Bfield, Bi, zmin, zmax, r, funcname.str().c_str());
110 
111  // Get parameters
112  for(UInt_t i=0; i<=order1; i++){
113  sec_pars[sec][i]->SetBinContent(sec_pars[sec][i]->FindBin(r) ,func[sec]->GetParameter(i+2));
114  }
115  }
116 
117  // Plot results
118  stringstream cut;
119  cut<<"(abs(r-"<<r<<")<"<<0.1<<")"; // select a single r bin
120  stringstream varexp;
121  varexp<<Bi<<":z";
122  Bfield->SetMarkerStyle(8);
123  Bfield->SetMarkerSize(0.7);
124  Bfield->Draw(varexp.str().c_str(), cut.str().c_str());
125 
126  // Overlay fit results for all sections
127  for(UInt_t sec=1; sec<=Nsec; sec++){
128  func[sec]->SetLineColor(colors[(sec-1)%Ncolors]);
129 
130  func[sec]->Draw("same");
131  }
132 
133  // Draw plot to output file
134  c1->Update();
135  c1->Print(gfname.c_str());
136  }
137 
138  // Now we want to fit the parameter histograms and save the
139  // parameters describing each.
140  TH1D *pphist = new TH1D("pphist","Parameterization of Parameters", order2+1, -0.5, (double)order2 + 0.5);
141  pphist->SetStats(0);
142  pphist->SetXTitle("Order of Chebyshev coefficient");
143  pphist->SetYTitle("Value of Chebyshev coefficient");
144 
145  // Loop over sections
146  for(UInt_t sec=1; sec<=Nsec; sec++){
147 
148  // Loop over 1st level parameters
149  for(UInt_t i=0; i<=order1; i++){
150 
151  TF1* f1 = chebyshev_Fit(sec_pars[sec][i], order2);
152 
153  char hname[256];
154  sprintf(hname, "sec%d_pp%d", sec, i);
155  TH1D *sec_ppars = (TH1D*)pphist->Clone(hname);
156  for(int j=0; j<=order2; j++)sec_ppars->SetBinContent(j, f1->GetParameter(j+2));
157 
158  // Draw plot to output file
159  f1->SetLineColor(colors[(sec-1)%Ncolors]);
160  sec_pars[sec][i]->SetMarkerStyle(8);
161  sec_pars[sec][i]->SetMarkerSize(0.7);
162 
163  sec_pars[sec][i]->Draw("P");
164  f1->Draw("same");
165  c1->Print(gfname.c_str());
166  }
167  }
168 
169  // Close out graphics file
170  c1->Clear();
171  c1->Print((gfname+"]").c_str());
172 
173  delete phist;
174  delete pphist;
175 
176  fout->Write();
177  delete fout;
178 }
179 
180 //---------------------
181 // FitSection
182 //---------------------
183 TF1* FitSection(int order, TTree *Bfield, const char *Bi, Double_t z_min_inches, Double_t z_max_inches, Double_t r, const char *fname)
184 {
185  cout<<"Fitting "<<fname<<" at r="<<r<<endl;
186 
187  // The following should be defined based on the grid spacing/location
188  // of points in the Bfield tree.
189  Double_t delta_z = 2.54; // in cm
190  Double_t delta_r = 2.54; // in cm
191 
192  // Determine proper histogram limits
193  Double_t z_min = z_min_inches*delta_z - delta_z/2.0;
194  Double_t z_max = z_max_inches*delta_z + delta_z/2.0;
195  Double_t epsilon = delta_z/1000.0; // used to prevent round-off problems
196  UInt_t NbinsZ = floor((z_max - z_min + epsilon)/delta_z);
197 
198  // Note that here we explicitly set the limits to be -1 to +1 so that the
199  // histogram conforms to the requirements of the chebyshev_FindBestFunction
200  // function.
201  TH1D *Bi_vs_znorm = new TH1D("Bi_vs_znorm", "Bi as function of modified z coordinate", NbinsZ, z_min, z_max);
202 
203  // Make varexp that properly converts from the lab z-coordinates,
204  // to the "Chebyshev compatible" coordinates where z_min=-1 and
205  // z_max=+1.
206  stringstream cut;
207  cut<<Bi<<"*(abs(r-"<<r<<")<"<<delta_r/10.0<<")"; // select a single r bin
208  Bfield->Project("Bi_vs_znorm", "z", cut.str().c_str());
209 
210  TF1 *func = chebyshev_Fit(Bi_vs_znorm, order);
211  TF1 *myfunc = (TF1*)func->Clone(fname);
212 
213  delete Bi_vs_znorm;
214 
215  return myfunc;
216 }
217 
218 
int Ncolors
Definition: root2email.cc:39
TF1 * FitSection(int order, TTree *Bfield, const char *Bi, Double_t z_min_inches, Double_t z_max_inches, Double_t r, const char *fname)
sprintf(text,"Post KinFit Cut")
int colors[]
double zmax
int Nr
Definition: bfield2root.cc:25
Double_t c1[2][NMODULES]
Definition: tw_corr.C:68
TF1 * f
Definition: FitGains.C:21
TFile f1(fnam)
void ParameterizeBField(void)
void Parameterize(TTree *Bfield, const char *Bi)
double func(double *x, double *par)
Definition: timewalk_fits.C:16
double zmin
TF1 * chebyshev_Fit(TH1D *h, UInt_t order=9)
Definition: chebyshev_fit.C:51