Please read tips for efficient and successful posting and posting code
ROOT Version: 6.18/00
Platform: Ubuntu 20.04
Compiler: ROOT/CLANG
Dear expert,
I am trying to read a .root file and rebin the histogram.
I wanted to fill the histogram and rebin it with skipping bins and the skipped bin content(value) as the mean value.
I want to fill the new histogram hp2t2
using the new bins to be as below: Double_t bins[]={....} and set the bin value as the mean of the skipped bins whenever skipped. Also scale with the new bin width
Here is my attempted code
convert_to_text.C (1.2 KB)
and the root file:
test.root (4.4 KB).
Here is the code snippet:
void root_to_text()
{
TFile* f = new TFile("test.root","READ");
TH1D* h = (TH1D*)f->Get("hprt");
h->Sumw2();
Double_t bins[] = {0. , 0.0025, 0.005 , 0.0075, 0.010 , 0.015, 0.02 , 0.025, 0.03 , 0.035, 0.04 , 0.045, 0.05 , 0.055
, 0.06 , 0.07 , 0.08 , 0.09 , 0.1 , 0.11 , 0.120, 0.130, 0.140, 0.150};
Int_t binN = sizeof(bins)/sizeof(Double_t) - 1; // # of Bins
TH1D* hp2t2 = new TH1D("hp2t2","",binN,bins);
Int_t nbin=h->GetNbinsX();
for (Int_t i = 1; i<=nbin; i++)
{
Double_t xmin = h->GetBinLowEdge(i);
Double_t value = h->GetBinContent(i);
Double_t error = h->GetBinError(i);
if (i<4){xmin = xmin ;hp2t2->Fill(xmin,value/0.0025);}
else if(i>=4 || i<=12){xmin = xmin+0.005;
hp2t2->Fill(xmin,value/0.005);}
else if(i>=4 || i<=12){xmin = xmin+0.010;
hp2t2->Fill(xmin,value/0.010);}
}
hp2t2->Draw("hist");
gPad->SetLogy(1);
}