TProfile above a certain threshold

I am using a root file which consists of histograms.
What I want to do is to find the average of those histograms.
To do that I am creating a 2D histogram with all the 1D histograms on top of the other.
Then I am using TProfile to get the average of the histogram like the following image shows

What I would like to do is “force” the average to be inside the white region of the 2D histogram. In order to do that I would need to compute the average ignoring the smaller z-values.

Is this possible?

Thank you very much in advance!

My code is the following

[code]#include “Riostream.h”
#include
#include
#include “TH1F.h”
#include “TH2F.h”
#include “TProfile.h”

TH2F* mean_Gamma_Flash() {
gStyle->SetPalette(52); //Use Grayscale palette–51-59

//Load File.
TFile *f = new TFile(“FIMG5.root”);

//Loop over each histogram and store pointer in vector.
std::vector<TH1F*> hists;
for (int i=0;i<305;i++) {
TH1F hIndv = (TH1F)f->Get(Form(“FIMG5_EV_%d;1”,i));
if (hIndv)
hists.push_back(hIndv);
}

//Compute average
TH2F *hAvg = Average(hists);
TProfile *hProf = hAvg->ProfileX(“hProf”);//Average histogram

//Turn off stats
hAvg->SetStats(kFALSE);

//Draw the 2D hist with the average prof on top.
hProf->SetLineColor(kRed);
hProf->SetMarkerColor(kRed);
//hAvg->Draw(“COLZ”);
//hProf->Draw(“SAME”);

//Use a log scale in Z
gPad->SetLogz();
gPad->Update();

TString histfilename = "mean_gamma.txt";
SingleExportAscii(hProf,histfilename);

return hAvg;
}

TH2F Average(std::vector<TH1F> hists) {
TH2F *hAvg = new TH2F(“hAvg”,“Avg”,8000,0,8000,300,0,300);
for (size_t i=0;i < hists.size(); i++) {
for (int bin = 0; bin < hists[i]->GetNbinsX(); bin++) {
int resultBin = hAvg->FindBin(bin, hists[i]->GetBinContent(bin));
int binContent = hAvg->GetBinContent(resultBin) + 1;
hAvg->SetBinContent(resultBin,binContent);
}
}
return hAvg;
}[/code]

Hi,
When making a Profile from a 2D histogram all the bins are considered by default.
You can select a range of bins in X and Y by passing the lower/higher bin numbers.

I think you want instead to select only bins with a certain threshold in z (the bin content). This is not possible,
but you can easily create a temporary 2D histogram from the original where the original bins with content lower (or upper) a given threshold are set to zero in the new histogram.

Best Regards

Lorenzo

Thank you very much for your post.

I wrote the following method which seems to work fine, but there might be a more efficient way to do it, therefore please let me know

TH2F* GetThreshold(TH2F* hist2D, int threshold) { TH2F* hThres = hist2D->Clone(); for (int xBin = 1; xBin <= hist2D->GetNbinsX(); ++xBin) { for (int yBin = 1; yBin <= hist2D->GetNbinsY(); ++yBin) { if (hist2D->GetBinContent(xBin,yBin) < threshold) hThres->SetBinContent(xBin,yBin,0); } } return hThres; }

I am creating the new histogram using

Hi,

The way you are doing seems fine to me. You might want to set a different names on the returned histogram.

If you really want to be faster you could also do either one of these two things:

  • copy the histogram using the copy-constructor instead of Clone: TH2F* hThres = new TH2F(*hist2D). This might be a little faster

  • create first an empty histogram with the same axis and do the reverse, set the contents for the bins above a threshold. This should be the fastest solution, but if you don’t need to apply the function on thousands of histograms, you will not notice any difference

Best Regards

Lorenzo

I don’t understand why, but it will make more sense probably!

I already did! Why the copy-constructor is faster than cloning?

I will in fact apply this to around 4000 histograms, so maybe it makes sense to change the code. The thing is that I don’t understand what do you mean “create a histogram with the same x-axis”. And what is the reverse?
I mean I cannot understand your approach. :frowning:

Cloning goes via the I/O of ROOT so it does a bit more than simply copying the content of th histogram. This could make it slower.

Now you are reforming to loops on the histogram bins:

  1. to copy the content
  2. to set to zero the values below a threshold

What you could do is

  1. Create an empty histograms with same axis without copying the content. This is faster
  2. Copy the content for the bins only above a threshold

Lorenzo

This is what I am doing, isn’t it?
I am filling a new 2D histogram, only if the the value is above a certain threshold.
I think I am not following…

From the code you have published:

if (hist2D->GetBinContent(xBin,yBin) < threshold) hThres->SetBinContent(xBin,yBin,0);

you are doing the opposite. Set a zero value if below threshold.

L.

Oh, now I see…
You are saying to do something like

Is this what you are suggesting?

Yes and create first an empty histogram instead of copying/cloning it.

The thing is that I don’t know the dimensions of the histogram, that’s why i am copying/cloning