# TH1F - read and made histogram

**URL:** <https://root-forum.cern.ch/t/th1f-read-and-made-histogram/32801>\
**Category:** ROOT\
**Created:** [February 19, 2019, 10:30am UTC](https://root-forum.cern.ch/t/th1f-read-and-made-histogram/32801 "2019-02-19T10:30:24Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_Fernandes](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/david_fernandes/32/19363_2.png) [@David\_Fernandes](https://root-forum.cern.ch/u/David_Fernandes)\
**Post date:** [February 19, 2019, 10:30am UTC](https://root-forum.cern.ch/t/th1f-read-and-made-histogram/32801/1 "2019-02-19T10:30:25Z")

</div>

* * *

_ROOT Version:_ 5.34.36  
_Platform:_ Ubuntu 16.04.05  
\_Compiler:\_gcc 4.8

* * *

Hi everybody!

I’m doing a macro to read a tree selected a set of branches and if a command is right doing a another command. This command may be a selected of a branch that correspond to the response of if, or with the calculation presented in the code. Something is wrong of the code that I’m not sure:

```auto
void enKin()
{
//defined the stack of the histogram and himself
gROOT->cd();
  THStack *hs =
    new THStack("energy",
                "Kinetic Energy;EKin;Counts");
  int nbins = 200;
  TH1F *h = new TH1F("h", "h", nbins, 0., 0.0455);
  TFile *f = TFile::Open("Example2ExSpher_tree_1000.root");
    if ((!f) || f->IsZombie()) { delete f; continue; } // just a precaution
    f->ls();
    TTree *t; f->GetObject("Example2ExSpher", t);
    if (!t) { delete f; continue; } // just a precaution
    gROOT->cd();
    string Step_FinalLogicalVolume;
    bool vol = t->SetBranchAddress("Step_FinalLogicalVolume", &Step_FinalLogicalVolume);
    //Double_t Ekin;
    if (vol == "control"){
    	t->Project("h", // each file re-fills it from scratch
               	   "(pow(Step_FinalMomX,2)+pow(Step_FinalMomY,2)+pow(Step_FinalMomZ,2))/(2*0.511)"); //this is the way by calculated, the order way will be substituted the expression by Step_FinalKineticEnergy.
    } else {cout << "Deu algo errado, verique por favor!!!!"}

    h->SetLineColor(kred); 
    h->SetTitle();
        
  hs->Draw("HIST L");

  delete f;

}

```

I’m attached the file root here [https://drive.google.com/drive/folders/17ihA4mhQ2vFbmn\_h8bROrGsktEFP\_QON?usp=sharing](https://drive.google.com/drive/folders/17ihA4mhQ2vFbmn_h8bROrGsktEFP_QON?usp=sharing)

Thank you all!

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [February 19, 2019, 11:43am UTC](https://root-forum.cern.ch/t/th1f-read-and-made-histogram/32801/2 "2019-02-19T11:43:41Z")

</div>

HI,  
I cannot access the ROOT file to reproduce your problem. But looking at the code I don’t understand these two lines:

```auto
bool vol = t->SetBranchAddress("Step_FinalLogicalVolume", &Step_FinalLogicalVolume);
    //Double_t Ekin;
    if (vol == "control"){
    	t->Project("h", ....
}  

```

The return value of SetBranchAddress is an integer, see  
[https://root.cern.ch/doc/v614/classTTree.html#a1a48bf75621868a514741b27252cad96](https://root.cern.ch/doc/v614/classTTree.html#a1a48bf75621868a514741b27252cad96)

and it is not correct to compare to a string

Lorenzo

---

<div class="post-metadata">

**Author:** ![David\_Fernandes](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/david_fernandes/32/19363_2.png) [@David\_Fernandes](https://root-forum.cern.ch/u/David_Fernandes)\
**Post date:** [February 19, 2019, 12:06pm UTC](https://root-forum.cern.ch/t/th1f-read-and-made-histogram/32801/3 "2019-02-19T12:06:19Z")

</div>

Thank you @moneta. I’m already share with everyone that has access to the link the root file. But concern with this problem the idea is that I want to do a verification if the in “Step\_FinalLogicalVolume” the string “control” receive any count, so if this occurs I used this to do the fill of the histogram, using the data of other branch with this condition but I can’t get that.

Thanks again!

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [February 19, 2019, 12:31pm UTC](https://root-forum.cern.ch/t/th1f-read-and-made-histogram/32801/4 "2019-02-19T12:31:26Z")

</div>

Well, if you try `t->Print();`, you will see that you have `Step_FinalLogicalVolume : vector<string>` (i.e. a “vector”).  
The easiest thing could be to try:

```auto
t->Project("h", // each file re-fills it from scratch
           "(pow(Step_FinalMomX,2)+pow(Step_FinalMomY,2)+pow(Step_FinalMomZ,2))/(2*0.511)", //this is the way by calculated, the order way will be substituted the expression by Step_FinalKineticEnergy.
           "Step_FinalLogicalVolume == \"control\""); // selection

```

---

<div class="post-metadata">

**Author:** ![David\_Fernandes](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/david_fernandes/32/19363_2.png) [@David\_Fernandes](https://root-forum.cern.ch/u/David_Fernandes)\
**Post date:** [February 20, 2019, 11:24am UTC](https://root-forum.cern.ch/t/th1f-read-and-made-histogram/32801/5 "2019-02-20T11:24:43Z")

</div>

Thanks @Wile_E_Coyote!

I appreciate the help!

I reproduce what you ask me to with the code:

```auto
void enKin1()
{
//defined the stack of the histogram and himself
gROOT->cd();
  THStack *hs =
    new THStack("energy",
                "Kinetic Energy;EKin;Counts");
  int nbins = 200;
  TH1F *h = new TH1F("h", "h", nbins, 0., 0.0455);
  TFile *f = TFile::Open("Example4ExSpher_tree_1000.root");
    if ((!f) || f->IsZombie()) { delete f; continue; } // just a precaution
    f->ls();
    TTree *t; f->GetObject("Example4ExSpher", t);
    if (!t) { delete f; continue; } // just a precaution
    gROOT->cd();
    //string Step_FinalLogicalVolume;
    //bool vol = t->SetBranchAddress("Step_FinalLogicalVolume", &Step_FinalLogicalVolume);
    //Double_t Ekin;
    //if (vol == "control"){
    	t->Project("h", // each file re-fills it from scratch
           "(pow(Step_FinalMomX,2)+pow(Step_FinalMomY,2)+pow(Step_FinalMomZ,2))/(2*0.511)", 
           "Step_FinalLogicalVolume == \"control\""); // selection; //this is the way by calculated, the order way will be substituted the expression by Step_FinalKineticEnergy.
    //} else {cout << "Deu algo errado, verique por favor!!!!"}

            
  hs->Draw("HIST L");

  delete f;

}

```

But the result was a histogram empty. I attached the histogram. I think that there was some error in how I proceed or maybe some code was missing (perhaps a function Fill, to fill the histogram?).

Best regards

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [February 20, 2019, 11:28am UTC](https://root-forum.cern.ch/t/th1f-read-and-made-histogram/32801/6 "2019-02-20T11:28:10Z")

</div>

And where do you add “h” to “hs”?

BTW. Note that, each time you execute `t->Project("h", ...);`, the “h” will first be reset. I guess you really want something like this (for each file / tree):

```auto
// ...
TH1F *h = new TH1F("h", "h", nbins, 0., 0.0455);
t->Project("h", ...);
h->SetDirectory(0);
hs->Add(h);
// ...

```

---

<div class="post-metadata">

**Author:** ![David\_Fernandes](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/david_fernandes/32/19363_2.png) [@David\_Fernandes](https://root-forum.cern.ch/u/David_Fernandes)\
**Post date:** [February 20, 2019, 12:30pm UTC](https://root-forum.cern.ch/t/th1f-read-and-made-histogram/32801/7 "2019-02-20T12:30:27Z")

</div>

Thanks @Wile_E_Coyote, it’s work fine!!!

Just one thing, for actived the lengend that appears the entries and other stuff how could I proceed, because in this case, the graphic seems ok but without legend

Best regards

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [February 20, 2019, 12:38pm UTC](https://root-forum.cern.ch/t/th1f-read-and-made-histogram/32801/8 "2019-02-20T12:38:24Z")

</div>

That’s maybe something for @couet  
In the meantime, you could try:

```auto
gStyle->SetOptStat(); // just a precaution
((TH1*)(hs->GetStack()->Last()))->Draw("HIST L"); // the sum
hs->Draw("SAME HIST L");

```

or:

```auto
gStyle->SetOptStat(); // just a precaution
TH1 *hl = ((TH1*)(hs->GetStack()->Last())); // the sum
hl->SetNameTitle("MyName", "my title;my X axis;my Y axis");
hl->Draw("HIST L");
hs->Draw("SAME HIST L");

```

where, instead of:

```auto
hl->SetNameTitle("MyName", "my title;my X axis;my Y axis");

```

you could use:

```auto
hl->SetNameTitle(hs->GetName(), hs->GetTitle());
hl->SetXTitle(hs->GetXaxis()->GetTitle());
hl->SetYTitle(hs->GetYaxis()->GetTitle());

```

---

<div class="post-metadata">

**Author:** ![David\_Fernandes](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/david_fernandes/32/19363_2.png) [@David\_Fernandes](https://root-forum.cern.ch/u/David_Fernandes)\
**Post date:** [February 20, 2019, 2:42pm UTC](https://root-forum.cern.ch/t/th1f-read-and-made-histogram/32801/9 "2019-02-20T14:42:39Z")

</div>

It’s nice the result @Wile_E_Coyote!

Just one thing, now the title of the legend and the title of the histogram appears like ‘h’, could I change this? And don’t appears like defined in the `THStack`, could I with this code return with what is defined before?

Best regards!

---

<div class="post-metadata">

**Author:** ![David\_Fernandes](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/david_fernandes/32/19363_2.png) [@David\_Fernandes](https://root-forum.cern.ch/u/David_Fernandes)\
**Post date:** [February 22, 2019, 5:30pm UTC](https://root-forum.cern.ch/t/th1f-read-and-made-histogram/32801/11 "2019-02-22T17:30:04Z")

</div>

Ok thanks @Wile_E_Coyote it’s now fine!

Another doubt:

I have this code to do :

```auto
//loop for index of the region of control
void teste()
{
//defined the stack of the histogram and himself
gROOT->cd();
  THStack *hs =
    new THStack("energy",
                "Kinetic Energy;EKin;Counts");
  int nbins = 400;

  TFile *f = TFile::Open("Example4ExSpher_tree_1000.root");
  if ((!f) || f->IsZombie()) { delete f; continue; } // just a precaution
  f->ls();
  TTree *t; f->GetObject("Example4ExSpher", t);
  if (!t) { delete f; continue; } // just a precaution
  t->Print();

/// histogram
gROOT->cd();
  TH1F *h = new TH1F("h", "h", nbins, 0., 6);
  
//loop for index of the region of control
vector<int> *Step_StepNumber; 
//vector<double> *Step_FinalKineticEnergy;
vector<string> *Step_FinalLogicalVolume;
int n = t->GetEntries();

for (int i=0; i<n;i++){
	while(Step_StepNumber->GetEntry(i) && Step_FinalLogicalVolume == "control"){
  	t->Project("h", // each file re-fills it from scratch
                  "Step_FinalKineticEnergy"); 
          // "Step_FinalLogicalVolume == \"control\"");
                  /*"Step_FinalKineticEnergy",//); 
                   "Step_FinalLogicalVolume == \"control\" & Step_FinalKineticEnergy>0.00001"); 
     	          //"Step_FinalLogicalVolume == \"sphere\""); */
                 //"Step_FinalLogicalVolume == \"spher1\""); 
  	h->SetDirectory(0);
  	hs->Add(h);
//Legend 
  	TH1 *hl = ((TH1*)(hs->GetStack()->Last())); // the sum
  	hl->SetNameTitle("energy", "TrackLength in sphere;EKin;Counts");
  	hl->Draw("HIST C");
  	hs->Draw("SAME HIST C");

  /*gStyle->SetOptStat(); // just a pecaution
  (TH1*)(hs->GetStack()->Last())->Draw("HIST L"); // the sum
  
  hs->Draw("SAME HIST L");
  //hs->Draw("hist L");
  //Tlengend */
	i++;
}
  delete f;
}

}

```

With this code I want to selected the entry that correspond to the first step when the Step\_FinalLogicalVolume is in “control” and plot the histogram to the first step only but I missed something in the code!

What I missed?

Thanks

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [February 25, 2019, 10:50am UTC](https://root-forum.cern.ch/t/th1f-read-and-made-histogram/32801/12 "2019-02-25T10:50:25Z")

</div>

> [@Header files for Trees](https://root-forum.cern.ch/t/header-files-for-trees/16308/2):
>
> “Most recent” documentation about how to generate and use an “analysis skeleton”: [TTree::MakeClass](http://root.cern.ch/root/html534/TTree.html#TTree:MakeClass) (see also [“ROOT User’s Guide” → “Trees” → “Using TTree::MakeClass”](http://root.cern.ch/root/html534/guides/users-guide/Trees.html#using-ttreemakeclass)) [TTree::MakeSelector](http://root.cern.ch/root/html534/TTree.html#TTree:MakeSelector) (see also [“ROOT Primer” → “File I/O and Parallel Analysis” → “N-tuples in ROOT” → “For the advanced user: Processing trees with a selector script”](http://root.cern.ch/root/htmldoc/guides/primer/ROOTPrimer.html#for-the-advanced-user-processing-trees-with-a-selector-script) and [“ROOT User’s Guide” → “Trees” → “Using TTree::MakeSelector”](http://root.cern.ch/root/html534/guides/users-guide/Trees.html#using-ttreemakeselector) and [“ROOT User’s Guide” → “Example Analysis”](http://root.cern.ch/root/html534/guides/users-guide/ExampleAnalysis.html)) [TTree::MakeProxy](http://root.cern.ch/root/html534/TTree.html#TTree:MakeProxy)[ROOT::RDataFrame](https://root.cern.ch/doc/master/classROOT_1_1RDataFrame.html) (uses the [T…](https://root.cern.ch/doc/master/classTTreeReader.html)

---

<div class="post-metadata">

**Author:** ![David\_Fernandes](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/david_fernandes/32/19363_2.png) [@David\_Fernandes](https://root-forum.cern.ch/u/David_Fernandes)\
**Post date:** [February 26, 2019, 9:53am UTC](https://root-forum.cern.ch/t/th1f-read-and-made-histogram/32801/13 "2019-02-26T09:53:05Z")

</div>

Thanks, @Wile_E_Coyote!

I look at this analysis skeleton and I have one question: I understand by the user guide how can I must do the read branch, but I can´t understand how I must determine the size of the branch read of the tree (Example4), can you help me in this?

Best Regards!

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [February 26, 2019, 10:48am UTC](https://root-forum.cern.ch/t/th1f-read-and-made-histogram/32801/14 "2019-02-26T10:48:57Z")

</div>

What is “Example4”?

If you have a branch with a “std::vector\<something\>” then the actual size could be “[your\_vector\_pointer-\>size()](http://www.cplusplus.com/reference/vector/vector/)” (for each tree entry).

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [March 12, 2019, 10:49am UTC](https://root-forum.cern.ch/t/th1f-read-and-made-histogram/32801/15 "2019-03-12T10:49:02Z")

</div>

Closed on March 12, 2019, 10:49am UTC

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
