THStack question

Hi,

I have a simple question about a stack of histograms:
If I have a THStack object plotted with an x-axis range of 1-3 GeV/c, how would I replot it so that the x-axis ranges from 4-6 GeV/c (i.e. translate the stack of histograms)?

Thanks

Example:

{
   THStack *hs = new THStack("hs","test stacked histograms");
   TH1F *h1 = new TH1F("h1","test hstack",100,-4,4);
   h1->FillRandom("gaus",20000);
   h1->SetFillColor(kRed);
   h1->SetMarkerStyle(21);
   h1->SetMarkerColor(kRed);
   hs->Add(h1);
   TH1F *h2 = new TH1F("h2","test hstack",100,-4,4);
   h2->FillRandom("gaus",15000);
   h2->SetFillColor(kBlue);
   h2->SetMarkerStyle(21);
   h2->SetMarkerColor(kBlue);
   hs->Add(h2);
   TH1F *h3 = new TH1F("h3","test hstack",100,-4,4);
   h3->FillRandom("gaus",10000);
   h3->SetFillColor(kGreen);
   h3->SetMarkerStyle(21);
   h3->SetMarkerColor(kGreen);
   hs->Add(h3);
                                                                                
   hs->Draw();
   hs->GetXaxis()->SetLimits(-10,10);
}

Hi,

Thanks for the example code, but maybe I wasn’t very clear in explaining what I wanted to do (or I probably didn’t understand the solution that you gave me) - I need to translate a set of histograms plotted as a THStack object. So, I would like to replot my original stack with a new X-axis value x’=x+3. This way, the original data points at 1GeV/c would now be at 4 GeV/c.
For the code you gave me, it would be analogous to having the plots shifted so that the gaussian peaks shift from 0 to 3. A method to do this for each histogram (just a simple translation of the plot along the X-axis) would also be very useful.

Thanks so much for your help.

I didn’t find any method already available in TAxis or in TH1 (may be I missed something), but it is quite easy to make one. The attached little example does the job.
translate.C (747 Bytes)


I couldn’t find any direct way of doing this either, but the macro works perfectly - thanks so much!

Note it can be improved. In particular, being lazy, I put a fixed name for the translated histogram. You can improve it by building a new name using the name (GetName()) of the original histogram as basis.

:slight_smile: No problem, this is part of a much bigger macro, and I did modify the code to meet my specifications. The translation problem is now solved, and that was my main issue.

Thanks for all the help.