Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-06 08:12:39

0001 #include "sPhenixStyle.C"
0002 
0003 int MakePlot_TCanvas()
0004 {
0005     std::string input_file_directory = "/sphenix/tg/tg01/commissioning/INTT/work/cwshih/seflgendata/run_54280_HR_Dec042024/completed/Run3/EvtVtxZ/TrackHist/completed/dNdEta/dNdEta_AllSensor_GeoAccCorr_VtxZ10_Mbin70"; 
0006     std::string input_file_name = "DataMc2Comp.root";
0007     std::string target_plot_name = "h1D_RotatedBkg_RecoTrackletEtaPerEvt";
0008     std::string output_directory = input_file_directory;
0009     std::string output_file_name = "h1D_RotatedBkg_RecoTrackletEtaPerEvt";
0010     std::pair<std::string, std::string> axes_label = {"Tracklet #eta", "Avg. Reco. Tracklets per event (/0.2)"};
0011     std::string sPH_label = "Internal";
0012     std::vector<std::tuple<double,double,std::string>> additional_text = {
0013         // {0.2, 0.9, "The cluster pairs post the VtxZ linking requirement are filled"},
0014 
0015         {0.22, 0.9, "Au+Au 200 GeV"},
0016         // {0.22, 0.86, "HIJING"},
0017         {0.22, 0.86, "Centrality 0-70%"},
0018         {0.22, 0.82, "|INTT vtxZ| #leq 10 cm"}
0019 
0020         // {0.22, 0.90, "Inner clusters rotated by #pi in #phi angle"},
0021         // {0.22, 0.86 + 0.04, "#eta: [0.5 - 0.7]"},
0022         // {0.22, 0.82 + 0.04, "Centrality 0-70%"},
0023         // {0.22, 0.78 + 0.04, "|INTT vtxZ| #leq 10 cm"}
0024 
0025     };
0026 
0027     // std::vector<std::tuple<int, std::string,std::string>> legend_text = {
0028     //     {4, "Nominal", "l"},
0029     //     {2, "Rotated", "l"},
0030     //     {3, "Subtracted", "l"}
0031     // };
0032 
0033     std::vector<std::tuple<int, std::string, std::string>> legend_text = {
0034         {1, "Data", "pl"},
0035         {2, "HIJING", "l"}
0036     };
0037 
0038     bool isSetLogY = false;
0039     bool isSetLogZ = false;
0040     double y_min = 0;
0041     double y_max = 350;
0042     bool set_X_505 = true;
0043 
0044     system(Form("mkdir -p %s", output_directory.c_str()));
0045     
0046     SetsPhenixStyle();
0047 
0048     TLegend * leg = new TLegend(0.22,0.68,0.42,0.77);
0049     // leg -> SetNColumns(2);
0050     leg -> SetBorderSize(0);
0051     leg -> SetTextSize(0.025);
0052     leg -> SetMargin(0.2);
0053 
0054     // TCanvas * c1 = new TCanvas("c1", "c1", 950, 800);
0055 
0056     TLatex * ltx = new TLatex();
0057     ltx->SetNDC();
0058     ltx->SetTextSize(0.045);
0059     ltx->SetTextAlign(31);
0060 
0061     TLatex * draw_text = new TLatex();
0062     draw_text -> SetNDC();
0063     draw_text -> SetTextSize(0.03);
0064 
0065     TFile * file_in = TFile::Open(Form("%s/%s", input_file_directory.c_str(), input_file_name.c_str()));
0066     if (!file_in){
0067         std::cout<<"Error: cannot open file: "<<input_file_name<<std::endl;
0068         return 1;
0069     }
0070 
0071     gStyle->SetCanvasDefH(950);
0072     gStyle->SetCanvasDefH(800);
0073 
0074     TCanvas * c1 = (TCanvas*)file_in->Get(Form("%s",target_plot_name.c_str()));    
0075     if (!c1){
0076         std::cout<<"Error: cannot find canvas: "<<target_plot_name<<std::endl;
0077         return 1;
0078     }
0079 
0080     // c1->SetWindowSize(500, 800);
0081 
0082     std::cout<<"c1->GetWindowHeight(): "<<c1->GetWindowHeight()<<", c1->GetWindowWidth(): "<<c1->GetWindowWidth()<<std::endl;
0083 
0084     if (isSetLogY){ // note : logy
0085         // temp_h1D -> SetMaximum(y_max);
0086         c1 -> SetLogy(true);
0087     }
0088     else if (!isSetLogY) // note : linear y
0089     {
0090         // temp_h1D -> SetMinimum(y_min);
0091         // temp_h1D -> SetMaximum(y_max);
0092     }
0093 
0094     TList* primitives = c1->GetListOfPrimitives(); // Get the list of objects (primitives) on the canvas
0095     if (!primitives) {
0096         std::cerr << "No primitives found in TCanvas!" << std::endl;
0097         file_in->Close();
0098         return 666;
0099     }
0100 
0101     int legend_index = 0;
0102     // Loop through the primitives and modify the TH1Ds
0103     for (auto* obj : *primitives) {
0104         TH1D* hist = dynamic_cast<TH1D*>(obj); // Check if the primitive is a TH1D
0105         if (hist) {
0106             
0107             int color = std::get<0>(legend_text[legend_index]);
0108             std::string text = std::get<1>(legend_text[legend_index]);
0109             std::string draw_type = std::get<2>(legend_text[legend_index]);
0110 
0111             hist -> SetLineColor( color );
0112             std::cout<<hist->GetName()<<", set line color : "<<hist->GetLineColor()<<std::endl;
0113             leg -> AddEntry(hist, text.c_str(), draw_type.c_str());
0114 
0115             hist -> SetMaximum(y_max);
0116             hist -> SetMinimum(y_min);
0117 
0118             hist -> SetMarkerSize(0.7);
0119 
0120             if (axes_label.first.size() > 0){
0121                 hist -> GetXaxis() -> SetTitle(axes_label.first.c_str());
0122             }
0123             if (axes_label.second.size() > 0){
0124                 hist -> GetYaxis() -> SetTitle(axes_label.second.c_str());
0125             }
0126         }
0127 
0128         legend_index += 1;
0129 
0130     }
0131 
0132     // if (axes_label.first.size() > 0){
0133     //     temp_h1D -> GetXaxis() -> SetTitle(axes_label.first.c_str());
0134     // }
0135     // if (axes_label.second.size() > 0){
0136     //     temp_h1D -> GetYaxis() -> SetTitle(axes_label.second.c_str());
0137     // }
0138 
0139     // if (set_X_505) {temp_h1D -> GetXaxis() -> SetNdivisions(505);}
0140 
0141 
0142     // todo : the drawing 
0143     c1 -> cd();
0144 
0145     gStyle->SetPaintTextFormat("1.3f");
0146 
0147     // hist_in -> SetMarkerSize(0.5);
0148     // hist_in -> Draw("hist TEXT90");
0149 
0150     // hstack1D_in -> SetMarkerSize(0.8);
0151     // hstack1D_in -> Draw("hist TEXT90 E");
0152     // temp_h1D -> Draw("hist");
0153     // hstack1D_in -> Draw("hist same");
0154 
0155 
0156 
0157     ltx->DrawLatex(1 - gPad->GetRightMargin(), 1 - gPad->GetTopMargin() + 0.01, Form("#it{#bf{sPHENIX}} %s", sPH_label.c_str())); 
0158 
0159     for (auto text : additional_text){
0160         double x = std::get<0>(text);
0161         double y = std::get<1>(text);
0162         std::string text_str = std::get<2>(text);
0163 
0164         draw_text -> DrawLatex(x, y, text_str.c_str());
0165     }
0166 
0167     leg -> Draw("same");
0168 
0169     c1 -> Print(Form("%s/%s.pdf", output_directory.c_str(), output_file_name.c_str()));
0170     c1 -> Clear();
0171 
0172     return 0;
0173 }