THStack with "nostack" and "c" options

Hi everyone,

I’ve been using THStacks without problems before, however I’ve found a behaviour that is confusing me. I want to plot a THStack with the “nostack” option but also the “c” option; that is, the visual result I want is several superimposed lines (not data point markers), and the histograms not added together.

So I tried:

but that didn’t work. I got the “nostack” option, but I have data markers and no lines.

If however I try the following:

I get what the command asks for; that is, summed histograms with a smooth line per histogram. In other words, the “c” option appears to work with “same” but not “nostack” for a THStack.

I’ve had a couple of people look at my code and there doesn’t appear to be anything I am doing to cause this behaviour. I’m afraid I’ve not got a very minimal example, but the code can be found here. I compile and run it with:

g++ -m32 -I$ROOTSYS/include `root-config --cflags --glibs` NpAll_unstrict_all.C -o npall_unstrict_all; ./npall_unstrict_all

Of course if this does turn out to be some stupid error of mine I apologise, but if not my questions are:

  • is this behaviour known?
  • is this a feature or a bug?
  • either way, can I override this behaviour?

As for a solution to my visual problem, I believe I can draw the individual histograms with the “same” option to get the desired effect which I will do. However, that is going to be somewhat inconvenient as it involves changing a lot of code.

Yours gratefully,

Michael

void stack5() {
   TH1F *hgaus1 = new TH1F("Hgaus1", "", 100, -10, 10);
   TH1F *hgaus2 = new TH1F("Hgaus2", "", 100, -20, 20);
   THStack *hst = new THStack();
   hgaus1->FillRandom("gaus", 30000); hst->Add(hgaus1, "C");
   hgaus2->FillRandom("gaus", 30000); hst->Add(hgaus2, "C");
   hst->Draw("nostack");
   hst->GetHistogram()->SetLabelSize(0.07, "xy");
}

Thank you for your reply.

When I try your example, it works as intended. However, when I make what appears to be the crucial changes to my own code i.e. analogous to:

hst->Add(hgaus2, "C"); hst->Draw("nostack");

I am still not getting the “c” effect.

I’m trying to make as minimal an example as possible.

Ok, you must be doing something else as my minimal example is working for you.

I’ve reduced my code to a minimum and I still can’t figure out what on Earth I am doing wrong. As you can see, producing a plot with the “hist c” option works, but “nostack c” does not.

Code and plots can be found here. For convenience here’s the code, followed by the two plots that it makes:

[code]#include <TH1D.h>
#include <TH2F.h>
#include <TFile.h>
#include <TString.h>
#include <TCanvas.h>
#include <TROOT.h>
#include <THStack.h>
#include <TKey.h>

int main(void){

TH2F* h2_0 = NULL;
TH2F* h2_temp;

TFile* f0;

h2_temp = NULL;
f0 = TFile::Open(“Np0.root”);
f0->cd(“WJESAnalysis”);
h2_temp = (TH2F*)gDirectory->Get(“twosubs_mass_unstrict_all”);
if ( h2_temp != NULL ) h2_0 = (TH2F*)h2_temp->Clone(“h2_copy_of_everything”);

TString numberPart;
THStack hs_projections(“hs_projections”,“Dijet mass spectra of first two subjets, per p_{T} cut value”);

for ( int i = (h2_0->GetNbinsX()-1); i > 1; i=i-6){
numberPart.Form("%d",i);
TH1D* h1_temp = h2_0->ProjectionY(numberPart,i,i+6);
h1_temp->Rebin(10);
hs_projections.Add( h1_temp, “c” );
}

TCanvas *c1 = new TCanvas();
hs_projections.Draw(“nostack c”);
c1->Update();
c1->Print(“NpAll_nostack_c_log.pdf”);

hs_projections.Draw(“hist c”);
c1->Update();
c1->Print(“NpAll_hist_c_log.pdf”);

return 0;
}
[/code]
NpAll_nostack_c_log.pdf (14.3 KB)
NpAll_hist_c_log.pdf (19 KB)

  1. Replace:
    numberPart.Form("%d",i);
    with:
    numberPart.Form(“h2_0_py_%d”, i);
  2. Replace:
    hs_projections.Add( h1_temp, “c” );
    with:
    hs_projections.Add(h1_temp, “HIST C”);
  3. Replace:
    hs_projections.Draw(“nostack c”);
    with:
    hs_projections.Draw(“NOSTACK”);
  4. Replace:
    hs_projections.Draw(“hist c”);
    with:
    hs_projections.Draw(); // () or ("") or (“C”)
  5. Enjoy!
    BTW. Don’t call your subroutine “main” unless it’s the really the “main” of a standalone application.

Thank you! I especially enjoy point 5.

Frankly I don’t understand why this works and my other things don’t, but it does, and I now have another magic incantation to add to the list.

Thanks again!

I’ve just realized that a simpler / more elegant remedy exists …

  1. Replace:
    numberPart.Form("%d",i);
    with:
    numberPart.Form(“h2_0_py_%d”, i);
  2. Replace:
    hs_projections.Draw(“nostack c”);
    with:
    hs_projections.Draw(“HIST NOSTACK”);
  3. Enjoy!