Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2026-07-16 08:16:43

0001 // This code creates a 1D distribution of temperature vs runnumber/runid by averaging the temperatures of the towers in the root file made by Calotemp macro. 
0002 // how to run: root -l makeAvgTempVsRun.C
0003 // You have to edit the input file directory, file names of the input an output accordingly. This macro by default analyzes the case of inner hcal. 
0004 
0005 #include <TFile.h>
0006 #include <TProfile2D.h>
0007 #include <TH1.h>
0008 #include <TGraph.h>
0009 #include <TCanvas.h>
0010 #include <TLegend.h>
0011 #include <TStyle.h>
0012 
0013 #include <fstream>
0014 #include <format>
0015 #include <iostream>
0016 #include <sstream>
0017 #include <string>
0018 #include <vector>
0019 
0020 void makeAvgTempVsRun(
0021   const char* dir = "/sphenix/user/anjsmenon/work_2025/macros/calibrations/calo/final_24_hcalcalib/makeTemp/temp_ihcal_pp_2024",
0022   //const char* dir = "/sphenix/user/anjsmenon/work_2025/macros/calibrations/calo/final_24_hcalcalib/condor_histmaker/makenewTemp/temp_ohcal_pp_2024",
0023   const char* runlist_path = "Full_ppGoldenRunList_Version3.list", // one run per line
0024   const char* outroot = "avgTemp_HCALIN.root",
0025   const char* outcsv  = "avgTemp_HCALIN.csv",
0026   double Tmin = 10.0, double Tmax = 30.0)
0027 {
0028   std::ifstream rin(runlist_path);
0029   if (!rin.is_open()) { std::cerr << "Cannot open runlist: " << runlist_path << std::endl; return; }
0030 
0031   std::vector<int> runs;
0032   for (std::string line; std::getline(rin, line); ) {
0033     if (line.empty()) continue;
0034     runs.push_back(std::stoi(line));
0035   }
0036   if (runs.empty()) { std::cerr << "Run list is empty.\n"; return; }
0037 
0038   TH1D* h_avg_vs_idx = new TH1D("h_avg_vs_idx","iHCal Avg Temperature per Run;run index;avg T [^{o}C]",
0039                                 runs.size(), 0.5, runs.size()+0.5);
0040   for (size_t i=0;i<runs.size();++i) h_avg_vs_idx->GetXaxis()->SetBinLabel(i+1, std::format("{}", runs[i]).c_str());
0041   h_avg_vs_idx->SetMarkerStyle(20);
0042 
0043   std::vector<double> x_run(runs.size());
0044   std::vector<double> y_avg(runs.size());
0045 
0046   std::ofstream csv(outcsv);
0047   csv << "run,avg_temp_C,ntowers_used\n";
0048 
0049   size_t idx = 0;
0050   for (int run : runs)
0051   {
0052     std::string fname = std::format("{}/HCALIN_temp_{}.root", dir, run);
0053     TFile f(fname.c_str(), "READ");
0054     if (f.IsZombie()) { 
0055       std::cerr << "[skip] cannot open " << fname << "\n";
0056       ++idx; continue;
0057     }
0058 
0059     auto *prof = dynamic_cast<TProfile2D*>(f.Get("h_HCALIN_temp"));
0060     if (!prof) {
0061       std::cerr << "[skip] missing h_HCALIN_temp in " << fname << "\n";
0062       ++idx; continue;
0063     }
0064 
0065     const int nx = prof->GetNbinsX();
0066     const int ny = prof->GetNbinsY();
0067     double sum = 0.0;
0068     int nused = 0;
0069 
0070     for (int ix=1; ix<=nx; ++ix) {
0071       for (int iy=1; iy<=ny; ++iy) {
0072         const int bin = prof->GetBin(ix,iy);
0073         // Only consider bins with entries
0074         if (prof->GetBinEntries(bin) <= 0) continue;
0075         const double val = prof->GetBinContent(bin);
0076         if (val < Tmin || val > Tmax) continue;
0077         sum += val;
0078         ++nused;
0079       }
0080     }
0081 
0082     const double avg = (nused>0) ? sum / nused : 0.0;
0083 
0084     // Fill outputs
0085     h_avg_vs_idx->SetBinContent(idx+1, avg);
0086     x_run[idx] = run;
0087     y_avg[idx] = avg;
0088     csv << run << "," << avg << "," << nused << "\n";
0089 
0090     ++idx;
0091   }
0092   csv.close();
0093 
0094   auto *gr = new TGraph((int)runs.size(), x_run.data(), y_avg.data());
0095   gr->SetName("g_avg_vs_run");
0096   gr->SetTitle("IHCal Avg Temperature per Run;run number;avg T [^{o}C]");
0097   gr->SetMarkerStyle(20);
0098 
0099   gStyle->SetOptStat(0);
0100   TCanvas* c1 = new TCanvas("c_avg_idx","Avg T vs run index",1200,500);
0101   h_avg_vs_idx->Draw("P");
0102   c1->SetBottomMargin(0.20); 
0103   //c1->SaveAs("avgTemp_vs_runIndex_ihcal.png");
0104 
0105   // as long as the canvas is not referenced - we do not need to assign a variable to it
0106   new TCanvas("c_avg_run","Avg T vs run number",900,600);
0107   gr->Draw("AP");
0108   //c2->SaveAs("avgTemp_vs_runNumber_ihcal.png");
0109 
0110   // Save to ROOT file
0111   TFile outf(outroot, "RECREATE");
0112   h_avg_vs_idx->Write();
0113   gr->Write();
0114   outf.Close();
0115 
0116   std::cout << "Done. Plots: avgTemp_vs_runIndex.png, avgTemp_vs_runNumber.png\n"
0117             << "ROOT: " << outroot << "\nCSV: " << outcsv << std::endl;
0118 }
0119