GRSISort "v4.0.0.5"
An extension of the ROOT analysis Framework
Loading...
Searching...
No Matches
GateBG.C
Go to the documentation of this file.
1TH1* NewProjectionXBGP(TH2* matrix, Double_t gate_low, Double_t gate_high, Double_t bg_low = -1, Double_t bg_high = -1)
2{
3 Int_t gate_low_bin = matrix->GetYaxis()->FindBin(gate_low);
4 Int_t gate_high_bin = matrix->GetYaxis()->FindBin(gate_high);
5 Int_t bg_low_bin = matrix->GetYaxis()->FindBin(bg_low);
6 Int_t bg_high_bin = matrix->GetYaxis()->FindBin(bg_high);
7
8 std::cout << "Gating from bin: " << gate_low_bin << " to " << gate_high_bin << std::endl;
9
10 //Tre to get the background under the peak if this has been done already
11 TH1* background_hist = (TH1*)(gDirectory->Get(Form("%s_px_background", matrix->GetName())));
12 if(!background_hist) {
13 std::cout << "Missing a background " << (Form("%s_px_background", matrix->GetName())) << " to use, not performing BG correction!!!" << std::endl;
14 return matrix->ProjectionX(Form("gated_%s_%.0f_to_%.0f_px", matrix->GetName(), gate_low, gate_high), gate_low_bin, gate_high_bin);
15 ;
16 }
17
18 //The peak should have came from the y-axis projection. We need to use this histogram to do things like get bin width, and the coord->bin mapping
19 TH1* y_projection = matrix->ProjectionY();
20
21 //Integrate the background
22 Double_t bg_counts_under_peak = background_hist->Integral(gate_low_bin, gate_high_bin);
23 Double_t bg_counts_in_bg_gate = y_projection->Integral(bg_low_bin, bg_high_bin);
24 if(bg_low == -1. && bg_high == -1.)
25 bg_counts_in_bg_gate = 1.;
26
27 Double_t scale_factor = bg_counts_under_peak / bg_counts_in_bg_gate;
28 std::cout << "Background counts = " << bg_counts_under_peak << std::endl;
29 std::cout << "Scale factor = " << scale_factor << std::endl;
30 //Use Get Axis and low edge, high edge etc.
31 //First thing we need to do is project out the matrix using the gate.
32 TH1* gated_histogram = matrix->ProjectionX(Form("gated_%s_%.0f_to_%.0f_px", matrix->GetName(), gate_low, gate_high), gate_low_bin, gate_high_bin);
33 TH1* bg_histogram = matrix->ProjectionX(Form("bg_%s_%.0f_to_%.0f_px", matrix->GetName(), bg_low, bg_high), bg_low_bin, bg_high_bin);
34
35 //Clone the gated histogram so that we can subtract the bg from it
36 TH1* corrected_histogram = static_cast<TH1*>(gated_histogram->Clone());
37 corrected_histogram->SetName(Form("corrected_%s_%.0f_%.0f_px", matrix->GetName(), gate_low, gate_high));
38 corrected_histogram->Add(bg_histogram, -scale_factor);
39
40 return corrected_histogram;
41}
42
43TH1* ProjectionXBGP(TH2* matrix, Double_t gate_low, Double_t gate_high, Double_t bg_low = -1, Double_t bg_high = -1)
44{
45 Int_t gate_low_bin = matrix->GetYaxis()->FindBin(gate_low);
46 Int_t gate_high_bin = matrix->GetYaxis()->FindBin(gate_high);
47 Int_t bg_low_bin = matrix->GetYaxis()->FindBin(bg_low);
48 Int_t bg_high_bin = matrix->GetYaxis()->FindBin(bg_high);
49
50 std::cout << "Gating from bin: " << gate_low_bin << " to " << gate_high_bin << std::endl;
51
52 std::cout << "Gating from bin: " << gate_low_bin << " to " << gate_high_bin << std::endl;
53
54 //The Projection functions are inclusive on the bins, we need to take this into account when coming up with our scale factor.
55 //Find the counts in the background of the GPeak.
56 //Get the background of the last peak fit
57 if(!GPeak::GetLastFit()) {
58 std::cout << "Missing a Fit to use, not performing BG correction!!!" << std::endl;
59 return matrix->ProjectionX(Form("gated_%s_%.0f_to_%.0f_px", matrix->GetName(), gate_low, gate_high), gate_low_bin, gate_high_bin);
60 ;
61 }
62
63 GPeak* peak = GPeak::GetLastFit();
64 //The peak should have came from the y-axis projection. We need to use this histogram to do things like get bin width, and the coord->bin mapping
65 TH1* y_projection = matrix->ProjectionY();
66 Double_t bin_width = y_projection->GetBinWidth(1);
67
68 //Integrate the background
69 Double_t bg_counts_under_peak = peak->Background()->Integral(y_projection->GetXaxis()->GetBinLowEdge(gate_low_bin), y_projection->GetXaxis()->GetBinUpEdge(gate_high_bin)) * bin_width;
70 Double_t bg_counts_in_bg_gate = y_projection->Integral(bg_low_bin, bg_high_bin);
71 if(bg_low == -1. && bg_high == -1.)
72 bg_counts_in_bg_gate = 1.;
73
74 Double_t scale_factor = bg_counts_under_peak / bg_counts_in_bg_gate;
75 std::cout << "Background counts = " << bg_counts_under_peak << std::endl;
76 std::cout << "Scale factor = " << scale_factor << std::endl;
77 //Use Get Axis and low edge, high edge etc.
78 //First thing we need to do is project out the matrix using the gate.
79 TH1* gated_histogram = matrix->ProjectionX(Form("gated_%s_%.0f_to_%.0f_px", matrix->GetName(), gate_low, gate_high), gate_low_bin, gate_high_bin);
80 TH1* bg_histogram = matrix->ProjectionX(Form("bg_%s_%.0f_to_%.0f_px", matrix->GetName(), bg_low, bg_high), bg_low_bin, bg_high_bin);
81
82 //Clone the gated histogram so that we can subtract the bg from it
83 TH1* corrected_histogram = static_cast<TH1*>(gated_histogram->Clone());
84 corrected_histogram->SetName(Form("corrected_%s_%.0f_%.0f_px", matrix->GetName(), gate_low, gate_high));
85 corrected_histogram->Add(bg_histogram, -scale_factor);
86
87 return corrected_histogram;
88}
89
90TH1* ProjectionYBGP(TH2* matrix, Double_t gate_low, Double_t gate_high, Double_t bg_low = -1, Double_t bg_high = -1)
91{
92 Int_t gate_low_bin = matrix->GetYaxis()->FindBin(gate_low);
93 Int_t gate_high_bin = matrix->GetYaxis()->FindBin(gate_high);
94 Int_t bg_low_bin = matrix->GetYaxis()->FindBin(bg_low);
95 Int_t bg_high_bin = matrix->GetYaxis()->FindBin(bg_high);
96
97 std::cout << "Gating from bin: " << gate_low_bin << " to " << gate_high_bin << std::endl;
98
99 //The Projection functions are inclusive on the bins, we need to take this into account when coming up with our scale factor.
100 //Find the counts in the background of the GPeak.
101 //Get the background of the last peak fit
102 if(!GPeak::GetLastFit()) {
103 std::cout << "Missing a Fit to use, not performing BG correction!!!" << std::endl;
104 return matrix->ProjectionY(Form("gated_%s_%.0f_to_%.0f_py", matrix->GetName(), gate_low, gate_high), gate_low_bin, gate_high_bin);
105 ;
106 }
107
108 GPeak* peak = GPeak::GetLastFit();
109 //The peak should have came from the y-axis projection. We need to use this histogram to do things like get bin width, and the coord->bin mapping
110 TH1* x_projection = matrix->ProjectionX();
111 Double_t bin_width = x_projection->GetBinWidth(1);
112
113 //Integrate the background
114 Double_t bg_counts_under_peak = peak->Background()->Integral(x_projection->GetXaxis()->GetBinLowEdge(gate_low_bin), x_projection->GetXaxis()->GetBinUpEdge(gate_high_bin)) * bin_width;
115 Double_t bg_counts_in_bg_gate = x_projection->Integral(bg_low_bin, bg_high_bin);
116 if(bg_low == -1. && bg_high == -1.)
117 bg_counts_in_bg_gate = 1.;
118
119 Double_t scale_factor = bg_counts_under_peak / bg_counts_in_bg_gate;
120 std::cout << "Background counts = " << bg_counts_under_peak << std::endl;
121 std::cout << "Scale factor = " << scale_factor << std::endl;
122 //Use Get Axis and low edge, high edge etc.
123 //First thing we need to do is project out the matrix using the gate.
124 TH1* gated_histogram = matrix->ProjectionY(Form("gated_%s_%.0f_to_%.0f_py", matrix->GetName(), gate_low, gate_high), gate_low_bin, gate_high_bin);
125 TH1* bg_histogram = matrix->ProjectionY(Form("bg_%s_%.0f_to_%.0f_py", matrix->GetName(), bg_low, bg_high), bg_low_bin, bg_high_bin);
126
127 //Clone the gated histogram so that we can subtract the bg from it
128 TH1* corrected_histogram = static_cast<TH1*>(gated_histogram->Clone());
129 corrected_histogram->SetName(Form("corrected_%s_%.0f_%.0f_py", matrix->GetName(), gate_low, gate_high));
130 corrected_histogram->Add(bg_histogram, -scale_factor);
131
132 return corrected_histogram;
133}
TH1 * NewProjectionXBGP(TH2 *matrix, Double_t gate_low, Double_t gate_high, Double_t bg_low=-1, Double_t bg_high=-1)
Definition GateBG.C:1
TH1 * ProjectionYBGP(TH2 *matrix, Double_t gate_low, Double_t gate_high, Double_t bg_low=-1, Double_t bg_high=-1)
Definition GateBG.C:90
TH1 * ProjectionXBGP(TH2 *matrix, Double_t gate_low, Double_t gate_high, Double_t bg_low=-1, Double_t bg_high=-1)
Definition GateBG.C:43
Definition GPeak.h:11
TF1 * Background(Option_t *="TF1")
Definition GPeak.h:31
static GPeak * GetLastFit()
Definition GPeak.h:68