Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2026-04-05 08:08:11

0001 #include "/sphenix/u/virgilemahaut/style/sPhenixStyle_Greg.C"
0002 
0003 #include <TCanvas.h>
0004 #include <TGraphErrors.h>
0005 #include <TLegend.h>
0006 #include <TAxis.h>
0007 #include <TStyle.h>
0008 
0009 #include <fstream>
0010 #include <sstream>
0011 #include <string>
0012 #include <vector>
0013 #include <algorithm>
0014 #include <iostream>
0015 
0016 double GetAbsMaxInRange(TGraphErrors *graph, double xMin, double xMax)
0017 {
0018   if (!graph ) return -1; // Error handling
0019 
0020   double maxVal = -1;
0021   int n = graph->GetN();
0022 
0023   for (int i = 0; i < n; i++)
0024   {
0025     double x, y, ey, yVal;
0026     graph->GetPoint(i, x, y);
0027     ey = graph->GetErrorY(i);
0028     yVal = std::max(std::abs(y-ey), std::abs(y+ey));
0029     if (x >= xMin && x <= xMax)
0030     {
0031       if (yVal > maxVal)
0032       {
0033         maxVal = yVal;
0034       }
0035     }
0036   }
0037   return maxVal;
0038 }
0039 
0040 
0041 void parse_file(const std::string& filename,
0042                 std::vector<double>& x,
0043                 std::vector<double>& y,
0044                 std::vector<double>& estat,
0045                 std::vector<double>& esyst,
0046                 std::vector<double>& exsyst,
0047                 const double width = 0.15
0048                 )
0049 {  
0050   std::ifstream fin(filename);
0051   if (!fin.is_open()) {
0052     std::cerr << "Cannot open file: " << filename << std::endl;
0053     return;
0054   }
0055 
0056   std::string line;
0057   while (std::getline(fin, line)) {
0058     // Skip empty lines
0059     if (line.empty()) continue;
0060 
0061     // Replace '&' with spaces so stringstream can parse numbers
0062     std::replace(line.begin(), line.end(), '&', ' ');
0063 
0064     std::stringstream ss(line);
0065     double xv, yv, statv, systv;
0066     if (!(ss >> xv >> yv >> statv >> systv)) {
0067       std::cerr << "Skipping malformed line: " << line << std::endl;
0068       continue;
0069     }
0070 
0071     x.push_back(xv);
0072     y.push_back(yv);
0073     estat.push_back(statv);
0074     esyst.push_back(systv);
0075 
0076     // Constant half-width for the systematic boxes in x.
0077     // Choose something visually reasonable for your spacing. 
0078     exsyst.push_back(width);
0079   }
0080 }
0081 
0082 void CreateForwardPlot()
0083 {
0084   std::string plots_folder_pt = "pt_asymmetries/SYSTEMATIC/";
0085 
0086   gSystem->Exec(("mkdir -p " + plots_folder_pt).c_str());
0087   
0088   const std::string inputfolder = "asym_values/";
0089 
0090   float pTBounds[2] = {-0.010, 0.020};
0091   
0092   std::stringstream inputfilename_low;
0093   inputfilename_low << inputfolder << "asym_summary_pi0_pT_low_xf" << ".txt";
0094   std::stringstream inputfilename_high;
0095   inputfilename_high << inputfolder << "asym_summary_pi0_pT_high_xf" << ".txt";
0096   std::stringstream canvas_name;
0097   canvas_name << "canvas_asym_pi0_pT_vs_xF";
0098   std::vector<double> x_low, y_low, estat_low, esyst_low, exsyst_low;
0099   std::vector<double> x_high, y_high, estat_high, esyst_high, exsyst_high;
0100 
0101   parse_file(inputfilename_low.str(), x_low, y_low, estat_low, esyst_low, exsyst_low, 0.12);
0102   parse_file(inputfilename_high.str(), x_high, y_high, estat_high, esyst_high, exsyst_high, 0.12);
0103   
0104   const int n = x_low.size();
0105   auto gSyst_low = new TGraphErrors(n);
0106   auto gStat_low = new TGraphErrors(n);
0107   auto gSyst_high = new TGraphErrors(n);
0108   auto gStat_high = new TGraphErrors(n);
0109 
0110   for (int i = 0; i < n; ++i) {
0111     gSyst_low->SetPoint(i, x_low[i], y_low[i]);
0112     gSyst_low->SetPointError(i, exsyst_low[i], esyst_low[i]);
0113 
0114     gStat_low->SetPoint(i, x_low[i], y_low[i]);
0115     gStat_low->SetPointError(i, 0.0, estat_low[i]);
0116   }
0117 
0118   // Skip first bin (too little stat for large xF)
0119   for (int i = 1; i < n; ++i) {
0120     gSyst_high->SetPoint(i-1, x_high[i]+0.1, y_high[i]);
0121     gSyst_high->SetPointError(i-1, exsyst_high[i], esyst_high[i]);
0122 
0123     gStat_high->SetPoint(i-1, x_high[i]+0.1, y_high[i]);
0124     gStat_high->SetPointError(i-1, 0.0, estat_high[i]);
0125   }
0126   
0127   SetsPhenixStyle();
0128 
0129   TCanvas *canvas = new TCanvas(canvas_name.str().c_str(), "", 1600, 900);
0130   gStat_low->SetTitle(";p_{T} [GeV];A_{N}");
0131 
0132   double y_bound = 0;
0133   y_bound = GetAbsMaxInRange(gStat_high, 1.5, 10);
0134   
0135   gStat_low->SetLineWidth(2);
0136   gStat_low->SetLineColor(kGray+2);
0137   gStat_low->SetMarkerColor(kGray+2);
0138   gStat_low->SetMarkerStyle(59);
0139   gStat_low->SetMarkerSize(2.6);
0140   // gStat_low->SetMinimum(-2 * y_bound);
0141   // gStat_low->SetMaximum(2 * y_bound);
0142   gStat_low->SetMinimum(pTBounds[0]);
0143   gStat_low->SetMaximum(pTBounds[1]);
0144   gStat_low->Draw("AP E1");
0145 
0146   gStat_high->SetLineWidth(2);
0147   gStat_high->SetLineColor(kRed);
0148   gStat_high->SetMarkerColor(kRed);
0149   gStat_high->SetMarkerStyle(kFullSquare);
0150   gStat_high->SetMarkerSize(2.4);
0151   // gStat_high->SetMinimum(-2 * y_bound);
0152   // gStat_high->SetMaximum(2 * y_bound);
0153   gStat_high->SetMinimum(pTBounds[0]);
0154   gStat_high->SetMaximum(pTBounds[1]);
0155   gStat_high->Draw("P E1 SAME");
0156 
0157   // Style systematic boxes (transparent fill)
0158   gSyst_low->SetFillColorAlpha(kGray, 0.65);
0159   gSyst_low->SetLineColor(kGray);
0160   gSyst_high->SetFillColorAlpha(kPink-9, 0.45);
0161   gSyst_high->SetLineColor(kPink-9);
0162 
0163   gSyst_low->SetMarkerStyle(0);
0164   gSyst_low->Draw("P E2 SAME");
0165   gSyst_high->SetMarkerStyle(0);
0166   gSyst_high->Draw("P E2 SAME");
0167 
0168   gStat_low->GetXaxis()->SetLimits(1.0, 10.0);
0169   
0170   TLegend *legend = new TLegend(0.22, 0.54, 0.38, 0.68);
0171   legend->SetFillColor(kWhite);
0172   legend->SetTextSize(0.05);
0173   legend->AddEntry(gStat_high, "x_{F} > 0.035");
0174   legend->AddEntry(gStat_low, "x_{F} < 0.035");
0175   legend->Draw();
0176 
0177   // TLegend *legend = new TLegend(0.62, 0.75, 0.88, 0.88);
0178   // legend->SetBorderSize(0);
0179   // legend->SetFillStyle(0);
0180 
0181   gPad->Modified();
0182   gPad->Update();
0183   double min_x = gPad->GetUxmin();
0184   double max_x = gPad->GetUxmax();
0185 
0186   // Add "sPHENIX internal"
0187   TPad *p = new TPad("p","p",0.,0.,1.,1.); p->SetFillStyle(0); p->Draw(); p->cd();
0188   TBox *whiteBox = new TBox(0.185, 0.72, 0.49, 0.90);
0189   whiteBox->Draw();
0190   canvas->cd();
0191   whiteBox->SetFillColorAlpha(kWhite, 1);
0192   std::stringstream stream;
0193   stream.str("");
0194   TLatex latex;
0195   latex.SetNDC();
0196   latex.SetTextColor(kBlack);
0197   latex.DrawLatex(0.22, 0.85, "#font[72]{sPHENIX} Internal");
0198   latex.DrawLatex(0.22, 0.79, "p^{#uparrow}+p #sqrt{s} = 200 GeV");
0199   latex.SetTextSize(0.03);
0200   latex.DrawLatex(0.19, 0.73, "7% polarization scale uncertainty not shown");
0201 
0202   latex.SetTextSize(0.07);
0203   latex.DrawLatex(0.57, 0.84, "p^{#uparrow}+p #rightarrow #pi^{0} X");
0204 
0205   TLine *tline = new TLine();
0206   tline->SetLineWidth(2);
0207   tline->SetLineColor(kGray+2);
0208   tline->SetLineStyle(kDashed);
0209   tline->DrawLine(min_x, 0, max_x, 0);
0210 
0211   canvas->Update();
0212   canvas->Draw();
0213   canvas->SaveAs((plots_folder_pt +  "/" + canvas_name.str() + ".png").c_str());
0214   canvas->SaveAs((plots_folder_pt +  "/" + canvas_name.str() + ".pdf").c_str());
0215   canvas->SaveAs((plots_folder_pt +  "/" + canvas_name.str() + ".C").c_str());
0216   gSystem->Exit(0);
0217 }