File indexing completed on 2025-08-05 08:14:56
0001 #include <TFile.h>
0002 #include <TDirectory.h>
0003 #include <TCanvas.h>
0004 #include <TLatex.h>
0005 #include <RooPlot.h>
0006 #include <TVectorD.h>
0007
0008 void plot_all_fits(const char* input_file = "/sphenix/tg/tg01/bulk/ecroft/sEPD/analysis_output/run_51154_analysis.root",
0009 const char* output_file = "run_51154_fits.pdf") {
0010 TFile* file = TFile::Open(input_file, "READ");
0011 if (!file || file->IsZombie()) {
0012 std::cerr << "Error opening file: " << input_file << std::endl;
0013 return;
0014 }
0015
0016 TCanvas* c = new TCanvas("c", "Fit Results", 1200, 800);
0017
0018 c->Print(Form("%s[", output_file));
0019
0020 std::regex run_pattern("run_(\\d+)");
0021
0022 TIter next(file->GetListOfKeys());
0023 TKey* key;
0024 while ((key = (TKey*)next())) {
0025 if (TString(key->GetClassName()) != "TDirectoryFile") continue;
0026
0027 TDirectory* run_dir = (TDirectory*)key->ReadObj();
0028 std::string dir_name = run_dir->GetName();
0029
0030 std::smatch match;
0031 int run_number = -1;
0032 if (std::regex_search(dir_name, match, run_pattern)) {
0033 run_number = std::stoi(match[1].str());
0034 }
0035
0036 TIter ch_next(run_dir->GetListOfKeys());
0037 TKey* ch_key;
0038 while ((ch_key = (TKey*)ch_next())) {
0039 if (!TString(ch_key->GetName()).BeginsWith("ch_")) continue;
0040
0041 TDirectory* ch_dir = (TDirectory*)ch_key->ReadObj();
0042 ch_dir->cd();
0043
0044 RooPlot* plot = (RooPlot*)gDirectory->Get("fit_plot");
0045 TVectorD* params = (TVectorD*)gDirectory->Get("fit_parameters");
0046 TH1F* hist = (TH1F*)gDirectory->Get("original_hist");
0047
0048 if (!plot || !params || !hist) continue;
0049
0050 c->Clear();
0051 c->Divide(1,2);
0052
0053 c->cd(1);
0054 hist->SetStats(0);
0055 hist->SetLineColor(kBlack);
0056 hist->SetTitle(Form("Run %d - Channel %s (Raw Spectrum)",
0057 run_number, ch_key->GetName()));
0058 hist->Draw();
0059
0060 c->cd(2);
0061 plot->SetTitle(Form("Run %d - Channel %s (Fit Result)",
0062 run_number, ch_key->GetName()));
0063 plot->Draw();
0064
0065 TLatex tex;
0066 tex.SetNDC();
0067 tex.SetTextSize(0.04);
0068
0069 c->cd(2);
0070 tex.DrawLatex(0.65, 0.80, Form("MPV = %.1f", (*params)[0]));
0071 tex.DrawLatex(0.65, 0.75, Form("#sigma_{Gauss} = %.1f", (*params)[1]));
0072 tex.DrawLatex(0.65, 0.70, Form("Entries = %d", hist->GetEntries()));
0073
0074
0075 c->Print(output_file);
0076 }
0077 }
0078
0079
0080 c->Print(Form("%s]", output_file));
0081 file->Close();
0082 delete c;
0083 }