# Getting Histogram from Tree and adding them after Cuts

**URL:** <https://root-forum.cern.ch/t/getting-histogram-from-tree-and-adding-them-after-cuts/45097>\
**Category:** Newbie\
**Tags:** tree, hist, root\
**Created:** [May 26, 2021, 6:45pm UTC](https://root-forum.cern.ch/t/getting-histogram-from-tree-and-adding-them-after-cuts/45097 "2021-05-26T18:45:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![matt](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/matt/32/30804_2.png) [@matt](https://root-forum.cern.ch/u/matt)\
**Post date:** [May 26, 2021, 6:45pm UTC](https://root-forum.cern.ch/t/getting-histogram-from-tree-and-adding-them-after-cuts/45097/1 "2021-05-26T18:45:28Z")

</div>

* * *

_ROOT Version:_ 6.18/00  
_Platform:_ Ubuntu 20.04  
_Compiler:_ Not Provided

* * *

Hello experts,

With reference to this [Getting the same histograms after cuts](https://root-forum.cern.ch/t/getting-the-same-histograms-after-cuts/24719) I tried the same, but I’m getting a blank histogram plot in a Canvas. I think the solution would be to replace “GOFF” with a “hist” which will give me a plot. But as I wanted to use each histogram for adding in the later case.  
Can you please help me sort out my problem?

Also, I would like to add this three histogram (what I want is to add three values e.g. Total\_KE = A + B + C, where A,B and C are the kinetic energy of particles). Will I get a correct result if I just add them?

I attached the code and the root file here [default\_ccqe\_Carbon.root](https://root-forum.cern.ch/uploads/short-url/6xoF3KLMPY25ICm6yYMsTMmqD4O.root) (330.4 KB)  
[E\_had.C](https://root-forum.cern.ch/uploads/short-url/i04b2UoswexaR7jrCUmFA2OeNTh.C) (1.3 KB)

Thanks in advance.

```auto
#include<iostream>
#include<iomanip>
#include<TFile.h>
#include<TTree.h>
#include<TChain.h>
#include<TCanvas.h>
#include<TLegend.h>
#include "TString.h"
#include "TROOT.h"

using namespace std;

void E_had() 
{
   const int nbins = 40;
   const int min = 0;
   const int max = 3;
    
   TFile* file_C12 = new TFile("default_ccqe_Carbon.root","OPEN");
   if (!file_C12 || file_C12->IsZombie() ) { delete file_C12; return; }
   
   TTree* t_C12 = (TTree*) file_C12->Get("gst");
   if (! t_C12) { delete file_C12; return;}
   
  
   auto c = new TCanvas();
   c->cd();
   
   TH1D* h_C12_default_KE_nucleon = new TH1D("h_C12_default_KE_nucleon "," ", nbins, min,max);
   TH1D* h_C12_default_pion_plus = new TH1D ("h_C12_default_pion_plus "," ", nbins, min,max);
   TH1D* h_C12_default_pion_0 = new TH1D ("h_C12_default_pion_0 "," ", nbins, min,max);
    
   h_C12_default_KE_nucleon->Sumw2(kTRUE);
 // t_C12->Draw("(Ef-0.938)>>h_C12_default_KE_nucleon","cc&&pdgf==2212"," goff");
   t_C12->Draw("(Ef-0.13957)>>h_C12_default_pion_plus","cc&&pdgf==211","goff");
   t_C12->Draw("(Ef-0.13498)>>h_C12_default_pion_0","cc&&pdgf==111","goff");

    TString cut_nucleon; 
    cut_nucleon = "cc&&pdgf==2212";
    
    t_C12->Project("h_C12_default_KE_nucleon", "(Ef-0.938)", cut_nucleon);  
    h_C12_default_KE_nucleon->Draw("hist"); 
   c->Update();  
}      

```

---

<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 18, 2021, 1:25pm UTC](https://root-forum.cern.ch/t/getting-histogram-from-tree-and-adding-them-after-cuts/45097/2 "2021-06-18T13:25:45Z")

</div>

You should do:

```auto
#include<iostream>
#include<iomanip>
#include<TFile.h>
#include<TTree.h>
#include<TChain.h>
#include<TCanvas.h>
#include<TLegend.h>
#include "TString.h"
#include "TROOT.h"

using namespace std;

void E_had()
{
   const int nbins = 40;
   const int min = 0;
   const int max = 3;

   TFile* file_C12 = new TFile("default_ccqe_Carbon.root","OPEN");
   if (!file_C12 || file_C12->IsZombie() ) { delete file_C12; return; }

   TTree* t_C12 = (TTree*) file_C12->Get("gst");
   if (! t_C12) { delete file_C12; return;}

   auto c = new TCanvas();
   c->cd();

   TH1D* h_C12_default_KE_nucleon = new TH1D("h_C12_default_KE_nucleon","h_C12_default_KE_nucleon", nbins, min,max);
   TH1D* h_C12_default_pion_plus = new TH1D ("h_C12_default_pion_plus ","h_C12_default_pion_plus", nbins, min,max);
   TH1D* h_C12_default_pion_0 = new TH1D ("h_C12_default_pion_0 ","h_C12_default_pion_0", nbins, min,max);

   h_C12_default_KE_nucleon->Sumw2(kTRUE);
   t_C12->Draw("(Ef-0.938)>>h_C12_default_KE_nucleon","cc&&pdgf==2212"," goff");
   t_C12->Draw("(Ef-0.13957)>>h_C12_default_pion_plus","cc&&pdgf==211","goff");
   t_C12->Draw("(Ef-0.13498)>>h_C12_default_pion_0","cc&&pdgf==111","goff");

    TString cut_nucleon;
    cut_nucleon = "cc&&pdgf==2212";

    t_C12->Project("h_C12_default_KE_nucleon", "(Ef-0.938)", cut_nucleon);

    h_C12_default_KE_nucleon->Draw("hist");

   c->Update();

}

```

Sorry for the delay.

---

<div class="post-metadata">

**Author:** ![matt](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/matt/32/30804_2.png) [@matt](https://root-forum.cern.ch/u/matt)\
**Post date:** [June 24, 2021, 5:26pm UTC](https://root-forum.cern.ch/t/getting-histogram-from-tree-and-adding-them-after-cuts/45097/3 "2021-06-24T17:26:07Z")

</div>

Its okay.  
Thanks alot. 🙂
