Convert TH1D to TH1F in PyROOT

Dear experts,

I declared a TH2F histogram and then I take the y-axis projection which is TH1D histogram. I like to fit the distribution using a macro that takes input as TH1F. I looked around a bit, and can’t figure out as how to convert TH1D to TH1F in PyROOT?

#!/usr/bin/env python
import os, sys, math, array
from ROOT import gROOT, TFile, gStyle, gDirectory, TTree, TCanvas, TH1F, TH2F, TH1D, TProfile, TStopwatch, TGaxis
gROOT.ProcessLine('.L ./fitting.C')
from ROOT import fitting

histo = {'chL1VsIntlumi': TH2F("chL1VsIntlumi", "charge in layer1 vs Int. lumi", 50, 0.0, 50.0, 70, 0.0, 120.),
         'chL2VsIntlumi': TH2F("chL2VsIntlumi", "charge in layer2 vs Int. lumi", 50, 0.0, 50.0, 70, 0.0, 120.),
         'chL3VsIntlumi': TH2F("chL3VsIntlumi", "charge in layer3 vs Int. lumi", 50, 0.0, 50.0, 70, 0.0, 120.),
         'chL4VsIntlumi': TH2F("chL4VsIntlumi", "charge in layer4 vs Int. lumi", 50, 0.0, 50.0, 70, 0.0, 120.)
        }
...
#Fill the histo

H = {}
#Plot
c = TCanvas("c","")
for h in histo:
    H[h]  = histo[h].Clone()
    y_proj = H[h].ProjectionY()
    y_proj.SetName(H[h].GetName()+"_projY")
    y_proj.Draw("same, hist")
    h_fit =  fitting(y_proj) # TypeError: TF1* ::fitting(TH1F* hist) => could not convert argument 1
    y_proj_h = TH1F()
    # y_proj_h.Copy(y_proj) # this gives an empty histogram
    # y_proj_h.Clone(y_proj) # this says, could not convert argument 1 (expected string or Unicode object, TH1D found)

I can rewrite the file fitting.C such as it takes TH1D histogram as input, but just wondering if there is a genuine solution to the problem.

Thanks,
Sadia


ROOT Version: 6.06/08:
Platform: OS 10.11.6
compiler:
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 7.3.0 (clang-703.0.31)
Target: x86_64-apple-darwin15.6.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin


1 Like

Hi @skhalil,

The copy operation should work fine, modulo the precision loss:

So you should do:

y_proj.Copy(y_proj_h)

Note that the order of the two histograms in the call is the inverse as what you had: you were copying the empty one into the filled one.

Cheers,

Enric

1 Like

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