# Overlaying multiple trials in histogram

**URL:** <https://root-forum.cern.ch/t/overlaying-multiple-trials-in-histogram/58254>\
**Category:** Newbie\
**Tags:** hist, root\
**Created:** [February 22, 2024, 7:00pm UTC](https://root-forum.cern.ch/t/overlaying-multiple-trials-in-histogram/58254 "2024-02-22T19:00:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Raye\_Qtie](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/raye_qtie/32/35750_2.png) [@Raye\_Qtie](https://root-forum.cern.ch/u/Raye_Qtie)\
**Post date:** [February 22, 2024, 7:00pm UTC](https://root-forum.cern.ch/t/overlaying-multiple-trials-in-histogram/58254/1 "2024-02-22T19:00:50Z")

</div>

_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:

```auto

    gStyle->SetOptStat(0); 

    // 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

```

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [February 23, 2024, 4:31am UTC](https://root-forum.cern.ch/t/overlaying-multiple-trials-in-histogram/58254/2 "2024-02-23T04:31:28Z")

</div>

If they have the same x-axis binning, the best option is probably a [THStack](https://root.cern/doc/master/classTHStack.html) 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](https://root.cern/doc/master/classTAttLine.html) for options).

---

<div class="post-metadata">

**Author:** ![Raye\_Qtie](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/raye_qtie/32/35750_2.png) [@Raye\_Qtie](https://root-forum.cern.ch/u/Raye_Qtie)\
**Post date:** [February 24, 2024, 3:35pm UTC](https://root-forum.cern.ch/t/overlaying-multiple-trials-in-histogram/58254/3 "2024-02-24T15:35:31Z")

</div>

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

```auto
// Set global style options
gStyle->SetOptStat(0);

// 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);
}
h1_new->SetLineColor(color_code);
h1_new->SetLineWidth(line_width);

// 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]))]);
    hist->SetLineWidth(line_width);
    hs->Add(hist, "HIST");
  } else {
    std::cout << "Histogram not found in file " << conditionFiles[i] << std::endl;
  }
}

// Draw the THStack with "nostack" option
hs->Draw("nostack");

// Update the canvas with the drawn histograms
gPad->Update();
// 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!
