# Empty histogram

**URL:** <https://root-forum.cern.ch/t/empty-histogram/59637>\
**Category:** Newbie\
**Created:** [June 13, 2024, 10:14am UTC](https://root-forum.cern.ch/t/empty-histogram/59637 "2024-06-13T10:14:35Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![rafal4](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/r/f19dbf/32.png) [@rafal4](https://root-forum.cern.ch/u/rafal4)\
**Post date:** [June 13, 2024, 10:14am UTC](https://root-forum.cern.ch/t/empty-histogram/59637/1 "2024-06-13T10:14:35Z")

</div>

Hello everyone  
I’m trying to draw three histograms on one Canvas. Unfortunately, the resulting histogram is empty. There are no errors in the console. I checked and the original histograms contain correct data, the X axis is in the same range. The titles of the X and Y axis are the same.  
Do you have any advice?  
Cheers,  
Rafal

```auto
#include <TFile.h>
#include <TH1.h>
#include <TCanvas.h>
#include <TLegend.h>
#include <TStyle.h>
#include <iostream>
#include <vector>
#include <TApplication.h>
#include <TKey.h>
#include <TClass.h>

void drawHistograms() {
    // ROOT file list
    std::vector<std::string> fileNames = {
        "Electron1MeV.root", "Electron2MeV.root", "Electron5MeV.root"
    };

    // Histogram names in ROOT files
    std::vector<std::string> histNames = {
        "E e- 1MeV", "E e- 2MeV", "E e- 5MeV"
    };

    // Creating canvas
    TCanvas *c = new TCanvas("c", "Combined Histograms", 800, 600);

    // Colors for histograms
    int colors[] = {kRed, kBlue, kGreen};

    // Loop over files
    for (size_t i = 0; i < fileNames.size(); ++i) {
        // Open ROOT file
        TFile *file = TFile::Open(fileNames[i].c_str());
        if (!file || file->IsZombie()) {
            std::cerr << "Cannot open file: " << fileNames[i] << std::endl;
            continue;
        }

        // Get histogram
        TH1 *hist = dynamic_cast<TH1*>(file->Get(histNames[i].c_str()));
        if (!hist) {
            std::cerr << "Cannot find histogram in file: " << fileNames[i] << std::endl;
            file->Close();
            continue;
        }

        // Set histogram attributes
        hist->SetLineColor(colors[i]);
        hist->SetLineWidth(2);

        // Draw histogram
        if (i == 0) {
            hist->Draw();
            hist->GetXaxis()->SetTitle("The value of the angle in degrees");
            hist->GetYaxis()->SetTitle("Number of Cherenkov photons");
        } else {
            hist->Draw("SAME");
        }

        // Close file
        file->Close();
    }

    // Update canvas
    c->Update();
}

int main(int argc, char **argv) {
    
    TApplication theApp("App", &argc, argv);
    drawHistograms();
    theApp.Run();
    return 0;
}

```

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [June 13, 2024, 10:19am UTC](https://root-forum.cern.ch/t/empty-histogram/59637/2 "2024-06-13T10:19:19Z")

</div>

Add:

```auto
        hist->SetDirectory(nullptr); // << --- to detach the histogram from the file

```

Just before:

```auto
        // Draw histogram
        if (i == 0) {

```

---

<div class="post-metadata">

**Author:** ![rafal4](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/r/f19dbf/32.png) [@rafal4](https://root-forum.cern.ch/u/rafal4)\
**Post date:** [June 13, 2024, 11:17am UTC](https://root-forum.cern.ch/t/empty-histogram/59637/3 "2024-06-13T11:17:06Z")

</div>

Thank you. I am already drawing, but unfortunately not correctly. Only one histogram from the last loaded file is drawn. The Y axis is filled incorrectly.

 ![histogramResoult1](https://root-forum.cern.ch/uploads/default/original/3X/e/5/e54b523b85df9b9e3fd4f5ce5d25c4e33e82cad7.png)

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [June 13, 2024, 11:18am UTC](https://root-forum.cern.ch/t/empty-histogram/59637/4 "2024-06-13T11:18:35Z")

</div>

I’ll let @couet comment on this

---

<div class="post-metadata">

**Author:** ![yus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/9fc348/32.png) [@yus](https://root-forum.cern.ch/u/yus)\
**Post date:** [June 13, 2024, 11:31am UTC](https://root-forum.cern.ch/t/empty-histogram/59637/5 "2024-06-13T11:31:04Z")

</div>

Hi,

> [@rafal4](#):
>
> Only one histogram from the last loaded file is drawn.

are you sure that the first two histograms are not drawn “behind” the third one?

---

<div class="post-metadata">

**Author:** ![rafal4](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/r/f19dbf/32.png) [@rafal4](https://root-forum.cern.ch/u/rafal4)\
**Post date:** [June 13, 2024, 11:44am UTC](https://root-forum.cern.ch/t/empty-histogram/59637/6 "2024-06-13T11:44:04Z")

</div>

> [@yus](#):
>
> are you sure that the first two histograms are not drawn “behind” the third one?

I don’t know that. My goal was to draw three histograms simultaneously on one Canvas to visually compare them. Each energy was supposed to be represented by a different color of the lines.

---

<div class="post-metadata">

**Author:** ![yus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/9fc348/32.png) [@yus](https://root-forum.cern.ch/u/yus)\
**Post date:** [June 13, 2024, 11:46am UTC](https://root-forum.cern.ch/t/empty-histogram/59637/7 "2024-06-13T11:46:09Z")

</div>

Do you know where you set this light-blue color the third histogram is filled with?

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [June 13, 2024, 11:46am UTC](https://root-forum.cern.ch/t/empty-histogram/59637/8 "2024-06-13T11:46:17Z")

</div>

Can you provide the data file so we can run your example ?

---

<div class="post-metadata">

**Author:** ![rafal4](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/r/f19dbf/32.png) [@rafal4](https://root-forum.cern.ch/u/rafal4)\
**Post date:** [June 13, 2024, 11:53am UTC](https://root-forum.cern.ch/t/empty-histogram/59637/9 "2024-06-13T11:53:02Z")

</div>

[Electron5MeV.root](https://root-forum.cern.ch/uploads/short-url/e5nLxkzA8SNOs1DNcfDI5uO8lsN.root) (5.8 KB)  
[Electron2MeV.root](https://root-forum.cern.ch/uploads/short-url/3W9pirKly1qv4mCeEv3rwgaQ8VG.root) (5.8 KB)  
[Electron1MeV.root](https://root-forum.cern.ch/uploads/short-url/cR1JJBWkcLQoZq4RVxYt99Bp5k2.root) (5.7 KB)

Thank you. My histogram files

---

<div class="post-metadata">

**Author:** ![yus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/9fc348/32.png) [@yus](https://root-forum.cern.ch/u/yus)\
**Post date:** [June 13, 2024, 12:07pm UTC](https://root-forum.cern.ch/t/empty-histogram/59637/10 "2024-06-13T12:07:11Z")

</div>

Try drawing your histograms in the reverse order, I expect it should make all three to appear.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [June 13, 2024, 12:10pm UTC](https://root-forum.cern.ch/t/empty-histogram/59637/11 "2024-06-13T12:10:02Z")

</div>

```auto
void drawHistograms() {
   // ROOT file list
   std::vector<std::string> fileNames = {
      "Electron1MeV.root", "Electron2MeV.root", "Electron5MeV.root"
   };

   // Histogram names in ROOT files
   std::vector<std::string> histNames = {
      "E e- 1MeV", "E e- 2MeV", "E e- 5MeV"
   };

   // Creating canvas
   auto c = new TCanvas("c", "Combined Histograms", 800, 600);

   // Colors for histograms
   int colors[] = {kRed, kBlue, kGreen};

   auto st = new THStack();

   // Loop over files
   for (size_t i = 0; i < fileNames.size(); ++i) {
      // Open ROOT file
      TFile *file = TFile::Open(fileNames[i].c_str());
      if (!file || file->IsZombie()) {
         std::cerr << "Cannot open file: " << fileNames[i] << std::endl;
         continue;
      }

      // Get histogram
      TH1 *hist = dynamic_cast<TH1*>(file->Get(histNames[i].c_str()));
      hist->SetDirectory(nullptr);
      if (!hist) {
         std::cerr << "Cannot find histogram in file: " << fileNames[i] << std::endl;
         file->Close();
         continue;
      }

      // Set histogram attributes
      hist->SetLineColor(colors[i]);
      hist->SetFillStyle(0);
      hist->SetLineWidth(3);

      st->Add(hist);

      // Close file
      file->Close();
   }

   st->Draw("nostack");
   st->GetXaxis()->SetTitle("The value of the angle in degrees");
   st->GetYaxis()->SetTitle("Number of Cherenkov photons");
   c->Modified();
   c->Update();
}

```

 ![Screenshot 2024-06-13 at 14.13.11](https://root-forum.cern.ch/uploads/default/original/3X/8/e/8ea002cfff4ab2b5a6770077719985d3ad9fdd62.jpeg)

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [June 13, 2024, 1:07pm UTC](https://root-forum.cern.ch/t/empty-histogram/59637/12 "2024-06-13T13:07:08Z")

</div>

In that particular case log scale along the Y axis might be better:

 ![Screenshot 2024-06-13 at 15.06.19](https://root-forum.cern.ch/uploads/default/original/3X/9/0/90553b38a8b3afded3a57fb4eddaf74abc21bf2b.jpeg)

---

<div class="post-metadata">

**Author:** ![rafal4](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/r/f19dbf/32.png) [@rafal4](https://root-forum.cern.ch/u/rafal4)\
**Post date:** [June 13, 2024, 5:58pm UTC](https://root-forum.cern.ch/t/empty-histogram/59637/13 "2024-06-13T17:58:07Z")

</div>

Thank you very much. You are very helpful!  
If I understand correctly, the first histogram is drawn on the Canvas first, then the second, and finally the third? That is why only the legend of the third histogram is visible because the third one has covered the previous ones?  
Cheers,  
Rafal

---

<div class="post-metadata">

**Author:** ![yus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/9fc348/32.png) [@yus](https://root-forum.cern.ch/u/yus)\
**Post date:** [June 13, 2024, 7:23pm UTC](https://root-forum.cern.ch/t/empty-histogram/59637/14 "2024-06-13T19:23:34Z")

</div>

> [@rafal4](#):
>
> If I understand correctly, the first histogram is drawn on the Canvas first, then the second, and finally the third? That is why only the legend of the third histogram is visible because the third one has covered the previous ones?

Yes and yes 🙂

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [June 13, 2024, 7:30pm UTC](https://root-forum.cern.ch/t/empty-histogram/59637/15 "2024-06-13T19:30:50Z")

</div>

> [@rafal4](#):
>
> If I understand correctly, the first histogram is drawn on the Canvas first, then the second, and finally the third? That is why only the legend of the third histogram is visible because the third one has covered the previous ones?

Yes, that’s why I recommend to us a THStack because you do not need to take care of the order… The whole range will be computed automatically
