# Stacking Four Different Histograms

**URL:** <https://root-forum.cern.ch/t/stacking-four-different-histograms/46120>\
**Category:** Newbie\
**Created:** [August 2, 2021, 3:53am UTC](https://root-forum.cern.ch/t/stacking-four-different-histograms/46120 "2021-08-02T03:53:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheScientist](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/82dd89/32.png) [@TheScientist](https://root-forum.cern.ch/u/TheScientist)\
**Post date:** [August 2, 2021, 3:53am UTC](https://root-forum.cern.ch/t/stacking-four-different-histograms/46120/1 "2021-08-02T03:53:25Z")

</div>

I am trying to stack four different histograms in one canvas. Here’s my code. Please let me know where my errors are and how I can improve them.

```auto
void histo()
{
        TFile* fInput = new TFile("TargetVStudy_40cm_POT1000.root", "READ");
        TTree* tree = (TTree*)fInput->Get("DUNETargetSim");

        Char_t pid[32];
        Double_t Energy;

        gStyle->SetOptStat(11111);
        tree->SetBranchAddress("pid", &pid);
        tree->SetBranchAddress("kineticEnergy", &Energy);

        Int_t nEntries = tree->GetEntries("kineticEnergy");

        THStack *histos = new THStack("histos", "");
        TH1D* hist1 = new TH1D("nu_e_Hist", "Histogram Title", 1200, 0, 120);
        hist1->SetMarkerStyle(20);
        hist1->SetMarkerColor(kRed);

        TH1D* hist2 = new TH1D("nu_mu_Hist", "Histogram Title", 1200, 0, 120);
        hist2->SetMarkerStyle(21);
        hist2->SetMarkerColor(kBlue);

        TH1D* hist3 = new TH1D("anti_nu_e_Hist", "Histogram Title", 1200, 0, 120);
        hist3->SetMarkerStyle(22);
        hist3->SetMarkerColor(kGreen);

        TH1D* hist4 = new TH1D("anti_nu_mu_Hist", "Histogram Title", 1200, 0, 120);
        hist4->SetMarkerStyle(23);
        hist4->SetMarkerColor(kBlack);

        histos->Add(hist1);
        histos->Add(hist2);
        histos->Add(hist3);
        histos->Add(hist4);

        for (Int_t i = 0; i < nEntries; i++)
        {
                tree->GetEntry(i);

                for (Int_t j = 0; j < 4; j++)
                {
                switch(j) {
                        case 0:
                                if(strcmp(pid, "nu_e") == 0) { hist1->Fill(Energy); }
                                break;
 case 1:
                                if(strcmp(pid, "nu_mu") == 0) { hist2->Fill(Energy); }
                                break;
                        case 2:
                                if(strcmp(pid, "anti_nu_e") == 0) { hist3->Fill(Energy); }
                                break;
                        case 3:
                                if(strcmp(pid, "anti_nu_mu") == 0) { hist4->Fill(Energy); }
                                break;
                        default:
                                ;
                        // cout << "Hey There!" << endl;

                        }
                }
        }
/*
Int_t a = hist1->GetEntries();
Int_t b = hist2->GetEntries();
Int_t c = hist3->GetEntries();
Int_t d = hist4->GetEntries();

cout << "nu_e: " << a << "; nu_mu: " << b << "; anti_nu_e: " << c << "; anti_nu_mu: " << d << endl;
*/
TCanvas* cs = new TCanvas();
histos->Draw();
}

```

---

<div class="post-metadata">

**Author:** ![Axel](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/axel/32/11084_2.png) [@Axel](https://root-forum.cern.ch/u/Axel)\
**Post date:** [August 3, 2021, 3:29pm UTC](https://root-forum.cern.ch/t/stacking-four-different-histograms/46120/2 "2021-08-03T15:29:27Z")

</div>

Do _you_ see a problem with the code? I might be able to guess, and I might even be able to guess correctly, but it’s good style to post the issue you have together with your code 🙂

---

<div class="post-metadata">

**Author:** ![TheScientist](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/82dd89/32.png) [@TheScientist](https://root-forum.cern.ch/u/TheScientist)\
**Post date:** [August 3, 2021, 9:39pm UTC](https://root-forum.cern.ch/t/stacking-four-different-histograms/46120/3 "2021-08-03T21:39:59Z")

</div>

Oops! My apologies. I forgot that part. 😃

My code is not reading all the data and I think it is only plotting hist1.

 ![Capture](https://root-forum.cern.ch/uploads/default/original/3X/b/c/bcecbbcf355999887dbf65acb098926fcdc26ed5.png)

---

<div class="post-metadata">

**Author:** ![Axel](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/axel/32/11084_2.png) [@Axel](https://root-forum.cern.ch/u/Axel)\
**Post date:** [August 4, 2021, 7:07am UTC](https://root-forum.cern.ch/t/stacking-four-different-histograms/46120/4 "2021-08-04T07:07:41Z")

</div>

I can see 4 histograms: the first bin shows four horizontal lines.

Please check the [drawing options](https://root.cern/doc/master/classTHistPainter.html#HP01a) to see how to style them differently: you are using different marker styles, but then you’re not drawing any markers.

Also, to avoid ownership issues, please create the histograms first, then open the `TFile`, extract the `TTree` etc.
