Write THStack with weights

Hi everyone,

I’m trying to create a stack of histograms and save it in a file, but I want to stack histograms with weight.

If I fill hist using (e.g.):

histo1->Fill(x1);
histo2->Fill(x2);

And then I stack them in a THStack object, there are no problem. If i fill them using something like:

histo1->Fill(x1,weight1);
histo2->Fill(x2,weight2);

And I try to stuck them, the histo are not colored. I found similar problem, and you solved using:

hist_stack->Draw("hist");

And I see that it works. What if I want to save it in a file? I tried to use Write(“hist”), but it doesn’t work and I cannot find another way… Can you help me?

Hello,

Can you provide a minimal reproducer of the problem?

G Ganis

I have a vector of signal, that I’ll call:

vector<TH1F*> hist_signal;

I have two kind of backgrounds, for semplicity:

TH1F *back1;
TH1F *back2;

I wrote several long routine in which i Fill this histograms with the method above:

back1->Fill(x1,weight)

And the same for the others, and the signal.
Now I want to write a function that creates a vector of stack, and write that vector on a file. I’m using something like:

vector<THStack*> hist_stack;
TFile out_stack("stack_vector.root","recreate");        

    for(unsigned int i=0;i<hist_stack->size();i++) {
        out_stack.cd();
        back1->SetFillColor(kBlue);  
        hist_stack->at(i)->Add(back1);
        back2->SetFillColor(kRed);    
        hist_stack->at(i)->Add(back2);
        hist_signal.at(i)->SetFillColor(kGreen);    
        hist_stack->at(i)->Add(hist_signal.at(i));
        hist_stack->at(i)->Write();
    }
    out_stack.Close();

The results is a non-colored stack in which I can’t recognize signal from background. If I don’t use method Fill(x,weight), but only Fill(x) I obtain the usual colored stack.
I found that if I want to see the stack with color (using Fill(x,w)) I have to use Draw(“hist”), but I want to Write on file, because this is a long vector.
Until now I create a canvas with Draw and save it in a file, but it’s not a very usefull solution. There is a way to save on file colored THStack filled with Fill(x,w)?
Thank you

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