Overlaying multiple trials in histogram

ROOT Version: 6.22/08
Hi i’m very very newbie and I’m working on a project where I need to visually compare three main histograms, each representing different conditions, on a single canvas. For each condition, I have data from 10 separate trials stored in individual ROOT files. My objective is to overlay the 10 trials for each main histogram onto a single canvas to compare the variations within and across the conditions.

The challenge is to overlay these trials in a way that allows for easy visual comparison. I appreciate any guidance or examples you can share to help structure this analysis correctly and efficiently. Thank you!
Here’s a conceptual outline of what I’m aiming to do:


    // Example for loading and processing the first main histogram
    TFile *f1 = new TFile("path/to/first/main/file.root");  
    TH2F h1 = (TH2F)f1->Get("histoName1");
    TH1F *h1_new = new TH1F("h1_new", "Histogram Title 1", 100, 0, 100); // Adjust binning as needed
        // Example of a for loop to process bins of h1
    Double_t max = h1->GetMaximum(); // Get the maximum value for normalization
    for (Int_t bin = 1; bin <= h1->GetNbinsX(); bin++) {
        Double_t binContent = h1->GetBinContent(bin);
        Double_t normalizedBinContent = binContent / max; // Normalize the bin content
        h1_new->SetBinContent(bin, normalizedBinContent);
   h1_new->Draw(); // Draw the first normalized histogram
    // Add a legend to the canvas
    // Repeat the process for the second and third histograms
    // Assuming h2 and h3 follow a similar pattern to h1
    // Example:
    // TFile *f2 = new TFile("path/to/second/main/file.root");  
    // TH2F h2 = (TH2F)f2->Get("histoName2");
    // TH1F *h2_new = processHistogram(f2, "h2_new", "Histogram Title 2", kRed);
    // ... and similarly for h3

If they have the same x-axis binning, the best option is probably a THStack drawn with the “nostack” option. Just make sure to change the line colours (SetLineColor(), similar to SetFillColor() in the examples) for better presentation; you can also change the line type, width, colour (see the examples and TAttLine for options).


Hi, thank you for your suggestion. I’ve adjusted my code accordingly, here’s a simplified version of what I’ve implemented:

// Set global style options

// Loading and normalizing histograms for the first condition
TFile *f1 = new TFile("file_path");
TH2F *h1 = (TH2F*)f1->Get("histo_name");
TH1F *h1_new = new TH1F("hist_id", "Histogram Title", bins, x_start, x_end);

// Normalize and style the first histogram
Double_t max1 = h1->GetMaximum();
for (Int_t i = 0; i <= h1->GetNbinsX(); i++) {
  Double_t norm = h1->GetBinContent(i) / max1;
  h1_new->SetBinContent(i, norm);

// Initialize a canvas
TCanvas *c = new TCanvas("c_id", "Canvas Title", width, height);

// Initialize THStack for overlaying histograms without stacking them
THStack *hs = new THStack("hs_id", "Stack Title;X-axis Title;Y-axis Title");

// Loop over each file, retrieve the histogram, normalize it, and add it to the THStack
const char* conditionFiles[] = {"file_paths"};
int colors[] = {color_codes}; // Colors for different trials

for (size_t i = 0; i < sizeof(conditionFiles) / sizeof(conditionFiles[0]); ++i) {
  TFile *file = new TFile(conditionFiles[i]);
  TH1F *hist = (TH1F*)file->Get("histo_name");

  if (hist) {
    // Normalize and style each histogram before adding to THStack
    Double_t max = hist->GetMaximum();
    for (Int_t bin = 1; bin <= hist->GetNbinsX(); bin++) {
      Double_t content = hist->GetBinContent(bin) / max;
      hist->SetBinContent(bin, content);
    hist->SetLineColor(colors[i % (sizeof(colors) / sizeof(colors[0]))]);
    hs->Add(hist, "HIST");
  } else {
    std::cout << "Histogram not found in file " << conditionFiles[i] << std::endl;

// Draw the THStack with "nostack" option

// Update the canvas with the drawn histograms
// Repeat the process for the second and third histograms

Could you please review this approach and let me know if there are any further optimizations or adjustments you’d recommend? Your insight is greatly appreciated!