# Draw an histo with Tree Problem

**URL:** <https://root-forum.cern.ch/t/draw-an-histo-with-tree-problem/45160>\
**Category:** Newbie\
**Tags:** tree, root\
**Created:** [May 31, 2021, 12:12pm UTC](https://root-forum.cern.ch/t/draw-an-histo-with-tree-problem/45160 "2021-05-31T12:12:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![IKoro](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/i/3e96dc/32.png) [@IKoro](https://root-forum.cern.ch/u/IKoro)\
**Post date:** [May 31, 2021, 12:12pm UTC](https://root-forum.cern.ch/t/draw-an-histo-with-tree-problem/45160/1 "2021-05-31T12:12:18Z")

</div>

Hey,

I don’t understand why i can’t draw my histo

```auto
void OMline()
{

TCanvas *c1 = new TCanvas();

TFile *input = new TFile("/home/dupuy/Documents/code_dupuy/Exercices/fichier root/valeurspics2.root","READ");

TTree *tree = (TTree*)input->Get("valeurspics1_tree;1");

double Constant1, Mean1, Sigma1, pics;
int om_number1;

tree->SetBranchAddress("om_number1", &om_number1);
tree->SetBranchAddress("Constant1", &Constant1);
tree->SetBranchAddress("Mean1", &Mean1);
tree->SetBranchAddress("Sigma1", &Sigma1);
tree->SetBranchAddress("pics", &pics);

int entries = tree->GetEntries();

cout << entries << endl;

TH2D *hist7 = new TH2D("hist7", "",1000, 0, 10, 1000, 0, 500000);

for(int i=0; i < entries; i++) // rentre les valeurs de la branche dans notre variable
    {
      tree->GetEntry(i);

      if (om_number1 == 301)
      {
      hist7->Fill(Mean1,pics);
      }
    }

hist7->Draw();
hist7->GetXaxis()->SetTitle("time (s)");
hist7->GetYaxis()->SetTitle("charge (U.A)");
hist7->GetXaxis()->SetTitleSize(0.05);
hist7->GetYaxis()->SetTitleSize(0.05);
hist7->GetXaxis()->SetLabelSize(0.05);
hist7->GetYaxis()->SetLabelSize(0.05);

}

```

[valeurspics2.root](https://root-forum.cern.ch/uploads/short-url/wUyJm7HQv4QCLlKIzDlpb6HOnjn.root) (6.6 KB)

---

<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:** [May 31, 2021, 12:58pm UTC](https://root-forum.cern.ch/t/draw-an-histo-with-tree-problem/45160/2 "2021-05-31T12:58:13Z")

</div>

1. the tree variables were not properly declared
2. om\_number1 is never equal to 301
3. the histogram limits are wrong
4. 1000x1000 bins ? really ?
5. the tree name is valeurspics2\_tree

after this fixes I get:

```auto
void OMline()
{

   TCanvas *c1 = new TCanvas();

   TFile *input = new TFile("valeurspics2.root");

   TTree *tree = (TTree*)input->Get("valeurspics2_tree");

   double Constant1, Mean1, Sigma1;
   int pics;
   double om_number1;

   tree->SetBranchAddress("om_number1", &om_number1);
   tree->SetBranchAddress("Constant1", &Constant1);
   tree->SetBranchAddress("Mean1", &Mean1);
   tree->SetBranchAddress("Sigma1", &Sigma1);
   tree->SetBranchAddress("pics", &pics);

   int entries = tree->GetEntries();

   cout << entries << endl;

   TH2D *hist7 = new TH2D("hist7", "",40, 0, 120000, 40, 0, 6);

   for(int i=0; i < entries; i++) {
      tree->GetEntry(i);
      hist7->Fill(Mean1,(double)pics);
   }

   hist7->Draw("col");
   hist7->GetXaxis()->SetTitle("time (s)");
   hist7->GetYaxis()->SetTitle("charge (U.A)");
   hist7->GetXaxis()->SetTitleSize(0.05);
   hist7->GetYaxis()->SetTitleSize(0.05);
   hist7->GetXaxis()->SetLabelSize(0.05);
   hist7->GetYaxis()->SetLabelSize(0.05);

}

```

The plot is:

 ![Screenshot 2021-05-31 at 14.57.06](https://root-forum.cern.ch/uploads/default/original/3X/3/c/3c2e35268e70a1117fc3c8478c866c907d1500de.jpeg)

---

<div class="post-metadata">

**Author:** ![IKoro](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/i/3e96dc/32.png) [@IKoro](https://root-forum.cern.ch/u/IKoro)\
**Post date:** [May 31, 2021, 1:04pm UTC](https://root-forum.cern.ch/t/draw-an-histo-with-tree-problem/45160/3 "2021-05-31T13:04:52Z")

</div>

Thank you so much. I did a bit of anything x)
