TH1F* h1 = new TH1F("h1","Example Histogram",20,-10,10); //Create a new histogram TH1F* h2 = new TH1F("h2","Example Histogram 2",20,-10,10); //Create another histogram h1->FillRandom("gaus",1000); //Fill h1 with random numbers distributed according to Gaussian distribution h2->FillRandom("gaus",500); //Fill h2 with random numbers distributed according to Gaussian distribution h1->Add(h2); //Add the contents of h2 to h1 h1->Draw(); //Draw the combined histogram
TFile* file1 = new TFile("file1.root","READ"); //Open a ROOT file TH1F* h1 = (TH1F*)file1->Get("histogram1"); //Get a histogram named 'histogram1' from the ROOT file TFile* file2 = new TFile("file2.root","READ"); //Open another ROOT file TH1F* h2 = (TH1F*)file2->Get("histogram2"); //Get a histogram named 'histogram2' from the ROOT file h1->Add(h2); //Add the contents of h2 to h1 h1->Draw(); //Draw the combined histogramThis code reads in two ROOT files, gets two histograms from each file, and then adds the contents of h2 to h1. The resulting histogram is then drawn. Package library: ROOT.