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;
}
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.
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
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?).
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):
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
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");
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?
//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!
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?