Hi,
I’m using pyroot, with root v5.34/01, python v2.7.3 and gcc v4.7.2,
I’m trying to use a custom function for weights in the selection field of TTree.Draw(). My custom function is defined as a C-style root macro:
#include <TH1F.h>
TH1F *h = 0;
double myfunction(double val = 0)
{
if (!h)
return 0.;
return h->GetBinContent(h->FindBin(val));
}
In the main program (below) I load the macro and use it in TTree.Draw(). This works fine (output reweighted.png looks fine), but (as far as I can judge) at the closing of my main function the program segfaults. My main concern is making this run cleanly, but maybe you can give me other suggestions to improve the method.
a) Why am I getting a segmentation fault and how do I solve it. Output is attached, but I don’t understand where the error is coming from.
b) Is it possible to define my custom function in python, e.g. “def myfunction(): …” syntax, in the main file? If so, how do I load it such that it is known to TTree.Draw().
Main (test) program:
[code]#!/usr/bin/env python
import ROOT
def main():
f0 = ROOT.TFile(“extra.root”,“read”)
f1 = ROOT.TFile(“data.root”,“read”)
ROOT.gROOT.ProcessLineSync('.x myfunction.C+')
f0.cd()
h = f0.Get("inputdir/inputhist;1")
h.SetName("h")
ROOT.gDirectory.cd("inputdir")
ROOT.gROOT.ProcessLineSync('TH1F *h = (TH1F*)gDirectory->Get("h")')
canvas = ROOT.TCanvas("c","c")
canvas.cd()
t1 = f1.Get("mytree")
t1.Draw("var","","",10000)
t1.Draw("var","(myfunction(var))*1.0","same",10000)
canvas.SaveAs("reweighted.png")
canvas.Close()
f0.Close()
f1.Close()
if name == “main”:
main()
[/code]
Thanks,
Sara
out.txt (6.13 KB)