Hi everybody!
In April/18 I post a topic here that the solution was:
{
gROOT->cd();
THStack *hs =
new THStack("energy",
"Energies 0.03509 a 0.13509MeV with Ti 0.05mm;ELost/EKin;Counts");
TH1F *h = new TH1F("h", "h", 100, 0.2, 1.8);
if (h->GetSumw2N() == 0) h->Sumw2(kTRUE);
const char *files[] = { "Energy_350f.root",
"Energy_360f.root",
"Energy_370f.root",
"Energy_450f.root",
"Energy_550f.root",
"Energy_850f.root",
"Energy_1350f.root" };
const int n = sizeof(files) / sizeof(char*);
int color = 0;
for (int i = 0; i < n; i++) {
TFile *f = TFile::Open(files[i]);
if ((!f) || f->IsZombie()) { delete f; continue; } // just a precaution
TTree *t; f->GetObject("GmDataTTree", t);
if (!t) { delete f; continue; } // just a precaution
gROOT->cd();
t->Project("h", // each file re-fills it from scratch
"(Event_AccumulatedEnergyLost / Event_InitialKineticEnergy)");
if (i<9) {
color = i + 1;
h->SetLineColor(color);
} else {
if (i > 10 && i < 17) {
color = 11 + i;
h->SetLineColor(color);
} else {
color = 20 + i;
h->SetLineColor(color);
}
}
h->SetLineWidth(2);
h->SetTitle(files[i]);
hs->Add((TH1F*)(h->Clone(TString::Format("h_%d", i))));
delete f; // automatically deletes "t", too
}
delete h; // no longer needed
TLegend *l = new TLegend(0.75, 0.9, 0.9, 0.25);
for (int i = 0; i < hs->GetNhists(); i++)
{ l->AddEntry(hs->GetHists()->At(i), "", "l"); }
hs->Draw("NOSTACK HIST L");
l->Draw();
}
I have a doubt: If my tree (GmDataTTree) inside the file Energy_360f.root, has the same name of the file without the extension root, for example: the file Energy_360f.root has a tree named Energy_360f; the file Energy_370f.root has a tree named Energy_370f and so one, how can I chage this code to do that? I think in a for loop but I can’t know how define the variable. Could someone help me?
Thanks