Multiple histograms not plotted when fitted

Hi

I have a macro which opens a number of files which contain THStacks saved
containing TH1Ds for different data samples. I want to plot the same histogram from each dataset on the same axes with a fit.

My code works perfectly (3 hists on one canvas) but when I put in the actual line calling the fit I only get one histogram in my canvas at a time - they are not superimposed anymore.

I don’t understand why fitting the function should change the drawing behaviour of my macro?

Any pointers would be greatly appreciated!

Thanks,

Mark

Code and version info:

Root version v4-00-04, gcc3.2, RH8.0

Bool_t GI_DAU     = kTRUE;
Bool_t GI_AUAU200 = kTRUE;
Bool_t GI_AUAU62  = kFALSE;
Bool_t GI_PP      = kTRUE;
TString G_BASE_PATH = "/auto/rnc2/horner";
const Int_t G_NHISTS = 1;
Int_t G_HISTS[G_NHISTS] = { 23 };
TString G_STACK = "assocptstack2";

void MJNHSWCompSpec()
{
  TFile *fdau,*fauau200,*fauau62,*fpp;
  if (GI_DAU)     fdau = new TFile(G_BASE_PATH + TString("/SWdAu/data/FullHistStacks.root")); 
  if (GI_AUAU200) fauau200 = new TFile(G_BASE_PATH + TString("/SW200CentralFF/data/FullHistStacks.root")); 
  if (GI_AUAU62)  fauau62 = new TFile(G_BASE_PATH + TString("/SW62.4/data/FullHistStacks.root")); 
  if (GI_PP)      fpp = new TFile(G_BASE_PATH + TString("/SWpp/data/FullHistStacks.root")); 
  TString opt = "";
  
  for (Int_t i=0;i<G_NHISTS;i++)
    {
      TCanvas* c = new TCanvas("c","Canvas",400,400);
      TLegend* legend = new TLegend(0.8,0.75,0.97,0.97);
      TH1D* temp;
      if (GI_DAU && fdau)
        {          
          temp = ((TH1D*)((TList*)((THStack*)fdau->Get(G_STACK))->GetHists())->At(G_HISTS[i]));
          temp->SetLineColor(kRed);
          SetHistStuff(temp,opt);
          temp->Draw(opt);
          opt="same";
          legend->AddEntry(temp,"dAu","l");
        }
      if (GI_AUAU200 && fauau200)
        {
          temp = ((TH1D*)((TList*)((THStack*)fauau200->Get(G_STACK))->GetHists())->At(G_HISTS[i]));
          temp->SetLineColor(kBlue);
          SetHistStuff(temp,opt);
          temp->Draw(opt);
          opt="same";
          legend->AddEntry(temp,"AuAu 200","l");
        }
      if (GI_AUAU62 && fauau62)
        {
          temp = ((TH1D*)((TList*)((THStack*)fauau62->Get(G_STACK))->GetHists())->At(G_HISTS[i]));
          temp->SetLineColor(kBlack);
          SetHistStuff(temp,opt);
          temp->Draw(opt);
          opt="same";
          legend->AddEntry(temp,"AuAu 62","l");
        }
      if (GI_PP && fpp)
        {
          temp = ((TH1D*)((TList*)((THStack*)fpp->Get(G_STACK))->GetHists())->At(G_HISTS[i]));
          temp->SetLineColor(kGreen);
          SetHistStuff(temp,opt);
          temp->Draw(opt);
          opt="same";
          legend->AddEntry(temp,"pp","l");
        }
      legend->Draw();
      c->SaveAs(TString("test")+i+TString(".gif"));
      delete c;
    }
}

void SetHistStuff(TH1D* temp, TString opt)
{
  TF1* formula = new TF1("formula","[1]-[0]*TMath::Cos(x) ",1.,temp->GetXaxis()->GetXmax());
  temp->SetTitle(legendname(temp->GetName()));
  temp->SetAxisRange(-1.0,1.0);
  temp->Scale(2.0/temp->GetMaximum());
  temp->SetAxisRange(-1.0,2.0*TMath::Pi()-1.0);

  // ********* Uncomment the next line and it stops
  // ********* working

  //  temp->Fit("formula","R");

}

TString legendname(TString name)
{
  if (name.Contains("AssocBin0")) leg = "300-800MeV/c";
  if (name.Contains("AssocBin1")) leg = "400-900MeV/c";
  if (name.Contains("AssocBin2")) leg = "500-1000MeV/c";
  if (name.Contains("AssocBin3")) leg = "600-1100MeV/c";
  if (name.Contains("AssocBin4")) leg = "700-1200MeV/c";
  if (name.Contains("AssocBin5")) leg = "800-1300MeV/c";
  if (name.Contains("AssocBin6")) leg = "900-1400MeV/c";
  if (name.Contains("AssocBin7")) leg = "1000-1500MeV/c";
  if (name.Contains("AssocBin8")) leg = "1100-1600MeV/c";
  if (name.Contains("AssocBin9")) leg = "1200-1700MeV/c";
  if (name.Contains("AssocBin10")) leg = "1300-1800MeV/c";
  if (name.Contains("AssocBin11")) leg = "1400-1900MeV/c";
  if (name.Contains("AssocBin12")) leg = "1500-2000MeV/c";
  if (name.Contains("AssocBin13")) leg = "500-1500MeV/c";
  if (name.Contains("AssocBin14")) leg = "500-1250MeV/c";
  if (name.Contains("AssocBin15")) leg = "700-2000MeV/c";
  if (name.Contains("AssocBin16")) leg = "100-400MeV/c";
  if (name.Contains("AssocBin17")) leg = "200-500MeV/c";

  if (name.Contains("AssocBin18")) leg = "300-400MeV/c";
  if (name.Contains("AssocBin19")) leg = "400-500MeV/c";
  if (name.Contains("AssocBin20")) leg = "500-600MeV/c";
  if (name.Contains("AssocBin21")) leg = "600-700MeV/c";
  if (name.Contains("AssocBin22")) leg = "700-800MeV/c";
  if (name.Contains("AssocBin23")) leg = "800-900MeV/c";
  if (name.Contains("AssocBin24")) leg = "900-1000MeV/c";
  if (name.Contains("AssocBin25")) leg = "1000-1100MeV/c";
  if (name.Contains("AssocBin26")) leg = "1100-1200MeV/c";
  if (name.Contains("AssocBin27")) leg = "1200-1300MeV/c";
  if (name.Contains("AssocBin28")) leg = "1300-1400MeV/c";
  if (name.Contains("AssocBin29")) leg = "1400-1500MeV/c";
  if (name.Contains("AssocBin30")) leg = "1400-1600MeV/c";
  if (name.Contains("AssocBin31")) leg = "150-4000MeV/c";
  if (name.Contains("AssocBin32")) leg = "1000-2000MeV/c";
  return leg;

}

Hi

I made a much simpler macro to reproduce the problem and I tried it
with root version v4-02-00.

I made 3 directories each with a file with one histogram in it. All hists
have the same name. If you uncomment the Fit line you will only
get one hist on the Canvas - otherwise three.

I presume it has something to do with the names of objects or something like that but I don’ t know what.

Can anyone provide me with a pointer to the right direction?

Thanks,

Mark

[code]
void test()
{

TFile* f1 = new TFile(“dir1/Hist.root”);
TFile* f2 = new TFile(“dir2/Hist.root”);
TFile* f3 = new TFile(“dir3/Hist.root”);

TCanvas* c = new TCanvas(“c”,“Canvas”,400,400);

TH1F* temp;
temp=(TH1F*)f1.Get(“hist”);
FitF(temp);
temp->DrawCopy();

temp=(TH1F*)f2.Get(“hist”);
FitF(temp);
temp->DrawCopy(“same”);

temp=(TH1F*)f3.Get(“hist”);
FitF(temp);
temp->DrawCopy(“same”);

}

void FitF(TH1F* temp)
{
TF1* formula = new TF1(“formula”,"[0]-[1]*cos(x)",-3,3);
// temp->Fit(formula,“R”);

}[/code]

Can you send a small working example reproducing the error ? (attached, not in the body of the post)

Hi

I have attached a little .tar.gz with my macro in it with
the directory strucutre - I used root v4-02-00.

Thanks,

Mark
test.tar.gz (5.31 KB)

Hi Mark,

Here is how your macro should be:

void test()
{
   TFile* f1 = new TFile("dir1/Hist.root");
   TFile* f2 = new TFile("dir2/Hist.root");
   TFile* f3 = new TFile("dir3/Hist.root");

   TCanvas* c = new TCanvas("c","Canvas",600,600);
   TH1F* temp;

   temp=(TH1F*)f3->Get("hist");
   FitF(temp, ""); 

   temp=(TH1F*)f2->Get("hist");
   FitF(temp, "SAME");

   temp=(TH1F*)f1->Get("hist");
   FitF(temp, "SAME");
}

void FitF(TH1F* temp, Option_t *option)
{
   TF1* formula = new TF1("formula","[0]-[1]*cos(x)",-3,3);
   temp->Fit(formula,"R",option);
}