Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2026-07-16 08:11:27

0001 template <typename T>
0002 T *getObjectFromFile(const std::string& filename, const std::string& objectname)
0003 {
0004     TFile file(filename.c_str(), "READ");
0005     if (file.IsZombie())
0006     {
0007         std::cerr << "Error opening file: " << filename << std::endl;
0008         return nullptr;
0009     }
0010 
0011     T* obj = dynamic_cast<T*>(file.Get(objectname.c_str()));
0012     if (!obj)
0013     {
0014         std::cerr << "Error retrieving object: " << objectname << " from file: " << filename << std::endl;
0015         file.Close();
0016         return nullptr;
0017     }
0018 
0019     // Clone the object to keep it in memory after closing the file
0020     T* obj_clone = dynamic_cast<T*>(obj->Clone());
0021     obj_clone->SetDirectory(nullptr); // Detach from any file directory
0022     file.Close();
0023     return obj_clone;
0024 }
0025 
0026 void draw1Dhistogram(TH1 *hist, bool logx, bool logy, std::string xtitle, std::string ytitle, std::vector<std::string> addinfo, std::string filename)
0027 {
0028     TCanvas *c = new TCanvas("c", "c", 800, 700);
0029     gPad->SetTopMargin(0.07);
0030     c->cd();
0031     c->SetLogx(logx);
0032     c->SetLogy(logy);
0033     hist->GetXaxis()->SetTitle(xtitle.c_str());
0034     hist->GetYaxis()->SetTitle(ytitle.c_str());
0035     hist->GetYaxis()->SetTitleOffset(1.6);
0036     hist->GetYaxis()->SetRangeUser((logy) ? hist->GetMinimum(0) * 0.5 : 0, (logy) ? hist->GetMaximum() * 50 : hist->GetMaximum() * 1.4);
0037     hist->Draw("e1");
0038 
0039     TLatex *latex = new TLatex();
0040     latex->SetTextSize(0.04);
0041     latex->SetTextAlign(12);
0042     latex->SetNDC();
0043     for (size_t i = 0; i < addinfo.size(); ++i)
0044     {
0045         latex->DrawLatex(0.2, 0.88 - i * 0.05, addinfo[i].c_str());
0046     }
0047     c->SaveAs(Form("%s.png", filename.c_str()));
0048     c->SaveAs(Form("%s.pdf", filename.c_str()));
0049     delete latex;
0050     delete c;
0051 }
0052 
0053 void KshortLambdaRawRatio()
0054 {
0055     const std::string kshortFile = "./figure_kshort_crosscheck/kshort_yield_pt.root";
0056     const std::string lambdaFile = "./figure_lambda_crosscheck/lambda_yield_pt.root";
0057 
0058     TH1* hKshort = getObjectFromFile<TH1>(kshortFile, "hkshortRawYieldPt");
0059     TH1* hLambda = getObjectFromFile<TH1>(lambdaFile, "hlambdaRawYieldPt");
0060 
0061     if (!hKshort || !hLambda)
0062     {
0063         std::cerr << "Error: Failed to retrieve histograms from files." << std::endl;
0064         return;
0065     }
0066 
0067     // scale kshort yield by 2
0068     hKshort->Scale(2.0);
0069 
0070     TH1* hRatio = dynamic_cast<TH1*>(hLambda->Clone("hRatio"));
0071     hRatio->SetTitle("; p_{T} [GeV]; #Lambda^{0}/2K_{S}^{0}");
0072     hRatio->Divide(hKshort);
0073 
0074     draw1Dhistogram(hRatio, false, false, "p_{T} [GeV]", "Raw yield ratio #Lambda^{0}/2K_{S}^{0}", {}, "RawYield_LFRatio");
0075 }