Problems with merging multiple histos using THStack

Thank you,I believe in you ! Haha

I guess you should make your histograms (h1, …, h13) and the “InitializeHistos” function global or static (and create / initialize these histograms only once).

So in principle it should work (I cannot tell for sure, since I don’t have access to the data files), but since your code is quite messy, this may lead to errors like for example badly placed curly braces… So could you try with this:

void Analyzer::Loop(TString Folder)
{
// This is the loop skeleton where:
// jentry is the global entry number in the chain
// ientry is the entry number in the current Tree
// Note that the argument to GetEntry must be:
// jentry for TChain::GetEntry
// ientry for TTree::GetEntry and TBranch::GetEntry
//
// To read only selected branches, Insert statements like:
// METHOD1:
// fChain->SetBranchStatus("*",0); // disable all branches
// fChain->SetBranchStatus("branchname",1); // activate branchname
//
//METHOD2: replace line
// fChain->GetEntry(jentry); //read all branches
// by
// b_branchname->GetEntry(ientry); //read only this branch

   if (fChain == 0) return;
   double weight = 1;
   double _k_factor = 1;
   double _lumi = 35.8;
   TFile *f = (TFile *)gROOT->GetListOfFiles()->FindObject("Moriond_2017/" + Folder + "/ZZ4lAnalysis.root");
   hCounters = (TH1F *)f->Get("ZZTree/Counters");
   double gen_sum_weights = (Long64_t)hCounters->GetBinContent(40);
   Long64_t nentries = fChain->GetEntriesFast();
   Long64_t nbytes = 0, nb = 0;
   for (Long64_t jentry = 0; jentry < nentries; jentry++) { //Loop po eventovima
      Long64_t ientry = LoadTree(jentry);
      if (ientry < 0) break;
      nb = fChain->GetEntry(jentry);
      nbytes += nb;
      // Ovdje analiziramo event i punimo ga u histograme
      // cout << "Event broj: " << EventNumber << "\n ZZMass = " << ZZMass << endl;
      if (Folder == "ZZTo4l") {
         _k_factor = KFactor_EW_qqZZ * KFactor_QCD_qqZZ_M;
      } else if (Folder == "ggTo2e2mu") {
         _k_factor = KFactor_QCD_ggZZ_Nominal;
      } else if (Folder == "ggTo2e2tau") {
         _k_factor = KFactor_QCD_ggZZ_Nominal;
      } else if (Folder == "ggTo2mu2tau") {
         _k_factor = KFactor_QCD_ggZZ_Nominal;
      } else if (Folder == "ggTo4e") {
         _k_factor = KFactor_QCD_ggZZ_Nominal;
      } else if (Folder == "ggTo4mu") {
         _k_factor = KFactor_QCD_ggZZ_Nominal;
      } else if (Folder == "ggTo4tau") {
         _k_factor = KFactor_QCD_ggZZ_Nominal;
      }

      cout << _k_factor << endl;
      if (Folder == "Data") {
         weight = 1;
      } else {
         weight = (_lumi * 1000 * xsec * _k_factor * overallEventWeight) / gen_sum_weights;
      }
      if (Folder == "ZZTo4l") {
         h1->Fill(ZZMass, weight);
      } else if (Folder == "ggTo4e") {
         h2->Fill(ZZMass, weight);
      } else if (Folder == "ggTo2mu2tau") {
         h3->Fill(ZZMass, weight);
      } else if (Folder == "VBFH125") {
         h4->Fill(ZZMass, weight);
      } else if (Folder == "ggH125") {
         h5->Fill(ZZMass, weight);
      } else if (Folder == "WminusH125") {
         h6->Fill(ZZMass, weight);
      } else if (Folder == "ggTo2e2mu") {
         h7->Fill(ZZMass, weight);
      } else if (Folder == "ggTo4mu") {
         h8->Fill(ZZMass, weight);
      } else if (Folder == "WplusH125") {
         h9->Fill(ZZMass, weight);
      } else if (Folder == "ggTo2e2tau") {
         h10->Fill(ZZMass, weight);
      } else if (Folder == "ggTo4tau") {
         h11->Fill(ZZMass, weight);
      } else if (Folder == "ttH125") {
         h12->Fill(ZZMass, weight);
      } else if (Folder == "ZH125") {
         h13->Fill(ZZMass, weight);
      }

//      H_Mass->Fill(ZZMass,weight);
   } // Kraj loop-a po eventovima
}

And if it still doesn’t work, you can debug it (or put some printf() in the code to understand what you’re reading/filling…)

Thanks on all your hard work,but this thing is killing me!
Errors are always similar and the same thing is repeating:

Error in TFormula::Compile: Bad numerical expression : "ZZMass"
Error in TF1::TF1: function: f1/ZZMass has 0 parameters instead of 1
Error in TH1F::FillRandom: Unknown function: f1
Error in TH1F::FillRandom: Unknown function: f1
Error in TH1F::FillRandom: Unknown function: f1
Warning in TFile::Append: Replacing existing TH1: h1 (Potential memory leak).
Error in TH1F::FillRandom: Unknown function: f1
Warning in TFile::Append: Replacing existing TH1: h1 (Potential memory leak).
Error in TH1F::FillRandom: Unknown function: f1
Warning in TFile::Append: Replacing existing TH1: h1 (Potential memory leak).
Error in TH1F::FillRandom: Unknown function: f1
Warning in TFile::Append: Replacing existing TH1: h1 (Potential memory leak).
Error in TH1F::FillRandom: Unknown function: f1
Warning in TFile::Append: Replacing existing TH1: h1 (Potential memory leak).
Error in TH1F::FillRandom: Unknown function: f1
Warning in TFile::Append: Replacing existing TH1: h1 (Potential memory leak).
Error in TH1F::FillRandom: Unknown function: f1
Warning in TFile::Append: Replacing existing TH1: h1 (Potential memory leak).
Error in TH1F::FillRandom: Unknown function: f1
Warning in TFile::Append: Replacing existing TH1: h1 (Potential memory leak).
Error in TH1F::FillRandom: Unknown function: f1
Warning in TFile::Append: Replacing existing TH1: h1 (Potential memory leak).
Error in TH1F::FillRandom: Unknown function: f1
Warning in TFile::Append: Replacing existing TH1: h1 (Potential memory leak).
Error in TH1F::FillRandom: Unknown function: f1

Is problem maybe in Filling with parameters (this part)?
//I’m using weight for filling,is this even a good thing to do?
auto f1 = new TF1(“f1”, “weight”, 70.0, 370.0);
f1->SetParameters(10.0, 220.0, 25.0);

auto h1= new TH1F(“h1”,“Histogram usporedbe”,300,70,370);
h1->FillRandom(“f1”,20000);
hs= new THStack(“hs”,"");

    h1= new TH1F("h1","Histogram usporedbe",300,70,370);
    h1->FillRandom("f1",5000);
    h1->SetFillColor(kRed);
    hs->Add(h1);

//… and so on…

Remove all the FillRandom("f1",20000); calls

Still same problem persists,nothing changes.Only last histogram shown when printed as canvas

OK, then, as I said, since I cannot run your code without the actual data, you should debug your code yourself (add some printf() to see what you actually do) to try to understand what’s happening…

Analyzer.InitializeHistos.cxx (2.6 KB)

You did it!Thanks alot ,both of you ,for your efforts.

Okay,so I’m now stuck at another part,adding “Data” histogram to this whole thing…Mind helping me again?:slight_smile:
run.cpp (989 Bytes)
Analyzer.C (8.2 KB)
Analyzer.h (121.0 KB)

I have to add “Data” to my final hist,drawn with Draw("SAME p E1 X0”) and with option SetBinErrorOption(TH1::kPoisson) ,however I recieve those warnings and I really wonder why:

Analyzer.C:259:16: warning: missing terminating " character
Analyzer.C:259: error: missing terminating " character
.Analyzer.C: In member function ‘virtual void Analyzer::Loop(TString)’:
Analyzer.C:85: error: a function-definition is not allowed here before ‘{’ token
Analyzer.C:264: error: expected ‘}’ at end of input
make: *** [Analyzer.o] Error 1

Analyzer.InitializeHistos.cxx (3.2 KB)

Added the new code,it’s still warning about this line mostly:
DataHist->Draw("SAME p E1 X0”);

Analyzer.C:283:16: warning: missing terminating " character
Analyzer.C:283: error: missing terminating " character
.Analyzer.C: In member function ‘virtual void Analyzer::DrawHistos(TString)’:
Analyzer.C:284: error: expected ‘)’ before ‘;’ token
make: *** [Analyzer.o] Error 1

As the error message says, replace:
DataHist->Draw("SAME p E1 X0”);
by
DataHist->Draw("SAME p E1 X0");

Sorry but what are the differences between those two?

Look carefully at the last double-quotes character ( vs ")

Loool id never figure that out on my own,thanks alot again,it seems that its working now

So the whole project is coming at the end and I have nobody to thank but you guys.
One last thing is left undone and it’s histo-print I’m getting.

First picture is print I’m getting,and second is how-it-should-look-like.
For some reason,in code I have a command for printing ErrorBars,but they aren’t shown in the graph as you can see.So I tried printing Data hist alone,but I get crashes.If you find anything that can cause this,please contact me!
Thank you in advance.
run.cpp (989 Bytes)
Analyzer.C (8.7 KB)
Analyzer.h (121.0 KB)

What kind of crashes?

All I do to make it print only “Data” is comment all those lines,commented below:

TCanvas *canv = new TCanvas("canv","Canvas za masu",900,600);
TLegend *legend = new TLegend(0.9,0.7,0.48,0.9);
legend->AddEntry(h1,"ZZTo4l","f");
legend->AddEntry(h2,"ggTo**","f");
legend->AddEntry(h4,"H(125)","f");
canv->cd();
DataHist->Draw();
//DataHist->Draw("SAME p E1 X0");
//hs->Draw();
//legend->Draw();
//hs->GetXaxis()->SetTitle("M_{4l} [GeV]");
//hs->GetYaxis()->SetTitle("Events/4 GeV");
canv->SaveAs("Histo_usporedbe.pdf");}

And I hoped for some print,but this time it prints blank histo(no crashes this time,I wonder if I did anything different):

It looks like the “Data” histogram has not been filled (see the number of entries is 0… )