#include #include #include void scale() { TH1F* hist1 = new TH1F("hist1", "Histogram 1", 100, -5, 5); TH1F* hist2 = new TH1F("hist2", "Histogram 2", 100, -5, 5); TH1F* hist3 = new TH1F("hist3", "Histogram 3", 100, -5, 5); TRandom3 randGen; for (int i = 0; i < 50000; i++) { double value1 = randGen.Gaus(0.0, 1.0); hist1->Fill(value1); } for (int i = 0; i < 20000; i++) { double value2 = randGen.Gaus(0.0, 1.0); hist2->Fill(value2); } for (int i = 0; i < 5000; i++) { double value3 = randGen.Gaus(0.0, 1.0); hist3->Fill(value3); } //Distribution without Normalization TCanvas* can1 = new TCanvas("can1", "Histogram", 800, 600); hist1->SetTitle(""); hist1->SetStats(0); hist1->SetLineColor(kBlue); hist1->Draw(); hist2->SetLineColor(kRed); hist2->Draw("same"); hist3->SetLineColor(kGreen); hist3->Draw("same"); TLegend* legend = new TLegend(0.7, 0.7, 0.8, 0.8); legend->SetBorderSize(0); legend->AddEntry(hist1, "Histogram 1", "l"); legend->AddEntry(hist2, "Histogram 2", "l"); legend->AddEntry(hist3, "Histogram 3", "l"); legend->Draw(); can1->Update(); can1->SaveAs("scale_v1.pdf"); //Normalization with respect to Area TCanvas* can2 = new TCanvas("can2", "Histogram", 800, 600); TH1F* hist1_cp = (TH1F*)hist1->Clone(); hist1_cp->Scale(1/hist1_cp->Integral()); TH1F* hist2_cp = (TH1F*)hist2->Clone(); hist2_cp->Scale(1/hist2_cp->Integral()); TH1F* hist3_cp = (TH1F*)hist3->Clone(); hist3_cp->Scale(1/hist3_cp->Integral()); hist1_cp->SetTitle(""); hist1_cp->SetStats(0); hist1_cp->GetYaxis()->SetRangeUser(0, 1.2 * hist1_cp->GetMaximum()); hist1_cp->SetLineColor(kBlue); hist1_cp->Draw("hist"); hist2_cp->SetLineColor(kRed); hist2_cp->Draw("same hist"); hist3_cp->SetLineColor(kGreen); hist3_cp->Draw("same hist"); TLegend* legend2 = new TLegend(0.7, 0.7, 0.8, 0.8); legend2->SetBorderSize(0); legend2->AddEntry(hist1_cp, "Histogram 1", "l"); legend2->AddEntry(hist2_cp, "Histogram 2", "l"); legend2->AddEntry(hist3_cp, "Histogram 3", "l"); legend2->Draw(); can2->Update(); can2->SaveAs("scale_v2.pdf"); //Normalization with respect to Maximum TCanvas* can3 = new TCanvas("can3", "Histogram", 800, 600); TH1F* hist1_cp2 = (TH1F*)hist1->Clone(); TH1F* hist2_cp2 = (TH1F*)hist2->Clone(); hist2_cp2->Scale(hist1_cp2->GetMaximum()/hist2_cp2->GetMaximum()); TH1F* hist3_cp2 = (TH1F*)hist3->Clone(); hist3_cp2->Scale(hist2->GetMaximum()/hist3_cp2->GetMaximum()); //hist3_cp2->Scale(hist2->Integral()/hist3_cp2->Integral()); hist1_cp2->SetTitle(""); hist1_cp2->SetStats(0); hist1_cp2->GetYaxis()->SetRangeUser(0, 1.2 * hist1_cp2->GetMaximum()); hist1_cp2->SetLineColor(kBlue); hist1_cp2->Draw("hist"); hist2_cp2->SetLineColor(kRed); hist2_cp2->Draw("same hist"); hist3_cp2->SetLineColor(kGreen); hist3_cp2->Draw("same hist"); TLegend* legend3 = new TLegend(0.7, 0.7, 0.8, 0.8); legend3->SetBorderSize(0); legend3->AddEntry(hist2_cp2, "Histogram 1", "l"); legend3->AddEntry(hist2_cp2, "Histogram 2", "l"); legend3->AddEntry(hist3_cp2, "Histogram 3", "l"); legend3->Draw(); can3->Update(); can3->SaveAs("scale_v3.pdf"); }