GamR  0.0.0
GammaROOT
Loading...
Searching...
No Matches
AngCorr.cc
Go to the documentation of this file.
1#include <TLegend.h>
2
3#include <spect/Cut.hh>
4
5#include "AngCorr.hh"
6
7namespace GamR {
8 namespace AngDist {
17 double AngCorr::Ak(double k, double delta1, double delta2)
18 {
19 double first = 0;
20 double second = 0;
21
22 int mult1 = 0;
23 int mult2 = 0;
24 for (int i=abs(spin1-spin2); i<=abs(spin1+spin2); ++i)
25 {
26 if (mult1 == 0) {
27 mult1 = i;
28 }
29 else {
30 mult2 = i;
31 break;
32 }
33 }
34 if (mult2 == 0)
35 {
36 first = GamR::AngDist::Fk(k, spin1, (double)mult1, (double)mult1, spin2);
37 }
38 else
39 {
40 first = GamR::AngDist::Ak(k, spin2, (double)mult1, (double)mult2, spin1, -delta1);
41 }
42
43 mult1 = 0;
44 mult2 = 0;
45 for (int i=abs(spin2-spin3); i<=abs(spin2+spin3); ++i)
46 {
47 if (mult1 == 0) {
48 mult1 = i;
49 }
50 else {
51 mult2 = i;
52 break;
53 }
54 }
55
56 if (mult2 == 0)
57 {
58 second = GamR::AngDist::Fk(k, spin3, (double)mult1, (double)mult1, spin2);
59 }
60 else
61 {
62 second = GamR::AngDist::Ak(k, spin2, (double)mult1, (double)mult2, spin3, delta2);
63 }
64
65 return first*second;
66 }
67
68 int AngCorr::Mixed(double s1, double s2)
69 {
70 int mult1 = 0;
71 int mult2 = 0;
72 for (int i=abs(s1-s2); i<=abs(s1+s2); ++i)
73 {
74 if (mult1 == 0) {
75 mult1 = i;
76 }
77 else {
78 mult2 = i;
79 break;
80 }
81 }
82 if (mult2==0 || mult2>2) return 0;
83 else return 1;
84 }
85
86 int AngCorr::Mixed(int i)
87 {
88 if (i==1) return Mixed1();
89 else if (i==2) return Mixed2();
90 else {
91 std::cout << "bad argument! " << i << std::endl;
92 return -1;
93 }
94 }
95
96 void AngCorrFit::SetData(std::vector<double> angs, std::vector<TH2D*> hists, GamR::Nucleus::Transition peak1, GamR::Nucleus::Transition peak2, TH1D *angNorm)
97 {
98 fColors = {kBlack, kBlue, kRed, kGreen, kMagenta};
99 for (int i=0; i<hists.size(); ++i) {
100 TH1D *hist = GamR::Spect::BackgroundSubtractY(hists[i], peak1, "temp");
101 double peak = peak2.Apply(hist);
102 if (peak < 0) { std::cout << angs[i] << " " << peak << std::endl; }
103 double error = peak2.ApplyError(hist);
104 fAngles.push_back(angs[i]);
105 double norm = angNorm->GetBinContent(angNorm->FindBin(angs[i]));
106 fIntensities.push_back(peak/norm);
107 fErrors.push_back(error/norm);
108 }
109 fixDelta.push_back(0);
110 fixDelta.push_back(0);
111 fDel.push_back(0);
112 fDel.push_back(0);
113 }
114
115 AngCorrFit::AngCorrFit(std::vector<double> angs, std::vector<TH2D*> hists, GamR::Nucleus::Transition peak1, GamR::Nucleus::Transition peak2, TH1D *angNorm)
116 {
117 SetData(angs, hists, peak1, peak2, angNorm);
118 std::vector<double> Qks = {1.0, 1.0, 1.0, 1.0, 1.0};
120 }
121
122 AngCorrFit::AngCorrFit(std::vector<double> angs, std::vector<TH2D*> hists, GamR::Nucleus::Transition peak1, GamR::Nucleus::Transition peak2, TH1D *angNorm, const char *name, const char *title) : TNamed(name, title)
123 {
124 SetData(angs, hists, peak1, peak2, angNorm);
125 std::vector<double> Qks = {1.0, 1.0, 1.0, 1.0, 1.0};
127 }
128
129 void AngCorrFit::ErasePoint(double ang)
130 {
131 for (int i=0; i<GetNPoints(); ++i) {
132 if (fAngles[i]==ang) {
133 fAngles.erase(fAngles.begin()+i);
134 fIntensities.erase(fIntensities.begin()+i);
135 fErrors.erase(fErrors.begin()+i);
136 }
137 }
138 }
139
141 {
142 double av;
143 for (int i=0; i<GetNPoints(); ++i) {
144 av += fIntensities[i]/GetNPoints();
145 }
146 return av;
147 }
148
149 TGraphErrors* AngCorrFit::GetGraph()
150 {
151 TGraphErrors *graph = new TGraphErrors();
152 for (int i=0; i<GetNPoints(); ++i) {
153 graph->SetPoint(i, fAngles[i], fIntensities[i]);
154 graph->SetPointError(i, 0, fErrors[i]);
155 }
156 graph->SetTitle(GetTitle());
157 return graph;
158 }
159
160 TGraphErrors *AngCorrFit::GetDispGraph(double start, double interval)
161 {
162 int count = 0;
163 std::vector<double> lowedges;
164 while (start < 180) {
165 lowedges.push_back(start);
166 start += interval;
167 }
168 int nDispPoints = lowedges.size();
169 std::vector<double> dispangs(lowedges.size());
170 std::vector<double> dispints(lowedges.size());
171 std::vector<double> invvars(lowedges.size());
172
173 for (int i=0; i<GetNPoints(); ++i) {
174 //find which 'bin' it belongs in
175 for (int p=0; p<nDispPoints; ++p) {
176 if (fAngles[i] < lowedges[p]) { continue; }
177 if (p < nDispPoints - 1) {
178 if (fAngles[i] >= lowedges[p+1] ) {
179 continue;
180 }
181 }
182 double oldSumX = dispangs[p]*invvars[p];
183 double oldSumY = dispints[p]*invvars[p];
184 double newSumX = oldSumX + fAngles[i]/pow(fErrors[i], 2);
185 double newSumY = oldSumY + fIntensities[i]/pow(fErrors[i], 2);
186 double newVar = invvars[p] + pow(1.0/fErrors[i],2);
187 dispangs[p] = newSumX/newVar;
188 dispints[p] = newSumY/newVar;
189 invvars[p] = newVar;
190
191 break;
192 }
193 }
194 //now make the actual graph
195 TGraphErrors *graph = new TGraphErrors();
196 for (int p=0; p<nDispPoints; ++p) {
197 //check there is actual data there
198 if (invvars[p]>0) {
199 graph->SetPoint(graph->GetN(), dispangs[p], dispints[p]);
200 graph->SetPointError(graph->GetN()-1, 0, sqrt(1.0/invvars[p]));
201 }
202 }
203 graph->SetTitle(GetTitle());
204 return graph;
205 }
206
208 {
209 fCorrs.clear();
210 fCorrFuncs.clear();
211 fChiGraphs.clear();
212 fDelta1.clear();
213 fDelta2.clear();
214 fChiSquared.clear();
215 for (int i = 0; i<spin1s.size(); ++i) {
216 for (int j = 0; j<spin2s.size(); ++j) {
217 for (int k = 0; k<spin3s.size(); ++k) {
218 std::string name((std::string)"AngCorr"+std::to_string(spin1s[i])+"_"+std::to_string(spin2s[j])+"_"+std::to_string(spin3s[k]));
219 AngCorr *corr = new AngCorr(spin1s[i], spin2s[j], spin3s[k], Qk);
220 TF1 *corrFunc = new TF1(name.c_str(), corr, 0, 181, 3, "GamR::AngDist::AngCorr");
221 TGraph *chiGraph = new TGraph();
222 chiGraph->SetLineColor(fColors[fCorrs.size()%fColors.size()]);
223 corrFunc->SetLineColor(fColors[fCorrs.size()%fColors.size()]);
224 TGraphErrors *graph = GetGraph();
225 corrFunc->SetParameter(0, Average());
226 int varpar = 0; //parameter to vary
227 int fixpar = 0; //parameter to fix
228
229 if ( fDel[0] == 1 ) {
230 varpar = 2;
231 fixpar = 1;
232 }
233 else if ( fDel[1] == 1 ) {
234 varpar = 1;
235 fixpar = 2;
236 }
237 else if (corr->Mixed1() + corr->Mixed2() == 2) {
238 if ( fDel[0] == 0 && fDel[1] == 0 ) {
239 //neither have been fixed,
240 //probably shouldn't do that (yet)
241 std::cout << "Two mixed transitions with no fixed delta?" << std::endl;
242 return;
243 }
244 }
245 else if (corr->Mixed1() + corr->Mixed2() == 0 ){
246 //two pure transitions, doesn't matter which way around we are
247 varpar = 1;
248 fixpar = 2;
249 }
250 else if (corr->Mixed1()) {
251 //first transition is mixed
252 varpar = 1;
253 fixpar = 2;
254 }
255 else if (corr->Mixed2()) {
256 fixpar = 1;
257 varpar = 2;
258 }
259
260 //both have been fixed
261 if (fDel[varpar-1] == 1) {
262 //delta has been fixed
263 //note that this is not really a situation I anticipate happening
264 corrFunc->SetParameter(1, fixDelta[varpar-1]);
265 corrFunc->FixParameter(1, fixDelta[varpar-1]);
266 graph->Fit(corrFunc, "Q");
267 chiGraph->SetPoint(0, fixDelta[varpar-1], corrFunc->GetChisquare());
268 varpar = 0;
269 }
270
271 if (corr->Mixed(varpar) == 0) {
272 //"varying" transition is actually pure
273 corrFunc->SetParameter(1, fixDelta[0]);
274 corrFunc->SetParameter(2, fixDelta[1]);
275 corrFunc->FixParameter(1, fixDelta[0]);
276 corrFunc->FixParameter(2, fixDelta[1]);
277 graph->Fit(corrFunc, "Q");
278 chiGraph->SetPoint(0, -TMath::Pi()/2.0+0.01, corrFunc->GetChisquare());
279 chiGraph->SetPoint(1, TMath::Pi()/2.0-0.01, corrFunc->GetChisquare());
280 varpar = 0;
281 }
282
283 //loop over delta
284 if (varpar != 0) {
285 int count = 0;
286 for (double d=-TMath::Pi()/2.0+0.01; d<TMath::Pi()/2-0.01; d+=0.3333333*0.01*(TMath::Pi()-0.02))
287 {
288 double delta = TMath::Tan(d);
289 corrFunc->SetParameter(varpar, delta);
290 corrFunc->FixParameter(varpar, delta);
291 //std::cout << "Fixing delta " << fixpar << " = " << fixDelta[fixpar-1] << std::endl;
292 corrFunc->SetParameter(fixpar, fixDelta[fixpar-1]);
293 corrFunc->FixParameter(fixpar, fixDelta[fixpar-1]);
294
295 graph->Fit(corrFunc, "Q");
296 chiGraph->SetPoint(count, d, corrFunc->GetChisquare());
297 ++count;
298 }
299 }
300 //best delta
301 double d;
302 double chi;
303 chiGraph->GetPoint(TMath::LocMin(chiGraph->GetN(), chiGraph->GetY()), d, chi);
304 double delta = TMath::Tan(d);
305 fChiSquared.push_back(chi);
306
307 double thresh = chi+1;
308
309 double low = -1e9;
310 double high = 1e9;
311 double tand_last;
312 double chi2_last;
313 chiGraph->GetPoint(0, tand_last, chi2_last);
314 for (int ig=1; ig<chiGraph->GetN(); ++ig) {
315 double tand;
316 double chi2;
317 chiGraph->GetPoint(ig, tand, chi2);
318
319 if (chi2_last > thresh && chi2 < thresh) {
320 low = TMath::Tan(tand);
321 }
322 if (chi2_last < thresh && chi2 > thresh) {
323 high = TMath::Tan(tand);
324 }
325
326 tand_last = tand;
327 chi2_last = chi2;
328 }
329
330 if (varpar != 0) {
331 corrFunc->SetParameter(varpar, delta);
332 corrFunc->FixParameter(varpar, delta);
333 corrFunc->SetParameter(fixpar, fixDelta[fixpar-1]);
334 corrFunc->FixParameter(fixpar, fixDelta[fixpar-1]);
335 }
336
337 graph->Fit(corrFunc, "Q");
338 if (varpar == 1) {
339 fDelta1.push_back(delta);
340 fDelta1Err.push_back({low - delta, high - delta});
341 fDelta2.push_back(fixDelta[1]);
342 }
343 else if (varpar == 2) {
344 fDelta1.push_back(fixDelta[0]);
345 fDelta2Err.push_back({low - delta, high - delta});
346 fDelta2.push_back(delta);
347 }
348 else {
349 fDelta1.push_back(fixDelta[0]);
350 fDelta2.push_back(fixDelta[1]);
351 }
352 fCorrs.push_back(corr);
353 fCorrFuncs.push_back(corrFunc);
354 fChiGraphs.push_back(chiGraph);
355 }
356 }
357 }
358 }
359
361 {
362 TMultiGraph *multgraph = new TMultiGraph();
363 for (int i=0; i<GetNCombs(); ++i) {
364 multgraph->Add(fChiGraphs[i]);
365 }
366 multgraph->SetTitle(GetTitle());
367 return multgraph;
368 }
369
370 void AngCorrFit::DrawChiGraph(TCanvas *c1, double ylow, double yhigh)
371 {
372 TMultiGraph *multgraph = GetChiGraph();
373 c1->cd();
374
375 //shading for confidence level
376 double fillareaX[4]={-TMath::Pi()/2.0, -TMath::Pi()/2.0, TMath::Pi()/2.0, TMath::Pi()/2.0};
377 double fillareaY[4]={0, 66.35, 66.35, 0};
378 TGraph *fillgraph = new TGraph(4, &fillareaX[0], &fillareaY[0]);
379 fillgraph->SetFillColor(kYellow);
380 fillgraph->SetFillStyle(3001);
381
382 multgraph->GetXaxis()->SetTitle("arctan(delta)");
383 multgraph->GetYaxis()->SetTitle("chi^2");
384 multgraph->Draw("AC");
385
386 //draw a legend
387 TLegend *legend = new TLegend(0.75, 0.75, 0.95, 0.95);
388 for (int i=0; i<GetNCombs(); ++i) {
389 TString entry;
390 entry.Form("%2.1f->%2.1f->%2.1f", fCorrs[i]->spin1, fCorrs[i]->spin2, fCorrs[i]->spin3);
391 legend->AddEntry(fChiGraphs[i], entry.Data());
392 }
393 legend->Draw();
394 multgraph->GetYaxis()->SetRangeUser(ylow, yhigh);
395 fillgraph->Draw("F");
396 }
397
398 void AngCorrFit::DrawAngCorr(TCanvas *c1, double start, double interval)
399 {
400 TGraph *graph = GetDispGraph(start, interval);
401 c1->cd();
402 graph->Draw("A*");
403 graph->GetXaxis()->SetLimits(0, 185);
404 TLegend *legend = new TLegend(0.9, 0.7, 1.0, 0.9);
405 for (int i=0; i<GetNCombs(); ++i) {
406 fCorrFuncs[i]->Draw("same");
407 TString entry;
408 entry.Form("%2.1f->%2.1f->%2.1f", fCorrs[i]->spin1, fCorrs[i]->spin2, fCorrs[i]->spin3);
409 legend->AddEntry(fCorrFuncs[i], entry.Data());
410 }
411 legend->Draw();
412 }
413
414 void AngCorrFit::Save(std::string filename, double start, double interval) {
415 std::ofstream ofile(filename);
416 TGraph *graph = GetDispGraph(start, interval);
417 for (int i = 0; i<graph->GetN(); ++i) {
418 double x, y, xerr, yerr;
419 graph->GetPoint(i, x, y);
420 xerr = graph->GetErrorX(i);
421 yerr = graph->GetErrorY(i);
422 ofile << x << " " << y << " " << xerr << " " << yerr << std::endl;
423 }
424 ofile << std::endl << std::endl;
425 for (int i=0; i<=180; ++i) {
426 ofile << i << " ";
427 for (int j=0; j<GetNCombs(); ++j) {
428 ofile << fCorrFuncs[j]->Eval(i) << " ";
429 }
430 ofile << std::endl;
431 }
432 ofile.close();
433 }
434
435 void AngCorrFit::SaveChiGraph(std::string filename)
436 {
437 std::ofstream ofile(filename);
438
439 for (int i=-90; i<=90; ++i) {
440 ofile << i << " ";
441 for (int j=0; j<fChiGraphs.size(); ++j) {
442 TGraph *g = fChiGraphs[j];
443 if (!g) { ofile.close(); return; }
444 ofile << g->Eval(i*TMath::Pi()/180.0) << " ";
445 }
446 ofile << std::endl;
447 }
448 ofile.close();
449 }
450
451 }
452}
std::pair< double, double > ig(TCanvas *canvas)
void DrawAngCorr(TCanvas *c1, double start, double interval)
Definition AngCorr.cc:398
std::vector< double > spin3s
Definition AngCorr.hh:85
std::vector< Color_t > fColors
Definition AngCorr.hh:81
std::vector< double > fDelta2
Definition AngCorr.hh:96
std::vector< double > fAngles
Definition AngCorr.hh:87
GamR::AngDist::SolidAttenuation * Qk
Definition AngCorr.hh:104
AngCorrFit(std::vector< double > angs, std::vector< TH2D * > hists, GamR::Nucleus::Transition peak1, GamR::Nucleus::Transition peak2, TH1D *angNorm, const char *name, const char *title)
Definition AngCorr.cc:122
std::vector< double > fIntensities
Definition AngCorr.hh:88
void SaveChiGraph(std::string filename)
Definition AngCorr.cc:435
std::vector< std::pair< double, double > > fDelta2Err
Definition AngCorr.hh:98
std::vector< double > spin2s
Definition AngCorr.hh:84
void SetData(std::vector< double > angs, std::vector< TH2D * > hists, GamR::Nucleus::Transition peak1, GamR::Nucleus::Transition peak2, TH1D *angNorm)
Definition AngCorr.cc:96
std::vector< double > fixDelta
Definition AngCorr.hh:101
TMultiGraph * GetChiGraph()
Definition AngCorr.cc:360
std::vector< double > fChiSquared
Definition AngCorr.hh:99
std::vector< AngCorr * > fCorrs
Definition AngCorr.hh:91
void ErasePoint(double ang)
Definition AngCorr.cc:129
std::vector< TGraph * > fChiGraphs
Definition AngCorr.hh:93
std::vector< int > fDel
Definition AngCorr.hh:102
std::vector< double > spin1s
Definition AngCorr.hh:83
TGraphErrors * GetDispGraph(double start, double interval)
Definition AngCorr.cc:160
std::vector< TF1 * > fCorrFuncs
Definition AngCorr.hh:92
std::vector< double > fErrors
Definition AngCorr.hh:89
std::vector< double > fDelta1
Definition AngCorr.hh:95
void Save(std::string filename, double start, double interval)
Definition AngCorr.cc:414
std::vector< std::pair< double, double > > fDelta1Err
Definition AngCorr.hh:97
void DrawChiGraph(TCanvas *c1, double ylow, double yhigh)
Definition AngCorr.cc:370
TGraphErrors * GetGraph()
Definition AngCorr.cc:149
int Mixed(double s1, double s2)
Definition AngCorr.cc:68
double Ak(double k, double delta1, double delta2)
Definition AngCorr.cc:17
double Apply(TH1D *hist) const
Definition Transition.cc:53
double ApplyError(TH1D *hist) const
Definition Transition.cc:56
double Ak(double k, double ji, double lambda, double lambdaPrime, double jf, double delta)
Definition AngDist.cc:66
double Fk(double k, double jf, double lambda, double lambdaPrime, double ji)
Definition AngDist.cc:32
TH1D * BackgroundSubtractY(TH2 *hist, GamR::TK::Gate peak, std::vector< GamR::TK::Gate > background, const char *name)
Definition Cut.cc:307
Definition Gain.cc:19