Plot of standard deviation for each bin in a 2D histogram

Dear Experts,

I have a 2D histogram “DeltaTVsChannel_RR” in the root file (attached )TBCResolution_100K_sample1_5K_correct.zip (2.3 MB). Now, I want to plot just the standard deviation for each bins(i.e SD for each channels in this case). Is there a way I could do this? It would be of great help if some body could suggest me a way to do this.

thanks,
Bibek

Hi Bibek,

from your post it’s not clear what would be the final result: a TH2 or a TH1? I would loop over all bins, ask for the bin error, save it in a container or directly fill it into the final histogram: what about that?

Best,
D

Try:

{
  TFile *f = TFile::Open("TBCResolution_100K_sample1_5K_correct.root");
  if ((!f) || f->IsZombie()) { delete f; return; } // file is not usable
  TH2F *h; f->GetObject("DeltaTVSChannel_RR", h);
  if (!h) { delete f; return; } // no histogram found
  
#if 1 /* 0 or 1 */
  TH2F *he = new TH2F(*h);
#else /* 0 or 1 */
  TH1 *he = ((TH1*)(h->Clone("he")));
#endif /* 0 or 1 */
  he->SetNameTitle(TString::Format("%s_ERRORS", h->GetName()),
                   TString::Format("%s (ERRORS)", h->GetTitle()));
  he->Reset("M");
#if ROOT_VERSION_CODE >= ROOT_VERSION(6,00,00)
  Int_t n = h->GetNcells(); // any TH in ROOT 6
#else
  Int_t n = ((h->GetNbinsX() + 2) * (h->GetNbinsY() + 2)); // a TH2 in ROOT 5
#endif
  for(Int_t i = 0; i < n; i++)
    if (h->GetBinError(i) != 0.) he->SetBinContent(i, h->GetBinError(i));
  
  TCanvas *c = new TCanvas("c", "c");
  c->Divide(1, 2);
  c->cd(1);
  h->Draw("colz");
  c->cd(2);
  he->Draw("colz");
  c->cd(0);
}

Hi, Wile,

Thank you for the code . I ran the code and got the plot as attached

In the second plot I wanted to have only the standard deviation in DeltaT, instead of DeltaT itself, as the Y axis. I wonder What changes I need to make in the code to get that. Can I have your guidance on this.

Regards,
-Bibek

Hi, dpiparo,

Thank You for the reply. This is what I wanted to do. I wanted the final result to be a TH2.

Regards,
Bibek

Try:

{
  TFile *f = TFile::Open("TBCResolution_100K_sample1_5K_correct.root");
  if ((!f) || f->IsZombie()) { delete f; return; } // file is not usable
  TH2F *h; f->GetObject("DeltaTVSChannel_RR", h);
  if (!h) { delete f; return; } // no histogram found
  
#if 1 /* 0 or 1 */
  TProfile *hp = h->ProfileX("_pfx", 1, -1, ""); // "" or "s" or "i" or "g"
#else /* 0 or 1 */
  TProfile *hp = h->ProfileX();
#endif /* 0 or 1 */
  hp->SetTitle(TString::Format("%s (Profile X)", h->GetTitle()));
  
  // note: we assume that "h" and "hp" are "fix bin sizes along X" histograms
  TH1D *he = new TH1D("he", "he", hp->GetNbinsX(),
                      hp->GetXaxis()->GetXmin(), hp->GetXaxis()->GetXmax());
  he->SetNameTitle(TString::Format("%s_ERRORS", hp->GetName()),
                   TString::Format("%s (ERRORS)", hp->GetTitle()));
  he->GetXaxis()->SetTitle(hp->GetXaxis()->GetTitle());
#if ROOT_VERSION_CODE >= ROOT_VERSION(6,00,00)
  Int_t n = hp->GetNcells(); // any TH in ROOT 6
#else
  Int_t n = (hp->GetNbinsX() + 2); // a TH1 in ROOT 5
#endif
  for(Int_t i = 0; i < n; i++)
    if (hp->GetBinError(i) != 0.) he->SetBinContent(i, hp->GetBinError(i));
  
  TCanvas *c = new TCanvas("c", "c");
  c->Divide(1, 3);
  c->cd(1);
  h->Draw();
  c->cd(2);
  hp->Draw();
  c->cd(3);
  he->Draw("HIST *P");
  c->cd(0);
}

Hi,

Thank you again for the code. What I understood was: error of the profile histogram are by default set as the standard error. However, I wanted to have Standard deviation displayed on the third plot. Is there a way that the error bar displayed on the profile histogram are the standard deviation rather than the standard error? Also, in order to display this Standard deviation as the function of channel number as a 2D histogram, What kind of changes are necessary in the code?

Regards,
Bibek

Try with:

  TProfile *hp = h->ProfileX("_pfx", 1, -1, "s"); // "" or "s" or "i" or "g"

Thanks, Attached is the plot I got after

TProfile *hp = h->ProfileX("_pfx", 1, -1, “s”);

Is there a way I can display the 3rd histogram as a 2D histogram?

This is a 1D histogram so you can try:

he->Draw("HIST P"); // "HIST *P" or "HIST P"

Ok thank you, can this relation between the standard deviation and channel number which we got as a 1D histogram be also displayed as a 2D histogram?

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