File indexing completed on 2025-08-05 08:14:57
0001 #include <TFile.h>
0002 #include <TH1.h>
0003 #include <TCanvas.h>
0004 #include <TLegend.h>
0005 #include <vector>
0006 #include <sPhenixStyle.C>
0007 #include <sPhenixStyle.h>
0008
0009 void PlotChannelExtrema(const char* inputFile = "/sphenix/user/ecroft/channel_analysis_zs20.root",
0010 const char* outputFile = "/sphenix/user/ecroft/channel_extrema_plots_zs20.pdf") {
0011
0012 TFile* f = TFile::Open(inputFile, "READ");
0013 if (!f || f->IsZombie()) {
0014 std::cerr << "Error opening input file: " << inputFile << std::endl;
0015 return;
0016 }
0017
0018
0019 TCanvas canvas("canvas", "Channel Extrema", 800, 600);
0020 canvas.Print(Form("%s[", outputFile));
0021
0022 const int numChannels = 744;
0023 TLegend legend(0.7, 0.7, 0.9, 0.9);
0024
0025 for (int ch = 0; ch < numChannels; ch++) {
0026
0027 TH1F* hmax = dynamic_cast<TH1F*>(f->Get(Form("ch%d_max_hist", ch)));
0028 TH1F* hmin = dynamic_cast<TH1F*>(f->Get(Form("ch%d_min_hist", ch)));
0029 TF1* fmax = dynamic_cast<TF1*>(f->Get(Form("ch%d_max_fit", ch)));
0030 TF1* fmin = dynamic_cast<TF1*>(f->Get(Form("ch%d_min_fit", ch)));
0031
0032
0033 if (!hmax || !hmin) {
0034 std::cout << "Skipping channel " << ch << " — missing hmax or hmin.\n";
0035 continue;
0036 }
0037 if (!fmin || ! fmax) {
0038 std::cout << "Skipping channel " << ch << " — missing fmin or fmax.\n";
0039 continue;
0040 }
0041
0042 hmax->SetLineColor(kRed);
0043 hmax->SetLineWidth(2);
0044 hmax->SetTitle(Form("Channel %d Extreme MPV Histograms;ADC;Counts", ch));
0045
0046 hmin->SetLineColor(kBlue);
0047 hmin->SetLineWidth(2);
0048 hmin->SetLineStyle(2);
0049
0050 fmax->SetLineColor(kGreen);
0051 fmin->SetLineColor(kMagenta);
0052
0053
0054 double ymax = std::max(hmax->GetMaximum(), hmin->GetMaximum());
0055 hmax->SetMaximum(ymax * 1.1);
0056
0057
0058 canvas.cd();
0059 hmax->Draw("HIST");
0060 hmin->Draw("HIST SAME");
0061 fmax->Draw("SAME");
0062 fmin->Draw("SAME");
0063
0064
0065 legend.Clear();
0066 legend.AddEntry(hmax, "Max MPV", "L");
0067 legend.AddEntry(hmin, "Min MPV", "L");
0068 legend.AddEntry(fmax, "Max MPV fit", "L");
0069 legend.AddEntry(fmin, "Min MPV fit", "L");
0070 legend.Draw();
0071
0072
0073 canvas.Print(outputFile);
0074
0075
0076 if (ch % 50 == 0) {
0077 std::cout << "Processed channel " << ch << "/" << numChannels-1
0078 << " (" << (ch*100/numChannels) << "%)\n";
0079 }
0080 }
0081
0082
0083 canvas.Print(Form("%s]", outputFile));
0084 f->Close();
0085
0086 std::cout << "Saved plots to: " << outputFile << std::endl;
0087 }