18 std::vector<TH1*> spectra;
19 for (
int i=0; i<(gDirectory->GetList()->GetSize()); ++i) {
20 TObject *obj = gDirectory->GetList()->At(i);
21 if (!obj->InheritsFrom(
"TH1") || obj->InheritsFrom(
"TH2") || obj->InheritsFrom(
"TH3"))
continue;
23 std::cout << iHist <<
" " << obj->GetName() << std::endl;
25 spectra.push_back((TH1*)obj);
29 for (
int i=0; i<(gDirectory->GetListOfKeys()->GetSize()); ++i) {
30 TKey *key =
static_cast<TKey*
>(gDirectory->GetListOfKeys()->At(i));
31 TClass *cl = gROOT->GetClass(key->GetClassName());
32 if (!cl->InheritsFrom(
"TH1") || cl->InheritsFrom(
"TH2") || cl->InheritsFrom(
"TH3"))
continue;
34 std::cout << iHist <<
" " << key->GetName() << std::endl;
38 for (
int j=0; j<(gDirectory->GetList()->GetSize()); ++j) {
39 if (ex == 1) {
break; }
40 if ((std::string)(gDirectory->GetList()->At(j)->GetName()) == (std::string)(key->GetName())) {
46 spectra.push_back((TH1*)key->ReadObj());
55 std::vector<TH2*> spectra;
56 for (
int i=0; i<(gDirectory->GetListOfKeys()->GetSize()); ++i) {
57 TKey *key =
static_cast<TKey*
>(gDirectory->GetListOfKeys()->At(i));
58 TClass *cl = gROOT->GetClass(key->GetClassName());
59 if (cl->InheritsFrom(
"TH2")) {
61 std::cout << iHist <<
" " << key->GetName() << std::endl;
65 for (
int j=0; j<(gDirectory->GetList()->GetSize()); ++j) {
66 if (ex == 1) {
break; }
67 if ((std::string)(gDirectory->GetList()->At(j)->GetName()) == (std::string)(key->GetName())) {
68 spectra.push_back((TH2*)gDirectory->GetList()->At(j));
73 spectra.push_back((TH2*)key->ReadObj());
81 void Draw(
int i, Option_t *option ) {
83 spectra[i]->Draw(option);
85 void Draw2D(
int i, Option_t *option ) {
87 spectra[i]->Draw(option);
90 void OverlaySpectra(std::vector<int> display_indexes, TCanvas *canvas, Option_t *option) {
92 std::vector<TH1*> display_spectra;
93 for (
auto &i : display_indexes) {
94 display_spectra.push_back(spectra[i]);
99 void OverlaySpectra(
int iStart,
int iStop, TCanvas *canvas, Option_t *option) {
101 std::vector<TH1*> display_spectra;
102 for (
int i=iStart; i<=iStop; ++i) {
103 display_spectra.push_back(spectra[i]);
108 void OverlaySpectra(std::vector<TH1*> hists, TCanvas *canvas, Option_t *option) {
109 if (!canvas) {
if (gPad) { canvas = gPad->GetCanvas(); }
else { canvas =
new TCanvas(); } }
115 std::vector<std::string> colors_hex = {
"#000076",
124 std::vector<int> colors;
125 for (
int i=0; i<(int)colors_hex.size(); ++i) {
126 colors.push_back(TColor::GetColor(colors_hex[i].c_str()));
129 TLegend *leg =
new TLegend(0.1, 0.7, 0.3, 0.9);
130 for (
int i=0; i<(int)hists.size(); ++i) {
131 TH1 *hist = hists[i];
132 hist->SetLineColor(colors[i%colors.size()]);
133 leg->AddEntry(hist, hist->GetName(),
"l");
134 hist->GetXaxis()->UnZoom();
135 if (i==0) { hist->Draw(option); }
136 else { hist->Draw(((std::string)option+
" same").c_str()); }
142 std::vector<TH1*> hists;
143 for (
int i=iStart; i<=iStop; ++i) {
144 std::string name = (std::string)hist->GetName()+
"_px_"+std::to_string(i);
145 hists.push_back((TH1*)hist->ProjectionX(name.c_str(), i,i));
151 std::vector<TH1*> hists;
152 for (
int i=0; i<indices.size(); ++i) {
153 int indx = indices[i];
154 std::string name = (std::string)hist->GetName()+
"_px_"+std::to_string(indx);
155 hists.push_back((TH1*)hist->ProjectionX(name.c_str(), indx,indx));
165 void OverlaySpectra(std::vector<std::string> files, std::string name,
int iX, Option_t *option) {
166 std::vector<TH1*> hists;
167 for (
int iFile = 0; iFile<files.size(); ++iFile) {
168 TFile *file =
new TFile(files[iFile].c_str());
169 TH2D* hist2 = (TH2D*)file->Get<TH2>(name.c_str());
170 TH1* hist = (TH1*)hist2->ProjectionX(files[iFile].substr(0, files[iFile].size()-5).c_str(), iX, iX);
171 hists.push_back(hist);
176 void OverlaySpectra(std::vector<std::string> files, std::string name,
int iXstart,
int iXstop, Option_t *option) {
177 std::vector<TH1*> hists;
178 for (
int iFile = 0; iFile<files.size(); ++iFile) {
179 TFile *file =
new TFile(files[iFile].c_str());
180 TH2D* hist2 = (TH2D*)file->Get<TH2>(name.c_str());
181 TH1* hist = (TH1*)hist2->ProjectionX(files[iFile].substr(0, files[iFile].size()-5).c_str(), iXstart, iXstop);
182 hists.push_back(hist);
187 void OverlaySpectra(std::vector<std::string> files, std::string name, Option_t *option) {
188 std::vector<TH1*> hists;
189 for (
int iFile = 0; iFile<files.size(); ++iFile) {
190 TFile *file =
new TFile(files[iFile].c_str());
191 TH1* hist = (TH1*)file->Get<TH1>(name.c_str());
192 hist->SetName(files[iFile].substr(0, files[iFile].size()-5).c_str());
193 hists.push_back(hist);
198 void StackSpectra(std::vector<int> display_indexes, TCanvas *canvas, Option_t *option) {
200 std::vector<TH1*> display_spectra;
201 for (
auto &i : display_indexes) {
202 display_spectra.push_back(spectra[i]);
207 void StackSpectra(
int iStart,
int iStop, TCanvas *canvas, Option_t *option) {
209 std::vector<TH1*> display_spectra;
210 for (
int i=iStart; i<=iStop; ++i) {
211 display_spectra.push_back(spectra[i]);
216 void StackSpectra(std::vector<TH1*> hists, TCanvas *canvas, Option_t *option) {
217 if (!canvas) {
if (gPad) { canvas = gPad->GetCanvas(); }
else { canvas =
new TCanvas(); } }
221 int num_hists = hists.size();
224 if (hists.size() < 2 ) { nx = 1; ny = 1; }
227 nx = (int)(std::sqrt((
double)num_hists));
228 ny = (int)((
double)(num_hists - 1)/(
double)(nx) + 1);
230 canvas->Divide(nx, ny, 0, 0);
232 for (
int i=0; i<num_hists; ++i) {
233 TH1 *hist = hists[i];
234 hist->GetXaxis()->UnZoom();
241 void StackSpectra(TH2 *hist,
int iStart,
int iStop, Option_t *option) {
242 std::vector<TH1*> hists;
243 for (
int i=iStart; i<=iStop; ++i) {
244 std::string name = (std::string)hist->GetName()+
"_px_"+std::to_string(i);
245 hists.push_back((TH1*)hist->ProjectionX(name.c_str(), i,i));
255 void StackSpectra(std::vector<std::string> files, std::string name,
int iX, Option_t *option) {
256 std::vector<TH1*> hists;
257 for (
int iFile = 0; iFile<files.size(); ++iFile) {
258 TFile *file =
new TFile(files[iFile].c_str());
259 TH2D* hist2 = (TH2D*)file->Get<TH2>(name.c_str());
260 TH1* hist = (TH1*)hist2->ProjectionX(files[iFile].substr(0, files[iFile].size()-5).c_str(), iX, iX);
261 hists.push_back(hist);
266 void StackSpectra(std::vector<std::string> files, std::string name, Option_t *option) {
267 std::vector<TH1*> hists;
268 for (
int iFile = 0; iFile<files.size(); ++iFile) {
269 TFile *file =
new TFile(files[iFile].c_str());
270 TH1* hist = (TH1*)file->Get<TH1>(name.c_str());
271 hist->SetName(files[iFile].substr(0, files[iFile].size()-5).c_str());
272 hists.push_back(hist);
283 int lowx = hist->GetXaxis()->GetFirst();
284 int highx = hist->GetXaxis()->GetLast();
285 int lowy = hist->GetYaxis()->GetFirst();
286 int highy = hist->GetYaxis()->GetLast();
288 double maxCont = hist->GetMaximum();
289 double minCont = hist->GetMinimum();
293 int nbins = std::min(1024, (
int)(maxCont-minCont));
294 std::vector<double> contsHist(nbins, 0);
296 for (
int i=lowx; i<=highx; ++i) {
297 for (
int j=lowy; j<=highy; ++j) {
298 double conts = hist->GetBinContent(i,j);
299 int bin = (int)((conts-minCont)/(maxCont-minCont)*(nbins-1) + 0.5);
300 if (bin < 0 || bin >= nbins) {
continue; }
301 contsHist[bin] += (1+conts*bias);
307 for (
int i=0; i<nbins; ++i) {
308 thresh += contsHist[i];
311 thresh /= (double)ncontours;
312 bool exceeded =
false;
318 for (
int i=0; i<nbins; ++i) {
319 if (contsHist[i] > thresh) {
320 contsHist[i] = thresh;
324 if (!exceeded) {
break; }
327 double levels[ncontours+1];
331 for (
int i=0; i<nbins; ++i) {
332 total += contsHist[i];
335 levels[0] = minCont-0.01*std::abs(minCont);
337 for (
int i=0; i<nbins; ++i) {
339 if (sum/total > (
double)
ct/(
double)ncontours) {
341 levels[
ct] = minCont + (double)i/((
double)nbins) * (maxCont - minCont);
346 levels[ncontours] = maxCont + 0.01*std::abs(maxCont);
348 hist->SetContour(ncontours+1, &levels[0]);
353 void ContourCalc(TVirtualPad *canvas,
int ncontours,
double bias) {
354 if (!canvas) {
if (!gPad) {
return; } canvas = gPad; }
364 for (
int i=0; i<nPads; ++i) {
373 if (!canvas) {
if (!gPad) {
return; } canvas = gPad; }
380 for (
int i=0; i<nPads; ++i) {
389 if (!canvas) {
if (!gPad) {
return; } canvas = gPad; }
397 for (
int i=0; i<nPads; ++i) {
405 void ZoomAllX(
double low,
double high, TVirtualPad *canvas) {
406 if (!canvas) {
if (!gPad) {
return; } canvas = gPad; }
413 hist->GetXaxis()->SetRangeUser(low, high);
417 hist2->GetXaxis()->SetRangeUser(low, high);
423 for (
int i=0; i<nPads; ++i) {
425 ZoomAllX(low, high, canvas->cd(i+1));
431 void ZoomAllY(
double low,
double high, TVirtualPad *canvas) {
432 if (!canvas) {
if (!gPad) {
return; } canvas = gPad; }
439 hist->GetYaxis()->SetRangeUser(low, high);
443 hist2->GetYaxis()->SetRangeUser(low, high);
449 for (
int i=0; i<nPads; ++i) {
451 ZoomAllY(low, high, canvas->cd(i+1));
458 if (!canvas) {
if (!gPad) {
return; } canvas = gPad; }
465 hist->GetXaxis()->UnZoom();
471 hist2->GetXaxis()->UnZoom();
477 for (
int i=0; i<nPads; ++i) {
486 if (!canvas) {
if (!gPad) {
return; } canvas = gPad; }
493 hist->GetYaxis()->UnZoom();
499 hist2->GetYaxis()->UnZoom();
505 for (
int i=0; i<nPads; ++i) {
514 if (!canvas) {
if (gPad) { canvas = gPad->GetCanvas(); }
else { std::cout <<
"No active canvas" << std::endl;
return; } }
516 TString opts(option);
520 for (
int i=0; i<spectra.size(); ++i) {
521 printf(
"%i %s\n", i, spectra[i]->GetName());
523 std::cout <<
"Enter normalising spectrum: ";
526 TH1D* norm_hist = spectra[ind];
527 if (opts.Contains(
"m")) {
528 for (
int i=0; i<spectra.size(); ++i) {
530 double scale = (double)norm_hist->GetMaximum()/(double)spectra[i]->GetMaximum();
531 spectra[i]->Scale(scale);
538 for (
int i=0; i<spectra.size(); ++i) {
540 gate.
Norm(spectra[i], norm_hist);
548 if (!canvas) {
if (gPad) { canvas = gPad->GetCanvas(); }
else { std::cout <<
"No active canvas" << std::endl;
return; } }
550 for (
int i=0; i<spectra.size(); ++i) {
551 printf(
"%i %s\n", i, spectra[i]->GetName());
553 std::cout <<
"Enter normalising spectrum: ";
556 TH1D* norm_hist = spectra[ind];
557 std::cout <<
"Set peak region: " << std::endl;
560 std::cout <<
"Set background region: " << std::endl;
563 for (
int i=0; i<spectra.size(); ++i) {
570 if (!canvas) {
if (gPad) { canvas = gPad->GetCanvas(); }
else { std::cout <<
"No active canvas" << std::endl;
return; } }
573 if (spectra.size() > 1) {
574 for (
int i=0; i<spectra.size(); ++i) {
575 printf(
"%i %s\n", i, spectra[i]->GetName());
577 std::cout <<
"Enter spectrum to rename: ";
580 TH1D* hist = spectra[ind];
581 std::cout <<
"Enter new name: ";
584 spectra[ind]->SetName(name.c_str());
589 if (!canvas) {
if (gPad) { canvas = gPad->GetCanvas(); }
else { std::cout <<
"No active canvas" << std::endl;
return; } }
591 canvas->SetCrosshair(1);
593 std::vector<std::string> messages = {
"Cursor: "};
594 int retval = click.
GetClicks(canvas, -1, messages, 0, 1);
597 canvas->SetCrosshair(0);
598 std::cout << std::endl;
std::pair< double, double > ct(TCanvas *canvas)
int GetClicks(TVirtualPad *canvas, int n, std::vector< std::string > &messages, int draw=0, int print=0)
void NormBackSub(TH1 *hist, TH1 *ref, Gate background)
void Norm(TH1 *hist, TH1 *ref)
std::vector< TH2 * > List2DSpectra(bool quiet)
void Draw2D(int i, Option_t *option)
void Draw(int i, Option_t *option)
std::vector< TH1 * > List1DSpectra(bool quiet)
void Rename(TVirtualPad *canvas)
void NormSpectra(TVirtualPad *canvas, Option_t *option)
void ContourCalc(TH2 *hist, int ncontours, double bias)
void Cursor(TVirtualPad *canvas)
void LinAll(TVirtualPad *canvas)
void UnZoomAllY(TVirtualPad *canvas)
void OverlaySpectra(std::vector< int > display_indexes, TCanvas *canvas, Option_t *option)
void LogAll(TVirtualPad *canvas)
void NormSpectraBackSub(TVirtualPad *canvas)
void StackSpectra(std::vector< int > display_indexes, TCanvas *canvas, Option_t *option)
void ZoomAllX(double low, double high, TVirtualPad *canvas)
void ZoomAllY(double low, double high, TVirtualPad *canvas)
void UnZoomAllX(TVirtualPad *canvas)
int GetNPads(TVirtualPad *pad)
TH1D * GetHist1D(TVirtualPad *canvas)
std::vector< TH1D * > GetHists1D(TVirtualPad *canvas)
TH2D * GetHist2D(TVirtualPad *canvas)