GamR  0.0.0
GammaROOT
Loading...
Searching...
No Matches
Calibrate.cc
Go to the documentation of this file.
1#include <iostream>
2
3#include <utils/Utilities.hh>
4#include <spect/Calibrate.hh>
5#include <spect/Transform.hh>
6
7#include <TSystem.h>
8#include <TSpectrum.h>
9#include <TMarker.h>
10#include <TLine.h>
11
12namespace GamR {
13 namespace Spect {
14 std::pair<double, double> TwoClickCalibrate(TVirtualPad *canvas) {
15 if (!canvas) { if (gPad) { canvas = gPad->GetCanvas(); } else { return {-1,-1}; } }
16 TH1D *hist = GamR::Utils::GetHist1D(canvas);
17
18 canvas->SetCrosshair();
19
20 canvas->Update();
21
23 std::vector<std::string> messages = {"Click for first point, press any key to exit...", "Click for second point, press any key to exit..."};
24
25 int retval = click.GetClicks(canvas, 2, messages, 1);
26
27 if (retval > 0) { // quit prematurely
28 std::cout << "calibration cancelled" << std::endl;
29 canvas->SetCrosshair(0);
30 return std::pair<double,double>(-1,-1);
31 }
32
33 double lowX = click.xs[0];
34 double lowY = click.ys[0];
35 double highX = click.xs[1];
36 double highY = click.ys[1];
37
38 canvas->SetCrosshair(0);
39 canvas->Modified();
40 canvas->Update();
41 gSystem->ProcessEvents();
42 canvas->Modified();
43 canvas->Update();
44
45 canvas->SetCrosshair(0);
46 double gradient = (highY-lowY)/(highX-lowX);
47 double offset = lowY - lowX*gradient;
48
49 return std::pair<double,double>(gradient,offset);
50 }
51
52 std::pair<double, double> TwoPointCalibrate(TVirtualPad *canvas, double sigma) {
53 if (!canvas) { if (gPad) { canvas = gPad->GetCanvas(); } else { return {-1,-1}; } }
54 TMarker *marker = new TMarker();
55 TObject *obj;
56 TSpectrum *spect = new TSpectrum();
57 TH1D *hist = GamR::Utils::GetHist1D(canvas);
58 double startlow = canvas->GetUxmin();
59 double starthigh = canvas->GetUxmax();;
60 double range = 10*sigma*hist->GetBinWidth(1);
61 if (range < 100*hist->GetBinWidth(1)) {
62 range = 100*hist->GetBinWidth(1);
63 }
64
65 int ex=-2;
66
67 canvas->SetCrosshair();
68
69 canvas->Update();
70
71 double low = -1;
72 double high = -1;
73
74 std::string canvasname = canvas->GetName();
75
77 canvas->Connect("ProcessedEvent(Int_t,Int_t,Int_t,TObject*)", "GamR::Utils::Clicker", &click, "GetClick(Int_t,Int_t,Int_t,TObject*)");
78
79 double lowPos = -1;
80 double highPos = -1;
81 double lowEn;
82 double highEn;
83
84 TLine *line_low;
85 TLine *line_high;
86 std::cout << "Click for lower peak" << std::endl;
87 while (true){
88 canvas->Modified();
89 canvas->Update();
90 click.waiting=false;
91 obj=canvas->WaitPrimitive();
92 if (!obj) break;
93 if (strncmp(obj->ClassName(),"TMarker",7)==0) {
94 marker=(TMarker*)obj;
95 if (ex==-2){
96 low = marker->GetX();
97 ex = ex + 1;
98
99 hist->GetXaxis()->SetRangeUser(low-range,low+range);
100 int nPeaks = spect->Search(hist,sigma);
101 double closest = 1e9;
102 for (int i=0; i<nPeaks; ++i) {
103 double posX = spect->GetPositionX()[i];
104 double dist = std::abs(posX-low);
105 if (dist < closest) {
106 closest = dist;
107 lowPos = posX;
108 line_low = new TLine(lowPos, canvas->GetUymin(), lowPos, canvas->GetUymax());
109 line_low->SetLineColor(kRed);
110 line_low->SetLineWidth(2);
111 line_low->Draw();
112 }
113 }
114 hist->GetXaxis()->SetRangeUser(startlow, starthigh);
115 canvas->Update();
116
117 std::cout << "Peak estimate: " << lowPos << std::endl;
118
119 std::cout << "Click for upper peak..." << std::endl;
120 } else {
121 high = marker->GetX();
122
123 hist->GetXaxis()->SetRangeUser(high-range,high+range);
124 int nPeaks = spect->Search(hist, sigma);
125 double closest = 1e9;
126
127 for (int i=0; i<nPeaks; ++i) {
128 double posX = spect->GetPositionX()[i];
129 double dist = std::abs(posX-high);
130 if (dist < closest) {
131 closest = dist;
132 highPos = posX;
133 line_high = new TLine(highPos, canvas->GetUymin(), highPos, canvas->GetUymax());
134 line_high->SetLineColor(kRed);
135 line_high->SetLineWidth(2);
136 line_high->Draw();
137 }
138 }
139 hist->GetXaxis()->SetRangeUser(startlow, starthigh);
140 canvas->Update();
141 canvas->Modified();
142
143 std::cout << "Peak estimate: " << highPos << std::endl;
144
145 delete marker;
146 ex = ex + 1;
147 break;
148 }
149 delete marker;
150 }
151 }
152 canvas->Disconnect("ProcessedEvent(Int_t,Int_t,Int_t,TObject*)", &click, "GetClick(Int_t,Int_t,Int_t,TObject*)");
153 canvas->SetCrosshair(0);
154 line_low->Draw();
155 line_high->Draw();
156 canvas->Modified();
157 canvas->Update();
158 gSystem->ProcessEvents();
159 canvas->Modified();
160 canvas->Update();
161
162 std::cout << "Low peak energy: ";
163 std::cin >> lowEn;
164 std::cout << "High peak energy: ";
165 std::cin >> highEn;
166
167 if (ex < 0) { // quit prematurely
168 std::cout << "calibration cancelled" << std::endl;
169 canvas->SetCrosshair(0);
170 line_low->Delete();
171 line_high->Delete();
172 return std::pair<double,double>(-1,-1);
173 } else {
174 canvas->SetCrosshair(0);
175 double gradient = (highEn-lowEn)/(highPos-lowPos);
176 double offset = lowEn - lowPos*gradient;
177
178 std::string answer;
179 while (true) {
180 std::cout << "Gain = " << gradient << " Offset = " << offset << std::endl;
181 std::cout << "Apply calibration? [y/n]:";
182 std::cin >> answer;
183 if (answer == "y") {
184 GamR::Spect::ScaleLabelsLinear(hist, gradient, offset);
185 break;
186 }
187 else if (answer == "n") {
188 break;
189 }
190 }
191 line_low->Delete();
192 line_high->Delete();
193 return std::pair<double,double>(gradient,offset);
194 }
195
196 }
197
198 std::pair<double, double> TwoPointCalibrate(TVirtualPad *canvas, double lowEn, double highEn, double sigma) {
199 if (!canvas) { if (gPad) { canvas = gPad->GetCanvas(); } else { return {-1,-1}; } }
200 TMarker *marker = new TMarker();
201 TObject *obj;
202 TSpectrum *spect = new TSpectrum();
203 TH1D *hist = GamR::Utils::GetHist1D(canvas);
204 canvas->Update();
205 double startlow = canvas->GetUxmin();
206 double starthigh = canvas->GetUxmax();
207 std::cout << startlow << " " << starthigh << std::endl;
208 double range = 10*sigma*hist->GetBinWidth(1);
209 if (range < 100*hist->GetBinWidth(1)) {
210 range = 100*hist->GetBinWidth(1);
211 }
212
213 int ex=-2;
214
215 canvas->SetCrosshair();
216
217 canvas->Update();
218
219 double low = -1;
220 double high = -1;
221
222 std::string canvasname = canvas->GetName();
223
224 //std::string functioncall = "GamR::Utils::GetClick("+canvasname+")";
226 canvas->Connect("ProcessedEvent(Int_t,Int_t,Int_t,TObject*)", "GamR::Utils::Clicker", &click, "GetClick(Int_t,Int_t,Int_t,TObject*)");
227
228 double lowPos = -1;
229 double highPos = -1;
230
231 TLine *line_low;
232 TLine *line_high;
233 std::cout << "Click for lower peak" << std::endl;
234 while (true){
235 canvas->Modified();
236 canvas->Update();
237 click.waiting=false;
238 obj=canvas->WaitPrimitive();
239 if (!obj) break;
240 if (strncmp(obj->ClassName(),"TMarker",7)==0) {
241 marker=(TMarker*)obj;
242 if (ex==-2){
243 low = marker->GetX();
244 ex = ex + 1;
245
246 hist->GetXaxis()->SetRangeUser(low-range,low+range);
247 int nPeaks = spect->Search(hist,sigma);
248 double closest = 1e9;
249 for (int i=0; i<nPeaks; ++i) {
250 double posX = spect->GetPositionX()[i];
251 double dist = std::abs(posX-low);
252 if (dist < closest) {
253 closest = dist;
254 lowPos = posX;
255 line_low = new TLine(lowPos, canvas->GetUymin(), lowPos, canvas->GetUymax());
256 line_low->SetLineColor(kRed);
257 line_low->SetLineWidth(2);
258 line_low->Draw();
259 }
260 }
261 hist->GetXaxis()->SetRangeUser(startlow, starthigh);
262 canvas->Update();
263
264 std::cout << "Peak estimate: " << lowPos << std::endl;
265
266 std::cout << "Click for upper peak..." << std::endl;
267 } else {
268 high = marker->GetX();
269
270 hist->GetXaxis()->SetRangeUser(high-range,high+range);
271 int nPeaks = spect->Search(hist, sigma);
272 double closest = 1e9;
273
274 for (int i=0; i<nPeaks; ++i) {
275 double posX = spect->GetPositionX()[i];
276 double dist = std::abs(posX-high);
277 if (dist < closest) {
278 closest = dist;
279 highPos = posX;
280 line_high = new TLine(highPos, canvas->GetUymin(), highPos, canvas->GetUymax());
281 line_high->SetLineColor(kRed);
282 line_high->SetLineWidth(2);
283 line_high->Draw();
284 }
285 }
286 hist->GetXaxis()->SetRangeUser(startlow, starthigh);
287 canvas->Update();
288 canvas->Modified();
289
290 std::cout << "Peak estimate: " << highPos << std::endl;
291
292 delete marker;
293 ex = ex + 1;
294 break;
295 }
296 delete marker;
297 }
298 }
299 canvas->Disconnect("ProcessedEvent(Int_t,Int_t,Int_t,TObject*)", &click, "GetClick(Int_t,Int_t,Int_t,TObject*)");
300 canvas->SetCrosshair(0);
301 line_low->Draw();
302 line_high->Draw();
303 canvas->Modified();
304 canvas->Update();
305 gSystem->ProcessEvents();
306 canvas->Modified();
307 canvas->Update();
308
309 if (ex < 0) { // quit prematurely
310 std::cout << "calibration cancelled" << std::endl;
311 canvas->SetCrosshair(0);
312 line_low->Delete();
313 line_high->Delete();
314 return std::pair<double,double>(-1,-1);
315 } else {
316 canvas->SetCrosshair(0);
317 double gradient = (highEn-lowEn)/(highPos-lowPos);
318 double offset = lowEn - lowPos*gradient;
319
320 line_low->Delete();
321 line_high->Delete();
322 return std::pair<double,double>(gradient,offset);
323 }
324 }
325
326 std::pair<double, double> TwoPointCalibrate(TH1 *hist, double lowEst, double highEst, double lowEn, double highEn, double sigma) {
327 TSpectrum *spect = new TSpectrum();
328 double range = 10*sigma;
329
330 hist->GetXaxis()->SetRangeUser(lowEst-range,lowEst+range);
331 int nPeaks = spect->Search(hist,sigma,"",0.1);
332 double closest = 1e9;
333 double lowPos = 0;
334 for (int i=0; i<nPeaks; ++i) {
335 double posX = spect->GetPositionX()[i];
336 double dist = std::abs(posX-lowEst);
337 if (dist < closest) {
338 closest = dist;
339 lowPos = posX;
340 }
341 }
342
343 hist->GetXaxis()->SetRangeUser(highEst-range,highEst+range);
344 nPeaks = spect->Search(hist,sigma,"",0.1);
345 double highPos = 0;
346 closest = 1e9;
347 for (int i=0; i<nPeaks; ++i) {
348 double posX = spect->GetPositionX()[i];
349 double dist = std::abs(posX-highEst);
350 if (dist < closest) {
351 closest = dist;
352 highPos = posX;
353 }
354 }
355 double gradient = (highEn-lowEn)/(highPos-lowPos);
356 double offset = lowEn - lowPos*gradient;
357
358 return std::pair<double,double>(gradient,offset);
359 }
360 }
361}
362
std::vector< double > xs
Definition Utilities.hh:38
std::vector< double > ys
Definition Utilities.hh:39
int GetClicks(TVirtualPad *canvas, int n, std::vector< std::string > &messages, int draw=0, int print=0)
Definition Utilities.cc:127
void ScaleLabelsLinear(TH1 *h, Double_t m, Double_t c)
Scales the labels of a histogram. Preferable over GamR::Spect::Scale and GamR::Spect::ScalePoly as it...
Definition Transform.cc:64
std::pair< double, double > TwoPointCalibrate(TVirtualPad *canvas, double sigma)
Definition Calibrate.cc:52
std::pair< double, double > TwoClickCalibrate(TVirtualPad *canvas)
Definition Calibrate.cc:14
TH1D * GetHist1D(TVirtualPad *canvas)
Definition Utilities.cc:182
Definition Gain.cc:19