Dear List,
I want to invert a 2D histogram by normalising each bin content to the maximum value across the distribution, and then subracting from 1. The histogram itself was generated using data from a function defined in a TF2 constructor.
I’ve extracted the maximum value across the bins, but I’m having some trouble normalising and inverting. Is there an elegant solution for this?
See sample script and plot below.
Many thanks in advance!
#include "TMath.h"
#include "TF1.h"
#include "TF2.h"
gROOT->Reset();
gStyle->SetOptStat(0);
gStyle->SetPalette(1);
//
void DQANew1()
{
//Define some sample 2D gaussian function
//
TCanvas *c1 = new TCanvas("c1","the fit canvas",500,400);
TF2 *f2 = new TF2("f2","sqrt((exp(-0.5*((x-[0])^2/([1]^2))))^2/[2]^2+(exp(-0.5*((y-[3])^2/([4]^2))))^2/[5]^2)",-3,3,-4,4);
Double_t params[] = {0,0.5,1,0.5,1.5,1};
//
//Set Paramaters
f2->SetParameters(params);
//
//Test 2D function
f2->Draw();
//
//:::::::::::Now plot function as 2D histogram
TCanvas *c2 = new TCanvas("c2","the fit canvas",500,400);
TH2F *h2 = new TH2F("h2","",20,-3.,3.,20,-4.,4.);
h2->SetFillColor(46);
h2->FillRandom("f2",4000000);
h2->Draw("surf1z");
//
// Extract maxmimum value
printf("Get Maximum bin value = %g\n",h2->GetMaximum());
Double_t MaxBinVal = h2->GetMaximum();
//
//check that MaxBinVal has been captured
printf("MaxBinVal = %g\n",h2->GetMaximum());
//
//::::::::::Now normalise and subtract from 1 to invert h2
TCanvas *c3 = new TCanvas("c3","the fit canvas",500,400);
TF2 *f3 = new TF2("f3","1-((sqrt((exp(-0.5*((x-[0])^2/([1]^2))))^2/[2]^2+(exp(-0.5*((y-[3])^2/([4]^2))))^2/[5]^2))/MaxBinVal)",-3,3,-4,4);
Double_t params1[] = {0,0.5,1,0.5,1.5,1};
f3->SetParameters(params1);
TH2F *h3 = new TH2F("h3","",20,-3.,3.,20,-4.,4.);
h3->FillRandom("f3",4000000);
h3->Draw("surf1z");
}
Plot that I would like to invert