TH1F - read and made histogram


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:

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

Thank you all!

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

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

and it is not correct to compare to a string

Lorenzo

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!

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:

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

Thanks @Wile_E_Coyote!

I appreciate the help!

I reproduce what you ask me to with the code:

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

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):

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

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

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

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

or:

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:

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

you could use:

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

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!

Ok thanks @Wile_E_Coyote it’s now fine!

Another doubt:

I have this code to do :

//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

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!

What is “Example4”?

If you have a branch with a “std::vector<something>” then the actual size could be “your_vector_pointer->size()” (for each tree entry).

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