I am trying to stack four different histograms in one canvas. Here’s my code. Please let me know where my errors are and how I can improve them.
void histo()
{
TFile* fInput = new TFile("TargetVStudy_40cm_POT1000.root", "READ");
TTree* tree = (TTree*)fInput->Get("DUNETargetSim");
Char_t pid[32];
Double_t Energy;
gStyle->SetOptStat(11111);
tree->SetBranchAddress("pid", &pid);
tree->SetBranchAddress("kineticEnergy", &Energy);
Int_t nEntries = tree->GetEntries("kineticEnergy");
THStack *histos = new THStack("histos", "");
TH1D* hist1 = new TH1D("nu_e_Hist", "Histogram Title", 1200, 0, 120);
hist1->SetMarkerStyle(20);
hist1->SetMarkerColor(kRed);
TH1D* hist2 = new TH1D("nu_mu_Hist", "Histogram Title", 1200, 0, 120);
hist2->SetMarkerStyle(21);
hist2->SetMarkerColor(kBlue);
TH1D* hist3 = new TH1D("anti_nu_e_Hist", "Histogram Title", 1200, 0, 120);
hist3->SetMarkerStyle(22);
hist3->SetMarkerColor(kGreen);
TH1D* hist4 = new TH1D("anti_nu_mu_Hist", "Histogram Title", 1200, 0, 120);
hist4->SetMarkerStyle(23);
hist4->SetMarkerColor(kBlack);
histos->Add(hist1);
histos->Add(hist2);
histos->Add(hist3);
histos->Add(hist4);
for (Int_t i = 0; i < nEntries; i++)
{
tree->GetEntry(i);
for (Int_t j = 0; j < 4; j++)
{
switch(j) {
case 0:
if(strcmp(pid, "nu_e") == 0) { hist1->Fill(Energy); }
break;
case 1:
if(strcmp(pid, "nu_mu") == 0) { hist2->Fill(Energy); }
break;
case 2:
if(strcmp(pid, "anti_nu_e") == 0) { hist3->Fill(Energy); }
break;
case 3:
if(strcmp(pid, "anti_nu_mu") == 0) { hist4->Fill(Energy); }
break;
default:
;
// cout << "Hey There!" << endl;
}
}
}
/*
Int_t a = hist1->GetEntries();
Int_t b = hist2->GetEntries();
Int_t c = hist3->GetEntries();
Int_t d = hist4->GetEntries();
cout << "nu_e: " << a << "; nu_mu: " << b << "; anti_nu_e: " << c << "; anti_nu_mu: " << d << endl;
*/
TCanvas* cs = new TCanvas();
histos->Draw();
}