15 if (!canvas) {
if (gPad) { canvas = gPad->GetCanvas(); }
else {
return {-1,-1}; } }
18 canvas->SetCrosshair();
23 std::vector<std::string> messages = {
"Click for first point, press any key to exit...",
"Click for second point, press any key to exit..."};
25 int retval = click.
GetClicks(canvas, 2, messages, 1);
28 std::cout <<
"calibration cancelled" << std::endl;
29 canvas->SetCrosshair(0);
30 return std::pair<double,double>(-1,-1);
33 double lowX = click.
xs[0];
34 double lowY = click.
ys[0];
35 double highX = click.
xs[1];
36 double highY = click.
ys[1];
38 canvas->SetCrosshair(0);
41 gSystem->ProcessEvents();
45 canvas->SetCrosshair(0);
46 double gradient = (highY-lowY)/(highX-lowX);
47 double offset = lowY - lowX*gradient;
49 return std::pair<double,double>(gradient,offset);
53 if (!canvas) {
if (gPad) { canvas = gPad->GetCanvas(); }
else {
return {-1,-1}; } }
54 TMarker *marker =
new TMarker();
56 TSpectrum *spect =
new TSpectrum();
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);
67 canvas->SetCrosshair();
74 std::string canvasname = canvas->GetName();
77 canvas->Connect(
"ProcessedEvent(Int_t,Int_t,Int_t,TObject*)",
"GamR::Utils::Clicker", &click,
"GetClick(Int_t,Int_t,Int_t,TObject*)");
86 std::cout <<
"Click for lower peak" << std::endl;
91 obj=canvas->WaitPrimitive();
93 if (strncmp(obj->ClassName(),
"TMarker",7)==0) {
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) {
108 line_low =
new TLine(lowPos, canvas->GetUymin(), lowPos, canvas->GetUymax());
109 line_low->SetLineColor(kRed);
110 line_low->SetLineWidth(2);
114 hist->GetXaxis()->SetRangeUser(startlow, starthigh);
117 std::cout <<
"Peak estimate: " << lowPos << std::endl;
119 std::cout <<
"Click for upper peak..." << std::endl;
121 high = marker->GetX();
123 hist->GetXaxis()->SetRangeUser(high-range,high+range);
124 int nPeaks = spect->Search(hist, sigma);
125 double closest = 1e9;
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) {
133 line_high =
new TLine(highPos, canvas->GetUymin(), highPos, canvas->GetUymax());
134 line_high->SetLineColor(kRed);
135 line_high->SetLineWidth(2);
139 hist->GetXaxis()->SetRangeUser(startlow, starthigh);
143 std::cout <<
"Peak estimate: " << highPos << std::endl;
152 canvas->Disconnect(
"ProcessedEvent(Int_t,Int_t,Int_t,TObject*)", &click,
"GetClick(Int_t,Int_t,Int_t,TObject*)");
153 canvas->SetCrosshair(0);
158 gSystem->ProcessEvents();
162 std::cout <<
"Low peak energy: ";
164 std::cout <<
"High peak energy: ";
168 std::cout <<
"calibration cancelled" << std::endl;
169 canvas->SetCrosshair(0);
172 return std::pair<double,double>(-1,-1);
174 canvas->SetCrosshair(0);
175 double gradient = (highEn-lowEn)/(highPos-lowPos);
176 double offset = lowEn - lowPos*gradient;
180 std::cout <<
"Gain = " << gradient <<
" Offset = " << offset << std::endl;
181 std::cout <<
"Apply calibration? [y/n]:";
187 else if (answer ==
"n") {
193 return std::pair<double,double>(gradient,offset);
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();
202 TSpectrum *spect =
new TSpectrum();
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);
215 canvas->SetCrosshair();
222 std::string canvasname = canvas->GetName();
226 canvas->Connect(
"ProcessedEvent(Int_t,Int_t,Int_t,TObject*)",
"GamR::Utils::Clicker", &click,
"GetClick(Int_t,Int_t,Int_t,TObject*)");
233 std::cout <<
"Click for lower peak" << std::endl;
238 obj=canvas->WaitPrimitive();
240 if (strncmp(obj->ClassName(),
"TMarker",7)==0) {
241 marker=(TMarker*)obj;
243 low = marker->GetX();
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) {
255 line_low =
new TLine(lowPos, canvas->GetUymin(), lowPos, canvas->GetUymax());
256 line_low->SetLineColor(kRed);
257 line_low->SetLineWidth(2);
261 hist->GetXaxis()->SetRangeUser(startlow, starthigh);
264 std::cout <<
"Peak estimate: " << lowPos << std::endl;
266 std::cout <<
"Click for upper peak..." << std::endl;
268 high = marker->GetX();
270 hist->GetXaxis()->SetRangeUser(high-range,high+range);
271 int nPeaks = spect->Search(hist, sigma);
272 double closest = 1e9;
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) {
280 line_high =
new TLine(highPos, canvas->GetUymin(), highPos, canvas->GetUymax());
281 line_high->SetLineColor(kRed);
282 line_high->SetLineWidth(2);
286 hist->GetXaxis()->SetRangeUser(startlow, starthigh);
290 std::cout <<
"Peak estimate: " << highPos << std::endl;
299 canvas->Disconnect(
"ProcessedEvent(Int_t,Int_t,Int_t,TObject*)", &click,
"GetClick(Int_t,Int_t,Int_t,TObject*)");
300 canvas->SetCrosshair(0);
305 gSystem->ProcessEvents();
310 std::cout <<
"calibration cancelled" << std::endl;
311 canvas->SetCrosshair(0);
314 return std::pair<double,double>(-1,-1);
316 canvas->SetCrosshair(0);
317 double gradient = (highEn-lowEn)/(highPos-lowPos);
318 double offset = lowEn - lowPos*gradient;
322 return std::pair<double,double>(gradient,offset);
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;
330 hist->GetXaxis()->SetRangeUser(lowEst-range,lowEst+range);
331 int nPeaks = spect->Search(hist,sigma,
"",0.1);
332 double closest = 1e9;
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) {
343 hist->GetXaxis()->SetRangeUser(highEst-range,highEst+range);
344 nPeaks = spect->Search(hist,sigma,
"",0.1);
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) {
355 double gradient = (highEn-lowEn)/(highPos-lowPos);
356 double offset = lowEn - lowPos*gradient;
358 return std::pair<double,double>(gradient,offset);