So I am writing code to combine histograms found within root files. I am running in ubuntu wsl, root is set up to work, the files are named after the following format “combined_run_13_DY.root”, the histogram names are correct (although I did also try with 1; and _ as seen in the image below), the files that I have been inputting are correct, their directory structure is also correct, the histograms are also filled, which is why I am posting this. It seems like it should work (it was actually working before), but it doesn’t. Attached is an image of the root browser looking at an example file structure.
Well, apparently I can’t attach links as a new user so here is the code:
#include <TFile.h>
#include <TH1F.h>
#include <TCanvas.h>
#include <TLegend.h>
#include
#include
#include
void combine_histograms(const char* file1_name, const char* file2_name, const char* file3_name, const char* file4_name, const char* file5_name, const char* file6_name, const char* file7_name, const char* file8_name, const char* output_file_name) {
TFile *file1 = TFile::Open(file1_name,"read");
TFile *file3 = TFile::Open(file3_name,"read");
TFile *file2 = TFile::Open(file2_name,"read");
TFile *file4 = TFile::Open(file4_name,"read");
TFile *file5 = TFile::Open(file5_name,"read");
TFile *file6 = TFile::Open(file6_name,"read");
TFile *file7 = TFile::Open(file7_name,"read");
TFile *file8 = TFile::Open(file8_name,"read");
//|| !file8
// Check if files are open
if (!file1 || !file2 || !file3 || !file4 || !file5 || !file6 || !file7 || !file8) {
std::cerr << "Error: Unable to open one of the input files." << std::endl;
return;
}
std::vector<std::string> variableArray = {"muonPt", "muonEta", "muonPhi", "muoniso", "muoniso_SEL", "muonVX", "muonVY", "muonVZ", "Nmuons_all", "Nmuons_mumuSEL", "MET_mumuSEL", "elecPt", "elecEta", "elecPhi", "eleciso", "elecVX", "elecVY", "elecVZ", "Nelectrons_all", "Nelectrons_eeSEL", "MET_eeSEL", "elec_muonPt", "elec_muonEta", "elec_muonPhi", "elec_muonVX", "elec_muonVY", "elec_muonVZ", "MET_emuSEL", "before_muonPt", "before_muonEta", "before_muonPhi", "before_muonVX", "before_muonVY", "before_muonVZ", "jetPt", "jetEta", "jetPhi", "jetVX", "jetVY", "jetVZ", "Njets_all_", "Njets_ee_", "Njets_mm_", "Njets_em_", "AK8_jetPt", "AK8_jetEta", "AK8_jetPhi", "AK8_jetVX", "AK8_jetVY", "AK8_jetVZ", "AK8_Njets_all_", "AK8_Njets_ee_", "AK8_Njets_mm_", "AK8_Njets_em_", "DeltaR_Elec", "DeltaR_Muon", "Generator_b", "MET_eeSel_Jet_", "MET_mumuSel_Jet_", "deltaRVal_mu1", "deltaRVal_mu2", "numMatched_mu1", "numMatched_mu2"};
// Create the output ROOT file
TFile *output_file = new TFile(output_file_name, "recreate");
// Using Index-Based For Loop
for (int i = 0; i<variableArray.size(); i++) {
// Get the histograms
std::string path = "analyzeBasicPat/" + variableArray[i];
TH1F* hist1 = (TH1F*)file1->Get("analyzeBasicPat/Njets_all_");
TH1F* hist2 = (TH1F*)file2->Get(path.c_str());
TH1F* hist3 = (TH1F*)file3->Get(path.c_str());
TH1F* hist4 = (TH1F*)file4->Get(path.c_str());
TH1F* hist5 = (TH1F*)file5->Get(path.c_str());
TH1F* hist6 = (TH1F*)file6->Get(path.c_str());
TH1F* hist7 = (TH1F*)file7->Get(path.c_str());
TH1F* hist8 = (TH1F*)file8->Get(path.c_str());
// Check if histograms are retrieved successfully
if (!hist1 || !hist2 || !hist3 || !hist4 || !hist5 || !hist6 || !hist7 || !hist8) {
//|| !hist8
std::cerr << "Error: Unable to retrieve histograms for " << variableArray[i] << " from input files." << std::endl;
continue;
}
// Set histogram styles
hist1->SetLineColor(kRed);
hist2->SetLineColor(kOrange);
hist3->SetLineColor(kYellow);
hist4->SetLineColor(kGreen);
hist5->SetLineColor(kBlue);
hist6->SetLineColor(kCyan);
hist7->SetLineColor(kGray);
hist8->SetLineColor(kBlack);
// Create a canvas
TCanvas *canvas = new TCanvas(("canvas_" + variableArray[i]).c_str(), "Combined Histograms", 1000, 1000);
Double_t factor = 1.;
// Draw the histograms on the same canvas
hist1->Scale(factor/hist1->GetEntries());
hist1->Draw("HIST");
hist2->Scale(factor/hist2->GetEntries());
hist2->Draw("HIST SAME");
hist3->Scale(factor/hist3->GetEntries());
hist3->Draw("HIST SAME");
hist4->Scale(factor/hist4->GetEntries());
hist4->Draw("HIST SAME");
hist5->Scale(factor/hist5->GetEntries());
hist5->Draw("HIST SAME");
hist6->Scale(factor/hist6->GetEntries());
hist6->Draw("HIST SAME");
hist7->Scale(factor/hist7->GetEntries());
hist7->Draw("HIST SAME");
hist8->Scale(factor/hist8->GetEntries());
// Add a legend
TLegend *legend = new TLegend(0.7, 0.7, 0.9, 0.9);
legend->AddEntry(hist1, "DY", "l"); //Was DY
legend->AddEntry(hist2, "WW", "l");
legend->AddEntry(hist3, "WZ", "l");
legend->AddEntry(hist4, "ZZ", "l");
legend->AddEntry(hist5, "1100", "l");
legend->AddEntry(hist6, "1400", "l");
legend->AddEntry(hist7, "1800", "l");
legend->AddEntry(hist8, "TT", "l");
legend->Draw();
// Save the canvas to a file
canvas->SaveAs(("combined_histograms_" + variableArray[i] + ".png").c_str());
// Write the histograms and canvas to the output file
output_file->cd();
hist1->Write();
hist2->Write();
hist3->Write();
hist4->Write();
hist5->Write();
hist6->Write();
hist7->Write();
hist8->Write();
canvas->Write();
// Clean up
delete canvas;
delete legend;
}
// Close the files
output_file->Close();
file1->Close();
file2->Close();
file3->Close();
file4->Close();
file5->Close();
file6->Close();
file7->Close();
file8->Close();
// Clean up
delete output_file;
delete file1;
delete file2;
delete file3;
delete file4;
delete file5;
delete file6;
delete file7;
delete file8;
}
int main() {
// Call the function with the file names
std::string run = “13”;
// Call the function with the file names
combine_histograms(("combined_run_" + run + "_DY.root").c_str(), ("combined_run_" + run + "_WW.root").c_str(), ("combined_run_" + run + "_WZ.root").c_str(), ("combined_run_" + run + "_ZZ.root").c_str(), ("combined_run_" + run + "_SUSY_1100.root").c_str(), ("combined_run_" + run + "_SUSY_1400.root").c_str(), ("combined_run_" + run + "_SUSY_1800.root").c_str(), ("combined_run_" + run + "_TT.root").c_str(), ("combined_Run_" + run + ".root").c_str());
}