#include #include #include #include #include #include #include "TH2.h" #include "TH1.h" #include "TRandom.h" #include #include #include "TObjArray.h" #include "TF1.h" #include "TSpectrum.h" #include "TVirtualFitter.h" #include "Riostream.h" void DrawLinearHistogram() { // Retrieving the histogram: TFile *file = new TFile("out_gate_2270_2570_2070_2170_2670_2770.root"); // Create a new canvas for the linear plot TCanvas* c1 = new TCanvas("c1", "64Ni Coincidence Analysis", 10,10,800,800); // Create a new histogram with linear binning TH1D * gate = new TH1D("gate", "gate", 4096, -2.0, 16382.0); TH1D * gated = new TH1D("gated", "gated", 4096, -2.0, 16382.0); TH1D * avg_bg = new TH1D("avg_bg", "avg_bg", 4096, -2.0, 16382.0); TH1D * gated_bgs = new TH1D("gated_bgs", "gated_bgs", 4096, -2.0, 16382.0); TH1D * background_gate = new TH1D("background_gate", "background_gate", 4096, -2.0, 16382.0); TH1D * background_gate_2 = new TH1D("background_gate_2", "background_gate_2", 4096, -2.0, 16382.0); TH1D * gated_cal = new TH1D("gated_cal", "gated_cal", 4096, -2.0, 16382.0); // calibrated spectrum TH1D *linearHist = nullptr; // Declare the variable outside the if statement // Copy the contents of the original histogram to the gate histogram: linearHist = (TH1D*)file->Get("gate"); if (linearHist != nullptr) { for (int i = 1; i <= linearHist->GetNbinsX(); i++) { double binContent = linearHist->GetBinContent(i); double binCenter = linearHist->GetBinCenter(i); std::cout << "Number of entries in linearHist: " << linearHist->GetEntries() << std::endl; gate->Fill(binCenter, binContent); } } else { // Handle the case when linearHist is null // You can leave this block empty or add appropriate error handling } // Placing in the gated spectra: c1->Divide(2,2); c1->cd(1); gated->Add(gate,1); gated->Draw(); // Placing the background regions into the canvas then subtract the gated spectrum from the avg_bg (scale factor determined in hdtv). Adding the background spectra (pad2): c1->cd(2); avg_bg->Add(background_gate, 0.5); avg_bg->Add(background_gate_2, 0.5); avg_bg->Draw(); // Subtracting gate from avg_bg into pad3: c1->cd(3); gated_bgs->Add(gate, 1); // Scaling factors to normalize the avg_bg: // new_labr_labr scaling: 1.693 (1), 2.281 (2), 2.848 (3), 2.983 (5), 2.906 (6), 2.923 (7), 2.712 (8), // projx scaling: 1345 -> , 2276 -> 1.7, 2610 -> 1.25, 2867 -> 1.453, 3025 -> 1.792, 3463 -> 1.406, 7346 -> 1.09 gated_bgs->Add(avg_bg, -1); gated_bgs->Add(avg_bg, -1.7); gated_bgs->Draw(); // Accessing linearHist here: if (linearHist != nullptr && linearHist->GetXaxis() != nullptr) { std::cout << "Number of entries in linearHist: " << linearHist->GetEntries() << std::endl; linearHist->GetXaxis()->SetTitle(linearHist->GetXaxis()->GetTitle()); linearHist->GetYaxis()->SetTitle(linearHist->GetYaxis()->GetTitle()); } // Plot the gate histogram in linear c1->cd(4); if (linearHist != nullptr && gate != nullptr) { linearHist->Add(gate, 1); linearHist->Add(avg_bg, -1); linearHist->Add(avg_bg, -1.7); linearHist->Draw(); } }