Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-07 08:12:22

0001 #ifndef DrawPlot_h
0002 #define DrawPlot_h
0003 
0004 #include "../sPhenixStyle.C"
0005 
0006 class DrawPlot 
0007 {
0008     public :
0009         TCanvas * c1;
0010         TH1F * hist1;
0011         TH1F * hist2;
0012 
0013         DrawPlot(vector<double> vec1, vector<double> vec2, int Nbins_1D, double edgeL_1D, double edgeR_1D, string X_title, string Y_title, bool set_logY, bool normalized = true, int normalized_binL = -1, int normalized_binR = -1, int sizeX = 950, int sizeY = 800, int stat_box = 0); // note : two vectors
0014         DrawPlot(long long N_event, TTree* tree1, string branch_name1, TTree* tree2, string branch_name2, int Nbins_1D, double edgeL_1D, double edgeR_1D, string X_title, string Y_title, bool set_logY, bool normalized = true, int normalized_binL = -1, int normalized_binR = -1, int sizeX = 950, int sizeY = 800, int stat_box = 0); // note : two trees
0015         void canvas_draw(string output_directoy, string plot_name);
0016     
0017     private :     
0018         int sizeX; 
0019         int sizeY;
0020         int Nbins_1D; 
0021         double edgeL_1D; 
0022         double edgeR_1D;
0023         bool set_logY;
0024         string X_title;
0025         string Y_title;
0026         bool normalized;
0027         int normalized_binL; 
0028         int normalized_binR;
0029         int stat_box;
0030 
0031         void canvas_init();
0032         void fill_1D_hist(TH1F * hist_in, vector<double> vec1);
0033         void hist_init(TH1F * hist_in, string LineColor);
0034         void hist_normalize (TH1F * hist_in);
0035         
0036 };
0037 
0038 DrawPlot::DrawPlot(vector<double> vec1, vector<double> vec2, int Nbins_1D, double edgeL_1D, double edgeR_1D, string X_title, string Y_title, bool set_logY, bool normalized, int normalized_binL, int normalized_binR, int sizeX, int sizeY, int stat_box)
0039 :sizeX(sizeX), sizeY(sizeY), Nbins_1D(Nbins_1D), edgeL_1D(edgeL_1D), edgeR_1D(edgeR_1D), set_logY(set_logY), X_title(X_title), Y_title(Y_title), normalized(normalized), normalized_binL(normalized_binL), normalized_binR(normalized_binR), stat_box(stat_box)
0040 {
0041     SetsPhenixStyle();
0042     canvas_init();
0043 
0044     hist1 = new TH1F("hist1","", Nbins_1D, edgeL_1D, edgeR_1D); hist_init(hist1, "#1A3947");
0045     hist2 = new TH1F("hist2","", Nbins_1D, edgeL_1D, edgeR_1D); hist_init(hist2, "#732d41");
0046     fill_1D_hist(hist1, vec1); 
0047     fill_1D_hist(hist2, vec2);
0048 
0049     if (normalized){
0050         hist_normalize(hist1);
0051         hist_normalize(hist2);
0052     }
0053 
0054     double Y_range_up = ( hist1->GetBinContent(hist1->GetMaximumBin()) > hist2->GetBinContent(hist2->GetMaximumBin()) ) ? hist1->GetBinContent(hist1->GetMaximumBin()) * 1.4 : hist2->GetBinContent(hist2->GetMaximumBin()) * 1.4;
0055     hist1 -> SetMaximum(Y_range_up);
0056     hist1 -> SetMinimum(0);
0057 
0058     hist1 -> Draw("hist");
0059     hist2 -> Draw("hist same");
0060 }
0061 
0062 DrawPlot::DrawPlot(long long N_event, TTree* tree1, string branch_name1, TTree* tree2, string branch_name2, int Nbins_1D, double edgeL_1D, double edgeR_1D, string X_title, string Y_title, bool set_logY, bool normalized, int normalized_binL, int normalized_binR, int sizeX, int sizeY, int stat_box)
0063 :sizeX(sizeX), sizeY(sizeY), Nbins_1D(Nbins_1D), edgeL_1D(edgeL_1D), edgeR_1D(edgeR_1D), set_logY(set_logY), X_title(X_title), Y_title(Y_title), normalized(normalized), normalized_binL(normalized_binL), normalized_binR(normalized_binR), stat_box(stat_box)
0064 {
0065     SetsPhenixStyle();
0066     canvas_init();
0067     
0068     hist1 = new TH1F("hist1","", Nbins_1D, edgeL_1D, edgeR_1D); hist_init(hist1, "#1A3947");
0069     hist2 = new TH1F("hist2","", Nbins_1D, edgeL_1D, edgeR_1D); hist_init(hist2, "#eb1765");
0070 
0071     tree1 -> Draw(Form("%s>>%s",branch_name1.c_str(),"hist1"), "", "", N_event);
0072     tree2 -> Draw(Form("%s>>%s",branch_name2.c_str(),"hist2"), "", "", N_event);
0073 
0074     if (normalized){
0075         hist_normalize(hist1);
0076         hist_normalize(hist2);
0077     }
0078 
0079     double Y_range_up = ( hist1->GetBinContent(hist1->GetMaximumBin()) > hist2->GetBinContent(hist2->GetMaximumBin()) ) ? hist1->GetBinContent(hist1->GetMaximumBin()) * 1.4 : hist2->GetBinContent(hist2->GetMaximumBin()) * 1.4;
0080     hist1 -> SetMaximum(Y_range_up);
0081     hist1 -> SetMinimum(0);
0082 
0083     hist1 -> Draw("hist");
0084     hist2 -> Draw("hist same");
0085 }
0086 
0087 void DrawPlot::hist_init(TH1F * hist_in, string LineColor)
0088 {
0089     hist_in -> SetLineColor(TColor::GetColor(LineColor.c_str())); 
0090     hist_in -> SetLineWidth(2);
0091     hist_in -> SetStats(stat_box);
0092     hist_in -> GetXaxis() -> SetTitle(X_title.c_str());
0093     hist_in -> GetXaxis() -> SetNdivisions(505);
0094     hist_in -> GetYaxis() -> SetTitle(Y_title.c_str());
0095 }
0096 
0097 void DrawPlot::fill_1D_hist(TH1F * hist_in, vector<double> vec1)
0098 {
0099     for (auto element : vec1){ hist_in -> Fill(element); }
0100 }
0101 
0102 void DrawPlot::hist_normalize(TH1F * hist_in)
0103 {
0104     cout<<"normalize consider region : "<<normalized_binL<<" "<<normalized_binR<<endl;
0105     cout<<"the input hist : "<< hist_in -> GetName() <<" integral : "<<hist_in -> Integral(normalized_binL,normalized_binR)<<endl;
0106     hist_in -> Scale(1./hist_in -> Integral(normalized_binL,normalized_binR));
0107 }
0108 
0109 void DrawPlot::canvas_init()
0110 {
0111     c1 = new TCanvas("","",sizeX,sizeY);
0112 }
0113 
0114 
0115 void DrawPlot::canvas_draw(string output_directoy, string plot_name)
0116 {
0117     c1 -> Print((output_directoy +"/"+ plot_name).c_str());
0118 }
0119 
0120 
0121 
0122 #endif