Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-05 08:14:56

0001 #include <TFile.h>
0002 #include <TTree.h>
0003 #include <TGraph.h>
0004 #include <TCanvas.h>
0005 #include <vector>
0006 #include <string>
0007 #include <sPhenixStyle.C>
0008 #include <sPhenixStyle.h>
0009 
0010 void plot_channel_mpvs(const char* input_filename = "/sphenix/user/ecroft/channel_analysis_zs20.root", 
0011                       const char* output_filename = "/sphenix/user/ecroft/channel_mpv_plots_zs20.pdf") {
0012     // Open input file
0013     TFile* input_file = TFile::Open(input_filename, "READ");
0014     if (!input_file || input_file->IsZombie()) {
0015         std::cerr << "Error opening input file: " << input_filename << std::endl;
0016         return;
0017     }
0018 
0019     // Get the TTree
0020     TTree* tree = dynamic_cast<TTree*>(input_file->Get("rundata"));
0021     if (!tree) {
0022         std::cerr << "Error: Could not find 'rundata' tree in input file" << std::endl;
0023         input_file->Close();
0024         return;
0025     }
0026 
0027     // Set up branch addresses
0028     int run_number;
0029     std::vector<float> *mpv_values = 0;
0030     tree->SetBranchAddress("run", &run_number);
0031     tree->SetBranchAddress("mpv_values", &mpv_values);
0032 
0033     // Data storage: vector per channel containing (run, mpv) pairs
0034     const int num_channels = 744;
0035     std::vector<std::vector<std::pair<int, float>>> channel_data(num_channels);
0036 
0037     // Read all data
0038     const Long64_t n_entries = tree->GetEntries();
0039     for (Long64_t i = 0; i < n_entries; i++) {
0040         tree->GetEntry(i);
0041         
0042         // Skip bad runs
0043         if (run_number < 0) continue;
0044 
0045         // Store data for each channel
0046         for (int ch = 0; ch < num_channels; ch++) {
0047             if (mpv_values->at(ch) > 0) { // Skip invalid MPVs
0048                 channel_data[ch].emplace_back(run_number, mpv_values->at(ch));
0049             }
0050         }
0051 
0052         // Progress update
0053         if (i % 100 == 0) {
0054             std::cout << "\rProcessed " << i << "/" << n_entries 
0055                       << " runs (" << (i*100/n_entries) << "%)" << std::flush;
0056         }
0057     }
0058     std::cout << "\nFinished processing " << n_entries << " runs" << std::endl;
0059 
0060 
0061     TCanvas canvas("canvas", "Channel MPV Plots", 1200, 800);
0062     canvas.Print(Form("%s[", output_filename)); // Open multi-page PDF
0063 
0064     for (int ch = 0; ch < num_channels; ch++) {
0065         const auto& data = channel_data[ch];
0066         if (data.empty()) continue;
0067 
0068  
0069         std::vector<double> runs, mpvs;
0070         for (const auto& point : data) {
0071             runs.push_back(point.first);
0072             mpvs.push_back(point.second);
0073         }
0074 
0075         TGraph graph(runs.size(), runs.data(), mpvs.data());
0076         graph.SetTitle(Form("Channel %d MPV History;Run Number;MPV [ADC]", ch));
0077         graph.SetMarkerStyle(20);
0078         graph.SetMarkerSize(0.8);
0079         graph.SetMarkerColor(kBlue);
0080 
0081       
0082         canvas.Clear();
0083         graph.Draw("AP");
0084         canvas.Update();
0085 
0086         // Add to PDF
0087         canvas.Print(output_filename);
0088 
0089         // Clean up
0090         if (ch % 50 == 0) {
0091             std::cout << "Plotted " << ch << "/" << num_channels 
0092                       << " channels (" << (ch*100/num_channels) << "%)" << std::endl;
0093         }
0094     }
0095 
0096     // Close PDF
0097     canvas.Print(Form("%s]", output_filename));
0098 
0099     // Cleanup
0100     input_file->Close();
0101     delete input_file;
0102 
0103     std::cout << "Saved plots to: " << output_filename << std::endl;
0104 }